Skip to content

Commit 0e1dc0f

Browse files
committed
fix(ledger): accept prior-attempt shards on rerun
1 parent 5bfa2d9 commit 0e1dc0f

6 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎.github/workflows/sweep.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4467,7 +4467,8 @@ jobs:
44674467
pnpm run --silent publish-action-events -- \
44684468
--source-root .clawsweeper-repair/action-ledger-download \
44694469
--state-root . \
4470-
--expected-producer-job review |
4470+
--expected-producer-job review \
4471+
--expected-producer-max-run-attempt "$GITHUB_RUN_ATTEMPT" |
44714472
jq -r '.paths[]?' >> .artifacts/action-ledger-paths.txt
44724473
pnpm run --silent publish-action-events -- \
44734474
--source-root "$CLAWSWEEPER_ACTION_LEDGER_OUTPUT_ROOT" \

‎src/action-ledger-runtime.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,17 @@ export type ActionEventShardImportResult = {
152152
paths: string[];
153153
};
154154

155-
export type ExpectedActionEventProducer = {
155+
type ExpectedActionEventProducerIdentity = {
156156
repository: string;
157157
sha: string;
158158
workflow: string;
159159
job: string;
160160
runId: string;
161-
runAttempt: number;
162161
};
163162

163+
export type ExpectedActionEventProducer = ExpectedActionEventProducerIdentity &
164+
({ runAttempt: number; maxRunAttempt?: never } | { runAttempt?: never; maxRunAttempt: number });
165+
164166
type ImportedActionEventShard = {
165167
relativePath: string;
166168
content: string;
@@ -1111,6 +1113,17 @@ function validateImportedActionEventProducer(
11111113
shards: readonly ImportedActionEventShard[],
11121114
expected: ExpectedActionEventProducer,
11131115
): void {
1116+
if (
1117+
(expected.runAttempt === undefined) === (expected.maxRunAttempt === undefined) ||
1118+
(expected.runAttempt !== undefined &&
1119+
(!Number.isSafeInteger(expected.runAttempt) || expected.runAttempt < 1)) ||
1120+
(expected.maxRunAttempt !== undefined &&
1121+
(!Number.isSafeInteger(expected.maxRunAttempt) || expected.maxRunAttempt < 1))
1122+
) {
1123+
throw new Error(
1124+
"action event shard producer provenance requires one positive run attempt constraint",
1125+
);
1126+
}
11141127
for (const shard of shards) {
11151128
const actual = shard.identity;
11161129
const mismatched = (
@@ -1120,14 +1133,23 @@ function validateImportedActionEventProducer(
11201133
["workflow", actual.workflow, expected.workflow],
11211134
["job", actual.job, expected.job],
11221135
["run_id", actual.runId, expected.runId],
1123-
["run_attempt", actual.runAttempt, expected.runAttempt],
11241136
] as const
11251137
).find(([, value, expectedValue]) => value !== expectedValue);
11261138
if (mismatched) {
11271139
throw new Error(
11281140
`action event shard producer provenance mismatch for ${mismatched[0]}: expected ${mismatched[2]}, got ${mismatched[1]}`,
11291141
);
11301142
}
1143+
if (expected.runAttempt !== undefined && actual.runAttempt !== expected.runAttempt) {
1144+
throw new Error(
1145+
`action event shard producer provenance mismatch for run_attempt: expected ${expected.runAttempt}, got ${actual.runAttempt}`,
1146+
);
1147+
}
1148+
if (expected.maxRunAttempt !== undefined && actual.runAttempt > expected.maxRunAttempt) {
1149+
throw new Error(
1150+
`action event shard producer provenance mismatch for run_attempt: expected at most ${expected.maxRunAttempt}, got ${actual.runAttempt}`,
1151+
);
1152+
}
11311153
}
11321154
}
11331155

‎src/clawsweeper-action-commands.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export function createActionCommands(dependencies: CreateActionCommandsDependenc
3434
throw new UserFacingCommandError("--expected-producer-job is required");
3535
}
3636
const expectedProducerRunAttempt = optionalNumberArg(args.expected_producer_run_attempt);
37+
const expectedProducerMaxRunAttempt = optionalNumberArg(args.expected_producer_max_run_attempt);
38+
if (expectedProducerRunAttempt !== undefined && expectedProducerMaxRunAttempt !== undefined) {
39+
throw new UserFacingCommandError(
40+
"--expected-producer-run-attempt and --expected-producer-max-run-attempt are mutually exclusive",
41+
);
42+
}
3743
if (
3844
expectedProducerRunAttempt !== undefined &&
3945
(!Number.isInteger(expectedProducerRunAttempt) || expectedProducerRunAttempt < 1)
@@ -42,6 +48,14 @@ export function createActionCommands(dependencies: CreateActionCommandsDependenc
4248
"--expected-producer-run-attempt must be a positive integer",
4349
);
4450
}
51+
if (
52+
expectedProducerMaxRunAttempt !== undefined &&
53+
(!Number.isInteger(expectedProducerMaxRunAttempt) || expectedProducerMaxRunAttempt < 1)
54+
) {
55+
throw new UserFacingCommandError(
56+
"--expected-producer-max-run-attempt must be a positive integer",
57+
);
58+
}
4559
const expectedProducerRunId = stringArg(args.expected_producer_run_id, "");
4660
if (expectedProducerRunId && !/^\d{1,30}$/.test(expectedProducerRunId)) {
4761
throw new UserFacingCommandError(
@@ -53,14 +67,18 @@ export function createActionCommands(dependencies: CreateActionCommandsDependenc
5367
throw new UserFacingCommandError("--expected-producer-sha must be a lowercase commit SHA");
5468
}
5569
const currentProducer = workflowActionProducer("action_event_publisher");
70+
const expectedRunAttempt =
71+
expectedProducerMaxRunAttempt === undefined
72+
? { runAttempt: expectedProducerRunAttempt ?? currentProducer.runAttempt }
73+
: { maxRunAttempt: expectedProducerMaxRunAttempt };
5674
const result = importActionEventShards(sourceRoot, stateRoot, {
5775
expectedProducer: {
5876
repository: currentProducer.repository,
5977
sha: expectedProducerSha || currentProducer.sha,
6078
workflow: currentProducer.workflow,
6179
job: expectedProducerJob,
6280
runId: expectedProducerRunId || currentProducer.runId,
63-
runAttempt: expectedProducerRunAttempt ?? currentProducer.runAttempt,
81+
...expectedRunAttempt,
6482
},
6583
});
6684
console.log(JSON.stringify(result, null, 2));

‎test/action-ledger-runtime.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,64 @@ test("state shard imports reject producer provenance outside the authenticated w
39343934
);
39353935
});
39363936

3937+
test("state shard imports accept retained prior attempts only through an explicit upper bound", async () => {
3938+
const root = tempRoot();
3939+
const source = trustedChildRoot(root, "source");
3940+
const destination = trustedChildRoot(root, "destination");
3941+
const attemptOneEnv = workflowEnv({ GITHUB_RUN_ATTEMPT: "1" });
3942+
const event = recordReview(root, attemptOneEnv);
3943+
assert.ok(event);
3944+
await flushWorkflowActionEvents(root, {
3945+
env: attemptOneEnv,
3946+
outputRoot: source,
3947+
});
3948+
const expectedIdentity = {
3949+
repository: event.producer.repository,
3950+
sha: event.producer.sha,
3951+
workflow: event.producer.workflow,
3952+
job: event.producer.job,
3953+
runId: event.producer.run_id,
3954+
};
3955+
3956+
assert.throws(
3957+
() =>
3958+
importActionEventShards(source, destination, {
3959+
expectedProducer: { ...expectedIdentity, runAttempt: 2 },
3960+
}),
3961+
/producer provenance mismatch for run_attempt: expected 2, got 1/,
3962+
);
3963+
assert.equal(
3964+
importActionEventShards(source, destination, {
3965+
expectedProducer: { ...expectedIdentity, maxRunAttempt: 2 },
3966+
}).created,
3967+
1,
3968+
);
3969+
3970+
const futureRoot = tempRoot();
3971+
const futureSource = trustedChildRoot(futureRoot, "source");
3972+
const futureDestination = trustedChildRoot(futureRoot, "destination");
3973+
const attemptThreeEnv = workflowEnv({ GITHUB_RUN_ATTEMPT: "3" });
3974+
recordReview(futureRoot, attemptThreeEnv);
3975+
await flushWorkflowActionEvents(futureRoot, {
3976+
env: attemptThreeEnv,
3977+
outputRoot: futureSource,
3978+
});
3979+
assert.throws(
3980+
() =>
3981+
importActionEventShards(futureSource, futureDestination, {
3982+
expectedProducer: { ...expectedIdentity, maxRunAttempt: 2 },
3983+
}),
3984+
/producer provenance mismatch for run_attempt: expected at most 2, got 3/,
3985+
);
3986+
assert.throws(
3987+
() =>
3988+
importActionEventShards(source, trustedChildRoot(root, "invalid-constraint"), {
3989+
expectedProducer: { ...expectedIdentity, maxRunAttempt: Number.NaN },
3990+
}),
3991+
/requires one positive run attempt constraint/,
3992+
);
3993+
});
3994+
39373995
test("state shard imports validate duplicate and causal integrity across shard parts", () => {
39383996
const root = tempRoot();
39393997
const event = recordReview(root);

‎test/clawsweeper-action-ledger.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,34 @@ test("action event import rejects an invalid expected producer run ID", async ()
102102
);
103103
});
104104

105+
test("action event import rejects an invalid maximum producer run attempt", async () => {
106+
await assert.rejects(
107+
main([
108+
"publish-action-events",
109+
"--expected-producer-job",
110+
"review",
111+
"--expected-producer-max-run-attempt",
112+
"0",
113+
]),
114+
/expected-producer-max-run-attempt must be a positive integer/,
115+
);
116+
});
117+
118+
test("action event import keeps exact and maximum producer attempts mutually exclusive", async () => {
119+
await assert.rejects(
120+
main([
121+
"publish-action-events",
122+
"--expected-producer-job",
123+
"review",
124+
"--expected-producer-run-attempt",
125+
"1",
126+
"--expected-producer-max-run-attempt",
127+
"2",
128+
]),
129+
/expected-producer-run-attempt and --expected-producer-max-run-attempt are mutually exclusive/,
130+
);
131+
});
132+
105133
test("action event import rejects an invalid expected producer SHA", async () => {
106134
await assert.rejects(
107135
main([

‎test/sweep-workflow.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5789,6 +5789,10 @@ test("every action-ledger publication authenticates the expected producer job",
57895789
assert.equal(commands.length, 7);
57905790
assert.ok(commands.every((command) => command.includes("--expected-producer-job")));
57915791
assert.match(workflow, /--expected-producer-job review/);
5792+
assert.match(
5793+
workflow,
5794+
/--expected-producer-job review \\\n\s+--expected-producer-max-run-attempt "\$GITHUB_RUN_ATTEMPT"/,
5795+
);
57925796
assert.match(workflow, /--expected-producer-job apply-proof/);
57935797
});
57945798

0 commit comments

Comments
 (0)