Skip to content
Merged
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
1 change: 1 addition & 0 deletions apps/server/src/auth/RpcAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const RPC_REQUIRED_SCOPES = {
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: AuthOrchestrationReadScope,
[WS_METHODS.projectsMutate]: AuthOrchestrationOperateScope,
[WS_METHODS.serverProbe]: AuthOrchestrationReadScope,
[WS_METHODS.serverGetConfig]: AuthOrchestrationReadScope,
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/observability/RpcInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const RPC_AGGREGATES = {
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeThread]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.subscribePromptSuggestion]: "orchestrationV2",
[WS_METHODS.projectsMutate]: "orchestration",
[WS_METHODS.serverProbe]: "server",
[WS_METHODS.serverGetConfig]: "server",
Expand Down
140 changes: 140 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
readonly freshQueueOnReopen?: boolean;
readonly forkSession?: ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerShape["forkSession"];
readonly allocateSessionId?: Effect.Effect<string>;
readonly promptSuggestions?: boolean;
}) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down Expand Up @@ -3329,6 +3330,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
providerSessionId: ProviderSessionId.make("provider-session-claude-wake"),
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
promptSuggestions: Effect.succeed(options?.promptSuggestions === true),
});
const providerThread = yield* runtime.ensureThread({
threadId,
Expand Down Expand Up @@ -7864,6 +7866,101 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);

it.effect.each(["root", "mcp", "subagent", "remote"] as const)(
"asks Claude for suggestions only on threads a person drives (%s)",
(lineage) =>
Effect.gen(function* () {
const harness = yield* makeWakeHarnessWithOptions({ promptSuggestions: true });
const base = makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now: yield* DateTime.now,
attemptId: RunAttemptId.make("attempt-prompt-suggestion"),
text: "Add a subtract function.",
attachments: [],
});
const parentThreadId = ThreadId.make("thread-claude-suggestion-parent");
// MCP-launched root threads carry `delegatedFrom: null` at runtime.
const offered = lineage === "root" || lineage === "mcp";
const input =
lineage === "root"
? base
: lineage === "mcp"
? {
...base,
appThread: {
...base.appThread,
delegatedFrom: null as unknown as undefined,
},
}
: lineage === "subagent"
? {
...base,
appThread: {
...base.appThread,
lineage: {
parentThreadId,
relationshipToParent: "subagent" as const,
rootThreadId: parentThreadId,
},
},
}
: {
...base,
appThread: {
...base.appThread,
delegatedFrom: {
environmentId: EnvironmentId.make("environment-claude-suggestion-parent"),
threadId: parentThreadId,
title: "Parent",
},
},
};
yield* harness.runtime.startTurn(input);
yield* Queue.offer(
harness.sdkMessages,
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000690", result: "Done." }),
);
yield* Queue.take(harness.terminalReceipts);
// Delegated threads have no one typing, so the SDK is never asked for suggestions.
assert.equal(harness.getOpenedOptions()?.promptSuggestions, offered);
if (!offered) return;
yield* Queue.offer(
harness.sdkMessages,
claudeSdkFrame({
type: "prompt_suggestion",
suggestion: " add tests for subtract\n",
uuid: "00000000-0000-4000-8000-000000000691",
session_id: WAKE_NATIVE_SESSION,
}),
);
yield* harness.until(() =>
harness.events.some((event) => event.type === "prompt_suggestion.updated"),
);
assert.deepEqual(
harness.events.filter((event) => event.type === "prompt_suggestion.updated"),
[
{
type: "prompt_suggestion.updated",
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
threadId: harness.threadId,
runId: input.runId,
text: "add tests for subtract",
},
],
);
}).pipe(
Effect.provide(
Layer.mergeAll(
IdAllocator.layer,
McpProviderSessions.layer,
NodeServices.layer,
NodeCrypto.layer,
),
),
),
);

it.effect("merges cumulative stream usage into the message_start counts", () =>
Effect.gen(function* () {
const harness = yield* makeWakeHarnessWithOptions({
Expand Down Expand Up @@ -12452,6 +12549,49 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);
assert.isFalse(yield* harness.hasPendingBackgroundWork);

// The continuation settled with no result of its own, so a late suggestion belongs to B.
yield* harness.offerAndWait(
claudeSdkFrame({
type: "prompt_suggestion",
suggestion: "Check the build logs",
uuid: "00000000-0000-4000-8000-000000000692",
session_id: WAKE_NATIVE_SESSION,
}),
);
const userTurn = makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: harness.providerThread,
now,
attemptId: RunAttemptId.make("attempt-claude-wake-4d"),
text: "Ship it.",
attachments: [],
providerTurnOrdinal: 4,
});
yield* harness.runtime.startTurn(userTurn);
yield* Queue.offer(
harness.sdkMessages,
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000693", result: "Shipped." }),
);
yield* harness.awaitTerminal(4);
yield* Queue.offer(
harness.sdkMessages,
claudeSdkFrame({
type: "prompt_suggestion",
suggestion: "Open the release notes",
uuid: "00000000-0000-4000-8000-000000000694",
session_id: WAKE_NATIVE_SESSION,
}),
);
yield* harness.until(() =>
harness.events.some((event) => event.type === "prompt_suggestion.updated"),
);
assert.deepEqual(
harness.events.flatMap((event) =>
event.type === "prompt_suggestion.updated" ? [[event.runId, event.text]] : [],
),
[[userTurn.runId, "Open the release notes"]],
);
}).pipe(
Effect.provide(
Layer.mergeAll(
Expand Down
28 changes: 28 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import {
type RunId,
type ThreadId,
resolveIdleCompaction,
isDelegatedAppThread,
} from "@t3tools/contracts";

import { FILE_HEADERS_ONLY, formatPatch, structuredPatch } from "diff";
Expand Down Expand Up @@ -1758,6 +1759,7 @@ export function makeClaudeQueryOptions(input: {
readonly cwd: string | null;
readonly attachmentsDir?: string;
readonly settings?: ClaudeSettings;
readonly promptSuggestions?: boolean;
readonly sdkSettings?: string | ClaudeSdkSettings;
readonly environment?: NodeJS.ProcessEnv;
readonly mcpServers?: ClaudeQueryOptions["mcpServers"];
Expand Down Expand Up @@ -1842,6 +1844,7 @@ export function makeClaudeQueryOptions(input: {
? "bypassPermissions"
: (input.permissionMode ?? "default")),
includePartialMessages: true,
promptSuggestions: input.promptSuggestions === true,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
...(compiledSelection.effort === undefined
? {}
: {
Expand Down Expand Up @@ -5102,6 +5105,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
}),
openSession: Effect.fn("ClaudeAdapterV2.openSession")(
function* (input: ProviderAdapter.ProviderAdapterV2OpenSessionInput) {
const promptSuggestionsEnabled = input.promptSuggestions ?? Effect.succeed(false);
const sessionScope = yield* Effect.scope;
const now = yield* DateTime.now;
const session = providerSession({
Expand Down Expand Up @@ -8821,6 +8825,27 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
}

const message = input.message;
if (message.type === "prompt_suggestion") {
// Arrives after `result`; offer it only while the completed turn is still the latest.
// A continuation can settle from buffered frames first, so its suggestion may be a user turn's.
const settled = settledTurnByNativeThread.get(liveQuery.nativeThreadId);
const text = message.suggestion.trim();
if (
settled !== undefined &&
!isClaudeProviderContinuationTurn(settled.input) &&
text.length > 0 &&
(yield* Ref.get(activeTurn)) === null
) {
yield* emitProviderEvent({
type: "prompt_suggestion.updated",
driver: CLAUDE_PROVIDER,
threadId: settled.input.threadId,
runId: settled.input.runId,
text,
});
}
return;
}
// Track before routing: idle-wake Monitors report tasks before replaying their tool calls.
yield* trackClaudeMonitorCalls(message);
// Update before routing because the idle wake gate reads this.
Expand Down Expand Up @@ -10924,6 +10949,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
cwd: turnInput.runtimePolicy.cwd,
attachmentsDir,
settings: adapterOptions.settings,
// Delegated threads have no one typing, so they never ask for suggestions.
promptSuggestions:
!isDelegatedAppThread(turnInput.appThread) && (yield* promptSuggestionsEnabled),
...(launchArgSettings === undefined
? {}
: { sdkSettings: claudeSdkLaunchArgSettings(launchArgSettings) }),
Expand Down
Loading
Loading