Files
wehub-resource-sync 426e9eeabd
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
Chat shell gestures / Chat shell gesture + parity e2e (push) Has been cancelled
Cloud Gateway Discord / Test (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Has been cancelled
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Has been cancelled
Build Agent Image / build-and-push (push) Has been cancelled
Dev Smoke / bun run dev onboarding chat (push) Has been cancelled
Dev Smoke / Vite HMR dependency-level smoke (push) Has been cancelled
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Has been cancelled
Publish @elizaos/example-code / check_npm (push) Has been cancelled
Publish @elizaos/example-code / publish_npm (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / verify_version (push) Has been cancelled
Publish @elizaos/plugin-elizacloud / publish_npm (push) Has been cancelled
Sandbox Live Smoke / Sandbox live smoke (push) Has been cancelled
Snap Build & Test / Build Snap (amd64) (push) Has been cancelled
Snap Build & Test / Build Snap (arm64) (push) Has been cancelled
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Has been cancelled
Cloud Gateway Webhook / Test (push) Has been cancelled
Cloud Tests / lint-and-types (push) Has been cancelled
Cloud Tests / unit-tests (push) Has been cancelled
Cloud Tests / integration-tests (push) Has been cancelled
Cloud Tests / e2e-tests (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Apps Worker (Product 2) / Determine environment (push) Has been cancelled
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Has been cancelled
Deploy Eliza Provisioning Worker / Determine environment (push) Has been cancelled
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Has been cancelled
Dev Smoke / Classify changed paths (push) Has been cancelled
supply-chain / sbom (push) Has been cancelled
supply-chain / vulnerability-scan (push) Has been cancelled
Build, Push & Deploy to Phala Cloud / build-and-push (push) Has been cancelled
Test Packaging / Validate Packaging Configs (push) Has been cancelled
Test Packaging / Build & Test PyPI Package (push) Has been cancelled
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Has been cancelled
Test Packaging / Pack & Test JS Tarballs (push) Has been cancelled
UI Fixture E2E / ui-fixture-e2e (push) Has been cancelled
UI Fixture E2E / fixture-e2e (push) Has been cancelled
UI Story Gate / story-gate (push) Has been cancelled
vault-ci / test (macos-latest) (push) Has been cancelled
vault-ci / test (ubuntu-latest) (push) Has been cancelled
vault-ci / test (windows-latest) (push) Has been cancelled
vault-ci / app-core wiring tests (push) Has been cancelled
verify-patches / verify patches/CHECKSUMS.sha256 (push) Has been cancelled
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Has been cancelled
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Has been cancelled
Voice Benchmark Smoke / voice bench smoke summary (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Has been cancelled
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Has been cancelled
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Has been cancelled
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

388 lines
12 KiB
TypeScript

/**
* Verifies TASKS:spawn_agent.
* Deterministic unit test with a stubbed runtime; no live model.
*/
import { describe, expect, it, vi } from "vitest";
// SPAWN_AGENT is `TASKS { action: "spawn_agent" }`.
import { spawnAgentAction } from "../../src/actions/tasks.js";
import {
callback,
memory,
runtimeWith,
serviceMock,
state,
} from "../../src/test-utils/action-test-utils.js";
const spawnOptions = { parameters: { action: "spawn_agent" } };
const TASK_ROOM = "11111111-2222-3333-4444-555555555555";
const WORKTREE_ROOM = "22222222-3333-4444-5555-666666666666";
describe("TASKS:spawn_agent", () => {
it("does not expose lockWorkdir to planner-generated tool calls", () => {
expect(
spawnAgentAction.parameters?.map((param) => param.name),
).not.toContain("lockWorkdir");
});
it("validates with explicit payload and a service available", async () => {
expect(
await spawnAgentAction.validate(
runtimeWith(serviceMock()),
memory({ task: "fix bug" }),
state,
),
).toBe(true);
expect(
await spawnAgentAction.validate(
runtimeWith(undefined),
memory({ task: "fix bug" }),
state,
),
).toBe(false);
});
it("keeps TASKS available for routed sub-agent terminal events", async () => {
expect(
await spawnAgentAction.validate(
runtimeWith(serviceMock()),
memory({
source: "sub_agent",
metadata: {
subAgent: true,
subAgentEvent: "task_complete",
subAgentSessionId: "abcdef123456",
},
}),
state,
),
).toBe(true);
expect(
await spawnAgentAction.validate(
runtimeWith(serviceMock()),
memory({ source: "sub_agent" }),
state,
),
).toBe(false);
});
it("spawns a session with compatible data shape", async () => {
const svc = serviceMock();
const cb = callback();
const workdir = process.cwd();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({ task: "fix bug", agentType: "codex", workdir }),
state,
spawnOptions,
cb,
);
expect(result?.success).toBe(true);
expect(result?.text).toBe("");
expect(cb).not.toHaveBeenCalled();
expect(result?.continueChain).toBe(false);
expect(result?.data).toMatchObject({
sessionId: "abcdef123456",
agentType: "codex",
workdir,
status: "ready",
});
});
it("carries the connector message id for platform-threaded final replies", async () => {
const svc = serviceMock();
await spawnAgentAction.handler(
runtimeWith(svc),
{
...memory({ task: "fix bug", agentType: "codex" }),
metadata: {
messageIdFull: "1506941896755249255",
discord: { messageId: "1506941896755249255" },
},
} as never,
state,
spawnOptions,
callback(),
);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
metadata?: Record<string, unknown>;
};
expect(call.metadata?.originConnectorMessageId).toBe("1506941896755249255");
});
it("stamps deterministic deduped task/worktree swarm room metadata", async () => {
const svc = serviceMock();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({
task: "coordinate the swarm",
agentType: "codex",
taskRoomId: TASK_ROOM,
worktreeRoomId: WORKTREE_ROOM,
}),
state,
spawnOptions,
callback(),
);
expect(result?.success).toBe(true);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
metadata?: Record<string, unknown>;
};
expect(call.metadata).toMatchObject({
roomId: TASK_ROOM,
originRoomId: "room1",
taskRoomId: TASK_ROOM,
worktreeRoomId: WORKTREE_ROOM,
swarmRooms: [
{ roomId: TASK_ROOM, roles: ["task"] },
{ roomId: WORKTREE_ROOM, roles: ["worktree"] },
],
});
});
it("injects focused coding and swarm coordination instructions", async () => {
const svc = serviceMock();
await spawnAgentAction.handler(
runtimeWith(svc),
memory({
task: "fix the failing tests",
agentType: "elizaos",
taskRoomId: TASK_ROOM,
worktreeRoomId: WORKTREE_ROOM,
}),
state,
spawnOptions,
callback(),
);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
initialTask?: string;
};
const initialTask = call.initialTask ?? "";
expect(initialTask).toContain("--- Swarm Coordination ---");
expect(initialTask).toContain("Keep working until the task is finished");
expect(initialTask).toContain("read/search files, edit/apply patches");
expect(initialTask).toContain("QUESTION_FOR_TASK_CREATOR");
expect(initialTask).toContain("AGENT_COORDINATION");
expect(initialTask).toContain(TASK_ROOM);
expect(initialTask).toContain(WORKTREE_ROOM);
// Regression for elizaOS/eliza#7935: sub-agents must not write
// routing-kind constants as markdown banners in user-visible prose.
// The router classifies routing from the session event; prose should
// stay as the actual question or coordination note.
expect(initialTask).toContain(
"Do not prefix the reply with routing-kind labels",
);
expect(initialTask).toContain("no markdown banners");
expect(initialTask).toContain(
"the orchestrator classifies routing from the session event, not your prose",
);
});
it("keeps both swarm roles when task room and worktree room are the same", async () => {
const svc = serviceMock();
await spawnAgentAction.handler(
runtimeWith(svc),
memory({
task: "coordinate in one room",
agentType: "codex",
taskRoomId: TASK_ROOM,
worktreeRoomId: TASK_ROOM,
}),
state,
spawnOptions,
callback(),
);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
metadata?: Record<string, unknown>;
};
expect(call.metadata?.swarmRooms).toEqual([
{ roomId: TASK_ROOM, roles: ["task", "worktree"] },
]);
});
it("does NOT defer from task text alone — deferral is structural, not regex", async () => {
// The planner emits the structured `deferUserReply` flag when the user asks
// for no interim reply; the orchestrator no longer regex-scans the task text
// for "reply only after …" phrasings (that was message-text inspection,
// which the project bans). The next test covers the structural path.
const svc = serviceMock();
const cb = callback();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({
task: "Build the app and verify the public URL. Reply only after verification with the final URL.",
agentType: "opencode",
}),
state,
spawnOptions,
cb,
);
expect(result?.success).toBe(true);
// No structured flag → not deferred, even though the text says "reply only after".
expect(result?.data).not.toMatchObject({ deferredUserReply: true });
});
it("honors explicit deferUserReply from planner parameters", async () => {
const svc = serviceMock();
const cb = callback();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({ task: "Build the app", agentType: "opencode" }),
state,
{ parameters: { action: "spawn_agent", deferUserReply: true } },
cb,
);
expect(result?.success).toBe(true);
expect(result?.text).toBe("");
expect(result?.data).toMatchObject({ deferredUserReply: true });
expect(cb).not.toHaveBeenCalled();
});
it("puts resolved route constraints before planner-authored task text", async () => {
const oldRoutes = process.env.TASK_AGENT_WORKDIR_ROUTES;
process.env.TASK_AGENT_WORKDIR_ROUTES = JSON.stringify([
{
id: "local-apps",
workdir: process.cwd(),
matchAny: ["counter"],
instructions: "Create app files under data/apps/<slug>/.",
urlMappings: [
{
urlPrefix: "https://example.test/apps/",
localPath: "data/apps/",
},
],
},
]);
try {
const svc = serviceMock();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({
task: "Create a counter at /srv/apps/opencode-check.",
agentType: "opencode",
}),
state,
spawnOptions,
callback(),
);
expect(result?.success).toBe(true);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
initialTask?: string;
workdir?: string;
};
expect(call.workdir).toBe(process.cwd());
const initialTask = call.initialTask ?? "";
expect(initialTask).toContain("--- Resolved Workspace ---");
expect(initialTask).toContain(`workdir: ${process.cwd()}`);
expect(initialTask).toContain("absolute path outside this workdir");
expect(initialTask).toContain(
"Create app files under data/apps/<slug>/.",
);
expect(initialTask).toContain("--- URL Path Mapping ---");
expect(initialTask).toContain(
"URL prefix https://example.test/apps/ maps to local path data/apps/ under the resolved workdir",
);
expect(initialTask).toContain(
"write files under data/apps/<slug>/, not apps/<slug>/ or public/apps/<slug>/",
);
expect(initialTask).toContain(
"do not leave synthetic external assets, pending-work comments, or partial sample code",
);
expect(initialTask).toContain('do not leave inert href="#" controls');
expect(initialTask.indexOf("--- Resolved Workspace ---")).toBeLessThan(
initialTask.indexOf("--- User Task ---"),
);
} finally {
if (oldRoutes === undefined) delete process.env.TASK_AGENT_WORKDIR_ROUTES;
else process.env.TASK_AGENT_WORKDIR_ROUTES = oldRoutes;
}
});
it("keeps an inherited workdir route for routed sub-agent follow-up turns", async () => {
const oldRoutes = process.env.TASK_AGENT_WORKDIR_ROUTES;
delete process.env.TASK_AGENT_WORKDIR_ROUTES;
try {
const svc = serviceMock();
const result = await spawnAgentAction.handler(
runtimeWith(svc),
memory({
source: "sub_agent",
metadata: {
subAgent: true,
workdirRoute: {
id: "local-apps",
workdir: process.cwd(),
instructions: "Write under data/apps/<slug>/.",
urlMappings: [
{
urlPrefix: "https://example.test/apps/",
localPath: "data/apps/",
},
],
},
},
}),
state,
{
parameters: {
action: "spawn_agent",
task: "Continue the failed static page build.",
agentType: "opencode",
},
},
callback(),
);
expect(result?.success).toBe(true);
const call = svc.spawnSession.mock.calls[0]?.[0] as {
initialTask?: string;
metadata?: Record<string, unknown>;
workdir?: string;
};
expect(call.workdir).toBe(process.cwd());
expect(call.metadata?.workdirRouteId).toBe("local-apps");
expect(call.initialTask).toContain("--- URL Path Mapping ---");
expect(call.initialTask).toContain("data/apps/<slug>/");
} finally {
if (oldRoutes === undefined) delete process.env.TASK_AGENT_WORKDIR_ROUTES;
else process.env.TASK_AGENT_WORKDIR_ROUTES = oldRoutes;
}
});
it("handles missing service and auth failures", async () => {
const cb = callback();
expect(
(
await spawnAgentAction.handler(
runtimeWith(undefined),
memory(),
state,
spawnOptions,
cb,
)
)?.error,
).toBe("SERVICE_UNAVAILABLE");
const svc = serviceMock({
spawnSession: vi.fn(async () => {
throw new Error("login required");
}),
});
expect(
(
await spawnAgentAction.handler(
runtimeWith(svc),
memory({ task: "x" }),
state,
spawnOptions,
callback(),
)
)?.error,
).toBe("INVALID_CREDENTIALS");
});
});