Skip to content

Commit b7c3ba1

Browse files
committed
fix(ledger): bind command publication manifests
1 parent 2508b86 commit b7c3ba1

11 files changed

Lines changed: 452 additions & 86 deletions

‎.github/workflows/repair-cluster-worker.yml‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,37 +1173,30 @@ jobs:
11731173
run: |
11741174
set -euo pipefail
11751175
: "${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
1176-
pnpm run --silent repair:action-ledger -- finalize
1176+
mkdir -p .artifacts
1177+
pnpm run --silent repair:action-ledger -- finalize \
1178+
--lane report-requeue \
1179+
> .artifacts/report-command-action-ledger-manifest.json
11771180
11781181
- name: Publish immutable report command action ledger
11791182
if: ${{ always() && steps.report-setup-pnpm.outcome == 'success' && steps.repair_requeue.outputs.count != '' && steps.repair_requeue.outputs.count != '0' }}
11801183
run: |
11811184
set -euo pipefail
11821185
test -n "${CLAWSWEEPER_STATE_DIR:-}"
11831186
source_root="${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
1184-
if [ ! -d "$source_root/ledger" ]; then
1185-
echo "Expected report command action event shards in $source_root, but none were finalized." >&2
1186-
exit 1
1187-
fi
1188-
command_shard_found=false
1189-
while IFS= read -r -d '' shard_path; do
1190-
if jq -e 'select(.event_type | startswith("command."))' "$shard_path" >/dev/null; then
1191-
command_shard_found=true
1192-
break
1193-
fi
1194-
done < <(find "$source_root/ledger/v1/events" -type f -name '*.jsonl' -print0)
1195-
if [ "$command_shard_found" != "true" ]; then
1196-
echo "Expected command receipt events, but the finalized shard set contained none." >&2
1197-
exit 1
1198-
fi
1187+
manifest_file=".artifacts/report-command-action-ledger-manifest.json"
1188+
test -s "$manifest_file"
11991189
event_paths_file=".artifacts/report-command-action-ledger-paths.txt"
12001190
import_result_file=".artifacts/report-command-action-ledger-import.json"
1201-
mkdir -p .artifacts
12021191
pnpm run --silent repair:action-ledger -- publish \
1192+
--lane report-requeue \
1193+
--manifest "$manifest_file" \
12031194
--source-root "$source_root" \
12041195
--state-root "$CLAWSWEEPER_STATE_DIR" > "$import_result_file"
1205-
if ! jq -e '.created > 0' "$import_result_file" >/dev/null; then
1206-
echo "Report command action event shards were expected but no new shards were imported." >&2
1196+
if ! jq -e --slurpfile manifest "$manifest_file" \
1197+
'.eventPaths == $manifest[0].event_paths' \
1198+
"$import_result_file" >/dev/null; then
1199+
echo "Imported report command action event paths do not match the finalized manifest." >&2
12071200
exit 1
12081201
fi
12091202
jq -r '.paths[]?' "$import_result_file" |

‎.github/workflows/repair-comment-router.yml‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -486,37 +486,30 @@ jobs:
486486
run: |
487487
set -euo pipefail
488488
: "${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
489-
pnpm run --silent repair:action-ledger -- finalize
489+
mkdir -p .artifacts
490+
pnpm run --silent repair:action-ledger -- finalize \
491+
--lane comment-router \
492+
> .artifacts/comment-router-command-action-ledger-manifest.json
490493
491494
- name: Publish immutable command action ledger
492495
if: ${{ always() && steps.setup-pnpm.outcome == 'success' }}
493496
run: |
494497
set -euo pipefail
495498
test -n "${CLAWSWEEPER_STATE_DIR:-}"
496499
source_root="${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
497-
if [ ! -d "$source_root/ledger" ]; then
498-
echo "Expected command action event shards in $source_root, but none were finalized." >&2
499-
exit 1
500-
fi
501-
command_shard_found=false
502-
while IFS= read -r -d '' shard_path; do
503-
if jq -e 'select(.event_type | startswith("command."))' "$shard_path" >/dev/null; then
504-
command_shard_found=true
505-
break
506-
fi
507-
done < <(find "$source_root/ledger/v1/events" -type f -name '*.jsonl' -print0)
508-
if [ "$command_shard_found" != "true" ]; then
509-
echo "Expected command receipt events, but the finalized shard set contained none." >&2
510-
exit 1
511-
fi
500+
manifest_file=".artifacts/comment-router-command-action-ledger-manifest.json"
501+
test -s "$manifest_file"
512502
event_paths_file=".artifacts/command-action-ledger-paths.txt"
513503
import_result_file=".artifacts/command-action-ledger-import.json"
514-
mkdir -p .artifacts
515504
pnpm run --silent repair:action-ledger -- publish \
505+
--lane comment-router \
506+
--manifest "$manifest_file" \
516507
--source-root "$source_root" \
517508
--state-root "$CLAWSWEEPER_STATE_DIR" > "$import_result_file"
518-
if ! jq -e '.created > 0' "$import_result_file" >/dev/null; then
519-
echo "Command action event shards were expected but no new shards were imported." >&2
509+
if ! jq -e --slurpfile manifest "$manifest_file" \
510+
'.eventPaths == $manifest[0].event_paths' \
511+
"$import_result_file" >/dev/null; then
512+
echo "Imported command action event paths do not match the finalized manifest." >&2
520513
exit 1
521514
fi
522515
jq -r '.paths[]?' "$import_result_file" |

‎.github/workflows/sweep.yml‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,35 +1250,35 @@ jobs:
12501250
if: ${{ always() && steps.publish-event-result.outputs.requeue_latest == 'true' && steps.complete-exact-review-queue.outcome != 'success' }}
12511251
run: exit 1
12521252

1253+
- name: Finalize late command status action ledger
1254+
if: ${{ always() && steps.setup-state.outcome == 'success' && steps.setup-pnpm.outcome == 'success' && steps.publish-event-result.outputs.requeue_latest == 'true' && steps.complete-exact-review-queue.outcome == 'success' }}
1255+
run: |
1256+
set -euo pipefail
1257+
: "${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
1258+
mkdir -p .artifacts
1259+
pnpm run --silent repair:action-ledger -- finalize \
1260+
--lane late-command-status \
1261+
> .artifacts/late-command-status-action-ledger-manifest.json
1262+
12531263
- name: Publish late command status action ledger
12541264
if: ${{ always() && steps.setup-state.outcome == 'success' && steps.setup-pnpm.outcome == 'success' && steps.publish-event-result.outputs.requeue_latest == 'true' && steps.complete-exact-review-queue.outcome == 'success' }}
12551265
run: |
12561266
set -euo pipefail
12571267
test -n "${CLAWSWEEPER_STATE_DIR:-}"
12581268
source_root="${CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT:?setup-action-ledger output root is required}"
1259-
if [ ! -d "$source_root/ledger" ]; then
1260-
echo "Expected late command status action event shards in $source_root, but none were finalized." >&2
1261-
exit 1
1262-
fi
1263-
command_shard_found=false
1264-
while IFS= read -r -d '' shard_path; do
1265-
if jq -e 'select(.event_type | startswith("command."))' "$shard_path" >/dev/null; then
1266-
command_shard_found=true
1267-
break
1268-
fi
1269-
done < <(find "$source_root/ledger/v1/events" -type f -name '*.jsonl' -print0)
1270-
if [ "$command_shard_found" != "true" ]; then
1271-
echo "Expected command receipt events, but the finalized shard set contained none." >&2
1272-
exit 1
1273-
fi
1269+
manifest_file=".artifacts/late-command-status-action-ledger-manifest.json"
1270+
test -s "$manifest_file"
12741271
event_paths_file=".artifacts/late-command-status-action-ledger-paths.txt"
12751272
import_result_file=".artifacts/late-command-status-action-ledger-import.json"
1276-
mkdir -p .artifacts
1277-
pnpm run --silent publish-action-events -- \
1273+
pnpm run --silent repair:action-ledger -- publish \
1274+
--lane late-command-status \
1275+
--manifest "$manifest_file" \
12781276
--source-root "$source_root" \
12791277
--state-root "$CLAWSWEEPER_STATE_DIR" > "$import_result_file"
1280-
if ! jq -e '.created > 0' "$import_result_file" >/dev/null; then
1281-
echo "Late command status action shards were expected but no new shards were imported." >&2
1278+
if ! jq -e --slurpfile manifest "$manifest_file" \
1279+
'.eventPaths == $manifest[0].event_paths' \
1280+
"$import_result_file" >/dev/null; then
1281+
echo "Imported late command status paths do not match the finalized manifest." >&2
12821282
exit 1
12831283
fi
12841284
jq -r '.paths[]?' "$import_result_file" |

‎CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ checkpoint, and status-only commits are intentionally omitted.
7979
production-wired durable attempt identities through dispatch claims and worker
8080
receipt keys; and bounded requeues dispatch the same original source path
8181
bound to their digest and depth before fail-closed immutable publication from
82-
the setup-provided action-ledger output root to the state repository.
82+
the setup-provided action-ledger output root to the state repository. Each
83+
command lane binds publication to a canonical, run-scoped finalized-shard
84+
manifest and rejects any missing producer path before state import.
8385
- Short-circuited authenticated duplicate comment deliveries when their exact
8486
body version is already terminal in the durable router ledger, while edited,
8587
retryable, and state-drifted commands retain the full routing path.

‎src/action-ledger-runtime.ts‎

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,10 @@ function startActionEventCrabFleetPost(
973973
export function importActionEventShards(
974974
sourceRoot: string,
975975
destinationRoot: string,
976-
options: { expectedProducer?: ExpectedActionEventProducer | undefined } = {},
976+
options: {
977+
expectedProducer?: ExpectedActionEventProducer | undefined;
978+
expectedEventPaths?: readonly string[] | undefined;
979+
} = {},
977980
): ActionEventShardImportResult {
978981
const emptyResult = (): ActionEventShardImportResult => ({
979982
created: 0,
@@ -983,21 +986,33 @@ export function importActionEventShards(
983986
completionPaths: [],
984987
paths: [],
985988
});
989+
const expectedEventPaths = options.expectedEventPaths
990+
? validateExpectedActionEventPaths(options.expectedEventPaths)
991+
: null;
986992
let safeSource: SafeReadRoot;
987993
try {
988994
safeSource = prepareSafeReadRoot(sourceRoot, "action event shard import source");
989995
} catch (error) {
990-
if (isNotFoundError(error)) return emptyResult();
996+
if (isNotFoundError(error) && expectedEventPaths === null) return emptyResult();
997+
if (isNotFoundError(error)) {
998+
throw new Error("action event shard manifest source root is missing");
999+
}
9911000
throw error;
9921001
}
9931002
let relativePaths: string[];
994-
try {
995-
relativePaths = collectActionEventShardFiles(safeSource);
996-
} catch (error) {
997-
if (isNotFoundError(error)) return emptyResult();
998-
throw error;
1003+
if (expectedEventPaths) {
1004+
relativePaths = expectedEventPaths;
1005+
} else {
1006+
try {
1007+
relativePaths = collectActionEventShardFiles(safeSource);
1008+
} catch (error) {
1009+
if (isNotFoundError(error)) return emptyResult();
1010+
throw error;
1011+
}
9991012
}
1000-
const shards = readImportedActionEventShards(safeSource, relativePaths);
1013+
const shards = readImportedActionEventShards(safeSource, relativePaths, {
1014+
requireManifestPaths: expectedEventPaths !== null,
1015+
});
10011016
if (options.expectedProducer) {
10021017
validateImportedActionEventProducer(shards, options.expectedProducer);
10031018
}
@@ -1359,14 +1374,23 @@ function readImportedActionEventIdentityBinding(
13591374
function readImportedActionEventShards(
13601375
source: SafeReadRoot,
13611376
relativePaths: readonly string[],
1377+
options: { requireManifestPaths?: boolean } = {},
13621378
): ImportedActionEventShard[] {
13631379
let totalBytes = 0;
13641380
const contents = relativePaths.map((relativePath) => {
13651381
if (!ACTION_EVENT_SHARD_PATH_PATTERN.test(relativePath)) {
13661382
throw new Error(`invalid action event shard path: ${relativePath}`);
13671383
}
13681384
const target = prepareSafeReadTarget(source, relativePath, "action event shard import source");
1369-
const content = readUtf8FileNoFollow(target, ACTION_EVENT_SHARD_IMPORT_LIMITS.maxFileBytes);
1385+
let content: string;
1386+
try {
1387+
content = readUtf8FileNoFollow(target, ACTION_EVENT_SHARD_IMPORT_LIMITS.maxFileBytes);
1388+
} catch (error) {
1389+
if (options.requireManifestPaths && isNotFoundError(error)) {
1390+
throw new Error(`action event shard manifest path is missing: ${relativePath}`);
1391+
}
1392+
throw error;
1393+
}
13701394
const bytes = Buffer.byteLength(content, "utf8");
13711395
totalBytes += bytes;
13721396
if (totalBytes > ACTION_EVENT_SHARD_IMPORT_LIMITS.maxTotalBytes) {
@@ -1401,6 +1425,30 @@ function readImportedActionEventShards(
14011425
return validated;
14021426
}
14031427

1428+
function validateExpectedActionEventPaths(paths: readonly string[]): string[] {
1429+
if (paths.length === 0) {
1430+
throw new Error("action event shard manifest is empty");
1431+
}
1432+
if (paths.length > ACTION_EVENT_SHARD_IMPORT_LIMITS.maxFiles) {
1433+
throw new Error(
1434+
`action event shard manifest exceeds ${ACTION_EVENT_SHARD_IMPORT_LIMITS.maxFiles} event paths`,
1435+
);
1436+
}
1437+
const canonical = [...new Set(paths)].sort();
1438+
if (
1439+
canonical.length !== paths.length ||
1440+
canonical.some((value, index) => value !== paths[index])
1441+
) {
1442+
throw new Error("action event shard manifest paths must be sorted and unique");
1443+
}
1444+
for (const relativePath of canonical) {
1445+
if (!ACTION_EVENT_SHARD_PATH_PATTERN.test(relativePath)) {
1446+
throw new Error(`invalid action event shard manifest path: ${relativePath}`);
1447+
}
1448+
}
1449+
return canonical;
1450+
}
1451+
14041452
function importedShardReplayEquivalent(
14051453
content: string,
14061454
candidate: ImportedActionEventShard,

‎src/repair/action-ledger-cli.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
#!/usr/bin/env node
2+
import fs from "node:fs";
23
import path from "node:path";
34

45
import { importActionEventShards } from "../action-ledger-runtime.js";
5-
import { flushCommandActionEvents } from "./command-action-ledger.js";
6+
import {
7+
finalizeCommandActionLedgerManifest,
8+
parseCommandActionLedgerManifest,
9+
serializeCommandActionLedgerManifest,
10+
} from "./command-action-ledger-manifest.js";
611
import { repoRoot } from "./paths.js";
712

813
const [command, ...argv] = process.argv.slice(2);
14+
const args = parseArgs(argv);
915

1016
if (command === "finalize") {
11-
const paths = await flushCommandActionEvents();
12-
console.log(JSON.stringify({ paths }, null, 2));
17+
const lane = requiredArg(args.lane, "--lane");
18+
const manifest = await finalizeCommandActionLedgerManifest(lane);
19+
process.stdout.write(serializeCommandActionLedgerManifest(manifest));
1320
} else if (command === "publish") {
14-
const args = parseArgs(argv);
21+
const lane = requiredArg(args.lane, "--lane");
22+
const manifestPath = path.resolve(requiredArg(args.manifest, "--manifest"));
23+
const manifest = parseCommandActionLedgerManifest(fs.readFileSync(manifestPath, "utf8"), lane);
1524
const sourceRoot = path.resolve(args.sourceRoot ?? actionLedgerOutputRoot());
1625
const stateRoot = path.resolve(args.stateRoot ?? repoRoot());
17-
console.log(JSON.stringify(importActionEventShards(sourceRoot, stateRoot), null, 2));
26+
console.log(
27+
JSON.stringify(
28+
importActionEventShards(sourceRoot, stateRoot, {
29+
expectedProducer: {
30+
repository: manifest.repository,
31+
sha: manifest.sha,
32+
workflow: manifest.workflow,
33+
job: manifest.job,
34+
runId: manifest.run_id,
35+
runAttempt: manifest.run_attempt,
36+
},
37+
expectedEventPaths: manifest.event_paths,
38+
}),
39+
null,
40+
2,
41+
),
42+
);
1843
} else {
1944
throw new Error(
20-
"usage: action-ledger-cli.ts <finalize|publish> [--source-root path --state-root path]",
45+
"usage: action-ledger-cli.ts <finalize|publish> --lane name [--manifest path --source-root path --state-root path]",
2146
);
2247
}
2348

@@ -29,10 +54,12 @@ function actionLedgerOutputRoot(): string {
2954
}
3055

3156
function parseArgs(argv: readonly string[]) {
32-
const parsed: { sourceRoot?: string; stateRoot?: string } = {};
57+
const parsed: { lane?: string; manifest?: string; sourceRoot?: string; stateRoot?: string } = {};
3358
for (let index = 0; index < argv.length; index += 1) {
3459
const arg = argv[index];
35-
if (arg === "--source-root") parsed.sourceRoot = requiredValue(argv, ++index, arg);
60+
if (arg === "--lane") parsed.lane = requiredValue(argv, ++index, arg);
61+
else if (arg === "--manifest") parsed.manifest = requiredValue(argv, ++index, arg);
62+
else if (arg === "--source-root") parsed.sourceRoot = requiredValue(argv, ++index, arg);
3663
else if (arg === "--state-root") parsed.stateRoot = requiredValue(argv, ++index, arg);
3764
else throw new Error(`unknown argument: ${arg}`);
3865
}
@@ -44,3 +71,8 @@ function requiredValue(argv: readonly string[], index: number, flag: string): st
4471
if (!value || value.startsWith("--")) throw new Error(`${flag} requires a value`);
4572
return value;
4673
}
74+
75+
function requiredArg(value: string | undefined, flag: string): string {
76+
if (!value) throw new Error(`${flag} is required`);
77+
return value;
78+
}

0 commit comments

Comments
 (0)