@@ -1032,6 +1032,7 @@ type ShellThreadRow = {
10321032 readonly latest_run_started_at : string | null ;
10331033 readonly latest_run_completed_at : string | null ;
10341034 readonly latest_run_stop_requested : number ;
1035+ readonly latest_run_background_wake : number ;
10351036 readonly active_run_id : string | null ;
10361037 readonly active_run_status : string | null ;
10371038 readonly activity_run_status : string | null ;
@@ -1043,6 +1044,7 @@ type ShellThreadRow = {
10431044 readonly blocking_run_started_at : string | null ;
10441045 readonly blocking_run_completed_at : string | null ;
10451046 readonly blocking_run_stop_requested : number ;
1047+ readonly blocking_run_background_wake : number ;
10461048 readonly blocking_failure_payload_json : string | null ;
10471049 readonly pending_request_payload_json : string | null ;
10481050 readonly pending_secret_request_payload_json : string | null ;
@@ -1658,6 +1660,8 @@ export function threadShellFromProjection(
16581660 projection . turnItems . some (
16591661 ( item ) => item . runId === latestRun . id && item . type === "run_interrupt_request" ,
16601662 ) ,
1663+ latestRunBackgroundWake :
1664+ latestRun != null && isBackgroundWakeRunForShell ( latestRun , projection ) ,
16611665 activeRunId : activeRun ?. id ?? null ,
16621666 blockedOnThreadWait : deriveBlockedOnThreadWait ( { activeRun, turnItems : projection . turnItems } ) ,
16631667 activityRunStatus : activityRun ?. status ?? null ,
@@ -1753,6 +1757,25 @@ function isSilentRunForShell(
17531757 ) ;
17541758}
17551759
1760+ const BACKGROUND_WAKE_SOURCE_KINDS = new Set ( [ "background_task" , "command" , "monitor" ] ) ;
1761+
1762+ function isBackgroundWakeRunForShell (
1763+ run : OrchestrationV2ThreadProjection [ "runs" ] [ number ] ,
1764+ projection : Pick < OrchestrationV2ThreadProjection , "messages" > ,
1765+ ) : boolean {
1766+ const message = projection . messages . find ( ( candidate ) => candidate . id === run . userMessageId ) ;
1767+ return (
1768+ message ?. delegatedCompletion === undefined &&
1769+ // Only progress wakes; a terminal one ("Command … finished") is the work reporting its end.
1770+ message ?. notification ?. outcome === "updated" &&
1771+ BACKGROUND_WAKE_SOURCE_KINDS . has ( message . notification . source . kind ) &&
1772+ // A follow-up the user steered into the wake makes the run theirs.
1773+ ! projection . messages . some (
1774+ ( candidate ) => candidate . runId === run . id && candidate . createdBy === "user" ,
1775+ )
1776+ ) ;
1777+ }
1778+
17561779function isInterruptibleRunForShell ( run : OrchestrationV2ThreadProjection [ "runs" ] [ number ] ) : boolean {
17571780 return run . status === "preparing" || run . status === "starting" || run . status === "running" ;
17581781}
@@ -1775,6 +1798,7 @@ type ShellThreadState = {
17751798 readonly latestRunStartedAt : DateTime . Utc | null ;
17761799 readonly latestRunCompletedAt : DateTime . Utc | null ;
17771800 readonly latestRunStopRequested : boolean ;
1801+ readonly latestRunBackgroundWake : boolean ;
17781802 readonly activeRunId : RunId | null ;
17791803 readonly blockedOnThreadWait : boolean ;
17801804 readonly activityRunStatus : ShellActivityRunStatus | null ;
@@ -1935,6 +1959,7 @@ function shellFromState(input: {
19351959 latestRunStartedAt : input . state . latestRunStartedAt ,
19361960 latestRunCompletedAt : input . state . latestRunCompletedAt ,
19371961 latestRunStopRequested : input . state . latestRunStopRequested ,
1962+ latestRunBackgroundWake : input . state . latestRunBackgroundWake ,
19381963 activeRunId : input . state . activeRunId ,
19391964 blockedOnThreadWait : input . state . blockedOnThreadWait ,
19401965 activityRunStatus : input . state . activityRunStatus ,
@@ -6267,6 +6292,25 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62676292 LIMIT 1
62686293 )` ;
62696294
6295+ // Mirrors isBackgroundWakeRunForShell. Stored sources keep the legacy encoding, where a
6296+ // command is background_command and a subagent is background_task with work "subagent".
6297+ const isBackgroundWakeShellRun = ( alias : string ) => {
6298+ const run = sql . literal ( alias ) ;
6299+ return sql `EXISTS (
6300+ SELECT 1 FROM orchestration_v2_projection_messages wake
6301+ WHERE wake.message_id = json_extract(${ run } .payload_json, '$.userMessageId')
6302+ AND json_extract(wake.payload_json, '$.notification.source.kind')
6303+ IN ('background_task', 'background_command', 'command', 'monitor')
6304+ AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL
6305+ AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated'
6306+ AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL
6307+ ) AND NOT EXISTS (
6308+ SELECT 1 FROM orchestration_v2_projection_messages steer
6309+ WHERE steer.run_id = ${ run } .run_id
6310+ AND json_extract(steer.payload_json, '$.createdBy') = 'user'
6311+ )` ;
6312+ } ;
6313+
62706314 const selectShellThreadRows = (
62716315 threadId ?: ThreadId ,
62726316 {
@@ -6299,6 +6343,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62996343 WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id
63006344 AND stop_request.type = 'run_interrupt_request'
63016345 ) AS latest_run_stop_requested,
6346+ ${ isBackgroundWakeShellRun ( "presented" ) } AS latest_run_background_wake,
63026347 active.run_id AS active_run_id,
63036348 active.status AS active_run_status,
63046349 (
@@ -6356,6 +6401,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
63566401 WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id
63576402 AND stop_request.type = 'run_interrupt_request'
63586403 ) AS blocking_run_stop_requested,
6404+ ${ isBackgroundWakeShellRun ( "blocked" ) } AS blocking_run_background_wake,
63596405 (
63606406 SELECT item.payload_json
63616407 FROM orchestration_v2_projection_turn_items item
@@ -6928,6 +6974,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
69286974 ? null
69296975 : DateTime . makeUnsafe ( row . latest_run_completed_at ) ;
69306976 let latestRunStopRequested = row . latest_run_stop_requested === 1 ;
6977+ let latestRunBackgroundWake = row . latest_run_background_wake === 1 ;
69316978 if ( row . blocking_run_id !== null && row . blocking_failure_payload_json !== null ) {
69326979 const blockingFailure = yield * decodeTurnItemPayload (
69336980 row . blocking_failure_payload_json ,
@@ -6941,6 +6988,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
69416988 terminalFailureItem = blockingFailure ;
69426989 latestRunId = RunId . make ( row . blocking_run_id ) ;
69436990 latestRunStopRequested = row . blocking_run_stop_requested === 1 ;
6991+ latestRunBackgroundWake = row . blocking_run_background_wake === 1 ;
69446992 latestRunStatus = "failed" ;
69456993 latestRunRequestedAt =
69466994 row . blocking_run_requested_at === null
@@ -6981,6 +7029,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
69817029 latestRunStartedAt,
69827030 latestRunCompletedAt,
69837031 latestRunStopRequested,
7032+ latestRunBackgroundWake,
69847033 activeRunId : row . active_run_id === null ? null : RunId . make ( row . active_run_id ) ,
69857034 blockedOnThreadWait : deriveBlockedOnThreadWait ( {
69867035 activeRun :
@@ -7251,6 +7300,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
72517300 activityRunStatus : state . activityRunStatus ,
72527301 latestRunCompletedAt : state . latestRunCompletedAt ,
72537302 latestRunStopRequested : state . latestRunStopRequested ,
7303+ latestRunBackgroundWake : state . latestRunBackgroundWake ,
72547304 pendingRuntimeRequest : state . pendingRuntimeRequest ,
72557305 pendingBackgroundTasks : state . pendingBackgroundTasks ,
72567306 } ) ) ,
0 commit comments