fix(server): dismiss native questions when their turn ends (#9851)
This commit is contained in:
1 parent
03c6cd8bad
commit
d7fe47fd09
2 files changed
+235
-1
No files matched your search
@@ -20,6 +20,7 @@ import {
|
||||
type OrchestrationCommand,
|
||||
ProjectId,
|
||||
ProviderItemId,
|
||||
RuntimeRequestId,
|
||||
type ServerSettings,
|
||||
ThreadId,
|
||||
TurnId,
|
||||
@@ -30,6 +31,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as ManagedRuntime from "effect/ManagedRuntime";
|
||||
import * as Option from "effect/Option";
|
||||
import * as PubSub from "effect/PubSub";
|
||||
import * as Scope from "effect/Scope";
|
||||
import * as Stream from "effect/Stream";
|
||||
@@ -372,6 +374,12 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
engine,
|
||||
dispatch,
|
||||
readModel: () => testRuntime.runPromise(snapshotQuery.getSnapshot()),
|
||||
readThreadShell: () =>
|
||||
testRuntime.runPromise(
|
||||
snapshotQuery
|
||||
.getThreadShellById(asThreadId("thread-1"))
|
||||
.pipe(Effect.map(Option.getOrThrow)),
|
||||
),
|
||||
emit: provider.emit,
|
||||
emitAndDrain,
|
||||
sqlCount: sqlCounter.count,
|
||||
@@ -2346,6 +2354,185 @@ describe("ProviderRuntimeIngestion", () => {
|
||||
expect(message?.streaming).toBe(false);
|
||||
});
|
||||
|
||||
function userInputEvent(
|
||||
turnId: string,
|
||||
requestId: string,
|
||||
responseMode?: "message",
|
||||
): ProviderRuntimeEvent {
|
||||
return {
|
||||
type: "user-input.requested",
|
||||
eventId: asEventId(`requested:${requestId}`),
|
||||
provider: ProviderDriverKind.make("codex"),
|
||||
threadId: asThreadId("thread-1"),
|
||||
turnId: asTurnId(turnId),
|
||||
requestId: RuntimeRequestId.make(requestId),
|
||||
createdAt: "2026-01-01T00:00:01.000Z",
|
||||
payload: {
|
||||
...(responseMode ? { responseMode } : {}),
|
||||
questions: ["first", "second"].map((id) => ({
|
||||
id,
|
||||
header: id,
|
||||
question: `Choose ${id}`,
|
||||
options: [{ label: "yes", description: "Continue" }],
|
||||
multiSelect: false,
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
it.each(["completed", "interrupted", "failed", "aborted"] as const)(
|
||||
"resolves native questions when their turn is %s",
|
||||
async (state) => {
|
||||
const harness = await createHarness();
|
||||
const request = userInputEvent("question-turn", "question-request");
|
||||
await harness.emitAndDrain([
|
||||
{
|
||||
type: "turn.started",
|
||||
eventId: asEventId("question-started"),
|
||||
provider: request.provider,
|
||||
threadId: request.threadId,
|
||||
turnId: request.turnId,
|
||||
createdAt: request.createdAt,
|
||||
},
|
||||
request,
|
||||
]);
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true);
|
||||
if (state === "interrupted") {
|
||||
await harness.dispatch({
|
||||
type: "thread.turn.interrupt",
|
||||
commandId: CommandId.make("question-interrupt"),
|
||||
threadId: request.threadId,
|
||||
createdAt: "2026-01-01T00:00:02.000Z",
|
||||
});
|
||||
}
|
||||
const completed: ProviderRuntimeEvent = {
|
||||
...(state === "aborted"
|
||||
? ({ type: "turn.aborted", payload: { reason: "Interrupted by user." } } as const)
|
||||
: ({ type: "turn.completed", payload: { state } } as const)),
|
||||
eventId: asEventId("question-completed"),
|
||||
provider: request.provider,
|
||||
threadId: request.threadId,
|
||||
turnId: request.turnId,
|
||||
createdAt: "2026-01-01T00:00:03.000Z",
|
||||
};
|
||||
await harness.emitAndDrain([completed]);
|
||||
const thread = (await harness.readModel()).threads[0]!;
|
||||
expect(thread.session?.activeTurnId).toBeNull();
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false);
|
||||
expect(
|
||||
thread.activities.filter((activity) => activity.kind === "user-input.resolved"),
|
||||
).toMatchObject([{ turnId: request.turnId, payload: { requestId: request.requestId } }]);
|
||||
|
||||
await harness.emitAndDrain([completed]);
|
||||
expect(
|
||||
(await harness.readModel()).threads[0]!.activities.filter(
|
||||
(activity) => activity.kind === "user-input.resolved",
|
||||
),
|
||||
).toHaveLength(1);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a stale request dismissed when its turn later completes", async () => {
|
||||
const harness = await createHarness();
|
||||
const request = userInputEvent("stale-turn", "stale-question");
|
||||
await harness.emitAndDrain([request]);
|
||||
await harness.dispatch({
|
||||
type: "thread.activity.append",
|
||||
commandId: CommandId.make("stale-question-response"),
|
||||
threadId: request.threadId,
|
||||
activity: {
|
||||
id: asEventId("stale-question-failed"),
|
||||
kind: "provider.user-input.respond.failed",
|
||||
tone: "error",
|
||||
summary: "User input response failed",
|
||||
turnId: request.turnId ?? null,
|
||||
payload: {
|
||||
requestId: request.requestId,
|
||||
detail: "Unknown pending user input request",
|
||||
},
|
||||
createdAt: "2026-01-01T00:00:02.000Z",
|
||||
},
|
||||
createdAt: "2026-01-01T00:00:02.000Z",
|
||||
});
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false);
|
||||
await harness.emitAndDrain([
|
||||
{
|
||||
type: "turn.completed",
|
||||
eventId: asEventId("stale-turn-completed"),
|
||||
provider: request.provider,
|
||||
threadId: request.threadId,
|
||||
turnId: request.turnId,
|
||||
createdAt: "2026-01-01T00:00:03.000Z",
|
||||
payload: { state: "completed" },
|
||||
},
|
||||
]);
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false);
|
||||
expect(
|
||||
(await harness.readModel()).threads[0]!.activities.filter(
|
||||
(activity) => activity.kind === "user-input.resolved",
|
||||
),
|
||||
).toMatchObject([{ payload: { requestId: request.requestId } }]);
|
||||
});
|
||||
|
||||
it("preserves answered questions and leaves newer, child and async questions pending", async () => {
|
||||
const harness = await createHarness();
|
||||
const answered = userInputEvent("old-turn", "answered-question");
|
||||
const unresolved = userInputEvent("old-turn", "old-question");
|
||||
const newer = userInputEvent("new-turn", "new-question");
|
||||
const child = userInputEvent("child-turn", "child-question");
|
||||
const asynchronous = userInputEvent("old-turn", "async-question", "message");
|
||||
const answer: ProviderRuntimeEvent = {
|
||||
type: "user-input.resolved",
|
||||
eventId: asEventId("normal-answer"),
|
||||
provider: answered.provider,
|
||||
threadId: answered.threadId,
|
||||
turnId: answered.turnId,
|
||||
requestId: answered.requestId,
|
||||
createdAt: "2026-01-01T00:00:02.000Z",
|
||||
payload: { answers: { first: "yes", second: "yes" } },
|
||||
};
|
||||
await harness.emitAndDrain([
|
||||
answered,
|
||||
unresolved,
|
||||
newer,
|
||||
child,
|
||||
asynchronous,
|
||||
answer,
|
||||
{
|
||||
type: "turn.started",
|
||||
eventId: asEventId("new-turn-started"),
|
||||
provider: newer.provider,
|
||||
threadId: newer.threadId,
|
||||
turnId: newer.turnId,
|
||||
createdAt: "2026-01-01T00:00:03.000Z",
|
||||
},
|
||||
]);
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true);
|
||||
await harness.emitAndDrain([
|
||||
{
|
||||
type: "turn.completed",
|
||||
eventId: asEventId("old-turn-completed"),
|
||||
provider: answered.provider,
|
||||
threadId: answered.threadId,
|
||||
turnId: answered.turnId,
|
||||
createdAt: "2026-01-01T00:00:04.000Z",
|
||||
payload: { state: "interrupted" },
|
||||
},
|
||||
]);
|
||||
const thread = (await harness.readModel()).threads[0]!;
|
||||
expect(thread.session?.activeTurnId).toBe(newer.turnId);
|
||||
expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true);
|
||||
expect(
|
||||
thread.activities.filter((activity) => activity.kind === "user-input.resolved"),
|
||||
).toMatchObject([
|
||||
{
|
||||
id: answer.eventId,
|
||||
payload: { requestId: answered.requestId, answers: answer.payload.answers },
|
||||
},
|
||||
{ turnId: unresolved.turnId, payload: { requestId: unresolved.requestId } },
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps streaming while an async question is pending", async () => {
|
||||
const harness = await createHarness({ serverSettings: { enableLegacyTokenStreaming: true } });
|
||||
const base = {
|
||||
|
||||
@@ -36,6 +36,8 @@ import { formatTokens } from "@t3tools/shared/usageFormat";
|
||||
import { ProviderService } from "../../provider/Services/ProviderService.ts";
|
||||
import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts";
|
||||
import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts";
|
||||
import { ProjectionThreadActivityRepository } from "../../persistence/Services/ProjectionThreadActivities.ts";
|
||||
import { ProjectionThreadActivityRepositoryLive } from "../../persistence/Layers/ProjectionThreadActivities.ts";
|
||||
import * as CheckpointStore from "../../checkpointing/CheckpointStore.ts";
|
||||
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
|
||||
import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts";
|
||||
@@ -955,6 +957,7 @@ const make = Effect.gen(function* () {
|
||||
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
|
||||
const providerService = yield* ProviderService;
|
||||
const projectionTurnRepository = yield* ProjectionTurnRepository;
|
||||
const projectionThreadActivityRepository = yield* ProjectionThreadActivityRepository;
|
||||
const serverSettingsService = yield* ServerSettingsService;
|
||||
const checkpointStore = yield* CheckpointStore.CheckpointStore;
|
||||
const providerCommandId = (event: ProviderRuntimeEvent, tag: string) =>
|
||||
@@ -1931,6 +1934,47 @@ const make = Effect.gen(function* () {
|
||||
const proposedPlans = detailedThread?.proposedPlans ?? [];
|
||||
const turnId = toTurnId(event.turnId);
|
||||
if (turnId) {
|
||||
const userInputActivities =
|
||||
yield* projectionThreadActivityRepository.listUserInputLifecycleByThreadId({
|
||||
threadId: thread.id,
|
||||
});
|
||||
const pendingRequestIds = new Set<string>();
|
||||
for (const activity of userInputActivities) {
|
||||
const payload =
|
||||
typeof activity.payload === "object" && activity.payload !== null
|
||||
? (activity.payload as Record<string, unknown>)
|
||||
: null;
|
||||
const requestId = payload?.requestId;
|
||||
if (typeof requestId !== "string") continue;
|
||||
if (
|
||||
activity.kind === "user-input.requested" &&
|
||||
activity.turnId === turnId &&
|
||||
payload?.responseMode !== "message"
|
||||
) {
|
||||
pendingRequestIds.add(requestId);
|
||||
} else if (activity.kind === "user-input.resolved") {
|
||||
pendingRequestIds.delete(requestId);
|
||||
}
|
||||
}
|
||||
// A terminal turn cannot accept native callback answers. Message-mode
|
||||
// questions may outlive that turn and still accept a later user message.
|
||||
for (const requestId of pendingRequestIds) {
|
||||
yield* orchestrationEngine.dispatch({
|
||||
type: "thread.activity.append",
|
||||
commandId: yield* providerCommandId(event, "terminal-user-input-resolved"),
|
||||
threadId: thread.id,
|
||||
activity: {
|
||||
id: EventId.make(`${event.eventId}:user-input-resolved:${requestId}`),
|
||||
createdAt: now,
|
||||
tone: "info",
|
||||
kind: "user-input.resolved",
|
||||
summary: "User input dismissed",
|
||||
payload: { requestId },
|
||||
turnId,
|
||||
},
|
||||
createdAt: now,
|
||||
});
|
||||
}
|
||||
const assistantMessageIds = yield* getAssistantMessageIdsForTurn(thread.id, turnId);
|
||||
yield* Effect.forEach(
|
||||
assistantMessageIds,
|
||||
@@ -2229,4 +2273,7 @@ const make = Effect.gen(function* () {
|
||||
export const ProviderRuntimeIngestionLive = Layer.effect(
|
||||
ProviderRuntimeIngestionService,
|
||||
make,
|
||||
).pipe(Layer.provide(ProjectionTurnRepositoryLive));
|
||||
).pipe(
|
||||
Layer.provide(ProjectionTurnRepositoryLive),
|
||||
Layer.provide(ProjectionThreadActivityRepositoryLive),
|
||||
);
|
||||
Reference in new issue
Block a user