The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Let agents resume themselves; workflow AI steps wait for a callback (#27397)
Félix Malfait9be3e50de692
diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/ai-agent-action/components/WorkflowRunStepAiAgentConversationButton.tsx b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/ai-agent-action/components/WorkflowRunStepAiAgentConversationButton.tsxindex d7754745ae1..df031ab4364 100644--- a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/ai-agent-action/components/WorkflowRunStepAiAgentConversationButton.tsx+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/ai-agent-action/components/WorkflowRunStepAiAgentConversationButton.tsx@@ -6,15 +6,20 @@ import { Button } from 'twenty-ui/primitives/input';import { useOpenAskAiThread } from '@/ai/hooks/useOpenAskAiThread';import { useWorkflowRunStepInfo } from '@/workflow/workflow-steps/hooks/useWorkflowRunStepInfo';+type WorkflowRunStepAiAgentConversationButtonProps = {+ stepId: string;+ stepLogThreadId?: string;+};+export const WorkflowRunStepAiAgentConversationButton = ({stepId,-}: {- stepId: string;-}) => {+ stepLogThreadId,+}: WorkflowRunStepAiAgentConversationButtonProps) => {const { t } = useLingui();const { openAskAiThread } = useOpenAskAiThread();const stepInfo = useWorkflowRunStepInfo({ stepId });- const threadId = stepInfo?.threadId;+ // runs from before the step log held the conversation kept it on the step info+ const threadId = stepLogThreadId ?? stepInfo?.threadId;if (!isDefined(threadId)) {return null;diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/components/WorkflowRunStepLogsDetail.tsx b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/components/WorkflowRunStepLogsDetail.tsxindex db26943f6f1..520ef505b16 100644--- a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/components/WorkflowRunStepLogsDetail.tsx+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/components/WorkflowRunStepLogsDetail.tsx@@ -97,7 +97,10 @@ export const WorkflowRunStepLogsDetail = ({ stepId }: { stepId: string }) => {case 'AI_AGENT':return (<>- <WorkflowRunStepAiAgentConversationButton stepId={stepId} />+ <WorkflowRunStepAiAgentConversationButton+ stepId={stepId}+ stepLogThreadId={stepLog.details.threadId}+ /><WorkflowRunStepLogsAiAgentDetail details={stepLog.details} /></>);diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791301683545-create-agent-run-suspension-table.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791301683545-create-agent-run-suspension-table.tsnew file mode 100644index 00000000000..536f6e510f9--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791301683545-create-agent-run-suspension-table.ts@@ -0,0 +1,17 @@+import { QueryRunner } from 'typeorm';++import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';+import { FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';++@RegisteredInstanceCommand('2.46.0', 1791301683545)+export class CreateAgentRunSuspensionTableFastInstanceCommand implements FastInstanceCommand {+ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query('CREATE TABLE "core"."agentRunSuspension" ("workspaceId" uuid NOT NULL, "id" uuid NOT NULL DEFAULT uuid_generate_v4(), "threadId" uuid NOT NULL, "caller" jsonb NOT NULL, "runSpec" jsonb, "summary" jsonb, "resumeCount" integer NOT NULL DEFAULT \'0\', "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "UQ_AGENT_RUN_SUSPENSION_THREAD_ID" UNIQUE ("threadId"), CONSTRAINT "PK_ffa8fcfd24c5a8e0122f5a682af" PRIMARY KEY ("id"))');+ await queryRunner.query('ALTER TABLE "core"."agentRunSuspension" ADD CONSTRAINT "FK_3005d9ef8e9f321e387a8ac0b6a" FOREIGN KEY ("workspaceId") REFERENCES "core"."workspace"("id") ON DELETE CASCADE ON UPDATE NO ACTION');+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query('ALTER TABLE "core"."agentRunSuspension" DROP CONSTRAINT "FK_3005d9ef8e9f321e387a8ac0b6a"');+ await queryRunner.query('DROP TABLE "core"."agentRunSuspension"');+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.tsindex 23ade3d642e..9303e7d1a93 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts@@ -11,6 +11,7 @@ import { BackfillAgentAndWorkflowIsSystemCommand } from 'src/database/commands/uimport { TurnHiddenAgentMessagesIntoSystemMessagesCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791204952095-turn-hidden-agent-messages-into-system-messages.command';import { AddAgentTurnRunFieldsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791227584393-add-agent-turn-run-fields.command';import { BackfillFailedAgentTurnsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791227584394-backfill-failed-agent-turns.command';+import { SuspendPausedAgentStepsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command';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';@@ -33,6 +34,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspaceDropAgentTurnEvaluationObjectCommand,AddAgentTurnRunFieldsCommand,BackfillFailedAgentTurnsCommand,+ SuspendPausedAgentStepsCommand,],})export class V2_46_UpgradeVersionCommandModule {}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791208395308-drop-workflow-run-from-chat-threads.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791208395308-drop-workflow-run-from-chat-threads.command.tsindex f70530e7ddc..9c0cb2686c5 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791208395308-drop-workflow-run-from-chat-threads.command.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791208395308-drop-workflow-run-from-chat-threads.command.ts@@ -2,9 +2,7 @@ import { Command } from 'nest-commander';import { STANDARD_OBJECTS } from 'twenty-shared/metadata';import { MetadataReadability } from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';-import { type EntityManager } from 'typeorm';-import { AgentHistoryUpgradeStorageService } from 'src/database/commands/agent-history/agent-history-upgrade-storage.service';import { ProvisionedWorkspaceCommandRunner } from 'src/database/commands/command-runners/provisioned-workspace.command-runner';import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';@@ -14,9 +12,7 @@ import { findFlatEntityByUniversalIdentifier } from 'src/engine/metadata-modulesimport { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';import { type FlatIndexMetadata } from 'src/engine/metadata-modules/flat-index-metadata/types/flat-index-metadata.type';import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';-import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';-import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';// Kept as literals because the entries they used to name have been removed// from the standard objects in the same change.@@ -27,48 +23,14 @@ const WORKFLOW_RUN_AGENT_CHAT_THREADS_FIELD_UNIVERSAL_IDENTIFIER =const CHAT_THREAD_WORKFLOW_RUN_INDEX_UNIVERSAL_IDENTIFIER ='cc9f8c37-a1ad-4d8d-8e27-894c2cf01a3b';-// A call still waiting in a run's conversation gets the step it waits for,-// which is how an answer or a wait outcome finds its step once the thread no-// longer names its run.-const stampPendingCallsWithTheirStep = async ({- manager,- workspaceId,-}: {- manager: EntityManager;- workspaceId: string;-}): Promise<number> => {- const schema = escapeIdentifier(getWorkspaceSchemaName(workspaceId));-- const stamped: { id: string }[] = await manager.query(- `UPDATE ${schema}."agentMessagePart" part- SET "toolOutput" = part."toolOutput" || jsonb_build_object(- 'workflowStep',- jsonb_build_object('workflowRunId', thread."workflowRunId", 'stepId', step.key)- )- FROM ${schema}."agentMessage" message- JOIN ${schema}."agentChatThread" thread ON thread.id = message."threadId"- JOIN ${schema}."workflowRun" run ON run.id = thread."workflowRunId"- CROSS JOIN LATERAL jsonb_each(COALESCE(run.state -> 'stepInfos', '{}'::jsonb)) AS step(key, value)- WHERE part."messageId" = message.id- AND step.value ->> 'threadId' = thread.id::text- AND jsonb_typeof(part."toolOutput") = 'object'- AND part."toolOutput" -> 'result' ->> 'status' = 'pending'- AND NOT part."toolOutput" ? 'workflowStep'- RETURNING part.id`,- );-- return stamped.length;-};-// A conversation an agent step held now belongs to the member it was routed-// to, as every other conversation does, rather than to its run. The calls it-// still waits on keep resuming their step, and threads read only through their-// own grants again.+// to, as every other conversation does, rather than to its run, and threads+// read only through their own grants again.@RegisteredWorkspaceCommand('2.46.0', 1791208395308)@Command({name: 'upgrade:2-46:drop-workflow-run-from-chat-threads',description:- 'Stop linking chat threads to workflow runs: pending calls name their step, threads become PRIVATE and the run link is dropped',+ 'Stop linking chat threads to workflow runs: threads become PRIVATE and the run link is dropped',})export class DropWorkflowRunFromChatThreadsCommand extends ProvisionedWorkspaceCommandRunner {constructor(@@ -76,7 +38,6 @@ export class DropWorkflowRunFromChatThreadsCommand extends ProvisionedWorkspaceCprivate readonly applicationService: ApplicationService,private readonly workspaceCacheService: WorkspaceCacheService,private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,- private readonly storage: AgentHistoryUpgradeStorageService,) {super(workspaceIteratorService);}@@ -142,22 +103,12 @@ export class DropWorkflowRunFromChatThreadsCommand extends ProvisionedWorkspaceCif (options.dryRun ?? false) {this.logger.log(- `[DRY RUN] Would stamp pending calls, make chat threads PRIVATE and drop ${fieldsToDelete.length} field(s) and ${indexesToDelete.length} index(es) for workspace ${workspaceId}`,+ `[DRY RUN] Would make chat threads PRIVATE and drop ${fieldsToDelete.length} field(s) and ${indexesToDelete.length} index(es) for workspace ${workspaceId}`,);return;}- const stampedCount = fieldsToDelete.some(- ({ universalIdentifier }) =>- universalIdentifier ===- CHAT_THREAD_WORKFLOW_RUN_FIELD_UNIVERSAL_IDENTIFIER,- )- ? await this.storage.run(workspaceId, ({ manager }) =>- stampPendingCallsWithTheirStep({ manager, workspaceId }),- )- : 0;-const { twentyStandardFlatApplication } =await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow({ workspaceId },@@ -206,13 +157,13 @@ export class DropWorkflowRunFromChatThreadsCommand extends ProvisionedWorkspaceC}this.logger.log(- `Workspace ${workspaceId}: stamped ${stampedCount} pending call(s), made chat threads PRIVATE and dropped their workflow run link`,+ `Workspace ${workspaceId}: made chat threads PRIVATE and dropped their workflow run link`,);}async down(_args: RunOnWorkspaceArgs): Promise<void> {// The standard objects no longer declare the run link, so it cannot be- // recreated, and the stamped calls resume their step without it.+ // recreated.}private async runMigration(diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.tsnew file mode 100644index 00000000000..9ccf912daa8--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts@@ -0,0 +1,145 @@+import { Command } from 'nest-commander';+import { STANDARD_OBJECTS } from 'twenty-shared/metadata';+import { isDefined } from 'twenty-shared/utils';++import { AgentHistoryUpgradeStorageService } from 'src/database/commands/agent-history/agent-history-upgrade-storage.service';+import { ProvisionedWorkspaceCommandRunner } from 'src/database/commands/command-runners/provisioned-workspace.command-runner';+import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';+import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';+import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator';+import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';+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';+import { buildWorkflowAgentRunSpec } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util';+import { type WorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';++type PausedAgentStep = {+ workflowRunId: string;+ stepId: string;+ threadId: string;+ step: WorkflowAiAgentAction;+ applicationId: string | null;+ summary: object | null;+};++// An agent step that paused on a question before 2.46 is PENDING with its conversation on+// stepInfo.threadId, and was continued by running the step again. The engine now continues the+// agent itself, so the step gets the suspension the engine continues it from, and its pending+// calls the mark that hands their answer to it.+@RegisteredWorkspaceCommand('2.46.0', 1791306663446)+@Command({+ name: 'upgrade:2-46:suspend-paused-agent-steps',+ description:+ 'Give agent steps paused on a question the suspension the engine continues them from',+})+export class SuspendPausedAgentStepsCommand extends ProvisionedWorkspaceCommandRunner {+ constructor(+ protected readonly workspaceIteratorService: WorkspaceIteratorService,+ private readonly workspaceCacheService: WorkspaceCacheService,+ private readonly storage: AgentHistoryUpgradeStorageService,+ ) {+ super(workspaceIteratorService);+ }++ override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {+ await this.up(args);+ }++ async up({ workspaceId, options }: RunOnWorkspaceArgs): Promise<void> {+ const { flatObjectMetadataMaps } =+ await this.workspaceCacheService.getOrRecompute(workspaceId, [+ 'flatObjectMetadataMaps',+ ]);++ if (+ !isDefined(+ flatObjectMetadataMaps.byUniversalIdentifier[+ STANDARD_OBJECTS.workflowRun.universalIdentifier+ ],+ )+ ) {+ this.logger.log(+ `workflowRun object not found for workspace ${workspaceId}, skipping`,+ );++ return;+ }++ const schema = escapeIdentifier(getWorkspaceSchemaName(workspaceId));++ const suspendedCount = await this.storage.run(+ workspaceId,+ async ({ manager, table }) => {+ const pausedSteps: PausedAgentStep[] = await manager.query(+ `SELECT run.id AS "workflowRunId", step.key AS "stepId",+ step.value ->> 'threadId' AS "threadId", flow_step.value AS "step",+ run."createdByContext" ->> 'applicationId' AS "applicationId",+ CASE WHEN run."stepLogs" -> step.key -> 'details' ->> 'type' = 'AI_AGENT'+ THEN (run."stepLogs" -> step.key -> 'details') - 'type' END AS "summary"+ FROM ${schema}."workflowRun" run+ CROSS JOIN LATERAL jsonb_each(COALESCE(run.state -> 'stepInfos', '{}'::jsonb)) AS step(key, value)+ JOIN LATERAL jsonb_array_elements(run.state -> 'flow' -> 'steps') AS flow_step(value)+ ON flow_step.value ->> 'id' = step.key+ WHERE run.status = 'RUNNING'+ AND run."deletedAt" IS NULL+ AND step.value ->> 'status' = 'PENDING'+ AND step.value ->> 'threadId' IS NOT NULL+ AND flow_step.value ->> 'type' = 'AI_AGENT'`,+ );++ if (pausedSteps.length === 0 || (options.dryRun ?? false)) {+ return pausedSteps.length;+ }++ await manager.query(+ `UPDATE ${table('agentMessagePart')} part+ SET "toolOutput" = part."toolOutput" || '{"awaitedByCaller": true}'::jsonb+ FROM ${table('agentMessage')} message+ WHERE part."messageId" = message.id+ AND message."threadId" = ANY($1::uuid[])+ AND jsonb_typeof(part."toolOutput") = 'object'+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'`,+ [pausedSteps.map(({ threadId }) => threadId)],+ );++ for (const pausedStep of pausedSteps) {+ await manager.query(+ `INSERT INTO "core"."agentRunSuspension" ("workspaceId", "threadId", "caller", "runSpec", "summary")+ VALUES ($1, $2, $3, $4, $5)+ ON CONFLICT ("threadId") DO NOTHING`,+ [+ workspaceId,+ pausedStep.threadId,+ JSON.stringify({+ type: 'WORKFLOW_STEP',+ ref: {+ workflowRunId: pausedStep.workflowRunId,+ stepId: pausedStep.stepId,+ },+ }),+ JSON.stringify(+ buildWorkflowAgentRunSpec({+ step: pausedStep.step,+ isApplicationBound: isDefined(pausedStep.applicationId),+ }),+ ),+ isDefined(pausedStep.summary)+ ? JSON.stringify(pausedStep.summary)+ : null,+ ],+ );+ }++ return pausedSteps.length;+ },+ );++ this.logger.log(+ `${options.dryRun ? '[DRY RUN] Would suspend' : 'Suspended'} ${suspendedCount} paused agent step(s) in workspace ${workspaceId}`,+ );+ }++ async down(_args: RunOnWorkspaceArgs): Promise<void> {+ // A step left with its suspension is still continued by the engine+ }+}diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.tsnew file mode 100644index 00000000000..ae700b8ff6f--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.ts@@ -0,0 +1,4 @@+// Referenced by @WasIntroducedInUpgrade on AgentRunSuspensionEntity so upgrade+// steps running below 2.46.0 don't query the table before this command creates it.+export const CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME =+ '2.46.0_CreateAgentRunSuspensionTableFastInstanceCommand_1791301683545';diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.tsindex f56ac836e72..7ca7e801302 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts@@ -227,6 +227,7 @@ import { DropAgentEvaluationInputsFastInstanceCommand } from 'src/database/commaimport { AddTriggersToAgentFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791219100450-add-triggers-to-agent';import { AddScopeAndDefaultValueToApplicationVariablesFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791274238040-add-scope-and-default-value-to-application-variables';import { CreatePendingWakeUpTableFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791299810581-create-pending-wake-up-table';+import { CreateAgentRunSuspensionTableFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791301683545-create-agent-run-suspension-table';export const INSTANCE_COMMANDS = [AddViewFieldGroupIdIndexOnViewFieldFastInstanceCommand,@@ -456,4 +457,5 @@ export const INSTANCE_COMMANDS = [AddTriggersToAgentFastInstanceCommand,AddScopeAndDefaultValueToApplicationVariablesFastInstanceCommand,CreatePendingWakeUpTableFastInstanceCommand,+ CreateAgentRunSuspensionTableFastInstanceCommand,];diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.tsindex eff3e54a79e..38b8284d0ad 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts@@ -6,9 +6,11 @@ import { PendingWakeUpSweepCronJob } from 'src/engine/core-modules/pending-wake-import { PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';import { ResumePendingWakeUpJob } from 'src/engine/core-modules/pending-wake-up/jobs/resume-pending-wake-up.job';import { PendingWakeUpDatabaseEventListener } from 'src/engine/core-modules/pending-wake-up/listeners/pending-wake-up-database-event.listener';+import { PendingWakeUpEventRecordService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service';import { PendingWakeUpOwnerHandlerRegistryService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service';import { PendingWakeUpResolverService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service';import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';+import { RecordCrudModule } from 'src/engine/core-modules/record-crud/record-crud.module';import { RecordShareModule } from 'src/engine/core-modules/record-share/record-share.module';import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/provide-workspace-scoped-repository';import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';@@ -16,12 +18,14 @@ import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache@Module({imports: [TypeOrmModule.forFeature([PendingWakeUpEntity]),+ RecordCrudModule,RecordShareModule,WorkspaceCacheModule,],providers: [PendingWakeUpService,PendingWakeUpOwnerHandlerRegistryService,+ PendingWakeUpEventRecordService,PendingWakeUpResolverService,ResumePendingWakeUpJob,PendingWakeUpDatabaseEventListener,@@ -32,6 +36,7 @@ import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cacheexports: [PendingWakeUpService,PendingWakeUpOwnerHandlerRegistryService,+ PendingWakeUpEventRecordService,PendingWakeUpSweepCronCommand,],})diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/__tests__/pending-wake-up-event-record.service.spec.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/__tests__/pending-wake-up-event-record.service.spec.tsnew file mode 100644index 00000000000..9fec20e2ada--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/__tests__/pending-wake-up-event-record.service.spec.ts@@ -0,0 +1,82 @@+import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { PendingWakeUpEventRecordService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service';+import { type FindRecordsService } from 'src/engine/core-modules/record-crud/services/find-records.service';++const EVENT = {+ eventName: 'company.updated',+ recordId: 'company-id',+ record: { id: 'company-id', secret: 'hidden' },+ updatedFields: ['name', 'secret'],+};++const decide = ({+ recordRead,+ recordReadAttempt = 0,+ onRecordReadGivenUp = jest.fn(),+}: {+ recordRead: {+ success: boolean;+ result?: { records: object[] };+ error?: string;+ };+ recordReadAttempt?: number;+ onRecordReadGivenUp?: jest.Mock;+}) =>+ new PendingWakeUpEventRecordService({+ execute: jest.fn().mockResolvedValue(recordRead),+ } as unknown as FindRecordsService).decideOnEventRecord({+ event: EVENT,+ authContext: {} as WorkspaceAuthContext,+ rolePermissionConfig: { intersectionOf: ['role-id'] },+ recordReadAttempt,+ context: 'context',+ onRecordReadGivenUp,+ });++describe('PendingWakeUpEventRecordService.decideOnEventRecord', () => {+ it('wakes the owner up with only what it can read of the record', async () => {+ expect(+ await decide({+ recordRead: {+ success: true,+ result: { records: [{ id: 'company-id', name: 'Acme' }] },+ },+ }),+ ).toEqual({+ type: 'RESOLVE',+ event: {+ ...EVENT,+ record: { id: 'company-id', name: 'Acme' },+ updatedFields: ['name'],+ },+ context: 'context',+ });+ });++ it('keeps waiting on a record the owner cannot read', async () => {+ expect(+ await decide({ recordRead: { success: true, result: { records: [] } } }),+ ).toEqual({+ type: 'IGNORE',+ });+ });++ it('tries a failed read again later', async () => {+ expect(+ await decide({ recordRead: { success: false }, recordReadAttempt: 2 }),+ ).toMatchObject({ type: 'RETRY_LATER', recordReadAttempt: 3 });+ });++ it('keeps waiting once the read kept failing, and says so', async () => {+ const onRecordReadGivenUp = jest.fn();++ expect(+ await decide({+ recordRead: { success: false, error: 'boom' },+ recordReadAttempt: 8,+ onRecordReadGivenUp,+ }),+ ).toEqual({ type: 'IGNORE' });+ expect(onRecordReadGivenUp).toHaveBeenCalledWith('boom');+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service.tsnew file mode 100644index 00000000000..d43305fa47c--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service.ts@@ -0,0 +1,81 @@+import { Injectable } from '@nestjs/common';++import { isPlainObject } from 'twenty-shared/utils';++import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';+import { type PendingWakeUpBeforeClaimDecision } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';+import { computePendingWakeUpRetryDelayMs } from 'src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util';+import { restrictPendingWakeUpEventToReadableRecord } from 'src/engine/core-modules/pending-wake-up/utils/restrict-pending-wake-up-event-to-readable-record.util';+import { FindRecordsService } from 'src/engine/core-modules/record-crud/services/find-records.service';+import { type RolePermissionConfig } from 'src/engine/twenty-orm/types/role-permission-config.type';++const RECORD_READ_MAX_ATTEMPTS = 8;++@Injectable()+export class PendingWakeUpEventRecordService {+ constructor(private readonly findRecordsService: FindRecordsService) {}++ // The event's record is read with its owner's permissions: a record the owner cannot read must not+ // wake it up, so the wait goes on for another event. A failed read cannot tell a transient error from+ // an object the owner cannot read, so it is retried a few times+ async decideOnEventRecord<TResolveContext>({+ event,+ authContext,+ rolePermissionConfig,+ recordReadAttempt,+ context,+ onRecordReadGivenUp,+ }: {+ event: PendingWakeUpEvent;+ authContext: WorkspaceAuthContext;+ rolePermissionConfig: RolePermissionConfig;+ recordReadAttempt: number;+ context: TResolveContext;+ onRecordReadGivenUp: (error?: string) => void;+ }): Promise<PendingWakeUpBeforeClaimDecision<TResolveContext>> {+ const [objectName, action] = event.eventName.split('.');++ const { success, result, error } = await this.findRecordsService.execute({+ objectName,+ // a deleted record only reads when the filter asks for deleted records+ filter:+ action === 'deleted'+ ? { id: { eq: event.recordId }, deletedAt: { is: 'NOT_NULL' } }+ : { id: { eq: event.recordId } },+ limit: 1,+ authContext,+ rolePermissionConfig,+ shouldBuildEffectiveSelectFields: false,+ });++ if (!success) {+ if (recordReadAttempt < RECORD_READ_MAX_ATTEMPTS) {+ return {+ type: 'RETRY_LATER',+ recordReadAttempt: recordReadAttempt + 1,+ delayMs: computePendingWakeUpRetryDelayMs(recordReadAttempt),+ };+ }++ onRecordReadGivenUp(error);++ return { type: 'IGNORE' };+ }++ const record = result?.records[0];++ if (!isPlainObject(record)) {+ return { type: 'IGNORE' };+ }++ return {+ type: 'RESOLVE',+ event: restrictPendingWakeUpEventToReadableRecord({+ event,+ readableRecord: record,+ }),+ context,+ };+ }+}diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.tsindex cdf5b51821e..01cae6b750f 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.ts@@ -1 +1 @@-export type PendingWakeUpOwnerType = 'WORKFLOW_STEP';+export type PendingWakeUpOwnerType = 'WORKFLOW_STEP' | 'AGENT_RUN';diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/__tests__/restrict-wait-event-to-readable-record.util.spec.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/__tests__/restrict-pending-wake-up-event-to-readable-record.util.spec.tssimilarity index 78%rename from packages/twenty-server/src/modules/workflow/workflow-wait/utils/__tests__/restrict-wait-event-to-readable-record.util.spec.tsrename to packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/__tests__/restrict-pending-wake-up-event-to-readable-record.util.spec.tsindex 04bac807c76..1291624e75f 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/__tests__/restrict-wait-event-to-readable-record.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/__tests__/restrict-pending-wake-up-event-to-readable-record.util.spec.ts@@ -1,9 +1,9 @@-import { restrictWaitEventToReadableRecord } from 'src/modules/workflow/workflow-wait/utils/restrict-wait-event-to-readable-record.util';+import { restrictPendingWakeUpEventToReadableRecord } from 'src/engine/core-modules/pending-wake-up/utils/restrict-pending-wake-up-event-to-readable-record.util';-describe('restrictWaitEventToReadableRecord', () => {+describe('restrictPendingWakeUpEventToReadableRecord', () => {it('drops the fields the run cannot read from every part of the event', () => {expect(- restrictWaitEventToReadableRecord({+ restrictPendingWakeUpEventToReadableRecord({event: {eventName: 'company.updated',recordId: 'company-id',@@ -24,7 +24,7 @@ describe('restrictWaitEventToReadableRecord', () => {it('leaves an event without a previous snapshot without one', () => {expect(- restrictWaitEventToReadableRecord({+ restrictPendingWakeUpEventToReadableRecord({event: {eventName: 'company.created',recordId: 'company-id',diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util.tsnew file mode 100644index 00000000000..d3542d44e6f--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util.ts@@ -0,0 +1,5 @@+const RETRY_BASE_DELAY_MS = 2_000;+const RETRY_MAX_DELAY_MS = 60_000;++export const computePendingWakeUpRetryDelayMs = (attempt: number): number =>+ Math.min(RETRY_BASE_DELAY_MS * 2 ** attempt, RETRY_MAX_DELAY_MS);diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/restrict-wait-event-to-readable-record.util.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/restrict-pending-wake-up-event-to-readable-record.util.tssimilarity index 83%rename from packages/twenty-server/src/modules/workflow/workflow-wait/utils/restrict-wait-event-to-readable-record.util.tsrename to packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/restrict-pending-wake-up-event-to-readable-record.util.tsindex fe8b84642cc..8c5adc0a80c 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/restrict-wait-event-to-readable-record.util.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/restrict-pending-wake-up-event-to-readable-record.util.ts@@ -2,8 +2,8 @@ import { isDefined } from 'twenty-shared/utils';import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';-// The record read under the run's permissions only holds the fields the run can read-export const restrictWaitEventToReadableRecord = ({+// The record read under the owner's permissions only holds the fields the owner can read+export const restrictPendingWakeUpEventToReadableRecord = ({event,readableRecord,}: {diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.tsindex f37ed47926e..65f4f8490eb 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts@@ -6,17 +6,23 @@ import { CacheLockModule } from 'src/engine/core-modules/cache-lock/cache-lock.mimport { FileEntity } from 'src/engine/core-modules/file/entities/file.entity';import { FileUrlModule } from 'src/engine/core-modules/file/file-url/file-url.module';import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';+import { PendingWakeUpModule } from 'src/engine/core-modules/pending-wake-up/pending-wake-up.module';import { ToolProviderModule } from 'src/engine/core-modules/tool-provider/tool-provider.module';import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module';import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';+import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';+import { ContinueAgentRunJob } from 'src/engine/metadata-modules/ai/ai-agent-execution/jobs/continue-agent-run.job';import { AgentMessagePartResolver } from 'src/engine/metadata-modules/ai/ai-agent-execution/resolvers/agent-message-part.resolver';import { AgentMessageResolver } from 'src/engine/metadata-modules/ai/ai-agent-execution/resolvers/agent-message.resolver';import { AgentRunResolver } from 'src/engine/metadata-modules/ai/ai-agent-execution/resolvers/agent-run.resolver';import { AgentActorContextService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service';import { AgentAsyncExecutorService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service';import { AgentCallerConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service';+import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';import { AgentRunConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service';+import { AgentRunPendingWakeUpHandlerService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service';import { AgentRunService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { AgentRunnerService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service';import { RunAgentAttachmentService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/run-agent-attachment.service';import { AiAgentModule } from 'src/engine/metadata-modules/ai/ai-agent/ai-agent.module';@@ -44,12 +50,14 @@ import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspacCacheLockModule,FileUrlModule,MetricsModule,+ PendingWakeUpModule,UserWorkspaceModule,UserRoleModule,PermissionsModule,ToolProviderModule,TypeOrmModule.forFeature([AgentEntity,+ AgentRunSuspensionEntity,FileEntity,RoleTargetEntity,WorkspaceEntity,@@ -59,23 +67,31 @@ import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspacAgentAsyncExecutorService,AgentActorContextService,AgentCallerConversationService,+ AgentRunCallerHandlerRegistryService,AgentRunConversationService,+ AgentRunSuspensionService,AiGraphqlApiExceptionInterceptor,AgentMessagePartResolver,AgentMessageResolver,AgentRunResolver,AgentRunService,AgentRunnerService,+ // continues suspended runs once answered or woken up, on the server and the worker alike+ AgentRunPendingWakeUpHandlerService,+ ContinueAgentRunJob,RunAgentAttachmentService,provideWorkspaceScopedRepository(RoleTargetEntity),provideWorkspaceScopedRepository(AgentEntity),+ provideWorkspaceScopedRepository(AgentRunSuspensionEntity),provideWorkspaceScopedRepository(FileEntity),],exports: [AgentAsyncExecutorService,AgentActorContextService,AgentCallerConversationService,+ AgentRunCallerHandlerRegistryService,AgentRunConversationService,+ AgentRunSuspensionService,AgentRunnerService,],})diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant.tsnew file mode 100644index 00000000000..fa1c7ab9264--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant.ts@@ -0,0 +1 @@+export const CONTINUE_AGENT_RUN_JOB_NAME = 'ContinueAgentRunJob';diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.tsnew file mode 100644index 00000000000..b4ef7189089--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.ts@@ -0,0 +1,50 @@+import {+ Column,+ CreateDateColumn,+ Entity,+ PrimaryGeneratedColumn,+ Unique,+ UpdateDateColumn,+} from 'typeorm';++import { CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant';+import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';+import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';+import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';+import { WorkspaceRelatedEntity } from 'src/engine/workspace-manager/types/workspace-related-entity.type';++// A run paused on an answer or a wait, kept until the engine continues it and hands its+// outcome to the caller. A conversation holds one at a time, as it waits on one thing at a time+@Entity({ name: 'agentRunSuspension', schema: 'core' })+@WasIntroducedInUpgrade({+ upgradeCommandName: CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME,+})+@Unique('UQ_AGENT_RUN_SUSPENSION_THREAD_ID', ['threadId'])+export class AgentRunSuspensionEntity extends WorkspaceRelatedEntity {+ @PrimaryGeneratedColumn('uuid')+ id: string;++ @Column({ type: 'uuid' })+ threadId: string;++ @Column({ type: 'jsonb' })+ caller: AgentRunCaller;++ // null when the caller posted the awaited call itself: the answer is its outcome+ @Column({ type: 'jsonb', nullable: true })+ runSpec: AgentRunSpec | null;++ // summed across the run's segments, so the caller gets one total+ @Column({ type: 'jsonb', nullable: true })+ summary: AgentRunSummary | null;++ @Column({ type: 'integer', default: 0 })+ resumeCount: number;++ @CreateDateColumn({ type: 'timestamptz' })+ createdAt: Date;++ @UpdateDateColumn({ type: 'timestamptz' })+ updatedAt: Date;+}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/jobs/continue-agent-run.job.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/jobs/continue-agent-run.job.tsnew file mode 100644index 00000000000..7c07f6992cb--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/jobs/continue-agent-run.job.ts@@ -0,0 +1,26 @@+import { Scope } from '@nestjs/common';++import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';+import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { CONTINUE_AGENT_RUN_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant';+import { AgentRunnerService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service';+import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';+import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';+import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';++@Processor({ queueName: MessageQueue.aiQueue, scope: Scope.REQUEST })+export class ContinueAgentRunJob {+ constructor(+ private readonly agentRunnerService: AgentRunnerService,+ private readonly workspaceOrmManager: WorkspaceOrmManager,+ ) {}++ @Process(CONTINUE_AGENT_RUN_JOB_NAME)+ async handle(jobData: ContinueAgentRunJobData): Promise<void> {+ await this.workspaceOrmManager.executeInWorkspaceContext(+ () => this.agentRunnerService.continue(jobData),+ buildSystemAuthContext(jobData.workspaceId),+ );+ }+}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-prompt.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-prompt.constant.tsnew file mode 100644index 00000000000..7235f66fb89--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-prompt.constant.ts@@ -0,0 +1 @@+export const AGENT_WAIT_PROMPT = `When the task depends on something that has not happened yet, you can pause instead of finishing early: call wait_for_event to resume when a record is created, updated or deleted, or wait_for_duration to resume after some time. Call a wait tool alone and last; you continue in this conversation with its outcome. Only wait when the task requires it.`;diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-tool-names.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-tool-names.constant.tsnew file mode 100644index 00000000000..465dac87ca1--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-tool-names.constant.ts@@ -0,0 +1,7 @@+import { WAIT_FOR_DURATION_TOOL_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-duration-tool-name.constant';+import { WAIT_FOR_EVENT_TOOL_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-event-tool-name.constant';++export const AGENT_WAIT_TOOL_NAMES: string[] = [+ WAIT_FOR_EVENT_TOOL_NAME,+ WAIT_FOR_DURATION_TOOL_NAME,+];diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-duration-tool-name.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-duration-tool-name.constant.tsnew file mode 100644index 00000000000..5766c5968cb--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-duration-tool-name.constant.ts@@ -0,0 +1 @@+export const WAIT_FOR_DURATION_TOOL_NAME = 'wait_for_duration';diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-event-tool-name.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-event-tool-name.constant.tsnew file mode 100644index 00000000000..18a1038dffe--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-event-tool-name.constant.ts@@ -0,0 +1 @@+export const WAIT_FOR_EVENT_TOOL_NAME = 'wait_for_event';diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/__tests__/create-workflow-agent-wait-tools.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/create-agent-wait-tools.util.spec.tssimilarity index 74%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/__tests__/create-workflow-agent-wait-tools.util.spec.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/create-agent-wait-tools.util.spec.tsindex 7fb2bfbe90c..7bde8f110f6 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/__tests__/create-workflow-agent-wait-tools.util.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/create-agent-wait-tools.util.spec.ts@@ -1,8 +1,8 @@-import { createWorkflowAgentWaitTools } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-workflow-agent-wait-tools.util';+import { createAgentWaitTools } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util';-describe('createWorkflowAgentWaitTools', () => {+describe('createAgentWaitTools', () => {it('refuses a second wait of the same execution so only one is pending', async () => {- const tools = createWorkflowAgentWaitTools();+ const tools = createAgentWaitTools();await expect(tools.wait_for_event.execute({@@ -18,13 +18,13 @@ describe('createWorkflowAgentWaitTools', () => {it('only keeps a field filter on updates, where events report changed fields', async () => {const createdEventWait =- await createWorkflowAgentWaitTools().wait_for_event.execute({+ await createAgentWaitTools().wait_for_event.execute({objectName: 'company',action: 'created',updatedFields: ['name'],});const updatedEventWait =- await createWorkflowAgentWaitTools().wait_for_event.execute({+ await createAgentWaitTools().wait_for_event.execute({objectName: 'company',action: 'updated',updatedFields: ['name'],@@ -46,8 +46,8 @@ describe('createWorkflowAgentWaitTools', () => {});it('gives each execution its own wait', async () => {- const firstExecutionTools = createWorkflowAgentWaitTools();- const secondExecutionTools = createWorkflowAgentWaitTools();+ const firstExecutionTools = createAgentWaitTools();+ const secondExecutionTools = createAgentWaitTools();await firstExecutionTools.wait_for_duration.execute({durationInMinutes: 5,diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/find-agent-step-wait.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/find-agent-run-wait.util.spec.tssimilarity index 65%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/find-agent-step-wait.util.spec.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/find-agent-run-wait.util.spec.tsindex 1c6fe497b87..96e379acfff 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/find-agent-step-wait.util.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/__tests__/find-agent-run-wait.util.spec.ts@@ -1,23 +1,25 @@-import { findAgentStepWait } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/find-agent-step-wait.util';+import { findAgentRunWait } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util';const WAIT = { type: 'EVENT', eventName: 'company.updated' };-describe('findAgentStepWait', () => {- it('finds the wait a run paused on', () => {+describe('findAgentRunWait', () => {+ it('finds the wait a run paused on, with the call that waits', () => {expect(- findAgentStepWait([+ findAgentRunWait([{+ toolCallId: 'wait-1',toolName: 'wait_for_event',output: { success: true, result: { status: 'pending', wait: WAIT } },},]),- ).toEqual(WAIT);+ ).toEqual({ toolCallId: 'wait-1', condition: WAIT });});it('ignores pending calls of tools that are not waits', () => {expect(- findAgentStepWait([+ findAgentRunWait([{+ toolCallId: 'ask-1',toolName: 'ask_question',output: { success: true, result: { status: 'pending', wait: WAIT } },},@@ -27,8 +29,9 @@ describe('findAgentStepWait', () => {it('ignores a wait call that no longer waits', () => {expect(- findAgentStepWait([+ findAgentRunWait([{+ toolCallId: 'wait-1',toolName: 'wait_for_event',output: {success: true,@@ -41,8 +44,9 @@ describe('findAgentStepWait', () => {it('ignores a wait call whose wait cannot be read', () => {expect(- findAgentStepWait([+ findAgentRunWait([{+ toolCallId: 'wait-1',toolName: 'wait_for_duration',output: { success: true, result: { status: 'pending' } },},diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-pending-output.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-pending-output.type.tsnew file mode 100644index 00000000000..bcb81ab85ca--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-pending-output.type.ts@@ -0,0 +1,7 @@+import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';++export type AgentWaitPendingOutput = {+ success: true;+ message: string;+ result: { status: 'pending'; wait: PendingWakeUpCondition };+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type.tsnew file mode 100644index 00000000000..62b750c70ba--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type.ts@@ -0,0 +1 @@+export type AgentWaitSlot = { isTaken: boolean };diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-second-wait-refusal-output.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-second-wait-refusal-output.util.tssimilarity index 100%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-second-wait-refusal-output.util.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-second-wait-refusal-output.util.tsdiff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-wait-outcome-tool-output.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.tssimilarity index 100%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-wait-outcome-tool-output.util.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.tsdiff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-pending-output.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-pending-output.util.tsnew file mode 100644index 00000000000..bb12a80c24d--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-pending-output.util.ts@@ -0,0 +1,15 @@+import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';++import { type AgentWaitPendingOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-pending-output.type';++export const buildWaitPendingOutput = ({+ message,+ wait,+}: {+ message: string;+ wait: PendingWakeUpCondition;+}): AgentWaitPendingOutput => ({+ success: true,+ message,+ result: { status: 'pending', wait },+});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util.tsnew file mode 100644index 00000000000..8122be577b7--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util.ts@@ -0,0 +1,17 @@+import { type ToolSet } from 'ai';++import { WAIT_FOR_DURATION_TOOL_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-duration-tool-name.constant';+import { WAIT_FOR_EVENT_TOOL_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/wait-for-event-tool-name.constant';+import { createWaitForDurationTool } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-duration.tool';+import { createWaitForEventTool } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-event.tool';+import { type AgentWaitSlot } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type';++// a run continues on one wait, so a second wait call of the same execution is refused instead of left pending+export const createAgentWaitTools = () => {+ const waitSlot: AgentWaitSlot = { isTaken: false };++ return {+ [WAIT_FOR_EVENT_TOOL_NAME]: createWaitForEventTool(waitSlot),+ [WAIT_FOR_DURATION_TOOL_NAME]: createWaitForDurationTool(waitSlot),+ } satisfies ToolSet;+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-duration.tool.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-duration.tool.tssimilarity index 55%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-duration.tool.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-duration.tool.tsindex 51a50d51346..cbe216e9c76 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-duration.tool.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-duration.tool.ts@@ -1,8 +1,8 @@import { z } from 'zod';-import { buildSecondWaitRefusalOutput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-second-wait-refusal-output.util';-import { buildWaitPendingOutput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-wait-pending-output.util';-import { type WorkflowAgentWaitSlot } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type';+import { buildSecondWaitRefusalOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-second-wait-refusal-output.util';+import { buildWaitPendingOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-pending-output.util';+import { type AgentWaitSlot } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type';const MAX_DURATION_IN_MINUTES = 60 * 24 * 365;@@ -15,9 +15,9 @@ const waitForDurationInputSchema = z.object({.describe('How long to wait, in minutes (e.g. 1440 for one day).'),});-export const createWaitForDurationTool = (waitSlot: WorkflowAgentWaitSlot) => ({+export const createWaitForDurationTool = (waitSlot: AgentWaitSlot) => ({description:- 'Pause the workflow for a while, for example before following up. You continue once the time has passed.',+ 'Pause for a while, for example before following up. You continue once the time has passed.',inputSchema: waitForDurationInputSchema,execute: async ({durationInMinutes,diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-event.tool.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-event.tool.tssimilarity index 73%rename from packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-event.tool.tsrename to packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-event.tool.tsindex 5d3d9011819..80ab84d32eb 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-event.tool.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-wait-for-event.tool.ts@@ -2,9 +2,9 @@ import { isNonEmptyString } from '@sniptt/guards';import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';import { z } from 'zod';-import { buildSecondWaitRefusalOutput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-second-wait-refusal-output.util';-import { buildWaitPendingOutput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-wait-pending-output.util';-import { type WorkflowAgentWaitSlot } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type';+import { buildSecondWaitRefusalOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-second-wait-refusal-output.util';+import { buildWaitPendingOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-pending-output.util';+import { type AgentWaitSlot } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/agent-wait-slot.type';const MAX_TIMEOUT_IN_MINUTES = 60 * 24 * 365;@@ -37,9 +37,9 @@ const waitForEventInputSchema = z.object({),});-export const createWaitForEventTool = (waitSlot: WorkflowAgentWaitSlot) => ({+export const createWaitForEventTool = (waitSlot: AgentWaitSlot) => ({description:- 'Pause the workflow until a record is created, updated or deleted. You then continue with the ' ++ 'Pause until a record is created, updated or deleted. You then continue with the ' +'id of the record that changed, which you can read with your tools, or learn that the wait timed out.',inputSchema: waitForEventInputSchema,execute: async ({@@ -58,7 +58,7 @@ export const createWaitForEventTool = (waitSlot: WorkflowAgentWaitSlot) => ({const eventName = `${objectName}.${action}`;return buildWaitPendingOutput({- message: `Waiting for ${eventName}; the workflow resumes when it happens.`,+ message: `Waiting for ${eventName}; you continue when it happens.`,wait: {type: 'EVENT',eventName,diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util.tsnew file mode 100644index 00000000000..2628bacc41c--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util.ts@@ -0,0 +1,32 @@+import { pendingWakeUpConditionSchema } from 'twenty-shared/pending-wake-up';+import { isPlainObject } from 'twenty-shared/utils';++import { AGENT_WAIT_TOOL_NAMES } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-tool-names.constant';+import { type AgentRunWait } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-wait.type';+import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';++// The wait a run paused on, when it paused on a wait tool call+export const findAgentRunWait = (+ toolResults: { toolCallId: string; toolName: string; output: unknown }[],+): AgentRunWait | undefined => {+ for (const { toolCallId, toolName, output } of toolResults) {+ if (+ !AGENT_WAIT_TOOL_NAMES.includes(toolName) ||+ !isAwaitingPausingToolOutput(output) ||+ !isPlainObject(output) ||+ !isPlainObject(output.result)+ ) {+ continue;+ }++ const parsedWait = pendingWakeUpConditionSchema.safeParse(+ output.result.wait,+ );++ if (parsedWait.success) {+ return { toolCallId, condition: parsedWait.data };+ }+ }++ return undefined;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.tsindex defce7e2ac7..4d81e4c725c 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts@@ -1,7 +1,4 @@-import { IsNull } from 'typeorm';-import { AgentCallerConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service';-import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';import {AiException,AiExceptionCode,@@ -21,52 +18,7 @@ const OPEN_ARGS = {fallbackThreadKey: 'run-id:step-id:run-id:step-id',};-const CALLER = {- type: 'WORKFLOW_STEP' as const,- ref: { workflowRunId: 'run-id', stepId: 'step-id' },-};--const QUESTION = {- header: 'Plan',- question: 'Which plan?',- options: [{ label: 'Pro' }, { label: 'Team' }],-};-const buildService = ({ isCreated = true } = {}) => {- const threadRepository = {- find: jest- .fn()- .mockResolvedValue([- { id: 'thread-id', pendingQuestionMessageId: 'message-id' },- ]),- update: jest.fn().mockResolvedValue({ affected: 1 }),- };- const messageRepository = { find: jest.fn().mockResolvedValue([]) };- const messagePartRepository = {- find: jest.fn().mockResolvedValue([- {- id: 'part-id',- messageId: 'message-id',- toolName: 'ask_question',- toolInput: QUESTION,- toolOutput: {- result: { question: QUESTION, status: 'pending' },- workflowStep: { workflowRunId: 'run-id', stepId: 'step-id' },- },- },- ]),- update: jest.fn(),- writePart: jest.fn(),- query: jest.fn(),- };-- messagePartRepository.query.mockImplementation(async (_workspaceId, run) =>- run({- table: (name: string) => name,- manager: { query: messagePartRepository.writePart },- }),- );-const agentInboxService = {openThread: jest.fn().mockImplementation(({ workspaceMemberId }) =>Promise.resolve({@@ -75,31 +27,16 @@ const buildService = ({ isCreated = true } = {}) => {}),),};- const threadRecordEventService = {- emitPendingQuestionCleared: jest.fn().mockResolvedValue(undefined),- };- const turnRecorderService = {- endWaitingTurn: jest.fn().mockResolvedValue(undefined),+ const agentRunSuspensionService = {+ findOne: jest.fn().mockResolvedValue(null),};const service = new AgentCallerConversationService(- threadRepository as never,- messageRepository as never,- messagePartRepository as never,agentInboxService as never,- threadRecordEventService as never,- turnRecorderService as never,+ agentRunSuspensionService as never,);- return {- service,- threadRepository,- messageRepository,- messagePartRepository,- agentInboxService,- threadRecordEventService,- turnRecorderService,- };+ return { service, agentInboxService, agentRunSuspensionService };};describe('AgentCallerConversationService', () => {@@ -231,211 +168,43 @@ describe('AgentCallerConversationService', () => {).resolves.toMatchObject({ threadId: 'thread-for-recipient-id' });});- it('reports a conversation the recipient also deleted under the fallback key', async () => {- const { service, agentInboxService } = buildService();+ it('starts its own conversation while the one its key names holds a suspended run', async () => {+ const { service, agentInboxService, agentRunSuspensionService } =+ buildService();- agentInboxService.openThread.mockResolvedValue({- thread: { id: 'deleted-thread-id', deletedAt: '2026-01-01' },+ agentInboxService.openThread.mockResolvedValueOnce({+ thread: { id: 'suspended-thread-id' },isCreated: false,});+ agentRunSuspensionService.findOne.mockImplementation(+ async ({ where }: { where: { threadId: string } }) =>+ where.threadId === 'suspended-thread-id'+ ? { id: 'suspension-id' }+ : null,+ );await expect(service.openConversation({...OPEN_ARGS,recipientWorkspaceMemberId: 'recipient-id',}),- ).resolves.toEqual({ status: 'DELETED' });- });- });-- describe('recordWaitOutcome', () => {- const waitPart = (id: string, workflowStep: Record<string, string>) => ({- id,- toolName: 'wait_for_duration',- toolOutput: { result: { status: 'pending' }, workflowStep },- });-- const recordWaitOutcome = (service: AgentCallerConversationService) =>- service.recordWaitOutcome({- workspaceId: 'workspace-id',- threadId: 'thread-id',- caller: CALLER,- toolNames: ['wait_for_duration'],- toolOutput: { success: true },- });-- it("settles the caller's own wait, not another caller's waiting in the same conversation", async () => {- const { service, messageRepository, messagePartRepository } =- buildService();-- messageRepository.find.mockResolvedValue([- {- parts: [- waitPart('other-run-part', {- workflowRunId: 'other-run-id',- stepId: 'step-id',- }),- ],- },- {- parts: [- waitPart('other-step-part', {- workflowRunId: 'run-id',- stepId: 'other-step-id',- }),- ],- },- {- parts: [- waitPart('own-part', {- workflowRunId: 'run-id',- stepId: 'step-id',- }),- ],- },- ]);-- await recordWaitOutcome(service);-- expect(messagePartRepository.update).toHaveBeenCalledWith(- 'workspace-id',- { id: 'own-part' },- { toolOutput: { success: true } },- );- });-- it('refuses to settle a wait the caller never posted', async () => {- const { service, messageRepository } = buildService();-- messageRepository.find.mockResolvedValue([- {- parts: [- waitPart('other-run-part', {- workflowRunId: 'other-run-id',- stepId: 'step-id',- }),- ],- },- ]);-- await expect(recordWaitOutcome(service)).rejects.toMatchObject({- code: AiExceptionCode.TOOL_CALL_NOT_FOUND,- });- });- });-- describe('cancelAwaitingConversations', () => {- const cancel = (service: AgentCallerConversationService) =>- service.cancelAwaitingConversations({- workspaceId: 'workspace-id',- threadIds: ['thread-id'],- caller: { type: 'WORKFLOW_STEP', ref: { workflowRunId: 'run-id' } },- });-- it('stops its conversations waiting and closes their calls as skipped', async () => {- const {- service,- threadRepository,- messagePartRepository,- turnRecorderService,- } = buildService();-- await cancel(service);-- expect(threadRepository.update).toHaveBeenCalledWith(- 'workspace-id',- {- id: 'thread-id',- pendingQuestionMessageId: 'message-id',- activeStreamId: IsNull(),- },- { pendingQuestionMessageId: null },- );- expect(turnRecorderService.endWaitingTurn).toHaveBeenCalledWith({- workspaceId: 'workspace-id',- messageId: 'message-id',- status: AgentTurnStatus.CANCELLED,- });- const [[closeQuery, [partId, closedToolOutput, expectedStatus]]] =- messagePartRepository.writePart.mock.calls;-- expect(closeQuery).toContain(`"toolOutput"->'result'->>'status' = $3`);- expect({- partId,- result: JSON.parse(closedToolOutput).result,- expectedStatus,- }).toEqual({- partId: 'part-id',- result: { question: QUESTION, status: 'skipped' },- expectedStatus: 'pending',- });- });-- it('tells open chat lists the conversation no longer waits on an answer', async () => {- const { service, threadRecordEventService } = buildService();-- await cancel(service);-- expect(- threadRecordEventService.emitPendingQuestionCleared,- ).toHaveBeenCalledWith({- workspaceId: 'workspace-id',- threadId: 'thread-id',- messageId: 'message-id',- });- });-- it('leaves open a call another caller posted in a conversation they share', async () => {- const { service, threadRepository, messagePartRepository } =- buildService();-- messagePartRepository.find.mockResolvedValue([- {- id: 'part-id',- messageId: 'message-id',- toolName: 'ask_question',- toolInput: QUESTION,- toolOutput: {- result: { question: QUESTION, status: 'pending' },- workflowStep: { workflowRunId: 'other-run-id', stepId: 'step-id' },- },- },- ]);-- await cancel(service);-- expect(threadRepository.update).not.toHaveBeenCalled();- expect(messagePartRepository.writePart).not.toHaveBeenCalled();- });-- it('leaves a conversation to the answer holding its claim', async () => {- const {- service,- threadRepository,- messagePartRepository,- threadRecordEventService,- } = buildService();-- threadRepository.update.mockResolvedValue({ affected: 0 });-- await cancel(service);-- expect(messagePartRepository.writePart).not.toHaveBeenCalled();- expect(- threadRecordEventService.emitPendingQuestionCleared,- ).not.toHaveBeenCalled();+ ).resolves.toMatchObject({ threadId: 'thread-for-recipient-id' });});- it('reads nothing without conversations', async () => {- const { service, threadRepository } = buildService();+ it('reports a conversation the recipient also deleted under the fallback key', async () => {+ const { service, agentInboxService } = buildService();- await service.cancelAwaitingConversations({- workspaceId: 'workspace-id',- threadIds: [],- caller: { type: 'WORKFLOW_STEP', ref: { workflowRunId: 'run-id' } },+ agentInboxService.openThread.mockResolvedValue({+ thread: { id: 'deleted-thread-id', deletedAt: '2026-01-01' },+ isCreated: false,});- expect(threadRepository.find).not.toHaveBeenCalled();+ await expect(+ service.openConversation({+ ...OPEN_ARGS,+ recipientWorkspaceMemberId: 'recipient-id',+ }),+ ).resolves.toEqual({ status: 'DELETED' });});});});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-conversation.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-conversation.service.spec.tsindex 0cf34f91d84..e62c5852e4a 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-conversation.service.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-conversation.service.spec.ts@@ -171,7 +171,7 @@ describe('AgentRunConversationService', () => {});});- it('names the caller on the calls it leaves pending', async () => {+ it('marks the calls it leaves pending as awaited by a caller', async () => {const { service, conversationWriterService, threadService } =buildService();@@ -180,16 +180,11 @@ describe('AgentRunConversationService', () => {title: 'Draft the quote',agentId: null,execution,- caller: {- type: 'WORKFLOW_STEP',- ref: { workflowRunId: 'run-id', stepId: 'step-id' },- },+ isAwaitedByCaller: true,});expect(conversationWriterService.insertExecutionReply).toHaveBeenCalledWith(- expect.objectContaining({- workflowStep: { workflowRunId: 'run-id', stepId: 'step-id' },- }),+ expect.objectContaining({ isAwaitedByCaller: true }),);expect(threadService.recordThreadActivity).not.toHaveBeenCalled();});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.tsindex b780f0b02dd..773d40b3cee 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts@@ -212,7 +212,10 @@ describe('AgentRunService', () => {agentRunnerService.run.mockResolvedValue({threadId: 'thread-id',- outcome: { status: 'NO_CREDITS' },+ outcome: {+ status: 'FAILED',+ error: 'Agent stopped: no more available credits.',+ },});await expect(diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.tsindex e40789a3282..dcf68a6c9f3 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts@@ -4,17 +4,14 @@ import { AgentRunnerService } from 'src/engine/metadata-modules/ai/ai-agent-execimport { type AgentExecutionResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-execution-result.type';import { type AgentRunnerRunInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type';-const CALLER = {- type: 'WORKFLOW_STEP' as const,- ref: { workflowRunId: 'run-id', stepId: 'step-id' },-};+type RunInput = Extract<AgentRunnerRunInput, { turn: unknown }>;const CREATED_BY = {source: 'WORKFLOW',name: 'New deals',workspaceMemberId: null,context: {},-} as Awaited<ReturnType<AgentRunnerRunInput['turn']['resolveCreatedBy']>>;+} as Awaited<ReturnType<RunInput['turn']['resolveCreatedBy']>>;const PRIOR_MESSAGES = [{ id: 'message-id', role: 'assistant', parts: [] }];@@ -48,10 +45,9 @@ const buildExecution = (...overrides,});-const RUN_INPUT: AgentRunnerRunInput = {+const RUN_INPUT: RunInput = {workspaceId: 'workspace-id',conversation: { threadId: 'thread-id', isCreated: false },- caller: CALLER,turn: {title: 'Draft the quote',senderUserWorkspaceId: 'user-workspace-id',@@ -87,6 +83,10 @@ const buildService = (execution = buildExecution()) => {agentAsyncExecutorService as never,agentRunConversationService as never,conversationReaderService as never,+ {} as never,+ {} as never,+ {} as never,+ {} as never,);return {@@ -97,22 +97,6 @@ const buildService = (execution = buildExecution()) => {};};-const pausedOnWait = buildExecution({- isPaused: true,- steps: [- { content: [], toolResults: [{ toolName: 'search', output: {} }] },- {- content: [],- toolResults: [- {- toolName: 'wait_for_event',- output: { result: { status: 'pending' } },- },- ],- },- ] as unknown as AgentExecutionResult['steps'],-});-describe('AgentRunnerService', () => {it('continues a conversation under its lock and records the turn', async () => {const {@@ -162,7 +146,7 @@ describe('AgentRunnerService', () => {expect.objectContaining({turnId: 'turn-id',title: 'Draft the quote',- caller: CALLER,+ isAwaitedByCaller: false,}),);});@@ -239,56 +223,16 @@ describe('AgentRunnerService', () => {expect(agentRunConversationService.closeTurn).not.toHaveBeenCalled();});- it('reports a run that ran out of credits', async () => {+ it('fails a run that ran out of credits', async () => {const { service } = buildService(buildExecution({ hasNoMoreAvailableCredits: true }),);- await expect(service.run(RUN_INPUT)).resolves.toMatchObject({- outcome: { status: 'NO_CREDITS' },- });- });-- it('reports a run awaiting an answer once its question is recorded', async () => {- const { service, agentRunConversationService } = buildService(- buildExecution({ isPaused: true }),- );-- agentRunConversationService.closeTurn.mockResolvedValue({- isAwaitingAnswer: true,- });-- await expect(service.run(RUN_INPUT)).resolves.toMatchObject({- outcome: { status: 'AWAITING_ANSWER' },- });- });-- it('hands back the calls of the last step a run paused on', async () => {- const { service } = buildService(pausedOnWait);-await expect(service.run(RUN_INPUT)).resolves.toMatchObject({outcome: {- status: 'PAUSED',- isResumable: true,- pausedToolResults: [- {- toolName: 'wait_for_event',- output: { result: { status: 'pending' } },- },- ],+ status: 'FAILED',+ error: 'Agent stopped: no more available credits.',},});});-- it('cannot resume a pause it failed to record', async () => {- const { service, agentRunConversationService } = buildService(pausedOnWait);-- agentRunConversationService.closeTurn.mockRejectedValue(- new Error('db down'),- );-- await expect(service.run(RUN_INPUT)).resolves.toMatchObject({- outcome: { status: 'PAUSED', isResumable: false },- });- });});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.tsindex 2a789aa5f4e..5e5a56e7f6b 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts@@ -1,48 +1,23 @@import { Injectable } from '@nestjs/common';import { isDefined } from 'twenty-shared/utils';-import { In, IsNull, Not } from 'typeorm';-import { closeOpenToolParts } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/close-open-tool-parts.util';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';-import { type AgentRunCallerFilter } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-filter.type';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { type AgentRunnerOpenedConversation } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-opened-conversation.type';-import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';-import { AgentChatThreadRecordEventService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-record-event.service';import { AgentInboxService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service';import { type AgentInboxSender } from 'src/engine/metadata-modules/ai/ai-chat/types/agent-inbox-sender.type';-import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';-import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';-import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';-import { AgentTurnRecorderService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-turn-recorder.service';-import { type AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';-import { type AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';-import { type AgentMessageWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message.workspace-entity';-import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';import {AiException,AiExceptionCode,} from 'src/engine/metadata-modules/ai/ai.exception';-const RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL = 50;--// The conversation of a run a caller drives, such as a workflow step: the calls-// the run pauses on name the caller, so an answer or an outcome can find its-// way back to it, and the caller can close them when it stops waiting.-// Kept apart from the runner so callers that only settle or cancel waits avoid the-// agent execution and tool dependencies.+// Opens the conversation of a run a caller drives, such as a workflow step. Kept apart from the+// runner so callers avoid the agent execution and tool dependencies@Injectable()export class AgentCallerConversationService {constructor(- @InjectAgentHistoryRepository('agentChatThread')- private readonly threadRepository: AgentHistoryRepository<AgentChatThreadWorkspaceEntity>,- @InjectAgentHistoryRepository('agentMessage')- private readonly messageRepository: AgentHistoryRepository<AgentMessageWorkspaceEntity>,- @InjectAgentHistoryRepository('agentMessagePart')- private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,private readonly agentInboxService: AgentInboxService,- private readonly threadRecordEventService: AgentChatThreadRecordEventService,- private readonly turnRecorderService: AgentTurnRecorderService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,) {}// A conversation the run opens is filed under done, and only comes back to@@ -88,10 +63,16 @@ export class AgentCallerConversationService {const keyedConversation = await openThreadUnderKey(threadKey);// a conversation the recipient deleted is not written to again, and one already waiting on an answer- // has no room for another question, so this run starts its own+ // or holding a suspended run has no room for another, so this run starts its ownconst isKeyedConversationUnavailable =isDefined(keyedConversation.thread.deletedAt) ||- isDefined(keyedConversation.thread.pendingQuestionMessageId);+ isDefined(keyedConversation.thread.pendingQuestionMessageId) ||+ isDefined(+ await this.agentRunSuspensionService.findOne({+ workspaceId,+ where: { threadId: keyedConversation.thread.id },+ }),+ );const { thread, isCreated } = isKeyedConversationUnavailable? await openThreadUnderKey(fallbackThreadKey): keyedConversation;@@ -103,159 +84,6 @@ export class AgentCallerConversationService {return { status: 'OPENED', threadId: thread.id, isCreated };}- // The paused call stays pending in the conversation until the caller resolves it, then carries the outcome- async recordWaitOutcome({- workspaceId,- threadId,- caller,- toolNames,- toolOutput,- }: {- workspaceId: string;- threadId: string;- caller: AgentRunCaller;- toolNames: string[];- toolOutput: Record<string, unknown>;- }): Promise<void> {- // messages can follow the call while it waits, so the latest one may not carry it- const recentMessages = await this.messageRepository.find(workspaceId, {- where: { threadId },- order: { createdAt: 'DESC' },- take: RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL,- relations: ['parts'],- });-- const pendingPart = recentMessages- .flatMap((message) => message.parts ?? [])- .find(- (part) =>- isDefined(part.toolName) &&- toolNames.includes(part.toolName) &&- isAwaitingPausingToolOutput(part.toolOutput) &&- isToolOutputAwaitedByCaller({ toolOutput: part.toolOutput, caller }),- );-- if (!isDefined(pendingPart)) {- throw new AiException(- 'The waiting call could not be found in the conversation',- AiExceptionCode.TOOL_CALL_NOT_FOUND,- );- }-- await this.messagePartRepository.update(- workspaceId,- { id: pendingPart.id },- { toolOutput },- );- }-- // A caller that stops waiting closes the calls its conversations wait on, so- // they no longer look waiting- async cancelAwaitingConversations({- workspaceId,- threadIds,- caller,- }: {- workspaceId: string;- threadIds: string[];- caller: AgentRunCallerFilter;- }): Promise<void> {- if (threadIds.length === 0) {- return;- }-- // An answer holding a conversation's claim closes its calls itself once it finds its caller gone- const awaitingThreads = await this.threadRepository.find(workspaceId, {- where: {- id: In(threadIds),- pendingQuestionMessageId: Not(IsNull()),- activeStreamId: IsNull(),- },- select: ['id', 'pendingQuestionMessageId'],- });-- const threadsAwaitingCaller = await this.filterThreadsAwaitingCaller({- threads: awaitingThreads,- caller,- workspaceId,- });-- for (const { id, pendingQuestionMessageId } of threadsAwaitingCaller) {- if (!isDefined(pendingQuestionMessageId)) {- continue;- }-- const { affected } = await this.threadRepository.update(- workspaceId,- { id, pendingQuestionMessageId, activeStreamId: IsNull() },- { pendingQuestionMessageId: null },- );-- if (affected === 0) {- continue;- }-- await this.threadRecordEventService.emitPendingQuestionCleared({- workspaceId,- threadId: id,- messageId: pendingQuestionMessageId,- });-- // the question is already cleared, so its calls must close before anything else can fail- await closeOpenToolParts({- messagePartRepository: this.messagePartRepository,- messageId: pendingQuestionMessageId,- workspaceId,- });-- await this.turnRecorderService.endWaitingTurn({- workspaceId,- messageId: pendingQuestionMessageId,- status: AgentTurnStatus.CANCELLED,- });- }- }-- // the member may have moved on to another question in their conversation, which the caller must not close- private async filterThreadsAwaitingCaller<- TThread extends { pendingQuestionMessageId: string | null },- >({- threads,- caller,- workspaceId,- }: {- threads: TThread[];- caller: AgentRunCallerFilter;- workspaceId: string;- }): Promise<TThread[]> {- const pendingMessageIds = threads- .map((thread) => thread.pendingQuestionMessageId)- .filter(isDefined);-- if (pendingMessageIds.length === 0) {- return [];- }-- const pendingParts = await this.messagePartRepository.find(workspaceId, {- where: { messageId: In(pendingMessageIds) },- select: ['messageId', 'toolOutput'],- });-- const messageIdsAwaitingCaller = new Set(- pendingParts- .filter((part) =>- isToolOutputAwaitedByCaller({ toolOutput: part.toolOutput, caller }),- )- .map((part) => part.messageId),- );-- return threads.filter(- (thread) =>- isDefined(thread.pendingQuestionMessageId) &&- messageIdsAwaitingCaller.has(thread.pendingQuestionMessageId),- );- }-// a member who cannot have the conversation, such as one who cannot use AI,// leaves a conversation no inbox receivesprivate async openThreadWithFallbackRecipient({diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.tsnew file mode 100644index 00000000000..9bc95c526fe--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.ts@@ -0,0 +1,36 @@+import { Injectable } from '@nestjs/common';++import { isDefined } from 'twenty-shared/utils';++import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';+import { type AgentRunCallerHandler } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type';+import {+ AiException,+ AiExceptionCode,+} from 'src/engine/metadata-modules/ai/ai.exception';++// callers register here so the engine hands them their runs' outcomes without importing them+@Injectable()+export class AgentRunCallerHandlerRegistryService {+ private readonly handlers = new Map<+ AgentRunCaller['type'],+ AgentRunCallerHandler+ >();++ register(handler: AgentRunCallerHandler): void {+ this.handlers.set(handler.callerType, handler);+ }++ getHandlerOrThrow(callerType: AgentRunCaller['type']): AgentRunCallerHandler {+ const handler = this.handlers.get(callerType);++ if (!isDefined(handler)) {+ throw new AiException(+ `No handler is registered for ${callerType} agent runs`,+ AiExceptionCode.AGENT_EXECUTION_FAILED,+ );+ }++ return handler;+ }+}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service.tsindex 0388945fda3..e4e91a47b7f 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service.ts@@ -10,8 +10,6 @@ import { type ActorMetadata } from 'twenty-shared/types';import { isDefined } from 'twenty-shared/utils';import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';-import { mapAgentRunCallerToToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/map-agent-run-caller-to-tool-call-workflow-step.util';import { AgentChatThreadService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service';import { findLastMessageText } from 'src/engine/metadata-modules/ai/ai-chat/utils/find-last-message-text.util';import { mapErrorToStreamError } from 'src/engine/metadata-modules/ai/ai-history/utils/map-error-to-stream-error.util';@@ -138,7 +136,7 @@ export class AgentRunConversationService {title,agentId,execution,- caller,+ isAwaitedByCaller,}: {workspaceId: string;threadId: string;@@ -146,7 +144,8 @@ export class AgentRunConversationService {title: string;agentId: string | null;execution: RecordableAgentExecution;- caller?: AgentRunCaller;+ // a caller such as a workflow step waits on the calls the run pauses on+ isAwaitedByCaller?: boolean;}): Promise<{ isAwaitingAnswer: boolean }> {const turn = { workspaceId, threadId, turnId, execution };@@ -158,9 +157,7 @@ export class AgentRunConversationService {turnId,agentId,execution,- workflowStep: isDefined(caller)- ? mapAgentRunCallerToToolCallWorkflowStep(caller)- : undefined,+ isAwaitedByCaller,}).catch(async (error: unknown) => {await this.turnRecorderService.finishExecutedTurn({diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.tsnew file mode 100644index 00000000000..6fe856b5538--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts@@ -0,0 +1,221 @@+import { Injectable, Logger, type OnModuleInit } from '@nestjs/common';++import { isDefined } from 'twenty-shared/utils';++import { type QueueJobOptions } from 'src/engine/core-modules/message-queue/drivers/interfaces/job-options.interface';+import { type PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';+import { PendingWakeUpEventRecordService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service';+import { PendingWakeUpOwnerHandlerRegistryService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service';+import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';+import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';+import {+ type PendingWakeUpBeforeClaimDecision,+ type PendingWakeUpOwnerHandler,+} from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';+import { computePendingWakeUpRetryDelayMs } from 'src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util';+import { type AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';+import { buildWaitOutcomeToolOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util';+import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';+import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';+import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';+import { type AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';+import { type AgentMessageWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message.workspace-entity';+import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';+import {+ AiException,+ AiExceptionCode,+} from 'src/engine/metadata-modules/ai/ai.exception';++const RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL = 50;++type AgentRunResolveContext = {+ suspension: AgentRunSuspensionEntity | null;+ // the caller stopped waiting, so the run is dropped instead of continued+ isCallerGone: boolean;+};++// An AGENT_RUN wake-up is owned by a suspended run and keyed by the wait call it paused on+@Injectable()+export class AgentRunPendingWakeUpHandlerService+ implements PendingWakeUpOwnerHandler<AgentRunResolveContext>, OnModuleInit+{+ readonly ownerType = 'AGENT_RUN';++ private readonly logger = new Logger(+ AgentRunPendingWakeUpHandlerService.name,+ );++ constructor(+ private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,+ private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,+ private readonly pendingWakeUpEventRecordService: PendingWakeUpEventRecordService,+ @InjectAgentHistoryRepository('agentMessage')+ private readonly messageRepository: AgentHistoryRepository<AgentMessageWorkspaceEntity>,+ @InjectAgentHistoryRepository('agentMessagePart')+ private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,+ ) {}++ onModuleInit(): void {+ this.pendingWakeUpOwnerHandlerRegistryService.register(this);+ }++ buildResumeJobOptions(): QueueJobOptions {+ return {};+ }++ async beforeClaim({+ wakeUp,+ event,+ attempt,+ recordReadAttempt,+ }: {+ wakeUp: PendingWakeUpEntity;+ event?: PendingWakeUpEvent;+ attempt: number;+ recordReadAttempt: number;+ }): Promise<PendingWakeUpBeforeClaimDecision<AgentRunResolveContext>> {+ const { workspaceId, ownerId: suspensionId } = wakeUp;++ const suspension = await this.agentRunSuspensionService.findOne({+ workspaceId,+ where: { id: suspensionId },+ });++ // claiming a wake-up whose run is gone only drops it+ if (!isDefined(suspension)) {+ return {+ type: 'RESOLVE',+ context: { suspension, isCallerGone: false },+ };+ }++ const waitingState =+ await this.agentRunSuspensionService.getCallerWaitingState({+ workspaceId,+ suspension,+ });++ // the wait is armed before its caller records that it waits, so the wake-up is held until it does+ if (waitingState === 'NOT_READY') {+ return {+ type: 'RETRY_LATER',+ attempt: attempt + 1,+ delayMs: computePendingWakeUpRetryDelayMs(attempt),+ };+ }++ if (waitingState === 'GONE' || !isDefined(event)) {+ return {+ type: 'RESOLVE',+ context: { suspension, isCallerGone: waitingState === 'GONE' },+ };+ }++ const { authContext, rolePermissionConfig } =+ await this.callerHandlerRegistry+ .getHandlerOrThrow(suspension.caller.type)+ .buildExecutionContext({ workspaceId, caller: suspension.caller });++ return this.pendingWakeUpEventRecordService.decideOnEventRecord({+ event,+ authContext,+ rolePermissionConfig,+ recordReadAttempt,+ context: { suspension, isCallerGone: false },+ onRecordReadGivenUp: (error) =>+ this.logger.warn(+ `Wait ${wakeUp.id} of agent run ${suspensionId} kept waiting after its ${event.eventName} record could not be read: ${error}`,+ ),+ });+ }++ async resolve({+ claimedWakeUp: { workspaceId, ownerKey: toolCallId },+ outcome,+ context: { suspension, isCallerGone },+ }: {+ claimedWakeUp: PendingWakeUpEntity;+ outcome: PendingWakeUpOutcome;+ context: AgentRunResolveContext;+ }): Promise<void> {+ if (!isDefined(suspension)) {+ return;+ }++ if (isCallerGone) {+ await this.agentRunSuspensionService.release({ workspaceId, suspension });++ return;+ }++ // the claimed wake-up is gone, so a run that cannot continue would wait forever+ try {+ await this.recordWaitOutcome({+ workspaceId,+ threadId: suspension.threadId,+ toolCallId,+ outcome,+ });++ await this.agentRunSuspensionService.scheduleContinuation({+ workspaceId,+ suspension,+ });+ } catch (error) {+ await this.agentRunSuspensionService.settle({+ workspaceId,+ suspension,+ outcome: {+ status: 'FAILED',+ error: `A waiting agent could not continue: ${error instanceof Error ? error.message : String(error)}`,+ },+ });++ throw error;+ }+ }++ // The wait call stays pending in the conversation until its wake-up resolves it, then carries the outcome+ private async recordWaitOutcome({+ workspaceId,+ threadId,+ toolCallId,+ outcome,+ }: {+ workspaceId: string;+ threadId: string;+ toolCallId: string;+ outcome: PendingWakeUpOutcome;+ }): Promise<void> {+ // messages can follow the call while it waits, so the latest one may not carry it+ const recentMessages = await this.messageRepository.find(workspaceId, {+ where: { threadId },+ order: { createdAt: 'DESC' },+ take: RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL,+ relations: ['parts'],+ });++ const pendingPart = recentMessages+ .flatMap((message) => message.parts ?? [])+ .find(+ (part) =>+ part.toolCallId === toolCallId &&+ isAwaitingPausingToolOutput(part.toolOutput),+ );++ if (!isDefined(pendingPart)) {+ throw new AiException(+ 'The waiting call could not be found in the conversation',+ AiExceptionCode.TOOL_CALL_NOT_FOUND,+ );+ }++ await this.messagePartRepository.update(+ workspaceId,+ { id: pendingPart.id },+ { toolOutput: buildWaitOutcomeToolOutput(outcome) },+ );+ }+}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.tsnew file mode 100644index 00000000000..9deb453ba92--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.ts@@ -0,0 +1,358 @@+import { Injectable } from '@nestjs/common';++import isEqual from 'lodash.isequal';+import { isDefined } from 'twenty-shared/utils';+import { IsNull, Not, Raw } from 'typeorm';+import { type QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';++import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';+import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';+import { CONTINUE_AGENT_RUN_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant';+import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';+import { closeOpenToolParts } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/close-open-tool-parts.util';+import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';+import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';+import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';+import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';+import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type';+import { type AgentRunWait } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-wait.type';+import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';+import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';+import { AgentChatThreadRecordEventService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-record-event.service';+import { isUniqueViolationError } from 'src/engine/metadata-modules/ai/ai-chat/utils/is-unique-violation-error.util';+import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';+import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';+import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';+import { AgentTurnRecorderService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-turn-recorder.service';+import { type AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';+import { type AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';+import {+ AiException,+ AiExceptionCode,+} from 'src/engine/metadata-modules/ai/ai.exception';+import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';+import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';++const AGENT_RUN_OWNER_TYPE = 'AGENT_RUN';++// The suspended runs callers wait on: what continues them, and who gets their outcome+@Injectable()+export class AgentRunSuspensionService {+ constructor(+ @InjectWorkspaceScopedRepository(AgentRunSuspensionEntity)+ private readonly suspensionRepository: WorkspaceScopedRepository<AgentRunSuspensionEntity>,+ @InjectAgentHistoryRepository('agentChatThread')+ private readonly threadRepository: AgentHistoryRepository<AgentChatThreadWorkspaceEntity>,+ @InjectAgentHistoryRepository('agentMessagePart')+ private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,+ private readonly threadRecordEventService: AgentChatThreadRecordEventService,+ private readonly turnRecorderService: AgentTurnRecorderService,+ private readonly pendingWakeUpService: PendingWakeUpService,+ private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,+ @InjectMessageQueue(MessageQueue.aiQueue)+ private readonly messageQueueService: MessageQueueService,+ ) {}++ async findOne({+ workspaceId,+ where,+ }: {+ workspaceId: string;+ where: { id: string } | { threadId: string };+ }): Promise<AgentRunSuspensionEntity | null> {+ return this.suspensionRepository.findOne(workspaceId, { where });+ }++ async suspend({+ workspaceId,+ threadId,+ caller,+ runSpec,+ summary,+ }: {+ workspaceId: string;+ threadId: string;+ caller: AgentRunCaller;+ runSpec: AgentRunSpec | null;+ summary: AgentRunSummary | null;+ }): Promise<AgentRunSuspensionEntity> {+ return this.suspensionRepository.insertAndReturnOne(workspaceId, {+ threadId,+ caller,+ runSpec,+ summary,+ } as QueryDeepPartialEntity<AgentRunSuspensionEntity>);+ }++ // A call the caller posted itself, such as a workflow step asking for approval: there is no+ // agent to continue, so the answer goes straight back to the caller. Posting again is a no-op+ async awaitCallerCall({+ workspaceId,+ threadId,+ caller,+ }: {+ workspaceId: string;+ threadId: string;+ caller: AgentRunCaller;+ }): Promise<void> {+ try {+ await this.suspend({+ workspaceId,+ threadId,+ caller,+ runSpec: null,+ summary: null,+ });+ } catch (error) {+ if (!isUniqueViolationError(error)) {+ throw error;+ }++ const existingSuspension = await this.findOne({+ workspaceId,+ where: { threadId },+ });++ // jsonb stores keys in its own order, so the stored caller is compared by value+ if (!isEqual(existingSuspension?.caller, caller)) {+ throw new AiException(+ 'The conversation is waiting on another run',+ AiExceptionCode.THREAD_AWAITING_ANSWER,+ );+ }+ }+ }++ async armWait({+ workspaceId,+ suspensionId,+ wait,+ }: {+ workspaceId: string;+ suspensionId: string;+ wait: AgentRunWait;+ }): Promise<void> {+ await this.pendingWakeUpService.arm({+ workspaceId,+ owner: {+ type: AGENT_RUN_OWNER_TYPE,+ id: suspensionId,+ key: wait.toolCallId,+ },+ condition: wait.condition,+ });+ }++ async scheduleContinuation({+ workspaceId,+ suspension,+ }: {+ workspaceId: string;+ suspension: Pick<AgentRunSuspensionEntity, 'id' | 'resumeCount'>;+ }): Promise<void> {+ await this.messageQueueService.add<ContinueAgentRunJobData>(+ CONTINUE_AGENT_RUN_JOB_NAME,+ {+ workspaceId,+ suspensionId: suspension.id,+ resumeCount: suspension.resumeCount,+ },+ );+ }++ async getCallerWaitingState({+ workspaceId,+ suspension: { caller },+ }: {+ workspaceId: string;+ suspension: AgentRunSuspensionEntity;+ }): Promise<AgentRunCallerWaitingState> {+ return this.callerHandlerRegistry+ .getHandlerOrThrow(caller.type)+ .getWaitingState({ workspaceId, caller });+ }++ // The caller marks its call before it records the suspension, so a call without one is not ready yet+ async findWaitingSuspension({+ workspaceId,+ threadId,+ }: {+ workspaceId: string;+ threadId: string;+ }): Promise<+ | { status: 'NOT_READY' }+ | { status: 'WAITING' | 'GONE'; suspension: AgentRunSuspensionEntity }+ > {+ const suspension = await this.findOne({ workspaceId, where: { threadId } });++ if (!isDefined(suspension)) {+ return { status: 'NOT_READY' };+ }++ const status = await this.getCallerWaitingState({+ workspaceId,+ suspension,+ });++ return status === 'NOT_READY' ? { status } : { status, suspension };+ }++ // The last answer continues the agent, or is itself the outcome of a call the caller posted+ async deliverAnswer({+ workspaceId,+ suspension,+ toolResult,+ }: {+ workspaceId: string;+ suspension: AgentRunSuspensionEntity;+ toolResult: Record<string, unknown>;+ }): Promise<void> {+ if (isDefined(suspension.runSpec)) {+ await this.scheduleContinuation({ workspaceId, suspension });++ return;+ }++ await this.settle({+ workspaceId,+ suspension,+ outcome: { status: 'COMPLETED', result: toolResult },+ });+ }++ // A run that ended, or could not go on, leaves nothing to wait on, and its caller gets the outcome+ async settle({+ workspaceId,+ suspension,+ outcome,+ summary = suspension.summary,+ }: {+ workspaceId: string;+ suspension: AgentRunSuspensionEntity;+ outcome: Exclude<AgentRunnerOutcome, { status: 'SUSPENDED' }>;+ summary?: AgentRunSummary | null;+ }): Promise<void> {+ await this.release({ workspaceId, suspension });++ await this.callerHandlerRegistry+ .getHandlerOrThrow(suspension.caller.type)+ .onOutcome({+ workspaceId,+ caller: suspension.caller,+ threadId: suspension.threadId,+ outcome,+ summary,+ });+ }++ // A caller that stops waiting drops its suspended runs, such as every step of a run that ended+ async releaseForCaller({+ workspaceId,+ caller,+ }: {+ workspaceId: string;+ caller: {+ type: AgentRunCaller['type'];+ ref: Partial<AgentRunCaller['ref']>;+ };+ }): Promise<void> {+ const suspensions = await this.suspensionRepository.find(workspaceId, {+ where: {+ caller: Raw((alias) => `${alias} @> :callerFilter::jsonb`, {+ callerFilter: JSON.stringify(caller),+ }),+ },+ });++ for (const suspension of suspensions) {+ await this.release({ workspaceId, suspension });+ }+ }++ // The calls the run waits on are closed too, so they no longer look waiting+ async release({+ workspaceId,+ suspension,+ }: {+ workspaceId: string;+ suspension: Pick<AgentRunSuspensionEntity, 'id' | 'threadId'>;+ }): Promise<void> {+ await this.pendingWakeUpService.cancelAllForOwner({+ workspaceId,+ ownerType: AGENT_RUN_OWNER_TYPE,+ ownerId: suspension.id,+ });+ await this.suspensionRepository.delete(workspaceId, { id: suspension.id });+ await this.closeAwaitedCalls({+ workspaceId,+ threadId: suspension.threadId,+ });+ }++ // An answer holding the conversation's claim closes its calls itself once it finds its caller gone,+ // and a question the member asked of their own in the conversation stays open+ async closeAwaitedCalls({+ workspaceId,+ threadId,+ }: {+ workspaceId: string;+ threadId: string;+ }): Promise<void> {+ const thread = await this.threadRepository.findOne(workspaceId, {+ where: {+ id: threadId,+ pendingQuestionMessageId: Not(IsNull()),+ activeStreamId: IsNull(),+ },+ select: ['id', 'pendingQuestionMessageId'],+ });+ const pendingQuestionMessageId = thread?.pendingQuestionMessageId;++ if (!isDefined(pendingQuestionMessageId)) {+ return;+ }++ const pendingParts = await this.messagePartRepository.find(workspaceId, {+ where: { messageId: pendingQuestionMessageId },+ select: ['toolOutput'],+ });++ if (+ !pendingParts.some((part) => isToolOutputAwaitedByCaller(part.toolOutput))+ ) {+ return;+ }++ const { affected } = await this.threadRepository.update(+ workspaceId,+ { id: threadId, pendingQuestionMessageId, activeStreamId: IsNull() },+ { pendingQuestionMessageId: null },+ );++ if (affected === 0) {+ return;+ }++ await this.threadRecordEventService.emitPendingQuestionCleared({+ workspaceId,+ threadId,+ messageId: pendingQuestionMessageId,+ });++ // the question is already cleared, so its calls must close before anything else can fail+ await closeOpenToolParts({+ messagePartRepository: this.messagePartRepository,+ messageId: pendingQuestionMessageId,+ workspaceId,+ });++ await this.turnRecorderService.endWaitingTurn({+ workspaceId,+ messageId: pendingQuestionMessageId,+ status: AgentTurnStatus.CANCELLED,+ });+ }+}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.tsindex c8e35d5e681..799211d662a 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts@@ -195,13 +195,8 @@ export class AgentRunService {},});- if (outcome.status === 'NO_CREDITS') {- return {- result: null,- error: 'Agent stopped: no more available credits.',- success: false,- threadId,- };+ if (outcome.status === 'FAILED') {+ return { result: null, error: outcome.error, success: false, threadId };}return {diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.tsindex ceda7fc66f1..db4e8ab45ac 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts@@ -1,18 +1,50 @@import { Injectable, Logger } from '@nestjs/common';+import { isNonEmptyString } from '@sniptt/guards';+import {+ ASK_QUESTION_TOOL_NAME,+ REQUEST_FORM_TOOL_NAME,+} from 'twenty-shared/ai';import { isDefined } from 'twenty-shared/utils';+import { type QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';+import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';+import { AGENT_WAIT_PROMPT } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-prompt.constant';+import { createAgentWaitTools } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util';+import { findAgentRunWait } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util';import { AgentAsyncExecutorService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service';+import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';import { AgentRunConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { type AgentExecutionResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-execution-result.type';+import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';+import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type';import { type AgentRunnerResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-result.type';import { type AgentRunnerRunInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type';+import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';import { buildAgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/build-agent-run-summary.util';+import { sumAgentRunSummaries } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util';+import { AgentEntity } from 'src/engine/metadata-modules/ai/ai-agent/entities/agent.entity';+import { createAskQuestionTool } from 'src/engine/metadata-modules/ai/ai-chat/tools/ask-question.tool';+import { createRequestFormTool } from 'src/engine/metadata-modules/ai/ai-chat/tools/request-form.tool';import { AgentConversationReaderService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service';import { withDedicatedAiTrace } from 'src/engine/metadata-modules/ai/ai-models/utils/with-dedicated-ai-trace.util';+import {+ AiException,+ AiExceptionCode,+} from 'src/engine/metadata-modules/ai/ai.exception';+import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';+import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';-// Runs one agent turn and records it in its conversation+type AgentRunnerTurnInput = Extract<AgentRunnerRunInput, { turn: unknown }>;++type AgentRunnerCallerInput = Extract<AgentRunnerRunInput, { caller: unknown }>;++// Runs an agent turn and records it in its conversation. A run a caller waits on that pauses on an+// answer or a wait is suspended: the engine continues it on its own, through this same runner,+// and hands the outcome to the caller's handler@Injectable()export class AgentRunnerService {private readonly logger = new Logger(AgentRunnerService.name);@@ -21,29 +53,148 @@ export class AgentRunnerService {private readonly agentAsyncExecutorService: AgentAsyncExecutorService,private readonly agentRunConversationService: AgentRunConversationService,private readonly conversationReaderService: AgentConversationReaderService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,+ private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,+ @InjectWorkspaceScopedRepository(AgentRunSuspensionEntity)+ private readonly suspensionRepository: WorkspaceScopedRepository<AgentRunSuspensionEntity>,+ @InjectWorkspaceScopedRepository(AgentEntity)+ private readonly agentRepository: WorkspaceScopedRepository<AgentEntity>,) {}run(input: AgentRunnerRunInput): Promise<AgentRunnerResult> {+ return this.runSegment({ input, suspension: null });+ }++ // Picks a suspended run up where it paused, with its answer or wait outcome already in its conversation+ async continue({+ workspaceId,+ suspensionId,+ resumeCount,+ }: ContinueAgentRunJobData): Promise<void> {+ const suspension = await this.agentRunSuspensionService.findOne({+ workspaceId,+ where: { id: suspensionId },+ });++ if (+ !isDefined(suspension) ||+ !isDefined(suspension.runSpec) ||+ suspension.resumeCount !== resumeCount+ ) {+ return;+ }++ const { caller, runSpec, threadId } = suspension;++ const waitingState =+ await this.agentRunSuspensionService.getCallerWaitingState({+ workspaceId,+ suspension,+ });++ if (waitingState === 'GONE') {+ await this.agentRunSuspensionService.release({ workspaceId, suspension });++ return;+ }++ // each continuation moves the count on, so of two deliveries of one continuation only the first runs+ const { affected } = await this.suspensionRepository.update(+ workspaceId,+ { id: suspensionId, resumeCount },+ { resumeCount: resumeCount + 1 },+ );++ if (affected === 0) {+ return;+ }++ const handler = this.callerHandlerRegistry.getHandlerOrThrow(caller.type);++ let result: AgentRunnerResult;++ try {+ result = await this.runSegment({+ input: {+ workspaceId,+ conversation: { threadId, isCreated: false },+ caller,+ spec: runSpec,+ agent: await this.findAgentOrThrow({+ workspaceId,+ agentId: runSpec.agentId,+ }),+ prompt: null,+ executionContext: await handler.buildExecutionContext({+ workspaceId,+ caller,+ }),+ resolveCreatedBy: () =>+ handler.resolveTurnAuthor({ workspaceId, caller }),+ },+ suspension,+ });+ } catch (error) {+ await this.agentRunSuspensionService.settle({+ workspaceId,+ suspension,+ outcome: {+ status: 'FAILED',+ error: error instanceof Error ? error.message : String(error),+ },+ });++ return;+ }++ if (result.outcome.status !== 'SUSPENDED') {+ await this.agentRunSuspensionService.settle({+ workspaceId,+ suspension,+ outcome: result.outcome,+ summary: result.summary,+ });+ }+ }++ private runSegment({+ input,+ suspension,+ }: {+ input: AgentRunnerRunInput;+ // the suspension a continued run resumes from+ suspension: AgentRunSuspensionEntity | null;+ }): Promise<AgentRunnerResult> {const { workspaceId, conversation } = input;+ const work = () => this.runTurn({ input, suspension });// a continued conversation is read before the run, so two runs on it must not interleavereturn conversation.isCreated- ? this.runTurn(input)+ ? work(): this.agentRunConversationService.withThreadLock({workspaceId,threadId: conversation.threadId,- work: () => this.runTurn(input),+ work,});}private async runTurn({- workspaceId,- conversation: { threadId, isCreated },- conversationActor,- caller,- turn,- execution,- }: AgentRunnerRunInput): Promise<AgentRunnerResult> {+ input,+ suspension,+ }: {+ input: AgentRunnerRunInput;+ suspension: AgentRunSuspensionEntity | null;+ }): Promise<AgentRunnerResult> {+ const {+ workspaceId,+ conversation: { threadId, isCreated },+ conversationActor,+ } = input;+ const { turn, execution } =+ 'caller' in input ? this.buildCallerTurn(input) : input;+ const callerRun =+ 'caller' in input ? { caller: input.caller, spec: input.spec } : null;+const priorMessages = isCreated? []: await this.conversationReaderService.loadMessages({@@ -102,19 +253,214 @@ export class AgentRunnerService {title: turn.title,agentId,execution: executionResult,- caller,+ isAwaitedByCaller: isDefined(callerRun),}),): null;+ const summary = sumAgentRunSummaries({+ previousSummary: suspension?.summary ?? null,+ nextSummary: buildAgentRunSummary({+ execution: executionResult,+ durationMs,+ }),+ });+return {threadId,- outcome: this.buildOutcome({+ summary,+ outcome: await this.settleTurn({+ workspaceId,+ threadId,+ callerRun,execution: executionResult,isRecorded: isDefined(closedTurn),isAwaitingAnswer: closedTurn?.isAwaitingAnswer ?? false,+ summary,+ suspension,}),- summary: buildAgentRunSummary({ execution: executionResult, durationMs }),+ };+ }++ private async settleTurn({+ workspaceId,+ threadId,+ callerRun,+ execution,+ isRecorded,+ isAwaitingAnswer,+ summary,+ suspension,+ }: {+ workspaceId: string;+ threadId: string;+ callerRun: { caller: AgentRunCaller; spec: AgentRunSpec } | null;+ execution: AgentExecutionResult;+ isRecorded: boolean;+ isAwaitingAnswer: boolean;+ summary: AgentRunSummary;+ suspension: AgentRunSuspensionEntity | null;+ }): Promise<AgentRunnerOutcome> {+ if (!execution.hasNoMoreAvailableCredits && !execution.isPaused) {+ return { status: 'COMPLETED', result: execution.result };+ }++ const wait = findAgentRunWait(+ execution.steps[execution.steps.length - 1]?.toolResults ?? [],+ );++ // continuing reads the conversation, so a pause it does not hold could never be continued+ const suspensionId =+ !execution.hasNoMoreAvailableCredits &&+ isDefined(callerRun) &&+ isRecorded &&+ (isAwaitingAnswer || isDefined(wait))+ ? await this.keepSuspended({+ workspaceId,+ threadId,+ callerRun,+ summary,+ suspension,+ })+ : null;++ if (!isDefined(suspensionId)) {+ // a pause the run could not keep leaves nothing to answer+ if (isDefined(callerRun) && !isDefined(suspension)) {+ await this.agentRunSuspensionService.closeAwaitedCalls({+ workspaceId,+ threadId,+ });+ }++ if (execution.hasNoMoreAvailableCredits) {+ return {+ status: 'FAILED',+ error: 'Agent stopped: no more available credits.',+ };+ }++ if (isDefined(wait)) {+ return {+ status: 'FAILED',+ error:+ 'Agent paused to wait but its conversation could not be recorded.',+ };+ }++ return {+ status: 'FAILED',+ error: 'Agent asked a question that could not be recorded.',+ };+ }++ if (isDefined(wait)) {+ await this.agentRunSuspensionService.armWait({+ workspaceId,+ suspensionId,+ wait,+ });+ }++ return { status: 'SUSPENDED', suspensionId };+ }++ // A failure to suspend only fails the run, like a pause that could not be recorded+ private async keepSuspended({+ workspaceId,+ threadId,+ callerRun: { caller, spec },+ summary,+ suspension,+ }: {+ workspaceId: string;+ threadId: string;+ callerRun: { caller: AgentRunCaller; spec: AgentRunSpec };+ summary: AgentRunSummary;+ suspension: AgentRunSuspensionEntity | null;+ }): Promise<string | null> {+ try {+ if (!isDefined(suspension)) {+ const { id } = await this.agentRunSuspensionService.suspend({+ workspaceId,+ threadId,+ caller,+ runSpec: spec,+ summary,+ });++ return id;+ }++ // a caller that stopped waiting while the run went on has released it+ const { affected } = await this.suspensionRepository.update(+ workspaceId,+ { id: suspension.id },+ { summary } as QueryDeepPartialEntity<AgentRunSuspensionEntity>,+ );++ return affected === 0 ? null : suspension.id;+ } catch (error) {+ this.logger.error(+ `Failed to suspend the agent run in thread ${threadId}`,+ error instanceof Error ? error.stack : error,+ );++ return null;+ }+ }++ // The toolset and prompt come from the spec alone, so a continued run gets those it paused with+ private buildCallerTurn({+ workspaceId,+ spec,+ agent,+ prompt,+ executionContext,+ resolveCreatedBy,+ }: AgentRunnerCallerInput): Pick<AgentRunnerTurnInput, 'turn' | 'execution'> {+ const messages = isDefined(prompt)+ ? [{ role: 'user' as const, content: prompt }]+ : [];+ const { canAskHumans, canProposeToolCalls, canWait } = spec.capabilities;++ return {+ turn: {+ title: spec.title,+ senderUserWorkspaceId: executionContext.userWorkspaceId,+ senderApplicationId: null,+ messages,+ resolveCreatedBy,+ },+ execution: {+ agent,+ messages,+ // the wait tools are the engine's, so it explains them; the caller's own instructions come last+ baseSystemPrompt: [+ spec.baseSystemPrompt,+ ...(canWait ? [AGENT_WAIT_PROMPT] : []),+ ...(isNonEmptyString(spec.instructions) ? [spec.instructions] : []),+ ].join('\n\n'),+ pausingTools: {+ ...(canWait ? createAgentWaitTools() : {}),+ ...(canAskHumans+ ? {+ [ASK_QUESTION_TOOL_NAME]: createAskQuestionTool({+ isWorkspaceSetupThread: false,+ }),+ [REQUEST_FORM_TOOL_NAME]: createRequestFormTool(),+ }+ : {}),+ },+ canProposeToolCalls,+ actorContext: executionContext.actorContext,+ authContext: executionContext.authContext,+ workspaceId,+ userWorkspaceId: executionContext.userWorkspaceId,+ additionalRoleRestrictionIds:+ executionContext.additionalRoleRestrictionIds,+ additionalExcludedToolNames: spec.additionalExcludedToolNames,+ },};}@@ -135,37 +481,28 @@ export class AgentRunnerService {}}- private buildOutcome({- execution,- isRecorded,- isAwaitingAnswer,+ private async findAgentOrThrow({+ workspaceId,+ agentId,}: {- execution: AgentExecutionResult;- isRecorded: boolean;- isAwaitingAnswer: boolean;- }): AgentRunnerOutcome {- if (execution.hasNoMoreAvailableCredits) {- return { status: 'NO_CREDITS' };+ workspaceId: string;+ agentId: string | null;+ }): Promise<AgentEntity | null> {+ if (!isDefined(agentId)) {+ return null;}- if (!execution.isPaused) {- return { status: 'COMPLETED', result: execution.result };- }+ const agent = await this.agentRepository.findOne(workspaceId, {+ where: { id: agentId },+ });- if (isAwaitingAnswer) {- return { status: 'AWAITING_ANSWER' };+ if (!isDefined(agent)) {+ throw new AiException(+ `Agent with id ${agentId} not found`,+ AiExceptionCode.AGENT_NOT_FOUND,+ );}- const lastStepToolResults =- execution.steps[execution.steps.length - 1]?.toolResults ?? [];-- return {- status: 'PAUSED',- isResumable: isRecorded,- pausedToolResults: lastStepToolResults.map(({ toolName, output }) => ({- toolName,- output,- })),- };+ return agent;}}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-filter.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-filter.type.tsdeleted file mode 100644index 942fc096cab..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-filter.type.ts+++ /dev/null@@ -1,7 +0,0 @@-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';--// matches every caller whose ref holds these values, such as every step of one run-export type AgentRunCallerFilter = {- type: AgentRunCaller['type'];- ref: Partial<AgentRunCaller['ref']>;-};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.tsnew file mode 100644index 00000000000..a04da2bc5ae--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.ts@@ -0,0 +1,35 @@+import { type ActorMetadata } from 'twenty-shared/types';++import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';+import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';+import { type AgentRunExecutionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type';+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';+import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type';++type AgentRunCallerInput = {+ workspaceId: string;+ caller: AgentRunCaller;+};++export type AgentRunCallerHandler = {+ callerType: AgentRunCaller['type'];++ buildExecutionContext(+ input: AgentRunCallerInput,+ ): Promise<AgentRunExecutionContext>;++ resolveTurnAuthor(input: AgentRunCallerInput): Promise<ActorMetadata>;++ getWaitingState(+ input: AgentRunCallerInput,+ ): Promise<AgentRunCallerWaitingState>;++ onOutcome(+ input: AgentRunCallerInput & {+ threadId: string;+ outcome: Exclude<AgentRunnerOutcome, { status: 'SUSPENDED' }>;+ // null for a call the caller posted itself, which no agent ran+ summary: AgentRunSummary | null;+ },+ ): Promise<void>;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type.tsnew file mode 100644index 00000000000..4c1eead4ee4--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type.ts@@ -0,0 +1,3 @@+// WAITING: the caller takes the outcome now; NOT_READY: it is still recording that it waits;+// GONE: it stopped waiting, such as a workflow run that ended+export type AgentRunCallerWaitingState = 'WAITING' | 'NOT_READY' | 'GONE';diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.tsindex 15d7d8711d8..da2d8dd1bb5 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts@@ -1,4 +1,5 @@-// who started a run and waits on what it pauses on; written on the calls it leaves pending+// who started a run and waits on its outcome; stored with a suspended run, so the engine+// can call it back without knowing what the ref meansexport type AgentRunCaller = {type: 'WORKFLOW_STEP';ref: { workflowRunId: string; stepId: string };diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type.tsnew file mode 100644index 00000000000..55edf0f2816--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type.ts@@ -0,0 +1,15 @@+import { type ActorMetadata } from 'twenty-shared/types';++import { type WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';+import { type RolePermissionConfig } from 'src/engine/twenty-orm/types/role-permission-config.type';++// The permissions a run acts with, which its caller derives on each segment instead of the engine storing them+export type AgentRunExecutionContext = {+ authContext: WorkspaceAuthContext;+ actorContext?: ActorMetadata;+ userWorkspaceId: string | null;+ // what the run itself can read, such as the record an awaited event is about+ rolePermissionConfig: RolePermissionConfig;+ // narrows the agent's own role, such as to the application a run belongs to+ additionalRoleRestrictionIds?: string[];+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-paused-tool-result.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-paused-tool-result.type.tsdeleted file mode 100644index df3b3f64a48..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-paused-tool-result.type.ts+++ /dev/null@@ -1,4 +0,0 @@-export type AgentRunPausedToolResult = {- toolName: string;- output: unknown;-};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type.tsnew file mode 100644index 00000000000..8ea7123081d--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type.ts@@ -0,0 +1,19 @@+// What a suspended run needs to continue without its caller building it again.+// Permissions are left out: the caller derives them anew on each continuation+export type AgentRunSpec = {+ // without an agent, the run uses the workspace's default model+ agentId: string | null;+ title: string;+ baseSystemPrompt: string;+ // the caller's own instructions, after what the engine says about its pausing tools+ instructions: string | null;+ capabilities: {+ // ask_question and request_form, answered from the conversation+ canAskHumans: boolean;+ // propose_tool_call, for an action a person approves first+ canProposeToolCalls: boolean;+ // wait_for_event and wait_for_duration, resolved by a wake-up+ canWait: boolean;+ };+ additionalExcludedToolNames?: string[];+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-wait.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-wait.type.tsnew file mode 100644index 00000000000..19163fdc24c--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-wait.type.ts@@ -0,0 +1,7 @@+import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';++// the wait call a run paused on, and what wakes it up+export type AgentRunWait = {+ toolCallId: string;+ condition: PendingWakeUpCondition;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.tsindex 5de11538507..0531a28f514 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts@@ -1,13 +1,5 @@-import { type AgentRunPausedToolResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-paused-tool-result.type';-export type AgentRunnerOutcome =| { status: 'COMPLETED'; result: object }- | { status: 'NO_CREDITS' }- | { status: 'AWAITING_ANSWER' }- // paused on a call that is not a question, such as a wait the caller resolves itself.- // Only a recorded pause can be continued, since continuing reads the conversation- | {- status: 'PAUSED';- isResumable: boolean;- pausedToolResults: AgentRunPausedToolResult[];- };+ // paused on an answer or a wait; the engine continues the run and calls its caller back+ | { status: 'SUSPENDED'; suspensionId: string }+ | { status: 'FAILED'; error: string };diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-result.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-result.type.tsindex aab1a56fc34..ae1b2b9d3d8 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-result.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-result.type.ts@@ -4,5 +4,6 @@ import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agentexport type AgentRunnerResult = {threadId: string;outcome: AgentRunnerOutcome;+ // the run so far, across every segment of a continued runsummary: AgentRunSummary;};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type.tsindex a4a8f5894f4..c992dd783f0 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type.ts@@ -4,6 +4,9 @@ import { type ActorMetadata } from 'twenty-shared/types';import { type AgentAsyncExecutorService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service';import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';import { type AgentRunConversation } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-conversation.type';+import { type AgentRunExecutionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type';+import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';+import { type AgentEntity } from 'src/engine/metadata-modules/ai/ai-agent/entities/agent.entity';import { type AgentConversationActor } from 'src/engine/metadata-modules/ai/ai-history/types/agent-conversation-actor.type';export type AgentRunnerRunInput = {@@ -11,19 +14,31 @@ export type AgentRunnerRunInput = {conversation: AgentRunConversation;// who the continued conversation is read for, so turns others acted on read as theirsconversationActor?: AgentConversationActor;- // set when the caller waits on the calls the run pauses on- caller?: AgentRunCaller;- turn: {- // names the thread when the turn creates it- title: string;- senderUserWorkspaceId: string | null;- senderApplicationId: string | null;- messages: RunAgentMessage[];- // resolved while recording the turn, so a failed lookup does not stop the run- resolveCreatedBy: () => Promise<ActorMetadata>;- };- execution: Omit<- Parameters<AgentAsyncExecutorService['executeAgent']>[0],- 'priorMessages'- >;-};+} & (+ | {+ turn: {+ // names the thread when the turn creates it+ title: string;+ senderUserWorkspaceId: string | null;+ senderApplicationId: string | null;+ messages: RunAgentMessage[];+ // resolved while recording the turn, so a failed lookup does not stop the run+ resolveCreatedBy: () => Promise<ActorMetadata>;+ };+ execution: Omit<+ Parameters<AgentAsyncExecutorService['executeAgent']>[0],+ 'priorMessages'+ >;+ }+ // A caller such as a workflow step waits on the outcome: a run that pauses is suspended with its+ // spec, and the engine continues it on its own and hands the outcome to the caller's handler+ | {+ caller: AgentRunCaller;+ spec: AgentRunSpec;+ agent: AgentEntity | null;+ // absent when the run continues its conversation without a new message+ prompt: string | null;+ executionContext: AgentRunExecutionContext;+ resolveCreatedBy: () => Promise<ActorMetadata>;+ }+);diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.tsnew file mode 100644index 00000000000..bf046a6f5f4--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts@@ -0,0 +1,6 @@+export type ContinueAgentRunJobData = {+ workspaceId: string;+ suspensionId: string;+ // the suspension's resume count when scheduled, so a duplicate job finds it moved on+ resumeCount: number;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/__tests__/sum-agent-run-summaries.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/__tests__/sum-agent-run-summaries.util.spec.tsnew file mode 100644index 00000000000..abf25c3c42c--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/__tests__/sum-agent-run-summaries.util.spec.ts@@ -0,0 +1,90 @@+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';+import { sumAgentRunSummaries } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util';++const buildSummary = ({+ modelId,+ tokens,+ toolCallNames,+ durationMs,+ reasoningTokens,+}: {+ modelId: string;+ tokens: number;+ toolCallNames: string[];+ durationMs: number;+ reasoningTokens?: number;+}): AgentRunSummary => ({+ modelId,+ usage: {+ inputTokens: tokens,+ outputTokens: tokens,+ reasoningTokens,+ totalTokens: tokens * 2,+ },+ cost: { totalCostInDollars: tokens / 100, creditsUsedMicro: tokens },+ nativeWebSearchCallCount: 1,+ toolCalls: toolCallNames.map((toolName) => ({+ toolName,+ toolCallId: `${toolName}-call`,+ state: 'success',+ })),+ durationMs,+});++describe('sumAgentRunSummaries', () => {+ it('adds the segment after a pause to the ones before it', () => {+ const summary = sumAgentRunSummaries({+ previousSummary: buildSummary({+ modelId: 'model-a',+ tokens: 10,+ toolCallNames: ['search', 'ask_question'],+ durationMs: 100,+ reasoningTokens: 10,+ }),+ nextSummary: buildSummary({+ modelId: 'model-b',+ tokens: 5,+ toolCallNames: ['send_email'],+ durationMs: 50,+ reasoningTokens: 5,+ }),+ });++ expect(summary).toMatchObject({+ modelId: 'model-b',+ usage: {+ inputTokens: 15,+ outputTokens: 15,+ reasoningTokens: 15,+ totalTokens: 30,+ },+ cost: { totalCostInDollars: expect.closeTo(0.15), creditsUsedMicro: 15 },+ nativeWebSearchCallCount: 2,+ durationMs: 150,+ });+ expect(summary.toolCalls.map(({ toolName }) => toolName)).toEqual([+ 'search',+ 'ask_question',+ 'send_email',+ ]);+ });++ it('leaves an optional count out when no segment reported it', () => {+ const summary = sumAgentRunSummaries({+ previousSummary: buildSummary({+ modelId: 'model-a',+ tokens: 1,+ toolCallNames: [],+ durationMs: 1,+ }),+ nextSummary: buildSummary({+ modelId: 'model-a',+ tokens: 1,+ toolCallNames: [],+ durationMs: 1,+ }),+ });++ expect(summary.usage.reasoningTokens).toBeUndefined();+ });+});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.tsindex 1110a1af3df..73ecbaa4b20 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts@@ -1,24 +1,4 @@-import { isDefined } from 'twenty-shared/utils';+import { isPlainObject } from 'twenty-shared/utils';-import { type AgentRunCallerFilter } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-filter.type';-import { readToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util';--export const isToolOutputAwaitedByCaller = ({- toolOutput,- caller,-}: {- toolOutput: unknown;- caller: AgentRunCallerFilter;-}): boolean => {- const workflowStep = readToolCallWorkflowStep(toolOutput);-- if (!isDefined(workflowStep) || caller.type !== 'WORKFLOW_STEP') {- return false;- }-- return (- (!isDefined(caller.ref.workflowRunId) ||- caller.ref.workflowRunId === workflowStep.workflowRunId) &&- (!isDefined(caller.ref.stepId) || caller.ref.stepId === workflowStep.stepId)- );-};+export const isToolOutputAwaitedByCaller = (toolOutput: unknown): boolean =>+ isPlainObject(toolOutput) && toolOutput.awaitedByCaller === true;diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/map-agent-run-caller-to-tool-call-workflow-step.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/map-agent-run-caller-to-tool-call-workflow-step.util.tsdeleted file mode 100644index 679b9ce5ffc..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/map-agent-run-caller-to-tool-call-workflow-step.util.ts+++ /dev/null@@ -1,10 +0,0 @@-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';-import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';--// the caller is written on pending calls in the shape answers already read-export const mapAgentRunCallerToToolCallWorkflowStep = (- caller: AgentRunCaller,-): ToolCallWorkflowStep => ({- workflowRunId: caller.ref.workflowRunId,- stepId: caller.ref.stepId,-});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util.tsnew file mode 100644index 00000000000..2bc895f693a--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util.ts@@ -0,0 +1,38 @@+import { isDefined } from 'twenty-shared/utils';++import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';++// a count no segment reported stays out+const sumCounts = <TCounts extends Record<string, number | undefined>>(+ previousCounts: TCounts,+ nextCounts: TCounts,+): TCounts =>+ Object.fromEntries(+ Object.keys({ ...previousCounts, ...nextCounts }).map((key) => [+ key,+ isDefined(previousCounts[key]) || isDefined(nextCounts[key])+ ? (previousCounts[key] ?? 0) + (nextCounts[key] ?? 0)+ : undefined,+ ]),+ ) as TCounts;++// A run continued after a pause spans several segments, which its caller sees as one run+export const sumAgentRunSummaries = ({+ previousSummary,+ nextSummary,+}: {+ previousSummary: AgentRunSummary | null;+ nextSummary: AgentRunSummary;+}): AgentRunSummary =>+ isDefined(previousSummary)+ ? {+ ...nextSummary,+ usage: sumCounts(previousSummary.usage, nextSummary.usage),+ cost: sumCounts(previousSummary.cost, nextSummary.cost),+ nativeWebSearchCallCount:+ previousSummary.nativeWebSearchCallCount ++ nextSummary.nativeWebSearchCallCount,+ toolCalls: [...previousSummary.toolCalls, ...nextSummary.toolCalls],+ durationMs: previousSummary.durationMs + nextSummary.durationMs,+ }+ : nextSummary;diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.tsindex c30e155de99..5e7f75d79ad 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts@@ -280,17 +280,17 @@ describe('AgentChatStreamingService claim & reap', () => {).toBeLessThan(messageQueueService.add.mock.invocationCallOrder[0]);});- it('refuses a message while a workflow step waits on the pending call', async () => {+ it('refuses a message while a caller waits on the pending call', async () => {const { send, agentChatService, messageQueueService } = buildService({thread: waitingThread,pendingToolOutput: {result: { questions: QUESTIONS, status: 'pending' },- workflowStep: { workflowRunId: 'workflow-run-id', stepId: 'step-id' },+ awaitedByCaller: true,},});await expect(send()).rejects.toMatchObject({- code: AiExceptionCode.THREAD_AWAITING_WORKFLOW_INPUT,+ code: AiExceptionCode.THREAD_AWAITING_CALLER_INPUT,});expect(agentChatService.closePendingToolCalls).not.toHaveBeenCalled();expect(agentChatService.addMessage).not.toHaveBeenCalled();diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.tsindex c750b7a85ba..f47e07b72a4 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts@@ -31,7 +31,7 @@ import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.import { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-role.enum';import { AgentMessageStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-status.enum';-import { readToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util';+import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';import { mapDBPartsToUIMessageParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-db-parts-to-ui-message-parts.util';import { type BrowsingContextType } from 'src/engine/metadata-modules/ai/ai-agent/types/browsing-context.type';import { AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';@@ -615,7 +615,7 @@ export class AgentChatStreamingService {}// a message sent while the agent waits on a person closes its pending calls as skipped, so the model sees why- // an answer holding the stream keeps them, and workflow-step calls gate the run, so a chat message never closes them+ // an answer holding the stream keeps them, and calls a caller waits on gate that caller, so a chat message never closes themprivate async settlePendingToolCallsBeforeSending({thread,workspaceId,@@ -632,10 +632,10 @@ export class AgentChatStreamingService {return;}- if (await this.isAwaitingWorkflowStep({ messageId, workspaceId })) {+ if (await this.isAwaitingCaller({ messageId, workspaceId })) {throw new AiException(- 'This conversation is waiting on an answer to its workflow run',- AiExceptionCode.THREAD_AWAITING_WORKFLOW_INPUT,+ 'This conversation is waiting on an answer to the run that asked',+ AiExceptionCode.THREAD_AWAITING_CALLER_INPUT,);}@@ -647,8 +647,8 @@ export class AgentChatStreamingService {});}- // a call a workflow step posted gates that step until it is answered- private async isAwaitingWorkflowStep({+ // a call a caller such as a workflow step waits on gates that caller until it is answered+ private async isAwaitingCaller({messageId,workspaceId,}: {@@ -660,9 +660,7 @@ export class AgentChatStreamingService {select: ['id', 'toolOutput'],});- return parts.some((part) =>- isDefined(readToolCallWorkflowStep(part.toolOutput)),- );+ return parts.some((part) => isToolOutputAwaitedByCaller(part.toolOutput));}private async enqueueStreamJob({diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.tsindex 7fbecf5e0cd..cd19bca4eaf 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts@@ -10,8 +10,7 @@ import { IsNull } from 'typeorm';import { findAwaitingPausingToolParts } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/find-awaiting-pausing-tool-parts.util';import { mapAiStepsToUIMessageParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-ai-steps-to-ui-message-parts.util';import { mapUIMessagePartsToDBParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-ui-message-parts-to-db-parts.util';-import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';-import { stampPendingToolPartsWithWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-with-workflow-step.util';+import { stampPendingToolPartsAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util';import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-role.enum';import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';@@ -216,7 +215,7 @@ export class AgentConversationWriterService {turnId,agentId,execution,- workflowStep,+ isAwaitedByCaller = false,scope,}: {workspaceId: string;@@ -224,18 +223,16 @@ export class AgentConversationWriterService {turnId: string;agentId: string | null;execution: RecordableAgentExecution;- workflowStep?: ToolCallWorkflowStep;+ // a caller such as a workflow step waits on the calls the run pauses on+ isAwaitedByCaller?: boolean;scope?: AgentHistoryTransactionScope;}): Promise<{isAwaitingAnswer: boolean;replyParts: ExtendedUIMessagePart[];}> {const mappedReplyParts = mapAiStepsToUIMessageParts(execution.steps ?? []);- const replyParts = isDefined(workflowStep)- ? stampPendingToolPartsWithWorkflowStep({- parts: mappedReplyParts,- workflowStep,- })+ const replyParts = isAwaitedByCaller+ ? stampPendingToolPartsAwaitedByCaller(mappedReplyParts): mappedReplyParts;if (replyParts.length === 0) {diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type.tsdeleted file mode 100644index e0f6a4cce44..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type.ts+++ /dev/null@@ -1,4 +0,0 @@-export type ToolCallWorkflowStep = {- workflowRunId: string;- stepId: string;-};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.tsnew file mode 100644index 00000000000..51c863b7841--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.ts@@ -0,0 +1,33 @@+import { type ExtendedUIMessagePart } from 'twenty-shared/ai';++import { stampPendingToolPartsAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util';++const toolPart = (status: string) =>+ ({+ type: 'tool-ask_question',+ toolCallId: `call-${status}`,+ state: 'output-available',+ input: {},+ output: { result: { status } },+ }) as unknown as ExtendedUIMessagePart;++describe('stampPendingToolPartsAwaitedByCaller', () => {+ it('marks pending calls only', () => {+ const textPart = { type: 'text', text: 'Hello' } as ExtendedUIMessagePart;++ expect(+ stampPendingToolPartsAwaitedByCaller([+ textPart,+ toolPart('pending'),+ toolPart('answered'),+ ]),+ ).toEqual([+ textPart,+ {+ ...toolPart('pending'),+ output: { result: { status: 'pending' }, awaitedByCaller: true },+ },+ toolPart('answered'),+ ]);+ });+});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-with-workflow-step.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-with-workflow-step.util.spec.tsdeleted file mode 100644index 6650c5b6070..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-with-workflow-step.util.spec.ts+++ /dev/null@@ -1,34 +0,0 @@-import { type ExtendedUIMessagePart } from 'twenty-shared/ai';--import { stampPendingToolPartsWithWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-with-workflow-step.util';--const WORKFLOW_STEP = { workflowRunId: 'run-id', stepId: 'step-id' };--const toolPart = (status: string) =>- ({- type: 'tool-ask_question',- toolCallId: `call-${status}`,- state: 'output-available',- input: {},- output: { result: { status } },- }) as unknown as ExtendedUIMessagePart;--describe('stampPendingToolPartsWithWorkflowStep', () => {- it('names the waiting step on pending calls only', () => {- const textPart = { type: 'text', text: 'Hello' } as ExtendedUIMessagePart;-- expect(- stampPendingToolPartsWithWorkflowStep({- parts: [textPart, toolPart('pending'), toolPart('answered')],- workflowStep: WORKFLOW_STEP,- }),- ).toEqual([- textPart,- {- ...toolPart('pending'),- output: { result: { status: 'pending' }, workflowStep: WORKFLOW_STEP },- },- toolPart('answered'),- ]);- });-});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util.tsdeleted file mode 100644index 00258535f42..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util.ts+++ /dev/null@@ -1,23 +0,0 @@-import { isNonEmptyString } from '@sniptt/guards';-import { isPlainObject } from 'twenty-shared/utils';--import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';--// a workflow step posting a call to a member's inbox records itself in the pending output,-// which the server writes and the answer replaces, so the answer can resume that step-export const readToolCallWorkflowStep = (- toolOutput: unknown,-): ToolCallWorkflowStep | null => {- const workflowStep = isPlainObject(toolOutput)- ? toolOutput.workflowStep- : undefined;-- return isPlainObject(workflowStep) &&- isNonEmptyString(workflowStep.workflowRunId) &&- isNonEmptyString(workflowStep.stepId)- ? {- workflowRunId: workflowStep.workflowRunId,- stepId: workflowStep.stepId,- }- : null;-};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.tsnew file mode 100644index 00000000000..9f2f39b86d9--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.ts@@ -0,0 +1,21 @@+import { isToolUIPart } from 'ai';+import { type ExtendedUIMessagePart } from 'twenty-shared/ai';+import { isPlainObject } from 'twenty-shared/utils';++import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';++// A pending call a caller waits on is marked, so a chat message cannot close it and its+// answer goes to the caller instead of starting a chat turn. The answer replaces the mark+export const stampPendingToolPartsAwaitedByCaller = (+ parts: ExtendedUIMessagePart[],+): ExtendedUIMessagePart[] =>+ parts.map((part) =>+ isToolUIPart(part) &&+ isPlainObject(part.output) &&+ isAwaitingPausingToolOutput(part.output)+ ? ({+ ...part,+ output: { ...part.output, awaitedByCaller: true },+ } as ExtendedUIMessagePart)+ : part,+ );diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-with-workflow-step.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-with-workflow-step.util.tsdeleted file mode 100644index b1839054e31..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-with-workflow-step.util.ts+++ /dev/null@@ -1,26 +0,0 @@-import { isToolUIPart } from 'ai';-import { type ExtendedUIMessagePart } from 'twenty-shared/ai';-import { isPlainObject } from 'twenty-shared/utils';--import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';-import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';--// A pending call names the step that waits on it, so its answer or outcome-// resumes that step even in a conversation other runs also write to-export const stampPendingToolPartsWithWorkflowStep = ({- parts,- workflowStep,-}: {- parts: ExtendedUIMessagePart[];- workflowStep: ToolCallWorkflowStep;-}): ExtendedUIMessagePart[] =>- parts.map((part) =>- isToolUIPart(part) &&- isPlainObject(part.output) &&- isAwaitingPausingToolOutput(part.output)- ? ({- ...part,- output: { ...part.output, workflowStep },- } as ExtendedUIMessagePart)- : part,- );diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.tsindex 91a448f0e4b..801026dad76 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts@@ -5,13 +5,12 @@ import { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-import { AgentChatStreamStateModule } from 'src/engine/metadata-modules/ai/ai-chat/agent-chat-stream-state.module';import { AiChatModule } from 'src/engine/metadata-modules/ai/ai-chat/ai-chat.module';import { AgentHistoryModule } from 'src/engine/metadata-modules/ai/ai-history/ai-history.module';-import { AwaitedToolCallHandlerModule } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/awaited-tool-call-handler.module';import { ToolCallAnswerResolver } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/resolvers/tool-call-answer.resolver';import { ToolCallAnswerService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service';import { AiGraphqlApiExceptionInterceptor } from 'src/engine/metadata-modules/ai/interceptors/ai-graphql-api-exception.interceptor';import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permissions.module';-// an answer resumes a chat here, or the run waiting on it through the handler it registered+// an answer resumes a chat here, or continues the suspended run its caller waits on@Module({imports: [AgentHistoryModule,@@ -20,7 +19,6 @@ import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permiAgentChatStreamStateModule,PermissionsModule,ToolProviderModule,- AwaitedToolCallHandlerModule,],providers: [ToolCallAnswerService,diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/awaited-tool-call-handler.module.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/awaited-tool-call-handler.module.tsdeleted file mode 100644index ec45051cf2e..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/awaited-tool-call-handler.module.ts+++ /dev/null@@ -1,9 +0,0 @@-import { Module } from '@nestjs/common';--import { AwaitedToolCallHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service';--@Module({- providers: [AwaitedToolCallHandlerRegistryService],- exports: [AwaitedToolCallHandlerRegistryService],-})-export class AwaitedToolCallHandlerModule {}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/resolvers/tool-call-answer.resolver.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/resolvers/tool-call-answer.resolver.tsindex 6883d815f0c..96555b1437d 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/resolvers/tool-call-answer.resolver.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/resolvers/tool-call-answer.resolver.ts@@ -27,7 +27,7 @@ import { AnswerToolCallInput } from 'src/engine/metadata-modules/ai/ai-tool-callimport { ToolCallAnswerService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service';import { PermissionsGraphqlApiExceptionFilter } from 'src/engine/metadata-modules/permissions/utils/permissions-graphql-api-exception.filter';-// permission depends on whether the call waits in a chat or a workflow run, so no class guard+// permission depends on whether the call waits in a chat or on a caller such as a workflow run, so no class guard@CoreResolver()@UsePipes(ResolverValidationPipe)@UseGuards(diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service.tsdeleted file mode 100644index cba8d69f351..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service.ts+++ /dev/null@@ -1,30 +0,0 @@-import { Injectable } from '@nestjs/common';--import { isDefined } from 'twenty-shared/utils';--import {- AiException,- AiExceptionCode,-} from 'src/engine/metadata-modules/ai/ai.exception';-import { type AwaitedToolCallHandler } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type';--// the workflow module registers here so the engine answers calls a run waits on without importing it-@Injectable()-export class AwaitedToolCallHandlerRegistryService {- private handler: AwaitedToolCallHandler | null = null;-- register(handler: AwaitedToolCallHandler): void {- this.handler = handler;- }-- getHandlerOrThrow(): AwaitedToolCallHandler {- if (!isDefined(this.handler)) {- throw new AiException(- 'No handler is registered for tool calls a run waits on',- AiExceptionCode.AGENT_EXECUTION_FAILED,- );- }-- return this.handler;- }-}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.tsindex 61c377b67df..8598811a7a5 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts@@ -23,7 +23,6 @@ import { AgentChatThreadService } from 'src/engine/metadata-modules/ai/ai-chat/simport { AgentChatTurnPreflightService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-turn-preflight.service';import { AgentChatService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service';import { formatErrorWithCause } from 'src/engine/metadata-modules/ai/ai-chat/utils/format-error-with-cause.util';-import { readToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-workflow-step.util';import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';import { type AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';@@ -32,9 +31,9 @@ import {AiExceptionCode,} from 'src/engine/metadata-modules/ai/ai.exception';import { PermissionsService } from 'src/engine/metadata-modules/permissions/permissions.service';-import { AwaitedToolCallHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service';-import { type AwaitedToolCallWaiter } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type';-import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';+import { type AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';+import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';type AnswerToolCallArgs = {@@ -66,7 +65,7 @@ export class ToolCallAnswerService {private readonly threadService: AgentChatThreadService,private readonly actorService: AgentChatActorService,private readonly eventPublisherService: AgentChatEventPublisherService,- private readonly awaitedToolCallHandlerRegistry: AwaitedToolCallHandlerRegistryService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,private readonly permissionsService: PermissionsService,private readonly turnPreflightService: AgentChatTurnPreflightService,private readonly agentActorContextService: AgentActorContextService,@@ -97,9 +96,9 @@ export class ToolCallAnswerService {);}- // a workflow step that posted the call waits on it, and the answer resumes that step. An answer- // replaces that output, so a call no longer pending starts nothing and is refused below- const workflowStep = readToolCallWorkflowStep(toolPart.toolOutput);+ // a caller such as a workflow step that waits on the call takes the answer instead of a chat turn.+ // An answer replaces that output, so a call no longer pending starts nothing and is refused below+ const isAwaitedByCaller = isToolOutputAwaitedByCaller(toolPart.toolOutput);const isToolCallPending =thread.pendingQuestionMessageId === toolPart.messageId &&isAwaitingPausingToolOutput(toolPart.toolOutput);@@ -107,7 +106,7 @@ export class ToolCallAnswerService {await this.assertCanAnswerInChat({...args,messageId: toolPart.messageId,- isStartingChatTurn: !isDefined(workflowStep) && isToolCallPending,+ isStartingChatTurn: !isAwaitedByCaller && isToolCallPending,});if (!isToolCallPending) {@@ -146,9 +145,7 @@ export class ToolCallAnswerService {);}- let resumeWaiter:- | Extract<AwaitedToolCallWaiter, { status: 'ready' }>['resume']- | null = null;+ let awaitingSuspension: AgentRunSuspensionEntity | null = null;let isClaimedAsRunning = false;let isLastAnswer: boolean;let answerText: string;@@ -165,32 +162,39 @@ export class ToolCallAnswerService {throw this.notPending();}- if (isDefined(workflowStep)) {- const waiter = await this.awaitedToolCallHandlerRegistry- .getHandlerOrThrow()- .findWaiter({ workspaceId, threadId, workflowStep });+ if (isAwaitedByCaller) {+ const waitingState =+ await this.agentRunSuspensionService.findWaitingSuspension({+ workspaceId,+ threadId,+ });- if (waiter.status === 'not_ready') {+ if (waitingState.status === 'NOT_READY') {throw new AiException(- 'The workflow step is not ready for an answer yet',+ 'The run waiting on this call is not ready for an answer yet',AiExceptionCode.TOOL_CALL_NOT_PENDING,{- userFriendlyMessage: msg`This workflow is still getting ready. Try again in a moment.`,+ userFriendlyMessage: msg`This request is still getting ready. Try again in a moment.`,},);}- if (waiter.status === 'gone') {+ if (waitingState.status === 'GONE') {await this.agentChatService.closePendingToolCalls({threadId,messageId: toolPart.messageId,workspaceId,});+ await this.agentRunSuspensionService.release({+ workspaceId,+ suspension: waitingState.suspension,+ });+throw this.notPending();}- resumeWaiter = waiter.resume;+ awaitingSuspension = waitingState.suspension;}const runningToolResult = pausingToolCall.toRunningToolResult?.(@@ -234,29 +238,27 @@ export class ToolCallAnswerService {answerText = completion.answerText;toolResult = completion.toolResult;} catch (error) {- // a call claimed as running cannot be answered again, so the run or turn fails, which- // closes the call as interrupted instead of leaving it waiting on an outcome+ // a call claimed as running cannot be answered again, so it closes as interrupted and the+ // run or turn fails, instead of leaving the call waiting on an outcomeif (isClaimedAsRunning) {- if (!isDefined(workflowStep)) {- await this.agentChatService- .closePendingToolCalls({- threadId,- messageId: toolPart.messageId,- workspaceId,- where: { activeStreamId: streamId },- })- .catch((closeError: unknown) =>- this.logger.warn(- `Could not close the interrupted call on thread ${threadId}: ${closeError instanceof Error ? closeError.message : String(closeError)}`,- ),- );- }+ await this.agentChatService+ .closePendingToolCalls({+ threadId,+ messageId: toolPart.messageId,+ workspaceId,+ where: { activeStreamId: streamId },+ })+ .catch((closeError: unknown) =>+ this.logger.warn(+ `Could not close the interrupted call on thread ${threadId}: ${closeError instanceof Error ? closeError.message : String(closeError)}`,+ ),+ );await this.failAfterAnswer({threadId,workspaceId,streamId,- workflowStep,+ awaitingSuspension,error,});} else {@@ -276,7 +278,7 @@ export class ToolCallAnswerService {threadId,userWorkspaceId,turnStatus:- isDefined(resumeWaiter) || !isLastAnswer+ isAwaitedByCaller || !isLastAnswer? AgentTurnStatus.COMPLETED: AgentTurnStatus.RUNNING,uiMessage: {@@ -304,16 +306,20 @@ export class ToolCallAnswerService {),);- // runs resume in their own executor, and only the last answer resumes a chat- if (isDefined(resumeWaiter) || !isLastAnswer) {+ // a suspended run continues in its own job, and only the last answer resumes a chat+ if (isAwaitedByCaller || !isLastAnswer) {await this.streamRecoveryService.releaseStreamClaim({threadId,workspaceId,streamId,});- if (isLastAnswer && isDefined(resumeWaiter)) {- await resumeWaiter(toolResult);+ if (isLastAnswer && isDefined(awaitingSuspension)) {+ await this.agentRunSuspensionService.deliverAnswer({+ workspaceId,+ suspension: awaitingSuspension,+ toolResult,+ });}return { streamId: null, turnId: answerMessage.turnId };@@ -336,7 +342,7 @@ export class ToolCallAnswerService {threadId,workspaceId,streamId,- workflowStep,+ awaitingSuspension,error,});@@ -348,16 +354,16 @@ export class ToolCallAnswerService {threadId,workspaceId,streamId,- workflowStep,+ awaitingSuspension,error,}: {threadId: string;workspaceId: string;streamId: string;- workflowStep: ToolCallWorkflowStep | null;+ awaitingSuspension: AgentRunSuspensionEntity | null;error: unknown;}): Promise<void> {- if (!isDefined(workflowStep)) {+ if (!isDefined(awaitingSuspension)) {await this.streamRecoveryService.failStream({threadId,workspaceId,@@ -373,13 +379,18 @@ export class ToolCallAnswerService {workspaceId,streamId,});- await this.awaitedToolCallHandlerRegistry- .getHandlerOrThrow()- .failWaiter({ workspaceId, workflowStep });+ await this.agentRunSuspensionService.settle({+ workspaceId,+ suspension: awaitingSuspension,+ outcome: {+ status: 'FAILED',+ error: 'The run could not resume after its question was answered',+ },+ });}- // an answer that resumes a workflow step starts no chat turn, so it skips the model and- // billing checks but still needs a thread the member can write to+ // an answer a caller such as a workflow step waits on starts no chat turn, so it skips the+ // model and billing checks but still needs a thread the member can write toprivate async assertCanAnswerInChat({threadId,messageId,diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type.tsdeleted file mode 100644index 8e0ff5b4392..00000000000--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type.ts+++ /dev/null@@ -1,21 +0,0 @@-import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';--export type AwaitedToolCallWaiter =- | {- status: 'ready';- resume: (toolResult: Record<string, unknown>) => Promise<void>;- }- | { status: 'not_ready' }- | { status: 'gone' };--export type AwaitedToolCallHandler = {- findWaiter: (args: {- workspaceId: string;- threadId: string;- workflowStep: ToolCallWorkflowStep;- }) => Promise<AwaitedToolCallWaiter>;- failWaiter: (args: {- workspaceId: string;- workflowStep: ToolCallWorkflowStep;- }) => Promise<void>;-};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai.exception.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai.exception.tsindex 84cea04e1d2..c0487d41707 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai.exception.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai.exception.ts@@ -32,7 +32,7 @@ export enum AiExceptionCode {EVALUATION_QUESTION_UNSUPPORTED = 'EVALUATION_QUESTION_UNSUPPORTED',INVALID_EVALUATION_REQUEST = 'INVALID_EVALUATION_REQUEST',TOOL_CALL_RESOLUTION_FORBIDDEN = 'TOOL_CALL_RESOLUTION_FORBIDDEN',- THREAD_AWAITING_WORKFLOW_INPUT = 'THREAD_AWAITING_WORKFLOW_INPUT',+ THREAD_AWAITING_CALLER_INPUT = 'THREAD_AWAITING_CALLER_INPUT',THREAD_AWAITING_ANSWER = 'THREAD_AWAITING_ANSWER',}@@ -92,8 +92,8 @@ const getAiExceptionUserFriendlyMessage = (code: AiExceptionCode) => {return msg`Invalid classification request.`;case AiExceptionCode.TOOL_CALL_RESOLUTION_FORBIDDEN:return msg`You are not allowed to answer this request.`;- case AiExceptionCode.THREAD_AWAITING_WORKFLOW_INPUT:- return msg`This workflow is waiting for an answer. Answer it before sending a message.`;+ case AiExceptionCode.THREAD_AWAITING_CALLER_INPUT:+ return msg`This conversation is waiting for an answer. Answer it before sending a message.`;case AiExceptionCode.THREAD_AWAITING_ANSWER:return msg`This conversation is waiting for an answer to an earlier request.`;default:diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/utils/ai-graphql-api-exception-handler.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/utils/ai-graphql-api-exception-handler.util.tsindex 35cbdb9353d..6fa49188e9c 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/utils/ai-graphql-api-exception-handler.util.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/utils/ai-graphql-api-exception-handler.util.ts@@ -35,7 +35,7 @@ export const aiGraphqlApiExceptionHandler = (error: Error) => {throw new UserInputError(error);case AiExceptionCode.AGENT_ALREADY_EXISTS:case AiExceptionCode.NO_FAILED_TURN_TO_RETRY:- case AiExceptionCode.THREAD_AWAITING_WORKFLOW_INPUT:+ case AiExceptionCode.THREAD_AWAITING_CALLER_INPUT:case AiExceptionCode.CHAT_THREAD_INBOX_STATE_UNAVAILABLE:case AiExceptionCode.THREAD_AWAITING_ANSWER:throw new ConflictError(error);diff --git a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.tsindex 144872f15dc..d8f9f2ebe03 100644--- a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts+++ b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts@@ -11,6 +11,7 @@ import {} from 'src/engine/core-modules/workflow/entities/workflow-version.entity';import { WorkflowEntity } from 'src/engine/core-modules/workflow/entities/workflow.entity';import { AgentRunConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import {AiException,AiExceptionCode,@@ -31,6 +32,8 @@ import {type WorkflowWorkspaceEntity,} from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';import { buildWorkflowStepCaller } from 'src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util';+import { isWorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/guards/is-workflow-ai-agent-action.guard';+import { buildWorkflowAgentRunSpec } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util';import { WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';@@ -172,6 +175,7 @@ export class DevSeederWorkflowPendingInputWorkspaceService {private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,private readonly workflowAgentConversationService: WorkflowAgentConversationWorkspaceService,private readonly agentRunConversationService: AgentRunConversationService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,private readonly workspaceOrmManager: WorkspaceOrmManager,@InjectWorkspaceScopedRepository(WorkflowEntity)private readonly coreWorkflowRepository: WorkspaceScopedRepository<WorkflowEntity>,@@ -285,16 +289,29 @@ export class DevSeederWorkflowPendingInputWorkspaceService {}),});- await this.agentRunConversationService.closeTurn({+ await this.agentRunSuspensionService.suspend({workspaceId,threadId,- turnId,caller: buildWorkflowStepCaller({workflowRunId,stepId: workflow.step.id,}),+ runSpec: isWorkflowAiAgentAction(workflow.step)+ ? buildWorkflowAgentRunSpec({+ step: workflow.step,+ isApplicationBound: false,+ })+ : null,+ summary: null,+ });++ await this.agentRunConversationService.closeTurn({+ workspaceId,+ threadId,+ turnId,title: workflow.step.name,agentId: null,+ isAwaitedByCaller: true,execution: {isPaused: true,steps: [{ content }],@@ -346,7 +363,7 @@ export class DevSeederWorkflowPendingInputWorkspaceService {await this.workflowRunWorkspaceService.updateWorkflowRunStepInfo({stepId: workflow.step.id,- stepInfo: { status: StepStatus.PENDING },+ stepInfo: { status: StepStatus.PENDING, wait: { type: 'CALLBACK' } },workflowRunId,workspaceId,});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.tsindex 92becd8fefc..f5aa422c817 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts@@ -1,15 +1,8 @@import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';-import { type WorkflowWaitResolution } from 'src/modules/workflow/workflow-wait/types/workflow-wait-resolution.type';-import { type WorkflowWaitResolutionInput } from 'src/modules/workflow/workflow-wait/types/workflow-wait-resolution-input.type';export interface WorkflowAction {execute(workflowActionInput: WorkflowActionInput,): Promise<WorkflowActionOutput>;-- // Without it, a resolved time or event wait completes the step with the outcome as its result- resolveWait?(- workflowWaitResolutionInput: WorkflowWaitResolutionInput,- ): Promise<WorkflowWaitResolution>;}diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-input.type.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-input.type.tsindex c9efd468248..02bcc1f4d80 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-input.type.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-action-input.type.ts@@ -1,5 +1,3 @@-import { type WorkflowRunStepLog } from 'twenty-shared/workflow';-import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';export type WorkflowRunInfo = {@@ -12,7 +10,4 @@ export type WorkflowActionInput = {steps: WorkflowAction[];context: Record<string, unknown>;runInfo: WorkflowRunInfo;- // Set only when the step resumes after its question was answered- resumedThreadId?: string;- previousStepLog?: WorkflowRunStepLog;};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-executor-input.type.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-executor-input.type.tsindex f93e54f525a..40e21968783 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-executor-input.type.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-executor-input.type.ts@@ -1,11 +1,13 @@+import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+export type WorkflowExecutorInput = {stepIds: string[];workflowRunId: string;workspaceId: string;shouldComputeWorkflowRunStatus?: boolean;executedStepsCount?: number;- // The conversation the resumed step continues; set only when stepIds is that one step, already claimed out of PENDING- resumedThreadId?: string;+ // What a step that waited on a callback was handed; set only when stepIds is that one step, already claimed out of PENDING+ awaitedActionOutput?: WorkflowActionOutput;};export type WorkflowBranchExecutorInput = {@@ -14,5 +16,5 @@ export type WorkflowBranchExecutorInput = {workflowRunId: string;workspaceId: string;executedStepsCount?: number;- resumedThreadId?: string;+ awaitedActionOutput?: WorkflowActionOutput;};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.tsindex 64e9e5c9101..8a27a5a6359 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/__tests__/build-step-infos-reset.util.spec.ts@@ -38,20 +38,6 @@ describe('buildStepInfosReset', () => {});});- it('should move the conversation to history so the next iteration starts without it', () => {- const result = buildStepInfosReset({- stepIds: ['agentStep'],- stepInfos: {- agentStep: { status: StepStatus.SUCCESS, threadId: 'thread-1' },- },- });-- expect(result.agentStep).toHaveProperty('threadId', undefined);- expect(result.agentStep.history).toEqual([- expect.objectContaining({ threadId: 'thread-1' }),- ]);- });-it('should leave out steps that were not given', () => {const result = buildStepInfosReset({stepIds: ['step1'],diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.tsindex e158c80a20c..b5b3ba07cf0 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-step-infos-reset.util.ts@@ -20,15 +20,12 @@ export const buildStepInfosReset = ({status: StepStatus.NOT_STARTED,result: undefined,error: undefined,- // Step infos are merged on write, so an omitted value would keep the previous iteration's conversation- threadId: undefined,history: [...(stepInfo.history ?? []),{result: stepInfo.result,error: stepInfo.error,status: stepInfo.status,- threadId: stepInfo.threadId,},],};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/ai-agent.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/ai-agent.workflow-action.tsindex f9e0ca0cf6b..5d2c25ce697 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/ai-agent.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/ai-agent.workflow-action.ts@@ -1,24 +1,12 @@-import { Injectable, Logger } from '@nestjs/common';+import { Injectable } from '@nestjs/common';import { isNonEmptyString, isString } from '@sniptt/guards';--import {- ASK_QUESTION_TOOL_NAME,- REQUEST_FORM_TOOL_NAME,-} from 'twenty-shared/ai';import { isDefined, isValidUuid, resolveInput } from 'twenty-shared/utils';-import { type WorkflowRunStepLog } from 'twenty-shared/workflow';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/interfaces/workflow-action.interface';-import { AgentCallerConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service';import { AgentRunnerService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service';-import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';-import { createAskQuestionTool } from 'src/engine/metadata-modules/ai/ai-chat/tools/ask-question.tool';-import { createRequestFormTool } from 'src/engine/metadata-modules/ai/ai-chat/tools/request-form.tool';-import { WORKFLOW_BASE_SYSTEM_PROMPT } from 'src/engine/metadata-modules/ai/ai-agent/constants/workflow-base-system-prompt.const';import { AgentEntity } from 'src/engine/metadata-modules/ai/ai-agent/entities/agent.entity';-import { getRoleIdsFromRolePermissionConfig } from 'src/engine/twenty-orm/utils/get-role-ids-from-role-permission-config.util';import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';import {@@ -32,30 +20,20 @@ import { buildWorkflowStepCaller } from 'src/modules/workflow/workflow-executor/import { buildStepExecutionKey } from 'src/modules/workflow/workflow-executor/utils/build-step-execution-key.util';import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';import { resolveConversationThreadKey } from 'src/modules/workflow/workflow-executor/utils/resolve-conversation-thread-key.util';-import { APPLICATION_BOUND_AGENT_EXCLUDED_TOOL_NAMES } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/application-bound-agent-excluded-tool-names.constant';-import { WORKFLOW_AGENT_WAIT_PROMPT } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-prompt.constant';-import { WORKFLOW_AGENT_WAIT_TOOL_NAMES } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant';-import { createWorkflowAgentWaitTools } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-workflow-agent-wait-tools.util';-import { buildWaitOutcomeToolOutput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-wait-outcome-tool-output.util';-import { findAgentStepWait } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/find-agent-step-wait.util';-import { type WorkflowWaitResolution } from 'src/modules/workflow/workflow-wait/types/workflow-wait-resolution.type';-import { type WorkflowWaitResolutionInput } from 'src/modules/workflow/workflow-wait/types/workflow-wait-resolution-input.type';-import { WORKFLOW_AGENT_HUMAN_INPUT_PROMPT } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-human-input-prompt.constant';import { WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';import { type WorkflowAiAgentActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/types/workflow-ai-agent-action-input.type';-import { mergeAiAgentStepLogs } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/merge-ai-agent-step-logs.util';-import { buildAiAgentStepLog } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util';+import { buildWorkflowAgentRunExecutionContext } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-execution-context.util';+import { buildWorkflowAgentRunSpec } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util';import { WorkflowRunStepLogWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service';import { isWorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/guards/is-workflow-ai-agent-action.guard';+// Starts the step's agent and hands it to the engine: an agent that pauses is continued by the+// engine, which calls the step back with its outcome@Injectable()export class AiAgentWorkflowAction implements WorkflowAction {- private readonly logger = new Logger(AiAgentWorkflowAction.name);-constructor(private readonly agentRunnerService: AgentRunnerService,- private readonly agentCallerConversationService: AgentCallerConversationService,private readonly workflowExecutionContextService: WorkflowExecutionContextService,private readonly workflowRunStepLogService: WorkflowRunStepLogWorkspaceService,private readonly workflowAgentConversationService: WorkflowAgentConversationWorkspaceService,@@ -68,8 +46,6 @@ export class AiAgentWorkflowAction implements WorkflowAction {steps,context,runInfo,- resumedThreadId,- previousStepLog,}: WorkflowActionInput): Promise<WorkflowActionOutput> {const step = findStepOrThrow({stepId: currentStepId,@@ -83,7 +59,7 @@ export class AiAgentWorkflowAction implements WorkflowAction {);}- const { agentId, prompt, humanInputInstructions } = step.settings.input;+ const { agentId, prompt } = step.settings.input;const workspaceId = runInfo.workspaceId;const {workspaceMemberId: recipientWorkspaceMemberId,@@ -142,191 +118,59 @@ export class AiAgentWorkflowAction implements WorkflowAction {);}- const userWorkspaceId =- executionContext.authContext.type === 'user'- ? executionContext.authContext.userWorkspaceId- : null;-- const resolvedPrompt = resolveInput(prompt, context) as string | undefined;-- // a resumed step continues where it paused, any other opens the conversation its key names- const conversation = isDefined(resumedThreadId)- ? { threadId: resumedThreadId, isCreated: false }- : await this.workflowAgentConversationService.openConversation({- runInfo,- stepId: currentStepId,- title: step.name,- recipientWorkspaceMemberId: isNonEmptyString(- recipientWorkspaceMemberId,- )- ? recipientWorkspaceMemberId- : null,- threadKey: resolveConversationThreadKey({- conversation: conversationSettings,- defaultScope: 'STEP',- workflowRunId: runInfo.workflowRunId,- stepExecutionKey: buildStepExecutionKey({- stepId: currentStepId,- steps,- context,- }),+ const conversation =+ await this.workflowAgentConversationService.openConversation({+ runInfo,+ stepId: currentStepId,+ title: step.name,+ recipientWorkspaceMemberId: isNonEmptyString(recipientWorkspaceMemberId)+ ? recipientWorkspaceMemberId+ : null,+ threadKey: resolveConversationThreadKey({+ conversation: conversationSettings,+ defaultScope: 'STEP',+ workflowRunId: runInfo.workflowRunId,+ stepExecutionKey: buildStepExecutionKey({+ stepId: currentStepId,+ steps,+ context,}),- });-- const trimmedHumanInputInstructions = humanInputInstructions?.trim();- const canAskForHumanInput = isNonEmptyString(trimmedHumanInputInstructions);-- // a resumed run continues from its answer, which is already the last message- const messages =- isDefined(resumedThreadId) || !isDefined(resolvedPrompt)- ? []- : [{ role: 'user' as const, content: resolvedPrompt }];+ }),+ });- const { outcome, summary } = await this.agentRunnerService.run({+ const { threadId, outcome, summary } = await this.agentRunnerService.run({workspaceId,conversation,caller: buildWorkflowStepCaller({workflowRunId: runInfo.workflowRunId,stepId: currentStepId,}),- turn: {- title: step.name,- senderUserWorkspaceId: userWorkspaceId,- senderApplicationId: null,- messages,- resolveCreatedBy: () =>- this.workflowAgentConversationService.findTurnCreatedBy(runInfo),- },- execution: {- agent,- messages,- baseSystemPrompt: canAskForHumanInput- ? `${WORKFLOW_BASE_SYSTEM_PROMPT}\n\n${WORKFLOW_AGENT_WAIT_PROMPT}\n\n${WORKFLOW_AGENT_HUMAN_INPUT_PROMPT}\n\n${trimmedHumanInputInstructions}`- : `${WORKFLOW_BASE_SYSTEM_PROMPT}\n\n${WORKFLOW_AGENT_WAIT_PROMPT}`,- pausingTools: {- ...createWorkflowAgentWaitTools(),- ...(canAskForHumanInput- ? {- [ASK_QUESTION_TOOL_NAME]: createAskQuestionTool({- isWorkspaceSetupThread: false,- }),- [REQUEST_FORM_TOOL_NAME]: createRequestFormTool(),- }- : {}),- },- canProposeToolCalls: canAskForHumanInput,- actorContext: executionContext.isActingOnBehalfOfUser- ? executionContext.initiator- : undefined,- authContext: executionContext.authContext,- workspaceId,- userWorkspaceId,- ...(isDefined(application)- ? {- additionalRoleRestrictionIds: getRoleIdsFromRolePermissionConfig(- executionContext.rolePermissionConfig,- ),- additionalExcludedToolNames:- APPLICATION_BOUND_AGENT_EXCLUDED_TOOL_NAMES,- }- : {}),- },+ spec: buildWorkflowAgentRunSpec({+ step,+ isApplicationBound: isDefined(application),+ }),+ agent,+ prompt: (resolveInput(prompt, context) as string | undefined) ?? null,+ executionContext: buildWorkflowAgentRunExecutionContext(executionContext),+ resolveCreatedBy: () =>+ this.workflowAgentConversationService.findTurnCreatedBy(runInfo),});- await this.persistStepLog({+ await this.workflowRunStepLogService.setAiAgentStepLog({workflowRunId: runInfo.workflowRunId,workspaceId,stepId: currentStepId,summary,- previousStepLog,+ threadId,});switch (outcome.status) {- case 'NO_CREDITS':- return {- error: 'Agent stopped: no more available credits.',- };- case 'AWAITING_ANSWER':- return { wait: { type: 'ANSWER' } };- case 'PAUSED': {- const wait = findAgentStepWait(outcome.pausedToolResults);-- // Resuming continues the conversation, so without it the run would wait forever- if (isDefined(wait) && outcome.isResumable) {- return { wait };- }-- return {- error: isDefined(wait)- ? 'Agent paused to wait but its conversation could not be recorded.'- : 'Agent asked a question that could not be recorded.',- };- }case 'COMPLETED':return { result: outcome.result };- }- }-- async resolveWait({- step,- stepInfo,- outcome,- runInfo,- }: WorkflowWaitResolutionInput): Promise<WorkflowWaitResolution> {- const threadId = stepInfo.threadId;-- if (!isDefined(threadId)) {- throw new WorkflowStepExecutorException(- 'A waiting agent step has no conversation to continue',- WorkflowStepExecutorExceptionCode.INTERNAL_ERROR,- );- }-- await this.agentCallerConversationService.recordWaitOutcome({- workspaceId: runInfo.workspaceId,- threadId,- caller: buildWorkflowStepCaller({- workflowRunId: runInfo.workflowRunId,- stepId: step.id,- }),- toolNames: WORKFLOW_AGENT_WAIT_TOOL_NAMES,- toolOutput: buildWaitOutcomeToolOutput(outcome),- });-- return { resumedThreadId: threadId };- }-- private async persistStepLog({- workflowRunId,- workspaceId,- stepId,- summary,- previousStepLog,- }: {- workflowRunId: string;- workspaceId: string;- stepId: string;- summary: AgentRunSummary;- previousStepLog?: WorkflowRunStepLog;- }): Promise<void> {- const stepLog = buildAiAgentStepLog(summary);-- try {- await this.workflowRunStepLogService.setStepLog({- workflowRunId,- workspaceId,- stepId,- stepLog: mergeAiAgentStepLogs({- previousStepLog,- nextStepLog: stepLog,- }),- });- } catch (error) {- this.logger.warn(- `Failed to persist step log for workflowRun=${workflowRunId} step=${stepId}: ${- error instanceof Error ? error.message : String(error)- }`,- );+ case 'SUSPENDED':+ return { wait: { type: 'CALLBACK' } };+ case 'FAILED':+ return { error: outcome.error };}}}diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-prompt.constant.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-prompt.constant.tsdeleted file mode 100644index 357212fac02..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-prompt.constant.ts+++ /dev/null@@ -1 +0,0 @@-export const WORKFLOW_AGENT_WAIT_PROMPT = `When the task depends on something that has not happened yet, you can pause the workflow instead of finishing early: call wait_for_event to resume when a record is created, updated or deleted, or wait_for_duration to resume after some time. Call a wait tool alone and last; you continue in this conversation with its outcome. Only wait when the task requires it.`;diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant.tsdeleted file mode 100644index e7c5f5de9ca..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant.ts+++ /dev/null@@ -1,8 +0,0 @@-export const WAIT_FOR_EVENT_TOOL_NAME = 'wait_for_event';--export const WAIT_FOR_DURATION_TOOL_NAME = 'wait_for_duration';--export const WORKFLOW_AGENT_WAIT_TOOL_NAMES: string[] = [- WAIT_FOR_EVENT_TOOL_NAME,- WAIT_FOR_DURATION_TOOL_NAME,-];diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/__tests__/workflow-agent-conversation.workspace-service.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/__tests__/workflow-agent-conversation.workspace-service.spec.tsindex 5aec8b8c8d4..82d7d898c10 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/__tests__/workflow-agent-conversation.workspace-service.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/__tests__/workflow-agent-conversation.workspace-service.spec.ts@@ -24,9 +24,6 @@ const buildService = () => {isCreated: true,}),};- const workflowRunWorkspaceService = {- setStepThreadId: jest.fn().mockResolvedValue(undefined),- };const workflowRunRecordShareService = {findCreatorWorkspaceMemberId: jest.fn().mockResolvedValue('creator-id'),};@@ -36,25 +33,22 @@ const buildService = () => {{findRunSenderOrThrow: jest.fn().mockResolvedValue(SENDER),} as never,- workflowRunWorkspaceService as never,workflowRunRecordShareService as never,);return {service,agentCallerConversationService,- workflowRunWorkspaceService,workflowRunRecordShareService,};};describe('WorkflowAgentConversationWorkspaceService', () => {describe('openConversation', () => {- it('opens the conversation with the recipient the step names and points the step at it', async () => {+ it('opens the conversation with the recipient the step names', async () => {const {service,agentCallerConversationService,- workflowRunWorkspaceService,workflowRunRecordShareService,} = buildService();@@ -79,12 +73,6 @@ describe('WorkflowAgentConversationWorkspaceService', () => {expect(workflowRunRecordShareService.findCreatorWorkspaceMemberId,).not.toHaveBeenCalled();- expect(workflowRunWorkspaceService.setStepThreadId).toHaveBeenCalledWith({- stepId: 'step-id',- threadId: 'thread-id',- workflowRunId: 'run-id',- workspaceId: 'workspace-id',- });});it("falls back to the workflow creator's inbox when the step names no recipient", async () => {@@ -106,11 +94,7 @@ describe('WorkflowAgentConversationWorkspaceService', () => {});it('fails when the recipient deleted the conversation', async () => {- const {- service,- agentCallerConversationService,- workflowRunWorkspaceService,- } = buildService();+ const { service, agentCallerConversationService } = buildService();agentCallerConversationService.openConversation.mockResolvedValue({status: 'DELETED',@@ -122,9 +106,6 @@ describe('WorkflowAgentConversationWorkspaceService', () => {recipientWorkspaceMemberId: 'recipient-id',}),).rejects.toThrow('The recipient deleted this conversation');- expect(- workflowRunWorkspaceService.setStepThreadId,- ).not.toHaveBeenCalled();});});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service.tsindex af1caca237f..78a1bd8db2a 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service.ts@@ -12,16 +12,13 @@ import {} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';import { WorkflowRunInboxSenderWorkspaceService } from 'src/modules/workflow/workflow-executor/services/workflow-run-inbox-sender.workspace-service';import { type WorkflowRunInfo } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';-import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';-// A step's conversation goes to the step's recipient or else the workflow's-// creator, and the step points at it as soon as it is open+// A step's conversation goes to the step's recipient or else the workflow's creator@Injectable()export class WorkflowAgentConversationWorkspaceService {constructor(private readonly agentCallerConversationService: AgentCallerConversationService,private readonly workflowRunInboxSenderService: WorkflowRunInboxSenderWorkspaceService,- private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,private readonly workflowRunRecordShareService: WorkflowRunRecordShareService,) {}@@ -71,13 +68,6 @@ export class WorkflowAgentConversationWorkspaceService {);}- await this.workflowRunWorkspaceService.setStepThreadId({- stepId,- threadId: openedConversation.threadId,- workflowRunId,- workspaceId,- });-return {threadId: openedConversation.threadId,isCreated: openedConversation.isCreated,diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-wait-pending-output.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-wait-pending-output.util.tsdeleted file mode 100644index c1c21dec422..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/build-wait-pending-output.util.ts+++ /dev/null@@ -1,19 +0,0 @@-import { type WorkflowStepWait } from 'twenty-shared/workflow';--export type WorkflowAgentWaitPendingOutput = {- success: true;- message: string;- result: { status: 'pending'; wait: WorkflowStepWait };-};--export const buildWaitPendingOutput = ({- message,- wait,-}: {- message: string;- wait: WorkflowStepWait;-}): WorkflowAgentWaitPendingOutput => ({- success: true,- message,- result: { status: 'pending', wait },-});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-workflow-agent-wait-tools.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-workflow-agent-wait-tools.util.tsdeleted file mode 100644index e6c0e76a9b2..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-workflow-agent-wait-tools.util.ts+++ /dev/null@@ -1,19 +0,0 @@-import { type ToolSet } from 'ai';--import {- WAIT_FOR_DURATION_TOOL_NAME,- WAIT_FOR_EVENT_TOOL_NAME,-} from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant';-import { createWaitForDurationTool } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-duration.tool';-import { createWaitForEventTool } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/create-wait-for-event.tool';-import { type WorkflowAgentWaitSlot } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type';--// a step resumes on one wait, so a second wait call of the same execution is refused instead of left pending-export const createWorkflowAgentWaitTools = () => {- const waitSlot: WorkflowAgentWaitSlot = { isTaken: false };-- return {- [WAIT_FOR_EVENT_TOOL_NAME]: createWaitForEventTool(waitSlot),- [WAIT_FOR_DURATION_TOOL_NAME]: createWaitForDurationTool(waitSlot),- } satisfies ToolSet;-};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type.tsdeleted file mode 100644index 0303558b8ec..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/tools/workflow-agent-wait-slot.type.ts+++ /dev/null@@ -1 +0,0 @@-export type WorkflowAgentWaitSlot = { isTaken: boolean };diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-ai-agent-step-log.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-ai-agent-step-log.util.spec.tsindex 51199a4cc4b..24385f8ee9d 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-ai-agent-step-log.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-ai-agent-step-log.util.spec.ts@@ -20,9 +20,11 @@ const SUMMARY: AgentRunSummary = {};describe('buildAiAgentStepLog', () => {- it('logs the run summary as the AI_AGENT step details', () => {- expect(buildAiAgentStepLog(SUMMARY)).toEqual({- details: { type: 'AI_AGENT', ...SUMMARY },+ it('logs the run summary and its conversation as the AI_AGENT step details', () => {+ expect(+ buildAiAgentStepLog({ summary: SUMMARY, threadId: 'thread-id' }),+ ).toEqual({+ details: { type: 'AI_AGENT', ...SUMMARY, threadId: 'thread-id' },entries: [],sizeBytes: 0,});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-workflow-agent-run-spec.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-workflow-agent-run-spec.util.spec.tsnew file mode 100644index 00000000000..80c5816e7da--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/build-workflow-agent-run-spec.util.spec.ts@@ -0,0 +1,74 @@+import { WorkflowActionType } from 'twenty-shared/workflow';++import { buildWorkflowAgentRunSpec } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util';+import { type WorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';++const buildStep = (+ input: WorkflowAiAgentAction['settings']['input'],+): WorkflowAiAgentAction =>+ ({+ id: 'step-id',+ name: 'Draft the quote',+ type: WorkflowActionType.AI_AGENT,+ valid: true,+ nextStepIds: [],+ settings: {+ input,+ outputSchema: {},+ errorHandlingOptions: {+ retryOnFailure: { value: 0 },+ continueOnFailure: { value: false },+ },+ },+ }) as WorkflowAiAgentAction;++describe('buildWorkflowAgentRunSpec', () => {+ it('lets the agent wait but not ask without human input instructions', () => {+ const spec = buildWorkflowAgentRunSpec({+ step: buildStep({+ prompt: 'Draft the quote',+ humanInputInstructions: ' ',+ }),+ isApplicationBound: false,+ });++ expect(spec).toMatchObject({+ agentId: null,+ title: 'Draft the quote',+ instructions: null,+ capabilities: {+ canAskHumans: false,+ canProposeToolCalls: false,+ canWait: true,+ },+ });+ expect(spec).not.toHaveProperty('additionalExcludedToolNames');+ });++ it('lets the agent ask humans with the instructions the step gives', () => {+ const spec = buildWorkflowAgentRunSpec({+ step: buildStep({+ agentId: 'agent-id',+ humanInputInstructions: ' Ask before choosing a plan. ',+ }),+ isApplicationBound: false,+ });++ expect(spec.agentId).toBe('agent-id');+ expect(spec.capabilities).toEqual({+ canAskHumans: true,+ canProposeToolCalls: true,+ canWait: true,+ });+ expect(spec.instructions).toMatch(/Ask before choosing a plan\.$/);+ });++ it('keeps tools out of reach of an application-bound run', () => {+ expect(+ buildWorkflowAgentRunSpec({+ step: buildStep({ prompt: 'Draft the quote' }),+ isApplicationBound: true,+ }).additionalExcludedToolNames,+ ).toContain('send_email');+ });+});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/merge-ai-agent-step-logs.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/merge-ai-agent-step-logs.util.spec.tsdeleted file mode 100644index 2220164b3d2..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/__tests__/merge-ai-agent-step-logs.util.spec.ts+++ /dev/null@@ -1,84 +0,0 @@-import { type WorkflowRunStepLog } from 'twenty-shared/workflow';--import { mergeAiAgentStepLogs } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/merge-ai-agent-step-logs.util';--const buildAgentStepLog = ({- modelId,- tokens,- toolCallNames,- durationMs,-}: {- modelId: string;- tokens: number;- toolCallNames: string[];- durationMs: number;-}): WorkflowRunStepLog => ({- details: {- type: 'AI_AGENT',- modelId,- usage: {- inputTokens: tokens,- outputTokens: tokens,- reasoningTokens: tokens,- totalTokens: tokens * 2,- },- cost: { totalCostInDollars: tokens / 100, creditsUsedMicro: tokens },- nativeWebSearchCallCount: 1,- toolCalls: toolCallNames.map((toolName) => ({- toolName,- toolCallId: `${toolName}-call`,- state: 'success',- })),- durationMs,- },- entries: [],- sizeBytes: 0,-});--describe('mergeAiAgentStepLogs', () => {- it('adds the segment after an answer to the one before it', () => {- const merged = mergeAiAgentStepLogs({- previousStepLog: buildAgentStepLog({- modelId: 'model-a',- tokens: 10,- toolCallNames: ['search', 'ask_question'],- durationMs: 100,- }),- nextStepLog: buildAgentStepLog({- modelId: 'model-b',- tokens: 5,- toolCallNames: ['send_email'],- durationMs: 50,- }),- });-- expect(merged.details).toMatchObject({- modelId: 'model-b',- usage: {- inputTokens: 15,- outputTokens: 15,- reasoningTokens: 15,- totalTokens: 30,- },- cost: { totalCostInDollars: expect.closeTo(0.15), creditsUsedMicro: 15 },- nativeWebSearchCallCount: 2,- durationMs: 150,- });- expect(- merged.details.type === 'AI_AGENT'- ? merged.details.toolCalls.map(({ toolName }) => toolName)- : [],- ).toEqual(['search', 'ask_question', 'send_email']);- });-- it('keeps the new log when there is nothing before it', () => {- const nextStepLog = buildAgentStepLog({- modelId: 'model-a',- tokens: 5,- toolCallNames: [],- durationMs: 50,- });-- expect(mergeAiAgentStepLogs({ nextStepLog })).toBe(nextStepLog);- });-});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util.tsindex 066338cebe4..792128f482d 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util.ts@@ -2,10 +2,14 @@ import { type WorkflowRunStepLog } from 'twenty-shared/workflow';import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';-export const buildAiAgentStepLog = (- summary: AgentRunSummary,-): WorkflowRunStepLog => ({- details: { type: 'AI_AGENT', ...summary },+export const buildAiAgentStepLog = ({+ summary,+ threadId,+}: {+ summary: AgentRunSummary;+ threadId: string;+}): WorkflowRunStepLog => ({+ details: { type: 'AI_AGENT', ...summary, threadId },entries: [],sizeBytes: 0,});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-execution-context.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-execution-context.util.tsnew file mode 100644index 00000000000..739404eb6b5--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-execution-context.util.ts@@ -0,0 +1,27 @@+import { isDefined } from 'twenty-shared/utils';++import { type AgentRunExecutionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type';+import { getRoleIdsFromRolePermissionConfig } from 'src/engine/twenty-orm/utils/get-role-ids-from-role-permission-config.util';+import { type WorkflowExecutionContext } from 'src/modules/workflow/workflow-executor/types/workflow-execution-context.type';++// The step's agent acts with the run's permissions, derived again each time the agent continues+export const buildWorkflowAgentRunExecutionContext = (+ executionContext: WorkflowExecutionContext,+): AgentRunExecutionContext => ({+ authContext: executionContext.authContext,+ actorContext: executionContext.isActingOnBehalfOfUser+ ? executionContext.initiator+ : undefined,+ userWorkspaceId:+ executionContext.authContext.type === 'user'+ ? executionContext.authContext.userWorkspaceId+ : null,+ rolePermissionConfig: executionContext.rolePermissionConfig,+ ...(isDefined(executionContext.application)+ ? {+ additionalRoleRestrictionIds: getRoleIdsFromRolePermissionConfig(+ executionContext.rolePermissionConfig,+ ),+ }+ : {}),+});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util.tsnew file mode 100644index 00000000000..136ba69e7f4--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util.ts@@ -0,0 +1,41 @@+import { isNonEmptyString } from '@sniptt/guards';++import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';+import { WORKFLOW_BASE_SYSTEM_PROMPT } from 'src/engine/metadata-modules/ai/ai-agent/constants/workflow-base-system-prompt.const';+import { type WorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';+import { APPLICATION_BOUND_AGENT_EXCLUDED_TOOL_NAMES } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/application-bound-agent-excluded-tool-names.constant';+import { WORKFLOW_AGENT_HUMAN_INPUT_PROMPT } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-human-input-prompt.constant';++// what the engine needs to run the step's agent, and to continue it after a pause+export const buildWorkflowAgentRunSpec = ({+ step,+ isApplicationBound,+}: {+ step: WorkflowAiAgentAction;+ isApplicationBound: boolean;+}): AgentRunSpec => {+ const { agentId, humanInputInstructions } = step.settings.input;+ const trimmedHumanInputInstructions = humanInputInstructions?.trim();+ const canAskHumans = isNonEmptyString(trimmedHumanInputInstructions);++ return {+ agentId: isNonEmptyString(agentId) ? agentId : null,+ title: step.name,+ baseSystemPrompt: WORKFLOW_BASE_SYSTEM_PROMPT,+ instructions: canAskHumans+ ? `${WORKFLOW_AGENT_HUMAN_INPUT_PROMPT}\n\n${trimmedHumanInputInstructions}`+ : null,+ capabilities: {+ canAskHumans,+ canProposeToolCalls: canAskHumans,+ canWait: true,+ },+ ...(isApplicationBound+ ? {+ additionalExcludedToolNames: [+ ...APPLICATION_BOUND_AGENT_EXCLUDED_TOOL_NAMES,+ ],+ }+ : {}),+ };+};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/find-agent-step-wait.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/find-agent-step-wait.util.tsdeleted file mode 100644index ffd540398ab..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/find-agent-step-wait.util.ts+++ /dev/null@@ -1,33 +0,0 @@-import { isPlainObject } from 'twenty-shared/utils';-import {- type WorkflowStepWait,- workflowStepWaitSchema,-} from 'twenty-shared/workflow';--import { type AgentRunPausedToolResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-paused-tool-result.type';-import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';-import { WORKFLOW_AGENT_WAIT_TOOL_NAMES } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/constants/workflow-agent-wait-tool-names.constant';--// The wait a run paused on, when it paused on a wait tool call-export const findAgentStepWait = (- pausedToolResults: AgentRunPausedToolResult[],-): WorkflowStepWait | undefined => {- for (const { toolName, output } of pausedToolResults) {- if (- !WORKFLOW_AGENT_WAIT_TOOL_NAMES.includes(toolName) ||- !isAwaitingPausingToolOutput(output) ||- !isPlainObject(output) ||- !isPlainObject(output.result)- ) {- continue;- }-- const parsedWait = workflowStepWaitSchema.safeParse(output.result.wait);-- if (parsedWait.success) {- return parsedWait.data;- }- }-- return undefined;-};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/merge-ai-agent-step-logs.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/merge-ai-agent-step-logs.util.tsdeleted file mode 100644index e2976a2790d..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/merge-ai-agent-step-logs.util.ts+++ /dev/null@@ -1,66 +0,0 @@-import { isDefined } from 'twenty-shared/utils';-import { type WorkflowRunStepLog } from 'twenty-shared/workflow';--// An agent that asked a question runs again once answered, so its log spans several segments-export const mergeAiAgentStepLogs = ({- previousStepLog,- nextStepLog,-}: {- previousStepLog?: WorkflowRunStepLog;- nextStepLog: WorkflowRunStepLog;-}): WorkflowRunStepLog => {- const previousDetails = previousStepLog?.details;- const nextDetails = nextStepLog.details;-- if (previousDetails?.type !== 'AI_AGENT' || nextDetails.type !== 'AI_AGENT') {- return nextStepLog;- }-- const sumOptional = (- previousValue: number | undefined,- nextValue: number | undefined,- ) =>- !isDefined(previousValue) && !isDefined(nextValue)- ? undefined- : (previousValue ?? 0) + (nextValue ?? 0);-- return {- ...nextStepLog,- details: {- ...nextDetails,- usage: {- inputTokens:- previousDetails.usage.inputTokens + nextDetails.usage.inputTokens,- outputTokens:- previousDetails.usage.outputTokens + nextDetails.usage.outputTokens,- reasoningTokens: sumOptional(- previousDetails.usage.reasoningTokens,- nextDetails.usage.reasoningTokens,- ),- cacheReadTokens: sumOptional(- previousDetails.usage.cacheReadTokens,- nextDetails.usage.cacheReadTokens,- ),- cacheCreationTokens: sumOptional(- previousDetails.usage.cacheCreationTokens,- nextDetails.usage.cacheCreationTokens,- ),- totalTokens:- previousDetails.usage.totalTokens + nextDetails.usage.totalTokens,- },- cost: {- totalCostInDollars:- previousDetails.cost.totalCostInDollars +- nextDetails.cost.totalCostInDollars,- creditsUsedMicro:- previousDetails.cost.creditsUsedMicro +- nextDetails.cost.creditsUsedMicro,- },- nativeWebSearchCallCount:- previousDetails.nativeWebSearchCallCount +- nextDetails.nativeWebSearchCallCount,- toolCalls: [...previousDetails.toolCalls, ...nextDetails.toolCalls],- durationMs: previousDetails.durationMs + nextDetails.durationMs,- },- };-};diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/workflow-agent-conversation.module.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/workflow-agent-conversation.module.tsindex 972d4707c3f..c367b5c889e 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/workflow-agent-conversation.module.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/ai-agent/workflow-agent-conversation.module.ts@@ -4,13 +4,11 @@ import { WorkflowRunRecordShareModule } from 'src/engine/core-modules/workflow/wimport { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module';import { WorkflowRunInboxSenderModule } from 'src/modules/workflow/workflow-executor/services/workflow-run-inbox-sender.module';import { WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';-import { WorkflowRunModule } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.module';@Module({imports: [AiAgentExecutionModule,WorkflowRunInboxSenderModule,- WorkflowRunModule,WorkflowRunRecordShareModule,],providers: [WorkflowAgentConversationWorkspaceService],diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/form/form.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/form/form.workflow-action.tsindex ac7427039f9..f96f7251892 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/form/form.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/form/form.workflow-action.ts@@ -29,6 +29,6 @@ export class FormWorkflowAction implements WorkflowAction {);}- return { wait: { type: 'ANSWER' } };+ return { wait: { type: 'CALLBACK' } };}}diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.tsindex aaa71f2f1e5..ed9428beef7 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts@@ -2,6 +2,7 @@ import { WorkflowActionType } from 'twenty-shared/workflow';import { type ToolRegistryService } from 'src/engine/core-modules/tool-provider/services/tool-registry.service';import { type WorkflowCoreSyncService } from 'src/engine/core-modules/workflow/services/workflow-core-sync.service';+import { type AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { type AgentInboxService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service';import { type WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';import { WorkflowStepExecutorExceptionCode } from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';@@ -10,7 +11,6 @@ import { WorkflowRunInboxSenderWorkspaceService } from 'src/modules/workflow/worimport { createMockIteratorStep } from 'src/modules/workflow/workflow-executor/utils/create-mock-workflow-steps.util';import { SendChatMessageWorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';-import { type WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';const WORKSPACE_ID = 'workspace-id';const WORKFLOW_RUN_ID = 'workflow-run-id';@@ -71,7 +71,7 @@ describe('SendChatMessageWorkflowAction', () => {{ findCoreWorkflowById } as unknown as WorkflowCoreSyncService,),{} as WorkflowExecutionContextService,- {} as WorkflowRunWorkspaceService,+ {} as AgentRunSuspensionService,{} as ToolRegistryService,);});diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message-action.module.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message-action.module.tsindex 6722955ebed..96f415a809f 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message-action.module.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message-action.module.ts@@ -1,18 +1,18 @@import { Module } from '@nestjs/common';import { ToolProviderModule } from 'src/engine/core-modules/tool-provider/tool-provider.module';+import { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module';import { AgentChatThreadModule } from 'src/engine/metadata-modules/ai/ai-chat/agent-chat-thread.module';import { WorkflowExecutionContextModule } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.module';import { WorkflowRunInboxSenderModule } from 'src/modules/workflow/workflow-executor/services/workflow-run-inbox-sender.module';import { SendChatMessageWorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action';-import { WorkflowRunModule } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.module';@Module({imports: [AgentChatThreadModule,+ AiAgentExecutionModule,WorkflowExecutionContextModule,WorkflowRunInboxSenderModule,- WorkflowRunModule,ToolProviderModule,],providers: [SendChatMessageWorkflowAction],diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.tsindex c8ec5c6e045..c49a90208eb 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts@@ -15,6 +15,7 @@ import { ToolRegistryService } from 'src/engine/core-modules/tool-provider/serviimport { type ToolContext } from 'src/engine/core-modules/tool-provider/types/tool-context.type';import { readToolCallStatus } from 'src/engine/metadata-modules/ai/ai-history/utils/read-tool-call-status.util';import { resolveProposedToolCall } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/resolve-proposed-tool-call.util';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { AgentInboxService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service';import { buildProposeToolCallPendingOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/propose-tool-call.pausing-tool';import { getRoleIdsFromRolePermissionConfig } from 'src/engine/twenty-orm/utils/get-role-ids-from-role-permission-config.util';@@ -26,6 +27,7 @@ import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-eimport { WorkflowRunInboxSenderWorkspaceService } from 'src/modules/workflow/workflow-executor/services/workflow-run-inbox-sender.workspace-service';import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+import { buildWorkflowStepCaller } from 'src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util';import { buildStepExecutionKey } from 'src/modules/workflow/workflow-executor/utils/build-step-execution-key.util';import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';import { resolveConversationThreadKey } from 'src/modules/workflow/workflow-executor/utils/resolve-conversation-thread-key.util';@@ -33,7 +35,6 @@ import { isWorkflowSendChatMessageAction } from 'src/modules/workflow/workflow-eimport { findMissingRequiredToolArguments } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/utils/find-missing-required-tool-arguments.util';import { buildSendChatMessageAnswerResult } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/utils/build-send-chat-message-answer-result.util';import { type WorkflowSendChatMessageActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/types/workflow-send-chat-message-action-input.type';-import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';const MAX_SEND_ATTEMPTS = 10;@@ -43,7 +44,7 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {private readonly agentInboxService: AgentInboxService,private readonly workflowRunInboxSenderService: WorkflowRunInboxSenderWorkspaceService,private readonly workflowExecutionContextService: WorkflowExecutionContextService,- private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,private readonly toolRegistryService: ToolRegistryService,) {}@@ -109,7 +110,6 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {toolCall,summary: text,runInfo,- stepId: currentStepId,});return awaitingToolCall;@@ -154,15 +154,17 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {}if (status === 'pending' || status === 'running') {- // the step waits for the member, and their answer finds it through this thread- await this.workflowRunWorkspaceService.setStepThreadId({- stepId: currentStepId,- threadId,- workflowRunId: runInfo.workflowRunId,+ // the step waits for the member, and the engine hands it their answer+ await this.agentRunSuspensionService.awaitCallerCall({workspaceId: runInfo.workspaceId,+ threadId,+ caller: buildWorkflowStepCaller({+ workflowRunId: runInfo.workflowRunId,+ stepId: currentStepId,+ }),});- return { wait: { type: 'ANSWER' } };+ return { wait: { type: 'CALLBACK' } };}return {@@ -184,12 +186,10 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {toolCall,summary,runInfo,- stepId,}: {toolCall: NonNullable<WorkflowSendChatMessageActionInput['toolCall']>;summary: string;runInfo: WorkflowActionInput['runInfo'];- stepId: string;}) {const { authContext, rolePermissionConfig, application } =await this.workflowExecutionContextService.getExecutionContext(runInfo);@@ -246,7 +246,7 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {input,output: {...buildProposeToolCallPendingOutput(resolution.proposal),- workflowStep: { workflowRunId: runInfo.workflowRunId, stepId },+ awaitedByCaller: true,},};}diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.tsindex 1049f984e7a..0c3ed4598ac 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts@@ -8,7 +8,6 @@ import {WORKFLOW_ACTION_FEATURE_FLAGS,WorkflowRunStepInfo,WorkflowRunStepInfos,- type WorkflowRunStepLog,} from 'twenty-shared/workflow';import {@@ -88,7 +87,7 @@ export class WorkflowExecutorWorkspaceService {workspaceId,shouldComputeWorkflowRunStatus = true,executedStepsCount = 0,- resumedThreadId,+ awaitedActionOutput,}: WorkflowExecutorInput) {await Promise.all(stepIds.map(async (stepIdToExecute) => {@@ -97,7 +96,7 @@ export class WorkflowExecutorWorkspaceService {workflowRunId,workspaceId,executedStepsCount,- resumedThreadId,+ awaitedActionOutput,});}),);@@ -115,7 +114,7 @@ export class WorkflowExecutorWorkspaceService {workflowRunId,workspaceId,executedStepsCount,- resumedThreadId,+ awaitedActionOutput,}: WorkflowBranchExecutorInput): Promise<void> {const workflowRun =await this.workflowRunWorkspaceService.getWorkflowRunOrFail({@@ -161,9 +160,9 @@ export class WorkflowExecutorWorkspaceService {let actionOutput: WorkflowActionOutput;- // A resumed step was claimed as started, which shouldExecuteStep refuses.+ // A step handed its output was claimed as started, which shouldExecuteStep refuses.if (- isDefined(resumedThreadId) ||+ isDefined(awaitedActionOutput) ||shouldExecuteStep({step: stepToExecute,steps,@@ -171,19 +170,17 @@ export class WorkflowExecutorWorkspaceService {workflowRunStatus: workflowRun.status,})) {- actionOutput = await this.executeStep({- step: stepToExecute,- steps,- stepInfos,- workflowRunId,- workspaceId,- billingSpenders,- runApplicationId: workflowRun.createdBy.context?.applicationId,- resumedThreadId,- previousStepLog: isDefined(resumedThreadId)- ? workflowRun.stepLogs?.[stepId]- : undefined,- });+ actionOutput =+ awaitedActionOutput ??+ (await this.executeStep({+ step: stepToExecute,+ steps,+ stepInfos,+ workflowRunId,+ workspaceId,+ billingSpenders,+ runApplicationId: workflowRun.createdBy.context?.applicationId,+ }));if (isDefined(actionOutput.error) && !actionOutput.isUserError) {const canRetryStep = stepHasRetryAttemptsLeft({@@ -194,7 +191,6 @@ export class WorkflowExecutorWorkspaceService {if (canRetryStep) {await this.scheduleStepRetry({stepId,- resumedThreadId,error: actionOutput.error,retryDelayMs: getStepRetryDelayMs({ stepInfo: stepInfos[stepId] }),workflowRunId,@@ -245,12 +241,12 @@ export class WorkflowExecutorWorkspaceService {const isError =isDefined(actionOutput.error) && !actionOutput.shouldFailSafely;- // A resumed step's node run was charged when it first ran and paused.+ // A step handed its output was charged when it first ran and paused.if (!isError &&!actionOutput.shouldFailSafely &&!actionOutput.shouldSkipStepExecution &&- !isDefined(resumedThreadId)+ !isDefined(awaitedActionOutput)) {await this.chargeWorkflowNodeRun(workspaceId, billingSpenders);}@@ -579,8 +575,6 @@ export class WorkflowExecutorWorkspaceService {workspaceId,billingSpenders,runApplicationId,- resumedThreadId,- previousStepLog,}: {step: WorkflowAction;steps: WorkflowAction[];@@ -589,8 +583,6 @@ export class WorkflowExecutorWorkspaceService {workspaceId: string;billingSpenders: WorkflowBillingSpenders;runApplicationId?: string;- resumedThreadId?: string;- previousStepLog?: WorkflowRunStepLog;}) {const stepId = step.id;@@ -614,10 +606,10 @@ export class WorkflowExecutorWorkspaceService {runApplicationId,});- // A resumed step's quota was checked when it first ran and paused.- const nodeRunRefusal = isDefined(resumedThreadId)- ? undefined- : await this.getNodeRunRefusal({ workspaceId, billingSpenders });+ const nodeRunRefusal = await this.getNodeRunRefusal({+ workspaceId,+ billingSpenders,+ });if (isDefined(nodeRunRefusal)) {return nodeRunRefusal;@@ -631,8 +623,6 @@ export class WorkflowExecutorWorkspaceService {workflowRunId,workspaceId,},- resumedThreadId,- previousStepLog,});} catch (error) {const isUserError = isUserFacingWorkflowExecutorError(error);@@ -758,18 +748,14 @@ export class WorkflowExecutorWorkspaceService {}}- // a step that failed after resuming on an answer retries from that conversation, so the- // person is not asked again and an approved call does not run a second timeprivate async scheduleStepRetry({stepId,- resumedThreadId,error,retryDelayMs,workflowRunId,workspaceId,}: {stepId: string;- resumedThreadId: string | undefined;error: string;retryDelayMs: number;workflowRunId: string;@@ -777,7 +763,6 @@ export class WorkflowExecutorWorkspaceService {}) {await this.workflowRunWorkspaceService.moveStepToRetry({stepId,- resumedThreadId,error,workflowRunId,workspaceId,diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/__tests__/run-workflow.job.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/__tests__/run-workflow.job.spec.tsindex be70a4b689d..41af34d6b55 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/__tests__/run-workflow.job.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/__tests__/run-workflow.job.spec.ts@@ -16,7 +16,6 @@ import { type WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-const WORKSPACE_ID = 'workspace-id';const WORKFLOW_RUN_ID = 'workflow-run-id';const AGENT_STEP_ID = 'agent-step-id';-const THREAD_ID = 'thread-id';describe('RunWorkflowJob', () => {const workflowRunWorkspaceService = {@@ -44,11 +43,14 @@ describe('RunWorkflowJob', () => {workspaceOrmManager as unknown as WorkspaceOrmManager,);- const resume = () =>+ const completeAwaitedStep = () =>job.handle({workspaceId: WORKSPACE_ID,workflowRunId: WORKFLOW_RUN_ID,- stepToResume: { stepId: AGENT_STEP_ID, threadId: THREAD_ID },+ awaitedStepOutput: {+ stepId: AGENT_STEP_ID,+ actionOutput: { error: 'Agent failed' },+ },});beforeEach(() => {@@ -56,16 +58,15 @@ describe('RunWorkflowJob', () => {workflowRunWorkspaceService.updateStepInfoIfPending.mockResolvedValue(true);});- describe('resuming an answered step', () => {- it('claims the step out of PENDING in its conversation and resumes it', async () => {- await resume();+ describe('ending a step that waited on a callback', () => {+ it('claims the step out of PENDING and ends it with its output like a step that just ran', async () => {+ await completeAwaitedStep();expect(workflowRunWorkspaceService.updateStepInfoIfPending,).toHaveBeenCalledWith({stepId: AGENT_STEP_ID,stepInfo: { status: StepStatus.RUNNING },- expectedThreadId: THREAD_ID,workflowRunId: WORKFLOW_RUN_ID,workspaceId: WORKSPACE_ID,});@@ -75,11 +76,8 @@ describe('RunWorkflowJob', () => {stepIds: [AGENT_STEP_ID],workflowRunId: WORKFLOW_RUN_ID,workspaceId: WORKSPACE_ID,- resumedThreadId: THREAD_ID,+ awaitedActionOutput: { error: 'Agent failed' },});- expect(- workflowRunWorkspaceService.updateWorkflowRunStepInfos,- ).not.toHaveBeenCalled();});it('does nothing when the step can no longer be claimed', async () => {@@ -87,7 +85,7 @@ describe('RunWorkflowJob', () => {false,);- await resume();+ await completeAwaitedStep();expect(workflowExecutorWorkspaceService.executeFromSteps,@@ -105,7 +103,7 @@ describe('RunWorkflowJob', () => {),);- await expect(resume()).resolves.toBeUndefined();+ await expect(completeAwaitedStep()).resolves.toBeUndefined();expect(workflowRunWorkspaceService.endWorkflowRun).not.toHaveBeenCalled();});@@ -115,7 +113,7 @@ describe('RunWorkflowJob', () => {new Error('Step blew up'),);- await expect(resume()).rejects.toThrow('Step blew up');+ await expect(completeAwaitedStep()).rejects.toThrow('Step blew up');expect(workflowRunWorkspaceService.endWorkflowRun).toHaveBeenCalledTimes(1,diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.tsindex 077e2f23495..dcc2391fab4 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts@@ -44,7 +44,7 @@ export class RunWorkflowJob {workflowRunId,lastExecutedStepId,stepIdsToRetry,- stepToResume,+ awaitedStepOutput,workspaceId,}: RunWorkflowJobData): Promise<void> {this.logger.log(@@ -54,11 +54,11 @@ export class RunWorkflowJob {await this.workspaceOrmManager.executeInWorkspaceContext(async () => {try {- if (isDefined(stepToResume)) {- await this.resumeAnsweredStep({+ if (isDefined(awaitedStepOutput)) {+ await this.completeAwaitedStep({workspaceId,workflowRunId,- stepToResume,+ awaitedStepOutput,});} else if (isDefined(stepIdsToRetry)) {await this.retryWorkflowExecution({@@ -220,50 +220,27 @@ export class RunWorkflowJob {});}- // a step that failed after resuming on an answer kept its conversation, and continues it- const resumedStepIds = stepIdsToRetry.filter((stepId) =>- isDefined(stepInfosToReset[stepId]?.threadId),- );- const restartedStepIds = stepIdsToRetry.filter(- (stepId) => !resumedStepIds.includes(stepId),- );-- await Promise.all([- ...(restartedStepIds.length > 0- ? [- this.workflowExecutorWorkspaceService.executeFromSteps({- stepIds: restartedStepIds,- workflowRunId,- workspaceId,- }),- ]- : []),- ...resumedStepIds.map((stepId) =>- this.workflowExecutorWorkspaceService.executeFromSteps({- stepIds: [stepId],- workflowRunId,- workspaceId,- resumedThreadId: stepInfosToReset[stepId].threadId,- }),- ),- ]);+ await this.workflowExecutorWorkspaceService.executeFromSteps({+ stepIds: stepIdsToRetry,+ workflowRunId,+ workspaceId,+ });}- // The step stays PENDING until claimed here, so its run can't complete while queued and a second resume no-ops- private async resumeAnsweredStep({+ // The step stays PENDING until claimed here, so its run can't complete while queued and a second delivery no-ops+ private async completeAwaitedStep({workflowRunId,- stepToResume: { stepId, threadId },+ awaitedStepOutput: { stepId, actionOutput },workspaceId,}: {workflowRunId: string;- stepToResume: { stepId: string; threadId: string };+ awaitedStepOutput: NonNullable<RunWorkflowJobData['awaitedStepOutput']>;workspaceId: string;}): Promise<void> {const isClaimed =await this.workflowRunWorkspaceService.updateStepInfoIfPending({stepId,stepInfo: { status: StepStatus.RUNNING },- expectedThreadId: threadId,workflowRunId,workspaceId,});@@ -276,7 +253,7 @@ export class RunWorkflowJob {stepIds: [stepId],workflowRunId,workspaceId,- resumedThreadId: threadId,+ awaitedActionOutput: actionOutput,});}diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/types/run-workflow-job-data.type.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/types/run-workflow-job-data.type.tsindex b945a6ac352..474f302fca3 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/types/run-workflow-job-data.type.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/types/run-workflow-job-data.type.ts@@ -1,7 +1,13 @@+import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';+export type RunWorkflowJobData = {workspaceId: string;workflowRunId: string;lastExecutedStepId?: string;stepIdsToRetry?: string[];- stepToResume?: { stepId: string; threadId: string };+ // what a step that waited on a callback was handed, such as the outcome of an agent run+ awaitedStepOutput?: {+ stepId: string;+ actionOutput: Pick<WorkflowActionOutput, 'result' | 'error'>;+ };};diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/__tests__/workflow-run.workspace-service.conversation.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/__tests__/workflow-run.workspace-service.conversation.spec.tsindex 7ab876cffc4..4a6b7357990 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/__tests__/workflow-run.workspace-service.conversation.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/__tests__/workflow-run.workspace-service.conversation.spec.ts@@ -3,7 +3,7 @@ import { StepStatus, WorkflowActionType } from 'twenty-shared/workflow';import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';-describe('WorkflowRunWorkspaceService conversations', () => {+describe('WorkflowRunWorkspaceService waiting steps', () => {const buildService = ({status = WorkflowRunStatus.RUNNING,stepInfo = {@@ -11,8 +11,8 @@ describe('WorkflowRunWorkspaceService conversations', () => {threadId: 'thread-id',} as Record<string, unknown>,} = {}) => {- const agentCallerConversationService = {- cancelAwaitingConversations: jest.fn().mockResolvedValue(undefined),+ const agentRunSuspensionService = {+ releaseForCaller: jest.fn().mockResolvedValue(undefined),};const service = new WorkflowRunWorkspaceService(@@ -21,7 +21,7 @@ describe('WorkflowRunWorkspaceService conversations', () => {{ incrementCounterForEvent: jest.fn() } as never,{} as never,{ cancelRunWaits: jest.fn().mockResolvedValue(undefined) } as never,- agentCallerConversationService as never,+ agentRunSuspensionService as never,);// The run lock serializes these methods with every other step write.@@ -61,153 +61,12 @@ describe('WorkflowRunWorkspaceService conversations', () => {service,step,workflowRun,- agentCallerConversationService,+ agentRunSuspensionService,updateWorkflowRun,};};- describe('findStepAwaitingAnswer', () => {- const findStep = (service: WorkflowRunWorkspaceService) =>- service.findStepAwaitingAnswer({- threadId: 'thread-id',- workflowRunId: 'workflow-run-id',- workspaceId: 'workspace-id',- });-- it('finds the PENDING step whose current execution holds the conversation', async () => {- const { service, step } = buildService();-- expect(await findStep(service)).toEqual(step);- });-- it('finds the step a call names among steps sharing one inbox conversation', async () => {- const { service, workflowRun } = buildService();- const approvalStep = {- id: 'second-step-id',- name: 'Approve',- type: WorkflowActionType.SEND_CHAT_MESSAGE,- };-- workflowRun.state = {- flow: { steps: [...workflowRun.state.flow.steps, approvalStep] },- stepInfos: {- 'step-id': { status: StepStatus.SUCCESS, threadId: 'thread-id' },- 'second-step-id': {- status: StepStatus.PENDING,- threadId: 'thread-id',- },- },- };-- const findNamedStep = (expectedStepId: string) =>- service.findStepAwaitingAnswer({- threadId: 'thread-id',- workflowRunId: 'workflow-run-id',- workspaceId: 'workspace-id',- expectedStepId,- });-- expect(await findNamedStep('second-step-id')).toEqual(approvalStep);- expect(await findNamedStep('step-id')).toBeNull();- expect(await findStep(service)).toEqual(approvalStep);- });-- it('finds nothing for a form step, which is submitted from its run', async () => {- const { service, workflowRun } = buildService();-- workflowRun.state.flow.steps = [- { id: 'step-id', name: 'Form', type: WorkflowActionType.FORM },- ];-- expect(await findStep(service)).toBeNull();- });-- it.each([- ['the run is no longer running', { status: WorkflowRunStatus.STOPPED }],- [- 'the step already resumed',- { stepInfo: { status: StepStatus.RUNNING, threadId: 'thread-id' } },- ],- [- 'a retry or a loop iteration replaced the conversation',- { stepInfo: { status: StepStatus.PENDING, threadId: 'other-thread' } },- ],- [- 'the step failed while waiting',- {- stepInfo: {- status: StepStatus.PENDING,- threadId: 'thread-id',- error: 'boom',- },- },- ],- ])('finds nothing when %s', async (_description, overrides) => {- const { service } = buildService(overrides);-- expect(await findStep(service)).toBeNull();- });- });-- describe('updateStepInfoIfPending in a conversation', () => {- const claimInConversation = (service: WorkflowRunWorkspaceService) =>- service.updateStepInfoIfPending({- stepId: 'step-id',- stepInfo: { status: StepStatus.RUNNING },- expectedThreadId: 'thread-id',- workflowRunId: 'workflow-run-id',- workspaceId: 'workspace-id',- });-- it('claims a PENDING step still holding the expected conversation', async () => {- const { service, updateWorkflowRun } = buildService();-- expect(await claimInConversation(service)).toBe(true);- expect(updateWorkflowRun).toHaveBeenCalledWith(- expect.objectContaining({- partialUpdate: {- state: expect.objectContaining({- stepInfos: {- 'step-id': {- status: StepStatus.RUNNING,- threadId: 'thread-id',- },- },- }),- },- }),- );- });-- it.each([- ['the run is no longer running', { status: WorkflowRunStatus.STOPPED }],- [- 'the step already resumed',- { stepInfo: { status: StepStatus.RUNNING, threadId: 'thread-id' } },- ],- [- 'the step holds another conversation',- { stepInfo: { status: StepStatus.PENDING, threadId: 'other-thread' } },- ],- [- 'the step failed while waiting',- {- stepInfo: {- status: StepStatus.PENDING,- threadId: 'thread-id',- error: 'boom',- },- },- ],- ])('claims nothing when %s', async (_description, overrides) => {- const { service, updateWorkflowRun } = buildService(overrides);-- expect(await claimInConversation(service)).toBe(false);- expect(updateWorkflowRun).not.toHaveBeenCalled();- });- });-- describe('updateStepInfoIfPending without an expected conversation', () => {+ describe('updateStepInfoIfPending', () => {const claim = (service: WorkflowRunWorkspaceService) =>service.updateStepInfoIfPending({stepId: 'step-id',@@ -216,7 +75,7 @@ describe('WorkflowRunWorkspaceService conversations', () => {workspaceId: 'workspace-id',});- it('claims a PENDING step whatever conversation it holds, only once', async () => {+ it('claims a PENDING step only once', async () => {const { service, updateWorkflowRun } = buildService({stepInfo: { status: StepStatus.PENDING, threadId: 'other-thread' },});@@ -242,16 +101,13 @@ describe('WorkflowRunWorkspaceService conversations', () => {status: WorkflowRunStatus.STOPPED,});- it('closes the calls its conversations wait on', async () => {- const { service, agentCallerConversationService } = buildService();+ it('drops the runs its steps wait on and closes the calls they wait on', async () => {+ const { service, agentRunSuspensionService } = buildService();await endRun(service);- expect(- agentCallerConversationService.cancelAwaitingConversations,- ).toHaveBeenCalledWith({+ expect(agentRunSuspensionService.releaseForCaller).toHaveBeenCalledWith({workspaceId: 'workspace-id',- threadIds: ['thread-id'],caller: {type: 'WORKFLOW_STEP',ref: { workflowRunId: 'workflow-run-id' },@@ -260,10 +116,10 @@ describe('WorkflowRunWorkspaceService conversations', () => {});it('still ends the run when its conversations cannot be closed', async () => {- const { service, agentCallerConversationService, updateWorkflowRun } =+ const { service, agentRunSuspensionService, updateWorkflowRun } =buildService();- agentCallerConversationService.cancelAwaitingConversations.mockRejectedValue(+ agentRunSuspensionService.releaseForCaller.mockRejectedValue(new Error('db down'),);@@ -271,17 +127,5 @@ describe('WorkflowRunWorkspaceService conversations', () => {expect(updateWorkflowRun).toHaveBeenCalled();});-- it('leaves conversations alone for a run that never had one', async () => {- const { service, agentCallerConversationService } = buildService({- stepInfo: { status: StepStatus.SUCCESS },- });-- await endRun(service);-- expect(- agentCallerConversationService.cancelAwaitingConversations,- ).not.toHaveBeenCalled();- });});});diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service.tsindex 1299e25cfd2..3fde690772c 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service.ts@@ -4,6 +4,8 @@ import { InjectDataSource } from '@nestjs/typeorm';import { type WorkflowRunStepLog } from 'twenty-shared/workflow';import { DataSource } from 'typeorm';+import { type AgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-summary.type';+import { buildAiAgentStepLog } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-ai-agent-step-log.util';import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';const MAX_STEP_LOG_BYTES = 256_000;@@ -58,4 +60,49 @@ export class WorkflowRunStepLogWorkspaceService {[stepId, JSON.stringify(stepLogWithSize), workflowRunId],);}++ // a log of the run, not its outcome, so a write failure must not fail the step+ async setAiAgentStepLog({+ workflowRunId,+ workspaceId,+ stepId,+ summary,+ threadId,+ }: {+ workflowRunId: string;+ workspaceId: string;+ stepId: string;+ summary: AgentRunSummary;+ threadId: string;+ }): Promise<void> {+ const stepLog = buildAiAgentStepLog({ summary, threadId });++ try {+ await this.setStepLog({+ workflowRunId,+ workspaceId,+ stepId,+ // tool calls are what outgrows the cap, and the usage and the conversation link must stay+ stepLog:+ computeSizeBytes(stepLog) > MAX_STEP_LOG_BYTES+ ? {+ ...buildAiAgentStepLog({+ summary: { ...summary, toolCalls: [] },+ threadId,+ }),+ truncated: {+ droppedEntries: summary.toolCalls.length,+ droppedBytes: computeSizeBytes(summary.toolCalls),+ },+ }+ : stepLog,+ });+ } catch (error) {+ this.logger.warn(+ `Failed to persist step log for workflowRun=${workflowRunId} step=${stepId}: ${+ error instanceof Error ? error.message : String(error)+ }`,+ );+ }+ }}diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service.tsindex 1f73d1d89d6..3656c8ccbb4 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service.ts@@ -10,7 +10,7 @@ import { MetricsService } from 'src/engine/core-modules/metrics/metrics.service'import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.type';import { RecordPositionService } from 'src/engine/core-modules/record-position/services/record-position.service';import { WorkflowRunRecordShareService } from 'src/engine/core-modules/workflow/services/workflow-run-record-share.service';-import { AgentCallerConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service';+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { PermissionsException } from 'src/engine/metadata-modules/permissions/permissions.exception';import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';@@ -22,8 +22,6 @@ import {import { setAllIteratorsStepInfosAsStopped } from 'src/modules/workflow/common/utils/set-all-iterators-step-infos-as-stopped.util';import { type WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';import { getStepRetryAttempt } from 'src/modules/workflow/workflow-executor/utils/get-step-retry-attempt.util';-import { isWorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/guards/is-workflow-ai-agent-action.guard';-import { isWorkflowSendChatMessageAction } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/guards/is-workflow-send-chat-message-action.guard';import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';import { type WorkflowTrigger } from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';import { WorkflowStepWaitWorkspaceService } from 'src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service';@@ -42,7 +40,7 @@ export class WorkflowRunWorkspaceService {private readonly metricsService: MetricsService,private readonly workflowRunRecordShareService: WorkflowRunRecordShareService,private readonly workflowStepWaitWorkspaceService: WorkflowStepWaitWorkspaceService,- private readonly agentCallerConversationService: AgentCallerConversationService,+ private readonly agentRunSuspensionService: AgentRunSuspensionService,) {}async createCoreWorkflowRun({@@ -212,27 +210,18 @@ export class WorkflowRunWorkspaceService {);});- const stepThreadIds = Object.values(- workflowRunToUpdate.state?.stepInfos ?? {},- )- .map((stepInfo) => stepInfo?.threadId)- .filter(isDefined);-- // An ended run cannot consume answers, so close the calls its conversations wait on.- // Best effort: a failure only leaves a call that looks waiting.- if (stepThreadIds.length > 0) {- await this.agentCallerConversationService- .cancelAwaitingConversations({- workspaceId,- threadIds: stepThreadIds,- caller: { type: 'WORKFLOW_STEP', ref: { workflowRunId } },- })- .catch((error: unknown) => {- this.logger.error(- `Failed to close the conversations of workflow run ${workflowRunId} in workspace ${workspaceId}: ${error instanceof Error ? error.message : String(error)}`,- );- });- }+ // An ended run cannot consume answers or agent outcomes, so drop the runs its steps wait on and+ // close the calls they wait on. Best effort: a failure only leaves a call that looks waiting.+ await this.agentRunSuspensionService+ .releaseForCaller({+ workspaceId,+ caller: { type: 'WORKFLOW_STEP', ref: { workflowRunId } },+ })+ .catch((error: unknown) => {+ this.logger.error(+ `Failed to close the conversations of workflow run ${workflowRunId} in workspace ${workspaceId}: ${error instanceof Error ? error.message : String(error)}`,+ );+ });const metricKey =status === WorkflowRunStatus.COMPLETED@@ -291,17 +280,14 @@ export class WorkflowRunWorkspaceService {await this.updateWorkflowRun({ workflowRunId, workspaceId, partialUpdate });}- // Built from persisted step info: only it carries the failed attempt's conversation@WithLock('workflowRunId')async moveStepToRetry({stepId,- resumedThreadId,error,workflowRunId,workspaceId,}: {stepId: string;- resumedThreadId?: string;error: string;workflowRunId: string;workspaceId: string;@@ -325,7 +311,6 @@ export class WorkflowRunWorkspaceService {...currentStepInfo,status: StepStatus.PENDING,error,- threadId: resumedThreadId,history: [...(currentStepInfo?.history ?? []),{@@ -333,7 +318,6 @@ export class WorkflowRunWorkspaceService {error,retryAttempt:getStepRetryAttempt({ stepInfo: currentStepInfo }) + 1,- threadId: currentStepInfo?.threadId,},],},@@ -429,18 +413,15 @@ export class WorkflowRunWorkspaceService {// Shares the step-info write lock so of two concurrent callers the second finds the step no longer PENDING.// A stop is refused: endWorkflowRun fails a pending step, or leaves it PENDING with nothing to resume.- // expectedThreadId must still be the step's: a retry or another loop iteration replaces it.@WithLock('workflowRunId')async updateStepInfoIfPending({stepId,stepInfo,- expectedThreadId,workflowRunId,workspaceId,}: {stepId: string;stepInfo: Partial<WorkflowRunStepInfo>;- expectedThreadId?: string;workflowRunId: string;workspaceId: string;}): Promise<boolean> {@@ -454,9 +435,7 @@ export class WorkflowRunWorkspaceService {if (workflowRunToUpdate.status !== WorkflowRunStatus.RUNNING ||currentStepInfo?.status !== StepStatus.PENDING ||- isDefined(currentStepInfo.error) ||- (isDefined(expectedThreadId) &&- currentStepInfo.threadId !== expectedThreadId)+ isDefined(currentStepInfo.error)) {return false;}@@ -479,122 +458,6 @@ export class WorkflowRunWorkspaceService {return true;}- // A conversation replaced by a retry or a later loop iteration belongs to no step- // several Send Message steps of one run post to the same inbox thread, so the step a call names wins- async findStepAwaitingAnswer({- threadId,- workflowRunId,- workspaceId,- expectedStepId,- }: {- threadId: string;- workflowRunId: string;- workspaceId: string;- expectedStepId?: string;- }): Promise<WorkflowAction | null> {- const workflowRun = await this.getWorkflowRunOrFail({- workflowRunId,- workspaceId,- });-- const [stepId, stepInfo] =- Object.entries(workflowRun.state?.stepInfos ?? {}).find(- ([candidateStepId, stepInfo]) =>- stepInfo?.threadId === threadId &&- (isDefined(expectedStepId)- ? candidateStepId === expectedStepId- : stepInfo?.status === StepStatus.PENDING),- ) ?? [];-- if (- workflowRun.status !== WorkflowRunStatus.RUNNING ||- !isDefined(stepId) ||- isDefined(stepInfo?.error) ||- stepInfo?.status !== StepStatus.PENDING- ) {- return null;- }-- const step = workflowRun.state?.flow?.steps?.find(- (candidateStep) => candidateStep.id === stepId,- );-- // a form step is submitted from its run, even one that older versions gave a conversation- return isDefined(step) &&- (isWorkflowAiAgentAction(step) || isWorkflowSendChatMessageAction(step))- ? step- : null;- }-- // a step posting a call still runs until its executor marks it pending, and an answer may arrive first;- // a step is named by its id when it posted to an inbox, or by its conversation on its own run- async isStepStillRunning({- stepId,- threadId,- workflowRunId,- workspaceId,- }: {- stepId?: string;- threadId: string;- workflowRunId: string;- workspaceId: string;- }): Promise<boolean> {- const workflowRun = await this.getWorkflowRunOrFail({- workflowRunId,- workspaceId,- });-- const stepInfos = workflowRun.state?.stepInfos ?? {};- const stepInfo = isDefined(stepId)- ? stepInfos[stepId]- : Object.values(stepInfos).find(- (candidateStepInfo) => candidateStepInfo?.threadId === threadId,- );-- return (- workflowRun.status === WorkflowRunStatus.RUNNING &&- stepInfo?.status === StepStatus.RUNNING- );- }-- @WithLock('workflowRunId')- async setStepThreadId({- stepId,- threadId,- workflowRunId,- workspaceId,- }: {- stepId: string;- threadId: string;- workflowRunId: string;- workspaceId: string;- }) {- const workflowRunToUpdate = await this.getWorkflowRunOrFail({- workflowRunId,- workspaceId,- });-- const currentStepInfo = workflowRunToUpdate.state?.stepInfos?.[stepId];-- if (!isDefined(currentStepInfo)) {- return;- }-- await this.updateWorkflowRun({- workflowRunId,- workspaceId,- partialUpdate: {- state: {- ...workflowRunToUpdate.state,- stepInfos: {- ...workflowRunToUpdate.state?.stepInfos,- [stepId]: { ...currentStepInfo, threadId },- },- },- },- });- }-@WithLock('workflowRunId')async updateWorkflowRunStep({workflowRunId,diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.tsindex b879c269e87..9dec7e01921 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workflow-runner.module.ts@@ -11,8 +11,9 @@ import { RunWorkflowJob } from 'src/modules/workflow/workflow-runner/jobs/run-woimport { WorkflowRunQueueModule } from 'src/modules/workflow/workflow-runner/workflow-run-queue/workflow-run-queue.module';import { WorkflowRunModule } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.module';import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service';-import { WorkflowAwaitedToolCallHandlerWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-awaited-tool-call-handler.workspace-service';-import { AwaitedToolCallHandlerModule } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/awaited-tool-call-handler.module';+import { WorkflowAgentRunCallerHandlerWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service';+import { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module';+import { WorkflowAgentConversationModule } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/workflow-agent-conversation.module';import { CoreWorkflowRunnerService } from 'src/modules/workflow/workflow-runner/services/core-workflow-runner.service';import { WorkflowCoreModule } from 'src/engine/core-modules/workflow/workflow-core.module';import { WorkflowVersionCoreModule } from 'src/engine/core-modules/workflow/workflow-version-core.module';@@ -32,13 +33,14 @@ import { WorkflowExecutionContextModule } from 'src/modules/workflow/workflow-exWorkflowCoreModule,WorkflowVersionCoreModule,WorkflowExecutionContextModule,- AwaitedToolCallHandlerModule,+ AiAgentExecutionModule,+ WorkflowAgentConversationModule,],providers: [WorkflowRunnerWorkspaceService,CoreWorkflowRunnerService,RunWorkflowJob,- WorkflowAwaitedToolCallHandlerWorkspaceService,+ WorkflowAgentRunCallerHandlerWorkspaceService,],exports: [WorkflowRunnerWorkspaceService, CoreWorkflowRunnerService],})diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/__tests__/workflow-awaited-tool-call-handler.workspace-service.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/__tests__/workflow-awaited-tool-call-handler.workspace-service.spec.tsdeleted file mode 100644index 276841ca9bb..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/__tests__/workflow-awaited-tool-call-handler.workspace-service.spec.ts+++ /dev/null@@ -1,51 +0,0 @@-import {- WorkflowRunException,- WorkflowRunExceptionCode,-} from 'src/modules/workflow/workflow-runner/exceptions/workflow-run.exception';-import { WorkflowAwaitedToolCallHandlerWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-awaited-tool-call-handler.workspace-service';--describe('WorkflowAwaitedToolCallHandlerWorkspaceService', () => {- const buildHandler = (findStepAwaitingAnswer: jest.Mock) => {- const workflowRunWorkspaceService = {- findStepAwaitingAnswer,- isStepStillRunning: jest.fn().mockResolvedValue(false),- };-- return new WorkflowAwaitedToolCallHandlerWorkspaceService(- { register: jest.fn() } as never,- workflowRunWorkspaceService as never,- {} as never,- );- };-- const findWaiter = (- handler: WorkflowAwaitedToolCallHandlerWorkspaceService,- ) =>- handler.findWaiter({- workspaceId: 'workspace-id',- threadId: 'thread-id',- workflowStep: { workflowRunId: 'workflow-run-id', stepId: 'step-id' },- });-- it('reports a deleted workflow run as gone', async () => {- const handler = buildHandler(- jest- .fn()- .mockRejectedValue(- new WorkflowRunException(- 'Workflow run not found',- WorkflowRunExceptionCode.WORKFLOW_RUN_NOT_FOUND,- ),- ),- );-- await expect(findWaiter(handler)).resolves.toEqual({ status: 'gone' });- });-- it('rethrows other errors', async () => {- const error = new Error('Database unavailable');- const handler = buildHandler(jest.fn().mockRejectedValue(error));-- await expect(findWaiter(handler)).rejects.toBe(error);- });-});diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.tsnew file mode 100644index 00000000000..42d8c525119--- /dev/null+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts@@ -0,0 +1,184 @@+import { Injectable, type OnModuleInit } from '@nestjs/common';++import { type ActorMetadata } from 'twenty-shared/types';+import { isDefined } from 'twenty-shared/utils';+import { StepStatus } from 'twenty-shared/workflow';++import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';+import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';+import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';+import { type AgentRunCallerHandler } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type';+import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';+import { type AgentRunExecutionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type';+import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';+import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.service';+import { WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';+import { buildWorkflowAgentRunExecutionContext } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-execution-context.util';+import { isWorkflowSendChatMessageAction } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/guards/is-workflow-send-chat-message-action.guard';+import { buildSendChatMessageAnswerResult } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/utils/build-send-chat-message-answer-result.util';+import { RUN_WORKFLOW_JOB_NAME } from 'src/modules/workflow/workflow-runner/constants/run-workflow-job-name';+import { type RunWorkflowJobData } from 'src/modules/workflow/workflow-runner/types/run-workflow-job-data.type';+import { buildRunWorkflowJobOptions } from 'src/modules/workflow/workflow-runner/utils/build-run-workflow-job-options.util';+import { WorkflowRunStepLogWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service';+import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';++type WorkflowStepCallerInput = Parameters<+ AgentRunCallerHandler['getWaitingState']+>[0];++// A step waiting on a CALLBACK takes the outcome of what it handed its work to: the agent run the+// engine continued, or the answer to a call the step posted itself+@Injectable()+export class WorkflowAgentRunCallerHandlerWorkspaceService+ implements AgentRunCallerHandler, OnModuleInit+{+ readonly callerType = 'WORKFLOW_STEP';++ constructor(+ private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,+ private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,+ private readonly workflowRunStepLogService: WorkflowRunStepLogWorkspaceService,+ private readonly workflowExecutionContextService: WorkflowExecutionContextService,+ private readonly workflowAgentConversationService: WorkflowAgentConversationWorkspaceService,+ @InjectMessageQueue(MessageQueue.workflowQueue)+ private readonly messageQueueService: MessageQueueService,+ ) {}++ onModuleInit(): void {+ this.callerHandlerRegistry.register(this);+ }++ async buildExecutionContext({+ workspaceId,+ caller,+ }: WorkflowStepCallerInput): Promise<AgentRunExecutionContext> {+ return buildWorkflowAgentRunExecutionContext(+ await this.workflowExecutionContextService.getExecutionContext({+ workflowRunId: caller.ref.workflowRunId,+ workspaceId,+ }),+ );+ }++ async resolveTurnAuthor({+ workspaceId,+ caller,+ }: WorkflowStepCallerInput): Promise<ActorMetadata> {+ return this.workflowAgentConversationService.findTurnCreatedBy({+ workflowRunId: caller.ref.workflowRunId,+ workspaceId,+ });+ }++ // a step handing its work off still runs until the executor marks it pending, and an outcome may come first+ async getWaitingState({+ workspaceId,+ caller,+ }: WorkflowStepCallerInput): Promise<AgentRunCallerWaitingState> {+ const workflowRun = await this.workflowRunWorkspaceService.getWorkflowRun({+ workflowRunId: caller.ref.workflowRunId,+ workspaceId,+ });+ const stepInfo = workflowRun?.state?.stepInfos?.[caller.ref.stepId];++ if (workflowRun?.status !== WorkflowRunStatus.RUNNING) {+ return 'GONE';+ }++ if (stepInfo?.status === StepStatus.RUNNING) {+ return 'NOT_READY';+ }++ return stepInfo?.status === StepStatus.PENDING && !isDefined(stepInfo.error)+ ? 'WAITING'+ : 'GONE';+ }++ // The run job ends the step with the outcome through the executor's usual path, so a failure is+ // retried or continues on failure like any failed step+ async onOutcome({+ workspaceId,+ caller: {+ ref: { workflowRunId, stepId },+ },+ threadId,+ outcome,+ summary,+ }: Parameters<AgentRunCallerHandler['onOutcome']>[0]): Promise<void> {+ if (isDefined(summary)) {+ await this.workflowRunStepLogService.setAiAgentStepLog({+ workflowRunId,+ workspaceId,+ stepId,+ summary,+ threadId,+ });+ }++ // the step stays pending until the job claims it, so the run must not stay running without one+ try {+ const actionOutput =+ outcome.status === 'FAILED'+ ? { error: outcome.error }+ : {+ result: await this.buildStepResult({+ workspaceId,+ workflowRunId,+ stepId,+ threadId,+ result: outcome.result,+ }),+ };++ await this.messageQueueService.add<RunWorkflowJobData>(+ RUN_WORKFLOW_JOB_NAME,+ {+ workspaceId,+ workflowRunId,+ awaitedStepOutput: { stepId, actionOutput },+ },+ buildRunWorkflowJobOptions(workflowRunId),+ );+ } catch (error) {+ await this.workflowRunWorkspaceService.endWorkflowRun({+ workflowRunId,+ workspaceId,+ status: WorkflowRunStatus.FAILED,+ error: 'The run could not resume after its step was handed its outcome',+ });++ throw error;+ }+ }++ // the member's answer already ran the call, so a Send Message step only reports it+ private async buildStepResult({+ workspaceId,+ workflowRunId,+ stepId,+ threadId,+ result,+ }: {+ workspaceId: string;+ workflowRunId: string;+ stepId: string;+ threadId: string;+ result: object;+ }): Promise<object> {+ const workflowRun = await this.workflowRunWorkspaceService.getWorkflowRun({+ workflowRunId,+ workspaceId,+ });+ const step = workflowRun?.state?.flow?.steps?.find(+ (candidateStep) => candidateStep.id === stepId,+ );++ return isDefined(step) && isWorkflowSendChatMessageAction(step)+ ? buildSendChatMessageAnswerResult({+ threadId,+ toolResult: result as Record<string, unknown>,+ })+ : result;+ }+}diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-awaited-tool-call-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-awaited-tool-call-handler.workspace-service.tsdeleted file mode 100644index f24feb7ca62..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-awaited-tool-call-handler.workspace-service.ts+++ /dev/null@@ -1,118 +0,0 @@-import { Injectable, type OnModuleInit } from '@nestjs/common';--import { isDefined } from 'twenty-shared/utils';--import { type ToolCallWorkflowStep } from 'src/engine/metadata-modules/ai/ai-history/types/tool-call-workflow-step.type';-import { AwaitedToolCallHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/awaited-tool-call-handler-registry.service';-import {- type AwaitedToolCallHandler,- type AwaitedToolCallWaiter,-} from 'src/engine/metadata-modules/ai/ai-tool-call-answer/types/awaited-tool-call-handler.type';-import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';-import {- WorkflowRunException,- WorkflowRunExceptionCode,-} from 'src/modules/workflow/workflow-runner/exceptions/workflow-run.exception';-import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';-import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service';--@Injectable()-export class WorkflowAwaitedToolCallHandlerWorkspaceService- implements AwaitedToolCallHandler, OnModuleInit-{- constructor(- private readonly registry: AwaitedToolCallHandlerRegistryService,- private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,- private readonly workflowRunnerWorkspaceService: WorkflowRunnerWorkspaceService,- ) {}-- onModuleInit(): void {- this.registry.register(this);- }-- async findWaiter({- workspaceId,- threadId,- workflowStep: { workflowRunId, stepId },- }: {- workspaceId: string;- threadId: string;- workflowStep: ToolCallWorkflowStep;- }): Promise<AwaitedToolCallWaiter> {- try {- return await this.findWaiterOfExistingRun({- workspaceId,- threadId,- workflowRunId,- stepId,- });- } catch (error) {- if (- error instanceof WorkflowRunException &&- error.code === WorkflowRunExceptionCode.WORKFLOW_RUN_NOT_FOUND- ) {- return { status: 'gone' };- }-- throw error;- }- }-- private async findWaiterOfExistingRun({- workspaceId,- threadId,- workflowRunId,- stepId,- }: {- workspaceId: string;- threadId: string;- workflowRunId: string;- stepId: string;- }): Promise<AwaitedToolCallWaiter> {- const step = await this.workflowRunWorkspaceService.findStepAwaitingAnswer({- threadId,- workflowRunId,- workspaceId,- expectedStepId: stepId,- });-- if (isDefined(step)) {- return {- status: 'ready',- resume: (toolResult) =>- this.workflowRunnerWorkspaceService.resumeAnsweredStep({- workspaceId,- workflowRunId,- step,- threadId,- toolResult,- }),- };- }-- const isStepStillRunning =- await this.workflowRunWorkspaceService.isStepStillRunning({- stepId,- threadId,- workflowRunId,- workspaceId,- });-- return { status: isStepStillRunning ? 'not_ready' : 'gone' };- }-- async failWaiter({- workspaceId,- workflowStep: { workflowRunId },- }: {- workspaceId: string;- workflowStep: ToolCallWorkflowStep;- }): Promise<void> {- await this.workflowRunWorkspaceService.endWorkflowRun({- workflowRunId,- workspaceId,- status: WorkflowRunStatus.FAILED,- error: 'The run could not resume after its question was answered',- });- }-}diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.tsindex e1025d84bc4..9ac6a30f49e 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-runner.workspace-service.ts@@ -22,13 +22,8 @@ import { WorkflowVersionStepOperationsWorkspaceService } from 'src/modules/workfimport { WorkflowStepExecutorException } from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception';import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.service';import { type WorkflowExecutionContext } from 'src/modules/workflow/workflow-executor/types/workflow-execution-context.type';-import {- type WorkflowFormAction,- type WorkflowAction,-} from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';+import { type WorkflowFormAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';import { isWorkflowFormAction } from 'src/modules/workflow/workflow-executor/workflow-actions/form/guards/is-workflow-form-action.guard';-import { isWorkflowSendChatMessageAction } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/guards/is-workflow-send-chat-message-action.guard';-import { buildSendChatMessageAnswerResult } from 'src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/utils/build-send-chat-message-answer-result.util';import {WorkflowRunException,WorkflowRunExceptionCode,@@ -111,56 +106,6 @@ export class WorkflowRunnerWorkspaceService {);}- // a Send Message step completes with the answer; an agent step stays PENDING until the resume job claims it- async resumeAnsweredStep({- workspaceId,- workflowRunId,- step,- threadId,- toolResult,- }: {- workspaceId: string;- workflowRunId: string;- step: WorkflowAction;- threadId: string;- toolResult: Record<string, unknown>;- }): Promise<void> {- // the member's answer already ran the call, so the step only reports it- if (isWorkflowSendChatMessageAction(step)) {- const hasCompletedStep =- await this.workflowRunWorkspaceService.updateStepInfoIfPending({- stepId: step.id,- stepInfo: {- status: StepStatus.SUCCESS,- result: buildSendChatMessageAnswerResult({ threadId, toolResult }),- },- expectedThreadId: threadId,- workspaceId,- workflowRunId,- });-- if (hasCompletedStep) {- await this.resume({- workspaceId,- workflowRunId,- lastExecutedStepId: step.id,- });- }-- return;- }-- await this.messageQueueService.add<RunWorkflowJobData>(- RunWorkflowJob.name,- {- workspaceId,- workflowRunId,- stepToResume: { stepId: step.id, threadId },- },- buildRunWorkflowJobOptions(workflowRunId),- );- }-async submitFormStep({workspaceId,workflowRunId,diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.tsindex e6e71e4acca..cbd49ac511b 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts@@ -1,5 +1,6 @@import { StepStatus } from 'twenty-shared/workflow';+import { PendingWakeUpEventRecordService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service';import { PendingWakeUpOwnerHandlerRegistryService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service';import { PendingWakeUpResolverService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service';import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';@@ -32,18 +33,14 @@ const buildService = ({storedWait = STORED_WAIT,runStatus = WorkflowRunStatus.RUNNING,stepStatus = StepStatus.PENDING,- stepType = 'WAIT_FOR_EVENT',readableRecords = [READABLE_RECORD],isRecordReadFailing = false,- resolveWait,}: {storedWait?: object | null;runStatus?: WorkflowRunStatus;stepStatus?: StepStatus;- stepType?: string;readableRecords?: object[];isRecordReadFailing?: boolean;- resolveWait?: jest.Mock;} = {}) => {const pendingWakeUpService = {find: jest.fn().mockResolvedValue(storedWait),@@ -54,16 +51,18 @@ const buildService = ({getWorkflowRun: jest.fn().mockResolvedValue({status: runStatus,state: {- flow: { steps: [{ id: STEP_ID, type: stepType }] },- stepInfos: { [STEP_ID]: { status: stepStatus, threadId: 'thread-id' } },+ flow: { steps: [{ id: STEP_ID, type: 'WAIT_FOR_EVENT' }] },+ stepInfos: { [STEP_ID]: { status: stepStatus } },},}),- updateStepInfoIfPending: jest.fn().mockResolvedValue(true),+ updateStepInfoIfPending: jest+ .fn()+ .mockResolvedValue(+ runStatus === WorkflowRunStatus.RUNNING &&+ stepStatus === StepStatus.PENDING,+ ),endWorkflowRun: jest.fn(),};- const workflowActionFactory = {- get: jest.fn().mockReturnValue({ execute: jest.fn(), resolveWait }),- };const workflowExecutionContextService = {getExecutionContext: jest.fn().mockResolvedValue({authContext: { type: 'system' },@@ -86,9 +85,8 @@ const buildService = ({new WorkflowStepPendingWakeUpHandlerWorkspaceService(registry,workflowRunWorkspaceService as never,- workflowActionFactory as never,workflowExecutionContextService as never,- findRecordsService as never,+ new PendingWakeUpEventRecordService(findRecordsService as never),messageQueueService as never,).onModuleInit();@@ -288,64 +286,22 @@ describe('WorkflowStepPendingWakeUpHandlerWorkspaceService', () => {);});- it('runs a step again on its conversation when the step resolves the wait itself', async () => {- const resolveWait = jest- .fn()- .mockResolvedValue({ resumedThreadId: 'thread-id' });- const { service, workflowRunWorkspaceService, messageQueueService } =- buildService({ stepType: 'AI_AGENT', resolveWait });-- await service.resolve({- workspaceId: WORKSPACE_ID,- wakeUpId: WAIT_ID,- event: EVENT,- });-- expect(resolveWait).toHaveBeenCalledWith(- expect.objectContaining({- outcome: {- type: 'EVENT_RECEIVED',- event: { ...EVENT, record: READABLE_RECORD },- },- }),- );- expect(- workflowRunWorkspaceService.updateStepInfoIfPending,- ).not.toHaveBeenCalled();- expect(messageQueueService.add).toHaveBeenCalledWith(- RUN_WORKFLOW_JOB_NAME,- {- workspaceId: WORKSPACE_ID,- workflowRunId: WORKFLOW_RUN_ID,- stepToResume: { stepId: STEP_ID, threadId: 'thread-id' },- },- expect.anything(),- );- });-it('removes the wait of a run that is no longer running without resuming it', async () => {- const {- service,- pendingWakeUpService,- workflowRunWorkspaceService,- messageQueueService,- } = buildService({ runStatus: WorkflowRunStatus.STOPPED });+ const { service, pendingWakeUpService, messageQueueService } = buildService(+ { runStatus: WorkflowRunStatus.STOPPED },+ );await service.resolve({ workspaceId: WORKSPACE_ID, wakeUpId: WAIT_ID });expect(pendingWakeUpService.claim).toHaveBeenCalled();- expect(- workflowRunWorkspaceService.updateStepInfoIfPending,- ).not.toHaveBeenCalled();expect(messageQueueService.add).not.toHaveBeenCalled();});it('fails the run when the step cannot resume, since its wait is gone', async () => {- const resolveWait = jest.fn().mockRejectedValue(new Error('boom'));- const { service, workflowRunWorkspaceService } = buildService({- stepType: 'AI_AGENT',- resolveWait,- });+ const { service, workflowRunWorkspaceService, messageQueueService } =+ buildService();++ messageQueueService.add.mockRejectedValue(new Error('boom'));await expect(service.resolve({ workspaceId: WORKSPACE_ID, wakeUpId: WAIT_ID }),diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.tsindex 6b6690531af..8cf5086a9ad 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts@@ -1,6 +1,6 @@import { Injectable, Logger, type OnModuleInit } from '@nestjs/common';-import { isDefined, isPlainObject } from 'twenty-shared/utils';+import { isDefined } from 'twenty-shared/utils';import { StepStatus } from 'twenty-shared/workflow';import { type QueueJobOptions } from 'src/engine/core-modules/message-queue/drivers/interfaces/job-options.interface';@@ -8,6 +8,7 @@ import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decoraimport { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';import { type PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';+import { PendingWakeUpEventRecordService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-event-record.service';import { PendingWakeUpOwnerHandlerRegistryService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service';import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';@@ -15,12 +16,11 @@ import {type PendingWakeUpBeforeClaimDecision,type PendingWakeUpOwnerHandler,} from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';-import { FindRecordsService } from 'src/engine/core-modules/record-crud/services/find-records.service';+import { computePendingWakeUpRetryDelayMs } from 'src/engine/core-modules/pending-wake-up/utils/compute-pending-wake-up-retry-delay-ms.util';import {WorkflowRunStatus,type WorkflowRunWorkspaceEntity,} from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';-import { WorkflowActionFactory } from 'src/modules/workflow/workflow-executor/factories/workflow-action.factory';import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.service';import { RUN_WORKFLOW_JOB_NAME } from 'src/modules/workflow/workflow-runner/constants/run-workflow-job-name';import { type RunWorkflowJobData } from 'src/modules/workflow/workflow-runner/types/run-workflow-job-data.type';@@ -28,19 +28,6 @@ import { buildRunWorkflowJobOptions } from 'src/modules/workflow/workflow-runnerimport { isWorkflowRunNotFoundError } from 'src/modules/workflow/workflow-runner/utils/is-workflow-run-not-found-error.util';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';import { buildDefaultWaitResult } from 'src/modules/workflow/workflow-wait/utils/build-default-wait-result.util';-import { restrictWaitEventToReadableRecord } from 'src/modules/workflow/workflow-wait/utils/restrict-wait-event-to-readable-record.util';--const RETRY_BASE_DELAY_MS = 2_000;-const RETRY_MAX_DELAY_MS = 60_000;-const RECORD_READ_MAX_ATTEMPTS = 8;--const computeRetryDelayMs = (attempt: number) =>- Math.min(RETRY_BASE_DELAY_MS * 2 ** attempt, RETRY_MAX_DELAY_MS);--type RunRecordRead =- | { status: 'READABLE'; record: Record<string, unknown> }- | { status: 'UNREADABLE' }- | { status: 'FAILED'; error?: string };type WorkflowStepResolveContext = {workflowRun: WorkflowRunWorkspaceEntity | null;@@ -62,9 +49,8 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServiceconstructor(private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,- private readonly workflowActionFactory: WorkflowActionFactory,private readonly workflowExecutionContextService: WorkflowExecutionContextService,- private readonly findRecordsService: FindRecordsService,+ private readonly pendingWakeUpEventRecordService: PendingWakeUpEventRecordService,@InjectMessageQueue(MessageQueue.workflowQueue)private readonly messageQueueService: MessageQueueService,) {}@@ -102,47 +88,28 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServicereturn {type: 'RETRY_LATER',attempt: attempt + 1,- delayMs: computeRetryDelayMs(attempt),+ delayMs: computePendingWakeUpRetryDelayMs(attempt),};}- // a record the run cannot read must not resume it, so the wait goes on for another eventif (isRunRunning && stepStatus === StepStatus.PENDING && isDefined(event)) {- const recordRead = await this.readRecordAsRun({- workspaceId,- workflowRunId,- event,- });-- // a failed read cannot tell a transient error from an object the run cannot read, so it is retried a few times- if (recordRead.status === 'FAILED') {- if (recordReadAttempt < RECORD_READ_MAX_ATTEMPTS) {- return {- type: 'RETRY_LATER',- recordReadAttempt: recordReadAttempt + 1,- delayMs: computeRetryDelayMs(recordReadAttempt),- };- }-- this.logger.warn(- `Wait ${wakeUp.id} of workflow run ${workflowRunId} kept waiting after its ${event.eventName} record could not be read: ${recordRead.error}`,- );+ const { authContext, rolePermissionConfig } =+ await this.workflowExecutionContextService.getExecutionContext({+ workflowRunId,+ workspaceId,+ });- return { type: 'IGNORE' };- }-- if (recordRead.status === 'UNREADABLE') {- return { type: 'IGNORE' };- }-- return {- type: 'RESOLVE',- event: restrictWaitEventToReadableRecord({- event,- readableRecord: recordRead.record,- }),+ return this.pendingWakeUpEventRecordService.decideOnEventRecord({+ event,+ authContext,+ rolePermissionConfig,+ recordReadAttempt,context: { workflowRun },- };+ onRecordReadGivenUp: (error) =>+ this.logger.warn(+ `Wait ${wakeUp.id} of workflow run ${workflowRunId} kept waiting after its ${event.eventName} record could not be read: ${error}`,+ ),+ });}return { type: 'RESOLVE', context: { workflowRun } };@@ -161,15 +128,32 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServicereturn;}- const { workspaceId, ownerId: workflowRunId } = claimedWakeUp;+ const {+ workspaceId,+ ownerId: workflowRunId,+ ownerKey: stepId,+ } = claimedWakeUp;// the claimed wait is gone, so a step that cannot resume would wait forevertry {- await this.resolveClaimedWait({- claimedWakeUp,- workflowRun,- outcome,- });+ const hasCompletedStep =+ await this.workflowRunWorkspaceService.updateStepInfoIfPending({+ stepId,+ stepInfo: {+ status: StepStatus.SUCCESS,+ result: buildDefaultWaitResult(outcome),+ },+ workflowRunId,+ workspaceId,+ });++ if (hasCompletedStep) {+ await this.messageQueueService.add<RunWorkflowJobData>(+ RUN_WORKFLOW_JOB_NAME,+ { workspaceId, workflowRunId, lastExecutedStepId: stepId },+ buildRunWorkflowJobOptions(workflowRunId),+ );+ }} catch (error) {if (isWorkflowRunNotFoundError(error)) {return;@@ -186,121 +170,4 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceServicethrow error;}}-- private async resolveClaimedWait({- claimedWakeUp,- workflowRun,- outcome,- }: {- claimedWakeUp: PendingWakeUpEntity;- workflowRun: WorkflowRunWorkspaceEntity;- outcome: PendingWakeUpOutcome;- }): Promise<void> {- const {- workspaceId,- ownerId: workflowRunId,- ownerKey: stepId,- } = claimedWakeUp;-- const step = workflowRun.state?.flow?.steps?.find(- (candidate) => candidate.id === stepId,- );- const stepInfo = workflowRun.state?.stepInfos?.[stepId];-- if (- workflowRun.status !== WorkflowRunStatus.RUNNING ||- !isDefined(step) ||- stepInfo?.status !== StepStatus.PENDING- ) {- return;- }-- const workflowAction = this.workflowActionFactory.get(step.type);-- const resolution = isDefined(workflowAction.resolveWait)- ? await workflowAction.resolveWait({- step,- stepInfo,- outcome,- runInfo: { workflowRunId, workspaceId },- })- : { result: buildDefaultWaitResult(outcome) };-- if ('resumedThreadId' in resolution) {- await this.enqueueRunWorkflowJob({- workspaceId,- workflowRunId,- stepToResume: { stepId, threadId: resolution.resumedThreadId },- });-- return;- }-- const hasCompletedStep =- await this.workflowRunWorkspaceService.updateStepInfoIfPending({- stepId,- stepInfo: { status: StepStatus.SUCCESS, result: resolution.result },- workflowRunId,- workspaceId,- });-- if (hasCompletedStep) {- await this.enqueueRunWorkflowJob({- workspaceId,- workflowRunId,- lastExecutedStepId: stepId,- });- }- }-- private async readRecordAsRun({- workspaceId,- workflowRunId,- event,- }: {- workspaceId: string;- workflowRunId: string;- event: PendingWakeUpEvent;- }): Promise<RunRecordRead> {- const [objectName, action] = event.eventName.split('.');-- const { authContext, rolePermissionConfig } =- await this.workflowExecutionContextService.getExecutionContext({- workflowRunId,- workspaceId,- });-- const { success, result, error } = await this.findRecordsService.execute({- objectName,- // a deleted record only reads when the filter asks for deleted records- filter:- action === 'deleted'- ? { id: { eq: event.recordId }, deletedAt: { is: 'NOT_NULL' } }- : { id: { eq: event.recordId } },- limit: 1,- authContext,- rolePermissionConfig,- shouldBuildEffectiveSelectFields: false,- });-- if (!success) {- return { status: 'FAILED', error };- }-- const record = result?.records[0];-- return isPlainObject(record)- ? { status: 'READABLE', record }- : { status: 'UNREADABLE' };- }-- private async enqueueRunWorkflowJob(- jobData: RunWorkflowJobData,- ): Promise<void> {- await this.messageQueueService.add<RunWorkflowJobData>(- RUN_WORKFLOW_JOB_NAME,- jobData,- buildRunWorkflowJobOptions(jobData.workflowRunId),- );- }}diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.tsindex d06dcfb73f3..752ac3e36b7 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts@@ -8,7 +8,7 @@ import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/seexport class WorkflowStepWaitWorkspaceService {constructor(private readonly pendingWakeUpService: PendingWakeUpService) {}- // An answer wait is resolved through the step's conversation, so it is not stored+ // A callback wait is resolved by what the step handed its work to, so it is not storedasync arm({workspaceId,workflowRunId,@@ -20,7 +20,7 @@ export class WorkflowStepWaitWorkspaceService {stepId: string;wait: WorkflowStepWait;}): Promise<void> {- if (wait.type === 'ANSWER') {+ if (wait.type === 'CALLBACK') {return;}diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution-input.type.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution-input.type.tsdeleted file mode 100644index f372cf80b5c..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution-input.type.ts+++ /dev/null@@ -1,12 +0,0 @@-import { type WorkflowRunStepInfo } from 'twenty-shared/workflow';--import { type WorkflowRunInfo } from 'src/modules/workflow/workflow-executor/types/workflow-action-input.type';-import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';-import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';--export type WorkflowWaitResolutionInput = {- step: WorkflowAction;- stepInfo: WorkflowRunStepInfo;- outcome: PendingWakeUpOutcome;- runInfo: WorkflowRunInfo;-};diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution.type.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution.type.tsdeleted file mode 100644index 66a157bac9c..00000000000--- a/packages/twenty-server/src/modules/workflow/workflow-wait/types/workflow-wait-resolution.type.ts+++ /dev/null@@ -1,4 +0,0 @@-export type WorkflowWaitResolution =- | { result: object }- // The step runs again on its conversation, where it recorded the outcome- | { resumedThreadId: string };diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-wait.module.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-wait.module.tsindex 0c4b88ed8db..6480c90b524 100644--- a/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-wait.module.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-wait.module.ts@@ -1,19 +1,15 @@import { Module } from '@nestjs/common';import { PendingWakeUpModule } from 'src/engine/core-modules/pending-wake-up/pending-wake-up.module';-import { RecordCrudModule } from 'src/engine/core-modules/record-crud/record-crud.module';import { WorkflowExecutionContextModule } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.module';-import { WorkflowExecutorModule } from 'src/modules/workflow/workflow-executor/workflow-executor.module';import { WorkflowRunModule } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.module';import { WorkflowStepPendingWakeUpHandlerWorkspaceService } from 'src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service';@Module({imports: [PendingWakeUpModule,- WorkflowExecutorModule,WorkflowRunModule,WorkflowExecutionContextModule,- RecordCrudModule,],providers: [WorkflowStepPendingWakeUpHandlerWorkspaceService],})diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.tsindex 998871697b6..4fa0adf1f69 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts@@ -1117,7 +1117,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const fixture = await createFixture({ steps: [agent, finalStep] });const runId = await runFixture(fixture);const run = await waitForStep(runId, agent.id, 'PENDING');- const threadId: string = run.state.stepInfos[agent.id].threadId;+ const threadId: string = run.stepLogs[agent.id].details.threadId;return { runId, agent, finalStep, threadId };};@@ -1164,7 +1164,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const runId = await runFixture(fixture);const run = await waitForRun(runId, 'COMPLETED');- const threadId: string = run.state.stepInfos[agent.id].threadId;+ const threadId: string = run.stepLogs[agent.id].details.threadId;expect(run.state.stepInfos[agent.id]).toMatchObject({status: 'SUCCESS',@@ -1182,6 +1182,59 @@ describe('core workflow execution and queue compatibility (e2e)', () => {).toEqual(['user', 'assistant']);});+ it('keeps the usage and the conversation in the step log when its tool calls outgrow it', async () => {+ const largeOutput = 'x'.repeat(64_000);+ const toolCallIds = ['find-1', 'find-2', 'find-3', 'find-4', 'find-5'];++ jest+ .spyOn(+ getAppProviderByClassName<AgentAsyncExecutorService>(+ 'AgentAsyncExecutorService',+ ),+ 'executeAgent',+ )+ .mockResolvedValueOnce(+ agentResult({+ result: { response: 'Quote sent' },+ steps: [+ {+ content: toolCallIds.flatMap((toolCallId) => [+ {+ type: 'tool-call',+ toolCallId,+ toolName: 'find_companies',+ input: {},+ },+ {+ type: 'tool-result',+ toolCallId,+ toolName: 'find_companies',+ input: {},+ output: { success: true, result: largeOutput },+ },+ ]),+ },+ ] as AgentExecutionResult['steps'],+ }),+ );+ const finalStep = emptyStep();+ const agent = agentStep([finalStep.id]);+ const fixture = await createFixture({ steps: [agent, finalStep] });+ const runId = await runFixture(fixture);++ const run = await waitForRun(runId, 'COMPLETED');++ expect(run.stepLogs[agent.id]).toMatchObject({+ details: {+ type: 'AI_AGENT',+ usage: { totalTokens: 2 },+ toolCalls: [],+ threadId: expect.any(String),+ },+ truncated: { droppedEntries: toolCallIds.length },+ });+ });+it('continues the conversation its key names across runs', async () => {const executeAgent = jest.spyOn(@@ -1209,9 +1262,9 @@ describe('core workflow execution and queue compatibility (e2e)', () => {await runFixture(fixture),'COMPLETED',);- const threadId: string = firstRun.state.stepInfos[agent.id].threadId;+ const threadId: string = firstRun.stepLogs[agent.id].details.threadId;- expect(secondRun.state.stepInfos[agent.id].threadId).toBe(threadId);+ expect(secondRun.stepLogs[agent.id].details.threadId).toBe(threadId);expect(executeAgent.mock.calls[0][0].priorMessages).toEqual([]);expect(JSON.stringify(executeAgent.mock.calls[1][0].priorMessages),@@ -1246,7 +1299,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const runId = await runFixture(fixture);const run = await waitForStep(runId, agent.id, 'PENDING');- const threadId: string = run.state.stepInfos[agent.id].threadId;+ const threadId: string = run.stepLogs[agent.id].details.threadId;expect(await getInboxState(threadId)).toMatchObject({workspaceMemberId: creator.workspaceMemberId,@@ -1287,7 +1340,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const runId = await runFixture(fixture);const run = await waitForStep(runId, agent.id, 'PENDING');- const threadId: string = run.state.stepInfos[agent.id].threadId;+ const threadId: string = run.stepLogs[agent.id].details.threadId;const [conversation] = await global.testDataSource.query(`SELECT "workspaceMemberId" FROM "${schema}"."agentChatThread" WHERE id = $1`,[threadId],@@ -1323,7 +1376,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {await waitForRun(runId, 'COMPLETED');});- it('retries a step that failed after its answer from the same conversation, without asking again', async () => {+ it('retries a step whose agent failed after its answer in the same conversation, which holds the answer', async () => {const executeAgent = jest.spyOn(getAppProviderByClassName<AgentAsyncExecutorService>(@@ -1347,7 +1400,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {const fixture = await createFixture({ steps: [agent] });const runId = await runFixture(fixture);const pausedRun = await waitForStep(runId, agent.id, 'PENDING');- const threadId: string = pausedRun.state.stepInfos[agent.id].threadId;+ const threadId: string = pausedRun.stepLogs[agent.id].details.threadId;expect((await answer({ threadId })).body.errors).toBeUndefined();@@ -1355,10 +1408,15 @@ describe('core workflow execution and queue compatibility (e2e)', () => {expect(run.state.stepInfos[agent.id]).toMatchObject({status: 'SUCCESS',- threadId,});+ expect(run.stepLogs[agent.id].details.threadId).toBe(threadId);expect(executeAgent).toHaveBeenCalledTimes(3);- expect(executeAgent.mock.calls[2][0].messages).toEqual([]);+ expect(executeAgent.mock.calls[2][0].messages).toEqual([+ { role: 'user', content: 'Draft the quote' },+ ]);+ expect(+ JSON.stringify(executeAgent.mock.calls[2][0].priorMessages),+ ).toContain('Send it');expect(await getToolCalls(threadId)).toMatchObject({isWaiting: false,calls: [{ toolCallId: 'ask-1', status: 'answered' }],@@ -1389,7 +1447,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {}});- it('pauses the run on the question and resumes the same conversation with the answer', async () => {+ it('pauses the run on the question and lets the engine continue the agent, without running the step again', async () => {const executeAgent = mockAgent();const { runId, agent, finalStep, threadId } = await startAskingRun();@@ -1411,7 +1469,10 @@ describe('core workflow execution and queue compatibility (e2e)', () => {expect(run.state.stepInfos[agent.id]).toMatchObject({status: 'SUCCESS',result: { response: 'Quote sent' },+ });+ expect(run.stepLogs[agent.id].details).toMatchObject({threadId,+ usage: { totalTokens: 4 },});expect(run.state.stepInfos[finalStep.id].status).toBe('SUCCESS');@@ -1458,6 +1519,130 @@ describe('core workflow execution and queue compatibility (e2e)', () => {]);});+ it('continues an agent that paused on a wait once its wake-up resolves, without running the step again', async () => {+ const resumeAt = new Date(Date.now() + 1_000).toISOString();+ const executeAgent = jest+ .spyOn(+ getAppProviderByClassName<AgentAsyncExecutorService>(+ 'AgentAsyncExecutorService',+ ),+ 'executeAgent',+ )+ .mockResolvedValueOnce(+ agentResult({+ isPaused: true,+ steps: [+ {+ content: [+ {+ type: 'tool-call',+ toolCallId: 'wait-1',+ toolName: 'wait_for_duration',+ input: { durationInMinutes: 1 },+ },+ {+ type: 'tool-result',+ toolCallId: 'wait-1',+ toolName: 'wait_for_duration',+ input: { durationInMinutes: 1 },+ output: {+ success: true,+ message: `Waiting until ${resumeAt}.`,+ result: {+ status: 'pending',+ wait: { type: 'TIME', resumeAt },+ },+ },+ },+ ],+ toolResults: [+ {+ toolCallId: 'wait-1',+ toolName: 'wait_for_duration',+ output: {+ success: true,+ result: {+ status: 'pending',+ wait: { type: 'TIME', resumeAt },+ },+ },+ },+ ],+ },+ ] as unknown as AgentExecutionResult['steps'],+ }),+ )+ .mockResolvedValueOnce(replyingResult);+ const finalStep = emptyStep();+ const agent = agentStep([finalStep.id]);+ const fixture = await createFixture({ steps: [agent, finalStep] });+ const runId = await runFixture(fixture);+ const pausedRun = await waitForStep(runId, agent.id, 'PENDING');+ const threadId: string = pausedRun.stepLogs[agent.id].details.threadId;++ expect(pausedRun.state.stepInfos[agent.id].wait).toEqual({+ type: 'CALLBACK',+ });+ expect(+ await global.testDataSource.query(+ `SELECT "ownerType", "ownerKey" FROM core."pendingWakeUp" WHERE "ownerId" = (SELECT id FROM core."agentRunSuspension" WHERE "threadId" = $1)`,+ [threadId],+ ),+ ).toEqual([{ ownerType: 'AGENT_RUN', ownerKey: 'wait-1' }]);++ const run = await waitForRun(runId, 'COMPLETED');++ expect(run.state.stepInfos[agent.id]).toMatchObject({+ status: 'SUCCESS',+ result: { response: 'Quote sent' },+ });+ expect(executeAgent).toHaveBeenCalledTimes(2);+ expect(executeAgent.mock.calls[1][0].messages).toEqual([]);+ expect(+ JSON.stringify(executeAgent.mock.calls[1][0].priorMessages),+ ).toContain('The wait is over.');+ expect(+ await global.testDataSource.query(+ `SELECT id FROM core."agentRunSuspension" WHERE "threadId" = $1`,+ [threadId],+ ),+ ).toEqual([]);+ });++ it('continues an agent step paused on a question by 2.45 once the upgrade suspends it', async () => {+ const executeAgent = mockAgent();+ const { runId, agent, threadId } = await startAskingRun();++ // the shape a step paused by 2.45 left behind+ await global.testDataSource.query(+ `DELETE FROM core."agentRunSuspension" WHERE "threadId" = $1`,+ [threadId],+ );+ await global.testDataSource.query(+ `UPDATE "${schema}"."agentMessagePart" SET "toolOutput" = "toolOutput" - 'awaitedByCaller' WHERE "toolCallId" = 'ask-1'`,+ );+ await global.testDataSource.query(+ `UPDATE "${schema}"."workflowRun" SET state = jsonb_set(state, ARRAY['stepInfos', $2::text], (state->'stepInfos'->$2::text) - 'wait' || jsonb_build_object('threadId', $3::text)) WHERE id = $1`,+ [runId, agent.id, threadId],+ );++ await getAppProviderByClassName<ProvisionedWorkspaceCommandRunner>(+ 'SuspendPausedAgentStepsCommand',+ ).runOnWorkspace({ workspaceId, options: {}, index: 0, total: 1 });++ expect((await answer({ threadId })).body.errors).toBeUndefined();++ const run = await waitForRun(runId, 'COMPLETED');++ expect(run.state.stepInfos[agent.id]).toMatchObject({+ status: 'SUCCESS',+ result: { response: 'Quote sent' },+ });+ expect(executeAgent).toHaveBeenCalledTimes(2);+ expect(executeAgent.mock.calls[1][0].messages).toEqual([]);+ expect(run.stepLogs[agent.id].details.usage.totalTokens).toBe(4);+ });+it('waits for every question asked at once and resumes once all are answered', async () => {const askCall = (toolCallId: string) => [{@@ -1555,7 +1740,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {);});- it('keeps the run alive while the resume is queued, even once a parallel branch has finished', async () => {+ it('keeps the run alive while the agent continuation is queued, even once a parallel branch has finished', async () => {const executeAgent = mockAgent();const agent = agentStep([]);const parallelBranch = emptyStep();@@ -1568,23 +1753,30 @@ describe('core workflow execution and queue compatibility (e2e)', () => {await waitForStep(runId, parallelBranch.id, 'SUCCESS');- const threadId: string = pausedRun.state.stepInfos[agent.id].threadId;+ const threadId: string = pausedRun.stepLogs[agent.id].details.threadId;const queue = global.app.get<MessageQueueService>(getQueueToken(MessageQueue.workflowQueue),);+ const aiQueue = global.app.get<MessageQueueService>(+ getQueueToken(MessageQueue.aiQueue),+ );- // Holds the resume back so the run's fate is decided while it is still queued.- const enqueue = jest.spyOn(queue, 'add').mockResolvedValueOnce(undefined);+ // Holds the continuation back so the run's fate is decided while it is still queued.+ const enqueue = jest+ .spyOn(aiQueue, 'add')+ .mockResolvedValueOnce(undefined);const response = await answer({ threadId });- const [[resumeJobName, resumeJobData, resumeJobOptions]] =+ const [[continueJobName, continueJobData, continueJobOptions]] =enqueue.mock.calls;enqueue.mockRestore();expect(response.body.errors).toBeUndefined();- expect(resumeJobData).toMatchObject({- stepToResume: { stepId: agent.id, threadId },+ expect(continueJobData).toMatchObject({+ workspaceId,+ suspensionId: expect.any(String),+ resumeCount: 0,});// The decision a parallel branch finishing in that window takes.@@ -1604,16 +1796,15 @@ describe('core workflow execution and queue compatibility (e2e)', () => {expect(queuedRun.status).toBe('RUNNING');expect(queuedRun.state.stepInfos[agent.id].status).toBe('PENDING');- // Delivered twice: the second finds the step already claimed.- await queue.add(resumeJobName, resumeJobData, resumeJobOptions);- await queue.add(resumeJobName, resumeJobData, resumeJobOptions);+ // Delivered twice: the second finds the run already continued.+ await aiQueue.add(continueJobName, continueJobData, continueJobOptions);+ await aiQueue.add(continueJobName, continueJobData, continueJobOptions);const run = await waitForRun(runId, 'COMPLETED');expect(run.state.stepInfos[agent.id]).toMatchObject({status: 'SUCCESS',result: { response: 'Quote sent' },- threadId,});expect(executeAgent).toHaveBeenCalledTimes(2);});@@ -1636,7 +1827,6 @@ describe('core workflow execution and queue compatibility (e2e)', () => {expect(refused.body.errors).toBeDefined();expect((await getRun(runId)).state.stepInfos[agent.id]).toMatchObject({status: 'PENDING',- threadId,});expect((await getConversation(threadId)).messages).toHaveLength(2);expect(await getToolCalls(threadId)).toEqual({@@ -1648,14 +1838,14 @@ describe('core workflow execution and queue compatibility (e2e)', () => {await waitForRun(runId, 'COMPLETED');});- it('keeps the answer and fails the run, so it can be retried, when the resume cannot be scheduled', async () => {+ it('keeps the answer and fails the run, so it can be retried, when the agent continuation cannot be scheduled', async () => {mockAgent();const { runId, threadId } = await startAskingRun();const enqueue = jest.spyOn(global.app.get<MessageQueueService>(- getQueueToken(MessageQueue.workflowQueue),+ getQueueToken(MessageQueue.aiQueue),),'add',)diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-visibility.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-visibility.integration-spec.tsindex 25eac120c81..339c64c8bb1 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-visibility.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-visibility.integration-spec.ts@@ -24,7 +24,6 @@ import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seederimport { WORKSPACE_MEMBER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/data/constants/workspace-member-data-seeds.constant';import { type AgentRunConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service';import { type WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';-import { type WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';const client = request(`http://localhost:${APP_PORT}`);@@ -848,10 +847,6 @@ describe('core workflow visibility (e2e)', () => {workspaceId: SEED_APPLE_WORKSPACE_ID,threadId,turnId,- caller: {- type: 'WORKFLOW_STEP',- ref: { workflowRunId, stepId: 'trigger' },- },title: 'Summarize the lead',agentId: null,execution: {@@ -874,23 +869,7 @@ describe('core workflow visibility (e2e)', () => {}});- it('points the step at the conversation, which holds the prompt and the reply', async () => {- const runResponse = await workflowGraphqlRequest(- `- query FindRun($id: UUID!) {- workflowRun(filter: { id: { eq: $id } }) {- state- }- }- `,- { id: workflowRunId },- );-- expect(runResponse.body.errors).toBeUndefined();- expect(- runResponse.body.data.workflowRun.state.stepInfos.trigger.threadId,- ).toBe(threadId);-+ it('records the conversation, which holds the prompt and the reply', async () => {const response = await readConversation(APPLE_JANE_ADMIN_ACCESS_TOKEN);expect(response.body.errors).toBeUndefined();@@ -921,32 +900,6 @@ describe('core workflow visibility (e2e)', () => {expect(response.body.data?.chatThread ?? null).toBeNull();expect(response.body.errors).toBeDefined();});-- it('keeps the conversation of an attempt that is retried in the step history', async () => {- await getAppProviderByClassName<WorkflowRunWorkspaceService>(- 'WorkflowRunWorkspaceService',- ).moveStepToRetry({- stepId: 'trigger',- error: 'The agent failed',- workflowRunId,- workspaceId: SEED_APPLE_WORKSPACE_ID,- });-- const [{ state }] = await global.testDataSource.query(- `SELECT state FROM "${getWorkspaceSchemaName(SEED_APPLE_WORKSPACE_ID)}"."workflowRun" WHERE id = $1`,- [workflowRunId],- );-- expect(state.stepInfos.trigger.status).toBe('PENDING');- expect(state.stepInfos.trigger.threadId).toBeUndefined();- const { history } = state.stepInfos.trigger;-- expect(history[history.length - 1]).toMatchObject({- status: 'FAILED',- error: 'The agent failed',- threadId,- });- });});// Removing a member deletes the membership, which nulls the creator of every workflow they created.diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.tsindex af9d7387958..03c8f45848a 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts@@ -13,6 +13,7 @@ import { getAppProviderByClassName } from 'test/integration/utils/get-app-providimport { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';import { type WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';+import { type AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';import { type AgentChatService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service';import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';@@ -120,7 +121,7 @@ describe('Send chat message workflow step', () => {await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');const [{ threadId }] = await global.testDataSource.query(- `SELECT state->'stepInfos'->$2->>'threadId' AS "threadId" FROM "${SCHEMA}"."workflowRun" WHERE id = $1`,+ `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,[workflowRunId, stepId],);@@ -226,6 +227,55 @@ describe('Send chat message workflow step', () => {expect(await readEmployees()).toBe(30);}, 120000);+ it('keeps waiting when the step posts its call again', async () => {+ const { status, stepResult } = await runWorkflowActionStep({+ name: 'Post a headcount check twice',+ stepType: 'SEND_CHAT_MESSAGE',+ input: {+ workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JANE,+ title: 'Headcount check',+ text: 'Raise the headcount to 25?',+ toolCall: {+ toolName: 'update_one_company',+ arguments: { id: companyId, employees: 25 },+ },+ },+ whileRunning: async ({ workflowRunId, stepId }) => {+ await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');++ const [{ threadId }] = await global.testDataSource.query(+ `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,+ [workflowRunId, stepId],+ );++ postedThreadId = threadId;++ await expect(+ getAppProviderByClassName<AgentRunSuspensionService>(+ 'AgentRunSuspensionService',+ ).awaitCallerCall({+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ threadId,+ caller: {+ type: 'WORKFLOW_STEP',+ ref: { workflowRunId, stepId },+ },+ }),+ ).resolves.toBeUndefined();++ await answerPostedCall({+ workflowRunId,+ stepId,+ response: { decision: 'approve' },+ });+ },+ });++ expect(status).toBe('COMPLETED');+ expect(stepResult).toMatchObject({ outcome: 'executed' });+ expect(await readEmployees()).toBe(25);+ }, 120000);+it('runs nothing when the recipient rejects the action', async () => {const { status, stepResult } = await runWorkflowActionStep({name: 'Reject a headcount change',@@ -320,7 +370,7 @@ describe('Send chat message workflow step', () => {);const [{ threadId }] = await global.testDataSource.query(- `SELECT state->'stepInfos'->$2->>'threadId' AS "threadId" FROM "${SCHEMA}"."workflowRun" WHERE id = $1`,+ `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,[workflowRunId, stepId],);@@ -381,7 +431,7 @@ describe('Send chat message workflow step', () => {await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');const [{ threadId }] = await global.testDataSource.query(- `SELECT state->'stepInfos'->$2->>'threadId' AS "threadId" FROM "${SCHEMA}"."workflowRun" WHERE id = $1`,+ `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,[workflowRunId, stepId],);@@ -444,7 +494,7 @@ describe('Send chat message workflow step', () => {await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');const [{ threadId }] = await global.testDataSource.query(- `SELECT state->'stepInfos'->$2->>'threadId' AS "threadId" FROM "${SCHEMA}"."workflowRun" WHERE id = $1`,+ `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,[workflowRunId, stepId],);diff --git a/packages/twenty-shared/src/workflow/schemas/workflow-run-state-step-info-schema.ts b/packages/twenty-shared/src/workflow/schemas/workflow-run-state-step-info-schema.tsindex e801229823f..6953fd79c46 100644--- a/packages/twenty-shared/src/workflow/schemas/workflow-run-state-step-info-schema.ts+++ b/packages/twenty-shared/src/workflow/schemas/workflow-run-state-step-info-schema.ts@@ -7,7 +7,7 @@ export const workflowRunStateStepInfoSchema = z.object({error: z.any().optional(),status: workflowRunStepStatusSchema,retryAttempt: z.number().optional(),- // The agent step's conversation, kept per history entry so each loop iteration's one stays reachable.+ // Legacy: the agent step's conversation, written by runs before it moved to the step logthreadId: z.string().optional(),// What a PENDING step waits onwait: workflowStepWaitSchema.optional(),diff --git a/packages/twenty-shared/src/workflow/schemas/workflow-run-step-log-schema.ts b/packages/twenty-shared/src/workflow/schemas/workflow-run-step-log-schema.tsindex 0144945c9fb..f414aad193f 100644--- a/packages/twenty-shared/src/workflow/schemas/workflow-run-step-log-schema.ts+++ b/packages/twenty-shared/src/workflow/schemas/workflow-run-step-log-schema.ts@@ -34,6 +34,8 @@ const aiAgentStepLogDetailsSchema = z.object({nativeWebSearchCallCount: z.number(),toolCalls: z.array(aiToolCallLogSchema),durationMs: z.number(),+ // the conversation the agent ran in+ threadId: z.string().optional(),});const codeStepLogDetailsSchema = z.object({diff --git a/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts b/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.tsindex cbc75678521..ff80a1d3f3a 100644--- a/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts+++ b/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts@@ -3,7 +3,7 @@ import { z } from 'zod';import { pendingWakeUpConditionSchema } from '@/pending-wake-up/schemas/pending-wake-up-condition-schema';export const workflowStepWaitSchema = z.discriminatedUnion('type', [- // resolved when someone answers the step's conversation- z.object({ type: z.literal('ANSWER') }),+ // resolved when what the step handed its work to calls it back, such as an answer or an agent run+ z.object({ type: z.literal('CALLBACK') }),...pendingWakeUpConditionSchema.options,]);