The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
refactor(ai): isolate agent history migration state from runtime (#26752)
Félix Malfait8192231bb3e2
diff --git a/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration-state.service.spec.ts b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration-state.service.spec.tsnew file mode 100644index 00000000000..5c2eff147d1--- /dev/null+++ b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration-state.service.spec.ts@@ -0,0 +1,164 @@+import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';++const WORKSPACE_ID = '20202020-1111-4111-8111-111111111111';+const TABLE_NAME =+ '"workspace_20202020111141118111111111111111"."agentChatThread"';++describe('AgentHistoryMigrationStateService', () => {+ const service = new AgentHistoryMigrationStateService();+ const query = jest.fn();+ const runner = { query };++ beforeEach(() => {+ query.mockReset();+ });++ it.each(['clearing', 'copying', 'aborting'] as const)(+ 'retains the %s cursor and rollback retention facts',+ async (phase) => {+ const state: AgentHistoryMigrationState = {+ storage: 'workspace',+ migration: {+ phase,+ target: 'core',+ tableIndex: 5,+ lastId: WORKSPACE_ID,+ },+ verifiedAt: '2026-09-01T00:00:00.000Z',+ cleanedAt: '2026-09-20T00:00:00.000Z',+ };+ query.mockResolvedValue([+ { workspaceId: WORKSPACE_ID, type: 'CONFIG_VARIABLE', value: state },+ ]);++ await expect(service.readState(runner, WORKSPACE_ID)).resolves.toEqual(+ state,+ );+ },+ );++ it.each([+ { storage: 'unknown' },+ { storage: 'workspace', verifiedAt: 'invalid' },+ { storage: 'workspace', cleanedAt: 'invalid' },+ {+ storage: 'core',+ migration: {+ phase: 'copying',+ target: 'core',+ tableIndex: 0,+ lastId: null,+ },+ },+ {+ storage: 'core',+ migration: {+ phase: 'copying',+ target: 'workspace',+ tableIndex: 6,+ lastId: null,+ },+ },+ {+ storage: 'core',+ migration: {+ phase: 'copying',+ target: 'workspace',+ tableIndex: 1,+ lastId: 'invalid',+ },+ },+ ])('refuses corrupt durable state: %j', async (value) => {+ query.mockResolvedValue([+ { workspaceId: WORKSPACE_ID, type: 'CONFIG_VARIABLE', value },+ ]);++ await expect(service.readState(runner, WORKSPACE_ID)).rejects.toMatchObject(+ {+ code: 'INVALID_STATE',+ },+ );+ });++ it('refuses a route stored under another key type', async () => {+ query.mockResolvedValue([+ {+ workspaceId: WORKSPACE_ID,+ type: 'USER_VAR',+ value: { storage: 'workspace' },+ },+ ]);++ await expect(service.readState(runner, WORKSPACE_ID)).rejects.toMatchObject(+ {+ code: 'INVALID_STATE',+ },+ );+ });++ it('starts unrouted history in core when the workspace table does not exist', async () => {+ query.mockResolvedValue([]);++ await expect(service.readState(runner, WORKSPACE_ID)).resolves.toEqual({+ storage: 'core',+ });+ });++ it('starts unrouted history in core when the workspace table is empty', async () => {+ query+ .mockResolvedValueOnce([])+ .mockResolvedValueOnce([+ { workspaceId: WORKSPACE_ID, tableName: TABLE_NAME },+ ])+ .mockResolvedValueOnce([]);++ await expect(service.readState(runner, WORKSPACE_ID)).resolves.toEqual({+ storage: 'core',+ });+ });++ it('refuses to select a stale core snapshot when a populated workspace lost its route', async () => {+ query+ .mockResolvedValueOnce([])+ .mockResolvedValueOnce([+ { workspaceId: WORKSPACE_ID, tableName: TABLE_NAME },+ ])+ .mockResolvedValueOnce([{ workspaceId: WORKSPACE_ID }]);++ await expect(service.readState(runner, WORKSPACE_ID)).rejects.toMatchObject(+ {+ code: 'MISSING_STATE',+ },+ );+ });++ it('writes the durable protocol understood by deployed servers', async () => {+ const state: AgentHistoryMigrationState = {+ storage: 'core',+ migration: {+ phase: 'copying',+ target: 'workspace',+ tableIndex: 2,+ lastId: WORKSPACE_ID,+ },+ };+ query.mockResolvedValue([{ key: 'agent-history-storage-v1' }]);++ await service.writeState(runner, WORKSPACE_ID, state);++ expect(query).toHaveBeenCalledWith(expect.any(String), [+ 'agent-history-storage-v1',+ WORKSPACE_ID,+ JSON.stringify(state),+ ]);+ });++ it('refuses to overwrite a conflicting non-configuration key', async () => {+ query.mockResolvedValue([]);++ await expect(+ service.writeState(runner, WORKSPACE_ID, { storage: 'workspace' }),+ ).rejects.toMatchObject({ code: 'INVALID_STATE' });+ });+});diff --git a/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.tsindex 8a7fc21ef64..cb1673189e7 100644--- a/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts+++ b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts@@ -39,6 +39,7 @@ import { Pool } from 'pg';import { type FeatureFlagKey } from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';+import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';import { AgentHistoryMigrationService } from 'src/database/commands/agent-history/agent-history-migration.service';import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-history-tables.constant';import { AGENT_HISTORY_TEST_SCHEMA } from 'src/database/commands/agent-history/__tests__/agent-history-test-schema.constant';@@ -64,9 +65,10 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);synchronize: false,});const storage = new AgentHistoryStorageService(dataSource);+ const migrationState = new AgentHistoryMigrationStateService();const migration = new AgentHistoryMigrationService(dataSource,- storage,+ migrationState,new AgentHistoryMigrationDataService(),new AgentHistoryMigrationValidationService(),);@@ -514,9 +516,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);'UPDATE core."agentChatThread" SET "updatedAt" = $1',[originalTimestamp],);- const writeState = storage.writeState.bind(storage);+ const writeState = migrationState.writeState.bind(migrationState);const injectedCrash = jest- .spyOn(storage, 'writeState')+ .spyOn(migrationState, 'writeState').mockImplementation(async (runner, workspaceId, state) => {if (state.migration?.phase === 'copying' &&@@ -1004,9 +1006,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);});it('keeps the fence when a copied value fails verification, then allows abort', async () => {- const writeState = storage.writeState.bind(storage);+ const writeState = migrationState.writeState.bind(migrationState);const corruptCopy = jest- .spyOn(storage, 'writeState')+ .spyOn(migrationState, 'writeState').mockImplementation(async (runner, workspaceId, state) => {await writeState(runner, workspaceId, state);if (@@ -1160,6 +1162,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);await expect(lifecycle.initializeWorkspace(WORKSPACE_ID)).rejects.toThrow('Invalid agent history storage state',);+ await expect(migration.inspect(WORKSPACE_ID)).rejects.toThrow(+ 'Invalid agent history storage state',+ );await dataSource.query('DELETE FROM core."keyValuePair"');await expect(storage.run(WORKSPACE_ID, async () => undefined),@@ -1167,6 +1172,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);await expect(lifecycle.initializeWorkspace(WORKSPACE_ID)).rejects.toThrow('route is missing',);+ await expect(migration.inspect(WORKSPACE_ID)).rejects.toThrow(+ 'route is missing',+ );});it('leaves core-stored history out of reports and keeps a report stable across core cleanup', async () => {@@ -1215,9 +1223,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);workspaceId: WORKSPACE_ID,target: 'workspace',});- const writeState = storage.writeState.bind(storage);+ const writeState = migrationState.writeState.bind(migrationState);const collision = jest- .spyOn(storage, 'writeState')+ .spyOn(migrationState, 'writeState').mockImplementation(async (runner, workspaceId, state) => {await writeState(runner, workspaceId, state);if (@@ -1566,9 +1574,9 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);expect(await readRoute()).toBe('workspace');});it('does not allow copy to resume after an interrupted abort', async () => {- const writeState = storage.writeState.bind(storage);+ const writeState = migrationState.writeState.bind(migrationState);const failCopy = jest- .spyOn(storage, 'writeState')+ .spyOn(migrationState, 'writeState').mockImplementation(async (runner, workspaceId, state) => {if (state.migration?.phase === 'copying' &&@@ -1582,7 +1590,7 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);).rejects.toThrow('crash');failCopy.mockRestore();const failAbort = jest- .spyOn(storage, 'writeState')+ .spyOn(migrationState, 'writeState').mockImplementation(async (runner, workspaceId, state) => {if (!state.migration) throw new Error('abort interrupted');await writeState(runner, workspaceId, state);diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-data.service.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-data.service.tsindex 490ca8fb297..8c7f46cabe7 100644--- a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-data.service.ts+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-data.service.ts@@ -6,10 +6,10 @@ import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-import { getAgentHistoryColumn } from 'src/database/commands/agent-history/utils/get-agent-history-column.util';import { getAgentHistoryMigrationColumns } from 'src/database/commands/agent-history/utils/get-agent-history-migration-columns.util';import { getAgentHistoryTable } from 'src/database/commands/agent-history/utils/get-agent-history-table.util';-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';-type Storage = AgentHistoryStorageState['storage'];+type Storage = AgentHistoryMigrationState['storage'];type HistoryTable = (typeof AGENT_HISTORY_TABLES)[number];@Injectable()diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.exception.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.exception.tsnew file mode 100644index 00000000000..020c211fce2--- /dev/null+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.exception.ts@@ -0,0 +1,13 @@+import { msg } from '@lingui/core/macro';++import { CustomException } from 'src/utils/custom-exception';++export class AgentHistoryMigrationStateException extends CustomException<+ 'INVALID_STATE' | 'MISSING_STATE'+> {+ constructor(code: 'INVALID_STATE' | 'MISSING_STATE', message: string) {+ super(message, code, {+ userFriendlyMessage: msg`Unable to access AI history. Please try again.`,+ });+ }+}diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.service.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.service.tsnew file mode 100644index 00000000000..37c4dfb7405--- /dev/null+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.service.ts@@ -0,0 +1,101 @@+import { Injectable } from '@nestjs/common';+import { type QueryRunner } from 'typeorm';++import { AgentHistoryMigrationStateException } from 'src/database/commands/agent-history/agent-history-migration-state.exception';+import {+ agentHistoryMigrationStateSchema,+ type AgentHistoryMigrationState,+} from 'src/database/commands/agent-history/agent-history-migration-state.type';+import { AGENT_HISTORY_MIGRATION_STORAGE_KEY } from 'src/database/commands/agent-history/agent-history-migration-storage-key.constant';+import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';+import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';++// This frozen protocol intentionally remains compatible with deployed 2.42 servers.+// Runtime changes must not redefine the persisted migration cursor.+@Injectable()+export class AgentHistoryMigrationStateService {+ async readState(+ runner: Pick<QueryRunner, 'query'>,+ workspaceId: string,+ ): Promise<AgentHistoryMigrationState> {+ return (await this.readStates(runner, [workspaceId])).get(workspaceId)!;+ }++ private async readStates(+ runner: Pick<QueryRunner, 'query'>,+ workspaceIds: string[],+ ): Promise<Map<string, AgentHistoryMigrationState>> {+ const rows: { workspaceId: string; type: string; value: unknown }[] =+ await runner.query(+ `SELECT "workspaceId", type, value FROM core."keyValuePair" WHERE "key" = $1 AND "workspaceId" = ANY($2::uuid[]) AND "userId" IS NULL AND "applicationId" IS NULL`,+ [AGENT_HISTORY_MIGRATION_STORAGE_KEY, workspaceIds],+ );+ const states = new Map<string, AgentHistoryMigrationState>();+ for (const row of rows) {+ const parsed = agentHistoryMigrationStateSchema.safeParse(row.value);+ if (row.type !== 'CONFIG_VARIABLE' || !parsed.success) {+ throw new AgentHistoryMigrationStateException(+ 'INVALID_STATE',+ `Invalid agent history storage state for ${row.workspaceId}`,+ );+ }+ states.set(row.workspaceId, parsed.data);+ }+ const missing = workspaceIds.filter((id) => !states.has(id));+ if (missing.length) {+ const prepared: { workspaceId: string; tableName: string }[] =+ await runner.query(+ `SELECT id AS "workspaceId", table_name AS "tableName" FROM unnest($1::uuid[], $2::text[]) AS candidate(id, table_name) WHERE to_regclass(table_name) IS NOT NULL`,+ [+ missing,+ missing.map(+ (id) =>+ `${escapeIdentifier(getWorkspaceSchemaName(id))}."agentChatThread"`,+ ),+ ],+ );+ // A lost route must never silently expose an empty legacy snapshot after cleanup.+ // Empty, never-routed stores remain indistinguishable during the expand release.+ for (let offset = 0; offset < prepared.length; offset += 50) {+ const batch = prepared.slice(offset, offset + 50);+ const populated: { workspaceId: string }[] = await runner.query(+ batch+ .map(+ ({ tableName }, index) =>+ `SELECT $${index + 1}::uuid AS "workspaceId" WHERE EXISTS (SELECT 1 FROM ${tableName})`,+ )+ .join(' UNION ALL '),+ batch.map(({ workspaceId }) => workspaceId),+ );+ if (populated.length) {+ throw new AgentHistoryMigrationStateException(+ 'MISSING_STATE',+ `Agent history route is missing for ${populated[0].workspaceId}; restore its durable state before serving traffic`,+ );+ }+ }+ for (const workspaceId of missing)+ states.set(workspaceId, { storage: 'core' });+ }+ return states;+ }++ async writeState(+ runner: Pick<QueryRunner, 'query'>,+ workspaceId: string,+ state: AgentHistoryMigrationState,+ ): Promise<void> {+ const rows = await runner.query(+ `INSERT INTO core."keyValuePair" ("key", "workspaceId", "type", "value") VALUES ($1, $2, 'CONFIG_VARIABLE', $3::jsonb)+ ON CONFLICT ("key", "workspaceId") WHERE "userId" IS NULL AND "applicationId" IS NULL+ DO UPDATE SET "value" = EXCLUDED."value", "updatedAt" = now() WHERE "keyValuePair".type = 'CONFIG_VARIABLE' RETURNING "key"`,+ [AGENT_HISTORY_MIGRATION_STORAGE_KEY, workspaceId, JSON.stringify(state)],+ );+ if (!rows.length) {+ throw new AgentHistoryMigrationStateException(+ 'INVALID_STATE',+ 'Agent history route conflicts with a non-configuration key',+ );+ }+ }+}diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.type.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.type.tsnew file mode 100644index 00000000000..d24e3c4dd0c--- /dev/null+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-state.type.ts@@ -0,0 +1,24 @@+import { z } from 'zod';+import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-history-tables.constant';++export const agentHistoryMigrationStateSchema = z+ .object({+ storage: z.enum(['core', 'workspace']),+ migration: z+ .object({+ phase: z.enum(['clearing', 'copying', 'aborting']),+ target: z.enum(['core', 'workspace']),+ tableIndex: z.number().int().min(0).max(AGENT_HISTORY_TABLES.length),+ lastId: z.string().uuid().nullable(),+ })+ .optional(),+ verifiedAt: z.string().datetime().optional(),+ cleanedAt: z.string().datetime().optional(),+ })+ .refine(+ (state) => !state.migration || state.migration.target !== state.storage,+ );++export type AgentHistoryMigrationState = z.infer<+ typeof agentHistoryMigrationStateSchema+>;diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-storage-key.constant.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-storage-key.constant.tsnew file mode 100644index 00000000000..dad90f4845c--- /dev/null+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-storage-key.constant.ts@@ -0,0 +1,2 @@+// This key and its advisory locks must stay compatible with deployed servers.+export const AGENT_HISTORY_MIGRATION_STORAGE_KEY = 'agent-history-storage-v1';diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-validation.service.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-validation.service.tsindex 98f129439d9..b1ffe080ed7 100644--- a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-validation.service.ts+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration-validation.service.ts@@ -6,11 +6,11 @@ import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-import { getAgentHistoryColumn } from 'src/database/commands/agent-history/utils/get-agent-history-column.util';import { getAgentHistoryMigrationColumns } from 'src/database/commands/agent-history/utils/get-agent-history-migration-columns.util';import { getAgentHistoryTable } from 'src/database/commands/agent-history/utils/get-agent-history-table.util';-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';-type Storage = AgentHistoryStorageState['storage'];+type Storage = AgentHistoryMigrationState['storage'];@Injectable()export class AgentHistoryMigrationValidationService {diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.module.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.module.tsindex dedf99d944b..5d26e426cf3 100644--- a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.module.ts+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.module.ts@@ -7,16 +7,19 @@ import { WorkspaceIteratorModule } from 'src/database/commands/command-runners/wimport { AgentHistoryMigrateCommand } from 'src/database/commands/agent-history/agent-history-migrate.command';import { AgentHistoryMigrationService } from 'src/database/commands/agent-history/agent-history-migration.service';import { AgentHistorySchemaService } from 'src/database/commands/agent-history/agent-history-schema.service';-import { AgentHistoryModule } from 'src/engine/metadata-modules/ai/ai-history/ai-history.module';+import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';import { ApplicationModule } from 'src/engine/core-modules/application/application.module';import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration.module';@Module({- exports: [AgentHistorySchemaService, AgentHistoryMigrationService],+ exports: [+ AgentHistorySchemaService,+ AgentHistoryMigrationService,+ AgentHistoryMigrationStateService,+ ],imports: [WorkspaceIteratorModule,- AgentHistoryModule,ApplicationModule,WorkspaceCacheModule,WorkspaceMigrationModule,@@ -25,6 +28,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspaceAgentHistoryCleanupCommand,AgentHistoryMigrateCommand,AgentHistoryMigrationService,+ AgentHistoryMigrationStateService,AgentHistoryMigrationDataService,AgentHistoryMigrationValidationService,AgentHistorySchemaService,diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.service.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.service.tsindex 492898283a8..38d2d596433 100644--- a/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.service.ts+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-migration.service.ts@@ -7,11 +7,11 @@ import { AgentHistoryMigrationDataService } from 'src/database/commands/agent-hiimport { AgentHistoryMigrationValidationService } from 'src/database/commands/agent-history/agent-history-migration-validation.service';import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-history-tables.constant';import { getAgentHistoryTable } from 'src/database/commands/agent-history/utils/get-agent-history-table.util';-import { AGENT_HISTORY_STORAGE_KEY } from 'src/engine/metadata-modules/ai/ai-history/constants/agent-history-storage-key.constant';-import { AgentHistoryStorageService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-history-storage.service';-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { AGENT_HISTORY_MIGRATION_STORAGE_KEY } from 'src/database/commands/agent-history/agent-history-migration-storage-key.constant';+import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';-type Storage = AgentHistoryStorageState['storage'];+type Storage = AgentHistoryMigrationState['storage'];@Injectable()export class AgentHistoryMigrationService {@@ -19,7 +19,7 @@ export class AgentHistoryMigrationService {constructor(@InjectDataSource() private readonly dataSource: DataSource,- private readonly storageService: AgentHistoryStorageService,+ private readonly migrationStateService: AgentHistoryMigrationStateService,private readonly dataService: AgentHistoryMigrationDataService,private readonly validationService: AgentHistoryMigrationValidationService,) {}@@ -38,7 +38,7 @@ export class AgentHistoryMigrationService {}const runner = this.dataSource.createQueryRunner('master');let ownsRunnerLock = false;- const runnerKey = `${AGENT_HISTORY_STORAGE_KEY}:runner:${workspaceId}`;+ const runnerKey = `${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:runner:${workspaceId}`;try {await runner.connect();const [{ acquired }]: { acquired: boolean }[] = await runner.query(@@ -51,7 +51,10 @@ export class AgentHistoryMigrationService {'An agent history migration is already running for this workspace',);}- let state = await this.storageService.readState(runner, workspaceId);+ let state = await this.migrationStateService.readState(+ runner,+ workspaceId,+ );if (state.migration?.phase === 'aborting') {throw new Error('Finish aborting the incomplete migration before starting another copy',@@ -71,7 +74,7 @@ export class AgentHistoryMigrationService {runner,workspaceId,work: async () => {- const current = await this.storageService.readState(+ const current = await this.migrationStateService.readState(runner,workspaceId,);@@ -95,7 +98,7 @@ export class AgentHistoryMigrationService {workspaceId,});}- const next: AgentHistoryStorageState = {+ const next: AgentHistoryMigrationState = {...current,migration: {phase: 'clearing',@@ -104,7 +107,11 @@ export class AgentHistoryMigrationService {lastId: null,},};- await this.storageService.writeState(runner, workspaceId, next);+ await this.migrationStateService.writeState(+ runner,+ workspaceId,+ next,+ );return next;},});@@ -119,7 +126,7 @@ export class AgentHistoryMigrationService {runner,workspaceId,work: async () => {- const next: AgentHistoryStorageState = {+ const next: AgentHistoryMigrationState = {...state,migration: {phase: 'copying',@@ -128,7 +135,11 @@ export class AgentHistoryMigrationService {lastId: null,},};- await this.storageService.writeState(runner, workspaceId, next);+ await this.migrationStateService.writeState(+ runner,+ workspaceId,+ next,+ );return next;},});@@ -141,7 +152,7 @@ export class AgentHistoryMigrationService {runner,workspaceId,work: async () => {- const current = await this.storageService.readState(+ const current = await this.migrationStateService.readState(runner,workspaceId,);@@ -159,7 +170,7 @@ export class AgentHistoryMigrationService {lastId: progress.lastId,batchSize,});- const next: AgentHistoryStorageState = {+ const next: AgentHistoryMigrationState = {...current,migration: {phase: 'copying',@@ -171,7 +182,11 @@ export class AgentHistoryMigrationService {lastId: ids.length < batchSize ? null : ids[ids.length - 1],},};- await this.storageService.writeState(runner, workspaceId, next);+ await this.migrationStateService.writeState(+ runner,+ workspaceId,+ next,+ );this.logger.log(`${workspaceId}: copied ${ids.length} ${table.name} rows`,);@@ -189,7 +204,7 @@ export class AgentHistoryMigrationService {workspaceId,storage: target,});- await this.storageService.writeState(runner, workspaceId, {+ await this.migrationStateService.writeState(runner, workspaceId, {storage: target,verifiedAt: new Date().toISOString(),});@@ -218,7 +233,7 @@ export class AgentHistoryMigrationService {dryRun: boolean;}): Promise<void> {const runner = this.dataSource.createQueryRunner('master');- const key = `${AGENT_HISTORY_STORAGE_KEY}:runner:${workspaceId}`;+ const key = `${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:runner:${workspaceId}`;let ownsLock = false;try {await runner.connect();@@ -230,7 +245,10 @@ export class AgentHistoryMigrationService {if (!acquired) {throw new Error('Stop the running migration before aborting');}- const state = await this.storageService.readState(runner, workspaceId);+ const state = await this.migrationStateService.readState(+ runner,+ workspaceId,+ );if (!isDefined(state.migration)) {return;}@@ -244,7 +262,7 @@ export class AgentHistoryMigrationService {runner,workspaceId,work: () =>- this.storageService.writeState(runner, workspaceId, {+ this.migrationStateService.writeState(runner, workspaceId, {...state,migration: { ...state.migration!, phase: 'aborting' },}),@@ -258,7 +276,7 @@ export class AgentHistoryMigrationService {runner,workspaceId,work: () =>- this.storageService.writeState(runner, workspaceId, {+ this.migrationStateService.writeState(runner, workspaceId, {...state,migration: undefined,}),@@ -281,7 +299,10 @@ export class AgentHistoryMigrationService {const runner = this.dataSource.createQueryRunner('master');try {await runner.connect();- const state = await this.storageService.readState(runner, workspaceId);+ const state = await this.migrationStateService.readState(+ runner,+ workspaceId,+ );this.logger.log(`${workspaceId}: ${JSON.stringify(state)}`);await this.validationService.assertNoActiveStreams({runner,@@ -318,7 +339,7 @@ export class AgentHistoryMigrationService {throw new Error('Retention must be at least one day');}const runner = this.dataSource.createQueryRunner('master');- const key = `${AGENT_HISTORY_STORAGE_KEY}:runner:${workspaceId}`;+ const key = `${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:runner:${workspaceId}`;let ownsLock = false;try {await runner.connect();@@ -333,7 +354,10 @@ export class AgentHistoryMigrationService {);return;}- const state = await this.storageService.readState(runner, workspaceId);+ const state = await this.migrationStateService.readState(+ runner,+ workspaceId,+ );if (state.storage !== 'workspace' ||isDefined(state.migration) ||@@ -362,7 +386,7 @@ export class AgentHistoryMigrationService {workspaceId,storage: 'core',});- await this.storageService.writeState(runner, workspaceId, {+ await this.migrationStateService.writeState(runner, workspaceId, {...state,cleanedAt: new Date().toISOString(),});@@ -390,7 +414,7 @@ export class AgentHistoryMigrationService {try {await runner.query('SELECT pg_advisory_xact_lock(hashtextextended($1, 0))',- [`${AGENT_HISTORY_STORAGE_KEY}:${workspaceId}`],+ [`${AGENT_HISTORY_MIGRATION_STORAGE_KEY}:${workspaceId}`],);const result = await work();await runner.commitTransaction();diff --git a/packages/twenty-server/src/database/commands/agent-history/agent-history-tables.constant.ts b/packages/twenty-server/src/database/commands/agent-history/agent-history-tables.constant.tsindex 5bdd99ced0c..55fb6322563 100644--- a/packages/twenty-server/src/database/commands/agent-history/agent-history-tables.constant.ts+++ b/packages/twenty-server/src/database/commands/agent-history/agent-history-tables.constant.ts@@ -1,7 +1,6 @@-import { AGENT_HISTORY_OBJECT_NAMES } from 'src/engine/metadata-modules/ai/ai-history/constants/agent-history-object-names.constant';-import { type AgentHistoryObjectName } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-object-name.type';-const HISTORY_TABLES_BY_NAME = {- agentChatThread: {+// Parent-first order is persisted in the 2.42 migration cursor; keep it frozen.+export const AGENT_HISTORY_TABLES = [+ {name: 'agentChatThread',columns: ['id',@@ -23,11 +22,11 @@ const HISTORY_TABLES_BY_NAME = {'updatedAt',],},- agentTurn: {+ {name: 'agentTurn',columns: ['id', 'threadId', 'agentId', 'createdAt'],},- agentMessage: {+ {name: 'agentMessage',columns: ['id',@@ -43,7 +42,7 @@ const HISTORY_TABLES_BY_NAME = {'createdAt',],},- agentMessagePart: {+ {name: 'agentMessagePart',columns: ['id',@@ -73,15 +72,8 @@ const HISTORY_TABLES_BY_NAME = {'createdAt',],},- agentTurnEvaluation: {+ {name: 'agentTurnEvaluation',columns: ['id', 'turnId', 'score', 'comment', 'createdAt'],},-} as const satisfies Record<- AgentHistoryObjectName,- { name: AgentHistoryObjectName; columns: readonly string[] }->;--export const AGENT_HISTORY_TABLES = AGENT_HISTORY_OBJECT_NAMES.map(- (name) => HISTORY_TABLES_BY_NAME[name],-);+] as const;diff --git a/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-column.util.ts b/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-column.util.tsindex 1a622e8d610..17af012cd13 100644--- a/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-column.util.ts+++ b/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-column.util.ts@@ -1,4 +1,4 @@-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';export const getAgentHistoryColumn = ({tableName,@@ -6,7 +6,7 @@ export const getAgentHistoryColumn = ({columnName,}: {tableName: string;- storage: AgentHistoryStorageState['storage'];+ storage: AgentHistoryMigrationState['storage'];columnName: string;}): string => {// Chat archive state must not enter the generic workspace trash lifecycle.diff --git a/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-table.util.ts b/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-table.util.tsindex 0e3d6099e18..b00d3c454bb 100644--- a/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-table.util.ts+++ b/packages/twenty-server/src/database/commands/agent-history/utils/get-agent-history-table.util.ts@@ -1,4 +1,4 @@-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';@@ -8,7 +8,7 @@ export const getAgentHistoryTable = ({name,}: {workspaceId: string;- storage: AgentHistoryStorageState['storage'];+ storage: AgentHistoryMigrationState['storage'];name: string;}): string =>`${escapeIdentifier(storage === 'core' ? 'core' : getWorkspaceSchemaName(workspaceId))}.${escapeIdentifier(name)}`;diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-upgrade-version-command.module.tsindex 1e9e97a1456..540d08874e6 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-upgrade-version-command.module.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-upgrade-version-command.module.ts@@ -3,7 +3,6 @@ import { Module } from '@nestjs/common';import { MigrateAgentHistoryToWorkspaceCommand } from 'src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789914239896-migrate-agent-history-to-workspace.command';import { AgentHistoryMigrationModule } from 'src/database/commands/agent-history/agent-history-migration.module';import { AgentChatStreamStateModule } from 'src/engine/metadata-modules/ai/ai-chat/agent-chat-stream-state.module';-import { AgentHistoryModule } from 'src/engine/metadata-modules/ai/ai-history/ai-history.module';import { BackfillWorkspaceWorkflowVersionIdCommand } from 'src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789652804001-backfill-workspace-workflow-version-id.command';import { BackfillWorkflowExecutionCoreIdsCommand } from 'src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131001-backfill-workflow-execution-core-ids.command';import { MakeWorkflowRunProjectionRelationsNullableCommand } from 'src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789719131002-make-workflow-run-projection-relations-nullable.command';@@ -31,7 +30,6 @@ import { UpdateRecordCreationCommandLabelsCommand } from 'src/database/commands/imports: [AgentHistoryMigrationModule,AgentChatStreamStateModule,- AgentHistoryModule,ApplicationModule,MetricsModule,WorkflowCoreModule,diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789914239896-migrate-agent-history-to-workspace.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789914239896-migrate-agent-history-to-workspace.command.tsindex 99b3a98d3cd..9e42da8f03e 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789914239896-migrate-agent-history-to-workspace.command.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-42/2-42-workspace-command-1789914239896-migrate-agent-history-to-workspace.command.ts@@ -14,9 +14,9 @@ import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decoimport { AgentChatEventPublisherService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-event-publisher.service';import { AgentChatStreamHeartbeatService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-stream-heartbeat.service';import { type AgentChatThreadLastStreamError } from 'src/engine/metadata-modules/ai/ai-chat/types/agent-chat-thread-last-stream-error.type';-import { AGENT_HISTORY_OBJECT_NAMES } from 'src/engine/metadata-modules/ai/ai-history/constants/agent-history-object-names.constant';-import { AgentHistoryStorageService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-history-storage.service';-import { type AgentHistoryStorageState } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-storage-state.type';+import { AGENT_HISTORY_TABLES } from 'src/database/commands/agent-history/agent-history-tables.constant';+import { AgentHistoryMigrationStateService } from 'src/database/commands/agent-history/agent-history-migration-state.service';+import { type AgentHistoryMigrationState } from 'src/database/commands/agent-history/agent-history-migration-state.type';import { AiExceptionCode } from 'src/engine/metadata-modules/ai/ai.exception';import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';@@ -31,7 +31,7 @@ export class MigrateAgentHistoryToWorkspaceCommand extends ProvisionedWorkspaceCprotected readonly workspaceIteratorService: WorkspaceIteratorService,private readonly schema: AgentHistorySchemaService,private readonly migration: AgentHistoryMigrationService,- private readonly storage: AgentHistoryStorageService,+ private readonly storage: AgentHistoryMigrationStateService,private readonly streamHeartbeatService: AgentChatStreamHeartbeatService,private readonly eventPublisherService: AgentChatEventPublisherService,private readonly metricsService: MetricsService,@@ -57,21 +57,21 @@ export class MigrateAgentHistoryToWorkspaceCommand extends ProvisionedWorkspaceCdataSource,target,}: RunOnWorkspaceArgs & {- target: AgentHistoryStorageState['storage'];+ target: AgentHistoryMigrationState['storage'];}): Promise<void> {const dryRun = options.dryRun ?? false;if (!isDefined(dataSource)) {throw new Error('Agent history upgrade requires a workspace data source');}const runner = dataSource.createQueryRunner('master');- let state: AgentHistoryStorageState;+ let state: AgentHistoryMigrationState;try {await runner.connect();state = await this.storage.readState(runner, workspaceId);if (!(await runner.hasSchema(getWorkspaceSchemaName(workspaceId)))) {const history = await runner.query(- `SELECT 1 WHERE ${AGENT_HISTORY_OBJECT_NAMES.map(- (name) =>+ `SELECT 1 WHERE ${AGENT_HISTORY_TABLES.map(+ ({ name }) =>`EXISTS (SELECT 1 FROM core."${name}" WHERE "workspaceId" = $1)`,).join(' OR ')}`,[workspaceId],@@ -121,7 +121,7 @@ export class MigrateAgentHistoryToWorkspaceCommand extends ProvisionedWorkspaceC}: {dataSource: NonNullable<RunOnWorkspaceArgs['dataSource']>;workspaceId: string;- source: AgentHistoryStorageState['storage'];+ source: AgentHistoryMigrationState['storage'];}): Promise<void> {const runner = dataSource.createQueryRunner('master');try {@@ -157,7 +157,7 @@ export class MigrateAgentHistoryToWorkspaceCommand extends ProvisionedWorkspaceC}: {runner: QueryRunner;workspaceId: string;- source: AgentHistoryStorageState['storage'];+ source: AgentHistoryMigrationState['storage'];thread: { id: string; activeStreamId: string };}): Promise<void> {if (await this.streamHeartbeatService.isAlive(thread.activeStreamId)) {@@ -218,7 +218,7 @@ export class MigrateAgentHistoryToWorkspaceCommand extends ProvisionedWorkspaceCsource,}: {workspaceId: string;- source: AgentHistoryStorageState['storage'];+ source: AgentHistoryMigrationState['storage'];}): string {return getAgentHistoryTable({workspaceId,