Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions pm-stack/aggregator/src/sources/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2046,31 +2046,30 @@ export function liveSource(
const alreadyRecorded = opts?.hasEvent
? await opts.hasEvent("orchestration-published", publishRef).catch(() => false)
: false;
if (opts?.onEvent && section.runId !== lastOrchEventRunId && !alreadyRecorded) {
opts.onEvent({
actor: "orchestrator",
kind: "orchestration-published",
ref: `orchestration:${section.runId}`,
summary:
`orchestration run ${section.runId} published: ` +
`${section.tiers.needsDrew.length} needs-user, ${section.tiers.needsOrchestrator.length} needs-orchestrator`,
});
if (opts?.onEvent && section.runId !== lastOrchEventRunId) {
// #17453: the run's full event set — its published actions,
// then the tier changes against the in-memory baseline. The
// publish marker is the completion record and is emitted
// LAST, so a durable marker can only be read as "emitted"
// together with the reconciliation below.
const siblings: NewStreamEvent[] = [];
for (const action of section.actions) {
opts.onEvent({
siblings.push({
at: action.at,
actor: "orchestrator",
kind: action.kind,
ref: orchestrationActionRef(action.target),
summary: capUnits(action.summary, 500),
});
}
const tierChanges: NewStreamEvent[] = [];
if (lastOrchEventRunId !== null) {
const allIds = new Set([...lastOrchTierById.keys(), ...tierById.keys()]);
for (const id of allIds) {
const prev = lastOrchTierById.get(id);
const tier = tierById.get(id);
if (prev !== tier) {
opts.onEvent({
tierChanges.push({
actor: "orchestrator",
kind: "tier-change",
ref: id,
Expand All @@ -2079,10 +2078,49 @@ export function liveSource(
}
}
}
if (!alreadyRecorded) {
for (const event of siblings) opts.onEvent(event);
for (const event of tierChanges) opts.onEvent(event);
opts.onEvent({
actor: "orchestrator",
kind: "orchestration-published",
ref: publishRef,
summary:
`orchestration run ${section.runId} published: ` +
`${section.tiers.needsDrew.length} needs-user, ${section.tiers.needsOrchestrator.length} needs-orchestrator`,
});
} else {
// #17453 reconciliation: a durable marker alone no longer
// proves the run was fully emitted — a partially emitted
// publish (marker persisted, siblings lost to a crash or a
// validation drop) used to suppress the run forever and
// silently advance the tier baseline past the gap. Emit
// every sibling whose durable identity is absent, exactly
// once. Tier-change events share their (kind, ref)
// identity across runs, so they cannot be checked
// individually: when any action proves missing the publish
// was partial, and the tier diff is re-emitted with it —
// a visible duplicate over a silent, permanent loss (the
// same fail-open direction as the hasEvent catch below).
let publishWasPartial = false;
for (const event of siblings) {
const present = opts.hasEvent
? await opts.hasEvent(event.kind, event.ref).catch(() => false)
: false;
if (!present) {
publishWasPartial = true;
opts.onEvent(event);
}
}
if (publishWasPartial) {
for (const event of tierChanges) opts.onEvent(event);
}
}
lastOrchEventRunId = section.runId;
lastOrchTierById = tierById;
} else if (alreadyRecorded) {
// The durable publication event proves this run was already emitted.
// Fully emitted (or no event sink wired): adopt the run and
// its tier membership as the current baseline.
lastOrchEventRunId = section.runId;
lastOrchTierById = tierById;
}
Expand Down
110 changes: 109 additions & 1 deletion pm-stack/aggregator/test/orchestration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,10 @@ describe("live source orchestration states (#1140)", () => {
const opts = {
onEvent: (event: { kind: string; ref: string }) => {
events.push(event);
if (event.kind === "orchestration-published") persisted.add(`${event.kind}:${event.ref}`);
// A genuinely fully emitted run: every event it produced is
// durable, the marker included (#17453 — the marker alone is a
// partial publish, reconciled by the next test).
persisted.add(`${event.kind}:${event.ref}`);
},
hasEvent: async (kind: string, ref: string) => persisted.has(`${kind}:${ref}`),
};
Expand All @@ -546,6 +549,111 @@ describe("live source orchestration states (#1140)", () => {
expect(events).toHaveLength(firstRunCount);
});

test("a marker-only partial publish is reconciled, not suppressed (#17453)", async () => {
const dir = await mkdtemp(join(tmpdir(), "pm-orch-partial-"));
const feedFile = join(dir, "fleet-feed.json");
const cfg = loadConfig({
PM_AGG_SOURCE: "live",
PM_AGG_FEED_FILE: feedFile,
PM_AGG_REGISTRY_FILE: join(dir, "registry.json"),
PM_PROJECTOR_URL: "off",
});
const writeRun = async (section: unknown) =>
writeFile(
feedFile,
JSON.stringify({
version: 1,
generatedAt: new Date().toISOString(),
quota: { pools: [], burn: [], choices: { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] } },
attention: [],
orchestration: section,
}),
);
const persisted = new Set<string>();
const events: Array<{ kind: string; ref: string; summary: string }> = [];
const opts = {
onEvent: (event: { kind: string; ref: string; summary: string }) => {
events.push(event);
persisted.add(`${event.kind}:${event.ref}`);
},
hasEvent: async (kind: string, ref: string) => persisted.has(`${kind}:${ref}`),
};
const src = liveSource(cfg, opts);
// Run 1 publishes fully: baseline tier membership is now durable
// in memory (code#812 needs-user) and every event is in the store.
await writeRun(LIVE_SHAPE);
await src.orchestration();
const run1Count = events.length;
expect(run1Count).toBeGreaterThan(0);
// Run 2's publish partially emitted before a crash: ONLY its
// orchestration-published marker reached the store; its action and
// the tier-change for the departed code#812 never landed.
const run2 = {
...LIVE_SHAPE,
runId: "orch-run2",
provenance: { ...LIVE_SHAPE.provenance, runId: "orch-run2" },
tiers: { needsDrew: [], needsOrchestrator: [] },
actions: [
{
at: "2026-10-27T04:14:56Z",
kind: "merge",
target: "code #999",
summary: "Merged the run-2 pull request.",
},
],
};
persisted.add(`orchestration-published:orchestration:${run2.runId}`);
await writeRun(run2);
await src.orchestration();
const reconciled = events.slice(run1Count);
// The missing action is emitted exactly once, the marker is NOT
// re-emitted (it is already durable), and the tier-change for the
// departed item rides along instead of being silently skipped by
// the baseline advancing past the partial publish.
expect(reconciled.filter((event) => event.kind === "merge")).toHaveLength(1);
expect(reconciled.filter((event) => event.kind === "orchestration-published")).toHaveLength(0);
expect(
reconciled.find((event) => event.kind === "tier-change" && event.ref === "developing-today/code#812")?.summary,
).toContain("needs-user → —");
// A later read of the same run emits nothing further.
const afterReconcile = events.length;
await src.orchestration();
expect(events).toHaveLength(afterReconcile);
});

test("a failing hasEvent stays fail-open: the run still emits (#17453)", async () => {
const dir = await mkdtemp(join(tmpdir(), "pm-orch-hasfail-"));
const feedFile = join(dir, "fleet-feed.json");
const cfg = loadConfig({
PM_AGG_SOURCE: "live",
PM_AGG_FEED_FILE: feedFile,
PM_AGG_REGISTRY_FILE: join(dir, "registry.json"),
PM_PROJECTOR_URL: "off",
});
await writeFile(
feedFile,
JSON.stringify({
version: 1,
generatedAt: new Date().toISOString(),
quota: { pools: [], burn: [], choices: { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] } },
attention: [],
orchestration: LIVE_SHAPE,
}),
);
const events: Array<{ kind: string; ref: string }> = [];
const src = liveSource(cfg, {
onEvent: (event: { kind: string; ref: string }) => events.push(event),
hasEvent: async () => {
throw new Error("store unreadable");
},
});
await src.orchestration();
// Duplicates over silent loss: with the durable check failing, the
// run emits its full set, marker included.
expect(events.some((event) => event.kind === "orchestration-published")).toBe(true);
expect(events.some((event) => event.kind === "verify")).toBe(true);
});

test("registry merge emits events for every newly merged session", async () => {
const dir = await mkdtemp(join(tmpdir(), "pm-registry-events-"));
const feedFile = join(dir, "fleet-feed.json");
Expand Down