Skip to content

Commit 90a65c3

Browse files
committed
fix(server): cited assistant quotes reach providers expanded again
The orchestrator v2 rewrite dropped the citation expansion that ran in ProviderService.sendTurn, so turn starts, steers, and context handoffs sent raw t3-citation:// links to providers. Provider-bound user text now goes through one helper that projects composer context and expands citations, and a message whose quotes expand past the 120,000-character provider limit fails its run with a validation error instead of being sent. Stored messages keep their clickable links. Fixes #16327
1 parent 8f75697 commit 90a65c3

7 files changed

Lines changed: 334 additions & 14 deletions

‎apps/server/src/orchestration-v2/ContextHandoffBudget.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
OrchestrationV2ProviderThread,
99
OrchestrationV2TurnItem,
1010
} from "@t3tools/contracts";
11+
import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations";
1112

1213
import * as Config from "effect/Config";
1314

@@ -142,7 +143,10 @@ export function historicalMessage(
142143
): OrchestrationV2HistoricalMessage | null {
143144
let text: string;
144145
switch (item.type) {
146+
// History is provider input; the stored message keeps its clickable citations.
145147
case "user_message":
148+
text = expandAssistantCitationsForProvider(item.text);
149+
break;
146150
case "assistant_message":
147151
text = item.text;
148152
break;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
type OrchestrationMessageContext,
3+
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
4+
} from "@t3tools/contracts";
5+
import { expandAssistantCitationsForProvider } from "@t3tools/shared/assistantCitations";
6+
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
7+
import * as Effect from "effect/Effect";
8+
import * as Schema from "effect/Schema";
9+
10+
class ProviderInputTooLongError extends Schema.TaggedError<ProviderInputTooLongError>()(
11+
"ProviderInputTooLongError",
12+
{ inputLength: Schema.Number },
13+
) {
14+
override get message(): string {
15+
const excess = this.inputLength - PROVIDER_SEND_TURN_MAX_INPUT_CHARS;
16+
return `Message is ${excess.toLocaleString("en-US")} ${excess === 1 ? "character" : "characters"} over the ${PROVIDER_SEND_TURN_MAX_INPUT_CHARS.toLocaleString("en-US")}-character limit once assistant quotes are expanded. Shorten it or quote less.`;
17+
}
18+
}
19+
20+
/**
21+
* The text a provider receives for a persisted user message: composer context references
22+
* become their envelope and assistant citations carry their saved quotes. The stored message
23+
* keeps its links. A message that fit as links can exceed the provider input limit once its
24+
* quotes expand, so the expanded text is checked again, as the composer does before sending.
25+
*/
26+
export const providerUserMessageText = (message: {
27+
readonly text: string;
28+
readonly context?: OrchestrationMessageContext | undefined;
29+
}): Effect.Effect<string, ProviderInputTooLongError> => {
30+
const projected = projectComposerContextForProvider({
31+
text: message.text,
32+
records: message.context?.records ?? [],
33+
});
34+
const text = expandAssistantCitationsForProvider(projected);
35+
return text !== projected && text.length > PROVIDER_SEND_TURN_MAX_INPUT_CHARS
36+
? Effect.fail(new ProviderInputTooLongError({ inputLength: text.length }))
37+
: Effect.succeed(text);
38+
};

‎apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { assert, it } from "@effect/vitest";
22
import {
3+
EnvironmentId,
4+
MessageId,
35
type ModelSelection,
46
NodeId,
7+
type OrchestrationV2ConversationMessage,
58
type OrchestrationV2ProviderThread,
9+
type OrchestrationV2Run,
610
type OrchestrationV2ThreadProjection,
711
ProjectId,
812
ProviderDriverKind,
@@ -14,6 +18,10 @@ import {
1418
RunId,
1519
ThreadId,
1620
} from "@t3tools/contracts";
21+
import {
22+
expandAssistantCitationsForProvider,
23+
serializeAssistantCitation,
24+
} from "@t3tools/shared/assistantCitations";
1725
import * as DateTime from "effect/DateTime";
1826
import * as Effect from "effect/Effect";
1927
import * as Exit from "effect/Exit";
@@ -323,3 +331,109 @@ it.effect(
323331
assert.equal(interrupted?.nativeThreadRef?.nativeId, "native-thread:restart-session");
324332
}),
325333
);
334+
335+
it.effect("steers with cited assistant quotes expanded for the provider", () =>
336+
Effect.gen(function* () {
337+
const now = yield* DateTime.now;
338+
const threadId = ThreadId.make("thread:steer-citation");
339+
const providerSessionId = ProviderSessionId.make("provider-session:steer-citation");
340+
const providerTurnId = ProviderTurnId.make("provider-turn:steer-citation");
341+
const attemptId = RunAttemptId.make("run-attempt:steer-citation");
342+
const providerThread: OrchestrationV2ProviderThread = {
343+
id: ProviderThreadId.make("provider-thread:steer-citation"),
344+
driver,
345+
providerInstanceId,
346+
providerSessionId,
347+
appThreadId: threadId,
348+
ownerNodeId: null,
349+
nativeThreadRef: { driver, nativeId: "native-thread:steer-citation", strength: "strong" },
350+
nativeConversationHeadRef: null,
351+
status: "idle",
352+
firstRunOrdinal: 1,
353+
lastRunOrdinal: 1,
354+
handoffIds: [],
355+
forkedFrom: null,
356+
createdAt: now,
357+
updatedAt: now,
358+
};
359+
const projection = makeProjection({ now, threadId, providerThread, providerTurnId, attemptId });
360+
const text = `Revisit ${serializeAssistantCitation({
361+
version: 1,
362+
environmentId: EnvironmentId.make("environment:steer-citation"),
363+
threadId,
364+
messageId: MessageId.make("message:steer-citation:source"),
365+
text: "Ship it on Friday.",
366+
start: 0,
367+
end: 18,
368+
prefix: "",
369+
suffix: "",
370+
})}`;
371+
const message = {
372+
id: MessageId.make("message:steer-citation"),
373+
text,
374+
attachments: [],
375+
createdBy: "user",
376+
creationSource: "web",
377+
} as unknown as OrchestrationV2ConversationMessage;
378+
const steered = yield* Ref.make<ReadonlyArray<string>>([]);
379+
const runtime: ProviderAdapterV2SessionRuntime = {
380+
instanceId: providerInstanceId,
381+
driver,
382+
providerSessionId,
383+
providerSession: {
384+
id: providerSessionId,
385+
driver,
386+
providerInstanceId,
387+
status: "running",
388+
cwd: "/workspace",
389+
model: modelSelection.model,
390+
capabilities: CodexProviderCapabilitiesV2,
391+
createdAt: now,
392+
updatedAt: now,
393+
lastError: null,
394+
},
395+
events: Stream.empty,
396+
ensureThread: () => Effect.die("unused ensureThread"),
397+
resumeThread: () => Effect.die("unused resumeThread"),
398+
startTurn: () => Effect.die("unused startTurn"),
399+
steerTurn: (input) => Ref.update(steered, (texts) => [...texts, input.message.text]),
400+
interruptTurn: () => Effect.die("unused interruptTurn"),
401+
respondToRuntimeRequest: () => Effect.die("unused respondToRuntimeRequest"),
402+
readThreadSnapshot: () => Effect.die("unused readThreadSnapshot"),
403+
rollbackThread: () => Effect.die("unused rollbackThread"),
404+
forkThread: () => Effect.die("unused forkThread"),
405+
};
406+
const layerControl = ProviderTurnControlService.layer.pipe(
407+
Layer.provide(
408+
Layer.merge(
409+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
410+
getProviderControlContext: () =>
411+
Effect.succeed({
412+
providerThread,
413+
providerTurn: projection.providerTurns[0],
414+
attempt: projection.attempts[0],
415+
message,
416+
run: { id: RunId.make("run:steer-citation") } as OrchestrationV2Run,
417+
}),
418+
}),
419+
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
420+
get: () => Effect.succeedSome(runtime),
421+
}),
422+
),
423+
),
424+
);
425+
426+
yield* Effect.gen(function* () {
427+
const control = yield* ProviderTurnControlService.ProviderTurnControlServiceV2;
428+
yield* control.steer({
429+
threadId,
430+
providerSessionId,
431+
providerThreadId: providerThread.id,
432+
providerTurnId,
433+
messageId: message.id,
434+
});
435+
}).pipe(Effect.provide(layerControl));
436+
437+
assert.deepEqual(yield* Ref.get(steered), [expandAssistantCitationsForProvider(text)]);
438+
}),
439+
);

‎apps/server/src/orchestration-v2/ProviderTurnControlService.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
21
import {
32
MessageId,
43
ProviderSessionId,
@@ -14,6 +13,7 @@ import * as Option from "effect/Option";
1413
import * as Schema from "effect/Schema";
1514

1615
import * as ProjectionStore from "./ProjectionStore.ts";
16+
import { providerUserMessageText } from "./ProviderMessageText.ts";
1717
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
1818

1919
const yieldToRuntime = Effect.yieldNow.pipe(
@@ -293,6 +293,7 @@ export const layer: Layer.Layer<
293293
cause: "The persisted steering message or target run is missing.",
294294
});
295295
}
296+
const text = yield* providerUserMessageText(message);
296297
yield* loaded.session.value
297298
.steerTurn({
298299
threadId: input.threadId,
@@ -301,10 +302,7 @@ export const layer: Layer.Layer<
301302
providerTurnId: loaded.providerTurn.id,
302303
message: {
303304
messageId: message.id,
304-
text: projectComposerContextForProvider({
305-
text: message.text,
306-
records: message.context?.records ?? [],
307-
}),
305+
text,
308306
attachments: message.attachments,
309307
createdBy: message.createdBy,
310308
creationSource: message.creationSource,

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect, it, vi } from "vite-plus/test";
22
import { it as effectIt } from "@effect/vitest";
33
import {
44
CheckpointScopeId,
5+
EnvironmentId,
56
MessageId,
67
NodeId,
78
ProviderSessionId,
@@ -15,7 +16,12 @@ import {
1516
ProjectId,
1617
type OrchestrationV2ThreadProjection,
1718
OrchestrationV2DomainEvent,
19+
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
1820
} from "@t3tools/contracts";
21+
import {
22+
expandAssistantCitationsForProvider,
23+
serializeAssistantCitation,
24+
} from "@t3tools/shared/assistantCitations";
1925
import * as Cause from "effect/Cause";
2026
import * as DateTime from "effect/DateTime";
2127
import * as Effect from "effect/Effect";
@@ -855,3 +861,41 @@ for (const previousMessages of [[], ["/compact", " /COMPACT "]]) {
855861
}),
856862
);
857863
}
864+
865+
effectIt.effect("fails a run whose expanded assistant quotes exceed the provider input limit", () =>
866+
Effect.gen(function* () {
867+
const citation = serializeAssistantCitation({
868+
version: 1,
869+
environmentId: EnvironmentId.make("environment-citation"),
870+
threadId: ThreadId.make("thread-citation-source"),
871+
messageId: MessageId.make("message-citation-source"),
872+
text: "Quoted claim",
873+
start: 0,
874+
end: 12,
875+
prefix: "",
876+
suffix: "",
877+
});
878+
// Fits as a link, which the composer allows, but not once the quote expands.
879+
const text = `${"x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS - citation.length - 1)} ${citation}`;
880+
expect(text.length).toBe(PROVIDER_SEND_TURN_MAX_INPUT_CHARS);
881+
expect(expandAssistantCitationsForProvider(text).length).toBeGreaterThan(
882+
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
883+
);
884+
const harness = makeLocalCommandHarness({ text });
885+
886+
yield* harness.start;
887+
888+
expect(harness.open).not.toHaveBeenCalled();
889+
expect(harness.startRootRun).not.toHaveBeenCalled();
890+
const projection = harness.projection();
891+
expect(projection.runs.at(-1)?.status).toBe("failed");
892+
expect(projection.messages.at(-1)?.text).toBe(text);
893+
expect(projection.turnItems).toMatchObject([
894+
{
895+
type: "error",
896+
title: "Message is too long",
897+
failure: { class: "validation_error" },
898+
},
899+
]);
900+
}),
901+
);

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { modelSelectionsEqual } from "@t3tools/shared/model";
2-
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
32
import {
43
CommandId,
54
latestProviderTurnForAttempt,
@@ -46,6 +45,7 @@ import * as IdAllocator from "./IdAllocator.ts";
4645
import * as ProjectionStore from "./ProjectionStore.ts";
4746
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
4847
import { makeProviderFailure } from "./ProviderFailure.ts";
48+
import { providerUserMessageText } from "./ProviderMessageText.ts";
4949
import * as RunExecutionService from "./RunExecutionService.ts";
5050
import * as RuntimePolicy from "./RuntimePolicy.ts";
5151
import {
@@ -369,6 +369,26 @@ export const layer: Layer.Layer<
369369
});
370370
},
371371
);
372+
const providerText = yield* Effect.result(providerUserMessageText(message));
373+
if (providerText._tag === "Failure") {
374+
yield* settleRunBeforeStart({
375+
signal: "provider-input-too-long",
376+
status: "failed",
377+
now: yield* DateTime.now,
378+
providerInstanceId: run.providerInstanceId,
379+
itemProviderThreadId: providerThread.id,
380+
item: {
381+
type: "error",
382+
title: "Message is too long",
383+
failure: makeProviderFailure({
384+
class: "validation_error",
385+
message: providerText.failure.message,
386+
}),
387+
},
388+
});
389+
return;
390+
}
391+
const userText = providerText.success;
372392
if (message.attachments.length === 0 && message.text.trimStart().startsWith("/")) {
373393
const isEmptyCompaction =
374394
message.text.trim().toLowerCase() === "/compact" && !projection.hasConversation;
@@ -402,10 +422,7 @@ export const layer: Layer.Layer<
402422
: yield* Effect.result(
403423
providerAuth.tryHandlePromptCommand({
404424
instanceId: authInstanceId,
405-
text: projectComposerContextForProvider({
406-
text: message.text,
407-
records: message.context?.records ?? [],
408-
}),
425+
text: userText,
409426
hasAttachments: false,
410427
}),
411428
);
@@ -944,10 +961,6 @@ export const layer: Layer.Layer<
944961
const routableSubagents = projection.subagents.filter((subagent) =>
945962
RunExecutionService.canRouteRelatedSubagent(subagent.status),
946963
);
947-
const userText = projectComposerContextForProvider({
948-
text: message.text,
949-
records: message.context?.records ?? [],
950-
});
951964
// Delivered once: this run's provider turn marks the work as told. A
952965
// restart continuation is prompted by its own text or resumes natively.
953966
const noteContinuation = isRestartNoteContinuation(

0 commit comments

Comments
 (0)