Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): clear pending auth after callback (#225)

Allen Zhou598b5e0ff962
diff --git a/.changeset/fresh-melons-explain.md b/.changeset/fresh-melons-explain.md
new file mode 100644
index 000000000..524edc1a7
--- /dev/null
+++ b/.changeset/fresh-melons-explain.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Clear pending connection/tool authorization state after a matching callback resumes a session, so Slack threads do not keep waiting for already-completed auth and swallow follow-up messages.
diff --git a/packages/eve/src/execution/workflow-steps.test.ts b/packages/eve/src/execution/workflow-steps.test.ts
index 9cbbf7143..90287e84d 100644
--- a/packages/eve/src/execution/workflow-steps.test.ts
+++ b/packages/eve/src/execution/workflow-steps.test.ts
@@ -8,6 +8,7 @@ import { AuthKey, ContinuationTokenKey, ModeKey, SessionIdKey } from "#context/k
import { BundleKey, ChannelKey } from "#runtime/sessions/runtime-context-keys.js";
import { serializeContext } from "#context/serialize.js";
import { setPendingRuntimeActionBatch } from "#harness/runtime-actions.js";
+import { getPendingAuthorization, setPendingAuthorization } from "#harness/authorization.js";
import type { HarnessSession, StepResult } from "#harness/types.js";
import { createEmptyHookRegistry } from "#runtime/hooks/registry.js";
import { getCompiledRuntimeAgentBundle } from "#runtime/sessions/compiled-agent-cache.js";
@@ -592,6 +593,81 @@ describe("turnStep", () => {
}),
});
});
+
+ it("clears pending authorization after a matching callback resumes the turn", async () => {
+ const challenge = {
+ challenge: {
+ instructions: "Sign in to continue",
+ url: "https://idp.example/authorize",
+ },
+ hookUrl: "https://app.example/eve/v1/connections/statuspage/callback/sess-test:auth",
+ name: "statuspage",
+ resume: { nonce: "n1" },
+ };
+ const session = createStubSession({
+ state: setPendingAuthorization({ retained: "yes" }, { challenges: [challenge] }),
+ });
+ installSessionStoreMocks([session]);
+ vi.mocked(getCompiledRuntimeAgentBundle).mockResolvedValue({
+ adapterRegistry: {
+ adaptersByKind: new Map([[threadContextAdapter.kind, threadContextAdapter]]),
+ },
+ compiledArtifactsSource: {} as never,
+ graph: {
+ nodesByNodeId: new Map(),
+ root: {
+ sandboxRegistry: { sandbox: null },
+ turnAgent: TestTurnAgent,
+ },
+ },
+ moduleMap: { nodes: {} },
+ hookRegistry: createEmptyHookRegistry(),
+ resolvedAgent: { config: {} },
+ subagentRegistry: {},
+ toolRegistry: {},
+ turnAgent: TestTurnAgent,
+ } as never);
+
+ let observedPendingAuth: unknown;
+ let observedStepInput: unknown = "not-called";
+ vi.mocked(createExecutionNodeStep).mockImplementation(() => {
+ return async (session, stepInput): Promise<StepResult> => {
+ observedPendingAuth = getPendingAuthorization(session.state);
+ observedStepInput = stepInput;
+ return { next: null, session };
+ };
+ });
+
+ const result = await turnStep({
+ input: {
+ kind: "deliver",
+ payloads: [
+ {
+ authorizationCallback: {
+ callback: { code: "oauth-code" },
+ connectionName: "statuspage",
+ },
+ },
+ ],
+ },
+ parentWritable: createTestWritable(),
+ serializedContext: createSerializedContext(),
+ sessionState: createStubSessionState(),
+ });
+
+ expect(observedPendingAuth).toBeUndefined();
+ expect(observedStepInput).toBeUndefined();
+ expect(result).toMatchObject({
+ action: "park",
+ hasPendingAuthorization: false,
+ });
+ if (result.action === "park") {
+ expect(result.authorizationNames).toBeUndefined();
+ }
+ const persistedSession = vi.mocked(createDurableSessionState).mock.calls.at(-1)?.[0].session;
+ expect(persistedSession?.state?.retained).toBe("yes");
+ expect(getPendingAuthorization(persistedSession?.state)).toBeUndefined();
+ });
});
describe("emitTerminalSessionFailureStep", () => {
diff --git a/packages/eve/src/execution/workflow-steps.ts b/packages/eve/src/execution/workflow-steps.ts
index 682e7287e..61cc92b83 100644
--- a/packages/eve/src/execution/workflow-steps.ts
+++ b/packages/eve/src/execution/workflow-steps.ts
@@ -38,6 +38,7 @@ import {
} from "#protocol/message.js";
import {
CallbackBaseUrlKey,
+ clearPendingAuthorization,
getPendingAuthorization,
PendingAuthorizationResultKey,
type AuthorizationResult,
@@ -119,17 +120,10 @@ export async function turnStep(rawInput: TurnStepInput): Promise<DurableStepResu
}),
);
- const durableSession = await readDurableSession(input.sessionState);
+ let durableSession = await readDurableSession(input.sessionState);
const ctx = await deserializeContext(input.serializedContext);
const adapter = ctx.require(ChannelKey);
const bundle = ctx.require(BundleKey);
- const initialSession = hydrateDurableSession({
- compactionOverrides: {
- thresholdPercent: bundle.resolvedAgent.config.compaction?.thresholdPercent,
- },
- durable: durableSession,
- turnAgent: bundle.turnAgent,
- });
// Populate the callback base URL so getHookUrl() works during
// tool execution. Reads from workflow metadata (available in steps).
@@ -179,6 +173,13 @@ export async function turnStep(rawInput: TurnStepInput): Promise<DurableStepResu
}
if (authResults.length > 0) {
ctx.set(PendingAuthorizationResultKey, authResults);
+ durableSession = {
+ ...durableSession,
+ state: clearPendingAuthorization(
+ durableSession.state,
+ authResults.map((result) => result.name),
+ ),
+ };
completedAuths = completed;
input =
remainingPayloads.length > 0
@@ -193,6 +194,14 @@ export async function turnStep(rawInput: TurnStepInput): Promise<DurableStepResu
ctx.set(AuthKey, input.input.auth ?? null);
}
+ const initialSession = hydrateDurableSession({
+ compactionOverrides: {
+ thresholdPercent: bundle.resolvedAgent.config.compaction?.thresholdPercent,
+ },
+ durable: durableSession,
+ turnAgent: bundle.turnAgent,
+ });
+
const adapterCtx = buildAdapterContext(adapter, ctx);
// Run the adapter's deliver hook for each queued payload and
diff --git a/packages/eve/src/harness/authorization.ts b/packages/eve/src/harness/authorization.ts
index 1ea919001..76bdd41f9 100644
--- a/packages/eve/src/harness/authorization.ts
+++ b/packages/eve/src/harness/authorization.ts
@@ -243,6 +243,37 @@ export function setPendingAuthorization(
return { ...sessionState, [PENDING_AUTHORIZATION_KEY]: value };
}
+export function clearPendingAuthorization(
+ sessionState: Record<string, unknown> | undefined,
+ names?: readonly string[],
+): Record<string, unknown> | undefined {
+ if (sessionState === undefined || sessionState[PENDING_AUTHORIZATION_KEY] === undefined) {
+ return sessionState;
+ }
+
+ if (names !== undefined) {
+ if (names.length === 0) return sessionState;
+
+ const pending = getPendingAuthorization(sessionState);
+ if (pending !== undefined) {
+ const completedNames = new Set(names);
+ const challenges = pending.challenges.filter(
+ (challenge) => !completedNames.has(challenge.name),
+ );
+ if (challenges.length > 0) {
+ return {
+ ...sessionState,
+ [PENDING_AUTHORIZATION_KEY]: { challenges },
+ };
+ }
+ }
+ }
+
+ const state = { ...sessionState };
+ delete state[PENDING_AUTHORIZATION_KEY];
+ return Object.keys(state).length > 0 ? state : undefined;
+}
+
export function getPendingAuthorization(
sessionState: Record<string, unknown> | undefined,
): PendingAuthorizationState | undefined {