Skip to content

Commit dda3a2d

Browse files
committed
fix: resolve remaining harness review findings
1 parent a1bb0a6 commit dda3a2d

12 files changed

Lines changed: 134 additions & 20 deletions

‎packages/opencode/src/cli/cmd/run-accounting.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Fork-only helpers for the `run` command:
22
// - honest turn accounting: compaction-machinery steps must not consume the
33
// --max-turns budget. `step-start` parts carry only messageID/sessionID, so
4-
// the owning message's agent is resolved via a lookup populated from
4+
// the owning message's summary flag is resolved via a lookup populated from
55
// `message.updated` events (the assistant message row is persisted — and its
66
// event published — before its first step-start part streams).
77
// - dual-attribution termination logging: every run records TWO independent
@@ -55,7 +55,7 @@ export function createRecoverableOverflowTraceErrors() {
5555
// challenge, so instruction and detection can never drift apart.
5656

5757
export function create() {
58-
const agents = new Map<string, string>()
58+
const summaryMessages = new Set<string>()
5959
let turnCount = 0
6060
let lastFinishReason: string | undefined
6161
// altimate_change start — upstream_fix: onText and onStepFinish are
@@ -96,13 +96,14 @@ export function create() {
9696
// altimate_change end
9797

9898
function isCompactionStep(messageID: string) {
99-
return agents.get(messageID) === "compaction"
99+
return summaryMessages.has(messageID)
100100
}
101101

102102
return {
103-
/** Record an assistant message's agent so later part events can be attributed. */
104-
onAssistantMessage(info: { id: string; agent?: string }) {
105-
agents.set(info.id, info.agent ?? "")
103+
/** Record whether an assistant message is actual compaction machinery. */
104+
onAssistantMessage(info: { id: string; agent?: string; summary?: boolean }) {
105+
if (info.summary === true) summaryMessages.add(info.id)
106+
else summaryMessages.delete(info.id)
106107
},
107108
isCompactionStep,
108109
/**

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,24 @@ export namespace SessionCompaction {
305305
const context = input.model.limit.context
306306
if (context === 0) return 0
307307

308+
// Accept the V2 retained-tail name directly as well as the legacy V1 field.
309+
// The main config path currently migrates V1, but callers supplying an
310+
// already-V2 object must not silently lose keep.tokens.
311+
const compaction = input.cfg.compaction as
312+
| (NonNullable<ConfigInfo["compaction"]> & { keep?: { tokens?: number } })
313+
| undefined
308314
const maxOutput = ProviderTransform.maxOutputTokens(input.model)
309-
const reserved = input.cfg.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, maxOutput)
315+
const reserved = compaction?.reserved ?? Math.min(COMPACTION_BUFFER, maxOutput)
310316
const usable = input.model.limit.input
311317
? Math.max(0, input.model.limit.input - reserved)
312318
: Math.max(0, context - maxOutput)
313319
const candidate =
314-
input.cfg.compaction?.preserve_recent_tokens ??
320+
compaction?.keep?.tokens ??
321+
compaction?.preserve_recent_tokens ??
315322
Math.min(MAX_PRESERVE_RECENT_TOKENS, Math.max(MIN_PRESERVE_RECENT_TOKENS, Math.floor(usable * 0.25)))
316323
// Clamp (applies to explicit config too — the no-churn invariant is
317324
// unconditional): tail budget + ledger budget <= fraction of the trigger.
318-
const triggerHeadroom = Math.max(input.cfg.compaction?.reserved ?? COMPACTION_BUFFER, maxOutput)
325+
const triggerHeadroom = Math.max(compaction?.reserved ?? COMPACTION_BUFFER, maxOutput)
319326
const base = input.model.limit.input ?? context
320327
if (base <= triggerHeadroom) return candidate // compaction disabled entirely; no trigger to protect
321328
const threshold = overflowThreshold({
@@ -958,7 +965,17 @@ export namespace SessionCompaction {
958965

959966
/** Path-like tokens (contain a dot or slash) — the artifact names a claim can be checked against. */
960967
function artifactTokens(text: string): string[] {
961-
return (text.match(/[A-Za-z0-9_@-]*[./][A-Za-z0-9_./-]+/g) ?? []).filter((t) => t.length >= 3 && /[A-Za-z]/.test(t))
968+
// URLs and release identifiers are context, not filesystem evidence. If a
969+
// verified claim also mentions its docs URL or runtime version, treating
970+
// those tokens as artifacts makes the all-artifacts check demote the claim.
971+
const scrubbed = text.replace(/\b[a-z][a-z0-9+.-]*:\/\/[^\s<>{}[\]"'`]+/gi, " ")
972+
return (scrubbed.match(/[A-Za-z0-9_@-]*[./][A-Za-z0-9_./-]+/g) ?? []).filter(
973+
(token) =>
974+
token.length >= 3 &&
975+
/[A-Za-z]/.test(token) &&
976+
!/^v?\d+(?:\.\d+)+(?:[-+][A-Za-z0-9.-]+)?$/i.test(token) &&
977+
!/@v?\d+(?:\.\d+)+(?:[-+][A-Za-z0-9.-]+)?$/i.test(token),
978+
)
962979
}
963980

964981
function itemCorroborated(text: string, ledger: Ledger): boolean {

‎packages/opencode/src/session/nudge.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ function strength(kind: string): number {
8686
export function register(sessionID: string, directive: Directive, generation?: Generation): void {
8787
const entry = bucket(sessionID, generation)
8888
if (!entry) return
89-
const existing = entry.directives.findIndex(
90-
(d) => d.source === directive.source && d.kind === directive.kind,
91-
)
89+
const existing = entry.directives.findIndex((d) => d.source === directive.source && d.kind === directive.kind)
9290
if (existing >= 0) entry.directives[existing] = directive
9391
else entry.directives.push(directive)
9492
}
@@ -99,6 +97,18 @@ export function pending(sessionID: string): readonly Directive[] {
9997
return pendingBySession.get(sessionID)?.directives ?? []
10098
}
10199

100+
/** Drop one detector's stale candidates without consuming directives from
101+
* other sources. A mode/exemption change can disarm starvation between the
102+
* registration step and delivery, while termination or budget directives
103+
* for that same turn remain valid. */
104+
export function discardSource(sessionID: string, source: Source, generation?: Generation): void {
105+
const entry = pendingBySession.get(sessionID)
106+
if (!entry || (generation !== undefined && entry.generation !== generation)) return
107+
entry.directives = entry.directives.filter((directive) => directive.source !== source)
108+
if (entry.directives.length === 0 && entry.generation === undefined) pendingBySession.delete(sessionID)
109+
else store(sessionID, entry)
110+
}
111+
102112
/** Return the single highest-precedence directive and clear ALL pending
103113
* directives for the session — at most one directive block per turn. */
104114
export function take(sessionID: string, generation?: Generation): Directive | undefined {

‎packages/opencode/src/session/processor.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,13 @@ export namespace SessionProcessor {
338338
})
339339
const starvation = sbGate.tracks ? SessionStarvation.forSession(input.sessionID, sbConfig) : undefined
340340
const sbArmed = sbGate.armed
341+
// A directive may have been registered on the previous step while the
342+
// breaker was armed. If this working turn is now off, annotate-only, or
343+
// agent-exempt, discard only that stale source; compaction summaries
344+
// intentionally leave all pending directives for the next working turn.
345+
if (!input.assistantMessage.summary && !sbArmed) {
346+
NudgeArbiter.discardSource(input.sessionID, "starvation_breaker", input.nudgeGeneration)
347+
}
341348
const sbMode = sbConfig.mode === "armed" ? ("armed" as const) : ("annotate" as const)
342349
let starvationStop = false
343350
// altimate_change start — per-tool-result dispatch cap, resolved once

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,10 @@ export namespace SessionPrompt {
29082908
if (input.capTokens <= 0) return undefined
29092909
const text = input.text
29102910
if (Token.estimate(text) <= input.capTokens) return text
2911+
// Slice by Unicode code points, not UTF-16 code units. Either head/tail
2912+
// boundary can otherwise bisect a surrogate pair and persist invalid text
2913+
// in the pinned task.
2914+
const characters = Array.from(text)
29112915
// Over cap: middle truncation alone deletes exactly the mid-prompt facts
29122916
// the evidence shows decaying — pair verbatim head+tail with the card.
29132917
const cardCap = Math.min(input.cardCapTokens, Math.floor(input.capTokens / 2))
@@ -2921,17 +2925,21 @@ export namespace SessionPrompt {
29212925
let charBudget = Math.floor(bodyBudget * 3.7)
29222926
while (charBudget >= 100) {
29232927
const half = Math.floor(charBudget / 2)
2924-
const candidate = text.slice(0, half) + marker + text.slice(text.length - half) + (card ? "\n\n" + card : "")
2928+
const candidate =
2929+
characters.slice(0, half).join("") +
2930+
marker +
2931+
characters.slice(characters.length - half).join("") +
2932+
(card ? "\n\n" + card : "")
29252933
if (Token.estimate(candidate) <= input.capTokens) return candidate
29262934
charBudget = Math.floor(charBudget * 0.85)
29272935
}
29282936
if (card) return card
29292937
// A positive body budget must always be renderable; otherwise compaction
29302938
// could omit the task from its summary while the corresponding pin silently
29312939
// disappears. Fall back to the longest verbatim prefix that fits.
2932-
let prefixLength = Math.min(text.length, Math.max(1, Math.ceil(input.capTokens * 4)))
2940+
let prefixLength = Math.min(characters.length, Math.max(1, Math.ceil(input.capTokens * 4)))
29332941
while (prefixLength > 0) {
2934-
const prefix = text.slice(0, prefixLength)
2942+
const prefix = characters.slice(0, prefixLength).join("")
29352943
if (Token.estimate(prefix) <= input.capTokens) return prefix
29362944
prefixLength = Math.floor(prefixLength * 0.75)
29372945
}

‎packages/opencode/src/session/starvation.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export const DEFAULTS: ResolvedConfig = {
7474
doomLoopThreshold: 3,
7575
pollingThresholdMultiplier: 5,
7676
pollingPattern: "\\b(sleep|watch|status)\\b",
77-
exemptAgents: ["plan", "review"],
77+
// `reviewer` is the built-in read-only agent. Keep `review` for existing
78+
// user-defined configs and older clients that used that spelling.
79+
exemptAgents: ["plan", "review", "reviewer"],
7880
// Generated/regenerating artifacts: re-reading these is expected to see new
7981
// content on every build, so unchanged-read annotation must not fire.
8082
generatedPathPatterns: [

‎packages/opencode/test/cli/run-accounting.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@ describe("RunAccounting turn accounting", () => {
3333
test("excludes compaction-machinery steps from turnCount", () => {
3434
const acc = RunAccounting.create()
3535
acc.onAssistantMessage({ id: "msg_work", agent: "build" })
36-
acc.onAssistantMessage({ id: "msg_compact", agent: "compaction" })
36+
acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true })
3737
expect(acc.onStepStart("msg_work")).toBe(true)
3838
expect(acc.onStepStart("msg_compact")).toBe(false)
3939
expect(acc.onStepStart("msg_compact")).toBe(false)
4040
expect(acc.onStepStart("msg_work")).toBe(true)
4141
expect(acc.turnCount).toBe(2)
4242
})
4343

44+
test("an explicitly selected agent named compaction still consumes the turn budget", () => {
45+
const acc = RunAccounting.create()
46+
acc.onAssistantMessage({ id: "msg_user_agent", agent: "compaction", summary: false })
47+
expect(acc.onStepStart("msg_user_agent")).toBe(true)
48+
expect(acc.turnCount).toBe(1)
49+
})
50+
4451
test("a step whose owning message is unknown is counted (conservative default)", () => {
4552
const acc = RunAccounting.create()
4653
expect(acc.onStepStart("msg_unknown")).toBe(true)
@@ -50,7 +57,7 @@ describe("RunAccounting turn accounting", () => {
5057
test("compaction steps do not perturb termination attribution", () => {
5158
const acc = RunAccounting.create()
5259
acc.onAssistantMessage({ id: "msg_work", agent: "build" })
53-
acc.onAssistantMessage({ id: "msg_compact", agent: "compaction" })
60+
acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true })
5461
acc.onStepFinish("msg_work", "stop")
5562
// compaction machinery finishing later must not overwrite the model's reason
5663
acc.onStepFinish("msg_compact", "tool-calls")

‎packages/opencode/test/session/compaction-ledger.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,26 @@ describe("SessionCompaction.corroborateCarry", () => {
564564
expect(out[0]!.status).toBe("claimed, unverified")
565565
})
566566

567+
test("URLs and version identifiers do not demote an otherwise corroborated artifact", () => {
568+
const out = SessionCompaction.corroborateCarry(
569+
[
570+
{
571+
text: "created models/orders.sql for v2.1.0; docs at https://docs.example.com/releases/v2.1.0",
572+
},
573+
],
574+
ledger,
575+
)
576+
expect(out[0]!.status).toBe("verified")
577+
})
578+
579+
test("filtering URL and version noise does not corroborate a missing real artifact", () => {
580+
const out = SessionCompaction.corroborateCarry(
581+
[{ text: "created reports/missing.csv for [email protected]; see https://example.com/changelog" }],
582+
ledger,
583+
)
584+
expect(out[0]!.status).toBe("claimed, unverified")
585+
})
586+
567587
test("commands alone never corroborate an artifact, even with exit zero", () => {
568588
const out = SessionCompaction.corroborateCarry(
569589
[{ text: "exported report.csv" }, { text: "validated broken_thing.json" }],

‎packages/opencode/test/session/compaction-loop.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,12 @@ describe("small-window retained-content clamp", () => {
663663
expect(budget).toBe(8_000)
664664
})
665665

666+
test("an already-V2 keep.tokens value configures the retained tail budget", () => {
667+
const model = createModel({ context: 200_000, output: 32_000 })
668+
const cfg = { compaction: { keep: { tokens: 3_500 } } } as any
669+
expect(SessionCompaction.preserveRecentBudget({ cfg, model })).toBe(3_500)
670+
})
671+
666672
// altimate_change start — PR #1171 review: the ledger reservation was taken out
667673
// of the tail budget even with both ledger and carry disabled, so a large
668674
// ledger_max_tokens could starve the retained tail for text never rendered.

‎packages/opencode/test/session/nudge-arbiter.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ describe("NudgeArbiter one-directive-per-turn contract", () => {
107107
expect(NudgeArbiter.pending(SID)).toHaveLength(0)
108108
})
109109

110+
test("discardSource removes stale starvation while preserving other sources", () => {
111+
const generation = NudgeArbiter.begin(SID)
112+
NudgeArbiter.register(SID, { source: "starvation_breaker", kind: "starvation", text: "stale" }, generation)
113+
NudgeArbiter.register(
114+
SID,
115+
{ source: "termination_challenge", kind: "completion_nudge", text: "still valid" },
116+
generation,
117+
)
118+
119+
NudgeArbiter.discardSource(SID, "starvation_breaker", generation)
120+
121+
expect(NudgeArbiter.pending(SID)).toEqual([
122+
{ source: "termination_challenge", kind: "completion_nudge", text: "still valid" },
123+
])
124+
expect(NudgeArbiter.take(SID, generation)?.text).toBe("still valid")
125+
})
126+
110127
test("stale callbacks cannot register or clear a newer loop generation", () => {
111128
const oldGeneration = NudgeArbiter.begin(SID)
112129
NudgeArbiter.register(SID, { source: "starvation_breaker", kind: "starvation", text: "old" }, oldGeneration)

0 commit comments

Comments
 (0)