@defrex/autobuild 1.10.0 → 1.11.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.
- package/README.md +1 -1
- package/SPEC.md +738 -68
- package/bin/ab-dispatch-generation.ts +3 -1
- package/docs/accounts.md +2 -1
- package/docs/answering-blocked-builds.md +20 -0
- package/docs/architecture.md +155 -25
- package/docs/build-store.md +10 -7
- package/docs/cli.md +33 -1
- package/docs/configuration-overview.md +4 -1
- package/docs/configuration.md +381 -29
- package/docs/dispatcher.md +63 -4
- package/docs/forges.md +7 -0
- package/docs/integration-tree.md +2 -2
- package/docs/operating.md +4 -2
- package/docs/operator-api.md +134 -6
- package/docs/operator-surfaces.md +85 -0
- package/docs/optimization-nodes.md +69 -11
- package/docs/plugin-authoring.md +37 -1
- package/docs/remote-store-protocol.md +94 -13
- package/docs/remote-ticket-protocol.md +40 -0
- package/docs/restacking.md +20 -4
- package/docs/setup.md +14 -2
- package/docs/ticket-dependencies.md +10 -3
- package/docs/ticket-sources.md +24 -2
- package/docs/tickets.md +7 -1
- package/docs/troubleshooting.md +8 -0
- package/docs/verification.md +4 -3
- package/docs/workspaces.md +3 -0
- package/package.json +9 -5
- package/packages/core/src/cli/accounts.ts +7 -2
- package/packages/core/src/cli/attention.ts +3 -1
- package/packages/core/src/cli/binary-direct.ts +2 -0
- package/packages/core/src/cli/binary-lifecycle.ts +2 -1
- package/packages/core/src/cli/build-control.ts +79 -1
- package/packages/core/src/cli/context.ts +4 -0
- package/packages/core/src/cli/dashboard/facts.ts +49 -53
- package/packages/core/src/cli/dashboard/header.ts +101 -0
- package/packages/core/src/cli/dashboard/model.ts +31 -39
- package/packages/core/src/cli/dashboard/objective-row.ts +46 -14
- package/packages/core/src/cli/dashboard/poll.ts +178 -76
- package/packages/core/src/cli/dashboard/render.ts +129 -230
- package/packages/core/src/cli/dashboard/row-text.ts +538 -0
- package/packages/core/src/cli/dashboard/timeline.ts +288 -0
- package/packages/core/src/cli/dashboard/vocabulary.ts +188 -0
- package/packages/core/src/cli/dispatch-args.ts +44 -1
- package/packages/core/src/cli/dispatch-frontend.ts +29 -38
- package/packages/core/src/cli/dispatch-launch-stop.ts +18 -0
- package/packages/core/src/cli/dispatch-launcher.ts +20 -7
- package/packages/core/src/cli/dispatch-web.ts +155 -0
- package/packages/core/src/cli/dispatch.ts +200 -34
- package/packages/core/src/cli/help.ts +87 -3
- package/packages/core/src/cli/init-validation.ts +3 -3
- package/packages/core/src/cli/main.ts +70 -6
- package/packages/core/src/cli/mcp.ts +35 -3
- package/packages/core/src/cli/objective.ts +1 -1
- package/packages/core/src/cli/operator-preference.ts +57 -0
- package/packages/core/src/cli/repo-lease.ts +2 -0
- package/packages/core/src/cli/repo-state.ts +56 -6
- package/packages/core/src/cli/repository-status.ts +97 -43
- package/packages/core/src/cli/status.ts +82 -1
- package/packages/core/src/cli/terminal-restore.ts +32 -7
- package/packages/core/src/cli/ticket.ts +98 -36
- package/packages/core/src/cli/wait.ts +14 -6
- package/packages/core/src/cli/watch.ts +20 -4
- package/packages/core/src/cli/web.ts +139 -0
- package/packages/core/src/config/live.ts +99 -14
- package/packages/core/src/config/load.ts +61 -20
- package/packages/core/src/config/local.ts +212 -0
- package/packages/core/src/config/pipeline-source.ts +2 -0
- package/packages/core/src/config/reference.ts +18 -0
- package/packages/core/src/config/schema.ts +45 -10
- package/packages/core/src/events/catalog.ts +4 -0
- package/packages/core/src/events/payloads.ts +97 -0
- package/packages/core/src/events/policy.ts +10 -0
- package/packages/core/src/events/repository.ts +46 -3
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +316 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/017d8dd5bb6dd80b.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/178e12f1759b7552.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/4932c70a2af97faf.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/536085773daf6b58.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/6bd6275061aaffa5.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/89625654b02e9ac1.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/a98af134be2da8a9.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/d5d5ccca80a893d8.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-corpora/f8155387eb5f375c.json.gz +0 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3c27f918b194e3b3.json +465 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/79f0e0f4ccd43ba2.json +455 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/859c09edac738823.json +1086 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a283c1f3462b1cc1.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a6e570fb126991ff.json +36 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a8acf22c968b6e3d.json +81 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/ba7b85895bf6698e.json +1153 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/fc66408cde0b6b04.json +637 -0
- package/packages/core/src/kernel/build-roster.ts +230 -0
- package/packages/core/src/kernel/dispatch-capacity.ts +4 -2
- package/packages/core/src/kernel/engine.ts +43 -5
- package/packages/core/src/kernel/execution-intervals.ts +61 -0
- package/packages/core/src/kernel/generators/optimization-log.ts +101 -0
- package/packages/core/src/kernel/generators/repository-journals.ts +13 -0
- package/packages/core/src/kernel/leaf-restack-base.ts +33 -7
- package/packages/core/src/kernel/leaf-restack-state.ts +82 -3
- package/packages/core/src/kernel/log-index.ts +19 -0
- package/packages/core/src/kernel/optimization-admission.ts +244 -3
- package/packages/core/src/kernel/optimization.ts +183 -4
- package/packages/core/src/kernel/phase-failures.ts +121 -0
- package/packages/core/src/kernel/published-branch-head.ts +60 -0
- package/packages/core/src/kernel/reducer-registry.ts +27 -8
- package/packages/core/src/kernel/reducer-revisions.ts +25 -0
- package/packages/core/src/kernel/reducer.ts +63 -4
- package/packages/core/src/kernel/rollup.ts +61 -0
- package/packages/core/src/kernel/setup-streak.ts +38 -0
- package/packages/core/src/kernel/step-durations.ts +257 -0
- package/packages/core/src/kernel/target-observation.ts +6 -1
- package/packages/core/src/kernel/target-rewrite-recovery.ts +116 -0
- package/packages/core/src/kernel/verify-diff-base.ts +5 -0
- package/packages/core/src/kernel/verify-stages.ts +13 -3
- package/packages/core/src/ontology.ts +5 -0
- package/packages/core/src/operator/agent-context-serve.ts +631 -0
- package/packages/core/src/operator/annotations.ts +75 -5
- package/packages/core/src/operator/attention.ts +470 -0
- package/packages/core/src/operator/canonical-skills.ts +35 -0
- package/packages/core/src/operator/cli-equivalents.ts +270 -0
- package/packages/core/src/operator/context-prompts.ts +61 -0
- package/packages/core/src/operator/index.ts +49 -1
- package/packages/core/src/operator/landings.ts +28 -19
- package/packages/core/src/operator/optimization.ts +7 -0
- package/packages/core/src/operator/protocol.ts +3 -0
- package/packages/core/src/operator/query.ts +122 -72
- package/packages/core/src/operator/read-view.ts +5 -2
- package/packages/core/src/operator/registry.ts +406 -46
- package/packages/core/src/operator/requests.ts +20 -18
- package/packages/core/src/operator/sandbox-host.ts +107 -11
- package/packages/core/src/operator/sandbox-resolver.ts +253 -0
- package/packages/core/src/operator/sandbox.ts +778 -82
- package/packages/core/src/operator/ticket-asset-list.ts +77 -0
- package/packages/core/src/operator/ticket-assets.ts +167 -0
- package/packages/core/src/operator/tickets.ts +33 -2
- package/packages/core/src/plugin-sdk/index.ts +22 -0
- package/packages/core/src/plugins/manifest.ts +1 -1
- package/packages/core/src/ports/forge/fake.ts +92 -1
- package/packages/core/src/ports/forge/git-blob.ts +15 -0
- package/packages/core/src/ports/forge/github.ts +82 -0
- package/packages/core/src/ports/forge/local-git.ts +61 -1
- package/packages/core/src/ports/forge-failure.ts +24 -0
- package/packages/core/src/ports/runner/antigravity-test.fixture.ts +149 -0
- package/packages/core/src/ports/runner/claude-test.fixture.ts +71 -0
- package/packages/core/src/ports/runner/codex-test.fixture.ts +63 -0
- package/packages/core/src/ports/tickets/contract.ts +71 -0
- package/packages/core/src/ports/tickets/fake.ts +11 -0
- package/packages/core/src/ports/tickets/file.ts +18 -0
- package/packages/core/src/ports/tickets/github-fake.ts +136 -4
- package/packages/core/src/ports/tickets/github.ts +387 -30
- package/packages/core/src/ports/tickets/hosted-index.ts +10 -0
- package/packages/core/src/ports/tickets/linear.ts +130 -0
- package/packages/core/src/ports/tickets/priority.ts +9 -0
- package/packages/core/src/ports/tickets/remote-protocol.ts +34 -0
- package/packages/core/src/ports/tickets/remote.ts +70 -6
- package/packages/core/src/ports/tickets/search.ts +139 -0
- package/packages/core/src/ports/types.ts +104 -2
- package/packages/core/src/ports/workspace/builtin-capabilities.ts +9 -0
- package/packages/core/src/ports/workspace/create.ts +15 -1
- package/packages/core/src/ports/workspace/fake.ts +340 -12
- package/packages/core/src/ports/workspace/git-worktree.ts +833 -42
- package/packages/core/src/ports/workspace/operator-sandbox.ts +129 -12
- package/packages/core/src/ports/workspace/process-group.ts +48 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +1260 -123
- package/packages/core/src/processes/agent-context-assemble.ts +576 -0
- package/packages/core/src/processes/agent-context.ts +469 -0
- package/packages/core/src/processes/build-runner.ts +387 -236
- package/packages/core/src/processes/dispatcher.ts +211 -55
- package/packages/core/src/processes/execution-no-progress.ts +4 -0
- package/packages/core/src/processes/optimization-activity.ts +120 -0
- package/packages/core/src/processes/phase-failure-budget.ts +1 -0
- package/packages/core/src/processes/repo-view.ts +78 -2
- package/packages/core/src/processes/sandbox-state.ts +53 -6
- package/packages/core/src/processes/tick-publication.ts +2 -0
- package/packages/core/src/processes/workspace-preparation.ts +76 -0
- package/packages/core/src/scenarios/harness.ts +0 -5
- package/packages/core/src/store/account-redaction.ts +1 -0
- package/packages/core/src/store/adapter.ts +52 -1
- package/packages/core/src/store/agent-context.ts +432 -0
- package/packages/core/src/store/build-feed.ts +291 -0
- package/packages/core/src/store/build-scope.ts +19 -1
- package/packages/core/src/store/contract.ts +911 -14
- package/packages/core/src/store/digest.ts +19 -6
- package/packages/core/src/store/histogram.ts +113 -0
- package/packages/core/src/store/local/schema.ts +62 -0
- package/packages/core/src/store/local/store.ts +628 -22
- package/packages/core/src/store/local/ticket-asset-heads.ts +23 -0
- package/packages/core/src/store/memory.ts +461 -69
- package/packages/core/src/store/open-builds.ts +19 -0
- package/packages/core/src/store/phase-session.ts +32 -4
- package/packages/core/src/store/reduced-reads.ts +14 -4
- package/packages/core/src/store/reducer-value-shapes.json +6987 -5749
- package/packages/core/src/store/reducer-values.ts +6 -1
- package/packages/core/src/store/remote/client.ts +208 -2
- package/packages/core/src/store/remote/compatibility.ts +1 -1
- package/packages/core/src/store/remote/index.ts +33 -0
- package/packages/core/src/store/remote/protocol.ts +69 -0
- package/packages/core/src/store/remote/released-clients.json +11 -0
- package/packages/core/src/store/remote/released-reducer-values/1.11.0.json +35680 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +18 -16
- package/packages/core/src/store/retention.ts +23 -1
- package/packages/core/src/store/rollups.ts +337 -0
- package/packages/core/src/store/session-handle.ts +20 -2
- package/packages/core/src/store/snapshots.ts +10 -0
- package/packages/core/src/store/types.ts +108 -9
- package/packages/core/src/testing/dispatch-read-budget.ts +1092 -2
- package/packages/core/src/testing/git-config-isolation.ts +7 -5
- package/packages/core/src/testing/index.ts +27 -0
- package/packages/core/src/testing/memory-operator-sandbox.ts +346 -0
- package/packages/core/src/testing/reduced-read-budget.ts +1873 -98
- package/packages/core/src/web/analytics-source.ts +303 -0
- package/packages/core/src/web/assets/Atkinson-OFL.txt +93 -0
- package/packages/core/src/web/assets/AtkinsonHyperlegibleNext.latin.woff2 +0 -0
- package/packages/core/src/web/assets/JetBrains-OFL.txt +93 -0
- package/packages/core/src/web/assets/JetBrainsMono.latin.woff2 +0 -0
- package/packages/core/src/web/assets/app.css +1 -0
- package/packages/core/src/web/assets/app.js +9 -0
- package/packages/core/src/web/assets/manifest.json +31 -0
- package/packages/core/src/web/assets/theme.js +1 -0
- package/packages/core/src/web/bounded.ts +31 -0
- package/packages/core/src/web/http.ts +315 -0
- package/packages/core/src/web/hub.ts +160 -0
- package/packages/core/src/web/options.ts +38 -0
- package/packages/core/src/web/overview-source.ts +322 -0
- package/packages/core/src/web/page.ts +56 -0
- package/packages/core/src/web/protocol.ts +151 -0
- package/packages/core/src/web/read-only.ts +23 -0
- package/packages/core/src/web/repository-source.ts +35 -0
- package/packages/core/src/web/server.ts +97 -0
- package/skills/code-review/SKILL.md +10 -2
- package/skills/guide/SKILL.md +366 -52
- package/skills/guide/references/accounts.md +2 -1
- package/skills/guide/references/integration-tree.md +2 -2
- package/skills/guide/references/operator-api.md +134 -6
- package/skills/guide/references/optimization-nodes.md +69 -11
- package/skills/guide/references/plugin-authoring.md +37 -1
- package/skills/guide/references/remote-store-protocol.md +94 -13
- package/skills/guide/references/remote-ticket-protocol.md +40 -0
- package/skills/guide/references/restacking.md +20 -4
- package/skills/guide/references/setup.md +14 -2
- package/skills/guide/references/ticket-dependencies.md +10 -3
- package/skills/implement/SKILL.md +23 -4
- package/skills/operate/SKILL.md +134 -32
- package/skills/plan/SKILL.md +10 -4
- package/skills/plan-review/SKILL.md +9 -2
- package/skills/spec/SKILL.md +34 -8
- package/skills/tickets/SKILL.md +51 -5
- package/templates/hosted-service/package.json +2 -2
|
@@ -6,6 +6,26 @@
|
|
|
6
6
|
* derives lifecycle state from the repository journal (`sandboxStates`), so
|
|
7
7
|
* a later dispatcher invocation can settle what this process left behind.
|
|
8
8
|
*
|
|
9
|
+
* Lifecycle (#1223, #1246): `open` returns promptly. Provisioning or
|
|
10
|
+
* resuming runs as a background attempt the provider owns; the service
|
|
11
|
+
* journals `provisioning` when it starts and `provisioned`/`resumed` or
|
|
12
|
+
* `provision-failed` once the attempt's outcome is known — from its `settled`
|
|
13
|
+
* promise in this process, or from a provider probe in any process (`status`,
|
|
14
|
+
* `open`, the gate). Every settlement goes through `settleOnce`, a bounded
|
|
15
|
+
* loop over the repository journal's compare-and-append
|
|
16
|
+
* (`appendRepoIfCurrent`), so an outcome is recorded exactly once even when
|
|
17
|
+
* several processes observe it. Every other tool passes a journal gate: it
|
|
18
|
+
* proceeds only on a `live` environment and otherwise refuses typed
|
|
19
|
+
* `not-ready` naming the state. Nothing but `open` (and `reset`, which reuses
|
|
20
|
+
* it) ever starts provisioning.
|
|
21
|
+
*
|
|
22
|
+
* Failure evidence in the provider is destroyed only after the journal
|
|
23
|
+
* authoritatively records that failure: `open` probes first and, when the
|
|
24
|
+
* provider reports `failed`, records it (`recordFailure`) before calling the
|
|
25
|
+
* provider's `open`, which may reap the failed environment. A provisioning
|
|
26
|
+
* attempt that died before its environment existed is failed by the orphan
|
|
27
|
+
* rule once its grace period passes.
|
|
28
|
+
*
|
|
9
29
|
* Credential freeness is structural upstream (the providers build guest
|
|
10
30
|
* environments from empty records); redaction here keeps credential VALUES
|
|
11
31
|
* out of typed error messages that travel to tool callers.
|
|
@@ -13,18 +33,25 @@
|
|
|
13
33
|
import { createHash } from 'node:crypto'
|
|
14
34
|
import { humanActor } from '../events/envelope'
|
|
15
35
|
import type { Via } from '../events/envelope'
|
|
36
|
+
import type { RepositoryEventWrite } from '../events/repository'
|
|
16
37
|
import type { Forge } from '../ports/types'
|
|
17
38
|
import {
|
|
18
|
-
|
|
39
|
+
redactSandboxMessage,
|
|
19
40
|
SandboxOperationError,
|
|
20
41
|
type SandboxCommandResult,
|
|
21
42
|
type SandboxEnvironmentIdentity,
|
|
43
|
+
type SandboxLifecycleState,
|
|
44
|
+
type SandboxProviderStatus,
|
|
45
|
+
type SandboxProvisionStage,
|
|
22
46
|
type SandboxWaitResult,
|
|
23
47
|
} from '../ports/workspace/operator-sandbox'
|
|
24
48
|
import type { WorkspaceProvider } from '../ports/types'
|
|
25
|
-
import {
|
|
49
|
+
import type { SandboxEnvironmentSnapshot } from '../processes/sandbox-state'
|
|
50
|
+
import { readRepositoryFacts, type RepositoryReadFacts } from '../store/reduced-reads'
|
|
26
51
|
import { systemClock, type BuildStore, type Clock } from '../store/types'
|
|
27
52
|
|
|
53
|
+
export { redactSandboxMessage }
|
|
54
|
+
|
|
28
55
|
/** Hard bound on one sandbox command wait, in seconds — shared with the
|
|
29
56
|
* adapters (`SANDBOX_MAX_WAIT_SECONDS`). */
|
|
30
57
|
export const SANDBOX_EXEC_MAX_SECONDS = 300
|
|
@@ -36,6 +63,24 @@ export const SANDBOX_WRITE_LIMIT_BYTES = 1_048_576
|
|
|
36
63
|
/** Minimum journal interval between two `orchestrator.sandbox.activity`
|
|
37
64
|
* facts for one environment. */
|
|
38
65
|
export const SANDBOX_ACTIVITY_INTERVAL_MS = 60_000
|
|
66
|
+
/** Bounded compare-and-append attempts of one settlement. A miss means the
|
|
67
|
+
* journal advanced (often through unrelated facts); exhausting them leaves
|
|
68
|
+
* the outcome in the provider for the next poll. */
|
|
69
|
+
export const SANDBOX_SETTLE_ATTEMPTS = 5
|
|
70
|
+
/** Grace after the latest evidence of a `provisioning` journal whose
|
|
71
|
+
* provider reports no environment and no attempt runs in this process,
|
|
72
|
+
* before the orphan rule fails it. */
|
|
73
|
+
export const SANDBOX_PROVISION_ORPHAN_MS = 120_000
|
|
74
|
+
/** The orphan rule's recorded failure message. */
|
|
75
|
+
export const SANDBOX_ORPHAN_MESSAGE =
|
|
76
|
+
'the provisioning attempt ended before its environment was created; call sandbox.open to retry'
|
|
77
|
+
/** `open`'s refusal when it could not record a failed environment before
|
|
78
|
+
* replacing it. */
|
|
79
|
+
export const SANDBOX_UNRECORDED_FAILURE_MESSAGE =
|
|
80
|
+
"could not record the previous attempt's failure; nothing was discarded; call sandbox.open again"
|
|
81
|
+
/** Delays between the background settle handler's retries of a failing
|
|
82
|
+
* settlement (store errors only). */
|
|
83
|
+
const SETTLE_RETRY_DELAYS_MS = [500, 2_000, 2_000] as const
|
|
39
84
|
|
|
40
85
|
/** Truncate one output stream to the byte bound, head-kept, with the
|
|
41
86
|
* documented marker appended past the bound. Exactly-at-bound output is
|
|
@@ -93,19 +138,6 @@ export function resolveSandboxRelativePath(path: string): string {
|
|
|
93
138
|
return segments.join('/')
|
|
94
139
|
}
|
|
95
140
|
|
|
96
|
-
/** Redact values of credential-named environment variables from a provider
|
|
97
|
-
* message (the existing credential-redaction discipline): the message may
|
|
98
|
-
* travel to a tool caller, so a value of any forbidden name never rides on
|
|
99
|
-
* it. Values are read from the host environment of the serving process. */
|
|
100
|
-
export function redactSandboxMessage(message: string): string {
|
|
101
|
-
let redacted = message
|
|
102
|
-
for (const name of SANDBOX_FORBIDDEN_ENV) {
|
|
103
|
-
const value = process.env[name]
|
|
104
|
-
if (value !== undefined && value !== '') redacted = redacted.split(value).join('[REDACTED]')
|
|
105
|
-
}
|
|
106
|
-
return redacted
|
|
107
|
-
}
|
|
108
|
-
|
|
109
141
|
function describeError(error: unknown): string {
|
|
110
142
|
return redactSandboxMessage(error instanceof Error ? error.message : String(error))
|
|
111
143
|
}
|
|
@@ -141,6 +173,24 @@ export interface OperatorSandboxConfig {
|
|
|
141
173
|
environmentVariables: readonly string[]
|
|
142
174
|
}
|
|
143
175
|
|
|
176
|
+
/** What `sandbox.open` (and `reset`) report: the environment's lifecycle
|
|
177
|
+
* state right after the call, the base head once ready, and the failure
|
|
178
|
+
* when a background attempt failed. */
|
|
179
|
+
export interface SandboxOpenResult {
|
|
180
|
+
state: SandboxLifecycleState
|
|
181
|
+
environmentId: string
|
|
182
|
+
provider: string
|
|
183
|
+
baseSha?: string
|
|
184
|
+
failure?: { stage: SandboxProvisionStage; message: string }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** What `sandbox.status` reports: the open result plus the checkout head and
|
|
188
|
+
* the latest journaled evidence time. */
|
|
189
|
+
export interface SandboxStatusResult extends SandboxOpenResult {
|
|
190
|
+
headSha?: string
|
|
191
|
+
lastEvidenceAt?: string
|
|
192
|
+
}
|
|
193
|
+
|
|
144
194
|
export interface OperatorSandboxService {
|
|
145
195
|
/** Dispose local composition resources only; separate from release(identity, repo).
|
|
146
196
|
* Caller-owned static services retain their lifetime across requests. */
|
|
@@ -148,6 +198,37 @@ export interface OperatorSandboxService {
|
|
|
148
198
|
/** True when both a forge and the provider's sandbox publication
|
|
149
199
|
* capability are present, i.e. `publish` is callable. */
|
|
150
200
|
readonly canPublish: boolean
|
|
201
|
+
/** Open the caller's environment and return at once. A fresh provision or
|
|
202
|
+
* a resume continues in the background of this process; poll `status`. */
|
|
203
|
+
open(identity: string, input: { repo: string }): Promise<SandboxOpenResult>
|
|
204
|
+
/** Bounded poll: one reduced repository read plus one provider probe. Its
|
|
205
|
+
* only write records a finished background attempt's outcome, exactly once
|
|
206
|
+
* across processes, through the repository compare-and-append; it never
|
|
207
|
+
* resumes or extends an idle environment. */
|
|
208
|
+
status(identity: string, input: { repo: string }): Promise<SandboxStatusResult>
|
|
209
|
+
/** Stop the environment and release its capacity, keeping the checkout; a
|
|
210
|
+
* later `open` resumes it. Cancels an in-flight attempt. A failed
|
|
211
|
+
* environment the provider kept is stopped too, its journaled failure
|
|
212
|
+
* unchanged. `noop` when there is nothing to close. */
|
|
213
|
+
close(
|
|
214
|
+
identity: string,
|
|
215
|
+
input: { repo: string },
|
|
216
|
+
): Promise<{ outcome: 'closed' | 'noop'; environmentId: string }>
|
|
217
|
+
/** Fast-forward a clean checkout to the current base-branch head. Refuses a
|
|
218
|
+
* dirty (`refresh-dirty`) or diverged (`refresh-diverged`) checkout and
|
|
219
|
+
* changes nothing then. */
|
|
220
|
+
refresh(
|
|
221
|
+
identity: string,
|
|
222
|
+
input: { repo: string },
|
|
223
|
+
): Promise<{
|
|
224
|
+
outcome: 'refreshed' | 'current'
|
|
225
|
+
headSha: string
|
|
226
|
+
baseSha: string
|
|
227
|
+
previousHeadSha: string
|
|
228
|
+
}>
|
|
229
|
+
/** Resolves once no background attempt for the operator is in flight in
|
|
230
|
+
* this process. For tests and orderly shutdown. */
|
|
231
|
+
settled(identity: string): Promise<void>
|
|
151
232
|
exec(
|
|
152
233
|
identity: string,
|
|
153
234
|
input: { repo: string; command: string; cwd?: string; timeoutSeconds?: number },
|
|
@@ -166,8 +247,9 @@ export interface OperatorSandboxService {
|
|
|
166
247
|
input: { repo: string; path: string; content: string; encoding: 'utf8' | 'base64' },
|
|
167
248
|
): Promise<void>
|
|
168
249
|
/** Destructive: journal `reset`, full teardown + snapshot purge, then a
|
|
169
|
-
* fresh provision from the CURRENT base head
|
|
170
|
-
|
|
250
|
+
* fresh provision from the CURRENT base head, started in the background
|
|
251
|
+
* like `open`; returns the open result. */
|
|
252
|
+
reset(identity: string, input: { repo: string }): Promise<SandboxOpenResult>
|
|
171
253
|
/** PR-only publication (AUT-343): push the sandbox checkout's head (or an
|
|
172
254
|
* explicit commit) to a deterministic operator branch and open or update
|
|
173
255
|
* the PR against the base branch. The base branch itself is never pushed
|
|
@@ -202,6 +284,16 @@ export interface OperatorSandboxServiceOptions {
|
|
|
202
284
|
forge?: Forge
|
|
203
285
|
clock?: Clock
|
|
204
286
|
capability?: WorkspaceProvider['orchestratorSandbox']
|
|
287
|
+
/** Receives a background attempt's journal-append failure. The outcome is
|
|
288
|
+
* retained and flushed before the next mutating call either way. */
|
|
289
|
+
report?: (error: unknown) => void
|
|
290
|
+
/** Hosted background seam (`waitUntil`, Next's `after()`): receives each
|
|
291
|
+
* attached attempt's settle promise so the serving function stays alive
|
|
292
|
+
* for the provider's host phase and its settlement. A throwing seam is
|
|
293
|
+
* reported and never fails `open`. */
|
|
294
|
+
background?: (work: Promise<unknown>) => void
|
|
295
|
+
/** Test seam for the settle handler's retry delays. */
|
|
296
|
+
sleep?: (ms: number) => Promise<void>
|
|
205
297
|
}
|
|
206
298
|
|
|
207
299
|
export async function createOperatorSandboxService(
|
|
@@ -217,6 +309,8 @@ export async function createOperatorSandboxService(
|
|
|
217
309
|
)
|
|
218
310
|
}
|
|
219
311
|
const clock = options.clock ?? systemClock
|
|
312
|
+
const sleep =
|
|
313
|
+
options.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)))
|
|
220
314
|
const canPublish = options.forge !== undefined && provider.sandboxPublication !== undefined
|
|
221
315
|
await store.ensureRepo(repo)
|
|
222
316
|
|
|
@@ -243,7 +337,11 @@ export async function createOperatorSandboxService(
|
|
|
243
337
|
| 'orchestrator.sandbox.released'
|
|
244
338
|
| 'orchestrator.sandbox.reset'
|
|
245
339
|
| 'orchestrator.sandbox.published'
|
|
246
|
-
| 'orchestrator.sandbox.publish-failed'
|
|
340
|
+
| 'orchestrator.sandbox.publish-failed'
|
|
341
|
+
| 'orchestrator.sandbox.provisioning'
|
|
342
|
+
| 'orchestrator.sandbox.provision-failed'
|
|
343
|
+
| 'orchestrator.sandbox.closed'
|
|
344
|
+
| 'orchestrator.sandbox.refreshed',
|
|
247
345
|
payload: Record<string, unknown>,
|
|
248
346
|
): Promise<void> => {
|
|
249
347
|
await store.appendRepo(repo, {
|
|
@@ -256,41 +354,487 @@ export async function createOperatorSandboxService(
|
|
|
256
354
|
/** The operator's journal state, or undefined when no environment exists. */
|
|
257
355
|
const operatorState = (
|
|
258
356
|
identity: string,
|
|
259
|
-
events:
|
|
260
|
-
) =>
|
|
357
|
+
events: RepositoryReadFacts,
|
|
358
|
+
): SandboxEnvironmentSnapshot | undefined =>
|
|
261
359
|
events.sandboxes
|
|
262
360
|
.filter((state) => state.operator === identity && state.state !== 'released')
|
|
263
361
|
.at(-1)
|
|
264
362
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
363
|
+
const keyOf = (identity: string): string => `${repo}\0${identity}`
|
|
364
|
+
const readFacts = async (): Promise<RepositoryReadFacts> =>
|
|
365
|
+
(await readRepositoryFacts(store, repo)).value
|
|
366
|
+
|
|
367
|
+
/** One background attempt this process attached a settle handler to.
|
|
368
|
+
* `source` is the provider's `settled` promise, so concurrent opens of one
|
|
369
|
+
* attempt attach once; `cancelled` is set by close/reset/release before the
|
|
370
|
+
* provider call so the queued handler drops the outcome. */
|
|
371
|
+
interface BackgroundEntry {
|
|
372
|
+
token: number
|
|
373
|
+
cancelled: boolean
|
|
374
|
+
source: Promise<SandboxProviderStatus>
|
|
375
|
+
promise: Promise<void>
|
|
376
|
+
}
|
|
377
|
+
const background = new Map<string, BackgroundEntry>()
|
|
378
|
+
let nextToken = 0
|
|
379
|
+
/** Outcomes whose append failed, flushed before the next mutating call and
|
|
380
|
+
* deleted only once appended. */
|
|
381
|
+
const unjournaled = new Map<string, { environmentId: string; outcome: SandboxProviderStatus }>()
|
|
382
|
+
|
|
383
|
+
const requireLifecycle = (): void => {
|
|
384
|
+
if (!capability.lifecycleSupported) {
|
|
385
|
+
throw new SandboxOperationError(
|
|
386
|
+
'unsupported',
|
|
387
|
+
`workspace provider "${provider.name}" does not implement the operator sandbox lifecycle (open/status/close/refresh)`,
|
|
388
|
+
)
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const notReady = (state: SandboxLifecycleState): SandboxOperationError =>
|
|
393
|
+
new SandboxOperationError(
|
|
394
|
+
'not-ready',
|
|
395
|
+
state === 'provisioning' || state === 'resuming'
|
|
396
|
+
? `operator sandbox is ${state}; poll sandbox.status until it reports ready`
|
|
397
|
+
: `operator sandbox is ${state}; call sandbox.open and poll sandbox.status until it reports ready`,
|
|
398
|
+
{ state },
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
const probe = async (identity: string): Promise<SandboxProviderStatus> =>
|
|
402
|
+
capability.status({ repo, operator: identity }).catch((error: unknown) => {
|
|
403
|
+
if (error instanceof SandboxOperationError) throw error
|
|
404
|
+
throw new SandboxOperationError('exec', describeError(error), { cause: error })
|
|
275
405
|
})
|
|
276
|
-
|
|
277
|
-
|
|
406
|
+
|
|
407
|
+
type SettleResult = 'appended' | 'settled-elsewhere' | 'nothing' | 'contended'
|
|
408
|
+
|
|
409
|
+
/** The fact that settles one attempt against a `provisioning` snapshot, or
|
|
410
|
+
* undefined for an in-flight or cancelled outcome (its closer journals).
|
|
411
|
+
* The fact kind comes from the journal's `pendingKind`, never from the
|
|
412
|
+
* provider; the base head from the provider's handle. */
|
|
413
|
+
const settlementFact = (
|
|
414
|
+
identity: string,
|
|
415
|
+
state: SandboxEnvironmentSnapshot,
|
|
416
|
+
outcome: SandboxProviderStatus,
|
|
417
|
+
handle: SandboxEnvironmentIdentity | undefined,
|
|
418
|
+
): RepositoryEventWrite | undefined => {
|
|
419
|
+
const actor = humanActor(identity)
|
|
420
|
+
if (outcome.state === 'ready' && handle !== undefined) {
|
|
421
|
+
if (state.pendingKind === 'provision') {
|
|
422
|
+
return {
|
|
423
|
+
actor,
|
|
424
|
+
type: 'orchestrator.sandbox.provisioned',
|
|
425
|
+
payload: {
|
|
426
|
+
operator: identity,
|
|
427
|
+
environmentId: state.environmentId,
|
|
428
|
+
provider: handle.provider,
|
|
429
|
+
...(handle.sessionId !== undefined ? { sessionId: handle.sessionId } : {}),
|
|
430
|
+
workspacePath: handle.workspacePath,
|
|
431
|
+
...(handle.baseSha !== undefined ? { baseSha: handle.baseSha } : {}),
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
actor,
|
|
437
|
+
type: 'orchestrator.sandbox.resumed',
|
|
438
|
+
payload: {
|
|
439
|
+
operator: identity,
|
|
440
|
+
environmentId: state.environmentId,
|
|
441
|
+
provider: handle.provider,
|
|
442
|
+
...(handle.sessionId !== undefined ? { sessionId: handle.sessionId } : {}),
|
|
443
|
+
},
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (outcome.state === 'absent' && outcome.cancelled === true) return undefined
|
|
447
|
+
if (outcome.state === 'provisioning' || outcome.state === 'resuming') return undefined
|
|
448
|
+
return failureFact(identity, state.environmentId, outcome)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const failureFact = (
|
|
452
|
+
identity: string,
|
|
453
|
+
environmentId: string,
|
|
454
|
+
outcome: SandboxProviderStatus,
|
|
455
|
+
): RepositoryEventWrite => {
|
|
456
|
+
const failure = outcome.failure ?? {
|
|
457
|
+
stage: 'provision' as const,
|
|
458
|
+
message: `the provider reported the environment ${outcome.state} when the attempt settled`,
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
actor: humanActor(identity),
|
|
462
|
+
type: 'orchestrator.sandbox.provision-failed',
|
|
463
|
+
payload: {
|
|
278
464
|
operator: identity,
|
|
279
|
-
environmentId
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
465
|
+
environmentId,
|
|
466
|
+
stage: failure.stage,
|
|
467
|
+
message: redactSandboxMessage(failure.message) || 'provisioning failed',
|
|
468
|
+
},
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Record one attempt's outcome exactly once across processes: append the
|
|
473
|
+
* settling fact only while the journal still says `provisioning` for
|
|
474
|
+
* `environmentId`, and only at the sequence just read. A miss re-reads and
|
|
475
|
+
* re-checks, a bounded number of times; another process's settlement ends
|
|
476
|
+
* the loop with nothing appended. Never falls back to an unconditional
|
|
477
|
+
* append. */
|
|
478
|
+
const settleOnce = async (
|
|
479
|
+
identity: string,
|
|
480
|
+
environmentId: string,
|
|
481
|
+
outcome: SandboxProviderStatus,
|
|
482
|
+
first?: { cursor: number; value: RepositoryReadFacts },
|
|
483
|
+
): Promise<SettleResult> => {
|
|
484
|
+
let handle: SandboxEnvironmentIdentity | undefined
|
|
485
|
+
let read = first
|
|
486
|
+
for (let attempt = 0; attempt < SANDBOX_SETTLE_ATTEMPTS; attempt += 1) {
|
|
487
|
+
const { cursor, value } = read ?? (await readRepositoryFacts(store, repo))
|
|
488
|
+
read = undefined
|
|
489
|
+
const state = operatorState(identity, value)
|
|
490
|
+
if (state?.state !== 'provisioning' || state.environmentId !== environmentId) {
|
|
491
|
+
return 'settled-elsewhere'
|
|
492
|
+
}
|
|
493
|
+
if (outcome.state === 'ready' && handle === undefined) {
|
|
494
|
+
handle = outcome.handle ?? (await capability.describe({ repo, operator: identity }))
|
|
495
|
+
}
|
|
496
|
+
const fact = settlementFact(identity, state, outcome, handle)
|
|
497
|
+
if (fact === undefined) return 'nothing'
|
|
498
|
+
if ((await store.appendRepoIfCurrent(repo, cursor, fact)) !== null) return 'appended'
|
|
499
|
+
}
|
|
500
|
+
return 'contended'
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** Make the journal authoritatively record a failure the provider reports,
|
|
504
|
+
* before anything may discard its evidence. `already-recorded` means an
|
|
505
|
+
* authoritative read shows this environment `failed`; a journal that never
|
|
506
|
+
* saw the attempt (absent or released) first gains its `provisioning` fact.
|
|
507
|
+
* Each append is conditional. */
|
|
508
|
+
const recordFailure = async (
|
|
509
|
+
identity: string,
|
|
510
|
+
environmentId: string,
|
|
511
|
+
provider: string,
|
|
512
|
+
outcome: SandboxProviderStatus,
|
|
513
|
+
): Promise<'appended' | 'already-recorded' | 'contended'> => {
|
|
514
|
+
for (let attempt = 0; attempt < SANDBOX_SETTLE_ATTEMPTS; attempt += 1) {
|
|
515
|
+
const { cursor, value } = await readRepositoryFacts(store, repo)
|
|
516
|
+
const state = operatorState(identity, value)
|
|
517
|
+
if (state?.environmentId === environmentId && state.state === 'failed') {
|
|
518
|
+
return 'already-recorded'
|
|
519
|
+
}
|
|
520
|
+
if (state === undefined || state.environmentId !== environmentId) {
|
|
521
|
+
await store.appendRepoIfCurrent(repo, cursor, {
|
|
522
|
+
actor: humanActor(identity),
|
|
523
|
+
type: 'orchestrator.sandbox.provisioning',
|
|
524
|
+
payload: { operator: identity, environmentId, provider, kind: 'provision' },
|
|
525
|
+
})
|
|
526
|
+
continue
|
|
527
|
+
}
|
|
528
|
+
const fact = failureFact(identity, environmentId, outcome)
|
|
529
|
+
if ((await store.appendRepoIfCurrent(repo, cursor, fact)) !== null) return 'appended'
|
|
530
|
+
}
|
|
531
|
+
return 'contended'
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** The orphan rule's precondition: a `provisioning` journal whose provider
|
|
535
|
+
* reports no environment, with no attempt in this process, past the grace
|
|
536
|
+
* after its latest evidence. */
|
|
537
|
+
const orphaned = (
|
|
538
|
+
identity: string,
|
|
539
|
+
state: SandboxEnvironmentSnapshot | undefined,
|
|
540
|
+
providerStatus: SandboxProviderStatus,
|
|
541
|
+
): boolean => {
|
|
542
|
+
if (state?.state !== 'provisioning' || providerStatus.state !== 'absent') return false
|
|
543
|
+
if (background.has(keyOf(identity))) return false
|
|
544
|
+
const last = Date.parse(state.lastEvidenceTs)
|
|
545
|
+
return Number.isFinite(last) && clock().getTime() - last > SANDBOX_PROVISION_ORPHAN_MS
|
|
546
|
+
}
|
|
547
|
+
const orphanOutcome: SandboxProviderStatus = {
|
|
548
|
+
state: 'failed',
|
|
549
|
+
failure: { stage: 'checkout', message: SANDBOX_ORPHAN_MESSAGE },
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** Append an outcome whose earlier append failed, before anything else on
|
|
553
|
+
* a mutating path. A no-op (no store read) when nothing is retained. */
|
|
554
|
+
const flushUnjournaled = async (
|
|
555
|
+
identity: string,
|
|
556
|
+
facts: RepositoryReadFacts,
|
|
557
|
+
): Promise<RepositoryReadFacts> => {
|
|
558
|
+
const key = keyOf(identity)
|
|
559
|
+
const entry = unjournaled.get(key)
|
|
560
|
+
if (entry === undefined) return facts
|
|
561
|
+
const state = operatorState(identity, facts)
|
|
562
|
+
if (
|
|
563
|
+
state === undefined ||
|
|
564
|
+
state.state !== 'provisioning' ||
|
|
565
|
+
state.environmentId !== entry.environmentId
|
|
566
|
+
) {
|
|
567
|
+
// The journal moved on (closed, released, or a newer attempt).
|
|
568
|
+
unjournaled.delete(key)
|
|
569
|
+
return facts
|
|
570
|
+
}
|
|
571
|
+
let result: SettleResult
|
|
572
|
+
try {
|
|
573
|
+
result = await settleOnce(identity, entry.environmentId, entry.outcome)
|
|
574
|
+
} catch (error) {
|
|
575
|
+
throw new SandboxOperationError(
|
|
576
|
+
'provision',
|
|
577
|
+
`could not journal the previous provisioning outcome: ${describeError(error)}`,
|
|
578
|
+
{ cause: error },
|
|
579
|
+
)
|
|
580
|
+
}
|
|
581
|
+
if (result === 'contended') {
|
|
582
|
+
throw new SandboxOperationError(
|
|
583
|
+
'provision',
|
|
584
|
+
'could not journal the previous provisioning outcome: the repository journal kept advancing; retry',
|
|
585
|
+
)
|
|
586
|
+
}
|
|
587
|
+
unjournaled.delete(key)
|
|
588
|
+
return readFacts()
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** The settle handler of one attached attempt, run on the serialize chain
|
|
592
|
+
* so it orders after in-flight calls. It settles only while its entry is
|
|
593
|
+
* current and uncancelled; a store failure is retried a bounded number of
|
|
594
|
+
* times inside the attached promise (and so inside a hosted background
|
|
595
|
+
* window) before the outcome is retained for the next mutating call. */
|
|
596
|
+
const onSettled = async (
|
|
597
|
+
identity: string,
|
|
598
|
+
entry: BackgroundEntry,
|
|
599
|
+
environmentId: string,
|
|
600
|
+
outcome: SandboxProviderStatus,
|
|
601
|
+
): Promise<void> => {
|
|
602
|
+
const key = keyOf(identity)
|
|
603
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
604
|
+
if (background.get(key) !== entry || entry.cancelled) return
|
|
605
|
+
let result: SettleResult
|
|
606
|
+
try {
|
|
607
|
+
result = await settleOnce(identity, environmentId, outcome)
|
|
608
|
+
} catch (error) {
|
|
609
|
+
const delay = SETTLE_RETRY_DELAYS_MS[attempt]
|
|
610
|
+
if (delay === undefined) {
|
|
611
|
+
unjournaled.set(key, { environmentId, outcome })
|
|
612
|
+
throw error
|
|
613
|
+
}
|
|
614
|
+
await sleep(delay)
|
|
615
|
+
continue
|
|
616
|
+
}
|
|
617
|
+
if (result === 'contended') {
|
|
618
|
+
// The evidence stays in the provider; the next poll settles it.
|
|
619
|
+
unjournaled.set(key, { environmentId, outcome })
|
|
620
|
+
throw new Error(
|
|
621
|
+
'could not journal the attempt outcome: the repository journal kept advancing',
|
|
622
|
+
)
|
|
623
|
+
}
|
|
624
|
+
return
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const attach = (
|
|
629
|
+
identity: string,
|
|
630
|
+
environmentId: string,
|
|
631
|
+
settled: Promise<SandboxProviderStatus>,
|
|
632
|
+
): void => {
|
|
633
|
+
const key = keyOf(identity)
|
|
634
|
+
const entry: BackgroundEntry = {
|
|
635
|
+
token: ++nextToken,
|
|
636
|
+
cancelled: false,
|
|
637
|
+
source: settled,
|
|
638
|
+
promise: Promise.resolve(),
|
|
639
|
+
}
|
|
640
|
+
entry.promise = settled
|
|
641
|
+
.then((outcome) => serialize(key, () => onSettled(identity, entry, environmentId, outcome)))
|
|
642
|
+
.catch((error: unknown) => {
|
|
643
|
+
options.report?.(error)
|
|
644
|
+
})
|
|
645
|
+
.finally(() => {
|
|
646
|
+
if (background.get(key)?.token === entry.token) background.delete(key)
|
|
284
647
|
})
|
|
285
|
-
|
|
286
|
-
|
|
648
|
+
background.set(key, entry)
|
|
649
|
+
try {
|
|
650
|
+
options.background?.(entry.promise)
|
|
651
|
+
} catch (error) {
|
|
652
|
+
options.report?.(error)
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** Mark any in-flight attempt cancelled before a provider teardown call. */
|
|
657
|
+
const cancelBackground = (identity: string): boolean => {
|
|
658
|
+
const entry = background.get(keyOf(identity))
|
|
659
|
+
if (entry === undefined || entry.cancelled) return false
|
|
660
|
+
entry.cancelled = true
|
|
661
|
+
return true
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const openReport = async (
|
|
665
|
+
identity: string,
|
|
666
|
+
status: SandboxProviderStatus,
|
|
667
|
+
environmentId: string,
|
|
668
|
+
providerName: string,
|
|
669
|
+
reread: boolean,
|
|
670
|
+
state: SandboxEnvironmentSnapshot | undefined,
|
|
671
|
+
): Promise<SandboxOpenResult> => {
|
|
672
|
+
const reported: SandboxLifecycleState =
|
|
673
|
+
status.state === 'absent' || status.state === 'stopped' ? 'provisioning' : status.state
|
|
674
|
+
const after = reread ? operatorState(identity, await readFacts()) : state
|
|
675
|
+
const baseSha = reported === 'ready' ? (after?.baseSha ?? status.handle?.baseSha) : undefined
|
|
676
|
+
const failure = reported === 'failed' ? (status.failure ?? after?.failure) : undefined
|
|
677
|
+
return {
|
|
678
|
+
state: reported,
|
|
679
|
+
environmentId,
|
|
680
|
+
provider: providerName,
|
|
681
|
+
...(baseSha !== undefined ? { baseSha } : {}),
|
|
682
|
+
...(failure !== undefined
|
|
683
|
+
? { failure: { stage: failure.stage, message: redactSandboxMessage(failure.message) } }
|
|
684
|
+
: {}),
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** The open path, run inside the operator's serialize chain (reset calls
|
|
689
|
+
* it while holding the chain).
|
|
690
|
+
*
|
|
691
|
+
* Ordering is what makes reaping safe: the provider is probed first, and a
|
|
692
|
+
* `failed` report is recorded in the journal (`recordFailure`) before the
|
|
693
|
+
* provider's `open` — which may reap the failed environment and reprovision
|
|
694
|
+
* — is called. When the failure cannot be recorded, `open` refuses and
|
|
695
|
+
* nothing is discarded. */
|
|
696
|
+
const openLocked = async (identity: string): Promise<SandboxOpenResult> => {
|
|
697
|
+
const facts = await flushUnjournaled(identity, await readFacts())
|
|
698
|
+
let state = operatorState(identity, facts)
|
|
699
|
+
const described = await capability.describe({ repo, operator: identity })
|
|
700
|
+
const current = await probe(identity)
|
|
701
|
+
if (state !== undefined && orphaned(identity, state, current)) {
|
|
702
|
+
await settleOnce(identity, state.environmentId, orphanOutcome)
|
|
703
|
+
state = operatorState(identity, await readFacts())
|
|
704
|
+
} else if (state?.state === 'provisioning' && current.state === 'ready') {
|
|
705
|
+
// A lagging journal: settle it from the provider before deciding.
|
|
706
|
+
const result = await settleOnce(identity, state.environmentId, current)
|
|
707
|
+
if (result === 'appended' || result === 'settled-elsewhere') {
|
|
708
|
+
return openReport(
|
|
709
|
+
identity,
|
|
710
|
+
current,
|
|
711
|
+
described.environmentId,
|
|
712
|
+
described.provider,
|
|
713
|
+
true,
|
|
714
|
+
state,
|
|
715
|
+
)
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
if (current.state === 'failed') {
|
|
719
|
+
let recorded: Awaited<ReturnType<typeof recordFailure>>
|
|
720
|
+
try {
|
|
721
|
+
recorded = await recordFailure(
|
|
722
|
+
identity,
|
|
723
|
+
described.environmentId,
|
|
724
|
+
described.provider,
|
|
725
|
+
current,
|
|
726
|
+
)
|
|
727
|
+
} catch (error) {
|
|
728
|
+
throw new SandboxOperationError(
|
|
729
|
+
'provision',
|
|
730
|
+
`${SANDBOX_UNRECORDED_FAILURE_MESSAGE}: ${describeError(error)}`,
|
|
731
|
+
{ cause: error },
|
|
732
|
+
)
|
|
733
|
+
}
|
|
734
|
+
if (recorded === 'contended') {
|
|
735
|
+
throw new SandboxOperationError('provision', SANDBOX_UNRECORDED_FAILURE_MESSAGE)
|
|
736
|
+
}
|
|
737
|
+
state = operatorState(identity, await readFacts())
|
|
738
|
+
}
|
|
739
|
+
let opened: Awaited<ReturnType<typeof capability.open>>
|
|
740
|
+
try {
|
|
741
|
+
opened = await capability.open({ repo, operator: identity, baseBranch: options.baseBranch })
|
|
742
|
+
} catch (error) {
|
|
743
|
+
if (error instanceof SandboxOperationError) throw error
|
|
744
|
+
throw new SandboxOperationError('provision', describeError(error), { cause: error })
|
|
745
|
+
}
|
|
746
|
+
const { status, settled } = opened
|
|
747
|
+
if (settled === undefined && status.state === 'ready') {
|
|
748
|
+
if (state?.state === 'live') {
|
|
749
|
+
return openReport(
|
|
750
|
+
identity,
|
|
751
|
+
status,
|
|
752
|
+
described.environmentId,
|
|
753
|
+
described.provider,
|
|
754
|
+
false,
|
|
755
|
+
state,
|
|
756
|
+
)
|
|
757
|
+
}
|
|
758
|
+
if (state?.state === 'provisioning') {
|
|
759
|
+
await settleOnce(identity, state.environmentId, status)
|
|
760
|
+
} else {
|
|
761
|
+
const handle = status.handle ?? described
|
|
762
|
+
if (state === undefined || status.kind === 'provision') {
|
|
763
|
+
await append(identity, 'orchestrator.sandbox.provisioned', {
|
|
764
|
+
operator: identity,
|
|
765
|
+
environmentId: described.environmentId,
|
|
766
|
+
provider: handle.provider,
|
|
767
|
+
...(handle.sessionId !== undefined ? { sessionId: handle.sessionId } : {}),
|
|
768
|
+
workspacePath: handle.workspacePath,
|
|
769
|
+
...(handle.baseSha !== undefined ? { baseSha: handle.baseSha } : {}),
|
|
770
|
+
})
|
|
771
|
+
} else {
|
|
772
|
+
await append(identity, 'orchestrator.sandbox.resumed', {
|
|
773
|
+
operator: identity,
|
|
774
|
+
environmentId: described.environmentId,
|
|
775
|
+
provider: handle.provider,
|
|
776
|
+
...(handle.sessionId !== undefined ? { sessionId: handle.sessionId } : {}),
|
|
777
|
+
})
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return openReport(identity, status, described.environmentId, described.provider, true, state)
|
|
781
|
+
}
|
|
782
|
+
// Work remains (or failed at once): open the attempt in the journal.
|
|
783
|
+
const kind = status.kind ?? (status.state === 'resuming' ? 'resume' : 'provision')
|
|
784
|
+
let journaled = state
|
|
785
|
+
if (
|
|
786
|
+
state?.state !== 'provisioning' ||
|
|
787
|
+
(status.kind !== undefined && status.kind !== state.pendingKind)
|
|
788
|
+
) {
|
|
789
|
+
await append(identity, 'orchestrator.sandbox.provisioning', {
|
|
287
790
|
operator: identity,
|
|
288
|
-
environmentId:
|
|
289
|
-
provider:
|
|
290
|
-
|
|
791
|
+
environmentId: described.environmentId,
|
|
792
|
+
provider: described.provider,
|
|
793
|
+
kind,
|
|
291
794
|
})
|
|
795
|
+
journaled = operatorState(identity, await readFacts())
|
|
796
|
+
}
|
|
797
|
+
if (settled !== undefined) {
|
|
798
|
+
const existing = background.get(keyOf(identity))
|
|
799
|
+
if (existing === undefined || existing.cancelled || existing.source !== settled) {
|
|
800
|
+
attach(identity, described.environmentId, settled)
|
|
801
|
+
}
|
|
802
|
+
} else if (status.state === 'failed' && journaled !== undefined) {
|
|
803
|
+
await settleOnce(identity, journaled.environmentId, status)
|
|
804
|
+
}
|
|
805
|
+
return openReport(
|
|
806
|
+
identity,
|
|
807
|
+
status,
|
|
808
|
+
described.environmentId,
|
|
809
|
+
described.provider,
|
|
810
|
+
false,
|
|
811
|
+
journaled,
|
|
812
|
+
)
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** The journal gate every non-lifecycle tool passes, inside the chain. */
|
|
816
|
+
const gate = async (
|
|
817
|
+
identity: string,
|
|
818
|
+
): Promise<{ resolved: SandboxEnvironmentIdentity; state: SandboxEnvironmentSnapshot }> => {
|
|
819
|
+
const facts = await flushUnjournaled(identity, await readFacts())
|
|
820
|
+
let state = operatorState(identity, facts)
|
|
821
|
+
if (state?.state === 'provisioning') {
|
|
822
|
+
const current = await probe(identity)
|
|
823
|
+
if (current.state === 'ready' || current.state === 'failed') {
|
|
824
|
+
await settleOnce(identity, state.environmentId, current)
|
|
825
|
+
} else if (orphaned(identity, state, current)) {
|
|
826
|
+
await settleOnce(identity, state.environmentId, orphanOutcome)
|
|
827
|
+
} else {
|
|
828
|
+
throw notReady(current.state === 'absent' ? 'provisioning' : current.state)
|
|
829
|
+
}
|
|
830
|
+
state = operatorState(identity, await readFacts())
|
|
831
|
+
}
|
|
832
|
+
if (state === undefined) throw notReady('absent')
|
|
833
|
+
if (state.state !== 'live') {
|
|
834
|
+
throw notReady(state.state === 'released' ? 'absent' : state.state)
|
|
292
835
|
}
|
|
293
|
-
|
|
836
|
+
const resolved = await capability.describe({ repo, operator: identity })
|
|
837
|
+
return { resolved, state }
|
|
294
838
|
}
|
|
295
839
|
|
|
296
840
|
/** Rate-limited activity evidence: at most one fact per interval per
|
|
@@ -323,42 +867,193 @@ export async function createOperatorSandboxService(
|
|
|
323
867
|
|
|
324
868
|
const run = async <T>(
|
|
325
869
|
identity: string,
|
|
326
|
-
stage: '
|
|
870
|
+
stage: 'exec' | 'publish' | 'refresh',
|
|
327
871
|
operation: (
|
|
328
872
|
resolved: SandboxEnvironmentIdentity,
|
|
329
|
-
state:
|
|
873
|
+
state: SandboxEnvironmentSnapshot,
|
|
330
874
|
) => Promise<T>,
|
|
331
|
-
): Promise<T> =>
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const events = (await readRepositoryFacts(store, repo)).value
|
|
335
|
-
const state = operatorState(identity, events)
|
|
336
|
-
const resolved = await ensureWithFacts(identity, state).catch((error: unknown) => {
|
|
337
|
-
if (error instanceof SandboxOperationError) throw error
|
|
338
|
-
throw new SandboxOperationError(
|
|
339
|
-
state === undefined ? 'provision' : 'resume',
|
|
340
|
-
describeError(error),
|
|
341
|
-
{ cause: error },
|
|
342
|
-
)
|
|
343
|
-
})
|
|
344
|
-
const freshEvents = (await readRepositoryFacts(store, repo)).value
|
|
345
|
-
const freshState = operatorState(identity, freshEvents)
|
|
875
|
+
): Promise<T> =>
|
|
876
|
+
serialize(keyOf(identity), async () => {
|
|
877
|
+
const { resolved, state } = await gate(identity)
|
|
346
878
|
try {
|
|
347
|
-
const result = await operation(resolved,
|
|
348
|
-
await maybeAppendActivity(identity,
|
|
879
|
+
const result = await operation(resolved, state)
|
|
880
|
+
await maybeAppendActivity(identity, state).catch(() => {})
|
|
349
881
|
return result
|
|
350
882
|
} catch (error) {
|
|
351
883
|
if (error instanceof SandboxOperationError) throw error
|
|
352
884
|
throw new SandboxOperationError(stage, describeError(error), { cause: error })
|
|
353
885
|
}
|
|
354
886
|
})
|
|
355
|
-
}
|
|
356
887
|
|
|
357
888
|
return {
|
|
358
889
|
/** True only when both a forge and a sandbox publication capability are
|
|
359
890
|
* wired; the registry filters `sandbox.publish` when false. */
|
|
360
891
|
canPublish,
|
|
361
892
|
|
|
893
|
+
async open(identity, input) {
|
|
894
|
+
requireRepo(input.repo, 'provision')
|
|
895
|
+
requireLifecycle()
|
|
896
|
+
return serialize(keyOf(identity), () => openLocked(identity))
|
|
897
|
+
},
|
|
898
|
+
|
|
899
|
+
async status(identity, input) {
|
|
900
|
+
requireRepo(input.repo)
|
|
901
|
+
requireLifecycle()
|
|
902
|
+
const read = await readRepositoryFacts(store, repo)
|
|
903
|
+
const state = operatorState(identity, read.value)
|
|
904
|
+
const described = await capability.describe({ repo, operator: identity })
|
|
905
|
+
const current = await probe(identity)
|
|
906
|
+
// The poll's only write: a finished attempt's outcome (or an orphaned
|
|
907
|
+
// attempt's failure) the journal still lacks, recorded exactly once.
|
|
908
|
+
// A store error or contention is reported, never thrown.
|
|
909
|
+
let orphanFailure: SandboxProviderStatus['failure']
|
|
910
|
+
if (state?.state === 'provisioning') {
|
|
911
|
+
const settling =
|
|
912
|
+
current.state === 'ready' || current.state === 'failed'
|
|
913
|
+
? current
|
|
914
|
+
: orphaned(identity, state, current)
|
|
915
|
+
? orphanOutcome
|
|
916
|
+
: undefined
|
|
917
|
+
if (settling !== undefined) {
|
|
918
|
+
if (settling === orphanOutcome) orphanFailure = orphanOutcome.failure
|
|
919
|
+
await serialize(keyOf(identity), () =>
|
|
920
|
+
settleOnce(identity, state.environmentId, settling, read),
|
|
921
|
+
).catch((error: unknown) => {
|
|
922
|
+
options.report?.(error)
|
|
923
|
+
})
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
let reported: SandboxLifecycleState
|
|
927
|
+
if (orphanFailure !== undefined) {
|
|
928
|
+
reported = 'failed'
|
|
929
|
+
} else if (
|
|
930
|
+
current.state === 'provisioning' ||
|
|
931
|
+
current.state === 'resuming' ||
|
|
932
|
+
current.state === 'failed'
|
|
933
|
+
) {
|
|
934
|
+
reported = current.state
|
|
935
|
+
} else if (current.state === 'ready') {
|
|
936
|
+
reported =
|
|
937
|
+
state?.state === 'live' || state?.state === 'provisioning'
|
|
938
|
+
? 'ready'
|
|
939
|
+
: state === undefined
|
|
940
|
+
? 'absent'
|
|
941
|
+
: state.state === 'released'
|
|
942
|
+
? 'absent'
|
|
943
|
+
: state.state
|
|
944
|
+
} else if (current.state === 'stopped' && state !== undefined && state.state !== 'released') {
|
|
945
|
+
// The provider's idle timer stopped it: no fact was journaled, so the
|
|
946
|
+
// journal may still say `live`. A failed journal keeps `failed`.
|
|
947
|
+
reported = state.state === 'failed' ? 'failed' : 'stopped'
|
|
948
|
+
} else if (current.state === 'absent' && state?.state === 'provisioning') {
|
|
949
|
+
// An attempt whose environment does not exist yet (inside the orphan
|
|
950
|
+
// grace, or running in this process).
|
|
951
|
+
reported = 'provisioning'
|
|
952
|
+
} else {
|
|
953
|
+
reported = state?.state === 'stopped' || state?.state === 'failed' ? state.state : 'absent'
|
|
954
|
+
}
|
|
955
|
+
const baseSha =
|
|
956
|
+
state?.state === 'provisioning'
|
|
957
|
+
? current.state === 'ready'
|
|
958
|
+
? current.handle?.baseSha
|
|
959
|
+
: undefined
|
|
960
|
+
: state?.baseSha
|
|
961
|
+
const failure =
|
|
962
|
+
reported === 'failed' ? (orphanFailure ?? current.failure ?? state?.failure) : undefined
|
|
963
|
+
const headSha = current.state === 'ready' ? current.headSha : undefined
|
|
964
|
+
return {
|
|
965
|
+
state: reported,
|
|
966
|
+
environmentId: described.environmentId,
|
|
967
|
+
provider: described.provider,
|
|
968
|
+
...(headSha !== undefined ? { headSha } : {}),
|
|
969
|
+
...(baseSha !== undefined ? { baseSha } : {}),
|
|
970
|
+
...(failure !== undefined
|
|
971
|
+
? { failure: { stage: failure.stage, message: redactSandboxMessage(failure.message) } }
|
|
972
|
+
: {}),
|
|
973
|
+
...(state !== undefined ? { lastEvidenceAt: state.lastEvidenceTs } : {}),
|
|
974
|
+
}
|
|
975
|
+
},
|
|
976
|
+
|
|
977
|
+
async close(identity, input) {
|
|
978
|
+
requireRepo(input.repo)
|
|
979
|
+
requireLifecycle()
|
|
980
|
+
return serialize(keyOf(identity), async () => {
|
|
981
|
+
const state = operatorState(identity, await flushUnjournaled(identity, await readFacts()))
|
|
982
|
+
const described = await capability.describe({ repo, operator: identity })
|
|
983
|
+
const inflight = cancelBackground(identity)
|
|
984
|
+
if (!inflight && (state === undefined || state.state === 'stopped')) {
|
|
985
|
+
return { outcome: 'noop' as const, environmentId: described.environmentId }
|
|
986
|
+
}
|
|
987
|
+
const result = await capability
|
|
988
|
+
.close({ repo, operator: identity, environmentId: described.environmentId })
|
|
989
|
+
.catch((error: unknown) => {
|
|
990
|
+
if (error instanceof SandboxOperationError) throw error
|
|
991
|
+
throw new SandboxOperationError('close', describeError(error), { cause: error })
|
|
992
|
+
})
|
|
993
|
+
if (state === undefined) {
|
|
994
|
+
return { outcome: 'noop' as const, environmentId: described.environmentId }
|
|
995
|
+
}
|
|
996
|
+
if (state.state === 'failed') {
|
|
997
|
+
// A failed environment the provider kept (its failure evidence)
|
|
998
|
+
// still holds capacity: the provider stops it, never discarding the
|
|
999
|
+
// evidence. The journal keeps the authoritative `failed` record, so
|
|
1000
|
+
// the next open replaces it at once.
|
|
1001
|
+
return {
|
|
1002
|
+
outcome: result.outcome === 'closed' ? ('closed' as const) : ('noop' as const),
|
|
1003
|
+
environmentId: described.environmentId,
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
// A provision that finished in the race keeps its base head.
|
|
1007
|
+
if (result.attempt !== undefined && state.state === 'provisioning') {
|
|
1008
|
+
await settleOnce(identity, state.environmentId, result.attempt)
|
|
1009
|
+
}
|
|
1010
|
+
await append(identity, 'orchestrator.sandbox.closed', {
|
|
1011
|
+
operator: identity,
|
|
1012
|
+
environmentId: described.environmentId,
|
|
1013
|
+
})
|
|
1014
|
+
return { outcome: 'closed' as const, environmentId: described.environmentId }
|
|
1015
|
+
})
|
|
1016
|
+
},
|
|
1017
|
+
|
|
1018
|
+
async refresh(identity, input) {
|
|
1019
|
+
requireRepo(input.repo)
|
|
1020
|
+
requireLifecycle()
|
|
1021
|
+
return run(identity, 'refresh', async (resolved) => {
|
|
1022
|
+
const result = await capability.refresh(resolved, {
|
|
1023
|
+
repo,
|
|
1024
|
+
baseBranch: options.baseBranch,
|
|
1025
|
+
})
|
|
1026
|
+
if (result.outcome === 'refused') {
|
|
1027
|
+
throw new SandboxOperationError(
|
|
1028
|
+
result.reason === 'dirty' ? 'refresh-dirty' : 'refresh-diverged',
|
|
1029
|
+
redactSandboxMessage(result.message),
|
|
1030
|
+
)
|
|
1031
|
+
}
|
|
1032
|
+
if (result.outcome === 'refreshed') {
|
|
1033
|
+
await append(identity, 'orchestrator.sandbox.refreshed', {
|
|
1034
|
+
operator: identity,
|
|
1035
|
+
environmentId: resolved.environmentId,
|
|
1036
|
+
baseSha: result.baseSha,
|
|
1037
|
+
headSha: result.headSha,
|
|
1038
|
+
previousHeadSha: result.previousHeadSha,
|
|
1039
|
+
})
|
|
1040
|
+
}
|
|
1041
|
+
return {
|
|
1042
|
+
outcome: result.outcome,
|
|
1043
|
+
headSha: result.headSha,
|
|
1044
|
+
baseSha: result.baseSha,
|
|
1045
|
+
previousHeadSha: result.previousHeadSha,
|
|
1046
|
+
}
|
|
1047
|
+
})
|
|
1048
|
+
},
|
|
1049
|
+
|
|
1050
|
+
async settled(identity) {
|
|
1051
|
+
const key = keyOf(identity)
|
|
1052
|
+
for (let entry = background.get(key); entry !== undefined; entry = background.get(key)) {
|
|
1053
|
+
await entry.promise
|
|
1054
|
+
}
|
|
1055
|
+
},
|
|
1056
|
+
|
|
362
1057
|
async exec(identity, input) {
|
|
363
1058
|
requireRepo(input.repo)
|
|
364
1059
|
const timeoutSeconds = validateBoundedInt(
|
|
@@ -534,7 +1229,7 @@ export async function createOperatorSandboxService(
|
|
|
534
1229
|
)
|
|
535
1230
|
}
|
|
536
1231
|
// 3. Refuse a commit that is not a descendant of the base head
|
|
537
|
-
// at provision or reset time.
|
|
1232
|
+
// at provision, refresh or reset time.
|
|
538
1233
|
const baseSha = freshState?.baseSha
|
|
539
1234
|
if (baseSha === undefined) {
|
|
540
1235
|
throw new SandboxOperationError(
|
|
@@ -555,7 +1250,7 @@ export async function createOperatorSandboxService(
|
|
|
555
1250
|
if (ancestorResult.exitCode === 1) {
|
|
556
1251
|
throw new SandboxOperationError(
|
|
557
1252
|
'publish',
|
|
558
|
-
`the commit is not a descendant of the base head ${baseSha} recorded at provision time; rebase onto the current base or run sandbox.reset`,
|
|
1253
|
+
`the commit is not a descendant of the base head ${baseSha} recorded at provision, refresh or reset time; rebase onto the current base, or run sandbox.refresh or sandbox.reset`,
|
|
559
1254
|
)
|
|
560
1255
|
}
|
|
561
1256
|
if (ancestorResult.exitCode !== 0) {
|
|
@@ -632,18 +1327,21 @@ export async function createOperatorSandboxService(
|
|
|
632
1327
|
|
|
633
1328
|
async reset(identity, input) {
|
|
634
1329
|
requireRepo(input.repo)
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
1330
|
+
// Refuse before tearing anything down: reset re-provisions through the
|
|
1331
|
+
// lifecycle, which a provider without it cannot do.
|
|
1332
|
+
requireLifecycle()
|
|
1333
|
+
return serialize(keyOf(identity), async () => {
|
|
1334
|
+
await flushUnjournaled(identity, await readFacts())
|
|
639
1335
|
const resolved = await capability.describe({ repo, operator: identity })
|
|
1336
|
+
cancelBackground(identity)
|
|
640
1337
|
await append(identity, 'orchestrator.sandbox.reset', {
|
|
641
1338
|
operator: identity,
|
|
642
1339
|
environmentId: resolved.environmentId,
|
|
643
1340
|
})
|
|
644
|
-
// Release first (snapshot purge
|
|
645
|
-
//
|
|
646
|
-
// carries reset → released →
|
|
1341
|
+
// Release first (snapshot purge; the provider cancels and awaits an
|
|
1342
|
+
// in-flight attempt), then reprovision from the CURRENT base head in
|
|
1343
|
+
// the background. The journal carries reset → released →
|
|
1344
|
+
// provisioning → provisioned as the evidence chain.
|
|
647
1345
|
const { snapshots } = await capability
|
|
648
1346
|
.release({ repo, operator: identity, environmentId: resolved.environmentId })
|
|
649
1347
|
.catch((error: unknown) => {
|
|
@@ -654,29 +1352,27 @@ export async function createOperatorSandboxService(
|
|
|
654
1352
|
environmentId: resolved.environmentId,
|
|
655
1353
|
snapshots,
|
|
656
1354
|
})
|
|
657
|
-
|
|
658
|
-
if (error instanceof SandboxOperationError) throw error
|
|
659
|
-
throw new SandboxOperationError('provision', describeError(error), { cause: error })
|
|
660
|
-
})
|
|
661
|
-
void state
|
|
1355
|
+
return openLocked(identity)
|
|
662
1356
|
})
|
|
663
1357
|
},
|
|
664
1358
|
|
|
665
1359
|
async release(identity, input) {
|
|
666
1360
|
requireRepo(input.repo)
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
const
|
|
670
|
-
const state = operatorState(identity, events)
|
|
1361
|
+
await serialize(keyOf(identity), async () => {
|
|
1362
|
+
const state = operatorState(identity, await flushUnjournaled(identity, await readFacts()))
|
|
1363
|
+
const inflight = cancelBackground(identity)
|
|
671
1364
|
// No environment ever provisioned: a no-op, with no fact and no
|
|
672
1365
|
// provider traffic beyond the pure describe.
|
|
673
|
-
if (state === undefined) return
|
|
1366
|
+
if (state === undefined && !inflight) return
|
|
674
1367
|
const resolved = await capability.describe({ repo, operator: identity })
|
|
1368
|
+
// The provider cancels and awaits an in-flight attempt before the
|
|
1369
|
+
// teardown, so the archive hook never waits on an unbounded setup.
|
|
675
1370
|
const { snapshots } = await capability
|
|
676
1371
|
.release({ repo, operator: identity, environmentId: resolved.environmentId })
|
|
677
1372
|
.catch((error: unknown) => {
|
|
678
1373
|
throw new SandboxOperationError('release', describeError(error), { cause: error })
|
|
679
1374
|
})
|
|
1375
|
+
if (state === undefined) return
|
|
680
1376
|
await append(identity, 'orchestrator.sandbox.released', {
|
|
681
1377
|
operator: identity,
|
|
682
1378
|
environmentId: resolved.environmentId,
|