Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

add authenticated subagent model selection (#453)

kt6c883334cb32
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index f17798b174..12a0cc62bd 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -5,6 +5,7 @@
- Changed top-level CLI help to show concise common options and commands without loading runtime resources ([ENG-4688](https://linear.app/primeintellect/issue/ENG-4688/help-command-is-obscenely-verbose)).
- Fixed completed subagents cancelling their RLM heartbeats before the first run ([ENG-4652](https://linear.app/primeintellect/issue/ENG-4652/subagent-heartbeats-dont-work)).
- Changed the fullscreen follow shortcut from `Alt+Down` to `Ctrl+Shift+Down` for more reliable terminal input ([ENG-4684](https://linear.app/primeintellect/issue/ENG-4684/altdown-doesnt-work)).
+- Added user-requested model selection for subagents with bounded account-authorized discovery and explicit parent-model fallback warnings ([ENG-4649](https://linear.app/primeintellect/issue/ENG-4649/allow-subagents-to-use-a-different-model-than-the-parent-agent)).
- Added subtle feature hints to longer-running agent turns ([ENG-4521](https://linear.app/primeintellect/issue/ENG-4521/add-subtle-hints-for-new-prime-agent-features)).
- Fixed active heartbeats not resuming after Prime Agent updates ([ENG-4657](https://linear.app/primeintellect/issue/ENG-4657/heartbeats-dont-survive-updatesdaemon-reboots)).
- Fixed the Agents View reordering sessions whenever prompts or heartbeats updated their activity timestamps ([ENG-4650](https://linear.app/primeintellect/issue/ENG-4650/agents-view-shifts-session-list-constantly)).
diff --git a/packages/coding-agent/docs/kernel-and-rlm-recursion.md b/packages/coding-agent/docs/kernel-and-rlm-recursion.md
index de5e88497b..f05e831eaf 100644
--- a/packages/coding-agent/docs/kernel-and-rlm-recursion.md
+++ b/packages/coding-agent/docs/kernel-and-rlm-recursion.md
@@ -69,13 +69,15 @@ packages/coding-agent/src/core/agent-session.ts
Parent session owns recursion settings and implements runRlmChild().
packages/coding-agent/src/core/rlm-runtime.ts
- TypeScript request/result types for rlm.run, rlm.list_subagents, and
- rlm.delete_subagent plus their typed host request handlers.
+ TypeScript request/result types for rlm.run, rlm.find_models,
+ rlm.list_subagents, and rlm.delete_subagent plus their typed host request
+ handlers.
prime-agent-runtime/src/rlm/__init__.py
Python shim installed into ~/.prime/agent/kernel-venv. Exposes rlm, rlm.run(),
- rlm.list_subagents(), rlm.delete_subagent(), host_request(), RLMResult,
- RLMSubagent, TokenUsage, and the session-backed harness state helper.
+ rlm.find_models(), rlm.list_subagents(), rlm.delete_subagent(),
+ host_request(), RLMResult, RLMModel, RLMSubagent, TokenUsage, and the
+ session-backed harness state helper.
prime-agent-runtime/src/rlm/harness.py
Session-backed JSON store for reset-free harness refinement notes: prompt
@@ -382,9 +384,11 @@ It exports:
```python
rlm
run(prompt: str, **kwargs)
+find_models(query: str = "", limit: int = 8)
list_subagents()
host_request(request_type: str, payload: dict | None = None)
RLMResult
+RLMModel
RLMSubagent
TokenUsage
```
@@ -396,6 +400,8 @@ result.answer # str
result.usage # TokenUsage
result.turns # int
result.session_dir # pathlib.Path | None
+result.model # exact provider/model selector
+result.warning # fallback notice to surface to the user, or None
```
The shim also makes the module callable so `import rlm; await rlm("...")` can work, but the ipython tool normally injects the callable object directly as `rlm`.
@@ -442,7 +448,7 @@ awaits the Future
The callback strips the reply's `status` field and converts the payload into `RLMResult`, or raises `RuntimeError` when the host reports an error. The comm-open/await machinery is the public `host_request()`; `run()` is a typed wrapper around it, and other kernel-side skills reuse `host_request()` for their own request types.
-The shim accepts `**kwargs` and includes them in the comm payload. The TypeScript host supports `name="..."` for an orchestrator-chosen child session name; all other kwargs are rejected with a clear error instead of being ignored.
+The shim accepts `**kwargs` and includes them in the comm payload. The TypeScript host supports `name="..."` for an orchestrator-chosen child session name and `model="provider/model"` for an exact model selection; all other kwargs are rejected with a clear error instead of being ignored. `find_models()` searches a bounded catalog containing only models backed by active, non-expired credentials and returns `provider`, `id`, `name`, and exact `selector` fields.
### Why Control-Channel Comm Replies Exist
@@ -510,8 +516,7 @@ options.hostHandlers[data.type](data)
sendCommMessage(comm_id, { status: "ok", ...result })
```
-The RLM handlers include `rlm.run` for child creation and
-`rlm.list_subagents` for the current parent session's automatic child registry.
+The RLM handlers include `rlm.run` for child creation, `rlm.find_models` for bounded authenticated model discovery, and `rlm.list_subagents` for the current parent session's automatic child registry.
Unknown types fail with `host request type "<type>" is not available in this session`.
@@ -544,10 +549,12 @@ hostHandlers: {
Sequence:
```text
-runRlmChild(prompt)
+runRlmChild(prompt, kwargs)
|
| check current depth < max depth
- | require selected model
+ | resolve an exact optional model selector against authenticated models
+ | fall back to the parent model with a user-facing warning if unavailable
+ | otherwise inherit the parent model
v
create child session dir
|
@@ -555,7 +562,7 @@ create child session dir
v
create child SessionManager
|
- | append model change
+ | append selected model change
| append thinking level change
v
create child Agent
@@ -592,6 +599,8 @@ The child returns:
};
turns: number;
session_dir: string | null;
+ model: string;
+ warning?: string;
}
```
@@ -606,6 +615,30 @@ Turns are counted as assistant messages in the child transcript.
The answer is the child's final assistant text. This matches the RLM-1 training surface: the model stops calling tools and the final assistant text is the answer.
+By default, a child inherits the parent's current model. When a user or skill
+requests a different model, the orchestrator first searches the bounded
+authenticated catalog, chooses a candidate, and passes its exact selector:
+
+```python
+matches = await rlm.find_models("DeepSeek V4 Flash")
+result = await rlm(
+ "Check the API",
+ model=matches[0].selector,
+)
+```
+
+`find_models()` returns up to eight matches by default and never more than 20.
+Each match exposes `provider`, `id`, `name`, and the exact `provider/model`
+`selector`; only models backed by active, non-expired credentials are included.
+The full catalog is never injected into the system prompt.
+
+`rlm.run(model=...)` intentionally accepts only an exact selector. If that
+selector is unknown, unauthenticated, expired, unavailable, or fails auth
+preflight, the child uses the parent model and the result returns the actual
+`.model` plus a `.warning` that the parent must surface to the user. A selected
+model is persisted with the child and remains active on later turns; changing
+the parent model does not update existing children.
+
### Parent-Scoped Subagent Registry
The host exposes the current parent session's direct children without relying on
@@ -823,6 +856,9 @@ no selected model
unsupported rlm.run kwargs
-> TS runRlmChild throws "Unsupported rlm.run kwargs: ..."
+unknown, unauthenticated, expired, or unavailable rlm.run model
+ -> TS runRlmChild uses the parent model and returns RLMResult.warning
+
child provider error
-> child agent final error behavior is reflected in its final assistant state
diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.ts
index c5c990338e..85e834ff5d 100644
--- a/packages/coding-agent/src/core/agent-session.ts
+++ b/packages/coding-agent/src/core/agent-session.ts
@@ -30,6 +30,7 @@ import {
type ThinkingLevel,
} from "@earendil-works/pi-agent-core";
import type {
+ Api,
AssistantMessage,
ImageContent,
Model,
@@ -201,10 +202,14 @@ import {
type CreateRlmSubagentRuntimeOptions,
createDefaultRlmSubagentSessionName,
createRlmDeleteSubagentHostHandler,
+ createRlmFindModelsHostHandler,
createRlmListSubagentsHostHandler,
createRlmRunHostHandler,
+ findRlmModelMatches,
+ normalizeRequestedRlmSubagentModel,
normalizeRequestedRlmSubagentSessionName,
type RlmDeleteSubagentResult,
+ type RlmFindModelsResult,
type RlmInternalRunResult,
type RlmListSubagentsResult,
type RlmRunResult,
@@ -251,6 +256,8 @@ export interface RlmChildAgentSnapshot {
activeSessionId?: string;
/** Stable daemon-visible session name for addressing/displaying the child. */
sessionName?: string;
+ /** Exact provider/model selector used by the child. */
+ model?: string;
label: string;
status: RlmChildAgentStatus;
durationMs?: number;
@@ -688,6 +695,11 @@ interface RlmChildRun {
detachedDeletion?: RlmSubagentRegistryEntry;
}
+interface RlmSubagentModelSelection {
+ model: Model<Api>;
+ warning?: string;
+}
+
// ============================================================================
// Constants
// ============================================================================
@@ -889,6 +901,7 @@ export class AgentSession {
private _rlmParentNodeId?: string;
private _subagentRuntimeHost?: SubagentRuntimeHost;
private _activeRlmChildRuns = new Map<string, RlmChildRun>();
+ private _pendingRlmSubagentSessionNames = new Set<string>();
// Inline mode keeps finished child sessions so the inspector can still read them;
// the daemon does the same by leaving the child session resident in its registry.
private _retainedRlmChildSessions = new Map<string, AgentSession>();
@@ -6005,6 +6018,7 @@ export class AgentSession {
"rlm.run": createRlmRunHostHandler(({ prompt, kwargs, cellSourceCode }) =>
this.runRlmChild(prompt, kwargs, cellSourceCode),
),
+ "rlm.find_models": createRlmFindModelsHostHandler((query, limit) => this.findRlmModels(query, limit)),
"rlm.list_subagents": createRlmListSubagentsHostHandler(() => this.listRlmSubagents()),
"rlm.delete_subagent": createRlmDeleteSubagentHostHandler((target) => this.deleteRlmSubagent(target)),
"model.info": async () => ({
@@ -6263,8 +6277,9 @@ export class AgentSession {
spawnCode: options.spawnCode,
sessionDir: options.sessionDir,
model: options.model,
- thinkingLevel: this.thinkingLevel,
- serviceTier: this.serviceTier,
+ thinkingLevel: clampThinkingLevel(options.model, this.thinkingLevel) as ThinkingLevel,
+ serviceTier:
+ this.serviceTier === "priority" && !supportsFastMode(options.model) ? "default" : this.serviceTier,
scopedModels: [...this._scopedModels],
activeToolNames: this.getActiveToolNames(),
allowedToolNames: this._allowedToolNames ? [...this._allowedToolNames] : undefined,
@@ -6315,16 +6330,14 @@ export class AgentSession {
}
childSessionManager.appendModelChange(options.model.provider, options.model.id);
childSessionManager.appendThinkingLevelChange(options.thinkingLevel);
- const serviceTier =
- options.serviceTier === "priority" && !supportsFastMode(options.model) ? "default" : options.serviceTier;
- childSessionManager.appendServiceTierChange(serviceTier);
+ childSessionManager.appendServiceTierChange(options.serviceTier);
const childAgent = new Agent({
initialState: {
systemPrompt: "",
model: options.model,
thinkingLevel: options.thinkingLevel,
- serviceTier,
+ serviceTier: options.serviceTier,
tools: [],
},
convertToLlm: this.agent.convertToLlm,
@@ -6696,6 +6709,9 @@ export class AgentSession {
}
private _assertRlmSubagentSessionNameAvailable(name: string): void {
+ if (this._pendingRlmSubagentSessionNames.has(name)) {
+ throw new Error(`RLM subagent session name "${name}" is already in use`);
+ }
const conflictsWithSelector = (endpoint: {
activeSessionId: string;
sessionId: string;
@@ -6736,25 +6752,86 @@ export class AgentSession {
}
}
- private _startRlmChildRun(prompt: string, kwargs: Record<string, unknown> = {}, spawnCode?: string): RlmChildRun {
- const { name: rawName, ...unsupported } = kwargs;
+ private async _authenticatedRlmModels(): Promise<Model<Api>[]> {
+ return (await this._modelRegistry.getExecutableModels()).filter((model) => {
+ const status = this._modelRegistry.getProviderAuthStatus(model.provider);
+ return status.source !== "stale" && status.label !== "expired";
+ });
+ }
+
+ async findRlmModels(query: string, limit: number): Promise<RlmFindModelsResult> {
+ return { models: findRlmModelMatches(query, await this._authenticatedRlmModels(), limit) };
+ }
+
+ private _rlmModelFallback(reference: string, parentModel: Model<Api>, reason: string): RlmSubagentModelSelection {
+ const parentSelector = `${parentModel.provider}/${parentModel.id}`;
+ return {
+ model: parentModel,
+ warning: `Requested subagent model "${reference}" ${reason}. Used the parent model "${parentSelector}" instead. Tell the user the subagent ran with "${parentSelector}", not "${reference}".`,
+ };
+ }
+
+ private async _resolveRlmSubagentModel(reference: string | undefined): Promise<RlmSubagentModelSelection> {
+ const parentModel = this.model;
+ if (!parentModel) {
+ throw new Error(formatNoModelSelectedMessage());
+ }
+ if (!reference) {
+ return { model: parentModel };
+ }
+
+ const normalizedReference = reference.toLowerCase();
+ if (`${parentModel.provider}/${parentModel.id}`.toLowerCase() === normalizedReference) {
+ return { model: parentModel };
+ }
+ const model = (await this._authenticatedRlmModels()).find(
+ (candidate) => `${candidate.provider}/${candidate.id}`.toLowerCase() === normalizedReference,
+ );
+ if (!model) {
+ return this._rlmModelFallback(reference, parentModel, "is unavailable, unauthenticated, or expired");
+ }
+
+ const auth = await this._modelRegistry.getApiKeyAndHeaders(model);
+ if (!auth.ok) {
+ return this._rlmModelFallback(reference, parentModel, "failed authentication preflight");
+ }
+ return { model };
+ }
+
+ private async _startRlmChildRun(
+ prompt: string,
+ kwargs: Record<string, unknown> = {},
+ spawnCode?: string,
+ ): Promise<RlmChildRun> {
+ const { name: rawName, model: rawModel, ...unsupported } = kwargs;
const unsupportedKwargs = Object.keys(unsupported);
if (unsupportedKwargs.length > 0) {
throw new Error(`Unsupported rlm.run kwargs: ${unsupportedKwargs.sort().join(", ")}`);
}
const requestedSessionName = normalizeRequestedRlmSubagentSessionName(rawName);
+ const requestedModel = normalizeRequestedRlmSubagentModel(rawModel);
if (requestedSessionName) {
assertDirectAgentMessageTarget(requestedSessionName);
- this._assertRlmSubagentSessionNameAvailable(requestedSessionName);
}
if (this._rlmDepth >= this._rlmMaxDepth) {
throw new Error(
`RLM recursion depth limit reached (RLM_DEPTH=${this._rlmDepth}, RLM_MAX_DEPTH=${this._rlmMaxDepth})`,
);
}
- const model = this.model;
- if (!model) {
- throw new Error(formatNoModelSelectedMessage());
+ if (requestedSessionName) {
+ this._assertRlmSubagentSessionNameAvailable(requestedSessionName);
+ this._pendingRlmSubagentSessionNames.add(requestedSessionName);
+ }
+ let modelSelection: RlmSubagentModelSelection;
+ try {
+ modelSelection = await this._resolveRlmSubagentModel(requestedModel);
+ } finally {
+ if (requestedSessionName) {
+ this._pendingRlmSubagentSessionNames.delete(requestedSessionName);
+ }
+ }
+ if (this._disposed || this._disposing) {
+ throw new Error("Cannot start a subagent after the parent session has been disposed");
}
const childSessionDir = this._createChildRlmSessionDir();
@@ -6785,12 +6862,14 @@ export class AgentSession {
this._activeRlmChildRuns.set(run.id, run);
// Status-only relay; the conversation is read from the child's own session.
const emitChildUpdate = () => {
+ const childModel = childSession?.model ?? modelSelection.model;
this._emit({
type: "rlm_child_update",
child: {
id: childNodeId,
parentId: this._rlmParentNodeId,
sessionName: childSession?.sessionName ?? sessionName,
+ model: `${childModel.provider}/${childModel.id}`,
label,
status: run.status,
durationMs,
@@ -6826,7 +6905,7 @@ export class AgentSession {
sessionName,
spawnCode,
sessionDir: childSessionDir,
- model,
+ model: modelSelection.model,
}),
onSessionPublished: publishChildSession,
};
@@ -6923,6 +7002,8 @@ export class AgentSession {
usage,
turns: child._assistantTurnCount(),
session_dir: childSessionDir,
+ model: `${modelSelection.model.provider}/${modelSelection.model.id}`,
+ ...(modelSelection.warning ? { warning: modelSelection.warning } : {}),
};
run.result = result;
return { ...result, assistantUsage };
@@ -7010,7 +7091,7 @@ export class AgentSession {
}
async runRlmChild(prompt: string, kwargs: Record<string, unknown> = {}, spawnCode?: string): Promise<RlmRunResult> {
- const run = this._startRlmChildRun(prompt, kwargs, spawnCode);
+ const run = await this._startRlmChildRun(prompt, kwargs, spawnCode);
if (!run.task) {
throw new Error("RLM child failed to start");
}
@@ -7020,6 +7101,8 @@ export class AgentSession {
usage: result.usage,
turns: result.turns,
session_dir: result.session_dir,
+ model: result.model,
+ ...(result.warning ? { warning: result.warning } : {}),
};
}
diff --git a/packages/coding-agent/src/core/kernel/bootstrap.ts b/packages/coding-agent/src/core/kernel/bootstrap.ts
index 68a1cf6612..9b12b4b413 100644
--- a/packages/coding-agent/src/core/kernel/bootstrap.ts
+++ b/packages/coding-agent/src/core/kernel/bootstrap.ts
@@ -51,7 +51,7 @@ const REQUIRED_HARNESS_METHODS = [
"delete_prompt_note",
"record_refinement",
];
-const RUNTIME_READY_CHECK = `import inspect; import rlm; from rlm import McpIntegration; from rlm.harness import HarnessEntry; _harness_methods = ${JSON.stringify(REQUIRED_HARNESS_METHODS)}; assert hasattr(rlm, 'run'); assert callable(rlm); assert hasattr(rlm, 'rlm'); assert callable(rlm.rlm); assert callable(rlm.host_request); assert hasattr(rlm, 'harness'); assert hasattr(rlm, 'get_harness_state'); assert hasattr(rlm.rlm, 'harness'); assert hasattr(rlm.rlm, 'get_harness_state'); assert all(callable(getattr(_harness, _method, None)) for _harness in (rlm.harness, rlm.rlm.harness) for _method in _harness_methods); assert 'reference' in HarnessEntry.__dataclass_fields__; assert 'scope' in HarnessEntry.__dataclass_fields__; assert 'reference' in inspect.signature(rlm.harness.create_skill).parameters; assert 'reference' in inspect.signature(rlm.harness.update_skill).parameters; assert 'global_' in inspect.signature(rlm.harness.create_memory).parameters; assert 'global_' in inspect.signature(rlm.get_harness_state).parameters; assert not hasattr(rlm, 'background'); assert not hasattr(rlm.rlm, 'background')`;
+const RUNTIME_READY_CHECK = `import inspect; import rlm; from rlm import McpIntegration; from rlm.harness import HarnessEntry; _harness_methods = ${JSON.stringify(REQUIRED_HARNESS_METHODS)}; assert hasattr(rlm, 'run'); assert callable(rlm); assert hasattr(rlm, 'rlm'); assert callable(rlm.rlm); assert callable(rlm.host_request); assert callable(rlm.find_models); assert callable(rlm.rlm.find_models); assert hasattr(rlm, 'harness'); assert hasattr(rlm, 'get_harness_state'); assert hasattr(rlm.rlm, 'harness'); assert hasattr(rlm.rlm, 'get_harness_state'); assert all(callable(getattr(_harness, _method, None)) for _harness in (rlm.harness, rlm.rlm.harness) for _method in _harness_methods); assert 'reference' in HarnessEntry.__dataclass_fields__; assert 'scope' in HarnessEntry.__dataclass_fields__; assert 'reference' in inspect.signature(rlm.harness.create_skill).parameters; assert 'reference' in inspect.signature(rlm.harness.update_skill).parameters; assert 'global_' in inspect.signature(rlm.harness.create_memory).parameters; assert 'global_' in inspect.signature(rlm.get_harness_state).parameters; assert not hasattr(rlm, 'background'); assert not hasattr(rlm.rlm, 'background')`;
const BOOTSTRAP_VERSION_FILE = ".bootstrap-version";
const BOOTSTRAP_LOCK_NAME = ".bootstrap.lock";
const BOOTSTRAP_LOCK_RETRY_MS = 100;
diff --git a/packages/coding-agent/src/core/model-registry.ts b/packages/coding-agent/src/core/model-registry.ts
index eb41d60c16..c9e78a4248 100644
--- a/packages/coding-agent/src/core/model-registry.ts
+++ b/packages/coding-agent/src/core/model-registry.ts
@@ -2,6 +2,7 @@
* Model registry - manages built-in and custom models, provides API key resolution.
*/
+import { Buffer } from "node:buffer";
import { createHash } from "node:crypto";
import {
type AnthropicMessagesCompat,
@@ -26,7 +27,7 @@ import { join } from "path";
import { type Static, type TProperties, Type } from "typebox";
import type { Validator } from "typebox/compile";
import type { TLocalizedValidationError } from "typebox/error";
-import { getAgentDir } from "../config.js";
+import { getAgentDir, VERSION } from "../config.js";
import type { AuthSourceToken, AuthStatus, AuthStorage } from "./auth-storage.js";
import { PRIME_INFERENCE_PROVIDER_ID } from "./prime-inference-auth.js";
import {
@@ -354,6 +355,47 @@ function applyModelOverride(model: Model<Api>, override: ModelOverride): Model<A
/** Clear the config value command cache. Exported for testing. */
export const clearApiKeyCache = clearConfigValueCache;
+function readOpenAICodexAccountId(token: string): string | undefined {
+ try {
+ const payload = JSON.parse(Buffer.from(token.split(".")[1] ?? "", "base64url").toString("utf8")) as {
+ "https://api.openai.com/auth"?: { chatgpt_account_id?: unknown };
+ };
+ const accountId = payload["https://api.openai.com/auth"]?.chatgpt_account_id;
+ return typeof accountId === "string" && accountId.length > 0 ? accountId : undefined;
+ } catch {
+ return undefined;
+ }
+}
+
+function openAICodexModelsUrl(baseUrl: string): string {
+ const normalized = baseUrl.replace(/\/+$/, "");
+ let path: string;
+ if (normalized.endsWith("/codex/responses")) {
+ path = `${normalized.slice(0, -"/responses".length)}/models`;
+ } else if (normalized.endsWith("/codex")) {
+ path = `${normalized}/models`;
+ } else {
+ path = `${normalized}/codex/models`;
+ }
+ const url = new URL(path);
+ url.searchParams.set("client_version", VERSION);
+ return url.toString();
+}
+
+function readOpenAICodexModelIds(value: unknown): Set<string> {
+ if (!value || typeof value !== "object" || !("models" in value) || !Array.isArray(value.models)) {
+ throw new Error("Invalid OpenAI Codex model catalog");
+ }
+ return new Set(
+ value.models.flatMap((model) => {
+ if (!model || typeof model !== "object" || !("slug" in model) || typeof model.slug !== "string") {
+ return [];
+ }
+ return [model.slug];
+ }),
+ );
+}
+
/**
* Model registry - loads and manages models, resolves API keys via AuthStorage.
*/
@@ -367,6 +409,7 @@ export class ModelRegistry {
private authorizedPrivatePrimeInferenceModelIds = new Set<string>();
private authorizedPrivatePrimeInferenceTeamId: string | undefined;
private explicitPrivatePrimeInferenceModelIds = new Set<string>();
+ private openAICodexModelsCache: { authFingerprint: string; modelIds: Set<string>; refreshedAt: number } | undefined;
private loadError: string | undefined = undefined;
/** Re-register dynamic OAuth providers (e.g. user MCP servers) after refresh() resets the registry. */
@@ -713,11 +756,21 @@ export class ModelRegistry {
const previousPrivateModelIds = new Set(this.authorizedPrivatePrimeInferenceModelIds);
const previousTeamId = this.authorizedPrivatePrimeInferenceTeamId;
this.refresh();
+ await this.refreshPrivatePrimeInferenceAuthorization(previousPrivateModelIds, previousTeamId);
+ return this.getAvailable();
+ }
+
+ private async refreshPrivatePrimeInferenceAuthorization(
+ previousPrivateModelIds = new Set(this.authorizedPrivatePrimeInferenceModelIds),
+ previousTeamId = this.authorizedPrivatePrimeInferenceTeamId,
+ ): Promise<void> {
const apiKey = await this.authStorage.getApiKey(PRIME_INFERENCE_PROVIDER_ID);
const teamHeaders = this.authStorage.getProviderHeaders(PRIME_INFERENCE_PROVIDER_ID);
const teamId = teamHeaders?.["X-Prime-Team-ID"];
if (!apiKey || !teamHeaders || !teamId) {
- return this.getAvailable();
+ this.authorizedPrivatePrimeInferenceModelIds.clear();
+ this.authorizedPrivatePrimeInferenceTeamId = undefined;
+ return;
}
try {
@@ -730,9 +783,11 @@ export class ModelRegistry {
if (teamId === previousTeamId) {
this.authorizedPrivatePrimeInferenceModelIds = previousPrivateModelIds;
this.authorizedPrivatePrimeInferenceTeamId = teamId;
+ } else {
+ this.authorizedPrivatePrimeInferenceModelIds.clear();
+ this.authorizedPrivatePrimeInferenceTeamId = undefined;
}
}
- return this.getAvailable();
}
async canUseModel(model: Model<Api>): Promise<boolean> {
@@ -747,6 +802,54 @@ export class ModelRegistry {
return availableModels.some((candidate) => candidate.provider === model.provider && candidate.id === model.id);
}
+ async getExecutableModels(): Promise<Model<Api>[]> {
+ await this.refreshPrivatePrimeInferenceAuthorization();
+ const availableModels = this.getAvailable();
+ const codexModels = availableModels.filter((model) => model.provider === "openai-codex");
+ if (codexModels.length === 0) {
+ return availableModels;
+ }
+
+ const auth = await this.getApiKeyAndHeaders(codexModels[0]!);
+ if (!auth.ok || !auth.apiKey) {
+ return availableModels.filter((model) => model.provider !== "openai-codex");
+ }
+ const authFingerprint = createHash("sha256").update(auth.apiKey).digest("hex");
+ const cached = this.openAICodexModelsCache;
+ if (cached?.authFingerprint === authFingerprint && Date.now() - cached.refreshedAt < 300_000) {
+ return availableModels.filter((model) => model.provider !== "openai-codex" || cached.modelIds.has(model.id));
+ }
+
+ const accountId = readOpenAICodexAccountId(auth.apiKey);
+ if (!accountId) {
+ return availableModels.filter((model) => model.provider !== "openai-codex");
+ }
+ try {
+ const response = await fetch(openAICodexModelsUrl(codexModels[0]!.baseUrl), {
+ headers: {
+ ...auth.headers,
+ Authorization: `Bearer ${auth.apiKey}`,
+ "chatgpt-account-id": accountId,
+ originator: "pi",
+ },
+ signal: AbortSignal.timeout(5_000),
+ });
+ if (!response.ok) {
+ throw new Error(`OpenAI Codex model discovery failed with HTTP ${response.status}`);
+ }
+ const modelIds = readOpenAICodexModelIds(await response.json());
+ this.openAICodexModelsCache = { authFingerprint, modelIds, refreshedAt: Date.now() };
+ return availableModels.filter((model) => model.provider !== "openai-codex" || modelIds.has(model.id));
+ } catch {
+ if (cached?.authFingerprint === authFingerprint && Date.now() - cached.refreshedAt < 300_000) {
+ return availableModels.filter(
+ (model) => model.provider !== "openai-codex" || cached.modelIds.has(model.id),
+ );
+ }
+ return availableModels.filter((model) => model.provider !== "openai-codex");
+ }
+ }
+
/**
* Find a model by provider and ID.
*/
diff --git a/packages/coding-agent/src/core/prompts/rlm.ts b/packages/coding-agent/src/core/prompts/rlm.ts
index d6d98087ff..4bfd21331a 100644
--- a/packages/coding-agent/src/core/prompts/rlm.ts
+++ b/packages/coding-agent/src/core/prompts/rlm.ts
@@ -83,8 +83,10 @@ export function buildRlmPrompt(options: RlmPromptOptions): string {
if (allowRecursion && hasIpython) {
parts.push(
"",
- "A callable `rlm` is already in your global namespace. It returns an `RLMResult` with `.answer` (string), `.usage`, `.turns`, and `.session_dir`. A direct `await rlm('sub-task')` is valid only when the result is immediately required.",
+ "A callable `rlm` is already in your global namespace. It returns an `RLMResult` with `.answer` (string), `.usage`, `.turns`, `.session_dir`, `.model`, and optional `.warning`. A direct `await rlm('sub-task')` is valid only when the result is immediately required.",
"Choose a stable child name with `await rlm('sub-task', name='api-reviewer')`; names must be non-empty and unique among addressable sessions. If omitted, the host generates a readable unique name.",
+ "A child inherits your current model by default. When the user or an applicable skill requests a different model, search the bounded authenticated catalog with `matches = await rlm.find_models('requested model')`, choose from each match's `provider`, `id`, `name`, and `selector`, then pass the exact `provider/model` selector with `model=matches[0].selector`. Do not choose a different model on your own.",
+ "If an `RLMResult.warning` is set, the requested model could not be used and the child fell back to `.model`; follow the warning and tell the user which model actually ran.",
"Sub-agents should not block Prime Agent by default: start them with `task = asyncio.create_task(rlm('sub-task'))`, keep the task handle, continue any independent work, and await the task only when you need its result.",
"For long-running fan-out, do not rely only on in-memory `asyncio.Task` handles: they can be lost if the kernel restarts or state is restored. Recover the current parent session's automatic child registry with `children = await rlm.list_subagents()`; each entry exposes `rlm_child_id`, `active_session_id`, `session_id`, `session_name`, `session_dir`, and `status`.",
"Delete a running or retained direct child with `await rlm.delete_subagent(child)` (or pass its name/ID); deletion cancels running work, closes the child runtime, and removes it from the registry and daemon addressability.",
@@ -118,6 +120,8 @@ export function buildSubagentGuidance(): string {
"The host automatically keeps a parent-scoped subagent registry across kernel restarts, state restore, and compaction. Recover it with `children = await rlm.list_subagents()` instead of maintaining a separate registry file or relying on lost `asyncio.Task` handles.",
"Successful subagent sessions remain in that registry after their initial `rlm()` call finishes, but only while the current parent session remains open. Failed or cancelled children are removed, and retained children close when their parent session closes.",
"Choose a child name at spawn time with `rlm('task', name='api-reviewer')`, or let the host generate a readable, unique default `session_name`. If the `agent_observe` skill is installed and a registry entry has `active_session_id`, inspect it by name with `await agent_observe.get_agent(child.session_name)` or read bounded previews with `await agent_observe.recent_messages(child.session_name, limit=...)`.",
+ "Subagents inherit the parent model. Use `model=...` only when the user or an applicable skill requests another model; call `rlm.find_models()` for a bounded authenticated shortlist and pass one returned exact `selector`. The selected model remains attached to that child across later turns.",
+ "When an `RLMResult` has a `.warning`, tell the user which `.model` actually ran instead of the requested model.",
"If the `agent_message` skill is installed and a registry entry has `active_session_id`, continue that same child by name with `await agent_message.send(child.session_name, message, mode='auto')`; use `mode='steer'` only when you intend to interrupt current work.",
"Delete a direct child by registry entry or selector with `await rlm.delete_subagent(child)` or `await rlm.delete_subagent('api-reviewer')`. Deleting a running child cancels it first; deleting any child closes its runtime, removes it from the parent registry, and makes it unavailable to messaging and observation.",
"",
diff --git a/packages/coding-agent/src/core/rlm-runtime.ts b/packages/coding-agent/src/core/rlm-runtime.ts
index 5f00f818b2..eb273950ce 100644
--- a/packages/coding-agent/src/core/rlm-runtime.ts
+++ b/packages/coding-agent/src/core/rlm-runtime.ts
@@ -1,5 +1,5 @@
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
-import type { Model, ServiceTier, Usage } from "@earendil-works/pi-ai";
+import type { Api, Model, ServiceTier, Usage } from "@earendil-works/pi-ai";
import type { AgentSession } from "./agent-session.js";
import type { ToolDefinition } from "./extensions/index.js";
import type { HostRequestHandler } from "./kernel/index.js";
@@ -21,6 +21,8 @@ export interface RlmRunResult {
usage: RlmUsage;
turns: number;
session_dir: string | null;
+ model: string;
+ warning?: string;
}
export interface RlmInternalRunResult extends RlmRunResult {
@@ -46,11 +48,25 @@ export interface RlmDeleteSubagentResult {
subagent: RlmSubagentRegistryEntry;
}
+export interface RlmModelMatch {
+ provider: string;
+ id: string;
+ name: string;
+ selector: string;
+}
+
+export interface RlmFindModelsResult {
+ models: RlmModelMatch[];
+}
+
export type RlmRunHandler = (request: RlmRunRequest) => Promise<RlmRunResult>;
export type RlmListSubagentsHandler = () => RlmListSubagentsResult;
export type RlmDeleteSubagentHandler = (target: string) => Promise<RlmDeleteSubagentResult>;
+export type RlmFindModelsHandler = (query: string, limit: number) => RlmFindModelsResult | Promise<RlmFindModelsResult>;
const RLM_SUBAGENT_SESSION_NAME_MAX_LENGTH = 64;
+export const DEFAULT_RLM_MODEL_SEARCH_LIMIT = 8;
+export const MAX_RLM_MODEL_SEARCH_LIMIT = 20;
/** Validate and normalize an orchestrator-supplied subagent session name. */
export function normalizeRequestedRlmSubagentSessionName(value: unknown): string | undefined {
@@ -70,6 +86,21 @@ export function normalizeRequestedRlmSubagentSessionName(value: unknown): string
return name;
}
+/** Validate and normalize an orchestrator-supplied subagent model reference. */
+export function normalizeRequestedRlmSubagentModel(value: unknown): string | undefined {
+ if (value === undefined) {
+ return undefined;
+ }
+ if (typeof value !== "string") {
+ throw new Error("rlm.run model must be a string");
+ }
+ const model = value.trim();
+ if (!model) {
+ throw new Error("rlm.run model must not be empty");
+ }
+ return model;
+}
+
/** Create a readable, collision-resistant default name usable as an agent-message selector. */
export function createDefaultRlmSubagentSessionName(prompt: string, childId: string): string {
const promptSlug = prompt
@@ -94,6 +125,39 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+function normalizeModelSearchText(value: string): string {
+ return value.toLowerCase().replace(/[^a-z0-9]+/g, "");
+}
+
+export function findRlmModelMatches(query: string, models: Model<Api>[], limit: number): RlmModelMatch[] {
+ const normalizedQuery = normalizeModelSearchText(query.trim());
+ return models
+ .map((model) => {
+ const selector = `${model.provider}/${model.id}`;
+ const fields = [selector, model.id, model.name || model.id];
+ const normalizedFields = fields.map(normalizeModelSearchText);
+ let score = normalizedQuery ? Number.POSITIVE_INFINITY : 0;
+ if (normalizedQuery) {
+ const exactIndex = normalizedFields.indexOf(normalizedQuery);
+ const prefixIndex = normalizedFields.findIndex((field) => field.startsWith(normalizedQuery));
+ const partialIndex = normalizedFields.findIndex((field) => field.includes(normalizedQuery));
+ if (exactIndex >= 0) score = exactIndex;
+ else if (prefixIndex >= 0) score = 3 + prefixIndex;
+ else if (partialIndex >= 0) score = 6 + partialIndex;
+ }
+ return { model, selector, score };
+ })
+ .filter((candidate) => Number.isFinite(candidate.score))
+ .sort((a, b) => a.score - b.score || a.selector.localeCompare(b.selector))
+ .slice(0, limit)
+ .map(({ model, selector }) => ({
+ provider: model.provider,
+ id: model.id,
+ name: model.name || model.id,
+ selector,
+ }));
+}
+
/** Adapt an RlmRunHandler into the typed "rlm.run" handler for the kernel host bridge. */
export function createRlmRunHostHandler(handler: RlmRunHandler): HostRequestHandler {
return async (payload) => {
@@ -107,6 +171,20 @@ export function createRlmRunHostHandler(handler: RlmRunHandler): HostRequestHand
};
}
+/** Search a bounded authenticated model catalog without adding it to the system prompt. */
+export function createRlmFindModelsHostHandler(handler: RlmFindModelsHandler): HostRequestHandler {
+ return async (payload) => {
+ if (typeof payload.query !== "string") {
+ throw new Error("rlm.find_models query must be a string");
+ }
+ const limit = payload.limit === undefined ? DEFAULT_RLM_MODEL_SEARCH_LIMIT : payload.limit;
+ if (!Number.isInteger(limit) || (limit as number) < 1 || (limit as number) > MAX_RLM_MODEL_SEARCH_LIMIT) {
+ throw new Error(`rlm.find_models limit must be an integer from 1 to ${MAX_RLM_MODEL_SEARCH_LIMIT}`);
+ }
+ return { models: (await handler(payload.query, limit as number)).models };
+ };
+}
+
/** Expose the current parent session's RLM child registry to its kernel. */
export function createRlmListSubagentsHostHandler(handler: RlmListSubagentsHandler): HostRequestHandler {
return async () => {
diff --git a/packages/coding-agent/src/core/tools/ipython.ts b/packages/coding-agent/src/core/tools/ipython.ts
index 3fc7a6cefe..7641fb74f6 100644
--- a/packages/coding-agent/src/core/tools/ipython.ts
+++ b/packages/coding-agent/src/core/tools/ipython.ts
@@ -51,6 +51,9 @@ except Exception as _prime_agent_rlm_error:
async def run(self, prompt, **kwargs):
self._raise_missing()
+ async def find_models(self, query="", limit=8):
+ self._raise_missing()
+
async def list_subagents(self):
self._raise_missing()
diff --git a/packages/coding-agent/src/modes/agent-connection/types.ts b/packages/coding-agent/src/modes/agent-connection/types.ts
index f67b0d14df..e1a9ec3e8a 100644
--- a/packages/coding-agent/src/modes/agent-connection/types.ts
+++ b/packages/coding-agent/src/modes/agent-connection/types.ts
@@ -479,6 +479,8 @@ export interface AgentConnectionRlmChildAgentSnapshot {
activeSessionId?: string;
/** Stable daemon-visible session name for addressing/displaying the child. */
sessionName?: string;
+ /** Exact provider/model selector used by the child. */
+ model?: string;
label: string;
status: AgentConnectionRlmChildAgentStatus;
durationMs?: number;
diff --git a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts b/packages/coding-agent/src/modes/daemon/daemon-session-list.ts
index 946e4bb6e5..71eec1d9e6 100644
--- a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts
+++ b/packages/coding-agent/src/modes/daemon/daemon-session-list.ts
@@ -310,6 +310,7 @@ function rlmChildSnapshotForActiveSession(
parentId: parentNodeId,
activeSessionId: activeSession.activeSessionId,
sessionName: session.sessionName,
+ model: session.model ? `${session.model.provider}/${session.model.id}` : undefined,
label: rlmChildLabel(metadata.prompt ?? ""),
status,
answerPreview,
diff --git a/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts b/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
index c4b89e7c91..1627a212ad 100644
--- a/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
+++ b/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
@@ -26,6 +26,8 @@ export interface ChildAgentInspectorNode {
activeSessionId?: string;
/** Stable daemon-visible session name for addressing/displaying the child. */
sessionName?: string;
+ /** Exact provider/model selector used by the child. */
+ model?: string;
label: string;
status: ChildAgentStatus;
durationMs?: number;
@@ -191,6 +193,25 @@ function padTableCell(value: string, width: number, ellipsis = ""): string {
return truncated + " ".repeat(Math.max(0, width - visibleWidth(truncated)));
}
+function truncateModelSelector(value: string, width: number): string {
+ if (visibleWidth(value) <= width) {
+ return value;
+ }
+ if (width <= 1) {
+ return truncateToWidth(value, width, "");
+ }
+ const prefixWidth = Math.max(1, Math.floor((width - 1) * 0.35));
+ const suffixWidth = width - prefixWidth - 1;
+ let suffix = "";
+ for (const character of [...value].reverse()) {
+ if (visibleWidth(character + suffix) > suffixWidth) {
+ break;
+ }
+ suffix = character + suffix;
+ }
+ return `${truncateToWidth(value, prefixWidth, "")}…${suffix}`;
+}
+
// Rows shown at once in the inline list below the prompt; extras scroll.
const SUMMARY_VISIBLE_ROWS = 5;
const SUMMARY_LIST_INDENT = 0;
@@ -198,12 +219,12 @@ const SHARED_PREFIX_MIN = 12;
const SUMMARY_COLUMN_GAP = 2;
const SUMMARY_PROMPT_MAX_WIDTH = 24;
const SUMMARY_RECAP_MIN_WIDTH = 12;
-const SUMMARY_TOOLS_WIDTH = 7;
+const SUMMARY_MODEL_WIDTH = 20;
const SUMMARY_TOKENS_WIDTH = 8;
const SUMMARY_DURATION_WIDTH = 4;
const SUMMARY_TOKEN_TIME_GAP = 2;
const SUMMARY_METRICS_WIDTH =
- SUMMARY_TOOLS_WIDTH + 1 + SUMMARY_TOKENS_WIDTH + SUMMARY_TOKEN_TIME_GAP + SUMMARY_DURATION_WIDTH;
+ SUMMARY_MODEL_WIDTH + 1 + SUMMARY_TOKENS_WIDTH + SUMMARY_TOKEN_TIME_GAP + SUMMARY_DURATION_WIDTH;
// Opening chars of the prompt kept for context before eliding a shared prefix.
const PROMPT_LEADING_CONTEXT = 6;
// Words of context kept on each side of the divergence.
@@ -449,16 +470,13 @@ export class ChildAgentSummaryComponent implements Component, Focusable {
const agentCell = theme.fg("muted", padTableCell(agentLabel, agentWidth));
const promptCell = this.elidePrompt(this.entryLabel(entry), sharedPrefix, sharedSuffix, promptWidth);
const recapCell = theme.fg("muted", padTableCell(childAgentRecap(entry.node), recapWidth, "…"));
- const tools =
- entry.node.toolUseCount === undefined
- ? ""
- : `${entry.node.toolUseCount} ${entry.node.toolUseCount === 1 ? "tool" : "tools"}`;
+ const model = entry.node.model ?? "";
const tokens = entry.node.tokenCount === undefined ? "" : `${formatTokenCount(entry.node.tokenCount)} tok`;
const duration = formatChildAgentDuration(entry.node.durationMs);
- const toolsCell = padTableCell(tools, SUMMARY_TOOLS_WIDTH, "…");
+ const modelCell = padTableCell(truncateModelSelector(model, SUMMARY_MODEL_WIDTH), SUMMARY_MODEL_WIDTH);
const tokensCell = padTableCell(tokens, SUMMARY_TOKENS_WIDTH, "…");
const durationCell = padTableCell(duration, SUMMARY_DURATION_WIDTH, "…");
- const metrics = `${toolsCell} ${tokensCell}${" ".repeat(SUMMARY_TOKEN_TIME_GAP)}${durationCell}`;
+ const metrics = `${modelCell} ${tokensCell}${" ".repeat(SUMMARY_TOKEN_TIME_GAP)}${durationCell}`;
const gap = " ".repeat(SUMMARY_COLUMN_GAP);
return `${indent}${icon} ${agentCell}${gap}${promptCell}${gap}${recapCell}${gap}${theme.fg("muted", metrics)}`;
}
@@ -804,6 +822,7 @@ export class ChildAgentDetailComponent implements Component, Focusable {
}
private detailHintLine(width: number): string {
+ const backAction = keyAction("app.agents.back", this.backHintLabel, { primaryOnly: true });
const expandAction = keyAction("app.tools.expand", this.toolsExpanded ? "to collapse" : "to expand");
const killable = this.node !== undefined && isKillableChildAgentStatus(this.node.status);
const stopAction = !killable
@@ -811,10 +830,14 @@ export class ChildAgentDetailComponent implements Component, Focusable {
: this.isKillConfirmationVisible()
? theme.fg("error", `${keyText("app.agents.delete", { primaryOnly: true }).trim()} again to stop`)
: keyAction("app.agents.delete", "stop", { primaryOnly: true });
- return hintLine(
- [keyAction("app.agents.back", this.backHintLabel, { primaryOnly: true }), expandAction, stopAction],
- width,
- );
+ const actions = [backAction, expandAction, stopAction];
+ const separatorWidth = visibleWidth(joinHints(["", ""]));
+ const modelWidth = Math.max(0, width - visibleWidth(joinHints(actions)) - separatorWidth);
+ const model =
+ this.node?.model && modelWidth >= 4
+ ? theme.fg("muted", truncateToWidth(this.node.model, modelWidth, "…"))
+ : undefined;
+ return hintLine([backAction, model, expandAction, stopAction], width);
}
private panelLine(line: string, width: number): string {
diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
index 967a743a87..ca36b71c8a 100644
--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts
+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
@@ -317,6 +317,7 @@ export function mergeChildAgentSnapshots(
parentId: incoming.parentId ?? previous.parentId,
activeSessionId: incoming.activeSessionId ?? previous.activeSessionId,
sessionName: incoming.sessionName ?? previous.sessionName,
+ model: incoming.model ?? previous.model,
durationMs: incoming.durationMs ?? previous.durationMs,
answerPreview: incoming.answerPreview ?? previous.answerPreview,
toolUseCount:
@@ -339,6 +340,7 @@ function childAgentSummaryChanged(
return (
previous.parentId !== next.parentId ||
previous.sessionName !== next.sessionName ||
+ previous.model !== next.model ||
previous.label !== next.label ||
previous.status !== next.status ||
previous.durationMs !== next.durationMs ||
@@ -5663,6 +5665,7 @@ export class InteractiveMode {
id: child.id,
activeSessionId: child.activeSessionId,
sessionName: child.sessionName,
+ model: child.model,
label: child.label,
status: child.status,
durationMs: child.durationMs,
diff --git a/packages/coding-agent/test/agent-session-recursion.test.ts b/packages/coding-agent/test/agent-session-recursion.test.ts
index 0e2657160a..be2223c70e 100644
--- a/packages/coding-agent/test/agent-session-recursion.test.ts
+++ b/packages/coding-agent/test/agent-session-recursion.test.ts
@@ -723,8 +723,8 @@ describe("AgentSession rlm recursion", () => {
it("rejects unsupported rlm.run kwargs loudly", async () => {
const root = createSession();
- await expect(root.runRlmChild("nested", { model: "other-model" })).rejects.toThrow(
- "Unsupported rlm.run kwargs: model",
+ await expect(root.runRlmChild("nested", { temperature: 0 })).rejects.toThrow(
+ "Unsupported rlm.run kwargs: temperature",
);
});
@@ -1322,6 +1322,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
};
}),
},
@@ -1349,6 +1350,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
});
expect(byCommId.get("comm-b")).toEqual({
status: "ok",
@@ -1356,6 +1358,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
});
} finally {
await manager.dispose();
@@ -1375,6 +1378,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
};
}),
},
@@ -1400,6 +1404,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
},
});
} finally {
@@ -1419,6 +1424,7 @@ describe("AgentSession rlm recursion", () => {
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
};
}),
},
@@ -1496,6 +1502,7 @@ print(_result.answer)
usage: { prompt_tokens: 1, completion_tokens: 1 },
turns: 1,
session_dir: null,
+ model: "test/model",
})),
},
});
diff --git a/packages/coding-agent/test/child-agent-summary-list.test.ts b/packages/coding-agent/test/child-agent-summary-list.test.ts
index ee8bb3cbae..699af06685 100644
--- a/packages/coding-agent/test/child-agent-summary-list.test.ts
+++ b/packages/coding-agent/test/child-agent-summary-list.test.ts
@@ -1,6 +1,8 @@
-import { visibleWidth } from "@earendil-works/pi-tui";
+import { setKeybindings, visibleWidth } from "@earendil-works/pi-tui";
import { beforeAll, describe, expect, it } from "vitest";
+import { KeybindingsManager } from "../src/core/keybindings.js";
import {
+ ChildAgentDetailComponent,
type ChildAgentInspectorNode,
ChildAgentSummaryComponent,
} from "../src/modes/interactive/components/child-agent-inspector.js";
@@ -18,6 +20,7 @@ function node(id: string, status: ChildAgentInspectorNode["status"] = "running")
describe("ChildAgentSummaryComponent inline list", () => {
beforeAll(() => {
initTheme("dark");
+ setKeybindings(new KeybindingsManager());
});
it("renders one row per subagent when within the cap", () => {
@@ -50,12 +53,21 @@ describe("ChildAgentSummaryComponent inline list", () => {
expect(out).not.toContain(" a ");
});
- it("shows tool and token usage beside each subagent", () => {
+ it("shows the model and token usage without a tool column", () => {
const summary = new ChildAgentSummaryComponent();
- summary.setNodes([{ ...node("a"), toolUseCount: 3, tokenCount: 41_000 }]);
+ summary.setNodes([{ ...node("a"), model: "openai/gpt-5.4", toolUseCount: 3, tokenCount: 41_000 }]);
const out = stripAnsi(summary.render(80).join("\n"));
- expect(out).toContain("3 tools");
+ expect(out).toContain("openai/gpt-5.4");
expect(out).toContain("41k tok");
+ expect(out).not.toContain("tools");
+ });
+
+ it("preserves the model suffix when its selector is truncated", () => {
+ const summary = new ChildAgentSummaryComponent();
+ summary.setNodes([{ ...node("a"), model: "prime-inference/internal/glm-5.2-fast" }]);
+ const out = stripAnsi(summary.render(80).join("\n"));
+ expect(out).toContain("prime-…/glm-5.2-fast");
+ expect(out).not.toContain("prime-inference/int…");
});
it("shows recaps in a fixed column and falls back to activity", () => {
@@ -182,6 +194,28 @@ describe("ChildAgentSummaryComponent inline list", () => {
expect(out).toMatch(/41k tok\s{3}5s/);
});
+ it("shows the model to the right of the back action in detail view", () => {
+ const detail = new ChildAgentDetailComponent();
+ detail.setNode({ ...node("a"), model: "anthropic/claude-opus-4-7" });
+ const hint = stripAnsi(detail.render(100).at(-1) ?? "");
+ expect(hint).toContain("back to chat");
+ expect(hint).toContain("anthropic/claude-opus-4-7");
+ expect(hint.indexOf("anthropic/claude-opus-4-7")).toBeGreaterThan(hint.indexOf("back to chat"));
+ });
+
+ it("truncates long models before hiding detail actions", () => {
+ const detail = new ChildAgentDetailComponent();
+ detail.setNode({
+ ...node("a"),
+ model: "provider-with-a-long-name/model-with-an-even-longer-name",
+ });
+ const hint = stripAnsi(detail.render(60).at(-1) ?? "");
+ expect(hint).toContain("back to chat");
+ expect(hint).toContain("provider-…");
+ expect(hint).toContain("to expand");
+ expect(hint).toContain("stop");
+ });
+
it("keeps the starting prompt compact so the recap gets more room", () => {
const summary = new ChildAgentSummaryComponent();
const prompt = "Investigate every scheduler dispatch path and identify all possible starvation conditions";
diff --git a/packages/coding-agent/test/daemon-session-list.test.ts b/packages/coding-agent/test/daemon-session-list.test.ts
index 393bf95f38..065de503f5 100644
--- a/packages/coding-agent/test/daemon-session-list.test.ts
+++ b/packages/coding-agent/test/daemon-session-list.test.ts
@@ -372,6 +372,7 @@ describe("buildRlmChildSnapshots", () => {
const parent = makeState({ activeSessionId: "parent", sessionFile: "/tmp/parent.jsonl" });
const child = makeState({
activeSessionId: "child",
+ model: { provider: "anthropic", id: "claude-opus-4-7" },
isStreaming: true,
metadata: {
kind: "subagent",
@@ -423,6 +424,7 @@ describe("buildRlmChildSnapshots", () => {
["sub-bbb", "sub-aaa", "done"],
]);
expect(snapshots[0]).toMatchObject({
+ model: "anthropic/claude-opus-4-7",
label: "Summarize the repo layout",
answerPreview: "The repo is an npm workspace.",
toolUseCount: 1,
@@ -529,6 +531,7 @@ describe("resolveAttachModelFallbackMessage", () => {
interface StateOptions {
activeSessionId: string;
+ model?: { provider: string; id: string };
sessionFile?: string;
sessionId?: string;
isStreaming?: boolean;
@@ -570,7 +573,7 @@ function makeState(options: StateOptions): ActiveSessionState {
metadata: options.metadata ?? { kind: "top-level", createdAt: 1 },
diagnostics: [],
session: {
- model: undefined,
+ model: options.model,
thinkingLevel: "off",
isStreaming: options.isStreaming ?? false,
isCompacting: false,
diff --git a/packages/coding-agent/test/interactive-mode-status.test.ts b/packages/coding-agent/test/interactive-mode-status.test.ts
index d54a3a8d06..1b8c0f7a17 100644
--- a/packages/coding-agent/test/interactive-mode-status.test.ts
+++ b/packages/coding-agent/test/interactive-mode-status.test.ts
@@ -113,6 +113,7 @@ describe("mergeChildAgentSnapshots", () => {
const rich: AgentConnectionRlmChildAgentSnapshot = {
id: "child-1",
activeSessionId: "active-child-1",
+ model: "openai/gpt-5.4",
label: "Inspect the scheduler",
status: "running",
durationMs: 5_000,
@@ -134,6 +135,7 @@ describe("mergeChildAgentSnapshots", () => {
expect(merged).toMatchObject({
activeSessionId: "active-child-1",
+ model: "openai/gpt-5.4",
durationMs: 5_000,
answerPreview: "Reading the queue",
toolUseCount: 4,
diff --git a/packages/coding-agent/test/ipython-bootstrap.test.ts b/packages/coding-agent/test/ipython-bootstrap.test.ts
index cad537f639..8aba99b463 100644
--- a/packages/coding-agent/test/ipython-bootstrap.test.ts
+++ b/packages/coding-agent/test/ipython-bootstrap.test.ts
@@ -13,6 +13,7 @@ describe("IPython RLM bootstrap", () => {
it("gives subagent registry operations the actionable missing-runtime fallback", () => {
const code = buildRlmBootstrapCode();
+ expect(code).toContain('async def find_models(self, query="", limit=8)');
expect(code).toContain("async def list_subagents(self)");
expect(code).toContain("async def delete_subagent(self, target)");
expect(code).toContain("self._raise_missing()");
diff --git a/packages/coding-agent/test/marquee-components.test.ts b/packages/coding-agent/test/marquee-components.test.ts
index b2e374defc..103f77f77e 100644
--- a/packages/coding-agent/test/marquee-components.test.ts
+++ b/packages/coding-agent/test/marquee-components.test.ts
@@ -436,12 +436,14 @@ describe("marquee TUI components", () => {
const node: ChildAgentInspectorNode = {
id: "sub-a",
label: "inspect training logs",
+ model: "openai/gpt-5.4",
status: "running",
sessionDir: "/tmp/session/sub-a",
children: [
{
id: "sub-b",
label: "check shard 2",
+ model: "anthropic/claude-sonnet-4-5",
status: "done",
sessionDir: "/tmp/session/sub-b",
},
@@ -456,7 +458,8 @@ describe("marquee TUI components", () => {
expect(summaryText).toContain("agents-sidebar");
expect(summaryText).toContain("37% context left");
expect(summaryText).toContain("S1");
- expect(summaryText).toContain("inspect training l…");
+ expect(summaryText).toContain("inspect train…");
+ expect(summaryText).toContain("openai/gpt-5.4");
const infoRow = summary.render(90)[0] ?? "";
expect(visibleWidth(infoRow)).toBe(90);
@@ -485,6 +488,7 @@ describe("marquee TUI components", () => {
expect(detail).toContain("inspect training logs");
expect(detail).toContain("reading shard metrics");
expect(detail).toContain("← back to chat");
+ expect(stripAnsi(detailComponent.render(80).at(-1) ?? "")).toContain("openai/gpt-5.4");
expect(detail).not.toContain("user: inspect training logs");
expect(detail).not.toContain("assistant: reading shard metrics");
});
diff --git a/packages/coding-agent/test/suite/regressions/3885-subagent-runtime-host.test.ts b/packages/coding-agent/test/suite/regressions/3885-subagent-runtime-host.test.ts
index eec8f415c4..49bd315a8e 100644
--- a/packages/coding-agent/test/suite/regressions/3885-subagent-runtime-host.test.ts
+++ b/packages/coding-agent/test/suite/regressions/3885-subagent-runtime-host.test.ts
@@ -41,7 +41,7 @@ describe("ENG-3885 subagent runtime host", () => {
vi.unstubAllEnvs();
});
- it("creates running RLM children as tracked AgentSessionRuntime instances", async () => {
+ it("creates explicitly modeled RLM children as tracked AgentSessionRuntime instances", async () => {
const tempDir = join(tmpdir(), `pi-3885-subagent-runtime-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
vi.stubEnv(SESSION_LEASES_ENABLED_ENV, "1");
@@ -129,8 +129,6 @@ describe("ENG-3885 subagent runtime host", () => {
}
});
- await runtime.session.setModel(faux.getModel("faux-child")!);
-
let releaseChild: () => void = () => {};
const release = new Promise<void>((resolve) => {
releaseChild = resolve;
@@ -146,7 +144,9 @@ describe("ENG-3885 subagent runtime host", () => {
},
]);
- const resultPromise = runtime.session.runRlmChild("inspect child runtime");
+ const resultPromise = runtime.session.runRlmChild("inspect child runtime", {
+ model: `${faux.getModel("faux-child")!.provider}/faux-child`,
+ });
await waitFor(() => childStarted && runtime.listSubagentRuntimes().length === 1);
const [childRuntime] = runtime.listSubagentRuntimes();
diff --git a/packages/coding-agent/test/suite/regressions/4649-subagent-model-selection.test.ts b/packages/coding-agent/test/suite/regressions/4649-subagent-model-selection.test.ts
new file mode 100644
index 0000000000..af0baf1dd7
--- /dev/null
+++ b/packages/coding-agent/test/suite/regressions/4649-subagent-model-selection.test.ts
@@ -0,0 +1,403 @@
+import { fauxAssistantMessage } from "@earendil-works/pi-ai";
+import { describe, expect, it, vi } from "vitest";
+import type { HostRequestHandlers } from "../../../src/core/kernel/index.js";
+import { SessionManager } from "../../../src/core/session-manager.js";
+import { createHarness } from "../harness.js";
+
+const provider = "faux-eng-4649";
+
+function openAICodexToken(accountId: string): string {
+ const payload = Buffer.from(
+ JSON.stringify({ "https://api.openai.com/auth": { chatgpt_account_id: accountId } }),
+ ).toString("base64url");
+ return `header.${payload}.signature`;
+}
+
+describe("ENG-4649 subagent model selection", () => {
+ it("searches a bounded authenticated catalog without advertising it", async () => {
+ const harness = await createHarness({
+ provider,
+ models: Array.from({ length: 320 }, (_, index) => ({ id: `model-${index}` })),
+ });
+ try {
+ const prompt = harness.session.agent.state.systemPrompt;
+ expect(prompt).not.toContain(`${provider}/model-319`);
+ const handlers = (
+ harness.session as unknown as { _createKernelHostHandlers(): HostRequestHandlers }
+ )._createKernelHostHandlers();
+ const findModels = handlers["rlm.find_models"];
+ if (!findModels) throw new Error("Missing rlm.find_models host handler");
+ await expect(findModels({ query: "model 319", limit: 5 })).resolves.toEqual({
+ models: [
+ {
+ provider,
+ id: "model-319",
+ name: "model-319",
+ selector: `${provider}/model-319`,
+ },
+ ],
+ });
+ await expect(findModels({ query: "model", limit: 21 })).rejects.toThrow("integer from 1 to 20");
+ harness.setResponses([fauxAssistantMessage("resolved child answer")]);
+
+ const result = await harness.session.runRlmChild("use the requested model", {
+ model: `${provider}/model-319`,
+ });
+
+ const childEntry = harness.session.listRlmSubagents().subagents[0];
+ const child = harness.session.getRlmChildSession(childEntry!.rlm_child_id);
+ expect(child?.model?.id).toBe("model-319");
+ expect(result.model).toBe(`${provider}/model-319`);
+ expect(result.warning).toBeUndefined();
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("omits providers whose credentials are marked expired", async () => {
+ const harness = await createHarness({ provider, models: [{ id: "parent-model" }, { id: "child-model" }] });
+ try {
+ expect((await harness.session.findRlmModels("child", 8)).models).toHaveLength(1);
+ expect(harness.session.modelRegistry.markProviderAuthStale(provider)).toBe(true);
+ expect(harness.session.modelRegistry.markProviderAuthStale(provider)).toBe(true);
+ expect(harness.session.modelRegistry.getProviderAuthStatus(provider)).toMatchObject({
+ source: "stale",
+ label: "expired",
+ });
+ await expect(harness.session.findRlmModels("", 8)).resolves.toEqual({ models: [] });
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("limits ChatGPT discovery and execution to the account model catalog", async () => {
+ const codexProvider = "openai-codex";
+ const harness = await createHarness({
+ provider: codexProvider,
+ models: [{ id: "parent-model" }, { id: "unsupported-model" }],
+ });
+ const fetchModels = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ models: [{ slug: "parent-model" }] }), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchModels);
+ try {
+ harness.authStorage.setRuntimeApiKey(codexProvider, openAICodexToken("account-1"));
+ const discovered = await harness.session.findRlmModels("", 20);
+ expect(discovered.models.map((model) => model.selector)).toEqual([`${codexProvider}/parent-model`]);
+ expect(fetchModels).toHaveBeenCalledWith(
+ expect.stringMatching(/\/codex\/models\?client_version=/),
+ expect.objectContaining({
+ headers: expect.objectContaining({ "chatgpt-account-id": "account-1" }),
+ }),
+ );
+
+ harness.setResponses([fauxAssistantMessage("parent fallback answer")]);
+ const result = await harness.session.runRlmChild("reject unsupported account model", {
+ model: `${codexProvider}/unsupported-model`,
+ });
+ expect(result.model).toBe(`${codexProvider}/parent-model`);
+ expect(result.warning).toContain(`not "${codexProvider}/unsupported-model"`);
+ } finally {
+ vi.unstubAllGlobals();
+ harness.cleanup();
+ }
+ });
+
+ it("includes private Prime models authorized for the selected team", async () => {
+ const harness = await createHarness({ provider, models: [{ id: "parent-model" }] });
+ const fetchModels = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ data: [{ id: "internal/glm-5.2-fast" }] }), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ }),
+ );
+ vi.stubGlobal("fetch", fetchModels);
+ try {
+ harness.authStorage.set("prime-inference", {
+ type: "api_key",
+ key: "prime-key",
+ primeTeam: { teamId: "engineering-team", name: "Prime Engineering" },
+ });
+
+ const discovered = await harness.session.findRlmModels("glm 5.2", 8);
+ expect(discovered.models.map((model) => model.selector)).toContain("prime-inference/internal/glm-5.2-fast");
+ expect(fetchModels).toHaveBeenCalledOnce();
+ } finally {
+ vi.unstubAllGlobals();
+ harness.cleanup();
+ }
+ });
+
+ it("does not reuse an expired ChatGPT model catalog after a refresh failure", async () => {
+ const codexProvider = "openai-codex";
+ const harness = await createHarness({ provider: codexProvider, models: [{ id: "parent-model" }] });
+ const fetchModels = vi
+ .fn()
+ .mockResolvedValueOnce(
+ new Response(JSON.stringify({ models: [{ slug: "parent-model" }] }), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ }),
+ )
+ .mockRejectedValueOnce(new Error("offline"));
+ vi.stubGlobal("fetch", fetchModels);
+ let now = Date.now();
+ const dateNow = vi.spyOn(Date, "now").mockImplementation(() => now);
+ try {
+ harness.authStorage.setRuntimeApiKey(codexProvider, openAICodexToken("account-1"));
+ await expect(harness.session.findRlmModels("parent", 8)).resolves.toMatchObject({
+ models: [{ selector: `${codexProvider}/parent-model` }],
+ });
+
+ now += 300_001;
+ await expect(harness.session.findRlmModels("parent", 8)).resolves.toEqual({ models: [] });
+ expect(fetchModels).toHaveBeenCalledTimes(2);
+ } finally {
+ dateNow.mockRestore();
+ vi.unstubAllGlobals();
+ harness.cleanup();
+ }
+ });
+
+ it("does not warn when an unavailable selector is already the parent model", async () => {
+ const codexProvider = "openai-codex";
+ const harness = await createHarness({ provider: codexProvider, models: [{ id: "parent-model" }] });
+ const fetchModels = vi.fn().mockRejectedValue(new Error("offline"));
+ vi.stubGlobal("fetch", fetchModels);
+ try {
+ harness.authStorage.setRuntimeApiKey(codexProvider, openAICodexToken("account-1"));
+ await expect(harness.session.findRlmModels("parent", 8)).resolves.toEqual({ models: [] });
+ harness.setResponses([fauxAssistantMessage("same parent answer")]);
+
+ const result = await harness.session.runRlmChild("keep the parent model", {
+ model: `${codexProvider}/parent-model`,
+ });
+ expect(result).toMatchObject({
+ answer: "same parent answer",
+ model: `${codexProvider}/parent-model`,
+ });
+ expect(result.warning).toBeUndefined();
+ } finally {
+ vi.unstubAllGlobals();
+ harness.cleanup();
+ }
+ });
+
+ it("does not start a child after its parent is disposed during preflight", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [{ id: "parent-model" }, { id: "child-model" }],
+ });
+ let releasePreflight!: () => void;
+ const preflightGate = new Promise<void>((resolve) => {
+ releasePreflight = resolve;
+ });
+ let providerCalls = 0;
+ try {
+ harness.setResponses([
+ () => {
+ providerCalls++;
+ return fauxAssistantMessage("late child ran");
+ },
+ ]);
+ const authPreflight = vi
+ .spyOn(harness.session.modelRegistry, "getApiKeyAndHeaders")
+ .mockImplementationOnce(async () => {
+ await preflightGate;
+ return { ok: true, apiKey: "faux-key" };
+ });
+ const run = harness.session.runRlmChild("do not run after disposal", {
+ model: `${provider}/child-model`,
+ });
+ await vi.waitFor(() => expect(authPreflight).toHaveBeenCalledOnce());
+ harness.session.dispose();
+ releasePreflight();
+
+ await expect(run).rejects.toThrow("parent session has been disposed");
+ expect(providerCalls).toBe(0);
+ expect(harness.session.listRlmSubagents().subagents).toEqual([]);
+ } finally {
+ releasePreflight();
+ harness.cleanup();
+ }
+ });
+
+ it("reserves an explicit child name while model validation is pending", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [{ id: "parent-model" }],
+ });
+ try {
+ harness.setResponses([fauxAssistantMessage("first child answer")]);
+
+ const first = harness.session.runRlmChild("first task", { name: "shared-reviewer" });
+ await expect(harness.session.runRlmChild("second task", { name: "shared-reviewer" })).rejects.toThrow(
+ 'RLM subagent session name "shared-reviewer" is already in use',
+ );
+ await expect(first).resolves.toMatchObject({ answer: "first child answer" });
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("runs and retains a child on an explicitly selected model", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [
+ { id: "parent-model", reasoning: true },
+ { id: "child-model", name: "Child Model", reasoning: false },
+ { id: "later-parent-model", reasoning: true },
+ ],
+ persistSession: true,
+ });
+ try {
+ harness.session.setThinkingLevel("high");
+ const seenModels: string[] = [];
+ harness.setResponses([
+ (_context, _options, _state, model) => {
+ seenModels.push(model.id);
+ return fauxAssistantMessage("initial child answer");
+ },
+ (_context, _options, _state, model) => {
+ seenModels.push(model.id);
+ return fauxAssistantMessage("follow-up child answer");
+ },
+ ]);
+
+ const result = await harness.session.runRlmChild("inspect the API", {
+ name: "api-reviewer",
+ model: `${provider}/child-model`,
+ });
+ const childEntry = harness.session.listRlmSubagents().subagents[0];
+ expect(childEntry?.status).toBe("completed");
+ const child = harness.session.getRlmChildSession(childEntry!.rlm_child_id);
+ expect(child?.model?.id).toBe("child-model");
+ expect(child?.thinkingLevel).toBe("off");
+
+ await harness.session.setModel(harness.getModel("later-parent-model")!);
+ await child!.prompt("check the follow-up", { expandPromptTemplates: false, source: "extension" });
+ await child!.agent.waitForIdle();
+
+ expect(result.answer).toBe("initial child answer");
+ expect(seenModels).toEqual(["child-model", "child-model"]);
+ expect(child?.model?.id).toBe("child-model");
+ expect(result.session_dir).not.toBeNull();
+ const childSessions = await SessionManager.list(harness.tempDir, result.session_dir!);
+ const persisted = SessionManager.open(childSessions[0]!.path, result.session_dir!);
+ expect(persisted.buildSessionContext().model).toEqual({
+ provider,
+ modelId: "child-model",
+ });
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("inherits the parent model when no override is supplied", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [{ id: "parent-model" }, { id: "child-model" }],
+ });
+ try {
+ let seenModel: string | undefined;
+ harness.setResponses([
+ (_context, _options, _state, model) => {
+ seenModel = model.id;
+ return fauxAssistantMessage("inherited child answer");
+ },
+ ]);
+
+ await harness.session.runRlmChild("inherit the model");
+ expect(seenModel).toBe("parent-model");
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("accepts a selected model authenticated by headers", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [{ id: "parent-model" }, { id: "child-model" }],
+ });
+ try {
+ harness.setResponses([fauxAssistantMessage("header-auth child answer")]);
+ const authPreflight = vi
+ .spyOn(harness.session.modelRegistry, "getApiKeyAndHeaders")
+ .mockResolvedValueOnce({ ok: true, headers: { Authorization: "Bearer header-token" } });
+
+ const result = await harness.session.runRlmChild("use header auth", {
+ model: `${provider}/child-model`,
+ });
+ authPreflight.mockRestore();
+
+ expect(result.model).toBe(`${provider}/child-model`);
+ expect(result.warning).toBeUndefined();
+ } finally {
+ harness.cleanup();
+ }
+ });
+
+ it("falls back to the parent model and returns a user-facing warning", async () => {
+ const harness = await createHarness({
+ provider,
+ models: [{ id: "parent-model" }, { id: "child-model" }],
+ });
+ try {
+ await expect(harness.session.runRlmChild("bad type", { model: 42 })).rejects.toThrow(
+ "rlm.run model must be a string",
+ );
+ harness.setResponses([
+ fauxAssistantMessage("unknown fallback"),
+ fauxAssistantMessage("unauthenticated fallback"),
+ fauxAssistantMessage("unavailable fallback"),
+ fauxAssistantMessage("preflight fallback"),
+ ]);
+
+ const unknown = await harness.session.runRlmChild("unknown model", {
+ model: `${provider}/missing-model`,
+ });
+ expect(unknown.model).toBe(`${provider}/parent-model`);
+ expect(unknown.warning).toContain(`not "${provider}/missing-model"`);
+ expect(unknown.warning).toContain("Tell the user");
+
+ const unauthenticated = await harness.session.runRlmChild("unauthenticated provider", {
+ model: "not-authed/missing-model",
+ });
+ expect(unauthenticated.model).toBe(`${provider}/parent-model`);
+ expect(unauthenticated.warning).toContain("unauthenticated");
+
+ const availability = vi
+ .spyOn(harness.session.modelRegistry, "getAvailable")
+ .mockReturnValue([harness.getModel("parent-model")!]);
+ const unavailable = await harness.session.runRlmChild("unavailable model", {
+ model: `${provider}/child-model`,
+ });
+ availability.mockRestore();
+
+ expect(unavailable.model).toBe(`${provider}/parent-model`);
+ expect(unavailable.warning).toContain(`not "${provider}/child-model"`);
+
+ const authPreflight = vi
+ .spyOn(harness.session.modelRegistry, "getApiKeyAndHeaders")
+ .mockResolvedValueOnce({ ok: false, error: "token expired" });
+ const failedPreflight = await harness.session.runRlmChild("failed auth preflight", {
+ model: `${provider}/child-model`,
+ });
+ authPreflight.mockRestore();
+ expect(failedPreflight.model).toBe(`${provider}/parent-model`);
+ expect(failedPreflight.warning).toContain("failed authentication preflight");
+
+ expect(harness.session.listRlmSubagents().subagents).toHaveLength(4);
+ for (const child of harness.session.listRlmSubagents().subagents) {
+ expect(harness.session.getRlmChildSession(child.rlm_child_id)?.model?.id).toBe("parent-model");
+ }
+ } finally {
+ harness.cleanup();
+ }
+ });
+});
diff --git a/packages/coding-agent/test/system-prompt.test.ts b/packages/coding-agent/test/system-prompt.test.ts
index 6ecc2d9470..8792282ee6 100644
--- a/packages/coding-agent/test/system-prompt.test.ts
+++ b/packages/coding-agent/test/system-prompt.test.ts
@@ -99,6 +99,22 @@ describe("buildRlmPrompt", () => {
expect(prompt).toContain("Each `%%bash` cell runs in a throw-away subshell");
});
+ test("discovers requested models through a bounded authenticated host search", () => {
+ const prompt = buildRlmPrompt({
+ cwd: "/repo",
+ messagesPath: "/repo/.pi/sessions/session.jsonl",
+ activeTools: ["ipython"],
+ });
+
+ expect(prompt).toContain("await rlm.find_models('requested model')");
+ expect(prompt).toContain("bounded authenticated catalog");
+ expect(prompt).toContain("exact `provider/model` selector");
+ expect(prompt).toContain("If an `RLMResult.warning` is set");
+ expect(prompt).toContain("tell the user which model actually ran");
+ expect(prompt).toContain("Do not choose a different model on your own");
+ expect(prompt).not.toContain("model choices for subagents");
+ });
+
test("only documents ipython shell prefixes when ipython is active", () => {
const prompt = buildRlmPrompt({
cwd: "/repo",
diff --git a/prime-agent-runtime/src/rlm/__init__.py b/prime-agent-runtime/src/rlm/__init__.py
index b3f2a8fe6d..937ca6a185 100644
--- a/prime-agent-runtime/src/rlm/__init__.py
+++ b/prime-agent-runtime/src/rlm/__init__.py
@@ -41,6 +41,16 @@ class RLMResult:
session_dir: Path | None = None
usage: TokenUsage = field(default_factory=TokenUsage)
turns: int = 0
+ model: str | None = None
+ warning: str | None = None
+
+
+@dataclass(frozen=True)
+class RLMModel:
+ provider: str
+ id: str
+ name: str
+ selector: str
@dataclass(frozen=True)
@@ -103,6 +113,8 @@ def _result_from_payload(payload: dict[str, Any]) -> RLMResult:
usage=usage,
turns=int(payload.get("turns", 0)),
session_dir=session_dir,
+ model=payload.get("model") if isinstance(payload.get("model"), str) else None,
+ warning=payload.get("warning") if isinstance(payload.get("warning"), str) else None,
)
@@ -166,7 +178,10 @@ async def host_request(request_type: str, payload: dict[str, Any] | None = None)
async def run(prompt: str, **kwargs: Any) -> RLMResult:
- """Run a recursive Prime Agent child through the TypeScript host."""
+ """Run a recursive Prime Agent child through the TypeScript host.
+
+ ``model`` selects a child with an exact ``provider/model`` selector.
+ """
if not isinstance(prompt, str):
raise TypeError(f"prompt must be str, got {type(prompt).__name__}")
_ensure_recursion_allowed()
@@ -174,6 +189,31 @@ async def run(prompt: str, **kwargs: Any) -> RLMResult:
return _result_from_payload(payload)
+def _model_from_payload(payload: Any) -> RLMModel:
+ if not isinstance(payload, dict):
+ raise RuntimeError("rlm.find_models returned an invalid model entry")
+ provider = payload.get("provider")
+ model_id = payload.get("id")
+ name = payload.get("name")
+ selector = payload.get("selector")
+ if not all(isinstance(value, str) and value for value in (provider, model_id, name, selector)):
+ raise RuntimeError("rlm.find_models returned an invalid model entry")
+ return RLMModel(provider=provider, id=model_id, name=name, selector=selector)
+
+
+async def find_models(query: str = "", limit: int = 8) -> list[RLMModel]:
+ """Search a bounded list of models backed by active user credentials."""
+ if not isinstance(query, str):
+ raise TypeError(f"query must be str, got {type(query).__name__}")
+ if not isinstance(limit, int):
+ raise TypeError(f"limit must be int, got {type(limit).__name__}")
+ payload = await host_request("rlm.find_models", {"query": query, "limit": limit})
+ models = payload.get("models")
+ if not isinstance(models, list):
+ raise RuntimeError("rlm.find_models returned an invalid models list")
+ return [_model_from_payload(model) for model in models]
+
+
def _subagent_from_payload(payload: Any, operation: str = "rlm.list_subagents") -> RLMSubagent:
if not isinstance(payload, dict):
raise RuntimeError(f"{operation} returned an invalid subagent entry")
@@ -286,6 +326,9 @@ class _RLMCallable:
async def run(self, prompt: str, **kwargs: Any) -> RLMResult:
return await run(prompt, **kwargs)
+ async def find_models(self, query: str = "", limit: int = 8) -> list[RLMModel]:
+ return await find_models(query, limit)
+
async def list_subagents(self) -> list[RLMSubagent]:
return await list_subagents()
@@ -314,11 +357,13 @@ __all__ = [
"McpIntegration",
"McpToolError",
"NotEnabled",
+ "RLMModel",
"RLMResult",
"RLMSubagent",
"RefinementEvent",
"TokenUsage",
"delete_subagent",
+ "find_models",
"get_harness_state",
"harness",
"host_request",
diff --git a/prime-agent-runtime/test/test_subagent_registry.py b/prime-agent-runtime/test/test_subagent_registry.py
index dff6eadc9b..3a01c405a1 100644
--- a/prime-agent-runtime/test/test_subagent_registry.py
+++ b/prime-agent-runtime/test/test_subagent_registry.py
@@ -39,23 +39,76 @@ class RlmSubagentRegistryTest(unittest.TestCase):
self.assertEqual(subagents[0].status, "completed")
host_request.assert_awaited_once_with("rlm.list_subagents")
- def test_forwards_orchestrator_chosen_name_to_host(self) -> None:
+ def test_forwards_orchestrator_chosen_name_and_model_to_host(self) -> None:
host_request = AsyncMock(
return_value={
"answer": "done",
"usage": {"prompt_tokens": 1, "completion_tokens": 1},
"turns": 1,
"session_dir": "/tmp/parent/sub-a1b2c3d4",
+ "model": "openai/gpt-5.4",
+ "warning": "Requested model unavailable; tell the user about the fallback.",
}
)
with patch.object(rlm_module, "host_request", host_request):
- asyncio.run(rlm_module.rlm("check the API", name="api-reviewer"))
+ result = asyncio.run(
+ rlm_module.rlm(
+ "check the API",
+ name="api-reviewer",
+ model="deepseek/deepseek-v4-flash",
+ )
+ )
host_request.assert_awaited_once_with(
"rlm.run",
- {"prompt": "check the API", "kwargs": {"name": "api-reviewer"}},
+ {
+ "prompt": "check the API",
+ "kwargs": {
+ "name": "api-reviewer",
+ "model": "deepseek/deepseek-v4-flash",
+ },
+ },
)
+ self.assertEqual(result.model, "openai/gpt-5.4")
+ self.assertIn("tell the user", result.warning)
+
+ def test_finds_authenticated_models_through_host(self) -> None:
+ host_request = AsyncMock(
+ return_value={
+ "models": [
+ {
+ "provider": "anthropic",
+ "id": "claude-opus-4-7",
+ "name": "Claude Opus 4.7",
+ "selector": "anthropic/claude-opus-4-7",
+ }
+ ]
+ }
+ )
+
+ with patch.object(rlm_module, "host_request", host_request):
+ models = asyncio.run(rlm_module.rlm.find_models("opus", limit=3))
+
+ self.assertEqual(models[0].provider, "anthropic")
+ self.assertEqual(models[0].id, "claude-opus-4-7")
+ self.assertEqual(models[0].name, "Claude Opus 4.7")
+ self.assertEqual(models[0].selector, "anthropic/claude-opus-4-7")
+ host_request.assert_awaited_once_with(
+ "rlm.find_models",
+ {"query": "opus", "limit": 3},
+ )
+
+ def test_rejects_invalid_model_search_input_and_response(self) -> None:
+ with self.assertRaisesRegex(TypeError, "query must be str"):
+ asyncio.run(rlm_module.find_models(123))
+ with self.assertRaisesRegex(TypeError, "limit must be int"):
+ asyncio.run(rlm_module.find_models("opus", limit="3"))
+
+ host_request = AsyncMock(return_value={"models": [{"provider": "anthropic"}]})
+ with patch.object(rlm_module, "host_request", host_request):
+ with self.assertRaisesRegex(RuntimeError, "invalid model entry"):
+ asyncio.run(rlm_module.find_models("opus"))
def test_deletes_subagent_by_name_through_host(self) -> None:
deleted_payload = {