Skip to content

Commit f24876a

Browse files
committed
feat(composer): skip predictions the agent will answer itself, edit the prompt in Labs
Two thirds of predictions on live data followed a turn that ended while the agent waited for its own tests, builds or subagents; the agent carried on by itself, so they read a long thread from cache for nothing or suggested "ok, waiting". A turn whose thread still has work the agent will be woken by now gets no prediction. The prompt now treats a question anywhere in the last message, or a closing plan of what the assistant will do once approved, as waiting on the user, and rules out acknowledgments that ask for nothing. The prompt can be edited under Settings → Labs once reply suggestions are on; an empty setting keeps the built-in prompt, which moves to contracts so the editor can show it.
1 parent e5a1e8f commit f24876a

8 files changed

Lines changed: 153 additions & 34 deletions

File tree

‎apps/server/src/composerSuggestion/ComposerSuggestionService.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION,
23
OrchestrationV2SuggestComposerReplyError,
34
type OrchestrationV2SuggestComposerReplyResult,
45
type RunId,
@@ -21,7 +22,6 @@ import { forkParked } from "../serverActivation.ts";
2122
import * as ServerSettings from "../serverSettings.ts";
2223
import { composerSuggestionForkMissedCache, composerSuggestionTarget } from "./decide.ts";
2324
import { cleanComposerSuggestion } from "./filter.ts";
24-
import { COMPOSER_SUGGESTION_INSTRUCTION } from "./instruction.ts";
2525
import { makeSuggestionCache, PREDICTION_CEILING_MS } from "./suggestionCache.ts";
2626

2727
/**
@@ -84,7 +84,15 @@ const make = Effect.gen(function* () {
8484
["messages", "providerTurns", "providerThreads"],
8585
{ messageRoles: ["assistant"] },
8686
);
87-
const target = composerSuggestionTarget(records, input.runId, nowMs);
87+
const shell = yield* projections.getThreadShell(input.threadId);
88+
const target = composerSuggestionTarget(
89+
{
90+
...records,
91+
thread: { ...records.thread, pendingBackgroundTasks: shell?.pendingBackgroundTasks ?? [] },
92+
},
93+
input.runId,
94+
nowMs,
95+
);
8896
if (target === null) return "";
8997
const providerThread = records.providerThreads.find(
9098
(candidate) => candidate.id === target.providerThreadId,
@@ -97,7 +105,9 @@ const make = Effect.gen(function* () {
97105
const result = yield* suggestReply.call(adapter, {
98106
threadId: input.threadId,
99107
providerThread,
100-
instruction: COMPOSER_SUGGESTION_INSTRUCTION,
108+
instruction:
109+
(yield* settings.getSettings).composerReplySuggestionInstruction ||
110+
DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION,
101111
});
102112
if (composerSuggestionForkMissedCache(result.cachedInputTokens, target.promptTokens)) {
103113
yield* Ref.update(pausedUntil, (current) =>

‎apps/server/src/composerSuggestion/decide.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const records = (
5454
archivedAt: null,
5555
activeProviderThreadId: providerThreadId,
5656
lineage: { relationshipToParent: null },
57+
pendingBackgroundTasks: [],
5758
...over.thread,
5859
},
5960
messages: over.messages ?? [userMessage, reply()],
@@ -88,6 +89,17 @@ describe("composerSuggestionTarget", () => {
8889
expect(target({ thread: { lineage: { relationshipToParent: "subagent" } } })).toBeNull();
8990
});
9091

92+
it("stays silent while the agent waits for work it will be woken by", () => {
93+
expect(target({ thread: { pendingBackgroundTasks: [{ kind: "subagent" }] } })).toBeNull();
94+
expect(
95+
target({ thread: { pendingBackgroundTasks: [{ kind: "command", awaited: true }] } }),
96+
).toBeNull();
97+
// A dev server left running is not something the agent waits on.
98+
expect(target({ thread: { pendingBackgroundTasks: [{ kind: "command" }] } })?.reply.id).toBe(
99+
"message-reply",
100+
);
101+
});
102+
91103
it("needs the thread's live native session", () => {
92104
expect(target({ providerThreads: [] })).toBeNull();
93105
expect(

‎apps/server/src/composerSuggestion/decide.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { MessageId, ProviderThreadId, RunId } from "@t3tools/contracts";
1+
import type {
2+
MessageId,
3+
OrchestrationV2PendingBackgroundTask,
4+
ProviderThreadId,
5+
RunId,
6+
} from "@t3tools/contracts";
7+
import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
28

39
/** Only the fields a suggestion decision reads; the projections carry more. */
410
export interface ComposerSuggestionMessage {
@@ -32,6 +38,10 @@ export interface ComposerSuggestionRecords {
3238
readonly archivedAt: unknown;
3339
readonly activeProviderThreadId: ProviderThreadId | null;
3440
readonly lineage: { readonly relationshipToParent: string | null };
41+
/** Work the agent started and will be woken by, from the thread shell. */
42+
readonly pendingBackgroundTasks: ReadonlyArray<
43+
Pick<OrchestrationV2PendingBackgroundTask, "kind" | "awaited">
44+
>;
3545
};
3646
readonly messages: ReadonlyArray<ComposerSuggestionMessage>;
3747
readonly providerTurns: ReadonlyArray<ComposerSuggestionTurn>;
@@ -99,7 +109,8 @@ export const composerSuggestionForkMissedCache = (
99109

100110
/**
101111
* The turn a suggestion would follow, or null when predicting it is pointless or unsafe: no
102-
* assistant reply, no live native session, or a cache that is cold or unreported.
112+
* assistant reply, an agent that will carry on by itself once its own background work reports,
113+
* no live native session, or a cache that is cold or unreported.
103114
*/
104115
export function composerSuggestionTarget(
105116
records: ComposerSuggestionRecords,
@@ -113,6 +124,7 @@ export function composerSuggestionTarget(
113124
} | null {
114125
const thread = records.thread;
115126
if (thread.archivedAt !== null || thread.lineage.relationshipToParent === "subagent") return null;
127+
if (backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks)) return null;
116128
const reply = lastAssistantReplyOfRun(records.messages, runId);
117129
if (reply === undefined) return null;
118130
const providerThread = records.providerThreads.find(

‎apps/server/src/composerSuggestion/instruction.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎apps/web/src/components/settings/LabsSettingsPanel.tsx‎

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
import { useAuth } from "@clerk/react";
22
import { Link } from "@tanstack/react-router";
3-
import { managedCapabilityDenial } from "@t3tools/contracts";
3+
import {
4+
DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION,
5+
managedCapabilityDenial,
6+
} from "@t3tools/contracts";
7+
import { useRef } from "react";
48

59
import { hasCloudPublicConfig } from "~/cloud/publicConfig";
610
import { useT3ConnectAuthPrompt } from "../clerk/useT3ConnectAuthPrompt";
711
import { Button } from "../ui/button";
812
import { Switch } from "../ui/switch";
13+
import { Textarea } from "../ui/textarea";
914
import { ScopedSwitch } from "./ScopedSwitch";
10-
import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
15+
import {
16+
SettingResetButton,
17+
SettingsPageContainer,
18+
SettingsRow,
19+
SettingsSection,
20+
} from "./settingsLayout";
1121
import { searchableSetting } from "./settingsSearch";
1222
import { LegacyFeaturesSection } from "./SettingsPanels";
13-
import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings";
23+
import {
24+
useScopedSettings,
25+
useScopedSettingsMixed,
26+
useUpdateScopedSettings,
27+
} from "./useScopedSettings";
1428
import { useOptionalSettingsScope } from "./SettingsScopeContext";
1529
import { firstManagedDenial, usePrimaryManagedPolicy } from "~/state/managedPolicy";
1630

@@ -25,6 +39,7 @@ export function LabsSettingsPanel() {
2539
{hasCloudPublicConfig() ? <GuestLinksRowWithSignIn /> : <GuestLinksRow />}
2640
<LinkedEnvironmentsRow />
2741
<ComposerReplySuggestionsRow />
42+
<ComposerReplySuggestionInstructionRow />
2843
<SimlockHostsRow />
2944
</SettingsSection>
3045
<LegacyFeaturesSection />
@@ -173,7 +188,7 @@ function ComposerReplySuggestionsRow() {
173188
serverScoped
174189
{...searchableSetting("composer-reply-suggestions")}
175190
settingKeys={["composerReplySuggestions"]}
176-
description="After a Claude turn finishes, predict your next message and show it as ghost text in an empty composer: Tab or Right arrow fills the draft, Esc hides it. The prediction reuses the finished turn's prompt cache, so it pays only the new text, and it never runs once that cache is gone."
191+
description="After a Claude turn finishes, predict your next message and show it as ghost text in an empty composer: Tab or Right arrow fills the draft, Esc hides it. The prediction reads the thread from the finished turn's prompt cache, so it never runs once that cache is gone."
177192
control={
178193
<ScopedSwitch
179194
settingKeys={["composerReplySuggestions"]}
@@ -188,6 +203,55 @@ function ComposerReplySuggestionsRow() {
188203
);
189204
}
190205

206+
function ComposerReplySuggestionInstructionRow() {
207+
const settings = useScopedSettings();
208+
const updateSettings = useUpdateScopedSettings();
209+
const mixed = useScopedSettingsMixed(["composerReplySuggestionInstruction"]);
210+
const edited = useRef(false);
211+
if (!settings.composerReplySuggestions) return null;
212+
const custom = settings.composerReplySuggestionInstruction;
213+
return (
214+
<SettingsRow
215+
serverScoped
216+
{...searchableSetting("composer-reply-suggestion-instruction")}
217+
settingKeys={["composerReplySuggestionInstruction"]}
218+
description="The request Claude answers to predict your next message. Its reply is the suggestion, or NONE for no suggestion."
219+
resetAction={
220+
mixed || custom !== "" ? (
221+
<SettingResetButton
222+
label="reply suggestion prompt"
223+
onClick={() => updateSettings({ composerReplySuggestionInstruction: "" })}
224+
/>
225+
) : null
226+
}
227+
>
228+
<div className="mt-3 max-w-2xl pb-3.5">
229+
<Textarea
230+
key={`${mixed}:${custom}`}
231+
aria-label="Reply suggestion prompt"
232+
rows={12}
233+
defaultValue={mixed ? "" : custom || DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION}
234+
placeholder={
235+
mixed ? "Mixed. Enter a prompt to apply to all selected environments." : undefined
236+
}
237+
onChange={() => {
238+
edited.current = true;
239+
}}
240+
onBlur={(event) => {
241+
const value = event.target.value.trim();
242+
// The built-in prompt stays unsaved, so it keeps following app updates.
243+
const next = value === DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION ? "" : value;
244+
if (edited.current && (mixed || next !== custom)) {
245+
updateSettings({ composerReplySuggestionInstruction: next });
246+
}
247+
edited.current = false;
248+
}}
249+
/>
250+
</div>
251+
</SettingsRow>
252+
);
253+
}
254+
191255
function SimlockHostsRow() {
192256
const settings = useScopedSettings();
193257
const updateSettings = useUpdateScopedSettings();

‎apps/web/src/components/settings/settingsSearch.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,13 @@ export const SETTINGS_SEARCH_ITEMS = [
486486
scope: "environment",
487487
searchTerms: ["beta ghost text composer predict next message suggestion autocomplete reply"],
488488
},
489+
{
490+
id: "composer-reply-suggestion-instruction",
491+
title: "Reply suggestion prompt",
492+
to: "/settings/labs",
493+
scope: "environment",
494+
searchTerms: ["beta ghost text composer predict next message suggestion prompt instruction"],
495+
},
489496
{
490497
id: "simlock-hosts",
491498
title: "Simlock device hosts (alpha)",

‎docs/user/composer.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,14 @@ The prediction runs on the thread's own Claude model right after a turn
161161
finishes, while the provider's prompt cache still holds that conversation: the
162162
whole thread is read from cache at a fraction of the input price, so a long
163163
thread costs more per suggestion than a short one. Other providers, subagent and
164-
archived threads, and turns whose cache has already expired get no suggestion. A
165-
thread whose prediction misses the cache anyway pauses suggestions for an
166-
hour. Turn it on in Settings → Labs → Reply suggestions.
164+
archived threads, turns whose cache has already expired, and turns that end
165+
while the agent waits for its own background work or subagents get no
166+
suggestion. A thread whose prediction misses the cache anyway pauses
167+
suggestions for an hour.
168+
169+
Turn it on in Settings → Labs → Reply suggestions. Once it is on, Reply
170+
suggestion prompt below it holds the request the model answers; edit it to
171+
change what gets suggested, or reset it to the built-in prompt.
167172

168173
## Edit an earlier prompt
169174

‎packages/contracts/src/settings.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,32 @@ export const StorageCleanupSettings = Schema.Struct({
11981198
});
11991199
export type StorageCleanupSettings = typeof StorageCleanupSettings.Type;
12001200

1201+
/**
1202+
* The user-role request appended to a forked Claude session to predict the user's next message.
1203+
* The fork reuses the conversation's prompt cache, so this text is the only new input; the model
1204+
* answers with the message or NONE.
1205+
*/
1206+
export const DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION = [
1207+
"[Composer prediction request from the app, not a message from the user. Do not answer the user, continue the task, or call tools.]",
1208+
"",
1209+
'Predict what the USER would type next, not what you think they should do. The test: would they think "I was just about to type that"?',
1210+
"",
1211+
"Start from the user's original request and their recent messages.",
1212+
'- They asked for several steps and one is done: the next step ("run the tests").',
1213+
'- Code was just written: "try it out".',
1214+
"- The assistant offered options: the one this user would pick, judging by the conversation.",
1215+
'- The assistant asked whether to continue: "yes" or "go ahead".',
1216+
'- The task is complete with an obvious follow-up: "commit this", "push it".',
1217+
"- The question or proposal waiting on the user can sit anywhere in the assistant's last message, not only at its end. A message that closes with what the assistant will do once approved is asking for approval.",
1218+
"- After an error or a misunderstanding: stay silent, they need to assess it themselves.",
1219+
"- The assistant is waiting for its own background work (tests, builds, other agents) and will continue without the user: stay silent.",
1220+
'Be specific: "run the tests" beats "continue".',
1221+
"",
1222+
'Never suggest: praise or thanks ("looks good"), an acknowledgment that asks for nothing ("ok", "waiting", "got it"), a question, anything in the assistant\'s voice ("Let me...", "I\'ll...", "Here\'s..."), a new idea the user did not ask about, or more than one sentence. Stay silent when the next step is not obvious from what the user said, or when the suggestion could be unsafe or touches credentials, private data, or security incidents.',
1223+
"",
1224+
"Format: 2-12 words in the user's own language and style. Output only the message text: no role label, no quotes, no explanation. To stay silent, output exactly NONE.",
1225+
].join("\n");
1226+
12011227
export const ServerSettings = Schema.Struct({
12021228
// Null means unmigrated; empty is authoritative and must not resurrect cleared preferences.
12031229
favorites: Schema.NullOr(ModelFavorites).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
@@ -1299,6 +1325,10 @@ export const ServerSettings = Schema.Struct({
12991325
* after it commits; off spends nothing.
13001326
*/
13011327
composerReplySuggestions: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
1328+
/** Replaces {@link DEFAULT_COMPOSER_REPLY_SUGGESTION_INSTRUCTION} when not empty. */
1329+
composerReplySuggestionInstruction: TrimmedString.pipe(
1330+
Schema.withDecodingDefault(Effect.succeed("")),
1331+
),
13021332
sidebarAutoSettleKeepPullRequests: Schema.Boolean.pipe(
13031333
Schema.withDecodingDefault(Effect.succeed(false)),
13041334
),
@@ -1681,6 +1711,7 @@ export const ServerSettingsPatch = Schema.Struct({
16811711
guestSharingEnabled: Schema.optionalKey(Schema.Boolean),
16821712
linkedEnvironmentsEnabled: Schema.optionalKey(Schema.Boolean),
16831713
composerReplySuggestions: Schema.optionalKey(Schema.Boolean),
1714+
composerReplySuggestionInstruction: Schema.optionalKey(TrimmedString),
16841715
backgroundActivity: Schema.optionalKey(
16851716
Schema.Struct({
16861717
schemaVersion: Schema.optionalKey(Schema.Literal(1)),

0 commit comments

Comments
 (0)