// Real scheduler -> runAgentTurn -> workflow-runner -> Write tool, no model/network. import { cp, readFile } from 'node:fs/promises'; import { join } from 'node:path'; import { createTask, upsertTask, recentRuns, patchTask } from '../src/store.js'; import { Scheduler } from '../src/scheduler.js'; import { runAgentTurn } from '../../../src/session/runAgentTurn.js'; import { _resetPluginLoader } from '../../../src/plugins/pluginLoader.js'; import { formatWorkflowCommand } from '../../workflow-runner/src/commandArgs.js'; const root = process.env.MINIMAL_AGENT_HOME!; await cp( new URL('../../../workflows/e2e-smoke', import.meta.url), join(root, 'workflows', 'e2e-smoke'), { recursive: true }, ); const provider = { name: 'test', baseURL: 'https://example.invalid', apiKey: 'test', model: 'test', contextWindow: 128000, }; const task = await upsertTask( createTask({ name: 'Scheduled workflow E2E', at: new Date(Date.now() + 500).toISOString(), timezone: 'UTC', cwd: root, sessionMode: 'fresh', catchUp: 'latest', action: { type: 'workflow', command: formatWorkflowCommand('e2e-smoke', { name: '计划任务链路', times: '2', }), }, }), ); const scheduler = new Scheduler({ provider, runAgentTurn }); scheduler.start(); try { const deadline = Date.now() + 15000; let firstRun: { id: string; path: string; sessionId: string } | undefined; while (true) { const run = (await recentRuns(task.id))[0]; if ( run && run.id !== firstRun?.id && !['running', 'queued'].includes(run.status) ) { if (run.status !== 'success') throw new Error(JSON.stringify(run)); if (!run.result?.includes('冒烟通过')) throw new Error(`No workflow result: ${run.result}`); // Result contains the registered relative artifact path. const path = run.result.match( /out\/e2e-smoke\/[^\s]+\/greeting\.md/, )?.[0]; if ( !path || !(await readFile(join(root, path), 'utf8')).includes( '你好,计划任务链路', ) ) throw new Error('Missing artifact'); const content = await readFile(join(root, path), 'utf8'); if (!content.includes('2. 你好') || content.includes('3. 你好')) throw new Error('Input parameter lost'); if (!firstRun) { firstRun = { id: run.id, path, sessionId: run.sessionId }; // Same workflow, inputs and Session: a new trigger still performs new work. await patchTask({ id: task.id, runtime: { sessionMode: 'persistent', sessionId: run.sessionId }, }); await scheduler.runTaskNow(task.id); continue; } if (run.sessionId !== firstRun.sessionId || path === firstRun.path) throw new Error( 'A new trigger reused old work instead of preserving only the Session', ); if (!(await readFile(join(root, firstRun.path), 'utf8')).includes('你好')) throw new Error('Previous scheduled artifact was lost'); console.log( JSON.stringify({ passed: true, sessionId: run.sessionId, result: run.result, path, }), ); break; } if (Date.now() > deadline) throw new Error('Scheduled workflow timeout'); await Bun.sleep(50); } } finally { await scheduler.stop(); _resetPluginLoader(); }