diff --git a/docs/architecture.md b/docs/architecture.md index 10899de..1135f2e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -203,7 +203,9 @@ status line under the prompt (recalling, then what it decided: injected, none fi did not answer, or arrived after the turn ended), `{ text, context }` for a command, `$.prompt.submit` for `/evolver-evolve`, and the turn summary as `turn.complete`'s `{ text }`. The turn summary names each injected strategy and its asset id, what the turn's last check says of its use (none ran: unverified), what EvoMap was told -(and whether the report carried evidence), and a saving only behind a passing check. Text +(and whether the report carried evidence), and a saving only behind a passing check. A recalled-for turn that +reported no strategy ends with the relay's notice of what its recall did instead: none fits, the Proxy did not +answer, the recall had not finished, or a strategy that landed after the model's last step and so went unused. Text appended in `turn.start` never reaches the turn's first request, so a recall never waits there: the strategy is appended when it lands, only while its turn is still open (and is then kept out of the session's later recalls), and booked at the turn's next main-loop `turn.step` after the first; a turn that ends first books nothing. In the diff --git a/hosts/claude-code/dist/hooks/register.ts b/hosts/claude-code/dist/hooks/register.ts index 1a7ef47..d41a4e6 100644 --- a/hosts/claude-code/dist/hooks/register.ts +++ b/hosts/claude-code/dist/hooks/register.ts @@ -24,7 +24,7 @@ const MODEL_MAX_TOKENS = 4_096 const MODEL_TIMEOUT_MS = 120_000 const MAX_PENDING_PROMPTS = 32 const UNKNOWN_ORIGIN = { kind: 'unknown' } -const RECALL_UNFINISHED_STATUS = 'evolver · recall did not finish for this prompt' +const RECALL_UNFINISHED_STATUS = '! recall did not finish for this prompt' // The status line is one per plugin, so only the turn that runs now may write it: a recall that answers once a later // turn has started describes a turn nobody is looking at. diff --git a/lib/channels/relay/server.js b/lib/channels/relay/server.js index f0f6dd3..af345e6 100644 --- a/lib/channels/relay/server.js +++ b/lib/channels/relay/server.js @@ -6,7 +6,7 @@ import { createServer } from 'node:http'; import { CHANNELS } from '../../agent/events.js'; import { profileNamed } from '../../agent/registry.js'; -import { arrivedLateStatusOf, recallStatusOf, RECALLING_STATUS } from '../../recall-status.js'; +import { arrivedLateStatusOf, recallStatusOf, RECALLING_STATUS, unreportedTurnNoticeOf } from '../../recall-status.js'; import { ownerIsGone } from '../../runtime/process/owner.js'; import { createPluginChannel } from '../plugin/adapter.js'; import { createModelQueue } from './model-queue.js'; @@ -16,6 +16,7 @@ const MODEL_POLL_MS = 20_000; const PARENT_POLL_MS = 5_000; const BODY_MAX_BYTES = 4 * 1024 * 1024; const MAX_LATE = 64; +const JOIN_WAIT_MS = 2_000; function bodyOf(request) { return new Promise((resolve, reject) => { @@ -45,11 +46,42 @@ function isAuthorized(request, token) { const reply = (response, status, value) => response.writeHead(status, { 'Content-Type': 'application/json' }).end(JSON.stringify(value ?? null)); +const turnKeyOf = (payload) => (payload?.turn_id ? JSON.stringify([payload.session_id ?? null, payload.turn_id]) : null); + +// Each recalled-for turn ends with a notice of what its recall did. A turn that reported a strategy has its summary; +// any other is told here, from the recall's trace as it stood when the turn ended and whether a model step carried +// what it injected. A turn is keyed by its session as well, and one whose strategy is being joined waits for the join's +// answer, so a join still in flight is not told as unused; one that has not answered within a short wait is told as +// still being added, since its outcome is not known yet. +function turnRecalls() { + const turns = new Map(); + return { + started(turnKey, traced) { + const turn = { trace: null, isSettled: false, isJoined: false, joining: null }; + traced.then((trace) => Object.assign(turn, { trace, isSettled: true }), () => Object.assign(turn, { isSettled: true })); + turns.set(turnKey, turn); + if (turns.size > MAX_LATE) turns.delete(turns.keys().next().value); + }, + joining(turnKey, joined) { + const turn = turns.get(turnKey); + if (turn) turn.joining = joined.then((isJoined) => { turn.isJoined ||= isJoined; }, () => {}); + }, + async ended(turnKey) { + const turn = turns.get(turnKey); + turns.delete(turnKey); + if (!turn) return null; + const joinedInTime = turn.joining && await Promise.race([turn.joining.then(() => true), new Promise((resolve) => setTimeout(resolve, JOIN_WAIT_MS, false).unref())]); + turn.isJoinPending = Boolean(turn.joining) && !joinedInTime; + return unreportedTurnNoticeOf(turn); + }, + }; +} + // A recall slower than the prompt's wait answers later: the module asks for it by id, and gets it only while its turn // is still open, then joins it to the turn once a model step carries it, which books it only while that turn is open. // A module that asks `withStatus` also gets the status line of what the recall decided, even when it brought nothing to // add; one that does not (a module older than the status line, beside a newer relay) gets only a strategy, as before. -function lateBook() { +function lateBook(turns) { const entries = new Map(); const remember = (value) => { const id = randomUUID(); @@ -58,7 +90,7 @@ function lateBook() { return id; }; return { - held: (late, traced) => remember({ late, traced }), + held: (late, traced, turnKey) => remember({ late, traced, turnKey }), async landed(id, { withStatus = false } = {}) { const entry = entries.get(id); const landed = await entry?.late; @@ -68,12 +100,14 @@ function lateBook() { const status = isTraced ? recallStatusOf(trace) : null; if (!landed) return status ? { status } : null; if (!(await landed.show())) return withStatus ? { status: arrivedLateStatusOf(trace?.name) } : null; - return { id: remember({ join: landed.join }), text: landed.text, ...(status && { status }) }; + return { id: remember({ join: landed.join, turnKey: entry.turnKey }), text: landed.text, ...(status && { status }) }; }, async joined(id) { - const join = entries.get(id)?.join; + const entry = entries.get(id); entries.delete(id); - return { joined: join ? await join() : false }; + const join = entry?.join ? entry.join() : Promise.resolve(false); + if (entry?.join) turns.joining(entry.turnKey, join); + return { joined: await join }; }, }; } @@ -87,11 +121,18 @@ function lateBook() { export function createRelay({ profile, env = process.env, token = randomBytes(24).toString('hex') }) { const models = createModelQueue(); const channel = createPluginChannel({ profile, env, overrides: { model: models.port } }); - const lates = lateBook(); + const turns = turnRecalls(); + const lates = lateBook(turns); const routes = { '/event': async ({ hostEvent, payload }) => { const { late, traced, ...answer } = await channel.handle(hostEvent, payload); - return late ? { ...answer, late: lates.held(late, traced), ...(traced && { status: RECALLING_STATUS }) } : answer; + const turnKey = turnKeyOf(payload); + if (traced && turnKey) turns.started(turnKey, traced); + if (hostEvent === 'turn.complete' && turnKey) { + const notice = await turns.ended(turnKey); + return !answer.text && notice ? { ...answer, text: notice } : answer; + } + return late ? { ...answer, late: lates.held(late, traced, turnKey), ...(traced && { status: RECALLING_STATUS }) } : answer; }, '/late': ({ id, withStatus }) => lates.landed(id, { withStatus: withStatus === true }), '/late/join': ({ id }) => lates.joined(id), diff --git a/lib/recall-status.js b/lib/recall-status.js index d886c05..e9d890d 100644 --- a/lib/recall-status.js +++ b/lib/recall-status.js @@ -2,19 +2,23 @@ // Copyright (c) 2026 EvoMap // The one line a host pins under its prompt while a turn runs: what the recall for that turn decided, including the -// recalls that injected nothing, so a quiet turn is told apart from one that never recalled. +// recalls that injected nothing, so a quiet turn is told apart from one that never recalled. The host already heads +// the line with the plugin's name, and draws it narrow and unstyled: a leading mark carries the decision at a glance, +// and the strategy's name, the part most often cut, comes last. -const NAME_MAX_CHARS = 48; -const PREFIX = 'evolver ·'; +const NAME_MAX_CHARS = 32; -const quoted = (name) => `"${String(name ?? 'EvoMap strategy').slice(0, NAME_MAX_CHARS)}"`; +const quoted = (name) => { + const text = String(name ?? 'EvoMap strategy'); + return `"${text.length > NAME_MAX_CHARS ? `${text.slice(0, NAME_MAX_CHARS - 1)}…` : text}"`; +}; -export const RECALLING_STATUS = `${PREFIX} recalling an EvoMap strategy for this prompt…`; +export const RECALLING_STATUS = '⟳ recalling an EvoMap strategy…'; function skippedStatusOf({ recalled, eligible }) { - if (!recalled) return `${PREFIX} no EvoMap strategy fits this prompt`; - const why = eligible ? 'none has enough steps to follow' : 'none is relevant enough or new to this session'; - return `${PREFIX} none injected: the Hub returned ${recalled}, ${why}`; + if (!recalled) return '○ no EvoMap strategy fits this prompt'; + const why = eligible ? 'none has enough steps' : 'none relevant or new'; + return `○ none injected · Hub returned ${recalled}, ${why}`; } /** The status line for a recall's last trace (`recallStrategy`'s `onTrace`); any other recall injected nothing. */ @@ -22,18 +26,34 @@ export function recallStatusOf(trace) { switch (trace?.outcome) { case 'injected': return trace.kind === 'recipe' - ? `${PREFIX} injected Recipe ${quoted(trace.name)} (${trace.steps} steps)` - : `${PREFIX} injected ${quoted(trace.name)} (the Hub returned ${trace.recalled})`; + ? `✓ injected Recipe ${quoted(trace.name)} · ${trace.steps} steps` + : `✓ injected ${quoted(trace.name)}`; case 'skipped': return skippedStatusOf(trace); case 'unavailable': - return `${PREFIX} recall skipped: the Evolver Proxy did not answer (/evolver-status)`; + return '! recall skipped · Evolver Proxy did not answer (/evolver-status)'; case 'short': - return `${PREFIX} prompt too short to recall for`; + return '○ prompt too short to recall for'; default: - return `${PREFIX} no EvoMap strategy injected`; + return '○ no EvoMap strategy injected'; } } /** The status line for a strategy that landed once its turn had ended, so it was never added. */ -export const arrivedLateStatusOf = (name) => `${PREFIX} ${quoted(name)} arrived after this turn ended, not injected`; +export const arrivedLateStatusOf = (name) => `○ ${quoted(name)} arrived after the turn ended, not injected`; + +/** + * The notice for a turn that reported no strategy, headed like the turn summary, so every recalled-for turn ends saying + * what its recall did: `isSettled` whether the recall had decided by then, `isJoined` whether a model step carried + * what it injected, `isJoinPending` whether that step's join had not answered yet. + */ +export function unreportedTurnNoticeOf({ trace, isSettled, isJoined, isJoinPending = false }) { + return `Evolver · ${unreportedDecisionOf({ trace, isSettled, isJoined, isJoinPending })}`; +} + +function unreportedDecisionOf({ trace, isSettled, isJoined, isJoinPending }) { + if (!isSettled) return '! recall had not finished when this turn ended, nothing injected'; + if (trace?.outcome === 'injected' && isJoinPending) return `○ ${quoted(trace.name)} was still being added when this turn ended`; + if (trace?.outcome === 'injected' && !isJoined) return `○ ${quoted(trace.name)} was recalled after the model's last step, so this turn did not use it`; + return recallStatusOf(trace); +} diff --git a/test/channels/relay.test.js b/test/channels/relay.test.js index 5ef452f..36d979d 100644 --- a/test/channels/relay.test.js +++ b/test/channels/relay.test.js @@ -40,10 +40,10 @@ test('a turn\'s recall answers later, by id; the strategy is joined to its turn const turn = { session_id: 's1', cwd: '/proj', turn_id: 't1', text: PROMPT, origin: { kind: 'composer' } }; const { body: answer } = await post('/event', { hostEvent: 'turn.start', payload: turn }); assert.equal(answer.append, undefined); - assert.equal(answer.status, 'evolver · recalling an EvoMap strategy for this prompt…'); + assert.equal(answer.status, '⟳ recalling an EvoMap strategy…'); const { body: landed } = await post('/late', { id: answer.late, withStatus: true }); assert.match(landed.text, /^\[Evolution Memory\] Fix flaky Redis connection retries/); - assert.match(landed.status, /^evolver · injected "Fix flaky Redis connection retries" \(the Hub returned \d+\)$/); + assert.match(landed.status, /^✓ injected "Fix flaky Redis connection retr…"$/); assert.deepEqual((await post('/late/join', { id: landed.id })).body, { joined: true }); assert.deepEqual((await post('/late/join', { id: landed.id })).body, { joined: false }); assert.deepEqual((await post('/event', { hostEvent: 'no.such.event', payload: turn })).body, {}); @@ -54,7 +54,7 @@ test('a recall that injects nothing still answers what it decided; a turn nobody const typed = { session_id: 's2', cwd: '/proj', turn_id: 't1', text: 'translate the release notes into Japanese please', origin: { kind: 'composer' } }; const { body: answer } = await post('/event', { hostEvent: 'turn.start', payload: typed }); const { body: late } = await post('/late', { id: answer.late, withStatus: true }); - assert.deepEqual(late, { status: 'evolver · no EvoMap strategy fits this prompt' }); + assert.deepEqual(late, { status: '○ no EvoMap strategy fits this prompt' }); const { body: older } = await post('/event', { hostEvent: 'turn.start', payload: { ...typed, turn_id: 't1b' } }); assert.equal((await post('/late', { id: older.late })).body, null, 'a module older than the status line gets only a strategy, as before'); const notification = { session_id: 's2', cwd: '/proj', turn_id: 't2', text: 'done', origin: { kind: 'task-notification' } }; @@ -86,3 +86,29 @@ test('the relay process serves only a relayed agent', () => { assert.equal(refused.status, 1); assert.match(refused.stderr, /"cursor" is not a relayed agent/); }); + +test('a recalled-for turn that reported no strategy ends with a notice of what its recall did', async (t) => { + const { post } = await started(t, { assets: [] }); + const turn = { session_id: 's3', cwd: '/proj', turn_id: 't1', text: 'translate the release notes into Japanese please', origin: { kind: 'composer' } }; + const { body: answer } = await post('/event', { hostEvent: 'turn.start', payload: turn }); + await post('/late', { id: answer.late, withStatus: true }); + const { body: ended } = await post('/event', { hostEvent: 'turn.complete', payload: { ...turn, reason: 'answer', answer: 'done' } }); + assert.equal(ended.text, 'Evolver · ○ no EvoMap strategy fits this prompt'); + const { body: again } = await post('/event', { hostEvent: 'turn.complete', payload: { ...turn, reason: 'answer', answer: 'done' } }); + assert.equal(again.text, undefined, 'a turn ends with its notice once'); +}); + +test('turns of two sessions that share a turn id each end with their own notice', async (t) => { + const { post } = await started(t, { assets: [] }); + const turnOf = (session_id, text) => ({ session_id, cwd: '/proj', turn_id: 't1', text, origin: { kind: 'composer' } }); + const first = turnOf('s4', 'translate the release notes into Japanese please'); + const second = turnOf('s5', 'hi'); + const { body: firstStart } = await post('/event', { hostEvent: 'turn.start', payload: first }); + await post('/late', { id: firstStart.late, withStatus: true }); + const { body: secondStart } = await post('/event', { hostEvent: 'turn.start', payload: second }); + if (secondStart.late) await post('/late', { id: secondStart.late, withStatus: true }); + const { body: secondEnd } = await post('/event', { hostEvent: 'turn.complete', payload: { ...second, reason: 'answer', answer: 'ok' } }); + const { body: firstEnd } = await post('/event', { hostEvent: 'turn.complete', payload: { ...first, reason: 'answer', answer: 'done' } }); + assert.equal(firstEnd.text, 'Evolver · ○ no EvoMap strategy fits this prompt'); + assert.notEqual(secondEnd.text, firstEnd.text); +}); diff --git a/test/hosts/claude-code/module.test.js b/test/hosts/claude-code/module.test.js index 8c9078d..9a7e124 100644 --- a/test/hosts/claude-code/module.test.js +++ b/test/hosts/claude-code/module.test.js @@ -159,8 +159,8 @@ test('a session starts the relay, registers the commands, and a whole turn is re const ended = await verifiedTurn(cc); assert.match(cc.seen.appended[0], /^\[Evolution Memory\] Fix flaky Redis connection retries[\s\S]*Apply them where they fit\.$/); - assert.equal(cc.seen.statuses[0], 'evolver · recalling an EvoMap strategy for this prompt…'); - assert.match(cc.seen.statuses.at(-1), /^evolver · injected "Fix flaky Redis connection retries"/); + assert.equal(cc.seen.statuses[0], '⟳ recalling an EvoMap strategy…'); + assert.match(cc.seen.statuses.at(-1), /^✓ injected "Fix flaky Redis connection retr…"$/); assert.match(ended.text, /^Evolver · injected "Fix flaky Redis connection retries" \(sha256:aaaaaaaa\) · ✓ last check passed: Run the tests · reported to EvoMap as success/); const state = join(cc.proxy.env.HOME, '.evolver', 'state'); const [sessionFile] = readdirSync(state).filter((name) => /^claude-code-session-.*\.json$/.test(name)); @@ -322,13 +322,13 @@ test('a recall that answers once a later turn has started leaves that turn\'s st await cc.call('turn.start', { text: PROMPT, turnId: 't1' }); await cc.call('prompt.submit', { ...captured.promptSubmit, text: PROMPT, origin: { kind: 'composer' } }); await cc.call('turn.start', { text: PROMPT, turnId: 't2' }); - await until(() => cc.seen.statuses.some((line) => /^evolver · injected /.test(line)), 'the second turn\'s recall'); + await until(() => cc.seen.statuses.some((line) => /^✓ injected /.test(line)), 'the second turn\'s recall'); const settled = cc.seen.statuses.length; release(); await until(() => lates === 2 && cc.seen.appended.length > 0, 'the first turn\'s answer'); await new Promise((resolve) => setTimeout(resolve, 200)); assert.equal(cc.seen.statuses.length, settled, 'the first turn wrote nothing once the second had started'); - assert.match(cc.seen.statuses.at(-1), /^evolver · injected /); + assert.match(cc.seen.statuses.at(-1), /^✓ injected /); }); test('a recall the relay cannot answer settles the status line instead of leaving it recalling', async (t) => { @@ -337,5 +337,5 @@ test('a recall the relay cannot answer settles the status line instead of leavin await cc.call('prompt.submit', { ...captured.promptSubmit, text: PROMPT, origin: { kind: 'composer' } }); await cc.call('turn.start', { text: PROMPT, turnId: 't1' }); await until(() => cc.seen.statuses.length >= 2, 'the settled status'); - assert.deepEqual(cc.seen.statuses, ['evolver · recalling an EvoMap strategy for this prompt…', 'evolver · recall did not finish for this prompt']); + assert.deepEqual(cc.seen.statuses, ['⟳ recalling an EvoMap strategy…', '! recall did not finish for this prompt']); }); diff --git a/test/recall-status.test.js b/test/recall-status.test.js index f22e09b..4a9078c 100644 --- a/test/recall-status.test.js +++ b/test/recall-status.test.js @@ -1,20 +1,34 @@ import assert from 'node:assert/strict'; import { test } from 'node:test'; -import { arrivedLateStatusOf, recallStatusOf } from '../lib/recall-status.js'; +import { arrivedLateStatusOf, recallStatusOf, unreportedTurnNoticeOf } from '../lib/recall-status.js'; -test('each recall decision has its own status line, injected or not', () => { - assert.equal(recallStatusOf({ outcome: 'injected', recalled: 2, eligible: 2, name: 'Fix flaky Redis retries' }), 'evolver · injected "Fix flaky Redis retries" (the Hub returned 2)'); - assert.equal(recallStatusOf({ outcome: 'injected', kind: 'recipe', name: 'Redis pipeline', steps: 3 }), 'evolver · injected Recipe "Redis pipeline" (3 steps)'); - assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 0, eligible: 0 }), 'evolver · no EvoMap strategy fits this prompt'); - assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 2, eligible: 0 }), 'evolver · none injected: the Hub returned 2, none is relevant enough or new to this session'); - assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 2, eligible: 1 }), 'evolver · none injected: the Hub returned 2, none has enough steps to follow'); - assert.equal(recallStatusOf({ outcome: 'unavailable' }), 'evolver · recall skipped: the Evolver Proxy did not answer (/evolver-status)'); - assert.equal(recallStatusOf({ outcome: 'short' }), 'evolver · prompt too short to recall for'); +test('each recall decision has its own status line, led by a mark, injected or not', () => { + assert.equal(recallStatusOf({ outcome: 'injected', recalled: 2, eligible: 2, name: 'Fix flaky Redis retries' }), '✓ injected "Fix flaky Redis retries"'); + assert.equal(recallStatusOf({ outcome: 'injected', kind: 'recipe', name: 'Redis pipeline', steps: 3 }), '✓ injected Recipe "Redis pipeline" · 3 steps'); + assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 0, eligible: 0 }), '○ no EvoMap strategy fits this prompt'); + assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 2, eligible: 0 }), '○ none injected · Hub returned 2, none relevant or new'); + assert.equal(recallStatusOf({ outcome: 'skipped', recalled: 2, eligible: 1 }), '○ none injected · Hub returned 2, none has enough steps'); + assert.equal(recallStatusOf({ outcome: 'unavailable' }), '! recall skipped · Evolver Proxy did not answer (/evolver-status)'); + assert.equal(recallStatusOf({ outcome: 'short' }), '○ prompt too short to recall for'); }); -test('a recall whose trace names no other decision says it injected nothing, and a long name is cut', () => { - assert.equal(recallStatusOf(null), 'evolver · no EvoMap strategy injected'); - assert.equal(recallStatusOf({ outcome: 'passed_over' }), 'evolver · no EvoMap strategy injected'); - assert.equal(arrivedLateStatusOf('x'.repeat(80)), `evolver · "${'x'.repeat(48)}" arrived after this turn ended, not injected`); +test('a recall whose trace names no other decision says it injected nothing, and a long name is cut with an ellipsis', () => { + assert.equal(recallStatusOf(null), '○ no EvoMap strategy injected'); + assert.equal(recallStatusOf({ outcome: 'passed_over' }), '○ no EvoMap strategy injected'); + assert.equal(arrivedLateStatusOf('x'.repeat(80)), `○ "${'x'.repeat(31)}…" arrived after the turn ended, not injected`); + assert.equal(recallStatusOf({ outcome: 'injected', name: 'y'.repeat(32) }), `✓ injected "${'y'.repeat(32)}"`); +}); + +test('a turn that reported no strategy ends with what its recall did', () => { + assert.equal(unreportedTurnNoticeOf({ trace: { outcome: 'skipped', recalled: 0 }, isSettled: true, isJoined: false }), 'Evolver · ○ no EvoMap strategy fits this prompt'); + assert.equal(unreportedTurnNoticeOf({ trace: null, isSettled: false, isJoined: false }), 'Evolver · ! recall had not finished when this turn ended, nothing injected'); + assert.equal(unreportedTurnNoticeOf({ trace: { outcome: 'injected', name: 'Fix retries' }, isSettled: true, isJoined: false }), + 'Evolver · ○ "Fix retries" was recalled after the model\'s last step, so this turn did not use it'); + assert.equal(unreportedTurnNoticeOf({ trace: { outcome: 'injected', name: 'Fix retries' }, isSettled: true, isJoined: true }), 'Evolver · ✓ injected "Fix retries"'); +}); + +test('a turn whose strategy was still being joined says so, neither used nor unused', () => { + assert.equal(unreportedTurnNoticeOf({ trace: { outcome: 'injected', name: 'Fix retries' }, isSettled: true, isJoined: false, isJoinPending: true }), + 'Evolver · ○ "Fix retries" was still being added when this turn ended'); });