Skip to content

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

AGE-1506: add created by to session (#79)

Heer Ambavi88a2e631d388
diff --git a/fern/openapi/openapi.json b/fern/openapi/openapi.json
index 01ea908c0..d3148ee20 100644
--- a/fern/openapi/openapi.json
+++ b/fern/openapi/openapi.json
@@ -2580,6 +2580,9 @@
"created_at": {
"type": "string"
},
+ "created_by": {
+ "type": "string"
+ },
"id": {
"type": "string"
},
@@ -2597,6 +2600,7 @@
"id",
"agent",
"title",
+ "created_by",
"created_at",
"updated_at"
],
@@ -4397,7 +4401,7 @@
},
"/api/v1/sessions": {
"get": {
- "description": "List sessions (newest first by default), token-paginated. Optional `agent_id` filters to sessions bound to that named agent. Pass `page_token` to fetch the next page, keeping the other query params constant.",
+ "description": "List sessions (newest first by default), token-paginated. Optional `agent_id` filters to sessions bound to that named agent; optional `created_by` filters by creator identity. Pass `page_token` to fetch the next page, keeping the other query params constant.",
"parameters": [
{
"description": "Page size. Defaults to 10, max 100.",
@@ -4463,6 +4467,17 @@
"minLength": 1,
"type": "string"
}
+ },
+ {
+ "description": "When set, only sessions created by this identity are returned.",
+ "in": "query",
+ "name": "created_by",
+ "required": false,
+ "schema": {
+ "description": "When set, only sessions created by this identity are returned.",
+ "minLength": 1,
+ "type": "string"
+ }
}
],
"responses": {
diff --git a/packages/frontend/tests/harnessServer.test.ts b/packages/frontend/tests/harnessServer.test.ts
index fcd790253..f0e88feb8 100644
--- a/packages/frontend/tests/harnessServer.test.ts
+++ b/packages/frontend/tests/harnessServer.test.ts
@@ -13,6 +13,7 @@ const session = {
},
},
title: null,
+ created_by: 'trueforge-default',
created_at: '2026-08-03T00:00:00.000Z',
updated_at: '2026-08-03T00:00:00.000Z',
};
diff --git a/packages/harness/src/agent-session/models/SessionRecord.ts b/packages/harness/src/agent-session/models/SessionRecord.ts
index 862376882..ca94bd349 100644
--- a/packages/harness/src/agent-session/models/SessionRecord.ts
+++ b/packages/harness/src/agent-session/models/SessionRecord.ts
@@ -7,6 +7,8 @@ import type { SessionAgent } from '../schemas/session';
export interface SessionRecord<TCustom extends object = Record<string, never>> {
tenant_id: string;
session_id: string;
+ /** Caller identity that created the session (immutable after create). */
+ created_by: string;
agent: SessionAgent;
/**
* Wire SessionSchema.title (nullable). Written via updateSession patch or
diff --git a/packages/harness/src/agent-session/schemas/session.ts b/packages/harness/src/agent-session/schemas/session.ts
index 39b11149c..1c5825d96 100644
--- a/packages/harness/src/agent-session/schemas/session.ts
+++ b/packages/harness/src/agent-session/schemas/session.ts
@@ -31,6 +31,8 @@ export const SessionSchema = z
id: z.string(),
agent: SessionAgentSchema,
title: z.string().nullable(),
+ /** Caller identity that created the session (immutable). */
+ created_by: z.string(),
created_at: z.string(),
updated_at: z.string(),
})
diff --git a/packages/harness/src/agent-session/store/ISessionStore.ts b/packages/harness/src/agent-session/store/ISessionStore.ts
index da3d5716f..1f5efedb1 100644
--- a/packages/harness/src/agent-session/store/ISessionStore.ts
+++ b/packages/harness/src/agent-session/store/ISessionStore.ts
@@ -19,7 +19,7 @@ import type { TerminalTurnState } from '../schemas/turn';
*/
export type CreateSessionInput<TSessionCustom extends object = Record<string, never>> = Pick<
SessionRecord<TSessionCustom>,
- 'tenant_id' | 'session_id' | 'agent'
+ 'tenant_id' | 'session_id' | 'agent' | 'created_by'
> & {
custom: TSessionCustom | null;
};
@@ -57,6 +57,8 @@ export interface ListSessionsInput {
end_timestamp: Date | undefined;
/** When set, only sessions bound to this named agent id. */
agent_id: string | undefined;
+ /** When set, only sessions created by this identity. */
+ created_by: string | undefined;
}
/** Turn row fields without assembled snapshot (create input / listTurns). */
@@ -211,6 +213,7 @@ export interface ISessionStore<
/**
* Persists a discriminated `agent` (ref | value). SQL backends may flatten to columns.
* `session_id` is globally unique across tenants.
+ * Persists caller-supplied `created_by` (immutable after create).
* Sets `last_activity_timestamp_ms` (= now) on create.
*/
createSession(input: CreateSessionInput<TSessionCustom>): Promise<void>;
@@ -236,7 +239,7 @@ export interface ISessionStore<
* Paginated list of the tenant's sessions ordered by `created_at`
* (`order` defaults to `desc`). `start_timestamp` / `end_timestamp` are
* inclusive instant bounds on `created_at`. Optional `agent_id` filters
- * ref-bound sessions.
+ * ref-bound sessions; optional `created_by` filters by creator identity.
* Does **not** bump `last_activity_timestamp_ms` (read path).
*/
listSessions(
diff --git a/packages/harness/src/agent-session/store/InMemorySessionStore.ts b/packages/harness/src/agent-session/store/InMemorySessionStore.ts
index 2cd8a592e..866b1536b 100644
--- a/packages/harness/src/agent-session/store/InMemorySessionStore.ts
+++ b/packages/harness/src/agent-session/store/InMemorySessionStore.ts
@@ -165,6 +165,7 @@ export class InMemorySessionStore<
const record: SessionRecord<TSessionCustom> = {
tenant_id: input.tenant_id,
session_id: input.session_id,
+ created_by: input.created_by,
agent: deepCopy(input.agent),
title: null,
last_turn_id: null,
@@ -229,6 +230,9 @@ export class InMemorySessionStore<
) {
continue;
}
+ if (input.created_by !== undefined && stored.record.created_by !== input.created_by) {
+ continue;
+ }
const createdAt = stored.record.created_at.getTime();
if (input.start_timestamp !== undefined && createdAt < input.start_timestamp.getTime()) continue;
if (input.end_timestamp !== undefined && createdAt > input.end_timestamp.getTime()) continue;
diff --git a/packages/harness/tests/agent-session/capabilityState.test.ts b/packages/harness/tests/agent-session/capabilityState.test.ts
index 16ba7b033..dd254ea71 100644
--- a/packages/harness/tests/agent-session/capabilityState.test.ts
+++ b/packages/harness/tests/agent-session/capabilityState.test.ts
@@ -62,6 +62,7 @@ describe('capability_state (tfy.plan fixture)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
@@ -133,6 +134,7 @@ describe('capability_state (tfy.plan fixture)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const planV1: JsonValue = {
@@ -225,6 +227,7 @@ describe('capability_state (tfy.plan fixture)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const turn = await session.createTurn({
diff --git a/packages/harness/tests/agent-session/resolveAgentSpec.test.ts b/packages/harness/tests/agent-session/resolveAgentSpec.test.ts
index ed44064f9..c57d3e337 100644
--- a/packages/harness/tests/agent-session/resolveAgentSpec.test.ts
+++ b/packages/harness/tests/agent-session/resolveAgentSpec.test.ts
@@ -25,6 +25,7 @@ describe('SessionHandle.createTurn named resolve', () => {
const session = await sessions.create({
tenant_id: 'tenant-1',
session_id: 's-named',
+ created_by: 'user-1',
agent: { type: 'ref', agent_id: 'agent-abc' },
});
diff --git a/packages/harness/tests/agent-session/sessions.test.ts b/packages/harness/tests/agent-session/sessions.test.ts
index a986caea5..c0c7748a2 100644
--- a/packages/harness/tests/agent-session/sessions.test.ts
+++ b/packages/harness/tests/agent-session/sessions.test.ts
@@ -13,6 +13,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const created = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec({ instructions: 'hydrate-me' }) },
custom: { tag: 'a' },
});
@@ -36,6 +37,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const turn = await session.createTurn({
@@ -65,6 +67,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const created = await session.createTurn({
@@ -101,6 +104,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const t1 = await session.createTurn({
@@ -130,6 +134,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
const first = await session.createTurn({
@@ -161,6 +166,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
await expect(
@@ -197,6 +203,7 @@ describe('Sessions / SessionHandle / TurnHandle (storage + createTurn)', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
});
diff --git a/packages/harness/tests/agent-session/store/storeContractSuite.ts b/packages/harness/tests/agent-session/store/storeContractSuite.ts
index d4a0cd235..941590f0a 100644
--- a/packages/harness/tests/agent-session/store/storeContractSuite.ts
+++ b/packages/harness/tests/agent-session/store/storeContractSuite.ts
@@ -82,6 +82,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: tenant,
session_id: sessionId,
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: agentSpec },
custom: null,
});
@@ -166,6 +167,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const session = await store.getSession({ tenant_id: tenant, session_id: sessionId });
expect(session).toBeDefined();
expect(mustGet(session).tenant_id).toBe(tenant);
+ expect(mustGet(session).created_by).toBe('user-1');
expect(mustGet(session).agent).toMatchObject({
type: 'value',
agent_spec: { model: { name: 'test-provider/test-model' } },
@@ -174,11 +176,38 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
expect(mustGet(session).title).toBeNull();
});
+ it('createSession persists created_by', async () => {
+ const store = createStore();
+ await store.createSession({
+ tenant_id: tenant,
+ session_id: 'created-by-session',
+ created_by: '[email protected]',
+ agent: { type: 'value', agent_spec: makeAgentSpec() },
+ custom: null,
+ });
+ const session = mustGet(await store.getSession({ tenant_id: tenant, session_id: 'created-by-session' }));
+ expect(session.created_by).toBe('[email protected]');
+
+ const listed = await store.listSessions({
+ tenant_id: tenant,
+ limit: 10,
+ page_token: undefined,
+ order: undefined,
+ start_timestamp: undefined,
+ end_timestamp: undefined,
+ agent_id: undefined,
+ created_by: undefined,
+ });
+ expect(listed.data.map(s => s.session_id)).toContain('created-by-session');
+ expect(listed.data.find(s => s.session_id === 'created-by-session')?.created_by).toBe('[email protected]');
+ });
+
it('persists ref agents and listSessions filters by agent_id', async () => {
const store = createStore();
await store.createSession({
tenant_id: tenant,
session_id: 'named-1',
+ created_by: 'user-1',
agent: { type: 'ref', agent_id: 'agent-abc' },
custom: null,
});
@@ -189,6 +218,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const filtered = await store.listSessions({
agent_id: 'agent-abc',
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
page_token: undefined,
@@ -204,6 +234,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: tenant,
session_id: 'named-1',
+ created_by: 'user-1',
agent: { type: 'ref', agent_id: 'agent-abc' },
custom: null,
});
@@ -261,6 +292,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
store.createSession({
tenant_id: 'other',
session_id: sessionId,
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
custom: null,
}),
@@ -275,6 +307,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: 'other',
session_id: 'other-session',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
custom: null,
});
@@ -357,6 +390,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const listed = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
page_token: undefined,
@@ -455,6 +489,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: tenant,
session_id: nested,
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
custom: null,
});
@@ -582,6 +617,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: tenant,
session_id: id,
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
custom: null,
});
@@ -595,12 +631,14 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
await store.createSession({
tenant_id: 'other',
session_id: 'sx',
+ created_by: 'user-1',
agent: { type: 'value', agent_spec: makeAgentSpec() },
custom: null,
});
const desc = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
page_token: undefined,
@@ -612,6 +650,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const asc = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
page_token: undefined,
@@ -628,6 +667,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const first = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 2,
page_token: undefined,
@@ -639,6 +679,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
expect(first.pagination.next_page_token).toBeDefined();
const second = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 2,
page_token: first.pagination.next_page_token,
@@ -651,6 +692,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const all = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
page_token: undefined,
@@ -663,6 +705,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
const middleCreatedAt = middleSession.created_at;
const bounded = await store.listSessions({
agent_id: undefined,
+ created_by: undefined,
tenant_id: tenant,
limit: 10,
order: 'asc',
@@ -672,6 +715,49 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
});
expect(bounded.data.map(s => s.session_id)).toEqual(['sb']);
});
+
+ it('filters by created_by', async () => {
+ const store = createStore();
+ await store.createSession({
+ tenant_id: tenant,
+ session_id: 'alice-session',
+ created_by: 'alice',
+ agent: { type: 'value', agent_spec: makeAgentSpec() },
+ custom: null,
+ });
+ await store.createSession({
+ tenant_id: tenant,
+ session_id: 'bob-session',
+ created_by: 'bob',
+ agent: { type: 'value', agent_spec: makeAgentSpec() },
+ custom: null,
+ });
+
+ const aliceOnly = await store.listSessions({
+ agent_id: undefined,
+ created_by: 'alice',
+ tenant_id: tenant,
+ limit: 10,
+ page_token: undefined,
+ order: undefined,
+ start_timestamp: undefined,
+ end_timestamp: undefined,
+ });
+ expect(aliceOnly.data.map(s => s.session_id)).toEqual(['alice-session']);
+ expect(aliceOnly.data[0]?.created_by).toBe('alice');
+
+ const unmatched = await store.listSessions({
+ agent_id: undefined,
+ created_by: 'nobody',
+ tenant_id: tenant,
+ limit: 10,
+ page_token: undefined,
+ order: undefined,
+ start_timestamp: undefined,
+ end_timestamp: undefined,
+ });
+ expect(unmatched.data).toEqual([]);
+ });
});
describe('createTurn', () => {
diff --git a/packages/harness/tests/agent-session/turnStream.test.ts b/packages/harness/tests/agent-session/turnStream.test.ts
index a829b0025..1451409ef 100644
--- a/packages/harness/tests/agent-session/turnStream.test.ts
+++ b/packages/harness/tests/agent-session/turnStream.test.ts
@@ -23,6 +23,7 @@ describe('TurnHandle.stream()', () => {
const session = await sessions.create({
tenant_id: tenant,
session_id: 's1',
+ created_by: 'user-1',
agent: {
type: 'value',
agent_spec: makeAgentSpec({
@@ -475,6 +476,7 @@ describe('TurnResourceResolver caches', () => {
const session = await sessions.create({
tenant_id: 't',
session_id: 's',
+ created_by: 'user-1',
agent: {
type: 'value',
agent_spec: makeAgentSpec({
diff --git a/packages/server/src/apis/sessions.ts b/packages/server/src/apis/sessions.ts
index fda647ba7..a39a157c1 100644
--- a/packages/server/src/apis/sessions.ts
+++ b/packages/server/src/apis/sessions.ts
@@ -57,6 +57,7 @@ export function toWireSession(record: SessionRecord): Session {
id: record.session_id,
agent: record.agent,
title: record.title,
+ created_by: record.created_by,
created_at: record.created_at.toISOString(),
updated_at: record.updated_at.toISOString(),
};
@@ -197,6 +198,7 @@ export function createSessionsRouter(deps: SessionsRouterDeps) {
const session = await deps.sessions.create({
tenant_id: TENANT_ID,
session_id: sessionId,
+ created_by: 'trueforge-default',
agent: body.agent,
});
return c.json({ data: toWireSession(session.record) }, 201);
@@ -213,6 +215,7 @@ export function createSessionsRouter(deps: SessionsRouterDeps) {
const session = await deps.sessions.create({
tenant_id: TENANT_ID,
session_id: sessionId,
+ created_by: 'trueforge-default',
agent: body.agent,
});
return c.json({ data: toWireSession(session.record) }, 201);
@@ -276,6 +279,7 @@ export function createSessionsRouter(deps: SessionsRouterDeps) {
try {
const { data, pagination } = await deps.sessionStore.listSessions({
agent_id: query.agent_id,
+ created_by: query.created_by,
tenant_id: TENANT_ID,
limit: query.limit,
order: query.order,
diff --git a/packages/server/src/db/postgres/migrations/20260811_000001_session_created_by.ts b/packages/server/src/db/postgres/migrations/20260811_000001_session_created_by.ts
new file mode 100644
index 000000000..1f161d49c
--- /dev/null
+++ b/packages/server/src/db/postgres/migrations/20260811_000001_session_created_by.ts
@@ -0,0 +1,15 @@
+import { sql, type Kysely } from 'kysely';
+
+/** Add immutable `created_by` identity on session. */
+export async function up(db: Kysely<unknown>): Promise<void> {
+ await sql`SET LOCAL lock_timeout = '5s'`.execute(db);
+ await db.schema
+ .alterTable('session')
+ .addColumn('created_by', 'text', col => col.notNull().defaultTo(''))
+ .execute();
+}
+
+export async function down(db: Kysely<unknown>): Promise<void> {
+ await sql`SET LOCAL lock_timeout = '5s'`.execute(db);
+ await db.schema.alterTable('session').dropColumn('created_by').execute();
+}
diff --git a/packages/server/src/db/postgres/session-store/queries/sessions.ts b/packages/server/src/db/postgres/session-store/queries/sessions.ts
index d7dbee61d..d16b50fd7 100644
--- a/packages/server/src/db/postgres/session-store/queries/sessions.ts
+++ b/packages/server/src/db/postgres/session-store/queries/sessions.ts
@@ -48,6 +48,7 @@ function parseSessionCustom(value: Record<string, unknown> | null): SessionCusto
function mapRowToSessionRecord(row: {
tenant_id: string;
session_id: string;
+ created_by: string;
agent_id: string | null;
agent_spec: AgentSpec | null;
title: string | null;
@@ -60,6 +61,7 @@ function mapRowToSessionRecord(row: {
return {
tenant_id: row.tenant_id,
session_id: row.session_id,
+ created_by: row.created_by,
agent: sessionAgentFromColumns({
session_id: row.session_id,
agent_id: row.agent_id,
@@ -84,6 +86,7 @@ export async function createSession(db: Kysely<Database>, input: CreateSessionIn
.values({
tenant_id: input.tenant_id,
session_id: input.session_id,
+ created_by: input.created_by,
agent_id: columns.agent_id,
agent_spec: columns.agent_spec !== null ? json(columns.agent_spec) : null,
title: null,
@@ -186,6 +189,9 @@ export async function listSessions(
if (input.agent_id !== undefined) {
query = query.where('agent_id', '=', input.agent_id);
}
+ if (input.created_by !== undefined) {
+ query = query.where('created_by', '=', input.created_by);
+ }
if (input.start_timestamp !== undefined) {
query = query.where('created_at', '>=', input.start_timestamp);
}
diff --git a/packages/server/src/db/postgres/types.ts b/packages/server/src/db/postgres/types.ts
index a11dc79dc..d359c8099 100644
--- a/packages/server/src/db/postgres/types.ts
+++ b/packages/server/src/db/postgres/types.ts
@@ -45,6 +45,8 @@ export interface SessionTable {
tenant_id: string;
/** key */
session_id: string;
+ /** Caller identity that created the session (immutable after create). */
+ created_by: string;
/**
* Named registry binding; XOR with `agent_spec`
* (CHECK session_agent_xor_check).
diff --git a/packages/server/src/db/sqlite/migrations/20260811_000001_session_created_by.ts b/packages/server/src/db/sqlite/migrations/20260811_000001_session_created_by.ts
new file mode 100644
index 000000000..1f7797241
--- /dev/null
+++ b/packages/server/src/db/sqlite/migrations/20260811_000001_session_created_by.ts
@@ -0,0 +1,15 @@
+import { sql, type Kysely } from 'kysely';
+
+/** Add immutable `created_by` identity on session. */
+export async function up(db: Kysely<unknown>): Promise<void> {
+ await sql`
+ ALTER TABLE session
+ ADD COLUMN created_by TEXT NOT NULL DEFAULT ''
+ `.execute(db);
+}
+
+export async function down(db: Kysely<unknown>): Promise<void> {
+ // SQLite cannot DROP COLUMN under STRICT without a table rebuild; irreversible.
+ void db;
+ return Promise.reject(new Error('20260811_000001_session_created_by is not reversible'));
+}
diff --git a/packages/server/src/db/sqlite/session-store/queries/sessions.ts b/packages/server/src/db/sqlite/session-store/queries/sessions.ts
index 9f7c3140e..a72bf2acd 100644
--- a/packages/server/src/db/sqlite/session-store/queries/sessions.ts
+++ b/packages/server/src/db/sqlite/session-store/queries/sessions.ts
@@ -39,6 +39,7 @@ function parseSessionCustom(value: Record<string, unknown> | null): SessionCusto
function mapRowToSessionRecord(row: {
tenant_id: string;
session_id: string;
+ created_by: string;
agent_id: string | null;
agent_spec: AgentSpec | null;
title: string | null;
@@ -51,6 +52,7 @@ function mapRowToSessionRecord(row: {
return {
tenant_id: row.tenant_id,
session_id: row.session_id,
+ created_by: row.created_by,
agent: sessionAgentFromColumns({
session_id: row.session_id,
agent_id: row.agent_id,
@@ -69,6 +71,7 @@ function sessionSelectColumns() {
return [
'tenant_id' as const,
'session_id' as const,
+ 'created_by' as const,
'agent_id' as const,
jsonText<AgentSpec | null>(sql.ref('agent_spec')).as('agent_spec'),
'title' as const,
@@ -90,6 +93,7 @@ export async function createSession(db: Kysely<Database>, input: CreateSessionIn
.values({
tenant_id: input.tenant_id,
session_id: input.session_id,
+ created_by: input.created_by,
agent_id: columns.agent_id,
agent_spec: columns.agent_spec !== null ? jsonbBind(columns.agent_spec) : null,
title: null,
@@ -187,6 +191,9 @@ export async function listSessions(
if (input.agent_id !== undefined) {
query = query.where('agent_id', '=', input.agent_id);
}
+ if (input.created_by !== undefined) {
+ query = query.where('created_by', '=', input.created_by);
+ }
if (input.start_timestamp !== undefined) {
query = query.where('created_at', '>=', input.start_timestamp.toISOString());
}
diff --git a/packages/server/src/db/sqlite/types.ts b/packages/server/src/db/sqlite/types.ts
index c496b2aac..3c62f9a00 100644
--- a/packages/server/src/db/sqlite/types.ts
+++ b/packages/server/src/db/sqlite/types.ts
@@ -48,6 +48,8 @@ type JsonbColumn<T extends object | null> = JSONColumnType<T, T | string, T | st
export interface SessionTable {
tenant_id: string;
session_id: string;
+ /** Caller identity that created the session (immutable after create). */
+ created_by: string;
/** Named registry binding; XOR with `agent_spec`. */
agent_id: string | null;
/** Inline draft binding; XOR with `agent_id`. */
diff --git a/packages/server/src/routes/sessionRoutes.ts b/packages/server/src/routes/sessionRoutes.ts
index 06602c750..6d1dcd890 100644
--- a/packages/server/src/routes/sessionRoutes.ts
+++ b/packages/server/src/routes/sessionRoutes.ts
@@ -142,7 +142,7 @@ export const listSessionsRoute = createRoute({
tags: [SESSIONS_TAG],
summary: 'List sessions',
description:
- 'List sessions (newest first by default), token-paginated. Optional `agent_id` filters to sessions bound to that named agent. Pass `page_token` to fetch the next page, keeping the other query params constant.',
+ 'List sessions (newest first by default), token-paginated. Optional `agent_id` filters to sessions bound to that named agent; optional `created_by` filters by creator identity. Pass `page_token` to fetch the next page, keeping the other query params constant.',
'x-fern-sdk-group-name': ['sessions'],
'x-fern-sdk-method-name': 'list',
'x-fern-pagination': TOKEN_PAGINATION,
diff --git a/packages/server/src/schemas/session.ts b/packages/server/src/schemas/session.ts
index 9a543e2f9..5eb4721bb 100644
--- a/packages/server/src/schemas/session.ts
+++ b/packages/server/src/schemas/session.ts
@@ -43,6 +43,7 @@ export const ListSessionsRequestQuerySchema = z
'Inclusive upper bound on `created_at` (ISO-8601 / RFC 3339).',
),
agent_id: z.string().min(1).optional().describe('When set, only sessions bound to this agent id are returned.'),
+ created_by: z.string().min(1).optional().describe('When set, only sessions created by this identity are returned.'),
})
.openapi('ListSessionsRequestQuery');
diff --git a/packages/server/tests/unit/apis/deletedSessionCrud.test.ts b/packages/server/tests/unit/apis/deletedSessionCrud.test.ts
index 1638b934b..b5a8d03a4 100644
--- a/packages/server/tests/unit/apis/deletedSessionCrud.test.ts
+++ b/packages/server/tests/unit/apis/deletedSessionCrud.test.ts
@@ -68,6 +68,7 @@ describe('public CRUD after session deletion', () => {
await sessionStore.createSession({
tenant_id: TENANT_ID,
session_id: 's1',
+ created_by: 'user-1',
agent: {
type: 'value',
agent_spec: AgentSpecSchema.parse({
diff --git a/packages/server/tests/unit/apis/sessionHttp.test.ts b/packages/server/tests/unit/apis/sessionHttp.test.ts
index 5b52cc787..295f1e934 100644
--- a/packages/server/tests/unit/apis/sessionHttp.test.ts
+++ b/packages/server/tests/unit/apis/sessionHttp.test.ts
@@ -80,10 +80,15 @@ describe('sessions HTTP agent binding', () => {
const res = await app.request('/', jsonInit('POST', { agent: { type: 'value', agent_spec: draftSpec } }));
expect(res.status).toBe(201);
const json = (await res.json()) as {
- data: { id: string; agent: { type: string; agent_spec?: { instructions?: string } } };
+ data: {
+ id: string;
+ created_by: string;
+ agent: { type: string; agent_spec?: { instructions?: string } };
+ };
};
expect(json.data.agent.type).toBe('value');
expect(json.data.agent.agent_spec?.instructions).toBe('draft');
+ expect(json.data.created_by).toBe('trueforge-default');
});
it('returns 404 when creating a session for an unknown agent_id', async () => {
@@ -117,6 +122,24 @@ describe('sessions HTTP agent binding', () => {
expect(listJson.data.some(row => row.id === json.data.id)).toBe(true);
});
+ it('filters list by created_by', async () => {
+ const created = await app.request('/', jsonInit('POST', { agent: { type: 'value', agent_spec: draftSpec } }));
+ expect(created.status).toBe(201);
+ const json = (await created.json()) as { data: { id: string; created_by: string } };
+ expect(json.data.created_by).toBe('trueforge-default');
+
+ const matched = await app.request('/?created_by=trueforge-default');
+ expect(matched.status).toBe(200);
+ const matchedJson = (await matched.json()) as { data: Array<{ id: string; created_by: string }> };
+ expect(matchedJson.data.some(row => row.id === json.data.id)).toBe(true);
+ expect(matchedJson.data.every(row => row.created_by === 'trueforge-default')).toBe(true);
+
+ const unmatched = await app.request('/?created_by=someone-else');
+ expect(unmatched.status).toBe(200);
+ const unmatchedJson = (await unmatched.json()) as { data: Array<{ id: string }> };
+ expect(unmatchedJson.data.some(row => row.id === json.data.id)).toBe(false);
+ });
+
it('rejects PATCH agent on a session bound by agent_id', async () => {
const agent = await agentStore.createAgent({
tenant_id: TENANT_ID,