diff --git a/pm-stack/dispatch/src/launch_annotations.ts b/pm-stack/dispatch/src/launch_annotations.ts index 3efa47a309..00159e93f7 100644 --- a/pm-stack/dispatch/src/launch_annotations.ts +++ b/pm-stack/dispatch/src/launch_annotations.ts @@ -14,6 +14,12 @@ /** The slice of the queue client launch annotations need. */ export interface LaunchAnnotationQueue { patchTask(id: number, fields: Record): Promise; + /** Optional read of the task row: when present, a pre-existing + * last_error (e.g. a lane-ledger note PATCHed earlier in the same + * launch) is preserved instead of overwritten. Absence (older + * stubs) or failure degrades to the previous overwrite behavior — + * the launch itself is never affected. */ + getTask?(id: number): Promise | null>; } /** The slice of the GitHub client launch annotations need. */ @@ -31,7 +37,9 @@ function errorText(e: unknown): string { * Returns one warning per failed annotation (empty when all succeed); * the wait:true path surfaces them in its success response. Each * failure is also noted on the task: the note accumulates, so when - * both annotations fail the final last_error names both. + * both annotations fail the final last_error names both. A last_error + * already on the task (e.g. the lane-ledger note from launchSession) + * is preserved: the note is prepended, never overwritten. */ export async function annotateLaunch( github: LaunchAnnotationGitHub, @@ -45,12 +53,32 @@ export async function annotateLaunch( const warnings: string[] = []; const failures: string[] = []; + // A last_error already on the task (the lane-ledger failure note is + // PATCHed before annotateLaunch runs). Read lazily on the first + // failure so the happy path issues no extra GET; a missing reader + // or a failed read degrades to no prefix — best-effort throughout. + let prefix: string | null = null; + let prefixLoaded = false; + async function loadPrefix(): Promise { + if (prefixLoaded) return; + prefixLoaded = true; + try { + const row = await queue.getTask?.(taskId); + const prior = row?.last_error; + if (typeof prior === "string" && prior.trim()) prefix = prior.trim(); + } catch { + /* best-effort: the annotation warning survives regardless */ + } + } + async function noteFailure(step: string, e: unknown): Promise { const warning = `launch annotation failed (launch unaffected): ${step}: ${errorText(e)}`; warnings.push(warning); failures.push(warning); + await loadPrefix(); + const note = prefix ? [prefix, ...failures].join("; ") : failures.join("; "); try { - await queue.patchTask(taskId, { last_error: failures.join("; ") }); + await queue.patchTask(taskId, { last_error: note }); } catch { /* nothing more to do — the claim stands and the warning remains */ } diff --git a/pm-stack/dispatch/test/launch_annotations.test.ts b/pm-stack/dispatch/test/launch_annotations.test.ts index 854bb1fc92..6df6dda820 100644 --- a/pm-stack/dispatch/test/launch_annotations.test.ts +++ b/pm-stack/dispatch/test/launch_annotations.test.ts @@ -10,12 +10,15 @@ import { annotateLaunch } from "../src/launch_annotations.ts"; // warning, and the remaining steps still run. The launch itself is // never rejected — these stubs pin that contract. -function stubs(opts: { failComment?: boolean; failLabels?: boolean; failPatch?: boolean } = {}) { +function stubs( + opts: { failComment?: boolean; failLabels?: boolean; failPatch?: boolean; failGet?: boolean; preExisting?: string | null } = {}, +) { const calls: { comments: [string, number, string][]; labels: [string, number, string[]][]; patches: [number, Record][]; - } = { comments: [], labels: [], patches: [] }; + gets: number[]; + } = { comments: [], labels: [], patches: [], gets: [] }; const github = { async comment(repo: string, issueNumber: number, body: string): Promise { calls.comments.push([repo, issueNumber, body]); @@ -31,6 +34,11 @@ function stubs(opts: { failComment?: boolean; failLabels?: boolean; failPatch?: calls.patches.push([id, fields]); if (opts.failPatch) throw new Error("queue unreachable"); }, + async getTask(id: number): Promise<{ last_error?: unknown } | null> { + calls.gets.push(id); + if (opts.failGet) throw new Error("queue read unreachable"); + return { last_error: opts.preExisting ?? null }; + }, }; return { calls, github, queue }; } @@ -97,4 +105,52 @@ describe("annotateLaunch", () => { expect(warnings).toHaveLength(1); expect(calls.labels).toHaveLength(1); }); + + test("the happy path issues no task read", async () => { + const { calls, github, queue } = stubs(); + const warnings = await run(github, queue); + expect(warnings).toEqual([]); + expect(calls.patches).toHaveLength(0); + expect(calls.gets).toHaveLength(0); + }); + + // #10087 (PR #4231 residual): a lane-ledger failure note already on + // the task must survive an annotation failure — the note is + // prepended, not overwritten. + test("a pre-existing last_error survives a failing annotation (#10087)", async () => { + const { calls, github, queue } = stubs({ + failComment: true, + preExisting: "lane assignment failed (launch unaffected): lane rpc 500", + }); + const warnings = await run(github, queue); + expect(warnings).toHaveLength(1); + expect(calls.gets).toEqual([5414]); + expect(calls.patches).toHaveLength(1); + const note = String(calls.patches[0][1].last_error); + expect(note).toContain("lane assignment failed"); + expect(note).toContain("github comment 500"); + }); + + test("a pre-existing last_error plus both annotations failing names all three (#10087)", async () => { + const { calls, github, queue } = stubs({ + failComment: true, + failLabels: true, + preExisting: "lane assignment failed (launch unaffected): lane rpc 500", + }); + const warnings = await run(github, queue); + expect(warnings).toHaveLength(2); + expect(calls.patches).toHaveLength(2); + const finalNote = String(calls.patches[1][1].last_error); + expect(finalNote).toContain("lane assignment failed"); + expect(finalNote).toContain("github comment 500"); + expect(finalNote).toContain("github labels 500"); + }); + + test("a failed task read still notes the annotation failure alone (#10087)", async () => { + const { calls, github, queue } = stubs({ failComment: true, failGet: true }); + const warnings = await run(github, queue); + expect(warnings).toHaveLength(1); + expect(calls.patches).toHaveLength(1); + expect(String(calls.patches[0][1].last_error)).toContain("github comment 500"); + }); });