@aisac/cli 0.4.0-beta.2 → 0.4.0-beta.21
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 +115 -10
- package/dist/assets/claude/hooks/context-compaction.ts +17 -5
- package/dist/assets/claude/hooks/native-host-contract-runtime.ts +1 -1
- package/dist/assets/claude/hooks/native-host-contract.test.ts +8 -4
- package/dist/assets/claude/hooks/provider-window.d.ts +83 -0
- package/dist/assets/codex-context-compaction.mjs +243 -5
- package/dist/assets/jev-paid-pilot-terminal.mjs +6 -0
- package/dist/claude-compatibility.mjs +26 -25
- package/dist/claude-observation.mjs +2 -1
- package/dist/cli.mjs +584 -35
- package/dist/codex-compatibility.mjs +48 -2
- package/dist/codex-engineering.mjs +165 -0
- package/dist/codex-hooks.mjs +411 -0
- package/dist/codex-permissions.mjs +67 -34
- package/dist/codex-protocol-fingerprint.json +11 -11
- package/dist/engineering-init.mjs +615 -0
- package/dist/engineering-pack.mjs +259 -23
- package/dist/engineering-scan-pins.json +42 -0
- package/dist/engineering-scan-report.mjs +1274 -0
- package/dist/engineering-scan-tools.mjs +1418 -0
- package/dist/engineering-scan.mjs +1265 -0
- package/dist/extensions-catalog.mjs +373 -0
- package/dist/extensions-cli.mjs +492 -0
- package/dist/extensions-preferences.mjs +151 -0
- package/dist/guardrails/continuity.mjs +239 -0
- package/dist/guardrails/format.mjs +111 -0
- package/dist/guardrails/guards.mjs +449 -0
- package/dist/guardrails/patch.mjs +33 -0
- package/dist/guardrails/run.mjs +286 -0
- package/dist/guardrails/shell-words.mjs +206 -0
- package/dist/guardrails-plugin.mjs +207 -0
- package/dist/launch.mjs +278 -34
- package/dist/managed-host.mjs +977 -0
- package/dist/mcp-servers.mjs +192 -0
- package/dist/mcp.mjs +10 -2
- package/dist/monitor.mjs +76 -1
- package/dist/package.json +1 -1
- package/dist/platform.mjs +78 -29
- package/dist/release-manifest-public-keys.json +10 -0
- package/dist/release-manifest.json +328 -0
- package/dist/release-manifest.mjs +817 -0
- package/dist/release-settings.mjs +210 -0
- package/dist/self-update.mjs +1036 -0
- package/dist/state.mjs +25 -1
- package/dist/token-guide.mjs +132 -0
- package/dist/update-actions.mjs +442 -0
- package/package.json +1 -1
|
@@ -0,0 +1,817 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signed release manifest on the client (ADR-0056; spec aisac-release-manifest
|
|
3
|
+
* FR1, FR2, FR4, FR8; wave R2). One document, signed offline by the owner, names
|
|
4
|
+
* the pinned and qualified host versions with their npm lock sets and the current
|
|
5
|
+
* client version per channel. This module fetches it at most every six hours,
|
|
6
|
+
* verifies it against the public keys pinned in the package, keeps the highest
|
|
7
|
+
* verified sequence as the anti-rollback mark, caches the exact bytes under the
|
|
8
|
+
* state directory and falls back to the copy bundled with the client. In R2 the
|
|
9
|
+
* manifest changes no behaviour: it is read by `aisac doctor` and `aisac status`,
|
|
10
|
+
* and its bundled copy is the one source of the qualified host version lists.
|
|
11
|
+
*
|
|
12
|
+
* Verification order (FR2): size, JSON, kind, schema version, signature format,
|
|
13
|
+
* pinned key id, canonical JSON and Ed25519, sequence against the high-water
|
|
14
|
+
* mark, field formats, then the offline invariants. Any refusal keeps the
|
|
15
|
+
* previous verified manifest; nothing from a refused body is printed.
|
|
16
|
+
*/
|
|
17
|
+
import { constants } from 'node:fs';
|
|
18
|
+
import { lstat, open, readFile, stat } from 'node:fs/promises';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
import { verify } from 'node:crypto';
|
|
21
|
+
import { canonicalJson, loadPinnedKeys } from './engineering-pack.mjs';
|
|
22
|
+
import { isTimeout, readBoundedText, unavailableReasonFor } from './engineering.mjs';
|
|
23
|
+
import { atomicBytes, atomicJson, ensurePrivateDirectory } from './state.mjs';
|
|
24
|
+
|
|
25
|
+
export const MANIFEST_PATH = '/release/v1/manifest';
|
|
26
|
+
export const MANIFEST_KIND = 'aisac-release-manifest';
|
|
27
|
+
export const MANIFEST_SCHEMA_VERSION = 1;
|
|
28
|
+
/** Bundled copies, next to `cli.mjs` in the installed package. */
|
|
29
|
+
export const BUNDLED_MANIFEST_FILE = 'release-manifest.json';
|
|
30
|
+
export const MANIFEST_PUBLIC_KEYS_FILE = 'release-manifest-public-keys.json';
|
|
31
|
+
/** Cache under the state directory: `<state>/release/`. */
|
|
32
|
+
export const RELEASE_STATE_DIRECTORY = 'release';
|
|
33
|
+
export const CACHED_MANIFEST_FILE = 'manifest.json';
|
|
34
|
+
export const MANIFEST_META_FILE = 'manifest.meta.json';
|
|
35
|
+
export const MAX_MANIFEST_BYTES = 64 * 1024;
|
|
36
|
+
export const MANIFEST_FETCH_INTERVAL_MS = 6 * 60 * 60 * 1000;
|
|
37
|
+
export const MANIFEST_FETCH_TIMEOUT_MS = 3_000;
|
|
38
|
+
/** Clock-skew tolerance when judging `expires_at` (FR4). */
|
|
39
|
+
export const MANIFEST_EXPIRY_TOLERANCE_MS = 24 * 60 * 60 * 1000;
|
|
40
|
+
export const MAX_MANIFEST_LIFETIME_MS = 90 * 24 * 60 * 60 * 1000;
|
|
41
|
+
/** The only package names the manifest may name (D3). */
|
|
42
|
+
export const CLIENT_PACKAGES = Object.freeze(['@aisac/cli', '@brunobracaioli/aibrain']);
|
|
43
|
+
export const HOST_PACKAGES = Object.freeze({
|
|
44
|
+
claude: '@anthropic-ai/claude-code',
|
|
45
|
+
codex: '@openai/codex',
|
|
46
|
+
});
|
|
47
|
+
/** @type {readonly ('claude' | 'codex')[]} */
|
|
48
|
+
export const HOSTS = Object.freeze(['claude', 'codex']);
|
|
49
|
+
export const PLATFORMS = Object.freeze(['linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64']);
|
|
50
|
+
export const CHANNELS = Object.freeze(['beta', 'latest']);
|
|
51
|
+
/** Lock sets are kept for the pinned version and the one before it (FR1). */
|
|
52
|
+
export const MAX_VERSIONS_WITH_LOCKS = 2;
|
|
53
|
+
export const CLIENT_VERSION_HEADER = 'x-aisac-client';
|
|
54
|
+
export const CLIENT_HOST_HEADER = 'x-aisac-host';
|
|
55
|
+
const MAX_META_BYTES = 4 * 1024;
|
|
56
|
+
const MAX_PACKAGE_JSON_BYTES = 64 * 1024;
|
|
57
|
+
const TOP_LEVEL_KEYS = Object.freeze([
|
|
58
|
+
'kind',
|
|
59
|
+
'schema_version',
|
|
60
|
+
'sequence',
|
|
61
|
+
'issued_at',
|
|
62
|
+
'expires_at',
|
|
63
|
+
'key_id',
|
|
64
|
+
'features',
|
|
65
|
+
'client',
|
|
66
|
+
'hosts',
|
|
67
|
+
'signature',
|
|
68
|
+
]);
|
|
69
|
+
const HOST_KEYS = ['package', 'pinned', 'qualified', 'minimum_client', 'versions'];
|
|
70
|
+
const LOCK_ENTRY_KEYS = ['name', 'version', 'resolved', 'integrity'];
|
|
71
|
+
const REGISTRY_TARBALL = /^https:\/\/registry\.npmjs\.org\/[A-Za-z0-9@%._/-]+\.tgz$/u;
|
|
72
|
+
const SRI_SHA512 = /^sha512-[A-Za-z0-9+/]{86}==$/u;
|
|
73
|
+
const SIGNATURE_BASE64 = /^[A-Za-z0-9+/]{86}==$/u;
|
|
74
|
+
const KEY_ID = /^[0-9a-f]{16}$/u;
|
|
75
|
+
const ISO_INSTANT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/u;
|
|
76
|
+
export const SEMVER =
|
|
77
|
+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?$/u;
|
|
78
|
+
const SAFE_DETAIL = /^[A-Za-z0-9@/._:-]{1,120}$/u;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @typedef {{ version: string, resolved: string, integrity: string, name?: string }} LockEntry
|
|
82
|
+
* @typedef {{ ignore_scripts: boolean, platforms: Record<string, { lock: Record<string, LockEntry> }> }} HostVersion
|
|
83
|
+
* @typedef {{ package: string, pinned: string, qualified: string[], minimum_client: Record<string, string>, versions: Record<string, HostVersion> }} HostEntry
|
|
84
|
+
* @typedef {{ version: string, integrity: Record<string, string> }} Channel
|
|
85
|
+
* @typedef {{
|
|
86
|
+
* kind: 'aisac-release-manifest', schema_version: 1, sequence: number, issued_at: string,
|
|
87
|
+
* expires_at: string, key_id: string,
|
|
88
|
+
* features: { self_update: boolean, managed_hosts: { claude: boolean, codex: boolean } },
|
|
89
|
+
* client: { packages: string[], minimum_version: string, channels: Record<string, Channel> },
|
|
90
|
+
* hosts: { claude: HostEntry, codex: HostEntry }, signature: string
|
|
91
|
+
* }} ReleaseManifest
|
|
92
|
+
* @typedef {{ ok: true, manifest: ReleaseManifest, sequence: number, keyId: string,
|
|
93
|
+
* issuedAt: string, expiresAt: string, expired: boolean }} Verified
|
|
94
|
+
* @typedef {{ ok: false, reason: 'manifest_invalid' | 'client_outdated' | 'manifest_replayed',
|
|
95
|
+
* detail: string }} Refused
|
|
96
|
+
* @typedef {{ code: string, detail: string }} Violation
|
|
97
|
+
* @typedef {(code: string, detail?: string) => void} Fail
|
|
98
|
+
* @typedef {'server' | 'cache' | 'bundled' | 'none'} ManifestSource
|
|
99
|
+
* @typedef {{ at: string, outcome: 'verified' | 'unavailable' | 'rejected', reason: string | null }} LastFetch
|
|
100
|
+
* @typedef {{ schemaVersion: 1, sequence: number, lastFetch: LastFetch | null }} ManifestMeta
|
|
101
|
+
* @typedef {{
|
|
102
|
+
* manifest: ReleaseManifest | null, source: ManifestSource,
|
|
103
|
+
* state: 'ok' | 'expired' | 'replayed_rejected' | 'invalid' | 'none',
|
|
104
|
+
* reason: string | null, sequence: number | null, keyId: string | null,
|
|
105
|
+
* issuedAt: string | null, expiresAt: string | null, lastFetch: LastFetch | null
|
|
106
|
+
* }} ResolvedManifest
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
110
|
+
function isRecord(value) {
|
|
111
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @param {unknown} value @returns {value is string} */
|
|
115
|
+
function isSemver(value) {
|
|
116
|
+
return typeof value === 'string' && value.length <= 64 && SEMVER.test(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Echo only identifiers the invariants already validated; anything else is masked.
|
|
121
|
+
* @param {...string} parts
|
|
122
|
+
*/
|
|
123
|
+
function detailOf(...parts) {
|
|
124
|
+
return parts.map((part) => (SAFE_DETAIL.test(part) ? part : 'invalid')).join(':');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Semantic version order with prerelease rules (numeric identifiers compare as
|
|
129
|
+
* numbers, a release is above its prereleases). Build metadata is not accepted.
|
|
130
|
+
* @param {string} a @param {string} b
|
|
131
|
+
*/
|
|
132
|
+
export function compareVersions(a, b) {
|
|
133
|
+
const left = SEMVER.exec(a);
|
|
134
|
+
const right = SEMVER.exec(b);
|
|
135
|
+
if (left === null || right === null) throw new Error('semver_invalid');
|
|
136
|
+
for (let index = 1; index <= 3; index += 1) {
|
|
137
|
+
const difference = Number(left[index]) - Number(right[index]);
|
|
138
|
+
if (difference !== 0) return difference < 0 ? -1 : 1;
|
|
139
|
+
}
|
|
140
|
+
const leftPre = left[4];
|
|
141
|
+
const rightPre = right[4];
|
|
142
|
+
if (leftPre === undefined && rightPre === undefined) return 0;
|
|
143
|
+
if (leftPre === undefined) return 1;
|
|
144
|
+
if (rightPre === undefined) return -1;
|
|
145
|
+
const leftIds = leftPre.split('.');
|
|
146
|
+
const rightIds = rightPre.split('.');
|
|
147
|
+
for (let index = 0; index < Math.max(leftIds.length, rightIds.length); index += 1) {
|
|
148
|
+
const l = leftIds[index];
|
|
149
|
+
const r = rightIds[index];
|
|
150
|
+
if (l === undefined) return -1;
|
|
151
|
+
if (r === undefined) return 1;
|
|
152
|
+
const lNumeric = /^\d+$/u.test(l);
|
|
153
|
+
const rNumeric = /^\d+$/u.test(r);
|
|
154
|
+
if (lNumeric && rNumeric) {
|
|
155
|
+
const difference = Number(l) - Number(r);
|
|
156
|
+
if (difference !== 0) return difference < 0 ? -1 : 1;
|
|
157
|
+
} else if (lNumeric !== rNumeric) {
|
|
158
|
+
return lNumeric ? -1 : 1;
|
|
159
|
+
} else if (l !== r) {
|
|
160
|
+
return l < r ? -1 : 1;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The offline invariants of FR1 over an already parsed document. `strictFields`
|
|
168
|
+
* refuses unknown fields (the gate); the client tolerates them, signed.
|
|
169
|
+
* @param {unknown} value
|
|
170
|
+
* @param {{ strictFields?: boolean }} [options]
|
|
171
|
+
* @returns {Violation[]}
|
|
172
|
+
*/
|
|
173
|
+
export function validateManifestInvariants(value, options = {}) {
|
|
174
|
+
const strict = options.strictFields === true;
|
|
175
|
+
/** @type {Violation[]} */
|
|
176
|
+
const violations = [];
|
|
177
|
+
/** @type {Fail} */
|
|
178
|
+
const fail = (code, detail = '') => {
|
|
179
|
+
violations.push({ code, detail });
|
|
180
|
+
};
|
|
181
|
+
if (!isRecord(value)) return [{ code: 'shape', detail: '' }];
|
|
182
|
+
if (strict) {
|
|
183
|
+
for (const key of Object.keys(value)) {
|
|
184
|
+
if (!TOP_LEVEL_KEYS.includes(key)) fail('unknown_field', detailOf(key));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (value.kind !== MANIFEST_KIND) fail('kind');
|
|
188
|
+
if (value.schema_version !== MANIFEST_SCHEMA_VERSION) fail('schema_version');
|
|
189
|
+
if (
|
|
190
|
+
typeof value.sequence !== 'number' ||
|
|
191
|
+
!Number.isSafeInteger(value.sequence) ||
|
|
192
|
+
value.sequence < 1
|
|
193
|
+
)
|
|
194
|
+
fail('sequence');
|
|
195
|
+
const issuedAt = value.issued_at;
|
|
196
|
+
const expiresAt = value.expires_at;
|
|
197
|
+
const issued = typeof issuedAt === 'string' && ISO_INSTANT.test(issuedAt);
|
|
198
|
+
const expires = typeof expiresAt === 'string' && ISO_INSTANT.test(expiresAt);
|
|
199
|
+
if (!issued) fail('issued_at');
|
|
200
|
+
if (!expires) fail('expires_at');
|
|
201
|
+
if (typeof issuedAt === 'string' && typeof expiresAt === 'string' && issued && expires) {
|
|
202
|
+
const lifetime = Date.parse(expiresAt) - Date.parse(issuedAt);
|
|
203
|
+
if (!(lifetime > 0) || lifetime > MAX_MANIFEST_LIFETIME_MS) fail('lifetime');
|
|
204
|
+
}
|
|
205
|
+
if (typeof value.key_id !== 'string' || !KEY_ID.test(value.key_id)) fail('key_id');
|
|
206
|
+
if (
|
|
207
|
+
value.signature !== undefined &&
|
|
208
|
+
(typeof value.signature !== 'string' || !SIGNATURE_BASE64.test(value.signature))
|
|
209
|
+
)
|
|
210
|
+
fail('signature_format');
|
|
211
|
+
validateFeatures(value.features, fail, strict);
|
|
212
|
+
validateClient(value.client, fail, strict);
|
|
213
|
+
validateHosts(value.hosts, fail, strict);
|
|
214
|
+
return violations;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** @param {unknown} features @param {Fail} fail @param {boolean} strict */
|
|
218
|
+
function validateFeatures(features, fail, strict) {
|
|
219
|
+
if (
|
|
220
|
+
!isRecord(features) ||
|
|
221
|
+
typeof features.self_update !== 'boolean' ||
|
|
222
|
+
!isRecord(features.managed_hosts)
|
|
223
|
+
) {
|
|
224
|
+
fail('features');
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const managed = features.managed_hosts;
|
|
228
|
+
for (const host of HOSTS) {
|
|
229
|
+
if (typeof managed[host] !== 'boolean') fail('features', detailOf('managed_hosts', host));
|
|
230
|
+
}
|
|
231
|
+
if (strict) {
|
|
232
|
+
if (Object.keys(features).some((key) => key !== 'self_update' && key !== 'managed_hosts'))
|
|
233
|
+
fail('unknown_field', 'features');
|
|
234
|
+
if (Object.keys(managed).some((key) => key !== 'claude' && key !== 'codex'))
|
|
235
|
+
fail('unknown_field', 'features.managed_hosts');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** @param {unknown} client @param {Fail} fail @param {boolean} strict */
|
|
240
|
+
function validateClient(client, fail, strict) {
|
|
241
|
+
if (!isRecord(client)) {
|
|
242
|
+
fail('client');
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (
|
|
246
|
+
strict &&
|
|
247
|
+
Object.keys(client).some((key) => !['packages', 'minimum_version', 'channels'].includes(key))
|
|
248
|
+
)
|
|
249
|
+
fail('unknown_field', 'client');
|
|
250
|
+
const packages = client.packages;
|
|
251
|
+
if (
|
|
252
|
+
!Array.isArray(packages) ||
|
|
253
|
+
packages.length !== CLIENT_PACKAGES.length ||
|
|
254
|
+
packages.some((entry, index) => entry !== CLIENT_PACKAGES[index])
|
|
255
|
+
)
|
|
256
|
+
fail('client_packages');
|
|
257
|
+
const minimum = client.minimum_version;
|
|
258
|
+
if (!isSemver(minimum)) fail('client_minimum_version');
|
|
259
|
+
if (!isRecord(client.channels)) {
|
|
260
|
+
fail('client_channels');
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
for (const [name, channel] of Object.entries(client.channels)) {
|
|
264
|
+
if (!CHANNELS.includes(name)) {
|
|
265
|
+
fail('channel', detailOf(name));
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (!isRecord(channel) || !isSemver(channel.version) || !isRecord(channel.integrity)) {
|
|
269
|
+
fail('channel', name);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (strict && Object.keys(channel).some((key) => key !== 'version' && key !== 'integrity'))
|
|
273
|
+
fail('unknown_field', detailOf('channels', name));
|
|
274
|
+
for (const pkg of CLIENT_PACKAGES) {
|
|
275
|
+
const integrity = channel.integrity[pkg];
|
|
276
|
+
if (typeof integrity !== 'string' || !SRI_SHA512.test(integrity))
|
|
277
|
+
fail('channel_integrity', detailOf(name, pkg));
|
|
278
|
+
}
|
|
279
|
+
if (Object.keys(channel.integrity).some((key) => !CLIENT_PACKAGES.includes(key)))
|
|
280
|
+
fail('channel_integrity', detailOf(name, 'unknown_package'));
|
|
281
|
+
if (isSemver(minimum) && compareVersions(minimum, channel.version) > 0)
|
|
282
|
+
fail('minimum_above_channel', name);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** @param {unknown} hosts @param {Fail} fail @param {boolean} strict */
|
|
287
|
+
function validateHosts(hosts, fail, strict) {
|
|
288
|
+
if (!isRecord(hosts)) {
|
|
289
|
+
fail('hosts');
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (strict && Object.keys(hosts).some((key) => key !== 'claude' && key !== 'codex'))
|
|
293
|
+
fail('unknown_field', 'hosts');
|
|
294
|
+
for (const host of HOSTS) {
|
|
295
|
+
const entry = hosts[host];
|
|
296
|
+
if (!isRecord(entry)) {
|
|
297
|
+
fail('host', host);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (strict && Object.keys(entry).some((key) => !HOST_KEYS.includes(key)))
|
|
301
|
+
fail('unknown_field', detailOf('hosts', host));
|
|
302
|
+
if (entry.package !== HOST_PACKAGES[host]) fail('host_package', host);
|
|
303
|
+
const qualified = entry.qualified;
|
|
304
|
+
/** @type {string[]} */
|
|
305
|
+
const qualifiedList =
|
|
306
|
+
Array.isArray(qualified) && qualified.every(isSemver)
|
|
307
|
+
? /** @type {string[]} */ (qualified)
|
|
308
|
+
: [];
|
|
309
|
+
const qualifiedOk =
|
|
310
|
+
qualifiedList.length > 0 &&
|
|
311
|
+
Array.isArray(qualified) &&
|
|
312
|
+
qualifiedList.length === qualified.length &&
|
|
313
|
+
qualifiedList.every(
|
|
314
|
+
(version, index) =>
|
|
315
|
+
index === 0 ||
|
|
316
|
+
compareVersions(/** @type {string} */ (qualifiedList[index - 1]), version) < 0,
|
|
317
|
+
);
|
|
318
|
+
if (!qualifiedOk) fail('host_qualified', host);
|
|
319
|
+
/** @param {unknown} version */
|
|
320
|
+
const inQualified = (version) =>
|
|
321
|
+
qualifiedOk && typeof version === 'string' && qualifiedList.includes(version);
|
|
322
|
+
if (!isSemver(entry.pinned) || !inQualified(entry.pinned)) fail('host_pinned', host);
|
|
323
|
+
if (!isRecord(entry.minimum_client)) fail('host_minimum_client', host);
|
|
324
|
+
else {
|
|
325
|
+
for (const [version, client] of Object.entries(entry.minimum_client)) {
|
|
326
|
+
if (!inQualified(version) || !isSemver(client))
|
|
327
|
+
fail('host_minimum_client', detailOf(host, version));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (!isRecord(entry.versions)) {
|
|
331
|
+
fail('host_versions', host);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
const versions = Object.keys(entry.versions);
|
|
335
|
+
if (versions.length > MAX_VERSIONS_WITH_LOCKS) fail('host_versions_count', host);
|
|
336
|
+
if (typeof entry.pinned === 'string' && !versions.includes(entry.pinned))
|
|
337
|
+
fail('host_pinned_lock', host);
|
|
338
|
+
for (const [version, detail] of Object.entries(entry.versions)) {
|
|
339
|
+
if (!inQualified(version)) fail('host_version_unqualified', detailOf(host, version));
|
|
340
|
+
validateHostVersion(host, version, detail, fail, strict);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* @param {'claude'|'codex'} host @param {string} version @param {unknown} detail
|
|
347
|
+
* @param {Fail} fail @param {boolean} strict
|
|
348
|
+
*/
|
|
349
|
+
function validateHostVersion(host, version, detail, fail, strict) {
|
|
350
|
+
const where = detailOf(host, version);
|
|
351
|
+
if (
|
|
352
|
+
!isRecord(detail) ||
|
|
353
|
+
typeof detail.ignore_scripts !== 'boolean' ||
|
|
354
|
+
!isRecord(detail.platforms)
|
|
355
|
+
) {
|
|
356
|
+
fail('host_version', where);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (strict && Object.keys(detail).some((key) => key !== 'ignore_scripts' && key !== 'platforms'))
|
|
360
|
+
fail('unknown_field', where);
|
|
361
|
+
const platforms = Object.keys(detail.platforms);
|
|
362
|
+
if (
|
|
363
|
+
platforms.length !== PLATFORMS.length ||
|
|
364
|
+
PLATFORMS.some((platform) => !platforms.includes(platform))
|
|
365
|
+
) {
|
|
366
|
+
fail('host_platforms', where);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const hostPackage = HOST_PACKAGES[host];
|
|
370
|
+
for (const platform of PLATFORMS) {
|
|
371
|
+
const set = detail.platforms[platform];
|
|
372
|
+
if (
|
|
373
|
+
!isRecord(set) ||
|
|
374
|
+
!isRecord(set.lock) ||
|
|
375
|
+
(strict && Object.keys(set).some((key) => key !== 'lock'))
|
|
376
|
+
) {
|
|
377
|
+
fail('lock_set', detailOf(host, version, platform));
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
const lock = set.lock;
|
|
381
|
+
const keys = Object.keys(lock);
|
|
382
|
+
const platformPackage = `${hostPackage}-${platform}`;
|
|
383
|
+
if (keys.length !== 2 || !keys.includes(hostPackage) || !keys.includes(platformPackage)) {
|
|
384
|
+
fail('lock_keys', detailOf(host, version, platform));
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
for (const key of keys)
|
|
388
|
+
validateLockEntry(host, version, platform, key, lock[key], fail, strict);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* @param {'claude'|'codex'} host @param {string} version @param {string} platform @param {string} key
|
|
394
|
+
* @param {unknown} entry @param {Fail} fail @param {boolean} strict
|
|
395
|
+
*/
|
|
396
|
+
function validateLockEntry(host, version, platform, key, entry, fail, strict) {
|
|
397
|
+
const where = detailOf(host, version, platform, key);
|
|
398
|
+
if (!isRecord(entry)) {
|
|
399
|
+
fail('lock_entry', where);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (strict && Object.keys(entry).some((k) => !LOCK_ENTRY_KEYS.includes(k)))
|
|
403
|
+
fail('unknown_field', where);
|
|
404
|
+
if (!isSemver(entry.version)) fail('lock_entry', detailOf(where, 'version'));
|
|
405
|
+
if (typeof entry.resolved !== 'string' || !REGISTRY_TARBALL.test(entry.resolved))
|
|
406
|
+
fail('lock_entry', detailOf(where, 'resolved'));
|
|
407
|
+
if (typeof entry.integrity !== 'string' || !SRI_SHA512.test(entry.integrity))
|
|
408
|
+
fail('lock_entry', detailOf(where, 'integrity'));
|
|
409
|
+
const hostPackage = HOST_PACKAGES[host];
|
|
410
|
+
// An aliased platform package (Codex) names the real package it resolves to.
|
|
411
|
+
if (entry.name !== undefined && entry.name !== hostPackage && entry.name !== key)
|
|
412
|
+
fail('lock_entry', detailOf(where, 'name'));
|
|
413
|
+
if (key === hostPackage && entry.version !== version)
|
|
414
|
+
fail('lock_entry', detailOf(where, 'version'));
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Verify one manifest document against the pinned keys (FR2 order).
|
|
419
|
+
* @param {Uint8Array | string} input
|
|
420
|
+
* @param {{ pinnedKeys: import('./engineering-pack.mjs').PinnedKey[], now?: Date, highWater?: number, strictFields?: boolean }} options
|
|
421
|
+
* @returns {Verified | Refused}
|
|
422
|
+
*/
|
|
423
|
+
export function verifyManifest(input, options) {
|
|
424
|
+
const { pinnedKeys, now = new Date(), highWater = 0, strictFields = false } = options;
|
|
425
|
+
const bytes = typeof input === 'string' ? Buffer.from(input, 'utf8') : Buffer.from(input);
|
|
426
|
+
if (bytes.length > MAX_MANIFEST_BYTES)
|
|
427
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'size' };
|
|
428
|
+
/** @type {unknown} */
|
|
429
|
+
let parsed;
|
|
430
|
+
try {
|
|
431
|
+
parsed = JSON.parse(bytes.toString('utf8'));
|
|
432
|
+
} catch {
|
|
433
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'json' };
|
|
434
|
+
}
|
|
435
|
+
if (!isRecord(parsed)) return { ok: false, reason: 'manifest_invalid', detail: 'shape' };
|
|
436
|
+
if (parsed.kind !== MANIFEST_KIND)
|
|
437
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'kind' };
|
|
438
|
+
if (parsed.schema_version !== MANIFEST_SCHEMA_VERSION) {
|
|
439
|
+
const higher =
|
|
440
|
+
typeof parsed.schema_version === 'number' &&
|
|
441
|
+
Number.isSafeInteger(parsed.schema_version) &&
|
|
442
|
+
parsed.schema_version > MANIFEST_SCHEMA_VERSION;
|
|
443
|
+
return higher
|
|
444
|
+
? { ok: false, reason: 'client_outdated', detail: 'schema_version' }
|
|
445
|
+
: { ok: false, reason: 'manifest_invalid', detail: 'schema_version' };
|
|
446
|
+
}
|
|
447
|
+
const { signature, ...unsigned } = parsed;
|
|
448
|
+
if (typeof signature !== 'string' || !SIGNATURE_BASE64.test(signature))
|
|
449
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'signature_format' };
|
|
450
|
+
const keyId = parsed.key_id;
|
|
451
|
+
if (typeof keyId !== 'string' || !KEY_ID.test(keyId))
|
|
452
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'key_id' };
|
|
453
|
+
const pinned = pinnedKeys.find((entry) => entry.keyId === keyId);
|
|
454
|
+
if (pinned === undefined) return { ok: false, reason: 'manifest_invalid', detail: 'unknown_key' };
|
|
455
|
+
let message;
|
|
456
|
+
try {
|
|
457
|
+
message = canonicalJson(unsigned);
|
|
458
|
+
} catch {
|
|
459
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'canonical' };
|
|
460
|
+
}
|
|
461
|
+
if (!verify(null, Buffer.from(message, 'utf8'), pinned.key, Buffer.from(signature, 'base64')))
|
|
462
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'bad_signature' };
|
|
463
|
+
const sequence = parsed.sequence;
|
|
464
|
+
if (typeof sequence !== 'number' || !Number.isSafeInteger(sequence) || sequence < 1)
|
|
465
|
+
return { ok: false, reason: 'manifest_invalid', detail: 'sequence' };
|
|
466
|
+
if (sequence < highWater) return { ok: false, reason: 'manifest_replayed', detail: 'sequence' };
|
|
467
|
+
const violations = validateManifestInvariants(parsed, { strictFields });
|
|
468
|
+
const first = violations[0];
|
|
469
|
+
if (first !== undefined)
|
|
470
|
+
return {
|
|
471
|
+
ok: false,
|
|
472
|
+
reason: 'manifest_invalid',
|
|
473
|
+
detail: first.detail === '' ? first.code : `${first.code}:${first.detail}`,
|
|
474
|
+
};
|
|
475
|
+
const manifest = /** @type {ReleaseManifest} */ (/** @type {unknown} */ (parsed));
|
|
476
|
+
return {
|
|
477
|
+
ok: true,
|
|
478
|
+
manifest,
|
|
479
|
+
sequence,
|
|
480
|
+
keyId,
|
|
481
|
+
issuedAt: manifest.issued_at,
|
|
482
|
+
expiresAt: manifest.expires_at,
|
|
483
|
+
expired: now.getTime() > Date.parse(manifest.expires_at) + MANIFEST_EXPIRY_TOLERANCE_MS,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* The pinned release keys shipped with the client.
|
|
489
|
+
* @param {string} packageRoot
|
|
490
|
+
*/
|
|
491
|
+
export function loadManifestPinnedKeys(packageRoot) {
|
|
492
|
+
return loadPinnedKeys(packageRoot, MANIFEST_PUBLIC_KEYS_FILE);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* The manifest bundled with this client release: the floor every launch has.
|
|
497
|
+
* It ships inside the installed package (possibly root-owned), so only the
|
|
498
|
+
* size is bounded, as for the pinned keys.
|
|
499
|
+
* @param {string} packageRoot
|
|
500
|
+
* @returns {Promise<Buffer>}
|
|
501
|
+
*/
|
|
502
|
+
export async function readBundledManifest(packageRoot) {
|
|
503
|
+
const path = join(packageRoot, BUNDLED_MANIFEST_FILE);
|
|
504
|
+
const entry = await stat(path);
|
|
505
|
+
if (!entry.isFile() || entry.size > MAX_MANIFEST_BYTES)
|
|
506
|
+
throw new Error('bundled_manifest_invalid');
|
|
507
|
+
return readFile(path);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* The installed client's version, read from the package manifest next to the
|
|
512
|
+
* code; null when it cannot be read (the header is then not sent).
|
|
513
|
+
* @param {string} packageRoot
|
|
514
|
+
* @returns {Promise<string | null>}
|
|
515
|
+
*/
|
|
516
|
+
export async function installedClientVersion(packageRoot) {
|
|
517
|
+
try {
|
|
518
|
+
const path = join(packageRoot, 'package.json');
|
|
519
|
+
const entry = await stat(path);
|
|
520
|
+
if (!entry.isFile() || entry.size > MAX_PACKAGE_JSON_BYTES) return null;
|
|
521
|
+
/** @type {unknown} */
|
|
522
|
+
const parsed = JSON.parse(await readFile(path, 'utf8'));
|
|
523
|
+
return isRecord(parsed) && isSemver(parsed.version) ? parsed.version : null;
|
|
524
|
+
} catch {
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Read a private state file of bounded size: a regular, owned file, opened
|
|
531
|
+
* without following symlinks. Null when missing or not ours.
|
|
532
|
+
* @param {string} path @param {number} maxBytes
|
|
533
|
+
* @returns {Promise<Buffer | null>}
|
|
534
|
+
*/
|
|
535
|
+
async function readOwnedBytes(path, maxBytes) {
|
|
536
|
+
/** @type {import('node:fs/promises').FileHandle | undefined} */
|
|
537
|
+
let handle;
|
|
538
|
+
try {
|
|
539
|
+
const link = await lstat(path);
|
|
540
|
+
if (!link.isFile() || link.size > maxBytes) return null;
|
|
541
|
+
if (typeof process.getuid === 'function' && link.uid !== process.getuid()) return null;
|
|
542
|
+
handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
543
|
+
return await handle.readFile();
|
|
544
|
+
} catch {
|
|
545
|
+
return null;
|
|
546
|
+
} finally {
|
|
547
|
+
await handle?.close().catch(() => undefined);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** @param {string} root @returns {Promise<ManifestMeta>} */
|
|
552
|
+
export async function readManifestMeta(root) {
|
|
553
|
+
const bytes = await readOwnedBytes(
|
|
554
|
+
join(root, RELEASE_STATE_DIRECTORY, MANIFEST_META_FILE),
|
|
555
|
+
MAX_META_BYTES,
|
|
556
|
+
);
|
|
557
|
+
/** @type {ManifestMeta} */
|
|
558
|
+
const empty = { schemaVersion: 1, sequence: 0, lastFetch: null };
|
|
559
|
+
if (bytes === null) return empty;
|
|
560
|
+
/** @type {unknown} */
|
|
561
|
+
let value;
|
|
562
|
+
try {
|
|
563
|
+
value = JSON.parse(bytes.toString('utf8'));
|
|
564
|
+
} catch {
|
|
565
|
+
return empty;
|
|
566
|
+
}
|
|
567
|
+
if (!isRecord(value) || value.schemaVersion !== 1) return empty;
|
|
568
|
+
const sequence =
|
|
569
|
+
typeof value.sequence === 'number' &&
|
|
570
|
+
Number.isSafeInteger(value.sequence) &&
|
|
571
|
+
value.sequence >= 0
|
|
572
|
+
? value.sequence
|
|
573
|
+
: 0;
|
|
574
|
+
const lastFetch = isLastFetch(value.lastFetch) ? value.lastFetch : null;
|
|
575
|
+
return { schemaVersion: 1, sequence, lastFetch };
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** @param {unknown} value @returns {value is LastFetch} */
|
|
579
|
+
function isLastFetch(value) {
|
|
580
|
+
return (
|
|
581
|
+
isRecord(value) &&
|
|
582
|
+
typeof value.at === 'string' &&
|
|
583
|
+
ISO_INSTANT.test(value.at) &&
|
|
584
|
+
(value.outcome === 'verified' ||
|
|
585
|
+
value.outcome === 'unavailable' ||
|
|
586
|
+
value.outcome === 'rejected') &&
|
|
587
|
+
(value.reason === null || (typeof value.reason === 'string' && SAFE_DETAIL.test(value.reason)))
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Fetch the manifest once: bearer PAT, bounded body, no redirects, short
|
|
593
|
+
* timeout. The two optional headers tell the server which client and host ask.
|
|
594
|
+
* @param {{ endpoint: string, token: string, clientVersion?: string | null, host?: 'claude' | 'codex' | null,
|
|
595
|
+
* fetchImpl?: typeof fetch, timeoutMs?: number }} input
|
|
596
|
+
* @returns {Promise<{ outcome: 'received', bytes: Buffer } | { outcome: 'unavailable', reason: string }>}
|
|
597
|
+
*/
|
|
598
|
+
export async function fetchManifest(input) {
|
|
599
|
+
const { fetchImpl = fetch, timeoutMs = MANIFEST_FETCH_TIMEOUT_MS } = input;
|
|
600
|
+
let url;
|
|
601
|
+
try {
|
|
602
|
+
url = new URL(MANIFEST_PATH, input.endpoint).toString();
|
|
603
|
+
} catch {
|
|
604
|
+
return { outcome: 'unavailable', reason: 'invalid_endpoint' };
|
|
605
|
+
}
|
|
606
|
+
/** @type {Record<string, string>} */
|
|
607
|
+
const headers = { Authorization: `Bearer ${input.token}`, Accept: 'application/json' };
|
|
608
|
+
if (isSemver(input.clientVersion)) headers[CLIENT_VERSION_HEADER] = input.clientVersion;
|
|
609
|
+
if (input.host === 'claude' || input.host === 'codex') headers[CLIENT_HOST_HEADER] = input.host;
|
|
610
|
+
let response;
|
|
611
|
+
try {
|
|
612
|
+
response = await fetchImpl(url, {
|
|
613
|
+
method: 'GET',
|
|
614
|
+
redirect: 'error',
|
|
615
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
616
|
+
headers,
|
|
617
|
+
});
|
|
618
|
+
} catch (error) {
|
|
619
|
+
return { outcome: 'unavailable', reason: isTimeout(error) ? 'timeout' : 'unreachable' };
|
|
620
|
+
}
|
|
621
|
+
let text;
|
|
622
|
+
try {
|
|
623
|
+
text = await readBoundedText(response, MAX_MANIFEST_BYTES);
|
|
624
|
+
} catch (error) {
|
|
625
|
+
return { outcome: 'unavailable', reason: isTimeout(error) ? 'timeout' : 'invalid_response' };
|
|
626
|
+
}
|
|
627
|
+
if (response.status === 200) return { outcome: 'received', bytes: Buffer.from(text, 'utf8') };
|
|
628
|
+
if (response.status === 503) {
|
|
629
|
+
let code = null;
|
|
630
|
+
try {
|
|
631
|
+
/** @type {unknown} */
|
|
632
|
+
const parsed = JSON.parse(text);
|
|
633
|
+
code = isRecord(parsed) && typeof parsed.error === 'string' ? parsed.error : null;
|
|
634
|
+
} catch {
|
|
635
|
+
code = null;
|
|
636
|
+
}
|
|
637
|
+
return {
|
|
638
|
+
outcome: 'unavailable',
|
|
639
|
+
reason: code === 'manifest_unavailable' ? 'manifest_unavailable' : 'service_unavailable',
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
return { outcome: 'unavailable', reason: unavailableReasonFor(response.status) };
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* Resolve the manifest this run uses (FR4 precedence): a freshly verified
|
|
647
|
+
* server document with a sequence at or above the high-water mark, else the
|
|
648
|
+
* cached verified document, else the bundled one. A fetch happens at most once
|
|
649
|
+
* per interval, only with an endpoint and a token, and never throws: every
|
|
650
|
+
* failure is recorded in the meta file and the previous manifest stands.
|
|
651
|
+
* @param {{ root: string, packageRoot: string, endpoint?: string | null, token?: string | null,
|
|
652
|
+
* host?: 'claude' | 'codex' | null, now?: () => Date, fetchImpl?: typeof fetch,
|
|
653
|
+
* timeoutMs?: number, force?: boolean, fetchIntervalMs?: number }} input
|
|
654
|
+
* @returns {Promise<ResolvedManifest>}
|
|
655
|
+
*/
|
|
656
|
+
export async function resolveReleaseManifest(input) {
|
|
657
|
+
const now = input.now ?? (() => new Date());
|
|
658
|
+
const interval = input.fetchIntervalMs ?? MANIFEST_FETCH_INTERVAL_MS;
|
|
659
|
+
let pinnedKeys;
|
|
660
|
+
try {
|
|
661
|
+
pinnedKeys = await loadManifestPinnedKeys(input.packageRoot);
|
|
662
|
+
} catch {
|
|
663
|
+
return none('pinned_keys_invalid');
|
|
664
|
+
}
|
|
665
|
+
const releaseDirectory = join(input.root, RELEASE_STATE_DIRECTORY);
|
|
666
|
+
const meta = await readManifestMeta(input.root);
|
|
667
|
+
let highWater = meta.sequence;
|
|
668
|
+
/** @type {LastFetch | null} */
|
|
669
|
+
let lastFetch = meta.lastFetch;
|
|
670
|
+
/** @type {Verified | null} */
|
|
671
|
+
let fromServer = null;
|
|
672
|
+
/** @type {{ reason: string, detail: string } | null} */
|
|
673
|
+
let rejected = null;
|
|
674
|
+
const { endpoint, token } = input;
|
|
675
|
+
const due =
|
|
676
|
+
input.force === true ||
|
|
677
|
+
lastFetch === null ||
|
|
678
|
+
Number.isNaN(Date.parse(lastFetch.at)) ||
|
|
679
|
+
now().getTime() - Date.parse(lastFetch.at) >= interval;
|
|
680
|
+
if (typeof endpoint === 'string' && typeof token === 'string' && due) {
|
|
681
|
+
const answer = await fetchManifest({
|
|
682
|
+
endpoint,
|
|
683
|
+
token,
|
|
684
|
+
clientVersion: await installedClientVersion(input.packageRoot),
|
|
685
|
+
host: input.host ?? null,
|
|
686
|
+
...(input.fetchImpl === undefined ? {} : { fetchImpl: input.fetchImpl }),
|
|
687
|
+
...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }),
|
|
688
|
+
});
|
|
689
|
+
const at = now().toISOString();
|
|
690
|
+
if (answer.outcome === 'received') {
|
|
691
|
+
const verified = verifyManifest(answer.bytes, { pinnedKeys, now: now(), highWater });
|
|
692
|
+
if (verified.ok) {
|
|
693
|
+
fromServer = verified;
|
|
694
|
+
highWater = Math.max(highWater, verified.sequence);
|
|
695
|
+
lastFetch = { at, outcome: 'verified', reason: null };
|
|
696
|
+
try {
|
|
697
|
+
await ensurePrivateDirectory(releaseDirectory);
|
|
698
|
+
await atomicBytes(join(releaseDirectory, CACHED_MANIFEST_FILE), answer.bytes);
|
|
699
|
+
} catch {
|
|
700
|
+
// The cache is a convenience; the verified document still serves this run.
|
|
701
|
+
}
|
|
702
|
+
} else {
|
|
703
|
+
rejected = { reason: verified.reason, detail: verified.detail };
|
|
704
|
+
lastFetch = { at, outcome: 'rejected', reason: verified.reason };
|
|
705
|
+
}
|
|
706
|
+
} else {
|
|
707
|
+
lastFetch = { at, outcome: 'unavailable', reason: answer.reason };
|
|
708
|
+
}
|
|
709
|
+
try {
|
|
710
|
+
await ensurePrivateDirectory(releaseDirectory);
|
|
711
|
+
await atomicJson(join(releaseDirectory, MANIFEST_META_FILE), {
|
|
712
|
+
schemaVersion: 1,
|
|
713
|
+
sequence: highWater,
|
|
714
|
+
lastFetch,
|
|
715
|
+
});
|
|
716
|
+
} catch {
|
|
717
|
+
// Same: a meta write failure costs one extra fetch later, nothing else.
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/** @type {Verified | null} */
|
|
721
|
+
let selected = fromServer;
|
|
722
|
+
/** @type {ManifestSource} */
|
|
723
|
+
let source = fromServer === null ? 'none' : 'server';
|
|
724
|
+
if (selected === null) {
|
|
725
|
+
const cached = await readOwnedBytes(
|
|
726
|
+
join(releaseDirectory, CACHED_MANIFEST_FILE),
|
|
727
|
+
MAX_MANIFEST_BYTES,
|
|
728
|
+
);
|
|
729
|
+
if (cached !== null) {
|
|
730
|
+
const verified = verifyManifest(cached, { pinnedKeys, now: now(), highWater });
|
|
731
|
+
if (verified.ok) {
|
|
732
|
+
selected = verified;
|
|
733
|
+
source = 'cache';
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (selected === null) {
|
|
738
|
+
try {
|
|
739
|
+
const verified = verifyManifest(await readBundledManifest(input.packageRoot), {
|
|
740
|
+
pinnedKeys,
|
|
741
|
+
now: now(),
|
|
742
|
+
});
|
|
743
|
+
if (verified.ok) {
|
|
744
|
+
selected = verified;
|
|
745
|
+
source = 'bundled';
|
|
746
|
+
} else {
|
|
747
|
+
rejected = rejected ?? { reason: verified.reason, detail: verified.detail };
|
|
748
|
+
}
|
|
749
|
+
} catch {
|
|
750
|
+
rejected = rejected ?? { reason: 'manifest_invalid', detail: 'bundled_missing' };
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
if (selected === null) return { ...none(rejected?.detail ?? 'none'), lastFetch };
|
|
754
|
+
/** @type {ResolvedManifest['state']} */
|
|
755
|
+
let state = 'ok';
|
|
756
|
+
if (rejected?.reason === 'manifest_replayed') state = 'replayed_rejected';
|
|
757
|
+
else if (rejected !== null) state = 'invalid';
|
|
758
|
+
else if (selected.expired) state = 'expired';
|
|
759
|
+
return {
|
|
760
|
+
manifest: selected.manifest,
|
|
761
|
+
source,
|
|
762
|
+
state,
|
|
763
|
+
reason: rejected === null ? (selected.expired ? 'manifest_expired' : null) : rejected.reason,
|
|
764
|
+
sequence: selected.sequence,
|
|
765
|
+
keyId: selected.keyId,
|
|
766
|
+
issuedAt: selected.issuedAt,
|
|
767
|
+
expiresAt: selected.expiresAt,
|
|
768
|
+
lastFetch,
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** @param {string} reason @returns {ResolvedManifest} */
|
|
773
|
+
function none(reason) {
|
|
774
|
+
return {
|
|
775
|
+
manifest: null,
|
|
776
|
+
source: 'none',
|
|
777
|
+
state: 'none',
|
|
778
|
+
reason: SAFE_DETAIL.test(reason) ? reason : 'invalid',
|
|
779
|
+
sequence: null,
|
|
780
|
+
keyId: null,
|
|
781
|
+
issuedAt: null,
|
|
782
|
+
expiresAt: null,
|
|
783
|
+
lastFetch: null,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* The `manifest` section of `aisac doctor --json` and `aisac status --json` (FR8).
|
|
789
|
+
* @param {ResolvedManifest} resolved
|
|
790
|
+
*/
|
|
791
|
+
export function manifestDoctorSection(resolved) {
|
|
792
|
+
return {
|
|
793
|
+
sequence: resolved.sequence,
|
|
794
|
+
issued_at: resolved.issuedAt,
|
|
795
|
+
expires_at: resolved.expiresAt,
|
|
796
|
+
source: resolved.source,
|
|
797
|
+
key_id: resolved.keyId,
|
|
798
|
+
state: resolved.state,
|
|
799
|
+
reason: resolved.reason,
|
|
800
|
+
last_fetch: resolved.lastFetch,
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Refresh the manifest before a host launch (FR4): bounded by the fetch
|
|
806
|
+
* timeout, never throws, changes nothing about the launch in R2.
|
|
807
|
+
* @param {{ root: string, packageRoot: string, endpoint: string, token: string, host: 'claude' | 'codex',
|
|
808
|
+
* fetchImpl?: typeof fetch }} input
|
|
809
|
+
* @returns {Promise<ResolvedManifest | null>}
|
|
810
|
+
*/
|
|
811
|
+
export async function refreshReleaseManifest(input) {
|
|
812
|
+
try {
|
|
813
|
+
return await resolveReleaseManifest(input);
|
|
814
|
+
} catch {
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
}
|