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

220 lines
8.1 KiB
TypeScript

/**
* Pins {@link OrchestratorTaskService.attachSession} — the public API used by
* `TASKS:create` to bind ACP sessions it spawned via `AcpService.spawnSession`
* directly (with its own multi-part label / prefix / model routing) into an
* existing task thread's session index. Without this the store's
* `sessionTaskIndex` never learns about those sessions, `resolveTaskId`
* returns undefined, the event bridge drops their events, and DTOs report
* `0/0 agents` with no token attribution.
*
* Pinned surfaces:
* 1. Attaching a LIVE session indexes it against the task (sessionCount
* goes up, latestSessionId flips, activeSessionCount reflects it) AND
* promotes the task status from `open` → `active`.
* 2. Attaching a TERMINAL-ON-ARRIVAL session (the chat create action's
* `runPromptAndClose` has already stopped it before mint) still indexes
* it for history + future token attribution, but does NOT falsely claim
* liveness (task status stays `open`, activeSessionCount stays 0).
* 3. `resolveTaskId` (exercised via the ACP event bridge) resolves the
* attached session back to its task, so post-attach session events land
* as task events and token usage rolls up onto the session record.
* 4. Idempotent: attaching the same sessionId twice is a no-op — no
* duplicate rows, no double-status-advance.
* 5. Unknown taskId returns `false` without throwing (attach failure in
* the create action must be logged, not demote the action's success).
*/
import type { IAgentRuntime } from "@elizaos/core";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { OrchestratorTaskService } from "../../src/services/orchestrator-task-service.js";
import { OrchestratorTaskStore } from "../../src/services/orchestrator-task-store.js";
// Suppress default-acceptance-criteria auto-fill so `task_complete` events in
// these tests don't fire the auto-verifier (matches the pattern used in
// orchestrator-task-service.test.ts).
const PREV_GOAL_CONTRACT = process.env.ELIZA_REQUIRE_GOAL_CONTRACT;
beforeAll(() => {
process.env.ELIZA_REQUIRE_GOAL_CONTRACT = "0";
});
afterAll(() => {
if (PREV_GOAL_CONTRACT === undefined)
delete process.env.ELIZA_REQUIRE_GOAL_CONTRACT;
else process.env.ELIZA_REQUIRE_GOAL_CONTRACT = PREV_GOAL_CONTRACT;
});
/**
* Minimal ACP stand-in: captures the orchestrator's event-subscription hook
* so a test can drive session events after attach (to exercise resolveTaskId
* via the bridge) without needing a real ACP subprocess.
*/
class FakeAcp {
private handler:
| ((sessionId: string, event: string, data: unknown) => void)
| null = null;
onSessionEvent(
cb: (sessionId: string, event: string, data: unknown) => void,
): () => void {
this.handler = cb;
return () => {
this.handler = null;
};
}
emit(sessionId: string, event: string, data: unknown = {}): void {
this.handler?.(sessionId, event, data);
}
}
function runtime(acp?: FakeAcp): IAgentRuntime {
return {
getService: () => acp ?? null,
logger: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
} as never;
}
async function makeService(acp?: FakeAcp): Promise<{
service: OrchestratorTaskService;
taskId: string;
}> {
const store = new OrchestratorTaskStore({ backend: "memory" });
const service = new OrchestratorTaskService(runtime(acp), { store });
await service.start();
const task = await service.createTask({
title: "Ship widget",
goal: "Bind chat-spawned sessions to the durable thread",
});
return { service, taskId: task.id };
}
/** Yield a macrotask so the event bridge's async work settles. */
const flush = (): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, 0));
describe("OrchestratorTaskService.attachSession", () => {
it("indexes a live session on the task and promotes status open→active", async () => {
const { service, taskId } = await makeService();
const before = await service.getTask(taskId);
expect(before?.status).toBe("open");
expect(before?.sessionCount).toBe(0);
expect(before?.activeSessionCount).toBe(0);
const ok = await service.attachSession(taskId, {
sessionId: "chat-sess-1",
agentType: "codex",
workdir: "/tmp/workdir",
status: "ready",
label: "planner",
originalTask: "wire attach",
model: "gpt-5.5",
});
expect(ok).toBe(true);
const after = await service.getTask(taskId);
expect(after?.sessionCount).toBe(1);
expect(after?.activeSessionCount).toBe(1);
expect(after?.latestSessionId).toBe("chat-sess-1");
expect(after?.status).toBe("active");
const attached = after?.sessions[0];
expect(attached?.sessionId).toBe("chat-sess-1");
expect(attached?.framework).toBe("codex");
expect(attached?.label).toBe("planner");
expect(attached?.model).toBe("gpt-5.5");
expect(attached?.originalTask).toBe("wire attach");
});
it("indexes a terminal-on-arrival session without falsely advancing status", async () => {
// Mirrors the real chat-create path: `runPromptAndClose` has already
// stopped the session before we mint the thread, so the SpawnResult's
// status is terminal by the time attach runs. We still want the session
// linked (for the widget's history + future token attribution), but the
// task should NOT be lied to about liveness.
const { service, taskId } = await makeService();
const ok = await service.attachSession(taskId, {
sessionId: "chat-sess-terminal",
agentType: "codex",
workdir: "/tmp/workdir",
status: "completed",
label: "planner",
});
expect(ok).toBe(true);
const detail = await service.getTask(taskId);
expect(detail?.sessionCount).toBe(1);
expect(detail?.activeSessionCount).toBe(0);
expect(detail?.status).toBe("open"); // NOT promoted
expect(detail?.sessions[0]?.status).toBe("completed");
expect(detail?.sessions[0]?.stoppedAt).toBeTypeOf("number");
});
it("makes resolveTaskId route post-attach session events onto the task", async () => {
// The event bridge's `resolveTaskId` is the whole reason this API exists.
// Attach → emit a session event on the fake ACP → the orchestrator
// resolves the session back to its task and records the event on the
// task's event log. Without attach the event would be dropped.
const acp = new FakeAcp();
const { service, taskId } = await makeService(acp);
await service.attachSession(taskId, {
sessionId: "chat-sess-2",
agentType: "codex",
workdir: "/tmp/workdir",
status: "ready",
label: "planner",
});
acp.emit("chat-sess-2", "message", { text: "hello from sub-agent" });
await flush();
const detail = await service.getTask(taskId);
const messageFromSubAgent = detail?.messages.find(
(m) => m.senderKind === "sub_agent" && m.sessionId === "chat-sess-2",
);
expect(messageFromSubAgent?.content).toBe("hello from sub-agent");
});
it("is idempotent: attaching the same sessionId twice does not duplicate rows", async () => {
const { service, taskId } = await makeService();
const first = await service.attachSession(taskId, {
sessionId: "chat-sess-3",
agentType: "codex",
workdir: "/tmp/workdir",
status: "ready",
label: "planner",
});
const second = await service.attachSession(taskId, {
sessionId: "chat-sess-3",
agentType: "codex",
workdir: "/tmp/workdir",
status: "ready",
label: "planner-2",
});
expect(first).toBe(true);
expect(second).toBe(true);
const detail = await service.getTask(taskId);
expect(detail?.sessionCount).toBe(1);
// The store's addSession upserts by sessionId — one row, regardless.
expect(
detail?.sessions.filter((s) => s.sessionId === "chat-sess-3").length,
).toBe(1);
});
it("returns false on unknown taskId without throwing", async () => {
const { service } = await makeService();
const ok = await service.attachSession("no-such-task", {
sessionId: "chat-sess-4",
agentType: "codex",
workdir: "/tmp/workdir",
status: "ready",
});
expect(ok).toBe(false);
});
});