fix(process): coalesce delayed capture progress
Retain one pending intermediate observation and drop stale live status before terminal cleanup. Preserve monotonic counts and final delivery while an observer is slow or rejects an update. Verify the send rate at its controlled-clock owner and lifecycle notification density at the CLI/MCP boundary, where receive timestamps include buffering and scheduling.
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
createProcessCaptureProgressTracker,
|
||||
type ProcessCaptureProgress,
|
||||
} from "./ProcessCaptureProgress.js";
|
||||
|
||||
const blockedProgress = () => {
|
||||
let release: () => void = () => undefined;
|
||||
const blocked = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const delivered: Parameters<ProcessCaptureProgress["report"]>[0][] = [];
|
||||
const progress: ProcessCaptureProgress = {
|
||||
async report(update) {
|
||||
delivered.push(update);
|
||||
if (delivered.length === 1) await blocked;
|
||||
},
|
||||
};
|
||||
return { progress, delivered, release: () => release() };
|
||||
};
|
||||
|
||||
it("coalesces intermediate observations while the progress receiver is blocked", async () => {
|
||||
const receiver = blockedProgress();
|
||||
const tracker = createProcessCaptureProgressTracker(receiver.progress);
|
||||
void tracker.report({
|
||||
phase: "running",
|
||||
counts: { frames: 1, samples: 0, interactions: 0 },
|
||||
});
|
||||
await Promise.resolve();
|
||||
let latest: Promise<void> | undefined;
|
||||
for (let frames = 2; frames <= 1_000; frames += 1) {
|
||||
latest = tracker.report({
|
||||
phase: "running",
|
||||
counts: { frames, samples: 0, interactions: 0 },
|
||||
});
|
||||
}
|
||||
expect(receiver.delivered).toHaveLength(1);
|
||||
receiver.release();
|
||||
await latest;
|
||||
expect(receiver.delivered.map((update) => update.completed)).toEqual([
|
||||
1, 1_000,
|
||||
]);
|
||||
expect(receiver.delivered[1]?.message).toContain("frames=1000");
|
||||
});
|
||||
|
||||
it("discards stale live observations before delivering final cleanup", async () => {
|
||||
const receiver = blockedProgress();
|
||||
const tracker = createProcessCaptureProgressTracker(receiver.progress);
|
||||
void tracker.report({
|
||||
phase: "running",
|
||||
counts: { frames: 1, samples: 0, interactions: 0 },
|
||||
});
|
||||
await Promise.resolve();
|
||||
void tracker.report({
|
||||
phase: "running",
|
||||
counts: { frames: 2, samples: 0, interactions: 0 },
|
||||
});
|
||||
void tracker.report({
|
||||
phase: "settling",
|
||||
counts: { frames: 2, samples: 1, interactions: 0 },
|
||||
});
|
||||
tracker.closeLive();
|
||||
await tracker.report({
|
||||
phase: "running",
|
||||
counts: { frames: 10, samples: 10, interactions: 10 },
|
||||
});
|
||||
const terminal = tracker.report({
|
||||
phase: "cleanup",
|
||||
counts: { frames: 2, samples: 2, interactions: 0 },
|
||||
disposition: "exited",
|
||||
terminal: true,
|
||||
});
|
||||
receiver.release();
|
||||
await terminal;
|
||||
expect(receiver.delivered.map((update) => update.phase)).toEqual([
|
||||
"running",
|
||||
"cleanup",
|
||||
]);
|
||||
expect(receiver.delivered.at(-1)).toMatchObject({
|
||||
completed: 4,
|
||||
total: null,
|
||||
terminal: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("still delivers terminal status after an observer rejects an update", async () => {
|
||||
const delivered: Parameters<ProcessCaptureProgress["report"]>[0][] = [];
|
||||
const tracker = createProcessCaptureProgressTracker({
|
||||
async report(update) {
|
||||
delivered.push(update);
|
||||
if (delivered.length === 1) throw new Error("Observer disconnected");
|
||||
},
|
||||
});
|
||||
await tracker.report({
|
||||
phase: "prepare",
|
||||
counts: { frames: 0, samples: 0, interactions: 0 },
|
||||
});
|
||||
tracker.closeLive();
|
||||
await expect(
|
||||
tracker.report({
|
||||
phase: "cleanup",
|
||||
counts: { frames: 1, samples: 1, interactions: 0 },
|
||||
disposition: "failed",
|
||||
terminal: true,
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
expect(delivered.at(-1)).toMatchObject({
|
||||
phase: "cleanup",
|
||||
completed: 2,
|
||||
terminal: true,
|
||||
});
|
||||
});
|
||||
@@ -66,10 +66,28 @@ export const createProcessCaptureProgressTracker = (
|
||||
const started = now();
|
||||
let completed = 0;
|
||||
let live = true;
|
||||
let pending = Promise.resolve();
|
||||
let next: Parameters<ProcessCaptureProgress["report"]>[0] | undefined;
|
||||
let pending: Promise<void> | undefined;
|
||||
const flush = async (): Promise<void> => {
|
||||
try {
|
||||
while (next !== undefined) {
|
||||
const update = next;
|
||||
next = undefined;
|
||||
try {
|
||||
await progress?.report(update);
|
||||
} catch {
|
||||
// Progress delivery is observational; capture and cleanup still finish.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pending = undefined;
|
||||
}
|
||||
};
|
||||
return {
|
||||
closeLive(): void {
|
||||
live = false;
|
||||
if (next?.terminal !== true) next = undefined;
|
||||
},
|
||||
report(update: ProcessCaptureProgressReport): Promise<void> {
|
||||
if (progress === undefined) return Promise.resolve();
|
||||
@@ -79,7 +97,8 @@ export const createProcessCaptureProgressTracker = (
|
||||
update.counts.samples +
|
||||
update.counts.interactions;
|
||||
if (units > completed) completed = units;
|
||||
const payload = {
|
||||
// A slow observer needs the latest observation, not a queue of old states.
|
||||
next = {
|
||||
phase: update.phase,
|
||||
completed,
|
||||
total: null as null,
|
||||
@@ -91,16 +110,8 @@ export const createProcessCaptureProgressTracker = (
|
||||
),
|
||||
...(update.terminal === true ? { terminal: true as const } : {}),
|
||||
};
|
||||
const send = () => progress.report(payload);
|
||||
const scheduled = pending.then(send, send);
|
||||
pending = scheduled.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
return scheduled.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
pending ??= Promise.resolve().then(flush);
|
||||
return pending;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user