Skip to content

Commit af567a9

Browse files
cliffhallclaude
andcommitted
fix(sdk-watch): remove only the scope labels the sweep itself applied
Copilot on #5285: a scope label a maintainer had applied before its server fell behind was taken as the sweep's once the rows called for it, so a later refresh removed it when the server caught up. The body now records the scope labels the sweep applied on its last line, and a refresh removes a label only if that record names it. A body without the record (filed before it existed) falls back to its rows, which is what the sweep applied at filing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 635a01f commit af567a9

2 files changed

Lines changed: 142 additions & 16 deletions

File tree

‎scripts/sdk-watch.mjs‎

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
// rewritten when it differs, as the monthly refresh does, except that a
5555
// ticked checklist box alone is not a difference. Its scope labels move by
5656
// the DELTA between what the old body's rows called for and what the new
57-
// rows do, so a label triage or a maintainer applied, or removed, stays as
58-
// they left it. That holds when the last outdated copy catches up or
57+
// rows do, and a label is removed only if the sweep applied it (recorded on
58+
// the body's last line, since a row can call for a label someone had
59+
// already applied), so a label triage or a maintainer applied, or removed,
60+
// stays as they left it. That holds when the last outdated copy catches up or
5961
// leaves the group too: the issue is rewritten to say nothing is behind,
6062
// though the sweep still closes nothing. A closed issue is left alone.
6163
//
@@ -341,31 +343,66 @@ export function bodyScopes(body) {
341343
);
342344
}
343345

346+
/** The record, on a body's last line, of the scope labels the sweep applied. */
347+
const APPLIED_RE = /^<!-- sdk-watch:scopes(?: ([^ ]+))? -->$/m;
348+
349+
/**
350+
* @param {string[]} scopes
351+
* @returns {string} the record line for `scopes`
352+
*/
353+
export function buildAppliedMarker(scopes) {
354+
return `<!-- sdk-watch:scopes${scopes.length ? ` ${scopes.join(",")}` : ""} -->`;
355+
}
356+
357+
/**
358+
* The scope labels the sweep itself applied to an issue. A row calling for
359+
* a label that was already there does not make it the sweep's, so this is
360+
* recorded rather than read off the rows. A body written before the record
361+
* existed falls back to its rows, which is what the sweep applied at filing.
362+
*
363+
* @param {string | undefined} body
364+
* @returns {string[]}
365+
*/
366+
export function appliedScopes(body) {
367+
const match = APPLIED_RE.exec(body ?? "");
368+
if (!match) return bodyScopes(body);
369+
return match[1] ? match[1].split(",") : [];
370+
}
371+
344372
/** A body with every checklist box unticked, so ticking one is not a change. */
345373
const unticked = (body) => (body ?? "").replace(/^- \[[xX]\]/gm, "- [ ]");
346374

347375
/**
348376
* What an OPEN issue for the current target needs to match the current
349377
* state, or `null` when it already does. Labels move only by the delta
350-
* between the old body's rows and the new ones, so labels the sweep did not
351-
* apply (or that someone removed) are left as they are.
378+
* between the old body's rows and the new ones: one is added only when the
379+
* rows newly call for it and it is absent, and removed only when the rows
380+
* stop calling for it and the sweep is the one that applied it. A label
381+
* triage or a maintainer applied, or removed, is left as they left it.
352382
*
353383
* @param {{body?: string, labels?: Array<{name?: string}>}} issue
354384
* @param {NonNullable<ReturnType<typeof groupState>>} state
355385
* @returns {{body: string, addLabels: string[], removeLabels: string[]} | null}
356386
*/
357387
export function refreshPlan(issue, state) {
358-
const body = buildIssueBody(state);
359-
// Labels follow the rows, so an unchanged body means unchanged labels.
360-
if (unticked(issue.body) === unticked(body)) return null;
361388
const had = new Set((issue.labels ?? []).map((l) => l?.name));
362389
const before = bodyScopes(issue.body);
363-
const after = bodyScopes(body);
364-
return {
365-
body,
366-
addLabels: after.filter((l) => !before.includes(l) && !had.has(l)),
367-
removeLabels: before.filter((l) => !after.includes(l) && had.has(l)),
368-
};
390+
const after = bodyScopes(buildIssueBody(state));
391+
const owned = appliedScopes(issue.body);
392+
const addLabels = after.filter((l) => !before.includes(l) && !had.has(l));
393+
const removeLabels = before.filter(
394+
(l) => !after.includes(l) && had.has(l) && owned.includes(l),
395+
);
396+
const applied = [
397+
...new Set([
398+
...owned.filter((l) => !removeLabels.includes(l)),
399+
...addLabels,
400+
]),
401+
].sort();
402+
const body = buildIssueBody(state, applied);
403+
// Labels follow the rows, so an unchanged body means unchanged labels.
404+
if (unticked(issue.body) === unticked(body)) return null;
405+
return { body, addLabels, removeLabels };
369406
}
370407

371408
/**
@@ -410,8 +447,15 @@ export function checklist(state) {
410447
];
411448
}
412449

413-
/** @param {NonNullable<ReturnType<typeof groupState>>} state */
414-
export function buildIssueBody(state) {
450+
/**
451+
* @param {NonNullable<ReturnType<typeof groupState>>} state
452+
* @param {string[]} [applied] the scope labels the sweep has applied; at
453+
* filing, the ones `issueLabels` gives
454+
*/
455+
export function buildIssueBody(
456+
state,
457+
applied = issueLabels(state).filter((l) => !SWEEP_LABELS.includes(l)),
458+
) {
415459
const { group, rows, target } = state;
416460
const registry = group.ecosystem === "pypi" ? "PyPI" : "npm";
417461
const table = rows
@@ -439,6 +483,7 @@ export function buildIssueBody(state) {
439483
`Release notes: https://github.com/${group.repo}/releases`,
440484
"",
441485
"Rewritten by the nightly SDK watch (#4874) when the last outdated copy cleared. The sweep closes nothing: close this issue by hand once a maintainer has confirmed it.",
486+
buildAppliedMarker(applied),
442487
].join("\n");
443488
}
444489

@@ -467,6 +512,7 @@ export function buildIssueBody(state) {
467512
"An automated review of what changed upstream, and which parts of these servers it touches, is posted as a comment below.",
468513
"",
469514
"A later run will not refile this issue. A **further** release files its own issue and leaves a supersession note here.",
515+
buildAppliedMarker(applied),
470516
].join("\n");
471517
}
472518

‎scripts/sdk-watch.test.mjs‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import { parse } from "yaml";
1717
import {
1818
ANALYSIS_MARKER,
1919
SDK_GROUPS,
20+
appliedScopes,
2021
assertEveryPackageWatched,
2122
bodyScopes,
23+
buildAppliedMarker,
2224
buildIssueBody,
2325
buildIssueTitle,
2426
buildMarker,
@@ -29,6 +31,7 @@ import {
2931
hasAnalysis,
3032
issueLabels,
3133
main,
34+
markRows,
3235
needsManifestEdit,
3336
parseMarker,
3437
parseSupersededMarker,
@@ -733,13 +736,90 @@ test("refreshPlan rewrites changed rows and moves only its own scope labels", ()
733736
body: buildIssueBody(was),
734737
labels: [{ name: "server-git" }, { name: "server-memory" }],
735738
};
739+
// The record keeps only what the sweep itself applied: time, not memory.
736740
assert.deepEqual(refreshPlan(trimmed, now), {
737-
body: buildIssueBody(now),
741+
body: buildIssueBody(now, ["server-time"]),
738742
addLabels: [],
739743
removeLabels: ["server-git"],
740744
});
741745
});
742746

747+
test("appliedScopes reads the record, falling back to the rows", () => {
748+
const state = groupState(
749+
group("python-sdk"),
750+
[
751+
row("mcp", "src/time", ">=1", "1.0.0"),
752+
row("mcp", "src/git", ">=1", "1.0.0"),
753+
],
754+
{ mcp: "2.0.0" },
755+
);
756+
const body = buildIssueBody(state);
757+
assert.equal(
758+
body.split("\n").at(-1),
759+
buildAppliedMarker(["server-git", "server-time"]),
760+
);
761+
assert.deepEqual(appliedScopes(body), ["server-git", "server-time"]);
762+
assert.deepEqual(appliedScopes(buildIssueBody(state, [])), []);
763+
// A body written before the record existed: what its rows called for.
764+
const legacy = body.split("\n").slice(0, -1).join("\n");
765+
assert.deepEqual(appliedScopes(legacy), ["server-git", "server-time"]);
766+
assert.deepEqual(appliedScopes(undefined), []);
767+
});
768+
769+
test("a scope label someone applied first survives successive refreshes", () => {
770+
const latest = { mcp: "1.30.0" };
771+
const time = row("mcp", "src/time", ">=1.29.0,<2", "1.29.0");
772+
const night1 = groupState(group("python-sdk"), [time], latest);
773+
// A maintainer had already put server-memory on the issue.
774+
let issue = {
775+
body: buildIssueBody(night1),
776+
labels: ["server-time", "server-memory"].map((name) => ({ name })),
777+
};
778+
const apply = (plan) => ({
779+
body: plan.body,
780+
labels: [
781+
...issue.labels.filter((l) => !plan.removeLabels.includes(l.name)),
782+
...plan.addLabels.map((name) => ({ name })),
783+
],
784+
});
785+
786+
// Night 2: memory falls behind. Its label is already there; not the sweep's.
787+
const night2 = groupState(
788+
group("python-sdk"),
789+
[time, row("mcp", "src/memory", ">=1.29.0,<2", "1.28.0")],
790+
latest,
791+
);
792+
const plan2 = refreshPlan(issue, night2);
793+
assert.deepEqual([plan2.addLabels, plan2.removeLabels], [[], []]);
794+
assert.deepEqual(appliedScopes(plan2.body), ["server-time"]);
795+
issue = apply(plan2);
796+
797+
// Night 3: memory catches up. The sweep did not apply its label, so keeps it.
798+
const night3 = groupState(
799+
group("python-sdk"),
800+
[time, row("mcp", "src/memory", ">=1.29.0,<2", "1.30.0")],
801+
latest,
802+
);
803+
const plan3 = refreshPlan(issue, night3);
804+
assert.deepEqual([plan3.addLabels, plan3.removeLabels], [[], []]);
805+
issue = apply(plan3);
806+
assert.ok(issue.labels.some((l) => l.name === "server-memory"));
807+
808+
// Night 4: time catches up too. Its label was the sweep's, so it goes.
809+
const done = markRows(
810+
group("python-sdk"),
811+
[
812+
row("mcp", "src/time", ">=1.29.0,<2", "1.30.0"),
813+
row("mcp", "src/memory", ">=1.29.0,<2", "1.30.0"),
814+
],
815+
latest,
816+
);
817+
const plan4 = refreshPlan(issue, done);
818+
assert.deepEqual(plan4.removeLabels, ["server-time"]);
819+
assert.deepEqual(appliedScopes(plan4.body), []);
820+
assert.equal(refreshPlan(apply(plan4), done), null);
821+
});
822+
743823
test("an open issue whose installs changed is refreshed in place, once", (t) => {
744824
const noPy = files({ "src/time/uv.lock": UV("2.3.0") });
745825
const [filed] = filedIssues({ files: noPy });

0 commit comments

Comments
 (0)