@aiwg/cli 2026.10.7 → 2026.10.9

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 (33) hide show
  1. package/dist/src/artifacts/cli.js +9 -5
  2. package/dist/src/auth/config.js +3 -8
  3. package/dist/src/auth/resource-credentials.js +35 -2
  4. package/dist/src/cli/handlers/cost-report.js +7 -1
  5. package/dist/src/cli/handlers/decision.js +33 -8
  6. package/dist/src/cli/handlers/diagnose.js +13 -0
  7. package/dist/src/cli/handlers/local-executor.js +33 -6
  8. package/dist/src/cli/handlers/resource-versions.js +8 -5
  9. package/dist/src/cli/handlers/script-runner.js +13 -1
  10. package/dist/src/cli/handlers/use.js +18 -3
  11. package/dist/src/cost/fleet-report.js +20 -22
  12. package/dist/src/decision/driver.js +2 -2
  13. package/dist/src/decision/jev-credentials.js +18 -5
  14. package/dist/src/decision/ledger.js +230 -34
  15. package/dist/src/mcp/cli.mjs +105 -19
  16. package/dist/src/mcp/credentials.d.mts +4 -0
  17. package/dist/src/mcp/credentials.mjs +44 -3
  18. package/dist/src/mcp/grok-build-config.mjs +2 -0
  19. package/dist/src/mcp/omp-config.mjs +2 -0
  20. package/dist/src/mcp/registry.js +19 -15
  21. package/dist/src/mcp/registry.mjs +2 -18
  22. package/dist/src/mcp/tools/agentic-sandbox.mjs +46 -28
  23. package/dist/src/models/grok-build-models.js +20 -0
  24. package/dist/src/providers/provider-definitions.js +2 -2
  25. package/dist/src/resources/web-release.js +76 -65
  26. package/dist/src/serve/shared-host-scheduler.js +4 -1
  27. package/dist/src/storage/backends/fortemi.js +112 -50
  28. package/dist/src/storage/backends/logseq.js +16 -3
  29. package/dist/src/storage/backends/postgres.js +74 -33
  30. package/dist/src/storage/backends/postgrest.js +37 -13
  31. package/dist/src/storage/config.js +6 -1
  32. package/dist/src/uhp/config.js +4 -0
  33. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * OpenRouter fleet spend observation and local activity correlation.
3
3
  *
4
- * Secrets are resolved only from the process environment or the user's
4
+ * Secrets are resolved from named sources or the user's
5
5
  * ~/.config/aiwg/keys directory. Fleet configuration contains references,
6
6
  * never credential values.
7
7
  *
@@ -11,6 +11,7 @@ import { promises as fs } from 'node:fs';
11
11
  import os from 'node:os';
12
12
  import path from 'node:path';
13
13
  import { load as loadYaml } from 'js-yaml';
14
+ import { resolveSensitiveInput } from '../auth/sensitive-input.js';
14
15
  const OPENROUTER_API = 'https://openrouter.ai/api/v1';
15
16
  const KEY_REF_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
16
17
  const SECRET_FIELD_PATTERN = /(?:api[_-]?key|token|secret|credential|authorization)/i;
@@ -60,6 +61,9 @@ function validateFleetConfig(value) {
60
61
  if (!KEY_REF_PATTERN.test(entry.key_ref)) {
61
62
  throw new Error(`fleet[${index}].key_ref contains unsupported characters.`);
62
63
  }
64
+ if (entry.key_store !== undefined && (typeof entry.key_store !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$/.test(entry.key_store))) {
65
+ throw new Error(`fleet[${index}].key_store must name a native-store entry (1–128 letters, digits, dots, underscores or hyphens).`);
66
+ }
63
67
  if (typeof entry.monthly_cap !== 'number' || !Number.isFinite(entry.monthly_cap) || entry.monthly_cap < 0) {
64
68
  throw new Error(`fleet[${index}].monthly_cap must be a non-negative number.`);
65
69
  }
@@ -73,6 +77,7 @@ function validateFleetConfig(value) {
73
77
  bot: entry.bot.trim(),
74
78
  machine: entry.machine.trim(),
75
79
  key_ref: entry.key_ref,
80
+ ...(entry.key_store !== undefined ? { key_store: entry.key_store } : {}),
76
81
  monthly_cap: entry.monthly_cap,
77
82
  };
78
83
  });
@@ -101,10 +106,16 @@ export async function loadFleetKey(keyRef, options = {}) {
101
106
  if (!KEY_REF_PATTERN.test(keyRef))
102
107
  throw new Error('Invalid key_ref.');
103
108
  const env = options.env ?? process.env;
109
+ const dependencies = { environment: env, readStore: options.readStore, isTTY: false };
110
+ // An explicit reference is scoped to this entry. Never probe a global key or
111
+ // another bot's store when the selected source is unavailable.
112
+ if (options.store !== undefined) {
113
+ return resolveSensitiveInput({ prefix: 'key', store: options.store }, dependencies);
114
+ }
104
115
  const environmentName = keyEnvironmentName(keyRef);
105
116
  const environmentValue = env[environmentName]?.trim();
106
117
  if (environmentValue)
107
- return environmentValue;
118
+ return resolveSensitiveInput({ prefix: 'key', env: environmentName }, dependencies);
108
119
  const homeDir = options.homeDir ?? os.homedir();
109
120
  const keyDirectory = path.resolve(homeDir, '.config', 'aiwg', 'keys');
110
121
  const keyPath = path.resolve(keyDirectory, keyRef);
@@ -116,7 +127,7 @@ export async function loadFleetKey(keyRef, options = {}) {
116
127
  }
117
128
  catch (error) {
118
129
  if (error.code === 'ENOENT') {
119
- throw new Error(`No secure credential directory exists for key_ref '${keyRef}'; use ${environmentName} or create ~/.config/aiwg/keys with mode 0700.`);
130
+ throw new Error(`No secure credential directory exists for key_ref '${keyRef}'; use ${environmentName}, --key-store <name> (fleet: key_store), or create ~/.config/aiwg/keys with mode 0700.`);
120
131
  }
121
132
  throw error;
122
133
  }
@@ -129,27 +140,12 @@ export async function loadFleetKey(keyRef, options = {}) {
129
140
  if (typeof process.getuid === 'function' && directoryStat.uid !== process.getuid()) {
130
141
  throw new Error('The AIWG credential directory must be owned by the current user.');
131
142
  }
132
- let stat;
133
143
  try {
134
- stat = await fs.lstat(keyPath);
144
+ return await resolveSensitiveInput({ prefix: 'key', file: keyPath }, dependencies);
135
145
  }
136
146
  catch (error) {
137
- if (error.code === 'ENOENT') {
138
- throw new Error(`No credential found for key_ref '${keyRef}' in the secure key directory or ${environmentName}.`);
139
- }
140
- throw error;
141
- }
142
- if (stat.isSymbolicLink() || !stat.isFile())
143
- throw new Error(`Credential for key_ref '${keyRef}' must be a regular file, not a symlink.`);
144
- if ((stat.mode & 0o077) !== 0)
145
- throw new Error(`Credential file for key_ref '${keyRef}' must not be accessible by group or other users.`);
146
- if (typeof process.getuid === 'function' && stat.uid !== process.getuid()) {
147
- throw new Error(`Credential file for key_ref '${keyRef}' must be owned by the current user.`);
147
+ throw new Error(`Credential for key_ref '${keyRef}': ${error.message} Use ${environmentName}, --key-store <name> (fleet: key_store), or a private ~/.config/aiwg/keys/<key_ref> file.`);
148
148
  }
149
- const value = (await fs.readFile(keyPath, 'utf8')).trim();
150
- if (!value)
151
- throw new Error(`Credential file for key_ref '${keyRef}' is empty.`);
152
- return value;
153
149
  }
154
150
  function parseActivityLog(raw) {
155
151
  const entries = [];
@@ -202,7 +198,9 @@ async function openRouterGet(endpoint, key, options) {
202
198
  }
203
199
  if (!payload || typeof payload !== 'object' || !payload.data)
204
200
  throw new Error('OpenRouter returned an invalid response.');
205
- return payload.data;
201
+ // Generation metadata can reflect a request header in free-text fields. Keep
202
+ // such content out of both structured and human-readable reports.
203
+ return JSON.parse(JSON.stringify(payload.data, (_name, value) => (typeof value === 'string' ? value.split(key).join('[REDACTED]') : value)));
206
204
  }
207
205
  function correlateGenerations(records) {
208
206
  const sessions = new Map();
@@ -223,7 +221,7 @@ function correlateGenerations(records) {
223
221
  }
224
222
  async function reportBot(entry, activity, options) {
225
223
  try {
226
- const key = await loadFleetKey(entry.key_ref, options);
224
+ const key = await loadFleetKey(entry.key_ref, { ...options, store: entry.key_store });
227
225
  const keyData = await openRouterGet('/key', key, options);
228
226
  const spend = Number(keyData.usage_monthly ?? keyData.usage ?? 0);
229
227
  if (!Number.isFinite(spend) || spend < 0)
@@ -4,12 +4,12 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
4
4
  import path from 'node:path';
5
5
  import { fileURLToPath, pathToFileURL } from 'node:url';
6
6
  import { askDecision, setupJev, DecisionAskUsageError, readDecisionContextFile, validateDecisionAskInput, } from './ask.js';
7
- import { recordDecisionAsk, decisionLedgerHistory, decisionLedgerStats, resolveDecisionSessionId, resolveDecisionSession, DECISION_SESSION_ENV_VARS, parseDecisionLedgerSince, DecisionLedgerSinceError, DECISION_LEDGER_SCHEMA, DECISION_LEDGER_SCHEMA_V1, ATTRIBUTION_FIELD_PATTERN, MAX_ATTRIBUTION_FIELD_LENGTH, MAX_RECORDED_CHOICE_IDS, MAX_RECORD_TEXT_BYTES, MAX_LEDGER_RECORD_BYTES, DECISION_LEDGER_RECORD_TIMEOUT_MS, DECISION_SAVINGS_REASONING_ALLOWANCE_TOKENS, DECISION_SAVINGS_ESTIMATION_METHOD, buildDecisionLedgerRecord, appendDecisionLedgerRecord, filterDecisionLedgerRecords, } from './ledger.js';
7
+ import { recordDecisionAsk, decisionLedgerHistory, decisionLedgerStats, rotateDecisionLedger, parseDecisionLedgerRetention, resolveDecisionSessionId, resolveDecisionSession, DECISION_SESSION_ENV_VARS, parseDecisionLedgerSince, DecisionLedgerSinceError, DECISION_LEDGER_SCHEMA, DECISION_LEDGER_SCHEMA_V1, ATTRIBUTION_FIELD_PATTERN, MAX_ATTRIBUTION_FIELD_LENGTH, MAX_RECORDED_CHOICE_IDS, MAX_RECORD_TEXT_BYTES, MAX_LEDGER_RECORD_BYTES, DECISION_LEDGER_RECORD_TIMEOUT_MS, DECISION_SAVINGS_REASONING_ALLOWANCE_TOKENS, DECISION_SAVINGS_ESTIMATION_METHOD, buildDecisionLedgerRecord, appendDecisionLedgerRecord, filterDecisionLedgerRecords, } from './ledger.js';
8
8
  export { DEFAULT_DECISION_SAVINGS_WEIGHTS, decisionSavingsWeights } from './savings.js';
9
9
  import { resolveActiveProvider } from '../cli/provider-resolution.js';
10
10
  export { appendDecisionOpportunity, buildDecisionOffloadSnapshot, decisionOffloadCategory, DECISION_OFFLOAD_CHECKPOINT_COVERAGE, DECISION_OFFLOAD_EXECUTABLE_COVERAGE, DECISION_OFFLOAD_SNAPSHOT_SCHEMA, DECISION_OPPORTUNITY_SCHEMA, runDecisionOffloadCheckpoint, } from './offload.js';
11
11
  import { assertDecisionEvaluateDispatcherConfig, decisionPatternPacks, getDecisionPatternPack, governedPatternArtifacts, listDecisionPatterns, patternInputSchema, patternOutputSchema, planLiveDecisionPattern, runOfflineDecisionPattern, validateAgainstSchema, validateDecisionDocument, validateDefinition, validateRuleset, } from './index.js';
12
- export { askDecision, setupJev, readDecisionContextFile, validateDecisionAskInput, DecisionAskUsageError, recordDecisionAsk, decisionLedgerHistory, decisionLedgerStats, resolveDecisionSessionId, resolveDecisionSession, DECISION_SESSION_ENV_VARS, parseDecisionLedgerSince, DecisionLedgerSinceError, DECISION_LEDGER_SCHEMA, DECISION_LEDGER_SCHEMA_V1, ATTRIBUTION_FIELD_PATTERN, MAX_ATTRIBUTION_FIELD_LENGTH, MAX_RECORDED_CHOICE_IDS, MAX_RECORD_TEXT_BYTES, MAX_LEDGER_RECORD_BYTES, DECISION_LEDGER_RECORD_TIMEOUT_MS, DECISION_SAVINGS_REASONING_ALLOWANCE_TOKENS, DECISION_SAVINGS_ESTIMATION_METHOD, buildDecisionLedgerRecord, appendDecisionLedgerRecord, filterDecisionLedgerRecords, resolveActiveProvider, };
12
+ export { askDecision, setupJev, readDecisionContextFile, validateDecisionAskInput, DecisionAskUsageError, recordDecisionAsk, decisionLedgerHistory, decisionLedgerStats, rotateDecisionLedger, parseDecisionLedgerRetention, resolveDecisionSessionId, resolveDecisionSession, DECISION_SESSION_ENV_VARS, parseDecisionLedgerSince, DecisionLedgerSinceError, DECISION_LEDGER_SCHEMA, DECISION_LEDGER_SCHEMA_V1, ATTRIBUTION_FIELD_PATTERN, MAX_ATTRIBUTION_FIELD_LENGTH, MAX_RECORDED_CHOICE_IDS, MAX_RECORD_TEXT_BYTES, MAX_LEDGER_RECORD_BYTES, DECISION_LEDGER_RECORD_TIMEOUT_MS, DECISION_SAVINGS_REASONING_ALLOWANCE_TOKENS, DECISION_SAVINGS_ESTIMATION_METHOD, buildDecisionLedgerRecord, appendDecisionLedgerRecord, filterDecisionLedgerRecords, resolveActiveProvider, };
13
13
  const DECISION_ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
14
14
  const VALIDATION_TARGETS = ['request', 'definition', 'ruleset', 'binding'];
15
15
  const OFFLINE_ADAPTER = 'agentic/code/addons/decision-engine/examples/fixture-jev-adapter.mjs';
@@ -1,10 +1,11 @@
1
1
  import { resolveDecisionEnablement } from '../config/decision-config.js';
2
2
  import { constants } from 'node:fs';
3
- import { access, chmod, mkdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
3
+ import { access, chmod, lstat, mkdir, rename, rm, stat, writeFile } from 'node:fs/promises';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
6
  import { randomUUID } from 'node:crypto';
7
7
  import { JevCredentialError } from './adapters/jev.js';
8
+ import { readPrivateValueFile } from '../auth/sensitive-input.js';
8
9
  export const JEV_CREDENTIAL_REF = 'typesafe-api';
9
10
  export const JEV_DEFAULT_REGION = 'us';
10
11
  function env(options) {
@@ -45,10 +46,22 @@ function parseRecord(raw) {
45
46
  async function readCredentialFile(options = {}) {
46
47
  const credentialPath = jevCredentialPath(options);
47
48
  try {
48
- const [fileStat, parentStat] = await Promise.all([stat(credentialPath), stat(path.dirname(credentialPath))]);
49
- if (isInsecureMode(fileStat.mode) || isInsecureMode(parentStat.mode))
50
- throw new JevCredentialError('denied');
51
- return parseRecord(await readFile(credentialPath, 'utf8'));
49
+ // Preserve the missing-file result and the existing directory requirement.
50
+ // The shared reader validates the opened descriptor, including ownership
51
+ // and link checks, before any credential record is parsed.
52
+ const [, parentStat] = await Promise.all([lstat(credentialPath), stat(path.dirname(credentialPath))]);
53
+ if (isInsecureMode(parentStat.mode)) {
54
+ throw Object.assign(new JevCredentialError('denied'), { message: 'Jev credential directory must not allow group or other access (chmod 700).' });
55
+ }
56
+ let raw;
57
+ try {
58
+ raw = await readPrivateValueFile(credentialPath, 'Jev credential file');
59
+ }
60
+ catch (error) {
61
+ // Shared private-file diagnostics contain no path or credential bytes.
62
+ throw Object.assign(new JevCredentialError('denied'), { message: error.message });
63
+ }
64
+ return parseRecord(raw);
52
65
  }
53
66
  catch (error) {
54
67
  if (error.code === 'ENOENT')
@@ -21,9 +21,9 @@
21
21
  * assumption that the artifact root is owned and written by the SAME local
22
22
  * user running `aiwg`. It does not and cannot defend against a HOSTILE
23
23
  * process running as that same user: such a process already has read/write
24
- * access to everything the ledger could protect. There is no cross-process
25
- * lock and no privilege boundary here, by design — adding one would be the
26
- * "second subsystem" this feature deliberately avoids. Every filesystem
24
+ * access to everything the ledger could protect. Appenders, rotation and
25
+ * readers share the existing cross-process directory-lock helper. There is
26
+ * no privilege boundary here. Every filesystem
27
27
  * check below uses only portable Node primitives (no `/proc`, no handle
28
28
  * anchoring): `mkdir`/`lstat` for the directory, `open`+`fstat` for the
29
29
  * file, with `O_NOFOLLOW` applied when the platform defines it (POSIX; not
@@ -39,25 +39,26 @@
39
39
  * single-write atomicity boundary most platforms also honor for `PIPE_BUF`
40
40
  * (4096 bytes on Linux): this makes a single, non-interleaved write the
41
41
  * overwhelmingly likely outcome for one record, but it is not a guarantee.
42
- * There is no cross-process append lock, so two concurrent writers can in
43
- * rare cases interleave and produce one malformed line; `history`/`stats`
44
- * detect and skip a malformed line (counted, never silently merged into a
45
- * neighboring record) rather than assuming it is impossible.
42
+ * The lock covers opening, checking the trailing newline, writing every
43
+ * byte and closing. Rotation holds that same lock through live-file
44
+ * replacement. `history`/`stats` count malformed lines as skipped; retention
45
+ * preserves their original bytes.
46
46
  *
47
47
  * Recording — attribution resolution and the write — is bounded by a short
48
48
  * deadline (`DECISION_LEDGER_RECORD_TIMEOUT_MS`). On timeout, the in-flight
49
49
  * attempt is not cancelled (Node has no way to abort an in-flight `open`),
50
50
  * so its outcome is genuinely unknown: the record may or may not eventually
51
51
  * land. The warning says exactly that ("completion unknown"), and no
52
- * further stage of that same attempt starts once the deadline has fired
53
- * (checked before each later `await`), so a slow attribution lookup cannot
54
- * itself delay the subsequent write past the deadline.
52
+ * attribution-to-append stage starts once the deadline has fired. An append
53
+ * already waiting for the ledger lock can still complete after that deadline.
55
54
  */
56
55
  import { randomUUID, createHash } from 'node:crypto';
57
56
  import { constants as fsConstants, createReadStream, existsSync } from 'node:fs';
58
- import { lstat, mkdir, open } from 'node:fs/promises';
57
+ import { lstat, mkdir, open, readdir, rename, rm } from 'node:fs/promises';
59
58
  import { createInterface } from 'node:readline';
60
- import { join } from 'node:path';
59
+ import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
60
+ import { acquireDirectoryLock } from '../artifacts/prebuilt-build-lock.js';
61
+ import { syncDirectory } from '../storage/protected-files.js';
61
62
  import { projectAiwgPath, projectAiwgWritePath } from '../config/project-artifacts.js';
62
63
  import { redactText } from '../governance/redaction.js';
63
64
  import { CanonicalJsonByteEstimator } from './context-plan.js';
@@ -121,6 +122,7 @@ export const MAX_LEDGER_RECORD_BYTES = 4000;
121
122
  export const DECISION_LEDGER_RECORD_TIMEOUT_MS = 250;
122
123
  const LEDGER_DIR_MODE = 0o700;
123
124
  const LEDGER_FILE_MODE = 0o600;
125
+ const LEDGER_LOCK_TIMEOUT_MS = 30_000;
124
126
  const IS_WINDOWS = process.platform === 'win32';
125
127
  const HAS_O_NOFOLLOW = typeof fsConstants.O_NOFOLLOW === 'number';
126
128
  function errorMessage(error) {
@@ -349,8 +351,8 @@ async function ensurePrivateDecisionDir(projectRoot, warn) {
349
351
  return decisionDir;
350
352
  }
351
353
  /**
352
- * Open (creating if needed) `ledger.jsonl` for append and verify it is safe
353
- * to write: a regular file with exactly one hard link (rejects a
354
+ * Open a private ledger/archive file (append by default, read-only for
355
+ * retention) and verify it is a regular file with exactly one hard link (rejects a
354
356
  * pre-planted hard link to an unrelated file, which `O_NOFOLLOW` alone does
355
357
  * not catch), and on POSIX owned by the current user with no group/other
356
358
  * permission bits. `O_NOFOLLOW` is applied when the platform defines it
@@ -360,7 +362,7 @@ async function ensurePrivateDecisionDir(projectRoot, warn) {
360
362
  * check is closed untouched (no chmod, no write) and recording is skipped
361
363
  * with exactly one warning naming the reason.
362
364
  */
363
- async function openLedgerFileForAppend(ledgerPath, warn) {
365
+ async function openPrivateLedgerFile(ledgerPath, warn, flags = fsConstants.O_APPEND | fsConstants.O_CREAT | fsConstants.O_WRONLY) {
364
366
  try {
365
367
  const pre = await lstat(ledgerPath);
366
368
  if (pre.isSymbolicLink()) {
@@ -374,10 +376,9 @@ async function openLedgerFileForAppend(ledgerPath, warn) {
374
376
  return null;
375
377
  }
376
378
  }
377
- const flags = fsConstants.O_APPEND | fsConstants.O_CREAT | fsConstants.O_WRONLY | (HAS_O_NOFOLLOW ? fsConstants.O_NOFOLLOW : 0);
378
379
  let handle;
379
380
  try {
380
- handle = await open(ledgerPath, flags, LEDGER_FILE_MODE);
381
+ handle = await open(ledgerPath, flags | (HAS_O_NOFOLLOW ? fsConstants.O_NOFOLLOW : 0), LEDGER_FILE_MODE);
381
382
  }
382
383
  catch (error) {
383
384
  warn(`warning: cannot open decision ledger file: ${errorMessage(error)}`);
@@ -466,10 +467,13 @@ export async function appendDecisionLedgerRecord(record, options) {
466
467
  if (!decisionDir)
467
468
  return false;
468
469
  const ledgerPath = join(decisionDir, DECISION_LEDGER_FILE_NAME);
469
- const handle = await openLedgerFileForAppend(ledgerPath, warn);
470
- if (!handle)
471
- return false;
470
+ let release;
471
+ let handle = null;
472
472
  try {
473
+ release = await lockDecisionLedger(decisionDir);
474
+ handle = await openPrivateLedgerFile(ledgerPath, warn);
475
+ if (!handle)
476
+ return false;
473
477
  const prefix = (await needsLeadingNewline(ledgerPath)) ? '\n' : '';
474
478
  await writeFully(handle, Buffer.from(prefix + line, 'utf8'), options.writeChunk);
475
479
  return true;
@@ -479,7 +483,162 @@ export async function appendDecisionLedgerRecord(record, options) {
479
483
  return false;
480
484
  }
481
485
  finally {
482
- await handle.close().catch(() => { });
486
+ await handle?.close().catch(() => { });
487
+ await release?.().catch(error => warn(`warning: cannot release decision ledger lock: ${errorMessage(error)}`));
488
+ }
489
+ }
490
+ function lockDecisionLedger(decisionDir) {
491
+ return acquireDirectoryLock(join(decisionDir, 'ledger.lock'), {
492
+ timeoutMs: LEDGER_LOCK_TIMEOUT_MS, pollMs: 10, reclaimDeadOwner: true,
493
+ });
494
+ }
495
+ export function parseDecisionLedgerRetention(keepLast, all = false) {
496
+ if (all) {
497
+ if (keepLast !== undefined)
498
+ throw new Error('--all cannot be combined with --keep-last');
499
+ return {};
500
+ }
501
+ const match = /^(\d+)(d?)$/.exec(keepLast ?? '90d');
502
+ const n = match ? Number(match[1]) : NaN;
503
+ if (!Number.isSafeInteger(n) || n <= 0 || !Number.isSafeInteger(n * (match?.[2] ? 86_400_000 : 1))) {
504
+ throw new Error('--keep-last must be a positive count or <N>d; rotating everything requires --all');
505
+ }
506
+ return match?.[2] ? { ms: n * 86_400_000 } : { count: n };
507
+ }
508
+ function isBeneath(root, target) {
509
+ const rel = relative(root, target);
510
+ return rel !== '' && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
511
+ }
512
+ /** Walk before mkdir: recursive mkdir alone could write through a symlinked ancestor. */
513
+ async function privateArchiveDirectory(root, target, create) {
514
+ let current = root;
515
+ for (const part of relative(root, target).split(sep)) {
516
+ current = join(current, part);
517
+ if (create) {
518
+ try {
519
+ await mkdir(current, { mode: LEDGER_DIR_MODE });
520
+ if (!IS_WINDOWS)
521
+ await syncDirectory(dirname(current));
522
+ }
523
+ catch (error) {
524
+ if (error.code !== 'EEXIST')
525
+ throw error;
526
+ }
527
+ }
528
+ let info;
529
+ try {
530
+ info = await lstat(current);
531
+ }
532
+ catch (error) {
533
+ if (!create && error.code === 'ENOENT')
534
+ return false;
535
+ throw error;
536
+ }
537
+ // archive/ is shared with the activity log and may already be readable.
538
+ // Its decision/ subtree (and any nested directories) must be private.
539
+ const modeMask = current === join(root, 'archive') ? 0o022 : 0o077;
540
+ if (!info.isDirectory() || (!IS_WINDOWS && (info.uid !== currentUid() || (info.mode & modeMask) !== 0))) {
541
+ throw new Error(`Unsafe decision archive directory (requires a private, owned real directory): ${current}`);
542
+ }
543
+ }
544
+ return true;
545
+ }
546
+ /** Retention preserves each raw line, including invalid UTF-8, CRLF and an unterminated final line. */
547
+ export async function rotateDecisionLedger(options) {
548
+ const retention = parseDecisionLedgerRetention(options.keepLast, options.all);
549
+ const now = (options.now ?? Date.now)();
550
+ const artifactRoot = projectAiwgWritePath(options.projectRoot);
551
+ const archiveRoot = join(artifactRoot, 'archive', 'decision');
552
+ const archivePath = resolve(artifactRoot, options.to ?? join('archive', 'decision', `${new Date(now).toISOString().replace(/[-:]/g, '').replace('T', '-').replace(/\.\d{3}Z$/, '')}-${randomUUID()}.jsonl`));
553
+ if (!isBeneath(artifactRoot, archivePath))
554
+ throw new Error('Archive path must stay inside the resolved artifact root');
555
+ if (!isBeneath(archiveRoot, archivePath) || !archivePath.endsWith('.jsonl')) {
556
+ throw new Error('Archive path must be a .jsonl file beneath archive/decision/ in the resolved artifact root');
557
+ }
558
+ const fail = (message) => { throw new Error(message); };
559
+ const decisionDir = await ensurePrivateDecisionDir(options.projectRoot, fail);
560
+ if (!decisionDir)
561
+ throw new Error('Cannot access decision ledger directory');
562
+ const release = await lockDecisionLedger(decisionDir);
563
+ const ledgerPath = join(decisionDir, DECISION_LEDGER_FILE_NAME);
564
+ const tempPath = join(decisionDir, `.ledger-${randomUUID()}.tmp`);
565
+ let archiveCreated = false;
566
+ try {
567
+ const live = await openPrivateLedgerFile(ledgerPath, fail, fsConstants.O_RDONLY | fsConstants.O_CREAT);
568
+ if (!live)
569
+ throw new Error('Cannot read decision ledger');
570
+ let content;
571
+ try {
572
+ content = await live.readFile();
573
+ }
574
+ finally {
575
+ await live.close();
576
+ }
577
+ const lines = [];
578
+ for (let start = 0; start < content.length;) {
579
+ const newline = content.indexOf(0x0a, start);
580
+ const end = newline < 0 ? content.length : newline + 1;
581
+ const bytes = content.subarray(start, end);
582
+ let timestamp = null;
583
+ try {
584
+ const record = JSON.parse(bytes.toString('utf8'));
585
+ if (isDecisionLedgerRecord(record))
586
+ timestamp = Date.parse(record.timestamp);
587
+ }
588
+ catch { /* preserve malformed bytes */ }
589
+ lines.push({ bytes, timestamp });
590
+ start = end;
591
+ }
592
+ const valid = lines.filter(line => line.timestamp !== null).sort((a, b) => a.timestamp - b.timestamp);
593
+ const recent = new Set(retention.count === undefined ? [] : valid.slice(-retention.count));
594
+ const kept = [], archived = [];
595
+ for (const line of lines) {
596
+ const keep = !options.all && (line.timestamp === null || (retention.count !== undefined
597
+ ? recent.has(line) : line.timestamp >= now - retention.ms));
598
+ (keep ? kept : archived).push(line.bytes);
599
+ }
600
+ const result = { archived: archived.length, retained: kept.length,
601
+ malformed: lines.length - valid.length, archivePath: archived.length ? archivePath : null };
602
+ if (!archived.length)
603
+ return result;
604
+ await privateArchiveDirectory(artifactRoot, dirname(archivePath), true);
605
+ const archive = await open(archivePath, 'wx', LEDGER_FILE_MODE);
606
+ archiveCreated = true;
607
+ try {
608
+ await writeFully(archive, Buffer.concat(archived));
609
+ await archive.sync();
610
+ }
611
+ finally {
612
+ await archive.close();
613
+ }
614
+ if (!IS_WINDOWS)
615
+ await syncDirectory(dirname(archivePath));
616
+ const replacement = await open(tempPath, 'wx', LEDGER_FILE_MODE);
617
+ try {
618
+ await writeFully(replacement, Buffer.concat(kept));
619
+ await replacement.sync();
620
+ }
621
+ finally {
622
+ await replacement.close();
623
+ }
624
+ await rename(tempPath, ledgerPath);
625
+ archiveCreated = false; // Both files now own their respective records.
626
+ if (!IS_WINDOWS)
627
+ await syncDirectory(decisionDir);
628
+ return result;
629
+ }
630
+ catch (error) {
631
+ if (archiveCreated)
632
+ await rm(archivePath, { force: true }); // Live file is still intact.
633
+ throw error;
634
+ }
635
+ finally {
636
+ try {
637
+ await rm(tempPath, { force: true });
638
+ }
639
+ finally {
640
+ await release();
641
+ }
483
642
  }
484
643
  }
485
644
  /** Never throws: a provider-detection failure (missing config, unreadable capability matrix, …) degrades to "unknown". */
@@ -723,20 +882,57 @@ async function* streamDecisionLedgerLines(path) {
723
882
  rl.close();
724
883
  }
725
884
  }
726
- /** Streams parsed-and-validated records one at a time; invalid lines yield `null` so callers can count them as skipped. */
727
- async function* streamDecisionLedgerRecords(projectRoot) {
728
- for await (const line of streamDecisionLedgerLines(ledgerReadPath(projectRoot))) {
729
- const trimmed = line.trim();
730
- if (!trimmed)
731
- continue;
732
- try {
733
- const parsed = JSON.parse(trimmed);
734
- yield isDecisionLedgerRecord(parsed) ? parsed : null;
885
+ async function archiveLedgerPaths(artifactRoot, directory = join(artifactRoot, 'archive', 'decision')) {
886
+ if (!await privateArchiveDirectory(artifactRoot, directory, false))
887
+ return [];
888
+ const paths = [];
889
+ for (const entry of await readdir(directory, { withFileTypes: true })) {
890
+ const target = join(directory, entry.name);
891
+ if (entry.isSymbolicLink())
892
+ throw new Error(`Unsafe decision archive symlink: ${target}`);
893
+ if (entry.isDirectory())
894
+ paths.push(...await archiveLedgerPaths(artifactRoot, target));
895
+ else if (entry.name.endsWith('.jsonl')) {
896
+ const handle = await openPrivateLedgerFile(target, message => { throw new Error(message); }, fsConstants.O_RDONLY);
897
+ if (!handle)
898
+ throw new Error(`Unsafe decision archive: ${target}`);
899
+ await handle.close();
900
+ paths.push(target);
735
901
  }
736
- catch {
737
- yield null;
902
+ }
903
+ return paths.sort();
904
+ }
905
+ /** Streams validated records under the rotation lock; invalid lines yield null. */
906
+ async function* streamDecisionLedgerRecords(projectRoot, includeArchives = false) {
907
+ const livePath = ledgerReadPath(projectRoot);
908
+ const decisionDir = dirname(livePath);
909
+ if (!existsSync(decisionDir))
910
+ return;
911
+ if (!(await lstat(decisionDir)).isDirectory())
912
+ throw new Error('Decision ledger directory must be a real directory');
913
+ // A single lock also prevents readers from observing the archive and live
914
+ // replacement at different points in a rotation (duplicates or omissions).
915
+ const release = await lockDecisionLedger(decisionDir);
916
+ try {
917
+ const paths = includeArchives ? [...await archiveLedgerPaths(projectAiwgPath(projectRoot)), livePath] : [livePath];
918
+ for (const path of paths) {
919
+ for await (const line of streamDecisionLedgerLines(path)) {
920
+ const trimmed = line.trim();
921
+ if (!trimmed)
922
+ continue;
923
+ try {
924
+ const parsed = JSON.parse(trimmed);
925
+ yield isDecisionLedgerRecord(parsed) ? parsed : null;
926
+ }
927
+ catch {
928
+ yield null;
929
+ }
930
+ }
738
931
  }
739
932
  }
933
+ finally {
934
+ await release();
935
+ }
740
936
  }
741
937
  function matchesFilters(record, filters) {
742
938
  if (filters.since && new Date(record.timestamp).getTime() < filters.since.getTime())
@@ -765,7 +961,7 @@ export function filterDecisionLedgerRecords(records, filters) {
765
961
  export async function decisionLedgerHistory(projectRoot, filters = {}) {
766
962
  const records = [];
767
963
  let skipped = 0;
768
- for await (const record of streamDecisionLedgerRecords(projectRoot)) {
964
+ for await (const record of streamDecisionLedgerRecords(projectRoot, filters.includeArchives)) {
769
965
  if (!record) {
770
966
  skipped += 1;
771
967
  continue;
@@ -897,7 +1093,7 @@ export async function decisionLedgerStats(projectRoot, filters = {}) {
897
1093
  const weights = decisionSavingsWeights(config?.decision?.ask);
898
1094
  const groups = new Map();
899
1095
  let skipped = 0;
900
- for await (const record of streamDecisionLedgerRecords(projectRoot)) {
1096
+ for await (const record of streamDecisionLedgerRecords(projectRoot, filters.includeArchives)) {
901
1097
  if (!record) {
902
1098
  skipped += 1;
903
1099
  continue;