@wjarka/cezarion 0.14.6 → 0.14.7-pr488.741

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/README.md +37 -2
  2. package/dist/agent-config/model-settings/shared.d.ts +2 -1
  3. package/dist/agent-config/model-settings/shared.js +1 -1
  4. package/dist/agent-config/model-settings/shared.js.map +1 -1
  5. package/dist/ci-wait/client.d.ts +4 -0
  6. package/dist/ci-wait/client.js +67 -0
  7. package/dist/ci-wait/client.js.map +1 -0
  8. package/dist/ci-wait/controller.d.ts +78 -0
  9. package/dist/ci-wait/controller.js +148 -0
  10. package/dist/ci-wait/controller.js.map +1 -0
  11. package/dist/ci-wait/github.d.ts +61 -0
  12. package/dist/ci-wait/github.js +209 -0
  13. package/dist/ci-wait/github.js.map +1 -0
  14. package/dist/ci-wait/injection.d.ts +3 -0
  15. package/dist/ci-wait/injection.js +26 -0
  16. package/dist/ci-wait/injection.js.map +1 -0
  17. package/dist/ci-wait/mcp.d.ts +97 -0
  18. package/dist/ci-wait/mcp.js +35 -0
  19. package/dist/ci-wait/mcp.js.map +1 -0
  20. package/dist/ci-wait/resources.d.ts +7 -0
  21. package/dist/ci-wait/resources.js +32 -0
  22. package/dist/ci-wait/resources.js.map +1 -0
  23. package/dist/ci-wait/supervisor.d.ts +21 -0
  24. package/dist/ci-wait/supervisor.js +163 -0
  25. package/dist/ci-wait/supervisor.js.map +1 -0
  26. package/dist/contract/ci-wait.d.ts +151 -0
  27. package/dist/contract/conversations.d.ts +105 -0
  28. package/dist/contract/delegation.d.ts +17 -0
  29. package/dist/contract/events.d.ts +10 -0
  30. package/dist/contract/index.d.ts +1 -0
  31. package/dist/contract/index.js +1046 -936
  32. package/dist/contract/runs.d.ts +496 -0
  33. package/dist/contract/skills.d.ts +99 -0
  34. package/dist/core/agent-env.js +2 -0
  35. package/dist/core/agent-env.js.map +1 -1
  36. package/dist/core/agent-runner.d.ts +6 -0
  37. package/dist/core/agent-runner.js +1 -0
  38. package/dist/core/agent-runner.js.map +1 -1
  39. package/dist/core/claude-cli-runner.js +10 -0
  40. package/dist/core/claude-cli-runner.js.map +1 -1
  41. package/dist/core/codex-app-server-runner.js +5 -1
  42. package/dist/core/codex-app-server-runner.js.map +1 -1
  43. package/dist/core/codex-app-server-transport.js +3 -1
  44. package/dist/core/codex-app-server-transport.js.map +1 -1
  45. package/dist/core/cursor-acp-runner.js +9 -1
  46. package/dist/core/cursor-acp-runner.js.map +1 -1
  47. package/dist/core/opencode-server-runner.js +6 -2
  48. package/dist/core/opencode-server-runner.js.map +1 -1
  49. package/dist/core/pi-runner.js +7 -1
  50. package/dist/core/pi-runner.js.map +1 -1
  51. package/dist/delegation/cli.js +110 -10
  52. package/dist/delegation/cli.js.map +1 -1
  53. package/dist/delegation/conversations.js +2 -1
  54. package/dist/delegation/conversations.js.map +1 -1
  55. package/dist/delegation/input.d.ts +7 -0
  56. package/dist/delegation/input.js +27 -0
  57. package/dist/delegation/input.js.map +1 -1
  58. package/dist/delegation/provision.js +4 -1
  59. package/dist/delegation/provision.js.map +1 -1
  60. package/dist/delegation/routes.d.ts +78 -0
  61. package/dist/delegation/routes.js +4 -1
  62. package/dist/delegation/routes.js.map +1 -1
  63. package/dist/delegation/service.d.ts +6 -2
  64. package/dist/delegation/service.js +113 -9
  65. package/dist/delegation/service.js.map +1 -1
  66. package/dist/delegation/transport.d.ts +78 -0
  67. package/dist/runs/delegation-state.d.ts +2 -0
  68. package/dist/runs/run-index.js +2 -3
  69. package/dist/runs/run-index.js.map +1 -1
  70. package/dist/runs/store.d.ts +534 -3
  71. package/dist/runs/store.js +186 -6
  72. package/dist/runs/store.js.map +1 -1
  73. package/dist/server/server.d.ts +1530 -12
  74. package/dist/workflows/run.d.ts +39 -1
  75. package/dist/workflows/run.js +606 -50
  76. package/dist/workflows/run.js.map +1 -1
  77. package/package.json +4 -3
  78. package/scripts/mock-ci-tool.mjs +53 -0
  79. package/scripts/mock-claude.mjs +8 -0
  80. package/scripts/mock-codex-app-server.mjs +212 -0
  81. package/scripts/mock-cursor-acp.mjs +6 -2
  82. package/scripts/mock-opencode-serve.mjs +392 -0
  83. package/scripts/mock-pi-rpc.mjs +7 -0
  84. package/scripts/pi-ci-wait.mjs +14 -0
  85. package/web/dist/assets/{agent-accounts-lP_xK83o.js → agent-accounts-DveiLHoy.js} +1 -1
  86. package/web/dist/assets/{alert-dialog-CmDuefYh.js → alert-dialog-BRmsCA3M.js} +1 -1
  87. package/web/dist/assets/{collapsible-BqX8REmY.js → collapsible-BWfM2PdZ.js} +1 -1
  88. package/web/dist/assets/{commit-list-BWvv1H7A.js → commit-list-C-rCgcKm.js} +1 -1
  89. package/web/dist/assets/{compare-variants-Bg7eLW3z.js → compare-variants-Ccp9IYp-.js} +1 -1
  90. package/web/dist/assets/{diff-0GBum40Q2.js → diff-BvZErqfg2.js} +2 -2
  91. package/web/dist/assets/{diff-controls-HwWHHait.js → diff-controls-BFI8ngtw.js} +1 -1
  92. package/web/dist/assets/{diff-stat-mNkhAc5m.js → diff-stat-CVFxf2Ar.js} +1 -1
  93. package/web/dist/assets/{diff-view-DW64doDY.js → diff-view-B35r8kQK.js} +1 -1
  94. package/web/dist/assets/{dropdown-menu-sZjYPcLh.js → dropdown-menu-C_fj6kzP.js} +1 -1
  95. package/web/dist/assets/{git-toolbar-DBW5EIcY.js → git-toolbar-BaqAvRC1.js} +1 -1
  96. package/web/dist/assets/{github-CkZ4hWI5.js → github-DIkBazPA.js} +1 -1
  97. package/web/dist/assets/{image-preview-DZicGNkU.js → image-preview-BBqbaKaq.js} +1 -1
  98. package/web/dist/assets/{index-DWWI-B9n.js → index-BIae70yp.js} +3 -3
  99. package/web/dist/assets/index-CXZukB1O.css +1 -0
  100. package/web/dist/assets/{markdown-AY2K3kIV.js → markdown-kouQNPB6.js} +1 -1
  101. package/web/dist/assets/{pill-CcJqA1cL.js → pill-mHX_rKXP.js} +1 -1
  102. package/web/dist/assets/{project-router-SwsXRKXs.js → project-router-Ds9EvxjN.js} +1 -1
  103. package/web/dist/assets/{prompt-templates-iV2WD2jy.js → prompt-templates-AaKKdIiY.js} +1 -1
  104. package/web/dist/assets/{repo-git-BW3TOs3U.js → repo-git-6StCwqhj.js} +1 -1
  105. package/web/dist/assets/{run-diff-DPUNj56q.js → run-diff-fChfUdwr.js} +1 -1
  106. package/web/dist/assets/run-header-B3ZPhDZr.js +1 -0
  107. package/web/dist/assets/{run-header-CSaJIGXl.css → run-header-BJQfFL2l.css} +1 -1
  108. package/web/dist/assets/{skills-Dv8Vwv5D.js → skills-DSWaVvcu.js} +1 -1
  109. package/web/dist/assets/{tab-link-DSftvkZS.js → tab-link-Db6yDTIQ.js} +1 -1
  110. package/web/dist/assets/{task-changes-CTCuVls1.js → task-changes-DdmerESW.js} +1 -1
  111. package/web/dist/assets/{task-commits-weJQ5GSl.js → task-commits-Co-bb1P6.js} +1 -1
  112. package/web/dist/assets/{task-files-B4bUimY8.js → task-files-B2KASfyL.js} +1 -1
  113. package/web/dist/assets/{task-thread-BuXrVB9K.js → task-thread-8kGLJO4Q.js} +4 -4
  114. package/web/dist/assets/task-thread-Dc3wplva.css +1 -0
  115. package/web/dist/assets/{textarea-CxRdTWlq.js → textarea-Qhdzn_oU.js} +1 -1
  116. package/web/dist/assets/thread-loading-C6ENzZYk.js +1 -0
  117. package/web/dist/assets/{token-metrics-Z3UFYbuD.js → token-metrics-BsT6Rqji.js} +1 -1
  118. package/web/dist/assets/utils-VFstALJW.js +64 -0
  119. package/web/dist/assets/{workflows-vA27Yi8W.js → workflows-UKoofNFJ.js} +1 -1
  120. package/web/dist/assets/{zoomable-image-DUE1KFJ0.js → zoomable-image-DpL5gtv4.js} +1 -1
  121. package/web/dist/index.html +14 -14
  122. package/web/dist/assets/index-DK0Mkv2m.css +0 -1
  123. package/web/dist/assets/run-header-DbDluWoV.js +0 -1
  124. package/web/dist/assets/task-thread-DMRD0IVt.css +0 -1
  125. package/web/dist/assets/thread-loading-JlP6dUpX.js +0 -1
  126. package/web/dist/assets/utils-CUnaAifW.js +0 -64
@@ -1,3 +1,5 @@
1
+ import { ciWaitRequestSchema, ciWaitResultSchema } from '../contract/index.js';
2
+ import { acquireCiResources } from '../ci-wait/resources.js';
1
3
  import { workerContextHash, workerWorkflowHash, workerStepIdentity, captureWorkerAccount, boundWorkerAccountEnv, WorkerIdentityError } from '../delegation/execution-identity.js';
2
4
  import { buildChildEnv } from '../core/agent-env.js';
3
5
  import { randomUUID } from 'node:crypto';
@@ -27,7 +29,7 @@ import { autosaveCommit, createWorktree, resolveBaseRef, worktreeDiff, worktreeS
27
29
  import { getHeadCommit, getRepoInfo } from '../server/git.js';
28
30
  import { ensureOwnedWorkspace, verifyOwnedWorkspace } from '../delegation/workspace.js';
29
31
  import { verifyWorkerContext } from '../delegation/context.js';
30
- import { enqueueAgentInput, nextAgentInput } from '../delegation/input.js';
32
+ import { enqueueAgentInput, agentInputBatch, hasLiveInboxClaim } from '../delegation/input.js';
31
33
  import { parseDelegationEffort } from '../delegation/effort.js';
32
34
  import { DelegationPolicyError } from '../delegation/policy.js';
33
35
  import { reconcileWorkerWait } from '../delegation/wait.js';
@@ -477,6 +479,14 @@ export class RunManager {
477
479
  if (this.executions.get(runId) === execution)
478
480
  this.executions.delete(runId);
479
481
  execution.resolve();
482
+ if (!this.disposed) {
483
+ try {
484
+ this.queueCiWake(runId);
485
+ }
486
+ catch {
487
+ this.retryCiAdmission(runId);
488
+ }
489
+ }
480
490
  if (!this.disposed && this.store.getRun(runId)?.stopping)
481
491
  this.store.updateRun(runId, { stopping: undefined });
482
492
  if (!this.disposed)
@@ -572,14 +582,24 @@ export class RunManager {
572
582
  /** Durable monitoring subset. Only the configured number receives the waiting-slot exemption. */
573
583
  monitoring = new Set();
574
584
  /** Separate from human waiting: these resumes MUST reacquire scheduler capacity. */
585
+ ciWakeQueuedAt = new Map();
586
+ ciWakeAdmitted = new Set();
587
+ ciWatches = new Map();
588
+ ciRegistrations = new Map();
589
+ ciRetries = new Map();
590
+ ciResources;
591
+ ciSupervisor;
575
592
  workerWaiting = new Set();
576
593
  workerWakeAdmitted = new Set();
577
594
  workerWakeQueuedAt = new Map();
578
595
  workerWaitTimers = new Map();
596
+ inboxClaimTimers = new Map();
579
597
  reconcilingWorkers = false;
580
598
  recovering = false;
581
599
  disposed = false;
582
600
  onDelegationRun = (run) => {
601
+ if (!this.disposed && !['queued', 'running', 'waiting'].includes(run.status))
602
+ this.withdrawCiWait(run.id);
583
603
  if (run.delegation && !['queued', 'running', 'waiting'].includes(run.status))
584
604
  this.active.get(run.id)?.revokeDelegation?.();
585
605
  if (this.disposed || this.recovering || this.reconcilingWorkers)
@@ -647,6 +667,8 @@ export class RunManager {
647
667
  this.repoRoot = repoRoot;
648
668
  this.dataDir = join(repoRoot, '.ai/cezar');
649
669
  this.semaphore = options.semaphore ?? new WorkspaceSemaphore();
670
+ this.ciResources = acquireCiResources(this.semaphore);
671
+ this.ciSupervisor = this.ciResources.supervisor;
650
672
  this.offSemaphore = this.semaphore.register({
651
673
  busySlots: () => this.busySlots(),
652
674
  pump: () => this.pump(),
@@ -672,6 +694,16 @@ export class RunManager {
672
694
  */
673
695
  dispose() {
674
696
  this.disposed = true;
697
+ for (const task of this.ciWatches.values())
698
+ task.abort.abort();
699
+ this.ciWatches.clear();
700
+ for (const task of this.ciRegistrations.values())
701
+ task.abort.abort();
702
+ this.ciRegistrations.clear();
703
+ for (const timer of this.ciRetries.values())
704
+ clearTimeout(timer);
705
+ this.ciRetries.clear();
706
+ this.ciResources.release();
675
707
  this.delegationProvisioner = undefined;
676
708
  this.finalizedWorkers.clear();
677
709
  for (const settle of this.terminationWaiters)
@@ -680,6 +712,9 @@ export class RunManager {
680
712
  for (const timer of this.workerWaitTimers.values())
681
713
  clearTimeout(timer);
682
714
  this.workerWaitTimers.clear();
715
+ for (const timer of this.inboxClaimTimers.values())
716
+ clearTimeout(timer);
717
+ this.inboxClaimTimers.clear();
683
718
  this.workerWaiting.clear();
684
719
  this.workerWakeAdmitted.clear();
685
720
  this.workerWakeQueuedAt.clear();
@@ -687,6 +722,8 @@ export class RunManager {
687
722
  this.offSemaphore();
688
723
  clearInterval(this.queueWatchdog);
689
724
  for (const [runId, state] of this.active) {
725
+ state.revokeCiTools?.();
726
+ state.revokeCiTools = undefined;
690
727
  state.revokeDelegation?.();
691
728
  state.revokeDelegation = undefined;
692
729
  this.clearIdleTimer(state);
@@ -917,6 +954,196 @@ export class RunManager {
917
954
  state.revokeDelegation = session?.revoke;
918
955
  return session;
919
956
  }
957
+ /** Provision the same capability and descriptor on every start, Continue and recovered launch. */
958
+ async provisionCiSession(runId, state) {
959
+ state.revokeCiTools?.();
960
+ state.revokeCiTools = undefined;
961
+ state.ciGeneration = randomUUID();
962
+ state.ciTurnId = randomUUID();
963
+ const generation = state.ciGeneration;
964
+ const controller = await this.ciResources.controller();
965
+ if (!controller || this.disposed || state.cancelled || this.active.get(runId) !== state)
966
+ return;
967
+ const provisioned = controller.provision((request, signal) => this.registerCiWait(runId, request, generation, signal));
968
+ state.revokeCiTools = provisioned.revoke;
969
+ return provisioned;
970
+ }
971
+ /** Trusted capability callbacks supply run identity; the model supplies only a PR and deadline. */
972
+ async registerCiWait(runId, request, generation, capabilitySignal) {
973
+ const parsed = ciWaitRequestSchema.parse(request);
974
+ const state = this.active.get(runId);
975
+ const authorized = () => {
976
+ const run = this.store.getRun(runId);
977
+ return !this.disposed && !capabilitySignal?.aborted && !!run && ['running', 'waiting'].includes(run.status) && !run.stopping &&
978
+ this.active.get(runId) === state && !!state?.session?.open && !state.cancelled && !state.finishRequested &&
979
+ state.ciGeneration === generation && !state.pendingHumanAsk && !this.hasUnansweredHumanAsk(runId) &&
980
+ !this.workerWait(runId) && !this.workerExecutionStopped(runId) && !this.executionBlockedByRootFinish(run);
981
+ };
982
+ if (!authorized() || !state)
983
+ throw Object.assign(new Error('Run cannot register a CI wait in its current state'), { code: 'unauthorized' });
984
+ const current = this.store.getRun(runId)?.ciWait;
985
+ if (current) {
986
+ if (current.prUrl.toLowerCase() === parsed.pr.replace(/\/$/, '').toLowerCase() && current.timeoutSeconds === parsed.timeout_seconds)
987
+ return current;
988
+ throw Object.assign(new Error('wait_conflict: a different CI wait is already registered'), { code: 'wait_conflict' });
989
+ }
990
+ const pending = this.ciRegistrations.get(runId);
991
+ if (pending) {
992
+ if (pending.pr === parsed.pr && pending.seconds === parsed.timeout_seconds)
993
+ return pending.promise;
994
+ throw Object.assign(new Error('wait_conflict: a different CI registration is already in progress'), { code: 'wait_conflict' });
995
+ }
996
+ const turnId = state.ciTurnId ??= randomUUID();
997
+ const abort = new AbortController();
998
+ const signal = capabilitySignal ? AbortSignal.any([abort.signal, capabilitySignal]) : abort.signal;
999
+ const promise = (async () => {
1000
+ const identity = await this.ciSupervisor.resolve(parsed.pr, signal);
1001
+ if (signal.aborted || !authorized() || state.ciTurnId !== turnId)
1002
+ throw Object.assign(new Error('Run cannot register a CI wait after interruption'), { code: 'unauthorized' });
1003
+ const registeredAt = new Date().toISOString();
1004
+ const wait = { ...identity, id: randomUUID(), generation, turnId, timeoutSeconds: parsed.timeout_seconds,
1005
+ registeredAt, deadline: new Date(Date.now() + parsed.timeout_seconds * 1000).toISOString(), phase: 'registered' };
1006
+ try {
1007
+ this.store.commitCiWait(runId, wait);
1008
+ }
1009
+ catch {
1010
+ throw Object.assign(new Error('CI registration could not be saved; retry when storage is writable'), { code: 'persistence' });
1011
+ }
1012
+ this.startCiWatch(runId, wait);
1013
+ if (state.atTurnBoundary === state.session || this.waiting.has(runId))
1014
+ this.parkCiWait(runId, state);
1015
+ return this.store.getRun(runId).ciWait ?? wait;
1016
+ })();
1017
+ this.ciRegistrations.set(runId, { abort, pr: parsed.pr, seconds: parsed.timeout_seconds, promise });
1018
+ try {
1019
+ return await promise;
1020
+ }
1021
+ finally {
1022
+ if (this.ciRegistrations.get(runId)?.abort === abort)
1023
+ this.ciRegistrations.delete(runId);
1024
+ }
1025
+ }
1026
+ startCiWatch(runId, wait) {
1027
+ if (this.disposed || this.ciWatches.has(runId) || wait.result)
1028
+ return;
1029
+ const abort = new AbortController();
1030
+ const watch = { id: wait.id, abort };
1031
+ this.ciWatches.set(runId, watch);
1032
+ const current = () => !this.disposed && !abort.signal.aborted && this.ciWatches.get(runId) === watch &&
1033
+ this.store.getRun(runId)?.ciWait?.id === wait.id;
1034
+ const settle = (result) => {
1035
+ if (!current())
1036
+ return;
1037
+ try {
1038
+ const latest = this.store.getRun(runId)?.ciWait;
1039
+ if (!latest || latest.id !== wait.id)
1040
+ return;
1041
+ const parsed = ciWaitResultSchema.safeParse(result);
1042
+ const validated = parsed.success ? parsed.data : { outcome: 'error', headSha: wait.headSha,
1043
+ observedAt: new Date().toISOString(), checks: [], totalChecks: 0, truncated: false,
1044
+ diagnostic: 'CI watcher returned an invalid observation; inspect the PR and register a new wait.' };
1045
+ const settled = { ...latest, phase: 'wake-pending', result: validated, wakeId: wait.id };
1046
+ const input = { id: wait.id, parentRunId: runId, source: 'lifecycle', createdAt: new Date().toISOString(),
1047
+ text: `CI wait observation ${wait.id} for ${wait.prUrl}. External data follows: ${JSON.stringify(validated)}. This observation is not merge approval or task completion. Reported checks may not include every expected workflow. Duplicate observations retain this ID.` };
1048
+ this.store.commitCiWait(runId, settled, input);
1049
+ this.ciWatches.delete(runId);
1050
+ try {
1051
+ this.queueCiWake(runId);
1052
+ }
1053
+ catch {
1054
+ this.retryCiAdmission(runId);
1055
+ }
1056
+ }
1057
+ catch {
1058
+ // A failed durable checkpoint may not drop the accepted intent or publish a false result.
1059
+ if (!current())
1060
+ return;
1061
+ console.warn('[cez] CI wait checkpoint unavailable; retaining result for retry.');
1062
+ const timer = setTimeout(() => { this.ciRetries.delete(runId); settle(result); }, 1000);
1063
+ timer.unref();
1064
+ this.ciRetries.set(runId, timer);
1065
+ }
1066
+ };
1067
+ void this.ciSupervisor.watch(wait, abort.signal).then(settle, () => settle({ outcome: 'error', headSha: wait.headSha,
1068
+ observedAt: new Date().toISOString(), checks: [], totalChecks: 0, truncated: false,
1069
+ diagnostic: 'CI watcher unavailable; inspect the pull request and retry the wait.' }));
1070
+ }
1071
+ retryCiAdmission(runId) {
1072
+ if (this.disposed || this.ciRetries.has(runId) || !this.store.getRun(runId)?.ciWait)
1073
+ return;
1074
+ const timer = setTimeout(() => {
1075
+ this.ciRetries.delete(runId);
1076
+ try {
1077
+ this.queueCiWake(runId);
1078
+ }
1079
+ catch {
1080
+ this.retryCiAdmission(runId);
1081
+ }
1082
+ }, 1000);
1083
+ timer.unref();
1084
+ this.ciRetries.set(runId, timer);
1085
+ }
1086
+ parkCiWait(runId, state) {
1087
+ const wait = this.store.getRun(runId)?.ciWait;
1088
+ if (!wait || !state.session?.open || state.cancelled || state.pendingHumanAsk || this.ciWakeAdmitted.has(runId))
1089
+ return false;
1090
+ if (wait.phase === 'registered')
1091
+ this.store.commitCiWait(runId, { ...wait, phase: 'parked' });
1092
+ this.clearIdleTimer(state);
1093
+ this.clearMonitoringWakeTimer(state, runId);
1094
+ this.waiting.add(runId);
1095
+ this.monitoring.add(runId);
1096
+ this.store.updateRun(runId, { status: 'running', activity: 'monitoring', monitoringWakeAt: undefined });
1097
+ if (state.currentStepId)
1098
+ this.store.updateStep(runId, state.currentStepId, { status: 'running' });
1099
+ this.queueCiWake(runId);
1100
+ this.releaseSlot();
1101
+ return true;
1102
+ }
1103
+ queueCiWake(runId) {
1104
+ const run = this.store.getRun(runId);
1105
+ const wait = run?.ciWait;
1106
+ if (this.disposed || this.recovering || !run || !wait?.result || !wait.wakeId || this.ciWakeQueuedAt.has(runId) ||
1107
+ this.ciWakeAdmitted.has(runId) || !['running', 'waiting', 'queued'].includes(run.status) || run.stopping ||
1108
+ this.hasPendingHumanAsk(runId) || this.workerExecutionStopped(runId) || this.executionBlockedByRootFinish(run))
1109
+ return;
1110
+ const state = this.active.get(runId);
1111
+ if (state) {
1112
+ if (!this.monitoring.has(runId) || state.atTurnBoundary !== state.session || state.pendingHumanAsk)
1113
+ return;
1114
+ this.ciWakeQueuedAt.set(runId, Date.now());
1115
+ if (!this.queue.includes(runId))
1116
+ this.queue.push(runId);
1117
+ }
1118
+ else if (!this.isActive(runId)) {
1119
+ // No live session: create a scheduler-owned continuation, never a direct provider call.
1120
+ this.store.updateRun(runId, { status: 'waiting', activity: undefined });
1121
+ const resumed = this.continueRun(runId, { text: 'Resume to receive the queued CI observation. End this opening turn so Cezar can deliver it; do not poll CI.' }, true);
1122
+ if (!resumed.ok) {
1123
+ this.store.appendEvent(runId, { type: 'note', message: `CI result retained; continuation unavailable: ${resumed.error ?? 'unknown'}` });
1124
+ return;
1125
+ }
1126
+ this.ciWakeQueuedAt.set(runId, Date.now());
1127
+ }
1128
+ this.releaseSlot();
1129
+ }
1130
+ withdrawCiWait(runId, message) {
1131
+ this.ciRegistrations.get(runId)?.abort.abort();
1132
+ this.ciRegistrations.delete(runId);
1133
+ this.store.commitCiWaitWithdrawal(runId, message);
1134
+ this.ciWatches.get(runId)?.abort.abort();
1135
+ this.ciWatches.delete(runId);
1136
+ const timer = this.ciRetries.get(runId);
1137
+ if (timer)
1138
+ clearTimeout(timer);
1139
+ this.ciRetries.delete(runId);
1140
+ if (this.ciWakeQueuedAt.delete(runId) && this.active.has(runId)) {
1141
+ const index = this.queue.indexOf(runId);
1142
+ if (index >= 0)
1143
+ this.queue.splice(index, 1);
1144
+ }
1145
+ this.ciWakeAdmitted.delete(runId);
1146
+ }
920
1147
  /** Snapshot of the actual active session, never guessed from partial run settings. */
921
1148
  delegationExecutionSettings(runId) {
922
1149
  const state = this.active.get(runId);
@@ -1117,7 +1344,7 @@ export class RunManager {
1117
1344
  const head = this.queue[0];
1118
1345
  if (!head)
1119
1346
  return null;
1120
- const wakeQueuedAt = this.workerWakeQueuedAt.get(head);
1347
+ const wakeQueuedAt = this.workerWakeQueuedAt.get(head) ?? this.ciWakeQueuedAt.get(head);
1121
1348
  if (wakeQueuedAt !== undefined)
1122
1349
  return wakeQueuedAt;
1123
1350
  const createdAt = this.store.getRun(head)?.createdAt;
@@ -1196,13 +1423,24 @@ export class RunManager {
1196
1423
  // Only pay for the config read when something is actually held: a queued record may name
1197
1424
  // no runner, and then the account it would use is the configured default.
1198
1425
  const defaultRunner = anyHold ? (await loadConfig(this.repoRoot)).defaultRunner : undefined;
1199
- while (this.queue.length > 0 && capacity()) {
1426
+ // Removing one monitor above the exemption cap needs no new slot: it already holds one.
1427
+ const chargedCiWake = (id) => this.ciWakeQueuedAt.has(id) && this.monitoring.has(id) &&
1428
+ this.monitoring.size > this.semaphore.maxMonitoringSessions();
1429
+ while (this.queue.length > 0) {
1200
1430
  // FIFO among the runs that CAN start; a held one keeps its place in the queue rather
1201
1431
  // than being dequeued and re-queued (which would churn its position and its record).
1202
1432
  const next = this.queue.findIndex((id) => {
1203
1433
  const queued = this.store.getRun(id);
1204
- return !queued || this.historyDeletionPending(id) || ((!this.executions.get(id)?.admitted || this.active.has(id)) && !this.executionBlockedByRootFinish(queued) &&
1205
- (!anyHold || !accountHeldFor(queued, holds, defaultRunner ?? 'claude')));
1434
+ // An inbox claim may arrive after a wake was queued but before admission.
1435
+ // Preserve its place and slot exemption until ACK/release/expiry re-pumps.
1436
+ if (this.workerWakeQueuedAt.has(id)) {
1437
+ const first = queued?.agentInputs?.find(input => !input.deliveredAt);
1438
+ const wake = queued?.agentInputs?.find(input => input.id === this.workerWait(id)?.wakeId);
1439
+ if ((first && hasLiveInboxClaim(first)) || (wake && hasLiveInboxClaim(wake)))
1440
+ return false;
1441
+ }
1442
+ return (capacity() || chargedCiWake(id)) && (!queued || this.historyDeletionPending(id) || ((!this.executions.get(id)?.admitted || this.active.has(id)) && !this.executionBlockedByRootFinish(queued) &&
1443
+ (!anyHold || !accountHeldFor(queued, holds, defaultRunner ?? 'claude'))));
1206
1444
  });
1207
1445
  if (next === -1)
1208
1446
  break; // every queued run has a durable reason to remain held
@@ -1226,6 +1464,19 @@ export class RunManager {
1226
1464
  const runId = this.queue.splice(next, 1)[0];
1227
1465
  if (!runId)
1228
1466
  break;
1467
+ if (this.ciWakeQueuedAt.has(runId) && this.active.has(runId)) {
1468
+ this.ciWakeQueuedAt.delete(runId);
1469
+ const state = this.active.get(runId);
1470
+ if (!state.pendingHumanAsk && this.store.getRun(runId)?.ciWait?.result && state.atTurnBoundary === state.session) {
1471
+ this.ciWakeAdmitted.add(runId);
1472
+ this.waiting.delete(runId);
1473
+ this.monitoring.delete(runId);
1474
+ this.flushAgentInputs(runId);
1475
+ }
1476
+ continue;
1477
+ }
1478
+ if (this.ciWakeQueuedAt.delete(runId))
1479
+ this.ciWakeAdmitted.add(runId);
1229
1480
  if (this.workerWakeQueuedAt.has(runId) && this.active.has(runId)) {
1230
1481
  this.workerWakeQueuedAt.delete(runId);
1231
1482
  const state = this.active.get(runId);
@@ -1428,6 +1679,10 @@ export class RunManager {
1428
1679
  return;
1429
1680
  this.recovering = true;
1430
1681
  try {
1682
+ for (const run of this.store.listRuns()) {
1683
+ this.store.clearExpiredInboxClaims(run.id, new Date().toISOString());
1684
+ this.armInboxClaimExpiry(run.id);
1685
+ }
1431
1686
  this.reconcileWorkerWaits();
1432
1687
  const live = this.store
1433
1688
  .listRuns()
@@ -1476,6 +1731,15 @@ export class RunManager {
1476
1731
  this.continueRun(run.id, {}, true);
1477
1732
  continue;
1478
1733
  }
1734
+ if (run.ciWait && !this.hasPendingHumanAsk(run.id)) {
1735
+ if (run.ciWait.phase === 'registered')
1736
+ this.store.commitCiWait(run.id, { ...run.ciWait, phase: 'parked' });
1737
+ this.store.updateRun(run.id, { status: 'running', activity: 'monitoring', monitoringWakeAt: undefined });
1738
+ this.startCiWatch(run.id, this.store.getRun(run.id).ciWait);
1739
+ continue;
1740
+ }
1741
+ if (run.ciWait)
1742
+ this.withdrawCiWait(run.id);
1479
1743
  if (run.status === 'queued') {
1480
1744
  await this.reviveQueuedRun(run, 'cezar restarted');
1481
1745
  continue;
@@ -1495,7 +1759,8 @@ export class RunManager {
1495
1759
  // runs. A worker reaches here only when it has neither a recoverable ask
1496
1760
  // nor a worker wait; preserving that record would strand both it and its
1497
1761
  // parent because workers cannot accept an ordinary inactive Continue.
1498
- if (run.status === 'waiting' && run.delegation?.role !== 'worker')
1762
+ if (run.status === 'waiting' && (run.delegation?.role !== 'worker' ||
1763
+ run.agentInputs?.some(input => !input.deliveredAt && hasLiveInboxClaim(input))))
1499
1764
  continue;
1500
1765
  if (run.status === 'waiting') {
1501
1766
  const finishedAt = new Date().toISOString();
@@ -1573,6 +1838,12 @@ export class RunManager {
1573
1838
  this.recovering = false;
1574
1839
  }
1575
1840
  this.reconcileWorkerWaits();
1841
+ for (const run of this.store.listRuns()) {
1842
+ if (run.ciWait && !['running', 'waiting', 'queued'].includes(run.status))
1843
+ this.withdrawCiWait(run.id);
1844
+ else if (run.ciWait)
1845
+ this.queueCiWake(run.id);
1846
+ }
1576
1847
  this.reconcileAutoResumes();
1577
1848
  void this.pump();
1578
1849
  }
@@ -1589,7 +1860,7 @@ export class RunManager {
1589
1860
  }
1590
1861
  isDisposedDelegatedRun(runId) {
1591
1862
  const role = this.store.getRun(runId)?.delegation?.role;
1592
- return this.disposed && (role === 'root' || role === 'worker');
1863
+ return this.disposed && (role === 'root' || role === 'worker' || !!this.store.getRun(runId)?.ciWait);
1593
1864
  }
1594
1865
  /** Shutdown is not completion. Explicit terminal controls retain their precedence. */
1595
1866
  preserveRunAfterDisposal(runId, state) {
@@ -1599,6 +1870,7 @@ export class RunManager {
1599
1870
  dropActive(runId) {
1600
1871
  const state = this.active.get(runId);
1601
1872
  state?.releaseRepoRoot?.();
1873
+ state?.revokeCiTools?.();
1602
1874
  if (state) {
1603
1875
  state.releaseRepoRoot = undefined;
1604
1876
  this.clearIdleTimer(state);
@@ -1608,9 +1880,17 @@ export class RunManager {
1608
1880
  this.monitoring.delete(runId);
1609
1881
  this.workerWaiting.delete(runId);
1610
1882
  this.workerWakeAdmitted.delete(runId);
1883
+ this.ciWakeAdmitted.delete(runId);
1884
+ if (this.ciWakeQueuedAt.delete(runId)) {
1885
+ const index = this.queue.indexOf(runId);
1886
+ if (index >= 0)
1887
+ this.queue.splice(index, 1);
1888
+ }
1611
1889
  if (state)
1612
1890
  this.clearMonitoringWakeTimer(state, runId);
1613
1891
  this.active.delete(runId);
1892
+ if (!this.disposed && this.store.getRun(runId)?.ciWait)
1893
+ this.queueCiWake(runId);
1614
1894
  if (!this.executions.has(runId) && this.store.getRun(runId)?.stopping)
1615
1895
  this.store.updateRun(runId, { stopping: undefined });
1616
1896
  this.memoryPausing.delete(runId);
@@ -2062,6 +2342,8 @@ export class RunManager {
2062
2342
  this.store.commitWorkerExecutionComplete(runId, proof.generation);
2063
2343
  }
2064
2344
  }
2345
+ const hadCiWait = !!this.store.getRun(runId)?.ciWait;
2346
+ this.withdrawCiWait(runId);
2065
2347
  const cancelledFinish = this.store.commitRootFinishCancellation(runId);
2066
2348
  this.withdrawWorkerWait(runId);
2067
2349
  // Still waiting in the queue: just drop it there.
@@ -2087,7 +2369,7 @@ export class RunManager {
2087
2369
  if (cancelledFinish)
2088
2370
  return true;
2089
2371
  const run = this.store.getRun(runId);
2090
- if (run?.delegation && ['queued', 'running', 'waiting'].includes(run.status)) {
2372
+ if (run && (run.delegation || hadCiWait) && ['queued', 'running', 'waiting'].includes(run.status)) {
2091
2373
  this.store.updateRun(runId, { status: 'cancelled', finishedAt: new Date().toISOString() });
2092
2374
  return true;
2093
2375
  }
@@ -2096,6 +2378,8 @@ export class RunManager {
2096
2378
  state.cancelled = true;
2097
2379
  this.store.updateRun(runId, { stopping: true });
2098
2380
  this.store.flush();
2381
+ state.revokeCiTools?.();
2382
+ state.revokeCiTools = undefined;
2099
2383
  state.revokeDelegation?.();
2100
2384
  state.revokeDelegation = undefined;
2101
2385
  this.clearIdleTimer(state);
@@ -2251,7 +2535,9 @@ export class RunManager {
2251
2535
  if (!run || run.stopping || this.workerExecutionStopped(runId) || this.executionBlockedByRootFinish(run))
2252
2536
  return null;
2253
2537
  const message = this.toQueuedMessage(runId, content);
2254
- if (this.workerWait(runId))
2538
+ if (run.ciWait)
2539
+ this.withdrawCiWait(runId, message);
2540
+ else if (this.workerWait(runId))
2255
2541
  this.withdrawWorkerWait(runId, message);
2256
2542
  else
2257
2543
  this.store.updateRun(runId, { queuedMessages: [...(run.queuedMessages ?? []), message] });
@@ -2381,8 +2667,10 @@ export class RunManager {
2381
2667
  return false;
2382
2668
  // The pending maps were removed only AFTER hydration. This entry therefore
2383
2669
  // needs live delivery, never a second fold into the already-created opening.
2384
- const message = this.workerWait(runId) ? this.toQueuedMessage(runId, content) : undefined;
2385
- if (message)
2670
+ const message = this.workerWait(runId) || run.ciWait ? this.toQueuedMessage(runId, content) : undefined;
2671
+ if (run.ciWait)
2672
+ this.withdrawCiWait(runId, message);
2673
+ else if (message)
2386
2674
  this.withdrawWorkerWait(runId, message);
2387
2675
  const pending = this.deferredMessages.get(runId) ?? [];
2388
2676
  pending.push({ content, ...(message ? { messageId: message.id } : {}) });
@@ -2533,10 +2821,9 @@ export class RunManager {
2533
2821
  const parsed = workerWaitRequestSchema.parse(request);
2534
2822
  const run = this.store.getRun(parentId);
2535
2823
  const state = this.active.get(parentId);
2536
- if (run?.delegation?.role !== 'root' || run.delegation.historyDeletion || !['running', 'waiting'].includes(run.status) ||
2537
- !state?.session?.open || state.cancelled || state.pendingHumanAsk || this.hasPendingHumanAsk(parentId) || run.delegation.wait || run.delegation.finishRequestedAt) {
2538
- throw new DelegationPolicyError('incompatible_state', 'parent cannot register a worker wait');
2539
- }
2824
+ this.assertCanRegisterWorkerWait(parentId);
2825
+ if (run?.delegation?.role !== 'root' || !state)
2826
+ throw new DelegationPolicyError('denied_scope', 'Only a parent may wait for owned workers');
2540
2827
  for (const id of parsed.workerIds) {
2541
2828
  const worker = this.store.getRun(id);
2542
2829
  if (worker?.delegation?.role !== 'worker' || worker.delegation.parentRunId !== parentId ||
@@ -2553,15 +2840,45 @@ export class RunManager {
2553
2840
  this.parkWorkerWait(parentId, state);
2554
2841
  return this.workerWait(parentId);
2555
2842
  }
2843
+ assertCanRegisterWorkerWait(runId) {
2844
+ const run = this.store.getRun(runId);
2845
+ const state = this.active.get(runId);
2846
+ const refuse = (message) => { throw new DelegationPolicyError('incompatible_state', message); };
2847
+ if (!run?.delegation || run.delegation.role === 'invalid')
2848
+ refuse('Worker waits require an owned worker or parent run');
2849
+ if (this.historyDeletionPending(runId))
2850
+ refuse('History deletion is pending; finish deletion before registering a wait');
2851
+ if (this.executionBlockedByRootFinish(run))
2852
+ refuse('Parent finish is pending; a finishing parent cannot register a wait');
2853
+ if (state?.pendingHumanAsk || this.hasUnansweredHumanAsk(runId))
2854
+ refuse('A human question is pending; answer it before registering a wait');
2855
+ if (run?.ciWait)
2856
+ refuse('A CI wait is already active; finish or withdraw it before registering a worker wait');
2857
+ const wait = this.workerWait(runId);
2858
+ if (wait)
2859
+ refuse(`Wait ${wait.id} is already active; keep it or run worker cancel-wait ${wait.id} before registering another`);
2860
+ if (!['running', 'waiting'].includes(run.status) || !state?.session?.open || state.cancelled) {
2861
+ refuse(`Run is ${run.status} with no available active session; register a wait from an executing parent or worker`);
2862
+ }
2863
+ }
2864
+ /** An answer already executing in this generation is not an unanswered question.
2865
+ * Keep the durable checkpoint at turn-end for crash recovery, and match its exact
2866
+ * sequence so it can never hide a newer question. */
2867
+ hasUnansweredHumanAsk(runId) {
2868
+ const seq = this.pendingHumanAskSeq(runId);
2869
+ const state = this.active.get(runId);
2870
+ return seq !== undefined && !(state?.session?.open && !state.cancelled && state.openingAnswerAskSeq === seq);
2871
+ }
2556
2872
  /** Both roles can select their own requests without acquiring worker-management authority. */
2557
2873
  registerRequestWait(runId, request) {
2558
2874
  const parsed = requestWaitRequestSchema.parse(request);
2559
2875
  this.reconcileWorkerWaits();
2560
2876
  const run = this.store.getRun(runId);
2561
2877
  const state = this.active.get(runId);
2878
+ this.assertCanRegisterWorkerWait(runId);
2562
2879
  if (!run?.delegation || run.delegation.role === 'invalid' || this.historyDeletionPending(runId) ||
2563
2880
  this.executionBlockedByRootFinish(run) || !['running', 'waiting'].includes(run.status) ||
2564
- !state?.session?.open || state.cancelled || state.pendingHumanAsk || this.hasPendingHumanAsk(runId) || run.delegation.wait) {
2881
+ !state?.session?.open || state.cancelled) {
2565
2882
  throw new DelegationPolicyError('incompatible_state', 'run cannot register a request wait');
2566
2883
  }
2567
2884
  const root = run.delegation.role === 'root' ? run : this.store.getRun(run.delegation.parentRunId);
@@ -2583,6 +2900,90 @@ export class RunManager {
2583
2900
  this.parkWorkerWait(runId, state);
2584
2901
  return registered;
2585
2902
  }
2903
+ /** A wait wake is only a pointer. Only provider submission/opening prompts own delivery. */
2904
+ providerOwnsInboxInput(runId, id) {
2905
+ const state = this.active.get(runId);
2906
+ return !!state?.agentInputFlight?.inputIds.includes(id) || state?.openingAgentInputId === id ||
2907
+ this.store.getRun(runId)?.continuationMessage?.agentInputId === id;
2908
+ }
2909
+ canClaimInboxInput(runId, id) {
2910
+ const run = this.store.getRun(runId);
2911
+ const input = run?.agentInputs?.find(entry => entry.id === id);
2912
+ return !this.disposed && !!run && ['queued', 'running', 'waiting'].includes(run.status) &&
2913
+ !this.historyDeletionPending(runId) && !this.executionBlockedByRootFinish(run) && !this.workerExecutionStopped(runId) &&
2914
+ !!input && input.source === 'agent' && !!input.conversation && !input.deliveredAt &&
2915
+ !hasLiveInboxClaim(input) && !this.providerOwnsInboxInput(runId, id);
2916
+ }
2917
+ /** The service supplies authorized, bounded IDs; claim and flight reservations are synchronous. */
2918
+ reserveInboxInputs(runId, generation, ids) {
2919
+ if (!ids.length || ids.some(id => !this.canClaimInboxInput(runId, id)) ||
2920
+ this.store.getRun(runId)?.agentInputs?.some(input => !input.deliveredAt && hasLiveInboxClaim(input)))
2921
+ return undefined;
2922
+ const claim = { receiptId: randomUUID(), generation, expiresAt: new Date(Date.now() + 120_000).toISOString() };
2923
+ this.store.claimInboxInputs(runId, ids, claim);
2924
+ this.armInboxClaimExpiry(runId);
2925
+ return { receiptId: claim.receiptId, expiresAt: claim.expiresAt, inputIds: [...ids] };
2926
+ }
2927
+ acknowledgeInbox(runId, generation, receiptId) {
2928
+ if (this.disposed || this.store.getRun(runId)?.agentInputs?.some(input => input.inboxClaim?.receiptId === receiptId &&
2929
+ !input.deliveredAt && this.providerOwnsInboxInput(runId, input.id)))
2930
+ throw new Error('inbox receipt displaced');
2931
+ const result = this.store.ackInboxInputs(runId, receiptId, generation, new Date().toISOString());
2932
+ this.armInboxClaimExpiry(runId);
2933
+ this.reconcileWorkerWaits();
2934
+ this.deliverConversationInput(runId);
2935
+ this.active.get(runId)?.resumeInboxAutoEnd?.();
2936
+ this.releaseSlot();
2937
+ return result;
2938
+ }
2939
+ releaseInbox(runId, generation, receiptId) {
2940
+ if (this.disposed)
2941
+ throw new Error('inbox manager disposed');
2942
+ const result = this.store.releaseInboxInputs(runId, receiptId, generation);
2943
+ this.armInboxClaimExpiry(runId);
2944
+ this.reconcileWorkerWaits();
2945
+ this.deliverConversationInput(runId);
2946
+ this.releaseSlot();
2947
+ return result;
2948
+ }
2949
+ /** Expiry is an independent wake source, including after the provider's last turn. */
2950
+ armInboxClaimExpiry(runId) {
2951
+ const previous = this.inboxClaimTimers.get(runId);
2952
+ if (previous)
2953
+ clearTimeout(previous);
2954
+ this.inboxClaimTimers.delete(runId);
2955
+ if (this.disposed)
2956
+ return;
2957
+ const deadlines = (this.store.getRun(runId)?.agentInputs ?? []).flatMap(input => input.inboxClaim && !input.inboxClaim.acknowledgedAt ? [Date.parse(input.inboxClaim.expiresAt)] : []);
2958
+ if (!deadlines.length)
2959
+ return;
2960
+ let warned = false;
2961
+ const expire = () => {
2962
+ this.inboxClaimTimers.delete(runId);
2963
+ if (this.disposed || !this.store.getRun(runId))
2964
+ return;
2965
+ try {
2966
+ this.store.clearExpiredInboxClaims(runId, new Date().toISOString());
2967
+ this.reconcileWorkerWaits();
2968
+ this.deliverConversationInput(runId);
2969
+ this.armInboxClaimExpiry(runId);
2970
+ this.releaseSlot();
2971
+ }
2972
+ catch {
2973
+ if (!warned)
2974
+ console.warn('[cez] inbox expiry checkpoint failed; retrying in 1 second');
2975
+ warned = true;
2976
+ if (!this.disposed)
2977
+ arm(1_000);
2978
+ }
2979
+ };
2980
+ const arm = (delay) => {
2981
+ const timer = setTimeout(expire, delay);
2982
+ timer.unref?.();
2983
+ this.inboxClaimTimers.set(runId, timer);
2984
+ };
2985
+ arm(Math.max(0, Math.min(...deadlines) - Date.now()));
2986
+ }
2586
2987
  /** Accepted conversation input uses the existing scheduler admission for idle recipients. */
2587
2988
  deliverConversationInput(runId) {
2588
2989
  if (this.disposed)
@@ -2592,7 +2993,13 @@ export class RunManager {
2592
2993
  this.executionBlockedByRootFinish(run) || !['queued', 'running', 'waiting'].includes(run.status) ||
2593
2994
  this.workerExecutionStopped(runId) || this.hasPendingHumanAsk(runId))
2594
2995
  return;
2595
- const input = run.agentInputs?.find(entry => entry.conversation && !entry.deliveredAt);
2996
+ // Retain conversation input until the registered CI wait receives scheduler admission.
2997
+ // Converting this monitor into a worker wake would strand both admission paths.
2998
+ if (run.ciWait) {
2999
+ this.queueCiWake(runId);
3000
+ return;
3001
+ }
3002
+ const input = run.agentInputs?.find(entry => entry.conversation && !entry.deliveredAt && !hasLiveInboxClaim(entry));
2596
3003
  if (!input)
2597
3004
  return;
2598
3005
  const state = this.active.get(runId);
@@ -2743,14 +3150,24 @@ export class RunManager {
2743
3150
  const wait = parent.delegation.wait;
2744
3151
  if (!wait)
2745
3152
  continue;
2746
- const delivered = wait.wakeId && parent.agentInputs?.find(input => input.id === wait.wakeId)?.deliveredAt;
3153
+ const delivered = parent.agentInputs?.find(input => input.id === wait.wakeId && input.deliveredAt);
2747
3154
  if (delivered) {
2748
3155
  // Receipt persistence precedes the live status update. After a crash
2749
3156
  // this is interrupted execution, not generic waiting-run success.
2750
- if (this.recovering && parent.status === 'waiting' && !this.hasPendingHumanAsk(parent.id)) {
3157
+ if (this.recovering && !delivered.inboxClaim?.acknowledgedAt && parent.status === 'waiting' && !this.hasPendingHumanAsk(parent.id)) {
2751
3158
  this.store.updateRun(parent.id, { status: 'running', activity: undefined });
2752
3159
  }
3160
+ const state = this.active.get(parent.id);
3161
+ const inboxParked = delivered.inboxClaim?.acknowledgedAt &&
3162
+ (this.workerWaiting.has(parent.id) || this.workerWakeAdmitted.has(parent.id)) &&
3163
+ state?.session?.open && state.atTurnBoundary === state.session;
2753
3164
  this.withdrawWorkerWait(parent.id);
3165
+ // Inbox ACK starts no provider turn. Retain the completed turn's slot
3166
+ // exemption if its registered wait parked while that ACK was in transit.
3167
+ if (inboxParked && state) {
3168
+ this.waiting.add(parent.id);
3169
+ this.armIdleTimer(parent.id, state);
3170
+ }
2754
3171
  continue;
2755
3172
  }
2756
3173
  const now = new Date().toISOString();
@@ -2788,15 +3205,21 @@ export class RunManager {
2788
3205
  const root = parent.delegation.role === 'root' ? parent : this.store.getRun(parent.delegation.parentRunId);
2789
3206
  const requests = root?.delegation?.role === 'root' ? root.delegation.conversation?.outcomes ?? [] : [];
2790
3207
  let next = reconcileWorkerWait(selected, outcomes, now, requests);
2791
- const incoming = parent.agentInputs?.find(input => input.conversation && !input.deliveredAt &&
3208
+ // The opening prompt (also during recovery) and a reserved submission are
3209
+ // already being processed. Their delayed receipts cannot interrupt a wait
3210
+ // registered by that very turn. Only inputs still awaiting a turn can.
3211
+ const flight = this.active.get(parent.id)?.agentInputFlight;
3212
+ const pendingInputs = parent.agentInputs?.filter(input => !input.deliveredAt && !hasLiveInboxClaim(input) &&
3213
+ input.id !== this.active.get(parent.id)?.openingAgentInputId && input.id !== parent.continuationMessage?.agentInputId && !flight?.inputIds.includes(input.id)) ?? [];
3214
+ const incoming = pendingInputs.find(input => input.conversation &&
2792
3215
  !(input.conversation.kind === 'reply' && input.conversation.requestId && wait.requestIds?.includes(input.conversation.requestId)));
2793
3216
  if (incoming && next.phase !== 'wake-pending')
2794
3217
  next = { ...next, phase: 'wake-pending', reason: 'message', wakeId: incoming.id };
2795
3218
  // A full queue of selected replies still needs admission. Use the last
2796
3219
  // already accepted input as the checkpoint rather than adding a 33rd input.
2797
3220
  // Its content is preserved; delivery includes the wait receipt separately.
2798
- const pendingInputs = parent.agentInputs?.filter(input => !input.deliveredAt) ?? [];
2799
- if (next.phase === 'wake-pending' && pendingInputs.length >= 32 &&
3221
+ if (next.phase === 'wake-pending' && pendingInputs.length &&
3222
+ (parent.agentInputs ?? []).filter(input => !input.deliveredAt).length >= 32 &&
2800
3223
  !parent.agentInputs?.some(input => input.id === next.wakeId)) {
2801
3224
  next = { ...next, wakeId: pendingInputs.at(-1).id };
2802
3225
  }
@@ -2861,7 +3284,7 @@ export class RunManager {
2861
3284
  !['queued', 'running', 'waiting'].includes(run.status))
2862
3285
  return;
2863
3286
  const existing = run.agentInputs?.find(input => input.id === wait.wakeId);
2864
- if (existing?.deliveredAt)
3287
+ if (existing?.deliveredAt || (existing && hasLiveInboxClaim(existing)))
2865
3288
  return;
2866
3289
  // 32 summaries, including worst-case JSON escaping, must fit the ordinary
2867
3290
  // input bound. Full 4k summaries remain in durable outcomes and events.
@@ -2881,6 +3304,10 @@ export class RunManager {
2881
3304
  return;
2882
3305
  }
2883
3306
  else if (!existing) {
3307
+ // A full inbox snapshot can temporarily own every reply. ACK/expiry will
3308
+ // reconcile again when a lifecycle input can fit or adopt an unread reply.
3309
+ if ((run.agentInputs ?? []).filter(input => !input.deliveredAt).length >= 32)
3310
+ return;
2884
3311
  this.store.commitAgentInputs(parentId, enqueueAgentInput(run, input));
2885
3312
  this.store.appendEvent(parentId, { type: 'agent-input', input });
2886
3313
  }
@@ -2989,11 +3416,11 @@ export class RunManager {
2989
3416
  this.flushAgentInputs(runId);
2990
3417
  }
2991
3418
  /** Reservation is synchronous; only an acknowledged transport may checkpoint delivery. */
2992
- submitAgentInput(runId, state, content, inputId) {
3419
+ submitAgentInput(runId, state, content, inputIds = []) {
2993
3420
  const session = state.session;
2994
3421
  if (!session?.open || state.agentInputFlight || state.cancelled || state.finishRequested || state.agentInputError || state.pendingHumanAsk)
2995
3422
  return false;
2996
- const flight = { session, inputId };
3423
+ const flight = { session, inputIds };
2997
3424
  state.agentInputFlight = flight;
2998
3425
  const current = () => !this.disposed && this.active.get(runId) === state && state.session === session &&
2999
3426
  state.agentInputFlight === flight && !state.cancelled && !state.finishRequested && !this.workerExecutionStopped(runId) &&
@@ -3036,13 +3463,24 @@ export class RunManager {
3036
3463
  if (!current())
3037
3464
  return;
3038
3465
  try {
3039
- if (inputId) {
3466
+ if (inputIds.length) {
3040
3467
  const queue = this.store.getRun(runId)?.agentInputs ?? [];
3041
- if (!queue.some(input => input.id === inputId && !input.deliveredAt))
3468
+ if (!queue.some(input => inputIds.includes(input.id) && !input.deliveredAt))
3042
3469
  return;
3043
- this.store.commitAgentInputs(runId, queue.map(input => input.id === inputId
3044
- ? { ...input, deliveredAt: new Date().toISOString() } : input));
3045
- if (this.workerWait(runId)?.wakeId === inputId)
3470
+ const ciWakeId = this.store.getRun(runId)?.ciWait?.wakeId;
3471
+ if (ciWakeId && inputIds.includes(ciWakeId)) {
3472
+ // CI lifecycle input owns its turn; never retire a conversation batch with it.
3473
+ if (inputIds.length !== 1)
3474
+ throw new Error('CI observation must be delivered separately');
3475
+ this.store.commitCiWaitDelivery(runId, ciWakeId);
3476
+ this.ciWakeAdmitted.delete(runId);
3477
+ }
3478
+ else {
3479
+ const deliveredAt = new Date().toISOString();
3480
+ this.store.commitAgentInputs(runId, queue.map(input => inputIds.includes(input.id) && !input.deliveredAt
3481
+ ? { ...input, deliveredAt } : input));
3482
+ }
3483
+ if (inputIds.includes(this.workerWait(runId)?.wakeId ?? ''))
3046
3484
  this.withdrawWorkerWait(runId);
3047
3485
  this.reconcileWorkerWaits();
3048
3486
  }
@@ -3059,7 +3497,7 @@ export class RunManager {
3059
3497
  return;
3060
3498
  if (this.flushAgentInputs(runId))
3061
3499
  return;
3062
- if (state.atTurnBoundary !== session || state.pendingHumanAsk || this.workerWait(runId) || this.hasQueuedAgentInputs(runId))
3500
+ if (state.atTurnBoundary !== session || state.pendingHumanAsk || this.workerWait(runId) || this.store.getRun(runId)?.ciWait || this.hasQueuedAgentInputs(runId))
3063
3501
  return;
3064
3502
  if (state.doneAtBoundary === session) {
3065
3503
  if (this.deferParentCompletion(runId))
@@ -3105,8 +3543,10 @@ export class RunManager {
3105
3543
  deliveries?.add(flight.settled);
3106
3544
  void flight.settled.then(() => deliveries?.delete(flight.settled));
3107
3545
  this.resumeParkedRun(runId, state);
3108
- if (inputId && this.workerWait(runId)?.wakeId === inputId)
3546
+ if (inputIds.includes(this.workerWait(runId)?.wakeId ?? ''))
3109
3547
  state.workerWakeTurn = session;
3548
+ if (inputIds.includes(this.store.getRun(runId)?.ciWait?.wakeId ?? ''))
3549
+ state.ciWakeTurn = session;
3110
3550
  return true;
3111
3551
  }
3112
3552
  /** False leaves the durable entry untouched. No human-answer/slash-expansion fallback. */
@@ -3119,9 +3559,23 @@ export class RunManager {
3119
3559
  (this.workerWait(runId)?.phase === 'registered') ||
3120
3560
  (this.workerWait(runId)?.phase === 'wake-pending' && !this.workerWakeAdmitted.has(runId)))
3121
3561
  return false;
3122
- const input = nextAgentInput(run.agentInputs ?? [], state.pendingHumanAsk);
3123
- if (!input)
3562
+ if (state.pendingHumanAsk || this.hasUnansweredHumanAsk(runId))
3563
+ return false;
3564
+ // The opening prompt already carries this input. It must not be resubmitted
3565
+ // by a readiness callback before the first successful turn checkpoints it.
3566
+ if (state.openingAgentInputId)
3124
3567
  return false;
3568
+ if (run.ciWait) {
3569
+ if (!this.ciWakeAdmitted.has(runId) || state.atTurnBoundary !== state.session)
3570
+ return false;
3571
+ // Preserve CI admission and atomic retirement; conversations batch on the next turn.
3572
+ const input = run.agentInputs?.find(entry => entry.id === run.ciWait?.wakeId && !entry.deliveredAt);
3573
+ return !!input && this.submitAgentInput(runId, state, [{ type: 'text', text: this.formatAgentInput(runId, input) }], [input.id]);
3574
+ }
3575
+ const batch = agentInputBatch(run.agentInputs ?? [], input => this.formatAgentInput(runId, input));
3576
+ return !!batch && this.submitAgentInput(runId, state, [{ type: 'text', text: batch.text }], batch.inputs.map(input => input.id));
3577
+ }
3578
+ formatAgentInput(runId, input) {
3125
3579
  const attribution = input.conversation;
3126
3580
  const text = attribution
3127
3581
  ? `Agent conversation ${JSON.stringify({ id: input.id, ...attribution })}${this.workerWait(runId)?.reason === 'message' ? `; interrupted wait ${this.workerWait(runId).id} to receive this message; outstanding requests remain pending` : ''}\n${input.text}`
@@ -3129,7 +3583,12 @@ export class RunManager {
3129
3583
  const wait = this.workerWait(runId);
3130
3584
  const receipt = wait?.wakeId === input.id && wait.wakeId !== wait.id && wait.reason !== 'message'
3131
3585
  ? `\nWait ${wait.id} (${wait.reason}); request outcomes: ${JSON.stringify(wait.requestOutcomes ?? [])}; worker outcomes: ${JSON.stringify(wait.outcomes.map(outcome => ({ ...outcome, ...(outcome.summary ? { summary: outcome.summary.slice(0, 256) } : {}) })))}. This is not review approval.` : '';
3132
- return this.submitAgentInput(runId, state, [{ type: 'text', text: text + receipt }], input.id);
3586
+ return text + receipt + this.agentInputOutcomeText(input);
3587
+ }
3588
+ agentInputOutcomeText(input) {
3589
+ const root = this.store.getRun(input.parentRunId);
3590
+ const outcome = input.conversation && root?.delegation?.role === 'root' ? root.delegation.conversation?.outcomes.find(entry => entry.requestId === (input.conversation?.requestId ?? input.id)) : undefined;
3591
+ return outcome ? `\nRequest outcome: ${outcome.status}. This delivery does not reopen the settled request.` : '';
3133
3592
  }
3134
3593
  /** Deliver anything `deferMessage` buffered, once the session is live. */
3135
3594
  flushDeferred(runId) {
@@ -3208,6 +3667,8 @@ export class RunManager {
3208
3667
  const run = this.store.getRun(runId);
3209
3668
  if (!run || run.stopping || this.workerExecutionStopped(runId) || this.executionBlockedByRootFinish(run))
3210
3669
  return false;
3670
+ if (!userAuthored && run.ciWait)
3671
+ return false;
3211
3672
  const text = content
3212
3673
  .filter((b) => b.type === 'text')
3213
3674
  .map((b) => b.text)
@@ -3217,7 +3678,10 @@ export class RunManager {
3217
3678
  // these as user attachments (vs. agent tool screenshots) on disk (#357).
3218
3679
  const persisted = userAuthored ? this.persistPastedAttachments(runId, content) : [];
3219
3680
  const images = persisted.map((saved) => saved.url);
3681
+ const ciHumanMessage = userAuthored && run.ciWait
3682
+ ? { id: randomUUID(), text, images, createdAt: new Date().toISOString() } : undefined;
3220
3683
  if (userAuthored) {
3684
+ this.withdrawCiWait(runId, ciHumanMessage);
3221
3685
  this.withdrawWorkerWait(runId);
3222
3686
  this.store.appendEvent(runId, {
3223
3687
  type: 'user-message',
@@ -3240,6 +3704,8 @@ export class RunManager {
3240
3704
  : this.submitAgentInput(runId, state, deliverable);
3241
3705
  if (delivered) {
3242
3706
  if (userAuthored) {
3707
+ if (ciHumanMessage)
3708
+ this.store.commitQueuedMessageDelivery(runId, ciHumanMessage.id);
3243
3709
  if (answeringAskSeq !== undefined) {
3244
3710
  this.store.appendEvent(runId, { type: 'human-input-delivered', askSeq: answeringAskSeq });
3245
3711
  }
@@ -3264,6 +3730,7 @@ export class RunManager {
3264
3730
  const state = this.active.get(runId);
3265
3731
  if (decision.action === 'session' && state?.session) {
3266
3732
  try {
3733
+ this.withdrawCiWait(runId);
3267
3734
  this.withdrawWorkerWait(runId);
3268
3735
  }
3269
3736
  catch {
@@ -3348,7 +3815,7 @@ export class RunManager {
3348
3815
  continueRun(runId, opts = {},
3349
3816
  /** Restart recovery may discover several interrupted tasks at once. Those
3350
3817
  * continuations are queued; an explicit user Continue remains immediate. */
3351
- deferForCapacity = false) {
3818
+ deferForCapacity = false, conversation) {
3352
3819
  if (agentModelsLocked(this.repoRoot) && (opts.model?.trim() || opts.effort?.trim())) {
3353
3820
  return { ok: false, error: AGENT_MODELS_LOCKED_ERROR };
3354
3821
  }
@@ -3357,6 +3824,9 @@ export class RunManager {
3357
3824
  const run = this.store.getRun(runId);
3358
3825
  if (!run)
3359
3826
  return { ok: false, error: 'not found' };
3827
+ if (conversation && (!deferForCapacity || run.delegation?.role !== 'worker' || run.delegation.parentRunId !== conversation.rootId ||
3828
+ !['done', 'review', 'failed'].includes(run.status)))
3829
+ return { ok: false, error: 'Only a settled owned worker can be resumed by its parent message' };
3360
3830
  if (run.stopping)
3361
3831
  return { ok: false, error: 'run is still stopping' };
3362
3832
  if (this.historyDeletionPending(runId))
@@ -3397,15 +3867,17 @@ export class RunManager {
3397
3867
  if (this.executionBlockedByRootFinish(run))
3398
3868
  return { ok: false, error: 'parent finish is pending' };
3399
3869
  const answers = deferForCapacity ? [run.continuationMessage, ...(run.queuedMessages ?? [])] : [opts];
3400
- if (pendingHumanAsk && ((deferForCapacity && run.continuationMessage?.origin !== 'human') ||
3870
+ if (pendingHumanAsk && (conversation || (deferForCapacity && run.continuationMessage?.origin !== 'human') ||
3401
3871
  !answers.some(answer => answer?.text?.trim() || answer?.images?.length))) {
3402
3872
  return { ok: false, error: 'pending human question requires an explicit answer' };
3403
3873
  }
3874
+ // A disconnected CI monitor has durable intent but no live session to receive human input.
3875
+ const disconnectedCiWait = !!run.ciWait && !this.isActive(runId) && !hasRegisteredRunProcess(runId);
3404
3876
  // `review` is continuable too — that's the "Send back" path (spec 009).
3405
- if (!['done', 'failed', 'cancelled', 'review'].includes(run.status) && !(pendingHumanAsk && run.status === 'waiting') &&
3877
+ if (!disconnectedCiWait && !['done', 'failed', 'cancelled', 'review'].includes(run.status) && !(pendingHumanAsk && run.status === 'waiting') &&
3406
3878
  !(run.status === 'waiting' && !this.isActive(runId) && ((!hasRegisteredRunProcess(runId) &&
3407
3879
  (run.delegation === undefined || run.delegation.role === 'root')) ||
3408
- (deferForCapacity && run.delegation?.role === 'worker' && !!this.workerWait(runId))))) {
3880
+ (deferForCapacity && run.delegation?.role === 'worker' && (!!this.workerWait(runId) || !!run.ciWait))))) {
3409
3881
  return { ok: false, error: `cannot continue a ${run.status} run` };
3410
3882
  }
3411
3883
  const sessionStep = [...run.steps].reverse().find((s) => s.sessionId);
@@ -3532,6 +4004,7 @@ export class RunManager {
3532
4004
  // bound UNATTENDED resumes.
3533
4005
  this.clearAutoResume(runId);
3534
4006
  if (!deferForCapacity) {
4007
+ this.withdrawCiWait(runId);
3535
4008
  this.withdrawWorkerWait(runId);
3536
4009
  this.resetParentCompletion(runId);
3537
4010
  }
@@ -3542,7 +4015,7 @@ export class RunManager {
3542
4015
  const continuationStep = { id: stepId, name: 'Continue', kind: 'agent', synthetic: 'continuation' };
3543
4016
  if (run.delegation?.role !== 'worker')
3544
4017
  this.store.addStep(runId, continuationStep);
3545
- const recovered = deferForCapacity ? run.continuationMessage : undefined;
4018
+ const recovered = deferForCapacity && !conversation ? run.continuationMessage : undefined;
3546
4019
  const message = recovered ?? this.toQueuedMessage(runId, [
3547
4020
  ...(opts.text?.trim() ? [{ type: 'text', text: opts.text.trim() }] : []),
3548
4021
  ...(opts.images ?? []),
@@ -3552,6 +4025,7 @@ export class RunManager {
3552
4025
  // and the original prompt/URLs must reach disk before the first startup await.
3553
4026
  const acceptedPatch = {
3554
4027
  continuationMessage: { ...message, id: stepId, text: prompt,
4028
+ ...(conversation ? { agentInputId: conversation.input.id } : {}),
3555
4029
  origin: recovered ? recovered.origin : deferForCapacity ? 'lifecycle' : 'human' },
3556
4030
  status: deferForCapacity ? 'queued' : 'running',
3557
4031
  error: undefined,
@@ -3563,7 +4037,7 @@ export class RunManager {
3563
4037
  };
3564
4038
  if (run.delegation?.role === 'worker') {
3565
4039
  try {
3566
- this.store.commitWorkerContinuation(runId, acceptedPatch, continuationStep);
4040
+ this.store.commitWorkerContinuation(runId, acceptedPatch, continuationStep, conversation);
3567
4041
  }
3568
4042
  catch {
3569
4043
  // No session or async launch exists yet. Release the private execution
@@ -3725,6 +4199,12 @@ export class RunManager {
3725
4199
  // blocks before they reach the model). An image ALSO rides along as a base64 block so the
3726
4200
  // model can view it; a file (#950) has nothing to view and travels as its path alone.
3727
4201
  const checkpoint = record?.continuationMessage;
4202
+ state.openingAgentInputId = checkpoint?.agentInputId;
4203
+ const openingInput = record?.agentInputs?.find(input => input.id === checkpoint?.agentInputId);
4204
+ // Hydration has already included later human updates and task edits. Append
4205
+ // the current obligation outcome without replacing that composed prompt.
4206
+ if (openingInput)
4207
+ prompt += this.agentInputOutcomeText(openingInput);
3728
4208
  const saved = this.readPersistedAttachments(runId, checkpoint?.id === stepId ? checkpoint.images ?? [] : [], 'continuation');
3729
4209
  const openingImages = [...(images.length ? contentBlocksOf(images) : saved.blocks), ...persistedImages];
3730
4210
  const attachments = [...saved.attachments, ...persistedAttachments];
@@ -3737,6 +4217,7 @@ export class RunManager {
3737
4217
  ...(attachments.length ? { images: attachments.map((saved) => saved.url) } : {}),
3738
4218
  });
3739
4219
  let openingAnswerAskSeq = checkpoint?.origin === 'human' ? this.pendingHumanAskSeq(runId) : undefined;
4220
+ state.openingAnswerAskSeq = openingAnswerAskSeq;
3740
4221
  state.pendingHumanAsk = openingAnswerAskSeq === undefined && this.hasPendingHumanAsk(runId);
3741
4222
  let stepCost = 0;
3742
4223
  let turnText = '';
@@ -3787,13 +4268,40 @@ export class RunManager {
3787
4268
  sawClaudeScheduleWakeup = true;
3788
4269
  if (event.type === 'turn-end') {
3789
4270
  state.workerWakeTurn = undefined;
4271
+ state.ciWakeTurn = undefined;
3790
4272
  state.atTurnBoundary = state.session;
4273
+ state.ciTurnId = randomUUID();
3791
4274
  // A completed turn acknowledges the opening payload. Until then a restart
3792
4275
  // must replay it, including a crash after startSession but before delivery.
3793
4276
  if (!state.cancelled) {
4277
+ if (state.openingAgentInputId && state.session?.open) {
4278
+ const id = state.openingAgentInputId;
4279
+ const deliveredAt = new Date().toISOString();
4280
+ try {
4281
+ this.store.commitAgentInputs(runId, (this.store.getRun(runId)?.agentInputs ?? []).map(input => input.id === id && !input.deliveredAt ? { ...input, deliveredAt } : input), id);
4282
+ }
4283
+ catch (error) {
4284
+ state.agentInputError = `agent input delivery checkpoint failed: ${error instanceof Error ? error.message : String(error)}`;
4285
+ try {
4286
+ state.session.interrupt();
4287
+ }
4288
+ finally {
4289
+ try {
4290
+ this.store.appendEvent(runId, { type: 'error', message: state.agentInputError });
4291
+ }
4292
+ catch {
4293
+ console.error(`[cez] ${state.agentInputError}`);
4294
+ }
4295
+ }
4296
+ return;
4297
+ }
4298
+ state.openingAgentInputId = undefined;
4299
+ this.reconcileWorkerWaits();
4300
+ }
3794
4301
  if (openingAnswerAskSeq !== undefined && state.session?.open) {
3795
4302
  this.store.appendEvent(runId, { type: 'human-input-delivered', askSeq: openingAnswerAskSeq });
3796
4303
  openingAnswerAskSeq = undefined;
4304
+ state.openingAnswerAskSeq = undefined;
3797
4305
  }
3798
4306
  this.store.updateRun(runId, { continuationMessage: undefined });
3799
4307
  this.store.flush();
@@ -3824,8 +4332,9 @@ export class RunManager {
3824
4332
  this.store.updateRun(runId, { invalidAsk: !ask && askNotes.length > 0 ? true : undefined });
3825
4333
  if (ask)
3826
4334
  this.prepareHumanAsk(runId, state);
3827
- const completionBlocked = !!done && this.deferParentCompletion(runId);
3828
- const workerWaitParked = completionBlocked || (!!sessionOpen && this.parkWorkerWait(runId, state));
4335
+ const ciWaitParked = !!sessionOpen && this.parkCiWait(runId, state);
4336
+ const completionBlocked = !ciWaitParked && !!done && this.deferParentCompletion(runId);
4337
+ const workerWaitParked = ciWaitParked || completionBlocked || (!!sessionOpen && this.parkWorkerWait(runId, state));
3829
4338
  state.doneAtBoundary = done ? state.session : undefined;
3830
4339
  state.parkAfterAck = sessionOpen && state.session ? { session: state.session, monitoring: !!monitoring } : undefined;
3831
4340
  const agentInputDelivered = !ask && !workerWaitParked && this.flushAgentInputs(runId);
@@ -4030,6 +4539,7 @@ export class RunManager {
4030
4539
  throw new Error('Worker stopped before launch');
4031
4540
  }
4032
4541
  const delegation = this.provisionSession(runId, state);
4542
+ const ciTools = await this.provisionCiSession(runId, state);
4033
4543
  try {
4034
4544
  session = runner.startSession({
4035
4545
  // The Continue step is a fresh agent session on the same run — the
@@ -4044,7 +4554,8 @@ export class RunManager {
4044
4554
  allowedTools: grants.allowedTools,
4045
4555
  bashAllowlist: grants.bashAllowlist,
4046
4556
  additionalDirectories: agentDirectories(join(this.dataDir, 'runs'), continueProfile.env),
4047
- env: { ...continueProfile.env, ...delegation?.env },
4557
+ env: { ...continueProfile.env, ...delegation?.env, ...ciTools?.env },
4558
+ ...(ciTools ? { cezarTools: ciTools.descriptor } : {}),
4048
4559
  ...(delegation ? { restrictNativeDelegation: delegation.restrictNativeDelegation } : {}),
4049
4560
  model: continueModel,
4050
4561
  effort: continueEffort,
@@ -4060,6 +4571,8 @@ export class RunManager {
4060
4571
  });
4061
4572
  }
4062
4573
  catch (error) {
4574
+ ciTools?.revoke();
4575
+ state.revokeCiTools = undefined;
4063
4576
  delegation?.revoke();
4064
4577
  state.revokeDelegation = undefined;
4065
4578
  throw error;
@@ -4080,6 +4593,10 @@ export class RunManager {
4080
4593
  if (this.preserveRunAfterDisposal(runId, state))
4081
4594
  return;
4082
4595
  this.persistWorkerAssistantResult(runId, stepId, result.text);
4596
+ if (this.store.getRun(runId)?.ciWait && !state.cancelled) {
4597
+ this.store.updateRun(runId, { status: 'running', activity: 'monitoring' });
4598
+ return;
4599
+ }
4083
4600
  if (sessionError || state.agentInputError)
4084
4601
  throw new Error(sessionError ?? state.agentInputError);
4085
4602
  if (!state.cancelled) {
@@ -4149,6 +4666,10 @@ export class RunManager {
4149
4666
  if (this.preserveRunAfterDisposal(runId, state))
4150
4667
  return;
4151
4668
  // Keep the provider failure that triggered teardown, even if teardown rejects.
4669
+ if (this.store.getRun(runId)?.ciWait && !state.cancelled) {
4670
+ this.store.updateRun(runId, { status: 'running', activity: 'monitoring' });
4671
+ return;
4672
+ }
4152
4673
  const message = sessionError ?? state.agentInputError ?? (err instanceof Error ? err.message : String(err));
4153
4674
  sink.sessionEnded('error', message);
4154
4675
  this.store.updateStep(runId, stepId, { status: 'failed', error: message, finishedAt: finishedAt() });
@@ -4162,6 +4683,8 @@ export class RunManager {
4162
4683
  this.store.appendEvent(runId, { type: 'lifecycle', message: `continue failed — ${message}` });
4163
4684
  }
4164
4685
  finally {
4686
+ ciTools?.revoke();
4687
+ state.revokeCiTools = undefined;
4165
4688
  delegation?.revoke();
4166
4689
  state.revokeDelegation = undefined;
4167
4690
  this.recordUsagePeaks(runId);
@@ -4463,6 +4986,11 @@ export class RunManager {
4463
4986
  this.requeueWhileHeld(runId, workflow, input, taskBackend, state, i);
4464
4987
  return;
4465
4988
  }
4989
+ if (this.store.getRun(runId)?.ciWait && !state.cancelled) {
4990
+ this.store.updateRun(runId, { status: 'running', activity: 'monitoring' });
4991
+ this.dropActive(runId);
4992
+ return;
4993
+ }
4466
4994
  if (failure) {
4467
4995
  this.finishStep(runId, step.id, 'failed', failure, emit);
4468
4996
  runError = `step "${step.id}" failed: ${failure}`;
@@ -4671,7 +5199,9 @@ export class RunManager {
4671
5199
  sawClaudeScheduleWakeup = true;
4672
5200
  if (event.type === 'turn-end') {
4673
5201
  state.workerWakeTurn = undefined;
5202
+ state.ciWakeTurn = undefined;
4674
5203
  state.atTurnBoundary = state.session;
5204
+ state.ciTurnId = randomUUID();
4675
5205
  // v2 `turn.completed` already flushed the coalescers; the v1 turn
4676
5206
  // boundary flushes again (idempotent) as a backstop.
4677
5207
  sink.flushAll();
@@ -4680,7 +5210,7 @@ export class RunManager {
4680
5210
  const done = interactive && sessionOpen && DONE_MARKER_RE.test(turnText.trimEnd());
4681
5211
  // `CEZ:ASK` → the user is blocked; wins over `CEZ:MONITORING`, loses to
4682
5212
  // `CEZ:DONE` (#473).
4683
- const askTurn = resolveAskTurn(turnText, completedAssistantText, Boolean((interactive || this.workerWait(runId)) && sessionOpen) && !done);
5213
+ const askTurn = resolveAskTurn(turnText, completedAssistantText, Boolean((interactive || this.workerWait(runId) || this.store.getRun(runId)?.ciWait) && sessionOpen) && !done);
4684
5214
  const { ask, notes: askNotes } = askTurn;
4685
5215
  discardQueuedMessagesOnAsk(state.session, askTurn);
4686
5216
  const monitoring = !this.parentCompletionAttention(runId) &&
@@ -4698,8 +5228,9 @@ export class RunManager {
4698
5228
  this.store.updateRun(runId, { invalidAsk: !ask && askNotes.length > 0 ? true : undefined });
4699
5229
  if (ask)
4700
5230
  this.prepareHumanAsk(runId, state);
4701
- const completionBlocked = !!done && this.deferParentCompletion(runId);
4702
- const workerWaitParked = completionBlocked || (!!sessionOpen && this.parkWorkerWait(runId, state));
5231
+ const ciWaitParked = !!sessionOpen && this.parkCiWait(runId, state);
5232
+ const completionBlocked = !ciWaitParked && !!done && this.deferParentCompletion(runId);
5233
+ const workerWaitParked = ciWaitParked || completionBlocked || (!!sessionOpen && this.parkWorkerWait(runId, state));
4703
5234
  state.doneAtBoundary = done ? state.session : undefined;
4704
5235
  state.parkAfterAck = interactive && sessionOpen && state.session ? { session: state.session, monitoring: !!monitoring } : undefined;
4705
5236
  const agentInputDelivered = !ask && !workerWaitParked && this.flushAgentInputs(runId);
@@ -4817,9 +5348,14 @@ export class RunManager {
4817
5348
  const runner = createRunner(stepBackend);
4818
5349
  state.agentSessionError = undefined;
4819
5350
  let session;
5351
+ const shouldAutoEnd = () => this.active.get(runId) !== state || state.session !== session ||
5352
+ (!this.workerWait(runId) && !this.store.getRun(runId)?.ciWait && !this.parentCompletionPending(runId) &&
5353
+ state.workerWakeTurn !== session && state.ciWakeTurn !== session && !state.agentInputFlight &&
5354
+ !this.hasQueuedAgentInputs(runId));
4820
5355
  state.currentStepId = step.id;
4821
5356
  this.beginUsageInvocation(runId, state, step.id);
4822
5357
  const delegation = this.provisionSession(runId, state);
5358
+ const ciTools = await this.provisionCiSession(runId, state);
4823
5359
  try {
4824
5360
  if (this.workerExecutionStopped(runId)) {
4825
5361
  state.cancelled = true;
@@ -4838,7 +5374,8 @@ export class RunManager {
4838
5374
  bashAllowlist: grants.bashAllowlist,
4839
5375
  // The handoff file lives outside the worktree — grant access.
4840
5376
  additionalDirectories: agentDirectories(join(this.dataDir, 'runs'), stepProfile.env),
4841
- env: { ...stepProfile.env, ...delegation?.env },
5377
+ env: { ...stepProfile.env, ...delegation?.env, ...ciTools?.env },
5378
+ ...(ciTools ? { cezarTools: ciTools.descriptor } : {}),
4842
5379
  ...(delegation ? { restrictNativeDelegation: delegation.restrictNativeDelegation } : {}),
4843
5380
  model: backendModel,
4844
5381
  effort: effectiveEffort,
@@ -4847,8 +5384,8 @@ export class RunManager {
4847
5384
  timeoutMs: interactive ? 0 : undefined,
4848
5385
  }, onEvent, {
4849
5386
  autoEndAfterFirstTurn: !interactive,
4850
- shouldAutoEnd: () => this.active.get(runId) !== state || state.session !== session ||
4851
- (!this.workerWait(runId) && !this.parentCompletionPending(runId) && state.workerWakeTurn !== session && !state.agentInputFlight),
5387
+ // Unread inbox claims must survive this window so release/expiry can replay them.
5388
+ shouldAutoEnd,
4852
5389
  onUiEvent: (event) => {
4853
5390
  completedAssistantText = appendCompletedAssistantText(completedAssistantText, event);
4854
5391
  this.handleRunnerUiEvent(runId, state, sink, event);
@@ -4857,6 +5394,8 @@ export class RunManager {
4857
5394
  });
4858
5395
  }
4859
5396
  catch (err) {
5397
+ ciTools?.revoke();
5398
+ state.revokeCiTools = undefined;
4860
5399
  delegation?.revoke();
4861
5400
  state.revokeDelegation = undefined;
4862
5401
  state.currentStepId = undefined;
@@ -4864,6 +5403,14 @@ export class RunManager {
4864
5403
  }
4865
5404
  const sessionClosed = this.trackWorkerSessionResult(runId, session);
4866
5405
  state.session = session;
5406
+ state.resumeInboxAutoEnd = interactive ? undefined : () => {
5407
+ // ACK may consume the last input after the runner's one-shot close timer fired.
5408
+ // Release/expiry instead submit a provider turn through normal admission.
5409
+ if (this.active.get(runId) === state && state.session === session && session?.open &&
5410
+ state.atTurnBoundary === session && !state.cancelled && !state.pendingHumanAsk &&
5411
+ !this.hasPendingHumanAsk(runId) && shouldAutoEnd())
5412
+ session.end();
5413
+ };
4867
5414
  state.sessionEverOpened = true;
4868
5415
  state.currentStepId = step.id;
4869
5416
  state.interrupt = () => session?.interrupt();
@@ -4906,6 +5453,8 @@ export class RunManager {
4906
5453
  return message;
4907
5454
  }
4908
5455
  finally {
5456
+ ciTools?.revoke();
5457
+ state.revokeCiTools = undefined;
4909
5458
  delegation?.revoke();
4910
5459
  state.revokeDelegation = undefined;
4911
5460
  this.recordUsagePeaks(runId);
@@ -4920,7 +5469,9 @@ export class RunManager {
4920
5469
  if (at >= 0)
4921
5470
  this.queue.splice(at, 1);
4922
5471
  }
5472
+ state.resumeInboxAutoEnd = undefined;
4923
5473
  state.workerWakeTurn = undefined;
5474
+ state.ciWakeTurn = undefined;
4924
5475
  state.atTurnBoundary = undefined;
4925
5476
  state.doneAtBoundary = undefined;
4926
5477
  state.parkAfterAck = undefined;
@@ -4962,6 +5513,7 @@ export class RunManager {
4962
5513
  /** Ask precedence also releases an admitted but not yet delivered worker wake.
4963
5514
  * Its durable input/wait remain queued until an actual human answer. */
4964
5515
  prepareHumanAsk(runId, state) {
5516
+ this.withdrawCiWait(runId);
4965
5517
  state.pendingHumanAsk = true;
4966
5518
  this.workerWaiting.delete(runId);
4967
5519
  this.workerWakeAdmitted.delete(runId);
@@ -5426,6 +5978,10 @@ export class RunManager {
5426
5978
  const run = this.store.getRun(runId);
5427
5979
  if (!run || run.stopping || this.workerExecutionStopped(runId) || this.executionBlockedByRootFinish(run))
5428
5980
  return;
5981
+ if (run.ciWait) {
5982
+ this.clearMonitoringWakeTimer(state, runId);
5983
+ return;
5984
+ }
5429
5985
  const minutes = this.semaphore.monitoringWakeIntervalMinutes();
5430
5986
  if (minutes === null) {
5431
5987
  this.clearMonitoringWakeTimer(state, runId);