Skip to content

Commit 1767e1e

Browse files
committed
fix: normalize downgraded publication demand
1 parent a39f19e commit 1767e1e

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

‎dashboard/exact-review-queue.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7710,7 +7710,8 @@ export class ExactReviewQueue {
77107710
}
77117711

77127712
private refreshPublicationControlSync(state: ExactReviewQueueState, now: number) {
7713-
const current = this.publicationControlSync();
7713+
const stored = this.storage.kv.get(EXACT_REVIEW_PUBLICATION_CONTROL_KEY);
7714+
const current = exactReviewPublicationControl(this.env, stored);
77147715
const publications = Object.values(state.items).filter(exactReviewQueueIsPublication);
77157716
const pending = publications.filter((item) => item.state === "pending");
77167717
const oldestPendingAt = pending.reduce<number | null>(
@@ -7724,7 +7725,7 @@ export class ExactReviewQueue {
77247725
oldestPendingAgeMs: oldestPendingAt === null ? 0 : Math.max(0, now - oldestPendingAt),
77257726
netDrainRatePerHour: flow.net_drain_rate_per_hour,
77267727
});
7727-
if (stableJson(next) !== stableJson(current)) {
7728+
if (stableJson(next) !== stableJson(objectValue(stored))) {
77287729
this.storage.kv.put(EXACT_REVIEW_PUBLICATION_CONTROL_KEY, next);
77297730
}
77307731
return next;
@@ -11819,19 +11820,31 @@ function exactReviewPublicationControl(env, value: unknown): ExactReviewPublicat
1181911820
const control = objectValue(value);
1182011821
const maximum = exactReviewPublicationMaximum(env);
1182111822
const minimum = exactReviewPublicationMinimum(env, maximum);
11823+
const base = exactReviewPublicationBase(env, maximum);
1182211824
const rawCeiling = Number(control.capacityCeiling);
1182311825
const rawDemandCapacity = Number(control.demandCapacity);
1182411826
const rawCooldown = Number(control.cooldownUntil);
1182511827
const rawRecoverySuccesses = Number(control.recoverySuccesses);
1182611828
const rawLastFailureAt = Number(control.lastFailureAt);
1182711829
const lastFailureKind = exactReviewPublicationFailureKind(control.lastFailureKind);
11830+
// A fixed higher-capacity policy persists both values above a later policy's
11831+
// maximum (for example the temporary 50/50/50 override). On downgrade, the
11832+
// ceiling may retain the new maximum, but demand must restart at the new base
11833+
// instead of turning the old fixed target into a permanent maximum target.
11834+
const fixedPolicyDowngrade =
11835+
Number.isSafeInteger(rawCeiling) &&
11836+
Number.isSafeInteger(rawDemandCapacity) &&
11837+
rawCeiling > maximum &&
11838+
rawDemandCapacity > maximum;
1182811839
return {
1182911840
capacityCeiling: Number.isSafeInteger(rawCeiling)
1183011841
? Math.max(minimum, Math.min(maximum, rawCeiling))
1183111842
: maximum,
11832-
demandCapacity: Number.isSafeInteger(rawDemandCapacity)
11833-
? Math.max(minimum, Math.min(maximum, rawDemandCapacity))
11834-
: exactReviewPublicationBase(env, maximum),
11843+
demandCapacity: fixedPolicyDowngrade
11844+
? base
11845+
: Number.isSafeInteger(rawDemandCapacity)
11846+
? Math.max(minimum, Math.min(maximum, rawDemandCapacity))
11847+
: base,
1183511848
cooldownUntil: Number.isSafeInteger(rawCooldown) && rawCooldown > 0 ? rawCooldown : 0,
1183611849
recoverySuccesses:
1183711850
Number.isSafeInteger(rawRecoverySuccesses) && rawRecoverySuccesses > 0

‎test/dashboard-worker.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,71 @@ test("exact-review publication admission applies the hysteresis controller deman
16481648
}
16491649
});
16501650

1651+
test("exact-review publication controller adopts the staged base after a fixed-policy downgrade", async () => {
1652+
const originalNow = Date.now;
1653+
const now = Date.parse("2026-08-10T09:00:00.000Z");
1654+
Date.now = () => now;
1655+
try {
1656+
const storage = new MemoryDurableStorage();
1657+
const items: Record<string, ReturnType<typeof leasedExactReviewPublicationItem>> = {};
1658+
for (let index = 0; index < 50; index += 1) {
1659+
const item = leasedExactReviewPublicationItem(20_000 + index, String(200_000 + index));
1660+
item.state = "pending";
1661+
item.createdAt = now - 60_000;
1662+
item.updatedAt = now - 60_000;
1663+
items[item.key] = item;
1664+
}
1665+
await storage.put("exact-review-queue", { deliveries: {}, items });
1666+
await storage.put("exact-review-publication-control:v1", {
1667+
capacityCeiling: 50,
1668+
demandCapacity: 50,
1669+
cooldownUntil: 0,
1670+
recoverySuccesses: 0,
1671+
demandSamples: 0,
1672+
demandTier: 0,
1673+
lastDemandSampleAt: now,
1674+
lastScaleAt: now,
1675+
});
1676+
const queue = new ExactReviewQueue(
1677+
{ storage },
1678+
{
1679+
EXACT_REVIEW_PUBLICATION_MIN_CONCURRENT: "8",
1680+
EXACT_REVIEW_PUBLICATION_BASE_CONCURRENT: "32",
1681+
EXACT_REVIEW_PUBLICATION_MAX_CONCURRENT: "40",
1682+
},
1683+
);
1684+
1685+
const stats = await (
1686+
await queue.fetch(new Request("https://clawsweeper-exact-review-queue/stats"))
1687+
).json();
1688+
assert.equal(stats.lanes.publication.pending, 50);
1689+
assert.deepEqual(stats.lanes.publication.capacity_control, {
1690+
mode: "adaptive",
1691+
minimum: 8,
1692+
base: 32,
1693+
maximum: 40,
1694+
ceiling: 40,
1695+
demand_capacity: 32,
1696+
demand_samples: 0,
1697+
demand_tier: 0,
1698+
last_scale_at: new Date(now).toISOString(),
1699+
cooldown_until: null,
1700+
recovery_successes: 0,
1701+
last_failure_at: null,
1702+
last_failure_kind: null,
1703+
});
1704+
assert.equal(stats.lanes.publication.capacity, 32);
1705+
const persisted = (await storage.get("exact-review-publication-control:v1")) as {
1706+
capacityCeiling: number;
1707+
demandCapacity: number;
1708+
};
1709+
assert.equal(persisted.capacityCeiling, 40);
1710+
assert.equal(persisted.demandCapacity, 32);
1711+
} finally {
1712+
Date.now = originalNow;
1713+
}
1714+
});
1715+
16511716
test("exact-review queue debounces fresh work and caps pending revision extensions", async () => {
16521717
const originalNow = Date.now;
16531718
let now = 1_000_000;

0 commit comments

Comments
 (0)