@indigoai-us/hq-cli 5.345.214 → 5.345.215
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/CHANGELOG.md +93 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/dist/command-catalog.generated.d.ts +180 -13
- package/dist/command-catalog.generated.js +221 -13
- package/dist/commands/agents.d.ts +51 -1
- package/dist/commands/agents.js +154 -2
- package/dist/commands/bot-jobs.d.ts +2 -15
- package/dist/commands/bot-jobs.js +2 -94
- package/dist/commands/bot-runtime.d.ts +46 -5
- package/dist/commands/bot-runtime.js +228 -18
- package/dist/commands/bot.d.ts +16 -9
- package/dist/commands/bot.js +231 -55
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +121 -40
- package/dist/commands/company-restore.d.ts +23 -0
- package/dist/commands/company-restore.js +128 -0
- package/dist/commands/company.js +2 -0
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/daemon.js +5 -0
- package/dist/commands/files-browse.d.ts +11 -4
- package/dist/commands/files-browse.js +21 -4
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +25 -11
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-connect.d.ts +10 -3
- package/dist/commands/integrations-connect.js +87 -13
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +28 -11
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sources.js +6 -0
- package/dist/commands/storage-offload.d.ts +12 -0
- package/dist/commands/storage-offload.js +41 -3
- package/dist/commands/sync-held.js +5 -2
- package/dist/commands/sync-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/lazy-commands.js +3 -5
- package/dist/lib/bot/config.d.ts +1 -0
- package/dist/lib/bot/config.js +10 -0
- package/dist/lib/bot/eventbridge-cron.d.ts +22 -0
- package/dist/lib/bot/eventbridge-cron.js +104 -0
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +35 -3
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/fleet-local.d.ts +33 -1
- package/dist/lib/bot/fleet-local.js +215 -31
- package/dist/lib/bot/fleet-python.d.ts +3 -0
- package/dist/lib/bot/fleet-python.js +34 -4
- package/dist/lib/bot/fleet-runtime-sources.d.ts +15 -0
- package/dist/lib/bot/fleet-runtime-sources.js +30 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +8 -0
- package/dist/lib/bot/legacy-provider-login.js +19 -2
- package/dist/lib/bot/probe.js +7 -1
- package/dist/lib/bot/prompt.js +1 -1
- package/dist/lib/bot/readiness.d.ts +8 -0
- package/dist/lib/bot/readiness.js +17 -3
- package/dist/lib/bot/run.js +20 -1
- package/dist/lib/bot/runtime/codex.js +1 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +5 -1
- package/dist/lib/bot/runtime/grok-acp.js +6 -3
- package/dist/lib/bot/runtime/index.js +19 -9
- package/dist/lib/bot/runtime/messages-stream.js +2 -2
- package/dist/lib/bot/runtime/types.d.ts +15 -0
- package/dist/lib/bot/runtime/types.js +9 -0
- package/dist/lib/bot/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/runtime-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- package/dist/lib/bot/status.d.ts +2 -0
- package/dist/lib/capacity/accounts.d.ts +8 -0
- package/dist/lib/capacity/accounts.js +19 -0
- package/dist/lib/capacity/available.d.ts +154 -0
- package/dist/lib/capacity/available.js +905 -0
- package/dist/lib/capacity/codex-availability.d.ts +14 -1
- package/dist/lib/capacity/codex-availability.js +26 -1
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +29 -2
- package/dist/lib/capacity/collectors/claude-oauth.js +104 -7
- package/dist/lib/capacity/collectors/codex.js +10 -7
- package/dist/lib/capacity/collectors/grok-refresh.d.ts +80 -0
- package/dist/lib/capacity/collectors/grok-refresh.js +189 -0
- package/dist/lib/capacity/collectors/grok.d.ts +5 -0
- package/dist/lib/capacity/collectors/grok.js +56 -5
- package/dist/lib/capacity/ledger.d.ts +37 -1
- package/dist/lib/capacity/ledger.js +207 -30
- package/dist/lib/capacity/project.d.ts +26 -0
- package/dist/lib/capacity/project.js +137 -8
- package/dist/lib/capacity/refusal-rows.d.ts +31 -0
- package/dist/lib/capacity/refusal-rows.js +61 -0
- package/dist/lib/capacity/refusal.d.ts +53 -2
- package/dist/lib/capacity/refusal.js +284 -65
- package/dist/lib/capacity/route.js +46 -11
- package/dist/lib/capacity/widget.d.ts +12 -1
- package/dist/lib/capacity/widget.js +91 -3
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/daemon/resolver.d.ts +2 -0
- package/dist/lib/daemon/resolver.js +3 -0
- package/dist/lib/daemon/run.d.ts +9 -0
- package/dist/lib/daemon/run.js +54 -1
- package/dist/lib/daemon/server.d.ts +2 -0
- package/dist/lib/daemon/server.js +10 -1
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +105 -0
- package/dist/lib/delivery/artifacts.js +567 -0
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -0
- package/dist/lib/delivery/backup.d.ts +78 -0
- package/dist/lib/delivery/backup.js +343 -0
- package/dist/lib/delivery/cache.d.ts +29 -0
- package/dist/lib/delivery/cache.js +33 -0
- package/dist/lib/delivery/contracts.d.ts +142 -0
- package/dist/lib/delivery/contracts.js +289 -0
- package/dist/lib/delivery/credentials.d.ts +54 -0
- package/dist/lib/delivery/credentials.js +119 -0
- package/dist/lib/delivery/disposable-target.d.ts +42 -0
- package/dist/lib/delivery/disposable-target.js +111 -0
- package/dist/lib/delivery/effects.d.ts +136 -0
- package/dist/lib/delivery/effects.js +541 -0
- package/dist/lib/delivery/enrollment.d.ts +146 -0
- package/dist/lib/delivery/enrollment.js +460 -0
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -0
- package/dist/lib/delivery/identity.d.ts +25 -0
- package/dist/lib/delivery/identity.js +82 -0
- package/dist/lib/delivery/inbox.d.ts +116 -0
- package/dist/lib/delivery/inbox.js +260 -0
- package/dist/lib/delivery/lease.d.ts +80 -0
- package/dist/lib/delivery/lease.js +134 -0
- package/dist/lib/delivery/migrations.d.ts +255 -0
- package/dist/lib/delivery/migrations.js +823 -0
- package/dist/lib/delivery/mqtt-adapter.d.ts +50 -0
- package/dist/lib/delivery/mqtt-adapter.js +242 -0
- package/dist/lib/delivery/mqtt.d.ts +63 -0
- package/dist/lib/delivery/mqtt.js +119 -0
- package/dist/lib/delivery/outbox.d.ts +95 -0
- package/dist/lib/delivery/outbox.js +187 -0
- package/dist/lib/delivery/provenance.d.ts +129 -0
- package/dist/lib/delivery/provenance.js +93 -0
- package/dist/lib/delivery/reconciler.d.ts +215 -0
- package/dist/lib/delivery/reconciler.js +703 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/rpc/client.d.ts +22 -0
- package/dist/lib/delivery/rpc/client.js +159 -0
- package/dist/lib/delivery/rpc/port.d.ts +40 -0
- package/dist/lib/delivery/rpc/port.js +16 -0
- package/dist/lib/delivery/rpc/server.d.ts +30 -0
- package/dist/lib/delivery/rpc/server.js +205 -0
- package/dist/lib/delivery/rpc/wire.d.ts +85 -0
- package/dist/lib/delivery/rpc/wire.js +143 -0
- package/dist/lib/delivery/store.d.ts +163 -0
- package/dist/lib/delivery/store.js +944 -0
- package/dist/lib/delivery/windows-service-install.d.ts +262 -0
- package/dist/lib/delivery/windows-service-install.js +892 -0
- package/dist/lib/delivery/workers/macos-job-helper.d.ts +103 -0
- package/dist/lib/delivery/workers/macos-job-helper.js +927 -0
- package/dist/lib/delivery/workers/macos.d.ts +191 -0
- package/dist/lib/delivery/workers/macos.js +943 -0
- package/dist/lib/delivery/workers/windows-job-helper.d.ts +35 -0
- package/dist/lib/delivery/workers/windows-job-helper.js +264 -0
- package/dist/lib/delivery/workers/windows-service-live.d.ts +121 -0
- package/dist/lib/delivery/workers/windows-service-live.js +711 -0
- package/dist/lib/delivery/workers/windows-service-runner.d.ts +33 -0
- package/dist/lib/delivery/workers/windows-service-runner.js +82 -0
- package/dist/lib/delivery/workers/windows-service.d.ts +140 -0
- package/dist/lib/delivery/workers/windows-service.js +584 -0
- package/dist/lib/delivery/workers/windows.d.ts +251 -0
- package/dist/lib/delivery/workers/windows.js +594 -0
- package/dist/lib/hot-channels/adapters/meetings.d.ts +15 -0
- package/dist/lib/hot-channels/adapters/meetings.js +36 -3
- package/dist/lib/hot-channels/signals-pass.d.ts +10 -0
- package/dist/lib/hot-channels/signals-pass.js +71 -2
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/signals/steps/extract.js +40 -4
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/cognito-callback-port.d.ts +14 -0
- package/dist/utils/cognito-callback-port.js +24 -0
- package/dist/utils/cognito-session.js +3 -3
- package/dist/utils/deleted-company-notice.d.ts +59 -0
- package/dist/utils/deleted-company-notice.js +156 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/dist/utils/login-browser.d.ts +1 -1
- package/dist/utils/login-browser.js +7 -5
- package/dist/utils/resident-package-preload.js +30 -15
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delivery controller: work admission, claims and restart recovery (US-003).
|
|
3
|
+
*
|
|
4
|
+
* The controller wraps one DeliveryStore handle. Every accept, claim and
|
|
5
|
+
* outcome is a single atomic store commit, so a crash leaves either all of
|
|
6
|
+
* it or none of it on disk.
|
|
7
|
+
*
|
|
8
|
+
* Recovery rule: when a controller opens the store, every attempt still
|
|
9
|
+
* "open" from an earlier epoch is marked abandoned (outcome "failed") in one
|
|
10
|
+
* commit, before any new work is admitted. Its job then allows exactly one
|
|
11
|
+
* new attempt, numbered after the abandoned one. A late report for an
|
|
12
|
+
* abandoned attempt is refused with ATTEMPT_ABANDONED and does not change it.
|
|
13
|
+
*
|
|
14
|
+
* Claimability: a job is claimable ("ready") when it has no passed attempt,
|
|
15
|
+
* no open attempt, its last attempt did not fail, and every dependency has a
|
|
16
|
+
* passed attempt. A failed job stays failed and blocks its dependents.
|
|
17
|
+
* Dependencies must name jobs in the same run; cycles and unknown
|
|
18
|
+
* dependencies are rejected at accept time.
|
|
19
|
+
*
|
|
20
|
+
* Attempts are idempotent by attemptId: claiming with a known attemptId for
|
|
21
|
+
* the same job returns the stored attempt, including after a restart, where
|
|
22
|
+
* it is reported as abandoned.
|
|
23
|
+
*
|
|
24
|
+
* US-004: claim and recordOutcome take an optional guard that runs inside the
|
|
25
|
+
* commit against the draft state, so an access check and the write it allows
|
|
26
|
+
* are one atomic step on the fenced store.
|
|
27
|
+
*
|
|
28
|
+
* Claim leases (US-011 slice 4): a claim made by a worker carries a lease of
|
|
29
|
+
* claimLeaseMs, which the worker extends with renewLease. Expiry is decided by
|
|
30
|
+
* the store's injected clock, never by Date.now, in claim(), recordOutcome(),
|
|
31
|
+
* returnAttempt(), renewLease() and the expireLeases() sweep: an open attempt
|
|
32
|
+
* whose lease time has been reached (now >= leaseExpiresAt) is abandoned in one
|
|
33
|
+
* commit, the same state the recovery rule leaves. Its job allows one new
|
|
34
|
+
* attempt with a new attemptId, and a late report is refused with
|
|
35
|
+
* ATTEMPT_ABANDONED. A claim made without a worker has no lease and never
|
|
36
|
+
* expires; neither do attempts migrated from store v6 or older.
|
|
37
|
+
*/
|
|
38
|
+
import { randomUUID } from "node:crypto";
|
|
39
|
+
import { DELIVERY_SCHEMA_VERSIONS, validateRecord, } from "./contracts.js";
|
|
40
|
+
import { DeliveryStoreError, findCycle, } from "./migrations.js";
|
|
41
|
+
import { ledgerEvents } from "./outbox.js";
|
|
42
|
+
import { DeliveryStore } from "./store.js";
|
|
43
|
+
export { DeliveryStoreError } from "./migrations.js";
|
|
44
|
+
/** Default claim lease: 120 s. A worker renews it every third of that (startLeaseHeartbeat). */
|
|
45
|
+
export const DEFAULT_CLAIM_LEASE_MS = 120_000;
|
|
46
|
+
/** True when `a` is open, has a lease, and the lease time has been reached at `nowMs`. */
|
|
47
|
+
export function leaseExpired(a, nowMs) {
|
|
48
|
+
return a.resolution === "open" && a.leaseExpiresAt !== null && nowMs >= Date.parse(a.leaseExpiresAt);
|
|
49
|
+
}
|
|
50
|
+
function iso(ms) {
|
|
51
|
+
return new Date(ms).toISOString();
|
|
52
|
+
}
|
|
53
|
+
/** JSON text with object keys sorted, so records that differ only in key order compare equal. */
|
|
54
|
+
function canonical(v) {
|
|
55
|
+
return JSON.stringify(v, (_key, value) => value !== null && typeof value === "object" && !Array.isArray(value)
|
|
56
|
+
? Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
|
|
57
|
+
: value);
|
|
58
|
+
}
|
|
59
|
+
function same(a, b) {
|
|
60
|
+
return canonical(a) === canonical(b);
|
|
61
|
+
}
|
|
62
|
+
function invalid(found, expected) {
|
|
63
|
+
return new DeliveryStoreError("INVALID_RECORD", { found, expected, action: "Fix the input and retry. Nothing was written." });
|
|
64
|
+
}
|
|
65
|
+
function deriveJobState(state, entry) {
|
|
66
|
+
const attempts = state.attempts.filter((a) => a.record.jobId === entry.record.jobId);
|
|
67
|
+
if (attempts.some((a) => a.record.outcome === "passed"))
|
|
68
|
+
return "passed";
|
|
69
|
+
const last = attempts.reduce((m, a) => (!m || a.record.number > m.record.number ? a : m), undefined);
|
|
70
|
+
if (last?.resolution === "open")
|
|
71
|
+
return "running";
|
|
72
|
+
if (last?.resolution === "reported" && last.record.outcome === "failed")
|
|
73
|
+
return "failed";
|
|
74
|
+
const passed = (jobId) => state.attempts.some((a) => a.record.jobId === jobId && a.record.outcome === "passed");
|
|
75
|
+
return entry.dependsOn.every(passed) ? "ready" : "waiting";
|
|
76
|
+
}
|
|
77
|
+
function jobView(state, j) {
|
|
78
|
+
return {
|
|
79
|
+
job: { ...j.record },
|
|
80
|
+
dependsOn: [...j.dependsOn],
|
|
81
|
+
state: deriveJobState(state, j),
|
|
82
|
+
attempts: state.attempts.filter((a) => a.record.jobId === j.record.jobId).length,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function attemptView(a) {
|
|
86
|
+
return { attempt: { ...a.record }, resolution: a.resolution, claimedEpoch: a.claimedEpoch, claimedBy: a.claimedBy };
|
|
87
|
+
}
|
|
88
|
+
/** Open a store directory as its single controller and apply the recovery rule. */
|
|
89
|
+
export function openDeliveryController(opts) {
|
|
90
|
+
return DeliveryController.open(opts);
|
|
91
|
+
}
|
|
92
|
+
export class DeliveryController {
|
|
93
|
+
recovery;
|
|
94
|
+
store;
|
|
95
|
+
ids;
|
|
96
|
+
leaseMs;
|
|
97
|
+
constructor(store, ids, recovery, leaseMs) {
|
|
98
|
+
this.store = store;
|
|
99
|
+
this.ids = ids;
|
|
100
|
+
this.recovery = recovery;
|
|
101
|
+
this.leaseMs = leaseMs;
|
|
102
|
+
}
|
|
103
|
+
static open(opts) {
|
|
104
|
+
const leaseMs = opts.claimLeaseMs ?? DEFAULT_CLAIM_LEASE_MS;
|
|
105
|
+
if (!Number.isSafeInteger(leaseMs) || leaseMs < 1)
|
|
106
|
+
throw invalid(`claimLeaseMs ${String(opts.claimLeaseMs)}`, "a positive integer number of milliseconds");
|
|
107
|
+
const store = DeliveryStore.open(opts);
|
|
108
|
+
try {
|
|
109
|
+
const epoch = store.epoch;
|
|
110
|
+
const stale = store.state.attempts.filter((a) => a.resolution === "open" && a.claimedEpoch < epoch).map((a) => a.record.attemptId);
|
|
111
|
+
if (stale.length > 0) {
|
|
112
|
+
const now = store.now();
|
|
113
|
+
store.commit(epoch, (draft) => {
|
|
114
|
+
for (const a of draft.attempts) {
|
|
115
|
+
if (!stale.includes(a.record.attemptId))
|
|
116
|
+
continue;
|
|
117
|
+
a.record.outcome = "failed";
|
|
118
|
+
a.resolution = "abandoned";
|
|
119
|
+
a.resolvedAt = now;
|
|
120
|
+
a.resolvedEpoch = epoch;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return new DeliveryController(store, opts.ids ?? (() => `attempt-${randomUUID()}`), { ...store.report, abandonedAttemptIds: stale }, leaseMs);
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
store.close();
|
|
128
|
+
throw err;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** The fencing epoch this controller writes with. */
|
|
132
|
+
get epoch() {
|
|
133
|
+
return this.store.epoch;
|
|
134
|
+
}
|
|
135
|
+
/** ISO timestamp from the store clock, the one commits are stamped with. */
|
|
136
|
+
now() {
|
|
137
|
+
return this.store.now();
|
|
138
|
+
}
|
|
139
|
+
runs() {
|
|
140
|
+
return this.store.state.runs.map((r) => ({ ...r.record }));
|
|
141
|
+
}
|
|
142
|
+
jobs(runId) {
|
|
143
|
+
const state = this.store.state;
|
|
144
|
+
return state.jobs.filter((j) => runId === undefined || j.record.runId === runId).map((j) => jobView(state, j));
|
|
145
|
+
}
|
|
146
|
+
/** One job's view, or undefined when no such job was accepted. Scans the attempts once, not once per job. */
|
|
147
|
+
job(jobId) {
|
|
148
|
+
const state = this.store.state;
|
|
149
|
+
const entry = state.jobs.find((j) => j.record.jobId === jobId);
|
|
150
|
+
return entry ? jobView(state, entry) : undefined;
|
|
151
|
+
}
|
|
152
|
+
/** Jobs whose state is "ready", in accept order. */
|
|
153
|
+
claimable(runId) {
|
|
154
|
+
return this.jobs(runId).filter((j) => j.state === "ready");
|
|
155
|
+
}
|
|
156
|
+
attempts(jobId) {
|
|
157
|
+
return this.store.state.attempts.filter((a) => jobId === undefined || a.record.jobId === jobId).map(attemptView);
|
|
158
|
+
}
|
|
159
|
+
jobState(jobId) {
|
|
160
|
+
const entry = this.store.state.jobs.find((j) => j.record.jobId === jobId);
|
|
161
|
+
if (!entry)
|
|
162
|
+
throw this.unknownJob(jobId);
|
|
163
|
+
return deriveJobState(this.store.state, entry);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Accept a run and its jobs in one commit. Replaying an identical accept is
|
|
167
|
+
* a no-op; a different run or job under an existing id is DUPLICATE_CONFLICT.
|
|
168
|
+
*/
|
|
169
|
+
acceptRun(input) {
|
|
170
|
+
const res = validateRecord("run", input.run);
|
|
171
|
+
if (!res.ok)
|
|
172
|
+
throw invalid(`run: ${res.errors.join("; ")}`, "a run valid under the US-001 contract");
|
|
173
|
+
const run = res.value;
|
|
174
|
+
const existing = this.store.state.runs.find((r) => r.record.runId === run.runId);
|
|
175
|
+
// Another tenant's runId is refused without saying that it exists or whose it is.
|
|
176
|
+
if (existing && existing.record.tenantId !== run.tenantId)
|
|
177
|
+
throw unavailableId("run", run.runId);
|
|
178
|
+
if (existing && !same(existing.record, run)) {
|
|
179
|
+
throw new DeliveryStoreError("DUPLICATE_CONFLICT", {
|
|
180
|
+
found: `run ${run.runId} already accepted with different content`,
|
|
181
|
+
expected: "the same run content, or a new runId",
|
|
182
|
+
action: "Use a new runId for a different run. Nothing was written.",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return this.admit(run, input.jobs, existing === undefined);
|
|
186
|
+
}
|
|
187
|
+
/** Add jobs to an accepted run in one commit. Dependencies may name existing jobs in the run. */
|
|
188
|
+
acceptJobs(input) {
|
|
189
|
+
const existing = this.store.state.runs.find((r) => r.record.runId === input.runId);
|
|
190
|
+
// Another tenant's run is answered exactly like a missing one.
|
|
191
|
+
if (!existing || existing.record.tenantId !== input.tenantId) {
|
|
192
|
+
throw new DeliveryStoreError("UNKNOWN_RUN", {
|
|
193
|
+
found: `no accepted run ${input.runId}`,
|
|
194
|
+
expected: `an accepted run ${input.runId} for tenant ${input.tenantId}`,
|
|
195
|
+
action: "Accept the run with acceptRun first. Nothing was written.",
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
return this.admit(existing.record, input.jobs, false);
|
|
199
|
+
}
|
|
200
|
+
admit(run, inputs, newRun) {
|
|
201
|
+
const state = this.store.state;
|
|
202
|
+
const stored = new Map(state.jobs.filter((j) => j.record.runId === run.runId).map((j) => [j.record.jobId, j]));
|
|
203
|
+
const batch = new Map();
|
|
204
|
+
for (const [i, input] of inputs.entries()) {
|
|
205
|
+
const res = validateRecord("job", input?.job);
|
|
206
|
+
if (!res.ok)
|
|
207
|
+
throw invalid(`jobs[${i}]: ${res.errors.join("; ")}`, "a job valid under the US-001 contract");
|
|
208
|
+
const job = res.value;
|
|
209
|
+
if (job.runId !== run.runId || job.tenantId !== run.tenantId) {
|
|
210
|
+
throw invalid(`job ${job.jobId} names run ${job.runId} for tenant ${job.tenantId}`, `run ${run.runId} for tenant ${run.tenantId}`);
|
|
211
|
+
}
|
|
212
|
+
const deps = input.dependsOn ?? [];
|
|
213
|
+
if (!Array.isArray(deps) || !deps.every((d) => typeof d === "string" && d !== "") || new Set(deps).size !== deps.length) {
|
|
214
|
+
throw invalid(`job ${job.jobId} dependsOn ${JSON.stringify(deps)}`, "a list of distinct jobIds");
|
|
215
|
+
}
|
|
216
|
+
if (batch.has(job.jobId)) {
|
|
217
|
+
throw new DeliveryStoreError("DUPLICATE_CONFLICT", {
|
|
218
|
+
found: `job ${job.jobId} twice in one accept`,
|
|
219
|
+
expected: "each jobId once",
|
|
220
|
+
action: "Remove the duplicate job. Nothing was written.",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const other = state.jobs.find((j) => j.record.jobId === job.jobId);
|
|
224
|
+
if (other && other.record.tenantId !== run.tenantId)
|
|
225
|
+
throw unavailableId("job", job.jobId);
|
|
226
|
+
if (other && (!same(other.record, job) || !same(other.dependsOn, deps))) {
|
|
227
|
+
throw new DeliveryStoreError("DUPLICATE_CONFLICT", {
|
|
228
|
+
found: `job ${job.jobId} already accepted with different content or dependencies`,
|
|
229
|
+
expected: "the same job and dependencies, or a new jobId",
|
|
230
|
+
action: "Use a new jobId for a different job. Nothing was written.",
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
batch.set(job.jobId, { job, dependsOn: [...deps] });
|
|
234
|
+
}
|
|
235
|
+
for (const [jobId, { dependsOn }] of batch) {
|
|
236
|
+
for (const dep of dependsOn) {
|
|
237
|
+
if (dep === jobId) {
|
|
238
|
+
throw new DeliveryStoreError("DEPENDENCY_CYCLE", {
|
|
239
|
+
found: `job ${jobId} depends on itself`,
|
|
240
|
+
expected: "an acyclic dependency graph",
|
|
241
|
+
action: "Remove the self-dependency. Nothing was written.",
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (batch.has(dep) || stored.has(dep))
|
|
245
|
+
continue;
|
|
246
|
+
// Only the caller's own jobs are visible: another tenant's job reads as unknown.
|
|
247
|
+
const elsewhere = state.jobs.find((j) => j.record.jobId === dep && j.record.tenantId === run.tenantId);
|
|
248
|
+
throw new DeliveryStoreError("UNKNOWN_DEPENDENCY", {
|
|
249
|
+
found: elsewhere
|
|
250
|
+
? `job ${jobId} depends on ${dep}, which belongs to run ${elsewhere.record.runId}`
|
|
251
|
+
: `job ${jobId} depends on unknown job ${dep}`,
|
|
252
|
+
expected: `dependencies on jobs in run ${run.runId}`,
|
|
253
|
+
action: elsewhere
|
|
254
|
+
? "Cross-run dependencies are not supported; put both jobs in one run. Nothing was written."
|
|
255
|
+
: `Accept ${dep} in the same call or earlier. Nothing was written.`,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const graph = new Map([...stored].map(([id, j]) => [id, j.dependsOn]));
|
|
260
|
+
for (const [id, j] of batch)
|
|
261
|
+
graph.set(id, j.dependsOn);
|
|
262
|
+
const cycle = findCycle(graph);
|
|
263
|
+
if (cycle) {
|
|
264
|
+
throw new DeliveryStoreError("DEPENDENCY_CYCLE", {
|
|
265
|
+
found: `dependency cycle ${cycle.join(" -> ")}`,
|
|
266
|
+
expected: "an acyclic dependency graph",
|
|
267
|
+
action: "Break the cycle. Nothing was written.",
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
const fresh = [...batch.values()].filter((j) => !stored.has(j.job.jobId));
|
|
271
|
+
if (!newRun && fresh.length === 0)
|
|
272
|
+
return { runId: run.runId, acceptedJobIds: [], replayed: true };
|
|
273
|
+
const now = this.store.now();
|
|
274
|
+
const epoch = this.epoch;
|
|
275
|
+
// Store copies: the caller keeps its objects, and later changes to them must not reach the store.
|
|
276
|
+
this.store.commit(epoch, (draft) => {
|
|
277
|
+
if (newRun)
|
|
278
|
+
draft.runs.push({ record: structuredClone(run), acceptedAt: now, acceptedEpoch: epoch });
|
|
279
|
+
for (const j of fresh) {
|
|
280
|
+
draft.jobs.push({ record: structuredClone(j.job), dependsOn: [...j.dependsOn], acceptedAt: now, acceptedEpoch: epoch });
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
return { runId: run.runId, acceptedJobIds: fresh.map((j) => j.job.jobId), replayed: false };
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Claim a ready job by committing a new open attempt. With a known
|
|
287
|
+
* attemptId for the same job, returns the stored attempt (replayed: true).
|
|
288
|
+
*/
|
|
289
|
+
claim(jobId, opts = {}) {
|
|
290
|
+
const nowMs = this.store.nowMs();
|
|
291
|
+
this.expireDue(nowMs);
|
|
292
|
+
const state = this.store.state;
|
|
293
|
+
if (opts.attemptId !== undefined) {
|
|
294
|
+
const known = state.attempts.find((a) => a.record.attemptId === opts.attemptId);
|
|
295
|
+
if (known && opts.workerId !== undefined) {
|
|
296
|
+
// A worker's replay (US-011 slice 4 B review C1) is answered only for its own live
|
|
297
|
+
// claim of this job: open, in lease, claimed by it. Anything else gets one generic
|
|
298
|
+
// refusal that names no job, worker or company, so a stale or foreign id reveals
|
|
299
|
+
// nothing and a replay can never hand out a dead claim (or its credentials) again.
|
|
300
|
+
// The leaseExpired condition is a second guard: expireDue(nowMs) above has already
|
|
301
|
+
// committed every lapsed open attempt as abandoned at this same nowMs, so a lapsed
|
|
302
|
+
// attempt fails the "open" condition first. It keeps the replay safe if the sweep
|
|
303
|
+
// is ever moved, skipped or given a different clock.
|
|
304
|
+
if (known.record.jobId !== jobId || known.claimedBy !== opts.workerId || known.resolution !== "open" || leaseExpired(known, nowMs)) {
|
|
305
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
306
|
+
found: `attemptId ${opts.attemptId} has no live claim of yours on this job to replay`,
|
|
307
|
+
expected: "a new attemptId, or the attemptId of your own open, unexpired claim of this job",
|
|
308
|
+
action: "Use a new attemptId. Nothing was written.",
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
this.store.assertOwner();
|
|
312
|
+
opts.guard?.(state);
|
|
313
|
+
return { ...attemptView(known), replayed: true };
|
|
314
|
+
}
|
|
315
|
+
if (known) {
|
|
316
|
+
if (known.record.jobId !== jobId) {
|
|
317
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
318
|
+
found: `attempt ${opts.attemptId} belongs to job ${known.record.jobId}`,
|
|
319
|
+
expected: `a new attemptId, or one stored for job ${jobId}`,
|
|
320
|
+
action: "Use a new attemptId. Nothing was written.",
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// A replay is answered only while this handle still owns the store (CX-4).
|
|
324
|
+
this.store.assertOwner();
|
|
325
|
+
if (known.claimedBy !== (opts.workerId ?? null))
|
|
326
|
+
throw notClaimant(opts.workerId, opts.attemptId);
|
|
327
|
+
opts.guard?.(state);
|
|
328
|
+
return { ...attemptView(known), replayed: true };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const entry = state.jobs.find((j) => j.record.jobId === jobId);
|
|
332
|
+
if (!entry)
|
|
333
|
+
throw this.unknownJob(jobId);
|
|
334
|
+
const jobState = deriveJobState(state, entry);
|
|
335
|
+
if (jobState !== "ready") {
|
|
336
|
+
const actions = {
|
|
337
|
+
waiting: `Wait until ${entry.dependsOn.join(", ")} have passed.`,
|
|
338
|
+
running: "An attempt is already open; record its outcome first.",
|
|
339
|
+
passed: "The job has already passed; nothing to do.",
|
|
340
|
+
failed: "The job failed; it stays failed and blocks its dependents.",
|
|
341
|
+
};
|
|
342
|
+
throw new DeliveryStoreError("NOT_CLAIMABLE", {
|
|
343
|
+
found: `job ${jobId} is ${jobState}`,
|
|
344
|
+
expected: "a ready job",
|
|
345
|
+
action: actions[jobState],
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
const attemptId = opts.attemptId ?? this.ids();
|
|
349
|
+
if (state.attempts.some((a) => a.record.attemptId === attemptId)) {
|
|
350
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
351
|
+
found: `generated attemptId ${attemptId} already exists`,
|
|
352
|
+
expected: "a unique attemptId",
|
|
353
|
+
action: "Fix the id generator. Nothing was written.",
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
const job = entry.record;
|
|
357
|
+
// max+1, not count+1: a gap in stored numbers must not produce a duplicate (CX-5).
|
|
358
|
+
const number = Math.max(0, ...state.attempts.filter((a) => a.record.jobId === jobId).map((a) => a.record.number)) + 1;
|
|
359
|
+
const record = {
|
|
360
|
+
schemaVersion: DELIVERY_SCHEMA_VERSIONS.attempt,
|
|
361
|
+
tenantId: job.tenantId,
|
|
362
|
+
attemptId,
|
|
363
|
+
jobId,
|
|
364
|
+
runId: job.runId,
|
|
365
|
+
number,
|
|
366
|
+
outcome: "pending",
|
|
367
|
+
};
|
|
368
|
+
const res = validateRecord("attempt", record);
|
|
369
|
+
if (!res.ok)
|
|
370
|
+
throw invalid(`attempt: ${res.errors.join("; ")}`, "an attempt valid under the US-001 contract");
|
|
371
|
+
const epoch = this.epoch;
|
|
372
|
+
const now = iso(nowMs);
|
|
373
|
+
// Only a worker's claim has a lease: it is the worker that renews it. A direct claim never expires.
|
|
374
|
+
const leaseExpiresAt = opts.workerId !== undefined ? iso(nowMs + this.leaseMs) : null;
|
|
375
|
+
const stored = { record, claimedAt: now, claimedEpoch: epoch, claimedBy: opts.workerId ?? null, leaseExpiresAt, resolution: "open", resolvedAt: null, resolvedEpoch: null };
|
|
376
|
+
this.store.commit(epoch, (draft) => {
|
|
377
|
+
opts.guard?.(draft);
|
|
378
|
+
draft.attempts.push(structuredClone(stored));
|
|
379
|
+
});
|
|
380
|
+
return { ...attemptView(stored), replayed: false };
|
|
381
|
+
}
|
|
382
|
+
/** Report the outcome of an open attempt. Repeating the same report is a no-op. */
|
|
383
|
+
recordOutcome(attemptId, outcome, opts = {}) {
|
|
384
|
+
if (outcome !== "passed" && outcome !== "failed")
|
|
385
|
+
throw invalid(`outcome ${JSON.stringify(outcome)}`, '"passed" or "failed"');
|
|
386
|
+
const nowMs = this.store.nowMs();
|
|
387
|
+
this.expireDue(nowMs);
|
|
388
|
+
const known = this.store.state.attempts.find((a) => a.record.attemptId === attemptId);
|
|
389
|
+
if (!known) {
|
|
390
|
+
throw new DeliveryStoreError("UNKNOWN_ATTEMPT", {
|
|
391
|
+
found: `no attempt ${attemptId}`,
|
|
392
|
+
expected: "an attemptId returned by claim",
|
|
393
|
+
action: "Check the attemptId. Nothing was written.",
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (known.resolution === "abandoned")
|
|
397
|
+
throw abandonedError(known);
|
|
398
|
+
if (known.resolution === "reported") {
|
|
399
|
+
if (known.record.outcome === outcome) {
|
|
400
|
+
opts.guard?.(this.store.state);
|
|
401
|
+
return attemptView(known);
|
|
402
|
+
}
|
|
403
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
404
|
+
found: `attempt ${attemptId} already reported ${known.record.outcome}`,
|
|
405
|
+
expected: `no report, or the same outcome ${known.record.outcome}`,
|
|
406
|
+
action: "Outcomes are final. Nothing was written.",
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
const epoch = this.epoch;
|
|
410
|
+
const now = iso(nowMs);
|
|
411
|
+
this.store.commit(epoch, (draft) => {
|
|
412
|
+
opts.guard?.(draft);
|
|
413
|
+
const a = draft.attempts.find((x) => x.record.attemptId === attemptId);
|
|
414
|
+
if (!a)
|
|
415
|
+
return;
|
|
416
|
+
if (a.resolution !== "open" || leaseExpired(a, nowMs))
|
|
417
|
+
throw abandonedError(a);
|
|
418
|
+
a.record.outcome = outcome;
|
|
419
|
+
a.resolution = "reported";
|
|
420
|
+
a.resolvedAt = now;
|
|
421
|
+
a.resolvedEpoch = epoch;
|
|
422
|
+
});
|
|
423
|
+
const updated = this.store.state.attempts.find((a) => a.record.attemptId === attemptId);
|
|
424
|
+
return attemptView(updated ?? known);
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* US-007: return an open attempt unfinished, so its job can run again. The
|
|
428
|
+
* attempt is marked abandoned (outcome "failed") in one fenced commit, the
|
|
429
|
+
* same state the recovery rule leaves a stale attempt in: the job allows
|
|
430
|
+
* exactly one new attempt and a late report is refused with
|
|
431
|
+
* ATTEMPT_ABANDONED. Returning an abandoned attempt again is a no-op; a
|
|
432
|
+
* reported attempt cannot be returned.
|
|
433
|
+
*/
|
|
434
|
+
returnAttempt(attemptId, opts = {}) {
|
|
435
|
+
const nowMs = this.store.nowMs();
|
|
436
|
+
this.expireDue(nowMs);
|
|
437
|
+
const known = this.store.state.attempts.find((a) => a.record.attemptId === attemptId);
|
|
438
|
+
if (!known) {
|
|
439
|
+
throw new DeliveryStoreError("UNKNOWN_ATTEMPT", {
|
|
440
|
+
found: `no attempt ${attemptId}`,
|
|
441
|
+
expected: "an attemptId returned by claim",
|
|
442
|
+
action: "Check the attemptId. Nothing was written.",
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
if (known.resolution === "abandoned") {
|
|
446
|
+
this.store.assertOwner();
|
|
447
|
+
opts.guard?.(this.store.state);
|
|
448
|
+
return attemptView(known);
|
|
449
|
+
}
|
|
450
|
+
if (known.resolution === "reported") {
|
|
451
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
452
|
+
found: `attempt ${attemptId} already reported ${known.record.outcome}`,
|
|
453
|
+
expected: "an open attempt",
|
|
454
|
+
action: "A reported attempt is final and cannot be returned. Nothing was written.",
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
const epoch = this.epoch;
|
|
458
|
+
const now = iso(nowMs);
|
|
459
|
+
this.store.commit(epoch, (draft) => {
|
|
460
|
+
opts.guard?.(draft);
|
|
461
|
+
const a = draft.attempts.find((x) => x.record.attemptId === attemptId);
|
|
462
|
+
if (!a)
|
|
463
|
+
return;
|
|
464
|
+
a.record.outcome = "failed";
|
|
465
|
+
a.resolution = "abandoned";
|
|
466
|
+
a.resolvedAt = now;
|
|
467
|
+
a.resolvedEpoch = epoch;
|
|
468
|
+
});
|
|
469
|
+
const updated = this.store.state.attempts.find((a) => a.record.attemptId === attemptId);
|
|
470
|
+
return attemptView(updated ?? known);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Extend the lease of an open attempt to now + claimLeaseMs on the store clock.
|
|
474
|
+
* Refused with ATTEMPT_ABANDONED when the lease has already expired (at
|
|
475
|
+
* exactly leaseExpiresAt it has), with ATTEMPT_CONFLICT for a reported attempt
|
|
476
|
+
* or one without a lease, and ACCESS_DENIED for another claimant. The guard and
|
|
477
|
+
* every check run again inside the commit, so the renewal and the expiry it
|
|
478
|
+
* races are decided once, at one clock reading.
|
|
479
|
+
*/
|
|
480
|
+
renewLease(attemptId, opts = {}) {
|
|
481
|
+
const nowMs = this.store.nowMs();
|
|
482
|
+
this.expireDue(nowMs);
|
|
483
|
+
const known = this.store.state.attempts.find((a) => a.record.attemptId === attemptId);
|
|
484
|
+
if (!known) {
|
|
485
|
+
throw new DeliveryStoreError("UNKNOWN_ATTEMPT", {
|
|
486
|
+
found: `no attempt ${attemptId}`,
|
|
487
|
+
expected: "an attemptId returned by claim",
|
|
488
|
+
action: "Check the attemptId. Nothing was written.",
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
if (known.claimedBy !== (opts.workerId ?? null))
|
|
492
|
+
throw notClaimant(opts.workerId, attemptId);
|
|
493
|
+
const check = (a) => {
|
|
494
|
+
if (!a)
|
|
495
|
+
throw notClaimant(opts.workerId, attemptId);
|
|
496
|
+
if (a.resolution === "abandoned" || leaseExpired(a, nowMs))
|
|
497
|
+
throw abandonedError(a);
|
|
498
|
+
if (a.resolution === "reported") {
|
|
499
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
500
|
+
found: `attempt ${attemptId} already reported ${a.record.outcome}`,
|
|
501
|
+
expected: "an open attempt",
|
|
502
|
+
action: "The outcome is final; there is no lease to renew. Nothing was written.",
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
if (a.leaseExpiresAt === null) {
|
|
506
|
+
throw new DeliveryStoreError("ATTEMPT_CONFLICT", {
|
|
507
|
+
found: `attempt ${attemptId} has no lease`,
|
|
508
|
+
expected: "an attempt claimed by a worker under a lease",
|
|
509
|
+
action: "Only a worker's claim has a lease to renew. Nothing was written.",
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
return a;
|
|
513
|
+
};
|
|
514
|
+
check(known);
|
|
515
|
+
const leaseExpiresAt = iso(nowMs + this.leaseMs);
|
|
516
|
+
this.store.commit(this.epoch, (draft) => {
|
|
517
|
+
opts.guard?.(draft);
|
|
518
|
+
const a = check(draft.attempts.find((x) => x.record.attemptId === attemptId));
|
|
519
|
+
if (a.claimedBy !== (opts.workerId ?? null))
|
|
520
|
+
throw notClaimant(opts.workerId, attemptId);
|
|
521
|
+
a.leaseExpiresAt = leaseExpiresAt;
|
|
522
|
+
});
|
|
523
|
+
return { attemptId, leaseExpiresAt };
|
|
524
|
+
}
|
|
525
|
+
/** The lease time of an attempt, or null when it has none. */
|
|
526
|
+
leaseExpiresAt(attemptId) {
|
|
527
|
+
const a = this.store.state.attempts.find((x) => x.record.attemptId === attemptId);
|
|
528
|
+
if (!a) {
|
|
529
|
+
throw new DeliveryStoreError("UNKNOWN_ATTEMPT", {
|
|
530
|
+
found: `no attempt ${attemptId}`,
|
|
531
|
+
expected: "an attemptId returned by claim",
|
|
532
|
+
action: "Check the attemptId.",
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
return a.leaseExpiresAt;
|
|
536
|
+
}
|
|
537
|
+
/** The claim lease length in ms that this controller gives a worker's claim. A heartbeat derives its period from it. */
|
|
538
|
+
get claimLeaseMs() {
|
|
539
|
+
return this.leaseMs;
|
|
540
|
+
}
|
|
541
|
+
/** True when the attempt is open under a lease that has run out on the store clock, but has not been swept yet. */
|
|
542
|
+
leaseLapsed(attemptId) {
|
|
543
|
+
const a = this.store.state.attempts.find((x) => x.record.attemptId === attemptId);
|
|
544
|
+
return a !== undefined && leaseExpired(a, this.store.nowMs());
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Abandon every open attempt whose lease has run out at the store clock's
|
|
548
|
+
* current time, in one commit. Returns the abandoned attemptIds (none: no commit).
|
|
549
|
+
*/
|
|
550
|
+
expireLeases() {
|
|
551
|
+
this.store.assertOwner();
|
|
552
|
+
return this.expireDue(this.store.nowMs());
|
|
553
|
+
}
|
|
554
|
+
/** Abandon the attempts whose lease has run out at `nowMs`. One commit, or none when nothing has expired. */
|
|
555
|
+
expireDue(nowMs) {
|
|
556
|
+
if (!this.store.state.attempts.some((a) => leaseExpired(a, nowMs)))
|
|
557
|
+
return [];
|
|
558
|
+
const epoch = this.epoch;
|
|
559
|
+
const expired = [];
|
|
560
|
+
this.store.commit(epoch, (draft) => {
|
|
561
|
+
for (const a of draft.attempts) {
|
|
562
|
+
if (!leaseExpired(a, nowMs))
|
|
563
|
+
continue;
|
|
564
|
+
abandon(a, iso(nowMs), epoch);
|
|
565
|
+
expired.push(a.record.attemptId);
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
return expired;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Revoke a worker and abandon its open attempts in the same fenced commit, so
|
|
572
|
+
* a revoked worker's claims are freed at once. `mutate` changes the worker
|
|
573
|
+
* records and may throw to refuse; nothing is written then. Returns the
|
|
574
|
+
* abandoned attemptIds.
|
|
575
|
+
* @internal Used by enrollment.ts revoke().
|
|
576
|
+
*/
|
|
577
|
+
commitRevocation(workerId, mutate) {
|
|
578
|
+
const epoch = this.epoch;
|
|
579
|
+
const now = this.store.now();
|
|
580
|
+
const freed = [];
|
|
581
|
+
this.store.commit(epoch, (draft) => {
|
|
582
|
+
mutate(draft.workers, { epoch, now });
|
|
583
|
+
for (const a of draft.attempts) {
|
|
584
|
+
if (a.resolution !== "open" || a.claimedBy !== workerId)
|
|
585
|
+
continue;
|
|
586
|
+
abandon(a, now, epoch);
|
|
587
|
+
freed.push(a.record.attemptId);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
return freed;
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Throw unless this handle still owns the store: STORE_CLOSED for a closed
|
|
594
|
+
* or failed handle, FENCED when another controller has taken over. Read only.
|
|
595
|
+
*/
|
|
596
|
+
assertOwner() {
|
|
597
|
+
this.store.assertOwner();
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* The notification events the committed ledger implies (US-005), in ledger
|
|
601
|
+
* order. Derived from committed records only, so an event exists exactly
|
|
602
|
+
* when its transition was committed. Only an owning handle may serve them.
|
|
603
|
+
*/
|
|
604
|
+
ledgerEvents() {
|
|
605
|
+
this.store.assertOwner();
|
|
606
|
+
return ledgerEvents(this.store.state);
|
|
607
|
+
}
|
|
608
|
+
/** Copies of the stored worker records, including secret digests. For the US-004 access layer. */
|
|
609
|
+
workerRecords() {
|
|
610
|
+
return structuredClone(this.store.state.workers);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Change worker records in one fenced store commit. `mutate` may throw to
|
|
614
|
+
* refuse; nothing is written then.
|
|
615
|
+
* @internal Used by enrollment.ts; callers outside the access layer must not write workers.
|
|
616
|
+
*/
|
|
617
|
+
commitWorkers(mutate) {
|
|
618
|
+
const epoch = this.epoch;
|
|
619
|
+
const now = this.store.now();
|
|
620
|
+
this.store.commit(epoch, (draft) => mutate(draft.workers, { epoch, now }));
|
|
621
|
+
}
|
|
622
|
+
/** US-008: copies of the artifact provenance records and cache entries. */
|
|
623
|
+
artifactRecords() {
|
|
624
|
+
return structuredClone({ artifacts: this.store.state.artifacts, cache: this.store.state.cache });
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Change artifact and cache records in one fenced store commit. `mutate`
|
|
628
|
+
* gets the whole draft so it can re-check workers and attempts against the
|
|
629
|
+
* state being written; it may throw to refuse, and nothing is written then.
|
|
630
|
+
* @internal Used by artifacts.ts; other callers must not write artifacts.
|
|
631
|
+
*/
|
|
632
|
+
commitArtifacts(mutate) {
|
|
633
|
+
const epoch = this.epoch;
|
|
634
|
+
const now = this.store.now();
|
|
635
|
+
this.store.commit(epoch, (draft) => mutate(draft, { epoch, now }));
|
|
636
|
+
}
|
|
637
|
+
/** US-009: copies of the deployment ledger. */
|
|
638
|
+
deploymentRecords() {
|
|
639
|
+
this.store.assertOwner();
|
|
640
|
+
return structuredClone(this.store.state.deployments);
|
|
641
|
+
}
|
|
642
|
+
/** US-009: copies of the stored company deploy policies. */
|
|
643
|
+
deployPolicyRecords() {
|
|
644
|
+
this.store.assertOwner();
|
|
645
|
+
return structuredClone(this.store.state.deployPolicies);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Change the deployment ledger in one fenced store commit. `mutate` gets the
|
|
649
|
+
* whole draft so it can re-check attempts against the state being written;
|
|
650
|
+
* it may throw to refuse, and nothing is written then.
|
|
651
|
+
* @internal Used by effects.ts; other callers must not write deployments.
|
|
652
|
+
*/
|
|
653
|
+
commitDeployments(mutate) {
|
|
654
|
+
const epoch = this.epoch;
|
|
655
|
+
const now = this.store.now();
|
|
656
|
+
this.store.commit(epoch, (draft) => mutate(draft, { epoch, now }));
|
|
657
|
+
}
|
|
658
|
+
/** Release ownership of the store. Idempotent. */
|
|
659
|
+
close() {
|
|
660
|
+
this.store.close();
|
|
661
|
+
}
|
|
662
|
+
unknownJob(jobId) {
|
|
663
|
+
return new DeliveryStoreError("UNKNOWN_JOB", {
|
|
664
|
+
found: `no accepted job ${jobId}`,
|
|
665
|
+
expected: "an accepted jobId",
|
|
666
|
+
action: "Accept the job first. Nothing was written.",
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
/** Mark an open attempt abandoned: outcome failed, resolved now in `epoch`. */
|
|
671
|
+
function abandon(a, now, epoch) {
|
|
672
|
+
a.record.outcome = "failed";
|
|
673
|
+
a.resolution = "abandoned";
|
|
674
|
+
a.resolvedAt = now;
|
|
675
|
+
a.resolvedEpoch = epoch;
|
|
676
|
+
}
|
|
677
|
+
/** The refusal of a late call for an abandoned (or lease-expired) attempt. */
|
|
678
|
+
function abandonedError(a) {
|
|
679
|
+
return new DeliveryStoreError("ATTEMPT_ABANDONED", {
|
|
680
|
+
found: a.resolution === "abandoned"
|
|
681
|
+
? `attempt ${a.record.attemptId} was abandoned at epoch ${a.resolvedEpoch} (its controller stopped, its lease expired, its worker was revoked or it was returned)`
|
|
682
|
+
: `attempt ${a.record.attemptId} lease expired at ${a.leaseExpiresAt}`,
|
|
683
|
+
expected: "an open attempt",
|
|
684
|
+
action: "Discard this result. Claim the job again to run a new attempt. Nothing was written.",
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
/** An id another tenant holds. The answer does not say that it exists or whose it is. */
|
|
688
|
+
function unavailableId(kind, id) {
|
|
689
|
+
return new DeliveryStoreError("DUPLICATE_CONFLICT", {
|
|
690
|
+
found: `${kind}Id ${id} cannot be used`,
|
|
691
|
+
expected: `a new ${kind}Id`,
|
|
692
|
+
action: `Use a different ${kind}Id. Nothing was written.`,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
/** An attempt claimed by a different worker. Names only the caller and the attemptId it presented. */
|
|
696
|
+
export function notClaimant(workerId, attemptId) {
|
|
697
|
+
return new DeliveryStoreError("ACCESS_DENIED", {
|
|
698
|
+
found: `${workerId === undefined ? "a caller without a worker" : `worker ${workerId}`} asked for attempt ${attemptId}, which it did not claim`,
|
|
699
|
+
expected: "an attempt claimed by the same worker",
|
|
700
|
+
action: "Report or return an attempt only from the worker that claimed it. Nothing was written.",
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
//# sourceMappingURL=reconciler.js.map
|