@terreno/ai 57.8.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +2 -2
- package/README.md +2 -1
- package/bunfig.toml +1 -1
- package/dist/aiApp.d.ts +16 -5
- package/dist/aiApp.js +17 -7
- package/dist/harness/adminScreens.d.ts +6 -0
- package/dist/harness/adminScreens.js +11 -0
- package/dist/harness/agentLoop.d.ts +61 -0
- package/dist/harness/agentLoop.js +798 -0
- package/dist/harness/agentTaskNames.d.ts +4 -0
- package/dist/harness/agentTaskNames.js +7 -0
- package/dist/harness/approvalPolicy.d.ts +3 -0
- package/dist/harness/approvalPolicy.js +23 -0
- package/dist/harness/approvals.d.ts +101 -0
- package/dist/harness/approvals.js +468 -0
- package/dist/harness/commit.d.ts +238 -0
- package/dist/harness/commit.js +678 -0
- package/dist/harness/conversation.d.ts +75 -0
- package/dist/harness/conversation.js +397 -0
- package/dist/harness/defineAgent.d.ts +9 -0
- package/dist/harness/defineAgent.js +65 -0
- package/dist/harness/defineTask.d.ts +11 -0
- package/dist/harness/defineTask.js +59 -0
- package/dist/harness/defineTool.d.ts +7 -0
- package/dist/harness/defineTool.js +36 -0
- package/dist/harness/definitionError.d.ts +8 -0
- package/dist/harness/definitionError.js +15 -0
- package/dist/harness/errors.d.ts +127 -0
- package/dist/harness/errors.js +101 -0
- package/dist/harness/eventHub.d.ts +45 -0
- package/dist/harness/eventHub.js +135 -0
- package/dist/harness/events.d.ts +95 -0
- package/dist/harness/events.js +267 -0
- package/dist/harness/executionEnv.d.ts +31 -0
- package/dist/harness/executionEnv.js +2 -0
- package/dist/harness/extensions.d.ts +82 -0
- package/dist/harness/extensions.js +274 -0
- package/dist/harness/harness.d.ts +163 -0
- package/dist/harness/harness.js +520 -0
- package/dist/harness/harnessApp.d.ts +44 -0
- package/dist/harness/harnessApp.js +62 -0
- package/dist/harness/internalRuntime.d.ts +16 -0
- package/dist/harness/internalRuntime.js +18 -0
- package/dist/harness/leases.d.ts +35 -0
- package/dist/harness/leases.js +86 -0
- package/dist/harness/memo.d.ts +24 -0
- package/dist/harness/memo.js +78 -0
- package/dist/harness/modelCall.d.ts +45 -0
- package/dist/harness/modelCall.js +147 -0
- package/dist/harness/models/harnessApproval.d.ts +2 -0
- package/dist/harness/models/harnessApproval.js +100 -0
- package/dist/harness/models/harnessConversation.d.ts +2 -0
- package/dist/harness/models/harnessConversation.js +117 -0
- package/dist/harness/models/harnessEvent.d.ts +3 -0
- package/dist/harness/models/harnessEvent.js +73 -0
- package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
- package/dist/harness/models/harnessInboxEvent.js +58 -0
- package/dist/harness/models/harnessMemo.d.ts +2 -0
- package/dist/harness/models/harnessMemo.js +42 -0
- package/dist/harness/models/harnessMessage.d.ts +2 -0
- package/dist/harness/models/harnessMessage.js +73 -0
- package/dist/harness/models/harnessOwner.d.ts +2 -0
- package/dist/harness/models/harnessOwner.js +36 -0
- package/dist/harness/models/harnessTask.d.ts +2 -0
- package/dist/harness/models/harnessTask.js +186 -0
- package/dist/harness/ownership.d.ts +89 -0
- package/dist/harness/ownership.js +438 -0
- package/dist/harness/registry.d.ts +29 -0
- package/dist/harness/registry.js +93 -0
- package/dist/harness/retryBackoff.d.ts +31 -0
- package/dist/harness/retryBackoff.js +50 -0
- package/dist/harness/routes/approvals.d.ts +13 -0
- package/dist/harness/routes/approvals.js +93 -0
- package/dist/harness/routes/conversations.d.ts +11 -0
- package/dist/harness/routes/conversations.js +66 -0
- package/dist/harness/routes/events.d.ts +19 -0
- package/dist/harness/routes/events.js +215 -0
- package/dist/harness/routes/tasks.d.ts +11 -0
- package/dist/harness/routes/tasks.js +108 -0
- package/dist/harness/runners/inProcessRunner.d.ts +57 -0
- package/dist/harness/runners/inProcessRunner.js +196 -0
- package/dist/harness/runtime.d.ts +34 -0
- package/dist/harness/runtime.js +551 -0
- package/dist/harness/subagent.d.ts +53 -0
- package/dist/harness/subagent.js +134 -0
- package/dist/harness/suspend.d.ts +8 -0
- package/dist/harness/suspend.js +16 -0
- package/dist/harness/transaction.d.ts +3 -0
- package/dist/harness/transaction.js +22 -0
- package/dist/harness/waits.d.ts +100 -0
- package/dist/harness/waits.js +214 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +45 -2
- package/dist/models/aiDataset.d.ts +2 -0
- package/dist/models/aiDataset.js +67 -0
- package/dist/models/aiRequest.js +1 -1
- package/dist/models/gptHistory.js +88 -2
- package/dist/observability/adminScreens.d.ts +5 -0
- package/dist/observability/adminScreens.js +32 -0
- package/dist/observability/compileTemplate.d.ts +1 -0
- package/dist/observability/compileTemplate.js +13 -0
- package/dist/observability/datasetImport.d.ts +11 -0
- package/dist/observability/datasetImport.js +185 -0
- package/dist/observability/evaluate.d.ts +31 -0
- package/dist/observability/evaluate.js +200 -0
- package/dist/observability/evaluatorTemplates.d.ts +17 -0
- package/dist/observability/evaluatorTemplates.js +111 -0
- package/dist/observability/jsonPath.d.ts +2 -0
- package/dist/observability/jsonPath.js +57 -0
- package/dist/observability/local/datasetStore.d.ts +87 -0
- package/dist/observability/local/datasetStore.js +326 -0
- package/dist/observability/local/evaluatorStore.d.ts +44 -0
- package/dist/observability/local/evaluatorStore.js +182 -0
- package/dist/observability/local/experimentRunner.d.ts +96 -0
- package/dist/observability/local/experimentRunner.js +419 -0
- package/dist/observability/local/localPlugin.d.ts +22 -0
- package/dist/observability/local/localPlugin.js +76 -0
- package/dist/observability/local/models/obsDataset.d.ts +2 -0
- package/dist/observability/local/models/obsDataset.js +37 -0
- package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
- package/dist/observability/local/models/obsDatasetItem.js +76 -0
- package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
- package/dist/observability/local/models/obsEvaluator.js +98 -0
- package/dist/observability/local/models/obsExperiment.d.ts +2 -0
- package/dist/observability/local/models/obsExperiment.js +87 -0
- package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
- package/dist/observability/local/models/obsExperimentItem.js +45 -0
- package/dist/observability/local/models/obsPrompt.d.ts +2 -0
- package/dist/observability/local/models/obsPrompt.js +42 -0
- package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
- package/dist/observability/local/models/obsPromptLabel.js +39 -0
- package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
- package/dist/observability/local/models/obsPromptVersion.js +96 -0
- package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
- package/dist/observability/local/models/obsReviewItem.js +75 -0
- package/dist/observability/local/models/obsScore.d.ts +2 -0
- package/dist/observability/local/models/obsScore.js +58 -0
- package/dist/observability/local/models/obsSpan.d.ts +2 -0
- package/dist/observability/local/models/obsSpan.js +64 -0
- package/dist/observability/local/models/obsTrace.d.ts +2 -0
- package/dist/observability/local/models/obsTrace.js +93 -0
- package/dist/observability/local/promptStore.d.ts +180 -0
- package/dist/observability/local/promptStore.js +444 -0
- package/dist/observability/local/reviewStore.d.ts +50 -0
- package/dist/observability/local/reviewStore.js +203 -0
- package/dist/observability/local/traceStore.d.ts +103 -0
- package/dist/observability/local/traceStore.js +325 -0
- package/dist/observability/observabilityApp.d.ts +24 -0
- package/dist/observability/observabilityApp.js +157 -0
- package/dist/observability/observabilityAppRegistry.d.ts +14 -0
- package/dist/observability/observabilityAppRegistry.js +16 -0
- package/dist/observability/permissions.d.ts +7 -0
- package/dist/observability/permissions.js +60 -0
- package/dist/observability/reviewPanels.d.ts +18 -0
- package/dist/observability/reviewPanels.js +46 -0
- package/dist/observability/routes/datasets.d.ts +7 -0
- package/dist/observability/routes/datasets.js +149 -0
- package/dist/observability/routes/evaluators.d.ts +7 -0
- package/dist/observability/routes/evaluators.js +123 -0
- package/dist/observability/routes/experiments.d.ts +7 -0
- package/dist/observability/routes/experiments.js +95 -0
- package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
- package/dist/observability/routes/observabilityRouteAccess.js +67 -0
- package/dist/observability/routes/prompts.d.ts +12 -0
- package/dist/observability/routes/prompts.js +172 -0
- package/dist/observability/routes/requestAiService.d.ts +13 -0
- package/dist/observability/routes/requestAiService.js +21 -0
- package/dist/observability/routes/review.d.ts +7 -0
- package/dist/observability/routes/review.js +79 -0
- package/dist/observability/routes/status.d.ts +5 -0
- package/dist/observability/routes/status.js +31 -0
- package/dist/observability/routes/testMultiStage.d.ts +9 -0
- package/dist/observability/routes/testMultiStage.js +67 -0
- package/dist/observability/routes/traces.d.ts +7 -0
- package/dist/observability/routes/traces.js +129 -0
- package/dist/observability/schemaValidation.d.ts +5 -0
- package/dist/observability/schemaValidation.js +26 -0
- package/dist/observability/sopThresholds.d.ts +2 -0
- package/dist/observability/sopThresholds.js +21 -0
- package/dist/observability/status.d.ts +23 -0
- package/dist/observability/status.js +34 -0
- package/dist/observability/testMultiStageSchemas.d.ts +10 -0
- package/dist/observability/testMultiStageSchemas.js +79 -0
- package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
- package/dist/observability/testMultiStageWorkflow.js +218 -0
- package/dist/observability/types.d.ts +159 -0
- package/dist/observability/types.js +67 -0
- package/dist/routes/files.js +20 -7
- package/dist/routes/gpt.js +179 -488
- package/dist/routes/gptActions.d.ts +9 -0
- package/dist/routes/gptActions.js +212 -0
- package/dist/routes/gptDatasets.d.ts +5 -0
- package/dist/routes/gptDatasets.js +94 -0
- package/dist/routes/gptHistories.d.ts +3 -1
- package/dist/routes/gptHistories.js +188 -2
- package/dist/service/aiDatasets.d.ts +57 -0
- package/dist/service/aiDatasets.js +253 -0
- package/dist/service/aiService.d.ts +50 -1
- package/dist/service/aiService.js +431 -77
- package/dist/service/askFiles.d.ts +49 -0
- package/dist/service/askFiles.js +162 -0
- package/dist/service/asks.d.ts +76 -0
- package/dist/service/asks.js +284 -0
- package/dist/service/chatTurn.d.ts +128 -0
- package/dist/service/chatTurn.js +1674 -0
- package/dist/service/fileStorage.d.ts +4 -0
- package/dist/service/fileStorage.js +7 -2
- package/dist/service/fileUploadsGate.d.ts +8 -0
- package/dist/service/fileUploadsGate.js +18 -0
- package/dist/service/fillActionIds.d.ts +6 -0
- package/dist/service/fillActionIds.js +79 -0
- package/dist/service/gemini.d.ts +3 -3
- package/dist/service/gemini.js +3 -3
- package/dist/service/jsonFenceModel.d.ts +7 -0
- package/dist/service/jsonFenceModel.js +45 -0
- package/dist/service/prompts.d.ts +52 -0
- package/dist/service/prompts.js +96 -1
- package/dist/service/sanitizeHtml.d.ts +11 -0
- package/dist/service/sanitizeHtml.js +108 -0
- package/dist/service/vertex.d.ts +6 -6
- package/dist/service/vertex.js +5 -5
- package/dist/types/harness.d.ts +934 -0
- package/dist/types/harness.js +133 -0
- package/dist/types/index.d.ts +219 -13
- package/dist/types/index.js +2 -0
- package/dist/types/observability.d.ts +288 -0
- package/dist/types/observability.js +2 -0
- package/package.json +17 -5
- package/src/aiApp.test.ts +207 -1
- package/src/aiApp.ts +31 -8
- package/src/harness/adminScreens.test.ts +34 -0
- package/src/harness/adminScreens.ts +11 -0
- package/src/harness/agentLoop.test.ts +1034 -0
- package/src/harness/agentLoop.ts +1082 -0
- package/src/harness/agentRecovery.test.ts +380 -0
- package/src/harness/agentTaskNames.ts +4 -0
- package/src/harness/approvalPolicy.ts +28 -0
- package/src/harness/approvals.test.ts +1094 -0
- package/src/harness/approvals.ts +644 -0
- package/src/harness/commit.ts +1049 -0
- package/src/harness/conversation.ts +503 -0
- package/src/harness/defineAgent.test.ts +92 -0
- package/src/harness/defineAgent.ts +82 -0
- package/src/harness/defineTask.test.ts +59 -0
- package/src/harness/defineTask.ts +76 -0
- package/src/harness/defineTool.ts +45 -0
- package/src/harness/definitionError.ts +13 -0
- package/src/harness/errors.test.ts +90 -0
- package/src/harness/errors.ts +119 -0
- package/src/harness/eventHub.test.ts +124 -0
- package/src/harness/eventHub.ts +159 -0
- package/src/harness/events.test.ts +757 -0
- package/src/harness/events.ts +384 -0
- package/src/harness/executionEnv.ts +34 -0
- package/src/harness/extensions.test.ts +1218 -0
- package/src/harness/extensions.ts +382 -0
- package/src/harness/harness.test.ts +693 -0
- package/src/harness/harness.ts +767 -0
- package/src/harness/harnessApp.ts +76 -0
- package/src/harness/internalRuntime.ts +44 -0
- package/src/harness/leases.ts +132 -0
- package/src/harness/memo.ts +107 -0
- package/src/harness/modelCall.test.ts +134 -0
- package/src/harness/modelCall.ts +200 -0
- package/src/harness/models/harnessApproval.ts +109 -0
- package/src/harness/models/harnessConversation.ts +142 -0
- package/src/harness/models/harnessEvent.ts +92 -0
- package/src/harness/models/harnessInboxEvent.ts +73 -0
- package/src/harness/models/harnessMemo.ts +43 -0
- package/src/harness/models/harnessMessage.ts +85 -0
- package/src/harness/models/harnessModels.test.ts +60 -0
- package/src/harness/models/harnessOwner.ts +40 -0
- package/src/harness/models/harnessTask.ts +201 -0
- package/src/harness/ownership.test.ts +1087 -0
- package/src/harness/ownership.ts +618 -0
- package/src/harness/recovery.test.ts +912 -0
- package/src/harness/registry.ts +124 -0
- package/src/harness/retries.test.ts +292 -0
- package/src/harness/retryBackoff.test.ts +100 -0
- package/src/harness/retryBackoff.ts +77 -0
- package/src/harness/routes/approvals.ts +123 -0
- package/src/harness/routes/conversations.test.ts +518 -0
- package/src/harness/routes/conversations.ts +102 -0
- package/src/harness/routes/events.ts +280 -0
- package/src/harness/routes/tasks.test.ts +198 -0
- package/src/harness/routes/tasks.ts +146 -0
- package/src/harness/runners/inProcessRunner.test.ts +402 -0
- package/src/harness/runners/inProcessRunner.ts +220 -0
- package/src/harness/runtime.ts +714 -0
- package/src/harness/subagent.test.ts +696 -0
- package/src/harness/subagent.ts +192 -0
- package/src/harness/suspend.ts +13 -0
- package/src/harness/transaction.ts +17 -0
- package/src/harness/versioning.test.ts +288 -0
- package/src/harness/waits.test.ts +693 -0
- package/src/harness/waits.ts +356 -0
- package/src/index.test.ts +13 -0
- package/src/index.ts +70 -0
- package/src/isolated/adminScreensConfig.isolated.ts +143 -0
- package/src/models/aiDataset.test.ts +34 -0
- package/src/models/aiDataset.ts +78 -0
- package/src/models/aiRequest.test.ts +2 -2
- package/src/models/aiRequest.ts +2 -1
- package/src/models/gptHistory.test.ts +156 -0
- package/src/models/gptHistory.ts +106 -4
- package/src/observability/adminScreens.test.ts +71 -0
- package/src/observability/adminScreens.ts +42 -0
- package/src/observability/compileTemplate.test.ts +14 -0
- package/src/observability/compileTemplate.ts +10 -0
- package/src/observability/datasetImport.test.ts +95 -0
- package/src/observability/datasetImport.ts +220 -0
- package/src/observability/evaluate.test.ts +549 -0
- package/src/observability/evaluate.ts +254 -0
- package/src/observability/evaluatorTemplates.ts +124 -0
- package/src/observability/jsonPath.test.ts +52 -0
- package/src/observability/jsonPath.ts +60 -0
- package/src/observability/local/datasetStore.test.ts +230 -0
- package/src/observability/local/datasetStore.ts +452 -0
- package/src/observability/local/evaluatorStore.test.ts +138 -0
- package/src/observability/local/evaluatorStore.ts +224 -0
- package/src/observability/local/experimentRunner.test.ts +489 -0
- package/src/observability/local/experimentRunner.ts +548 -0
- package/src/observability/local/localPlugin.ts +88 -0
- package/src/observability/local/models/obsDataset.ts +38 -0
- package/src/observability/local/models/obsDatasetItem.ts +80 -0
- package/src/observability/local/models/obsEvaluator.ts +102 -0
- package/src/observability/local/models/obsExperiment.ts +91 -0
- package/src/observability/local/models/obsExperimentItem.ts +52 -0
- package/src/observability/local/models/obsPrompt.ts +43 -0
- package/src/observability/local/models/obsPromptLabel.ts +43 -0
- package/src/observability/local/models/obsPromptVersion.ts +107 -0
- package/src/observability/local/models/obsReviewItem.ts +79 -0
- package/src/observability/local/models/obsScore.ts +59 -0
- package/src/observability/local/models/obsSpan.ts +65 -0
- package/src/observability/local/models/obsTrace.ts +94 -0
- package/src/observability/local/obsModels.test.ts +119 -0
- package/src/observability/local/promptStore.test.ts +351 -0
- package/src/observability/local/promptStore.ts +665 -0
- package/src/observability/local/reviewStore.test.ts +164 -0
- package/src/observability/local/reviewStore.ts +264 -0
- package/src/observability/local/traceStore.test.ts +264 -0
- package/src/observability/local/traceStore.ts +440 -0
- package/src/observability/observabilityApp.test.ts +92 -0
- package/src/observability/observabilityApp.ts +185 -0
- package/src/observability/observabilityAppRegistry.test.ts +30 -0
- package/src/observability/observabilityAppRegistry.ts +35 -0
- package/src/observability/permissions.test.ts +96 -0
- package/src/observability/permissions.ts +78 -0
- package/src/observability/reviewPanels.test.ts +35 -0
- package/src/observability/reviewPanels.ts +64 -0
- package/src/observability/routes/datasets.test.ts +176 -0
- package/src/observability/routes/datasets.ts +252 -0
- package/src/observability/routes/evaluators.test.ts +119 -0
- package/src/observability/routes/evaluators.ts +207 -0
- package/src/observability/routes/experiments.test.ts +282 -0
- package/src/observability/routes/experiments.ts +152 -0
- package/src/observability/routes/observabilityRbac.test.ts +767 -0
- package/src/observability/routes/observabilityRouteAccess.ts +106 -0
- package/src/observability/routes/prompts.test.ts +320 -0
- package/src/observability/routes/prompts.ts +263 -0
- package/src/observability/routes/requestAiService.ts +36 -0
- package/src/observability/routes/review.test.ts +120 -0
- package/src/observability/routes/review.ts +125 -0
- package/src/observability/routes/status.test.ts +116 -0
- package/src/observability/routes/status.ts +47 -0
- package/src/observability/routes/testMultiStage.test.ts +214 -0
- package/src/observability/routes/testMultiStage.ts +96 -0
- package/src/observability/routes/traces.test.ts +167 -0
- package/src/observability/routes/traces.ts +172 -0
- package/src/observability/schemaValidation.ts +29 -0
- package/src/observability/sopThresholds.ts +20 -0
- package/src/observability/status.test.ts +38 -0
- package/src/observability/status.ts +56 -0
- package/src/observability/testMultiStageSchemas.ts +95 -0
- package/src/observability/testMultiStageWorkflow.test.ts +114 -0
- package/src/observability/testMultiStageWorkflow.ts +307 -0
- package/src/observability/types.ts +262 -0
- package/src/routes/files.test.ts +84 -9
- package/src/routes/files.ts +23 -7
- package/src/routes/gpt.test.ts +3279 -0
- package/src/routes/gpt.ts +214 -552
- package/src/routes/gptActions.test.ts +241 -0
- package/src/routes/gptActions.ts +255 -0
- package/src/routes/gptApprovals.test.ts +762 -0
- package/src/routes/gptDatasets.test.ts +86 -0
- package/src/routes/gptDatasets.ts +104 -0
- package/src/routes/gptFiles.test.ts +589 -0
- package/src/routes/gptHistories.test.ts +933 -0
- package/src/routes/gptHistories.ts +266 -5
- package/src/routes/routes.test.ts +676 -5
- package/src/service/aiDatasets.test.ts +123 -0
- package/src/service/aiDatasets.ts +316 -0
- package/src/service/aiService.test.ts +614 -0
- package/src/service/aiService.ts +575 -111
- package/src/service/askFiles.test.ts +96 -0
- package/src/service/askFiles.ts +222 -0
- package/src/service/asks.test.ts +946 -0
- package/src/service/asks.ts +423 -0
- package/src/service/chatTurn.ts +2287 -0
- package/src/service/fileStorage.test.ts +11 -0
- package/src/service/fileStorage.ts +10 -2
- package/src/service/fileUploadsGate.ts +23 -0
- package/src/service/fillActionIds.test.ts +65 -0
- package/src/service/fillActionIds.ts +98 -0
- package/src/service/gemini.test.ts +16 -10
- package/src/service/gemini.ts +3 -3
- package/src/service/jsonFenceModel.test.ts +47 -0
- package/src/service/jsonFenceModel.ts +53 -0
- package/src/service/prompts.test.ts +21 -0
- package/src/service/prompts.ts +147 -0
- package/src/service/sanitizeHtml.test.ts +61 -0
- package/src/service/sanitizeHtml.ts +109 -0
- package/src/service/vertex.test.ts +49 -49
- package/src/service/vertex.ts +7 -8
- package/src/tests/bunSetup.test.ts +7 -4
- package/src/tests/bunSetup.ts +7 -7
- package/src/tests/chatHarness.ts +478 -0
- package/src/tests/generateStream.ts +49 -0
- package/src/tests/harnessErrors.ts +22 -0
- package/src/tests/harnessStreaming.ts +227 -0
- package/src/tests/helpers.ts +9 -2
- package/src/types/harness.ts +1095 -0
- package/src/types/index.ts +237 -12
- package/src/types/observability.ts +344 -0
- package/src/types/sanitize-html.d.ts +25 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.runClaimedTask = exports.recoverExpiredTasks = exports.claimNextTask = void 0;
|
|
7
|
+
const api_1 = require("@terreno/api");
|
|
8
|
+
const luxon_1 = require("luxon");
|
|
9
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
10
|
+
const harness_1 = require("../types/harness");
|
|
11
|
+
const agentTaskNames_1 = require("./agentTaskNames");
|
|
12
|
+
const approvals_1 = require("./approvals");
|
|
13
|
+
const commit_1 = require("./commit");
|
|
14
|
+
const defineTask_1 = require("./defineTask");
|
|
15
|
+
const definitionError_1 = require("./definitionError");
|
|
16
|
+
const errors_1 = require("./errors");
|
|
17
|
+
const events_1 = require("./events");
|
|
18
|
+
const internalRuntime_1 = require("./internalRuntime");
|
|
19
|
+
const leases_1 = require("./leases");
|
|
20
|
+
const memo_1 = require("./memo");
|
|
21
|
+
const ownership_1 = require("./ownership");
|
|
22
|
+
const retryBackoff_1 = require("./retryBackoff");
|
|
23
|
+
const subagent_1 = require("./subagent");
|
|
24
|
+
const suspend_1 = require("./suspend");
|
|
25
|
+
const waits_1 = require("./waits");
|
|
26
|
+
/** Registry key of the built-in agent turn every subagent runs. */
|
|
27
|
+
const AGENT_TURN_KEY = (0, defineTask_1.taskDefinitionKey)({ name: agentTaskNames_1.AGENT_TURN_TASK_NAME, version: 1 });
|
|
28
|
+
/** Most expired tasks one recovery pass handles; the next pass picks up the rest. */
|
|
29
|
+
const RECOVERY_BATCH_SIZE = 100;
|
|
30
|
+
const assertValidNext = (definition, next) => {
|
|
31
|
+
if ("terminal" in next) {
|
|
32
|
+
const { status } = next.terminal;
|
|
33
|
+
if (status !== "completed" && status !== "failed") {
|
|
34
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: terminal status must be "completed" or "failed"`);
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!definition.phases[next.phase]) {
|
|
39
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.commit to unknown phase "${next.phase}"`);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const registeredFilter = (definitions) => {
|
|
43
|
+
return [...definitions.values()].map(({ name, version }) => ({ name, version }));
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Atomically move the oldest runnable, registered task (`pending` and due, or `waiting` on
|
|
47
|
+
* an event or sleep whose `timeoutAt` passed) to `running` and give it a fresh lease (owner, fencing token, expiry) for the phase about to start.
|
|
48
|
+
*/
|
|
49
|
+
const claimNextTask = async ({ definitions, lease, models, }) => {
|
|
50
|
+
if (definitions.size === 0) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const registered = registeredFilter(definitions);
|
|
54
|
+
const now = luxon_1.DateTime.now().toJSDate();
|
|
55
|
+
return models.task.findOneAndUpdate({
|
|
56
|
+
$and: [
|
|
57
|
+
{ $or: registered },
|
|
58
|
+
{
|
|
59
|
+
$or: [
|
|
60
|
+
{
|
|
61
|
+
$or: [{ runAt: { $exists: false } }, { runAt: null }, { runAt: { $lte: now } }],
|
|
62
|
+
status: harness_1.HARNESS_TASK_STATUSES.pending,
|
|
63
|
+
},
|
|
64
|
+
// An event wait or sleep whose timeout passed resumes like a due retry.
|
|
65
|
+
{
|
|
66
|
+
status: harness_1.HARNESS_TASK_STATUSES.waiting,
|
|
67
|
+
"waiting.kind": { $in: [harness_1.HARNESS_WAIT_KINDS.event, harness_1.HARNESS_WAIT_KINDS.sleep] },
|
|
68
|
+
"waiting.timeoutAt": { $lte: now },
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
// A task being aborted never starts another run.
|
|
74
|
+
"abortRequested.at": { $exists: false },
|
|
75
|
+
}, {
|
|
76
|
+
$set: { lease: (0, commit_1.newTaskLease)(lease), status: harness_1.HARNESS_TASK_STATUSES.running },
|
|
77
|
+
$unset: { waiting: 1 },
|
|
78
|
+
}, { returnDocument: "after", sort: { created: 1 } });
|
|
79
|
+
};
|
|
80
|
+
exports.claimNextTask = claimNextTask;
|
|
81
|
+
/**
|
|
82
|
+
* Find `running` tasks whose lease expired (their runner died or froze mid-phase) and
|
|
83
|
+
* resolve each by its current phase's `replay`: `safe` goes back to `pending` at the same
|
|
84
|
+
* checkpoint; anything else is parked `interrupted`, or failed (owner woken) when the
|
|
85
|
+
* definition sets `onInterrupt: "fail"`. Tasks of unregistered versions are
|
|
86
|
+
* left for a runner that registers them. Then wake tasks whose child wait settled
|
|
87
|
+
* without a wake (a crash between a child's outcome and its owner check). Returns how
|
|
88
|
+
* many tasks became runnable.
|
|
89
|
+
*/
|
|
90
|
+
const recoverExpiredTasks = async (engine) => {
|
|
91
|
+
const { definitions, models, testHooks } = engine;
|
|
92
|
+
if (definitions.size === 0) {
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
const expired = await models.task
|
|
96
|
+
.find({ ...(0, commit_1.expiredLeaseFilter)(luxon_1.DateTime.now()), $and: [{ $or: registeredFilter(definitions) }] })
|
|
97
|
+
.sort({ created: 1 })
|
|
98
|
+
.limit(RECOVERY_BATCH_SIZE);
|
|
99
|
+
let runnable = 0;
|
|
100
|
+
for (const task of expired) {
|
|
101
|
+
const definition = definitions.get((0, defineTask_1.taskDefinitionKey)(task));
|
|
102
|
+
const action = interruptionAction(definition, task.phase);
|
|
103
|
+
try {
|
|
104
|
+
const recovered = await (0, commit_1.commitInterruption)({ action, models, task, testHooks });
|
|
105
|
+
api_1.logger.warn(`Harness task ${task._id} (${(0, defineTask_1.taskDefinitionKey)(task)}) was interrupted in phase "${task.phase}"; ${INTERRUPTION_LOG[action]}`);
|
|
106
|
+
if (action === "replay") {
|
|
107
|
+
runnable += 1;
|
|
108
|
+
}
|
|
109
|
+
if (action === "fail") {
|
|
110
|
+
await (0, ownership_1.settleTaskOwner)({ engine, task: recovered });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
// Another runner recovered it first, or its lease was renewed after the scan.
|
|
115
|
+
if (error instanceof commit_1.HarnessCommitConflictError) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
api_1.logger.error(`Harness could not recover task ${task._id}: ${(0, errors_1.errorMessage)(error)}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return runnable + (await (0, ownership_1.sweepWaitingTasks)(engine)) + (await (0, ownership_1.sweepQueuedConversations)(engine));
|
|
122
|
+
};
|
|
123
|
+
exports.recoverExpiredTasks = recoverExpiredTasks;
|
|
124
|
+
const INTERRUPTION_LOG = {
|
|
125
|
+
fail: "failed it (not retried)",
|
|
126
|
+
park: "parked as interrupted",
|
|
127
|
+
replay: "re-running it",
|
|
128
|
+
};
|
|
129
|
+
/** What recovery does with an expired task in `phase`, by its definition's policies. */
|
|
130
|
+
const interruptionAction = (definition, phase) => {
|
|
131
|
+
if (definition?.phases[phase]?.replay === "safe") {
|
|
132
|
+
return "replay";
|
|
133
|
+
}
|
|
134
|
+
return definition?.onInterrupt === "fail" ? "fail" : "park";
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Run a claimed task's phases in order until it stops: terminal, `pending` for a retry,
|
|
138
|
+
* `waiting`, or aborted. A phase that throws is retried under the task's `retry` policy
|
|
139
|
+
* and fails the task once attempts run out. When a commit transaction fails, or the lease
|
|
140
|
+
* was lost, the task is left at its last checkpoint and expired-lease recovery decides
|
|
141
|
+
* what happens next. A terminal task re-checks the task that owns it.
|
|
142
|
+
*/
|
|
143
|
+
const runClaimedTask = async ({ engine, lease, task, }) => {
|
|
144
|
+
const definition = engine.definitions.get((0, defineTask_1.taskDefinitionKey)(task));
|
|
145
|
+
if (!definition) {
|
|
146
|
+
throw (0, errors_1.harnessError)({
|
|
147
|
+
detail: `No registered task definition for ${(0, defineTask_1.taskDefinitionKey)(task)}`,
|
|
148
|
+
kind: "notRegistered",
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
const taskId = String(task._id);
|
|
152
|
+
const controller = new AbortController();
|
|
153
|
+
engine.controllers.set(taskId, controller);
|
|
154
|
+
try {
|
|
155
|
+
let current = task;
|
|
156
|
+
let isFirstPhase = true;
|
|
157
|
+
while (current.status === harness_1.HARNESS_TASK_STATUSES.running && !controller.signal.aborted) {
|
|
158
|
+
// An abort from another process may have fenced the task since the last commit.
|
|
159
|
+
if (!isFirstPhase && !(await holdsLease(engine.models, current))) {
|
|
160
|
+
api_1.logger.info(`Harness task ${taskId} lost its lease between phases; stopping`);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
isFirstPhase = false;
|
|
164
|
+
const result = await runPhase({ controller, definition, engine, lease, task: current });
|
|
165
|
+
if (!result) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
current = result;
|
|
169
|
+
}
|
|
170
|
+
if (harness_1.HARNESS_TERMINAL_STATUSES.has(current.status)) {
|
|
171
|
+
await (0, ownership_1.settleTaskOwner)({ engine, task: current });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
engine.controllers.delete(taskId);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
exports.runClaimedTask = runClaimedTask;
|
|
179
|
+
/** Whether `task`'s current lease token is still the one stored. */
|
|
180
|
+
const holdsLease = async (models, task) => {
|
|
181
|
+
const held = await models.task.countDocuments({
|
|
182
|
+
_id: task._id,
|
|
183
|
+
"lease.token": task.lease?.token ?? null,
|
|
184
|
+
status: harness_1.HARNESS_TASK_STATUSES.running,
|
|
185
|
+
});
|
|
186
|
+
return held > 0;
|
|
187
|
+
};
|
|
188
|
+
const toObjectId = (id) => {
|
|
189
|
+
return typeof id === "string" ? new mongoose_1.default.Types.ObjectId(id) : id;
|
|
190
|
+
};
|
|
191
|
+
const runPhase = async ({ controller, definition, engine, lease, task, }) => {
|
|
192
|
+
const { models, testHooks } = engine;
|
|
193
|
+
const taskId = String(task._id);
|
|
194
|
+
const phaseStartedAt = luxon_1.DateTime.now();
|
|
195
|
+
let committed = null;
|
|
196
|
+
let commitStarted = false;
|
|
197
|
+
let commitFailure;
|
|
198
|
+
let childIndex = 0;
|
|
199
|
+
let agentIndex = 0;
|
|
200
|
+
let waitIndex = 0;
|
|
201
|
+
const heartbeat = (0, leases_1.startTaskHeartbeat)({
|
|
202
|
+
lease,
|
|
203
|
+
models,
|
|
204
|
+
onLost: () => controller.abort(new Error("Task lease lost")),
|
|
205
|
+
taskId: task._id,
|
|
206
|
+
testHooks,
|
|
207
|
+
token: task.lease?.token,
|
|
208
|
+
});
|
|
209
|
+
const assertOpen = (method) => {
|
|
210
|
+
if (commitStarted) {
|
|
211
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: ${method} called after the phase "${task.phase}" committed or started waiting`);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
/** Run one fenced write that ends the phase; record its failure for the caller. */
|
|
215
|
+
const settlePhase = async (write) => {
|
|
216
|
+
commitStarted = true;
|
|
217
|
+
// The commit decides the lease's fate: renewed under a new token, cleared, or
|
|
218
|
+
// (on failure) left to expire for recovery.
|
|
219
|
+
await heartbeat.stop();
|
|
220
|
+
try {
|
|
221
|
+
committed = await write();
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
commitFailure = error;
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const commitWithWrites = async (next, writes) => {
|
|
229
|
+
if (commitStarted) {
|
|
230
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.commit called more than once in phase "${task.phase}"`);
|
|
231
|
+
}
|
|
232
|
+
assertValidNext(definition, next);
|
|
233
|
+
await settlePhase(() => (0, commit_1.commitPhase)({ lease, models, next, phaseStartedAt, task, testHooks, writes }));
|
|
234
|
+
};
|
|
235
|
+
const commit = (next) => commitWithWrites(next);
|
|
236
|
+
const createTask = async (childDefinition, input, options = {}) => {
|
|
237
|
+
assertOpen("rt.createTask");
|
|
238
|
+
const registered = engine.definitions.get(childDefinition.key);
|
|
239
|
+
if (!registered) {
|
|
240
|
+
throw new definitionError_1.HarnessDefinitionError(`${childDefinition.key} is not in this harness registry`);
|
|
241
|
+
}
|
|
242
|
+
const key = options.key ?? String(childIndex);
|
|
243
|
+
childIndex += 1;
|
|
244
|
+
const child = await (0, commit_1.createChildTaskRecords)({
|
|
245
|
+
definition: registered,
|
|
246
|
+
input,
|
|
247
|
+
key,
|
|
248
|
+
lease,
|
|
249
|
+
models,
|
|
250
|
+
options,
|
|
251
|
+
parent: task,
|
|
252
|
+
});
|
|
253
|
+
engine.wake();
|
|
254
|
+
return String(child._id);
|
|
255
|
+
};
|
|
256
|
+
const waitForTasks = async (ids, options = {}) => {
|
|
257
|
+
assertOpen("rt.waitForTasks");
|
|
258
|
+
const policy = options.policy ?? harness_1.HARNESS_WAIT_POLICIES.all;
|
|
259
|
+
if (!Object.values(harness_1.HARNESS_WAIT_POLICIES).includes(policy)) {
|
|
260
|
+
throw new definitionError_1.HarnessDefinitionError(`rt.waitForTasks policy must be one of ${Object.values(harness_1.HARNESS_WAIT_POLICIES).join(", ")}`);
|
|
261
|
+
}
|
|
262
|
+
const childIds = [...new Set(ids.map(String))].map(toObjectId);
|
|
263
|
+
const owned = await models.task.countDocuments({
|
|
264
|
+
_id: { $in: childIds },
|
|
265
|
+
"ownership.id": task._id,
|
|
266
|
+
"ownership.kind": "task",
|
|
267
|
+
});
|
|
268
|
+
if (owned !== childIds.length) {
|
|
269
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.waitForTasks only waits on tasks this task created with rt.createTask`);
|
|
270
|
+
}
|
|
271
|
+
const { children, isSettled } = await (0, ownership_1.settleChildren)({
|
|
272
|
+
engine,
|
|
273
|
+
ids: childIds,
|
|
274
|
+
ownerId: task._id,
|
|
275
|
+
policy,
|
|
276
|
+
});
|
|
277
|
+
if (isSettled) {
|
|
278
|
+
return (0, ownership_1.childOutcomes)(ids.map(String), children);
|
|
279
|
+
}
|
|
280
|
+
await settlePhase(async () => {
|
|
281
|
+
const parked = await (0, commit_1.commitWaiting)({
|
|
282
|
+
models,
|
|
283
|
+
phaseStartedAt,
|
|
284
|
+
task,
|
|
285
|
+
testHooks,
|
|
286
|
+
waiting: { kind: harness_1.HARNESS_WAIT_KINDS.tasks, policy, taskIds: childIds },
|
|
287
|
+
});
|
|
288
|
+
return parked.task;
|
|
289
|
+
});
|
|
290
|
+
// A child may have settled between the check above and the waiting commit.
|
|
291
|
+
await (0, ownership_1.checkTaskWait)({ engine, taskId: task._id });
|
|
292
|
+
throw new suspend_1.HarnessSuspendSignal(taskId);
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Shared by `rt.waitFor`, `rt.sleep`, and `rt.approval`: resolve the call now, or park the
|
|
296
|
+
* task on it. `plan` builds the request from the call's key.
|
|
297
|
+
*/
|
|
298
|
+
const waitCall = async (method, plan) => {
|
|
299
|
+
assertOpen(method);
|
|
300
|
+
// Taken before any await, so the call's key depends only on its order in the phase.
|
|
301
|
+
const callKey = (0, waits_1.waitCallKey)({ index: waitIndex, task });
|
|
302
|
+
waitIndex += 1;
|
|
303
|
+
const { afterPark, onPark, request } = plan(callKey);
|
|
304
|
+
let result;
|
|
305
|
+
try {
|
|
306
|
+
result = await (0, waits_1.resolveWaitCall)({
|
|
307
|
+
callKey,
|
|
308
|
+
label: definition.key,
|
|
309
|
+
lease,
|
|
310
|
+
models,
|
|
311
|
+
request,
|
|
312
|
+
task,
|
|
313
|
+
testHooks,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
// A lost lease ends the run like a failed commit: nothing more may be written.
|
|
318
|
+
if (error instanceof commit_1.HarnessCommitConflictError) {
|
|
319
|
+
commitStarted = true;
|
|
320
|
+
commitFailure = error;
|
|
321
|
+
}
|
|
322
|
+
throw error;
|
|
323
|
+
}
|
|
324
|
+
if (result.isResolved) {
|
|
325
|
+
return result.payload;
|
|
326
|
+
}
|
|
327
|
+
const { entry } = result;
|
|
328
|
+
let isWoken = false;
|
|
329
|
+
await settlePhase(async () => {
|
|
330
|
+
const parked = await (0, commit_1.commitWaiting)({
|
|
331
|
+
models,
|
|
332
|
+
phaseStartedAt,
|
|
333
|
+
task,
|
|
334
|
+
testHooks,
|
|
335
|
+
waitCall: { entry, key: callKey },
|
|
336
|
+
waiting: { key: entry.key, kind: entry.kind, timeoutAt: entry.timeoutAt },
|
|
337
|
+
writes: onPark,
|
|
338
|
+
});
|
|
339
|
+
isWoken = parked.isWoken;
|
|
340
|
+
return parked.task;
|
|
341
|
+
});
|
|
342
|
+
// An event buffered before the wait committed sent the task straight back to pending.
|
|
343
|
+
if (isWoken) {
|
|
344
|
+
engine.wake();
|
|
345
|
+
}
|
|
346
|
+
await afterPark?.();
|
|
347
|
+
throw new suspend_1.HarnessSuspendSignal(taskId);
|
|
348
|
+
};
|
|
349
|
+
const requestApproval = async (key, options, extension) => {
|
|
350
|
+
assertOpen("rt.approval");
|
|
351
|
+
const timeout = (0, approvals_1.parseApprovalRequest)({ key, label: definition.key, options });
|
|
352
|
+
let approval;
|
|
353
|
+
await waitCall("rt.approval", (callKey) => {
|
|
354
|
+
approval = (0, approvals_1.approvalWaitPlan)({ callKey, extension, key, models, options, task, timeout });
|
|
355
|
+
return approval;
|
|
356
|
+
});
|
|
357
|
+
return approval.result();
|
|
358
|
+
};
|
|
359
|
+
const waitFor = async (event, options = {}) => {
|
|
360
|
+
if (typeof event !== "string" || !event.trim()) {
|
|
361
|
+
assertOpen("rt.waitFor");
|
|
362
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.waitFor requires an event name`);
|
|
363
|
+
}
|
|
364
|
+
const timeout = options?.timeout === undefined
|
|
365
|
+
? undefined
|
|
366
|
+
: (0, waits_1.parseWaitDuration)(options.timeout, `${definition.key}: rt.waitFor timeout`);
|
|
367
|
+
return waitCall("rt.waitFor", () => ({
|
|
368
|
+
request: { duration: timeout, event, kind: harness_1.HARNESS_WAIT_KINDS.event },
|
|
369
|
+
}));
|
|
370
|
+
};
|
|
371
|
+
const sleep = async (duration) => {
|
|
372
|
+
const length = (0, waits_1.parseWaitDuration)(duration, `${definition.key}: rt.sleep duration`);
|
|
373
|
+
await waitCall("rt.sleep", () => ({
|
|
374
|
+
request: { duration: length, kind: harness_1.HARNESS_WAIT_KINDS.sleep },
|
|
375
|
+
}));
|
|
376
|
+
};
|
|
377
|
+
const runAgent = async (agent, options) => {
|
|
378
|
+
assertOpen("rt.runAgent");
|
|
379
|
+
if (!options || typeof options !== "object") {
|
|
380
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.runAgent requires {input}`);
|
|
381
|
+
}
|
|
382
|
+
// Taken before any await, so the call's key depends only on its order in the phase.
|
|
383
|
+
const callIndex = agentIndex;
|
|
384
|
+
agentIndex += 1;
|
|
385
|
+
if (!agent?.name || engine.agents.get(agent.name) !== agent) {
|
|
386
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.runAgent agent "${agent?.name}" is not in this harness registry`);
|
|
387
|
+
}
|
|
388
|
+
const content = (0, subagent_1.subagentContent)(options.input);
|
|
389
|
+
if (content === undefined) {
|
|
390
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.runAgent requires non-empty input`);
|
|
391
|
+
}
|
|
392
|
+
const turn = engine.definitions.get(AGENT_TURN_KEY);
|
|
393
|
+
if (!turn) {
|
|
394
|
+
throw (0, errors_1.harnessError)({ detail: `${AGENT_TURN_KEY} is not registered`, kind: "notRegistered" });
|
|
395
|
+
}
|
|
396
|
+
const schema = (options.output ?? agent.output);
|
|
397
|
+
let outputSchema;
|
|
398
|
+
if (schema) {
|
|
399
|
+
try {
|
|
400
|
+
outputSchema = await (0, subagent_1.serializeOutputSchema)(schema);
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.runAgent output for "${agent.name}" cannot be expressed as JSON Schema: ${(0, errors_1.errorMessage)(error)}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const { conversationId, turnTask } = await (0, subagent_1.startSubagentTurn)({
|
|
407
|
+
agent,
|
|
408
|
+
callIndex,
|
|
409
|
+
content,
|
|
410
|
+
extensions: engine.extensions,
|
|
411
|
+
instructions: options.instructions,
|
|
412
|
+
lease,
|
|
413
|
+
models,
|
|
414
|
+
outputSchema,
|
|
415
|
+
parent: task,
|
|
416
|
+
turn,
|
|
417
|
+
});
|
|
418
|
+
engine.wake();
|
|
419
|
+
const [outcome] = await waitForTasks([turnTask._id]);
|
|
420
|
+
return (0, subagent_1.subagentResult)({
|
|
421
|
+
agent,
|
|
422
|
+
conversationId,
|
|
423
|
+
outcome: outcome,
|
|
424
|
+
schema,
|
|
425
|
+
});
|
|
426
|
+
};
|
|
427
|
+
const failOrRetry = async ({ error, isRetryable, }) => {
|
|
428
|
+
await heartbeat.stop();
|
|
429
|
+
const policy = (0, retryBackoff_1.resolveRetryPolicy)(task.retry);
|
|
430
|
+
const failedAttempts = task.attempt + 1;
|
|
431
|
+
try {
|
|
432
|
+
if (isRetryable && failedAttempts < policy.maxAttempts) {
|
|
433
|
+
const runAt = (0, retryBackoff_1.nextRetryAt)({ failedAttempts, policy, random: testHooks?.random });
|
|
434
|
+
const retried = await (0, commit_1.commitRetry)({
|
|
435
|
+
error,
|
|
436
|
+
failedAttempts,
|
|
437
|
+
maxAttempts: policy.maxAttempts,
|
|
438
|
+
models,
|
|
439
|
+
phaseStartedAt,
|
|
440
|
+
runAt,
|
|
441
|
+
task,
|
|
442
|
+
testHooks,
|
|
443
|
+
});
|
|
444
|
+
api_1.logger.warn(`Harness task ${taskId} phase "${task.phase}" failed (attempt ${failedAttempts} of ${policy.maxAttempts}); retrying at ${runAt.toISO()}: ${error}`);
|
|
445
|
+
return retried;
|
|
446
|
+
}
|
|
447
|
+
return await (0, commit_1.commitPhase)({
|
|
448
|
+
failedAttempts: isRetryable ? failedAttempts : undefined,
|
|
449
|
+
lease,
|
|
450
|
+
models,
|
|
451
|
+
next: { terminal: { error, status: "failed" } },
|
|
452
|
+
phaseStartedAt,
|
|
453
|
+
task,
|
|
454
|
+
testHooks,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
catch (commitError) {
|
|
458
|
+
api_1.logger.error(`Harness task ${taskId} could not record failure of phase "${task.phase}": ${(0, errors_1.errorMessage)(commitError)}`);
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
const memoFor = (scopeTaskId) => (0, memo_1.createMemo)({
|
|
463
|
+
assertWritable: () => assertOpen("rt.memo"),
|
|
464
|
+
fence: { phase: task.phase, taskId: task._id, token: task.lease?.token },
|
|
465
|
+
lease,
|
|
466
|
+
models,
|
|
467
|
+
scopeTaskId: toObjectId(scopeTaskId),
|
|
468
|
+
});
|
|
469
|
+
const output = async (text) => {
|
|
470
|
+
assertOpen("rt.output");
|
|
471
|
+
if (typeof text !== "string") {
|
|
472
|
+
throw new definitionError_1.HarnessDefinitionError(`${definition.key}: rt.output takes a string`);
|
|
473
|
+
}
|
|
474
|
+
if (!text) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
await (0, events_1.appendTaskEvents)({
|
|
478
|
+
events: [
|
|
479
|
+
{
|
|
480
|
+
payload: { attempt: task.attempt, phase: task.phase, text },
|
|
481
|
+
type: harness_1.HARNESS_EVENT_TYPES.output,
|
|
482
|
+
},
|
|
483
|
+
],
|
|
484
|
+
models,
|
|
485
|
+
task,
|
|
486
|
+
});
|
|
487
|
+
};
|
|
488
|
+
const phase = definition.phases[task.phase];
|
|
489
|
+
if (!phase) {
|
|
490
|
+
return failOrRetry({
|
|
491
|
+
error: `${definition.key}: unknown phase "${task.phase}"`,
|
|
492
|
+
isRetryable: false,
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
const rt = {
|
|
496
|
+
[internalRuntime_1.HARNESS_INTERNAL_RUNTIME]: {
|
|
497
|
+
approvalFor: (extension) => (key, options) => requestApproval(key, options, extension),
|
|
498
|
+
commitWithWrites,
|
|
499
|
+
memoFor,
|
|
500
|
+
},
|
|
501
|
+
approval: (key, options) => requestApproval(key, options),
|
|
502
|
+
commit,
|
|
503
|
+
createTask: createTask,
|
|
504
|
+
env: engine.env,
|
|
505
|
+
memo: memoFor(task._id),
|
|
506
|
+
output,
|
|
507
|
+
runAgent: runAgent,
|
|
508
|
+
signal: controller.signal,
|
|
509
|
+
sleep,
|
|
510
|
+
taskId,
|
|
511
|
+
waitFor: waitFor,
|
|
512
|
+
waitForTasks,
|
|
513
|
+
};
|
|
514
|
+
let thrown;
|
|
515
|
+
try {
|
|
516
|
+
await phase.run((0, ownership_1.toTaskView)(task), rt);
|
|
517
|
+
}
|
|
518
|
+
catch (error) {
|
|
519
|
+
thrown = { error };
|
|
520
|
+
}
|
|
521
|
+
await heartbeat.stop();
|
|
522
|
+
if (commitFailure !== undefined && controller.signal.aborted) {
|
|
523
|
+
api_1.logger.info(`Harness task ${taskId} phase "${task.phase}" was aborted; its commit was discarded`);
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
if (commitFailure !== undefined) {
|
|
527
|
+
api_1.logger.error(`Harness task ${taskId} phase "${task.phase}" commit failed; task stays at its last checkpoint: ${(0, errors_1.errorMessage)(commitFailure)}`);
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
if (committed) {
|
|
531
|
+
if (thrown && !(thrown.error instanceof suspend_1.HarnessSuspendSignal)) {
|
|
532
|
+
api_1.logger.warn(`Harness task ${taskId} phase "${task.phase}" threw after committing; keeping the checkpoint: ${(0, errors_1.errorMessage)(thrown.error)}`);
|
|
533
|
+
}
|
|
534
|
+
return committed;
|
|
535
|
+
}
|
|
536
|
+
// Aborted (or taken over) mid-phase: the abort already settled the task; record nothing.
|
|
537
|
+
if (controller.signal.aborted) {
|
|
538
|
+
api_1.logger.info(`Harness task ${taskId} phase "${task.phase}" stopped after its abort signal`);
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
if (thrown) {
|
|
542
|
+
return failOrRetry({
|
|
543
|
+
error: (0, errors_1.errorMessage)(thrown.error),
|
|
544
|
+
isRetryable: !(thrown.error instanceof definitionError_1.HarnessDefinitionError),
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
return failOrRetry({
|
|
548
|
+
error: `${definition.key}: phase "${task.phase}" returned without calling rt.commit`,
|
|
549
|
+
isRetryable: false,
|
|
550
|
+
});
|
|
551
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { z } from "@terreno/api";
|
|
2
|
+
import type { HarnessAgentDefinition, HarnessChildOutcome, HarnessExtensionDefinition, HarnessLeaseSettings, HarnessTaskDefinition, HarnessTaskDocument } from "../types/harness";
|
|
3
|
+
import { type HarnessModels } from "./commit";
|
|
4
|
+
/** A subagent run by `rt.runAgent` did not produce a usable answer. */
|
|
5
|
+
export declare class HarnessSubagentError extends Error {
|
|
6
|
+
readonly agentName: string;
|
|
7
|
+
readonly conversationId: string;
|
|
8
|
+
/** Terminal status of the subagent's turn task. */
|
|
9
|
+
readonly status: HarnessChildOutcome["status"];
|
|
10
|
+
readonly turnTaskId: string;
|
|
11
|
+
constructor({ agentName, conversationId, message, outcome, }: {
|
|
12
|
+
agentName: string;
|
|
13
|
+
conversationId: string;
|
|
14
|
+
message: string;
|
|
15
|
+
outcome: HarnessChildOutcome;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** The subagent's user message: strings as-is, anything else as JSON. */
|
|
19
|
+
export declare const subagentContent: (input: unknown) => string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Serialize `schema` as JSON Schema with the AI SDK's own converter, so the turn requests
|
|
22
|
+
* exactly the structure `Output.object` would. Stored as a string: JSON Schema keys such
|
|
23
|
+
* as `$schema` and `$ref` are not valid Mongo field names.
|
|
24
|
+
*/
|
|
25
|
+
export declare const serializeOutputSchema: (schema: z.ZodType) => Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* Create the subagent's conversation, its user message, and its turn (a child task of
|
|
28
|
+
* `parent` with an `AGENT` span named after the agent) in one transaction. A re-run of
|
|
29
|
+
* the same phase attempt finds the turn created first, and with it the same conversation.
|
|
30
|
+
*/
|
|
31
|
+
export declare const startSubagentTurn: ({ agent, callIndex, content, extensions, instructions, lease, models, outputSchema, parent, turn, }: {
|
|
32
|
+
agent: HarnessAgentDefinition;
|
|
33
|
+
callIndex: number;
|
|
34
|
+
content: string;
|
|
35
|
+
/** Registered extensions, for the conversation's tool snapshot. */
|
|
36
|
+
extensions: Map<string, HarnessExtensionDefinition>;
|
|
37
|
+
instructions?: string;
|
|
38
|
+
lease: HarnessLeaseSettings;
|
|
39
|
+
models: HarnessModels;
|
|
40
|
+
outputSchema?: string;
|
|
41
|
+
parent: HarnessTaskDocument;
|
|
42
|
+
turn: HarnessTaskDefinition;
|
|
43
|
+
}) => Promise<{
|
|
44
|
+
conversationId: string;
|
|
45
|
+
turnTask: HarnessTaskDocument;
|
|
46
|
+
}>;
|
|
47
|
+
/** The value `rt.runAgent` returns for a settled turn, or the error it throws. */
|
|
48
|
+
export declare const subagentResult: <Result>({ agent, conversationId, outcome, schema, }: {
|
|
49
|
+
agent: HarnessAgentDefinition;
|
|
50
|
+
conversationId: string;
|
|
51
|
+
outcome: HarnessChildOutcome;
|
|
52
|
+
schema?: z.ZodType<Result>;
|
|
53
|
+
}) => Result;
|