@indigoai-us/hq-cli 5.345.212 → 5.345.215-beta.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/CHANGELOG.md +49 -0
- package/dist/bin/hq-auth-refresh.js +2 -1
- package/dist/command-catalog.generated.d.ts +442 -50
- package/dist/command-catalog.generated.js +541 -50
- package/dist/commands/agent-inbox.d.ts +11 -1
- package/dist/commands/agent-inbox.js +128 -38
- package/dist/commands/agent-kit.js +2 -2
- 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 +169 -51
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +120 -39
- 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-handoff.js +1 -0
- package/dist/commands/core-hq-session-native.js +2 -2
- package/dist/commands/core-pin-native.js +1 -1
- package/dist/commands/core-policy.js +1 -1
- package/dist/commands/core-resolve-company-native.js +1 -1
- package/dist/commands/core-sentry-report-fast.d.ts +2 -0
- package/dist/commands/core-sentry-report-fast.js +3 -1
- package/dist/commands/daemon.js +11 -1
- package/dist/commands/integrations-connect.d.ts +17 -4
- package/dist/commands/integrations-connect.js +224 -26
- package/dist/commands/lanes-link.js +10 -5
- package/dist/commands/lanes.js +26 -9
- package/dist/commands/rescue.js +13 -0
- package/dist/commands/secrets.d.ts +11 -0
- package/dist/commands/secrets.js +41 -7
- 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-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/index.js +2 -0
- package/dist/lazy-commands.js +2 -0
- package/dist/lib/agent-kit/inbox-fetch.d.ts +122 -1
- package/dist/lib/agent-kit/inbox-fetch.js +215 -3
- package/dist/lib/agent-kit/inbox-redact.js +1 -1
- package/dist/lib/agent-kit/inbox-state.d.ts +98 -1
- package/dist/lib/agent-kit/inbox-state.js +261 -4
- package/dist/lib/agent-kit/mcp/tools.d.ts +3 -3
- package/dist/lib/agent-kit/mcp/tools.js +68 -25
- package/dist/lib/agent-kit/run/inbox.js +4 -2
- package/dist/lib/agent-kit/skills.js +4 -1
- package/dist/lib/agent-kit/wake.d.ts +9 -1
- package/dist/lib/agent-kit/wake.js +10 -3
- 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.js +193 -14
- package/dist/lib/bot/fleet-download.d.ts +2 -0
- package/dist/lib/bot/fleet-download.js +23 -0
- package/dist/lib/bot/fleet-local.d.ts +34 -1
- package/dist/lib/bot/fleet-local.js +254 -37
- 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/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/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/status.d.ts +2 -0
- 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/core-utils/session-id.d.ts +1 -1
- package/dist/lib/core-utils/session-id.js +4 -0
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/daemon/run.js +9 -1
- package/dist/lib/daemon/updater.d.ts +34 -2
- package/dist/lib/daemon/updater.js +51 -2
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/artifacts.d.ts +99 -0
- package/dist/lib/delivery/artifacts.js +548 -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/enrollment.d.ts +142 -0
- package/dist/lib/delivery/enrollment.js +382 -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/migrations.d.ts +139 -0
- package/dist/lib/delivery/migrations.js +623 -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 +61 -0
- package/dist/lib/delivery/mqtt.js +112 -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 +151 -0
- package/dist/lib/delivery/reconciler.js +492 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/store.d.ts +161 -0
- package/dist/lib/delivery/store.js +940 -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 +176 -0
- package/dist/lib/delivery/workers/macos.js +901 -0
- package/dist/lib/delivery/workers/windows-boundary.d.ts +453 -0
- package/dist/lib/delivery/workers/windows-boundary.js +693 -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.d.ts +241 -0
- package/dist/lib/delivery/workers/windows.js +608 -0
- package/dist/lib/doctor/checks/release-links.d.ts +50 -8
- package/dist/lib/doctor/checks/release-links.js +260 -24
- package/dist/lib/doctor/fix/apply.js +24 -8
- package/dist/lib/doctor/fix/remediation.js +15 -5
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/lib/lanes/autopilot-registry.d.ts +14 -0
- package/dist/lib/lanes/autopilot-registry.js +59 -7
- package/dist/lib/lanes/links.d.ts +14 -0
- package/dist/lib/lanes/links.js +320 -25
- package/dist/lib/lanes/outpost-pruner-install.d.ts +10 -0
- package/dist/lib/lanes/outpost-pruner-install.js +4 -3
- package/dist/main.js +3 -1
- package/dist/sentry-init.d.ts +2 -0
- package/dist/sentry-init.js +13 -2
- package/dist/sentry.d.ts +2 -0
- package/dist/sentry.js +5 -1
- package/dist/utils/cli-network-lifecycle.d.ts +9 -0
- package/dist/utils/cli-network-lifecycle.js +52 -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/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/safe-link.d.ts +63 -0
- package/dist/utils/safe-link.js +246 -0
- package/dist/utils/sentry-keepalive-transport.d.ts +28 -0
- package/dist/utils/sentry-keepalive-transport.js +70 -0
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verified backups of the self-hosted delivery control plane (US-010).
|
|
3
|
+
* Retention and recovery objectives: docs/delivery-recovery.md.
|
|
4
|
+
*
|
|
5
|
+
* What is copied: the exact committed state.json bytes and every artifact
|
|
6
|
+
* blob that state references. Nothing else in the store directory is read
|
|
7
|
+
* (owner.lock, owner.db, stray files such as an .env are never copied).
|
|
8
|
+
*
|
|
9
|
+
* Consistency: the state is read and every referenced blob is copied while
|
|
10
|
+
* holding the store lock (withStoreSnapshot), so no commit lands in between.
|
|
11
|
+
* The controller keeps its ownership; its commits wait for the lock for at
|
|
12
|
+
* most gateWaitMs and are retried by the caller. Blobs are content-addressed
|
|
13
|
+
* and immutable, so the copy is the bytes the state names.
|
|
14
|
+
*
|
|
15
|
+
* Verification on write: each blob is re-hashed against its signed
|
|
16
|
+
* provenance (digest, size and HMAC signature) as it is copied. The backup
|
|
17
|
+
* is written into `.partial-<id>` and verified from disk again (verifyBackup)
|
|
18
|
+
* before it is renamed to `<id>`. A crash leaves only a `.partial-*`
|
|
19
|
+
* directory, which is never listed or restorable and is removed by the next
|
|
20
|
+
* backup or prune.
|
|
21
|
+
*
|
|
22
|
+
* Manifest: format, id, time, epoch, schema, state digest and size, one
|
|
23
|
+
* entry per artifact (digest, size, file), signed with HMAC-SHA256 using the
|
|
24
|
+
* provenance signing key. Only the key's ref is stored.
|
|
25
|
+
*
|
|
26
|
+
* Secrets: credentials stay refs. The store holds only digests of worker
|
|
27
|
+
* tokens and hashes of env values. As a second check, every byte written is
|
|
28
|
+
* scanned for the signing key value and for each probed credential value;
|
|
29
|
+
* a match refuses the backup and names the ref, never the value.
|
|
30
|
+
*/
|
|
31
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
32
|
+
import { chmodSync, closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync, writeSync, } from "node:fs";
|
|
33
|
+
import { basename, join } from "node:path";
|
|
34
|
+
import { blobRelativePath } from "./artifacts.js";
|
|
35
|
+
import { DeliveryStoreError, parseStoreText } from "./migrations.js";
|
|
36
|
+
import { canonicalJson, sha256Hex, signatureMatches } from "./provenance.js";
|
|
37
|
+
import { STATE_FILE, withStoreSnapshot } from "./store.js";
|
|
38
|
+
export const BACKUP_FORMAT = "hq-delivery.backup.v1";
|
|
39
|
+
export const BACKUP_MANIFEST_FILE = "manifest.json";
|
|
40
|
+
export const BACKUP_BLOBS_DIR = "blobs";
|
|
41
|
+
const PARTIAL_PREFIX = ".partial-";
|
|
42
|
+
const DEFAULT_LOCK_WAIT_MS = 2000;
|
|
43
|
+
export function backupInvalid(found, expected, action = "Use another backup. Nothing was restored or written.") {
|
|
44
|
+
return new DeliveryStoreError("BACKUP_INVALID", { found, expected, action });
|
|
45
|
+
}
|
|
46
|
+
function errCode(err) {
|
|
47
|
+
return err?.code;
|
|
48
|
+
}
|
|
49
|
+
export function resolveKey(signing) {
|
|
50
|
+
let value;
|
|
51
|
+
try {
|
|
52
|
+
value = signing.source.resolve(signing.ref);
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
value = "";
|
|
56
|
+
}
|
|
57
|
+
if (typeof value !== "string" || value === "") {
|
|
58
|
+
throw new DeliveryStoreError("CREDENTIAL_UNAVAILABLE", {
|
|
59
|
+
found: `no value for backup signing key ${signing.ref}`,
|
|
60
|
+
expected: `a non-empty value for credential ${signing.ref}`,
|
|
61
|
+
action: `Configure credential ${signing.ref}. Nothing was written.`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
function manifestSignature(body, key) {
|
|
67
|
+
return createHmac("sha256", key).update(`${BACKUP_FORMAT}\n${canonicalJson(body)}`).digest("hex");
|
|
68
|
+
}
|
|
69
|
+
/** Write `bytes` to a new file, fsync it and make it read-only. */
|
|
70
|
+
export function writeFileDurable(path, bytes, mode = 0o444) {
|
|
71
|
+
const fd = openSync(path, "wx", 0o600);
|
|
72
|
+
try {
|
|
73
|
+
let off = 0;
|
|
74
|
+
while (off < bytes.length)
|
|
75
|
+
off += writeSync(fd, bytes, off, bytes.length - off);
|
|
76
|
+
fsyncSync(fd);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
closeSync(fd);
|
|
80
|
+
}
|
|
81
|
+
chmodSync(path, mode);
|
|
82
|
+
}
|
|
83
|
+
export function fsyncDirectory(dir) {
|
|
84
|
+
let fd;
|
|
85
|
+
try {
|
|
86
|
+
fd = openSync(dir, "r");
|
|
87
|
+
fsyncSync(fd);
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
if (!["EISDIR", "EINVAL", "EPERM", "EBADF"].includes(errCode(err) ?? ""))
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
if (fd !== undefined)
|
|
95
|
+
closeSync(fd);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Read a regular, non-symlink file or return null when it is missing. */
|
|
99
|
+
function readRegular(path) {
|
|
100
|
+
let st;
|
|
101
|
+
try {
|
|
102
|
+
st = lstatSync(path);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
if (errCode(err) === "ENOENT")
|
|
106
|
+
return null;
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
if (!st.isFile())
|
|
110
|
+
return null;
|
|
111
|
+
return readFileSync(path);
|
|
112
|
+
}
|
|
113
|
+
function probeValues(opts, key) {
|
|
114
|
+
const out = [{ ref: opts.signing.ref, value: Buffer.from(key) }];
|
|
115
|
+
for (const ref of opts.secretProbe?.refs ?? []) {
|
|
116
|
+
let v;
|
|
117
|
+
try {
|
|
118
|
+
v = opts.secretProbe.source.resolve(ref);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
continue; // an unconfigured credential cannot leak
|
|
122
|
+
}
|
|
123
|
+
if (typeof v === "string" && v.length >= 8)
|
|
124
|
+
out.push({ ref, value: Buffer.from(v) });
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
function scan(bytes, what, probes) {
|
|
129
|
+
for (const p of probes) {
|
|
130
|
+
if (bytes.includes(p.value)) {
|
|
131
|
+
throw backupInvalid(`${what} contains the plaintext value of credential ${p.ref}`, "a backup with credential refs only, never values", `Rotate credential ${p.ref}, rebuild the artifact without it, then back up again. No backup was written.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function newBackupId(now) {
|
|
136
|
+
const stamp = new Date(now).toISOString().replace(/[-:.]/g, "");
|
|
137
|
+
return `backup-${stamp}-${randomBytes(3).toString("hex")}`;
|
|
138
|
+
}
|
|
139
|
+
function removePartials(root) {
|
|
140
|
+
for (const name of readdirSync(root))
|
|
141
|
+
if (name.startsWith(PARTIAL_PREFIX))
|
|
142
|
+
rmSync(join(root, name), { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
/** Create a verified backup of `storeDir` under `backupRoot`. */
|
|
145
|
+
export function createBackup(opts) {
|
|
146
|
+
const key = resolveKey(opts.signing);
|
|
147
|
+
const probes = probeValues(opts, key);
|
|
148
|
+
const clock = opts.clock ?? Date.now;
|
|
149
|
+
mkdirSync(opts.backupRoot, { recursive: true });
|
|
150
|
+
removePartials(opts.backupRoot);
|
|
151
|
+
const now = clock();
|
|
152
|
+
const backupId = newBackupId(now);
|
|
153
|
+
const partial = join(opts.backupRoot, `${PARTIAL_PREFIX}${backupId}`);
|
|
154
|
+
const final = join(opts.backupRoot, backupId);
|
|
155
|
+
let crashed = false;
|
|
156
|
+
const crash = (step) => {
|
|
157
|
+
try {
|
|
158
|
+
opts.crashPoint?.(step);
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
crashed = true; // a simulated crash leaves the partial directory behind, as a real one would
|
|
162
|
+
throw err;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
try {
|
|
166
|
+
mkdirSync(partial, { mode: 0o700 });
|
|
167
|
+
mkdirSync(join(partial, BACKUP_BLOBS_DIR), { mode: 0o700 });
|
|
168
|
+
const body = withStoreSnapshot(opts.storeDir, opts.gateWaitMs ?? DEFAULT_LOCK_WAIT_MS, (stateText) => {
|
|
169
|
+
const parsed = parseStoreText(stateText, join(opts.storeDir, STATE_FILE));
|
|
170
|
+
const stateBytes = Buffer.from(stateText);
|
|
171
|
+
scan(stateBytes, STATE_FILE, probes);
|
|
172
|
+
writeFileDurable(join(partial, STATE_FILE), stateBytes);
|
|
173
|
+
crash("snapshot");
|
|
174
|
+
const artifacts = copyBlobs(opts.storeDir, partial, parsed.state, key, probes);
|
|
175
|
+
const version = JSON.parse(stateText).storeSchemaVersion;
|
|
176
|
+
return {
|
|
177
|
+
format: BACKUP_FORMAT,
|
|
178
|
+
backupId,
|
|
179
|
+
createdAt: new Date(now).toISOString(),
|
|
180
|
+
sourceDir: opts.storeDir,
|
|
181
|
+
storeSchemaVersion: version,
|
|
182
|
+
epoch: parsed.state.epoch,
|
|
183
|
+
stateSha256: sha256Hex(stateBytes),
|
|
184
|
+
stateSize: stateBytes.length,
|
|
185
|
+
artifacts,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
fsyncDirectory(join(partial, BACKUP_BLOBS_DIR));
|
|
189
|
+
crash("blobs-copied");
|
|
190
|
+
const manifest = {
|
|
191
|
+
...body,
|
|
192
|
+
signature: { algorithm: "hmac-sha256", keyRef: opts.signing.ref, value: manifestSignature(body, key) },
|
|
193
|
+
};
|
|
194
|
+
const manifestBytes = Buffer.from(`${JSON.stringify(manifest, null, 2)}\n`);
|
|
195
|
+
scan(manifestBytes, BACKUP_MANIFEST_FILE, probes);
|
|
196
|
+
writeFileDurable(join(partial, BACKUP_MANIFEST_FILE), manifestBytes);
|
|
197
|
+
fsyncDirectory(partial);
|
|
198
|
+
crash("manifest-written");
|
|
199
|
+
verifyBackupContents(partial, opts.signing);
|
|
200
|
+
renameSync(partial, final);
|
|
201
|
+
fsyncDirectory(opts.backupRoot);
|
|
202
|
+
return { backupId, path: final, manifest };
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
if (!crashed)
|
|
206
|
+
rmSync(partial, { recursive: true, force: true });
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function copyBlobs(storeDir, partial, state, key, probes) {
|
|
211
|
+
const entries = [];
|
|
212
|
+
const missing = [];
|
|
213
|
+
for (const a of state.artifacts) {
|
|
214
|
+
const p = a.provenance;
|
|
215
|
+
const src = join(storeDir, blobRelativePath(p.tenantId, p.partition, p.digest.value));
|
|
216
|
+
const bytes = readRegular(src);
|
|
217
|
+
if (bytes === null) {
|
|
218
|
+
missing.push(a.record.artifactId);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const actual = sha256Hex(bytes);
|
|
222
|
+
if (actual !== p.digest.value || bytes.length !== p.size) {
|
|
223
|
+
throw backupInvalid(`artifact ${a.record.artifactId}: blob digest ${actual} (${bytes.length} bytes) does not match provenance ${p.digest.value} (${p.size} bytes)`, "every referenced blob to match its signed digest", "Investigate the store directory; rebuild the artifact. No backup was written.");
|
|
224
|
+
}
|
|
225
|
+
if (!signatureMatches(a, key)) {
|
|
226
|
+
throw backupInvalid(`artifact ${a.record.artifactId}: provenance signature does not verify`, "provenance signed with the configured key", "Investigate the store; a tampered record is not backed up. No backup was written.");
|
|
227
|
+
}
|
|
228
|
+
scan(bytes, `artifact ${a.record.artifactId}`, probes);
|
|
229
|
+
const file = join(BACKUP_BLOBS_DIR, p.digest.value);
|
|
230
|
+
if (!existsSync(join(partial, file)))
|
|
231
|
+
writeFileDurable(join(partial, file), bytes);
|
|
232
|
+
entries.push({ artifactId: a.record.artifactId, tenantId: p.tenantId, partition: p.partition, digest: p.digest.value, size: p.size, file });
|
|
233
|
+
}
|
|
234
|
+
if (missing.length > 0) {
|
|
235
|
+
throw backupInvalid(`referenced artifact blob missing for ${missing.join(", ")}`, "every artifact the state references to have its blob", "Restore the blobs or rebuild those artifacts, then back up again. No backup was written.");
|
|
236
|
+
}
|
|
237
|
+
return entries;
|
|
238
|
+
}
|
|
239
|
+
function verifyBackupContents(path, signing) {
|
|
240
|
+
const manifestBytes = readRegular(join(path, BACKUP_MANIFEST_FILE));
|
|
241
|
+
if (manifestBytes === null)
|
|
242
|
+
throw backupInvalid(`no ${BACKUP_MANIFEST_FILE} in ${path}`, "a complete backup with a manifest");
|
|
243
|
+
let m;
|
|
244
|
+
try {
|
|
245
|
+
m = JSON.parse(manifestBytes.toString("utf8"));
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
throw backupInvalid(`${BACKUP_MANIFEST_FILE} in ${path} is not valid JSON`, "a complete manifest");
|
|
249
|
+
}
|
|
250
|
+
if (m?.format !== BACKUP_FORMAT || typeof m.signature?.value !== "string" || !Array.isArray(m.artifacts)) {
|
|
251
|
+
throw backupInvalid(`${BACKUP_MANIFEST_FILE} in ${path} has format ${String(m?.format)}`, `a ${BACKUP_FORMAT} manifest`);
|
|
252
|
+
}
|
|
253
|
+
if (m.signature.keyRef !== signing.ref) {
|
|
254
|
+
throw backupInvalid(`backup signed with key ref ${m.signature.keyRef}`, `key ref ${signing.ref}`);
|
|
255
|
+
}
|
|
256
|
+
const { signature, ...body } = m;
|
|
257
|
+
const expected = Buffer.from(manifestSignature(body, resolveKey(signing)), "hex");
|
|
258
|
+
const got = Buffer.from(signature.value, "hex");
|
|
259
|
+
if (got.length !== expected.length || !timingSafeEqual(got, expected)) {
|
|
260
|
+
throw backupInvalid(`manifest signature in ${path} does not verify`, "a manifest signed with the configured key");
|
|
261
|
+
}
|
|
262
|
+
const state = readRegular(join(path, STATE_FILE));
|
|
263
|
+
if (state === null || sha256Hex(state) !== m.stateSha256 || state.length !== m.stateSize) {
|
|
264
|
+
throw backupInvalid(`${STATE_FILE} in ${path} is missing or does not match the manifest digest`, `sha256 ${m.stateSha256}`);
|
|
265
|
+
}
|
|
266
|
+
let parsed;
|
|
267
|
+
try {
|
|
268
|
+
parsed = parseStoreText(state.toString("utf8"), join(path, STATE_FILE)).state;
|
|
269
|
+
}
|
|
270
|
+
catch (err) {
|
|
271
|
+
throw backupInvalid(`${STATE_FILE} in ${path} does not load: ${err.message}`, "a valid store file");
|
|
272
|
+
}
|
|
273
|
+
if (parsed.epoch !== m.epoch)
|
|
274
|
+
throw backupInvalid(`state epoch ${parsed.epoch}, manifest epoch ${m.epoch}`, "matching epochs");
|
|
275
|
+
const listed = new Map(m.artifacts.map((e) => [e.artifactId, e]));
|
|
276
|
+
for (const a of parsed.artifacts) {
|
|
277
|
+
const e = listed.get(a.record.artifactId);
|
|
278
|
+
if (!e || e.digest !== a.provenance.digest.value) {
|
|
279
|
+
throw backupInvalid(`artifact ${a.record.artifactId} is referenced by the state but not in the manifest`, "every referenced artifact in the backup");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (listed.size !== parsed.artifacts.length)
|
|
283
|
+
throw backupInvalid("manifest lists artifacts the state does not reference", "the same artifact set");
|
|
284
|
+
for (const e of m.artifacts) {
|
|
285
|
+
if (e.file !== join(BACKUP_BLOBS_DIR, e.digest))
|
|
286
|
+
throw backupInvalid(`artifact ${e.artifactId} file ${e.file}`, `${BACKUP_BLOBS_DIR}/<digest>`);
|
|
287
|
+
const bytes = readRegular(join(path, e.file));
|
|
288
|
+
if (bytes === null)
|
|
289
|
+
throw backupInvalid(`artifact ${e.artifactId}: blob ${e.file} missing from ${path}`, "every referenced blob in the backup");
|
|
290
|
+
if (sha256Hex(bytes) !== e.digest || bytes.length !== e.size) {
|
|
291
|
+
throw backupInvalid(`artifact ${e.artifactId}: blob ${e.file} digest mismatch`, `sha256 ${e.digest} (${e.size} bytes)`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return m;
|
|
295
|
+
}
|
|
296
|
+
/** Verify a finished backup from disk: manifest signature, state digest and load, every blob digest. */
|
|
297
|
+
export function verifyBackup(path, signing) {
|
|
298
|
+
if (basename(path).startsWith(PARTIAL_PREFIX)) {
|
|
299
|
+
throw backupInvalid(`${path} is an incomplete backup left by a crash`, "a finished backup directory");
|
|
300
|
+
}
|
|
301
|
+
return verifyBackupContents(path, signing);
|
|
302
|
+
}
|
|
303
|
+
/** Finished backups under `root`, oldest first. Partial directories are skipped. Not verified; use verifyBackup. */
|
|
304
|
+
export function listBackups(root) {
|
|
305
|
+
if (!existsSync(root))
|
|
306
|
+
return [];
|
|
307
|
+
const out = [];
|
|
308
|
+
for (const name of readdirSync(root)) {
|
|
309
|
+
if (name.startsWith(PARTIAL_PREFIX))
|
|
310
|
+
continue;
|
|
311
|
+
const bytes = readRegular(join(root, name, BACKUP_MANIFEST_FILE));
|
|
312
|
+
if (bytes === null)
|
|
313
|
+
continue;
|
|
314
|
+
try {
|
|
315
|
+
const m = JSON.parse(bytes.toString("utf8"));
|
|
316
|
+
if (m.format === BACKUP_FORMAT && m.backupId === name)
|
|
317
|
+
out.push({ backupId: name, path: join(root, name), createdAt: m.createdAt, epoch: m.epoch });
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
// not a backup
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return out.sort((a, b) => (a.createdAt === b.createdAt ? a.backupId.localeCompare(b.backupId) : a.createdAt < b.createdAt ? -1 : 1));
|
|
324
|
+
}
|
|
325
|
+
/** Retention: keep the newest `keep` (>= 1) backups, remove older ones and crash leftovers. Returns removed backup ids. */
|
|
326
|
+
export function pruneBackups(root, opts) {
|
|
327
|
+
if (!Number.isInteger(opts.keep) || opts.keep < 1) {
|
|
328
|
+
throw new DeliveryStoreError("INVALID_RECORD", {
|
|
329
|
+
found: `keep ${String(opts.keep)}`,
|
|
330
|
+
expected: "keep to be an integer of at least 1",
|
|
331
|
+
action: "Retention never deletes every backup. Nothing was removed.",
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (!existsSync(root))
|
|
335
|
+
return [];
|
|
336
|
+
removePartials(root);
|
|
337
|
+
const all = listBackups(root);
|
|
338
|
+
const drop = all.slice(0, Math.max(0, all.length - opts.keep));
|
|
339
|
+
for (const b of drop)
|
|
340
|
+
rmSync(b.path, { recursive: true, force: true });
|
|
341
|
+
return drop.map((b) => b.backupId);
|
|
342
|
+
}
|
|
343
|
+
//# sourceMappingURL=backup.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build cache entries (US-008).
|
|
3
|
+
*
|
|
4
|
+
* An entry maps a cache key to one artifact. The key covers the tenant, the
|
|
5
|
+
* trust partition and the full input digest (provenance.ts), so a lookup
|
|
6
|
+
* never matches another company's entry, an entry from another partition, or
|
|
7
|
+
* an entry built from different source, recipe, toolchain, platform or
|
|
8
|
+
* allowlisted environment.
|
|
9
|
+
*
|
|
10
|
+
* Invalidation: recording an entry replaces any entry with the same key and
|
|
11
|
+
* removes every entry in the same tenant, partition and slot (same source and
|
|
12
|
+
* recipe id) whose inputs differ, so a changed toolchain, recipe digest,
|
|
13
|
+
* platform or env value for the same source drops the earlier entry. A hit
|
|
14
|
+
* whose artifact fails verification is evicted by artifacts.ts.
|
|
15
|
+
*
|
|
16
|
+
* These functions edit a store draft inside a fenced commit; they never
|
|
17
|
+
* write on their own.
|
|
18
|
+
*/
|
|
19
|
+
import type { StoredCacheEntry, TrustPartition } from "./provenance.js";
|
|
20
|
+
export declare function findCacheEntry(cache: readonly StoredCacheEntry[], query: {
|
|
21
|
+
tenantId: string;
|
|
22
|
+
partition: TrustPartition;
|
|
23
|
+
cacheKey: string;
|
|
24
|
+
}): StoredCacheEntry | undefined;
|
|
25
|
+
/** Add `entry` to `cache` in place. Returns the cache keys it invalidated, excluding a same-key replacement. */
|
|
26
|
+
export declare function recordCacheEntry(cache: StoredCacheEntry[], entry: StoredCacheEntry): string[];
|
|
27
|
+
/** Remove every entry pointing at `artifactId`. Returns how many were removed. */
|
|
28
|
+
export declare function evictArtifact(cache: StoredCacheEntry[], artifactId: string): number;
|
|
29
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function findCacheEntry(cache, query) {
|
|
2
|
+
return cache.find((c) => c.cacheKey === query.cacheKey && c.tenantId === query.tenantId && c.partition === query.partition);
|
|
3
|
+
}
|
|
4
|
+
/** Add `entry` to `cache` in place. Returns the cache keys it invalidated, excluding a same-key replacement. */
|
|
5
|
+
export function recordCacheEntry(cache, entry) {
|
|
6
|
+
const invalidated = [];
|
|
7
|
+
for (let i = cache.length - 1; i >= 0; i -= 1) {
|
|
8
|
+
const c = cache[i];
|
|
9
|
+
if (c.tenantId !== entry.tenantId || c.partition !== entry.partition)
|
|
10
|
+
continue;
|
|
11
|
+
if (c.cacheKey === entry.cacheKey) {
|
|
12
|
+
cache.splice(i, 1);
|
|
13
|
+
}
|
|
14
|
+
else if (c.slot === entry.slot && c.inputDigest !== entry.inputDigest) {
|
|
15
|
+
invalidated.unshift(c.cacheKey);
|
|
16
|
+
cache.splice(i, 1);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
cache.push(entry);
|
|
20
|
+
return invalidated;
|
|
21
|
+
}
|
|
22
|
+
/** Remove every entry pointing at `artifactId`. Returns how many were removed. */
|
|
23
|
+
export function evictArtifact(cache, artifactId) {
|
|
24
|
+
let removed = 0;
|
|
25
|
+
for (let i = cache.length - 1; i >= 0; i -= 1) {
|
|
26
|
+
if (cache[i].artifactId === artifactId) {
|
|
27
|
+
cache.splice(i, 1);
|
|
28
|
+
removed += 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return removed;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portable delivery contracts (US-001).
|
|
3
|
+
*
|
|
4
|
+
* Provider-neutral, versioned records for HQ delivery. Nothing here names a
|
|
5
|
+
* cloud or forge provider; adapters translate these records at the edges.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DELIVERY_SCHEMA_VERSIONS: {
|
|
8
|
+
readonly tenant: "hq-delivery.tenant.v1";
|
|
9
|
+
readonly run: "hq-delivery.run.v1";
|
|
10
|
+
readonly job: "hq-delivery.job.v1";
|
|
11
|
+
readonly attempt: "hq-delivery.attempt.v1";
|
|
12
|
+
readonly lease: "hq-delivery.lease.v1";
|
|
13
|
+
readonly recipe: "hq-delivery.recipe.v1";
|
|
14
|
+
readonly policy: "hq-delivery.policy.v1";
|
|
15
|
+
readonly artifact: "hq-delivery.artifact.v1";
|
|
16
|
+
};
|
|
17
|
+
export type DeliveryKind = keyof typeof DELIVERY_SCHEMA_VERSIONS;
|
|
18
|
+
interface Base<K extends DeliveryKind> {
|
|
19
|
+
schemaVersion: (typeof DELIVERY_SCHEMA_VERSIONS)[K];
|
|
20
|
+
tenantId: string;
|
|
21
|
+
}
|
|
22
|
+
export interface Tenant extends Base<"tenant"> {
|
|
23
|
+
displayName: string;
|
|
24
|
+
}
|
|
25
|
+
export interface Run extends Base<"run"> {
|
|
26
|
+
runId: string;
|
|
27
|
+
recipeId: string;
|
|
28
|
+
status: "queued" | "running" | "succeeded" | "failed" | "cancelled";
|
|
29
|
+
}
|
|
30
|
+
export interface Job extends Base<"job"> {
|
|
31
|
+
jobId: string;
|
|
32
|
+
runId: string;
|
|
33
|
+
name: string;
|
|
34
|
+
}
|
|
35
|
+
export interface Attempt extends Base<"attempt"> {
|
|
36
|
+
attemptId: string;
|
|
37
|
+
jobId: string;
|
|
38
|
+
runId: string;
|
|
39
|
+
number: number;
|
|
40
|
+
outcome: "pending" | "passed" | "failed";
|
|
41
|
+
}
|
|
42
|
+
export interface Lease extends Base<"lease"> {
|
|
43
|
+
leaseId: string;
|
|
44
|
+
runId: string;
|
|
45
|
+
holder: string;
|
|
46
|
+
expiresAt: string;
|
|
47
|
+
}
|
|
48
|
+
export interface Recipe extends Base<"recipe"> {
|
|
49
|
+
recipeId: string;
|
|
50
|
+
steps: string[];
|
|
51
|
+
}
|
|
52
|
+
export interface Policy extends Base<"policy"> {
|
|
53
|
+
policyId: string;
|
|
54
|
+
rules: string[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A source pin records a repository, a full 40-hex commit SHA and the remote
|
|
58
|
+
* ref (`refs/heads/<name>` or `refs/tags/<name>`) that the writer attests the
|
|
59
|
+
* commit was found on. `verifiedOn` is that writer's attestation. Validators
|
|
60
|
+
* check its shape only and never contact a remote, so a pin cannot prove
|
|
61
|
+
* provenance. It never proves deployment.
|
|
62
|
+
*/
|
|
63
|
+
export interface SourcePin {
|
|
64
|
+
repository: string;
|
|
65
|
+
commitSha: string;
|
|
66
|
+
verifiedOn: string;
|
|
67
|
+
}
|
|
68
|
+
export type DeployedState = "unverified" | "verified";
|
|
69
|
+
export interface Artifact extends Base<"artifact"> {
|
|
70
|
+
artifactId: string;
|
|
71
|
+
runId: string;
|
|
72
|
+
sourcePin?: SourcePin;
|
|
73
|
+
deployedState: DeployedState;
|
|
74
|
+
}
|
|
75
|
+
export type DeliveryRecord = Tenant | Run | Job | Attempt | Lease | Recipe | Policy | Artifact;
|
|
76
|
+
export type ValidationResult<T> = {
|
|
77
|
+
ok: true;
|
|
78
|
+
value: T;
|
|
79
|
+
} | {
|
|
80
|
+
ok: false;
|
|
81
|
+
errors: string[];
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Validate one record. Never throws: an unknown kind or a non-object record
|
|
85
|
+
* returns `{ ok: false, errors }`.
|
|
86
|
+
*/
|
|
87
|
+
export declare function validateRecord<K extends DeliveryKind>(kind: K, input: unknown): ValidationResult<Extract<DeliveryRecord, Base<K>>>;
|
|
88
|
+
export type DeliveryChildKind = Exclude<DeliveryKind, "run" | "tenant">;
|
|
89
|
+
export interface DeliveryBundle {
|
|
90
|
+
run: unknown;
|
|
91
|
+
children: Array<{
|
|
92
|
+
kind: DeliveryChildKind;
|
|
93
|
+
record: unknown;
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Validate a run with its children. Every child must share the run's tenant
|
|
98
|
+
* and run, a recipe child must match the run's recipeId, and run or tenant
|
|
99
|
+
* records are not allowed as children. On success the returned bundle holds
|
|
100
|
+
* the normalized records from validateRecord. Never throws: malformed input
|
|
101
|
+
* returns `{ ok: false, errors }`.
|
|
102
|
+
*/
|
|
103
|
+
export declare function validateBundle(bundle: DeliveryBundle): ValidationResult<DeliveryBundle>;
|
|
104
|
+
/**
|
|
105
|
+
* Shape check for a source pin: non-empty repository, full 40-hex commit SHA,
|
|
106
|
+
* a remote-ref-shaped `verifiedOn`, and no undeclared fields. The pin is the
|
|
107
|
+
* writer's attestation; this check cannot prove provenance.
|
|
108
|
+
*/
|
|
109
|
+
export declare function isSourcePin(v: unknown): v is SourcePin;
|
|
110
|
+
/** One implementation or predecessor artifact under lineage review. */
|
|
111
|
+
export interface LineageEntry {
|
|
112
|
+
id: string;
|
|
113
|
+
/** Present only when the writer attests the commit is on a remote ref (shape-checked, not verified live). */
|
|
114
|
+
sourcePin?: SourcePin;
|
|
115
|
+
/** Predecessor flags such as PRD passes=true. Never establish readiness. */
|
|
116
|
+
completionFlag?: boolean;
|
|
117
|
+
/** Path of an unpinned local checkout. Never establishes readiness. */
|
|
118
|
+
localCheckout?: string;
|
|
119
|
+
/** Set when the artifact cannot be located on any remote. */
|
|
120
|
+
unresolved?: {
|
|
121
|
+
reason: string;
|
|
122
|
+
nextOwner: string;
|
|
123
|
+
};
|
|
124
|
+
deployedState?: DeployedState;
|
|
125
|
+
}
|
|
126
|
+
export type Readiness = {
|
|
127
|
+
id: string;
|
|
128
|
+
status: "ready";
|
|
129
|
+
sourcePin: SourcePin;
|
|
130
|
+
deployedState: DeployedState;
|
|
131
|
+
} | {
|
|
132
|
+
id: string;
|
|
133
|
+
status: "blocked";
|
|
134
|
+
reason: string;
|
|
135
|
+
nextOwner: string;
|
|
136
|
+
deployedState: DeployedState;
|
|
137
|
+
};
|
|
138
|
+
/** Next owner recorded for any blocked entry that does not name a non-empty one. */
|
|
139
|
+
export declare const UNRESOLVED_NEXT_OWNER = "unresolved; project lead/root to identify";
|
|
140
|
+
export declare function checkReadiness(entry: LineageEntry): Readiness;
|
|
141
|
+
export {};
|
|
142
|
+
//# sourceMappingURL=contracts.d.ts.map
|