Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
05b1bed
feat(scheduled-tasks): run each task in its own dedicated, named session
wenshao Jul 6, 2026
1cd4395
fix(scheduled-tasks): address code review on per-session task feature
wenshao Jul 6, 2026
cd6b6b1
fix(scheduled-tasks): isolate task sessions, fix catch-up/jitter/revive
wenshao Jul 6, 2026
84509a3
fix(scheduled-tasks): sync bound session name on task rename
wenshao Jul 6, 2026
f187a9f
Merge remote-tracking branch 'origin/main' into feat/scheduled-task-s…
wenshao Jul 6, 2026
a33ac48
fix(scheduled-tasks): mirror run sessionId on client type; clarify se…
wenshao Jul 6, 2026
9061ba9
fix(scheduled-tasks): couple archive/enable + record manual run only …
wenshao Jul 6, 2026
2a12cba
fix(scheduled-tasks): close dormancy/orphan/overflow gaps from review
wenshao Jul 6, 2026
434b857
fix(scheduled-tasks): guard catch-up double-fire, run-now hang, /run …
wenshao Jul 6, 2026
bb30690
fix(scheduled-tasks): block disabled-task run in UI; record manual ru…
wenshao Jul 6, 2026
f8cd199
fix(scheduled-tasks): guard tick double-fire, cap rehydration, harden…
wenshao Jul 6, 2026
f93a848
fix(scheduled-tasks): one-shot run/edit correctness; tick persist non…
wenshao Jul 6, 2026
a960edb
fix(scheduled-tasks): ref-count fire guard, real rehydration cap, aut…
wenshao Jul 6, 2026
a192d88
fix(scheduled-tasks): catch-up non-regression, disabled-edit re-seat,…
wenshao Jul 6, 2026
e1b2976
fix(scheduled-tasks): one-shot anchor on unarchive, memoize next-fire…
wenshao Jul 6, 2026
36de896
fix(scheduled-tasks): re-seat one-shot anchor on re-enable; guard dup…
wenshao Jul 6, 2026
b244941
fix(scheduled-tasks): strip bidi override/isolate chars from session …
wenshao Jul 7, 2026
8cbe20a
fix(scheduled-tasks): close review findings — rehydrate deadlock, man…
wenshao Jul 7, 2026
e0dcd35
chore: merge origin/main into feat/scheduled-task-sessions
qwen-code-dev-bot Jul 7, 2026
5374dfb
fix(web-shell): "create via chat" opens a fresh session in scheduled …
wenshao Jul 7, 2026
50efce4
fix(scheduled-tasks): address follow-up review findings
wenshao Jul 7, 2026
04ebdc2
fix(web-shell): don't prime the composer when "create via chat" can't…
wenshao Jul 7, 2026
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
179 changes: 178 additions & 1 deletion packages/cli/src/serve/routes/scheduled-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,54 @@ function safeBody(req: Request): Record<string, unknown> {
: {};
}

/** Stub session bridge: mints sequential fake session ids and records spawns /
* closes so tests can assert binding and rollback without a real child. */
interface StubBridge {
spawnOrAttach(req: { workspaceCwd: string }): Promise<{ sessionId: string }>;
closeSession(sessionId: string): Promise<unknown>;
updateSessionMetadata(
sessionId: string,
metadata: { displayName?: string },
): unknown;
spawned: string[];
closed: string[];
named: Array<{ sessionId: string; displayName?: string }>;
failNext: boolean;
}

function makeStubBridge(): StubBridge {
let seq = 0;
const bridge: StubBridge = {
spawned: [],
closed: [],
named: [],
failNext: false,
async spawnOrAttach() {
if (bridge.failNext) {
bridge.failNext = false;
throw new Error('spawn failed');
}
const sessionId = `sess-${++seq}`;
bridge.spawned.push(sessionId);
return { sessionId };
},
async closeSession(sessionId: string) {
bridge.closed.push(sessionId);
return undefined;
},
updateSessionMetadata(sessionId, metadata) {
bridge.named.push({ sessionId, ...metadata });
return metadata;
},
};
return bridge;
}

interface Harness {
app: express.Application;
scratch: string;
workspace: string;
bridge: StubBridge;
}

async function makeHarness(): Promise<Harness> {
Expand All @@ -34,15 +78,17 @@ async function makeHarness(): Promise<Harness> {
// ~/.qwen — redirect it into the scratch dir for the duration of the test.
Storage.setRuntimeBaseDir(scratch);

const bridge = makeStubBridge();
const app = express();
app.use(express.json());
registerScheduledTasksRoutes(app, {
boundWorkspace: workspace,
// Non-strict mutate is a passthrough (matches the loopback web-shell).
mutate: () => (_req, _res, next) => next(),
safeBody,
bridge,
});
return { app, scratch, workspace };
return { app, scratch, workspace, bridge };
}

async function teardown(h: Harness): Promise<void> {
Expand Down Expand Up @@ -91,6 +137,46 @@ describe('scheduled-tasks routes', () => {
expect(list.body.tasks[0].id).toBe(res.body.id);
});

it('binds a created task to a freshly minted session', async () => {
const res = await create({ cron: '0 9 * * *', prompt: 'p' });
expect(res.status).toBe(201);
// The task carries the id of the session the bridge minted for it.
expect(h.bridge.spawned).toHaveLength(1);
expect(res.body.sessionId).toBe(h.bridge.spawned[0]);
// And it's persisted on disk, not just in the response.
const list = await request(h.app).get('/scheduled-tasks');
expect(list.body.tasks[0].sessionId).toBe(h.bridge.spawned[0]);
// No teardown on the happy path.
expect(h.bridge.closed).toEqual([]);
});

it('names the bound session after the task (name preferred over prompt)', async () => {
const named = await create({
name: 'Digest',
cron: '0 9 * * *',
prompt: 'summarize the day',
});
expect(h.bridge.named).toEqual([
{ sessionId: named.body.sessionId, displayName: '⏰ Digest' },
]);

const unnamed = await create({ cron: '0 9 * * *', prompt: 'do the thing' });
expect(h.bridge.named[1]).toEqual({
sessionId: unnamed.body.sessionId,
displayName: '⏰ do the thing',
});
});

it('returns 500 and writes nothing when the session cannot be minted', async () => {
h.bridge.failNext = true;
const res = await create({ cron: '0 9 * * *', prompt: 'p' });
expect(res.status).toBe(500);
expect(res.body.code).toBe('scheduled_tasks_session_failed');
// The task must not land on disk without its session.
const list = await request(h.app).get('/scheduled-tasks');
expect(list.body.tasks).toEqual([]);
});

it('rejects an unparseable cron', async () => {
const res = await create({ cron: 'not a cron', prompt: 'x' });
expect(res.status).toBe(400);
Expand Down Expand Up @@ -170,9 +256,35 @@ describe('scheduled-tasks routes', () => {
const del = await request(h.app).delete(`/scheduled-tasks/${id}`);
expect(del.status).toBe(200);
expect(del.body).toEqual({ deleted: true, id });
// The task's dedicated session is torn down with it (no resident leak).
expect(h.bridge.closed).toEqual([created.body.sessionId]);

const again = await request(h.app).delete(`/scheduled-tasks/${id}`);
expect(again.status).toBe(404);
// A no-op delete (already gone) closes nothing further.
expect(h.bridge.closed).toEqual([created.body.sessionId]);
});

it('records a manual run: advances lastFiredAt and appends a manual run', async () => {
const created = await create({ cron: '0 9 * * *', prompt: 'p' });
const id = created.body.id as string;
const before = created.body.lastFiredAt as number;

const res = await request(h.app).post(`/scheduled-tasks/${id}/run`);
expect(res.status).toBe(200);
expect(res.body.lastFiredAt).toBeGreaterThan(before);
expect(res.body.runs).toHaveLength(1);
expect(res.body.runs[0].kind).toBe('manual');
// The manual run is tagged with the task's bound session.
expect(res.body.runs[0].sessionId).toBe(created.body.sessionId);
});

it('404s a manual run for an unknown task', async () => {
const res = await request(h.app).post(
'/scheduled-tasks/does-not-exist/run',
);
expect(res.status).toBe(404);
expect(res.body.code).toBe('task_not_found');
});

it('rejects a create past the max-tasks cap', async () => {
Expand All @@ -183,6 +295,10 @@ describe('scheduled-tasks routes', () => {
const over = await create({ cron: '0 9 * * *', prompt: 'overflow' });
expect(over.status).toBe(409);
expect(over.body.code).toBe('max_tasks_reached');
// The over-cap create still minted a session before the write was rejected;
// it must be rolled back so a bounced create doesn't leak a resident one.
expect(h.bridge.spawned).toHaveLength(51);
expect(h.bridge.closed).toEqual([h.bridge.spawned[50]]);
});

it('updates cron / prompt / recurring via PATCH', async () => {
Expand Down Expand Up @@ -290,6 +406,67 @@ describe('scheduled-tasks routes', () => {
name: null,
enabled: true,
});
// Absent runs normalizes to an empty array (never undefined on the wire).
expect(res.body.tasks[0].runs).toEqual([]);
});

it('surfaces on-disk run history on GET', async () => {
await seedTask({
id: 'h1',
cron: '0 9 * * *',
prompt: 'p',
recurring: true,
createdAt: 1_700_000_000_000,
lastFiredAt: 1_700_000_540_000,
runs: [
{ at: 1_700_000_480_000, kind: 'scheduled' },
{ at: 1_700_000_540_000, kind: 'catch-up' },
],
});
const res = await request(h.app).get('/scheduled-tasks');
expect(res.status).toBe(200);
expect(res.body.tasks[0].runs).toEqual([
{ at: 1_700_000_480_000, kind: 'scheduled' },
{ at: 1_700_000_540_000, kind: 'catch-up' },
]);
});

it('computes nextRunAt for an enabled task and nulls it when disabled', async () => {
const created = await create({ cron: '0 9 * * *', prompt: 'p' });
expect(created.status).toBe(201);
// Enabled → a concrete future fire time.
expect(typeof created.body.nextRunAt).toBe('number');
expect(created.body.nextRunAt).toBeGreaterThan(Date.now());

// Disabling drops it — a paused task has no next run.
const patched = await request(h.app)
.patch(`/scheduled-tasks/${created.body.id}`)
.send({ enabled: false });
expect(patched.status).toBe(200);
expect(patched.body.nextRunAt).toBeNull();

// Re-enabling brings it back.
const reenabled = await request(h.app)
.patch(`/scheduled-tasks/${created.body.id}`)
.send({ enabled: true });
expect(typeof reenabled.body.nextRunAt).toBe('number');
});

it('rejects a task whose run history is malformed (fix-or-delete)', async () => {
// A present-but-corrupt `runs` routes through the same read failure as any
// other corrupt field rather than being silently dropped.
await seedTask({
id: 'bad1',
cron: '0 9 * * *',
prompt: 'p',
recurring: true,
createdAt: 1_700_000_000_000,
lastFiredAt: null,
runs: [{ at: 'not-a-number' }],
});
const res = await request(h.app).get('/scheduled-tasks');
expect(res.status).toBe(500);
expect(res.body.code).toBe('scheduled_tasks_read_failed');
});

it('re-enabling a previously-fired task resumes from now (no catch-up)', async () => {
Expand Down
Loading
Loading