@defrex/autobuild 1.13.0 → 1.15.0

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 (47) hide show
  1. package/README.md +1 -1
  2. package/SPEC.md +61 -11
  3. package/docs/answering-blocked-builds.md +5 -3
  4. package/docs/architecture.md +29 -0
  5. package/docs/configuration.md +16 -8
  6. package/docs/dispatcher-hosting.md +10 -0
  7. package/docs/operating.md +8 -0
  8. package/docs/operator-api.md +1 -1
  9. package/docs/operator-surfaces.md +1 -1
  10. package/docs/remote-store-protocol.md +1 -1
  11. package/docs/troubleshooting.md +35 -1
  12. package/package.json +3 -3
  13. package/packages/core/src/cli/dispatch.ts +105 -5
  14. package/packages/core/src/cli/repository-status.ts +15 -0
  15. package/packages/core/src/config/schema.ts +2 -2
  16. package/packages/core/src/events/policy.ts +2 -0
  17. package/packages/core/src/events/repository.ts +102 -0
  18. package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +4 -0
  19. package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/be211f0e31a65006.json.gz +0 -0
  20. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/23d33121de6d3325.json +107 -0
  21. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/312cc4090820a612.json +1078 -0
  22. package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a2780567b4989fb6.json +105 -0
  23. package/packages/core/src/kernel/done-escalations.ts +8 -4
  24. package/packages/core/src/kernel/reducer-revisions.ts +6 -0
  25. package/packages/core/src/kernel/reducer.ts +15 -1
  26. package/packages/core/src/kernel/repository-read-facts.ts +18 -0
  27. package/packages/core/src/ports/workspace/build-execution.ts +9 -1
  28. package/packages/core/src/ports/workspace/vercel-sandbox.ts +99 -12
  29. package/packages/core/src/processes/dispatcher.ts +957 -185
  30. package/packages/core/src/processes/execution-settlement.ts +24 -3
  31. package/packages/core/src/processes/pass-stages.ts +294 -0
  32. package/packages/core/src/processes/tick-publication.ts +1 -0
  33. package/packages/core/src/store/contract.ts +7 -0
  34. package/packages/core/src/store/reducer-value-shapes.json +5733 -5345
  35. package/packages/core/src/store/reducer-value-validation.ts +7 -0
  36. package/packages/core/src/store/remote/released-clients.json +22 -0
  37. package/packages/core/src/store/remote/released-reducer-values/1.14.0.json +36076 -0
  38. package/packages/core/src/store/remote/released-reducer-values/1.15.0.json +36354 -0
  39. package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
  40. package/packages/core/src/store/repo-state-events.ts +2 -0
  41. package/packages/core/src/testing/dispatch-read-budget.ts +12 -2
  42. package/packages/core/src/testing/reduced-read-budget.ts +405 -1
  43. package/skills/guide/SKILL.md +29 -9
  44. package/skills/guide/references/operator-api.md +1 -1
  45. package/skills/guide/references/remote-store-protocol.md +1 -1
  46. package/skills/operate/SKILL.md +64 -5
  47. package/templates/hosted-service/package.json +2 -2
@@ -0,0 +1,105 @@
1
+ {
2
+ "records": [
3
+ {
4
+ "name": "repositoryReadFacts",
5
+ "identity": "repositoryReadFacts",
6
+ "revisions": [
7
+ "repositoryAttention/fold/u6bhzf2af0",
8
+ "repositoryReadFacts/fold/4gnmhbji3n",
9
+ "repositoryReadFacts/fold/5porrk9y1h",
10
+ "repositoryReadFacts/fold/fk3ka9gb3z",
11
+ "repositoryReadFacts/fold/vvcfmd23zp",
12
+ "repositoryReadFacts/shape/5yo149tiqz",
13
+ "repositoryReadFacts/shape/7hbgb52kw8",
14
+ "repositoryReadFacts/shape/e2u2pgiup3",
15
+ "repositoryReadFacts/shape/hgw4spz5c0",
16
+ "repositoryReadFacts/shape/mggr8x6l5z",
17
+ "repositoryReadFacts/shape/o7791nz2gt",
18
+ "repositoryReadFacts/shape/se5kk44boc",
19
+ "repositoryReadFacts/shape/xyn7875cxw",
20
+ "sandboxStates/fold/hjdgop7xoh",
21
+ "sandboxStates/shape/gbrf8k4qxq",
22
+ "ticketStarts/fold/8fp156vze2",
23
+ "ticketStarts/fold/eglrhnb0ta",
24
+ "ticketStarts/shape/dzxrbuuq7e",
25
+ "ticketStarts/shape/eecofz22vy"
26
+ ],
27
+ "entries": {
28
+ "017d8dd5bb6dd80b": "9b19c8eb254d503fbe74d8de7039566f",
29
+ "10a4657408d4e121": "218f9c92364c4b9176b70c9bee8a29f8",
30
+ "1411d1cdbdd9911e": "a5b60fa55d3c1d3999e4ee947878bd23",
31
+ "1444fe1fa7fc802c": "81c89822174d63012aa06ad744fae9e6",
32
+ "178e12f1759b7552": "61197c43eb41d8451ce5182402f23373",
33
+ "1921f86f752428c9": "abf8d370c9d94b6a50ab70d52d33454b",
34
+ "1972c1d66008ef6b": "56c634f6d1a0076256c385b2a9feab7c",
35
+ "19e8e43e7f3f1a06": "b18b2ccd2d11b39684f2fc991526b0d6",
36
+ "21ce2c0bf58318e1": "8eedb8f516b89cb65230c9cedb1bd903",
37
+ "293dcb3a94fb3470": "203e641af713b42a0e5369f9486f2301",
38
+ "2ab7436356267118": "fc4031327a07006ccdc946bf07aba04f",
39
+ "2d084641735caaff": "1061299214f493d5d51f9c11afae68ab",
40
+ "3237bcecf68e5db9": "79f6d09249036fd77dc5d3400e18124a",
41
+ "33d0cc593610e56f": "bd88ed40c1ada79d8657af1b339a6a9f",
42
+ "35cf9ccc4478b2d2": "5a2279bc99403d0f7d40cede4c8bb573",
43
+ "3649e7bd1203f154": "9d3742c0e29c7a3e999c9c9a8c6eac27",
44
+ "38c75099d9191471": "203e641af713b42a0e5369f9486f2301",
45
+ "431c9bb2ee8838e0": "0715d441ad523323105867bf09108797",
46
+ "49064fc079964a5c": "844ca8b44174ab640ce17a713b580d7d",
47
+ "4932c70a2af97faf": "0c732c6ead43fcb8d5f34159ac5e0e0b",
48
+ "4f00c0f2c8afd159": "9688f560966f37d57b4177bf5aa4b7f0",
49
+ "516a5a7aadf97587": "e63f56f546ed51d4e04df1e86c9fc476",
50
+ "536085773daf6b58": "57901f1d28ee614272cc8915a80e38ff",
51
+ "59ffaa6974094c72": "d1757ea5344f9164aca22e3ca6ca165a",
52
+ "5f0ca42af862c4ee": "840d8a2c1947ef0941e1ab8bf3afd42e",
53
+ "6118031132db9bfc": "8c44d3ef5b8a0241b5c712f994d56907",
54
+ "643d70429ce03dad": "c2b7bfc98da1427b6a3858d814d3f57b",
55
+ "677ea92572c29082": "4067b95ee98f33bc298d71c7489fabba",
56
+ "6ae5bf60504d6467": "58975765e668c4f361659977261d56ed",
57
+ "6bd6275061aaffa5": "c84c2528779e3ecc21e549e0ef2adedd",
58
+ "6efdc8defc75fccb": "d5b512d4f4c16b9d4bcadbee8f8206ad",
59
+ "7833e42d16301424": "b32c86839cd9044a4d7757d51b62db14",
60
+ "7d4edad0614adaf1": "54a48a0b441082d54a07d786c217153e",
61
+ "837365898d04f45a": "54a48a0b441082d54a07d786c217153e",
62
+ "85f9f0c921b8b5bb": "0926918433e11ebd637e34dae2a2d615",
63
+ "8879f57a24733897": "58975765e668c4f361659977261d56ed",
64
+ "88cb9e71bdec972d": "3f7ed5a476e6457efedb7cdf6412fd23",
65
+ "89625654b02e9ac1": "bac4fc52c015d8cb7b9f833dda540b85",
66
+ "8cc15eb1807c0157": "10c221f0b9db3782b1dca2ed6eb1f5c9",
67
+ "8ced97c44bc0af3a": "d998291081ba51e1bef3205aae7595ef",
68
+ "8d4144004720b9f8": "ef6389a96f072b54e91a0e0f19c827b8",
69
+ "8db633ead1807f8b": "c4fe43ef3276b5877db1b08a38d607cb",
70
+ "8e42cdbd0994ab0c": "e12aa5b2f6373eceb51f303a929e8d5a",
71
+ "9539b995af5c54a9": "1061299214f493d5d51f9c11afae68ab",
72
+ "9ee3919fbd4f9458": "a553618f38080bfcb814cb4a5416cfb1",
73
+ "a0b32c12e4e6edb6": "d323237478cd65b159eda1faab172bcf",
74
+ "a55218252707bdd3": "54a48a0b441082d54a07d786c217153e",
75
+ "a98af134be2da8a9": "9a0830cc47de7a66b3c2f1a85a7eb852",
76
+ "af953392d81911da": "58975765e668c4f361659977261d56ed",
77
+ "b0d877c44810ecb0": "64a155224f6c8526212f92f64e7ef575",
78
+ "be211f0e31a65006": "64a155224f6c8526212f92f64e7ef575",
79
+ "c68b7ee2e2610f30": "05034ebd16ae9bf93b398663b30aea92",
80
+ "c7f7e0378aeea237": "1061299214f493d5d51f9c11afae68ab",
81
+ "c98cb1659eb142b6": "64a155224f6c8526212f92f64e7ef575",
82
+ "ca830adf06ed55a1": "54a48a0b441082d54a07d786c217153e",
83
+ "cb3a219f33e437ca": "feab7b027da5be2ecf041dd93fb55f1d",
84
+ "cf1e0c0d82567e0c": "cac9aba9f25adeb55568e902396839a4",
85
+ "d1432a819d8e48c5": "203e641af713b42a0e5369f9486f2301",
86
+ "d185ed6aa9192aff": "235467b1c87a46b383a7fa8550d8a2f8",
87
+ "d5d5ccca80a893d8": "0fdfd82c7426fd151bbb7544cd415187",
88
+ "d873fad29d6c7e55": "3f7ed5a476e6457efedb7cdf6412fd23",
89
+ "e2f18fa32fe1f6d0": "e5878dff98f5f30c5f82c1656fa82737",
90
+ "e37393fed29e84c5": "b32c86839cd9044a4d7757d51b62db14",
91
+ "e6a8585e2a054ff6": "ed2947f48b407a9a4f3bbfd151d8bc63",
92
+ "eb7ddd5319d09e27": "e7fa56dec140a7688e4a7d470b419aba",
93
+ "eca4959b2b87ae15": "5d66240a5fdec0ca59deafae16615bc5",
94
+ "ee10d7959ec4843c": "7a0b8bf4fb7a11ea26c5431bc205f96d",
95
+ "ef1b68347966ab26": "3f7ed5a476e6457efedb7cdf6412fd23",
96
+ "f3e55425cc41f230": "54a48a0b441082d54a07d786c217153e",
97
+ "f5b9ace9b59351e7": "3f7ed5a476e6457efedb7cdf6412fd23",
98
+ "f8155387eb5f375c": "5c119a1b47ade210ed5517f68b3babfc",
99
+ "fa8f1475a55be935": "8ecd3e416726e4af9b75f8eaa983f171",
100
+ "fb21116a15d0b1f1": "22c5ba7ef2445e8d6a9e7bbeb1b06d52",
101
+ "fe95444fbeabe592": "4b033ccb2ce1d4edc32bb44601049293"
102
+ }
103
+ }
104
+ ]
105
+ }
@@ -1,16 +1,18 @@
1
1
  /**
2
2
  * `doneEscalations`, a rollup (SPEC §7.7): every `done` build with an open
3
3
  * escalation, the post-completion escalations operator surfaces list until an
4
- * operator acknowledges them. Completion closes no escalation, so a build
5
- * completed while blocked (abort cleanup included) is listed too.
4
+ * operator acknowledges them. Completion closes no escalation, but the first
5
+ * `build.aborted` closes every escalation open then, so an aborted build is
6
+ * listed only for an escalation raised after its abort, such as a cleanup
7
+ * failure.
6
8
  *
7
9
  * - `doneEscalationEntry` is the build-scope reducer it reads: the build's
8
10
  * kind, its latest terminal fact and its open escalation ids.
9
11
  * It mirrors
10
12
  * exactly the build reducer's open-escalation rules (`kernel/reducer.ts`):
11
13
  * raises, deferrals, janitor escalations and ticket waits open; answers,
12
- * wait ends, janitor recovery, `wait-unresolvable` and verify supersession
13
- * close. A tiny reducer of its own keeps the rollup's version independent of
14
+ * wait ends, janitor recovery, `wait-unresolvable`, verify supersession and
15
+ * the first abort close. A tiny reducer of its own keeps the rollup's version independent of
14
16
  * the build reducer's frequent revisions, so a `build` revision never
15
17
  * re-reduces every finished build in every project.
16
18
  * - The accumulator holds only the listed builds; nothing expires.
@@ -142,6 +144,8 @@ export const doneEscalationEntryReducer = defineReducer<EntryAcc, AbEvent, DoneE
142
144
  acc.terminal = 'done'
143
145
  break
144
146
  case 'build.aborted':
147
+ // The first abort closes every escalation open then (§15.5).
148
+ if (acc.terminal === undefined) acc.open = []
145
149
  acc.terminal = 'aborted'
146
150
  break
147
151
  case 'phase.deferral-escalated':
@@ -92,4 +92,10 @@ export const REDUCER_REVISIONS: Readonly<Record<string, string>> = {
92
92
  'ticketStarts/fold/8fp156vze2': '#1354 high and low levels refine build and start priority',
93
93
  'doneEscalationEntry/shape/4d674ku017': 'Track outcome; abort cleanup is never listed',
94
94
  'doneEscalationEntry/shape/j7xarl33s3': 'Drop outcome; completion closes nothing',
95
+ 'build/fold/gsed6uivnv': '#1387 the first abort closes the escalations open then',
96
+ 'doneEscalationEntry/fold/9mnmr74oat': '#1387 the first abort closes the escalations open then',
97
+ 'repositoryReadFacts/shape/e2u2pgiup3': '#1406 deferred work hint and recoveryDeferred counter',
98
+ 'repositoryReadFacts/fold/5porrk9y1h': '#1406 latest dispatcher.work-deferred is carried forward',
99
+ 'repositoryReadFacts/shape/ez3eddcdoo': '#1409 pass cursors and stage timings',
100
+ 'repositoryReadFacts/fold/0xl1c98cpy': '#1409 latest dispatcher.pass-cursors is carried forward',
95
101
  }
@@ -407,7 +407,8 @@ export interface BuildState {
407
407
  * clear it: the phase still needs re-running (§15.6-B/C). */
408
408
  currentPhase?: PhaseContext
409
409
  lastCompletedPhase?: PhaseContext
410
- /** In raise order. Non-empty ≡ blocked (§15.5). */
410
+ /** In raise order. Non-empty ≡ blocked (§15.5). The first
411
+ * `build.aborted` closes every entry open then, without answering it. */
411
412
  openEscalations: OpenEscalation[]
412
413
  /** In answer order, with resolutions for engine routing (§15.6-B). */
413
414
  answeredEscalations: AnsweredEscalation[]
@@ -435,6 +436,8 @@ export interface BuildState {
435
436
  /** seq of the first `target.rewritten` of the open episode: the run of
436
437
  * rewrites with the same branch and rejected baseline. */
437
438
  targetRewriteSince?: number
439
+ /** The open janitor failure episode. `build.janitor-recovered` and the
440
+ * first `build.aborted` end it. */
438
441
  janitor?: {
439
442
  firstFailureAt: string
440
443
  attempt: number
@@ -687,6 +690,8 @@ export interface BuildAcc {
687
690
  /** seq of the first `target.rewritten` of the open episode: the run of
688
691
  * rewrites with the same branch and rejected baseline. */
689
692
  targetRewriteSince?: number
693
+ /** The open janitor failure episode. `build.janitor-recovered` and the
694
+ * first `build.aborted` end it. */
690
695
  janitor?: {
691
696
  firstFailureAt: string
692
697
  attempt: number
@@ -1330,6 +1335,15 @@ function foldBuild(acc: BuildAcc, events: readonly AbEvent[]): void {
1330
1335
  pending.resume = []
1331
1336
  break
1332
1337
  case 'build.aborted':
1338
+ // §15.5: the first `build.aborted` accepts the abort. It closes every
1339
+ // escalation open at that moment, janitor ones included, without an
1340
+ // answer, and ends the janitor failure episode, so a cleanup failure
1341
+ // after it escalates under a new id. A repeated abort fact, or one
1342
+ // after completion, closes nothing.
1343
+ if (terminal === undefined) {
1344
+ openEscalations.clear()
1345
+ delete acc.janitor
1346
+ }
1333
1347
  terminal = 'aborted'
1334
1348
  pending.abort = []
1335
1349
  break
@@ -88,6 +88,18 @@ export interface RepositoryReadAcc {
88
88
  dispatch: ReportedDispatch
89
89
  scanSeq?: number
90
90
  scan?: Extract<RepositoryEvent, { type: 'dispatcher.ticket-scan-completed' }>['payload']
91
+ /** The latest `dispatcher.work-deferred` hint: builds the next tick sweeps
92
+ * first. */
93
+ deferredWork?: {
94
+ seq: number
95
+ builds: Extract<RepositoryEvent, { type: 'dispatcher.work-deferred' }>['payload']['builds']
96
+ }
97
+ /** The latest `dispatcher.pass-cursors` hint: where each rotated loop of
98
+ * the next bounded pass continues. */
99
+ passCursors?: {
100
+ seq: number
101
+ cursors: Extract<RepositoryEvent, { type: 'dispatcher.pass-cursors' }>['payload']['cursors']
102
+ }
91
103
  recentRuns: [string, DispatcherRunFacts][]
92
104
  latestRun?: string
93
105
  latestStoppedRun?: string
@@ -246,6 +258,10 @@ export const repositoryReadFactsReducer = defineReducer({
246
258
  acc.scan = event.payload
247
259
  acc.scanSeq = event.seq
248
260
  }
261
+ if (event.type === 'dispatcher.work-deferred')
262
+ acc.deferredWork = { seq: event.seq, builds: event.payload.builds }
263
+ if (event.type === 'dispatcher.pass-cursors')
264
+ acc.passCursors = { seq: event.seq, cursors: event.payload.cursors }
249
265
  if (event.type === 'dispatcher.run-started') acc.latestRun = event.payload.run
250
266
  if (event.type === 'dispatcher.run-stopped') acc.latestStoppedRun = event.payload.run
251
267
  if (
@@ -313,6 +329,8 @@ export const repositoryReadFactsReducer = defineReducer({
313
329
  : undefined,
314
330
  scan: acc.scan,
315
331
  scanSeq: acc.scanSeq,
332
+ deferredWork: acc.deferredWork === undefined ? undefined : structuredClone(acc.deferredWork),
333
+ passCursors: acc.passCursors === undefined ? undefined : structuredClone(acc.passCursors),
316
334
  recentRuns: structuredClone(acc.recentRuns),
317
335
  latestRun: acc.latestRun,
318
336
  latestStoppedRun: acc.latestStoppedRun,
@@ -1,3 +1,5 @@
1
+ import type { WorkspaceRecoveryStep } from './recovery-step'
2
+
1
3
  /** Immutable identity passed when a workspace-adjacent executor is asked to
2
4
  * start a build. Build state, workspace location, config, progress, and outcome
3
5
  * are deliberately absent: both sides obtain those only from the BuildStore. */
@@ -112,7 +114,13 @@ export interface BuildExecutionHandle {
112
114
  * exit code; `lost` means the provider proved the environment/command no
113
115
  * longer exists without an exit code. */
114
116
  export type ExecutionObservation = (
115
- | { state: 'running' }
117
+ | {
118
+ state: 'running'
119
+ /** Observation could not finish within the invocation budget; this
120
+ * proves neither liveness nor exit, and the caller retries. `step`
121
+ * names the recovery step it waited on. */
122
+ deferred?: { step: WorkspaceRecoveryStep; detail: string }
123
+ }
116
124
  | { state: 'ended'; exitCode?: number }
117
125
  | { state: 'lost' }
118
126
  ) & {
@@ -16,6 +16,7 @@ import { setTimeout as sleep } from 'node:timers/promises'
16
16
  import { EXECUTION_EXIT_FILE_ENV, parseExecutionExit } from './execution-exit-marker'
17
17
  import {
18
18
  isRecoveryDeferred,
19
+ recoveryStepOf,
19
20
  WorkspaceRecoveryDeferred,
20
21
  WorkspaceRecoveryStepError,
21
22
  type WorkspaceRecoveryStep,
@@ -271,10 +272,17 @@ export const VERCEL_OBSERVE_WAIT_MS = 5_000
271
272
  * finishes. */
272
273
  export const VERCEL_PROVISION_ESTIMATE_MS = 150_000
273
274
 
274
- /** Default budget for one stopped-sandbox recovery: an explicit cold resume
275
- * plus the guest git work that follows it (`recoveryTimeoutMs`). */
275
+ /** Default ceiling for one stopped-sandbox recovery: an explicit cold resume
276
+ * plus the guest git work that follows it (`recoveryTimeoutMs`). A bounded
277
+ * invocation admits a recovery on `VERCEL_RECOVERY_ESTIMATE_MS`, not on this
278
+ * ceiling. */
276
279
  export const VERCEL_RECOVERY_TIMEOUT_MS = 120_000
277
280
 
281
+ /** Expected wall time of one stopped-sandbox recovery: a cold resume plus
282
+ * guest git. A bounded invocation starts a recovery when this plus the
283
+ * cleanup reserve fits; `recoveryTimeoutMs` stays the ceiling. */
284
+ export const VERCEL_RECOVERY_ESTIMATE_MS = 45_000
285
+
278
286
  /** Interval between session polls while a resume or stop settles. */
279
287
  const SESSION_POLL_MS = 500
280
288
 
@@ -1576,6 +1584,10 @@ export interface VercelSandboxProviderOptions {
1576
1584
  /** Test seam: the expected wall time of one `provision`. Defaults to
1577
1585
  * `VERCEL_PROVISION_ESTIMATE_MS`. */
1578
1586
  provisionEstimateMs?: number
1587
+ /** Test seam: the expected wall time of one stopped-sandbox recovery, used
1588
+ * to admit it in a bounded invocation. Defaults to
1589
+ * `VERCEL_RECOVERY_ESTIMATE_MS`; `recoveryTimeoutMs` stays the ceiling. */
1590
+ recoveryEstimateMs?: number
1579
1591
  /** The repository's raw `[commands].setup` shell string, run once at the end
1580
1592
  * of a fresh operator-sandbox provision (never on resume). Set once at
1581
1593
  * construction; never per call. */
@@ -1650,6 +1662,7 @@ export class VercelSandboxProvider implements WorkspaceProvider {
1650
1662
  private readonly sessions = new Map<string, VercelSandboxHandle>()
1651
1663
  private readonly observeWaitMs: number
1652
1664
  readonly provisionEstimateMs: number
1665
+ private readonly recoveryEstimateMs: number
1653
1666
 
1654
1667
  /** The archive bytes this provider installs: the injected `packageArchive`
1655
1668
  * seam when set, else the per-process memoized running distribution
@@ -1687,6 +1700,7 @@ export class VercelSandboxProvider implements WorkspaceProvider {
1687
1700
  // Constructor-body assignment, never a field initializer (see the
1688
1701
  // `useDefineForClassFields` note on `sandboxPublication`).
1689
1702
  this.provisionEstimateMs = options.provisionEstimateMs ?? VERCEL_PROVISION_ESTIMATE_MS
1703
+ this.recoveryEstimateMs = options.recoveryEstimateMs ?? VERCEL_RECOVERY_ESTIMATE_MS
1690
1704
  this.buildExecution = {
1691
1705
  start: (input, auth) => this.start(input, auth),
1692
1706
  observe: (identity) =>
@@ -2252,12 +2266,44 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2252
2266
  return VercelSandboxProvider.bounded(this.recoveryTimeoutMs(), this.recoveryWorkBound())
2253
2267
  }
2254
2268
 
2255
- /** Whether a cold resume plus its cleanup still fits before the
2256
- * invocation deadline. Always true without one. */
2269
+ /** The time a recovery needs left before the invocation deadline to start:
2270
+ * a realistic resume (never more than the `recoveryTimeoutMs` ceiling)
2271
+ * plus the cleanup reserve. */
2272
+ private recoveryAdmissionMs(): number {
2273
+ return Math.min(this.recoveryEstimateMs, this.recoveryTimeoutMs()) + this.cleanupReserveMs()
2274
+ }
2275
+
2276
+ /** Whether a realistic cold resume plus its cleanup still fits before the
2277
+ * invocation deadline. Always true without one. Admission uses the
2278
+ * estimate; `recoveryTimeoutMs` stays the ceiling on the recovery itself,
2279
+ * and the deadline bounds it further. */
2257
2280
  private recoveryBudgetAvailable(): boolean {
2258
2281
  const deadline = this.options.invocationDeadlineAt
2259
2282
  if (deadline === undefined) return true
2260
- return deadline - Date.now() >= this.recoveryTimeoutMs() + this.cleanupReserveMs()
2283
+ return deadline - Date.now() >= this.recoveryAdmissionMs()
2284
+ }
2285
+
2286
+ /** Leave a session a cut-short recovery resumed (or may have resumed)
2287
+ * stopped, or marked uncertain for a later fence: never running
2288
+ * unsupervised. Runs in the reserved cleanup window and never throws. */
2289
+ private async stopAfterCutRecovery(ref: string, handle?: VercelSandboxHandle): Promise<void> {
2290
+ try {
2291
+ const sandbox = handle ?? (await this.facade.get(ref, this.recoveryMetadataSignal()))
2292
+ if (sandbox === null) {
2293
+ this.forget(ref)
2294
+ return
2295
+ }
2296
+ const status = sandbox.sessionStatus
2297
+ if (status === 'stopped' || status === 'stopping' || status === 'snapshotting') {
2298
+ this.sessions.delete(ref)
2299
+ return
2300
+ }
2301
+ await this.recoveryStop(ref, sandbox)
2302
+ this.sessions.delete(ref)
2303
+ } catch {
2304
+ this.uncertain.add(ref)
2305
+ this.sessions.delete(ref)
2306
+ }
2261
2307
  }
2262
2308
 
2263
2309
  private remainingMs(): number {
@@ -2324,7 +2370,7 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2324
2370
  if (!this.recoveryBudgetAvailable())
2325
2371
  throw new WorkspaceRecoveryDeferred('resume', {
2326
2372
  remainingMs: this.remainingMs(),
2327
- requiredMs: this.recoveryTimeoutMs() + this.cleanupReserveMs(),
2373
+ requiredMs: this.recoveryAdmissionMs(),
2328
2374
  })
2329
2375
  if (this.facade.resume === undefined)
2330
2376
  throw new WorkspaceRecoveryStepError(
@@ -2336,7 +2382,10 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2336
2382
  resumed = await this.facade.resume(ref, signal)
2337
2383
  } catch (error) {
2338
2384
  if (isMissingVercelSandbox(error)) throw error
2339
- if (this.deadlineAbort(error))
2385
+ if (this.deadlineAbort(error)) {
2386
+ // The resume may still be completing provider-side: stop it inside
2387
+ // the reserved cleanup window, or leave the ref uncertain.
2388
+ await this.stopAfterCutRecovery(ref)
2340
2389
  throw new WorkspaceRecoveryDeferred(
2341
2390
  'resume',
2342
2391
  { remainingMs: this.remainingMs() },
@@ -2344,6 +2393,7 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2344
2393
  cause: error,
2345
2394
  },
2346
2395
  )
2396
+ }
2347
2397
  throw new WorkspaceRecoveryStepError(
2348
2398
  'resume',
2349
2399
  signal.aborted
@@ -2944,8 +2994,9 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2944
2994
  * before exposing a terminal observation. Command exit alone does not prove
2945
2995
  * descendants have stopped writing to the shared workspace. Stop failures
2946
2996
  * propagate so settlement retains the execution and lease and retries. A
2947
- * call the invocation deadline cuts short reads as `running`: nothing is
2948
- * settled and the next invocation observes again. */
2997
+ * call the invocation deadline cuts short, or one the budget cannot admit,
2998
+ * reads as `running` marked `deferred` with the step it waited on: nothing
2999
+ * is settled, nothing is charged, and the next invocation observes again. */
2949
3000
  private async observeExecution(identity: BuildExecutionIdentity): Promise<ExecutionObservation> {
2950
3001
  if (identity.commandId === undefined) {
2951
3002
  throw new Error(
@@ -2955,7 +3006,14 @@ export class VercelSandboxProvider implements WorkspaceProvider {
2955
3006
  try {
2956
3007
  return await this.observeOnce(identity, identity.commandId)
2957
3008
  } catch (error) {
2958
- if (isRecoveryDeferred(error) || this.deadlineAbort(error)) return { state: 'running' }
3009
+ if (isRecoveryDeferred(error) || this.deadlineAbort(error))
3010
+ return {
3011
+ state: 'running',
3012
+ deferred: {
3013
+ step: recoveryStepOf(error) ?? 'fence',
3014
+ detail: error instanceof Error ? error.message : String(error),
3015
+ },
3016
+ }
2959
3017
  throw error
2960
3018
  }
2961
3019
  }
@@ -3050,7 +3108,14 @@ export class VercelSandboxProvider implements WorkspaceProvider {
3050
3108
  { signal: this.recoveryMetadataSignal() },
3051
3109
  )
3052
3110
  } catch (error) {
3053
- if (this.deadlineAbort(error)) return { state: 'running' }
3111
+ if (this.deadlineAbort(error))
3112
+ return {
3113
+ state: 'running',
3114
+ deferred: {
3115
+ step: 'fence',
3116
+ detail: `renewing the account policy of sandbox ${ref} stopped at the invocation deadline; retrying next invocation`,
3117
+ },
3118
+ }
3054
3119
  await sandbox.stop({ signal: this.recoveryMetadataSignal() })
3055
3120
  return { state: 'lost', attention: 'Account policy renewal failed; sandbox stopped' }
3056
3121
  }
@@ -3111,7 +3176,17 @@ export class VercelSandboxProvider implements WorkspaceProvider {
3111
3176
  let handle = sandbox
3112
3177
  let resumedHere = false
3113
3178
  if (sandbox.sessionStatus !== undefined && sandbox.sessionStatus !== 'running') {
3114
- if (!this.recoveryBudgetAvailable()) return { state: 'running' }
3179
+ if (!this.recoveryBudgetAvailable())
3180
+ return {
3181
+ state: 'running',
3182
+ deferred: {
3183
+ step: 'resume',
3184
+ detail: new WorkspaceRecoveryDeferred('resume', {
3185
+ remainingMs: this.remainingMs(),
3186
+ requiredMs: this.recoveryAdmissionMs(),
3187
+ }).message,
3188
+ },
3189
+ }
3115
3190
  let resumed: VercelSandboxHandle | null
3116
3191
  try {
3117
3192
  resumed = await this.resumeForRecovery(ref, sandbox)
@@ -3144,6 +3219,18 @@ export class VercelSandboxProvider implements WorkspaceProvider {
3144
3219
  )
3145
3220
  } catch (error) {
3146
3221
  if (isMissingVercelSandbox(error)) return { state: 'lost' }
3222
+ // A session this call resumed is never left running after a read that
3223
+ // failed or that the deadline cut short; a session it did not resume
3224
+ // belongs to someone else and is never stopped here.
3225
+ if (resumedHere) await this.stopAfterCutRecovery(ref, handle)
3226
+ if (this.deadlineAbort(error))
3227
+ return {
3228
+ state: 'running',
3229
+ deferred: {
3230
+ step: 'fence',
3231
+ detail: `reading the exit record of execution ${instance} stopped at the invocation deadline; retrying next invocation`,
3232
+ },
3233
+ }
3147
3234
  throw error
3148
3235
  }
3149
3236
  if (exitCode !== null) return this.settledObservation(ref, handle, { state: 'ended', exitCode })