Skip to content

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

Make tenant identity part of session records.

debajyoti-truefoundry89946fb981ff
diff --git a/packages/harness/src/agentSession/SessionHandle.ts b/packages/harness/src/agentSession/SessionHandle.ts
index 0988453d7..d8a46782c 100644
--- a/packages/harness/src/agentSession/SessionHandle.ts
+++ b/packages/harness/src/agentSession/SessionHandle.ts
@@ -65,16 +65,10 @@ export class SessionHandle<
TTurnCustom extends object = Record<string, never>,
> {
private readonly store: ISessionStore<TSessionCustom, TTurnCustom>;
- private readonly tenantName: string;
private session: SessionRecord<TSessionCustom>;
- constructor(options: {
- store: ISessionStore<TSessionCustom, TTurnCustom>;
- tenantName: string;
- session: SessionRecord<TSessionCustom>;
- }) {
+ constructor(options: { store: ISessionStore<TSessionCustom, TTurnCustom>; session: SessionRecord<TSessionCustom> }) {
this.store = options.store;
- this.tenantName = options.tenantName;
this.session = options.session;
}
@@ -83,7 +77,7 @@ export class SessionHandle<
}
get tenant_name(): string {
- return this.tenantName;
+ return this.session.tenant_name;
}
get agent_spec() {
@@ -133,7 +127,7 @@ export class SessionHandle<
const previousTurnId = resolvePreviousTurnId(input.previous_turn_id, this.session.last_turn_id);
const previous = previousTurnId
? await this.store.getTurn({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
session_id: this.session.session_id,
turn_id: previousTurnId,
})
@@ -208,13 +202,13 @@ export class SessionHandle<
};
await this.store.createTurn({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
turn: turnRecord,
update_session_title_if_not_exist: input.update_session_title_if_not_exist,
});
const refreshed = await this.store.getSession({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
session_id: this.session.session_id,
});
if (refreshed) {
@@ -223,7 +217,7 @@ export class SessionHandle<
return new TurnHandle({
store: this.store,
- tenantName: this.tenantName,
+ tenantName: this.tenant_name,
turn: turnRecord,
orchestrator,
resolver: input.resolver,
@@ -242,7 +236,7 @@ export class SessionHandle<
/** Returns the turn handle (store-backed; not executable), or undefined if not found. */
async getTurn(turn_id: string): Promise<TurnHandle<TTurnCustom> | undefined> {
const turn = await this.store.getTurn({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
session_id: this.session.session_id,
turn_id,
});
@@ -251,7 +245,7 @@ export class SessionHandle<
}
return TurnHandle.fromRecord({
store: this.store,
- tenantName: this.tenantName,
+ tenantName: this.tenant_name,
turn,
});
}
@@ -262,7 +256,7 @@ export class SessionHandle<
page_token?: string | undefined;
}): Promise<{ data: TurnRecord<TTurnCustom>[]; pagination: TokenPagination }> {
return this.store.listTurns({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
session_id: this.session.session_id,
limit: input.limit,
page_token: input.page_token,
@@ -283,7 +277,7 @@ export class SessionHandle<
pagination: TokenPagination;
}> {
return this.store.listSessionEvents({
- tenant_name: this.tenantName,
+ tenant_name: this.tenant_name,
session_id: this.session.session_id,
limit: input.limit,
page_token: input.page_token,
diff --git a/packages/harness/src/agentSession/Sessions.ts b/packages/harness/src/agentSession/Sessions.ts
index 635233559..a8c13a51a 100644
--- a/packages/harness/src/agentSession/Sessions.ts
+++ b/packages/harness/src/agentSession/Sessions.ts
@@ -30,7 +30,6 @@ export class Sessions<
}
return new SessionHandle({
store: this.store,
- tenantName: input.tenant_name,
session: record,
});
}
@@ -47,7 +46,6 @@ export class Sessions<
}
return new SessionHandle({
store: this.store,
- tenantName: input.tenant_name,
session: record,
});
}
diff --git a/packages/harness/src/agentSession/models/SessionRecord.ts b/packages/harness/src/agentSession/models/SessionRecord.ts
index 0c70e8cad..65f82273c 100644
--- a/packages/harness/src/agentSession/models/SessionRecord.ts
+++ b/packages/harness/src/agentSession/models/SessionRecord.ts
@@ -8,6 +8,7 @@ import type { AgentSpec } from '../schemas/agentSpec';
* binding via the updateSession patch.
*/
export interface SessionRecord<TCustom extends object = Record<string, never>> {
+ tenant_name: string;
session_id: string;
/** Always hydrated on read. Source of `spec` in SessionHandle.run(). */
agent_spec: AgentSpec;
diff --git a/packages/harness/src/agentSession/store/ISessionStore.ts b/packages/harness/src/agentSession/store/ISessionStore.ts
index 361ef89e7..b42fc97f9 100644
--- a/packages/harness/src/agentSession/store/ISessionStore.ts
+++ b/packages/harness/src/agentSession/store/ISessionStore.ts
@@ -14,14 +14,16 @@ import type { TokenPagination } from '../schemas/pagination';
import type { TerminalTurnState } from '../schemas/turn';
/** Caller-supplied fields for creating a session; the store owns timestamps and tip state. */
-export type CreateSessionInput<TSessionCustom extends object = Record<string, never>> = {
- tenant_name: string;
-} & Pick<SessionRecord<TSessionCustom>, 'session_id' | 'agent_spec' | 'custom'>;
+export type CreateSessionInput<TSessionCustom extends object = Record<string, never>> = Pick<
+ SessionRecord<TSessionCustom>,
+ 'tenant_name' | 'session_id' | 'agent_spec' | 'custom'
+>;
/** PATCH fields for an existing session; omitted keys are left unchanged. */
-export type UpdateSessionInput<TSessionCustom extends object = Record<string, never>> = {
- tenant_name: string;
-} & Pick<SessionRecord<TSessionCustom>, 'session_id'> &
+export type UpdateSessionInput<TSessionCustom extends object = Record<string, never>> = Pick<
+ SessionRecord<TSessionCustom>,
+ 'tenant_name' | 'session_id'
+> &
Partial<Pick<SessionRecord<TSessionCustom>, 'agent_spec' | 'title'>>;
export type GetSessionInput = {
diff --git a/packages/harness/src/agentSession/store/InMemorySessionStore.ts b/packages/harness/src/agentSession/store/InMemorySessionStore.ts
index b120bb6fd..391b3c174 100644
--- a/packages/harness/src/agentSession/store/InMemorySessionStore.ts
+++ b/packages/harness/src/agentSession/store/InMemorySessionStore.ts
@@ -92,6 +92,7 @@ export class InMemorySessionStore<
}
const now = new Date().toISOString();
const record: SessionRecord<TSessionCustom> = {
+ tenant_name: input.tenant_name,
session_id: input.session_id,
agent_spec: deepCopy(input.agent_spec),
title: null,
diff --git a/packages/harness/tests/agentSession/inMemorySessionStore.test.ts b/packages/harness/tests/agentSession/inMemorySessionStore.test.ts
index 8ff2622e6..8893e3d46 100644
--- a/packages/harness/tests/agentSession/inMemorySessionStore.test.ts
+++ b/packages/harness/tests/agentSession/inMemorySessionStore.test.ts
@@ -34,6 +34,7 @@ function runStoreContractSuite(createStore: () => ISessionStore) {
await seedSession(store);
const session = await store.getSession({ tenant_name: tenant, session_id: sessionId });
expect(session).toBeDefined();
+ expect(mustGet(session).tenant_name).toBe(tenant);
expect(mustGet(session).agent_spec.model.name).toBe('test-model');
expect(mustGet(session).last_activity_timestamp_ms).toBeGreaterThanOrEqual(before);
expect(mustGet(session).title).toBeNull();