@fitlab-ai/agent-infra 0.9.5 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/internal-cli.js +8 -3
- package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +171 -20
- package/dist/lib/agent-clients/adapters/codex-lifecycle/build-identity.js +162 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/capability-store.js +244 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +14 -2
- package/dist/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.js +370 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +68 -8
- package/dist/lib/agent-clients/adapters/codex.js +1 -1
- package/dist/lib/defaults.json +10 -2
- package/dist/lib/git/workflow.js +30 -4
- package/dist/lib/internal/codex-lifecycle.js +169 -3
- package/dist/lib/internal/codex-sandbox-controller.js +106 -0
- package/dist/lib/internal/release-workflow.js +108 -11
- package/dist/lib/internal/sandbox-control.js +43 -6
- package/dist/lib/internal/task-create.js +12 -4
- package/dist/lib/internal/task-orchestration.js +40 -7
- package/dist/lib/platform/context.js +6 -4
- package/dist/lib/platform/github-client.js +55 -2
- package/dist/lib/platform/issue-comments.js +2 -1
- package/dist/lib/process-data/checkpoint.js +319 -0
- package/dist/lib/process-data/index.js +128 -60
- package/dist/lib/process-data/materialize.js +84 -0
- package/dist/lib/process-data/normalize.js +34 -1
- package/dist/lib/process-data/reconcile.js +55 -1
- package/dist/lib/process-data/sources.js +422 -1
- package/dist/lib/process-data/store.js +341 -11
- package/dist/lib/release/workflow.js +29 -21
- package/dist/lib/sandbox/commands/create.js +111 -60
- package/dist/lib/sandbox/commands/prune.js +40 -16
- package/dist/lib/sandbox/commands/rm.js +372 -94
- package/dist/lib/sandbox/commands/start.js +4 -3
- package/dist/lib/sandbox/control/client.js +138 -22
- package/dist/lib/sandbox/control/container-identity.js +64 -0
- package/dist/lib/sandbox/control/executor.js +142 -0
- package/dist/lib/sandbox/control/lifecycle.js +557 -0
- package/dist/lib/sandbox/control/protocol.js +69 -26
- package/dist/lib/sandbox/control/server.js +395 -99
- package/dist/lib/sandbox/control/state.js +191 -0
- package/dist/lib/sandbox/control/timing.js +8 -0
- package/dist/lib/sandbox/index.js +2 -2
- package/dist/lib/sandbox/managed-fs.js +14 -2
- package/dist/lib/sandbox/mounts.js +22 -19
- package/dist/lib/sandbox/recovery.js +284 -50
- package/dist/lib/sandbox/workspace-view.js +90 -10
- package/dist/lib/sandbox/worktree-safety.js +196 -0
- package/dist/lib/server/daemon.js +1 -1
- package/dist/lib/server/process-control.js +17 -1
- package/dist/lib/server/process-state.js +57 -20
- package/dist/lib/task/artifact-lifecycle.js +87 -15
- package/dist/lib/task/artifact-receipts.js +128 -0
- package/dist/lib/task/codex-orchestration.js +236 -0
- package/dist/lib/task/commands/status.js +3 -0
- package/dist/lib/task/commands/validate.js +289 -0
- package/dist/lib/task/commit-finalization.js +36 -9
- package/dist/lib/task/create.js +10 -2
- package/dist/lib/task/delegation-receipts.js +109 -2
- package/dist/lib/task/events.js +149 -6
- package/dist/lib/task/finalization-artifacts.js +2 -1
- package/dist/lib/task/implementation-inputs.js +1 -7
- package/dist/lib/task/index.js +9 -0
- package/dist/lib/task/ledger.js +13 -1
- package/dist/lib/task/orchestration.js +365 -15
- package/dist/lib/task/sections.js +5 -1
- package/dist/lib/task/task-execution-lock.js +5 -4
- package/dist/lib/task/verification-engine.js +78 -14
- package/dist/lib/task/verification.js +1 -0
- package/dist/lifecycle-build-manifest.json +6 -0
- package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +202 -18
- package/lib/agent-clients/adapters/codex-lifecycle/build-identity.ts +227 -0
- package/lib/agent-clients/adapters/codex-lifecycle/capability-store.ts +321 -0
- package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +19 -3
- package/lib/agent-clients/adapters/codex-lifecycle/manifest-files.json +26 -0
- package/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.ts +453 -0
- package/lib/agent-clients/adapters/codex-lifecycle/store.ts +83 -6
- package/lib/agent-clients/adapters/codex.ts +1 -1
- package/lib/defaults.json +10 -2
- package/lib/git/workflow.ts +25 -3
- package/lib/internal/codex-lifecycle.ts +175 -4
- package/lib/internal/codex-sandbox-controller.ts +109 -0
- package/lib/internal/release-workflow.ts +107 -11
- package/lib/internal/sandbox-control.ts +38 -6
- package/lib/internal/task-create.ts +16 -4
- package/lib/internal/task-orchestration.ts +34 -7
- package/lib/platform/context.ts +8 -5
- package/lib/platform/github-client.ts +61 -3
- package/lib/platform/issue-comments.ts +2 -1
- package/lib/process-data/checkpoint.ts +335 -0
- package/lib/process-data/index.ts +112 -60
- package/lib/process-data/materialize.ts +83 -0
- package/lib/process-data/normalize.ts +29 -1
- package/lib/process-data/reconcile.ts +64 -2
- package/lib/process-data/sources.ts +449 -2
- package/lib/process-data/store.ts +304 -11
- package/lib/process-data/types.ts +124 -2
- package/lib/release/workflow.ts +69 -20
- package/lib/sandbox/commands/create.ts +70 -25
- package/lib/sandbox/commands/prune.ts +53 -15
- package/lib/sandbox/commands/rm.ts +423 -108
- package/lib/sandbox/commands/start.ts +4 -3
- package/lib/sandbox/control/client.ts +152 -28
- package/lib/sandbox/control/container-identity.ts +93 -0
- package/lib/sandbox/control/executor.ts +167 -0
- package/lib/sandbox/control/lifecycle.ts +573 -0
- package/lib/sandbox/control/protocol.ts +121 -68
- package/lib/sandbox/control/server.ts +398 -109
- package/lib/sandbox/control/state.ts +213 -0
- package/lib/sandbox/control/timing.ts +12 -0
- package/lib/sandbox/index.ts +2 -2
- package/lib/sandbox/managed-fs.ts +17 -2
- package/lib/sandbox/mounts.ts +16 -8
- package/lib/sandbox/recovery.ts +335 -65
- package/lib/sandbox/workspace-view.ts +102 -11
- package/lib/sandbox/worktree-safety.ts +234 -0
- package/lib/server/daemon.ts +1 -1
- package/lib/server/process-control.ts +24 -3
- package/lib/server/process-state.ts +82 -27
- package/lib/task/artifact-lifecycle.ts +78 -14
- package/lib/task/artifact-receipts.ts +161 -0
- package/lib/task/codex-orchestration.ts +319 -0
- package/lib/task/commands/status.ts +2 -0
- package/lib/task/commands/validate.ts +282 -0
- package/lib/task/commit-finalization.ts +52 -9
- package/lib/task/create.ts +11 -2
- package/lib/task/delegation-receipts.ts +208 -7
- package/lib/task/events.ts +131 -5
- package/lib/task/finalization-artifacts.ts +2 -1
- package/lib/task/implementation-inputs.ts +1 -5
- package/lib/task/index.ts +8 -0
- package/lib/task/ledger.ts +16 -1
- package/lib/task/orchestration.ts +552 -18
- package/lib/task/sections.ts +6 -2
- package/lib/task/task-execution-lock.ts +11 -9
- package/lib/task/verification-engine.ts +93 -24
- package/lib/task/verification.ts +2 -1
- package/package.json +1 -1
- package/templates/.agents/README.en.md +7 -0
- package/templates/.agents/README.zh-CN.md +5 -0
- package/templates/.agents/hooks/lifecycle-delegation.js +72 -12
- package/templates/.agents/rules/lifecycle-orchestration.en.md +8 -2
- package/templates/.agents/rules/lifecycle-orchestration.zh-CN.md +8 -2
- package/templates/.agents/rules/no-mid-flow-questions.en.md +1 -0
- package/templates/.agents/rules/no-mid-flow-questions.zh-CN.md +1 -0
- package/templates/.agents/rules/pr-checks-commands.github.en.md +1 -1
- package/templates/.agents/rules/pr-checks-commands.github.zh-CN.md +1 -1
- package/templates/.agents/rules/review-handshake.en.md +3 -1
- package/templates/.agents/rules/review-handshake.zh-CN.md +3 -1
- package/templates/.agents/skills/code-task/reference/dual-mode.en.md +3 -1
- package/templates/.agents/skills/code-task/reference/dual-mode.zh-CN.md +3 -1
- package/templates/.agents/skills/complete-task/SKILL.en.md +4 -0
- package/templates/.agents/skills/complete-task/SKILL.zh-CN.md +4 -0
- package/templates/.agents/skills/post-release/SKILL.en.md +32 -6
- package/templates/.agents/skills/post-release/SKILL.zh-CN.md +32 -6
- package/templates/.agents/skills/run-manual-validation/SKILL.en.md +42 -0
- package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +42 -0
- package/templates/.agents/skills/run-manual-validation/config/verify.en.json +25 -0
- package/templates/.agents/skills/run-manual-validation/config/verify.zh-CN.json +25 -0
- package/templates/.agents/skills/run-manual-validation/reference/discovery-and-execution.en.md +67 -0
- package/templates/.agents/skills/run-manual-validation/reference/discovery-and-execution.zh-CN.md +67 -0
- package/templates/.agents/skills/run-manual-validation/reference/report-template.en.md +69 -0
- package/templates/.agents/skills/run-manual-validation/reference/report-template.zh-CN.md +69 -0
- package/templates/.agents/skills/run-task/SKILL.en.md +9 -7
- package/templates/.agents/skills/run-task/SKILL.zh-CN.md +9 -7
- package/templates/.agents/skills/run-task/reference/host-validation.en.md +5 -3
- package/templates/.agents/skills/run-task/reference/host-validation.zh-CN.md +5 -3
- package/templates/.agents/skills/update-agent-infra/scripts/sync-templates.js +75 -3
- package/templates/.claude/commands/run-manual-validation.en.md +9 -0
- package/templates/.claude/commands/run-manual-validation.zh-CN.md +9 -0
- package/templates/.codex/hooks.json +8 -8
- package/templates/.opencode/commands/run-manual-validation.en.md +11 -0
- package/templates/.opencode/commands/run-manual-validation.zh-CN.md +11 -0
- package/templates/.agents/workspace/README.en.md +0 -26
- package/templates/.agents/workspace/README.zh-CN.md +0 -26
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { execFileSync } from 'node:child_process';
|
|
4
3
|
|
|
5
4
|
import { detectRepoRoot } from '../task/resolve-ref.ts';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
5
|
+
import { acquireGitHubCheckpoint, commitGitHubCheckpoint, readGitHubCheckpoint, releaseGitHubCheckpoint } from './checkpoint.ts';
|
|
6
|
+
import { materializeJson, materializeSnapshot } from './materialize.ts';
|
|
7
|
+
import { normalizeResources } from './normalize.ts';
|
|
8
|
+
import { reconcileRecords, reconcileResourceRecords } from './reconcile.ts';
|
|
8
9
|
import { readAppliedOverlays, repairSnapshot } from './repair.ts';
|
|
9
|
-
import {
|
|
10
|
-
import { canonicalJsonBytes, createObjectStore,
|
|
10
|
+
import { collectGitHubBoundary, resolveGitHubRepository } from './sources.ts';
|
|
11
|
+
import { canonicalJsonBytes, createObjectStore, findSnapshot, publishSnapshotV2, verifySnapshot } from './store.ts';
|
|
12
|
+
import { createGitHubClient } from '../platform/github-client.ts';
|
|
11
13
|
|
|
12
|
-
import type {
|
|
14
|
+
import type { NormalizedRecord } from './types.ts';
|
|
13
15
|
|
|
14
16
|
const USAGE = `Usage: ai data <command> [options]
|
|
15
17
|
|
|
16
18
|
Commands:
|
|
17
|
-
capture [--source
|
|
19
|
+
capture [--source github] [--root <dir>] [--full-reconcile]
|
|
18
20
|
verify <snapshot-id> [--root <dir>]
|
|
19
21
|
audit <snapshot-id> [--root <dir>] [--format json|text]
|
|
20
22
|
repair <snapshot-id> [--root <dir>] [--apply]
|
|
21
|
-
export <snapshot-id> [--root <dir>] [--repairs none|applied] [--output <file|->]
|
|
23
|
+
export <snapshot-id> [--root <dir>] [--repairs none|applied] [--as-of <ISO>] [--output <file|->]
|
|
22
24
|
`;
|
|
23
25
|
|
|
24
26
|
type ParsedArgs = { positionals: string[]; options: Map<string, string | true> };
|
|
@@ -64,14 +66,6 @@ function dataRoot(repoRoot: string, requested?: string): string {
|
|
|
64
66
|
return resolved;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
function repositoryIdentity(repoRoot: string): string {
|
|
68
|
-
try {
|
|
69
|
-
return execFileSync('git', ['config', '--get', 'remote.origin.url'], { cwd: repoRoot, encoding: 'utf8' }).trim() || path.basename(repoRoot);
|
|
70
|
-
} catch {
|
|
71
|
-
return path.basename(repoRoot);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
69
|
function writeJson(value: unknown): void {
|
|
76
70
|
process.stdout.write(`${JSON.stringify(value)}\n`);
|
|
77
71
|
}
|
|
@@ -82,54 +76,106 @@ function optionString(parsed: ParsedArgs, name: string): string | undefined {
|
|
|
82
76
|
}
|
|
83
77
|
|
|
84
78
|
function capture(args: string[]): number {
|
|
85
|
-
const parsed = parseArgs(args, new Set(['--source', '--root']), new Set(['--include-excerpts']));
|
|
79
|
+
const parsed = parseArgs(args, new Set(['--source', '--root']), new Set(['--full-reconcile', '--include-excerpts']));
|
|
86
80
|
if (parsed.positionals.length > 0) throw new Error('capture does not accept positional arguments');
|
|
87
|
-
const scope = optionString(parsed, '--source') ?? '
|
|
88
|
-
if (
|
|
81
|
+
const scope = optionString(parsed, '--source') ?? 'github';
|
|
82
|
+
if (scope !== 'github') throw new Error('new process-data capture only supports --source github; local data is already normalized and --source all is retired');
|
|
83
|
+
if (parsed.options.has('--include-excerpts')) throw new Error('--include-excerpts is not supported for GitHub-only capture');
|
|
89
84
|
const repoRoot = detectRepoRoot();
|
|
90
85
|
const root = dataRoot(repoRoot, optionString(parsed, '--root'));
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
86
|
+
const client = createGitHubClient();
|
|
87
|
+
const repositoryResult = resolveGitHubRepository(repoRoot, client);
|
|
88
|
+
if (!repositoryResult.ok) { writeJson({ ok: false, error: repositoryResult.error }); return 2; }
|
|
89
|
+
const repository = repositoryResult.value;
|
|
90
|
+
const leaseResult = acquireGitHubCheckpoint(root, repository);
|
|
91
|
+
if (!leaseResult.ok) { writeJson({ ok: false, error: leaseResult.error }); return 2; }
|
|
92
|
+
const lease = leaseResult.value;
|
|
93
|
+
try {
|
|
94
|
+
const checkpointResult = readGitHubCheckpoint(root, repository);
|
|
95
|
+
if (!checkpointResult.ok) { writeJson({ ok: false, error: checkpointResult.error }); return 2; }
|
|
96
|
+
const checkpoint = checkpointResult.value;
|
|
97
|
+
const full = parsed.options.has('--full-reconcile');
|
|
98
|
+
const observedFrom = new Date().toISOString();
|
|
99
|
+
const github = collectGitHubBoundary(repoRoot, {
|
|
100
|
+
client,
|
|
101
|
+
fromInclusive: full ? null : checkpoint?.watermark ?? null,
|
|
102
|
+
reconciliation: full ? 'full' : 'incremental'
|
|
103
|
+
});
|
|
103
104
|
if (!github.ok) { writeJson({ ok: false, error: github.error }); return 2; }
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
105
|
+
const store = createObjectStore(root);
|
|
106
|
+
for (const object of github.value.objects) {
|
|
107
|
+
if (object.content !== undefined && object.disposition?.state === 'included') {
|
|
108
|
+
const stored = store.put(Buffer.from(object.content, 'utf8'));
|
|
109
|
+
if (stored.sha256 !== object.sha256) throw new Error(`source hash mismatch: ${object.sourceIdentity}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
let parent: NormalizedRecord[] = [];
|
|
113
|
+
if (checkpoint) {
|
|
114
|
+
const parentPath = findSnapshot(root, checkpoint.snapshotId);
|
|
115
|
+
if (!parentPath) throw new Error(`checkpoint head snapshot is missing: ${checkpoint.snapshotId}`);
|
|
116
|
+
const materialized = materializeSnapshot(root, checkpoint.snapshotId);
|
|
117
|
+
if (!materialized.manifest || materialized.manifest.manifestSha256 !== checkpoint.manifestSha256 || materialized.manifest.watermark !== checkpoint.watermark) {
|
|
118
|
+
throw new Error('checkpoint does not match its verified snapshot head');
|
|
119
|
+
}
|
|
120
|
+
parent = materialized.records;
|
|
121
|
+
}
|
|
122
|
+
const current = normalizeResources(github.value.objects);
|
|
123
|
+
const reconciliation = reconcileResourceRecords(current, {
|
|
124
|
+
parent,
|
|
125
|
+
full,
|
|
126
|
+
deferred: github.value.deferred,
|
|
127
|
+
unavailable: github.value.unavailable
|
|
128
|
+
});
|
|
129
|
+
const quality = reconcileRecords(reconciliation.records, 'github');
|
|
130
|
+
const fromInclusive = checkpoint?.watermark ?? null;
|
|
131
|
+
const hasStrictSinceEndpoint = github.value.endpoints.some((endpoint) => endpoint.queryMode === 'strict-since');
|
|
132
|
+
const queryAfter = !full && fromInclusive && hasStrictSinceEndpoint
|
|
133
|
+
? new Date(new Date(fromInclusive).getTime() - 1000).toISOString()
|
|
134
|
+
: null;
|
|
135
|
+
const responseDates = github.value.responseDates;
|
|
136
|
+
const published = publishSnapshotV2(root, {
|
|
137
|
+
scope: 'github',
|
|
138
|
+
repository,
|
|
139
|
+
observedFrom,
|
|
140
|
+
observedTo: github.value.watermark,
|
|
141
|
+
objects: github.value.objects.map(({ content: _content, ...object }) => object),
|
|
142
|
+
endpoints: github.value.endpoints,
|
|
143
|
+
excerptsEnabled: false,
|
|
144
|
+
records: reconciliation.records,
|
|
145
|
+
quality: quality.findings,
|
|
146
|
+
repairs: quality.repairs,
|
|
147
|
+
snapshotKind: checkpoint ? 'delta' : 'base',
|
|
148
|
+
parentSnapshotId: checkpoint?.snapshotId ?? null,
|
|
149
|
+
checkpointBefore: checkpoint?.snapshotId ?? null,
|
|
150
|
+
watermark: github.value.watermark,
|
|
151
|
+
window: { fromInclusive, queryAfter, toExclusive: github.value.watermark, precision: 'second' },
|
|
152
|
+
observation: {
|
|
153
|
+
cutoffSource: 'github-response-date',
|
|
154
|
+
preflightDate: github.value.preflightDate,
|
|
155
|
+
responseDates
|
|
156
|
+
},
|
|
157
|
+
coverage: { mode: 'boundary-reread-with-full-reconcile', absoluteCompleteness: false },
|
|
158
|
+
reconciliation: full ? 'full' : 'incremental',
|
|
159
|
+
operations: reconciliation.operations
|
|
160
|
+
});
|
|
161
|
+
const verified = verifySnapshot(root, published.snapshotId);
|
|
162
|
+
if (!verified.ok || !verified.manifest || verified.manifest.schema !== 'raw-manifest/v2') {
|
|
163
|
+
throw new Error(`published snapshot failed verification: ${verified.errors.join('; ')}`);
|
|
114
164
|
}
|
|
165
|
+
const committed = commitGitHubCheckpoint(lease, {
|
|
166
|
+
schema: 'github-checkpoint/v1',
|
|
167
|
+
repository,
|
|
168
|
+
snapshotId: published.snapshotId,
|
|
169
|
+
watermark: github.value.watermark,
|
|
170
|
+
manifestSha256: verified.manifest.manifestSha256,
|
|
171
|
+
committedAt: new Date().toISOString()
|
|
172
|
+
});
|
|
173
|
+
if (!committed.ok) { writeJson({ ok: false, error: committed.error }); return 2; }
|
|
174
|
+
writeJson({ ok: true, snapshotId: published.snapshotId, scope, snapshotKind: checkpoint ? 'delta' : 'base', reconciliation: full ? 'full' : 'incremental', created: published.created, objects: github.value.objects.length, records: reconciliation.records.length, watermark: github.value.watermark, deferred: github.value.deferred.length });
|
|
175
|
+
return 0;
|
|
176
|
+
} finally {
|
|
177
|
+
releaseGitHubCheckpoint(lease);
|
|
115
178
|
}
|
|
116
|
-
const records = normalizeObjects(objects);
|
|
117
|
-
const reconciliation = reconcileRecords(records, scope as 'all' | 'local' | 'github');
|
|
118
|
-
const observedTo = new Date().toISOString();
|
|
119
|
-
const published = publishSnapshot(root, {
|
|
120
|
-
scope: scope as 'all' | 'local' | 'github',
|
|
121
|
-
repository,
|
|
122
|
-
observedFrom,
|
|
123
|
-
observedTo,
|
|
124
|
-
objects: objects.map(({ content: _content, ...object }) => object),
|
|
125
|
-
endpoints,
|
|
126
|
-
excerptsEnabled: parsed.options.has('--include-excerpts'),
|
|
127
|
-
records,
|
|
128
|
-
quality: reconciliation.findings,
|
|
129
|
-
repairs: reconciliation.repairs
|
|
130
|
-
});
|
|
131
|
-
writeJson({ ok: true, snapshotId: published.snapshotId, scope, created: published.created, objects: objects.length, records: records.length });
|
|
132
|
-
return 0;
|
|
133
179
|
}
|
|
134
180
|
|
|
135
181
|
function verify(args: string[]): number {
|
|
@@ -175,7 +221,7 @@ function repair(args: string[]): number {
|
|
|
175
221
|
}
|
|
176
222
|
|
|
177
223
|
function exportSnapshot(args: string[]): number {
|
|
178
|
-
const parsed = parseArgs(args, new Set(['--root', '--repairs', '--output']), new Set());
|
|
224
|
+
const parsed = parseArgs(args, new Set(['--root', '--repairs', '--output', '--as-of']), new Set());
|
|
179
225
|
if (parsed.positionals.length !== 1) throw new Error('export requires one snapshot id');
|
|
180
226
|
const repairMode = optionString(parsed, '--repairs') ?? 'none';
|
|
181
227
|
if (!['none', 'applied'].includes(repairMode)) throw new Error('--repairs must be none or applied');
|
|
@@ -184,7 +230,13 @@ function exportSnapshot(args: string[]): number {
|
|
|
184
230
|
const root = dataRoot(repoRoot, optionString(parsed, '--root'));
|
|
185
231
|
const verified = verifySnapshot(root, parsed.positionals[0]!);
|
|
186
232
|
if (!verified.ok || !verified.snapshotPath) { writeJson(verified); return 1; }
|
|
187
|
-
let content
|
|
233
|
+
let content: string;
|
|
234
|
+
if (verified.manifest?.schema === 'raw-manifest/v2') {
|
|
235
|
+
content = materializeJson(root, parsed.positionals[0]!, { asOf: optionString(parsed, '--as-of') });
|
|
236
|
+
} else {
|
|
237
|
+
if (optionString(parsed, '--as-of')) throw new Error('--as-of is only supported for v2 GitHub snapshots');
|
|
238
|
+
content = fs.readFileSync(path.join(verified.snapshotPath, 'normalized.jsonl'), 'utf8');
|
|
239
|
+
}
|
|
188
240
|
if (repairMode === 'applied') content += readAppliedOverlays(root, parsed.positionals[0]!);
|
|
189
241
|
if (output === '-') process.stdout.write(content);
|
|
190
242
|
else {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { findSnapshot, verifySnapshot } from './store.ts';
|
|
5
|
+
import type { NormalizedRecord, SnapshotManifestV2 } from './types.ts';
|
|
6
|
+
|
|
7
|
+
type MaterializeOptions = { asOf?: string };
|
|
8
|
+
type MaterializeResult = { snapshotId: string; records: NormalizedRecord[]; manifest: SnapshotManifestV2 | null };
|
|
9
|
+
|
|
10
|
+
function readRecords(snapshotPath: string, schema: 'normalized/v1' | 'normalized/v2'): NormalizedRecord[] {
|
|
11
|
+
const file = path.join(snapshotPath, 'normalized.jsonl');
|
|
12
|
+
if (!fs.existsSync(file)) return [];
|
|
13
|
+
return fs.readFileSync(file, 'utf8').split('\n').filter(Boolean).flatMap((line) => {
|
|
14
|
+
const value = JSON.parse(line) as { schema?: string } & NormalizedRecord;
|
|
15
|
+
if (value.schema !== schema) return [];
|
|
16
|
+
const { schema: _schema, ...record } = value;
|
|
17
|
+
return [record];
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function materializeSnapshot(root: string, snapshotId: string, options: MaterializeOptions = {}): MaterializeResult {
|
|
22
|
+
const target = options.asOf ? new Date(options.asOf) : null;
|
|
23
|
+
if (target && !Number.isFinite(target.getTime())) throw new Error('as-of must be an ISO timestamp');
|
|
24
|
+
const chain: Array<{ id: string; path: string; manifest: SnapshotManifestV2 }> = [];
|
|
25
|
+
let currentId: string | null = snapshotId;
|
|
26
|
+
let repository: string | null = null;
|
|
27
|
+
while (currentId) {
|
|
28
|
+
const currentPath = findSnapshot(root, currentId);
|
|
29
|
+
if (!currentPath) throw new Error(`snapshot not found: ${currentId}`);
|
|
30
|
+
const verified = verifySnapshot(root, currentId);
|
|
31
|
+
if (!verified.ok || !verified.manifest) throw new Error(`snapshot is not verified: ${currentId}`);
|
|
32
|
+
if (verified.manifest.schema !== 'raw-manifest/v2') {
|
|
33
|
+
if (chain.length > 0) throw new Error('v1 and v2 snapshots cannot share a lineage');
|
|
34
|
+
return { snapshotId, records: readRecords(currentPath, 'normalized/v1'), manifest: null };
|
|
35
|
+
}
|
|
36
|
+
const manifest = verified.manifest;
|
|
37
|
+
if (repository && manifest.repository !== repository) throw new Error('snapshot lineage repository mismatch');
|
|
38
|
+
repository = manifest.repository;
|
|
39
|
+
chain.push({ id: currentId, path: currentPath, manifest });
|
|
40
|
+
currentId = manifest.parentSnapshotId;
|
|
41
|
+
}
|
|
42
|
+
if (chain.length === 0) throw new Error('snapshot lineage is empty');
|
|
43
|
+
const chronological = [...chain].reverse();
|
|
44
|
+
for (let index = 1; index < chronological.length; index += 1) {
|
|
45
|
+
if (new Date(chronological[index - 1]!.manifest.watermark).getTime() >= new Date(chronological[index]!.manifest.watermark).getTime()) {
|
|
46
|
+
throw new Error('snapshot watermark is not monotonic');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const view = new Map<string, NormalizedRecord>();
|
|
50
|
+
let appliedManifest: SnapshotManifestV2 | null = null;
|
|
51
|
+
for (const entry of chronological) {
|
|
52
|
+
if (target && new Date(entry.manifest.watermark).getTime() > target.getTime()) continue;
|
|
53
|
+
appliedManifest = entry.manifest;
|
|
54
|
+
for (const record of readRecords(entry.path, 'normalized/v2')) {
|
|
55
|
+
const identity = record.resourceIdentity ?? record.sourceIdentity;
|
|
56
|
+
switch (record.operation) {
|
|
57
|
+
case 'tombstone':
|
|
58
|
+
view.delete(identity);
|
|
59
|
+
break;
|
|
60
|
+
case 'unavailable':
|
|
61
|
+
view.set(identity, record);
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
view.set(identity, { ...record, operation: undefined });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (target && !appliedManifest) throw new Error('no verified snapshot is available at the requested as-of time');
|
|
70
|
+
return {
|
|
71
|
+
snapshotId,
|
|
72
|
+
records: [...view.values()].sort((left, right) => left.recordId.localeCompare(right.recordId)),
|
|
73
|
+
manifest: appliedManifest
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function materializeJson(root: string, snapshotId: string, options: MaterializeOptions = {}): string {
|
|
78
|
+
const result = materializeSnapshot(root, snapshotId, options);
|
|
79
|
+
return result.records.map((record) => JSON.stringify({ schema: 'normalized/v2', ...record })).join('\n') + (result.records.length ? '\n' : '');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { materializeJson, materializeSnapshot };
|
|
83
|
+
export type { MaterializeOptions, MaterializeResult };
|
|
@@ -199,4 +199,32 @@ function normalizeObjects(objects: CapturedObject[]): NormalizedRecord[] {
|
|
|
199
199
|
return records.sort((left, right) => left.recordId.localeCompare(right.recordId));
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
|
|
202
|
+
function normalizeResources(objects: CapturedObject[]): NormalizedRecord[] {
|
|
203
|
+
const records: NormalizedRecord[] = [];
|
|
204
|
+
for (const object of objects) {
|
|
205
|
+
if (object.role !== 'resource') continue;
|
|
206
|
+
const identity = object.resourceIdentity ?? object.sourceIdentity;
|
|
207
|
+
let data: JsonValue = { availability: 'unknown' };
|
|
208
|
+
try { data = JSON.parse(object.content ?? '{}') as JsonValue; } catch { data = { availability: 'unavailable', reason: 'invalid resource JSON' }; }
|
|
209
|
+
const binding = typeof data === 'object' && data !== null && !Array.isArray(data) && typeof data.body === 'string'
|
|
210
|
+
? data.body.match(TASK_ID_RE)?.[0]
|
|
211
|
+
: undefined;
|
|
212
|
+
records.push({
|
|
213
|
+
recordId: recordId('platform-resource', identity),
|
|
214
|
+
kind: 'platform-resource',
|
|
215
|
+
sourceIdentity: identity,
|
|
216
|
+
resourceIdentity: identity,
|
|
217
|
+
sourceSha256: object.sha256,
|
|
218
|
+
...(binding ? { binding } : {}),
|
|
219
|
+
...(object.eventTime ? { observedAt: { state: 'known', value: object.eventTime } } : {}),
|
|
220
|
+
...(object.parentIdentity ? { parentIdentity: object.parentIdentity } : {}),
|
|
221
|
+
...(object.pageSha256 ? {
|
|
222
|
+
evidence: [{ resourceIdentity: identity, resourceSha256: object.sha256, pageSha256: object.pageSha256 }]
|
|
223
|
+
} : {}),
|
|
224
|
+
data
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return records.sort((left, right) => left.recordId.localeCompare(right.recordId));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { normalizeObjects, normalizeResources };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { sha256 } from './store.ts';
|
|
2
2
|
|
|
3
|
-
import type { NormalizedRecord, QualityFinding, RepairAction, SnapshotManifest } from './types.ts';
|
|
3
|
+
import type { NormalizedRecord, QualityFinding, RepairAction, SnapshotManifest, SnapshotOperations } from './types.ts';
|
|
4
4
|
|
|
5
5
|
function reconcileRecords(
|
|
6
6
|
records: NormalizedRecord[],
|
|
@@ -107,4 +107,66 @@ function reconcileRecords(
|
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
type ResourceReconcileOptions = {
|
|
111
|
+
parent?: NormalizedRecord[];
|
|
112
|
+
full: boolean;
|
|
113
|
+
deferred?: Iterable<string>;
|
|
114
|
+
unavailable?: Iterable<string>;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
function reconcileResourceRecords(
|
|
118
|
+
current: NormalizedRecord[],
|
|
119
|
+
options: ResourceReconcileOptions
|
|
120
|
+
): { records: NormalizedRecord[]; operations: SnapshotOperations } {
|
|
121
|
+
const parent = new Map((options.parent ?? []).filter((record) => record.resourceIdentity).map((record) => [record.resourceIdentity!, record]));
|
|
122
|
+
const currentMap = new Map(current.filter((record) => record.resourceIdentity).map((record) => [record.resourceIdentity!, record]));
|
|
123
|
+
const deferred = new Set(options.deferred ?? []);
|
|
124
|
+
const unavailable = new Set(options.unavailable ?? []);
|
|
125
|
+
const records: NormalizedRecord[] = [];
|
|
126
|
+
const operations: SnapshotOperations = { upsert: 0, supersede: 0, tombstone: 0, unavailable: 0 };
|
|
127
|
+
for (const record of currentMap.values()) {
|
|
128
|
+
const identity = record.resourceIdentity!;
|
|
129
|
+
const previous = parent.get(identity);
|
|
130
|
+
if (!previous) {
|
|
131
|
+
records.push({ ...record, operation: 'upsert' });
|
|
132
|
+
operations.upsert += 1;
|
|
133
|
+
} else if (previous.sourceSha256 !== record.sourceSha256) {
|
|
134
|
+
records.push({ ...record, operation: 'supersede' });
|
|
135
|
+
operations.supersede += 1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (options.full) {
|
|
139
|
+
for (const [identity, previous] of parent) {
|
|
140
|
+
if (currentMap.has(identity) || deferred.has(identity) || unavailable.has(identity)
|
|
141
|
+
|| (previous.parentIdentity && deferred.has(previous.parentIdentity))
|
|
142
|
+
|| (previous.parentIdentity && unavailable.has(previous.parentIdentity))) continue;
|
|
143
|
+
records.push({
|
|
144
|
+
recordId: previous.recordId,
|
|
145
|
+
kind: 'platform-resource',
|
|
146
|
+
sourceIdentity: identity,
|
|
147
|
+
resourceIdentity: identity,
|
|
148
|
+
sourceSha256: previous.sourceSha256,
|
|
149
|
+
operation: 'tombstone',
|
|
150
|
+
data: { availability: 'tombstone', previousSha256: previous.sourceSha256 }
|
|
151
|
+
});
|
|
152
|
+
operations.tombstone += 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const identity of unavailable) {
|
|
156
|
+
if (!parent.has(identity) && !currentMap.has(identity)) continue;
|
|
157
|
+
const previous = parent.get(identity) ?? currentMap.get(identity)!;
|
|
158
|
+
records.push({
|
|
159
|
+
recordId: previous.recordId,
|
|
160
|
+
kind: 'platform-resource',
|
|
161
|
+
sourceIdentity: identity,
|
|
162
|
+
resourceIdentity: identity,
|
|
163
|
+
sourceSha256: previous.sourceSha256,
|
|
164
|
+
operation: 'unavailable',
|
|
165
|
+
data: { availability: 'unavailable', reason: 'source-unavailable' }
|
|
166
|
+
});
|
|
167
|
+
operations.unavailable += 1;
|
|
168
|
+
}
|
|
169
|
+
return { records: records.sort((left, right) => left.recordId.localeCompare(right.recordId)), operations };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { reconcileRecords, reconcileResourceRecords };
|