Skip to content

Commit 1cfa23d

Browse files
committed
fix(apply): separate possible label receipts
1 parent f42fc59 commit 1cfa23d

5 files changed

Lines changed: 45 additions & 18 deletions

‎src/clawsweeper-apply-decision-workflow.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
504504
mutationGuardBoundaryReason = null;
505505
resetGuardReadCache();
506506
};
507-
const rememberPublishedLabelMutation = (): void => {
508-
rememberPublishedLabelSync();
507+
const rememberLabelMutationResult = (confirmed: boolean): void => {
508+
if (confirmed) rememberPublishedLabelSync();
509509
rememberSelfMutationUpdatedAt();
510510
deferredSelfMutationReceipt = false;
511511
resetMutationGuardBoundary();
@@ -517,7 +517,7 @@ export function createApplyDecisionWorkflow(dependencies: CreateApplyDecisionWor
517517
const result = flushIssueLabelMutationBatch(
518518
number,
519519
resetMutationGuardBoundary,
520-
rememberPublishedLabelMutation,
520+
rememberLabelMutationResult,
521521
);
522522
if (result.skippedAdditions.length > 0) {
523523
reconcileSkippedIssueLabelAdditions(result.skippedAdditions);

‎src/clawsweeper-apply-dependencies.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export interface CreateApplyDecisionWorkflowDependencies {
195195
flushIssueLabelMutationBatch: (
196196
number: number,
197197
beforeItemMutation?: () => void,
198-
afterItemMutation?: () => void,
198+
afterItemMutation?: (confirmed: boolean) => void,
199199
) => {
200200
itemMutationPublished: boolean;
201201
repositoryDefinitionMutated: boolean;

‎src/clawsweeper-label-mutations.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export function createLabelMutationOperations(
220220
function flushIssueLabelMutationBatch(
221221
number: number,
222222
beforeItemMutation?: () => void,
223-
afterItemMutation?: () => void,
223+
afterItemMutation?: (confirmed: boolean) => void,
224224
): {
225225
itemMutationPublished: boolean;
226226
repositoryDefinitionMutated: boolean;
@@ -303,7 +303,7 @@ export function createLabelMutationOperations(
303303
args,
304304
onMutation: batch.onMutation,
305305
});
306-
afterItemMutation?.();
306+
afterItemMutation?.(true);
307307
return {
308308
itemMutationPublished: true,
309309
repositoryDefinitionMutated: definitionMutated,
@@ -312,9 +312,10 @@ export function createLabelMutationOperations(
312312
} catch (error) {
313313
if (!isOptionalFailure(error)) throw error;
314314
// `gh issue edit` can apply removals before a missing optional addition
315-
// makes the command exit nonzero. Conservatively receipt the live item
316-
// before any guarded fallback command.
317-
afterItemMutation?.();
315+
// makes the command exit nonzero. Refresh the live self-mutation receipt
316+
// before any guarded fallback command, but do not claim a confirmed label
317+
// sync unless a fallback succeeds.
318+
afterItemMutation?.(false);
318319
console.warn(
319320
`Combined optional label sync for item ${number} failed; retrying its final operations individually: ${
320321
error instanceof Error ? error.message : String(error)
@@ -328,7 +329,7 @@ export function createLabelMutationOperations(
328329
args: ["issue", "edit", String(number), "--remove-label", removals.join(",")],
329330
onMutation: batch.onMutation,
330331
});
331-
afterItemMutation?.();
332+
afterItemMutation?.(true);
332333
itemMutationPublished = true;
333334
}
334335
const fallbackAdditions = [
@@ -347,7 +348,7 @@ export function createLabelMutationOperations(
347348
optional &&
348349
(missingLabelError(fallbackError, label) || labelCapacityError(fallbackError)),
349350
});
350-
afterItemMutation?.();
351+
afterItemMutation?.(true);
351352
itemMutationPublished = true;
352353
} catch (fallbackError) {
353354
if (

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,12 @@ test("blocked exact close publication discards staged labels before writing the
774774
const blockedCloseStart = source.indexOf("if (closeBlockedForCommentSync) {");
775775
const blockedCloseEnd = source.indexOf("clawSweeperLabelsChanged &&", blockedCloseStart);
776776
const blockedClose = source.slice(blockedCloseStart, blockedCloseEnd);
777-
const publishedLabelReceiptStart = source.indexOf("const rememberPublishedLabelMutation =");
778-
const publishedLabelReceiptEnd = source.indexOf(
777+
const labelMutationResultStart = source.indexOf("const rememberLabelMutationResult =");
778+
const labelMutationResultEnd = source.indexOf(
779779
"const flushIssueLabelBatch =",
780-
publishedLabelReceiptStart,
780+
labelMutationResultStart,
781781
);
782-
const publishedLabelReceipt = source.slice(publishedLabelReceiptStart, publishedLabelReceiptEnd);
782+
const labelMutationResult = source.slice(labelMutationResultStart, labelMutationResultEnd);
783783
const stagedLabelReceiptStart = source.indexOf("const rememberLabelMutationUpdatedAt =");
784784
const stagedLabelReceiptEnd = source.indexOf(
785785
"const previousApplyMutationRunner =",
@@ -793,7 +793,10 @@ test("blocked exact close publication discards staged labels before writing the
793793
blockedClose,
794794
/if \(!needsReviewCommentSync\) \{\s*discardIssueLabelBatch\(\);[\s\S]*?writeReportMarkdown\(path, markdown\);/,
795795
);
796-
assert.match(publishedLabelReceipt, /rememberPublishedLabelSync\(\);/);
796+
assert.ok(labelMutationResultStart >= 0);
797+
assert.ok(labelMutationResultEnd > labelMutationResultStart);
798+
assert.match(labelMutationResult, /if \(confirmed\) rememberPublishedLabelSync\(\);/);
799+
assert.match(labelMutationResult, /rememberSelfMutationUpdatedAt\(\);/);
797800
assert.match(
798801
stagedLabelReceipt,
799802
/if \(issueLabelBatchActive\) deferredSelfMutationReceipt = true;/,

‎test/label-mutation-batch.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ test("optional batch failures retain successful final operations and report skip
278278
const result = operations.flushIssueLabelMutationBatch(
279279
321,
280280
() => events.push("freshness"),
281-
() => events.push("receipt"),
281+
(confirmed) => events.push(confirmed ? "receipt" : "possible mutation"),
282282
);
283283

284284
assert.equal(result.itemMutationPublished, true);
@@ -295,7 +295,7 @@ test("optional batch failures retain successful final operations and report skip
295295
assert.deepEqual(events, [
296296
"freshness",
297297
"issue edit 321 --add-label impact:message-loss,P2 --remove-label P1",
298-
"receipt",
298+
"possible mutation",
299299
"freshness",
300300
"issue edit 321 --remove-label P1",
301301
"receipt",
@@ -307,6 +307,29 @@ test("optional batch failures retain successful final operations and report skip
307307
]);
308308
});
309309

310+
test("a rejected optional-only batch does not publish a label-sync receipt", () => {
311+
const capacityFailure = new Error("labels can have a maximum of 100 labels");
312+
let labelsSyncedAt: string | undefined;
313+
const receiptCertainty: boolean[] = [];
314+
const { operations } = createOperations({
315+
mutate: ({ args }) => {
316+
if (args.includes("--add-label")) throw capacityFailure;
317+
},
318+
});
319+
320+
operations.beginIssueLabelMutationBatch(321);
321+
operations.tryAddOptionalLabel({ number: 321, label: "P2", currentLabels: [] });
322+
const result = operations.flushIssueLabelMutationBatch(321, undefined, (confirmed) => {
323+
receiptCertainty.push(confirmed);
324+
if (confirmed) labelsSyncedAt = new Date().toISOString();
325+
});
326+
327+
assert.equal(result.itemMutationPublished, false);
328+
assert.deepEqual(result.skippedAdditions, ["P2"]);
329+
assert.deepEqual(receiptCertainty, [false]);
330+
assert.equal(labelsSyncedAt, undefined);
331+
});
332+
310333
test("capacity fallback publishes required additions before optional additions", () => {
311334
const capacityFailure = new Error("labels can have a maximum of 100 labels");
312335
let individualAdditionPublished = false;

0 commit comments

Comments
 (0)