Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Stop enforcing one active turn per session in the process-local registry.

debajyoti-truefoundryebac8b5c1974
diff --git a/packages/server/.env.example b/packages/server/.env.example
index 7ac6e8eed..cb0602318 100644
--- a/packages/server/.env.example
+++ b/packages/server/.env.example
@@ -8,9 +8,11 @@
# HTTP port the server listens on. Optional, defaults to 8790.
PORT=8790
-# Note: models.yaml, mcp.yaml and skills.yaml are always read from ./registry
-# (relative to the working directory). The model API base URL is the
-# base_url field in models.yaml; only its API key lives here.
+# Directory containing the YAML config files (models.yaml, mcp.yaml,
+# skills.yaml). Optional, defaults to ./registry; relative paths are resolved
+# against the working directory. The model API base URL is the base_url field
+# in models.yaml; only its API key lives here.
+# REGISTRY_DIR=registry
# Required. Default API key for the OpenAI-compatible API at models.yaml's
# base_url, sent as "Authorization: Bearer <key>" on every model request.
diff --git a/packages/server/src/apis/sessions.ts b/packages/server/src/apis/sessions.ts
index 977e7cdf3..86f75cd90 100644
--- a/packages/server/src/apis/sessions.ts
+++ b/packages/server/src/apis/sessions.ts
@@ -131,15 +131,22 @@ export function createSessionsRouter(deps: SessionsRouterDeps) {
}
};
- // Cancelling with no running turn is a 200 no-op, matching the turn state
- // machine (first terminal write wins).
+ // Cancels only the session's tail (last_turn_id). Cancelling with no
+ // running turn is a 200 no-op, matching the turn state machine (first
+ // terminal write wins).
const cancelSessionHandler: RouteHandler<typeof cancelSessionRoute> = async c => {
const { sessionId } = c.req.valid('param');
const record = await deps.sessionStore.getSession({ tenant_name: TENANT_NAME, session_id: sessionId });
if (!record) {
return c.json({ error: { message: `Session not found: ${sessionId}` } }, 404);
}
- deps.activeTurns.cancelIfRunning({ sessionId, abortReason: CancellationReason.ClientCancelled });
+ if (record.last_turn_id) {
+ deps.activeTurns.cancelIfRunning({
+ sessionId,
+ turnId: record.last_turn_id,
+ abortReason: CancellationReason.ClientCancelled,
+ });
+ }
return c.json({}, 200);
};
diff --git a/packages/server/src/apis/turns.ts b/packages/server/src/apis/turns.ts
index 1a2cd283c..1c0cd6f5f 100644
--- a/packages/server/src/apis/turns.ts
+++ b/packages/server/src/apis/turns.ts
@@ -1,7 +1,6 @@
import { OpenAPIHono, type RouteHandler } from '@hono/zod-openapi';
import type { Sessions, TurnStreamingEvent } from '@truefoundry/utils/agent-session';
import {
- CancellationReason,
SessionStoreNotFoundError,
TurnResourceResolver,
type TurnInputItem,
@@ -119,9 +118,6 @@ export function createTurnsRouter(deps: TurnsRouterDeps) {
return c.json({ error: { message: `Session not found: ${sessionId}` } }, 404);
}
- // Starting a new turn supersedes the session's running turn, if any.
- deps.activeTurns.cancelIfRunning({ sessionId, abortReason: CancellationReason.CancelledForNextTurn });
-
const abortController = new AbortController();
const resolver = createTurnResolver({
modelStore: deps.modelStore,
diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts
index e8f5d5196..2464332f0 100644
--- a/packages/server/src/config.ts
+++ b/packages/server/src/config.ts
@@ -12,10 +12,7 @@ import path from 'node:path';
// CONFIG FILES
// ============================================================================
-/** Directory containing the YAML config files: `./registry` relative to the working directory. */
-export const CONFIG_DIR = path.resolve('registry');
-
-/** YAML files the stores load from CONFIG_DIR at startup. */
+/** YAML files the stores load from `REGISTRY_DIR` at startup. */
export const CONFIG_FILES = {
models: 'models.yaml',
mcpServers: 'mcp.yaml',
@@ -159,6 +156,13 @@ export const DEFAULT_PORT = 8790;
export interface ServerConfiguration {
/** HTTP port the server listens on. Env: `PORT`. */
PORT: number;
+ /**
+ * Absolute path to the directory containing the YAML config files
+ * (models.yaml, mcp.yaml, skills.yaml). Relative values are resolved
+ * against the working directory. Env: `REGISTRY_DIR`, defaults to
+ * `./registry`.
+ */
+ REGISTRY_DIR: string;
/**
* Default API key for the OpenAI-compatible API at models.yaml's base_url,
* sent as `Authorization: Bearer <key>` on every model request.
@@ -213,6 +217,7 @@ export interface ServerConfiguration {
const configuration: ServerConfiguration = {
PORT: parsePort(getEnv('PORT')),
+ REGISTRY_DIR: path.resolve(getEnv('REGISTRY_DIR', { defaultValue: 'registry' }) ?? 'registry'),
MODEL_API_KEY: getEnv('MODEL_API_KEY', { required: true }) ?? '',
MODEL_API_KEY_BY_NAME: parseApiKeysByName(),
MODEL_HEADERS: parseHeaders('MODEL_HEADERS', getEnv('MODEL_HEADERS')),
diff --git a/packages/server/src/runtime/activeTurns.ts b/packages/server/src/runtime/activeTurns.ts
index f90c02c02..18d288e12 100644
--- a/packages/server/src/runtime/activeTurns.ts
+++ b/packages/server/src/runtime/activeTurns.ts
@@ -1,42 +1,49 @@
/**
- * In-memory registry of running turns, at most one per session. Cancel and
- * turn-creation code abort a run through its AbortController; TurnHandle.stream()
- * writes the terminal state when the signal fires.
+ * In-memory registry of turns running in this process, keyed by session. A
+ * session may have any number of concurrent turns; the registry makes no
+ * attempt to enforce one turn per session (that would not hold across
+ * replicas anyway). Cancel code aborts runs through their AbortControllers;
+ * TurnHandle.stream() writes the terminal state when the signal fires.
*/
-interface ActiveTurn {
- turnId: string;
- abortController: AbortController;
-}
-
export class ActiveTurnRegistry {
- private readonly runs = new Map<string, ActiveTurn>();
+ /** sessionId -> (turnId -> AbortController) */
+ private readonly runs = new Map<string, Map<string, AbortController>>();
- /** Registers the session's running turn. The previous entry, if any, must already be cancelled. */
+ /** Registers a running turn under its session. */
register(input: { sessionId: string; turnId: string; abortController: AbortController }): void {
- this.runs.set(input.sessionId, { turnId: input.turnId, abortController: input.abortController });
+ let sessionRuns = this.runs.get(input.sessionId);
+ if (!sessionRuns) {
+ sessionRuns = new Map();
+ this.runs.set(input.sessionId, sessionRuns);
+ }
+ sessionRuns.set(input.turnId, input.abortController);
}
/** Removes the entry once the turn's stream has fully drained. */
finish(input: { sessionId: string; turnId: string }): void {
- const active = this.runs.get(input.sessionId);
- if (active?.turnId === input.turnId) {
+ const sessionRuns = this.runs.get(input.sessionId);
+ if (!sessionRuns) {
+ return;
+ }
+ sessionRuns.delete(input.turnId);
+ if (sessionRuns.size === 0) {
this.runs.delete(input.sessionId);
}
}
/**
- * Aborts the session's running turn if there is one. Returns true when a
- * run was found (already-aborted runs are not re-aborted). Cancelling a
- * session with no running turn is a no-op, mirroring the store's
+ * Aborts the given turn if it is running in this process. Returns true when
+ * the run was found (already-aborted runs are not re-aborted). Cancelling a
+ * turn that is not running is a no-op, mirroring the store's
* first-terminal-write-wins rule.
*/
- cancelIfRunning(input: { sessionId: string; abortReason?: string }): boolean {
- const active = this.runs.get(input.sessionId);
- if (!active) {
+ cancelIfRunning(input: { sessionId: string; turnId: string; abortReason?: string }): boolean {
+ const abortController = this.runs.get(input.sessionId)?.get(input.turnId);
+ if (!abortController) {
return false;
}
- if (!active.abortController.signal.aborted) {
- active.abortController.abort(input.abortReason);
+ if (!abortController.signal.aborted) {
+ abortController.abort(input.abortReason);
}
return true;
}
diff --git a/packages/server/src/store/loadYaml.ts b/packages/server/src/store/loadYaml.ts
index f1b5128e3..c45472a89 100644
--- a/packages/server/src/store/loadYaml.ts
+++ b/packages/server/src/store/loadYaml.ts
@@ -7,10 +7,10 @@ import fs from 'node:fs';
import path from 'node:path';
import { parse } from 'yaml';
import type { z } from 'zod';
-import { CONFIG_DIR } from '../config';
+import configuration from '../config';
export function loadYamlFile<T>(fileName: string, schema: z.ZodType<T>): T {
- const filePath = path.join(CONFIG_DIR, fileName);
+ const filePath = path.join(configuration.REGISTRY_DIR, fileName);
let raw: string;
try {