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:
N0zoM1z0
2026-10-10 18:54:57 +08:00
committed by Ysopwnable
parent 706bba6e13
commit 6cd5ea0fb2
5 changed files with 203 additions and 32 deletions
+23 -12
View File
@@ -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;
},
};
};