feat: preserve bounded model requests and complete task history

This commit is contained in:
Agent Workbench Dev
2026-10-29 21:09:50 +08:00
parent 19b738b468
commit 8f32dec6cc
58 changed files with 1269 additions and 108 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ export async function upgradeRunning({ url, pid, currentRoot, newRoot, handlerRo
if (!fs.existsSync(originalScript) || !fs.existsSync(upgradeScript) || !fs.existsSync(path.join(next, 'dist-client', 'index.html'))) throw new Error('Upgrade bundles are incomplete.');
if (!Number.isInteger(pid) || pid < 1 || pid === process.pid) throw new Error('Invalid target process.');
const before = await readUpgradeJson(origin + '/api/bootstrap', 5000, 'Read original Workbench');
if (!before.token || !['0.1.0', '1.0.0', '1.1.0', '1.2.0', '1.3.0', '1.4.0'].includes(before.version)) throw new Error('This migration accepts a supported existing Workbench.');
if (!before.token || !['0.1.0', '1.0.0', '1.1.0', '1.2.0', '1.3.0', '1.4.0', '1.5.0'].includes(before.version)) throw new Error('This migration accepts a supported existing Workbench.');
if (before.sessions.some(session => ['running', 'awaiting_approval', 'stopping'].includes(session.status))) throw new Error('Wait for existing tasks and approvals before upgrading.');
let protocol, socket, enabledHere = false, paused = false;
const breakpoints = [];
+7 -5
View File
@@ -33,7 +33,9 @@ child.stdout.on('data', value => { output += value; for (const line of output.sp
child.stderr.on('data', value => { errors += value; });
const wait = async predicate => { const deadline = Date.now() + 15000; while (!await predicate()) { if (Date.now() > deadline) throw new Error('UI fixture timed out: ' + errors); await new Promise(resolve => setTimeout(resolve, 40)); } };
await wait(() => url); const bootstrap = await (await fetch(url + '/api/bootstrap')).json();
assert.equal(bootstrap.version, '1.4.0'); assert.equal(bootstrap.config.mode, 'demo'); assert.equal(bootstrap.config.hasKey, false);
assert.ok(['1.4.0', '1.5.0'].includes(bootstrap.version)); assert.equal(bootstrap.config.mode, 'demo'); assert.equal(bootstrap.config.hasKey, false);
const proofPrefix = bootstrap.version === '1.4.0' ? 'm2-batch' : 'context-batch';
const screenshotPrefix = bootstrap.version === '1.4.0' ? '1.4-batch' : 'context-batch';
const api = async (route, input) => { const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' },
...(input === undefined ? {} : { body: JSON.stringify(input) }) }); const value = await response.json(); assert.ok(response.ok, value.error); return value; };
const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
@@ -67,7 +69,7 @@ async function viewport(name) {
});
assert.equal(bounds.scrollY, 0); assert.ok(bounds.scrollWidth <= bounds.width + 1 && bounds.scrollHeight <= bounds.height + 1, JSON.stringify(bounds));
if (bounds.modal) assert.ok(bounds.modal.top >= 0 && bounds.modal.bottom <= bounds.height + 1 && bounds.modal.scrollWidth <= bounds.modal.width + 1 && bounds.close.bottom <= bounds.height, JSON.stringify(bounds));
const destination = '.qa/screenshots/1.4-batch-' + name + '.png'; await page.screenshot({ path: destination, scale: 'css' }); screenshots.push(destination);
const destination = '.qa/screenshots/' + screenshotPrefix + '-' + name + '.png'; await page.screenshot({ path: destination, scale: 'css' }); screenshots.push(destination);
}
try {
await page.goto(url, { waitUntil: 'networkidle' });
@@ -126,11 +128,11 @@ try {
await expect(page.locator('.toast')).toHaveCount(0); await expect(dialog()).toHaveCount(0); await expect(page.locator('.session-item.selected')).toContainText('批次验收 normal');
passed.push('A failed batch response after its component unmounts cannot display an error in the newly selected project');
assert.equal(pageErrors.length, 0, pageErrors.join('\n')); assert.equal(requestFailures.length, 0, requestFailures.join('\n'));
const result = { success: true, version: '1.4.0', testedAt: new Date().toISOString(), cases: passed, pageErrors, requestFailures, screenshots, projectRoot: root,
const result = { success: true, version: bootstrap.version, testedAt: new Date().toISOString(), cases: passed, pageErrors, requestFailures, screenshots, projectRoot: root,
scope: 'Real independent demo host and durable batch states; UI fixtures include actual owner conflict/rollback results. No real model credentials or user project were used.' };
fs.writeFileSync('docs/m2-batch-ui-validation.json', JSON.stringify(result, null, 2) + '\n'); console.log(JSON.stringify(result, null, 2));
fs.writeFileSync('docs/' + proofPrefix + '-ui-validation.json', JSON.stringify(result, null, 2) + '\n'); console.log(JSON.stringify(result, null, 2));
} catch (error) {
await page.screenshot({ path: '.qa/screenshots/1.4-batch-failure.png', scale: 'css' }); fs.writeFileSync('.qa/batch-ui-failure.json', JSON.stringify({ passed, message: error.message, pageErrors, requestFailures }, null, 2)); throw error;
await page.screenshot({ path: '.qa/screenshots/' + screenshotPrefix + '-failure.png', scale: 'css' }); fs.writeFileSync('.qa/' + proofPrefix + '-ui-failure.json', JSON.stringify({ passed, message: error.message, pageErrors, requestFailures }, null, 2)); throw error;
} finally {
await browser.close(); try { await api('/shutdown', {}); await wait(() => child.exitCode !== null); } catch { if (child.exitCode === null) child.kill(); }
}
+148
View File
@@ -0,0 +1,148 @@
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { spawn } from 'node:child_process';
import { chromium, expect } from 'playwright/test';
import { Store } from '../src/server/store.mjs';
import { RequestPlanner } from '../src/core/context/request-plan.mjs';
import { toolDefinitions } from '../src/server/provider.mjs';
import { call } from '../tests/helpers.mjs';
const root = path.resolve('.qa', 'context-ui-' + randomUUID()), data = path.join(root, 'data');
fs.mkdirSync(root, { recursive: true }); fs.mkdirSync('.qa/screenshots', { recursive: true });
const store = new Store(data), cases = {};
for (const name of ['long', 'other', 'paused']) {
const projectRoot = path.join(root, 'context-' + name); fs.mkdirSync(projectRoot);
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Owned context UI fixture\n');
const project = store.addProject(projectRoot), session = store.createSession(project.id, '上下文验收 ' + name);
const goal = '这是独立界面验收样本。完整任务要求与实际原记录保留,读取工件不得重新执行工具。';
store.message(session, 'user', goal); session.providerMessages.push({ role: 'user', content: goal });
const run = store.beginRun(session, goal, false, store.config), planner = new RequestPlanner(store);
if (name === 'paused') {
session.status = 'paused'; store.message(session, 'system', '完整任务要求超过请求上下文预算,请核对配置后继续。');
store.finishRun(session, run, 'context_limit'); cases[name] = { project, session, projectRoot, runs: [run.id] }; continue;
}
if (name === 'long') for (let index = 0; index < 18; index++) {
const tool = call('ui-original-' + index, 'read_file', { path: 'README.md', fixture: 'Preserve exact historical JSON.' });
const event = { id: randomUUID(), toolCallId: tool.id, tool: 'read_file', kind: 'tool', phase: 'queued', label: '读取文件',
createdAt: new Date().toISOString(), startedAt: new Date().toISOString(), input: { path: 'README.md' }, runId: run.id, taskId: run.taskId };
session.events.push(event); store.registerInvocation(session, tool, run, event);
store.beginInvocation(session, tool, run, event); store.invocationState(session, event, 'running');
const result = { status: 'completed', path: 'README.md', content: ('完整原记录🙂 ' + index + '\n').repeat(1200) };
event.phase = 'completed'; event.output = '已保存完整原记录。'; event.finishedAt = new Date().toISOString();
store.finishInvocation(session, event, result);
session.providerMessages.push({ role: 'assistant', content: 'Saved fixture read.', tool_calls: [tool] },
{ role: 'tool', tool_call_id: tool.id, content: JSON.stringify(result) });
}
const step = store.beginStep(session, run), first = planner.prepare(session, run, store.config, {
system: goal + '\n' + JSON.stringify({ project_id: project.id, session_id: session.id, task_id: run.taskId }), tools: toolDefinitions, stepId: step.id });
const response = { message: { role: 'assistant', content: '独立界面样本已保存;没有请求真实模型。' }, usage: { prompt_tokens: 2400, completion_tokens: 300 } };
planner.finish(session, first.record, { response, attempts: 1 }); session.providerMessages.push(response.message);
store.message(session, 'assistant', response.message.content); store.finishStep(session, step); session.status = 'completed'; store.finishRun(session, run);
const runs = [run.id], plans = [first.record.id];
if (name === 'long') {
const resumed = store.beginRun(session, '', true, store.config), nextStep = store.beginStep(session, resumed);
const next = planner.prepare(session, resumed, store.config, { system: goal, tools: toolDefinitions, stepId: nextStep.id });
planner.finish(session, next.record, { response, attempts: 1 }); store.finishStep(session, nextStep);
session.status = 'completed'; store.finishRun(session, resumed); runs.push(resumed.id); plans.push(next.record.id);
}
cases[name] = { project, session, projectRoot, runs, plans };
}
store.close();
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: process.cwd(), AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '0' };
delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(process.execPath, ['dist-server/server.mjs'], { cwd: process.cwd(), env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let output = '', errors = '', url = '';
child.stdout.on('data', bytes => { output += bytes.toString('utf8'); for (const line of output.split('\n')) { try { const item = JSON.parse(line); if (item.ready) url = item.url; } catch {} } });
child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
const until = async predicate => { const deadline = Date.now() + 20000; while (!await predicate()) { if (Date.now() > deadline) throw new Error('Context UI fixture timeout: ' + errors); await new Promise(resolve => setTimeout(resolve, 40)); } };
await until(() => url || child.exitCode !== null); assert.ok(url, errors);
const bootstrap = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json();
assert.equal(bootstrap.version, '1.5.0'); assert.equal(bootstrap.config.mode, 'demo'); assert.equal(bootstrap.config.hasKey, false);
const api = async route => { const response = await fetch(url + '/api' + route, { headers: { 'X-Workbench-Token': bootstrap.token, Connection: 'close' } }); const value = await response.json(); assert.ok(response.ok, value.error); return value; };
const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
const passed = [], pageErrors = [], requestFailures = [], screenshots = [];
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => { if (request.resourceType() !== 'eventsource') requestFailures.push(request.failure()?.errorText); });
const mark = value => { passed.push(value); console.log('PASS ' + value); };
const panel = () => page.getByLabel('请求上下文', { exact: true }), history = () => page.getByRole('dialog', { name: '完整历史记录', exact: true });
async function screenshot(name) {
const file = '.qa/screenshots/context-' + name + '.png'; await page.screenshot({ path: file, fullPage: true }); screenshots.push(file);
}
async function choose(name) {
const select = page.getByLabel('当前项目', { exact: true });
if (await select.inputValue() !== cases[name].project.id) {
if (!await select.isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
await expect(select).toBeVisible(); await select.selectOption(cases[name].project.id);
}
await expect(page.locator('.session-item.selected')).toContainText('上下文验收 ' + name);
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await expect(page.getByLabel('任务记录', { exact: true })).toBeVisible();
}
async function lateFailure(pattern, trigger, label) {
let release, entered = false, finished = false;
const gate = new Promise(resolve => { release = resolve; });
await page.route(pattern, async route => { entered = true; await gate; await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ error: label }) }); finished = true; });
await trigger(); await until(() => entered);
if (await history().isVisible()) await page.keyboard.press('Escape');
await choose('other'); release(); await until(() => finished);
await page.waitForTimeout(350); await expect(page.locator('.toast')).toHaveCount(0);
await expect(panel()).toContainText('2,400 tokens');
await page.unroute(pattern); mark(label + ' stays in its original scope after switching projects');
}
try {
await page.goto(url); await page.waitForLoadState('networkidle'); await choose('long');
await expect(panel()).toContainText('2,400 tokens'); await expect(panel()).toContainText('16,000 tokens');
await expect(panel().getByRole('button', { name: '检索完整历史', exact: true })).toBeVisible();
await panel().scrollIntoViewIfNeeded(); await screenshot('desktop-plan'); mark('desktop request estimate, actual usage, complete goal and history identity are visible');
await panel().getByRole('button', { name: '检索完整历史', exact: true }).click();
await expect(history().locator('.context-history-group')).toHaveCount(12);
await history().getByRole('button', { name: '下一页', exact: true }).click();
await expect(history().locator('.artifact-pagination')).toContainText('13–18 / 18');
await history().getByRole('button', { name: '上一页', exact: true }).click();
await expect(history().locator('.artifact-pagination')).toContainText('1–12 / 18'); await screenshot('desktop-history');
await history().getByRole('button', { name: '读取工具结果', exact: true }).first().click();
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true });
await expect(artifact.locator('pre')).toContainText('完整原记录');
await page.keyboard.press('Escape'); mark('paged complete history opens actual tool result artifacts without executing tools');
await page.getByLabel('选择运行记录', { exact: true }).selectOption(cases.long.runs[0]);
await expect(panel()).toContainText('2,400 tokens');
const selected = await api('/sessions/' + cases.long.session.id + '/context-plans?run=' + cases.long.runs[0]);
await expect(panel().locator('.context-sha')).toHaveAttribute('title', selected[0].requestSha256);
await page.getByLabel('选择运行记录', { exact: true }).selectOption(cases.long.runs[1]); mark('historical and resumed Runs retain their own request records');
await page.setViewportSize({ width: 390, height: 844 }); await choose('long');
await panel().getByRole('button', { name: '检索完整历史', exact: true }).click();
await expect(history().locator('.context-history-group')).toHaveCount(12); await screenshot('mobile-history');
const bounds = await history().boundingBox(); assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= 390 && bounds.y + bounds.height <= 844);
await history().getByRole('button', { name: '读取原记录', exact: true }).first().click(); await expect(artifact.locator('pre')).toContainText('Preserve exact historical JSON');
await screenshot('mobile-original'); await page.keyboard.press('Escape');
await expect(panel()).toBeVisible();
await panel().getByRole('button', { name: '查看发送内容', exact: true }).click(); await expect(artifact.locator('pre')).toContainText('history_index');
await screenshot('mobile-request-body'); await page.keyboard.press('Escape');
await expect(panel()).toBeVisible();
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth) <= 390); mark('390px history, original JSON and sent-body dialogs fit and retain close controls');
await page.setViewportSize({ width: 1440, height: 1000 }); await choose('long');
const currentPlanId = cases.long.plans[1];
await lateFailure('**/context-plans/' + currentPlanId + '/history?*', () => panel().getByRole('button', { name: '检索完整历史', exact: true }).click(), 'late old history failure');
await choose('long');
await lateFailure('**/context-plans?run=' + cases.long.runs[0], () => page.getByLabel('选择运行记录', { exact: true }).selectOption(cases.long.runs[0]), 'late old Run failure');
await choose('long');
const currentPlan = await api('/sessions/' + cases.long.session.id + '/context-plans/' + currentPlanId);
await lateFailure('**/artifacts/' + currentPlan.requestArtifactId + '?*', () => panel().getByRole('button', { name: '查看发送内容', exact: true }).click(), 'late old request-body failure');
await choose('paused'); await expect(page.locator('.run-stop')).toContainText('完整任务要求超过请求上下文预算');
await expect(page.getByRole('button', { name: '继续任务', exact: true })).toBeVisible(); mark('context-limit pause explains the intact requirement and explicit continuation');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
const result = { success: true, version: bootstrap.version, testedAt: new Date().toISOString(), passed, pageErrors, requestFailures, screenshots, root, data,
mode: 'saved synthetic context evidence; no real model or project commands executed by this UI verifier', browser: browser.version() };
fs.writeFileSync('.qa/context-ui-validation.json', JSON.stringify(result, null, 2) + '\n'); console.log(JSON.stringify(result, null, 2));
} catch (error) {
await screenshot('failure').catch(() => {});
fs.writeFileSync('.qa/context-ui-validation.json', JSON.stringify({ success: false, version: bootstrap.version, passed, pageErrors, requestFailures, error: error.message, screenshots }, null, 2) + '\n');
throw error;
} finally {
await browser.close();
const response = await fetch(url + '/api/shutdown', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' }, body: '{}' }).catch(() => null);
if (response) assert.ok(response.ok);
await until(() => child.exitCode !== null || child.signalCode !== null).catch(() => { if (child.exitCode === null && child.signalCode === null) child.kill(); });
}
+1 -1
View File
@@ -112,7 +112,7 @@ try {
await settings.getByText('上下文与任务预算', { exact: true }).click(); await settings.getByLabel('单次运行token预算').fill('1000'); await settings.getByLabel('运行时间预算').fill('2'); await settings.getByLabel('任务累计token预算').fill('50000');
await settings.getByRole('button', { name: '保存连接' }).click(); mode = 'budget'; await newTask('保留预算暂停的任务目标'); await waitState('paused');
const original = await state(); assert.equal(original.execution.run.stopReason, 'token_limit'); assert.equal(original.approvals.length, 0);
await page.getByRole('button', { name: '模型连接', exact: true }).click(); await settings.getByText('上下文与任务预算', { exact: true }).click(); await settings.getByLabel('单次运行token预算').fill('10000'); await settings.getByRole('button', { name: '保存连接' }).click();
await page.getByRole('button', { name: '模型连接', exact: true }).click(); await settings.getByText('上下文与任务预算', { exact: true }).click(); await settings.getByLabel('单次运行token预算').fill('16000'); await settings.getByRole('button', { name: '保存连接' }).click();
mode = 'answer'; await page.getByRole('button', { name: '继续任务', exact: true }).click(); await waitState('completed');
const resumed = await state(); assert.equal(resumed.execution.task.id, original.execution.task.id); assert.equal(resumed.execution.runs.length, 2);
await page.getByRole('button', { name: '查看任务记录' }).click(); await taskPanel().getByLabel('选择运行记录').selectOption(original.execution.run.id);
+1 -1
View File
@@ -6,7 +6,7 @@ import { createHash } from 'node:crypto';
const url = process.env.AGENT_WORKBENCH_URL || 'http://127.0.0.1:4318';
const sessionId = process.env.AGENT_WORKBENCH_SESSION || '569c9457-d399-46e6-b53e-4b1fdbb02109';
const bootstrap = await (await fetch(url + '/api/bootstrap')).json();
assert.ok(['1.0.0', '1.1.0', '1.2.0', '1.3.0', '1.4.0'].includes(bootstrap.version));
assert.ok(['1.0.0', '1.1.0', '1.2.0', '1.3.0', '1.4.0', '1.5.0'].includes(bootstrap.version));
assert.equal(bootstrap.config.mode, 'compatible');
assert.ok(bootstrap.config.hasKey, 'The existing in-memory connection must be present.');
const api = async (route, input) => {