Skip to content

Commit f0bff6e

Browse files
anandgupta42claude
andcommitted
fix: [#832] address fourth-pass re-review — case-insensitive normalizeAgentName
Reviewer flagged that the helper only matches the exact lowercase string "build". A future config, custom prompt, or hand-edited persisted session could surface "Build" or "BUILD" and the phantom telemetry bucket would come back. One toLowerCase() costs nothing and pins the contract regardless of caller hygiene. Added a regex-anchored test in plan-layer-e2e.test.ts that pins the case-fold pattern — fails if a refactor drops the toLowerCase() or switches back to a raw string compare. 71/71 tests pass; marker check clean. Out of scope (deferred to follow-up issue): - Three persisted-state sites still write `lastUser.agent` raw (compaction summary at L611, SessionCompaction.create at L650 and L1088). Execution works via the Agent.get() alias but new "build" rows can still be written. Author chose telemetry boundary scope for this PR. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent b05a532 commit f0bff6e

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ export namespace SessionPrompt {
8484
// emits below so they can never drift. Future telemetry events with an `agent`
8585
// field should route through this helper too.
8686
function normalizeAgentName(name: string | undefined): string {
87-
if (!name || name === "build") return "builder"
87+
// Case-insensitive guard: a future config, custom prompt, or hand-edited
88+
// persisted session could surface "Build" or "BUILD" and the phantom
89+
// bucket would come back. One toLowerCase() costs nothing and pins the
90+
// contract regardless of caller hygiene.
91+
if (!name || name.toLowerCase() === "build") return "builder"
8892
return name
8993
}
9094
// altimate_change end

‎packages/opencode/test/session/plan-layer-e2e.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ describe("sessionAgentName fix safety", () => {
315315
const declarations = promptTs.match(/function\s+normalizeAgentName\s*\(/g) ?? []
316316
expect(declarations.length).toBe(1)
317317
})
318+
319+
test("normalizeAgentName comparison is case-insensitive", async () => {
320+
// A future config, custom prompt, or hand-edited persisted session
321+
// could surface "Build" or "BUILD". Without case-folding, the phantom
322+
// bucket comes back. Pin that the helper does the toLowerCase() guard
323+
// so a refactor can't silently drop it.
324+
const promptTs = await fs.readFile(
325+
path.join(__dirname, "../../src/session/prompt.ts"),
326+
"utf-8",
327+
)
328+
const declIdx = promptTs.indexOf("function normalizeAgentName")
329+
expect(declIdx).toBeGreaterThan(-1)
330+
const body = promptTs.slice(declIdx, declIdx + 400)
331+
// The body should case-fold before comparing — either toLowerCase() or
332+
// toUpperCase() before the equality check. Anchored so a refactor to a
333+
// raw string compare fails this test.
334+
expect(body).toMatch(/name\.toLowerCase\(\)\s*===\s*"build"/)
335+
})
318336
})
319337

320338
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)