diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index 8f67476..c721481 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -5382,6 +5382,28 @@ const DSH_PLAN_MODE_RE = /(?:^|\r?\n)The user switched this session (?:to plan|b // DeepSeek Harness sends a runtime-context snapshot as its own user message; // a gateway that merges user messages into one string could lose text after it. const DSH_RUNTIME_SNAPSHOT_RE = /^Current runtime context(?:\. This snapshot supersedes earlier runtime-context snapshots\.(?:\r?\n[\s\S]*)?|: none\. Earlier runtime-context snapshots no longer apply\.)$/ +// tmux-context's location reading, a finished background job's notice and the +// note that the model changed: user-role text DSH adds on its own too. +const DSH_TMUX_RE = /(?:^|\r?\n)tmux location \(turn \d+\):\r?\nsession [^\r\n]*\r?\nwindow active=[^\r\n]*/g +const DSH_JOB_RE = /(?:^|\r?\n)background job \S+ \([^\r\n]*\) finished [^\r\n]*/g +const DSH_MODEL_NOTE_RE = /(?:^|\r?\n)\[model changed: assistant turns above this point were generated by [^\r\n]*\]/g +const DSH_NOTE_RES = [DSH_TIME_CONTEXT_RE, DSH_REPEAT_TOOL_RE, DSH_PLAN_MODE_RE, DSH_TMUX_RE, DSH_JOB_RE, DSH_MODEL_NOTE_RE] + +// DSH's own notes in a user text, apart from what the user wrote. Beside a +// live tool_result they are context for the model, never the user steering +// (seven_7cx on X: every step's clock note dropped the live session). +function takeDshNotes(text) { + if (DSH_RUNTIME_SNAPSHOT_RE.test(text.trim())) return { notes: [text.trim()], rest: "" } + const notes = [] + let rest = text + for (const re of DSH_NOTE_RES) { + rest = rest.replace(re, (match) => { + notes.push(match.trim()) + return "" + }) + } + return { notes, rest } +} function isMetaOnlyUserText(text) { const rest = stripMagpiePlaceholderNotice(text) @@ -5391,6 +5413,9 @@ function isMetaOnlyUserText(text) { .replace(DSH_TIME_CONTEXT_RE, "") .replace(DSH_REPEAT_TOOL_RE, "") .replace(DSH_PLAN_MODE_RE, "") + .replace(DSH_TMUX_RE, "") + .replace(DSH_JOB_RE, "") + .replace(DSH_MODEL_NOTE_RE, "") .replace(/\[Request interrupted by user(?: for tool use)?\]/g, "") .replace(/\[(?:SYSTEM NOTIFICATION|MESSAGE FROM NON-USER SOURCE) - NOT USER INPUT\]/g, "") .trim() @@ -5840,7 +5865,14 @@ function inspectToolTurn(results, body) { out = appendResultPart(out, resultIndex, parseUserImage(block)) continue } - const rawText = blockText(block) + let rawText = blockText(block) + if (message.role === "user") { + const dsh = takeDshNotes(rawText) + if (dsh.notes.length) { + notices.push(...dsh.notes) + rawText = dsh.rest + } + } if (!seenResult && message.role === "user" && rawText.trim()) { // A user can steer a pending tool turn from either side of a tool_result. // Replay the instruction as a user prompt, never as an MCP tool response. diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 289bab5..88aeb30 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.98", + "version": "0.1.99", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/repeat-batch.mjs b/packages/agy-cli/test/repeat-batch.mjs index 2ab818d..75c060d 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -1694,6 +1694,59 @@ test("live tool-turn drop logs drop_live and carries exact reason into session_n assert.equal(sessionNews.at(-1)?.reason, "history_mismatch") }) +// seven_7cx on X (DSH + agy-cli): a multi-step tool task dropped its live +// session at nearly every step (drop_live "interrupted"), so the cache read +// fell to 0 and the whole history was replayed. DSH adds user-role notes of +// its own before a step (time-context's clock, tmux-context's reading, a +// finished job's notice, a model-change note); none is the user steering. +const DSH_STEP_NOTES = [ + "Time sampled while preparing turn 3, step 2: 2026-11-18T10:24:33+08:00[Asia/Shanghai]\nBrowser time zone for this request: Asia/Shanghai. Interpret otherwise-unqualified dates and times in this zone.\nElapsed since the preceding step context: 12s.", + "Time sampled while preparing turn 3, step 3: 2026-11-18T10:24:45Z", + "Current runtime context. This snapshot supersedes earlier runtime-context snapshots.\nsession: main", + "Current runtime context: none. Earlier runtime-context snapshots no longer apply.", + "You are repeating the exact same tool call with identical arguments. Carefully analyze the previous result before calling again: if the task is not complete, try a different approach or different arguments instead of repeating the call.", + "The user switched this session to plan mode.", + "tmux location (turn 3):\nsession dev, window 1 \"main\", pane 0 %4\nwindow active=1, pane active=1, layout b25d,200x50,0,0,2", + "background job job_7 (bash: npm test) finished exit 0. Read its output with job_output.", + "[model changed: assistant turns above this point were generated by deepseek-v4; the session continues with agy-cli/gemini]", +] + +for (const shape of ["same message", "own message"]) { + test(`a multi-step tool turn with DSH's own notes (${shape}) keeps its live session`, async () => { + const stderr = captureStderr() + try { + await withKit({}, async (kit) => { + kit.defaultPrompt = async (agent) => { + for (let step = 0; step < DSH_STEP_NOTES.length; step++) await agent.tool("Read", { path: `/repo/${step}.txt` }) + await agent.text("done") + return { stopReason: "end_turn" } + } + const meta = { user_id: `dsh-live-${shape}` } + let body = baseBody({ metadata: meta, messages: [{ role: "user", content: "TOOL read everything" }] }) + let parsed = await readSse(await kit.request(body)) + for (const note of DSH_STEP_NOTES) { + assert.equal(parsed.stopReason, "tool_use", JSON.stringify(parsed)) + const call = parsed.content.find((block) => block.type === "tool_use") + const result = { type: "tool_result", tool_use_id: call.id, content: `ok ${call.id}` } + body = baseBody({ metadata: meta, messages: [ + ...body.messages, + { role: "assistant", content: [call] }, + ...(shape === "same message" + ? [{ role: "user", content: [result, { type: "text", text: note }] }] + : [{ role: "user", content: [result] }, { role: "user", content: [{ type: "text", text: note }] }]), + ] }) + parsed = await readSse(await kit.request(body)) + } + assert.equal(parsed.stopReason, "end_turn", JSON.stringify(parsed)) + assert.equal(sessionsOpened(kit), 1, "one live session for the whole turn") + }) + } finally { + stderr.restore() + } + assert.deepEqual(stderr.lines.filter((row) => row.action === "drop_live"), []) + }) +} + test("A16: oversized history[0] cannot evict the newest history reply before ", async () => { await withKit({ env: { AGY_BRIDGE_MAX_PROMPT_BYTES: String(16 * 1024) } }, async (kit) => { scriptedAgent(kit)