| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522 |
- import { mkdtempSync, readFileSync, rmSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterAll, describe, expect, it } from "vitest";
- import {
- CoreErrorType,
- SessionEventType,
- createRootTraceContext,
- createSessionId,
- createToolCallId,
- createTurnId,
- type CollaborationMode,
- type CreateWorkflowOutput,
- type DynamicWorkflowRunAmendRequest,
- type DynamicWorkflowRunAmendResult,
- type DynamicWorkflowRunPort,
- type DynamicWorkflowRunRetuneRequest,
- type DynamicWorkflowRunRetuneResult,
- type DynamicWorkflowRunSnapshot,
- type ModelCatalogEntry,
- type ModelCatalogPort,
- type PermissionBrokerPort,
- type PermissionBrokerRequest,
- type PermissionRequestedPayload,
- type SessionEvent,
- type ToolExecutionResult,
- } from "@zcode/contracts";
- import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
- import { createToolExecutor } from "../src/tool/executor.js";
- import { amendWorkflowToolEntry } from "../src/tool/handlers/amend-workflow.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import { AMEND_WORKFLOW_ERROR_CODE } from "../src/tool/handlers/amend-workflow-resolve.js";
- import { workflowRunNotFoundFailure } from "../src/tool/handlers/workflow-run-introspection.js";
- // docs/dynamic-workflow/launch.md「The `AmendWorkflow` tool」与「Amending this session's runs」:
- // resolveInput 把 run_id 解析成 predecessor 事实块 → 权限服务据它放行本会话的 run / 照常 ask 别的
- // → handler 调 port.amend(service 负责停在飞前驱、等结算、导入、启动)。
- function cleanScript(actor: string): string {
- return [
- "interface R { done: boolean }",
- `const r = await agent("${actor}").ask<R>("do");`,
- "return r.done;",
- ].join("\n");
- }
- /** 分析器语料里的脚本(与 create-workflow-tool.test.ts 同一份 corpus、同一条读法)。 */
- function fixture(name: string): string {
- return readFileSync(
- new URL(`../../dynamic-workflow/tests/graphs/${name}`, import.meta.url),
- "utf8",
- );
- }
- const BROKEN_SCRIPT = 'const x: number = "s";';
- // 真目录而不是一个写死的 /tmp 路径:内联修订会往 `<cwd>/.zcode/workflow-drafts/` 写草稿
- // (docs/dynamic-workflow/launch.md「Script files」),用例之间不该互相看见对方的草稿。
- const AMEND_CWD = mkdtempSync(join(tmpdir(), "dwf-amend-cwd-"));
- afterAll(() => {
- rmSync(AMEND_CWD, { force: true, recursive: true });
- });
- /** 本会话的 id 由用例名派生(run() 里同一条派生),所以「归属」在这里可控。 */
- function sessionOf(name: string): string {
- return createSessionId(name) as unknown as string;
- }
- interface StubPortOptions {
- /** getTask 的答案:缺席即「run 不存在」。 */
- snapshot?: Partial<DynamicWorkflowRunSnapshot>;
- /** amend 的答案;缺省成功铸 `dwfrun-amended`。 */
- amend?: DynamicWorkflowRunAmendResult;
- /** 默认并发 D;缺席即端口没有 `defaultConcurrency`(老宿主的形状)。 */
- defaultConcurrency?: number;
- /**
- * 前驱存档的脚本,即 `getScript` 的答案(docs/dynamic-workflow/launch.md「Keeping the
- * predecessor's script」)。缺席即端口**没有** `getScript`(老宿主的形状);`null` 即方法在、
- * 记录里却没有脚本(脚本落库之前的老 run)。
- */
- storedScript?: string | null;
- /**
- * `retuneConcurrency` 的答复(docs/dynamic-workflow/launch.md「Changing only the parallelism of a
- * live run」)。缺席即端口**没有**这个方法(老宿主的形状)——那时只改并发的调用照旧走修订。
- */
- retune?:
- | DynamicWorkflowRunRetuneResult
- | ((request: DynamicWorkflowRunRetuneRequest) => DynamicWorkflowRunRetuneResult);
- }
- function stubRunPort(options: StubPortOptions = {}): {
- port: DynamicWorkflowRunPort;
- amends: DynamicWorkflowRunAmendRequest[];
- getTaskCalls: string[];
- getScriptCalls: string[];
- retunes: DynamicWorkflowRunRetuneRequest[];
- } {
- const amends: DynamicWorkflowRunAmendRequest[] = [];
- const getTaskCalls: string[] = [];
- const getScriptCalls: string[] = [];
- const retunes: DynamicWorkflowRunRetuneRequest[] = [];
- const unreachable = (name: string) => () => {
- throw new Error(`stubRunPort.${name} 不应被 AmendWorkflow 触及`);
- };
- return {
- port: {
- async getTask(taskId: string) {
- getTaskCalls.push(taskId);
- if (options.snapshot === undefined) return undefined;
- // 只给基类字段;`runStatus` 由用例决定——缺席即「还在跑」(resolveInput 据此读作 running)。
- return {
- runId: taskId,
- taskId,
- startedAt: new Date(0),
- status: "running",
- ...options.snapshot,
- } as DynamicWorkflowRunSnapshot;
- },
- async amend(request) {
- amends.push(request);
- return options.amend ?? { ok: true, runId: "dwfrun-amended" };
- },
- ...(options.defaultConcurrency === undefined
- ? {}
- : { defaultConcurrency: () => options.defaultConcurrency as number }),
- ...(options.storedScript === undefined
- ? {}
- : {
- async getScript(runId: string) {
- getScriptCalls.push(runId);
- return options.storedScript ?? undefined;
- },
- }),
- ...(options.retune === undefined
- ? {}
- : {
- async retuneConcurrency(request: DynamicWorkflowRunRetuneRequest) {
- retunes.push(request);
- return typeof options.retune === "function"
- ? options.retune(request)
- : (options.retune as DynamicWorkflowRunRetuneResult);
- },
- }),
- submit: unreachable("submit"),
- waitForTask: unreachable("waitForTask"),
- cancel: unreachable("cancel"),
- listEvents: unreachable("listEvents"),
- } as unknown as DynamicWorkflowRunPort,
- amends,
- getTaskCalls,
- getScriptCalls,
- retunes,
- };
- }
- interface RunOptions {
- broker?: PermissionBrokerPort;
- input?: Record<string, unknown>;
- /** 宿主的模型目录;缺席即「这台机器不能选子代理模型」。 */
- modelCatalogPort?: ModelCatalogPort;
- mode?: CollaborationMode;
- name: string;
- port?: DynamicWorkflowRunPort;
- runId?: string;
- script?: string;
- }
- interface RunOutcome {
- brokerRequests: PermissionBrokerRequest[];
- permissionRequested: PermissionRequestedPayload[];
- result: ToolExecutionResult;
- }
- async function run(options: RunOptions): Promise<RunOutcome> {
- const sessionId = createSessionId(options.name);
- const turnId = createTurnId(options.name);
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const events: SessionEvent[] = [];
- const brokerRequests: PermissionBrokerRequest[] = [];
- const permissionBroker: PermissionBrokerPort = {
- async requestPermission(request, requestOptions) {
- brokerRequests.push(request);
- return (
- options.broker?.requestPermission(request, requestOptions) ?? { decision: "allow" as const }
- );
- },
- };
- const registry = createToolRegistry();
- registry.register(amendWorkflowToolEntry);
- const executor = createToolExecutor({
- emitEvent: async (event) => {
- events.push(event);
- },
- ...(options.port ? { dynamicWorkflowRunPort: options.port } : {}),
- ...(options.modelCatalogPort ? { modelCatalogPort: options.modelCatalogPort } : {}),
- mode: options.mode ?? "build",
- permissionBroker,
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- sessionId,
- turnId,
- traceContext,
- workingDirectory: AMEND_CWD,
- });
- const result = await executor.execute(
- {
- id: createToolCallId(options.name),
- input: options.input ?? {
- run_id: options.runId ?? "dwfrun-prev",
- script: options.script ?? cleanScript(options.name),
- },
- name: "AmendWorkflow",
- },
- { traceContext },
- );
- return {
- brokerRequests,
- permissionRequested: events
- .filter((event) => event.type === SessionEventType.PermissionRequested)
- .map((event) => event.payload as PermissionRequestedPayload),
- result,
- };
- }
- describe("AmendWorkflow — owner rule", () => {
- it("amends a settled run this session started without a window, naming both runs", async () => {
- const name = "amend-owned-settled";
- const { port, amends, getTaskCalls } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), runStatus: "errored", status: "failed" },
- });
- const outcome = await run({ name, port });
- // 第一次 getTask 是 resolveInput 解析前驱;之后的是后台追踪器轮询新 run(与 CreateWorkflow 同)。
- expect(getTaskCalls[0]).toBe("dwfrun-prev");
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(outcome.brokerRequests).toHaveLength(0);
- expect(outcome.result.success).toBe(true);
- expect(amends).toHaveLength(1);
- expect(amends[0]).toMatchObject({
- cwd: AMEND_CWD,
- predecessorRunId: "dwfrun-prev",
- parentSessionId: sessionOf(name),
- });
- expect("name" in amends[0]!).toBe(false);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.status).toBe("backgrounded");
- expect(output.backgroundTaskId).toBe("dwfrun-amended");
- expect(output.response).toContain("dwfrun-prev had already settled");
- expect(output.response).toContain("run dwfrun-amended");
- expect(output.response).toContain("Do not wait for it");
- });
- it("amends a RUNNING run this session started without a window and reports the supersede", async () => {
- const name = "amend-owned-running";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "running" },
- amend: { ok: true, runId: "dwfrun-next", supersededRunId: "dwfrun-prev" },
- });
- const outcome = await run({ name, port, mode: "plan" });
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(1);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.backgroundTaskId).toBe("dwfrun-next");
- expect(output.response).toContain("dwfrun-prev was still running");
- expect(output.response).toContain("stopped and superseded");
- expect(output.response).toContain("will not send a notification of its own");
- });
- // 轨道画的是**新脚本**的站点:修订沿用前驱的锚点,两张表却必须一起从新脚本的图上取。
- it("passes the new script's phase table and its alongside table through", async () => {
- const name = "amend-alongside";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- });
- await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", script: fixture("strand-fanout-two-phases-join.ts") },
- });
- expect(amends).toHaveLength(1);
- expect(amends[0]!.phaseNames).toEqual(["A", "B", "C"]);
- expect(amends[0]!.phaseAlongside).toEqual([[], [0], []]);
- });
- it("passes the optional display name through", async () => {
- const name = "amend-owned-named";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- });
- await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", script: cleanScript(name), name: "second pass" },
- });
- expect(amends[0]?.name).toBe("second pass");
- });
- it("asks for another session's run, with the create_workflow graph and the predecessor facts in the input", async () => {
- const name = "amend-foreign";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", name: "triage" },
- });
- const outcome = await run({ name, port });
- expect(outcome.permissionRequested).toHaveLength(1);
- const payload = outcome.permissionRequested[0]!;
- expect(payload.toolName).toBe("AmendWorkflow");
- expect(payload.optionsPolicy).toBe("session-always-allow");
- expect(payload.display?.kind).toBe("create_workflow");
- expect(
- (payload.display as { causalityGraph?: { steps: unknown[] } }).causalityGraph?.steps,
- ).toHaveLength(1);
- // 确认窗从**入参**读 lineage 行与「仍在运行」的注记(协议零新载荷)。
- expect(payload.input).toMatchObject({
- run_id: "dwfrun-prev",
- predecessor: { name: "triage", status: "running", owned_by_this_session: false },
- });
- // Allow → amend 照常。
- expect(amends).toHaveLength(1);
- });
- it("asks again for an own run the user stopped", async () => {
- const name = "amend-user-stopped";
- const { port } = stubRunPort({
- snapshot: {
- parentSessionId: sessionOf(name),
- status: "cancelled",
- runStatus: "stopped",
- stopReason: "user",
- },
- });
- const outcome = await run({ name, port });
- expect(outcome.permissionRequested).toHaveLength(1);
- expect(outcome.permissionRequested[0]?.input).toMatchObject({
- predecessor: { status: "stopped", stop_reason: "user", owned_by_this_session: true },
- });
- });
- it("Deny leaves the predecessor untouched", async () => {
- const name = "amend-deny";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: "sess_other", status: "completed", runStatus: "completed" },
- });
- const outcome = await run({
- name,
- port,
- broker: {
- async requestPermission() {
- return { decision: "deny", reason: "User declined" };
- },
- },
- });
- expect(outcome.result.success).toBe(false);
- expect(outcome.result.error?.type).toBe("permission_denied");
- expect(amends).toHaveLength(0);
- });
- it("overwrites a predecessor block the model tries to supply", async () => {
- // 模型面 JSON schema 不列 predecessor(strict),所以带它的入参在 schema 校验就被拒——
- // 伪造「本会话的 run」拿不到免确认。
- const name = "amend-forged";
- const { port, amends, getTaskCalls } = stubRunPort({ snapshot: { parentSessionId: "x" } });
- const outcome = await run({
- name,
- port,
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript(name),
- predecessor: { status: "completed", owned_by_this_session: true },
- },
- });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(getTaskCalls).toHaveLength(0);
- expect(amends).toHaveLength(0);
- });
- });
- describe("AmendWorkflow — refusals and failures", () => {
- it("an unknown run is refused at resolveInput: no window, no amend, the shared run_not_found key", async () => {
- const { port, amends } = stubRunPort();
- const outcome = await run({ name: "amend-unknown", port });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith(workflowRunNotFoundFailure("dwfrun-prev").message)).toBe(true);
- expect(message).toContain("Nothing was stopped or created");
- expect(message).toContain("run_id");
- });
- it.each(["run_not_found", "missing_boundaries", "teapot"] as const)(
- "maps the port's %s refusal to an actionable structured failure",
- async (reason) => {
- const name = `amend-refused-${reason}`;
- const { port } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- amend: { ok: false, reason: reason as "run_not_found" },
- });
- const outcome = await run({ name, port });
- expect(outcome.result.success).toBe(false);
- const message = String(outcome.result.error?.message ?? "");
- if (reason === "run_not_found") {
- expect(message.startsWith("run_not_found:")).toBe(true);
- } else if (reason === "missing_boundaries") {
- expect(message.startsWith("workflow_amend_missing_boundaries:")).toBe(true);
- expect(message).toMatch(/fresh CreateWorkflow/u);
- } else {
- expect(message.startsWith("workflow_amend_refused:")).toBe(true);
- expect(message).toContain("teapot");
- }
- expect(message).toContain("dwfrun-prev");
- expect(message).toMatch(/nothing was stopped or created/iu);
- },
- );
- it("a script that does not compile never asks and never touches the predecessor", async () => {
- const name = "amend-broken";
- const { port, amends } = stubRunPort({ snapshot: { parentSessionId: "sess_other" } });
- const outcome = await run({ name, port, script: BROKEN_SCRIPT });
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(0);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.ok).toBe(false);
- expect(output.diagnostics.length).toBeGreaterThan(0);
- expect(output.response).toContain("Run dwfrun-prev was not touched");
- });
- it("without a run port the script is only typechecked", async () => {
- const outcome = await run({ name: "amend-no-port" });
- // 端口缺席:resolveInput 放行、权限照常 ask(没有归属事实)、handler 只报编译通过。
- expect(outcome.permissionRequested).toHaveLength(1);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.ok).toBe(true);
- expect(output.backgroundTaskId).toBeUndefined();
- expect(output.response).toContain("NOT executed");
- });
- });
- // 并发上界的三态(docs/dynamic-workflow/concurrency.md「Two bounds on a run」):省略 = 沿用前驱、
- // `null` = 解除、数 = 设定。三态只活到 resolveInput——handler 与确认窗此后只面对「一个数或没有」,
- // 而 handler 自己会用 .strict() 重新 parse 一遍归一化后的入参,所以形状越界在这些用例里必炸。
- describe("AmendWorkflow — max_concurrency", () => {
- /** 归一化后递到端口的那个数;键缺席即 undefined。 */
- async function amendWith(
- name: string,
- options: { defaultConcurrency?: number; inherited?: number; requested?: number | null },
- ): Promise<{ amends: DynamicWorkflowRunAmendRequest[]; outcome: RunOutcome }> {
- const { port, amends } = stubRunPort({
- snapshot: {
- parentSessionId: sessionOf(name),
- status: "completed",
- runStatus: "completed",
- ...(options.inherited === undefined ? {} : { maxConcurrency: options.inherited }),
- },
- ...(options.defaultConcurrency === undefined
- ? {}
- : { defaultConcurrency: options.defaultConcurrency }),
- });
- const outcome = await run({
- name,
- port,
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript(name),
- ...(options.requested === undefined ? {} : { max_concurrency: options.requested }),
- },
- });
- return { amends, outcome };
- }
- it("a number above the default goes through unclamped and is named in the result", async () => {
- const { amends, outcome } = await amendWith("amend-maxconc-raised", {
- defaultConcurrency: 8,
- requested: 32,
- });
- expect(amends[0]?.maxConcurrency).toBe(32);
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "At most 32 subagents run at once.",
- );
- });
- it("a number equal to the default is named as the default", async () => {
- const { amends, outcome } = await amendWith("amend-maxconc-at-default", {
- defaultConcurrency: 8,
- requested: 8,
- });
- expect(amends[0]?.maxConcurrency).toBe(8);
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "At most 8 subagents run at once (the default).",
- );
- });
- it("a number under the default goes through unchanged", async () => {
- const { amends, outcome } = await amendWith("amend-maxconc-set", {
- defaultConcurrency: 8,
- requested: 2,
- });
- expect(amends[0]?.maxConcurrency).toBe(2);
- const response = (outcome.result.output as CreateWorkflowOutput).response;
- expect(response).toContain("At most 2 subagents run at once.");
- expect(response).not.toContain("(the default)");
- });
- it("null removes the predecessor's limit: the key is gone, not zeroed", async () => {
- const { amends, outcome } = await amendWith("amend-maxconc-null", {
- defaultConcurrency: 8,
- inherited: 2,
- requested: null,
- });
- expect(amends).toHaveLength(1);
- expect("maxConcurrency" in amends[0]!).toBe(false);
- expect((outcome.result.output as CreateWorkflowOutput).response).not.toContain("At most");
- });
- it("omitting the field inherits the predecessor's limit", async () => {
- const { amends, outcome } = await amendWith("amend-maxconc-inherit", {
- defaultConcurrency: 8,
- inherited: 2,
- });
- expect(amends[0]?.maxConcurrency).toBe(2);
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "At most 2 subagents run at once.",
- );
- });
- it("omitting the field inherits a raised predecessor's limit as is", async () => {
- // 快照只在不等于默认时带 maxConcurrency:一个被调高过的前驱把它的数原样传下去,不再被钳。
- const { amends } = await amendWith("amend-maxconc-inherit-raised", {
- defaultConcurrency: 8,
- inherited: 40,
- });
- expect(amends[0]?.maxConcurrency).toBe(40);
- });
- it("omitting it stays absent when the predecessor ran at the default", async () => {
- // 快照只在不等于默认时带 maxConcurrency,所以「前驱没设过」在这里就是字段缺席。
- const { amends } = await amendWith("amend-maxconc-none", { defaultConcurrency: 8 });
- expect(amends).toHaveLength(1);
- expect("maxConcurrency" in amends[0]!).toBe(false);
- });
- it("shows the inherited limit in the confirmation window of another session's run", async () => {
- // 确认窗读的是归一化后的入参:沿用下来的上界必须在那里,否则用户批准的是另一个 run。
- const { port } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", maxConcurrency: 2 },
- defaultConcurrency: 8,
- });
- const outcome = await run({
- name: "amend-maxconc-window",
- port,
- input: { run_id: "dwfrun-prev", script: cleanScript("amend-maxconc-window") },
- });
- expect(outcome.permissionRequested).toHaveLength(1);
- expect(outcome.permissionRequested[0]!.input).toMatchObject({ max_concurrency: 2 });
- });
- it("strips null and passes a number through when the session has no run port", async () => {
- // 端口缺席:既没有前驱也没有默认并发,三态塌成「一个数或没有」,且归一化后的入参仍要
- // 过得了 handler 的 .strict() 重解析。
- const removed = await run({
- name: "amend-maxconc-no-port-null",
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript("amend-maxconc-no-port-null"),
- max_concurrency: null,
- },
- });
- expect(removed.result.success).toBe(true);
- expect(removed.permissionRequested[0]!.input).not.toHaveProperty("max_concurrency");
- const kept = await run({
- name: "amend-maxconc-no-port-number",
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript("amend-maxconc-no-port-number"),
- max_concurrency: 4,
- },
- });
- expect(kept.permissionRequested[0]!.input).toMatchObject({ max_concurrency: 4 });
- });
- });
- // 子代理模型的三态(docs/dynamic-workflow/launch.md):省略 = 沿用前驱、`null` = 回到会话模型、
- // 字符串 = 设定。与并发上界逐条同构,唯一的差别是**沿用的那一个也要重新解析一遍**——前驱可能
- // 是几天前起的,它选的模型此后可能被删掉或停用。
- describe("AmendWorkflow — subagent_model", () => {
- function entry(
- providerId: string,
- modelId: string,
- extra: Partial<ModelCatalogEntry> = {},
- ): ModelCatalogEntry {
- return { providerId, modelId, reasoningLevels: [], current: false, ...extra };
- }
- const CATALOG: ModelCatalogPort = {
- listModels: () => [
- entry("bigmodel", "GLM-4.6", { current: true }),
- entry("openai", "gpt-5"),
- ],
- };
- /** 目录里只剩一个模型:前驱选过的那一个已经不在了。 */
- const SHRUNK_CATALOG: ModelCatalogPort = {
- listModels: () => [entry("openai", "gpt-5", { current: true })],
- };
- async function amendWith(
- name: string,
- options: {
- catalog?: ModelCatalogPort;
- inherited?: string;
- requested?: string | null;
- },
- ): Promise<{ amends: DynamicWorkflowRunAmendRequest[]; outcome: RunOutcome }> {
- const { port, amends } = stubRunPort({
- snapshot: {
- parentSessionId: sessionOf(name),
- status: "completed",
- runStatus: "completed",
- ...(options.inherited === undefined ? {} : { subagentModel: options.inherited }),
- },
- });
- const outcome = await run({
- name,
- port,
- ...(options.catalog === undefined ? {} : { modelCatalogPort: options.catalog }),
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript(name),
- ...(options.requested === undefined ? {} : { subagent_model: options.requested }),
- },
- });
- return { amends, outcome };
- }
- it("a model id is resolved to its canonical form and named in the result", async () => {
- const { amends, outcome } = await amendWith("amend-model-set", {
- catalog: CATALOG,
- requested: "gpt-5",
- });
- expect(amends[0]?.subagentModel).toEqual({ providerId: "openai", modelId: "gpt-5" });
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "Subagents run on openai/gpt-5 (the main agent stays on the session model).",
- );
- });
- it("null puts the subagents back on the session model: the key is gone, not blanked", async () => {
- const { amends, outcome } = await amendWith("amend-model-null", {
- catalog: CATALOG,
- inherited: "bigmodel/GLM-4.6",
- requested: null,
- });
- expect(amends).toHaveLength(1);
- expect("subagentModel" in amends[0]!).toBe(false);
- expect((outcome.result.output as CreateWorkflowOutput).response).not.toContain(
- "Subagents run on",
- );
- });
- it("omitting the field inherits the predecessor's model", async () => {
- const { amends, outcome } = await amendWith("amend-model-inherit", {
- catalog: CATALOG,
- inherited: "bigmodel/GLM-4.6",
- });
- expect(amends[0]?.subagentModel).toEqual({ providerId: "bigmodel", modelId: "GLM-4.6" });
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "Subagents run on bigmodel/GLM-4.6",
- );
- });
- it("omitting it stays absent when the predecessor ran on the session model", async () => {
- const { amends } = await amendWith("amend-model-none", { catalog: CATALOG });
- expect(amends).toHaveLength(1);
- expect("subagentModel" in amends[0]!).toBe(false);
- });
- /**
- * 沿用的那一个也要重新解析:不重解的话,失败要等到子代理第一次开口时才发生,那时看起来像
- * 运行时故障而不是一次可以当场改掉的选择。
- */
- it("fails now, not at the first ask, when the inherited model has vanished from the catalog", async () => {
- const { amends, outcome } = await amendWith("amend-model-vanished", {
- catalog: SHRUNK_CATALOG,
- inherited: "bigmodel/GLM-4.6",
- });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- expect(outcome.permissionRequested).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- // 文案必须说清它是**继承来的**:这次调用压根没提模型名。
- expect(message).toContain("inherited the predecessor run's subagent model");
- expect(message).toContain("bigmodel/GLM-4.6");
- expect(message).toContain("subagent_model: null");
- expect(message).toContain("Nothing was stopped or created.");
- });
- it("an unresolvable id stops the call before anything is stopped or created", async () => {
- const { amends, outcome } = await amendWith("amend-model-miss", {
- catalog: CATALOG,
- requested: "gemini-3",
- });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(String(outcome.result.error?.message ?? "")).toContain("No configured model matches");
- });
- it("shows the inherited model in the confirmation window of another session's run", async () => {
- // 确认窗读的是归一化后的入参:沿用下来的模型必须在那里,否则用户批准的是另一个 run。
- const { port } = stubRunPort({
- snapshot: {
- parentSessionId: "sess_someone-else",
- status: "running",
- subagentModel: "bigmodel/GLM-4.6",
- },
- });
- const outcome = await run({
- name: "amend-model-window",
- port,
- modelCatalogPort: CATALOG,
- input: { run_id: "dwfrun-prev", script: cleanScript("amend-model-window") },
- });
- expect(outcome.permissionRequested).toHaveLength(1);
- expect(outcome.permissionRequested[0]!.input).toMatchObject({
- subagent_model: "bigmodel/GLM-4.6",
- });
- });
- it("refuses a model id on a host with no catalog, and still strips null", async () => {
- const refused = await amendWith("amend-model-no-catalog", { requested: "gpt-5" });
- expect(refused.outcome.result.success).toBe(false);
- expect(String(refused.outcome.result.error?.message ?? "")).toContain(
- "This host cannot choose a subagent model",
- );
- const removed = await amendWith("amend-model-no-catalog-null", { requested: null });
- expect(removed.outcome.result.success).toBe(true);
- expect("subagentModel" in removed.amends[0]!).toBe(false);
- });
- it("strips the field and passes a resolved id through when the session has no run port", async () => {
- // 端口缺席:没有前驱可沿用,`null` 与省略都塌成缺席,而给了字符串仍然要解析一次——
- // 归一化后的入参还要过得了 handler 的 .strict() 重解析。
- const resolved = await run({
- name: "amend-model-no-port",
- modelCatalogPort: CATALOG,
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript("amend-model-no-port"),
- subagent_model: "gpt-5",
- },
- });
- expect(resolved.result.success).toBe(true);
- expect(resolved.permissionRequested[0]!.input).toMatchObject({
- subagent_model: "openai/gpt-5",
- });
- const removed = await run({
- name: "amend-model-no-port-null",
- modelCatalogPort: CATALOG,
- input: {
- run_id: "dwfrun-prev",
- script: cleanScript("amend-model-no-port-null"),
- subagent_model: null,
- },
- });
- expect(removed.result.success).toBe(true);
- expect(removed.permissionRequested[0]!.input).not.toHaveProperty("subagent_model");
- });
- });
- // 省略 `script` = 沿用前驱的脚本(docs/dynamic-workflow/launch.md「Keeping the predecessor's
- // script」):与两个设定同一条规则、同一个生命周期——resolveInput 读前驱存档的脚本回填进入参并
- // 盖上 `predecessor.script_inherited`,此后 hook、确认窗、prepareApproval 与 handler 面对的都是
- // 「一份脚本」,端口也永远收到一份脚本。
- describe("AmendWorkflow — omitted script", () => {
- const STORED = fixture("strand-fanout-two-phases-join.ts");
- it("keeps the predecessor's script byte for byte and changes only the settings", async () => {
- const name = "amend-keep-script";
- const { port, amends, getScriptCalls } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- defaultConcurrency: 8,
- storedScript: STORED,
- });
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(outcome.result.success).toBe(true);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(getScriptCalls).toEqual(["dwfrun-prev"]);
- expect(amends).toHaveLength(1);
- expect(amends[0]!.scriptText).toBe(STORED);
- expect(amends[0]!.maxConcurrency).toBe(2);
- // 阶段表从回填的脚本上取:侧栏轨道画的仍是将要跑的那份脚本。
- expect(amends[0]!.phaseNames).toEqual(["A", "B", "C"]);
- expect(amends[0]!.phaseAlongside).toEqual([[], [0], []]);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.status).toBe("backgrounded");
- expect(output.response).toContain(
- "The script of run dwfrun-prev started unchanged in the background as run dwfrun-amended.",
- );
- expect(output.response).not.toContain("revised script");
- expect(output.response).toContain("At most 2 subagents run at once.");
- });
- it("with nothing but run_id, re-runs the script under the predecessor's settings", async () => {
- const name = "amend-keep-everything";
- const catalog: ModelCatalogPort = {
- listModels: () => [
- { providerId: "openai", modelId: "gpt-5", reasoningLevels: [], current: true },
- ],
- };
- const { port, amends } = stubRunPort({
- snapshot: {
- parentSessionId: sessionOf(name),
- status: "failed",
- runStatus: "errored",
- maxConcurrency: 3,
- subagentModel: "openai/gpt-5",
- },
- defaultConcurrency: 8,
- storedScript: STORED,
- });
- const outcome = await run({
- name,
- port,
- modelCatalogPort: catalog,
- input: { run_id: "dwfrun-prev" },
- });
- expect(outcome.result.success).toBe(true);
- expect(amends).toHaveLength(1);
- expect(amends[0]).toMatchObject({
- scriptText: STORED,
- maxConcurrency: 3,
- subagentModel: { providerId: "openai", modelId: "gpt-5" },
- });
- });
- it("asks for another session's run with the predecessor's script, its graph and the inherited flag", async () => {
- const stored = cleanScript("amend-keep-foreign");
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", name: "triage" },
- storedScript: stored,
- });
- const outcome = await run({
- name: "amend-keep-foreign",
- port,
- input: { run_id: "dwfrun-prev" },
- });
- expect(outcome.permissionRequested).toHaveLength(1);
- const payload = outcome.permissionRequested[0]!;
- // 确认窗与 hook 读的是归一化后的入参:脚本就是将要跑的那一份,flag 让窗上说「脚本不变」。
- expect(payload.input).toMatchObject({
- run_id: "dwfrun-prev",
- script: stored,
- predecessor: {
- name: "triage",
- status: "running",
- owned_by_this_session: false,
- script_inherited: true,
- },
- });
- expect(payload.display?.kind).toBe("create_workflow");
- expect(
- (payload.display as { causalityGraph?: { steps: unknown[] } }).causalityGraph?.steps,
- ).toHaveLength(1);
- expect(amends).toHaveLength(1);
- expect(amends[0]!.scriptText).toBe(stored);
- });
- it("a passed script never reads the stored one and carries no inherited flag", async () => {
- const script = cleanScript("amend-pass-script");
- const { port, amends, getScriptCalls } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "completed" },
- storedScript: "return 'stale';",
- });
- const outcome = await run({ name: "amend-pass-script", port, script });
- expect(getScriptCalls).toHaveLength(0);
- const predecessor = (outcome.permissionRequested[0]!.input as Record<string, unknown>)
- .predecessor as Record<string, unknown>;
- expect(predecessor).not.toHaveProperty("script_inherited");
- expect(amends[0]!.scriptText).toBe(script);
- expect((outcome.result.output as CreateWorkflowOutput).response).toContain(
- "The revised script started in the background as run dwfrun-amended.",
- );
- });
- it("an unknown run is refused as run_not_found before the script is read", async () => {
- const { port, amends, getScriptCalls } = stubRunPort({ storedScript: STORED });
- const outcome = await run({
- name: "amend-keep-unknown",
- port,
- input: { run_id: "dwfrun-prev" },
- });
- expect(outcome.result.success).toBe(false);
- expect(String(outcome.result.error?.message ?? "").startsWith("run_not_found:")).toBe(true);
- expect(getScriptCalls).toHaveLength(0);
- expect(amends).toHaveLength(0);
- });
- it.each([
- [
- "a predecessor whose record has no script",
- "no-record",
- { storedScript: null },
- "has no stored script",
- ],
- ["a host whose port cannot read scripts", "no-method", {}, "cannot read"],
- ] as const)(
- "%s is refused before any window, pointing at the whole script",
- async (_label, key, options, detail) => {
- const name = `amend-keep-unavailable-${key}`;
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- ...options,
- });
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_amend_script_unavailable:")).toBe(true);
- expect(message).toContain("dwfrun-prev");
- expect(message).toContain(detail);
- expect(message).toContain("pass the whole script");
- expect(message).toContain("Nothing was stopped or created.");
- },
- );
- it("a session without a run port is refused the same way: there is nothing to typecheck", async () => {
- const outcome = await run({ name: "amend-keep-no-port", input: { run_id: "dwfrun-prev" } });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_amend_script_unavailable:")).toBe(true);
- expect(message).toContain("pass the whole script");
- });
- /**
- * 继承来的脚本编不过(facade 在前驱之后变过):模型这次调用没写这些行,不说清就会把诊断读成
- * 自己传错了参数——与「继承来的模型已不可用」同一条理由。
- */
- it("an inherited script that no longer compiles says it was inherited, and touches nothing", async () => {
- const name = "amend-keep-broken";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "running" },
- storedScript: BROKEN_SCRIPT,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(0);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.ok).toBe(false);
- expect(output.diagnostics.length).toBeGreaterThan(0);
- const [lead] = output.response.split("\n");
- expect(lead).toContain("run dwfrun-prev's script");
- expect(lead).toContain("inherited because you omitted both `script` and `path`");
- expect(output.response).toMatch(/L\d+:C\d+ /u);
- // 沿用的脚本与任何不来自文件的脚本一样落成草稿(docs/dynamic-workflow/launch.md「Script files」),
- // 所以下一步是去改那个文件,而不是把整份脚本内联再抄一遍。
- expect(output.response).toContain("Edit that file in place and resubmit with `path:");
- expect(output.response).toContain("Run dwfrun-prev was not touched");
- });
- });
- // 只改并发、run 还活着 = 就地调并发,不是一次修订(docs/dynamic-workflow/launch.md「Changing only
- // the parallelism of a live run」):同一个 run id、不铸后继、不 supersede、不读脚本、不编译、
- // 一个窗都不弹。路由在 resolveInput 判定,handler 调的是 `port.retuneConcurrency`。
- describe("AmendWorkflow — 只改并发即就地生效", () => {
- const STORED = fixture("strand-fanout-two-phases-join.ts");
- /** 一个还在跑、属于本会话、端口带 retuneConcurrency 的前驱。 */
- function liveRetunablePort(
- name: string,
- options: {
- defaultConcurrency?: number;
- inherited?: number;
- owner?: string;
- retune?: StubPortOptions["retune"];
- runStatus?: DynamicWorkflowRunSnapshot["runStatus"];
- storedScript?: string | null;
- } = {},
- ): ReturnType<typeof stubRunPort> {
- return stubRunPort({
- snapshot: {
- parentSessionId: options.owner ?? sessionOf(name),
- status: "running",
- ...(options.runStatus === undefined ? {} : { runStatus: options.runStatus }),
- ...(options.inherited === undefined ? {} : { maxConcurrency: options.inherited }),
- },
- defaultConcurrency: options.defaultConcurrency ?? 8,
- retune: options.retune ?? { ok: true, maxConcurrency: 2, previous: 8, defaultConcurrency: 8 },
- ...(options.storedScript === undefined ? {} : { storedScript: options.storedScript }),
- });
- }
- it("retunes in place: one run named, no successor, no compile, no script read, no window", async () => {
- const name = "retune-ok";
- const { port, amends, retunes, getScriptCalls } = liveRetunablePort(name, {
- retune: { ok: true, maxConcurrency: 2, previous: 8, defaultConcurrency: 8 },
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(true);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(outcome.brokerRequests).toHaveLength(0);
- // 这条路不读脚本、不编译、不起后继。
- expect(getScriptCalls).toHaveLength(0);
- expect(amends).toHaveLength(0);
- expect(retunes).toEqual([{ runId: "dwfrun-prev", maxConcurrency: 2 }]);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.ok).toBe(true);
- expect(output.diagnostics).toEqual([]);
- expect(output.status).toBeUndefined();
- expect(output.backgroundTaskId).toBeUndefined();
- expect(output.causalityGraph).toBeUndefined();
- // 「就地生效」是一个显式的块,消费方不按形状猜;工具卡据它不画「已编译」。
- expect(output.retuned).toEqual({
- runId: "dwfrun-prev",
- maxConcurrency: 2,
- previous: 8,
- defaultConcurrency: 8,
- });
- // 没有 display 载荷:这条路一行脚本都没编译,而 `create_workflow` 的 `ok` 在卡上读作「已编译」。
- expect(outcome.result.display).toBeUndefined();
- expect(output.response).toContain("Applied to the running run dwfrun-prev");
- expect(output.response).toContain("at most 2 subagents run at once");
- expect(output.response).toContain("no new run was started");
- // 只点名一个 run:模型据此分辨这次调用走的是哪条路。
- expect(output.response).not.toContain("superseded");
- });
- it("null travels to the port as null and the applied default is named as the default", async () => {
- const name = "retune-null";
- const { port, retunes } = liveRetunablePort(name, {
- inherited: 2,
- retune: { ok: true, maxConcurrency: 8, previous: 2, defaultConcurrency: 8 },
- });
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: null },
- });
- // 端口收的是三态里的 `null` 本身:默认并发那个数只有端口知道,工具不猜第二遍。
- expect(retunes).toEqual([{ runId: "dwfrun-prev", maxConcurrency: null }]);
- const response = (outcome.result.output as CreateWorkflowOutput).response;
- expect(response).toContain("Applied to the running run dwfrun-prev");
- expect(response).toContain("at most 8 subagents run at once (the default)");
- });
- it("a number above the default reaches the port as is and is applied as is", async () => {
- const name = "retune-raised";
- const { port, retunes } = liveRetunablePort(name, {
- defaultConcurrency: 16,
- inherited: 2,
- retune: { ok: true, maxConcurrency: 32, previous: 2, defaultConcurrency: 16 },
- });
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 32 },
- });
- // 默认并发不是上限:工具不钳,端口也不钳,模型读到的就是它要的那个 32。
- expect(retunes[0]?.maxConcurrency).toBe(32);
- const response = (outcome.result.output as CreateWorkflowOutput).response;
- expect(response).toContain("at most 32 subagents run at once");
- expect(response).not.toContain("(the default)");
- });
- it("the same bound is refused `unchanged` before any hook, naming the bound in force", async () => {
- const name = "retune-unchanged";
- const { port, retunes, amends, getScriptCalls } = liveRetunablePort(name, { inherited: 2 });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- // 早于 hook 与确认窗:端口一次都没被碰过。
- expect(retunes).toHaveLength(0);
- expect(amends).toHaveLength(0);
- expect(getScriptCalls).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_retune_unchanged:")).toBe(true);
- expect(message).toContain("dwfrun-prev");
- expect(message).toContain("2");
- expect(message).toMatch(/nothing was stopped/iu);
- });
- it("null against a run already at the default is `unchanged` too", async () => {
- const name = "retune-unchanged-default";
- const { port, retunes } = liveRetunablePort(name);
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: null },
- });
- expect(outcome.result.success).toBe(false);
- expect(retunes).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_retune_unchanged:")).toBe(true);
- expect(message).toContain(
- "already runs at the default parallelism (at most 8 subagents at once)",
- );
- });
- it("the port's own `unchanged` (the value moved under us) is the same refusal", async () => {
- const name = "retune-unchanged-race";
- const { port, amends } = liveRetunablePort(name, {
- inherited: 4,
- retune: { ok: false, reason: "unchanged", current: 2 },
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_retune_unchanged:")).toBe(true);
- expect(message).toContain("2");
- });
- it.each([
- ["a revised script", { script: cleanScript("retune-with-script") }],
- ["a script file", { path: "nope.ts" }],
- ["a subagent model", { subagent_model: null }],
- ["a name", { name: "second pass" }],
- ] as const)("%s sends the call down the amendment path", async (_label, extra) => {
- const name = `retune-not-only-${Object.keys(extra)[0]}`;
- const { port, retunes, amends } = liveRetunablePort(name, { storedScript: STORED });
- const outcome = await run({
- name,
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2, ...extra },
- });
- expect(retunes).toHaveLength(0);
- if ("path" in extra) {
- // 读不出来的文件是修订自己的失败——证明这次调用确实走了修订。
- expect(outcome.result.success).toBe(false);
- expect(String(outcome.result.error?.message ?? "")).toContain(
- "workflow_script_file_unreadable:",
- );
- return;
- }
- expect(amends).toHaveLength(1);
- expect((outcome.result.output as CreateWorkflowOutput).status).toBe("backgrounded");
- });
- it("a run that already settled takes the amendment, unchanged", async () => {
- const name = "retune-settled";
- const { port, retunes, amends, getScriptCalls } = liveRetunablePort(name, {
- runStatus: "errored",
- storedScript: STORED,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(retunes).toHaveLength(0);
- expect(getScriptCalls).toEqual(["dwfrun-prev"]);
- expect(amends).toHaveLength(1);
- expect(amends[0]?.maxConcurrency).toBe(2);
- expect((outcome.result.output as CreateWorkflowOutput).status).toBe("backgrounded");
- });
- it("a port with no retuneConcurrency keeps today's amendment, window and all", async () => {
- const { port, amends, getScriptCalls } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", name: "triage" },
- defaultConcurrency: 8,
- storedScript: STORED,
- });
- const outcome = await run({
- name: "retune-old-host",
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(getScriptCalls).toEqual(["dwfrun-prev"]);
- expect(outcome.permissionRequested).toHaveLength(1);
- expect(amends).toHaveLength(1);
- expect((outcome.result.output as CreateWorkflowOutput).status).toBe("backgrounded");
- });
- // 「没有窗」对任何归属都成立:窗是为了把脚本摆到人面前,而这条路一段脚本都不跑。
- /**
- * 拒绝必须与本工具既有的那些(run_not_found / missing_boundaries / script_unchanged)走**同一个
- * 出口**:`success: false` + 带码的 `error`,没有 output。这条路不带 display 载荷,读侧(工具卡)
- * 因此只能靠「是不是错误」分辨「调成功了」与「被拒了」——两种结局的入参长得一模一样。
- */
- /**
- * 回话文本是**唯一**过得了 v4 的事实(这条路没有 display 载荷,协议的 toolOutput 只带 text),
- * 工具卡拿它当整行画。所以两种上界都逐字钉住:改词等于改 UI。
- */
- it.each([
- [
- "a bound below the default",
- { ok: true, maxConcurrency: 2, previous: 8, defaultConcurrency: 8 } as const,
- "Applied to the running run dwfrun-prev; at most 2 subagents run at once. Run dwfrun-prev keeps running under it: nothing was stopped and no new run was started.",
- ],
- [
- "a bound of one",
- { ok: true, maxConcurrency: 1, previous: 8, defaultConcurrency: 8 } as const,
- "Applied to the running run dwfrun-prev; at most 1 subagent runs at once. Run dwfrun-prev keeps running under it: nothing was stopped and no new run was started.",
- ],
- [
- "the default",
- { ok: true, maxConcurrency: 8, previous: 2, defaultConcurrency: 8 } as const,
- "Applied to the running run dwfrun-prev; at most 8 subagents run at once (the default). Run dwfrun-prev keeps running under it: nothing was stopped and no new run was started.",
- ],
- [
- "a bound above the default",
- { ok: true, maxConcurrency: 40, previous: 8, defaultConcurrency: 8 } as const,
- "Applied to the running run dwfrun-prev; at most 40 subagents run at once. Run dwfrun-prev keeps running under it: nothing was stopped and no new run was started.",
- ],
- ] as const)("reads back verbatim for %s", async (label, retune, expected) => {
- const name = `retune-text-${label.replace(/\s+/gu, "-")}`;
- const { port } = liveRetunablePort(name, { inherited: 4, retune });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect((outcome.result.output as CreateWorkflowOutput).response).toBe(expected);
- });
- it("a refusal comes back as a tool error with no output, like every other one", async () => {
- const name = "retune-refusal-shape";
- const { port } = liveRetunablePort(name, { inherited: 2 });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(false);
- expect(outcome.result.output).toBeNull();
- expect(outcome.result.error?.type).toBe(CoreErrorType.ToolExecutionFailed);
- // 码是稳定判别键(house rule:不拿错误文本做流程判断),取自码表而不是散落的字面量。
- expect(outcome.result.error?.code).toBe(String(AMEND_WORKFLOW_ERROR_CODE.RETUNE_UNCHANGED));
- expect(
- String(outcome.result.error?.message ?? "").startsWith("workflow_retune_unchanged:"),
- ).toBe(true);
- });
- it("another session's live run is retuned with no window either", async () => {
- const { port, retunes } = liveRetunablePort("retune-foreign-live", {
- owner: "sess_someone-else",
- });
- const outcome = await run({
- name: "retune-foreign-live",
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(outcome.brokerRequests).toHaveLength(0);
- expect(retunes).toHaveLength(1);
- expect(outcome.result.success).toBe(true);
- });
- });
- // 结算竞态(docs/dynamic-workflow/launch.md「When the port says `not_live`」):同一份入参此刻
- // 描述的是一次修订,能不能做取决于「那次修订本来要不要开窗」。
- describe("AmendWorkflow — 就地调并发遇上 not_live", () => {
- const STORED = fixture("strand-fanout-two-phases-join.ts");
- /**
- * resolveInput 看到的还是活的(端口随后答 not_live);handler 再读一次快照时它已是 `settled`
- * 描述的那个终态。两次 getTask 答不同的事实,正是这条路要覆盖的竞态。
- */
- function racingPort(options: {
- owner: string;
- inherited?: number;
- settled?: Partial<DynamicWorkflowRunSnapshot>;
- storedScript?: string | null;
- }): ReturnType<typeof stubRunPort> {
- const base = stubRunPort({
- snapshot: {
- parentSessionId: options.owner,
- status: "running",
- ...(options.inherited === undefined ? {} : { maxConcurrency: options.inherited }),
- },
- defaultConcurrency: 8,
- retune: { ok: false, reason: "not_live" },
- ...(options.storedScript === undefined ? {} : { storedScript: options.storedScript }),
- });
- let reads = 0;
- const port = base.port as unknown as Record<string, unknown>;
- const first = port.getTask as (runId: string) => Promise<DynamicWorkflowRunSnapshot>;
- port.getTask = async (runId: string) => {
- const snapshot = await first(runId);
- reads += 1;
- // 第一次(resolveInput)仍读作在跑;第二次(handler 的落回路)读到终态。
- return reads === 1 ? snapshot : ({ ...snapshot, ...options.settled } as typeof snapshot);
- };
- return base;
- }
- it("this session's own run falls through to a real amendment, reading the script at that point", async () => {
- const name = "retune-race-own";
- const { port, retunes, amends, getScriptCalls } = racingPort({
- owner: sessionOf(name),
- settled: { status: "completed", runStatus: "completed" },
- storedScript: STORED,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(retunes).toHaveLength(1);
- // 脚本是**这时候**才读的:retune 路上一次都没读过。
- expect(getScriptCalls).toEqual(["dwfrun-prev"]);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(1);
- expect(amends[0]).toMatchObject({ scriptText: STORED, maxConcurrency: 2 });
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.status).toBe("backgrounded");
- expect(output.response).toContain("started unchanged in the background as run dwfrun-amended");
- });
- it("null on the fall-through removes the limit: the port gets no maxConcurrency key", async () => {
- const name = "retune-race-own-null";
- const { port, amends } = racingPort({
- owner: sessionOf(name),
- // 前驱本来压在 2 上,所以 `null`(回默认)确实是一次改动,不是 `unchanged`。
- inherited: 2,
- settled: { status: "completed", runStatus: "completed" },
- storedScript: STORED,
- });
- await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: null } });
- expect(amends).toHaveLength(1);
- expect("maxConcurrency" in amends[0]!).toBe(false);
- });
- it("a missing script on the fall-through is reported as the amendment's own refusal", async () => {
- const name = "retune-race-own-no-script";
- const { port, amends } = racingPort({
- owner: sessionOf(name),
- settled: { status: "completed", runStatus: "completed" },
- storedScript: null,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_amend_script_unavailable:")).toBe(true);
- expect(message).toContain("pass the whole script");
- });
- it("a script that no longer compiles on the fall-through comes back as diagnostics", async () => {
- const name = "retune-race-own-broken";
- const { port, amends } = racingPort({
- owner: sessionOf(name),
- settled: { status: "failed", runStatus: "errored" },
- storedScript: BROKEN_SCRIPT,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(amends).toHaveLength(0);
- const output = outcome.result.output as CreateWorkflowOutput;
- expect(output.ok).toBe(false);
- expect(output.diagnostics.length).toBeGreaterThan(0);
- expect(output.response).toContain("Run dwfrun-prev was not touched");
- });
- it("another session's settled run is refused `run_settled`, not amended", async () => {
- const { port, amends } = racingPort({
- owner: "sess_someone-else",
- settled: { status: "completed", runStatus: "completed" },
- storedScript: STORED,
- });
- const outcome = await run({
- name: "retune-race-foreign-settled",
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(outcome.result.success).toBe(false);
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(0);
- // 与既有拒绝同一个出口:错误 + output 为 null(读侧没有 display 可看,只能靠这个分辨结局)。
- expect(outcome.result.output).toBeNull();
- expect(outcome.result.error?.type).toBe(CoreErrorType.ToolExecutionFailed);
- expect(outcome.result.error?.code).toBe(String(AMEND_WORKFLOW_ERROR_CODE.RUN_SETTLED));
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_run_settled:")).toBe(true);
- expect(message).toContain("dwfrun-prev");
- expect(message).toMatch(/call .*again/iu);
- });
- it("another session's run this agent never held live is refused `not_retunable`", async () => {
- const { port, amends } = racingPort({
- owner: "sess_someone-else",
- settled: { runStatus: "pending" },
- storedScript: STORED,
- });
- const outcome = await run({
- name: "retune-race-foreign-pending",
- port,
- input: { run_id: "dwfrun-prev", max_concurrency: 2 },
- });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- expect(outcome.result.output).toBeNull();
- expect(outcome.result.error?.type).toBe(CoreErrorType.ToolExecutionFailed);
- expect(outcome.result.error?.code).toBe(String(AMEND_WORKFLOW_ERROR_CODE.NOT_RETUNABLE));
- const message = String(outcome.result.error?.message ?? "");
- expect(message.startsWith("workflow_run_not_retunable:")).toBe(true);
- expect(message).toContain("dwfrun-prev");
- });
- // 用户亲手停下的 run 在修订路上本来要开窗(owner 规则把它排除在外),所以落回路不能替它跳过。
- it("an own run the user stopped is refused instead of silently amended", async () => {
- const name = "retune-race-own-user-stopped";
- const { port, amends } = racingPort({
- owner: sessionOf(name),
- settled: { status: "cancelled", runStatus: "stopped", stopReason: "user" },
- storedScript: STORED,
- });
- const outcome = await run({ name, port, input: { run_id: "dwfrun-prev", max_concurrency: 2 } });
- expect(outcome.result.success).toBe(false);
- expect(amends).toHaveLength(0);
- expect(String(outcome.result.error?.message ?? "").startsWith("workflow_run_settled:")).toBe(
- true,
- );
- });
- });
- // docs/dynamic-workflow/launch.md「What the model is told」:三条 bullet 说的是**同一条**规则(省略即
- // 沿用前驱),脚本在最前——模型若以为脚本必填,就会为了改一个数把几千 token 的脚本再抄一遍。
- // 2026-09-21 起描述只剩路由(docs/dynamic-workflow/launch.md「What the model is told」):沿用规则的
- // 三态字段、两条来路的先后与就地调并发那一句都搬进了 dynamic-workflows 技能 §16.4,由技能门保证
- // 读过;它们的顺序断言在 bootstrap/tests/dynamic-workflow-skill.test.ts。这里只钉描述必须留下的路由。
- describe("AmendWorkflow — description", () => {
- it("routes: a settings-only revision names neither script field, resume is another tool", () => {
- const description = amendWorkflowToolEntry.metadata.description;
- expect(description).toContain("neither `path` nor `script`");
- // 停下的 run 不改任何东西就继续:那是 ResumeWorkflowRun 的活,不是一次空修订。
- expect(description).toContain("To continue a stopped run unchanged, use ResumeWorkflowRun");
- // 跑到一半就修,不先停不等结束——门的边界也写在这里:带脚本才需要技能。
- expect(description).toContain("Do not TaskStop it first");
- expect(description).toContain("a settings-only call is not");
- });
- });
- // docs/dynamic-workflow/launch.md「Adjusting the settings in the window」:另一个会话的 run 才开窗,
- // 窗里的调整与 CreateWorkflow 同一条路——回填块、批准后落进入参、结果先说一句。
- describe("AmendWorkflow — settings adjusted in the confirmation window", () => {
- const CATALOG: ModelCatalogPort = {
- listModels: () => [
- { providerId: "openai", modelId: "gpt-5", reasoningLevels: [], current: true },
- ],
- };
- it("the ask carries the adjustable_settings block next to the predecessor facts", async () => {
- const { port } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", maxConcurrency: 2 },
- defaultConcurrency: 8,
- });
- const outcome = await run({ modelCatalogPort: CATALOG, name: "amend-adjustable", port });
- expect(outcome.permissionRequested[0]!.input).toMatchObject({
- max_concurrency: 2,
- adjustable_settings: { subagent_model: true, concurrency_ceiling: 8 },
- });
- });
- it("applies the adjustments to the amendment and names them in the result", async () => {
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: "sess_someone-else", status: "running", maxConcurrency: 2 },
- defaultConcurrency: 8,
- });
- const outcome = await run({
- broker: {
- async requestPermission() {
- return {
- decision: "allow",
- inputAdjustments: { subagent_model: "gpt-5", max_concurrency: null },
- };
- },
- },
- modelCatalogPort: CATALOG,
- name: "amend-adjusted",
- port,
- });
- expect(amends).toHaveLength(1);
- expect(amends[0]!.subagentModel).toEqual({ providerId: "openai", modelId: "gpt-5" });
- expect("maxConcurrency" in amends[0]!).toBe(false);
- const response = (outcome.result.output as CreateWorkflowOutput).response;
- expect(response).toContain(
- "Before approving, the user adjusted the settings in the confirmation window: subagents run on openai/gpt-5 (the main agent stays on the session model); the limit on subagents at once is back to the default.",
- );
- expect(response).not.toContain("At most 2");
- });
- it("an own run amends without a window, so it carries the block but never an adjustment", async () => {
- const name = "amend-adjustable-owned";
- const { port, amends } = stubRunPort({
- snapshot: { parentSessionId: sessionOf(name), status: "completed", runStatus: "completed" },
- defaultConcurrency: 8,
- });
- const outcome = await run({ name, port });
- expect(outcome.permissionRequested).toHaveLength(0);
- expect(amends).toHaveLength(1);
- expect((outcome.result.output as CreateWorkflowOutput).response).not.toContain(
- "Before approving",
- );
- });
- });
|