Skip to content

Commit eb510cb

Browse files
committed
fix(agents): answer /btw side questions on gateway-hosted sessions
/btw resolved its prepared model runtime without allowGatewaySubagentBinding, but that flag became part of the prepared-runtime owner identity in #117587, and gateway startup publishes configured owners with it set. The request therefore matched no published owner, and standalone activation is refused while the gateway lifecycle owns configured identities - so every gateway-hosted /btw failed with 'prepared model runtime owner was not published' before any side-question work was dispatched. The embedded TUI path has no gateway lifecycle and kept working. Thread the flag from the gateway-originated caller instead of granting it in the shared helper: runBtwSideQuestion takes an explicit optional allowGatewaySubagentBinding param, the /btw command handler (auto-reply/reply/commands-btw.ts) opts in the way its sibling handlers commands-compact.ts and commands-system-prompt.ts already do at the same layer, and the embedded TUI call site stays unset so local side questions keep the local-runtime boundary and cannot borrow the active registry's subagent and node capabilities. Pins: the gateway request input and the helper's no-grant default in btw.test.ts, the TUI call-site absence in embedded-backend.test.ts, and the owner-keying constraint in prepared-model-runtime.owner-selection.test.ts.
1 parent adedc32 commit eb510cb

5 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎src/agents/btw.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const parseSessionEntriesMock = vi.fn();
1818
const migrateSessionEntriesMock = vi.fn();
1919
const buildSessionContextMock = vi.fn();
2020
const ensureOpenClawModelsJsonMock = vi.fn();
21+
const loadPreparedModelRuntimeSnapshotMock = vi.fn();
2122
const discoverAuthStorageMock = vi.fn();
2223
const discoverModelsMock = vi.fn();
2324
const getModelRegistryRuntimeMock = vi.fn();
@@ -95,7 +96,9 @@ vi.mock("./prepared-model-runtime.js", () => ({
9596
config: unknown;
9697
inheritedAuthDir?: string;
9798
workspaceDir?: string;
99+
allowGatewaySubagentBinding?: boolean;
98100
}) => {
101+
loadPreparedModelRuntimeSnapshotMock(params);
99102
const workspaceOptions = params.workspaceDir ? { workspaceDir: params.workspaceDir } : {};
100103
await ensureOpenClawModelsJsonMock(params.config, params.agentDir, workspaceOptions);
101104
const authStorage = discoverAuthStorageMock(params.agentDir, {
@@ -575,6 +578,7 @@ describe("runBtwSideQuestion", () => {
575578
migrateSessionEntriesMock.mockReset();
576579
buildSessionContextMock.mockReset();
577580
ensureOpenClawModelsJsonMock.mockReset();
581+
loadPreparedModelRuntimeSnapshotMock.mockReset();
578582
discoverAuthStorageMock.mockReset();
579583
discoverModelsMock.mockReset();
580584
getModelRegistryRuntimeMock.mockReset();
@@ -752,6 +756,35 @@ describe("runBtwSideQuestion", () => {
752756
});
753757
});
754758

759+
it("resolves the prepared runtime the way gateway-published owners are keyed", async () => {
760+
// Gateway startup publishes configured owners with allowGatewaySubagentBinding
761+
// (server-startup-post-attach.ts), and that flag is part of the owner key
762+
// (prepared-model-runtime.owner.ts). A gateway-hosted BTW request that omits
763+
// it matches no owner, and standalone activation is refused while the gateway
764+
// lifecycle is active, so the side question fails with "owner was not published".
765+
mockDoneAnswer("Final answer.");
766+
767+
await runSideQuestion({ allowGatewaySubagentBinding: true });
768+
769+
expect(mockCall(loadPreparedModelRuntimeSnapshotMock)?.[0]).toMatchObject({
770+
agentDir: DEFAULT_AGENT_DIR,
771+
allowGatewaySubagentBinding: true,
772+
});
773+
});
774+
775+
it("keeps gateway subagent binding off for local callers such as the embedded TUI", async () => {
776+
// The embedded TUI calls runBtwSideQuestion directly and must not borrow the
777+
// active registry's subagent and node capabilities, so the flag stays unset
778+
// unless a gateway-hosted caller opts in.
779+
mockDoneAnswer("Final answer.");
780+
781+
await runSideQuestion();
782+
783+
expect(mockCall(loadPreparedModelRuntimeSnapshotMock)?.[0]).not.toHaveProperty(
784+
"allowGatewaySubagentBinding",
785+
);
786+
});
787+
755788
it("routes Codex-selected BTW questions through the harness side-question hook", async () => {
756789
const supports = vi.fn(supportsPreparedOpenAIAuth);
757790
const codexSideQuestionMock = registerCodexSideQuestionHarness({

‎src/agents/btw.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ type RunBtwSideQuestionParams = {
591591
sessionStore?: Record<string, StoredSessionEntry>;
592592
sessionKey?: string;
593593
sandboxSessionKey?: string;
594+
/**
595+
* Set by gateway-hosted callers so the prepared runtime resolves the owner the
596+
* gateway published. Left unset by local callers such as the embedded TUI,
597+
* which must not borrow the active registry's subagent and node capabilities.
598+
*/
599+
allowGatewaySubagentBinding?: boolean;
594600
storePath?: string;
595601
resolvedThinkLevel?: ThinkLevel;
596602
resolvedReasoningLevel: ReasoningLevel;
@@ -715,6 +721,9 @@ export async function runBtwSideQuestion(
715721
agentDir: params.agentDir,
716722
inheritedAuthDir: resolveDefaultAgentDir(params.cfg),
717723
workspaceDir: requestedWorkspaceDir,
724+
// Gateway-published owners are keyed with this flag, so a gateway-hosted
725+
// request that omits it can never match one.
726+
...(params.allowGatewaySubagentBinding ? { allowGatewaySubagentBinding: true as const } : {}),
718727
});
719728
const sessionAgentId =
720729
preparedModelRuntime.agentId ??

‎src/agents/prepared-model-runtime.owner-selection.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ vi.mock("../logging/subsystem.js", () => ({
136136
import {
137137
acquireAgentRunPreparedModelRuntime,
138138
getPreparedModelRuntimeSnapshot,
139+
loadPreparedModelRuntimeSnapshot,
139140
prepareModelRuntimeSnapshot,
140141
publishPreparedModelRuntimeSnapshot,
141142
refreshPreparedModelRuntimeSnapshots,
@@ -247,6 +248,34 @@ describe("prepared model runtime owner selection", () => {
247248
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
248249
});
249250

251+
it("resolves a gateway-published owner only for requests carrying the binding flag", async () => {
252+
// Gateway startup publishes configured owners with allowGatewaySubagentBinding,
253+
// and that flag is part of the owner key. A request that omits it matches no
254+
// owner, and standalone activation stays refused while the lifecycle is active.
255+
mocks.configuredAgentIds = ["default"];
256+
const config = { agents: { defaults: { model: "openai/gpt-5.5" } } };
257+
await refreshPreparedModelRuntimeSnapshots(config, {
258+
allowGatewaySubagentBinding: true,
259+
catalogMode: "static",
260+
gatewayLifecycle: true,
261+
defaultWorkspaceDir: "/tmp/gateway-launch-workspace",
262+
});
263+
const request = {
264+
config,
265+
agentId: "default",
266+
agentDir: "/tmp/unused-agent",
267+
inheritedAuthDir: "/tmp/unused-agent",
268+
workspaceDir: "/tmp/gateway-launch-workspace",
269+
};
270+
271+
await expect(
272+
loadPreparedModelRuntimeSnapshot({ ...request, allowGatewaySubagentBinding: true }),
273+
).resolves.toMatchObject({ config });
274+
await expect(loadPreparedModelRuntimeSnapshot(request)).rejects.toThrow(
275+
"prepared model runtime owner was not published",
276+
);
277+
});
278+
250279
it("reuses the configured owner for its prepared plugin harness selections", async () => {
251280
mocks.configuredAgentIds = ["default"];
252281
const config = { agents: { defaults: { model: "openai/gpt-5.5" } } };

‎src/auto-reply/reply/commands-btw.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const handleBtwCommand: CommandHandler = defineAuthorizedTextCommand(
8787
sessionEntry: targetSessionEntry,
8888
sessionStore: params.sessionStore,
8989
sessionKey: params.sessionKey,
90+
allowGatewaySubagentBinding: true,
9091
...(params.ctx.RuntimePolicySessionKey
9192
? { sandboxSessionKey: params.ctx.RuntimePolicySessionKey }
9293
: {}),

‎src/tui/embedded-backend.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,35 @@ describe("EmbeddedTuiBackend", () => {
958958
});
959959
});
960960

961+
it("keeps gateway subagent binding off for embedded /btw side questions", async () => {
962+
// The embedded TUI runs the side question locally, so it must not borrow the
963+
// active registry's subagent and node capabilities. Only gateway-hosted
964+
// callers opt into allowGatewaySubagentBinding.
965+
loadSessionEntryMock.mockReturnValue({
966+
cfg: {},
967+
canonicalKey: "global",
968+
storePath: "/tmp/openclaw-btw-sessions.json",
969+
store: {},
970+
entry: { sessionId: "session-btw-local" },
971+
});
972+
runBtwSideQuestionMock.mockResolvedValueOnce({ text: "side done" });
973+
974+
const { EmbeddedTuiBackend } = await import("./embedded-backend.js");
975+
const backend = new EmbeddedTuiBackend();
976+
backend.start();
977+
await backend.sendChat({
978+
sessionKey: "global",
979+
message: "/btw local only",
980+
runId: "run-btw-local",
981+
});
982+
await vi.waitFor(() => expect(runBtwSideQuestionMock).toHaveBeenCalledTimes(1));
983+
await backend.stop();
984+
985+
expect(runBtwSideQuestionMock.mock.calls[0]?.[0]).not.toHaveProperty(
986+
"allowGatewaySubagentBinding",
987+
);
988+
});
989+
961990
it("reports the newest matching non-BTW local run in embedded history", async () => {
962991
loadSessionEntryMock.mockImplementation((sessionKey: string) => ({
963992
cfg: {},

0 commit comments

Comments
 (0)