plugin-agent-orchestrator 1.1.3 → 1.1.4
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/dist/client/index.js +1 -1
- package/dist/client-v2/214.b063f33ba044a808.js +10 -0
- package/dist/client-v2/253.59577b40d9875be0.js +10 -0
- package/dist/client-v2/264.7ed8bee71fa5935c.js +10 -0
- package/dist/client-v2/{41.ba6e080cc0488143.js → 41.3deb9e57a0f64fc4.js} +1 -1
- package/dist/client-v2/418.58fa350f5496efd3.js +10 -0
- package/dist/client-v2/{53.7c29c28043e9112d.js → 53.9a10455323390f14.js} +1 -1
- package/dist/client-v2/619.0a2144aaffefe018.js +10 -0
- package/dist/client-v2/{70.bda9518881c05360.js → 70.ffff2997c10fd4b0.js} +1 -1
- package/dist/client-v2/{794.9bbb8d3a61b2bdf2.js → 794.a67a2614405ba627.js} +1 -1
- package/dist/client-v2/925.e5925bf21ed68e7a.js +10 -0
- package/dist/client-v2/{993.82778b6fd6db3cb1.js → 993.b0688fdc5ccb000a.js} +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -6
- package/dist/locale/en-US.json +48 -28
- package/dist/locale/vi-VN.json +48 -28
- package/dist/locale/zh-CN.json +48 -28
- package/dist/server/collections/agent-execution-spans.js +47 -0
- package/dist/server/collections/agent-harness-profile-versions.js +56 -0
- package/dist/server/collections/agent-harness-profiles.js +12 -0
- package/dist/server/collections/agent-loop-action-approvals.js +68 -0
- package/dist/server/collections/agent-loop-artifacts.js +55 -0
- package/dist/server/collections/agent-loop-circuit-states.js +53 -0
- package/dist/server/collections/agent-loop-control-settings.js +51 -0
- package/dist/server/collections/agent-loop-events.js +27 -0
- package/dist/server/collections/agent-loop-path-locks.js +57 -0
- package/dist/server/collections/agent-loop-patterns.js +72 -0
- package/dist/server/collections/agent-loop-runs.js +163 -2
- package/dist/server/collections/agent-loop-steps.js +69 -0
- package/dist/server/collections/agent-loop-usage-buckets.js +52 -0
- package/dist/server/collections/agent-loop-worktrees.js +55 -0
- package/dist/server/migrations/{20260427000000-change-packages-to-text.js → 20260427000002-change-packages-to-text.js} +2 -0
- package/dist/server/migrations/20260429000000-add-llm-fields.js +3 -2
- package/dist/server/migrations/{20260429000000-fix-inputargs-json-to-text.js → 20260429000001-fix-inputargs-json-to-text.js} +14 -6
- package/dist/server/migrations/20260503000000-add-orchestrator-trace-fields.js +2 -1
- package/dist/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.js +2 -1
- package/dist/server/migrations/20260803010000-backfill-loop-control-plane-data.js +262 -0
- package/dist/server/plugin.js +52 -150
- package/dist/server/resources/agent-loop-approval.js +83 -0
- package/dist/server/resources/agent-loop-artifact.js +86 -0
- package/dist/server/resources/agent-loop-events-stream.js +130 -0
- package/dist/server/resources/agent-loop.js +181 -139
- package/dist/server/resources/resource-helpers.js +63 -0
- package/dist/server/resources/tracing.js +121 -107
- package/dist/server/services/DurableCircuitBreakerService.js +229 -0
- package/dist/server/services/HarnessCompiler.js +142 -0
- package/dist/server/services/HarnessProfileService.js +168 -0
- package/dist/server/services/HarnessSchema.js +167 -0
- package/dist/server/services/LoopBudgetService.js +269 -0
- package/dist/server/services/LoopControlService.js +139 -0
- package/dist/server/services/LoopPatternSchema.js +180 -0
- package/dist/server/services/LoopPatternService.js +132 -0
- package/dist/server/services/LoopRunRepository.js +205 -0
- package/dist/server/services/LoopRunStateMachine.js +389 -0
- package/dist/server/services/LoopSchedulerService.js +193 -0
- package/dist/server/services/LoopTriggerService.js +128 -0
- package/dist/server/services/LoopWorkerService.js +461 -0
- package/dist/server/services/PathLockService.js +288 -0
- package/dist/server/services/PluginAiRuntimeAdapter.js +215 -0
- package/dist/server/services/VerificationSchema.js +59 -0
- package/dist/server/services/VerificationService.js +169 -0
- package/dist/server/skill-hub/actions/git-import.js +13 -7
- package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +1 -5
- package/dist/server/utils/ensure-indexes.js +111 -0
- package/dist/server/utils/loop-pattern-context.js +56 -0
- package/package.json +1 -1
- package/src/client/OrchestratorSettings.tsx +44 -0
- package/src/client-v2/components/AgentRunsTab.tsx +576 -494
- package/src/client-v2/components/HarnessProfilesTab.tsx +13 -8
- package/src/client-v2/components/MemoryInspectorTab.tsx +19 -3
- package/src/client-v2/components/TracingTab.tsx +515 -432
- package/src/client-v2/hooks/useApiRequest.ts +141 -121
- package/src/client-v2/skill-hub/components/SkillMetrics.tsx +8 -6
- package/src/locale/en-US.json +52 -28
- package/src/locale/vi-VN.json +52 -28
- package/src/locale/zh-CN.json +52 -28
- package/src/server/__tests__/circuit-breaker.test.ts +225 -146
- package/src/server/__tests__/harness-compiler.test.ts +131 -0
- package/src/server/__tests__/harness-profile-service.test.ts +116 -0
- package/src/server/__tests__/harness-tool-plan.test.ts +148 -0
- package/src/server/__tests__/loop-budget-service.test.ts +255 -0
- package/src/server/__tests__/loop-control-plane-backfill.test.ts +266 -0
- package/src/server/__tests__/loop-pattern-schema.test.ts +56 -0
- package/src/server/__tests__/loop-pattern-service.test.ts +148 -0
- package/src/server/__tests__/loop-run-state-machine.test.ts +90 -0
- package/src/server/__tests__/loop-worker-claim.test.ts +341 -0
- package/src/server/__tests__/loop-worker-execution.test.ts +236 -0
- package/src/server/__tests__/path-lock-service.test.ts +254 -0
- package/src/server/__tests__/smoke.test.ts +62 -21
- package/src/server/collections/agent-execution-spans.ts +47 -0
- package/src/server/collections/agent-harness-profile-versions.ts +26 -0
- package/src/server/collections/agent-harness-profiles.ts +12 -0
- package/src/server/collections/agent-loop-action-approvals.ts +38 -0
- package/src/server/collections/agent-loop-artifacts.ts +25 -0
- package/src/server/collections/agent-loop-circuit-states.ts +23 -0
- package/src/server/collections/agent-loop-control-settings.ts +21 -0
- package/src/server/collections/agent-loop-events.ts +27 -0
- package/src/server/collections/agent-loop-path-locks.ts +27 -0
- package/src/server/collections/agent-loop-patterns.ts +42 -0
- package/src/server/collections/agent-loop-runs.ts +163 -2
- package/src/server/collections/agent-loop-steps.ts +69 -0
- package/src/server/collections/agent-loop-usage-buckets.ts +22 -0
- package/src/server/collections/agent-loop-worktrees.ts +25 -0
- package/src/server/migrations/{20260427000000-change-packages-to-text.ts → 20260427000002-change-packages-to-text.ts} +3 -0
- package/src/server/migrations/20260429000000-add-llm-fields.ts +3 -2
- package/src/server/migrations/{20260429000000-fix-inputargs-json-to-text.ts → 20260429000001-fix-inputargs-json-to-text.ts} +13 -6
- package/src/server/migrations/20260503000000-add-orchestrator-trace-fields.ts +2 -1
- package/src/server/migrations/20260524000000-add-agent-loop-fields-to-skill-executions.ts +2 -1
- package/src/server/migrations/20260803010000-backfill-loop-control-plane-data.ts +293 -0
- package/src/server/plugin.ts +53 -167
- package/src/server/resources/agent-loop-approval.ts +52 -0
- package/src/server/resources/agent-loop-artifact.ts +56 -0
- package/src/server/resources/agent-loop-events-stream.ts +105 -0
- package/src/server/resources/agent-loop.ts +202 -143
- package/src/server/resources/agent-monitor.ts +482 -482
- package/src/server/resources/resource-helpers.ts +33 -0
- package/src/server/resources/tracing.ts +145 -137
- package/src/server/services/DurableCircuitBreakerService.ts +239 -0
- package/src/server/services/HarnessCompiler.ts +133 -0
- package/src/server/services/HarnessProfileService.ts +157 -0
- package/src/server/services/HarnessSchema.ts +163 -0
- package/src/server/services/LoopBudgetService.ts +275 -0
- package/src/server/services/LoopControlService.ts +124 -0
- package/src/server/services/LoopPatternSchema.ts +167 -0
- package/src/server/services/LoopPatternService.ts +143 -0
- package/src/server/services/LoopRunRepository.ts +237 -0
- package/src/server/services/LoopRunStateMachine.ts +432 -0
- package/src/server/services/LoopSchedulerService.ts +199 -0
- package/src/server/services/LoopTriggerService.ts +115 -0
- package/src/server/services/LoopWorkerService.ts +522 -0
- package/src/server/services/PathLockService.ts +321 -0
- package/src/server/services/PluginAiRuntimeAdapter.ts +315 -0
- package/src/server/services/VerificationSchema.ts +31 -0
- package/src/server/services/VerificationService.ts +185 -0
- package/src/server/skill-hub/actions/__tests__/git-import.test.ts +15 -1
- package/src/server/skill-hub/actions/git-import.ts +9 -3
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +1 -5
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +1 -3
- package/src/server/utils/ensure-indexes.ts +96 -0
- package/src/server/utils/logging.ts +0 -1
- package/src/server/utils/loop-pattern-context.ts +20 -0
- package/dist/client-v2/214.79650a549273f163.js +0 -10
- package/dist/client-v2/253.9f0537713cee9e6c.js +0 -10
- package/dist/client-v2/264.718a107e43fc163c.js +0 -10
- package/dist/client-v2/418.ce8f5b2b8a229d1d.js +0 -10
- package/dist/client-v2/619.bd3c5698b40705c3.js +0 -10
- package/dist/client-v2/925.f5370de8f6632d65.js +0 -10
- package/dist/server/services/AgentHarness.js +0 -81
- package/dist/server/services/AgentLoopController.js +0 -1023
- package/dist/server/services/AgentLoopRepository.js +0 -192
- package/dist/server/services/AgentLoopService.js +0 -142
- package/dist/server/services/AgentPlanValidator.js +0 -99
- package/dist/server/services/AgentPlannerService.js +0 -99
- package/dist/server/services/CircuitBreaker.js +0 -125
- package/dist/server/services/ContextAggregator.js +0 -201
- package/dist/server/services/PlanningPromptService.js +0 -95
- package/dist/server/tools/agent-loop.js +0 -373
- package/dist/server/tools/orchestrator-plan.js +0 -254
- package/src/server/__tests__/agent-loop-controller.test.ts +0 -375
- package/src/server/__tests__/context-aggregator.test.ts +0 -222
- package/src/server/__tests__/parallel-execution.test.ts +0 -318
- package/src/server/__tests__/planning-prompt-service.test.ts +0 -23
- package/src/server/services/AgentHarness.ts +0 -67
- package/src/server/services/AgentLoopController.ts +0 -1263
- package/src/server/services/AgentLoopRepository.ts +0 -178
- package/src/server/services/AgentLoopService.ts +0 -177
- package/src/server/services/AgentPlanValidator.ts +0 -73
- package/src/server/services/AgentPlannerService.ts +0 -80
- package/src/server/services/CircuitBreaker.ts +0 -126
- package/src/server/services/ContextAggregator.ts +0 -239
- package/src/server/services/PlanningPromptService.ts +0 -72
- package/src/server/tools/agent-loop.ts +0 -360
- package/src/server/tools/orchestrator-plan.ts +0 -255
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { Application } from '@nocobase/server';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { compileHarness } from '../services/HarnessCompiler';
|
|
4
|
+
import { planHarnessTools } from '../services/PluginAiRuntimeAdapter';
|
|
5
|
+
|
|
6
|
+
type ToolEntry = { scope?: string; defaultPermission?: string };
|
|
7
|
+
|
|
8
|
+
// Mirrors `DefaultToolsManager.getTools(name, filter)`: a statically registered tool resolves
|
|
9
|
+
// immediately, but a dynamic provider (the MCP one, for instance) only registers its tools when a
|
|
10
|
+
// filter carrying `ctx` is supplied. A mock that ignores the second argument cannot tell the two
|
|
11
|
+
// apart, which is how the context-scoped discovery gap went unnoticed.
|
|
12
|
+
function createApp(tools: Record<string, ToolEntry>, contextTools: Record<string, ToolEntry> = {}) {
|
|
13
|
+
return {
|
|
14
|
+
aiManager: {
|
|
15
|
+
toolsManager: {
|
|
16
|
+
async getTools(name: string, filter?: { ctx?: unknown }) {
|
|
17
|
+
if (tools[name]) return tools[name];
|
|
18
|
+
if (!filter?.ctx) return undefined;
|
|
19
|
+
return contextTools[name];
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
} as unknown as Application;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function harness(tools: {
|
|
27
|
+
allow?: string[];
|
|
28
|
+
ask?: string[];
|
|
29
|
+
deny?: string[];
|
|
30
|
+
effects?: Record<string, 'read' | 'write' | 'external'>;
|
|
31
|
+
trustedPreHandlerTools?: string[];
|
|
32
|
+
}) {
|
|
33
|
+
return compileHarness([{ source: 'test', settings: { tools } }]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('planHarnessTools', () => {
|
|
37
|
+
it('sends a string filter to the constructor and objects to the employee override', async () => {
|
|
38
|
+
const app = createApp({
|
|
39
|
+
readFile: { scope: 'CUSTOM' },
|
|
40
|
+
applyPatch: { scope: 'CUSTOM' },
|
|
41
|
+
});
|
|
42
|
+
const plan = await planHarnessTools(
|
|
43
|
+
app,
|
|
44
|
+
harness({
|
|
45
|
+
allow: ['readFile', 'applyPatch'],
|
|
46
|
+
effects: { readFile: 'read', applyPatch: 'write' },
|
|
47
|
+
trustedPreHandlerTools: ['applyPatch'],
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// plugin-ai filters candidate tools with `toolFilter.includes(t.definition.name)`. Passing
|
|
52
|
+
// objects here matched nothing and silently dropped every tool the harness granted.
|
|
53
|
+
expect(plan.filter).toEqual(['applyPatch', 'readFile']);
|
|
54
|
+
expect(plan.filter.every((name) => typeof name === 'string')).toBe(true);
|
|
55
|
+
expect(plan.employeeTools).toEqual([
|
|
56
|
+
{ name: 'applyPatch', autoCall: true },
|
|
57
|
+
{ name: 'readFile', autoCall: true },
|
|
58
|
+
]);
|
|
59
|
+
expect(plan.withheld).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('marks an ask decision as a non-auto-call instead of exposing it un-gated', async () => {
|
|
63
|
+
const app = createApp({ deployService: { scope: 'CUSTOM' } });
|
|
64
|
+
// Allowed but not trusted for a write effect, so `decideTool` downgrades it to `ask`.
|
|
65
|
+
const plan = await planHarnessTools(
|
|
66
|
+
app,
|
|
67
|
+
harness({ allow: ['deployService'], effects: { deployService: 'write' } }),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
expect(plan.filter).toEqual(['deployService']);
|
|
71
|
+
expect(plan.employeeTools).toEqual([{ name: 'deployService', autoCall: false }]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('withholds a tool whose approval gate plugin-ai would ignore', async () => {
|
|
75
|
+
// `isAutoCall` short-circuits for non-CUSTOM scope and returns `defaultPermission === 'ALLOW'`,
|
|
76
|
+
// so an `ask` decision on this tool would auto-call anyway. Withholding is the safe outcome.
|
|
77
|
+
const app = createApp({ searchWeb: { scope: 'GENERAL', defaultPermission: 'ALLOW' } });
|
|
78
|
+
const plan = await planHarnessTools(app, harness({ ask: ['searchWeb'] }));
|
|
79
|
+
|
|
80
|
+
expect(plan.filter).toEqual([]);
|
|
81
|
+
expect(plan.employeeTools).toEqual([]);
|
|
82
|
+
expect(plan.withheld).toEqual([{ name: 'searchWeb', reason: 'approval cannot be enforced for this tool' }]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('keeps an enforceable ask on a general tool that does not default to allow', async () => {
|
|
86
|
+
const app = createApp({ searchWeb: { scope: 'GENERAL', defaultPermission: 'ASK' } });
|
|
87
|
+
const plan = await planHarnessTools(app, harness({ ask: ['searchWeb'] }));
|
|
88
|
+
|
|
89
|
+
expect(plan.filter).toEqual(['searchWeb']);
|
|
90
|
+
expect(plan.employeeTools).toEqual([{ name: 'searchWeb', autoCall: false }]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('withholds denied and unregistered tools', async () => {
|
|
94
|
+
const app = createApp({ readFile: { scope: 'CUSTOM' } });
|
|
95
|
+
const plan = await planHarnessTools(
|
|
96
|
+
app,
|
|
97
|
+
harness({ allow: ['readFile', 'ghostTool'], ask: ['dropDatabase'], deny: ['dropDatabase'] }),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(plan.filter).toEqual(['readFile']);
|
|
101
|
+
expect(plan.withheld).toEqual([
|
|
102
|
+
{ name: 'dropDatabase', reason: 'denied by harness' },
|
|
103
|
+
{ name: 'ghostTool', reason: 'not registered with plugin-ai' },
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('reports every ungranted system tool so the caller can switch off its injector', async () => {
|
|
108
|
+
const app = createApp({ readFile: { scope: 'CUSTOM' } });
|
|
109
|
+
const plan = await planHarnessTools(app, harness({ allow: ['readFile'], deny: ['knowledge-base-retrieve'] }));
|
|
110
|
+
|
|
111
|
+
// plugin-ai keeps system tools through the constructor filter unconditionally
|
|
112
|
+
// (`systemTools.includes(name) || toolFilter.includes(name)`), so dropping a denied system tool
|
|
113
|
+
// from `filter` does nothing. It has to be reported separately and disabled at its source.
|
|
114
|
+
expect(plan.filter).toEqual(['readFile']);
|
|
115
|
+
expect(plan.blockedSystemTools).toEqual([
|
|
116
|
+
'aiEmployeeWorkflowTaskOutput',
|
|
117
|
+
'knowledge-base-retrieve',
|
|
118
|
+
'subAgentWebSearch',
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('leaves a granted system tool out of the blocked list', async () => {
|
|
123
|
+
const app = createApp({ subAgentWebSearch: { scope: 'SPECIFIED', defaultPermission: 'ALLOW' } });
|
|
124
|
+
const plan = await planHarnessTools(
|
|
125
|
+
app,
|
|
126
|
+
harness({ allow: ['subAgentWebSearch'], effects: { subAgentWebSearch: 'read' } }),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
expect(plan.filter).toEqual(['subAgentWebSearch']);
|
|
130
|
+
expect(plan.blockedSystemTools).not.toContain('subAgentWebSearch');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('finds a context-scoped tool only when discovery passes the invocation ctx', async () => {
|
|
134
|
+
const app = createApp({}, { 'mcp-personal-search': { scope: 'CUSTOM' } });
|
|
135
|
+
const granted = harness({ allow: ['mcp-personal-search'] });
|
|
136
|
+
|
|
137
|
+
// Without ctx the MCP provider never registers the user's tools, so the lookup misses and the
|
|
138
|
+
// harness grant is silently withheld even though `AIEmployee.getToolsMap()` would resolve it.
|
|
139
|
+
await expect(planHarnessTools(app, granted)).resolves.toMatchObject({
|
|
140
|
+
filter: [],
|
|
141
|
+
withheld: [{ name: 'mcp-personal-search', reason: 'not registered with plugin-ai' }],
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const plan = await planHarnessTools(app, granted, { ctx: { auth: {} }, sessionId: 'session-1' });
|
|
145
|
+
expect(plan.filter).toEqual(['mcp-personal-search']);
|
|
146
|
+
expect(plan.withheld).toEqual([]);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import type { Database } from '@nocobase/database';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { LoopBudgetService } from '../services/LoopBudgetService';
|
|
4
|
+
import { loopPatternPolicySchema } from '../services/LoopPatternSchema';
|
|
5
|
+
|
|
6
|
+
type Row = Record<string, unknown>;
|
|
7
|
+
|
|
8
|
+
function matches(row: Row, filter: Row) {
|
|
9
|
+
return Object.entries(filter).every(([key, value]) => row[key] === value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class MemoryRepository {
|
|
13
|
+
private nextId = 1;
|
|
14
|
+
|
|
15
|
+
constructor(readonly rows: Row[] = []) {
|
|
16
|
+
this.resetSequence();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async find(options: Row = {}) {
|
|
20
|
+
const filter = (options.filter as Row | undefined) || {};
|
|
21
|
+
return this.rows.filter((row) => matches(row, filter));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async findOne(options: Row) {
|
|
25
|
+
if (options.filterByTk !== undefined) return this.rows.find((row) => row.id === options.filterByTk) || null;
|
|
26
|
+
return this.rows.find((row) => matches(row, (options.filter as Row | undefined) || {})) || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async create(options: Row) {
|
|
30
|
+
const row = { id: this.nextId++, ...((options.values as Row | undefined) || {}) };
|
|
31
|
+
this.rows.push(row);
|
|
32
|
+
return row;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async update(options: Row) {
|
|
36
|
+
const values = (options.values as Row | undefined) || {};
|
|
37
|
+
for (const row of this.rows) {
|
|
38
|
+
if (options.filterByTk !== undefined && row.id === options.filterByTk) Object.assign(row, values);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
restore(rows: Row[]) {
|
|
43
|
+
this.rows.splice(0, this.rows.length, ...structuredClone(rows));
|
|
44
|
+
this.resetSequence();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private resetSequence() {
|
|
48
|
+
this.nextId = this.rows.reduce((maximum, row) => Math.max(maximum, Number(row.id) || 0), 0) + 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class SerialLock {
|
|
53
|
+
readonly keys: string[] = [];
|
|
54
|
+
private readonly tails = new Map<string, Promise<void>>();
|
|
55
|
+
|
|
56
|
+
async runExclusive<T>(key: string, operation: () => Promise<T>) {
|
|
57
|
+
this.keys.push(key);
|
|
58
|
+
const previous = this.tails.get(key) || Promise.resolve();
|
|
59
|
+
let release = () => {};
|
|
60
|
+
const pending = new Promise<void>((resolve) => {
|
|
61
|
+
release = resolve;
|
|
62
|
+
});
|
|
63
|
+
const tail = previous.then(() => pending);
|
|
64
|
+
this.tails.set(key, tail);
|
|
65
|
+
await previous;
|
|
66
|
+
try {
|
|
67
|
+
return await operation();
|
|
68
|
+
} finally {
|
|
69
|
+
release();
|
|
70
|
+
if (this.tails.get(key) === tail) this.tails.delete(key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function run(id: number, patternId: number, values: Row = {}) {
|
|
76
|
+
return {
|
|
77
|
+
id,
|
|
78
|
+
patternId,
|
|
79
|
+
runtimeVersion: 'control-plane-v2',
|
|
80
|
+
invocationCount: 0,
|
|
81
|
+
toolCallCount: 0,
|
|
82
|
+
delegationCount: 0,
|
|
83
|
+
verificationCount: 0,
|
|
84
|
+
totalInputTokens: 0,
|
|
85
|
+
totalOutputTokens: 0,
|
|
86
|
+
totalTokens: 0,
|
|
87
|
+
totalCost: 0,
|
|
88
|
+
...values,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function createService(input: { runs?: Row[]; buckets?: Row[]; control?: Row } = {}) {
|
|
93
|
+
const repositories = new Map<string, MemoryRepository>([
|
|
94
|
+
['agentLoopRuns', new MemoryRepository(input.runs || [run(1, 1)])],
|
|
95
|
+
['agentLoopUsageBuckets', new MemoryRepository(input.buckets || [])],
|
|
96
|
+
[
|
|
97
|
+
'agentLoopControlSettings',
|
|
98
|
+
new MemoryRepository([
|
|
99
|
+
{
|
|
100
|
+
id: 1,
|
|
101
|
+
key: 'global',
|
|
102
|
+
state: 'running',
|
|
103
|
+
dailyMaxTokens: null,
|
|
104
|
+
dailyMaxCost: null,
|
|
105
|
+
...(input.control || {}),
|
|
106
|
+
},
|
|
107
|
+
]),
|
|
108
|
+
],
|
|
109
|
+
]);
|
|
110
|
+
const transaction = { LOCK: { UPDATE: 'UPDATE' } };
|
|
111
|
+
const database = {
|
|
112
|
+
getRepository(name: string) {
|
|
113
|
+
const repository = repositories.get(name);
|
|
114
|
+
if (!repository) throw new Error(`Unexpected repository ${name}`);
|
|
115
|
+
return repository;
|
|
116
|
+
},
|
|
117
|
+
sequelize: {
|
|
118
|
+
async transaction<T>(callback: (value: typeof transaction) => Promise<T>) {
|
|
119
|
+
const before = new Map(
|
|
120
|
+
Array.from(repositories, ([name, repository]) => [name, structuredClone(repository.rows)]),
|
|
121
|
+
);
|
|
122
|
+
try {
|
|
123
|
+
return await callback(transaction);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
for (const [name, rows] of before) repositories.get(name)?.restore(rows);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
const lock = new SerialLock();
|
|
132
|
+
return {
|
|
133
|
+
lock,
|
|
134
|
+
repositories,
|
|
135
|
+
service: new LoopBudgetService(database as unknown as Database, lock),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function policy(input: Record<string, unknown> = {}) {
|
|
140
|
+
return loopPatternPolicySchema.parse(input);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const now = new Date('2026-08-03T12:00:00.000Z');
|
|
144
|
+
|
|
145
|
+
describe('LoopBudgetService', () => {
|
|
146
|
+
it('reserves run and daily usage atomically without undercounting token totals', async () => {
|
|
147
|
+
const { lock, repositories, service } = createService();
|
|
148
|
+
const result = await service.reserve({
|
|
149
|
+
runId: 1,
|
|
150
|
+
patternId: 1,
|
|
151
|
+
policy: policy(),
|
|
152
|
+
delta: { invocations: 1, toolCalls: 2, inputTokens: 30, outputTokens: 20, totalTokens: 10, cost: 0.25 },
|
|
153
|
+
now,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(result).toMatchObject({
|
|
157
|
+
bucketDate: '2026-08-03',
|
|
158
|
+
reserved: { invocations: 1, toolCalls: 2, inputTokens: 30, outputTokens: 20, totalTokens: 50, cost: 0.25 },
|
|
159
|
+
});
|
|
160
|
+
expect(repositories.get('agentLoopRuns')?.rows[0]).toMatchObject({
|
|
161
|
+
invocationCount: 1,
|
|
162
|
+
toolCallCount: 2,
|
|
163
|
+
totalInputTokens: 30,
|
|
164
|
+
totalOutputTokens: 20,
|
|
165
|
+
totalTokens: 50,
|
|
166
|
+
totalCost: 0.25,
|
|
167
|
+
});
|
|
168
|
+
expect(repositories.get('agentLoopUsageBuckets')?.rows[0]).toMatchObject({
|
|
169
|
+
patternId: 1,
|
|
170
|
+
bucketDate: '2026-08-03',
|
|
171
|
+
invocationCount: 1,
|
|
172
|
+
toolCallCount: 2,
|
|
173
|
+
totalTokens: 50,
|
|
174
|
+
totalCost: 0.25,
|
|
175
|
+
});
|
|
176
|
+
expect(lock.keys).toEqual(['agent-loop:budget:global:2026-08-03']);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('rolls back both rows when a per-run or pattern-daily limit rejects the reservation', async () => {
|
|
180
|
+
const { repositories, service } = createService({
|
|
181
|
+
runs: [run(1, 1, { invocationCount: 1, totalTokens: 5 })],
|
|
182
|
+
buckets: [{ id: 1, patternId: 1, bucketDate: '2026-08-03', invocationCount: 1, totalTokens: 5 }],
|
|
183
|
+
});
|
|
184
|
+
const runPolicy = policy({ perRun: { maxInvocations: 1 } });
|
|
185
|
+
await expect(
|
|
186
|
+
service.reserve({ runId: 1, patternId: 1, policy: runPolicy, delta: { invocations: 1 }, now }),
|
|
187
|
+
).rejects.toThrow('Per-run invocation budget exceeded');
|
|
188
|
+
|
|
189
|
+
const dailyPolicy = policy({ daily: { maxTokens: 5 } });
|
|
190
|
+
await expect(
|
|
191
|
+
service.reserve({ runId: 1, patternId: 1, policy: dailyPolicy, delta: { totalTokens: 1 }, now }),
|
|
192
|
+
).rejects.toThrow('Daily token budget exceeded');
|
|
193
|
+
|
|
194
|
+
expect(repositories.get('agentLoopRuns')?.rows[0]).toMatchObject({ invocationCount: 1, totalTokens: 5 });
|
|
195
|
+
expect(repositories.get('agentLoopUsageBuckets')?.rows[0]).toMatchObject({ invocationCount: 1, totalTokens: 5 });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('enforces global daily caps across different patterns', async () => {
|
|
199
|
+
const { repositories, service } = createService({
|
|
200
|
+
runs: [run(1, 1), run(2, 2)],
|
|
201
|
+
buckets: [
|
|
202
|
+
{ id: 1, patternId: 1, bucketDate: '2026-08-03', totalTokens: 60, totalCost: 1 },
|
|
203
|
+
{ id: 2, patternId: 2, bucketDate: '2026-08-03', totalTokens: 30, totalCost: 1 },
|
|
204
|
+
],
|
|
205
|
+
control: { dailyMaxTokens: 100, dailyMaxCost: 2.5 },
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
await expect(
|
|
209
|
+
service.reserve({ runId: 2, patternId: 2, policy: policy(), delta: { totalTokens: 11 }, now }),
|
|
210
|
+
).rejects.toThrow('Global daily token budget exceeded');
|
|
211
|
+
await expect(
|
|
212
|
+
service.reserve({ runId: 2, patternId: 2, policy: policy(), delta: { cost: 0.6 }, now }),
|
|
213
|
+
).rejects.toThrow('Global daily cost budget exceeded');
|
|
214
|
+
|
|
215
|
+
expect(repositories.get('agentLoopRuns')?.rows[1]).toMatchObject({ totalTokens: 0, totalCost: 0 });
|
|
216
|
+
expect(repositories.get('agentLoopUsageBuckets')?.rows[1]).toMatchObject({ totalTokens: 30, totalCost: 1 });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('serializes racing reservations so only one can cross the remaining budget boundary', async () => {
|
|
220
|
+
const { repositories, service } = createService();
|
|
221
|
+
const limited = policy({ perRun: { maxTokens: 10 }, daily: { maxTokens: 10 } });
|
|
222
|
+
const requests = await Promise.allSettled([
|
|
223
|
+
service.reserve({ runId: 1, patternId: 1, policy: limited, delta: { totalTokens: 6 }, now }),
|
|
224
|
+
service.reserve({ runId: 1, patternId: 1, policy: limited, delta: { totalTokens: 6 }, now }),
|
|
225
|
+
]);
|
|
226
|
+
|
|
227
|
+
expect(requests.filter((request) => request.status === 'fulfilled')).toHaveLength(1);
|
|
228
|
+
expect(requests.filter((request) => request.status === 'rejected')).toHaveLength(1);
|
|
229
|
+
expect(repositories.get('agentLoopRuns')?.rows[0].totalTokens).toBe(6);
|
|
230
|
+
expect(repositories.get('agentLoopUsageBuckets')?.rows[0].totalTokens).toBe(6);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('fails closed for invalid deltas, historical runs, mismatched patterns, and paused control', async () => {
|
|
234
|
+
const invalid = createService();
|
|
235
|
+
await expect(
|
|
236
|
+
invalid.service.reserve({ runId: 1, patternId: 1, policy: policy(), delta: { toolCalls: 0.5 }, now }),
|
|
237
|
+
).rejects.toThrow('nonnegative integer');
|
|
238
|
+
expect(invalid.repositories.get('agentLoopUsageBuckets')?.rows).toHaveLength(0);
|
|
239
|
+
|
|
240
|
+
const historical = createService({ runs: [run(1, 1, { runtimeVersion: 'legacy-plan-v1' })] });
|
|
241
|
+
await expect(
|
|
242
|
+
historical.service.reserve({ runId: 1, patternId: 1, policy: policy(), delta: { invocations: 1 }, now }),
|
|
243
|
+
).rejects.toThrow('Historical plan-era');
|
|
244
|
+
|
|
245
|
+
const mismatched = createService();
|
|
246
|
+
await expect(
|
|
247
|
+
mismatched.service.reserve({ runId: 1, patternId: 2, policy: policy(), delta: { invocations: 1 }, now }),
|
|
248
|
+
).rejects.toThrow('does not belong to pattern');
|
|
249
|
+
|
|
250
|
+
const paused = createService({ control: { state: 'paused', reason: 'Maintenance' } });
|
|
251
|
+
await expect(
|
|
252
|
+
paused.service.reserve({ runId: 1, patternId: 1, policy: policy(), delta: { invocations: 1 }, now }),
|
|
253
|
+
).rejects.toThrow('Maintenance');
|
|
254
|
+
});
|
|
255
|
+
});
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { backfillLoopControlPlaneData } from '../migrations/20260803010000-backfill-loop-control-plane-data';
|
|
3
|
+
|
|
4
|
+
type Row = Record<string, unknown>;
|
|
5
|
+
|
|
6
|
+
function matches(row: Row, filter: Row) {
|
|
7
|
+
return Object.entries(filter).every(([key, value]) => row[key] === value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class MemoryRepository {
|
|
11
|
+
private nextId: number;
|
|
12
|
+
|
|
13
|
+
constructor(readonly rows: Row[] = []) {
|
|
14
|
+
this.nextId = rows.reduce((maximum, row) => Math.max(maximum, Number(row.id) || 0), 0) + 1;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async find(options: Row = {}) {
|
|
18
|
+
const filter = (options.filter as Row | undefined) || {};
|
|
19
|
+
return this.rows.filter((row) => matches(row, filter));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async findOne(options: Row) {
|
|
23
|
+
const filter = (options.filter as Row | undefined) || {};
|
|
24
|
+
return this.rows.find((row) => matches(row, filter)) || null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async create(options: Row) {
|
|
28
|
+
const values = { ...((options.values as Row | undefined) || {}) };
|
|
29
|
+
const row = { id: this.nextId++, ...values };
|
|
30
|
+
this.rows.push(row);
|
|
31
|
+
return row;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async update(options: Row) {
|
|
35
|
+
const filterByTk = options.filterByTk;
|
|
36
|
+
const filter = (options.filter as Row | undefined) || null;
|
|
37
|
+
const values = (options.values as Row | undefined) || {};
|
|
38
|
+
for (const row of this.rows) {
|
|
39
|
+
if ((filterByTk !== undefined && row.id === filterByTk) || (filter && matches(row, filter))) {
|
|
40
|
+
Object.assign(row, values);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createDatabase() {
|
|
47
|
+
const repositories = new Map<string, MemoryRepository>([
|
|
48
|
+
[
|
|
49
|
+
'agentHarnessProfiles',
|
|
50
|
+
new MemoryRepository([
|
|
51
|
+
{ id: 1, tag: 'default', schemaVersion: 1, settings: { tools: { deny: ['shell'] } } },
|
|
52
|
+
{ id: 2, tag: 'draft-only', schemaVersion: 1, settings: { tools: { allow: ['read_file'] } } },
|
|
53
|
+
{
|
|
54
|
+
id: 3,
|
|
55
|
+
tag: 'published-current',
|
|
56
|
+
schemaVersion: 1,
|
|
57
|
+
currentVersionId: 30,
|
|
58
|
+
settings: { tools: { deny: ['legacy'] } },
|
|
59
|
+
},
|
|
60
|
+
]),
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
'agentHarnessProfileVersions',
|
|
64
|
+
new MemoryRepository([
|
|
65
|
+
{ id: 20, profileId: 2, version: 1, status: 'draft', settings: { tools: { allow: ['read_file'] } } },
|
|
66
|
+
{ id: 30, profileId: 3, version: 4, status: 'published', settings: { tools: { deny: ['published'] } } },
|
|
67
|
+
]),
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
'agentLoopRuns',
|
|
71
|
+
new MemoryRepository([
|
|
72
|
+
{ id: 10, status: 'running', metadata: { source: 'legacy' }, lockedBy: 'worker-1' },
|
|
73
|
+
{ id: 11, status: 'succeeded', metadata: {} },
|
|
74
|
+
{
|
|
75
|
+
id: 12,
|
|
76
|
+
rootRunId: 'loop-v2',
|
|
77
|
+
runtimeVersion: 'control-plane-v2',
|
|
78
|
+
recordMode: 'observed-execution',
|
|
79
|
+
patternId: 7,
|
|
80
|
+
status: 'queued',
|
|
81
|
+
policySnapshot: { maxConcurrency: 1 },
|
|
82
|
+
metadata: { source: 'control-plane' },
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 13,
|
|
86
|
+
runtimeVersion: 'control-plane-v2',
|
|
87
|
+
recordMode: 'observed-execution',
|
|
88
|
+
status: 'planning',
|
|
89
|
+
patternId: 7,
|
|
90
|
+
policySnapshot: { maxConcurrency: 1 },
|
|
91
|
+
planSource: 'legacy-planner',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
id: 14,
|
|
95
|
+
runtimeVersion: 'control-plane-v2',
|
|
96
|
+
recordMode: 'observed-execution',
|
|
97
|
+
status: 'queued',
|
|
98
|
+
patternId: 7,
|
|
99
|
+
policySnapshot: { malformed: true },
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: 15,
|
|
103
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
104
|
+
recordMode: 'legacy-plan',
|
|
105
|
+
status: 'blocked',
|
|
106
|
+
metadata: { legacyStatus: 'running', legacyRuntimeRetired: true },
|
|
107
|
+
},
|
|
108
|
+
]),
|
|
109
|
+
],
|
|
110
|
+
[
|
|
111
|
+
'agentLoopSteps',
|
|
112
|
+
new MemoryRepository([
|
|
113
|
+
{ id: 20, runId: 10, status: 'running' },
|
|
114
|
+
{ id: 21, runId: 11, status: 'succeeded' },
|
|
115
|
+
{ id: 22, runId: 12, status: 'pending', runtimeVersion: 'control-plane-v2' },
|
|
116
|
+
]),
|
|
117
|
+
],
|
|
118
|
+
['agentLoopEvents', new MemoryRepository()],
|
|
119
|
+
[
|
|
120
|
+
'agentExecutionSpans',
|
|
121
|
+
new MemoryRepository([
|
|
122
|
+
{ id: 30, metadata: { agentLoopRunId: '10' } },
|
|
123
|
+
{ id: 31, agentLoopRunId: 11, metadata: { agentLoopRunId: 10 } },
|
|
124
|
+
]),
|
|
125
|
+
],
|
|
126
|
+
['agentLoopControlSettings', new MemoryRepository()],
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
const database = {
|
|
130
|
+
getRepository(name: string) {
|
|
131
|
+
return repositories.get(name);
|
|
132
|
+
},
|
|
133
|
+
sequelize: {
|
|
134
|
+
async transaction<T>(callback: (transaction: unknown) => Promise<T>) {
|
|
135
|
+
return callback({});
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
return { database, repositories };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
describe('Loop Control Plane data backfill', () => {
|
|
144
|
+
it('publishes legacy harness settings and converts plan-era records safely', async () => {
|
|
145
|
+
const { database, repositories } = createDatabase();
|
|
146
|
+
|
|
147
|
+
await backfillLoopControlPlaneData(database);
|
|
148
|
+
|
|
149
|
+
const profiles = repositories.get('agentHarnessProfiles')?.rows || [];
|
|
150
|
+
const versions = repositories.get('agentHarnessProfileVersions')?.rows || [];
|
|
151
|
+
expect(versions).toHaveLength(4);
|
|
152
|
+
expect(versions).toContainEqual(
|
|
153
|
+
expect.objectContaining({
|
|
154
|
+
profileId: 1,
|
|
155
|
+
version: 1,
|
|
156
|
+
status: 'published',
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
expect(versions.find((version) => version.profileId === 1)?.settings).toMatchObject({
|
|
160
|
+
tools: { deny: ['shell'] },
|
|
161
|
+
});
|
|
162
|
+
expect(versions).toContainEqual(
|
|
163
|
+
expect.objectContaining({
|
|
164
|
+
profileId: 2,
|
|
165
|
+
version: 2,
|
|
166
|
+
status: 'published',
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
expect(versions.find((version) => version.profileId === 2)?.settings).toMatchObject({
|
|
170
|
+
tools: { allow: ['read_file'] },
|
|
171
|
+
});
|
|
172
|
+
expect(profiles.find((profile) => profile.id === 1)?.currentVersionId).toBeGreaterThan(30);
|
|
173
|
+
expect(profiles.find((profile) => profile.id === 2)?.currentVersionId).toBeGreaterThan(30);
|
|
174
|
+
expect(profiles.find((profile) => profile.id === 3)?.currentVersionId).toBe(30);
|
|
175
|
+
expect(versions.find((version) => version.id === 20)?.status).toBe('draft');
|
|
176
|
+
|
|
177
|
+
const runs = repositories.get('agentLoopRuns')?.rows || [];
|
|
178
|
+
expect(runs[0]).toMatchObject({
|
|
179
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
180
|
+
recordMode: 'legacy-plan',
|
|
181
|
+
status: 'blocked',
|
|
182
|
+
lockedBy: null,
|
|
183
|
+
lockedUntil: null,
|
|
184
|
+
metadata: {
|
|
185
|
+
source: 'legacy',
|
|
186
|
+
legacyStatus: 'running',
|
|
187
|
+
legacyRuntimeRetired: true,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
expect(runs[1]).toMatchObject({
|
|
191
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
192
|
+
recordMode: 'legacy-plan',
|
|
193
|
+
status: 'succeeded',
|
|
194
|
+
metadata: {
|
|
195
|
+
legacyStatus: 'succeeded',
|
|
196
|
+
legacyRuntimeRetired: true,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
expect(runs[2]).toMatchObject({
|
|
200
|
+
runtimeVersion: 'control-plane-v2',
|
|
201
|
+
recordMode: 'observed-execution',
|
|
202
|
+
status: 'queued',
|
|
203
|
+
patternId: 7,
|
|
204
|
+
policySnapshot: { maxConcurrency: 1 },
|
|
205
|
+
metadata: { source: 'control-plane' },
|
|
206
|
+
});
|
|
207
|
+
expect(runs[3]).toMatchObject({
|
|
208
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
209
|
+
recordMode: 'legacy-plan',
|
|
210
|
+
status: 'blocked',
|
|
211
|
+
blockedReason: 'The legacy plan runtime was retired. This historical run is read-only.',
|
|
212
|
+
metadata: { legacyStatus: 'planning', legacyRuntimeRetired: true },
|
|
213
|
+
});
|
|
214
|
+
expect(runs[4]).toMatchObject({
|
|
215
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
216
|
+
recordMode: 'legacy-plan',
|
|
217
|
+
status: 'blocked',
|
|
218
|
+
metadata: { legacyStatus: 'queued', legacyRuntimeRetired: true },
|
|
219
|
+
});
|
|
220
|
+
expect(runs[5]).toMatchObject({
|
|
221
|
+
runtimeVersion: 'legacy-plan-v1',
|
|
222
|
+
recordMode: 'legacy-plan',
|
|
223
|
+
status: 'blocked',
|
|
224
|
+
metadata: { legacyStatus: 'running', legacyRuntimeRetired: true },
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(repositories.get('agentLoopSteps')?.rows).toEqual([
|
|
228
|
+
{ id: 20, runId: 10, status: 'running', runtimeVersion: 'legacy-plan-v1' },
|
|
229
|
+
{ id: 21, runId: 11, status: 'succeeded', runtimeVersion: 'legacy-plan-v1' },
|
|
230
|
+
{ id: 22, runId: 12, status: 'pending', runtimeVersion: 'control-plane-v2' },
|
|
231
|
+
]);
|
|
232
|
+
expect(repositories.get('agentLoopEvents')?.rows).toHaveLength(3);
|
|
233
|
+
expect(repositories.get('agentLoopEvents')?.rows).toEqual(
|
|
234
|
+
expect.arrayContaining([
|
|
235
|
+
expect.objectContaining({ runId: 10, type: 'legacy_runtime_retired', status: 'blocked' }),
|
|
236
|
+
expect.objectContaining({ runId: 13, type: 'legacy_runtime_retired', status: 'blocked' }),
|
|
237
|
+
expect.objectContaining({ runId: 14, type: 'legacy_runtime_retired', status: 'blocked' }),
|
|
238
|
+
]),
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
expect(repositories.get('agentExecutionSpans')?.rows).toMatchObject([
|
|
242
|
+
{ id: 30, agentLoopRunId: 10 },
|
|
243
|
+
{ id: 31, agentLoopRunId: 11 },
|
|
244
|
+
]);
|
|
245
|
+
expect(repositories.get('agentLoopControlSettings')?.rows).toMatchObject([
|
|
246
|
+
{ key: 'global', acceptNewRuns: true, state: 'running', globalMaxConcurrency: 5 },
|
|
247
|
+
]);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('is idempotent for profile versions, retirement events, and global control', async () => {
|
|
251
|
+
const { database, repositories } = createDatabase();
|
|
252
|
+
|
|
253
|
+
await backfillLoopControlPlaneData(database);
|
|
254
|
+
const versionsAfterFirstRun = structuredClone(repositories.get('agentHarnessProfileVersions')?.rows || []);
|
|
255
|
+
const runsAfterFirstRun = structuredClone(repositories.get('agentLoopRuns')?.rows || []);
|
|
256
|
+
const eventsAfterFirstRun = structuredClone(repositories.get('agentLoopEvents')?.rows || []);
|
|
257
|
+
const controlsAfterFirstRun = structuredClone(repositories.get('agentLoopControlSettings')?.rows || []);
|
|
258
|
+
|
|
259
|
+
await backfillLoopControlPlaneData(database);
|
|
260
|
+
|
|
261
|
+
expect(repositories.get('agentHarnessProfileVersions')?.rows).toEqual(versionsAfterFirstRun);
|
|
262
|
+
expect(repositories.get('agentLoopRuns')?.rows).toEqual(runsAfterFirstRun);
|
|
263
|
+
expect(repositories.get('agentLoopEvents')?.rows).toEqual(eventsAfterFirstRun);
|
|
264
|
+
expect(repositories.get('agentLoopControlSettings')?.rows).toEqual(controlsAfterFirstRun);
|
|
265
|
+
});
|
|
266
|
+
});
|