1- import type { StatementSync } from 'node:sqlite' ;
1+ import type { SQLOutputValue , StatementSync } from 'node:sqlite' ;
22import { LEGACY_SUBJECT , type Run } from '../core/contracts.js' ;
33import { Fault } from '../core/util.js' ;
44import type { Projection } from './output.js' ;
@@ -19,6 +19,77 @@ const scheduling = `SELECT r.owner,r.subject,r.agent_id,r.session_id,r.status,r.
1919const queued = scheduling + "r.status='queued'" ,
2020 parked = scheduling + "r.status IN ('requires_action','queued')" ;
2121type Scope = { owner ?: string ; subject ?: string } ;
22+ const matchesIndexedRun = ( run : Run , row : Record < string , SQLOutputValue > , id : string ) =>
23+ run ?. checkpoint &&
24+ Array . isArray ( run . checkpoint . messages ) &&
25+ run . metadata &&
26+ run . route &&
27+ typeof run . digest === 'string' &&
28+ run . id === id &&
29+ run . owner === row . owner &&
30+ run . status === row . status &&
31+ run . agentId === row . agent_id &&
32+ run . sessionId === row . session_id ;
33+ const matchesSubject = ( run : Run , subject : string ) =>
34+ run . subject == null || run . subject === subject ;
35+
36+ /** A mismatched subject is never returned as a recoverable run. The scheduler can
37+ * use the indexed subject only to retire a queued child with no scoped parent. */
38+ export function readRecoveryRun (
39+ store : Reader ,
40+ id : string ,
41+ project : Project ,
42+ cancelMismatchedChild ?: ( run : Run ) => boolean ,
43+ ) : Run | undefined {
44+ const row = store
45+ . prepare ( 'SELECT owner,subject,agent_id,session_id,status,payload FROM runs WHERE id=?' )
46+ . get ( id ) ;
47+ if ( ! row ) return ;
48+ const subject = String ( row . subject ?? LEGACY_SUBJECT ) ;
49+ const quarantine = ( ) => {
50+ store . terminalPublications . detachedRun ( id ) ;
51+ return undefined ;
52+ } ;
53+ let run : Run ;
54+ let wrongSubject : boolean ;
55+ try {
56+ run = JSON . parse ( String ( row . payload ) ) as Run ;
57+ if (
58+ ! matchesIndexedRun ( run , row , id ) ||
59+ typeof run . metadata !== 'object' ||
60+ Array . isArray ( run . metadata ) ||
61+ Object . values ( run . metadata ) . some ( ( value ) => typeof value !== 'string' ) ||
62+ typeof run . input !== 'string' ||
63+ typeof run . route . id !== 'string' ||
64+ typeof run . route . model !== 'string' ||
65+ ! Array . isArray ( run . fileIds ) ||
66+ ! Array . isArray ( run . artifactIds ) ||
67+ ( run . checkpoint . keyId !== undefined && typeof run . checkpoint . keyId !== 'string' ) ||
68+ ( run . checkpoint . parentStepId !== undefined &&
69+ typeof run . checkpoint . parentStepId !== 'string' ) ||
70+ ( run . checkpoint . suspension && ! Array . isArray ( run . checkpoint . suspension . approvalIds ) )
71+ )
72+ return quarantine ( ) ;
73+ wrongSubject = ! matchesSubject ( run , subject ) ;
74+ if ( wrongSubject && ( ! cancelMismatchedChild || run . status !== 'queued' ) ) return quarantine ( ) ;
75+ const projection = project ( id , run . owner ) ;
76+ run . subject = subject ;
77+ run . output = projection . messages . map ( ( m ) => ( { ...m } ) ) ;
78+ run . updatedAt = Math . max ( run . updatedAt , projection . updatedAt ) ;
79+ } catch ( error ) {
80+ if (
81+ error instanceof SyntaxError ||
82+ ( error instanceof Fault &&
83+ ( error . code === 'invalid_output_event' || error . code === 'run_not_found' ) )
84+ )
85+ return quarantine ( ) ;
86+ throw error ;
87+ }
88+ if ( ! wrongSubject ) return run ;
89+ if ( ! cancelMismatchedChild ?.( run ) ) return quarantine ( ) ;
90+ return undefined ;
91+ }
92+
2293export function readRun ( store : Reader , id : string , project : Project , scope : Scope ) : Run ;
2394export function readRun (
2495 store : Reader ,
@@ -63,17 +134,8 @@ export function readRun(
63134 const subject = String ( row . subject ?? LEGACY_SUBJECT ) ;
64135 if (
65136 admit &&
66- ( ! run ?. checkpoint ||
67- ! Array . isArray ( run . checkpoint . messages ) ||
68- ! run . metadata ||
69- ! run . route ||
70- typeof run . digest !== 'string' ||
71- run . id !== id ||
72- run . owner !== row . owner ||
73- run . status !== row . status ||
74- run . agentId !== row . agent_id ||
75- run . sessionId !== row . session_id ||
76- ( run . subject != null && run . subject !== subject ) ||
137+ ( ! matchesIndexedRun ( run , row , id ) ||
138+ ! matchesSubject ( run , subject ) ||
77139 typeof row . summary !== 'string' )
78140 ) {
79141 store . terminalPublications . detachedRun ( id , boundary ) ;
0 commit comments