diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index 105d21956f..9b3c01ec09 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -512,6 +512,13 @@ export function derivedAncestorSessions(session: Session): Session[] { // turn) and a short steering section is appended. No store/fold/injection // mechanics are touched. Absent plan state or disabled ⇒ byte-identical // output (the stable sort degrades to the kernel's lexical order). +// #5047: plan signal comes from the ACTIVE tier of a recognized complete +// TodoWrite/todowrite snapshot only (in_progress descriptions, else pending); +// terminal tasks stop contributing while their historical blocks stay +// searchable through the lexical pool. Unrecognized inputs (custom planning +// tools, incremental updates, malformed JSON) keep the legacy whole-text +// tokenization. Ranking additionally protects explicit queries: a query term +// that discriminates within the candidate pool outranks plan-only affinity. const EFFECTIVE_SEARCH_PLAN_KEY = "searchPlanAware"; @@ -565,6 +572,51 @@ function planTokenize(text: string): string[] { return out; } +// #5047: known TodoWrite status vocabulary. A snapshot whose items all use +// this vocabulary is a recognized complete snapshot; anything else (custom +// planning tools, incremental updates, foreign status values) keeps the +// legacy whole-text tokenization below. +const PLAN_TODO_STATUSES = new Set(["pending", "in_progress", "completed", "cancelled"]); + +/** Recognize a complete TodoWrite/todowrite snapshot in a planning tool + * call's raw JSON input (`{"todos":[{"content":string,"status":string},…]}`). + * Null when the shape is not a recognized complete snapshot (malformed or + * truncated JSON, missing fields, unknown status vocabulary) — callers then + * keep the pre-#5047 whole-text behavior for that message. */ +function parseTodoSnapshot(text: string): Array<{ content: string; status: string }> | null { + const trimmed = text.trim(); + if (!trimmed.startsWith("{")) return null; + let doc: unknown; + try { + doc = JSON.parse(trimmed); + } catch { + return null; + } + if (typeof doc !== "object" || doc === null || Array.isArray(doc)) return null; + const todos = (doc as Record).todos; + if (!Array.isArray(todos) || todos.length === 0) return null; + const items: Array<{ content: string; status: string }> = []; + for (const entry of todos) { + if (typeof entry !== "object" || entry === null) return null; + const rec = entry as Record; + if (typeof rec.content !== "string" || typeof rec.status !== "string") return null; + if (!PLAN_TODO_STATUSES.has(rec.status)) return null; + items.push({ content: rec.content, status: rec.status }); + } + return items; +} + +/** #5047: the plan-signal tier of a recognized snapshot — in_progress + * descriptions, or pending descriptions when nothing is in progress. + * Terminal (completed/cancelled) descriptions contribute no plan signal; + * their historical blocks stay reachable through the lexical pool. Empty + * result ⇒ the whole snapshot is terminal. */ +function activeTodoDescriptions(items: Array<{ content: string; status: string }>): string { + const inProgress = items.filter((t) => t.status === "in_progress").map((t) => t.content); + const tier = inProgress.length > 0 ? inProgress : items.filter((t) => t.status === "pending").map((t) => t.content); + return tier.join("\n"); +} + function resolveDerivedDecompress( args: Record, ctx: ProxyToolCtx, @@ -600,7 +652,10 @@ function resolveDerivedDecompress( * tool call matching each planning pattern (mirrors the kernel's * collectLatestProtected latest-wins semantics — per projected path for * slash patterns, so `skill/*` keeps one slot per skill, #1947) plus the - * most recent user turn. Returns null when nothing usable is in context. */ + * most recent user turn. Recognized complete TodoWrite/todowrite snapshots + * contribute only their active tier (#5047); unrecognized inputs keep the + * legacy whole-text tokenization. Returns null when nothing usable is in + * context. */ export function extractPlanState(messages: CoreMessage[], extraPatterns?: string[]): { terms: PlanTerms } | null { const patterns = [...new Set([...PLANNING_TOOL_PATTERNS, ...(extraPatterns ?? [])])]; const start = Math.max(0, messages.length - PLAN_WINDOW_MESSAGES); @@ -623,7 +678,19 @@ export function extractPlanState(messages: CoreMessage[], extraPatterns?: string } } const sources: Array<{ text: string; weight: number }> = []; - for (const msg of latestByPattern.values()) if (msg.text) sources.push({ text: msg.text, weight: 2 }); + for (const msg of latestByPattern.values()) { + if (!msg.text) continue; + // #5047: recognized complete snapshots contribute their ACTIVE tier + // only (terminal tasks stop steering retrieval); unrecognized shapes + // keep the legacy whole-text tokenization. + const snapshot = parseTodoSnapshot(msg.text); + if (snapshot) { + const active = activeTodoDescriptions(snapshot); + if (active) sources.push({ text: active, weight: 2 }); + continue; + } + sources.push({ text: msg.text, weight: 2 }); + } if (lastUserText) sources.push({ text: lastUserText, weight: 1 }); if (sources.length === 0) return null; const terms: PlanTerms = new Map(); @@ -803,8 +870,20 @@ export function executeSearchContext( if (plan && !foreignSessionId && pool.length > limit) { const planState = extractPlanState(plan.messages, plan.protectedPatterns); if (planState) { - const withScores = pool.map((b, i) => ({ b, i, score: planBlockScore(b.topic, b.summary, planState.terms) })); - withScores.sort((x, y) => y.score - x.score || x.i - y.i); + // #5047: explicit-query protection — a query term that discriminates + // within the candidate pool (matches SOME but not ALL blocks) marks the + // blocks it hits as explicit-query matches; they outrank blocks whose + // only claim is plan affinity. A non-discriminating term (hits every + // block, e.g. a shared noun like "token") carries no such signal, so a + // genuinely ambiguous query degrades to the pure #1336 plan ranking. + const discriminating = terms.filter((t) => { + const f = freq.get(t) ?? 0; + return f > 0 && f < pool.length; + }); + const qMatch = (b: CompressionBlock): boolean => + discriminating.some((t) => (b.topic ?? "").toLowerCase().includes(t) || b.summary.toLowerCase().includes(t)); + const withScores = pool.map((b, i) => ({ b, i, score: planBlockScore(b.topic, b.summary, planState.terms), q: qMatch(b) ? 1 : 0 })); + withScores.sort((x, y) => y.q - x.q || y.score - x.score || x.i - y.i); ranked = withScores.map((s) => s.b); scored = withScores.map((s) => ({ block: s.b, score: s.score })); plan.log(`[acp-search-plan] "${query}": ${withScores.filter((s) => s.score > 0).length}/${pool.length} candidates plan-relevant → ${scored.slice(0, limit).map((s) => `${s.block.blockId}:${s.score}`).join(", ")}`); diff --git a/tests/search-plan-aware.test.ts b/tests/search-plan-aware.test.ts index ea5bac1e30..214aa2a730 100644 --- a/tests/search-plan-aware.test.ts +++ b/tests/search-plan-aware.test.ts @@ -122,6 +122,108 @@ test("extractPlanState: bounded to the last 40 messages", () => { assert.ok(st!.terms.has("kubernetes"), "in-window user turn term present"); }); +// ===================== #5047: status-aware plan signal + explicit-query protection ===================== +// Pre-#5047 the extractor tokenized the ENTIRE latest planning snapshot, so a +// task's status (stopworded) never changed the extracted terms and repeated +// completed descriptions could dominate the single active task. #5047 makes a +// recognized complete TodoWrite/todowrite snapshot contribute only its ACTIVE +// tier (in_progress, else pending) and lets a discriminating explicit query +// outrank plan-only affinity. + +function foldTwoBlocks(): Folded { + const core = createCore(); + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: true, minToolTokens: 50 } }) as Config; + const session = getSession(`sp5047-${Math.random().toString(36).slice(2)}`); + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + const turn = core.processTurn({ messages: makeMsgs(40), state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const ctx = { core, config, messages: turn.messages, session, log: () => {} }; + applyRanges(parseCompressInput({ + content: [ + { startId: "m00001", endId: "m00005", summary: "Authentication history: how login token issuance and validation were built.", topic: "token authentication history" }, + { startId: "m00006", endId: "m00010", summary: "Ratelimit history: rate limiting middleware added to the token endpoints.", topic: "token ratelimit history" }, + ], + }), ctx); + assert.equal(session.state.blocks.filter((b) => b.active).length, 2, "two blocks created"); + return { core, config, session, msgs: turn.messages }; +} + +function todoOnly(todos: Array>): CoreMessage[] { + return [{ id: "t_plan", role: "assistant", contentType: "tool-call", toolName: "TodoWrite", text: JSON.stringify({ todos }) }]; +} + +function optsFor(f: Folded, msgs: CoreMessage[], logs: string[]) { + return { messages: msgs, session: f.session, log: (m: string) => logs.push(m), protectedPatterns: f.config.protectedLatestTools }; +} + +test("#5047 extractPlanState: recognized snapshot contributes only its active tier", () => { + const st = extractPlanState(todoOnly([ + { content: "zebra stripes migration", status: "completed" }, + { content: "giraffe feed rollout", status: "in_progress" }, + { content: "ox herd expansion", status: "pending" }, + ])); + assert.ok(st, "plan state extracted"); + assert.ok(st!.terms.has("giraffe"), "in_progress description kept"); + assert.ok(!st!.terms.has("zebra"), "completed description dropped"); + assert.ok(!st!.terms.has("ox"), "pending description dropped while something is in progress"); +}); + +test("#5047 extractPlanState: pending tier used when nothing is in progress", () => { + const st = extractPlanState(todoOnly([ + { content: "hyena pack survey", status: "pending" }, + { content: "impala fence repair", status: "completed" }, + ])); + assert.ok(st, "plan state extracted"); + assert.ok(st!.terms.has("hyena"), "pending description kept"); + assert.ok(!st!.terms.has("impala"), "completed description dropped"); +}); + +test("#5047 extractPlanState: fully terminal snapshot contributes no plan signal", () => { + const st = extractPlanState(todoOnly([ + { content: "lion pride census", status: "completed" }, + { content: "rhino horn audit", status: "cancelled" }, + ])); + assert.equal(st, null, "all-terminal snapshot + no user turn → no usable plan state"); +}); + +test("#5047 extractPlanState: unrecognized shapes keep the legacy whole-text fallback", () => { + const trunc = extractPlanState([{ id: "t1", role: "assistant", contentType: "tool-call", toolName: "TodoWrite", text: '{"todos":[{"content":"kubernetes upgrade",' }]); + assert.ok(trunc && trunc.terms.has("kubernetes"), "invalid JSON falls back to whole-text tokenization"); + const foreign = extractPlanState(todoOnly([{ content: "wolf den rebuild", status: "on_hold" }])); + assert.ok(foreign && foreign.terms.has("wolf"), "unknown status → not a recognized snapshot → whole-text"); + const missing = extractPlanState(todoOnly([{ content: "camel caravan route" }])); + assert.ok(missing && missing.terms.has("camel"), "missing status → whole-text"); +}); + +test("#5047 extractPlanState: oversized active description is char-capped, not dropped", () => { + const big = "elephant tusk inventory ".repeat(200); + const st = extractPlanState(todoOnly([{ content: big, status: "in_progress" }])); + assert.ok(st, "plan state extracted despite size"); + assert.ok(st!.terms.has("elephant"), "leading terms survive the cap"); +}); + +test("#5047 status-swap flips retrieval (the reported repro)", () => { + const f = foldTwoBlocks(); + const a = executeSearchContext({ query: "token", limit: 1 }, f.core, f.session.state, undefined, + optsFor(f, todoOnly([{ content: "authentication", status: "completed" }, { content: "ratelimit", status: "in_progress" }]), [])).text; + assert.match(a, /Found 1 block\(s\) for "token"/); + assert.ok(a.includes('"token ratelimit history"'), "active task's block returned for the ambiguous query"); + assert.ok(!a.includes('"token authentication history"'), "completed task's block displaced"); + const b = executeSearchContext({ query: "token", limit: 1 }, f.core, f.session.state, undefined, + optsFor(f, todoOnly([{ content: "authentication", status: "in_progress" }, { content: "ratelimit", status: "completed" }]), [])).text; + assert.ok(b.includes('"token authentication history"'), "swapping statuses flips the winner"); + assert.ok(!b.includes('"token ratelimit history"'), "previous winner displaced after the swap"); +}); + +test("#5047 explicit historical query outranks a less-relevant active task", () => { + const f = foldTwoBlocks(); + const logs: string[] = []; + const r = executeSearchContext({ query: "authentication", limit: 1 }, f.core, f.session.state, undefined, + optsFor(f, todoOnly([{ content: "authentication", status: "completed" }, { content: "ratelimit", status: "in_progress" }]), logs)).text; + assert.ok(r.includes('"token authentication history"'), "explicit discriminating query returns its own block first"); + assert.ok(!r.includes('"token ratelimit history"'), "plan-active block does not displace an explicit hit"); +}); + test("byte-identical output when plan state is absent or the pool fits the limit", () => { const f = foldBlocks(true); const base = executeSearchContext({ query: "token" }, f.core, f.session.state).text;