From f548cfd8438f525862e55776f172d926cd7eac4c Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sat, 17 Oct 2026 02:41:20 +0200 Subject: [PATCH] Time-skip: a step that runs longer than 20 minutes is stopped and left as it is with a plain note; one shared step runner for A and B --- apps/host/server/timeskip-fork.mjs | 61 ++++++++++++--------- apps/host/tests/helpers/scripted-worker.mjs | 1 + apps/host/tests/timeskip-fork.test.mjs | 13 ++++- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/apps/host/server/timeskip-fork.mjs b/apps/host/server/timeskip-fork.mjs index e7e44668..578f65d4 100644 --- a/apps/host/server/timeskip-fork.mjs +++ b/apps/host/server/timeskip-fork.mjs @@ -82,7 +82,7 @@ export function improvementPrompt({ request, steps, lever, doneSummaries }) { const stepObjectiveBriefing = ctx => ctx; -export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.now }) { +export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.now, stepTimeoutMs = 20 * 60_000 }) { mkdirSync(dir, { recursive: true }); const runs = new Map(); // id -> internal run @@ -110,6 +110,34 @@ export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.n return text; } + /** + * One plan step in its own session: tried twice if it ends early; stopped when it takes longer than `stepTimeoutMs` (a small model that keeps + * debugging one step must not hold the whole version for an hour). A step that cannot finish (too long, or a look-in-a-browser step on a machine + * with no screen) is left as it is with a plain note and the next step goes on; any other step that fails ends the version. + * Returns 'done' | 'skipped' | 'failed' | 'stopped'. + */ + async function runOneStep(r, { tag, chat, planId, plan, at, root, choice, thinking, onFrame }) { + let finished = false, timedOut = false; + for (let attempt = 0; attempt < 2 && !finished && !timedOut && !r.stopping; attempt++) { + if (attempt) { agent.reopenPlan(chat); say(r, tag + '-retry', `${tag.toUpperCase()}, trying step ${at + 1} again…`, { step: at + 1 }); } + agent.resetSession(chat); + const turn = agent.planGoal({ chatId: chat, planId, once: true, workspacePath: root, mode: 'bypass', choice, thinking, onFrame }); + let timer; + const raced = await Promise.race([turn.then(() => 'ended'), new Promise(res => { timer = setTimeout(() => res('timeout'), stepTimeoutMs); timer.unref?.(); })]); + clearTimeout(timer); + if (raced === 'timeout') { timedOut = true; try { await agent.stop(chat, { waitMs: 15_000 }); } catch { /* ending anyway */ } await turn.catch(() => {}); } + if (r.stopping) return 'stopped'; + finished = agent.planState(chat)?.steps[at]?.state === 'done'; + } + if (finished) return 'done'; + if (timedOut || isViewStep(plan.steps[at].text)) { + agent.skipStep(chat, at, timedOut ? 'This step took too long, so it was left as it is.' : 'Could not look at the page here; the files were checked instead.'); + say(r, tag + '-skip', timedOut ? `${tag.toUpperCase()}, step ${at + 1} took too long and was left as it is.` : `${tag.toUpperCase()}, step ${at + 1} (viewing the page) was skipped on this machine.`, { step: at + 1 }); + return 'skipped'; + } + return 'failed'; + } + async function runFlow(r, { choice, thinking, slots }) { const aChat = `ts:${r.id}:A`, bChat = `ts:${r.id}:B`, leverChat = `ts:${r.id}:lever`; r.chats = [aChat, bChat, leverChat]; @@ -152,21 +180,14 @@ export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.n r.b.plan = bp.steps.map(s => ({ id: s.id, text: s.text })); r.b.state = 'running'; r.b.total = bp.steps.length; announce(r); for (let at = 0; at < bp.steps.length && !stopped(); at++) { - let finished = false; - for (let attempt = 0; attempt < 2 && !finished && !stopped(); attempt++) { - if (attempt) { agent.reopenPlan(bChat); say(r, 'b-retry', `B, trying step ${at + 1} again…`, { step: at + 1 }); } - agent.resetSession(bChat); - await agent.planGoal({ chatId: bChat, planId: bp.id, once: true, workspacePath: r.b.root, mode: 'bypass', choice, thinking, + const res = await runOneStep(r, { tag: 'b', chat: bChat, planId: bp.id, plan: bp, at, root: r.b.root, choice, thinking, onFrame: f => { if (f.type === 'error') r.b.note = String(f.message ?? '').slice(0, 240); if (f.type === 'goal' && f.goal?.blocked) r.b.note = String(f.goal.blocked).slice(0, 240); if (f.type === 'plan-step') { r.b.step = f.index; r.b.total = f.total; r.b.text = f.text; r.b.line = f.line; say(r, 'b-step', `B, ${f.line}`, { step: f.index, steps: f.total }); } } }); - if (stopped()) return; - finished = agent.planState(bChat)?.steps[at]?.state === 'done'; - } - if (agent.planState(bChat)?.steps[at]?.state !== 'done' && isViewStep(bp.steps[at].text)) { agent.skipStep(bChat, at, 'Could not look at the page here; the files were checked instead.'); say(r, 'b-skip', `B, step ${at + 1} (viewing the page) was skipped on this machine.`, { step: at + 1 }); } - if (!['done', 'skipped'].includes(agent.planState(bChat)?.steps[at]?.state)) break; + if (res === 'stopped') return; + if (res === 'failed') break; } const end = agent.planState(bChat); r.b.state = end?.steps.every(x => ['done', 'skipped'].includes(x.state)) ? 'done' : 'failed'; @@ -193,12 +214,7 @@ export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.n // Each step is its own turn in its own agent session (the folder carries the work from step to step): a small model window never fills up // across a long plan. The fork happens between two steps, so B gets exactly the files of the finished steps. for (let at = 0; at < plan.steps.length && !stopped(); at++) { - let finished = false; - // A step that does not finish (the model stopped, a program never returned) is tried once more in a fresh session before the run gives up. - for (let attempt = 0; attempt < 2 && !finished && !stopped(); attempt++) { - if (attempt) { agent.reopenPlan(aChat); say(r, 'a-retry', `A, trying step ${at + 1} again…`, { step: at + 1 }); } - agent.resetSession(aChat); - await agent.planGoal({ chatId: aChat, planId: plan.id, once: true, workspacePath: r.a.root, mode: 'bypass', choice, thinking, + const res = await runOneStep(r, { tag: 'a', chat: aChat, planId: plan.id, plan, at, root: r.a.root, choice, thinking, onFrame: f => { if (f.type === 'error') r.a.note = String(f.message ?? '').slice(0, 240); if (f.type === 'goal' && f.goal) r.a.goal = { phase: f.goal.phase, blocked: f.goal.blocked ?? null, round: f.goal.round }; @@ -208,15 +224,8 @@ export function createForkTimeskip({ agent, dir, notify = () => {}, now = Date.n say(r, 'a-step', `A, ${f.line}`, { step: f.index, steps: f.total }); } } }); - if (stopped()) return; - finished = agent.planState(aChat)?.steps[at]?.state === 'done'; - } - if (agent.planState(aChat)?.steps[at]?.state !== 'done' && isViewStep(plan.steps[at].text)) { - // a look-in-a-browser step must not fail the whole version - agent.skipStep(aChat, at, 'Could not look at the page here; the files were checked instead.'); say(r, 'a-skip', `A, step ${at + 1} (viewing the page) was skipped on this machine.`, { step: at + 1 }); - } - const now = agent.planState(aChat); - if (!now || !['done', 'skipped'].includes(now.steps[at]?.state)) break; + if (res === 'stopped') return; + if (res === 'failed') break; if (r.lever && at + 1 === r.lever.step - 1) { try { forkNow(); } catch (e) { r.b = { ...r.b, state: 'failed', note: 'B could not be prepared, so only A is shown.' }; } } } if (stopped()) return; diff --git a/apps/host/tests/helpers/scripted-worker.mjs b/apps/host/tests/helpers/scripted-worker.mjs index 17437752..73c7c5de 100644 --- a/apps/host/tests/helpers/scripted-worker.mjs +++ b/apps/host/tests/helpers/scripted-worker.mjs @@ -124,6 +124,7 @@ export async function startScriptedWorker({ port = 0, chunkDelayMs = 40, model = } return say(['Step finished. The result is checked.']); } + if (directive?.[1] === 'planslow') return say(['## Slow plan\n\n', '1. First thing SCRIPT:goalstep\n', '2. A step that never finishes SCRIPT:slow 4000\n', '3. Third thing SCRIPT:goalstep\n']); if (directive?.[1] === 'planreply') return say(['I looked around the folder first.\n\n## The pagoda plan\n\n', '1. First thing SCRIPT:goalstep\n', '2. Second thing SCRIPT:goalstep\n', '3. Third thing SCRIPT:goalstep\n']); if (last.role === 'tool') { const result = text(last).replace(/\s+/g, ' ').slice(0, 120); diff --git a/apps/host/tests/timeskip-fork.test.mjs b/apps/host/tests/timeskip-fork.test.mjs index 59c7ab96..ea091fad 100644 --- a/apps/host/tests/timeskip-fork.test.mjs +++ b/apps/host/tests/timeskip-fork.test.mjs @@ -38,13 +38,13 @@ test('copyProject leaves out dependency and VCS folders', () => { } finally { rmSync(dir, { recursive: true, force: true }); } }); -async function rig(t, slots) { +async function rig(t, slots, opts = {}) { const worker = await startScriptedWorker({}); const dir = mkdtempSync(join(tmpdir(), 'strata-tsf-')); const project = join(dir, 'project'); mkdirSync(project); writeFileSync(join(project, 'keep.txt'), 'original'); const bridge = createAgentBridge({ dataDir: join(dir, 'data'), localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots }], connections: () => [], env: process.env, startTimeoutMs: 90_000 }); const events = []; - const ts = createForkTimeskip({ agent: bridge, dir: join(dir, 'ts'), notify: (type, data) => events.push(data) }); + const ts = createForkTimeskip({ agent: bridge, dir: join(dir, 'ts'), notify: (type, data) => events.push(data), ...opts }); t.after(async () => { await bridge.close(); await worker.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } }); return { ts, events, project, dir, worker }; } @@ -110,3 +110,12 @@ test('a step about looking at the page in a browser is recognised, carries the n assert.match(stepObjective(plan, 1), /Never wait for a program to close/); assert.match(stepObjective(plan, 1), /Start-Process/); assert.ok(!/Start-Process/.test(stepObjective(plan, 0))); assert.match(UNATTENDED, /Never open a visible window/); }); + +test('a step that never finishes is stopped after the time limit and left as it is with a plain note; the version still completes', { skip }, async (t) => { + const r = await rig(t, 2, { stepTimeoutMs: 4000 }); + const run = r.ts.start({ prompt: 'SCRIPT:planslow', workspace: r.project, choice: { instanceId: 'i' }, slots: 2 }); + const done = await until(() => { const s = r.ts.status(run.id); return ['completed', 'failed'].includes(s.phase) ? s : null; }, 150_000); + assert.equal(done.phase, 'completed', done.error); + assert.ok(r.events.some(e => /step 2 took too long and was left as it is/.test(e.activity?.text ?? '')), 'the person was told which step was left'); + assert.equal(done.a.state, 'done'); +});