diff --git a/packages/agy-cli/index.mjs b/packages/agy-cli/index.mjs index c94d16c..be4580d 100644 --- a/packages/agy-cli/index.mjs +++ b/packages/agy-cli/index.mjs @@ -2395,6 +2395,7 @@ async function acquireProcLocked(agyPath, convKey, opts, isFreshConversation, fo const candidate = findLiveContinuedProc(opts) if (candidate && lockedKeys.has(candidate.convKey)) existing = rekeyProc(candidate, convKey) } + let why = "no_live" if (existing) { if (!existing.dead && existing.turn) { throw new BusyError("agy bridge: this conversation still has an unfinished response") @@ -2424,6 +2425,10 @@ async function acquireProcLocked(agyPath, convKey, opts, isFreshConversation, fo await existing.updateTools(opts.tools) return { proc: existing, history: matchedHistory } } + why = forceNew ? "forced" : existing.dead ? "dead" : existing.retiring ? "retiring" : + existing.workDir !== opts.workDir ? "work_dir" : + !sameInstructions(existing.instructions, opts.instructions) ? "instructions" : + !(existing.matchesHistory(matchedHistory) || continuedFresh) ? "history" : "fresh_after_first" existing.retiring = true dropFromPool(existing) await existing.stop().catch(() => {}) @@ -2442,6 +2447,12 @@ async function acquireProcLocked(agyPath, convKey, opts, isFreshConversation, fo throw error } pool.set(convKey, proc) + // Why this request got a new agy session, so a cache that never hits can be + // traced to the check that failed (#1058). Counts only, never content. + logSessionEvent(proc, why, { + fresh: !!isFreshConversation, resumed: !!proc.resumed, + parked: !!resume, history: Array.isArray(opts.history) ? opts.history.length : null, + }) return { proc, history: opts.history } } @@ -2496,6 +2507,21 @@ function logBridgeEvent(proc, turn, action, reason, meta = {}, extra = {}) { try { process.stderr.write("[agy-bridge] " + JSON.stringify(row) + "\n") } catch {} } +// One row per new agy session, on a budget of its own so it never crowds out +// the tool-state rows above. +let sessionWindowStart = 0 +let sessionWindowCount = 0 + +function logSessionEvent(proc, reason, extra) { + if (process.env.AGY_BRIDGE_EVENTS === "0") return + const now = Date.now() + if (now - sessionWindowStart >= EVENT_WINDOW_MS) { sessionWindowStart = now; sessionWindowCount = 0 } + if (sessionWindowCount++ >= EVENT_MAX_PER_WINDOW) return + const row = { event: "agy_bridge_session", host_epoch: HOST_EPOCH, ts: now, + conv: proc?.convKey ? digest(proc.convKey).slice(0, 12) : null, action: "session_new", reason, ...extra } + try { process.stderr.write("[agy-bridge] " + JSON.stringify(row) + "\n") } catch {} +} + // Per-conversation counter of consecutive unmatched tool-result requests that // hit the same turn while its lastProgressAt did not move. A different turn, // any progress, a window timeout or a reclaim restarts it from zero. diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 907da67..c4d0e93 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.29", + "version": "0.1.30", "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 e7dfd30..dc71e99 100644 --- a/packages/agy-cli/test/repeat-batch.mjs +++ b/packages/agy-cli/test/repeat-batch.mjs @@ -279,6 +279,35 @@ test("A11: AGY_BRIDGE_EVENTS=verbose reports layout_flip with a req_id and no co assert.ok(!JSON.stringify(flips).includes("hello") && !JSON.stringify(flips).includes("Opening hook")) }) +// ------------------------------------------------- #1058 session_new rows + +test("#1058: a new agy session says why the live one was not reused, with no content", async () => { + const stderr = captureStderr() + try { + await withKit({}, async (kit) => { + scriptedAgent(kit) + const first = baseBody({ metadata: { user_id: "why-new" }, system: "base system", + messages: [{ role: "user", content: "hello secret" }] }) + const one = await readSse(await kit.request(first)) + const reply = { role: "assistant", content: one.content.map((block) => ({ type: "text", text: block.text })) } + const second = { ...first, messages: [...first.messages, reply, { role: "user", content: "again" }] } + const two = await readSse(await kit.request(second)) + assert.equal(kit.acpSpawns.length, 1, "the second turn continues the live session") + const changed = { ...second, system: "a different system", + messages: [...second.messages, { role: "assistant", content: two.content.map((block) => ({ type: "text", text: block.text })) }, + { role: "user", content: "third" }] } + await readSse(await kit.request(changed)) + }) + } finally { + stderr.restore() + } + const rows = stderr.lines.filter((row) => row.action === "session_new") + assert.deepEqual(rows.map((row) => row.reason), ["no_live", "no_live"], + "a changed system prompt is a different conversation key, so it starts with no live session") + assert.notEqual(rows[0].conv, rows[1].conv) + assert.ok(!JSON.stringify(rows).includes("secret") && !JSON.stringify(rows).includes("system")) +}) + // ------------------------------------------------------- A15 pool pressure test("A15: a full pool evicts the oldest long-detached tool turn instead of answering 503, and that conversation replays later", async () => {