feat(web): lock agent-bound projects to their Hermes agent

Threads in an agent's synthetic project are now bound to that agent
end to end on the client:

- The composer's model chip renders as a static label (the Home-thread
  treatment) for every thread and draft in an agent project, and the
  selection always resolves to the project's agentInstanceId — beating
  draft picks, sticky state, and fallbacks, and holding while the agent
  is offline. Started threads bound to a different instance keep their
  own binding.
- New drafts in an agent project are born pinned to the agent
  (agent projects are created with defaultModelSelection null, so the
  pin lives in buildLocalDraftThread rather than relying on it).
- Picking a Hermes agent from an unstarted draft in an ordinary project
  jumps to the agent's own project (ensureAgentProject + new thread),
  carrying the typed prompt and attachments along without touching
  sticky model state.
- A bound instance missing from the snapshot feeds the existing
  "restore it in Settings" message naming the exact instance id.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Ben DavisandClaude Fable 5 committed 2026-07-28 13:09:02 -07:00
1 parent 31113dfe7f
commit 73ddc93f4d
4 files changed
+195 -5

No files matched your search

+101 -2
View File
@@ -1,5 +1,6 @@
import {
type ApprovalRequestId,
DEFAULT_HERMES_MODEL,
DEFAULT_MODEL,
defaultInstanceIdForDriver,
type EnvironmentId,
@@ -180,6 +181,7 @@ import { buildDraftThreadRouteParams } from "../threadRoutes";
import {
type ComposerAttachment,
type DraftThreadEnvMode,
hydrateImagesFromPersisted,
useComposerDraftStore,
type DraftId,
} from "../composerDraftStore";
@@ -1142,6 +1144,9 @@ function ChatViewContent(props: ChatViewProps) {
);
const routeThreadKey = useMemo(() => scopedThreadKey(routeThreadRef), [routeThreadRef]);
const updateProject = useAtomCommand(projectEnvironment.update, { reportFailure: false });
const ensureAgentProject = useAtomCommand(serverEnvironment.providerEnsureAgentProject, {
reportFailure: false,
});
const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, {
reportFailure: false,
});
@@ -1416,10 +1421,22 @@ function ChatViewContent(props: ChatViewProps) {
? buildLocalDraftThread(
threadId,
draftThread,
fallbackDraftProject?.defaultModelSelection ?? NO_PROVIDER_MODEL_SELECTION,
// A draft in an agent's synthetic project is born bound to that
// agent. The pin cannot rely on the project's
// `defaultModelSelection` — agent projects are created with it
// null — and must beat the carried-over selection from the
// previously viewed thread.
fallbackDraftProject?.agentInstanceId
? { instanceId: fallbackDraftProject.agentInstanceId, model: DEFAULT_HERMES_MODEL }
: (fallbackDraftProject?.defaultModelSelection ?? NO_PROVIDER_MODEL_SELECTION),
)
: undefined,
[draftThread, fallbackDraftProject?.defaultModelSelection, threadId],
[
draftThread,
fallbackDraftProject?.agentInstanceId,
fallbackDraftProject?.defaultModelSelection,
threadId,
],
);
// Promotion is data-driven: the draft route keeps rendering while the
// server thread (same pre-allocated ref) starts, so live state must not
@@ -1589,6 +1606,10 @@ function ChatViewContent(props: ChatViewProps) {
? scopeProjectRef(activeThread.environmentId, activeThread.projectId)
: null;
const activeProject = useProject(activeProjectRef);
// Non-null iff the thread lives in an agent's synthetic project. Every
// thread there is bound to that one agent instance, so the composer's
// model picker locks to it and drafts can never route elsewhere.
const activeProjectAgentInstanceId = activeProject?.agentInstanceId ?? null;
const handleNewThreadInActiveProject = useCallback(() => {
startNewThreadForProject(activeProjectRef, handleNewThread);
}, [activeProjectRef, handleNewThread]);
@@ -5392,6 +5413,61 @@ function ChatViewContent(props: ChatViewProps) {
[activeThread, providerStatuses],
);
// Picking an agent from the model picker is a navigation, not a model
// change: agent threads only live in the agent's own synthetic project, so
// the selection jumps there (mirroring AgentPage's "New thread" flow) and
// carries the typed draft along. Sticky model state is deliberately left
// untouched — the agent selection lives only inside the agent project, so
// new threads in ordinary projects keep the previous non-agent model.
const jumpToAgentProjectDraft = useCallback(
async (instanceId: ProviderInstanceId) => {
const agentEnvironmentId = primaryEnvironment?.environmentId ?? null;
if (agentEnvironmentId === null) return;
// Get-or-create, never assume: the synthetic project may not exist yet,
// or may have been soft-deleted, and this call heals either case.
const result = await ensureAgentProject({
environmentId: agentEnvironmentId,
input: { instanceId },
});
if (result._tag !== "Success") {
toastManager.add({
type: "error",
title: "Could not open the agent's project",
description: "The agent project could not be created. Check the agent in Settings.",
});
return;
}
const agentProjectRef = scopeProjectRef(agentEnvironmentId, result.value.projectId);
const draftStore = useComposerDraftStore.getState();
const sourceDraft = draftStore.getComposerDraft(composerDraftTarget);
const carriedPrompt = sourceDraft?.prompt ?? "";
const carriedAttachments = sourceDraft?.persistedAttachments ?? [];
await handleNewThread(agentProjectRef);
const targetSession = useComposerDraftStore
.getState()
.getDraftSessionByProjectRef(agentProjectRef);
if (!targetSession) return;
// Seed the agent draft with what the user had typed, but never clobber
// text already sitting in the agent project's own draft. Attachments
// ride over via their persisted (data-URL) form so the copies share no
// revocable blob URLs with the source draft.
const targetDraft = useComposerDraftStore.getState().getComposerDraft(targetSession.draftId);
const targetIsEmpty =
(targetDraft?.prompt ?? "").trim().length === 0 && (targetDraft?.images.length ?? 0) === 0;
const hasCarriedContent = carriedPrompt.trim().length > 0 || carriedAttachments.length > 0;
if (!targetIsEmpty || !hasCarriedContent) return;
const store = useComposerDraftStore.getState();
if (carriedPrompt.trim().length > 0) {
store.setPrompt(targetSession.draftId, carriedPrompt);
}
if (carriedAttachments.length > 0) {
store.addImages(targetSession.draftId, hydrateImagesFromPersisted(carriedAttachments));
}
store.clearComposerPromptAndImages(composerDraftTarget);
},
[composerDraftTarget, ensureAgentProject, handleNewThread, primaryEnvironment?.environmentId],
);
const onProviderModelSelect = useCallback(
(instanceId: ProviderInstanceId, model: string) => {
if (!activeThread) return;
@@ -5400,6 +5476,25 @@ function ChatViewContent(props: ChatViewProps) {
// are rejected by returning early; the server remains authoritative too.
const entry = providerStatuses.find((snapshot) => snapshot.instanceId === instanceId);
const resolvedDriverKind = entry?.driver ?? null;
// An agent project is bound to exactly one instance. The chip renders
// static there so the picker can't normally fire, but keybindings and
// programmatic paths still land here — the binding is absolute.
if (activeProjectAgentInstanceId !== null && instanceId !== activeProjectAgentInstanceId) {
scheduleComposerFocus();
return;
}
// Choosing an agent while drafting in an ordinary project jumps to the
// agent's own project instead of retargeting this draft. Started
// threads never take this path — `requiresNewThreadForModelChange`
// already blocks them below.
if (
resolvedDriverKind === "hermes" &&
activeProjectAgentInstanceId === null &&
isLocalDraftThread
) {
void jumpToAgentProjectDraft(instanceId);
return;
}
if (
lockedProvider !== null &&
resolvedDriverKind !== null &&
@@ -5459,7 +5554,10 @@ function ChatViewContent(props: ChatViewProps) {
scheduleComposerFocus();
},
[
activeProjectAgentInstanceId,
activeThread,
isLocalDraftThread,
jumpToAgentProjectDraft,
lockedProvider,
scheduleComposerFocus,
setComposerDraftModelSelection,
@@ -5865,6 +5963,7 @@ function ChatViewContent(props: ChatViewProps) {
activeProjectDefaultModelSelection={
activeProject?.defaultModelSelection
}
activeProjectAgentInstanceId={activeProjectAgentInstanceId}
activeThreadModelSelection={activeThread?.modelSelection}
activeThreadActivities={activeThread?.activities}
resolvedTheme={resolvedTheme}
+21 -3
View File
@@ -670,6 +670,13 @@ export interface ChatComposerProps {
lockedProvider: ProviderDriverKind | null;
providerStatuses: ServerProvider[];
activeProjectDefaultModelSelection: ModelSelection | null | undefined;
/**
* `agentInstanceId` of the thread's project when it is an agent's
* synthetic project. Non-null binds the composer to that instance: the
* model chip renders static (like a Home thread) and the selection always
* resolves to the bound agent regardless of draft or sticky state.
*/
activeProjectAgentInstanceId: ProviderInstanceId | null;
activeThreadModelSelection: ModelSelection | null | undefined;
// Context window
@@ -765,6 +772,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
lockedProvider,
providerStatuses,
activeProjectDefaultModelSelection,
activeProjectAgentInstanceId,
activeThreadModelSelection,
activeThreadActivities,
resolvedTheme,
@@ -915,11 +923,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
threadInstanceId: activeThread?.session?.providerInstanceId,
threadModelInstanceId: activeThreadModelSelection?.instanceId,
projectInstanceId: activeProjectDefaultModelSelection?.instanceId,
projectAgentInstanceId: activeProjectAgentInstanceId,
requestedDriverKind,
lockedProvider,
lockedContinuationGroupKey,
});
}, [
activeProjectAgentInstanceId,
activeProjectDefaultModelSelection?.instanceId,
activeThread?.session?.providerInstanceId,
activeThreadModelSelection?.instanceId,
@@ -940,9 +950,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const noProviderAvailable =
selectedProviderEntry === undefined ||
!isProviderInstanceComposerAvailable(selectedProviderEntry);
// An agent project's binding survives the instance disappearing from the
// snapshot (agent removed / not yet re-enrolled), so it feeds the same
// "restore it in Settings" message a started Hermes thread gets.
const missingBoundHermesInstanceId =
selectedProviderEntry === undefined && lockedProvider === "hermes"
? (activeThread?.session?.providerInstanceId ?? activeThreadModelSelection?.instanceId)
selectedProviderEntry === undefined
? lockedProvider === "hermes"
? (activeThread?.session?.providerInstanceId ?? activeThreadModelSelection?.instanceId)
: (activeProjectAgentInstanceId ?? undefined)
: undefined;
const providerUnavailableMessage = getProviderInstanceComposerUnavailableMessage(
selectedProviderEntry,
@@ -968,7 +983,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
// `thread.create` fixes both, and Hermes sets
// `requiresNewThreadForModelChange`. A picker there would offer a choice
// that cannot be taken, so the chip renders as a static label instead.
// The same is true for every thread in an agent's synthetic project: the
// project itself is the binding, so drafts there are equally choiceless.
const isHomeThread = isHomeThreadId(providerStatuses, activeThread?.id);
const isAgentBoundComposer = activeProjectAgentInstanceId !== null;
const { modelOptions: composerModelOptions, selectedModel } = useEffectiveComposerModelState({
threadRef: composerDraftTarget,
@@ -3350,7 +3368,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
compact={isComposerFooterCompact}
activeInstanceId={selectedInstanceId}
model={selectedModelForPickerWithCustomFallback}
staticLabel={isHomeThread}
staticLabel={isHomeThread || isAgentBoundComposer}
lockedProvider={lockedProvider}
lockedContinuationGroupKey={lockedContinuationGroupKey}
instanceEntries={providerInstanceEntries}
+56
View File
@@ -129,6 +129,62 @@ describe("Hermes composer availability", () => {
}),
).toContain("hermes-research");
});
it("pins the composer to an agent project's bound instance over drafts and fallbacks", () => {
const boundAgent = ProviderInstanceId.make("hermes-siva");
const otherAgent = ProviderInstanceId.make("hermes-lab");
const entries = deriveProviderInstanceEntries([
provider({ provider: ProviderDriverKind.make("hermes"), instanceId: otherAgent }),
provider({ provider: ProviderDriverKind.make("codex"), instanceId: "codex" }),
]);
// Wins over a draft pick carried from the previously viewed thread.
expect(
resolveComposerProviderInstanceId({
entries,
draftInstanceId: ProviderInstanceId.make("codex"),
threadInstanceId: undefined,
threadModelInstanceId: undefined,
projectInstanceId: undefined,
projectAgentInstanceId: boundAgent,
requestedDriverKind: ProviderDriverKind.make("codex"),
lockedProvider: null,
lockedContinuationGroupKey: null,
}),
).toBe(boundAgent);
// Holds while the bound instance is absent from the snapshot: the
// project stays bound, never rerouted to another connected gateway.
expect(
resolveComposerProviderInstanceId({
entries,
draftInstanceId: null,
threadInstanceId: undefined,
threadModelInstanceId: undefined,
projectInstanceId: otherAgent,
projectAgentInstanceId: boundAgent,
requestedDriverKind: ProviderDriverKind.make("hermes"),
lockedProvider: null,
lockedContinuationGroupKey: null,
}),
).toBe(boundAgent);
// A thread whose session started on a different instance keeps its own
// binding — the project lock applies to new work, not history.
expect(
resolveComposerProviderInstanceId({
entries,
draftInstanceId: null,
threadInstanceId: otherAgent,
threadModelInstanceId: otherAgent,
projectInstanceId: undefined,
projectAgentInstanceId: boundAgent,
requestedDriverKind: ProviderDriverKind.make("hermes"),
lockedProvider: ProviderDriverKind.make("hermes"),
lockedContinuationGroupKey: null,
}),
).toBe(otherAgent);
});
});
describe("isProviderInstancePickerVisible", () => {
+17
View File
@@ -358,10 +358,27 @@ export function resolveComposerProviderInstanceId(input: {
readonly threadInstanceId: ProviderInstanceId | undefined;
readonly threadModelInstanceId: ProviderInstanceId | undefined;
readonly projectInstanceId: ProviderInstanceId | undefined;
/**
* The `agentInstanceId` of the thread's project when that project is an
* agent's synthetic project. Every thread in such a project belongs to
* that one agent, so the binding wins over draft picks, sticky state, and
* fallbacks alike — and holds even while the instance is offline, exactly
* like a started Hermes thread's session binding.
*/
readonly projectAgentInstanceId?: ProviderInstanceId | null;
readonly requestedDriverKind: ProviderDriverKind;
readonly lockedProvider: ProviderDriverKind | null;
readonly lockedContinuationGroupKey: string | null;
}): ProviderInstanceId {
if (
input.projectAgentInstanceId &&
// A thread already started on another instance keeps its own binding —
// agent projects lock *new* work to the agent; historical mismatches
// (threads created before the lock existed) are left exactly as-is.
(!input.threadInstanceId || input.threadInstanceId === input.projectAgentInstanceId)
) {
return input.projectAgentInstanceId;
}
const boundThreadInstanceId = input.threadInstanceId ?? input.threadModelInstanceId;
if (input.lockedProvider === "hermes" && boundThreadInstanceId) {
return boundThreadInstanceId;