Skip to content

The agent that grows with you

fix(desktop): a hand-off result still correlates after the heartbeat moved marker line 2

Tekniuma10792bb5b9c
diff --git a/apps/desktop/electron/handoff-result.test.ts b/apps/desktop/electron/handoff-result.test.ts
index c88fea897a0..c9f93a51101 100644
--- a/apps/desktop/electron/handoff-result.test.ts
+++ b/apps/desktop/electron/handoff-result.test.ts
@@ -80,23 +80,25 @@ test('stable run identity outranks heartbeat time; only legacy receipts use time
]
for (const outcome of outcomes) {
- const receipt = { ...outcome, started_at: 100, finished_at }
+ const started_at = finished_at - 600
+ const receipt = { ...outcome, started_at, finished_at }
write(home, { ...receipt, run_id: 'own-run' })
- assert.ok(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: 200 }))
+ assert.ok(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: finished_at + 60 }))
for (const run_id of ['foreign-run', '', null, 42, 'bad run', 'own-run\n']) {
write(home, { ...receipt, run_id })
- assert.equal(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: 100 }), null)
+ assert.equal(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: started_at }), null)
}
// An older script has no run_id, even if its marker carried a run: line.
write(home, receipt)
- assert.ok(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: 100 }))
+ assert.ok(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: started_at }))
+ // ...and line 2 from a run that began after this result finished is not this run.
write(home, receipt)
- assert.equal(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: 200 }), null)
+ assert.equal(readAndConsumeHandoffResult(home, { expectedRunId: 'own-run', expectedStartedAt: finished_at + 60 }), null)
// Older markers and post-update boots have no expected stable identity.
write(home, { ...receipt, run_id: 'own-run' })
- assert.ok(readAndConsumeHandoffResult(home, { expectedStartedAt: 100 }))
+ assert.ok(readAndConsumeHandoffResult(home, { expectedStartedAt: started_at }))
write(home, { ...receipt, run_id: 'own-run' })
assert.ok(readAndConsumeHandoffResult(home))
}
@@ -106,14 +108,40 @@ test('stable run identity outranks heartbeat time; only legacy receipts use time
// Legacy producers correlate started_at; a result from another run is not
// reported as the one this boot waited on.
-test('a result whose started_at differs from the parked run is discarded; a match is reported with warnings', () => {
+test('a result from a run that finished before the parked one is discarded; a match is reported with warnings', () => {
const home = tempHome()
- const finished_at = Math.floor(Date.now() / 1000)
- write(home, { ok: false, exit_code: 1, message: 'old run', branch: 'main', started_at: 100, finished_at })
- assert.equal(readAndConsumeHandoffResult(home, { expectedStartedAt: 200 }), null)
+ const now = Math.floor(Date.now() / 1000)
+ const parked = now - 300
+ write(home, { ok: false, exit_code: 1, message: 'old run', branch: 'main', started_at: now - 900, finished_at: now - 800 })
+ assert.equal(readAndConsumeHandoffResult(home, { expectedStartedAt: parked }), null)
+
+ write(home, { ok: true, exit_code: 0, message: '', branch: 'main', started_at: parked, finished_at: now, warnings: ['gateway restart'] })
+ assert.deepEqual(readAndConsumeHandoffResult(home, { expectedStartedAt: parked })?.warnings, ['gateway restart'])
+})
- write(home, { ok: true, exit_code: 0, message: '', branch: 'main', started_at: 200, finished_at, warnings: ['gateway restart'] })
- assert.deepEqual(readAndConsumeHandoffResult(home, { expectedStartedAt: 200 })?.warnings, ['gateway restart'])
+// Review 5411223284 regression 2: with no `run:` line the gate sees line 2,
+// which the scripts' 300 s heartbeat moves while their run lives. A run
+// longer than 5 minutes must still have its result reported.
+test('line 2 moved by the heartbeat inside the run still correlates its result', () => {
+ const home = tempHome()
+ const now = Math.floor(Date.now() / 1000)
+ const started_at = now - 900
+ const result = { ok: false, exit_code: 3, message: 'failed after 15 min', branch: 'main', started_at, finished_at: now }
+
+ for (const heartbeat of [started_at + 300, started_at + 600, now]) {
+ write(home, { ...result, run_id: 'script-run' })
+ assert.equal(readAndConsumeHandoffResult(home, { expectedStartedAt: heartbeat })?.message, 'failed after 15 min')
+ write(home, result)
+ assert.equal(readAndConsumeHandoffResult(home, { expectedStartedAt: heartbeat })?.exitCode, 3)
+ }
+
+ // Line 2 from before this run started, or after it finished, is another run.
+ for (const other of [started_at - 1, now + 1]) {
+ write(home, result)
+ assert.equal(readAndConsumeHandoffResult(home, { expectedStartedAt: other }), null)
+ }
+
+ fs.rmSync(home, { recursive: true, force: true })
})
test('absent file returns null', () => {
diff --git a/apps/desktop/electron/handoff-result.ts b/apps/desktop/electron/handoff-result.ts
index 46999cc6d39..37afb51d8b1 100644
--- a/apps/desktop/electron/handoff-result.ts
+++ b/apps/desktop/electron/handoff-result.ts
@@ -105,13 +105,27 @@ function handoffRunMismatch(
return runId !== expectedRunId ? `[updates] hand-off result is for run ${runId}, not ${expectedRunId}; discarded` : null
}
- if (expectedStartedAt !== null && Number.isFinite(startedAt) && startedAt !== expectedStartedAt) {
+ if (expectedStartedAt !== null && Number.isFinite(startedAt) && !lineTwoWithinRun(expectedStartedAt, parsed)) {
return `[updates] hand-off result is for the run started at ${startedAt}, not ${expectedStartedAt}; discarded`
}
return null
}
+/**
+ * Whether the marker line 2 this boot saw belongs to the result's run. The
+ * scripts' heartbeat rewrites line 2 with the current time every 5 minutes
+ * while their run lives (old Desktops age-delete a marker 20 minutes after
+ * line 2), so a marker without a `run:` line can show any time from the run's
+ * start to its finish. A finished earlier run ended before this one began.
+ */
+function lineTwoWithinRun(lineTwo: number, parsed: any): boolean {
+ const startedAt = Number(parsed?.started_at)
+ const finishedAt = Number(parsed?.finished_at)
+
+ return lineTwo === startedAt || (lineTwo > startedAt && Number.isFinite(finishedAt) && lineTwo <= finishedAt)
+}
+
function toHandoffResult(parsed: any, manual: boolean): HandoffResult {
return {
ok: Boolean(parsed?.ok),
@@ -128,7 +142,8 @@ function toHandoffResult(parsed: any, manual: boolean): HandoffResult {
* `.corrupt` and logged — never silently dropped — so a torn result stays
* inspectable. Match the stable marker run ID, not line 2 (a heartbeat that
* can change before this Desktop even opens). Only older producers without
- * run_id, or boots without an identified marker, use started_at correlation.
+ * run_id, or boots without an identified marker, use line 2, and then as a
+ * time inside the run (started_at..finished_at), never as its exact start.
*/
export function readAndConsumeHandoffResult(
hermesHome: string,