Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thread-run-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@truefoundry/trueforge-core': patch
'@truefoundry/trueforge': patch
---

Persist the model each thread runs on, so a rebuilt subagent resumes on it. A subagent on a different model from its parent runs on that model's own defaults instead of the parent's params. `AgentDefinition` no longer carries the model; it's a separate `AgentModel`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Logger } from 'winston';
import type { AgentCapability } from '../core/capabilities/AgentCapability';
import type { AgentDefinition } from '../core/runtime/AgentDefinition';
import type { AgentModel } from '../core/runtime/AgentModel';
import type { AgentInfo } from '../core/runtime/AgentThread.types';
import type { HarnessSandbox, SandboxInfo } from '../core/sandbox/Sandbox';
import type { AgentTracing } from '../core/tracing/AgentTracing';
Expand All @@ -11,6 +12,7 @@ import type { AgentSpec } from './schemas/agentSpec';
/** Result of {@link ITurnResourceResolver.resolveAgentDefinition}. */
export interface ResolvedAgentDefinition {
definition: AgentDefinition;
model: AgentModel;
extraCapabilities?: AgentCapability[] | undefined;
}

Expand Down
25 changes: 17 additions & 8 deletions packages/trueforge-core/src/agent-session/SessionHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export function resolvePreviousTurnId(
}

function toNewThreadInit(snapshot: AgentThreadSnapshot): NewThreadInit {
const { context, current_context_usage, completion, capability_state, ...rest } = snapshot;
const { context, current_context_usage, completion, capability_state, model, ...rest } = snapshot;
void context;
void current_context_usage;
void completion;
void capability_state;
void model;
return rest;
}

Expand Down Expand Up @@ -255,6 +256,10 @@ export class SessionHandle<
thread_id,
capability_state: thread.toSnapshot().capability_state,
})),
thread_models: [...agentThreads].map(([thread_id, thread]) => ({
Comment thread
datadog-official[bot] marked this conversation as resolved.
Resolved
thread_id,
model: thread.model.name,
})),
update_session_title_if_not_exist: input.update_session_title_if_not_exist ?? null,
});

Expand Down Expand Up @@ -507,9 +512,12 @@ export class SessionHandle<
await Promise.all(
threadIds.map(async threadId => {
const previousThreadSnapshot = snapshots?.[threadId];
const agentInfo = previousThreadSnapshot?.agent_info ?? undefined;
const savedModel = previousThreadSnapshot?.model ?? undefined;
const resolved = await input.resolver.resolveAgentDefinition({
spec: input.spec,
agent_info: previousThreadSnapshot?.agent_info ?? undefined,
// A subagent comes back on the model it ran on; the root follows its spec.
agent_info: agentInfo && savedModel ? { ...agentInfo, model: savedModel } : agentInfo,
previousTurn: input.previous,
signal: input.signal,
tracing: input.tracing,
Expand Down Expand Up @@ -560,12 +568,12 @@ export class SessionHandle<
tracing: AgentTracing;
resolvedDefinition: ResolvedAgentDefinition;
}): AgentThread {
const { definition, extraCapabilities } = input.resolvedDefinition;
const { definition, model, extraCapabilities } = input.resolvedDefinition;
const isChild = Boolean(input.previousThreadSnapshot?.parent);
const capabilities = [
...builtinsFromSpec({
spec: input.spec,
definition,
model,
isChild,
sandboxAvailable: Boolean(input.sandbox),
tracing: input.tracing,
Expand All @@ -576,6 +584,7 @@ export class SessionHandle<
];
return new AgentThread({
definition,
model,
threadId: input.threadId,
title: input.previousThreadSnapshot?.agent_info?.name ?? (isChild ? input.threadId : 'main'),
sandbox: input.sandbox,
Expand All @@ -600,15 +609,14 @@ export class SessionHandle<
tracing: AgentTracing;
}): CreateDynamicSubAgentThread {
return async params => {
const { definition, extraCapabilities } = await input.resolver.resolveAgentDefinition({
const { definition, model, extraCapabilities } = await input.resolver.resolveAgentDefinition({
spec: input.spec,
agent_info: params.request,
previousTurn: input.previous,
signal: params.signal,
tracing: input.tracing,
});
// Model override (request.model) is resolved inside resolveAgentDefinition
// into modelClient — do not re-label a stale client here.
// Model override (request.model) is resolved inside resolveAgentDefinition into `model`.
const childDefinition: AgentDefinition = {
...definition,
toolSets: params.parentDefinition.toolSets ?? definition.toolSets,
Expand All @@ -622,7 +630,7 @@ export class SessionHandle<
const capabilities = [
...builtinsFromSpec({
spec: input.spec,
definition: childDefinition,
model,
isChild: true,
sandboxAvailable: Boolean(input.sandbox),
tracing: input.tracing,
Expand All @@ -633,6 +641,7 @@ export class SessionHandle<
];
return new AgentThread({
definition: childDefinition,
model,
threadId: params.threadId,
title: params.request.name,
sandbox: input.sandbox,
Expand Down
1 change: 1 addition & 0 deletions packages/trueforge-core/src/agent-session/TurnHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ export class TurnHandle<TTransaction = never, TTurnCustom extends object = Recor
thread_id: event.thread_id,
parent: event.parent,
agent_info: event.agent_info,
model: event.agent_info.model ?? null,
context: [],
current_context_usage: getEmptyCurrentContextUsage(),
completion: null,
Expand Down
25 changes: 14 additions & 11 deletions packages/trueforge-core/src/agent-session/TurnResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
DEFAULT_REQUIRE_APPROVAL_FOR_TOOLS,
} from '../core/mcp/toolSelectors';
import { ToolSet } from '../core/mcp/ToolSet';
import type { AgentDefinition } from '../core/runtime/AgentDefinition';
import type { AgentModel } from '../core/runtime/AgentModel';
import type { AgentInfo } from '../core/runtime/AgentThread.types';
import type { HarnessSandbox, SandboxInfo } from '../core/sandbox/Sandbox';
import type { AgentTracing } from '../core/tracing/AgentTracing';
Expand Down Expand Up @@ -73,7 +73,7 @@ function toSelectors(entry: {
interface ResolvedModel {
modelClient: ILLM;
defaultModelParams: ModelParams;
modelProperties?: AgentDefinition['modelProperties'];
modelProperties?: AgentModel['properties'];
/** Explicit `undefined` when the host has no provider label for the model call series. */
providerType: VercelAIProviderName | undefined;
}
Expand Down Expand Up @@ -253,25 +253,28 @@ export class TurnResourceResolver<
...(agentInfo === undefined ? (this.deps.rootCapabilities?.() ?? []) : []),
];
// Sub-agents may request a different catalog model via agent_info.model;
// resolve that name so modelClient matches the override (not just a label).
// resolve that name so the model's client matches the override (not just a label).
const modelName = agentInfo?.model ?? spec.model.name;
const resolvedModel = await this.getModel(modelName);
return {
definition: {
modelClient: resolvedModel.modelClient,
modelName,
model: {
client: resolvedModel.modelClient,
name: modelName,
...(resolvedModel.providerType !== undefined ? { providerType: resolvedModel.providerType } : {}),
modelProperties: resolvedModel.modelProperties,
properties: resolvedModel.modelProperties,
// The spec's params are set for its own model; any other model runs on its own defaults.
params:
modelName === spec.model.name
? { ...resolvedModel.defaultModelParams, ...spec.model.params }
: { ...resolvedModel.defaultModelParams, parallel_tool_calls: true },
},
definition: {
// Sub-agents receive the delegated task as a user message; their system
// prompt is SUB_AGENT_IDENTITY (added by AgentThread), not user instructions.
instruction: agentInfo ? undefined : spec.instructions,
messages: agentInfo
? [{ role: 'user' as const, content: agentInfo.input }]
: spec.messages?.map(m => ({ role: 'user' as const, content: m.content })),
modelParams: {
...resolvedModel.defaultModelParams,
...spec.model.params,
},
// Sub-agents should return free-form summaries to the parent, not the user-facing structured response.
responseFormat: agentInfo ? undefined : spec.response_format,
iterationLimit: spec.config.iteration_limit,
Expand Down
10 changes: 5 additions & 5 deletions packages/trueforge-core/src/agent-session/builtinsFromSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import { dynamicSubAgents } from '../core/capabilities/builtins/DynamicSubAgents
import { largeToolResponse } from '../core/capabilities/builtins/LargeToolResponse';
import { openUI } from '../core/capabilities/builtins/OpenUI';
import { webSearch } from '../core/capabilities/builtins/WebSearch';
import type { AgentDefinition } from '../core/runtime/AgentDefinition';
import type { AgentModel } from '../core/runtime/AgentModel';
import type { AgentTracing } from '../core/tracing/AgentTracing';
import type { IWebSearchProvider } from '../core/web-search/WebSearchProvider';
import type { AgentSpec } from './schemas/agentSpec';

export function builtinsFromSpec(input: {
spec: AgentSpec;
definition: AgentDefinition;
model: AgentModel;
/** Root thread only — child threads omit interactive builtins. */
isChild: boolean;
sandboxAvailable: boolean;
tracing: AgentTracing;
webSearchProvider: IWebSearchProvider | undefined;
logger: Logger;
}): AgentCapability[] {
const { spec, definition, isChild, sandboxAvailable, tracing, webSearchProvider, logger } = input;
const { spec, model, isChild, sandboxAvailable, tracing, webSearchProvider, logger } = input;
const config = spec.config;
const capabilities: AgentCapability[] = [currentDateTime({ tracing })];

if (config.context_management.compaction.enabled) {
capabilities.push(
contextCompaction({
definition,
model,
compactionThresholdTokens: config.context_management.compaction.trigger?.value,
}),
);
Expand All @@ -57,7 +57,7 @@ export function builtinsFromSpec(input: {
capabilities.push(
largeToolResponse({
dynamicSubAgentsPresent: !isChild && dynamicSubAgentsEnabled,
modelContextLength: definition.modelProperties?.contextLength,
modelContextLength: model.properties?.contextLength,
logger,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ export type TurnRecordWithoutSnapshot<TTurnCustom extends object = Record<string
/** Thread registration; mutable per-turn data arrives through dedicated createTurn fields. */
export type NewThreadInit = Omit<
AgentThreadSnapshot,
'context' | 'current_context_usage' | 'completion' | 'pending_mcp_auth' | 'capability_state'
'context' | 'current_context_usage' | 'completion' | 'pending_mcp_auth' | 'capability_state' | 'model'
>;

/** Full post-send capability map for one thread. */
export type ThreadCapabilityStateInit = Pick<AgentThreadSnapshot, 'thread_id' | 'capability_state'>;

/** Model one thread runs on in this turn. */
export type ThreadModelInit = Pick<AgentThreadSnapshot, 'thread_id' | 'model'>;

/** One drained send() append batch for one thread; writer = the new turn. */
export interface TurnContextAppend {
thread_id: string;
Expand All @@ -139,6 +142,8 @@ export interface CreateTurnInput<TTurnCustom extends object = Record<string, nev
* Authoritative for this turn; the store does not carry capability rows forward.
*/
capability_states: ThreadCapabilityStateInit[];
/** The model each turn thread runs on in this turn. A thread without an entry keeps its previous value. */
thread_models: ThreadModelInit[];
/** Set only if session.title is NULL (first write wins). */
update_session_title_if_not_exist: string | null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function newThreadSnapshot(thread: NewThreadInit): AgentThreadSnapshot {
thread_id: thread.thread_id,
parent: thread.parent ?? null,
agent_info: thread.agent_info ?? null,
model: null,
capability_state: null,
context: [],
current_context_usage: getEmptyCurrentContextUsage(),
Expand Down Expand Up @@ -141,6 +142,7 @@ function buildSnapshotFromDelta(input: {
new_threads: NewThreadInit[];
new_context_appends: TurnContextAppend[];
capability_states: CreateTurnInput['capability_states'];
thread_models: CreateTurnInput['thread_models'];
created_at: Date;
}): TurnSnapshot {
const threads: Record<string, AgentThreadSnapshot> = input.previousSnapshot
Expand Down Expand Up @@ -169,6 +171,13 @@ function buildSnapshotFromDelta(input: {
}
thread.capability_state = deepCopy(capability.capability_state);
}
for (const { thread_id, model } of input.thread_models) {
const thread = threads[thread_id];
if (thread === undefined) {
throw new SessionStoreInvariantError(`thread_models references unknown thread ${thread_id}`);
}
thread.model = model;
}

return {
threads,
Expand Down Expand Up @@ -442,6 +451,7 @@ export class InMemorySessionStore<
new_threads: input.new_threads,
new_context_appends: input.new_context_appends,
capability_states: input.capability_states,
thread_models: input.thread_models,
created_at: input.turn.created_at,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventType, newEventId, type CompactionTrigger } from '../../events/schema';
import type { ILLM, LLMCreateParams } from '../../llm/ILLM';
import { getOperationMetrics } from '../../observability/operationMetrics';
import type { AgentDefinition } from '../../runtime/AgentDefinition';
import type { AgentModel } from '../../runtime/AgentModel';
import type { ContextMessage } from '../../runtime/AgentThread.types';
import { internalSystemMessage, isInternalSystemMessage, isLLMContextMessage } from '../../runtime/contextUtils';
import type { AgentCapability } from '../AgentCapability';
Expand All @@ -17,7 +17,7 @@ export const DEFAULT_CONTEXT_COMPACTION_RATIO = 0.8;
export function resolveCompactionThresholdTokens(input: {
configuredThresholdTokens: number | undefined;
modelContextLength: number | undefined;
modelParams: AgentDefinition['modelParams'];
modelParams: AgentModel['params'];
}): number {
if (input.configuredThresholdTokens !== undefined) {
return input.configuredThresholdTokens;
Expand Down Expand Up @@ -264,19 +264,19 @@ export interface ContextCompactionSettings {
}

export function contextCompaction(options: {
definition: AgentDefinition;
model: AgentModel;
settings?: ContextCompactionSettings | undefined;
compactionThresholdTokens?: number | undefined;
}): AgentCapability {
const threshold = resolveCompactionThresholdTokens({
configuredThresholdTokens: options.compactionThresholdTokens ?? options.settings?.compactionThresholdTokens,
modelContextLength: options.definition.modelProperties?.contextLength,
modelParams: options.definition.modelParams,
modelContextLength: options.model.properties?.contextLength,
modelParams: options.model.params,
});
return {
preLLMProcessors: [
new ContextCompaction({
modelClient: options.definition.modelClient,
modelClient: options.model.client,
compactionThresholdTokens: threshold,
}),
],
Expand Down
1 change: 1 addition & 0 deletions packages/trueforge-core/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
export { ModelParamsSchema, ModelSchema } from './llm/model';
export type { Model, ModelParams } from './llm/model';
export type { AgentDefinition } from './runtime/AgentDefinition';
export type { AgentModel } from './runtime/AgentModel';
export { AgentThread } from './runtime/AgentThread';
export type {
AgentInfo,
Expand Down
16 changes: 1 addition & 15 deletions packages/trueforge-core/src/core/runtime/AgentDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
import type { ILLM } from '../llm/ILLM';
import type { LLMUserMessage } from '../llm/LLMTypes';
import type { ModelParams } from '../llm/model';
import type { ResponseFormat } from '../llm/responseFormat';
import type { VercelAIProviderName } from '../llm/VercelAILLM';
import type { IToolSet } from '../mcp/IMCPServer';

/**
* Static definition of an agent. Represents the authored configuration,
* not the execution state. Inherited by sub-agent definitions.
* not the execution state or the model it runs on. Inherited by sub-agent definitions.
*/
export interface AgentDefinition {
modelClient: ILLM;
modelName: string;
/** Configured provider. Omitted when the host did not resolve one. */
providerType?: VercelAIProviderName | undefined;
modelProperties?:
| {
/** Maximum combined input/output context for the resolved model, when known. */
contextLength: number | undefined;
}
| undefined;
instruction?: string | undefined;
messages?: readonly LLMUserMessage[] | undefined;
modelParams?: ModelParams | undefined;
/** Same wire shape as AgentSpec.response_format (Zod ResponseFormat). */
responseFormat?: ResponseFormat | undefined;
iterationLimit?: number | undefined;
Expand Down
19 changes: 19 additions & 0 deletions packages/trueforge-core/src/core/runtime/AgentModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ILLM } from '../llm/ILLM';
import type { ModelParams } from '../llm/model';
import type { VercelAIProviderName } from '../llm/VercelAILLM';

/** The model a thread runs on, resolved for that thread. */
export interface AgentModel {
client: ILLM;
/** `provider/model`. */
name: string;
/** Configured provider. Omitted when the host did not resolve one. */
providerType?: VercelAIProviderName | undefined;
properties?:
| {
/** Maximum combined input/output context for the resolved model, when known. */
contextLength: number | undefined;
}
| undefined;
params?: ModelParams | undefined;
}
Loading
Loading