@@ -1025,6 +1025,7 @@ type ShellThreadRow = {
10251025 readonly latest_run_started_at : string | null ;
10261026 readonly latest_run_completed_at : string | null ;
10271027 readonly latest_run_stop_requested : number ;
1028+ readonly latest_run_background_wake : number ;
10281029 readonly active_run_id : string | null ;
10291030 readonly active_run_status : string | null ;
10301031 readonly activity_run_status : string | null ;
@@ -1036,6 +1037,7 @@ type ShellThreadRow = {
10361037 readonly blocking_run_started_at : string | null ;
10371038 readonly blocking_run_completed_at : string | null ;
10381039 readonly blocking_run_stop_requested : number ;
1040+ readonly blocking_run_background_wake : number ;
10391041 readonly blocking_failure_payload_json : string | null ;
10401042 readonly pending_request_payload_json : string | null ;
10411043 readonly pending_secret_request_payload_json : string | null ;
@@ -1651,6 +1653,8 @@ export function threadShellFromProjection(
16511653 projection . turnItems . some (
16521654 ( item ) => item . runId === latestRun . id && item . type === "run_interrupt_request" ,
16531655 ) ,
1656+ latestRunBackgroundWake :
1657+ latestRun != null && isBackgroundWakeRunForShell ( latestRun , projection ) ,
16541658 activeRunId : activeRun ?. id ?? null ,
16551659 blockedOnThreadWait : deriveBlockedOnThreadWait ( { activeRun, turnItems : projection . turnItems } ) ,
16561660 activityRunStatus : activityRun ?. status ?? null ,
@@ -1746,6 +1750,23 @@ function isSilentRunForShell(
17461750 ) ;
17471751}
17481752
1753+ const BACKGROUND_WAKE_SOURCE_KINDS = new Set ( [ "background_task" , "command" , "monitor" ] ) ;
1754+
1755+ function isBackgroundWakeRunForShell (
1756+ run : OrchestrationV2ThreadProjection [ "runs" ] [ number ] ,
1757+ projection : Pick < OrchestrationV2ThreadProjection , "messages" > ,
1758+ ) : boolean {
1759+ const message = projection . messages . find ( ( candidate ) => candidate . id === run . userMessageId ) ;
1760+ return (
1761+ message ?. delegatedCompletion === undefined &&
1762+ BACKGROUND_WAKE_SOURCE_KINDS . has ( message ?. notification ?. source . kind ?? "" ) &&
1763+ // A follow-up the user steered into the wake makes the run theirs.
1764+ ! projection . messages . some (
1765+ ( candidate ) => candidate . runId === run . id && candidate . createdBy === "user" ,
1766+ )
1767+ ) ;
1768+ }
1769+
17491770function isInterruptibleRunForShell ( run : OrchestrationV2ThreadProjection [ "runs" ] [ number ] ) : boolean {
17501771 return run . status === "preparing" || run . status === "starting" || run . status === "running" ;
17511772}
@@ -1768,6 +1789,7 @@ type ShellThreadState = {
17681789 readonly latestRunStartedAt : DateTime . Utc | null ;
17691790 readonly latestRunCompletedAt : DateTime . Utc | null ;
17701791 readonly latestRunStopRequested : boolean ;
1792+ readonly latestRunBackgroundWake : boolean ;
17711793 readonly activeRunId : RunId | null ;
17721794 readonly blockedOnThreadWait : boolean ;
17731795 readonly activityRunStatus : ShellActivityRunStatus | null ;
@@ -1928,6 +1950,7 @@ function shellFromState(input: {
19281950 latestRunStartedAt : input . state . latestRunStartedAt ,
19291951 latestRunCompletedAt : input . state . latestRunCompletedAt ,
19301952 latestRunStopRequested : input . state . latestRunStopRequested ,
1953+ latestRunBackgroundWake : input . state . latestRunBackgroundWake ,
19311954 activeRunId : input . state . activeRunId ,
19321955 blockedOnThreadWait : input . state . blockedOnThreadWait ,
19331956 activityRunStatus : input . state . activityRunStatus ,
@@ -6188,6 +6211,24 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61886211 )` ;
61896212 } ;
61906213
6214+ // Mirrors isBackgroundWakeRunForShell. Stored sources keep the legacy encoding, where a
6215+ // command is background_command and a subagent is background_task with work "subagent".
6216+ const isBackgroundWakeShellRun = ( alias : string ) => {
6217+ const run = sql . literal ( alias ) ;
6218+ return sql `EXISTS (
6219+ SELECT 1 FROM orchestration_v2_projection_messages wake
6220+ WHERE wake.message_id = json_extract(${ run } .payload_json, '$.userMessageId')
6221+ AND json_extract(wake.payload_json, '$.notification.source.kind')
6222+ IN ('background_task', 'background_command', 'command', 'monitor')
6223+ AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL
6224+ AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL
6225+ ) AND NOT EXISTS (
6226+ SELECT 1 FROM orchestration_v2_projection_messages steer
6227+ WHERE steer.run_id = ${ run } .run_id
6228+ AND json_extract(steer.payload_json, '$.createdBy') = 'user'
6229+ )` ;
6230+ } ;
6231+
61916232 const selectShellThreadRows = (
61926233 threadId ?: ThreadId ,
61936234 {
@@ -6220,6 +6261,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62206261 WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id
62216262 AND stop_request.type = 'run_interrupt_request'
62226263 ) AS latest_run_stop_requested,
6264+ ${ isBackgroundWakeShellRun ( "presented" ) } AS latest_run_background_wake,
62236265 active.run_id AS active_run_id,
62246266 active.status AS active_run_status,
62256267 (
@@ -6277,6 +6319,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62776319 WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id
62786320 AND stop_request.type = 'run_interrupt_request'
62796321 ) AS blocking_run_stop_requested,
6322+ ${ isBackgroundWakeShellRun ( "blocked" ) } AS blocking_run_background_wake,
62806323 (
62816324 SELECT item.payload_json
62826325 FROM orchestration_v2_projection_turn_items item
@@ -6860,6 +6903,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
68606903 ? null
68616904 : DateTime . makeUnsafe ( row . latest_run_completed_at ) ;
68626905 let latestRunStopRequested = row . latest_run_stop_requested === 1 ;
6906+ let latestRunBackgroundWake = row . latest_run_background_wake === 1 ;
68636907 if ( row . blocking_run_id !== null && row . blocking_failure_payload_json !== null ) {
68646908 const blockingFailure = yield * decodeTurnItemPayload (
68656909 row . blocking_failure_payload_json ,
@@ -6873,6 +6917,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
68736917 terminalFailureItem = blockingFailure ;
68746918 latestRunId = RunId . make ( row . blocking_run_id ) ;
68756919 latestRunStopRequested = row . blocking_run_stop_requested === 1 ;
6920+ latestRunBackgroundWake = row . blocking_run_background_wake === 1 ;
68766921 latestRunStatus = "failed" ;
68776922 latestRunRequestedAt =
68786923 row . blocking_run_requested_at === null
@@ -6913,6 +6958,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
69136958 latestRunStartedAt,
69146959 latestRunCompletedAt,
69156960 latestRunStopRequested,
6961+ latestRunBackgroundWake,
69166962 activeRunId : row . active_run_id === null ? null : RunId . make ( row . active_run_id ) ,
69176963 blockedOnThreadWait : deriveBlockedOnThreadWait ( {
69186964 activeRun :
@@ -7182,6 +7228,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
71827228 activityRunStatus : state . activityRunStatus ,
71837229 latestRunCompletedAt : state . latestRunCompletedAt ,
71847230 latestRunStopRequested : state . latestRunStopRequested ,
7231+ latestRunBackgroundWake : state . latestRunBackgroundWake ,
71857232 pendingRuntimeRequest : state . pendingRuntimeRequest ,
71867233 pendingBackgroundTasks : state . pendingBackgroundTasks ,
71877234 } ) ) ,
0 commit comments