@aisac/cli 0.4.0-beta.2 → 0.4.0-beta.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +146 -10
  2. package/dist/assets/claude/hooks/context-compaction.ts +17 -5
  3. package/dist/assets/claude/hooks/native-host-contract-runtime.ts +1 -1
  4. package/dist/assets/claude/hooks/native-host-contract.test.ts +8 -4
  5. package/dist/assets/claude/hooks/provider-window.d.ts +83 -0
  6. package/dist/assets/codex-context-compaction.mjs +846 -14
  7. package/dist/assets/jev-paid-pilot-terminal.mjs +12 -1
  8. package/dist/claude-compatibility.mjs +26 -25
  9. package/dist/claude-observation.mjs +2 -1
  10. package/dist/cli.mjs +641 -35
  11. package/dist/codex-compatibility.mjs +48 -2
  12. package/dist/codex-engineering.mjs +165 -0
  13. package/dist/codex-hooks.mjs +411 -0
  14. package/dist/codex-permissions.mjs +67 -34
  15. package/dist/codex-protocol-fingerprint.json +11 -11
  16. package/dist/engineering-init.mjs +615 -0
  17. package/dist/engineering-pack.mjs +276 -23
  18. package/dist/engineering-scan-pins.json +42 -0
  19. package/dist/engineering-scan-report.mjs +1274 -0
  20. package/dist/engineering-scan-tools.mjs +1418 -0
  21. package/dist/engineering-scan.mjs +1265 -0
  22. package/dist/extensions-catalog.mjs +390 -0
  23. package/dist/extensions-cli.mjs +492 -0
  24. package/dist/extensions-preferences.mjs +151 -0
  25. package/dist/guardrails/continuity.mjs +239 -0
  26. package/dist/guardrails/format.mjs +111 -0
  27. package/dist/guardrails/guards.mjs +1557 -0
  28. package/dist/guardrails/patch.mjs +33 -0
  29. package/dist/guardrails/run.mjs +286 -0
  30. package/dist/guardrails/shell-words.mjs +206 -0
  31. package/dist/guardrails-plugin.mjs +207 -0
  32. package/dist/launch.mjs +403 -47
  33. package/dist/managed-host.mjs +977 -0
  34. package/dist/manager/bus.mjs +1921 -0
  35. package/dist/manager/delegation.mjs +354 -0
  36. package/dist/manager/hooks.mjs +918 -0
  37. package/dist/manager/launch-role.mjs +476 -0
  38. package/dist/manager/mcp-stdio.mjs +463 -0
  39. package/dist/manager/messages.mjs +791 -0
  40. package/dist/manager/reserve.mjs +2166 -0
  41. package/dist/manager/room.mjs +106 -0
  42. package/dist/manager/server.mjs +1792 -0
  43. package/dist/manager/status.mjs +118 -0
  44. package/dist/manager/watch.mjs +87 -0
  45. package/dist/manager/worker.mjs +2793 -0
  46. package/dist/manager/workers-cli.mjs +1403 -0
  47. package/dist/mcp-servers.mjs +192 -0
  48. package/dist/mcp.mjs +10 -2
  49. package/dist/monitor.mjs +76 -1
  50. package/dist/package.json +1 -1
  51. package/dist/platform.mjs +78 -29
  52. package/dist/release-manifest-public-keys.json +10 -0
  53. package/dist/release-manifest.json +328 -0
  54. package/dist/release-manifest.mjs +817 -0
  55. package/dist/release-settings.mjs +210 -0
  56. package/dist/self-update.mjs +1036 -0
  57. package/dist/state.mjs +25 -1
  58. package/dist/token-guide.mjs +132 -0
  59. package/dist/update-actions.mjs +442 -0
  60. package/package.json +1 -1
@@ -0,0 +1,354 @@
1
+ // Delegated approvals, the data (spec aisac-engineering-manager D16, FR13):
2
+ // the grant file of one worker and the decision record. The command the user
3
+ // types to grant one is `workers-cli.mjs`; the hold and the decision are in
4
+ // the worker's adapter (`worker.mjs`). No tool writes a grant.
5
+
6
+ import { randomBytes } from 'node:crypto';
7
+ import { readdir, unlink } from 'node:fs/promises';
8
+ import { join } from 'node:path';
9
+ import {
10
+ BUS_SCHEMA_VERSION,
11
+ BusError,
12
+ CODE_PATTERN,
13
+ LIMITS,
14
+ NAME_PATTERN,
15
+ SESSION_ID_PATTERN,
16
+ appendJsonLine,
17
+ isPlainRecord,
18
+ isPrivateDirectory,
19
+ readBoundedFile,
20
+ readJsonFile,
21
+ removeIfUnchanged,
22
+ writePrivateFile,
23
+ } from './bus.mjs';
24
+ import { MESSAGE_ID_PATTERN, cutText } from './messages.mjs';
25
+
26
+ export const DECISION_OUTCOMES = Object.freeze(
27
+ /** @type {const} */ (['reserved', 'approved', 'denied', 'escalated', 'timed_out', 'withdrawn']),
28
+ );
29
+
30
+ const GRANT_ID_PATTERN = /^[0-9a-f]{32}$/u;
31
+ const GRANT_ID_BYTES = 16;
32
+ const GRANT_FILE_BYTES_MAX = 1024;
33
+ const DURATION_PATTERN = /^(\d{1,4})(m|h)$/u;
34
+ const MINUTE_MS = 60_000;
35
+ const HOUR_MS = 60 * MINUTE_MS;
36
+ const WIRE_ID_MAX = 128;
37
+ /**
38
+ * How far a grant's start may lie ahead of the reader's clock, and its expiry
39
+ * beyond 24 hours from it: the writer and the reader are processes of one
40
+ * machine, so a few seconds cover any difference; a clock stepped back by more
41
+ * ends the grant.
42
+ */
43
+ const GRANT_CLOCK_SKEW_MS = 5_000;
44
+ /** How much of each decision file is read: its last 8 MiB, twice the rotation size. */
45
+ const DECISIONS_READ_BYTES_MAX = 8 * 1024 * 1024;
46
+ const LINE_BREAK = '\n';
47
+ const DECISION_KINDS = ['command', 'fileChange', 'permissions', 'unknown'];
48
+ const GRANT_KEYS = ['schemaVersion', 'grantId', 'workerSessionId', 'startedAt', 'expiresAt'];
49
+ const DECISION_KEYS = [
50
+ 'at',
51
+ 'grantId',
52
+ 'workerSessionId',
53
+ 'workerName',
54
+ 'approvalId',
55
+ 'turnId',
56
+ 'itemId',
57
+ 'kind',
58
+ 'reserveClass',
59
+ 'outcome',
60
+ 'reason',
61
+ 'text',
62
+ ];
63
+
64
+ /** @typedef {import('./bus.mjs').RoomPaths} RoomPaths */
65
+ /** @typedef {(typeof DECISION_OUTCOMES)[number]} DecisionOutcome */
66
+ /**
67
+ * One live delegation: the user lets the manager answer this worker's
68
+ * approval requests until `expiresAt` (FR13).
69
+ * @typedef {object} Grant
70
+ * @property {1} schemaVersion
71
+ * @property {string} grantId
72
+ * @property {string} workerSessionId
73
+ * @property {number} startedAt
74
+ * @property {number} expiresAt
75
+ */
76
+ /**
77
+ * One line of `decisions.jsonl`: the only place of the bus that keeps the text
78
+ * of a request (D12), so that the user can review what was decided.
79
+ * @typedef {object} DecisionEntry
80
+ * @property {number} at
81
+ * @property {string} grantId
82
+ * @property {string} workerSessionId
83
+ * @property {string} workerName
84
+ * @property {string | null} approvalId null for a reserved request: it never reached the manager
85
+ * @property {string | null} turnId
86
+ * @property {string | null} itemId
87
+ * @property {'command' | 'fileChange' | 'permissions' | 'unknown'} kind
88
+ * @property {string | null} reserveClass
89
+ * @property {DecisionOutcome} outcome
90
+ * @property {string | null} reason the manager's reason, or the adapter's code for an outcome it produced itself
91
+ * @property {string} text the command, or the file paths one per line, cut at 4,000 characters
92
+ */
93
+
94
+ /**
95
+ * `45m` or `8h`, from ten minutes to twenty-four hours (FR13).
96
+ * @param {string} text
97
+ * @returns {number | null} milliseconds, or null when the text is not a valid duration
98
+ */
99
+ export function parseDuration(text) {
100
+ const match = DURATION_PATTERN.exec(text);
101
+ if (!match) return null;
102
+ const milliseconds = Number(match[1]) * (match[2] === 'h' ? HOUR_MS : MINUTE_MS);
103
+ return milliseconds >= LIMITS.delegationMinMs && milliseconds <= LIMITS.delegationMaxMs
104
+ ? milliseconds
105
+ : null;
106
+ }
107
+
108
+ /** @param {unknown} value */
109
+ function isTime(value) {
110
+ return Number.isSafeInteger(value) && /** @type {number} */ (value) >= 0;
111
+ }
112
+
113
+ /** @param {unknown} value @returns {value is Grant} */
114
+ function isGrant(value) {
115
+ if (!isPlainRecord(value)) return false;
116
+ const keys = Object.keys(value);
117
+ if (keys.length !== GRANT_KEYS.length || keys.some((key) => !GRANT_KEYS.includes(key)))
118
+ return false;
119
+ return (
120
+ value.schemaVersion === BUS_SCHEMA_VERSION &&
121
+ typeof value.grantId === 'string' &&
122
+ GRANT_ID_PATTERN.test(value.grantId) &&
123
+ typeof value.workerSessionId === 'string' &&
124
+ SESSION_ID_PATTERN.test(value.workerSessionId) &&
125
+ isTime(value.startedAt) &&
126
+ isTime(value.expiresAt) &&
127
+ /** @type {number} */ (value.expiresAt) - /** @type {number} */ (value.startedAt) >=
128
+ LIMITS.delegationMinMs &&
129
+ /** @type {number} */ (value.expiresAt) - /** @type {number} */ (value.startedAt) <=
130
+ LIMITS.delegationMaxMs
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Whether a grant holds now (FR13): it has started (within a few seconds of
136
+ * skew), it has not expired, and it ends no later than 24 hours from now. A
137
+ * grant file dated in the future, or a clock stepped back, never stretches a
138
+ * delegation past its 24 hours.
139
+ * @param {Grant} grant
140
+ * @param {number} now
141
+ */
142
+ function grantIsLive(grant, now) {
143
+ return (
144
+ grant.startedAt <= now + GRANT_CLOCK_SKEW_MS &&
145
+ grant.expiresAt > now &&
146
+ grant.expiresAt - now <= LIMITS.delegationMaxMs + GRANT_CLOCK_SKEW_MS
147
+ );
148
+ }
149
+
150
+ /** @param {RoomPaths} paths @param {string} workerSessionId */
151
+ function grantFile(paths, workerSessionId) {
152
+ if (!SESSION_ID_PATTERN.test(workerSessionId))
153
+ throw new BusError('bus_unavailable', 'session_id_invalid');
154
+ return join(paths.delegation, `${workerSessionId}.json`);
155
+ }
156
+
157
+ /**
158
+ * @param {{ workerSessionId: string, durationMs: number, now: number }} input
159
+ * @returns {Grant}
160
+ */
161
+ export function buildGrant({ workerSessionId, durationMs, now }) {
162
+ if (
163
+ !Number.isSafeInteger(durationMs) ||
164
+ durationMs < LIMITS.delegationMinMs ||
165
+ durationMs > LIMITS.delegationMaxMs
166
+ )
167
+ throw new BusError('duration_invalid');
168
+ return {
169
+ schemaVersion: BUS_SCHEMA_VERSION,
170
+ grantId: randomBytes(GRANT_ID_BYTES).toString('hex'),
171
+ workerSessionId,
172
+ startedAt: now,
173
+ expiresAt: now + durationMs,
174
+ };
175
+ }
176
+
177
+ /** @param {RoomPaths} paths @param {Grant} grant */
178
+ export async function writeGrant(paths, grant) {
179
+ if (!isGrant(grant)) throw new BusError('bus_unavailable', 'grant_invalid');
180
+ await writePrivateFile(
181
+ paths,
182
+ grantFile(paths, grant.workerSessionId),
183
+ `${JSON.stringify(grant)}\n`,
184
+ );
185
+ }
186
+
187
+ /**
188
+ * The worker's live grant (`grantIsLive`), or null. A grant of this worker
189
+ * that is not live (expired, dated in the future, ending more than 24 hours
190
+ * from now) is removed. A file that is not a grant of this worker (a newer
191
+ * schema, a damaged or forged file) is no grant, and is removed only a day
192
+ * after its last change; a file that could not be read is left alone. Nothing
193
+ * is removed that was replaced since it was read, nor through a delegation
194
+ * directory that is not a real directory of this user.
195
+ * @param {RoomPaths} paths
196
+ * @param {string} workerSessionId
197
+ * @param {number} now
198
+ * @returns {Promise<Grant | null>}
199
+ */
200
+ export async function readGrant(paths, workerSessionId, now) {
201
+ const path = grantFile(paths, workerSessionId);
202
+ const file = await readJsonFile(path, GRANT_FILE_BYTES_MAX);
203
+ if (!file.ok && (file.reason === 'missing' || file.reason === 'io_error')) return null;
204
+ const grant = file.ok ? file.value : null;
205
+ if (isGrant(grant) && grant.workerSessionId === workerSessionId) {
206
+ if (grantIsLive(grant, now)) return grant;
207
+ await removeIfUnchanged(path, file.ino);
208
+ return null;
209
+ }
210
+ if (file.modifiedAt !== null && file.modifiedAt < now - LIMITS.retentionMs)
211
+ await removeIfUnchanged(path, file.ino);
212
+ return null;
213
+ }
214
+
215
+ /**
216
+ * The worker's live grant, or null, exactly as `readGrant` decides it, but
217
+ * without deleting or writing anything: for readers that must not change the
218
+ * room (the manager's server, listings).
219
+ * @param {RoomPaths} paths
220
+ * @param {string} workerSessionId
221
+ * @param {number} now
222
+ * @returns {Promise<Grant | null>}
223
+ */
224
+ export async function peekGrant(paths, workerSessionId, now) {
225
+ const file = await readJsonFile(grantFile(paths, workerSessionId), GRANT_FILE_BYTES_MAX);
226
+ if (!file.ok) return null;
227
+ const grant = file.value;
228
+ return isGrant(grant) && grant.workerSessionId === workerSessionId && grantIsLive(grant, now)
229
+ ? grant
230
+ : null;
231
+ }
232
+
233
+ /**
234
+ * Remove a worker's grant file, live or not; never through a delegation
235
+ * directory that is not a real directory of this user.
236
+ * @param {RoomPaths} paths
237
+ * @param {string} workerSessionId
238
+ * @returns {Promise<boolean>} whether a grant file was removed
239
+ */
240
+ export async function removeGrant(paths, workerSessionId) {
241
+ const path = grantFile(paths, workerSessionId);
242
+ if (!(await isPrivateDirectory(paths.delegation))) return false;
243
+ return unlink(path).then(
244
+ () => true,
245
+ () => false,
246
+ );
247
+ }
248
+
249
+ /**
250
+ * Every live grant of the room.
251
+ * @param {RoomPaths} paths
252
+ * @param {number} now
253
+ * @returns {Promise<Grant[]>}
254
+ */
255
+ export async function listGrants(paths, now) {
256
+ const names = await readdir(paths.delegation).catch(() => []);
257
+ /** @type {Grant[]} */
258
+ const grants = [];
259
+ for (const name of names.sort()) {
260
+ const workerSessionId = name.replace(/\.json$/u, '');
261
+ if (!SESSION_ID_PATTERN.test(workerSessionId)) continue;
262
+ const grant = await readGrant(paths, workerSessionId, now);
263
+ if (grant !== null) grants.push(grant);
264
+ }
265
+ return grants;
266
+ }
267
+
268
+ /** @param {unknown} value @param {number} max */
269
+ function isNullableText(value, max) {
270
+ return value === null || (typeof value === 'string' && value.length <= max);
271
+ }
272
+
273
+ /** @param {unknown} value @returns {value is DecisionEntry} */
274
+ function isDecisionEntry(value) {
275
+ if (!isPlainRecord(value)) return false;
276
+ const keys = Object.keys(value);
277
+ if (keys.length !== DECISION_KEYS.length || keys.some((key) => !DECISION_KEYS.includes(key)))
278
+ return false;
279
+ return (
280
+ isTime(value.at) &&
281
+ typeof value.grantId === 'string' &&
282
+ GRANT_ID_PATTERN.test(value.grantId) &&
283
+ typeof value.workerSessionId === 'string' &&
284
+ SESSION_ID_PATTERN.test(value.workerSessionId) &&
285
+ typeof value.workerName === 'string' &&
286
+ NAME_PATTERN.test(value.workerName) &&
287
+ (value.approvalId === null ||
288
+ (typeof value.approvalId === 'string' && MESSAGE_ID_PATTERN.test(value.approvalId))) &&
289
+ isNullableText(value.turnId, WIRE_ID_MAX) &&
290
+ isNullableText(value.itemId, WIRE_ID_MAX) &&
291
+ DECISION_KINDS.includes(/** @type {string} */ (value.kind)) &&
292
+ (value.reserveClass === null ||
293
+ (typeof value.reserveClass === 'string' && CODE_PATTERN.test(value.reserveClass))) &&
294
+ DECISION_OUTCOMES.includes(/** @type {DecisionOutcome} */ (value.outcome)) &&
295
+ isNullableText(value.reason, LIMITS.decisionReasonMax) &&
296
+ typeof value.text === 'string' &&
297
+ value.text.length <= LIMITS.commandTextMax
298
+ );
299
+ }
300
+
301
+ /**
302
+ * Append one outcome to the decision record (FR13). Text is cut at the limits;
303
+ * the file rotates at 4 MiB and keeps one previous file.
304
+ * @param {RoomPaths} paths
305
+ * @param {DecisionEntry} entry
306
+ */
307
+ export async function appendDecision(paths, entry) {
308
+ /** @type {DecisionEntry} */
309
+ const line = {
310
+ ...entry,
311
+ turnId: entry.turnId === null ? null : entry.turnId.slice(0, WIRE_ID_MAX),
312
+ itemId: entry.itemId === null ? null : entry.itemId.slice(0, WIRE_ID_MAX),
313
+ reason: entry.reason === null ? null : cutText(entry.reason, LIMITS.decisionReasonMax).text,
314
+ text: cutText(entry.text, LIMITS.commandTextMax).text,
315
+ };
316
+ if (!isDecisionEntry(line)) throw new BusError('bus_unavailable', 'decision_invalid');
317
+ await appendJsonLine(paths.decisions, line, LIMITS.decisionsRotateBytes, { paths });
318
+ }
319
+
320
+ /**
321
+ * The lines of one decision file, read from its last 8 MiB at most, without
322
+ * following a symlink. A line the cut began inside is dropped.
323
+ * @param {string} file
324
+ * @returns {Promise<string[]>}
325
+ */
326
+ async function decisionLines(file) {
327
+ const read = await readBoundedFile(file, DECISIONS_READ_BYTES_MAX, { tail: true });
328
+ if (!read.ok) return [];
329
+ const lines = read.data.toString('utf8').split(LINE_BREAK);
330
+ return read.size > DECISIONS_READ_BYTES_MAX ? lines.slice(1) : lines;
331
+ }
332
+
333
+ /**
334
+ * The decision record, oldest first: the previous file, then the current one.
335
+ * Lines that are not valid entries are skipped.
336
+ * @param {RoomPaths} paths
337
+ * @returns {Promise<DecisionEntry[]>}
338
+ */
339
+ export async function readDecisions(paths) {
340
+ /** @type {DecisionEntry[]} */
341
+ const entries = [];
342
+ for (const file of [`${paths.decisions}.1`, paths.decisions]) {
343
+ for (const line of await decisionLines(file)) {
344
+ if (line.length === 0) continue;
345
+ try {
346
+ const value = JSON.parse(line);
347
+ if (isDecisionEntry(value)) entries.push(value);
348
+ } catch {
349
+ // A torn or foreign line is not an entry.
350
+ }
351
+ }
352
+ }
353
+ return entries;
354
+ }