@aiwg/cli 2026.10.15 → 2026.10.16

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 (180) hide show
  1. package/agentic/code/providers/pi/aiwg-bridge.ts +45 -7
  2. package/dist/src/artifacts/citation-parser.js +7 -6
  3. package/dist/src/artifacts/corpus-tools/citation-densify.js +4 -2
  4. package/dist/src/artifacts/corpus-tools/cli.js +8 -3
  5. package/dist/src/artifacts/corpus-tools/discovery-log.js +26 -24
  6. package/dist/src/artifacts/corpus-tools/expand-cli.js +3 -2
  7. package/dist/src/artifacts/corpus-tools/expand-dates.js +107 -0
  8. package/dist/src/artifacts/corpus-tools/expand-queue.js +4 -1
  9. package/dist/src/artifacts/corpus-tools/expand-review.js +104 -0
  10. package/dist/src/artifacts/corpus-tools/expand-state.js +17 -1
  11. package/dist/src/artifacts/corpus-tools/expand.js +16 -17
  12. package/dist/src/artifacts/corpus-tools/induction-audit.js +7 -6
  13. package/dist/src/artifacts/corpus-tools/integrity-scan.js +3 -3
  14. package/dist/src/artifacts/corpus-tools/radar-shared.js +7 -9
  15. package/dist/src/artifacts/corpus-tools/ref-files.js +11 -0
  16. package/dist/src/artifacts/corpus-tools/resolve-cli.js +11 -3
  17. package/dist/src/artifacts/corpus-tools/resolve.js +23 -9
  18. package/dist/src/artifacts/corpus-tools/retrieval-lab.js +2 -1
  19. package/dist/src/artifacts/corpus-tools/sidecar-lint.js +4 -4
  20. package/dist/src/artifacts/corpus-tools/sidecar-repair.js +2 -1
  21. package/dist/src/artifacts/corpus-views/ref-parser.js +3 -17
  22. package/dist/src/artifacts/graph-query.js +5 -3
  23. package/dist/src/artifacts/hybrid-query.js +11 -2
  24. package/dist/src/artifacts/index-refresh.js +2 -0
  25. package/dist/src/cli/handlers/daemon.js +17 -1
  26. package/dist/src/cli/handlers/doctor-args.js +10 -5
  27. package/dist/src/cli/handlers/doctor-help.js +4 -1
  28. package/dist/src/cli/handlers/index.js +2 -2
  29. package/dist/src/cli/handlers/models-resource-sample.js +10 -0
  30. package/dist/src/cli/handlers/models.js +3 -2
  31. package/dist/src/cli/handlers/regenerate.js +8 -16
  32. package/dist/src/cli/handlers/sessions.js +7 -7
  33. package/dist/src/cli/handlers/subcommands.js +8 -0
  34. package/dist/src/cli/handlers/update.js +157 -0
  35. package/dist/src/cli/handlers/use.js +14 -4
  36. package/dist/src/cli/handlers/utilities.js +3 -560
  37. package/dist/src/cli/handlers/utility-doctor.js +412 -0
  38. package/dist/src/cli/services/deployment-verification.js +0 -1
  39. package/dist/src/config/aiwg-config.js +2 -1
  40. package/dist/src/config/authorization-cli.js +7 -3
  41. package/dist/src/config/dispatch-thresholds-runtime.d.mts +3 -2
  42. package/dist/src/config/dispatch-thresholds-runtime.mjs +17 -7
  43. package/dist/src/config/framework-aliases.js +12 -0
  44. package/dist/src/config/host-resource-report.d.mts +19 -0
  45. package/dist/src/config/host-resource-report.mjs +80 -0
  46. package/dist/src/config/host-resource-runtime.d.mts +34 -0
  47. package/dist/src/config/host-resource-runtime.mjs +293 -0
  48. package/dist/src/config/installed-selection.js +22 -0
  49. package/dist/src/config/worker-invocation-marker.d.mts +1 -0
  50. package/dist/src/config/worker-invocation-marker.mjs +2 -0
  51. package/dist/src/decision/heldout/access.js +74 -0
  52. package/dist/src/decision/heldout/calibration.js +1 -0
  53. package/dist/src/decision/heldout/collector.js +8 -1
  54. package/dist/src/effects/verifiers/file.js +22 -5
  55. package/dist/src/extensions/claude-attribution.js +82 -0
  56. package/dist/src/extensions/claude-hooks-installer.js +115 -69
  57. package/dist/src/extensions/commands/daemon.js +109 -0
  58. package/dist/src/extensions/commands/definitions.js +5 -89
  59. package/dist/src/gates/heldout.js +17 -7
  60. package/dist/src/lint/check-patterns.js +163 -0
  61. package/dist/src/lint/checks.js +268 -0
  62. package/dist/src/lint/reference-resolution.js +54 -0
  63. package/dist/src/lint/runner.js +3 -465
  64. package/dist/src/mcp/helpers.mjs +11 -1
  65. package/dist/src/memory/project-registry.js +44 -4
  66. package/dist/src/models/worker-plan.js +36 -5
  67. package/dist/src/models/wrapper-deployment.js +25 -12
  68. package/dist/src/models/wrapper-enforcement.js +47 -7
  69. package/dist/src/network-analysis/research.js +2 -1
  70. package/dist/src/policy/hitl-check.js +14 -4
  71. package/dist/src/policy/hitl-consume.js +54 -0
  72. package/dist/src/policy/hitl-receipt.js +2 -0
  73. package/dist/src/policy/hitl.js +11 -3
  74. package/dist/src/providers/provider-context-contracts.js +162 -0
  75. package/dist/src/providers/provider-definition-schema.js +136 -0
  76. package/dist/src/providers/provider-definitions.js +8 -1298
  77. package/dist/src/providers/provider-seeds-1.js +360 -0
  78. package/dist/src/providers/provider-seeds-2.js +387 -0
  79. package/dist/src/providers/provider-seeds-3.js +262 -0
  80. package/dist/src/research/clients/arxiv.js +4 -30
  81. package/dist/src/research/clients/base.js +48 -22
  82. package/dist/src/research/identity.js +34 -2
  83. package/dist/src/research/query-cli.js +3 -87
  84. package/dist/src/research/query-options.js +87 -0
  85. package/dist/src/research/reference-match.js +70 -24
  86. package/dist/src/research/services/acquisition.js +9 -7
  87. package/dist/src/research/services/citation.js +5 -2
  88. package/dist/src/research/types.js +2 -0
  89. package/dist/src/serve/mission-conductor.js +48 -4
  90. package/dist/src/sessions/adapters/grok-build-native.js +23 -0
  91. package/dist/src/sessions/adapters/grok-build.js +14 -7
  92. package/dist/src/sessions/workspace-discovery.js +5 -5
  93. package/dist/src/smiths/context-pipeline/aiwg-md.js +16 -46
  94. package/dist/src/smiths/context-pipeline/claude-hook.js +1 -1
  95. package/dist/src/smiths/context-pipeline/code-regions.js +28 -0
  96. package/dist/src/smiths/context-pipeline/finalization.js +81 -51
  97. package/dist/src/smiths/context-pipeline/generator.js +9 -10
  98. package/dist/src/smiths/context-pipeline/managed-hook.js +1 -1
  99. package/dist/src/smiths/context-pipeline/muse-bridge.js +2 -2
  100. package/dist/src/smiths/context-pipeline/provider-policy.js +6 -12
  101. package/dist/src/smiths/context-pipeline/workspace-audit.js +299 -0
  102. package/dist/src/smiths/context-pipeline/workspace-bootstrap.js +148 -0
  103. package/dist/src/smiths/context-pipeline/workspace-context-shared.js +111 -0
  104. package/dist/src/smiths/context-pipeline/workspace-context.js +8 -1373
  105. package/dist/src/smiths/context-pipeline/workspace-doctor.js +192 -0
  106. package/dist/src/smiths/context-pipeline/workspace-document.js +201 -0
  107. package/dist/src/smiths/context-pipeline/workspace-extraction.js +205 -0
  108. package/dist/src/smiths/context-pipeline/workspace-migration.js +250 -0
  109. package/package.json +10 -1
  110. package/schemas/decision/D29Study.v10.schema.json +65 -0
  111. package/tools/agents/deploy-agents.mjs +10 -0
  112. package/tools/agents/providers/claude-post-deploy.mjs +12 -1
  113. package/tools/agents/providers/context-write.mjs +4 -0
  114. package/tools/agents/providers/pi.mjs +3 -1
  115. package/tools/agents/providers/rule-delivery-surface.mjs +332 -0
  116. package/tools/agents/providers/rule-delivery.mjs +20 -1144
  117. package/tools/agents/providers/rule-deployment-commit.mjs +144 -0
  118. package/tools/agents/providers/rule-deployment-prepare.mjs +452 -0
  119. package/tools/agents/providers/rule-deployment-state.mjs +300 -0
  120. package/tools/daemon/README.md +511 -0
  121. package/tools/daemon/adapters/base-adapter.mjs +84 -0
  122. package/tools/daemon/adapters/webhook-adapter.mjs +107 -0
  123. package/tools/daemon/agent-supervisor.mjs +299 -0
  124. package/tools/daemon/automation-engine.mjs +310 -0
  125. package/tools/daemon/autonomous-engine.mjs +346 -0
  126. package/tools/daemon/behavior-loader.mjs +316 -0
  127. package/tools/daemon/behavior.mjs +174 -0
  128. package/tools/daemon/concierge/intent-router.mjs +401 -0
  129. package/tools/daemon/concierge/orchestrator.mjs +272 -0
  130. package/tools/daemon/concierge/response-translator.mjs +462 -0
  131. package/tools/daemon/config.mjs +377 -0
  132. package/tools/daemon/cron-scheduler.mjs +178 -0
  133. package/tools/daemon/daemon-main.mjs +1169 -0
  134. package/tools/daemon/event-router.mjs +84 -0
  135. package/tools/daemon/file-watcher.mjs +383 -0
  136. package/tools/daemon/index.mjs +1181 -0
  137. package/tools/daemon/init-profile.mjs +124 -0
  138. package/tools/daemon/ipc-client.mjs +197 -0
  139. package/tools/daemon/ipc-ops-handlers.mjs +236 -0
  140. package/tools/daemon/ipc-server.mjs +275 -0
  141. package/tools/daemon/maintenance-owner.mjs +79 -0
  142. package/tools/daemon/memory-manager.mjs +386 -0
  143. package/tools/daemon/message-router.mjs +194 -0
  144. package/tools/daemon/provider-watcher.mjs +585 -0
  145. package/tools/daemon/pty-adapter.mjs +420 -0
  146. package/tools/daemon/repl-chat.mjs +359 -0
  147. package/tools/daemon/resource-bounds.mjs +88 -0
  148. package/tools/daemon/sandbox-transport.mjs +295 -0
  149. package/tools/daemon/scheduled-task-runner.mjs +535 -0
  150. package/tools/daemon/task-store.mjs +450 -0
  151. package/tools/daemon/tmux-manager.mjs +324 -0
  152. package/tools/daemon/update-policy.mjs +517 -0
  153. package/tools/daemon/web-server.mjs +399 -0
  154. package/tools/daemon/web-ui/index.html +585 -0
  155. package/tools/daemon/web-ui/styles.css +156 -0
  156. package/tools/docker/Dockerfile.daemon +35 -0
  157. package/tools/docker/container-manager.mjs +291 -0
  158. package/tools/messaging/adapter-registry.mjs +230 -0
  159. package/tools/messaging/adapters/base.mjs +326 -0
  160. package/tools/messaging/adapters/discord.mjs +462 -0
  161. package/tools/messaging/adapters/slack.mjs +402 -0
  162. package/tools/messaging/adapters/telegram.mjs +552 -0
  163. package/tools/messaging/chat-handler.mjs +297 -0
  164. package/tools/messaging/command-router.mjs +269 -0
  165. package/tools/messaging/event-bus.mjs +199 -0
  166. package/tools/messaging/index.mjs +456 -0
  167. package/tools/messaging/message-formatter.mjs +320 -0
  168. package/tools/messaging/room-manager.mjs +277 -0
  169. package/tools/messaging/types.mjs +94 -0
  170. package/tools/providers/pi-bridge-policy.mjs +4 -0
  171. package/tools/ralph-external/daemon-supervisor.mjs +394 -0
  172. package/tools/ralph-external/lib/behavior-registry.mjs +270 -0
  173. package/tools/ralph-external/lib/worker-invocation-processes.mjs +3 -2
  174. package/tools/ralph-external/lib/worker-isolation-args.mjs +217 -0
  175. package/tools/ralph-external/lib/worker-isolation-status.mjs +19 -0
  176. package/tools/ralph-external/lib/worker-isolation.mjs +11 -237
  177. package/tools/ralph-external/lib/worker-overflow.mjs +10 -2
  178. package/tools/ralph-external/lib/worker-unit-cleanup.mjs +72 -0
  179. package/tools/ralph-external/session-launcher.mjs +7 -7
  180. package/tools/scripts/resource-preflight.mjs +3 -242
@@ -1,22 +1,27 @@
1
1
  /** AIWG's reviewed Pi bridge. Loaded only after Pi project trust is granted. */
2
2
  import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
3
3
  import { execFile } from 'node:child_process';
4
+ import { createHash } from 'node:crypto';
4
5
  import { mkdtemp, rm, writeFile } from 'node:fs/promises';
5
- import { tmpdir } from 'node:os';
6
+ import { tmpdir, userInfo } from 'node:os';
6
7
  import { basename, join } from 'node:path';
7
8
  import { promisify } from 'node:util';
9
+ // Deployment binds this import to the installed implementation, including packed dist/src.
10
+ import { loadHitlLayers } from '../../../../tools/providers/pi-bridge-policy.mjs';
8
11
 
9
12
  const execute = promisify(execFile);
10
- type Action = { category: string; repo: string; environment: string; resources: string[]; reversible: boolean; impact: 'low' | 'medium' | 'high' };
11
- type Check = { decision: string; receipt?: { file: string; effectId: string } };
13
+ type Action = { category: string; repo: string; environment: string; resources: string[]; reversible: boolean; impact: 'low' | 'medium' | 'high'; actor?: string; inputDigest?: string };
14
+ type Receipt = { file: string; effectId: string };
15
+ type Check = { decision: string; reason?: string; receipt?: Receipt };
12
16
 
13
17
  /** Deployed bridges call the installed CLI, never import checkout-only source paths. */
14
- export async function checkAiwgAuthorization(projectDir: string, action: Action): Promise<Check> {
18
+ export async function checkAiwgAuthorization(projectDir: string, action: Action, receipt?: Receipt): Promise<Check> {
15
19
  const temporary = await mkdtemp(join(tmpdir(), 'aiwg-pi-authorization-'));
16
20
  try {
17
21
  const file = join(temporary, 'action.json');
18
22
  await writeFile(file, JSON.stringify(action), { mode: 0o600 });
19
- const result = await execute('aiwg', ['config', 'authorization', 'check', '--action-file', file, '--target', projectDir],
23
+ const result = await execute('aiwg', ['config', 'authorization', 'check', '--action-file', file, '--target', projectDir,
24
+ ...(receipt ? ['--receipt-file', receipt.file, '--effect-id', receipt.effectId] : [])],
20
25
  { cwd: projectDir, timeout: 15_000, maxBuffer: 64 * 1024 });
21
26
  return JSON.parse(result.stdout) as Check;
22
27
  } catch { return { decision: 'escalate' }; }
@@ -26,18 +31,37 @@ export async function checkAiwgAuthorization(projectDir: string, action: Action)
26
31
  /** Host-only descriptors and callbacks; tool/issue/model payloads cannot supply these authority fields. */
27
32
  export async function evaluateAiwgPiAction(projectDir: string, action: Action, hasUI: boolean,
28
33
  confirm?: () => Promise<boolean>, check = checkAiwgAuthorization,
34
+ verify = checkAiwgAuthorization,
29
35
  ): Promise<{ block: true; reason: string } | undefined> {
30
36
  let result: Check;
31
37
  try { result = await check(projectDir, action); } catch { result = { decision: 'escalate' }; }
32
- if (result.decision === 'auto-authorized' && result.receipt?.file && result.receipt.effectId) return undefined;
38
+ if (result.decision === 'auto-authorized' && result.receipt?.file && result.receipt.effectId) {
39
+ try {
40
+ const consumed = await verify(projectDir, action, result.receipt);
41
+ if (consumed.decision === 'auto-authorized' && consumed.receipt) return undefined;
42
+ return { block: true, reason: consumed.reason ?? 'approval-unverified' };
43
+ } catch { return { block: true, reason: 'approval-unverified' }; }
44
+ }
33
45
  if (result.decision === 'deny') return { block: true, reason: 'Denied by AIWG operator policy.' };
34
- if (!hasUI) return { block: true, reason: 'AIWG policy blocked a destructive or package-mutating command in headless mode.' };
46
+ if (!hasUI) return { block: true, reason: action.category === 'repo.edit'
47
+ ? "This edit needs approval under the project's AIWG policy in headless mode."
48
+ : 'AIWG policy blocked a destructive or package-mutating command in headless mode.' };
35
49
  return await confirm?.() ? undefined : { block: true, reason: 'Denied by AIWG operator policy.' };
36
50
  }
37
51
 
38
52
  const destructive = /(?:^|[;&|]\s*)(?:sudo\s+)?(?:rm\s+-\S*r\S*\s+|git\s+(?:reset\s+--hard|clean\s+-)|chmod\s+777|chown\s+-R)/i;
39
53
  const packageMutation = /(?:^|[;&|]\s*)(?:npm|pnpm|yarn|bun|pipx?|uv|cargo)\s+(?:install|add|update|upgrade)\b/i;
40
54
 
55
+ /** Bind the native tool name and every input field; strings retain their exact bytes. */
56
+ export function describeAiwgPiEdit(projectDir: string, toolName: string, input: Record<string, unknown>): Action {
57
+ const canonical = (value: unknown): unknown => Array.isArray(value) ? value.map(canonical)
58
+ : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
59
+ .map(([key, item]) => [key, canonical(item)])) : value;
60
+ return { category: 'repo.edit', repo: basename(projectDir), environment: 'local',
61
+ resources: typeof input.path === 'string' ? [input.path] : [], reversible: true, impact: 'low',
62
+ actor: `pi:${userInfo().username}`, inputDigest: `sha256:${createHash('sha256').update(JSON.stringify(canonical({ toolName, input }))).digest('hex')}` };
63
+ }
64
+
41
65
  export async function evaluateAiwgPiCommand(
42
66
  command: string,
43
67
  hasUI: boolean,
@@ -59,6 +83,20 @@ export async function evaluateAiwgPiCommand(
59
83
 
60
84
  export default function aiwgBridge(pi: ExtensionAPI): void {
61
85
  pi.on('tool_call', async (event, context) => {
86
+ if (event.toolName === 'edit' || event.toolName === 'write') {
87
+ try {
88
+ // Reuse the check's canonical ancestor/member resolution, without a subprocess or cache.
89
+ if (!(await loadHitlLayers(context.cwd)).layers.some(layer => layer.policy?.enabled)) return undefined;
90
+ } catch { return { block: true, reason: 'authorization-check-unavailable' }; }
91
+ let action: Action;
92
+ // The host identity is part of the approval; without it, fail closed instead of throwing.
93
+ try { action = describeAiwgPiEdit(context.cwd, event.toolName, event.input); }
94
+ catch { return { block: true, reason: 'authorization-check-unavailable' }; }
95
+ return evaluateAiwgPiAction(context.cwd, action, context.hasUI, async () => {
96
+ const choice = await context.ui.select('AIWG policy requires explicit approval for this edit.', ['Deny', 'Allow once']);
97
+ return choice === 'Allow once';
98
+ });
99
+ }
62
100
  if (event.toolName !== 'bash') return undefined;
63
101
  const command = typeof event.input?.command === 'string' ? event.input.command : '';
64
102
  return evaluateAiwgPiCommand(command, context.hasUI, async () => {
@@ -21,19 +21,20 @@
21
21
  * @source @src/artifacts/types.ts
22
22
  * @tests @test/unit/artifacts/citation-parser.test.ts
23
23
  */
24
+ import { isRefId, REF_ID_PATTERN } from '../research/identity.js';
24
25
  import { parseFrontmatter } from './index-builder.js';
25
26
  import { CITATION_HEADER_ALIASES, CITATION_SECTION_ALIASES } from './citation-table-schema.js';
26
27
  /**
27
28
  * Match a single node identifier (REF-* or PROF-*) anywhere in a string.
28
29
  * Used by `extractRefsFromTable` to pull every ID out of a table cell.
29
30
  */
30
- const NODE_ID_PATTERN = /(?:REF-\d+[a-z]?|PROF-[POFG]-[a-z0-9-]+)/g;
31
+ const NODE_ID_PATTERN = new RegExp(`(?:${REF_ID_PATTERN}|PROF-[POFG]-[a-z0-9-]+)`, 'g');
31
32
  /**
32
33
  * Validate that a string is a complete node identifier.
33
34
  * Used by `parseCitationSidecar` and `buildRefToPathMap` to gate
34
35
  * frontmatter `ref` values.
35
36
  */
36
- const NODE_ID_FULL = /^(?:REF-\d+[a-z]?|PROF-[POFG]-[a-z0-9-]+)$/;
37
+ const PROFILE_ID_FULL = /^PROF-[POFG]-[a-z0-9-]+$/;
37
38
  /** An unnamed resolution cell must contain only one REF, optionally decorated. */
38
39
  function standaloneRef(value) {
39
40
  let text = value.trim();
@@ -43,7 +44,7 @@ function standaloneRef(value) {
43
44
  const code = text.match(/^`([^`]+)`$/);
44
45
  if (code)
45
46
  text = code[1].trim();
46
- return /^REF-\d+[a-z]?$/.test(text) ? text : null;
47
+ return isRefId(text) ? text : null;
47
48
  }
48
49
  /** Resolution aliases are shared by every registered bibliographic form. */
49
50
  const CITATION_REF_COLUMNS = CITATION_HEADER_ALIASES.ref;
@@ -115,7 +116,7 @@ function isNonCitationContext(value) {
115
116
  * match the four-letter type code form).
116
117
  */
117
118
  export function isNodeId(value) {
118
- return typeof value === 'string' && NODE_ID_FULL.test(value);
119
+ return typeof value === 'string' && (isRefId(value) || PROFILE_ID_FULL.test(value));
119
120
  }
120
121
  /**
121
122
  * Extract node identifiers from a markdown table column.
@@ -237,7 +238,7 @@ export function parseCitationSidecar(content) {
237
238
  const { data, body } = parseFrontmatter(content);
238
239
  // Must have a node identifier in frontmatter (REF-* or PROF-*)
239
240
  const ref = typeof data.ref === 'string' ? data.ref : null;
240
- if (!ref || !NODE_ID_FULL.test(ref))
241
+ if (!ref || !isNodeId(ref))
241
242
  return null;
242
243
  // ATX level-two headings allow indentation, tabs and closing hashes.
243
244
  const sectionHeading = /^ {0,3}##[ \t]+([^\r\n]+)/m;
@@ -302,7 +303,7 @@ export function buildRefToPathMap(entries) {
302
303
  const map = new Map();
303
304
  for (const [filePath, data] of entries) {
304
305
  const ref = typeof data.ref === 'string' ? data.ref : null;
305
- if (ref && NODE_ID_FULL.test(ref)) {
306
+ if (ref && isNodeId(ref)) {
306
307
  map.set(ref, filePath);
307
308
  }
308
309
  }
@@ -18,11 +18,13 @@
18
18
  * @tests @test/unit/artifacts/citation-densify.test.ts
19
19
  */
20
20
  import * as fs from 'fs';
21
+ import { REF_ID_PATTERN } from '../../research/identity.js';
22
+ import { refFilesForId } from './ref-files.js';
21
23
  import * as path from 'path';
22
24
  import { refTitle } from './sidecar-lint.js';
23
25
  const CITES_DIR = ['documentation', 'citations'];
24
26
  const REFS_DIR = ['documentation', 'references'];
25
- const REF_ID_RE = /\bREF-\d+[a-z]?\b/g;
27
+ const REF_ID_RE = new RegExp(REF_ID_PATTERN, 'g');
26
28
  function citesDir(root) {
27
29
  return path.join(root, ...CITES_DIR);
28
30
  }
@@ -45,7 +47,7 @@ function analysisDoc(root, ref) {
45
47
  const dir = path.join(root, ...REFS_DIR);
46
48
  if (!fs.existsSync(dir))
47
49
  return null;
48
- const m = fs.readdirSync(dir).find((f) => f === `${ref}.md` || f.startsWith(`${ref}-`));
50
+ const m = refFilesForId(dir, ref)[0];
49
51
  return m ? path.join(dir, m) : null;
50
52
  }
51
53
  /** Edge tokens in an analysis doc's cross-reference-style sections (port of extract_crossref_refs). */
@@ -39,6 +39,7 @@ import { extractPages, renderExtract, resolveAdapter, rasterizePdf } from './vis
39
39
  import { runSnapshot } from './snapshot.js';
40
40
  import { resolveCli, RESOLVE_USAGE } from './resolve-cli.js';
41
41
  import { expandCli, EXPAND_USAGE } from './expand-cli.js';
42
+ import { reviewCli, REVIEW_USAGE } from './expand-review.js';
42
43
  import { exitCodeFor, isAiwgError } from '../../cli/errors.js';
43
44
  function flagValue(args, name) {
44
45
  const i = args.indexOf(name);
@@ -51,6 +52,7 @@ const HELP = `aiwg corpus — research-corpus tools
51
52
 
52
53
  Usage:
53
54
  aiwg corpus expand [--seeds REF-a,REF-b | --since YYYY-MM-DD | --all] [--per-seed N] [--from recent|all-cs] [--min-year YYYY] [--reexplore-after 180d] [--max-seeds N] [--overlap] [--write] [--json]
55
+ aiwg corpus review [list | accept <id> [--brief] | reject <id> [--reason TEXT] | defer <id>] [--write] [--json]
54
56
  aiwg corpus resolve "<title | URL | arXiv ID | DOI | bibliography line>" [--year YYYY] [--json]
55
57
  aiwg corpus resolve --from-file refs.txt [--year YYYY] [--json]
56
58
  aiwg corpus radar-init --ref REF-XXX [--cadence C] [--cluster T] [--write]
@@ -81,8 +83,9 @@ Usage:
81
83
  aiwg corpus retrieval-lab --queries PATH --concepts PATH [--expected-scheme-hash SHA256] [--limit K] [--latency-ceiling MS] [--json] [--out PATH]
82
84
  aiwg corpus vision-extract --slug SLUG [--images DIR] [--out DIR] [--start N] [--end N] [--provider codex|command] [--command TMPL] [--model M] [--retries N] [--title T] [--force] [--rasterize PDF] [--dpi N]
83
85
 
84
- resolve verifies paper identities, refuses ambiguous matches (exit 2) and reports unresolved references (exit 3).
86
+ resolve verifies paper identities, refuses ambiguous matches (exit 2) and reports unresolved references (exit 3) or deferred incomplete lookups (exit 4; retry).
85
87
  expand queues similar papers by convergence and recency (dry-run unless --write; never inducts or files issues).
88
+ review lists saved candidates and previews accept/reject/defer (persist with --write; optional accept --brief).
86
89
  radar-init scaffolds radar sidecars (dry-run unless --write; skips existing).
87
90
  radar-status reports overdue radars (most-overdue-first).
88
91
  radar-report aggregates corpus/cluster freshness.
@@ -263,14 +266,16 @@ export async function corpusMain(args, cwd = process.cwd()) {
263
266
  process.stdout.write(HELP);
264
267
  return;
265
268
  }
266
- if (sub === 'resolve' || sub === 'expand') {
269
+ if (sub === 'resolve' || sub === 'expand' || sub === 'review') {
267
270
  const json = hasFlag(rest, '--json');
268
271
  if (hasFlag(rest, '--help') || hasFlag(rest, '-h')) {
269
- const usage = sub === 'resolve' ? RESOLVE_USAGE : EXPAND_USAGE;
272
+ const usage = sub === 'resolve' ? RESOLVE_USAGE : sub === 'expand' ? EXPAND_USAGE : REVIEW_USAGE;
270
273
  process.stdout.write(json ? JSON.stringify({ usage }) + '\n' : usage);
271
274
  return;
272
275
  }
273
276
  try {
277
+ if (sub === 'review')
278
+ return await reviewCli(rest, cwd);
274
279
  const root = await resolveCorpusRoot(cwd);
275
280
  return await (sub === 'resolve' ? resolveCli(rest, root, cwd) : expandCli(rest, root, cwd));
276
281
  }
@@ -11,36 +11,39 @@
11
11
  */
12
12
  import * as fs from 'fs';
13
13
  import * as path from 'path';
14
+ import { stringify } from 'yaml';
14
15
  const SURFACE_VOCAB = ['x-account', 'x-search', 'x-bookmarks', 'x-foryou', 'x-following', 'rss', 'newsletter', 'web', 'referral', 'direct', 's2-recommendation'];
15
16
  /** Render the `discovery:` YAML block (2-space indented under the key). */
16
17
  export function renderDiscoveryBlock(f, today) {
17
- const lines = ['discovery:', ` date: ${f.date || today}`, ` surface: ${f.surface}`];
18
+ // Quote and escape strings for YAML 1.1 readers, including date-like values.
19
+ const scalar = (value, quoted = false) => stringify(value, {
20
+ schema: 'yaml-1.1',
21
+ defaultStringType: quoted ? 'QUOTE_DOUBLE' : 'PLAIN',
22
+ blockQuote: false,
23
+ doubleQuotedAsJSON: true,
24
+ lineWidth: 0,
25
+ }).trimEnd();
26
+ const lines = ['discovery:', ` date: ${scalar(f.date || today)}`, ` surface: ${scalar(f.surface)}`];
18
27
  if (f.via)
19
- lines.push(` via: "${f.via}"`);
20
- lines.push(` curator-id: ${f.curatorId || 'null'}`);
28
+ lines.push(` via: ${scalar(f.via, true)}`);
29
+ lines.push(` curator-id: ${scalar(f.curatorId || null)}`);
21
30
  if (f.harvestBatch)
22
- lines.push(` harvest-batch: ${f.harvestBatch}`);
31
+ lines.push(` harvest-batch: ${scalar(f.harvestBatch)}`);
23
32
  if (f.harvestedBy)
24
- lines.push(` harvested-by: ${f.harvestedBy}`);
33
+ lines.push(` harvested-by: ${scalar(f.harvestedBy)}`);
25
34
  return lines.join('\n');
26
35
  }
27
- /** Drop an existing top-level `discovery:` block (key + its indented lines) from frontmatter lines. */
28
- function stripDiscovery(fmLines) {
29
- const out = [];
30
- let skipping = false;
31
- for (const line of fmLines) {
32
- if (skipping) {
33
- if (/^\s/.test(line) || line.trim() === '')
34
- continue; // still inside the block's indented body
35
- skipping = false;
36
- }
37
- if (/^discovery:\s*$/.test(line)) {
38
- skipping = true;
39
- continue;
40
- }
41
- out.push(line);
42
- }
43
- return out;
36
+ /** Replace discovery in place, preserving surrounding frontmatter and blank lines. */
37
+ function spliceDiscovery(fmLines, block) {
38
+ const start = fmLines.findIndex(line => /^discovery:\s*(?:#.*)?$/.test(line));
39
+ if (start < 0)
40
+ return [...fmLines, block].join('\n');
41
+ let end = start + 1;
42
+ while (end < fmLines.length && (/^\s/.test(fmLines[end]) || fmLines[end] === ''))
43
+ end++;
44
+ while (end > start + 1 && fmLines[end - 1].trim() === '')
45
+ end--;
46
+ return [...fmLines.slice(0, start), block, ...fmLines.slice(end)].join('\n');
44
47
  }
45
48
  /** Add or replace the discovery block in REF-XXX-citations.md frontmatter. */
46
49
  export function logDiscovery(corpusRoot, refId, opts) {
@@ -58,8 +61,7 @@ export function logDiscovery(corpusRoot, refId, opts) {
58
61
  if (!m) {
59
62
  return { refId, status: 'skip', message: `SKIP ${refId}: citation sidecar has no YAML frontmatter`, block };
60
63
  }
61
- const fmLines = stripDiscovery(m[1].split('\n'));
62
- const newFm = [...fmLines, block].join('\n');
64
+ const newFm = spliceDiscovery(m[1].split('\n'), block);
63
65
  const content = `---\n${newFm}\n---\n${m[2]}`;
64
66
  if (!opts.write) {
65
67
  return { refId, status: 'dry-run', message: `DRY-RUN ${refId}: would set discovery (surface=${opts.surface}${opts.curatorId ? `, curator=${opts.curatorId}` : ''})`, block, content };
@@ -1,7 +1,8 @@
1
1
  // Validate expansion controls and expose the corpus router's JSON and exit-code contract.
2
+ import { isRefId } from '../../research/identity.js';
2
3
  import { AiwgError } from '../../cli/errors.js';
3
4
  import { expandCorpus, renderExpansion } from './expand.js';
4
- export const EXPAND_USAGE = 'aiwg corpus expand [--seeds REF-0001,REF-0002 | --since YYYY-MM-DD | --all] [--per-seed 20] [--from recent|all-cs] [--min-year 2018] [--reexplore-after 180d] [--max-seeds N] [--overlap] [--write] [--json]\nA scope (--seeds, --since or --all) is required. Default: dry run. --since uses induction/discovery date, then document mtime.\n--overlap checks at most 20 candidates, after convergence and recency.\nExit codes: 0 complete, 3 incomplete/retryable, 1 usage or storage error.\n';
5
+ export const EXPAND_USAGE = 'aiwg corpus expand [--seeds REF-0001,REF-0002 | --since YYYY-MM-DD | --all] [--per-seed 20] [--from recent|all-cs] [--min-year 2018] [--reexplore-after 180d] [--max-seeds N] [--overlap] [--write] [--json]\nA scope (--seeds, --since or --all) is required. Default: dry run. --since uses induction/discovery date (including citation sidecars), then Git first-add date, then document mtime with a summary warning.\n--overlap checks at most 20 candidates, after convergence and recency.\nExit codes: 0 complete, 3 incomplete/retryable, 1 usage or storage error.\n';
5
6
  function usage(message) {
6
7
  throw new AiwgError({ code: 'ERR_CORPUS_EXPAND_USAGE', message, exitCode: 1, hint: EXPAND_USAGE.trim() });
7
8
  }
@@ -47,7 +48,7 @@ export async function expandCli(args, root, cwd) {
47
48
  switch (flag) {
48
49
  case '--seeds':
49
50
  options.seeds = value.split(',').map(ref => ref.trim());
50
- if (options.seeds.some(ref => !/^REF-\d+[a-z]?$/.test(ref)))
51
+ if (options.seeds.some(ref => !isRefId(ref)))
51
52
  usage('--seeds requires comma-separated REF identifiers');
52
53
  options.seeds = [...new Set(options.seeds)];
53
54
  break;
@@ -0,0 +1,107 @@
1
+ // Resolve seed dates from frontmatter, batched Git addition history, then filesystem mtime.
2
+ import { statSync } from 'node:fs';
3
+ import { relative, resolve, sep } from 'node:path';
4
+ import { boundedExecFileSync } from '../../process/bounded-child-process.mjs';
5
+ function frontmatterDate(record) {
6
+ for (const fields of record.fields) {
7
+ const discovery = fields.discovery;
8
+ for (const value of [fields.inducted, fields.inducted_on, fields.added, fields.created, fields.date, discovery?.date]) {
9
+ const date = value instanceof Date ? value.getTime() : typeof value === 'string' ? Date.parse(value) : NaN;
10
+ if (Number.isFinite(date))
11
+ return date;
12
+ }
13
+ }
14
+ return undefined;
15
+ }
16
+ function gitAdditionDates(root, records) {
17
+ const dates = new Map();
18
+ let shallow = false;
19
+ const git = (cwd, args) => String(boundedExecFileSync('git', args, 'quick', {
20
+ cwd, encoding: 'utf8', timeout: 30_000, maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'],
21
+ }));
22
+ try {
23
+ const repository = git(root, ['rev-parse', '--show-toplevel']).trim();
24
+ shallow = git(repository, ['rev-parse', '--is-shallow-repository']).trim() === 'true';
25
+ const tracked = new Set(git(repository, ['ls-files', '-z']).split('\0'));
26
+ const pending = new Map();
27
+ for (const record of records) {
28
+ const path = relative(repository, resolve(record.path)).split(sep).join('/');
29
+ if (tracked.has(path))
30
+ pending.set(path, [record.path]);
31
+ }
32
+ if (!pending.size)
33
+ return { dates, shallow };
34
+ // Walk backwards through renames without a pathspec: predecessors may live outside the corpus.
35
+ // NUL-delimited paths also handle spaces, tabs, newlines and Git's quoted-path syntax.
36
+ // Combined diffs expose merge-created files (AA), but omit additions inherited from a
37
+ // parent: those resolve at their original branch commit, not at the merge date.
38
+ const tokens = git(repository, ['log', '--topo-order', '--full-history', '--diff-merges=combined',
39
+ '--format=%x00aiwg-date:%aI %P%x00',
40
+ '--name-status', '-z', '--find-renames', '--diff-filter=AR', '--']).split('\0');
41
+ let date = NaN;
42
+ for (let i = 0; i < tokens.length && pending.size; i++) {
43
+ const token = tokens[i].replace(/^\n+/, '');
44
+ if (token.startsWith('aiwg-date:')) {
45
+ const [authored, parents] = token.slice(10).split(' ');
46
+ // A shallow boundary masquerades as a root addition; its true date is unknown.
47
+ date = shallow && !parents ? NaN : Date.parse(authored);
48
+ continue;
49
+ }
50
+ if (/^A+$/.test(token)) {
51
+ const path = tokens[++i];
52
+ if (Number.isFinite(date))
53
+ for (const original of pending.get(path) ?? [])
54
+ dates.set(original, date);
55
+ pending.delete(path);
56
+ }
57
+ else if (/^R\d+$/.test(token)) {
58
+ const oldPath = tokens[++i];
59
+ const newPath = tokens[++i];
60
+ const originals = pending.get(newPath);
61
+ if (originals) {
62
+ pending.delete(newPath);
63
+ pending.set(oldPath, [...(pending.get(oldPath) ?? []), ...originals]);
64
+ }
65
+ }
66
+ }
67
+ }
68
+ catch {
69
+ // Missing Git, no repository, unavailable history or a bounded query failure use the same fallback.
70
+ return { dates: new Map(), shallow };
71
+ }
72
+ return { dates, shallow };
73
+ }
74
+ export function expansionDates(root, records) {
75
+ const dates = new Map();
76
+ const missing = [];
77
+ for (const record of records) {
78
+ const date = frontmatterDate(record);
79
+ if (date === undefined)
80
+ missing.push(record);
81
+ else
82
+ dates.set(record.path, date);
83
+ }
84
+ const { dates: additions, shallow } = missing.length ? gitAdditionDates(root, missing) : { dates: new Map(), shallow: false };
85
+ let mtimeCount = 0;
86
+ for (const record of records) {
87
+ try {
88
+ const mtime = statSync(record.path).mtimeMs;
89
+ if (dates.has(record.path))
90
+ continue;
91
+ const date = additions.get(record.path);
92
+ if (date === undefined)
93
+ mtimeCount++;
94
+ dates.set(record.path, date ?? mtime);
95
+ }
96
+ catch (error) {
97
+ if (!['ENOENT', 'ENOTDIR'].includes(error.code ?? ''))
98
+ throw error;
99
+ dates.delete(record.path);
100
+ console.warn(`[aiwg] corpus expand --since: ${record.ref} document disappeared; skipping seed (${record.path}).`);
101
+ }
102
+ }
103
+ if (mtimeCount)
104
+ console.warn(`[aiwg] corpus expand --since: ${mtimeCount} REFs used document mtime because no Git first-add date was available.${shallow ? ' Shallow Git history may omit first additions; use a full clone for stable --since scoping.' : ''}`);
105
+ return dates;
106
+ }
107
+ //# sourceMappingURL=expand-dates.js.map
@@ -15,6 +15,7 @@ export class ExpansionQueue {
15
15
  const keys = new Set([...identityKeys(paper), ...(previous?.identity_keys ?? [])]);
16
16
  const matches = new Set([...keys].flatMap(key => this.byKey.get(key) ? [this.byKey.get(key)] : []));
17
17
  const seeds = new Set(refs);
18
+ let review = previous?.review;
18
19
  let overlap = previous?.score_parts.overlap ?? null;
19
20
  for (const match of matches) {
20
21
  this.rows.delete(match);
@@ -23,10 +24,12 @@ export class ExpansionQueue {
23
24
  for (const ref of match.seed_refs)
24
25
  seeds.add(ref);
25
26
  paper = { ...match.paper, ...Object.fromEntries(Object.entries(paper).filter(([, value]) => value !== undefined)) };
27
+ if (match.review && (!review || match.review.reviewed_on > review.reviewed_on))
28
+ review = match.review;
26
29
  overlap ??= match.score_parts.overlap;
27
30
  }
28
31
  const seedRefs = [...seeds].sort();
29
- const row = { paper, identity_keys: [...keys].sort(), seed_refs: seedRefs,
32
+ const row = { ...(review ? { review } : {}), paper, identity_keys: [...keys].sort(), seed_refs: seedRefs,
30
33
  score_parts: { convergence: seedRefs.length, recency: paper.year || 0, overlap },
31
34
  discovery: { surface: 's2-recommendation', via: `S2 recommendations for ${seedRefs.join(', ')}` } };
32
35
  this.rows.add(row);
@@ -0,0 +1,104 @@
1
+ // Review persisted expansion identities without requesting papers or triggering induction.
2
+ import { AiwgError } from '../../cli/errors.js';
3
+ import { identityKeys, parseResearchIdentifier } from '../../research/identity.js';
4
+ import { compactState, expansionPaths, readJsonl, writeJsonl, writeQueue } from './expand-state.js';
5
+ import { renderExpansion } from './expand.js';
6
+ export const REVIEW_USAGE = `aiwg corpus review [list] [--json]
7
+ aiwg corpus review accept <id> [--brief] [--write] [--json]
8
+ aiwg corpus review reject <id> [--reason TEXT] [--write] [--json]
9
+ aiwg corpus review defer <id> [--write] [--json]
10
+ IDs: paper.id, full identity keys, bare S2 IDs, or complete DOI/arXiv/OpenReview/ACL/S2 aliases and URLs.
11
+ Default: dry run. Accept/defer retain the candidate with review metadata; reject records its identities in rejected-candidates.jsonl and removes it from the queue.
12
+ --brief emits an induct-research brief to stdout (JSON: brief); it never inducts or files issues.
13
+ Exit codes: 0 complete, 1 usage or storage error.
14
+ `;
15
+ function usage(message) {
16
+ throw new AiwgError({ code: 'ERR_CORPUS_REVIEW_USAGE', message, exitCode: 1, hint: REVIEW_USAGE.trim() });
17
+ }
18
+ function matches(row, id) {
19
+ const recognized = parseResearchIdentifier(id);
20
+ const parsed = recognized?.matched === id ? recognized : undefined;
21
+ const field = parsed && { arxiv: 'arxivId', doi: 'doi', s2: 's2PaperId', openreview: 'openReviewId', acl: 'aclId' }[parsed.kind];
22
+ const keys = new Set([id, ...identityKeys({ s2PaperId: id, ...(field ? { [field]: parsed.id } : {}) })]);
23
+ return row.paper.id === id || [...row.identity_keys, ...identityKeys(row.paper)]
24
+ .some(key => keys.has(key));
25
+ }
26
+ function brief(row) {
27
+ return ['# Induction brief for induct-research', '', `Title: ${row.paper.title}`,
28
+ `Paper ID: ${row.paper.id}`, `IDs: ${row.identity_keys.filter(key => !key.startsWith('title:')).join(', ')}`,
29
+ `Seed REFs: ${row.seed_refs.join(', ')}`,
30
+ `Score components: convergence=${row.score_parts.convergence}, recency=${row.score_parts.recency}, overlap=${row.score_parts.overlap ?? 'not checked'}`,
31
+ `Discovery: ${row.discovery.surface}; via: ${row.discovery.via}`, '',
32
+ 'Use induct-research only when explicitly requested. Preserve the seed REFs and discovery provenance in the citation sidecar.', ''].join('\n');
33
+ }
34
+ export async function reviewCli(args, cwd) {
35
+ const positional = [];
36
+ const seen = new Set();
37
+ let reason;
38
+ for (let i = 0; i < args.length; i++) {
39
+ const token = args[i];
40
+ if (!token.startsWith('-')) {
41
+ positional.push(token);
42
+ continue;
43
+ }
44
+ if (!['--json', '--write', '--brief', '--reason'].includes(token))
45
+ usage(`Unknown review option: ${token}`);
46
+ if (seen.has(token))
47
+ usage(`${token} may only be supplied once`);
48
+ seen.add(token);
49
+ if (token === '--reason') {
50
+ reason = args[++i];
51
+ if (!reason?.trim() || reason.startsWith('-'))
52
+ usage('--reason requires text');
53
+ }
54
+ }
55
+ const action = (positional[0] ?? 'list');
56
+ if (!['list', 'accept', 'reject', 'defer'].includes(action))
57
+ usage(`Unknown review action: ${action}`);
58
+ if (positional.length !== (action === 'list' ? (positional.length ? 1 : 0) : 2))
59
+ usage(`${action} ${action === 'list' ? 'takes no ID' : 'requires one ID'}`);
60
+ if (seen.has('--reason') && action !== 'reject')
61
+ usage('--reason is only valid with reject');
62
+ if (seen.has('--brief') && action !== 'accept')
63
+ usage('--brief is only valid with accept');
64
+ if (seen.has('--write') && action === 'list')
65
+ usage('--write requires accept, reject or defer');
66
+ const paths = expansionPaths(cwd);
67
+ const candidates = readJsonl(paths.queue);
68
+ // Validate all storage before mutating any file, including the ledger compacted on writes.
69
+ const rejected = readJsonl(paths.rejected);
70
+ readJsonl(paths.ledger);
71
+ let change;
72
+ if (action !== 'list') {
73
+ const found = candidates.filter(row => matches(row, positional[1]));
74
+ if (found.length !== 1)
75
+ usage(found.length ? `Ambiguous candidate ID: ${positional[1]}` : `Unknown candidate ID: ${positional[1]}`);
76
+ const before = found[0];
77
+ const after = { ...before, review: {
78
+ status: action === 'accept' ? 'accepted' : action === 'reject' ? 'rejected' : 'deferred',
79
+ reviewed_on: new Date().toISOString(), ...(reason !== undefined ? { reason } : {}),
80
+ } };
81
+ change = { before, after };
82
+ const index = candidates.indexOf(before);
83
+ if (action === 'reject')
84
+ candidates.splice(index, 1);
85
+ else
86
+ candidates[index] = after;
87
+ }
88
+ const result = { dry_run: !seen.has('--write'), action, candidates, ...(change ? { change } : {}),
89
+ ...(seen.has('--brief') ? { brief: brief(change.after) } : {}) };
90
+ const report = renderExpansion({ ...result, incomplete: false, scoped: 0, due: 0, remaining: 0,
91
+ processed: [], overlap_errors: [], paths });
92
+ if (seen.has('--write')) {
93
+ // Persist suppression first: a failed queue write cannot resurrect a rejected identity.
94
+ if (action === 'reject')
95
+ writeJsonl(paths.rejected, [...rejected, change.after]);
96
+ writeQueue(paths, candidates, report);
97
+ // Queue/report may already be replaced on compaction failure; expand reconciles
98
+ // from persisted suppression and retries compaction without resurrecting rejections.
99
+ compactState(paths);
100
+ }
101
+ const summary = change ? `${result.dry_run ? 'Dry run' : 'Written'}: ${action} ${change.after.paper.id}${reason ? ` (${reason})` : ''}\n` : '';
102
+ process.stdout.write(seen.has('--json') ? JSON.stringify(result, null, 2) + '\n' : summary + report + (result.brief ?? ''));
103
+ }
104
+ //# sourceMappingURL=expand-review.js.map
@@ -1,5 +1,5 @@
1
1
  // Read frontier/queue JSONL and checkpoint queue-before-ledger under the artifact path owner.
2
- import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  import { randomUUID } from 'node:crypto';
5
5
  import { resolveProjectAiwgDirForWrite } from '../../config/project-artifacts.js';
@@ -24,6 +24,21 @@ export function readJsonl(file) {
24
24
  }
25
25
  });
26
26
  }
27
+ export function writeJsonl(file, rows) {
28
+ const temporary = `${file}.${randomUUID()}.tmp`;
29
+ try {
30
+ writeFileSync(temporary, rows.map(row => JSON.stringify(row)).join('\n') + (rows.length ? '\n' : ''), 'utf8');
31
+ renameSync(temporary, file);
32
+ }
33
+ finally {
34
+ rmSync(temporary, { force: true });
35
+ }
36
+ }
37
+ export function compactState(paths) {
38
+ const rows = readJsonl(paths.ledger);
39
+ if (rows.length)
40
+ writeJsonl(paths.ledger, [...new Map(rows.map(row => [row.ref, row])).values()]);
41
+ }
27
42
  export function writeQueue(paths, rows, report) {
28
43
  mkdirSync(paths.directory, { recursive: true });
29
44
  for (const [file, content] of [[paths.queue, rows.map(row => JSON.stringify(row)).join('\n') + (rows.length ? '\n' : '')], [paths.report, report]]) {
@@ -36,5 +51,6 @@ export function writeQueue(paths, rows, report) {
36
51
  }
37
52
  export function appendState(paths, row) {
38
53
  appendFileSync(paths.ledger, JSON.stringify(row) + '\n', 'utf8');
54
+ compactState(paths);
39
55
  }
40
56
  //# sourceMappingURL=expand-state.js.map