Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 80 additions & 20 deletions apps/server/src/orchestration-v2/Adapters/AntigravityAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2569,8 +2569,8 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
}
return startResult;
}),
loadSession: () => Effect.succeed(startResult),
resumeSession: () => Effect.succeed(startResult),
loadSession: (sessionId) => Effect.succeed({ ...startResult, sessionId }),
resumeSession: (sessionId) => Effect.succeed({ ...startResult, sessionId }),
forkSession: () => Effect.succeed(startResult),
listSessions: () => Effect.succeed({ sessions: [] }),
closeSession: () => Effect.succeed({}),
Expand Down Expand Up @@ -2678,6 +2678,7 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
ordinal: number,
overrides?: {
readonly text?: string;
readonly inlineContextLength?: number;
readonly delegated?: boolean;
readonly continuation?: boolean;
},
Expand All @@ -2693,6 +2694,9 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
});
return {
...turn,
...(overrides?.inlineContextLength === undefined
? {}
: { inlineContextLength: overrides.inlineContextLength }),
runId: RunId.make(`run-suggestion-${ordinal}`),
runOrdinal: ordinal,
providerTurnOrdinal: ordinal,
Expand Down Expand Up @@ -2749,7 +2753,11 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
}),
});

const turn1 = makeTurn(1, { text: "Add a subtract function." });
const handoffPrefix = "Earlier handoff summary\n\n";
const turn1 = makeTurn(1, {
text: `${handoffPrefix}Add a subtract function.`,
inlineContextLength: handoffPrefix.length,
});
yield* session.startTurn(turn1);
const events1 = yield* until((event) => event.type === "prompt_suggestion.updated");
const suggestionEvent = events1.find((event) => event.type === "prompt_suggestion.updated");
Expand All @@ -2770,11 +2778,22 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
},
]);

yield* Ref.set(enabled, false);
const turn2 = makeTurn(2, { text: "Now add divide." });
// Later turns still strip the earlier handoff turn's inline context prefix.
const turn2 = makeTurn(2, { text: "Now add multiply." });
yield* session.startTurn(turn2);
yield* until((event) => event.type === "prompt_suggestion.updated");
assert.deepEqual(sampled[1]?.messages, [
{ role: "user", text: "Add a subtract function." },
{ role: "assistant", text: "Added subtract in math.ts." },
{ role: "user", text: "Now add multiply." },
{ role: "assistant", text: "Added subtract in math.ts." },
]);

yield* Ref.set(enabled, false);
const turn3 = makeTurn(3, { text: "Now add divide." });
yield* session.startTurn(turn3);
yield* until((event) => event.type === "turn.terminal");
assert.equal(sampled.length, 1);
assert.equal(sampled.length, 2);
}).pipe(Effect.provide(layerSession)),
);

Expand All @@ -2801,20 +2820,24 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
);

it.effect(
"cancels an in-flight suggestion on next turn, rollback, forced session replacement, or admitted continuation",
"cancels an in-flight suggestion on next turn, rollback, forced session replacement, resume, snapshot switch, or admitted continuation",
() =>
Effect.gen(function* () {
const started = [
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
] as const;
const interrupted = [
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
yield* Deferred.make<void>(),
] as const;
const continuationOffered =
yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>();
Expand All @@ -2836,7 +2859,7 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
Effect.gen(function* () {
const index = callCount;
callCount += 1;
if (index < 4) {
if (index < 6) {
yield* Deferred.succeed(started[index]!, undefined);
return yield* Effect.never.pipe(
Effect.onInterrupt(() =>
Expand Down Expand Up @@ -2888,12 +2911,49 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
});
yield* Deferred.await(interrupted[2]);

// 4. Admitting a late continuation cancels the fourth in-flight suggestion and suppresses
// sampling while the continuation remains pending.
// 4. Resuming a different native session cancels the fourth in-flight suggestion.
yield* session.startTurn(makeTurn(4, { text: "Fourth turn" }));
yield* until((event) => event.type === "turn.terminal");
yield* Deferred.await(started[3]);

const otherProviderThread = {
...providerThread,
nativeThreadRef: {
driver: providerThread.driver,
nativeId: "mock-other-session",
strength: "strong" as const,
},
};
yield* session.resumeThread({
providerThread: otherProviderThread,
modelSelection,
});
yield* Deferred.await(interrupted[3]);
yield* session.resumeThread({
providerThread,
modelSelection,
});

// 5. Reading a snapshot for a different native session cancels the fifth in-flight suggestion.
yield* session.startTurn(makeTurn(5, { text: "Fifth turn" }));
yield* until((event) => event.type === "turn.terminal");
yield* Deferred.await(started[4]);

yield* session.readThreadSnapshot({
providerThread: otherProviderThread,
});
yield* Deferred.await(interrupted[4]);
yield* session.resumeThread({
providerThread,
modelSelection,
});

// 6. Admitting a late continuation cancels the sixth in-flight suggestion and suppresses
// sampling while the continuation remains pending.
yield* session.startTurn(makeTurn(6, { text: "Sixth turn" }));
yield* until((event) => event.type === "turn.terminal");
yield* Deferred.await(started[5]);

yield* emitSessionUpdate({
sessionId: "mock-suggestion-session",
update: {
Expand All @@ -2902,25 +2962,25 @@ describe("AntigravityAdapterV2 prompt suggestions", () => {
},
});
const offeredRequest = yield* Deferred.await(continuationOffered);
yield* Deferred.await(interrupted[3]);
yield* Deferred.await(interrupted[5]);

yield* session.startTurn(makeTurn(5, { text: "Fifth turn while continuation pending" }));
yield* session.startTurn(makeTurn(7, { text: "Seventh turn while continuation pending" }));
yield* until((event) => event.type === "turn.terminal");
assert.equal(callCount, 4);
assert.equal(callCount, 6);

// 5. Once the pending continuation clears, a subsequent normal turn emits its suggestion.
// 7. Once the pending continuation clears, a subsequent normal turn emits its suggestion.
yield* offeredRequest.clearIfCurrent?.() ?? Effect.void;
const turn6 = makeTurn(6, { text: "Sixth turn" });
yield* session.startTurn(turn6);
const events6 = yield* until((event) => event.type === "prompt_suggestion.updated");
const turn8 = makeTurn(8, { text: "Eighth turn" });
yield* session.startTurn(turn8);
const events8 = yield* until((event) => event.type === "prompt_suggestion.updated");
assert.deepEqual(
events6.filter((event) => event.type === "prompt_suggestion.updated"),
events8.filter((event) => event.type === "prompt_suggestion.updated"),
[
{
type: "prompt_suggestion.updated",
driver: "antigravity",
threadId: turn6.threadId,
runId: turn6.runId,
threadId: turn8.threadId,
runId: turn8.runId,
text: "run the test suite",
},
],
Expand Down
71 changes: 45 additions & 26 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,8 @@ interface SnapshotMessageState {
readonly messages: Map<string, OrchestrationV2ConversationMessage>;
/** Messages loaded from thought chunks; `messages` stores them as assistant text. */
readonly reasoningIds: ReadonlySet<OrchestrationV2ConversationMessage["id"]>;
/** Leading handoff context length per stored user message, stripped for prompt suggestions. */
readonly inlineContextLengths: Map<string, number>;
loadingRole: "user" | "assistant" | "thought" | null;
loadingMessageId: string | null;
loadingIndex: number;
Expand All @@ -1996,6 +1998,7 @@ const emptySnapshotState = (): SnapshotMessageState => ({
order: [],
messages: new Map(),
reasoningIds: new Set(),
inlineContextLengths: new Map(),
loadingRole: null,
loadingMessageId: null,
loadingIndex: 0,
Expand Down Expand Up @@ -2650,10 +2653,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
) {
continue;
}
const inlineContextLength =
state.inlineContextLengths.get(key) ??
(message.id === context.input.message.messageId
? context.input.inlineContextLength
: undefined);
const rawText =
message.id === context.input.message.messageId &&
context.input.inlineContextLength !== undefined
? message.text.slice(context.input.inlineContextLength)
inlineContextLength !== undefined
? message.text.slice(inlineContextLength)
: message.text;
const text = rawText.trim();
if (text.length === 0) continue;
Expand Down Expand Up @@ -3205,11 +3212,19 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
: undefined;
});

const rememberSnapshotMessage = (message: OrchestrationV2ConversationMessage) =>
const rememberSnapshotMessage = (
message: OrchestrationV2ConversationMessage,
inlineContextLength?: number,
) =>
Ref.update(snapshot, (current) => {
const key = String(message.id);
if (!current.messages.has(key)) current.order.push(key);
current.messages.set(key, message);
if (inlineContextLength !== undefined && inlineContextLength > 0) {
current.inlineContextLengths.set(key, inlineContextLength);
} else {
current.inlineContextLengths.delete(key);
}
return current;
});

Expand Down Expand Up @@ -8837,8 +8852,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
) {
const sessionId = yield* Ref.get(activeSessionId);
if (sessionId !== null) {
offeredContinuation = true;
yield* offerContinuationRun(sessionId);
offeredContinuation = yield* offerContinuationRun(sessionId);
}
}
if (settledStatus !== "completed") {
Expand Down Expand Up @@ -9619,26 +9633,29 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
driver,
providerThread: activeProviderThread,
});
yield* rememberSnapshotMessage({
createdBy: turnInput.message.createdBy,
creationSource: turnInput.message.creationSource,
...(turnInput.message.scheduledTaskId === undefined
? {}
: { scheduledTaskId: turnInput.message.scheduledTaskId }),
...(turnInput.message.senderThreadId === undefined
? {}
: { senderThreadId: turnInput.message.senderThreadId }),
id: turnInput.message.messageId,
threadId: turnInput.threadId,
runId: turnInput.runId,
nodeId: turnInput.rootNodeId,
role: "user",
text: turnInput.message.text,
attachments: [...turnInput.message.attachments],
streaming: false,
createdAt: startedAt,
updatedAt: startedAt,
});
yield* rememberSnapshotMessage(
{
createdBy: turnInput.message.createdBy,
creationSource: turnInput.message.creationSource,
...(turnInput.message.scheduledTaskId === undefined
? {}
: { scheduledTaskId: turnInput.message.scheduledTaskId }),
...(turnInput.message.senderThreadId === undefined
? {}
: { senderThreadId: turnInput.message.senderThreadId }),
id: turnInput.message.messageId,
threadId: turnInput.threadId,
runId: turnInput.runId,
nodeId: turnInput.rootNodeId,
role: "user",
text: turnInput.message.text,
attachments: [...turnInput.message.attachments],
streaming: false,
createdAt: startedAt,
updatedAt: startedAt,
},
turnInput.inlineContextLength,
);
if (followsAgentWorkState)
yield* runtimeCallbackPermit.withPermit(syncNativeChildren(context));
const attached = turnAttached;
Expand Down Expand Up @@ -10102,6 +10119,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
threadInput.providerThread.appThreadId,
).pipe(Effect.tapError(() => restorePreviousItemIdentity));
if ((yield* Ref.get(activeSessionId)) !== sessionId || restartAfterInterrupt) {
yield* cancelPromptSuggestion();
yield* Ref.set(snapshot, emptySnapshotState());
const activated = yield* activateSession(
sessionId,
Expand Down Expand Up @@ -10642,6 +10660,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
detail: "ACP driver does not support session/load snapshots",
});
}
yield* cancelPromptSuggestion();
yield* Ref.set(snapshot, emptySnapshotState());
const mcpContext = yield* readMcpContext(
snapshotInput.providerThread.appThreadId,
Expand Down
Loading