fix(pi): keep active runs alive during background sleep

This commit is contained in:
2026-08-25 20:56:17 +08:00
parent 92f4c91088
commit 621ebb1781
22 changed files with 1061 additions and 68 deletions

View File

@@ -50,6 +50,36 @@ describe('BackgroundLifecycleController', () => {
controller.dispose();
});
it('does not stop the runtime when a lease is acquired while sleep preparation is in flight', async () => {
vi.useFakeTimers();
let finishSleepPreparation!: () => void;
const sleepPreparation = new Promise<void>((resolve) => {
finishSleepPreparation = resolve;
});
const onSleep = vi.fn(() => sleepPreparation);
const onStopRuntime = vi.fn().mockResolvedValue(undefined);
const controller = new BackgroundLifecycleController({
idleStopMs: 60_000,
onSleep,
onStopRuntime,
});
controller.setActivity({ visible: false, module: 'learning' });
await vi.advanceTimersByTimeAsync(60_000);
expect(onSleep).toHaveBeenCalledTimes(1);
const release = controller.acquireLease({ id: 'coding-run:1', kind: 'coding-run' });
finishSleepPreparation();
await vi.runAllTicks();
expect(onStopRuntime).not.toHaveBeenCalled();
release();
await vi.advanceTimersByTimeAsync(60_000);
expect(onStopRuntime).toHaveBeenCalledTimes(1);
controller.dispose();
});
it('cancels the idle stop when programming becomes visible again', async () => {
vi.useFakeTimers();
const onSleep = vi.fn();