fix(todo-continuation): normalize agent name to config key before promptAsync (#3149)
The todo-continuation-enforcer was passing raw agent names (which could be display names like 'Sisyphus (Ultraworker)') to promptAsync. These names contain spaces/parentheses that violate HTTP header specs, causing the x-opencode-agent-name header validation to fail with 'unknown error' toast. Added normalizeAgentForPromptKey() that converts display names to config keys (e.g., 'Sisyphus (Ultraworker)' -> 'sisyphus') before API calls. TDD: Added regression test that verifies config key is sent to promptAsync.
This commit is contained in:
@@ -5,6 +5,44 @@ import { injectContinuation } from "./continuation-injection"
|
|||||||
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||||
|
|
||||||
describe("injectContinuation", () => {
|
describe("injectContinuation", () => {
|
||||||
|
test("normalizes built-in display names to config keys before promptAsync", async () => {
|
||||||
|
// given
|
||||||
|
let capturedAgent: string | undefined
|
||||||
|
const ctx = {
|
||||||
|
directory: "/tmp/test",
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
todo: async () => ({ data: [{ id: "1", content: "todo", status: "pending", priority: "high" }] }),
|
||||||
|
promptAsync: async (input: {
|
||||||
|
body: {
|
||||||
|
agent?: string
|
||||||
|
}
|
||||||
|
}) => {
|
||||||
|
capturedAgent = input.body.agent
|
||||||
|
return {}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const sessionStateStore = {
|
||||||
|
getExistingState: () => ({ inFlight: false, lastInjectedAt: 0, consecutiveFailures: 0 }),
|
||||||
|
}
|
||||||
|
|
||||||
|
// when
|
||||||
|
await injectContinuation({
|
||||||
|
ctx: ctx as never,
|
||||||
|
sessionID: "ses_display_name_agent",
|
||||||
|
resolvedInfo: {
|
||||||
|
agent: "Sisyphus (Ultraworker)",
|
||||||
|
model: { providerID: "anthropic", modelID: "claude-sonnet-4-20250514" },
|
||||||
|
},
|
||||||
|
sessionStateStore: sessionStateStore as never,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(capturedAgent).toBe("sisyphus")
|
||||||
|
})
|
||||||
|
|
||||||
test("inherits tools from resolved message info when reinjecting", async () => {
|
test("inherits tools from resolved message info when reinjecting", async () => {
|
||||||
// given
|
// given
|
||||||
let capturedTools: Record<string, boolean> | undefined
|
let capturedTools: Record<string, boolean> | undefined
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ import {
|
|||||||
} from "../../features/hook-message-injector"
|
} from "../../features/hook-message-injector"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
import { getAgentConfigKey } from "../../shared/agent-display-names"
|
import {
|
||||||
|
getAgentConfigKey,
|
||||||
|
normalizeAgentForPromptKey,
|
||||||
|
} from "../../shared/agent-display-names"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CONTINUATION_PROMPT,
|
CONTINUATION_PROMPT,
|
||||||
@@ -122,12 +125,14 @@ export async function injectContinuation(args: {
|
|||||||
tools = tools ?? previousMessage?.tools
|
tools = tools ?? previousMessage?.tools
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentName && skipAgents.some(s => getAgentConfigKey(s) === getAgentConfigKey(agentName))) {
|
const promptAgent = normalizeAgentForPromptKey(agentName)
|
||||||
|
|
||||||
|
if (promptAgent && skipAgents.some(s => getAgentConfigKey(s) === getAgentConfigKey(promptAgent))) {
|
||||||
log(`[${HOOK_NAME}] Skipped: agent in skipAgents list`, { sessionID, agent: agentName })
|
log(`[${HOOK_NAME}] Skipped: agent in skipAgents list`, { sessionID, agent: agentName })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!agentName) {
|
if (!promptAgent) {
|
||||||
const compactionState = sessionStateStore.getExistingState(sessionID)
|
const compactionState = sessionStateStore.getExistingState(sessionID)
|
||||||
if (compactionState && isCompactionGuardActive(compactionState, Date.now())) {
|
if (compactionState && isCompactionGuardActive(compactionState, Date.now())) {
|
||||||
log(`[${HOOK_NAME}] Skipped: agent unknown after compaction`, { sessionID })
|
log(`[${HOOK_NAME}] Skipped: agent unknown after compaction`, { sessionID })
|
||||||
@@ -162,7 +167,7 @@ ${todoList}`
|
|||||||
try {
|
try {
|
||||||
log(`[${HOOK_NAME}] Injecting continuation`, {
|
log(`[${HOOK_NAME}] Injecting continuation`, {
|
||||||
sessionID,
|
sessionID,
|
||||||
agent: agentName,
|
agent: promptAgent,
|
||||||
model,
|
model,
|
||||||
incompleteCount: freshIncompleteCount,
|
incompleteCount: freshIncompleteCount,
|
||||||
})
|
})
|
||||||
@@ -172,7 +177,7 @@ ${todoList}`
|
|||||||
await ctx.client.session.promptAsync({
|
await ctx.client.session.promptAsync({
|
||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: {
|
body: {
|
||||||
agent: agentName,
|
agent: promptAgent,
|
||||||
...(model !== undefined ? { model } : {}),
|
...(model !== undefined ? { model } : {}),
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
parts: [createInternalAgentTextPart(prompt)],
|
parts: [createInternalAgentTextPart(prompt)],
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect } from "bun:test"
|
import { describe, it, expect } from "bun:test"
|
||||||
import { AGENT_DISPLAY_NAMES, getAgentConfigKey, getAgentDisplayName, getAgentListDisplayName, normalizeAgentForPrompt } from "./agent-display-names"
|
import { AGENT_DISPLAY_NAMES, getAgentConfigKey, getAgentDisplayName, getAgentListDisplayName, normalizeAgentForPrompt, normalizeAgentForPromptKey } from "./agent-display-names"
|
||||||
|
|
||||||
describe("getAgentDisplayName", () => {
|
describe("getAgentDisplayName", () => {
|
||||||
it("returns display name for lowercase config key (new format)", () => {
|
it("returns display name for lowercase config key (new format)", () => {
|
||||||
@@ -196,6 +196,16 @@ describe("normalizeAgentForPrompt", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("normalizeAgentForPromptKey", () => {
|
||||||
|
it("converts built-in display names to config keys", () => {
|
||||||
|
expect(normalizeAgentForPromptKey("Sisyphus (Ultraworker)")).toBe("sisyphus")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("preserves custom agents", () => {
|
||||||
|
expect(normalizeAgentForPromptKey("MyCustomAgent")).toBe("MyCustomAgent")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("AGENT_DISPLAY_NAMES", () => {
|
describe("AGENT_DISPLAY_NAMES", () => {
|
||||||
it("contains all expected agent mappings", () => {
|
it("contains all expected agent mappings", () => {
|
||||||
// given expected mappings
|
// given expected mappings
|
||||||
|
|||||||
@@ -98,3 +98,25 @@ export function normalizeAgentForPrompt(agentName: string | undefined): string |
|
|||||||
|
|
||||||
return trimmed
|
return trimmed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function normalizeAgentForPromptKey(agentName: string | undefined): string | undefined {
|
||||||
|
if (typeof agentName !== "string") {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const trimmed = stripAgentListSortPrefix(agentName.trim())
|
||||||
|
if (!trimmed) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const lower = trimmed.toLowerCase()
|
||||||
|
const reversed = REVERSE_DISPLAY_NAMES[lower]
|
||||||
|
if (reversed !== undefined) {
|
||||||
|
return reversed
|
||||||
|
}
|
||||||
|
if (AGENT_DISPLAY_NAMES[lower] !== undefined) {
|
||||||
|
return lower
|
||||||
|
}
|
||||||
|
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user