Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

test(eve): cut slow and duplicate integration tests (#3790)

Andrew Barbae5c3d56c2a93
diff --git a/.agents/skills/test-audit/CAMPAIGN.md b/.agents/skills/test-audit/CAMPAIGN.md
new file mode 100644
index 000000000..d1b89af69
--- /dev/null
+++ b/.agents/skills/test-audit/CAMPAIGN.md
@@ -0,0 +1,120 @@
+# Test-pruning campaign
+
+Campaign mode prunes one subsystem's whole test surface in one PR: one core
+area such as `packages/eve/src/execution`, one tier slice such as the
+dev-server scenario suites, or one package such as `packages/eve-code`. The
+value bar, retention bar, candidate evidence, and validation in
+[SKILL.md](SKILL.md) apply to every lane. This file adds the order of work and
+the lessons of a full campaign. Each step ends on its completion criterion; do
+not start the next step early.
+
+## 1. Baseline
+
+Record the subsystem's test and support line counts, every test file's
+pass/fail state, and per-file and per-test durations (JSON reporter) at a
+pinned `main` SHA. Keep baseline failures in their own list: they are often
+real product bugs, not stale tests.
+
+Done when every in-scope test file has a recorded baseline result.
+
+## 2. Lanes and inventory
+
+Split the surface into **lanes** along production owner boundaries, not file
+prefixes. For example: session entry, workflow tools, tasks, client streaming,
+dev-server host, compiler, and build output. Include the subsystem's cases at
+shared core boundaries, its scenario suites, and its `e2e/` fixture evals.
+
+Done when every test file and e2e eval the subsystem owns belongs to exactly
+one lane.
+
+## 3. Read-only ledger per lane
+
+Give each lane to its own read-only agent. The agent reads every assigned test
+in full, including parameter tables. It also reads the production owners and
+their entry points, callers, history, and CI routing. Each test declaration
+goes into a written **ledger** with one mark. An `it.each` is one declaration
+unless its rows need different marks; then mark each row.
+
+- `R`: retain, naming the contract and the bug it catches; a retained test that
+ only moves to a better-named file or a cheaper tier stays `R` with the move
+ noted;
+- `F`: retain the contract but repair the assertion, such as a vacuous negative
+ that passes when only one of several items is missing;
+- `C`: consolidate, naming the owner that absorbs the assertion first: a sibling
+ table case, a stronger boundary suite, or the shared owner in another package;
+- `D`: delete, naming the proof that remains, or why no contract exists.
+
+Judge a test by its assertions, not its name. A test named for terminating a
+child can assert only that the child never completed.
+
+Done when every declaration in the lane has a mark and an evidence line.
+
+## 4. Layer plan per lane
+
+Treat the per-test ledger as input, not as the edit list. A second read-only
+pass, starting from the ledger, looks for the redundant **layer**: for example,
+an older scenario suite whose every case was later re-covered, more strongly,
+by newer chaos or connection suites. Name the **keeper** suite for each
+contract. Prefer the real transport boundary with a fake network over a mocked
+collaborator, and the cheapest tier that still reaches that boundary. Correct
+any ledger errors this pass finds.
+
+Done when each lane plan names its retired files, its keeper per contract, the
+assertions to carry into keepers, and the test-only production seams unlocked.
+
+## 5. Cutover
+
+Edit lane by lane. Serialize changes to shared harnesses and support files
+(`packages/eve/test/scenarios/dev-server-harness.ts`,
+`packages/eve/test/scenarios/dev-server-descriptors.ts`,
+`packages/eve/test/_helpers/`, `packages/eve/src/internal/testing/`) through
+one owner. With each lane, remove the test-only production seams it
+unlocks: injection parameters, getters, reset exports, and indirection layers.
+Update CI routing when suites move tiers. Shrink `pnpm guard:invariants`
+baselines when files disappear; baselines may only shrink. Put durable
+test-ownership rules in the relevant `AGENTS.md`, drawn from mistakes this
+campaign actually found.
+
+Done when every lane plan is applied and each lane's keepers pass.
+
+## 6. Preservation review
+
+Before claiming completion, have independent reviewers compare deleted
+coverage against the keepers, one reviewer per boundary group. They look for
+contracts that lost their only proof. They also look for new assertions that
+cannot fail, such as a rejection row the production code never reaches.
+
+For each restored contract, make one deliberate **mutation** of the production
+owner and confirm the keeper goes red. Then restore the source byte for byte.
+
+Done when every reported gap is restored or rejected with source evidence, and
+every restored contract has a caught mutation.
+
+## 7. Product defects
+
+A baseline failure that survives into a keeper is a bug report. Fix it at its
+owner as a separate commit with a changeset, and prove it through the real user
+flow, with a **control** run that reverts the fix and shows the old behavior.
+Record unrelated product discrepancies you find as follow-ups instead of fixing
+them in the campaign.
+
+Done when each repaired defect has a failing control and a passing candidate
+on the same harness.
+
+## 8. Reconcile and hand off
+
+Campaigns outlive many `main` commits. Merge `main` rather than rebasing a
+long, many-commit campaign. When `main` modified a file the campaign deleted,
+keep the deletion. Port the new contract into the keeper instead, and confirm
+every new regression `main` added still has a home. Rerun the whole subsystem
+suite on the merged head.
+
+Expect review tooling to see a truncated file list on a diff this large.
+
+Hand off with the [SKILL.md](SKILL.md) report, plus:
+
+- baseline and final test/support line counts, with production counted separately;
+- baseline and final tier wall times;
+- lanes, retired layers, and keepers;
+- preservation gaps found and their mutations;
+- product defects with control and candidate proof.
diff --git a/.agents/skills/test-audit/SKILL.md b/.agents/skills/test-audit/SKILL.md
new file mode 100644
index 000000000..8adf96dbb
--- /dev/null
+++ b/.agents/skills/test-audit/SKILL.md
@@ -0,0 +1,206 @@
+---
+name: test-audit
+description: "Invoke whenever writing, changing, reviewing, or sweeping tests in the eve repository. Authoring gate for new tests plus audit workflow for low-value, slow, implementation-coupled, or duplicative tests and the test-only production seams they demand."
+---
+
+# Test audit
+
+Adapted from the OpenClaw
+[`test-audit`](https://github.com/openclaw/openclaw/blob/main/.agents/skills/test-audit/SKILL.md)
+skill for eve's test tiers and tooling.
+
+Three modes, one value bar. Authoring mode gates every new or changed test at
+write time. Audit mode runs focused sweeps of tests that re-assert source,
+duplicate stronger proof, couple behavior to implementation, or keep test-only
+production seams alive. Continue broad audits as separate coherent follow-up
+PRs; optimize for confidence, not deletion count. Campaign mode prunes one
+whole subsystem's test surface (every test file one core area or package
+owns); before starting one, read [CAMPAIGN.md](CAMPAIGN.md).
+
+Read the root [`AGENTS.md`](../../../AGENTS.md) testing section first. It
+defines eve's four tiers (unit, integration, scenario, e2e); a test belongs in
+the tightest tier that can express its assertion.
+
+## Authoring gate
+
+Before adding any test, answer four questions; a missing answer means do not
+add it yet:
+
+1. What observable behavior, invariant, or independent contract does it protect?
+2. What credible regression makes it fail?
+3. Why does existing coverage not already catch that failure? Each contract has
+ one primary test owner at the strongest boundary; another layer needs its
+ own distinct risk, such as a transport or lifecycle failure the owner cannot
+ reach. Prefer extending a table-driven case or shared fixture over a
+ near-duplicate test; consolidate duplicated setup in the same change.
+4. Does it need a production seam (export, flag, wrapper, injection hook) that no
+ production caller needs? If yes, move the test to the real boundary instead.
+
+Then check the test against every [junk pattern](#junk-patterns); a match fails
+the gate unless the [retention bar](#retention-bar) names the contract it
+independently guards. A test that would break under behavior-preserving
+refactoring is asserting implementation, not behavior; rewrite it at the
+owning boundary before landing it.
+
+Also check its cost. A scenario test that boots a dev server, installs a
+tarball, or runs a real build must need that subprocess, port, or bundler. If
+it only needs `import "eve"` to resolve or an in-memory runtime, it belongs in
+integration (`useTemporaryAppRoots`, `createTestRuntime`). A test that waits
+out a real production timeout or backoff should use fake timers or an existing
+public option instead of wall-clock time; do not add a test-only seam to
+shorten it.
+
+Bug regression tests must fail on the pre-fix code for the intended reason and
+pass after the owner-boundary repair. A regression test that never demonstrably
+failed proves the mock, not the fix. One regression at the owner boundary
+covers the bug; do not replay the same scenario at every layer it crosses.
+
+## Junk patterns
+
+The shared checklist for both modes: the authoring gate rejects a new test that
+matches one, and audits hunt for existing tests that do.
+
+- assertion-free coverage probes;
+- self-comparisons and identity copiers;
+- copied fixtures, inventories, manifests, or export lists;
+- exact source, import, or string greps;
+- private predicate or call-shape tests duplicated at real boundaries;
+- duplicate invocations of the same contract, including the same scenario
+ replayed across several dev-server or build suites;
+- provider-local replays of shared helpers;
+- tests whose only purpose is preserving test-only exports, globals, or wrappers;
+- dead production code whose only callers are tests;
+- expected values produced by the helper or renderer under test;
+- mocks that implement the asserted behavior, or one identical mock standing in
+ for different APIs;
+- fixtures that supply the receipt, admission, or callback ordering the owner
+ should produce, or persistence asserted against a store the path never writes;
+- capability tests that restate declared flags instead of exercising the
+ delivery or acknowledgement the flag promises;
+- negative controls that pass for an unrelated reason, such as a denial from a
+ different guard or a rejection the production path never reaches;
+- names or fixtures that promise more than the input exercises, such as a
+ "terminates the child" test asserting only that the child never completed;
+- parameter matrices whose rows exercise the same code path;
+- suites gated on env flags or platforms that no CI job ever sets.
+
+## Value bar
+
+Tests justify their maintenance cost by protecting behavior, a credible
+regression, or an independently meaningful contract. In an audit, an existing
+test that must change for behavior-preserving source reorganization is suspect,
+not automatically deletable; the authoring gate still rejects new ones.
+
+Before judging a candidate, read the complete test and production owner, its
+entry point, callers, callees, sibling implementations, overlapping tests, CI
+routing (`.github/workflows/`), and relevant history. Read root and scoped
+`AGENTS.md` files first. When the test claims dependency-backed behavior,
+inspect the dependency source or types directly.
+
+## Discovery
+
+Keep discovery read-only and report evidence before editing. Baseline with the
+JSON reporter so slow files and tests are visible:
+
+```sh
+pnpm --filter eve exec vitest run --config vitest.<tier>.config.ts \
+ --reporter=json --outputFile=/tmp/<tier>.json
+```
+
+For broad scope, run parallel discovery lanes when available, split along
+production owner boundaries:
+
+- core unit tests (`packages/eve/src/{execution,harness,runtime,compiler,...}`);
+- CLI, setup, evals, tracing, and instrumentation;
+- public API, client, channels, and `src/internal`;
+- integration tier (`*.integration.test.ts`);
+- scenario tier (`*.scenario.test.ts`, `packages/eve/test/scenarios/`);
+- other packages and apps (`packages/eve-code`, `packages/eve-catalog`,
+ `apps/docs`, `e2e/`);
+- a cross-cutting pattern, test-only-export, and console-noise sweep.
+
+Outside campaign mode, prefer a few high-confidence candidates over a large
+speculative inventory. Hunt for the [junk patterns](#junk-patterns).
+
+## Retention bar
+
+Keep a test when it independently enforces a public API (`eve/*` exports),
+protocol, config, migration, storage, security, platform, default, prompt-byte,
+package, release, or architecture contract. Also keep:
+
+- call ordering when order is observable behavior;
+- regressions with a credible failure mode;
+- source inspection when it is the cheapest independent guard: it fails when
+ the contract changes (the user-facing key, byte, or path) and survives an
+ identifier-only refactor;
+- a retained test that fails on the baseline: treat it as a possible product
+ bug, reproduce it, and repair the owner rather than deleting it.
+
+Static or slow is not a deletion reason on its own. A slow test with a unique
+contract gets cheaper (shared boot, fake timers, lower tier), not deleted. A
+test that resembles implementation may still be the independent contract;
+prove otherwise before removing it.
+
+## Candidate evidence
+
+Record every field below before editing. A missing field means the candidate is
+not ready for deletion:
+
+- exact test name and location;
+- what failure it can actually detect;
+- non-test callers of the covered production or support seam;
+- stronger remaining owner-boundary proof, or why no proof is needed;
+- relevant history and the reason the test or seam exists;
+- production or test-support deletion unlocked;
+- risk, the focused validation command, and time saved when relevant.
+
+## Edit shape
+
+Choose one coherent owner-boundary batch. Delete obsolete test-only exports,
+globals, wrappers, and dead production paths instead of preserving aliases.
+Move retained regressions to their canonical owners. Consolidate repeated
+package or dependency assertions into one generic contract.
+
+Prefer net-negative production LOC. Do not add replacement tests that restate
+the same implementation, and do not convert uncertain candidates into cleanup
+to increase deletion counts. Do not commit fixture trees under
+`packages/eve/test/fixtures/`; scenario apps stay inline descriptors.
+
+## Validation
+
+Never edit source or tests while Vitest is running in the checkout.
+
+1. Run the smallest owner and sibling tests with the tier config, never bare
+ `vitest run`:
+ `pnpm --filter eve exec vitest run --config vitest.<tier>.config.ts <path>`.
+ Run `pnpm --filter eve build:compiled` first if `#compiled/*` changed, and
+ `pnpm build` before scenario runs.
+2. For removed source greps or invariant assertions, run the script that owns
+ the real contract, such as `pnpm guard:invariants`.
+3. Run `pnpm fmt`, `pnpm lint`, `pnpm typecheck`, then `git diff --check`.
+4. When deleted tests were the only callers of production code, `pnpm typecheck`
+ and `pnpm lint` are the proof that nothing else depended on it.
+5. Inspect `git diff --numstat`; report production/tooling separately from
+ tests and test support. Report before and after tier timings for speed work.
+6. Follow the root `AGENTS.md` changeset rule. Test-only changes need none;
+ removing dead production code from `packages/eve` still needs one.
+
+## Landing and continuation
+
+Commit, push, open a PR, or land only when authorized. Sign off every commit
+(`git commit -s`) and use the
+[`gh-pr-description`](../gh-pr-description/SKILL.md) skill. Land one coherent
+PR at a time; after landing, refresh from current `main` and rerun read-only
+discovery for the next high-confidence batch.
+
+## Handoff
+
+Report:
+
+- root cause and removed low-value categories;
+- production owner simplifications;
+- retained false positives and why they remain valuable;
+- focused and full proof actually run, with tier timings;
+- production versus test LOC;
+- PR and merge state;
+- named follow-ups.
diff --git a/AGENTS.md b/AGENTS.md
index 092560b8a..25903d5ee 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -160,6 +160,9 @@ or ownership relationship materially clearer.
## Testing
+Use the [`test-audit`](./.agents/skills/test-audit/SKILL.md) skill when
+writing, reviewing, or pruning tests.
+
Tests belong in one of four tiers. Pick the tightest tier that can express the
assertion:
diff --git a/packages/eve/src/cli/commands/init.integration.test.ts b/packages/eve/src/cli/commands/init.integration.test.ts
index 1fd84e612..26bf6b17b 100644
--- a/packages/eve/src/cli/commands/init.integration.test.ts
+++ b/packages/eve/src/cli/commands/init.integration.test.ts
@@ -595,15 +595,6 @@ describe("runInitCommand", () => {
expect(deps.runPackageManagerInstall).not.toHaveBeenCalled();
});
- it("does not offer self-modification when init was launched by a coding agent", async () => {
- const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-agent-launched-selfmod-"));
- const output = logger();
- const deps = dependencies();
- deps.isCodingAgentLaunch.mockResolvedValue(true);
-
- await runInitCommand(output, parentDirectory, "my-agent", {}, deps);
- });
-
it("uses an explicit init package spec for fresh project scaffolds", async () => {
const parentDirectory = await mkdtemp(join(tmpdir(), "eve-init-package-spec-"));
const output = logger();
diff --git a/packages/eve/src/client/stream-follow.integration.test.ts b/packages/eve/src/client/stream-follow.integration.test.ts
index 804b8647a..db0e80956 100644
--- a/packages/eve/src/client/stream-follow.integration.test.ts
+++ b/packages/eve/src/client/stream-follow.integration.test.ts
@@ -39,6 +39,8 @@ function follow(host: string, options?: { follow?: boolean }) {
resolveHeaders: () => Promise.resolve(new Headers()),
path: "/eve/v1/session/s/stream",
startIndex: 0,
+ // Keeps the default attempt budget so connection counts still pin it.
+ streamReconnectPolicy: { streamIdleReconnectPolicy: { baseDelayMs: 1, maxDelayMs: 1 } },
});
}
diff --git a/packages/eve/src/execution/durable-session-store.integration.test.ts b/packages/eve/src/execution/durable-session-store.integration.test.ts
index 5826776d9..e605aa129 100644
--- a/packages/eve/src/execution/durable-session-store.integration.test.ts
+++ b/packages/eve/src/execution/durable-session-store.integration.test.ts
@@ -33,34 +33,11 @@ describe("durableSessionStore integration", () => {
{ historyDepth: 3, marker: "beta", sessionId: result.sessionId },
{ historyDepth: 5, marker: "gamma", sessionId: result.sessionId },
]);
- });
- });
-
- it("a standalone read step after several writes returns the latest returned state", async () => {
- const runtime = await createTestRuntime({
- agent: { name: "durable-session-store-fixture-tail" },
- });
-
- await runtime.run(async () => {
- const run = await start(durableSessionStoreFixtureWorkflow, [
- {
- markers: [
- { marker: "first", historyDepth: 0 },
- { marker: "second", historyDepth: 2 },
- { marker: "third", historyDepth: 4 },
- { marker: "fourth", historyDepth: 6 },
- ],
- },
- ]);
-
- const result = await run.returnValue;
-
- // The read step runs after the loop with no intervening write of
- // its own, so this asserts the returned state still carries the
- // latest snapshot across a later step boundary.
+ // The tail read runs after the loop with no write of its own, so the
+ // returned state must still carry the latest snapshot across a step boundary.
expect(result.tailReadAfterAllWrites).toEqual({
- historyDepth: 6,
- marker: "fourth",
+ historyDepth: 5,
+ marker: "gamma",
sessionId: result.sessionId,
});
});
diff --git a/packages/eve/src/execution/legacy-session/import.integration.test.ts b/packages/eve/src/execution/legacy-session/import.integration.test.ts
index da2e011c5..11097b2cb 100644
--- a/packages/eve/src/execution/legacy-session/import.integration.test.ts
+++ b/packages/eve/src/execution/legacy-session/import.integration.test.ts
@@ -18,8 +18,6 @@ import {
describe("legacy session import", () => {
it.each([
{ inputVersion: 1 as const, inboxVersion: 1 },
- { inputVersion: 1 as const, inboxVersion: 3 },
- { inputVersion: 2 as const, inboxVersion: 7 },
{ inputVersion: 2 as const, inboxVersion: 7, duplicateImport: true },
{ inputVersion: 2 as const, inboxVersion: 7, committedInput: true },
])(
diff --git a/packages/eve/src/execution/session/entry-authorization.integration.test.ts b/packages/eve/src/execution/session/entry-authorization.integration.test.ts
new file mode 100644
index 000000000..e11bec8e1
--- /dev/null
+++ b/packages/eve/src/execution/session/entry-authorization.integration.test.ts
@@ -0,0 +1,664 @@
+import { describe, expect, it } from "vitest";
+import { resumeHook, start } from "#internal/workflow/runtime.js";
+import { filterEventsByType } from "#internal/testing/events.js";
+import { createTestRuntime } from "#internal/testing/app-harness.js";
+import { waitForHook } from "#internal/testing/workflow-test-helpers.js";
+import { waitForParkedTurnStep } from "#internal/testing/session-test-helpers.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import {
+ sessionCommandHookToken,
+ sessionInboxHookToken,
+} from "#execution/session-inbox/address.js";
+import { createToolExecuteWithAuth } from "#execution/tool-auth.js";
+import { ROOT_COMPILED_AGENT_NODE_ID } from "#compiler/manifest.js";
+import { ConnectionAuthorizationRequiredError } from "#connections/errors.js";
+import type { MessageStreamEvent } from "#protocol/message.js";
+import type { ToolContext } from "#tools/definition.js";
+import type {
+ AuthorizationDefinition,
+ ConnectionPrincipal,
+ TokenResult,
+} from "#shared/connection-types.js";
+import type { ResolvedToolDefinition } from "#runtime/types.js";
+import { toInputSchema } from "#tools/schema.js";
+import {
+ buildSerializedContext,
+ captureEvents,
+ expectHookClaims,
+ expectSingleTurn,
+} from "#internal/testing/entry-test-helpers.js";
+
+describe("workflowEntry integration", () => {
+ it("resumes normal follow-ups after an interactive authorization callback", async () => {
+ const { completeCalls, runtime } = await createWeatherAuthRuntime(
+ "workflow-entry-auth-followup",
+ );
+ const continuationToken = "http:workflow-entry-auth-followup";
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ serializedContext: buildSerializedContext({
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ channelKind: "http",
+ continuationToken,
+ mode: "conversation",
+ }),
+ },
+ ]);
+
+ const stream = captureEvents(run);
+
+ try {
+ const firstTurn = await stream.nextUntil(
+ "initial auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+ const required = filterEventsByType(firstTurn, "authorization.required");
+
+ expect(firstTurn.at(-1)?.type).toBe("authorization.required");
+ expect(required).toHaveLength(1);
+ expect(required[0]?.data).toMatchObject({
+ name: "weather",
+ authorization: { displayName: "Weather" },
+ });
+
+ // The authorization park closes its turn boundary so stream
+ // consumers do not hang on the parked turn.
+ const parkBoundary = await stream.nextUntil(
+ "authorization park boundary",
+ (event) => event.type === "session.waiting",
+ );
+ expect(parkBoundary.at(-1)?.type).toBe("session.waiting");
+ await expectHookClaims(run.runId, [sessionCommandHookToken(run.runId), continuationToken]);
+
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [
+ {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(firstTurn),
+ callback: {
+ method: "GET",
+ params: { code: "oauth-code" },
+ },
+ connectionName: "weather",
+ },
+ },
+ ],
+ });
+
+ const authorizedTurn = await stream.nextUntil(
+ "authorization callback turn",
+ (event) => event.type === "session.waiting",
+ );
+ const completed = filterEventsByType(authorizedTurn, "authorization.completed");
+
+ expect(completeCalls()).toBe(1);
+ expectSingleTurn(authorizedTurn, "turn_1");
+ expect(authorizedTurn.at(-1)?.type).toBe("session.waiting");
+ expect(completed).toHaveLength(1);
+ expect(completed[0]?.data).toMatchObject({
+ name: "weather",
+ outcome: "authorized",
+ });
+ expect(
+ authorizedTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("Used local weather tool for Lisbon") === true,
+ ),
+ ).toBe(true);
+
+ await waitForHook(
+ { runId: run.runId },
+ {
+ token: sessionInboxHookToken(continuationToken),
+ },
+ );
+ await resumeHook(sessionInboxHookToken(continuationToken), {
+ kind: "send",
+ payload: { message: "follow up after auth" },
+ });
+
+ const followupTurn = await stream.nextUntil(
+ "post-auth follow-up turn",
+ (event) => event.type === "session.waiting",
+ );
+
+ expect(followupTurn.at(-1)?.type).toBe("session.waiting");
+ expect(
+ followupTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("follow up after auth") === true,
+ ),
+ ).toBe(true);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+
+ it("runs ordinary deliveries while an authorization challenge stays open", async () => {
+ const { completeCalls, completedPrincipals, runtime } = await createWeatherAuthRuntime(
+ "workflow-entry-auth-open",
+ );
+ const continuationToken = "http:workflow-entry-auth-open";
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ serializedContext: buildSerializedContext({
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ channelKind: "http",
+ continuationToken,
+ mode: "conversation",
+ }),
+ },
+ ]);
+
+ const stream = captureEvents(run);
+
+ try {
+ const firstTurn = await stream.nextUntil(
+ "initial auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+ // Consume the park's own turn boundary so the next wait delimits
+ // the intervening message turn.
+ await stream.nextUntil(
+ "authorization park boundary",
+ (event) => event.type === "session.waiting",
+ );
+
+ // An ordinary message while the challenge is open runs as a normal
+ // turn instead of queueing behind the callback.
+ await waitForHook(
+ { runId: run.runId },
+ { token: sessionInboxHookToken(continuationToken) },
+ );
+ await resumeHook(sessionInboxHookToken(continuationToken), {
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-2",
+ principalType: "user",
+ },
+ kind: "send",
+ payload: { message: "Quick status note while I sign in." },
+ });
+
+ const interveningTurn = await stream.nextUntil(
+ "intervening message turn",
+ (event) => event.type === "session.waiting",
+ );
+ expect(
+ interveningTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("Quick status note while I sign in.") === true,
+ ),
+ ).toBe(true);
+ expect(filterEventsByType(interveningTurn, "authorization.completed")).toHaveLength(0);
+ expect(completeCalls()).toBe(0);
+
+ // The callback still lands on the retained read and closes the
+ // challenge exactly once.
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [
+ {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(firstTurn),
+ callback: {
+ method: "GET",
+ params: { code: "oauth-code" },
+ },
+ connectionName: "weather",
+ },
+ },
+ ],
+ });
+
+ const callbackTurn = await stream.nextUntil(
+ "authorization callback turn",
+ (event) => event.type === "session.waiting",
+ );
+ const completed = filterEventsByType(callbackTurn, "authorization.completed");
+ expectSingleTurn(callbackTurn, "turn_2");
+ expect(completed).toHaveLength(1);
+ expect(completed[0]?.data).toMatchObject({
+ name: "weather",
+ outcome: "authorized",
+ });
+
+ // The granted authorization serves the next explicit tool request.
+ // (No waitForHook here: it only reports never-received hooks, and
+ // the continuation hook already received the intervening message.)
+ await resumeHook(sessionInboxHookToken(continuationToken), {
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ kind: "send",
+ payload: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ });
+
+ const toolTurn = await stream.nextUntil(
+ "post-authorization tool turn",
+ (event) => event.type === "session.waiting",
+ );
+ expect(completeCalls()).toBe(1);
+ expect(completedPrincipals()).toEqual([
+ expect.objectContaining({ id: "user-1", type: "user" }),
+ ]);
+ expect(
+ toolTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("Used local weather tool for Lisbon") === true &&
+ event.data.message.includes("authorized with weather-token"),
+ ),
+ ).toBe(true);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+
+ it("defers ordinary deliveries while a task waits for authorization", async () => {
+ const { completeCalls, runtime } = await createWeatherAuthRuntime(
+ "workflow-entry-task-auth-open",
+ );
+ const continuationToken = "http:workflow-entry-task-auth-open";
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ serializedContext: buildSerializedContext({
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ channelKind: "http",
+ continuationToken,
+ mode: "task",
+ }),
+ },
+ ]);
+ const stream = captureEvents(run);
+
+ try {
+ const firstTurn = await stream.nextUntil(
+ "initial task auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+
+ await waitForHook(
+ { runId: run.runId },
+ { token: sessionInboxHookToken(continuationToken) },
+ );
+ await resumeHook(sessionInboxHookToken(continuationToken), {
+ kind: "send",
+ payload: { message: "This must not become a second task turn." },
+ });
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [
+ {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(firstTurn),
+ callback: { method: "GET", params: { code: "oauth-code" } },
+ connectionName: "weather",
+ },
+ },
+ ],
+ });
+
+ const completion = await stream.nextUntil(
+ "authorized task completion",
+ (event) => event.type === "session.completed",
+ );
+ const allEvents = [...firstTurn, ...completion];
+ expect(filterEventsByType(allEvents, "turn.started")).toHaveLength(1);
+ expect(filterEventsByType(allEvents, "message.received")).toHaveLength(1);
+ expect(filterEventsByType(allEvents, "authorization.completed")).toHaveLength(1);
+ expect(filterEventsByType(allEvents, "session.waiting")).toHaveLength(0);
+ expect(completeCalls()).toBe(1);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+
+ it("ignores stale and duplicate callbacks after a challenge is replaced", async () => {
+ const { completeCalls, runtime } = await createWeatherAuthRuntime(
+ "workflow-entry-auth-replaced",
+ );
+ const continuationToken = "http:workflow-entry-auth-replaced";
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ serializedContext: buildSerializedContext({
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ channelKind: "http",
+ continuationToken,
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureEvents(run);
+
+ try {
+ const firstAttempt = await stream.nextUntil(
+ "first auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+ await stream.nextUntil(
+ "first authorization park",
+ (event) => event.type === "session.waiting",
+ );
+
+ await waitForHook(
+ { runId: run.runId },
+ { token: sessionInboxHookToken(continuationToken) },
+ );
+ await resumeHook(sessionInboxHookToken(continuationToken), {
+ kind: "send",
+ payload: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ });
+ const replacementAttempt = await stream.nextUntil(
+ "replacement auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+ expect(filterEventsByType(replacementAttempt, "authorization.completed")).toMatchObject([
+ { data: { name: "weather", outcome: "failed" } },
+ ]);
+ await stream.nextUntil(
+ "replacement authorization park",
+ (event) => event.type === "session.waiting",
+ );
+
+ const stalePayload = {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(firstAttempt),
+ callback: { method: "GET", params: { code: "stale-oauth-code" } },
+ connectionName: "weather",
+ },
+ };
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [stalePayload],
+ });
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [stalePayload],
+ });
+
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [
+ {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(replacementAttempt),
+ callback: { method: "GET", params: { code: "oauth-code" } },
+ connectionName: "weather",
+ },
+ },
+ ],
+ });
+
+ const callbackTurn = await stream.nextUntil(
+ "replacement authorization callback turn",
+ (event) => event.type === "session.waiting",
+ );
+ expect(filterEventsByType(callbackTurn, "authorization.completed")).toHaveLength(1);
+ expect(completeCalls()).toBe(1);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+
+ it("completes the challenge after a no-op cancel consumed the parked wait", async () => {
+ const { completeCalls, runtime } = await createWeatherAuthRuntime("workflow-entry-auth-cancel");
+ const continuationToken = "http:workflow-entry-auth-cancel";
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: "Use the get_weather tool to check the weather in Lisbon." },
+ serializedContext: buildSerializedContext({
+ auth: {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ channelKind: "http",
+ continuationToken,
+ mode: "conversation",
+ }),
+ },
+ ]);
+
+ const stream = captureEvents(run);
+
+ try {
+ const firstTurn = await stream.nextUntil(
+ "initial auth-required event",
+ (event) => event.type === "authorization.required",
+ );
+ await stream.nextUntil(
+ "authorization park boundary",
+ (event) => event.type === "session.waiting",
+ );
+
+ await waitForParkedTurnStep(run.runId);
+
+ // A cancel with no active turn is consumed by the parked wait
+ // without producing a parent turn. The callback must still surface
+ // in the continued wait instead of stalling until unrelated
+ // session activity re-parks the owner.
+ await waitForHook(
+ { runId: run.runId },
+ { token: sessionInboxHookToken(continuationToken) },
+ );
+ await resumeHook(sessionInboxHookToken(continuationToken), { kind: "cancel" });
+ // Let the owner consume the no-op cancel and re-enter the parked
+ // wait before the callback fires; back-to-back resumes could
+ // otherwise surface the callback in the first wait iteration and
+ // mask a wait that ignores callbacks after a consumed cancel.
+ await new Promise((resolve) => setTimeout(resolve, 250));
+
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
+ kind: "authorization-callback",
+ payloads: [
+ {
+ authorizationCallback: {
+ attemptId: authorizationAttemptId(firstTurn),
+ callback: {
+ method: "GET",
+ params: { code: "oauth-code" },
+ },
+ connectionName: "weather",
+ },
+ },
+ ],
+ });
+
+ const callbackTurn = await stream.nextUntil(
+ "authorization callback turn",
+ (event) => event.type === "session.waiting",
+ );
+ const completed = filterEventsByType(callbackTurn, "authorization.completed");
+
+ expect(completeCalls()).toBe(1);
+ expect(completed).toHaveLength(1);
+ expect(completed[0]?.data).toMatchObject({
+ name: "weather",
+ outcome: "authorized",
+ });
+ expect(filterEventsByType(callbackTurn, "turn.cancelled")).toHaveLength(0);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+});
+
+interface WeatherAuthRuntime {
+ completeCalls(): number;
+ completedPrincipals(): readonly ConnectionPrincipal[];
+ runtime: Awaited<ReturnType<typeof createTestRuntime>>;
+}
+
+/**
+ * A get_weather tool behind an interactive authorization: getToken always
+ * requires sign-in, and completeAuthorization mints `weather-token` from the
+ * `oauth-code` callback. Shared by the callback-resume and
+ * challenge-stays-open owner tests.
+ */
+async function createWeatherAuthRuntime(agentName: string): Promise<WeatherAuthRuntime> {
+ let completeCalls = 0;
+ const completedPrincipals: ConnectionPrincipal[] = [];
+ const weatherAuth: AuthorizationDefinition<{ nonce: string }> = {
+ principalType: "user",
+ async getToken(): Promise<TokenResult> {
+ throw new ConnectionAuthorizationRequiredError("weather");
+ },
+ async startAuthorization({ callbackUrl }) {
+ return {
+ challenge: {
+ displayName: "Weather",
+ instructions: "Sign in to continue.",
+ url: `https://idp.example/authorize?callback=${encodeURIComponent(callbackUrl)}`,
+ },
+ resume: { nonce: "weather-nonce" },
+ };
+ },
+ async completeAuthorization({ callback, principal, resume }): Promise<TokenResult> {
+ completeCalls += 1;
+ completedPrincipals.push(principal);
+ expect(callback.params.code).toBe("oauth-code");
+ expect(resume).toEqual({ nonce: "weather-nonce" });
+ return { token: "weather-token" };
+ },
+ };
+ const getWeatherTool: ResolvedToolDefinition = {
+ description: "Get the current weather for a city.",
+ execute: createToolExecuteWithAuth({
+ scope: "get_weather",
+ async execute(rawInput, rawCtx) {
+ const ctx = rawCtx as ToolContext;
+ const token = await ctx.getToken(weatherAuth, {
+ authKey: "weather",
+ displayName: "Weather",
+ });
+ const city =
+ typeof rawInput === "object" &&
+ rawInput !== null &&
+ typeof (rawInput as { city?: unknown }).city === "string"
+ ? (rawInput as { city: string }).city
+ : "Lisbon";
+ return {
+ city,
+ condition: "Sunny",
+ summary: `authorized with ${token.token}`,
+ temperatureF: 72,
+ };
+ },
+ }),
+ inputSchema: toInputSchema({
+ additionalProperties: false,
+ properties: {
+ city: { type: "string" },
+ },
+ required: ["city"],
+ type: "object",
+ }),
+ logicalPath: "tools/get_weather.ts",
+ name: "get_weather",
+ owner: { kind: "application" },
+ sourceId: "tools/get_weather.ts",
+ sourceKind: "module",
+ };
+ const runtime = await createTestRuntime({
+ agent: { name: agentName },
+ tools: [getWeatherTool],
+ });
+ const manifestTool = runtime.manifest.tools.find((tool) => tool.name === getWeatherTool.name);
+ if (manifestTool === undefined) {
+ throw new Error("Expected get_weather to be present in the test manifest.");
+ }
+ runtime.moduleMap.nodes[ROOT_COMPILED_AGENT_NODE_ID]!.modules[manifestTool.sourceId] = {
+ default: {
+ execute: getWeatherTool.execute,
+ },
+ };
+ return {
+ completeCalls: () => completeCalls,
+ completedPrincipals: () => completedPrincipals,
+ runtime,
+ };
+}
+
+function authorizationAttemptId(events: readonly MessageStreamEvent[]): string {
+ const required = filterEventsByType(events, "authorization.required")[0];
+ const webhookUrl = required?.data.webhookUrl;
+ if (webhookUrl === undefined) throw new Error("Missing authorization callback URL.");
+ const segments = new URL(webhookUrl).pathname.split("/");
+ const callbackIndex = segments.lastIndexOf("callback");
+ const attemptId = segments[callbackIndex + 1];
+ if (callbackIndex === -1 || attemptId === undefined) {
+ throw new Error("Authorization callback URL is missing its attempt ID.");
+ }
+ return decodeURIComponent(attemptId);
+}
diff --git a/packages/eve/src/execution/session/entry-handoff-lifetime.integration.test.ts b/packages/eve/src/execution/session/entry-handoff-lifetime.integration.test.ts
new file mode 100644
index 000000000..9178c0479
--- /dev/null
+++ b/packages/eve/src/execution/session/entry-handoff-lifetime.integration.test.ts
@@ -0,0 +1,230 @@
+import { DEFAULT_SESSION_TIMEOUT_MS } from "#execution/session/timeout.js";
+import { describe, expect, it, vi } from "vitest";
+import { getWorld, resumeHook, start } from "#internal/workflow/runtime.js";
+import { hydrateStepReturnValue, hydrateWorkflowArguments } from "@workflow/core/serialization";
+import { captureTurnEvents } from "#internal/testing/events.js";
+import { createTestRuntime } from "#internal/testing/app-harness.js";
+import { waitForParkedTurnStep } from "#internal/testing/session-test-helpers.js";
+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import {
+ sessionCommandHookToken,
+ sessionInboxHookToken,
+} from "#execution/session-inbox/address.js";
+import { createWorkflowRuntime, waitForCommandHookOwner } from "#execution/workflow-runtime.js";
+import {
+ buildSerializedContext,
+ handoffFollowUp,
+ listCallerStepNames,
+} from "#internal/testing/entry-test-helpers.js";
+
+describe("workflowEntry integration", () => {
+ describe("deployment handoff", () => {
+ it.each([undefined, 60_000, false] as const)(
+ "renews the configured lifetime across handoffs and keeps the original stream (%s)",
+ async (sessionTimeoutMs) => {
+ const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff" } });
+
+ await runtime.run(async () => {
+ const anchor = await start(workflowEntry, [
+ {
+ kind: "initial",
+ sessionTimeoutMs,
+ ownerDeploymentId: "dpl_a",
+ input: { message: "hello from a" },
+ serializedContext: buildSerializedContext({
+ acceptedDeploymentId: "dpl_a",
+ channelKind: "http",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(anchor);
+ const world = await getWorld();
+ const workflowRuntime = createWorkflowRuntime({
+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
+ });
+ let completed = false;
+ try {
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ await waitForParkedTurnStep(anchor.runId);
+
+ const originalTimer =
+ sessionTimeoutMs === false ? undefined : await readSessionTimer(anchor.runId);
+
+ await expect(
+ workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "hello from b", "delivery-b"),
+ sessionId: anchor.runId,
+ }),
+ ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
+
+ const secondTurn = await stream.nextTurn();
+ expect(secondTurn.at(-1)?.type).toBe("session.waiting");
+ expect(
+ secondTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("hello from b") === true,
+ ),
+ ).toBe(true);
+
+ // The stable inbox now belongs to a successor run; the original run
+ // holds only its anchor (plus the SDK's abort-signal hook from its
+ // own earlier turn).
+ const successor = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ );
+ expect(successor.runId).not.toBe(anchor.runId);
+ const successorTimer =
+ sessionTimeoutMs === false ? undefined : await readSessionTimer(successor.runId);
+ if (
+ sessionTimeoutMs !== false &&
+ originalTimer !== undefined &&
+ successorTimer !== undefined
+ ) {
+ expect(successorTimer.deadline.getTime()).toBeGreaterThan(
+ originalTimer.deadline.getTime(),
+ );
+ const owner = await world.runs.get(successor.runId);
+ expect(successorTimer.deadline.getTime()).toBeGreaterThanOrEqual(
+ (owner.startedAt ?? owner.createdAt).getTime() +
+ (sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS),
+ );
+ await vi.waitFor(async () =>
+ expect((await world.runs.get(originalTimer.runId)).status).toBe("cancelled"),
+ );
+ }
+ // A timer from the old owner can win its race with cancellation.
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(anchor.runId)), {
+ kind: "session-timeout",
+ ownerRunId: anchor.runId,
+ });
+ // The successor can finish its turn before the old owner resumes
+ // from activation and disposes its temporary handoff hook.
+ await vi.waitFor(async () => {
+ const anchorHooks = await world.hooks.list({ runId: anchor.runId });
+ expect(
+ anchorHooks.data
+ .map((hook) => hook.token)
+ .filter((token) => !token.startsWith("abrt_")),
+ ).toEqual([`${anchor.runId}:anchor`]);
+ });
+
+ // A third delivery through the stable session id reaches the successor
+ // and still streams on the original run.
+ await expect(
+ workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "third message", "delivery-c"),
+ sessionId: anchor.runId,
+ }),
+ ).resolves.toMatchObject({ status: "accepted" });
+ const thirdTurn = await stream.nextTurn();
+ expect(
+ thirdTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("third message") === true,
+ ),
+ ).toBe(true);
+
+ if (successorTimer !== undefined) {
+ expect(await readSessionTimer(successor.runId)).toEqual(successorTimer);
+ }
+ await waitForParkedTurnStep(successor.runId, 2);
+ await workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_c", "fourth message", "delivery-d"),
+ sessionId: anchor.runId,
+ });
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ const nextOwner = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ );
+ expect(nextOwner.runId).not.toBe(successor.runId);
+ await vi.waitFor(async () =>
+ expect((await world.runs.get(successor.runId)).status).toBe("completed"),
+ );
+ expect((await world.runs.get(anchor.runId)).status).toBe("running");
+ expect(
+ (await world.steps.list({ runId: successor.runId })).data.some((step) =>
+ step.stepName.endsWith("//signalSessionAnchorStep"),
+ ),
+ ).toBe(false);
+ if (sessionTimeoutMs !== false && successorTimer !== undefined) {
+ const nextTimer = await readSessionTimer(nextOwner.runId);
+ const owner = await world.runs.get(nextOwner.runId);
+ expect(nextTimer.deadline.getTime()).toBeGreaterThan(
+ successorTimer.deadline.getTime(),
+ );
+ expect(nextTimer.deadline.getTime()).toBeGreaterThanOrEqual(
+ (owner.startedAt ?? owner.createdAt).getTime() +
+ (sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS),
+ );
+ await vi.waitFor(async () =>
+ expect((await world.runs.get(successorTimer.runId)).status).toBe("cancelled"),
+ );
+ } else {
+ for (const runId of [anchor.runId, successor.runId, nextOwner.runId]) {
+ const steps = await world.steps.list({ runId });
+ expect(
+ steps.data.some((step) => step.stepName.endsWith("//startSessionTimeoutStep")),
+ ).toBe(false);
+ }
+ }
+
+ await waitForParkedTurnStep(nextOwner.runId);
+ await workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_d", "fifth message", "delivery-e"),
+ sessionId: anchor.runId,
+ });
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ const finalOwner = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ );
+ expect(finalOwner.runId).not.toBe(nextOwner.runId);
+ await vi.waitFor(async () =>
+ expect((await world.runs.get(nextOwner.runId)).status).toBe("completed"),
+ );
+ expect((await world.runs.get(anchor.runId)).status).toBe("running");
+ expect(
+ (await world.steps.list({ runId: nextOwner.runId })).data.some((step) =>
+ step.stepName.endsWith("//signalSessionAnchorStep"),
+ ),
+ ).toBe(false);
+
+ // Reset ends the session on the final owner; the anchor closes the stream once.
+ await workflowRuntime.dispatchSession({
+ command: { kind: "reset", reason: "handoff test" },
+ sessionId: anchor.runId,
+ });
+ await expect(anchor.returnValue).resolves.toEqual({ output: "" });
+ completed = true;
+ expect(await listCallerStepNames(anchor.runId)).toEqual([]);
+ } finally {
+ stream.dispose();
+ if (!completed) await anchor.cancel();
+ }
+ });
+ },
+ );
+ });
+});
+
+async function readSessionTimer(ownerRunId: string): Promise<{ runId: string; deadline: Date }> {
+ const world = await getWorld();
+ let timer: { runId: string; deadline: Date } | undefined;
+ await vi.waitFor(async () => {
+ const steps = await world.steps.list({ runId: ownerRunId, resolveData: "all" });
+ const start = steps.data.find((step) => step.stepName.endsWith("//startSessionTimeoutStep"));
+ expect(start?.output).toBeDefined();
+ const result = (await hydrateStepReturnValue(start!.output, ownerRunId, undefined)) as {
+ runId: string;
+ };
+ const run = await world.runs.get(result.runId);
+ const [input] = (await hydrateWorkflowArguments(run.input, run.runId, undefined)) as [
+ { deadline: Date },
+ ];
+ timer = { runId: run.runId, deadline: input.deadline };
+ });
+ return timer!;
+}
diff --git a/packages/eve/src/execution/session/entry-handoff.integration.test.ts b/packages/eve/src/execution/session/entry-handoff.integration.test.ts
new file mode 100644
index 000000000..b5a75f7cc
--- /dev/null
+++ b/packages/eve/src/execution/session/entry-handoff.integration.test.ts
@@ -0,0 +1,446 @@
+import type { HandoffWorkflowEntryInput } from "#execution/session/entry-input.js";
+import type { RunCreatedEventRequest } from "@workflow/world";
+import { assert, describe, expect, it, vi } from "vitest";
+import { getWorld, resumeHook, start } from "#internal/workflow/runtime.js";
+import {
+ dehydrateWorkflowArguments,
+ hydrateStepReturnValue,
+ hydrateWorkflowArguments,
+} from "@workflow/core/serialization";
+import { captureTurnEvents } from "#internal/testing/events.js";
+import { createTestRuntime } from "#internal/testing/app-harness.js";
+import { waitForParkedTurnStep } from "#internal/testing/session-test-helpers.js";
+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import {
+ sessionCommandHookToken,
+ sessionInboxHookToken,
+} from "#execution/session-inbox/address.js";
+import { createWorkflowRuntime, waitForCommandHookOwner } from "#execution/workflow-runtime.js";
+import { buildSerializedContext, handoffFollowUp } from "#internal/testing/entry-test-helpers.js";
+
+describe("workflowEntry integration", () => {
+ describe("deployment handoff", () => {
+ it("recovers the original owner when target rejects nested state", async () => {
+ const runtime = await createTestRuntime({ agent: { name: "handoff-validation" } });
+ await runtime.run(async () => {
+ const anchor = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_a",
+ sessionTimeoutMs: false,
+ input: { message: "Alice opens a research session." },
+ serializedContext: buildSerializedContext({
+ acceptedDeploymentId: "dpl_a",
+ channelKind: "http",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(anchor);
+ const world = await getWorld();
+ const workflowRuntime = createWorkflowRuntime({
+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
+ });
+ const rewritten = new Map<string, Promise<unknown>>();
+ let candidateId: string | undefined;
+ // Model a value readable by the source but incompatible with the target.
+ // Only the candidate snapshot changes; the source retains its healthy state.
+ const incompatibleInput = (runId: string, encoded: unknown): Promise<unknown> => {
+ let pending = rewritten.get(runId);
+ if (pending === undefined) {
+ pending = (async () => {
+ const args = (await hydrateWorkflowArguments(encoded, runId, undefined)) as [
+ HandoffWorkflowEntryInput,
+ ];
+ expect(args[0].kind).toBe("handoff");
+ candidateId = runId;
+ const session = args[0].checkpoint.sessionState.snapshot.session;
+ Object.assign(session, {
+ state: {
+ ...session.state,
+ "eve.workflowTool": {
+ version: 3,
+ runs: [
+ {
+ callId: "task",
+ toolName: "research",
+ lifetime: "session" as const,
+ origin: { turnId: "turn", stepIndex: 0 },
+ address: { runId: "run", hookToken: 42 },
+ task: {
+ taskId: "task",
+ metadata: { kind: "tool", name: "research" },
+ outcome: {
+ status: "cancelled",
+ },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ },
+ ],
+ },
+ },
+ });
+
+ const operations: Promise<void>[] = [];
+ const result = await dehydrateWorkflowArguments(args, runId, undefined, operations);
+ await Promise.all(operations);
+ return result;
+ })();
+ rewritten.set(runId, pending);
+ }
+ return pending;
+ };
+ const createEvent = world.events.create.bind(world.events);
+ const created = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
+ const [runId] = args;
+ const event = args[1] as (typeof args)[1] | RunCreatedEventRequest;
+ if (event.eventType === "run_created" && event.eventData.deploymentId === "dpl_b") {
+ event.eventData.input = await incompatibleInput(runId, event.eventData.input);
+ }
+ return createEvent(...args);
+ });
+ const queue = world.queue.bind(world);
+ const queued = vi.spyOn(world, "queue").mockImplementation(async (...args) => {
+ const message = args[1] as {
+ runId?: string;
+ runInput?: { deploymentId?: string; input: unknown };
+ };
+ if (message.runId !== undefined && message.runInput?.deploymentId === "dpl_b") {
+ message.runInput.input = await incompatibleInput(message.runId, message.runInput.input);
+ }
+ return queue(...args);
+ });
+ try {
+ await stream.nextTurn();
+ await waitForParkedTurnStep(anchor.runId);
+ await workflowRuntime.dispatchSession({
+ command: handoffFollowUp(
+ "dpl_b",
+ "Bob requests the next research step.",
+ "validation-trigger",
+ ),
+ sessionId: anchor.runId,
+ });
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ assert(candidateId !== undefined);
+ expect(
+ (
+ await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ )
+ ).runId,
+ ).toBe(anchor.runId);
+ expect(
+ created.mock.calls.some(
+ ([runId, event]) => runId === candidateId && event.eventType === "hook_created",
+ ),
+ ).toBe(false);
+ const candidateHooks = await world.hooks.list({ runId: candidateId });
+ expect(candidateHooks.data).toEqual([]);
+ const turns = await vi.waitFor(
+ async () => {
+ const steps = await world.steps.list({ runId: anchor.runId, resolveData: "all" });
+ const turns = steps.data.filter((step) => step.stepName.endsWith("//turnStep"));
+ expect(turns).toHaveLength(2);
+ // The waiting event is streamed before the step's return value is persisted.
+ expect(turns.every((step) => step.output !== undefined)).toBe(true);
+ return turns;
+ },
+ { timeout: 5000 },
+ );
+ const histories = await Promise.all(
+ turns.map(async (step) => {
+ const output = await hydrateStepReturnValue(step.output, anchor.runId, undefined);
+ return output.sessionState.snapshot.session.history as Array<{
+ role: string;
+ content: unknown;
+ }>;
+ }),
+ );
+ const deliveries = histories.map((history) =>
+ history.filter(
+ (message) =>
+ message.role === "user" &&
+ JSON.stringify(message.content).includes("Bob requests the next research step."),
+ ),
+ );
+ expect(deliveries.map((messages) => messages.length).sort()).toEqual([0, 1]);
+ } finally {
+ created.mockRestore();
+ queued.mockRestore();
+ await workflowRuntime.dispatchSession({
+ command: { kind: "reset", reason: "validation test" },
+ sessionId: anchor.runId,
+ });
+ await anchor.returnValue;
+ stream.dispose();
+ }
+ });
+ });
+
+ it("retains a message accepted just before durable hook disposal", async () => {
+ const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff" } });
+
+ await runtime.run(async () => {
+ const anchor = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_a",
+ input: { message: "hello from a" },
+ serializedContext: buildSerializedContext({
+ acceptedDeploymentId: "dpl_a",
+ channelKind: "http",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(anchor);
+ const world = await getWorld();
+ const workflowRuntime = createWorkflowRuntime({
+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
+ });
+ let injected = false;
+ const createEvent = world.events.create.bind(world.events);
+ const spy = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
+ const [runId, event] = args;
+ if (
+ !injected &&
+ runId === anchor.runId &&
+ event.eventType === "hook_disposed" &&
+ event.eventData?.token === sessionInboxHookToken(sessionCommandHookToken(anchor.runId))
+ ) {
+ injected = true;
+ for (let index = 0; index < 3; index++) {
+ await resumeHook(sessionInboxHookToken(sessionCommandHookToken(anchor.runId)), {
+ kind: "send",
+ payload: { message: `Alice sends input ${index} during release.` },
+ turnPolicy: "queue",
+ });
+ }
+ }
+ return await createEvent(...args);
+ });
+ try {
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ await waitForParkedTurnStep(anchor.runId);
+
+ await expect(
+ workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "hello from b", "delivery-b"),
+ sessionId: anchor.runId,
+ }),
+ ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
+
+ const secondTurn = await stream.nextTurn();
+ expect(secondTurn.at(-1)?.type).toBe("session.waiting");
+ expect(
+ secondTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("hello from b") === true,
+ ),
+ ).toBe(true);
+
+ expect(injected).toBe(true);
+ spy.mockRestore();
+ const owner = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ );
+ expect(owner.runId).toBe(anchor.runId);
+ await workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "Bob sends a later sentinel.", "sentinel"),
+ sessionId: anchor.runId,
+ });
+ await stream.nextTurn();
+ let saved: string | undefined;
+ await vi.waitFor(
+ async () => {
+ const steps = await world.steps.list({
+ runId: owner.runId,
+ resolveData: "all",
+ pagination: { limit: 1000 },
+ });
+ for (const step of steps.data) {
+ if (!step.stepName.endsWith("//turnStep") || step.output === undefined) continue;
+ const result = await hydrateStepReturnValue(step.output, owner.runId, undefined);
+ const history = JSON.stringify(result.sessionState.snapshot.session.history);
+ if (history.includes("Bob sends a later sentinel.")) saved = history;
+ }
+ expect(saved).toBeDefined();
+ },
+ { timeout: 5000 },
+ );
+ for (let index = 0; index < 3; index++) {
+ expect(saved).toContain(`Alice sends input ${index} during release.`);
+ }
+ expect(saved!.indexOf("Alice sends input 0")).toBeLessThan(
+ saved!.indexOf("Alice sends input 1"),
+ );
+ expect(saved!.indexOf("Alice sends input 1")).toBeLessThan(
+ saved!.indexOf("Alice sends input 2"),
+ );
+ } finally {
+ spy.mockRestore();
+ stream.dispose();
+ if ((await anchor.status) === "running") await anchor.cancel();
+ }
+ });
+ });
+
+ it("hands off an alias-addressed session and keeps the alias resolving through the gap", async () => {
+ const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff-alias" } });
+ const continuationToken = "http:workflow-entry-handoff-alias";
+
+ await runtime.run(async () => {
+ const anchor = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_a",
+ input: { message: "hello from a" },
+ serializedContext: buildSerializedContext({
+ acceptedDeploymentId: "dpl_a",
+ channelKind: "http",
+ continuationToken,
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(anchor);
+ const world = await getWorld();
+ const workflowRuntime = createWorkflowRuntime({
+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
+ });
+ // A channel delivery lands on the alias while the old owner has released
+ // it and the successor has not yet claimed it. The handoff marker must
+ // make ingress wait for the successor instead of reporting the session
+ // gone (which would let the channel start a replacement session).
+ let gapDelivery: Promise<unknown> | undefined;
+ const createBatch = world.events.createBatch;
+ world.events.createBatch = undefined;
+ const createEvent = world.events.create.bind(world.events);
+ const spy = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
+ const [runId, event] = args;
+ const created = await createEvent(...args);
+ if (
+ gapDelivery === undefined &&
+ runId === anchor.runId &&
+ event.eventType === "hook_disposed" &&
+ event.eventData?.token === sessionInboxHookToken(continuationToken)
+ ) {
+ gapDelivery = workflowRuntime.dispatchContinuation({
+ command: handoffFollowUp("dpl_b", "Alice writes during the gap.", "delivery-gap"),
+ continuationToken,
+ });
+ }
+ return created;
+ });
+ try {
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ await waitForParkedTurnStep(anchor.runId);
+
+ await expect(
+ workflowRuntime.dispatchContinuation({
+ command: handoffFollowUp("dpl_b", "hello from b", "delivery-b"),
+ continuationToken,
+ }),
+ ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
+
+ const secondTurn = await stream.nextTurn();
+ expect(
+ secondTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("hello from b") === true,
+ ),
+ ).toBe(true);
+ spy.mockRestore();
+ expect(gapDelivery).toBeDefined();
+ await expect(gapDelivery).resolves.toMatchObject({
+ sessionId: anchor.runId,
+ status: "accepted",
+ });
+ const gapTurn = await stream.nextTurn();
+ expect(
+ gapTurn.some(
+ (event) =>
+ event.type === "message.completed" &&
+ event.data.message?.includes("Alice writes during the gap.") === true,
+ ),
+ ).toBe(true);
+
+ const successor = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
+ );
+ expect(successor.runId).not.toBe(anchor.runId);
+ await expect(
+ waitForCommandHookOwner(sessionInboxHookToken(continuationToken)),
+ ).resolves.toMatchObject({ runId: successor.runId });
+ // No marker outlives the handoff.
+ const markers = (await world.hooks.list({ runId: anchor.runId })).data.filter((hook) =>
+ hook.token.startsWith("eve:inbox:handoff:"),
+ );
+ expect(markers).toEqual([]);
+ // Only one session exists for this alias.
+ await expect(workflowRuntime.resolveContinuation(continuationToken)).resolves.toEqual({
+ sessionId: anchor.runId,
+ });
+ } finally {
+ spy.mockRestore();
+ world.events.createBatch = createBatch;
+ stream.dispose();
+ await anchor.cancel();
+ }
+ });
+ });
+
+ it("keeps the session on the current owner when it is not idle", async () => {
+ const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff-busy" } });
+
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_a",
+ input: { message: "hello from a" },
+ serializedContext: buildSerializedContext({
+ acceptedDeploymentId: "dpl_a",
+ channelKind: "http",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+ const workflowRuntime = createWorkflowRuntime({
+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
+ });
+ try {
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+
+ // Two deliveries accepted back to back: the second is pending when the
+ // first is evaluated, so neither may trigger a handoff.
+ await Promise.all([
+ workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "first burst", "delivery-1"),
+ sessionId: run.runId,
+ }),
+ workflowRuntime.dispatchSession({
+ command: handoffFollowUp("dpl_b", "second burst", "delivery-2"),
+ sessionId: run.runId,
+ }),
+ ]);
+ const turn = await stream.nextTurn();
+ expect(turn.at(-1)?.type).toBe("session.waiting");
+ expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
+ const owner = await waitForCommandHookOwner(
+ sessionInboxHookToken(sessionCommandHookToken(run.runId)),
+ );
+ expect(owner.runId).toBe(run.runId);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ });
+ });
+});
diff --git a/packages/eve/src/execution/session/entry.integration.test.ts b/packages/eve/src/execution/session/entry.integration.test.ts
index f79735832..412636e24 100644
--- a/packages/eve/src/execution/session/entry.integration.test.ts
+++ b/packages/eve/src/execution/session/entry.integration.test.ts
@@ -1,225 +1,41 @@
-import type { HandoffWorkflowEntryInput } from "./entry-input.js";
-import type { RunCreatedEventRequest } from "@workflow/world";
-import { DEFAULT_SESSION_TIMEOUT_MS } from "#execution/session/timeout.js";
-import { assert, afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { getWorld, resumeHook, start } from "#internal/workflow/runtime.js";
-import {
- dehydrateWorkflowArguments,
- hydrateWorkflowArguments,
- hydrateStepReturnValue,
-} from "@workflow/core/serialization";
-
+import { hydrateWorkflowArguments } from "@workflow/core/serialization";
import { createChannelAddress } from "#channel/channel-address.js";
import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
import { createTestRuntime } from "#internal/testing/app-harness.js";
import { waitForHook } from "#internal/testing/workflow-test-helpers.js";
-import { waitForParkedTurnStep } from "#internal/testing/session-test-helpers.js";
import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
import { workflowEntry } from "#execution/session/entry.js";
-import { sessionInboxHookToken } from "#execution/session-inbox/address.js";
-import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
+import {
+ sessionCommandHookToken,
+ sessionInboxHookToken,
+} from "#execution/session-inbox/address.js";
import {
buildSessionAttributes,
buildSubagentRootAttributes,
} from "#execution/eve-workflow-attributes.js";
-import { createToolExecuteWithAuth } from "#execution/tool-auth.js";
-import { createWorkflowRuntime, waitForCommandHookOwner } from "#execution/workflow-runtime.js";
+import { createWorkflowRuntime } from "#execution/workflow-runtime.js";
import { normalizeEveAttributes } from "#runtime/attributes/normalize.js";
-import { ROOT_COMPILED_AGENT_NODE_ID } from "#compiler/manifest.js";
-import { ConnectionAuthorizationRequiredError } from "#connections/errors.js";
import { defineHook } from "#public/definitions/hook.js";
import type { MessageStreamEvent } from "#protocol/message.js";
import { isEventId } from "#protocol/event-id.js";
import { always } from "#tools/approval/policies.js";
-import { defineTool, type ToolContext } from "#tools/definition.js";
-import type {
- AuthorizationDefinition,
- ConnectionPrincipal,
- TokenResult,
-} from "#shared/connection-types.js";
-import type { ResolvedToolDefinition } from "#runtime/types.js";
-import { toInputSchema } from "#tools/schema.js";
-import { ConversationContextKey } from "#shared/conversation-context.js";
+import { defineTool } from "#tools/definition.js";
import { SessionTitleKey } from "#context/keys.js";
-
-function buildSerializedContext(overrides: {
- acceptedDeploymentId?: string;
- audience?: "public" | "private" | "unknown";
- auth?: Record<string, unknown>;
- channelKind: string;
- channelState?: Record<string, unknown>;
- continuationToken?: string;
- mode: string;
- parent?: {
- readonly callId: string;
- readonly rootSessionId: string;
- readonly sessionId: string;
- readonly turn: {
- readonly id: string;
- readonly sequence: number;
- };
- };
-}): Record<string, unknown> {
- const channel: { kind: unknown; state: unknown } = {
- kind: overrides.channelKind,
- state: overrides.channelState ?? {},
- };
- const context: Record<string, unknown> = {
- "eve.auth": overrides.auth ?? null,
- "eve.bundle": { source: createBundledRuntimeCompiledArtifactsSource() },
- "eve.channel": channel,
- "eve.mode": overrides.mode,
- };
- if (overrides.audience !== undefined) {
- context[ConversationContextKey.name] = {
- audience: overrides.audience,
- channel: { kind: overrides.channelKind },
- environment: "production",
- mode: overrides.mode,
- principalType: "anonymous",
- };
- }
- if (overrides.acceptedDeploymentId !== undefined) {
- context["eve.channelDelivery"] = {
- acceptedDeploymentId: overrides.acceptedDeploymentId,
- channelKind: overrides.channelKind,
- channelName: "test",
- deliveryId: "delivery-initial",
- };
- }
- if (overrides.continuationToken !== undefined) {
- context["eve.continuationToken"] = overrides.continuationToken;
- }
- if (overrides.parent !== undefined) {
- context["eve.parentSession"] = overrides.parent;
- }
- return context;
-}
+import {
+ buildSerializedContext,
+ captureEvents,
+ expectHookClaims,
+ expectSingleTurn,
+ listCallerStepNames,
+ withTimeout,
+} from "#internal/testing/entry-test-helpers.js";
afterEach(() => {
vi.unstubAllEnvs();
});
-interface WeatherAuthRuntime {
- completeCalls(): number;
- completedPrincipals(): readonly ConnectionPrincipal[];
- runtime: Awaited<ReturnType<typeof createTestRuntime>>;
-}
-
-/**
- * A get_weather tool behind an interactive authorization: getToken always
- * requires sign-in, and completeAuthorization mints `weather-token` from the
- * `oauth-code` callback. Shared by the callback-resume and
- * challenge-stays-open owner tests.
- */
-async function createWeatherAuthRuntime(agentName: string): Promise<WeatherAuthRuntime> {
- let completeCalls = 0;
- const completedPrincipals: ConnectionPrincipal[] = [];
- const weatherAuth: AuthorizationDefinition<{ nonce: string }> = {
- principalType: "user",
- async getToken(): Promise<TokenResult> {
- throw new ConnectionAuthorizationRequiredError("weather");
- },
- async startAuthorization({ callbackUrl }) {
- return {
- challenge: {
- displayName: "Weather",
- instructions: "Sign in to continue.",
- url: `https://idp.example/authorize?callback=${encodeURIComponent(callbackUrl)}`,
- },
- resume: { nonce: "weather-nonce" },
- };
- },
- async completeAuthorization({ callback, principal, resume }): Promise<TokenResult> {
- completeCalls += 1;
- completedPrincipals.push(principal);
- expect(callback.params.code).toBe("oauth-code");
- expect(resume).toEqual({ nonce: "weather-nonce" });
- return { token: "weather-token" };
- },
- };
- const getWeatherTool: ResolvedToolDefinition = {
- description: "Get the current weather for a city.",
- execute: createToolExecuteWithAuth({
- scope: "get_weather",
- async execute(rawInput, rawCtx) {
- const ctx = rawCtx as ToolContext;
- const token = await ctx.getToken(weatherAuth, {
- authKey: "weather",
- displayName: "Weather",
- });
- const city =
- typeof rawInput === "object" &&
- rawInput !== null &&
- typeof (rawInput as { city?: unknown }).city === "string"
- ? (rawInput as { city: string }).city
- : "Lisbon";
- return {
- city,
- condition: "Sunny",
- summary: `authorized with ${token.token}`,
- temperatureF: 72,
- };
- },
- }),
- inputSchema: toInputSchema({
- additionalProperties: false,
- properties: {
- city: { type: "string" },
- },
- required: ["city"],
- type: "object",
- }),
- logicalPath: "tools/get_weather.ts",
- name: "get_weather",
- owner: { kind: "application" },
- sourceId: "tools/get_weather.ts",
- sourceKind: "module",
- };
- const runtime = await createTestRuntime({
- agent: { name: agentName },
- tools: [getWeatherTool],
- });
- const manifestTool = runtime.manifest.tools.find((tool) => tool.name === getWeatherTool.name);
- if (manifestTool === undefined) {
- throw new Error("Expected get_weather to be present in the test manifest.");
- }
- runtime.moduleMap.nodes[ROOT_COMPILED_AGENT_NODE_ID]!.modules[manifestTool.sourceId] = {
- default: {
- execute: getWeatherTool.execute,
- },
- };
- return {
- completeCalls: () => completeCalls,
- completedPrincipals: () => completedPrincipals,
- runtime,
- };
-}
-
-function authorizationAttemptId(events: readonly MessageStreamEvent[]): string {
- const required = filterEventsByType(events, "authorization.required")[0];
- const webhookUrl = required?.data.webhookUrl;
- if (webhookUrl === undefined) throw new Error("Missing authorization callback URL.");
- const segments = new URL(webhookUrl).pathname.split("/");
- const callbackIndex = segments.lastIndexOf("callback");
- const attemptId = segments[callbackIndex + 1];
- if (callbackIndex === -1 || attemptId === undefined) {
- throw new Error("Authorization callback URL is missing its attempt ID.");
- }
- return decodeURIComponent(attemptId);
-}
-
-function expectSingleTurn(events: readonly MessageStreamEvent[], turnId: string): void {
- expect(filterEventsByType(events, "turn.started")).toHaveLength(1);
- const eventTurnIds = events.flatMap((event) => {
- if (!("data" in event) || typeof event.data !== "object" || event.data === null) return [];
- if (!("turnId" in event.data) || typeof event.data.turnId !== "string") return [];
- return [event.data.turnId];
- });
- expect(eventTurnIds.length).toBeGreaterThan(0);
- expect(new Set(eventTurnIds)).toEqual(new Set([turnId]));
-}
-
describe("workflowEntry integration", () => {
it("parks before initialization and initializes with the first message identity and title", async () => {
let initializedSessions = 0;
@@ -359,530 +175,6 @@ describe("workflowEntry integration", () => {
}
});
- it("resumes normal follow-ups after an interactive authorization callback", async () => {
- const { completeCalls, runtime } = await createWeatherAuthRuntime(
- "workflow-entry-auth-followup",
- );
- const continuationToken = "http:workflow-entry-auth-followup";
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Use the get_weather tool to check the weather in Lisbon." },
- serializedContext: buildSerializedContext({
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- channelKind: "http",
- continuationToken,
- mode: "conversation",
- }),
- },
- ]);
-
- const stream = captureEvents(run);
-
- try {
- const firstTurn = await stream.nextUntil(
- "initial auth-required event",
- (event) => event.type === "authorization.required",
- );
- const required = filterEventsByType(firstTurn, "authorization.required");
-
- expect(firstTurn.at(-1)?.type).toBe("authorization.required");
- expect(required).toHaveLength(1);
- expect(required[0]?.data).toMatchObject({
- name: "weather",
- authorization: { displayName: "Weather" },
- });
-
- // The authorization park closes its turn boundary so stream
- // consumers do not hang on the parked turn.
- const parkBoundary = await stream.nextUntil(
- "authorization park boundary",
- (event) => event.type === "session.waiting",
- );
- expect(parkBoundary.at(-1)?.type).toBe("session.waiting");
- await expectHookClaims(run.runId, [sessionCommandHookToken(run.runId), continuationToken]);
-
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [
- {
- authorizationCallback: {
- attemptId: authorizationAttemptId(firstTurn),
- callback: {
- method: "GET",
- params: { code: "oauth-code" },
- },
- connectionName: "weather",
- },
- },
- ],
- });
-
- const authorizedTurn = await stream.nextUntil(
- "authorization callback turn",
- (event) => event.type === "session.waiting",
- );
- const completed = filterEventsByType(authorizedTurn, "authorization.completed");
-
- expect(completeCalls()).toBe(1);
- expectSingleTurn(authorizedTurn, "turn_1");
- expect(authorizedTurn.at(-1)?.type).toBe("session.waiting");
- expect(completed).toHaveLength(1);
- expect(completed[0]?.data).toMatchObject({
- name: "weather",
- outcome: "authorized",
- });
- expect(
- authorizedTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("Used local weather tool for Lisbon") === true,
- ),
- ).toBe(true);
-
- await waitForHook(
- { runId: run.runId },
- {
- token: sessionInboxHookToken(continuationToken),
- },
- );
- await resumeHook(sessionInboxHookToken(continuationToken), {
- kind: "send",
- payload: { message: "follow up after auth" },
- });
-
- const followupTurn = await stream.nextUntil(
- "post-auth follow-up turn",
- (event) => event.type === "session.waiting",
- );
-
- expect(followupTurn.at(-1)?.type).toBe("session.waiting");
- expect(
- followupTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("follow up after auth") === true,
- ),
- ).toBe(true);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
-
- it("runs ordinary deliveries while an authorization challenge stays open", async () => {
- const { completeCalls, completedPrincipals, runtime } = await createWeatherAuthRuntime(
- "workflow-entry-auth-open",
- );
- const continuationToken = "http:workflow-entry-auth-open";
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Use the get_weather tool to check the weather in Lisbon." },
- serializedContext: buildSerializedContext({
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- channelKind: "http",
- continuationToken,
- mode: "conversation",
- }),
- },
- ]);
-
- const stream = captureEvents(run);
-
- try {
- const firstTurn = await stream.nextUntil(
- "initial auth-required event",
- (event) => event.type === "authorization.required",
- );
- // Consume the park's own turn boundary so the next wait delimits
- // the intervening message turn.
- await stream.nextUntil(
- "authorization park boundary",
- (event) => event.type === "session.waiting",
- );
-
- // An ordinary message while the challenge is open runs as a normal
- // turn instead of queueing behind the callback.
- await waitForHook(
- { runId: run.runId },
- { token: sessionInboxHookToken(continuationToken) },
- );
- await resumeHook(sessionInboxHookToken(continuationToken), {
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-2",
- principalType: "user",
- },
- kind: "send",
- payload: { message: "Quick status note while I sign in." },
- });
-
- const interveningTurn = await stream.nextUntil(
- "intervening message turn",
- (event) => event.type === "session.waiting",
- );
- expect(
- interveningTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("Quick status note while I sign in.") === true,
- ),
- ).toBe(true);
- expect(filterEventsByType(interveningTurn, "authorization.completed")).toHaveLength(0);
- expect(completeCalls()).toBe(0);
-
- // The callback still lands on the retained read and closes the
- // challenge exactly once.
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [
- {
- authorizationCallback: {
- attemptId: authorizationAttemptId(firstTurn),
- callback: {
- method: "GET",
- params: { code: "oauth-code" },
- },
- connectionName: "weather",
- },
- },
- ],
- });
-
- const callbackTurn = await stream.nextUntil(
- "authorization callback turn",
- (event) => event.type === "session.waiting",
- );
- const completed = filterEventsByType(callbackTurn, "authorization.completed");
- expectSingleTurn(callbackTurn, "turn_2");
- expect(completed).toHaveLength(1);
- expect(completed[0]?.data).toMatchObject({
- name: "weather",
- outcome: "authorized",
- });
-
- // The granted authorization serves the next explicit tool request.
- // (No waitForHook here: it only reports never-received hooks, and
- // the continuation hook already received the intervening message.)
- await resumeHook(sessionInboxHookToken(continuationToken), {
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- kind: "send",
- payload: { message: "Use the get_weather tool to check the weather in Lisbon." },
- });
-
- const toolTurn = await stream.nextUntil(
- "post-authorization tool turn",
- (event) => event.type === "session.waiting",
- );
- expect(completeCalls()).toBe(1);
- expect(completedPrincipals()).toEqual([
- expect.objectContaining({ id: "user-1", type: "user" }),
- ]);
- expect(
- toolTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("Used local weather tool for Lisbon") === true &&
- event.data.message.includes("authorized with weather-token"),
- ),
- ).toBe(true);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
-
- it("defers ordinary deliveries while a task waits for authorization", async () => {
- const { completeCalls, runtime } = await createWeatherAuthRuntime(
- "workflow-entry-task-auth-open",
- );
- const continuationToken = "http:workflow-entry-task-auth-open";
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Use the get_weather tool to check the weather in Lisbon." },
- serializedContext: buildSerializedContext({
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- channelKind: "http",
- continuationToken,
- mode: "task",
- }),
- },
- ]);
- const stream = captureEvents(run);
-
- try {
- const firstTurn = await stream.nextUntil(
- "initial task auth-required event",
- (event) => event.type === "authorization.required",
- );
-
- await waitForHook(
- { runId: run.runId },
- { token: sessionInboxHookToken(continuationToken) },
- );
- await resumeHook(sessionInboxHookToken(continuationToken), {
- kind: "send",
- payload: { message: "This must not become a second task turn." },
- });
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [
- {
- authorizationCallback: {
- attemptId: authorizationAttemptId(firstTurn),
- callback: { method: "GET", params: { code: "oauth-code" } },
- connectionName: "weather",
- },
- },
- ],
- });
-
- const completion = await stream.nextUntil(
- "authorized task completion",
- (event) => event.type === "session.completed",
- );
- const allEvents = [...firstTurn, ...completion];
- expect(filterEventsByType(allEvents, "turn.started")).toHaveLength(1);
- expect(filterEventsByType(allEvents, "message.received")).toHaveLength(1);
- expect(filterEventsByType(allEvents, "authorization.completed")).toHaveLength(1);
- expect(filterEventsByType(allEvents, "session.waiting")).toHaveLength(0);
- expect(completeCalls()).toBe(1);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
-
- it("ignores stale and duplicate callbacks after a challenge is replaced", async () => {
- const { completeCalls, runtime } = await createWeatherAuthRuntime(
- "workflow-entry-auth-replaced",
- );
- const continuationToken = "http:workflow-entry-auth-replaced";
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Use the get_weather tool to check the weather in Lisbon." },
- serializedContext: buildSerializedContext({
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- channelKind: "http",
- continuationToken,
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureEvents(run);
-
- try {
- const firstAttempt = await stream.nextUntil(
- "first auth-required event",
- (event) => event.type === "authorization.required",
- );
- await stream.nextUntil(
- "first authorization park",
- (event) => event.type === "session.waiting",
- );
-
- await waitForHook(
- { runId: run.runId },
- { token: sessionInboxHookToken(continuationToken) },
- );
- await resumeHook(sessionInboxHookToken(continuationToken), {
- kind: "send",
- payload: { message: "Use the get_weather tool to check the weather in Lisbon." },
- });
- const replacementAttempt = await stream.nextUntil(
- "replacement auth-required event",
- (event) => event.type === "authorization.required",
- );
- expect(filterEventsByType(replacementAttempt, "authorization.completed")).toMatchObject([
- { data: { name: "weather", outcome: "failed" } },
- ]);
- await stream.nextUntil(
- "replacement authorization park",
- (event) => event.type === "session.waiting",
- );
-
- const stalePayload = {
- authorizationCallback: {
- attemptId: authorizationAttemptId(firstAttempt),
- callback: { method: "GET", params: { code: "stale-oauth-code" } },
- connectionName: "weather",
- },
- };
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [stalePayload],
- });
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [stalePayload],
- });
-
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [
- {
- authorizationCallback: {
- attemptId: authorizationAttemptId(replacementAttempt),
- callback: { method: "GET", params: { code: "oauth-code" } },
- connectionName: "weather",
- },
- },
- ],
- });
-
- const callbackTurn = await stream.nextUntil(
- "replacement authorization callback turn",
- (event) => event.type === "session.waiting",
- );
- expect(filterEventsByType(callbackTurn, "authorization.completed")).toHaveLength(1);
- expect(completeCalls()).toBe(1);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
-
- it("completes the challenge after a no-op cancel consumed the parked wait", async () => {
- const { completeCalls, runtime } = await createWeatherAuthRuntime("workflow-entry-auth-cancel");
- const continuationToken = "http:workflow-entry-auth-cancel";
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Use the get_weather tool to check the weather in Lisbon." },
- serializedContext: buildSerializedContext({
- auth: {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- channelKind: "http",
- continuationToken,
- mode: "conversation",
- }),
- },
- ]);
-
- const stream = captureEvents(run);
-
- try {
- const firstTurn = await stream.nextUntil(
- "initial auth-required event",
- (event) => event.type === "authorization.required",
- );
- await stream.nextUntil(
- "authorization park boundary",
- (event) => event.type === "session.waiting",
- );
-
- await waitForParkedTurnStep(run.runId);
-
- // A cancel with no active turn is consumed by the parked wait
- // without producing a parent turn. The callback must still surface
- // in the continued wait instead of stalling until unrelated
- // session activity re-parks the owner.
- await waitForHook(
- { runId: run.runId },
- { token: sessionInboxHookToken(continuationToken) },
- );
- await resumeHook(sessionInboxHookToken(continuationToken), { kind: "cancel" });
- // Let the owner consume the no-op cancel and re-enter the parked
- // wait before the callback fires; back-to-back resumes could
- // otherwise surface the callback in the first wait iteration and
- // mask a wait that ignores callbacks after a consumed cancel.
- await new Promise((resolve) => setTimeout(resolve, 250));
-
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(run.runId)), {
- kind: "authorization-callback",
- payloads: [
- {
- authorizationCallback: {
- attemptId: authorizationAttemptId(firstTurn),
- callback: {
- method: "GET",
- params: { code: "oauth-code" },
- },
- connectionName: "weather",
- },
- },
- ],
- });
-
- const callbackTurn = await stream.nextUntil(
- "authorization callback turn",
- (event) => event.type === "session.waiting",
- );
- const completed = filterEventsByType(callbackTurn, "authorization.completed");
-
- expect(completeCalls()).toBe(1);
- expect(completed).toHaveLength(1);
- expect(completed[0]?.data).toMatchObject({
- name: "weather",
- outcome: "authorized",
- });
- expect(filterEventsByType(callbackTurn, "turn.cancelled")).toHaveLength(0);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
-
it("parks in conversation mode and resumes via runtime delivery", async () => {
vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
const runtime = await createTestRuntime({ agent: { name: "workflow-entry-conversation" } });
@@ -1314,640 +606,6 @@ describe("workflowEntry integration", () => {
});
}, 60_000);
- describe("deployment handoff", () => {
- const followUp = (acceptedDeploymentId: string, message: string, deliveryId: string) => ({
- turnPolicy: "queue" as const,
- auth: null,
- delivery: {
- acceptedDeploymentId,
- channelKind: "http",
- channelName: "test",
- deliveryId,
- },
- kind: "send" as const,
- payload: { message },
- });
-
- it.each([undefined, 60_000, false] as const)(
- "renews the configured lifetime across handoffs and keeps the original stream (%s)",
- async (sessionTimeoutMs) => {
- const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff" } });
-
- await runtime.run(async () => {
- const anchor = await start(workflowEntry, [
- {
- kind: "initial",
- sessionTimeoutMs,
- ownerDeploymentId: "dpl_a",
- input: { message: "hello from a" },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_a",
- channelKind: "http",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(anchor);
- const world = await getWorld();
- const workflowRuntime = createWorkflowRuntime({
- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- });
- let completed = false;
- try {
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- await waitForParkedTurnStep(anchor.runId);
-
- const originalTimer =
- sessionTimeoutMs === false ? undefined : await readSessionTimer(anchor.runId);
-
- await expect(
- workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "hello from b", "delivery-b"),
- sessionId: anchor.runId,
- }),
- ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
-
- const secondTurn = await stream.nextTurn();
- expect(secondTurn.at(-1)?.type).toBe("session.waiting");
- expect(
- secondTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("hello from b") === true,
- ),
- ).toBe(true);
-
- // The stable inbox now belongs to a successor run; the original run
- // holds only its anchor (plus the SDK's abort-signal hook from its
- // own earlier turn).
- const successor = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- );
- expect(successor.runId).not.toBe(anchor.runId);
- const successorTimer =
- sessionTimeoutMs === false ? undefined : await readSessionTimer(successor.runId);
- if (
- sessionTimeoutMs !== false &&
- originalTimer !== undefined &&
- successorTimer !== undefined
- ) {
- expect(successorTimer.deadline.getTime()).toBeGreaterThan(
- originalTimer.deadline.getTime(),
- );
- const owner = await world.runs.get(successor.runId);
- expect(successorTimer.deadline.getTime()).toBeGreaterThanOrEqual(
- (owner.startedAt ?? owner.createdAt).getTime() +
- (sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS),
- );
- await vi.waitFor(async () =>
- expect((await world.runs.get(originalTimer.runId)).status).toBe("cancelled"),
- );
- }
- // A timer from the old owner can win its race with cancellation.
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(anchor.runId)), {
- kind: "session-timeout",
- ownerRunId: anchor.runId,
- });
- // The successor can finish its turn before the old owner resumes
- // from activation and disposes its temporary handoff hook.
- await vi.waitFor(async () => {
- const anchorHooks = await world.hooks.list({ runId: anchor.runId });
- expect(
- anchorHooks.data
- .map((hook) => hook.token)
- .filter((token) => !token.startsWith("abrt_")),
- ).toEqual([`${anchor.runId}:anchor`]);
- });
-
- // A third delivery through the stable session id reaches the successor
- // and still streams on the original run.
- await expect(
- workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "third message", "delivery-c"),
- sessionId: anchor.runId,
- }),
- ).resolves.toMatchObject({ status: "accepted" });
- const thirdTurn = await stream.nextTurn();
- expect(
- thirdTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("third message") === true,
- ),
- ).toBe(true);
-
- if (successorTimer !== undefined) {
- expect(await readSessionTimer(successor.runId)).toEqual(successorTimer);
- }
- await waitForParkedTurnStep(successor.runId, 2);
- await workflowRuntime.dispatchSession({
- command: followUp("dpl_c", "fourth message", "delivery-d"),
- sessionId: anchor.runId,
- });
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- const nextOwner = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- );
- expect(nextOwner.runId).not.toBe(successor.runId);
- await vi.waitFor(async () =>
- expect((await world.runs.get(successor.runId)).status).toBe("completed"),
- );
- expect((await world.runs.get(anchor.runId)).status).toBe("running");
- expect(
- (await world.steps.list({ runId: successor.runId })).data.some((step) =>
- step.stepName.endsWith("//signalSessionAnchorStep"),
- ),
- ).toBe(false);
- if (sessionTimeoutMs !== false && successorTimer !== undefined) {
- const nextTimer = await readSessionTimer(nextOwner.runId);
- const owner = await world.runs.get(nextOwner.runId);
- expect(nextTimer.deadline.getTime()).toBeGreaterThan(
- successorTimer.deadline.getTime(),
- );
- expect(nextTimer.deadline.getTime()).toBeGreaterThanOrEqual(
- (owner.startedAt ?? owner.createdAt).getTime() +
- (sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS),
- );
- await vi.waitFor(async () =>
- expect((await world.runs.get(successorTimer.runId)).status).toBe("cancelled"),
- );
- } else {
- for (const runId of [anchor.runId, successor.runId, nextOwner.runId]) {
- const steps = await world.steps.list({ runId });
- expect(
- steps.data.some((step) => step.stepName.endsWith("//startSessionTimeoutStep")),
- ).toBe(false);
- }
- }
-
- await waitForParkedTurnStep(nextOwner.runId);
- await workflowRuntime.dispatchSession({
- command: followUp("dpl_d", "fifth message", "delivery-e"),
- sessionId: anchor.runId,
- });
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- const finalOwner = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- );
- expect(finalOwner.runId).not.toBe(nextOwner.runId);
- await vi.waitFor(async () =>
- expect((await world.runs.get(nextOwner.runId)).status).toBe("completed"),
- );
- expect((await world.runs.get(anchor.runId)).status).toBe("running");
- expect(
- (await world.steps.list({ runId: nextOwner.runId })).data.some((step) =>
- step.stepName.endsWith("//signalSessionAnchorStep"),
- ),
- ).toBe(false);
-
- // Reset ends the session on the final owner; the anchor closes the stream once.
- await workflowRuntime.dispatchSession({
- command: { kind: "reset", reason: "handoff test" },
- sessionId: anchor.runId,
- });
- await expect(anchor.returnValue).resolves.toEqual({ output: "" });
- completed = true;
- expect(await listCallerStepNames(anchor.runId)).toEqual([]);
- } finally {
- stream.dispose();
- if (!completed) await anchor.cancel();
- }
- });
- },
- );
-
- it.each(["nested state", "checkpoint version"] as const)(
- "recovers the original owner when target rejects %s",
- async (incompatibility) => {
- const runtime = await createTestRuntime({ agent: { name: "handoff-validation" } });
- await runtime.run(async () => {
- const anchor = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_a",
- sessionTimeoutMs: false,
- input: { message: "Alice opens a research session." },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_a",
- channelKind: "http",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(anchor);
- const world = await getWorld();
- const workflowRuntime = createWorkflowRuntime({
- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- });
- const rewritten = new Map<string, Promise<unknown>>();
- let candidateId: string | undefined;
- // Model a value readable by the source but incompatible with the target.
- // Only the candidate snapshot changes; the source retains its healthy state.
- const incompatibleInput = (runId: string, encoded: unknown): Promise<unknown> => {
- let pending = rewritten.get(runId);
- if (pending === undefined) {
- pending = (async () => {
- const args = (await hydrateWorkflowArguments(encoded, runId, undefined)) as [
- HandoffWorkflowEntryInput,
- ];
- expect(args[0].kind).toBe("handoff");
- candidateId = runId;
- const session = args[0].checkpoint.sessionState.snapshot.session;
- if (incompatibility === "checkpoint version") {
- Object.assign(args[0].checkpoint, { version: 4 });
- } else {
- Object.assign(session, {
- state: {
- ...session.state,
- "eve.workflowTool": {
- version: 3,
- runs: [
- {
- callId: "task",
- toolName: "research",
- lifetime: "session" as const,
- origin: { turnId: "turn", stepIndex: 0 },
- address: { runId: "run", hookToken: 42 },
- task: {
- taskId: "task",
- metadata: { kind: "tool", name: "research" },
- outcome: {
- status: "cancelled",
- },
- dispatchContext: { auth: { current: null, initiator: null } },
- },
- },
- ],
- },
- },
- });
- }
- const operations: Promise<void>[] = [];
- const result = await dehydrateWorkflowArguments(args, runId, undefined, operations);
- await Promise.all(operations);
- return result;
- })();
- rewritten.set(runId, pending);
- }
- return pending;
- };
- const createEvent = world.events.create.bind(world.events);
- const created = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
- const [runId] = args;
- const event = args[1] as (typeof args)[1] | RunCreatedEventRequest;
- if (event.eventType === "run_created" && event.eventData.deploymentId === "dpl_b") {
- event.eventData.input = await incompatibleInput(runId, event.eventData.input);
- }
- return createEvent(...args);
- });
- const queue = world.queue.bind(world);
- const queued = vi.spyOn(world, "queue").mockImplementation(async (...args) => {
- const message = args[1] as {
- runId?: string;
- runInput?: { deploymentId?: string; input: unknown };
- };
- if (message.runId !== undefined && message.runInput?.deploymentId === "dpl_b") {
- message.runInput.input = await incompatibleInput(
- message.runId,
- message.runInput.input,
- );
- }
- return queue(...args);
- });
- try {
- await stream.nextTurn();
- await waitForParkedTurnStep(anchor.runId);
- await workflowRuntime.dispatchSession({
- command: followUp(
- "dpl_b",
- "Bob requests the next research step.",
- "validation-trigger",
- ),
- sessionId: anchor.runId,
- });
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- assert(candidateId !== undefined);
- expect(
- (
- await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- )
- ).runId,
- ).toBe(anchor.runId);
- expect(
- created.mock.calls.some(
- ([runId, event]) => runId === candidateId && event.eventType === "hook_created",
- ),
- ).toBe(false);
- const candidateHooks = await world.hooks.list({ runId: candidateId });
- expect(candidateHooks.data).toEqual([]);
- const turns = await vi.waitFor(
- async () => {
- const steps = await world.steps.list({ runId: anchor.runId, resolveData: "all" });
- const turns = steps.data.filter((step) => step.stepName.endsWith("//turnStep"));
- expect(turns).toHaveLength(2);
- // The waiting event is streamed before the step's return value is persisted.
- expect(turns.every((step) => step.output !== undefined)).toBe(true);
- return turns;
- },
- { timeout: 5000 },
- );
- const histories = await Promise.all(
- turns.map(async (step) => {
- const output = await hydrateStepReturnValue(step.output, anchor.runId, undefined);
- return output.sessionState.snapshot.session.history as Array<{
- role: string;
- content: unknown;
- }>;
- }),
- );
- const deliveries = histories.map((history) =>
- history.filter(
- (message) =>
- message.role === "user" &&
- JSON.stringify(message.content).includes("Bob requests the next research step."),
- ),
- );
- expect(deliveries.map((messages) => messages.length).sort()).toEqual([0, 1]);
- } finally {
- created.mockRestore();
- queued.mockRestore();
- await workflowRuntime.dispatchSession({
- command: { kind: "reset", reason: "validation test" },
- sessionId: anchor.runId,
- });
- await anchor.returnValue;
- stream.dispose();
- }
- });
- },
- );
-
- it("retains a message accepted just before durable hook disposal", async () => {
- const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff" } });
-
- await runtime.run(async () => {
- const anchor = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_a",
- input: { message: "hello from a" },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_a",
- channelKind: "http",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(anchor);
- const world = await getWorld();
- const workflowRuntime = createWorkflowRuntime({
- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- });
- let injected = false;
- const createEvent = world.events.create.bind(world.events);
- const spy = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
- const [runId, event] = args;
- if (
- !injected &&
- runId === anchor.runId &&
- event.eventType === "hook_disposed" &&
- event.eventData?.token === sessionInboxHookToken(sessionCommandHookToken(anchor.runId))
- ) {
- injected = true;
- for (let index = 0; index < 3; index++) {
- await resumeHook(sessionInboxHookToken(sessionCommandHookToken(anchor.runId)), {
- kind: "send",
- payload: { message: `Alice sends input ${index} during release.` },
- turnPolicy: "queue",
- });
- }
- }
- return await createEvent(...args);
- });
- try {
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- await waitForParkedTurnStep(anchor.runId);
-
- await expect(
- workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "hello from b", "delivery-b"),
- sessionId: anchor.runId,
- }),
- ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
-
- const secondTurn = await stream.nextTurn();
- expect(secondTurn.at(-1)?.type).toBe("session.waiting");
- expect(
- secondTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("hello from b") === true,
- ),
- ).toBe(true);
-
- expect(injected).toBe(true);
- spy.mockRestore();
- const owner = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- );
- expect(owner.runId).toBe(anchor.runId);
- await workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "Bob sends a later sentinel.", "sentinel"),
- sessionId: anchor.runId,
- });
- await stream.nextTurn();
- let saved: string | undefined;
- await vi.waitFor(
- async () => {
- const steps = await world.steps.list({
- runId: owner.runId,
- resolveData: "all",
- pagination: { limit: 1000 },
- });
- for (const step of steps.data) {
- if (!step.stepName.endsWith("//turnStep") || step.output === undefined) continue;
- const result = await hydrateStepReturnValue(step.output, owner.runId, undefined);
- const history = JSON.stringify(result.sessionState.snapshot.session.history);
- if (history.includes("Bob sends a later sentinel.")) saved = history;
- }
- expect(saved).toBeDefined();
- },
- { timeout: 5000 },
- );
- for (let index = 0; index < 3; index++) {
- expect(saved).toContain(`Alice sends input ${index} during release.`);
- }
- expect(saved!.indexOf("Alice sends input 0")).toBeLessThan(
- saved!.indexOf("Alice sends input 1"),
- );
- expect(saved!.indexOf("Alice sends input 1")).toBeLessThan(
- saved!.indexOf("Alice sends input 2"),
- );
- } finally {
- spy.mockRestore();
- stream.dispose();
- if ((await anchor.status) === "running") await anchor.cancel();
- }
- });
- });
-
- it("hands off an alias-addressed session and keeps the alias resolving through the gap", async () => {
- const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff-alias" } });
- const continuationToken = "http:workflow-entry-handoff-alias";
-
- await runtime.run(async () => {
- const anchor = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_a",
- input: { message: "hello from a" },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_a",
- channelKind: "http",
- continuationToken,
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(anchor);
- const world = await getWorld();
- const workflowRuntime = createWorkflowRuntime({
- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- });
- // A channel delivery lands on the alias while the old owner has released
- // it and the successor has not yet claimed it. The handoff marker must
- // make ingress wait for the successor instead of reporting the session
- // gone (which would let the channel start a replacement session).
- let gapDelivery: Promise<unknown> | undefined;
- const createBatch = world.events.createBatch;
- world.events.createBatch = undefined;
- const createEvent = world.events.create.bind(world.events);
- const spy = vi.spyOn(world.events, "create").mockImplementation(async (...args) => {
- const [runId, event] = args;
- const created = await createEvent(...args);
- if (
- gapDelivery === undefined &&
- runId === anchor.runId &&
- event.eventType === "hook_disposed" &&
- event.eventData?.token === sessionInboxHookToken(continuationToken)
- ) {
- gapDelivery = workflowRuntime.dispatchContinuation({
- command: followUp("dpl_b", "Alice writes during the gap.", "delivery-gap"),
- continuationToken,
- });
- }
- return created;
- });
- try {
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- await waitForParkedTurnStep(anchor.runId);
-
- await expect(
- workflowRuntime.dispatchContinuation({
- command: followUp("dpl_b", "hello from b", "delivery-b"),
- continuationToken,
- }),
- ).resolves.toMatchObject({ sessionId: anchor.runId, status: "accepted" });
-
- const secondTurn = await stream.nextTurn();
- expect(
- secondTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("hello from b") === true,
- ),
- ).toBe(true);
- spy.mockRestore();
- expect(gapDelivery).toBeDefined();
- await expect(gapDelivery).resolves.toMatchObject({
- sessionId: anchor.runId,
- status: "accepted",
- });
- const gapTurn = await stream.nextTurn();
- expect(
- gapTurn.some(
- (event) =>
- event.type === "message.completed" &&
- event.data.message?.includes("Alice writes during the gap.") === true,
- ),
- ).toBe(true);
-
- const successor = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(anchor.runId)),
- );
- expect(successor.runId).not.toBe(anchor.runId);
- await expect(
- waitForCommandHookOwner(sessionInboxHookToken(continuationToken)),
- ).resolves.toMatchObject({ runId: successor.runId });
- // No marker outlives the handoff.
- const markers = (await world.hooks.list({ runId: anchor.runId })).data.filter((hook) =>
- hook.token.startsWith("eve:inbox:handoff:"),
- );
- expect(markers).toEqual([]);
- // Only one session exists for this alias.
- await expect(workflowRuntime.resolveContinuation(continuationToken)).resolves.toEqual({
- sessionId: anchor.runId,
- });
- } finally {
- spy.mockRestore();
- world.events.createBatch = createBatch;
- stream.dispose();
- await anchor.cancel();
- }
- });
- });
-
- it("keeps the session on the current owner when it is not idle", async () => {
- const runtime = await createTestRuntime({ agent: { name: "workflow-entry-handoff-busy" } });
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_a",
- input: { message: "hello from a" },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_a",
- channelKind: "http",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
- const workflowRuntime = createWorkflowRuntime({
- compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),
- });
- try {
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
-
- // Two deliveries accepted back to back: the second is pending when the
- // first is evaluated, so neither may trigger a handoff.
- await Promise.all([
- workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "first burst", "delivery-1"),
- sessionId: run.runId,
- }),
- workflowRuntime.dispatchSession({
- command: followUp("dpl_b", "second burst", "delivery-2"),
- sessionId: run.runId,
- }),
- ]);
- const turn = await stream.nextTurn();
- expect(turn.at(-1)?.type).toBe("session.waiting");
- expect((await stream.nextTurn()).at(-1)?.type).toBe("session.waiting");
- const owner = await waitForCommandHookOwner(
- sessionInboxHookToken(sessionCommandHookToken(run.runId)),
- );
- expect(owner.runId).toBe(run.runId);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- });
- });
-
it("exits a competing continuation owner before its first turn", async () => {
const runtime = await createTestRuntime({ agent: { name: "workflow-entry-hook-owner" } });
const continuationToken = "http:workflow-entry-hook-owner";
@@ -2245,138 +903,6 @@ describe("workflowEntry integration", () => {
});
});
-const CALLER_STEP_NAMES = new Set([
- "bindTurnCallerContextStep",
- "notifyTurnCallerStep",
- "resolveInitialTurnCallerStep",
-]);
-
-async function listCallerStepNames(runId: string): Promise<string[]> {
- return (await listStepNames(runId)).filter((name) => CALLER_STEP_NAMES.has(name)).sort();
-}
-
-async function listStepNames(runId: string): Promise<string[]> {
- const world = await getWorld();
- const steps = await world.steps.list({
- pagination: { limit: 1_000 },
- resolveData: "none",
- runId,
- });
- return steps.data.map((step) => step.stepName.split("//").at(-1) ?? "");
-}
-
-interface CapturedEventStream {
- dispose(): void;
- nextUntil(
- label: string,
- predicate: (event: MessageStreamEvent) => boolean,
- ): Promise<MessageStreamEvent[]>;
-}
-
-function captureEvents(run: Parameters<typeof captureTurnEvents>[0]): CapturedEventStream {
- const reader = run.readable.getReader();
- const decoder = new TextDecoder();
- let buffer = "";
- let disposed = false;
-
- return {
- dispose() {
- if (disposed) return;
- disposed = true;
- reader.releaseLock();
- },
- nextUntil(label, predicate) {
- if (disposed) {
- return Promise.reject(new Error("CapturedEventStream: stream already disposed."));
- }
- return withTimeout(readUntil(reader, decoder, buffer, predicate), label).then((result) => {
- buffer = result.buffer;
- return result.events;
- });
- },
- };
-}
-
-async function readUntil(
- reader: ReadableStreamDefaultReader<Uint8Array>,
- decoder: InstanceType<typeof TextDecoder>,
- initialBuffer: string,
- predicate: (event: MessageStreamEvent) => boolean,
-): Promise<{ buffer: string; events: MessageStreamEvent[] }> {
- const events: MessageStreamEvent[] = [];
- let buffer = initialBuffer;
-
- while (true) {
- const { done, value } = await reader.read();
-
- if (done) {
- throw new Error("Workflow stream closed before reaching the expected event.");
- }
-
- buffer += decoder.decode(value);
-
- for (
- let newlineIndex = buffer.indexOf("\n");
- newlineIndex !== -1;
- newlineIndex = buffer.indexOf("\n")
- ) {
- const line = buffer.slice(0, newlineIndex).trim();
- buffer = buffer.slice(newlineIndex + 1);
-
- if (line.length === 0) {
- continue;
- }
-
- const event = JSON.parse(line) as MessageStreamEvent;
- events.push(event);
-
- if (predicate(event)) {
- return { buffer, events };
- }
- }
- }
-}
-
-async function withTimeout<T>(promise: Promise<T>, label: string): Promise<T> {
- let timeout: ReturnType<typeof setTimeout> | undefined;
- try {
- return await Promise.race([
- promise,
- new Promise<never>((_resolve, reject) => {
- timeout = setTimeout(() => {
- reject(new Error(`Timed out waiting for ${label}.`));
- }, 30_000);
- }),
- ]);
- } finally {
- if (timeout !== undefined) {
- clearTimeout(timeout);
- }
- }
-}
-
-async function expectHookClaims(
- runId: string,
- tokens: string[],
- options: { readonly turnStarted?: boolean } = {},
-): Promise<void> {
- const events = await (
- await getWorld()
- ).events.list({
- runId,
- pagination: { limit: 1000 },
- resolveData: "all",
- });
- const claims = events.data.flatMap((event) =>
- event.eventType === "hook_created" ? [event.eventData.token] : [],
- );
- const signals = claims.filter((token) => token.startsWith("abrt_"));
- expect(signals).toHaveLength(options.turnStarted === false ? 0 : 2);
- expect(claims.filter((token) => !token.startsWith("abrt_")).sort()).toEqual(
- tokens.map(sessionInboxHookToken).sort(),
- );
-}
-
async function waitForRuntimeActionResult(runId: string, callId: string): Promise<unknown> {
const world = await getWorld();
const deadline = Date.now() + 10_000;
@@ -2458,22 +984,3 @@ function hasSubagentResult(value: unknown, callId: string): boolean {
result.callId === callId,
);
}
-
-async function readSessionTimer(ownerRunId: string): Promise<{ runId: string; deadline: Date }> {
- const world = await getWorld();
- let timer: { runId: string; deadline: Date } | undefined;
- await vi.waitFor(async () => {
- const steps = await world.steps.list({ runId: ownerRunId, resolveData: "all" });
- const start = steps.data.find((step) => step.stepName.endsWith("//startSessionTimeoutStep"));
- expect(start?.output).toBeDefined();
- const result = (await hydrateStepReturnValue(start!.output, ownerRunId, undefined)) as {
- runId: string;
- };
- const run = await world.runs.get(result.runId);
- const [input] = (await hydrateWorkflowArguments(run.input, run.runId, undefined)) as [
- { deadline: Date },
- ];
- timer = { runId: run.runId, deadline: input.deadline };
- });
- return timer!;
-}
diff --git a/packages/eve/src/execution/tasks/parent/cancel-notification.integration.test.ts b/packages/eve/src/execution/tasks/parent/cancel-notification.integration.test.ts
deleted file mode 100644
index 29fc7cab6..000000000
--- a/packages/eve/src/execution/tasks/parent/cancel-notification.integration.test.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { WORKFLOW_CANCELLATION_SETTLE_MS } from "#execution/tools/workflow/cancellation-policy.js";
-import { describe, expect, it } from "vitest";
-
-import { createTestRuntime } from "#internal/testing/app-harness.js";
-import { taskCancelNotificationWorkflow } from "#internal/testing/task-cancel-notification-workflow.js";
-import { start } from "#internal/workflow/runtime.js";
-
-describe("task cancellation parent notification", () => {
- it(
- "delivers cancellation from the parent without a child task-view stream",
- async () => {
- const runtime = await createTestRuntime({ agent: { name: "task-cancel-notification" } });
- await runtime.run(async () => {
- const run = await start(taskCancelNotificationWorkflow, []);
- try {
- const result = await run.returnValue;
- expect(result.taskRunStatus).toBe("cancelled");
- expect(result.view.status).toBe("cancelled");
- expect(result.notification).toMatchObject({
- kind: "send",
- payload: {
- message: `Background task ${result.view.taskId} (slow-cancel) is cancelled.`,
- task: { views: [result.view] },
- },
- taskDeliveryId: `${result.view.taskId}:ready:cancelled`,
- });
- } finally {
- const status = await run.status;
- if (status === "pending" || status === "running") await run.cancel();
- }
- });
- },
- WORKFLOW_CANCELLATION_SETTLE_MS + 15_000,
- );
-});
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization-close.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization-close.integration.test.ts
new file mode 100644
index 000000000..d15da7fb5
--- /dev/null
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization-close.integration.test.ts
@@ -0,0 +1,111 @@
+import { describe, expect, it } from "vitest";
+import { getWorld, start } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
+import { handleConnectionCallbackRequest } from "#execution/connections/callback-route.js";
+import { resumeSessionInbox } from "#execution/session-inbox/resume.js";
+import { authorizedDeployWorkflow } from "#internal/testing/workflow-tool-fixtures.js";
+import {
+ buildWorkflowToolSerializedContext,
+ createWorkflowToolRuntime,
+ waitForWorkflowToolRunTerminal,
+} from "#internal/testing/workflow-tool-run-harness.js";
+
+describe("workflow step authorization failures", () => {
+ it.each([
+ { background: false, disposition: "denied" },
+ { background: false, disposition: "rejected" },
+ { background: false, disposition: "cancel" },
+ { background: true, disposition: "cancel" },
+ ])(
+ "closes authorization on $disposition (background=$background)",
+ async ({ background, disposition }) => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-step-auth-failure",
+ background,
+ execute: authorizedDeployWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: `Run deploy_service with service "${disposition}"` },
+ serializedContext: {
+ ...buildWorkflowToolSerializedContext({
+ continuationToken: "http:step-auth-failure",
+ mode: "conversation",
+ requestInput: true,
+ }),
+ "eve.auth": {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ },
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+ try {
+ const events = [];
+ for (
+ let i = 0;
+ i < 5 && filterEventsByType(events, "authorization.required").length === 0;
+ i++
+ )
+ events.push(...(await stream.nextTurn()));
+ const required = filterEventsByType(events, "authorization.required")[0]!;
+ expect(required).toBeDefined();
+ const url = new URL(required.data.webhookUrl!);
+ const token = decodeURIComponent(url.pathname.split("/").at(-1)!);
+ const world = await getWorld();
+ const executorRunId = (await world.hooks.getByToken(token)).runId;
+ const params = { token, attemptId: required.data.attemptId!, name: required.data.name };
+ if (disposition === "cancel") {
+ await resumeSessionInbox(
+ sessionCommandHookToken(run.runId),
+ background ? { kind: "cancel", tasks: true } : { kind: "cancel", turnId: "turn_0" },
+ );
+ } else {
+ url.searchParams.set("code", disposition === "denied" ? "denied" : "approved");
+ expect(
+ (await handleConnectionCallbackRequest(new Request(url), { params } as never)).status,
+ ).toBe(200);
+ }
+ if (disposition === "cancel") {
+ if (!background) {
+ events.push(...(await stream.nextTurn()));
+ expect(filterEventsByType(events, "turn.cancelled")).toHaveLength(1);
+ }
+ } else {
+ for (
+ let i = 0;
+ i < 6 && filterEventsByType(events, "authorization.completed").length === 0;
+ i++
+ )
+ events.push(...(await stream.nextTurn()));
+ expect(
+ filterEventsByType(events, "authorization.completed").map(
+ (event) => event.data.outcome,
+ ),
+ ).toEqual(["failed"]);
+ }
+ expect(filterEventsByType(events, "authorization.required")).toHaveLength(1);
+ await waitForWorkflowToolRunTerminal(executorRunId);
+ expect(
+ (await handleConnectionCallbackRequest(new Request(url), { params } as never)).status,
+ ).toBe(404);
+ expect(JSON.stringify(events)).not.toContain("secret:");
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ },
+ 60_000,
+ );
+});
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization.integration.test.ts
new file mode 100644
index 000000000..eed50e979
--- /dev/null
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run-authorization.integration.test.ts
@@ -0,0 +1,174 @@
+import { describe, expect, it } from "vitest";
+import { getRun, getWorld, start } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import { hydrateWorkflowReturnValue } from "@workflow/core/serialization";
+import { handleConnectionCallbackRequest } from "#execution/connections/callback-route.js";
+import { authorizedDeployWorkflow } from "#internal/testing/workflow-tool-fixtures.js";
+import {
+ buildWorkflowToolSerializedContext,
+ createWorkflowToolRuntime,
+} from "#internal/testing/workflow-tool-run-harness.js";
+
+describe("workflow step authorization", () => {
+ it("resolves a user token inside a step", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-step-token",
+ execute: authorizedDeployWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "preauthorized"' },
+ serializedContext: {
+ ...buildWorkflowToolSerializedContext({
+ continuationToken: "http:step-token",
+ mode: "conversation",
+ }),
+ "eve.auth": {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ },
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+ try {
+ let text = "";
+ for (let i = 0; i < 5 && !text.includes("authenticatedAs"); i++) {
+ text += JSON.stringify(await stream.nextTurn());
+ }
+ expect(text).toContain("authenticatedAs");
+ expect(text).toContain("user-1");
+ expect(text).not.toContain("secret:");
+ expect(text).not.toContain("authorization.required");
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 60_000);
+
+ it.each([
+ { background: false, service: "interactive" },
+ { background: true, service: "interactive" },
+ { background: false, service: "retry" },
+ ])(
+ "parks on its own callback and resumes the step (background=$background, service=$service)",
+ async ({ background, service }) => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-step-auth",
+ background,
+ execute: authorizedDeployWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: `Run deploy_service with service "${service}"` },
+ serializedContext: {
+ ...buildWorkflowToolSerializedContext({
+ continuationToken: "http:step-auth",
+ mode: "conversation",
+ requestInput: true,
+ }),
+ "eve.auth": {
+ attributes: {},
+ authenticator: "test-idp",
+ issuer: "test-idp",
+ principalId: "user-1",
+ principalType: "user",
+ },
+ },
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+ try {
+ const events = [];
+ for (
+ let i = 0;
+ i < 5 && filterEventsByType(events, "authorization.required").length === 0;
+ i++
+ )
+ events.push(...(await stream.nextTurn()));
+ const required = filterEventsByType(events, "authorization.required")[0]!;
+ expect(required).toBeDefined();
+ const url = new URL(required.data.webhookUrl!);
+ const parts = url.pathname.split("/").map(decodeURIComponent);
+ const token = parts.at(-1)!;
+ expect(token).not.toBe(`${run.runId}:auth`);
+ const world = await getWorld();
+ const executorRunId = (await world.hooks.getByToken(token)).runId;
+ url.searchParams.set("code", "approved");
+ await handleConnectionCallbackRequest(new Request(url), {
+ params: { token, attemptId: "another-attempt", name: required.data.name },
+ } as never);
+ await handleConnectionCallbackRequest(new Request(url), {
+ params: { token, attemptId: required.data.attemptId!, name: "another-provider" },
+ } as never);
+ const response = await handleConnectionCallbackRequest(new Request(url), {
+ params: { token, attemptId: required.data.attemptId!, name: required.data.name },
+ } as never);
+ expect(response.status).toBe(200);
+ for (let i = 0; i < 6 && !JSON.stringify(events).includes("authenticatedAs"); i++) {
+ events.push(...(await stream.nextTurn()));
+ }
+ expect(
+ filterEventsByType(events, "authorization.completed").map(
+ (event) => event.data.outcome,
+ ),
+ ).toEqual(["authorized"]);
+ const text = JSON.stringify(events);
+ expect(text).toContain("authenticatedAs");
+ expect(text).toContain("user-1");
+ expect(text).not.toContain("secret:");
+ const steps = await world.steps.list({
+ runId: executorRunId,
+ pagination: { limit: 1000 },
+ });
+ expect(
+ steps.data.filter((step) => step.stepName.endsWith("//planDeployStep")),
+ ).toHaveLength(1);
+ const attempts = steps.data.filter((step) =>
+ step.stepName.endsWith("//authorizedDeployStep:eve-authorization"),
+ );
+ expect(attempts).toHaveLength(2);
+ if (service === "retry") {
+ expect(attempts.map((step) => step.attempt).sort()).toEqual([1, 2]);
+ const retried = attempts.find((step) => step.attempt === 2)!;
+ const marker = getRun(executorRunId).getReadable({
+ namespace: `eve.authorization.${retried.stepId}.${required.data.attemptId}`,
+ });
+ const reader = marker.getReader();
+ try {
+ expect((await reader.read()).value).toBe(true);
+ } finally {
+ await reader.cancel();
+ reader.releaseLock();
+ }
+ }
+ for (const step of attempts) {
+ const output = await hydrateWorkflowReturnValue(step.output, executorRunId, undefined);
+ expect(JSON.stringify(output)).not.toContain("secret:");
+ }
+ const duplicate = await handleConnectionCallbackRequest(new Request(url), {
+ params: { token, attemptId: required.data.attemptId!, name: required.data.name },
+ } as never);
+ expect(duplicate.status).toBe(404);
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ },
+ 60_000,
+ );
+});
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run-background.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run-background.integration.test.ts
new file mode 100644
index 000000000..ab682b64d
--- /dev/null
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run-background.integration.test.ts
@@ -0,0 +1,217 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { start } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
+import { resumeSessionInbox } from "#execution/session-inbox/resume.js";
+import type { TestRuntime } from "#internal/testing/app-harness.js";
+import {
+ backgroundDeployWorkflow,
+ confirmDeployWorkflow,
+ failingDeployWorkflow,
+} from "#internal/testing/workflow-tool-fixtures.js";
+import type { InputRequestedStreamEvent } from "#protocol/message.js";
+import {
+ buildWorkflowToolSerializedContext,
+ createWorkflowToolRuntime,
+} from "#internal/testing/workflow-tool-run-harness.js";
+
+describe("background workflow tools", () => {
+ afterEach(() => vi.unstubAllEnvs());
+ it("runs a session-owned background workflow invocation", async () => {
+ vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-tool-background",
+ execute: backgroundDeployWorkflow,
+ toolName: "report_deploy",
+ });
+
+ await runtime.run(async () => {
+ enableBackgroundTool(runtime, "report_deploy");
+
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run report_deploy with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ acceptedDeploymentId: "dpl_inline",
+ continuationToken: "http:workflow-tool-background",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+
+ try {
+ const receiptTurn = await stream.nextTurn();
+ const receipt = filterEventsByType(receiptTurn, "action.result").find(
+ (event) => event.data.result.kind === "tool-result",
+ );
+ expect(receipt?.data.result.output).toMatchObject({ status: "working" });
+ expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
+
+ const notifications: string[] = [];
+ for (
+ let turn = 0;
+ turn < 3 && !notifications.some((text) => text.includes("is completed"));
+ turn += 1
+ ) {
+ const woken = await stream.nextTurn();
+ expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
+ notifications.push(eventsText(filterEventsByType(woken, "message.received")));
+ }
+ const text = notifications.join("\n");
+ expect(text).not.toContain("Review plan:api");
+ expect(text).not.toContain("update: planned api");
+ expect(text).toContain("is completed");
+ expect(text).toContain("plan:api");
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 90_000);
+
+ it("wakes the agent with the failure when a background workflow tool throws", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-tool-background-fail",
+ execute: failingDeployWorkflow,
+ toolName: "deploy_service",
+ });
+
+ await runtime.run(async () => {
+ enableBackgroundTool(runtime, "deploy_service");
+
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ continuationToken: "http:workflow-tool-background-fail",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+
+ try {
+ const receiptTurn = await stream.nextTurn();
+ expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
+
+ const notifications: string[] = [];
+ for (
+ let turn = 0;
+ turn < 3 && !notifications.some((t) => t.includes("failed"));
+ turn += 1
+ ) {
+ const woken = await stream.nextTurn();
+ expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
+ notifications.push(eventsText(filterEventsByType(woken, "message.received")));
+ }
+ const text = notifications.join("\n");
+ expect(text).toContain("(deploy_service) failed.");
+ expect(text).toContain("deploy of api exploded");
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 90_000);
+
+ it("lets a background workflow tool ask now and act when answered", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-tool-background-hitl",
+ execute: confirmDeployWorkflow,
+ toolName: "confirm_deploy",
+ });
+
+ await runtime.run(async () => {
+ enableBackgroundTool(runtime, "confirm_deploy");
+
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run confirm_deploy with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ continuationToken: "http:workflow-tool-background-hitl",
+ mode: "conversation",
+ requestInput: true,
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+
+ try {
+ const receiptTurn = await stream.nextTurn();
+ expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
+ expect(
+ filterEventsByType(receiptTurn, "action.result").some(
+ (event) =>
+ event.data.result.kind === "tool-result" &&
+ typeof event.data.result.output === "object" &&
+ event.data.result.output !== null &&
+ "taskId" in event.data.result.output,
+ ),
+ ).toBe(true);
+
+ // The question arrives after the turn that called the tool ended.
+ let request: InputRequestedStreamEvent["data"]["requests"][number] | undefined;
+ for (let turn = 0; turn < 3 && request === undefined; turn += 1) {
+ const woken = await stream.nextTurn();
+ expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
+ request = (
+ filterEventsByType(woken, "input.requested")[0] as InputRequestedStreamEvent | undefined
+ )?.data.requests[0];
+ }
+ expect(request).toMatchObject({ kind: "question", prompt: "Apply plan:api?" });
+
+ // The stable session inbox already took the task's wake, so it is
+ // known to exist; answer through it directly.
+ await resumeSessionInbox(sessionCommandHookToken(run.runId), {
+ kind: "send",
+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },
+ });
+
+ const notifications: string[] = [];
+ for (
+ let turn = 0;
+ turn < 3 && !notifications.some((text) => text.includes("is completed"));
+ turn += 1
+ ) {
+ const woken = await stream.nextTurn();
+ expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
+ notifications.push(eventsText(filterEventsByType(woken, "message.received")));
+ }
+ const text = notifications.join("\n");
+ expect(text).toContain("is completed");
+ expect(text).toContain('\\"approved\\":true');
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 90_000);
+});
+
+/** Flags one manifest tool as `execution: "background"` under `experimental.tasks`. */
+function enableBackgroundTool(runtime: TestRuntime, toolName: string): void {
+ const artifacts = runtime.session.compiledArtifacts;
+ if (artifacts === null) throw new Error("expected installed compiled artifacts");
+ runtime.session.compiledArtifacts = {
+ ...artifacts,
+ manifest: {
+ ...artifacts.manifest,
+ config: artifacts.manifest.config,
+ tools: artifacts.manifest.tools.map((tool) =>
+ tool.name === toolName ? { ...tool, execution: "background" as const } : tool,
+ ),
+ },
+ };
+}
+
+function eventsText(events: readonly { readonly data?: unknown }[]): string {
+ return events.map((event) => JSON.stringify(event.data ?? null)).join("\n");
+}
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run-cancellation.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run-cancellation.integration.test.ts
new file mode 100644
index 000000000..7cc468fc3
--- /dev/null
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run-cancellation.integration.test.ts
@@ -0,0 +1,272 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { getRun, getWorld, start } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { waitForHook } from "#internal/testing/workflow-test-helpers.js";
+import { workflowEntry } from "#execution/session/entry.js";
+import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
+import { resumeSessionInbox } from "#execution/session-inbox/resume.js";
+import { cancelBackgroundAgentTask } from "#execution/tools/subagent/task-cancel.js";
+import { setAgentHandleStore } from "#subagents/handles/store.js";
+import {
+ holdUntilAbortedWorkflow,
+ receiveDelegatedResultWorkflow,
+} from "#internal/testing/workflow-tool-fixtures.js";
+import { workflowToolRunWorkflowReference } from "#execution/workflow-runtime.js";
+import {
+ buildWorkflowToolSerializedContext,
+ createWorkflowToolRuntime,
+ waitForWorkflowToolRunTerminal,
+} from "#internal/testing/workflow-tool-run-harness.js";
+
+describe("workflow tool cancellation", () => {
+ afterEach(() => vi.unstubAllEnvs());
+ it("fails the retained caller when a yielded child receives reset", async () => {
+ const kind = "reset";
+ const runtime = await createWorkflowToolRuntime({
+ agentName: `yielded-child-${kind}`,
+ background: true,
+ execute: holdUntilAbortedWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const parentToken = `parent-result-${kind}`;
+ const parent = await start(receiveDelegatedResultWorkflow, [parentToken]);
+ let child: ReturnType<typeof getRun> | undefined;
+ let stream: ReturnType<typeof captureTurnEvents> | undefined;
+ let nestedRunId: string | undefined;
+ try {
+ await waitForHook(parent, { token: parentToken });
+ const before = await listWorkflowToolRunIds();
+ child = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: {
+ ...buildWorkflowToolSerializedContext({
+ continuationToken: `child-${kind}`,
+ mode: "conversation",
+ }),
+ "eve.channel": {
+ kind: "subagent",
+ state: {
+ callId: "delegate",
+ subagentName: "detector",
+ parentContinuationToken: parentToken,
+ parentSessionId: parent.runId,
+ },
+ },
+ },
+ },
+ ]);
+ stream = captureTurnEvents(child);
+ const yielded = await stream.nextTurn();
+ expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
+ expect(yielded.at(-1)?.type).toBe("session.waiting");
+ nestedRunId = await waitForNewWorkflowToolRun(before);
+ await waitForHook({ runId: nestedRunId });
+ expect(await parent.status).toBe("running");
+ await resumeSessionInbox(sessionCommandHookToken(child.runId), { kind });
+ await expect(parent.returnValue).resolves.toMatchObject({
+ kind: "runtime-action-result",
+ results: [
+ {
+ callId: "delegate",
+ isError: true,
+ outcome: {
+ kind: "terminal",
+ result: { kind: "failed" },
+ },
+ },
+ ],
+ });
+ await expect(child.returnValue).resolves.toEqual({
+ output: "The session ended before the delegated task completed.",
+ });
+ expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
+ } finally {
+ stream?.dispose();
+ if (nestedRunId !== undefined) {
+ const nested = getRun(nestedRunId);
+ const status = await nested.status;
+ if (status === "pending" || status === "running") await nested.cancel();
+ }
+ for (const run of [child, parent]) {
+ if (run === undefined) continue;
+ const status = await run.status;
+ if (status === "pending" || status === "running") await run.cancel();
+ }
+ }
+ });
+ }, 30_000);
+
+ it("cancels nested work after its owning child has yielded", async () => {
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "yielded-child-cancel",
+ background: true,
+ execute: holdUntilAbortedWorkflow,
+ toolName: "deploy_service",
+ });
+ await runtime.run(async () => {
+ const before = await listWorkflowToolRunIds();
+ const child = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ continuationToken: "http:yielded-child-cancel",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(child);
+ let nestedRunId: string | undefined;
+ try {
+ const yielded = await stream.nextTurn();
+ expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
+ expect(yielded.at(-1)?.type).toBe("session.waiting");
+ nestedRunId = await waitForNewWorkflowToolRun(before);
+ await waitForHook({ runId: nestedRunId });
+ expect(await getRun(nestedRunId).status).toBe("running");
+
+ await cancelBackgroundAgentTask({
+ entry: {
+ callId: "delegate",
+ toolName: "child",
+ lifetime: "session",
+ origin: { turnId: "parent-turn", stepIndex: 0 },
+ address: { runId: child.runId, hookToken: "unused" },
+ task: {
+ taskId: "outer-task",
+ metadata: { kind: "subagent", name: "child" },
+ dispatchContext: { auth: { current: null, initiator: null } },
+ },
+ },
+ session: {
+ state: setAgentHandleStore(undefined, {
+ handles: [
+ {
+ phase: "claimed",
+ ownerId: "outer-task",
+ operationId: "delegate",
+ identity: { id: "child", name: "child", nodeId: "subagents/child" },
+ address: {
+ kind: "agent/local",
+ sessionId: child.runId,
+ continuationToken: "child",
+ },
+ },
+ ],
+ }),
+ },
+ serializedContext: {},
+ });
+
+ // The held step can finish only after its abort signal fires or its 60s timer expires.
+ // Finishing within 15s proves cancellation reached the yielded child's work.
+ expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
+
+ // This input follows the cancellation notification in the child's inbox.
+ // It must be the next turn, without a task notification waking the child first.
+ await resumeSessionInbox(sessionCommandHookToken(child.runId), {
+ kind: "send",
+ payload: { message: "Hello again" },
+ });
+ const next = await stream.nextTurn();
+ expect(
+ filterEventsByType(next, "message.received").map((event) => event.data.message),
+ ).toEqual(["Hello again"]);
+ expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);
+ } finally {
+ stream.dispose();
+ if (nestedRunId !== undefined) {
+ const nested = getRun(nestedRunId);
+ const status = await nested.status;
+ if (status === "pending" || status === "running") await nested.cancel();
+ }
+ await child.cancel();
+ }
+ });
+ }, 30_000);
+
+ it("cancels the run when the waiting turn is cancelled and lets the body clean up", async () => {
+ vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
+ const runtime = await createWorkflowToolRuntime({
+ agentName: "workflow-tool-cancel",
+ execute: holdUntilAbortedWorkflow,
+ toolName: "deploy_service",
+ });
+
+ await runtime.run(async () => {
+ const before = await listWorkflowToolRunIds();
+ const run = await start(workflowEntry, [
+ {
+ kind: "initial",
+ ownerDeploymentId: "dpl_inline",
+ input: { message: 'Run deploy_service with service "api"' },
+ serializedContext: buildWorkflowToolSerializedContext({
+ acceptedDeploymentId: "dpl_inline",
+ continuationToken: "http:workflow-tool-cancel",
+ mode: "conversation",
+ }),
+ },
+ ]);
+ const stream = captureTurnEvents(run);
+
+ try {
+ const workflowToolRunId = await waitForNewWorkflowToolRun(before);
+ await waitForHook({ runId: workflowToolRunId });
+ const commandToken = sessionCommandHookToken(run.runId);
+ await resumeSessionInbox(commandToken, { kind: "cancel", turnId: "turn_0" });
+
+ // The body is holding in a step that received ctx.abortSignal, so the
+ // run ends well inside its grace period once the step rejects and
+ // `finally` runs; a run that ignored the signal would still be running.
+ expect(await waitForWorkflowToolRunTerminal(workflowToolRunId)).toBe("completed");
+
+ await resumeSessionInbox(commandToken, {
+ kind: "send",
+ payload: { message: "Thanks, no deploy today." },
+ });
+ const next = await stream.nextTurn();
+ expect(filterEventsByType(next, "turn.started")).toHaveLength(1);
+ expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);
+ expect(next.at(-1)?.type).toBe("session.waiting");
+ } finally {
+ stream.dispose();
+ await run.cancel();
+ }
+ });
+ }, 60_000);
+});
+
+/** Ids of every workflow tool run in the shared world, so a test can spot the one it started. */
+async function listWorkflowToolRunIds(): Promise<Set<string>> {
+ const world = await getWorld();
+ const page = await world.runs.list({ pagination: { limit: 100 } });
+ return new Set(
+ page.data
+ .filter(
+ (entry: { readonly workflowName?: string }) =>
+ entry.workflowName === workflowToolRunWorkflowReference.workflowId,
+ )
+ .map((entry: { readonly runId: string }) => entry.runId),
+ );
+}
+
+/** Polls until exactly one workflow tool run exists that was not in `before`. */
+async function waitForNewWorkflowToolRun(
+ before: ReadonlySet<string>,
+ timeout = 15_000,
+): Promise<string> {
+ const deadline = Date.now() + timeout;
+ while (Date.now() < deadline) {
+ const started = [...(await listWorkflowToolRunIds())].filter((runId) => !before.has(runId));
+ if (started.length === 1) return started[0]!;
+ if (started.length > 1)
+ throw new Error(`Expected one new workflow tool run, found ${started.length}.`);
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ }
+ throw new Error("Timed out waiting for a workflow tool run to start.");
+}
diff --git a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
index 68f33620e..357559648 100644
--- a/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
+++ b/packages/eve/src/execution/tools/workflow/workflow-tool-run.integration.test.ts
@@ -1,416 +1,24 @@
-import { hydrateWorkflowReturnValue } from "@workflow/core/serialization";
import { afterEach, describe, expect, it, vi } from "vitest";
-
-import { handleConnectionCallbackRequest } from "#execution/connections/callback-route.js";
+import { start } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { workflowEntry } from "#execution/session/entry.js";
import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
-import { executeSleepTool, SLEEP_INPUT_SCHEMA } from "#execution/tools/sleep.js";
+import { SLEEP_INPUT_SCHEMA, executeSleepTool } from "#execution/tools/sleep.js";
import { resumeSessionInbox } from "#execution/session-inbox/resume.js";
-import { cancelBackgroundAgentTask } from "#execution/tools/subagent/task-cancel.js";
-import { setAgentHandleStore } from "#subagents/handles/store.js";
-import { workflowEntry } from "#execution/session/entry.js";
-import { createTestRuntime, type TestRuntime } from "#internal/testing/app-harness.js";
-import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
import {
askThenRaceWorkflow,
- authorizedDeployWorkflow,
- backgroundDeployWorkflow,
confirmDeployWorkflow,
deployServiceWorkflow,
failingDeployWorkflow,
- holdUntilAbortedWorkflow,
reportingDeployWorkflow,
- receiveDelegatedResultWorkflow,
- stepThenRaceWorkflow,
stepReferenceWorkflow,
workflowContextMisuseWorkflow,
} from "#internal/testing/workflow-tool-fixtures.js";
-import { waitForHook } from "#internal/testing/workflow-test-helpers.js";
-import { getRun, getWorld, start } from "#internal/workflow/runtime.js";
-import { workflowToolRunWorkflowReference } from "#execution/workflow-runtime.js";
-import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
import type { InputRequestedStreamEvent } from "#protocol/message.js";
-import type { ResolvedToolDefinition } from "#runtime/types.js";
import {
- defineWorkflowTool,
- type BlockingWorkflowToolDefinition,
-} from "#tools/workflow-definition.js";
-import { serializeInputSchema, toInputSchema } from "#tools/schema.js";
-
-const DEPLOY_INPUT_SCHEMA = toInputSchema({
- additionalProperties: false,
- properties: { service: { type: "string" } },
- required: ["service"],
- type: "object",
-});
-
-function buildSerializedContext(input: {
- readonly acceptedDeploymentId?: string;
- readonly continuationToken: string;
- readonly mode: "conversation" | "task";
- readonly requestInput?: boolean;
-}): Record<string, unknown> {
- return {
- "eve.auth": null,
- "eve.bundle": { source: createBundledRuntimeCompiledArtifactsSource() },
- "eve.capabilities": { requestInput: input.requestInput ?? false },
- "eve.channel": { kind: "http", state: {} },
- ...(input.acceptedDeploymentId === undefined
- ? {}
- : {
- "eve.channelDelivery": {
- acceptedDeploymentId: input.acceptedDeploymentId,
- channelKind: "http",
- channelName: "test",
- deliveryId: "delivery-initial",
- },
- }),
- "eve.continuationToken": input.continuationToken,
- "eve.mode": input.mode,
- };
-}
-
-/**
- * Registers one fixture workflow as an authored tool. The fixture module
- * passed through the test tier's client transform, so `execute` is the stub
- * the real pipeline produces: a function carrying its `workflowId`.
- */
-async function createWorkflowToolRuntime(input: {
- readonly agentName: string;
- readonly background?: boolean;
- readonly execute: (...args: never[]) => unknown;
- readonly inputSchema?: ResolvedToolDefinition["inputSchema"];
- readonly toolName: string;
-}): Promise<TestRuntime> {
- return await createTestRuntime({
- agent: { name: input.agentName },
- modules: [
- {
- logicalPath: `tools/${input.toolName}.ts`,
- loadNamespace: async () => ({
- default: defineWorkflowTool({
- execution: input.background === true ? "background" : undefined,
- description: `Deploys a service (${input.toolName}).`,
- execute: input.execute as BlockingWorkflowToolDefinition["execute"],
- inputSchema: serializeInputSchema(input.inputSchema ?? DEPLOY_INPUT_SCHEMA) ?? {},
- }),
- }),
- },
- ],
- });
-}
-
-/** Ids of every workflow tool run in the shared world, so a test can spot the one it started. */
-async function listWorkflowToolRunIds(): Promise<Set<string>> {
- const world = await getWorld();
- const page = await world.runs.list({ pagination: { limit: 100 } });
- return new Set(
- page.data
- .filter(
- (entry: { readonly workflowName?: string }) =>
- entry.workflowName === workflowToolRunWorkflowReference.workflowId,
- )
- .map((entry: { readonly runId: string }) => entry.runId),
- );
-}
-
-/** Polls until exactly one workflow tool run exists that was not in `before`. */
-async function waitForNewWorkflowToolRun(
- before: ReadonlySet<string>,
- timeout = 15_000,
-): Promise<string> {
- const deadline = Date.now() + timeout;
- while (Date.now() < deadline) {
- const started = [...(await listWorkflowToolRunIds())].filter((runId) => !before.has(runId));
- if (started.length === 1) return started[0]!;
- if (started.length > 1)
- throw new Error(`Expected one new workflow tool run, found ${started.length}.`);
- await new Promise((resolve) => setTimeout(resolve, 50));
- }
- throw new Error("Timed out waiting for a workflow tool run to start.");
-}
-
-/** Polls one run until it reaches a terminal status, returning that status. */
-async function waitForWorkflowToolRunTerminal(runId: string, timeout = 15_000): Promise<string> {
- const terminal = new Set(["completed", "failed", "cancelled"]);
- const deadline = Date.now() + timeout;
- while (Date.now() < deadline) {
- const status = await getRun(runId).status;
- if (terminal.has(status)) return status;
- await new Promise((resolve) => setTimeout(resolve, 50));
- }
- throw new Error(`Timed out waiting for run ${runId} to reach a terminal status.`);
-}
-
-function eventsText(events: readonly { readonly data?: unknown }[]): string {
- return events.map((event) => JSON.stringify(event.data ?? null)).join("\n");
-}
-
-describe("workflow step authorization", () => {
- it.each([false, true])(
- "resolves a user token inside a step (background=%s)",
- async (background) => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-step-token",
- background,
- execute: authorizedDeployWorkflow,
- toolName: "deploy_service",
- });
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run deploy_service with service "preauthorized"' },
- serializedContext: {
- ...buildSerializedContext({
- continuationToken: "http:step-token",
- mode: "conversation",
- }),
- "eve.auth": {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- },
- },
- ]);
- const stream = captureTurnEvents(run);
- try {
- let text = "";
- for (let i = 0; i < 5 && !text.includes("authenticatedAs"); i++) {
- text += JSON.stringify(await stream.nextTurn());
- }
- expect(text).toContain("authenticatedAs");
- expect(text).toContain("user-1");
- expect(text).not.toContain("secret:");
- expect(text).not.toContain("authorization.required");
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- },
- 60_000,
- );
-
- it.each([
- { background: false, service: "interactive" },
- { background: true, service: "interactive" },
- { background: false, service: "retry" },
- { background: true, service: "retry" },
- ])(
- "parks on its own callback and resumes the step (background=$background, service=$service)",
- async ({ background, service }) => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-step-auth",
- background,
- execute: authorizedDeployWorkflow,
- toolName: "deploy_service",
- });
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: `Run deploy_service with service "${service}"` },
- serializedContext: {
- ...buildSerializedContext({
- continuationToken: "http:step-auth",
- mode: "conversation",
- requestInput: true,
- }),
- "eve.auth": {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- },
- },
- ]);
- const stream = captureTurnEvents(run);
- try {
- const events = [];
- for (
- let i = 0;
- i < 5 && filterEventsByType(events, "authorization.required").length === 0;
- i++
- )
- events.push(...(await stream.nextTurn()));
- const required = filterEventsByType(events, "authorization.required")[0]!;
- expect(required).toBeDefined();
- const url = new URL(required.data.webhookUrl!);
- const parts = url.pathname.split("/").map(decodeURIComponent);
- const token = parts.at(-1)!;
- expect(token).not.toBe(`${run.runId}:auth`);
- const world = await getWorld();
- const executorRunId = (await world.hooks.getByToken(token)).runId;
- url.searchParams.set("code", "approved");
- await handleConnectionCallbackRequest(new Request(url), {
- params: { token, attemptId: "another-attempt", name: required.data.name },
- } as never);
- await handleConnectionCallbackRequest(new Request(url), {
- params: { token, attemptId: required.data.attemptId!, name: "another-provider" },
- } as never);
- const response = await handleConnectionCallbackRequest(new Request(url), {
- params: { token, attemptId: required.data.attemptId!, name: required.data.name },
- } as never);
- expect(response.status).toBe(200);
- for (let i = 0; i < 6 && !JSON.stringify(events).includes("authenticatedAs"); i++) {
- events.push(...(await stream.nextTurn()));
- }
- expect(
- filterEventsByType(events, "authorization.completed").map(
- (event) => event.data.outcome,
- ),
- ).toEqual(["authorized"]);
- const text = JSON.stringify(events);
- expect(text).toContain("authenticatedAs");
- expect(text).toContain("user-1");
- expect(text).not.toContain("secret:");
- const steps = await world.steps.list({
- runId: executorRunId,
- pagination: { limit: 1000 },
- });
- expect(
- steps.data.filter((step) => step.stepName.endsWith("//planDeployStep")),
- ).toHaveLength(1);
- const attempts = steps.data.filter((step) =>
- step.stepName.endsWith("//authorizedDeployStep:eve-authorization"),
- );
- expect(attempts).toHaveLength(2);
- if (service === "retry") {
- expect(attempts.map((step) => step.attempt).sort()).toEqual([1, 2]);
- const retried = attempts.find((step) => step.attempt === 2)!;
- const marker = getRun(executorRunId).getReadable({
- namespace: `eve.authorization.${retried.stepId}.${required.data.attemptId}`,
- });
- const reader = marker.getReader();
- try {
- expect((await reader.read()).value).toBe(true);
- } finally {
- await reader.cancel();
- reader.releaseLock();
- }
- }
- for (const step of attempts) {
- const output = await hydrateWorkflowReturnValue(step.output, executorRunId, undefined);
- expect(JSON.stringify(output)).not.toContain("secret:");
- }
- const duplicate = await handleConnectionCallbackRequest(new Request(url), {
- params: { token, attemptId: required.data.attemptId!, name: required.data.name },
- } as never);
- expect(duplicate.status).toBe(404);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- },
- 60_000,
- );
-
- it.each([
- { background: false, disposition: "denied" },
- { background: true, disposition: "denied" },
- { background: false, disposition: "rejected" },
- { background: true, disposition: "rejected" },
- { background: false, disposition: "cancel" },
- { background: true, disposition: "cancel" },
- ])(
- "closes authorization on $disposition (background=$background)",
- async ({ background, disposition }) => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-step-auth-failure",
- background,
- execute: authorizedDeployWorkflow,
- toolName: "deploy_service",
- });
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: `Run deploy_service with service "${disposition}"` },
- serializedContext: {
- ...buildSerializedContext({
- continuationToken: "http:step-auth-failure",
- mode: "conversation",
- requestInput: true,
- }),
- "eve.auth": {
- attributes: {},
- authenticator: "test-idp",
- issuer: "test-idp",
- principalId: "user-1",
- principalType: "user",
- },
- },
- },
- ]);
- const stream = captureTurnEvents(run);
- try {
- const events = [];
- for (
- let i = 0;
- i < 5 && filterEventsByType(events, "authorization.required").length === 0;
- i++
- )
- events.push(...(await stream.nextTurn()));
- const required = filterEventsByType(events, "authorization.required")[0]!;
- expect(required).toBeDefined();
- const url = new URL(required.data.webhookUrl!);
- const token = decodeURIComponent(url.pathname.split("/").at(-1)!);
- const world = await getWorld();
- const executorRunId = (await world.hooks.getByToken(token)).runId;
- const params = { token, attemptId: required.data.attemptId!, name: required.data.name };
- if (disposition === "cancel") {
- await resumeSessionInbox(
- sessionCommandHookToken(run.runId),
- background ? { kind: "cancel", tasks: true } : { kind: "cancel", turnId: "turn_0" },
- );
- } else {
- url.searchParams.set("code", disposition === "denied" ? "denied" : "approved");
- expect(
- (await handleConnectionCallbackRequest(new Request(url), { params } as never)).status,
- ).toBe(200);
- }
- if (disposition === "cancel") {
- if (!background) {
- events.push(...(await stream.nextTurn()));
- expect(filterEventsByType(events, "turn.cancelled")).toHaveLength(1);
- }
- } else {
- for (
- let i = 0;
- i < 6 && filterEventsByType(events, "authorization.completed").length === 0;
- i++
- )
- events.push(...(await stream.nextTurn()));
- expect(
- filterEventsByType(events, "authorization.completed").map(
- (event) => event.data.outcome,
- ),
- ).toEqual(["failed"]);
- }
- expect(filterEventsByType(events, "authorization.required")).toHaveLength(1);
- await waitForWorkflowToolRunTerminal(executorRunId);
- expect(
- (await handleConnectionCallbackRequest(new Request(url), { params } as never)).status,
- ).toBe(404);
- expect(JSON.stringify(events)).not.toContain("secret:");
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- },
- 60_000,
- );
-});
+ buildWorkflowToolSerializedContext,
+ createWorkflowToolRuntime,
+} from "#internal/testing/workflow-tool-run-harness.js";
describe("workflow tools", () => {
afterEach(() => vi.unstubAllEnvs());
@@ -426,7 +34,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "schedule:step-reference",
mode: "task",
}),
@@ -451,7 +59,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: "Run sleep" },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "schedule:workflow-tool-sleep",
mode: "task",
}),
@@ -477,7 +85,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "schedule:workflow-tool-wait",
mode: "task",
}),
@@ -504,7 +112,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "schedule:workflow-step-context-misuse",
mode: "task",
}),
@@ -534,7 +142,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "schedule:workflow-tool-fail",
mode: "task",
}),
@@ -561,7 +169,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run confirm_deploy with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
acceptedDeploymentId: "dpl_inline",
continuationToken: "http:workflow-tool-hitl",
mode: "conversation",
@@ -618,40 +226,6 @@ describe("workflow tools", () => {
});
}, 60_000);
- it("times out a hook raced against a sleep when it is not resumed", async () => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-tool-deadline",
- execute: stepThenRaceWorkflow,
- toolName: "deploy_service",
- });
-
- await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "Run deploy_service" },
- serializedContext: buildSerializedContext({
- continuationToken: "http:workflow-tool-deadline",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
- try {
- const settled = await stream.nextTurn();
- const outputs = filterEventsByType(settled, "action.result").map((event) =>
- JSON.stringify(event.data.result.output),
- );
- expect(outputs.some((output) => output.includes('"decided":"timed out"'))).toBe(true);
- expect(filterEventsByType(settled, "turn.failed")).toHaveLength(0);
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- }, 30_000);
-
it("lets a deadline win a race against an unanswered ask", async () => {
const runtime = await createWorkflowToolRuntime({
agentName: "workflow-tool-ask-deadline",
@@ -665,7 +239,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run confirm_deploy with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "http:workflow-tool-ask-deadline",
mode: "conversation",
requestInput: true,
@@ -698,287 +272,6 @@ describe("workflow tools", () => {
});
}, 30_000);
- it.each(["session-timeout", "reset"] as const)(
- "fails the retained caller when a yielded child receives %s",
- async (kind) => {
- const runtime = await createWorkflowToolRuntime({
- agentName: `yielded-child-${kind}`,
- background: true,
- execute: holdUntilAbortedWorkflow,
- toolName: "deploy_service",
- });
- await runtime.run(async () => {
- const parentToken = `parent-result-${kind}`;
- const parent = await start(receiveDelegatedResultWorkflow, [parentToken]);
- let child: ReturnType<typeof getRun> | undefined;
- let stream: ReturnType<typeof captureTurnEvents> | undefined;
- let nestedRunId: string | undefined;
- try {
- await waitForHook(parent, { token: parentToken });
- const before = await listWorkflowToolRunIds();
- child = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run deploy_service with service "api"' },
- serializedContext: {
- ...buildSerializedContext({
- continuationToken: `child-${kind}`,
- mode: "conversation",
- }),
- "eve.channel": {
- kind: "subagent",
- state: {
- callId: "delegate",
- subagentName: "detector",
- parentContinuationToken: parentToken,
- parentSessionId: parent.runId,
- },
- },
- },
- },
- ]);
- stream = captureTurnEvents(child);
- const yielded = await stream.nextTurn();
- expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
- expect(yielded.at(-1)?.type).toBe("session.waiting");
- nestedRunId = await waitForNewWorkflowToolRun(before);
- await waitForHook({ runId: nestedRunId });
- expect(await parent.status).toBe("running");
- await resumeSessionInbox(
- sessionCommandHookToken(child.runId),
- kind === "session-timeout" ? { kind, ownerRunId: child.runId } : { kind },
- );
- await expect(parent.returnValue).resolves.toMatchObject({
- kind: "runtime-action-result",
- results: [
- {
- callId: "delegate",
- isError: true,
- outcome: {
- kind: "terminal",
- result: { kind: "failed" },
- },
- },
- ],
- });
- await expect(child.returnValue).resolves.toEqual({
- output: "The session ended before the delegated task completed.",
- });
- expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
- } finally {
- stream?.dispose();
- if (nestedRunId !== undefined) {
- const nested = getRun(nestedRunId);
- const status = await nested.status;
- if (status === "pending" || status === "running") await nested.cancel();
- }
- for (const run of [child, parent]) {
- if (run === undefined) continue;
- const status = await run.status;
- if (status === "pending" || status === "running") await run.cancel();
- }
- }
- });
- },
- 30_000,
- );
-
- it("cancels nested work after its owning child has yielded", async () => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "yielded-child-cancel",
- background: true,
- execute: holdUntilAbortedWorkflow,
- toolName: "deploy_service",
- });
- await runtime.run(async () => {
- const before = await listWorkflowToolRunIds();
- const child = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
- continuationToken: "http:yielded-child-cancel",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(child);
- let nestedRunId: string | undefined;
- try {
- const yielded = await stream.nextTurn();
- expect(filterEventsByType(yielded, "turn.completed")).toHaveLength(1);
- expect(yielded.at(-1)?.type).toBe("session.waiting");
- nestedRunId = await waitForNewWorkflowToolRun(before);
- await waitForHook({ runId: nestedRunId });
- expect(await getRun(nestedRunId).status).toBe("running");
-
- await cancelBackgroundAgentTask({
- entry: {
- callId: "delegate",
- toolName: "child",
- lifetime: "session",
- origin: { turnId: "parent-turn", stepIndex: 0 },
- address: { runId: child.runId, hookToken: "unused" },
- task: {
- taskId: "outer-task",
- metadata: { kind: "subagent", name: "child" },
- dispatchContext: { auth: { current: null, initiator: null } },
- },
- },
- session: {
- state: setAgentHandleStore(undefined, {
- handles: [
- {
- phase: "claimed",
- ownerId: "outer-task",
- operationId: "delegate",
- identity: { id: "child", name: "child", nodeId: "subagents/child" },
- address: {
- kind: "agent/local",
- sessionId: child.runId,
- continuationToken: "child",
- },
- },
- ],
- }),
- },
- serializedContext: {},
- });
-
- // The held step can finish only after its abort signal fires or its 60s timer expires.
- // Finishing within 15s proves cancellation reached the yielded child's work.
- expect(await waitForWorkflowToolRunTerminal(nestedRunId)).toBe("completed");
-
- // This input follows the cancellation notification in the child's inbox.
- // It must be the next turn, without a task notification waking the child first.
- await resumeSessionInbox(sessionCommandHookToken(child.runId), {
- kind: "send",
- payload: { message: "Hello again" },
- });
- const next = await stream.nextTurn();
- expect(
- filterEventsByType(next, "message.received").map((event) => event.data.message),
- ).toEqual(["Hello again"]);
- expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);
- } finally {
- stream.dispose();
- if (nestedRunId !== undefined) {
- const nested = getRun(nestedRunId);
- const status = await nested.status;
- if (status === "pending" || status === "running") await nested.cancel();
- }
- await child.cancel();
- }
- });
- }, 30_000);
-
- it("cancels the run when the waiting turn is cancelled and lets the body clean up", async () => {
- vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-tool-cancel",
- execute: holdUntilAbortedWorkflow,
- toolName: "deploy_service",
- });
-
- await runtime.run(async () => {
- const before = await listWorkflowToolRunIds();
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_inline",
- continuationToken: "http:workflow-tool-cancel",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
-
- try {
- const workflowToolRunId = await waitForNewWorkflowToolRun(before);
- await waitForHook({ runId: workflowToolRunId });
- const commandToken = sessionCommandHookToken(run.runId);
- await resumeSessionInbox(commandToken, { kind: "cancel", turnId: "turn_0" });
-
- // The body is holding in a step that received ctx.abortSignal, so the
- // run ends well inside its grace period once the step rejects and
- // `finally` runs; a run that ignored the signal would still be running.
- expect(await waitForWorkflowToolRunTerminal(workflowToolRunId)).toBe("completed");
-
- await resumeSessionInbox(commandToken, {
- kind: "send",
- payload: { message: "Thanks, no deploy today." },
- });
- const next = await stream.nextTurn();
- expect(filterEventsByType(next, "turn.started")).toHaveLength(1);
- expect(filterEventsByType(next, "turn.failed")).toHaveLength(0);
- expect(next.at(-1)?.type).toBe("session.waiting");
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- }, 60_000);
-
- it("runs a session-owned background workflow invocation", async () => {
- vi.stubEnv("VERCEL_DEPLOYMENT_ID", "dpl_inline");
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-tool-background",
- execute: backgroundDeployWorkflow,
- toolName: "report_deploy",
- });
-
- await runtime.run(async () => {
- enableBackgroundTool(runtime, "report_deploy");
-
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run report_deploy with service "api"' },
- serializedContext: buildSerializedContext({
- acceptedDeploymentId: "dpl_inline",
- continuationToken: "http:workflow-tool-background",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
-
- try {
- const receiptTurn = await stream.nextTurn();
- const receipt = filterEventsByType(receiptTurn, "action.result").find(
- (event) => event.data.result.kind === "tool-result",
- );
- expect(receipt?.data.result.output).toMatchObject({ status: "working" });
- expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
-
- const notifications: string[] = [];
- for (
- let turn = 0;
- turn < 3 && !notifications.some((text) => text.includes("is completed"));
- turn += 1
- ) {
- const woken = await stream.nextTurn();
- expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
- notifications.push(eventsText(filterEventsByType(woken, "message.received")));
- }
- const text = notifications.join("\n");
- expect(text).not.toContain("Review plan:api");
- expect(text).not.toContain("update: planned api");
- expect(text).toContain("is completed");
- expect(text).toContain("plan:api");
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- }, 90_000);
-
it("streams a waiting tool's yields as action.partial and settles with its return", async () => {
const runtime = await createWorkflowToolRuntime({
agentName: "workflow-tool-progress",
@@ -992,7 +285,7 @@ describe("workflow tools", () => {
kind: "initial",
ownerDeploymentId: "dpl_inline",
input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
+ serializedContext: buildWorkflowToolSerializedContext({
continuationToken: "http:workflow-tool-progress",
mode: "conversation",
}),
@@ -1016,142 +309,4 @@ describe("workflow tools", () => {
}
});
}, 30_000);
-
- it("wakes the agent with the failure when a background workflow tool throws", async () => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-tool-background-fail",
- execute: failingDeployWorkflow,
- toolName: "deploy_service",
- });
-
- await runtime.run(async () => {
- enableBackgroundTool(runtime, "deploy_service");
-
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run deploy_service with service "api"' },
- serializedContext: buildSerializedContext({
- continuationToken: "http:workflow-tool-background-fail",
- mode: "conversation",
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
-
- try {
- const receiptTurn = await stream.nextTurn();
- expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
-
- const notifications: string[] = [];
- for (
- let turn = 0;
- turn < 3 && !notifications.some((t) => t.includes("failed"));
- turn += 1
- ) {
- const woken = await stream.nextTurn();
- expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
- notifications.push(eventsText(filterEventsByType(woken, "message.received")));
- }
- const text = notifications.join("\n");
- expect(text).toContain("(deploy_service) failed.");
- expect(text).toContain("deploy of api exploded");
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- }, 90_000);
-
- it("lets a background workflow tool ask now and act when answered", async () => {
- const runtime = await createWorkflowToolRuntime({
- agentName: "workflow-tool-background-hitl",
- execute: confirmDeployWorkflow,
- toolName: "confirm_deploy",
- });
-
- await runtime.run(async () => {
- enableBackgroundTool(runtime, "confirm_deploy");
-
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: 'Run confirm_deploy with service "api"' },
- serializedContext: buildSerializedContext({
- continuationToken: "http:workflow-tool-background-hitl",
- mode: "conversation",
- requestInput: true,
- }),
- },
- ]);
- const stream = captureTurnEvents(run);
-
- try {
- const receiptTurn = await stream.nextTurn();
- expect(filterEventsByType(receiptTurn, "turn.failed")).toHaveLength(0);
- expect(
- filterEventsByType(receiptTurn, "action.result").some(
- (event) =>
- event.data.result.kind === "tool-result" &&
- typeof event.data.result.output === "object" &&
- event.data.result.output !== null &&
- "taskId" in event.data.result.output,
- ),
- ).toBe(true);
-
- // The question arrives after the turn that called the tool ended.
- let request: InputRequestedStreamEvent["data"]["requests"][number] | undefined;
- for (let turn = 0; turn < 3 && request === undefined; turn += 1) {
- const woken = await stream.nextTurn();
- expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
- request = (
- filterEventsByType(woken, "input.requested")[0] as InputRequestedStreamEvent | undefined
- )?.data.requests[0];
- }
- expect(request).toMatchObject({ kind: "question", prompt: "Apply plan:api?" });
-
- // The stable session inbox already took the task's wake, so it is
- // known to exist; answer through it directly.
- await resumeSessionInbox(sessionCommandHookToken(run.runId), {
- kind: "send",
- payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },
- });
-
- const notifications: string[] = [];
- for (
- let turn = 0;
- turn < 3 && !notifications.some((text) => text.includes("is completed"));
- turn += 1
- ) {
- const woken = await stream.nextTurn();
- expect(filterEventsByType(woken, "turn.failed")).toHaveLength(0);
- notifications.push(eventsText(filterEventsByType(woken, "message.received")));
- }
- const text = notifications.join("\n");
- expect(text).toContain("is completed");
- expect(text).toContain('\\"approved\\":true');
- } finally {
- stream.dispose();
- await run.cancel();
- }
- });
- }, 90_000);
});
-
-/** Flags one manifest tool as `execution: "background"` under `experimental.tasks`. */
-function enableBackgroundTool(runtime: TestRuntime, toolName: string): void {
- const artifacts = runtime.session.compiledArtifacts;
- if (artifacts === null) throw new Error("expected installed compiled artifacts");
- runtime.session.compiledArtifacts = {
- ...artifacts,
- manifest: {
- ...artifacts.manifest,
- config: artifacts.manifest.config,
- tools: artifacts.manifest.tools.map((tool) =>
- tool.name === toolName ? { ...tool, execution: "background" as const } : tool,
- ),
- },
- };
-}
diff --git a/packages/eve/src/execution/turn-cancellation.integration.test.ts b/packages/eve/src/execution/turn-cancellation.integration.test.ts
index 7bc70171b..510083119 100644
--- a/packages/eve/src/execution/turn-cancellation.integration.test.ts
+++ b/packages/eve/src/execution/turn-cancellation.integration.test.ts
@@ -434,7 +434,6 @@ describe("turn cancellation integration", () => {
});
it.each([
- { turnPolicy: undefined, turnCount: 1 },
{ turnPolicy: "steer" as const, turnCount: 1 },
{ turnPolicy: "queue" as const, turnCount: 2 },
])(
diff --git a/packages/eve/src/internal/testing/app-harness.integration.test.ts b/packages/eve/src/internal/testing/app-harness.integration.test.ts
index b6e8d27d1..f061b7f47 100644
--- a/packages/eve/src/internal/testing/app-harness.integration.test.ts
+++ b/packages/eve/src/internal/testing/app-harness.integration.test.ts
@@ -20,32 +20,7 @@ function buildSerializedContext(overrides: {
};
}
-describe("AppHarness pilot", () => {
- it("runs a task-mode turn end-to-end against an in-memory test runtime", async () => {
- const runtime = await createTestRuntime({ agent: { name: "pilot-agent" } });
-
- const output = await runtime.run(async () => {
- const run = await start(workflowEntry, [
- {
- kind: "initial",
- ownerDeploymentId: "dpl_inline",
- input: { message: "hello pilot harness" },
- serializedContext: buildSerializedContext({
- channelKind: "http",
- continuationToken: "schedule:app-harness-pilot",
- mode: "task",
- }),
- },
- ]);
-
- const result = await run.returnValue;
- return result.output;
- });
-
- expect(typeof output).toBe("string");
- expect(output).toContain("hello pilot harness");
- });
-
+describe("AppHarness", () => {
it("keeps compiled artifacts scoped to the test runtime session", async () => {
const runtime = await createTestRuntime({ agent: { name: "scope-probe" } });
diff --git a/packages/eve/src/internal/testing/app-harness.ts b/packages/eve/src/internal/testing/app-harness.ts
index 6edcb8288..be92c5907 100644
--- a/packages/eve/src/internal/testing/app-harness.ts
+++ b/packages/eve/src/internal/testing/app-harness.ts
@@ -1,7 +1,7 @@
import type { JsonObject } from "#shared/json.js";
import type { ChannelAdapter } from "#channel/adapter.js";
import { compileFromMemory, type CompileFromMemoryInput } from "#compiler/compile-from-memory.js";
-import type { CompiledAgentManifest, CompiledSkillDefinition } from "#compiler/manifest.js";
+import type { CompiledAgentManifest } from "#compiler/manifest.js";
import type { CompiledModuleMap } from "#compiler/module-map.js";
import type { ProgrammaticAgentModule } from "#compiler/source-graph.js";
import type { SessionParent, SessionTurn } from "#context/keys.js";
@@ -54,12 +54,6 @@ export interface TestAppDescriptor {
* `runAsSession` for tool dispatch.
*/
readonly tools?: readonly ResolvedToolDefinition[];
- /**
- * Authored skills projected into the compiled manifest. Use `mockSkill`
- * to describe them declaratively; pass the `.source` field here and
- * forward it on `runAsSession` when the test reads reference files.
- */
- readonly skills?: readonly CompiledSkillDefinition[];
/** Additional authored modules compiled into the synthetic application. */
readonly modules?: readonly ProgrammaticAgentModule[];
}
@@ -102,8 +96,6 @@ export interface TestRuntime {
readonly moduleMap: CompiledModuleMap;
/** Descriptor-declared tools. Exposed for test-side registry wiring. */
readonly tools: readonly ResolvedToolDefinition[];
- /** Descriptor-declared skills. Exposed for test-side registry wiring. */
- readonly skills: readonly CompiledSkillDefinition[];
/**
* Runs `fn` with this app's runtime session active. Compiled-artifact
* reads and bundle-cache writes during `fn` target this scoped session,
@@ -205,27 +197,9 @@ export async function createTestRuntime(descriptor: TestAppDescriptor = {}): Pro
});
}
- if (descriptor.skills !== undefined && descriptor.skills.length > 0) {
- Object.assign(compileInput, {
- skills: descriptor.skills.map((skill) => {
- const entry: { name: string; description: string; markdown?: string } = {
- name: skill.name,
- description: skill.description,
- };
-
- if (skill.markdown !== undefined) {
- entry.markdown = skill.markdown;
- }
-
- return entry;
- }),
- });
- }
-
const { manifest, moduleMap } = await compileFromMemory(compileInput);
const session = createRuntimeSession(descriptor.agent?.name ?? DEFAULT_AGENT_NAME);
const tools = descriptor.tools ?? [];
- const skills = descriptor.skills ?? [];
function install(): void {
installBundledCompiledArtifacts({ manifest, moduleMap });
@@ -283,7 +257,6 @@ export async function createTestRuntime(descriptor: TestAppDescriptor = {}): Pro
run,
runAsSession,
session,
- skills,
tools,
};
}
diff --git a/packages/eve/src/internal/testing/entry-test-helpers.ts b/packages/eve/src/internal/testing/entry-test-helpers.ts
new file mode 100644
index 000000000..d07b827f9
--- /dev/null
+++ b/packages/eve/src/internal/testing/entry-test-helpers.ts
@@ -0,0 +1,220 @@
+import { expect } from "vitest";
+import { getWorld } from "#internal/workflow/runtime.js";
+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";
+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
+import { sessionInboxHookToken } from "#execution/session-inbox/address.js";
+import type { MessageStreamEvent } from "#protocol/message.js";
+import { ConversationContextKey } from "#shared/conversation-context.js";
+
+export function buildSerializedContext(overrides: {
+ acceptedDeploymentId?: string;
+ audience?: "public" | "private" | "unknown";
+ auth?: Record<string, unknown>;
+ channelKind: string;
+ channelState?: Record<string, unknown>;
+ continuationToken?: string;
+ mode: string;
+ parent?: {
+ readonly callId: string;
+ readonly rootSessionId: string;
+ readonly sessionId: string;
+ readonly turn: {
+ readonly id: string;
+ readonly sequence: number;
+ };
+ };
+}): Record<string, unknown> {
+ const channel: { kind: unknown; state: unknown } = {
+ kind: overrides.channelKind,
+ state: overrides.channelState ?? {},
+ };
+ const context: Record<string, unknown> = {
+ "eve.auth": overrides.auth ?? null,
+ "eve.bundle": { source: createBundledRuntimeCompiledArtifactsSource() },
+ "eve.channel": channel,
+ "eve.mode": overrides.mode,
+ };
+ if (overrides.audience !== undefined) {
+ context[ConversationContextKey.name] = {
+ audience: overrides.audience,
+ channel: { kind: overrides.channelKind },
+ environment: "production",
+ mode: overrides.mode,
+ principalType: "anonymous",
+ };
+ }
+ if (overrides.acceptedDeploymentId !== undefined) {
+ context["eve.channelDelivery"] = {
+ acceptedDeploymentId: overrides.acceptedDeploymentId,
+ channelKind: overrides.channelKind,
+ channelName: "test",
+ deliveryId: "delivery-initial",
+ };
+ }
+ if (overrides.continuationToken !== undefined) {
+ context["eve.continuationToken"] = overrides.continuationToken;
+ }
+ if (overrides.parent !== undefined) {
+ context["eve.parentSession"] = overrides.parent;
+ }
+ return context;
+}
+
+export function expectSingleTurn(events: readonly MessageStreamEvent[], turnId: string): void {
+ expect(filterEventsByType(events, "turn.started")).toHaveLength(1);
+ const eventTurnIds = events.flatMap((event) => {
+ if (!("data" in event) || typeof event.data !== "object" || event.data === null) return [];
+ if (!("turnId" in event.data) || typeof event.data.turnId !== "string") return [];
+ return [event.data.turnId];
+ });
+ expect(eventTurnIds.length).toBeGreaterThan(0);
+ expect(new Set(eventTurnIds)).toEqual(new Set([turnId]));
+}
+
+const CALLER_STEP_NAMES = new Set([
+ "bindTurnCallerContextStep",
+ "notifyTurnCallerStep",
+ "resolveInitialTurnCallerStep",
+]);
+
+export async function listCallerStepNames(runId: string): Promise<string[]> {
+ return (await listStepNames(runId)).filter((name) => CALLER_STEP_NAMES.has(name)).sort();
+}
+
+async function listStepNames(runId: string): Promise<string[]> {
+ const world = await getWorld();
+ const steps = await world.steps.list({
+ pagination: { limit: 1_000 },
+ resolveData: "none",
+ runId,
+ });
+ return steps.data.map((step) => step.stepName.split("//").at(-1) ?? "");
+}
+
+export interface CapturedEventStream {
+ dispose(): void;
+ nextUntil(
+ label: string,
+ predicate: (event: MessageStreamEvent) => boolean,
+ ): Promise<MessageStreamEvent[]>;
+}
+
+export function captureEvents(run: Parameters<typeof captureTurnEvents>[0]): CapturedEventStream {
+ const reader = run.readable.getReader();
+ const decoder = new TextDecoder();
+ let buffer = "";
+ let disposed = false;
+
+ return {
+ dispose() {
+ if (disposed) return;
+ disposed = true;
+ reader.releaseLock();
+ },
+ nextUntil(label, predicate) {
+ if (disposed) {
+ return Promise.reject(new Error("CapturedEventStream: stream already disposed."));
+ }
+ return withTimeout(readUntil(reader, decoder, buffer, predicate), label).then((result) => {
+ buffer = result.buffer;
+ return result.events;
+ });
+ },
+ };
+}
+
+async function readUntil(
+ reader: ReadableStreamDefaultReader<Uint8Array>,
+ decoder: InstanceType<typeof TextDecoder>,
+ initialBuffer: string,
+ predicate: (event: MessageStreamEvent) => boolean,
+): Promise<{ buffer: string; events: MessageStreamEvent[] }> {
+ const events: MessageStreamEvent[] = [];
+ let buffer = initialBuffer;
+
+ while (true) {
+ const { done, value } = await reader.read();
+
+ if (done) {
+ throw new Error("Workflow stream closed before reaching the expected event.");
+ }
+
+ buffer += decoder.decode(value, { stream: true });
+
+ for (
+ let newlineIndex = buffer.indexOf("\n");
+ newlineIndex !== -1;
+ newlineIndex = buffer.indexOf("\n")
+ ) {
+ const line = buffer.slice(0, newlineIndex).trim();
+ buffer = buffer.slice(newlineIndex + 1);
+
+ if (line.length === 0) {
+ continue;
+ }
+
+ const event = JSON.parse(line) as MessageStreamEvent;
+ events.push(event);
+
+ if (predicate(event)) {
+ return { buffer, events };
+ }
+ }
+ }
+}
+
+export async function withTimeout<T>(promise: Promise<T>, label: string): Promise<T> {
+ let timeout: ReturnType<typeof setTimeout> | undefined;
+ try {
+ return await Promise.race([
+ promise,
+ new Promise<never>((_resolve, reject) => {
+ timeout = setTimeout(() => {
+ reject(new Error(`Timed out waiting for ${label}.`));
+ }, 30_000);
+ }),
+ ]);
+ } finally {
+ if (timeout !== undefined) {
+ clearTimeout(timeout);
+ }
+ }
+}
+
+export async function expectHookClaims(
+ runId: string,
+ tokens: string[],
+ options: { readonly turnStarted?: boolean } = {},
+): Promise<void> {
+ const events = await (
+ await getWorld()
+ ).events.list({
+ runId,
+ pagination: { limit: 1000 },
+ resolveData: "all",
+ });
+ const claims = events.data.flatMap((event) =>
+ event.eventType === "hook_created" ? [event.eventData.token] : [],
+ );
+ const signals = claims.filter((token) => token.startsWith("abrt_"));
+ expect(signals).toHaveLength(options.turnStarted === false ? 0 : 2);
+ expect(claims.filter((token) => !token.startsWith("abrt_")).sort()).toEqual(
+ tokens.map(sessionInboxHookToken).sort(),
+ );
+}
+
+/** A queued follow-up delivery accepted by `acceptedDeploymentId`. */
+export function handoffFollowUp(acceptedDeploymentId: string, message: string, deliveryId: string) {
+ return {
+ turnPolicy: "queue" as const,
+ auth: null,
+ delivery: {
+ acceptedDeploymentId,
+ channelKind: "http",
+ channelName: "test",
+ deliveryId,
+ },
+ kind: "send" as const,
+ payload: { message },
+ };
+}
diff --git a/packages/eve/src/internal/testing/mocks/mock-skill.integration.test.ts b/packages/eve/src/internal/testing/mocks/mock-skill.integration.test.ts
deleted file mode 100644
index ffd3c4740..000000000
--- a/packages/eve/src/internal/testing/mocks/mock-skill.integration.test.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { access } from "node:fs/promises";
-
-import { describe, expect, it } from "vitest";
-
-import { mockSkill } from "#internal/testing/mocks/mock-skill.js";
-
-describe("mockSkill", () => {
- it("materializes skill package files and cleans them after the test", async ({
- onTestFinished,
- }) => {
- const skill = await mockSkill({
- description: "Weather guidance.",
- name: "weather",
- references: {
- "forecast.md": "Use the latest forecast.",
- },
- });
-
- const source = skill.source;
- expect(source.sourceKind).toBe("skill-package");
-
- if (source.sourceKind !== "skill-package") {
- throw new Error("Expected mock skill source to be a skill package.");
- }
-
- onTestFinished(async () => {
- await expect(access(source.rootPath)).rejects.toMatchObject({ code: "ENOENT" });
- });
- const referencesPath = source.referencesPath;
- expect(referencesPath).toBeDefined();
-
- if (referencesPath === undefined) {
- throw new Error("Expected mock skill to materialize references.");
- }
-
- await expect(access(source.rootPath)).resolves.toBeUndefined();
- await expect(access(source.skillFilePath)).resolves.toBeUndefined();
- await expect(access(referencesPath)).resolves.toBeUndefined();
- });
-
- it("allows explicit cleanup to run more than once", async () => {
- const skill = await mockSkill({ name: "weather", description: "Weather guidance." });
- if (skill.source.sourceKind !== "skill-package") {
- throw new Error("Expected mock skill source to be a skill package.");
- }
- await expect(access(skill.source.rootPath)).resolves.toBeUndefined();
- await skill.cleanup();
- await skill.cleanup();
- await expect(access(skill.source.rootPath)).rejects.toMatchObject({ code: "ENOENT" });
- });
-});
diff --git a/packages/eve/src/internal/testing/mocks/mock-skill.ts b/packages/eve/src/internal/testing/mocks/mock-skill.ts
deleted file mode 100644
index f668e6627..000000000
--- a/packages/eve/src/internal/testing/mocks/mock-skill.ts
+++ /dev/null
@@ -1,204 +0,0 @@
-import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
-
-import { afterEach } from "vitest";
-
-import type { CompiledSkillDefinition } from "#compiler/manifest.js";
-
-/**
- * Declarative description of one synthetic authored skill.
- *
- * Resource files are materialized under one tmpdir and cleaned up through
- * an automatically registered `afterEach`.
- */
-export interface MockSkillInput {
- /** Stable skill name exposed to the runtime. */
- readonly name: string;
- /** Semantic description of the skill. */
- readonly description: string;
- /**
- * Markdown body written to `SKILL.md`. Defaults to the description when
- * omitted.
- */
- readonly markdown?: string;
- /**
- * Reference files keyed by their logical filename (e.g. `catalog.yml`).
- * Pass this to simulate the `references/` subtree of a skill package.
- */
- readonly references?: Readonly<Record<string, string>>;
- /**
- * Script files keyed by logical filename.
- */
- readonly scripts?: Readonly<Record<string, string>>;
- /**
- * Asset files keyed by logical filename.
- */
- readonly assets?: Readonly<Record<string, string>>;
-}
-
-/**
- * A materialized mock skill returned from {@link mockSkill}.
- */
-export interface MockSkill {
- /**
- * Compiled skill definition suitable for AppHarness descriptors.
- */
- readonly source: CompiledSkillDefinition;
- /**
- * Removes any on-disk files written on behalf of this skill.
- *
- * Tests do **not** need to wire this into their own `afterEach` —
- * this module installs an automatic cleanup hook at import time. This
- * method is retained as an escape hatch for tests that want to release
- * the tmpdir mid-body (e.g. to prove the runtime gracefully handles
- * missing reference files). Calling it more than once is safe.
- */
- cleanup(): Promise<void>;
-}
-
-/**
- * Registers an `afterEach` hook that cleans up every {@link MockSkill}
- * materialized during a test. The hook is installed at module import time
- * so it is bound to the file-level suite rather than a nested suite.
- *
- * Using a module-level registration (rather than re-registering per call)
- * keeps the vitest hook list short — vitest reports each `afterEach`
- * registration, so one shared hook avoids cluttering the runner's
- * accounting.
- */
-const pendingMockSkillCleanups: Array<() => Promise<void>> = [];
-
-afterEach(async () => {
- const cleanups = pendingMockSkillCleanups.splice(0);
- await Promise.all(
- cleanups.map(async (cleanup) => {
- try {
- await cleanup();
- } catch {
- // Best-effort cleanup; a leaked tmpdir must not fail the run.
- }
- }),
- );
-});
-
-/**
- * Builds a {@link MockSkill} for the AppHarness.
- *
- * File subtree materialization happens eagerly at construction time so the
- * returned `source.referencesPath` / `scriptsPath` / `assetsPath` are
- * ready before the first test read. Cleanup of any materialized directory
- * runs automatically via this module's `afterEach` hook.
- */
-export async function mockSkill(input: MockSkillInput): Promise<MockSkill> {
- const hasReferences = input.references !== undefined && Object.keys(input.references).length > 0;
- const hasScripts = input.scripts !== undefined && Object.keys(input.scripts).length > 0;
- const hasAssets = input.assets !== undefined && Object.keys(input.assets).length > 0;
-
- let rootPath: string | undefined;
- let referencesPath: string | undefined;
- let scriptsPath: string | undefined;
- let assetsPath: string | undefined;
-
- rootPath = await mkdtemp(join(tmpdir(), `eve-mock-skill-${input.name}-`));
- await writeFile(join(rootPath, "SKILL.md"), input.markdown ?? input.description);
-
- if (hasReferences) {
- referencesPath = join(rootPath, "references");
- await materializeSubtree(referencesPath, input.references ?? {});
- }
-
- if (hasScripts) {
- scriptsPath = join(rootPath, "scripts");
- await materializeSubtree(scriptsPath, input.scripts ?? {});
- }
-
- if (hasAssets) {
- assetsPath = join(rootPath, "assets");
- await materializeSubtree(assetsPath, input.assets ?? {});
- }
-
- const source: CompiledSkillDefinition = buildSkillSource({
- assetsPath,
- description: input.description,
- markdown: input.markdown ?? input.description,
- name: input.name,
- referencesPath,
- rootPath,
- scriptsPath,
- });
-
- let cleanedUp = false;
- const cleanup = async (): Promise<void> => {
- if (cleanedUp) {
- return;
- }
-
- cleanedUp = true;
-
- if (rootPath !== undefined) {
- await rm(rootPath, { force: true, recursive: true });
- }
- };
-
- if (rootPath !== undefined) {
- pendingMockSkillCleanups.push(cleanup);
- }
-
- return {
- cleanup,
- source,
- };
-}
-
-async function materializeSubtree(
- directory: string,
- files: Readonly<Record<string, string>>,
-): Promise<void> {
- await mkdir(directory, { recursive: true });
-
- for (const [name, content] of Object.entries(files)) {
- await writeFile(join(directory, name), content);
- }
-}
-
-function buildSkillSource(input: {
- assetsPath: string | undefined;
- description: string;
- markdown: string;
- name: string;
- referencesPath: string | undefined;
- rootPath: string;
- scriptsPath: string | undefined;
-}): CompiledSkillDefinition {
- const source: CompiledSkillDefinition = {
- description: input.description,
- logicalPath: `skills/${input.name}/SKILL.md`,
- markdown: input.markdown,
- name: input.name,
- owner: { kind: "application" },
- sourceId: `skills/${input.name}/SKILL.md`,
- sourceKind: "skill-package",
- skillId: input.name,
- skillFilePath: join(input.rootPath, "SKILL.md"),
- rootPath: input.rootPath,
- };
-
- const mutable = source as {
- assetsPath?: string;
- referencesPath?: string;
- scriptsPath?: string;
- };
-
- if (input.assetsPath !== undefined) {
- mutable.assetsPath = input.assetsPath;
- }
- if (input.referencesPath !== undefined) {
- mutable.referencesPath = input.referencesPath;
- }
- if (input.scriptsPath !== undefined) {
- mutable.scriptsPath = input.scriptsPath;
- }
-
- return source;
-}
diff --git a/packages/eve/src/internal/testing/task-cancel-notification-workflow.ts b/packages/eve/src/internal/testing/task-cancel-notification-workflow.ts
deleted file mode 100644
index 0a9e2277b..000000000
--- a/packages/eve/src/internal/testing/task-cancel-notification-workflow.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import { createHook, getWorkflowMetadata, sleep } from "#compiled/@workflow/core/index.js";
-
-import { createSessionInbox } from "#execution/session-inbox/inbox.js";
-import { sessionCommandHookToken } from "#execution/session-inbox/address.js";
-import { cancelOwnedTask } from "#execution/tasks/parent/dispatch.js";
-import { waitForCommandHookOwner } from "#execution/workflow-runtime.js";
-import { getRun, start } from "#internal/workflow/runtime.js";
-import type { HarnessSession } from "#harness/types.js";
-import type { BackgroundWorkflowToolRun } from "#harness/workflow-tool-runs.js";
-import type { TaskCommandHookPayload } from "#tasks/types.js";
-
-/** Models a task whose executor cannot finish cooperative cleanup. */
-export async function slowCancelledTaskWorkflow(input: {
- readonly taskId: string;
- readonly taskInboxToken: string;
-}): Promise<void> {
- "use workflow";
-
- using commands = createHook<TaskCommandHookPayload>({ token: input.taskInboxToken });
- const delivery = await commands;
- if (delivery.kind !== "task-command" || delivery.command.kind !== "cancel") {
- throw new Error("Expected the task cancellation command.");
- }
- await sleep("1h");
-}
-
-export async function startSlowCancelledTaskStep(input: {
- readonly sessionId: string;
-}): Promise<BackgroundWorkflowToolRun> {
- "use step";
-
- const taskId = `${input.sessionId}-task`;
- const taskInboxToken = `${input.sessionId}:slow-cancel`;
- const run = await start(slowCancelledTaskWorkflow, [{ taskId, taskInboxToken }]);
- await waitForCommandHookOwner(taskInboxToken);
- return {
- callId: taskId,
- toolName: { kind: "tool", name: "slow-cancel" }.name,
- lifetime: "session" as const,
- origin: { turnId: "turn_0", stepIndex: 0 },
- address: { runId: run.runId, hookToken: taskInboxToken },
- task: {
- dispatchContext: { auth: { current: null, initiator: null } },
- metadata: { kind: "tool", name: "slow-cancel" },
- taskId,
- },
- };
-}
-
-export async function cancelSlowTaskFromParentStep(input: {
- readonly entry: BackgroundWorkflowToolRun;
- readonly sessionId: string;
-}) {
- "use step";
-
- const view = await cancelOwnedTask({
- entry: input.entry,
- session: { sessionId: input.sessionId } as HarnessSession,
- });
- return { view, taskRunStatus: await getRun(input.entry.address.runId).status };
-}
-
-export async function taskCancelNotificationWorkflow() {
- "use workflow";
-
- const { workflowRunId: sessionId } = getWorkflowMetadata();
- const inbox = createSessionInbox(sessionId);
- try {
- await inbox.claimSessionHook(sessionCommandHookToken(sessionId));
- const entry = await startSlowCancelledTaskStep({ sessionId });
- const cancelled = await cancelSlowTaskFromParentStep({ entry, sessionId });
- const notification = await inbox.next();
- if (notification === undefined)
- throw new Error("Session inbox closed before task cancellation.");
- return { ...cancelled, notification };
- } finally {
- await inbox.dispose();
- }
-}
diff --git a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
index 05eedf718..52c8f3bf4 100644
--- a/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
+++ b/packages/eve/src/internal/testing/workflow-tool-fixtures.ts
@@ -150,26 +150,6 @@ export async function failingDeployWorkflow(input: DeployInput): Promise<never>
throw new Error(`deploy of ${input.service} exploded`);
}
-export async function subagentShapedDeployWorkflow(): Promise<{
- readonly callId: string;
- readonly isError: true;
- readonly kind: "subagent-result";
- readonly origin: "dispatch";
- readonly output: string;
- readonly subagentName: string;
-}> {
- "use workflow";
-
- return {
- callId: "authored-call",
- isError: true,
- kind: "subagent-result",
- origin: "dispatch",
- output: "authored payload",
- subagentName: "authored-name",
- };
-}
-
export async function* reportingDeployWorkflow(
input: DeployInput,
): AsyncGenerator<string, { readonly plan: string }> {
@@ -198,16 +178,6 @@ async function planDeployStep(service: string): Promise<string> {
return `plan:${service}`;
}
-export async function stepThenRaceWorkflow(
- input: DeployInput,
-): Promise<{ readonly decided: string; readonly service: string }> {
- "use workflow";
- const gate = createHook<{ readonly optionId: string }>();
- await planDeployStep(input.service);
- const answer = await Promise.race([gate, workflowSleep("50ms")]);
- return { decided: answer === undefined ? "timed out" : "answered", service: input.service };
-}
-
/** Holds in a step until `ctx.abortSignal` fires, then cleans up in `finally`. */
export async function holdUntilAbortedWorkflow(
input: DeployInput,
diff --git a/packages/eve/src/internal/testing/workflow-tool-run-harness.ts b/packages/eve/src/internal/testing/workflow-tool-run-harness.ts
new file mode 100644
index 000000000..8483032b6
--- /dev/null
+++ b/packages/eve/src/internal/testing/workflow-tool-run-harness.ts
@@ -0,0 +1,87 @@
+import { type TestRuntime, createTestRuntime } from "#internal/testing/app-harness.js";
+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";
+import type { ResolvedToolDefinition } from "#runtime/types.js";
+import { serializeInputSchema, toInputSchema } from "#tools/schema.js";
+import { getRun } from "#internal/workflow/runtime.js";
+import {
+ type BlockingWorkflowToolDefinition,
+ defineWorkflowTool,
+} from "#tools/workflow-definition.js";
+
+const DEPLOY_INPUT_SCHEMA = toInputSchema({
+ additionalProperties: false,
+ properties: { service: { type: "string" } },
+ required: ["service"],
+ type: "object",
+});
+
+export function buildWorkflowToolSerializedContext(input: {
+ readonly acceptedDeploymentId?: string;
+ readonly continuationToken: string;
+ readonly mode: "conversation" | "task";
+ readonly requestInput?: boolean;
+}): Record<string, unknown> {
+ return {
+ "eve.auth": null,
+ "eve.bundle": { source: createBundledRuntimeCompiledArtifactsSource() },
+ "eve.capabilities": { requestInput: input.requestInput ?? false },
+ "eve.channel": { kind: "http", state: {} },
+ ...(input.acceptedDeploymentId === undefined
+ ? {}
+ : {
+ "eve.channelDelivery": {
+ acceptedDeploymentId: input.acceptedDeploymentId,
+ channelKind: "http",
+ channelName: "test",
+ deliveryId: "delivery-initial",
+ },
+ }),
+ "eve.continuationToken": input.continuationToken,
+ "eve.mode": input.mode,
+ };
+}
+
+/**
+ * Registers one fixture workflow as an authored tool. The fixture module
+ * passed through the test tier's client transform, so `execute` is the stub
+ * the real pipeline produces: a function carrying its `workflowId`.
+ */
+export async function createWorkflowToolRuntime(input: {
+ readonly agentName: string;
+ readonly background?: boolean;
+ readonly execute: (...args: never[]) => unknown;
+ readonly inputSchema?: ResolvedToolDefinition["inputSchema"];
+ readonly toolName: string;
+}): Promise<TestRuntime> {
+ return await createTestRuntime({
+ agent: { name: input.agentName },
+ modules: [
+ {
+ logicalPath: `tools/${input.toolName}.ts`,
+ loadNamespace: async () => ({
+ default: defineWorkflowTool({
+ execution: input.background === true ? "background" : undefined,
+ description: `Deploys a service (${input.toolName}).`,
+ execute: input.execute as BlockingWorkflowToolDefinition["execute"],
+ inputSchema: serializeInputSchema(input.inputSchema ?? DEPLOY_INPUT_SCHEMA) ?? {},
+ }),
+ }),
+ },
+ ],
+ });
+}
+
+/** Polls one run until it reaches a terminal status, returning that status. */
+export async function waitForWorkflowToolRunTerminal(
+ runId: string,
+ timeout = 15_000,
+): Promise<string> {
+ const terminal = new Set(["completed", "failed", "cancelled"]);
+ const deadline = Date.now() + timeout;
+ while (Date.now() < deadline) {
+ const status = await getRun(runId).status;
+ if (terminal.has(status)) return status;
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ }
+ throw new Error(`Timed out waiting for run ${runId} to reach a terminal status.`);
+}