Skip to content

The agent that grows with you

fix(desktop): declare the branch methods in the typed gateway contract

brooklyn!ecacf3d0c967
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index bf5c2b742d7..8bb6d8491a7 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -261,8 +261,6 @@ describe('desktop branch creation idempotency', () => {
expect(requestGateway).toHaveBeenCalledWith(
'session.branch_stored',
expect.objectContaining({
- copy_parent_history: true,
- omit_messages: true,
parent_session_id: 'parent',
source: 'desktop'
})
@@ -2129,8 +2127,6 @@ describe('branchStoredSession desktop source tagging', () => {
'default',
'session.branch_stored',
expect.objectContaining({
- copy_parent_history: true,
- omit_messages: true,
parent_session_id: 'stored-parent',
source: 'desktop'
})
@@ -2299,7 +2295,7 @@ describe('branchStoredSession desktop source tagging', () => {
await expect(branchCurrentSession!()).resolves.toBe(true)
expect(getAllSessionMessages).not.toHaveBeenCalled()
- expect(branchParams).toEqual({ session_id: 'live-parent', omit_messages: true })
+ expect(branchParams).toEqual({ session_id: 'live-parent' })
})
it('aborts if the active runtime changes while the branch transcript is hydrating', async () => {
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index b4485ef1b30..f009d2b88a0 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -2367,8 +2367,6 @@ export function useSessionActions({
const createParams = {
cols: 96,
- copy_parent_history: true,
- omit_messages: true,
source: 'desktop',
...(cwd && { cwd }),
...(profile ? { profile } : {}),
@@ -2379,7 +2377,7 @@ export function useSessionActions({
sourceSessionId
? requestBranchGateway<SessionCreateResponse>(
branchCount === undefined ? 'session.branch_whole' : 'session.branch',
- branchCount === undefined ? { ...branchParams, omit_messages: true } : branchParams
+ branchParams
).catch(err => {
if (!isMissingRpcMethod(err)) {
throw err
diff --git a/apps/shared/src/gateway-contract.generated.ts b/apps/shared/src/gateway-contract.generated.ts
index 434ad9f2e46..014b14d797c 100644
--- a/apps/shared/src/gateway-contract.generated.ts
+++ b/apps/shared/src/gateway-contract.generated.ts
@@ -2817,6 +2817,20 @@ export interface ToolLabel {
}
/** Which surface one inner call of a bridged ``tool_call`` runs on. */
export type ToolLabelKind = 'connector' | 'mcp' | 'tool'
+export interface SessionBranchStoredParams {
+ profile?: string | null
+ parent_session_id: string
+ cols?: number | null
+ source?: string | null
+ cwd?: string | null
+}
+export interface SessionBranchStoredResult {
+ session_id: string
+ stored_session_id: string
+ message_count: number
+ messages_omitted: boolean
+ info: SessionLiveInfo
+}
/** ``session_id`` is the STORED id (or an exact title); the reply's ``session_id`` is the runtime id. */
export interface SessionResumeParams {
session_id: string
@@ -3069,6 +3083,20 @@ export interface SessionBranchResult {
messages: TranscriptMessage[]
info: SessionLiveInfo
}
+export interface SessionBranchWholeParams {
+ session_id: string
+ profile?: string | null
+ name?: string | null
+}
+export interface SessionBranchWholeResult {
+ session_id: string
+ stored_session_id: string
+ title: string
+ parent: string
+ message_count: number
+ messages_omitted: boolean
+ info: SessionLiveInfo
+}
export interface SessionUndoParams {
session_id: string
profile?: string | null
@@ -4972,6 +5000,10 @@ export interface RpcMethods {
'session.active_list': { params: SessionActiveListParams; result: SessionActiveListResult }
/** Fork a live session into a new stored child that shares the parent's history so far. */
'session.branch': { params: SessionBranchParams; result: SessionBranchResult }
+ /** Whole-session branch of a stored parent: the owning backend reads and copies the transcript, which never crosses the wire (a separate method so an older gateway fails loudly, not with an empty branch). */
+ 'session.branch_stored': { params: SessionBranchStoredParams; result: SessionBranchStoredResult }
+ /** session.branch of the whole history without echoing the copied transcript back. */
+ 'session.branch_whole': { params: SessionBranchWholeParams; result: SessionBranchWholeResult }
/** Tear down a live session (its stored row stays resumable). */
'session.close': { params: SessionCloseParams; result: SessionCloseResult }
/** Manual /compress of an idle session, optionally focused on a topic. */
@@ -5279,6 +5311,8 @@ export const RPC_METHODS = [
'session.activate',
'session.active_list',
'session.branch',
+ 'session.branch_stored',
+ 'session.branch_whole',
'session.close',
'session.compress',
'session.context_breakdown',
diff --git a/apps/shared/src/gateway-contract.openrpc.json b/apps/shared/src/gateway-contract.openrpc.json
index 8d8f8a06983..6a081c7a9b2 100644
--- a/apps/shared/src/gateway-contract.openrpc.json
+++ b/apps/shared/src/gateway-contract.openrpc.json
@@ -2994,6 +2994,42 @@
}
}
},
+ {
+ "name": "session.branch_stored",
+ "summary": "Whole-session branch of a stored parent: the owning backend reads and copies the transcript, which never crosses the wire (a separate method so an older gateway fails loudly, not with an empty branch).",
+ "params": [
+ {
+ "name": "params",
+ "schema": {
+ "$ref": "#/components/schemas/SessionBranchStoredParams"
+ }
+ }
+ ],
+ "result": {
+ "name": "result",
+ "schema": {
+ "$ref": "#/components/schemas/SessionBranchStoredResult"
+ }
+ }
+ },
+ {
+ "name": "session.branch_whole",
+ "summary": "session.branch of the whole history without echoing the copied transcript back.",
+ "params": [
+ {
+ "name": "params",
+ "schema": {
+ "$ref": "#/components/schemas/SessionBranchWholeParams"
+ }
+ }
+ ],
+ "result": {
+ "name": "result",
+ "schema": {
+ "$ref": "#/components/schemas/SessionBranchWholeResult"
+ }
+ }
+ },
{
"name": "session.close",
"summary": "Tear down a live session (its stored row stays resumable).",
@@ -27696,6 +27732,183 @@
"title": "SessionBranchResult",
"type": "object"
},
+ "SessionBranchStoredParams": {
+ "additionalProperties": false,
+ "properties": {
+ "profile": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Profile"
+ },
+ "parent_session_id": {
+ "minLength": 1,
+ "title": "Parent Session Id",
+ "type": "string"
+ },
+ "cols": {
+ "anyOf": [
+ {
+ "type": "integer"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Cols"
+ },
+ "source": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Source"
+ },
+ "cwd": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Cwd"
+ }
+ },
+ "required": [
+ "parent_session_id"
+ ],
+ "title": "SessionBranchStoredParams",
+ "type": "object"
+ },
+ "SessionBranchStoredResult": {
+ "additionalProperties": false,
+ "properties": {
+ "session_id": {
+ "title": "Session Id",
+ "type": "string"
+ },
+ "stored_session_id": {
+ "title": "Stored Session Id",
+ "type": "string"
+ },
+ "message_count": {
+ "title": "Message Count",
+ "type": "integer"
+ },
+ "messages_omitted": {
+ "title": "Messages Omitted",
+ "type": "boolean"
+ },
+ "info": {
+ "$ref": "#/components/schemas/SessionLiveInfo"
+ }
+ },
+ "required": [
+ "session_id",
+ "stored_session_id",
+ "message_count",
+ "messages_omitted",
+ "info"
+ ],
+ "title": "SessionBranchStoredResult",
+ "type": "object"
+ },
+ "SessionBranchWholeParams": {
+ "additionalProperties": false,
+ "properties": {
+ "session_id": {
+ "title": "Session Id",
+ "type": "string"
+ },
+ "profile": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Profile"
+ },
+ "name": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "default": null,
+ "title": "Name"
+ }
+ },
+ "required": [
+ "session_id"
+ ],
+ "title": "SessionBranchWholeParams",
+ "type": "object"
+ },
+ "SessionBranchWholeResult": {
+ "additionalProperties": false,
+ "properties": {
+ "session_id": {
+ "title": "Session Id",
+ "type": "string"
+ },
+ "stored_session_id": {
+ "title": "Stored Session Id",
+ "type": "string"
+ },
+ "title": {
+ "title": "Title",
+ "type": "string"
+ },
+ "parent": {
+ "title": "Parent",
+ "type": "string"
+ },
+ "message_count": {
+ "title": "Message Count",
+ "type": "integer"
+ },
+ "messages_omitted": {
+ "title": "Messages Omitted",
+ "type": "boolean"
+ },
+ "info": {
+ "$ref": "#/components/schemas/SessionLiveInfo"
+ }
+ },
+ "required": [
+ "session_id",
+ "stored_session_id",
+ "title",
+ "parent",
+ "message_count",
+ "messages_omitted",
+ "info"
+ ],
+ "title": "SessionBranchWholeResult",
+ "type": "object"
+ },
"SessionCloseParams": {
"additionalProperties": false,
"properties": {
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 0e278e4256c..de4586d444a 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -15699,8 +15699,8 @@ def test_session_branch_writes_to_parent_profile_db(monkeypatch, tmp_path):
resp = server.handle_request(
{
"id": "1",
- "method": "session.branch",
- "params": {"session_id": "parent", "name": "forked", "omit_messages": True},
+ "method": "session.branch_whole",
+ "params": {"session_id": "parent", "name": "forked"},
}
)
assert "result" in resp, resp
@@ -15818,7 +15818,7 @@ def test_session_create_persists_seeded_branch_child(monkeypatch):
server._sessions.pop(runtime_sid, None)
-def test_session_create_copies_parent_history_without_returning_transcript(monkeypatch):
+def test_session_branch_stored_copies_parent_history_without_returning_transcript(monkeypatch):
"""Whole-session desktop branches read the parent in the gateway, not in the renderer."""
class _Scope:
@@ -15857,11 +15857,9 @@ def test_session_create_copies_parent_history_without_returning_transcript(monke
resp = server.handle_request(
{
"id": "1",
- "method": "session.create",
+ "method": "session.branch_stored",
"params": {
"cols": 96,
- "copy_parent_history": True,
- "omit_messages": True,
"parent_session_id": "parent",
"source": "desktop",
},
diff --git a/tui_gateway/contracts/sessions.py b/tui_gateway/contracts/sessions.py
index 82d6dd0932d..d2c1809cbda 100644
--- a/tui_gateway/contracts/sessions.py
+++ b/tui_gateway/contracts/sessions.py
@@ -144,6 +144,27 @@ method("session.create", params=SessionCreateParams, result=SessionCreateResult,
doc="Mint a live session (agent builds after the reply); a DB row appears on the first prompt unless seeded.")
+class SessionBranchStoredParams(ProfileParams):
+ parent_session_id: str = Field(min_length=1)
+ cols: int | None = None
+ source: str | None = None
+ cwd: str | None = None
+
+
+class SessionBranchStoredResult(Result):
+ session_id: str
+ stored_session_id: str
+ message_count: int
+ messages_omitted: bool
+ info: SessionLiveInfo
+
+
+method("session.branch_stored", params=SessionBranchStoredParams, result=SessionBranchStoredResult,
+ doc="Whole-session branch of a stored parent: the owning backend reads and copies the transcript, "
+ "which never crosses the wire (a separate method so an older gateway fails loudly, not with an empty "
+ "branch).")
+
+
# ── session.resume / activate ─────────────────────────────────────────────────────────────────
@@ -359,6 +380,24 @@ method("session.branch", params=SessionBranchParams, result=SessionBranchResult,
doc="Fork a live session into a new stored child that shares the parent's history so far.")
+class SessionBranchWholeParams(SessionParams):
+ name: str | None = None
+
+
+class SessionBranchWholeResult(Result):
+ session_id: str
+ stored_session_id: str
+ title: str
+ parent: str
+ message_count: int
+ messages_omitted: bool
+ info: SessionLiveInfo
+
+
+method("session.branch_whole", params=SessionBranchWholeParams, result=SessionBranchWholeResult,
+ doc="session.branch of the whole history without echoing the copied transcript back.")
+
+
class SessionUndoParams(SessionParams):
pass
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index 6af42fd87b9..a41aeff7b73 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -331,7 +331,9 @@ def _create_overrides(params: dict) -> tuple:
return model_override, reasoning_override, service_tier_override
-def _create_session(rid, params: dict) -> dict:
+def _create_session(rid, params: dict, *, copy_parent_history: bool = False) -> dict:
+ """``session.create``; ``copy_parent_history`` (``session.branch_stored``) reads the parent's
+ transcript server-side and omits it from the reply."""
# ``profile`` (app-global remote mode): stored so the build and every turn re-bind HERMES_HOME.
profile_home = _profile_home(profile := (params.get("profile") or "").strip() or None)
# Reject an incoherent model×provider pair BEFORE any state exists: minting it only defers the
@@ -343,8 +345,6 @@ def _create_session(rid, params: dict) -> dict:
history = _coerce_seed_history(params.get("messages"))
# Branch: links back so list_sessions_rich keeps it visible and the sidebar nests it.
parent_session_id = _str_param(params, "parent_session_id") or None
- copy_parent_history = _flag(params, "copy_parent_history")
- omit_messages = _flag(params, "omit_messages")
if copy_parent_history:
if not parent_session_id:
return _err(rid, 4008, "parent_session_id is required when copying parent history")
@@ -419,7 +419,7 @@ def _create_session(rid, params: dict) -> dict:
messages = _history_to_messages(history, profile_home=profile_home) # hidden seed rows are not on the wire; count what is (as resume does)
return _ok(rid, {
"session_id": sid, "stored_session_id": key, "message_count": len(messages),
- **({"messages": messages} if not omit_messages else {"messages_omitted": True}),
+ **({"messages_omitted": True} if copy_parent_history else {"messages": messages}),
# Reflect the override now so the client doesn't clobber its sticky pick.
"info": {"model": override.get("model") if override else _session_default_model(_sessions[sid]),
**({"provider": override["provider"]} if override.get("provider") else {}),
@@ -435,16 +435,9 @@ def _(rid, params: dict) -> dict:
@method("session.branch_stored")
def _(rid, params: dict) -> dict:
- """Create a whole-session branch from a stored parent without routing its transcript through the client.
-
- This is a distinct method, rather than an overloaded ``session.create`` flag, so older Desktop clients can
- safely detect the capability and older gateways cannot silently create an empty branch when they ignore an
- unknown parameter.
- """
- branch_params = dict(params)
- branch_params["copy_parent_history"] = True
- branch_params["omit_messages"] = True
- return _create_session(rid, branch_params)
+ """Whole-session branch of a stored parent without routing its transcript through the client
+ (a distinct method so an older gateway answers "unknown method" instead of an empty branch)."""
+ return _create_session(rid, params, copy_parent_history=True)
def _unarchive_recoverable(db, session_id: str) -> bool:
@@ -2081,7 +2074,7 @@ def _branch_source_history(db, session: dict, old_key: str) -> list:
return history or _visible_branch_history(in_memory_history)
-def _branch_live(rid, params: dict, session: dict) -> dict:
+def _branch_live(rid, params: dict, session: dict, *, omit_messages: bool = False) -> dict:
# Write into the parent's profile-scoped state.db; the launch handle would orphan rows.
with _session_db(session) as db:
if db is None:
@@ -2109,7 +2102,7 @@ def _branch_live(rid, params: dict, session: dict) -> dict:
return _err(rid, 5000, f"agent init failed on branch: {e}")
response = {"session_id": new_sid, "stored_session_id": new_key, "title": title, "parent": old_key,
"message_count": len(history), "info": _session_info(agent, _sessions.get(new_sid))}
- if _flag(params, "omit_messages"):
+ if omit_messages:
response["messages_omitted"] = True
else:
response["messages"] = _history_to_messages(history, profile_home=session.get("profile_home"))
@@ -2123,11 +2116,8 @@ def _(rid, params: dict, session: dict) -> dict:
@_session_method("session.branch_whole", live=True)
def _(rid, params: dict, session: dict) -> dict:
- """Whole-session branch response seam for Desktop clients that do not need the copied transcript echoed back."""
- branch_params = dict(params)
- branch_params.pop("count", None)
- branch_params["omit_messages"] = True
- return _branch_live(rid, branch_params, session)
+ """Whole-history ``session.branch`` that doesn't echo the copied transcript back."""
+ return _branch_live(rid, params, session, omit_messages=True)
# ── interrupt / steer / redirect ─────────────────────────────────────