|
9 | 9 | getTaskFlowById, |
10 | 10 | listTaskFlowRecords, |
11 | 11 | requestFlowCancel, |
| 12 | + setFlowWaiting, |
12 | 13 | } from "./task-flow-registry.js"; |
13 | 14 | import { |
14 | 15 | getInspectableTaskFlowAuditSummary, |
@@ -143,6 +144,115 @@ describe("task-flow-registry maintenance", () => { |
143 | 144 | }); |
144 | 145 | }); |
145 | 146 |
|
| 147 | + it.each(["preview", "apply"] as const)( |
| 148 | + "preserves old blocked managed flows without an end timestamp during %s maintenance", |
| 149 | + async (mode) => { |
| 150 | + await withTaskFlowMaintenanceStateDir(async () => { |
| 151 | + const blockedAt = Date.now() - 8 * 24 * 60 * 60_000; |
| 152 | + const flow = createManagedTaskFlow({ |
| 153 | + ownerKey: "agent:main:main", |
| 154 | + controllerId: "tests/task-flow-maintenance", |
| 155 | + goal: "Wait for an external approval", |
| 156 | + status: "running", |
| 157 | + createdAt: blockedAt, |
| 158 | + updatedAt: blockedAt, |
| 159 | + }); |
| 160 | + const blocked = setFlowWaiting({ |
| 161 | + flowId: flow.flowId, |
| 162 | + expectedRevision: flow.revision, |
| 163 | + blockedSummary: "Waiting for an external approval", |
| 164 | + updatedAt: blockedAt, |
| 165 | + }); |
| 166 | + expect(blocked.applied).toBe(true); |
| 167 | + expect(getInspectableTaskFlowAuditSummary().byCode.stale_blocked).toBe(1); |
| 168 | + |
| 169 | + const maintenance = |
| 170 | + mode === "preview" |
| 171 | + ? previewTaskFlowRegistryMaintenance() |
| 172 | + : await runTaskFlowRegistryMaintenance(); |
| 173 | + |
| 174 | + expect(getTaskFlowById(flow.flowId)).toMatchObject({ |
| 175 | + status: "blocked", |
| 176 | + blockedSummary: "Waiting for an external approval", |
| 177 | + updatedAt: blockedAt, |
| 178 | + }); |
| 179 | + expect(getTaskFlowById(flow.flowId)?.endedAt).toBeUndefined(); |
| 180 | + expect(maintenance).toEqual({ reconciled: 0, pruned: 0 }); |
| 181 | + expect(getInspectableTaskFlowAuditSummary().byCode.stale_blocked).toBe(1); |
| 182 | + }); |
| 183 | + }, |
| 184 | + ); |
| 185 | + |
| 186 | + it("prunes ended blocked flows without removing resumable managed flows", async () => { |
| 187 | + await withTaskFlowMaintenanceStateDir(async () => { |
| 188 | + const endedAt = Date.now() - 8 * 24 * 60 * 60_000; |
| 189 | + const endedManaged = createManagedTaskFlow({ |
| 190 | + ownerKey: "agent:main:ended-managed", |
| 191 | + controllerId: "tests/task-flow-maintenance", |
| 192 | + goal: "Completed managed flow", |
| 193 | + status: "blocked", |
| 194 | + blockedSummary: "Completed with a blocked result", |
| 195 | + createdAt: endedAt, |
| 196 | + updatedAt: endedAt, |
| 197 | + endedAt, |
| 198 | + }); |
| 199 | + const endedMirrored = createFlowRecord({ |
| 200 | + syncMode: "task_mirrored", |
| 201 | + ownerKey: "agent:main:ended-mirrored", |
| 202 | + goal: "Completed mirrored flow", |
| 203 | + status: "blocked", |
| 204 | + blockedSummary: "Completed with a blocked result", |
| 205 | + createdAt: endedAt, |
| 206 | + updatedAt: endedAt, |
| 207 | + endedAt, |
| 208 | + }); |
| 209 | + const activeManaged = createManagedTaskFlow({ |
| 210 | + ownerKey: "agent:main:active-managed", |
| 211 | + controllerId: "tests/task-flow-maintenance", |
| 212 | + goal: "Resume after approval", |
| 213 | + status: "blocked", |
| 214 | + blockedSummary: "Waiting for an external approval", |
| 215 | + createdAt: endedAt, |
| 216 | + updatedAt: endedAt, |
| 217 | + }); |
| 218 | + |
| 219 | + expect(previewTaskFlowRegistryMaintenance()).toEqual({ reconciled: 0, pruned: 2 }); |
| 220 | + expect(await runTaskFlowRegistryMaintenance()).toEqual({ reconciled: 0, pruned: 2 }); |
| 221 | + expect(getTaskFlowById(endedManaged.flowId)).toBeUndefined(); |
| 222 | + expect(getTaskFlowById(endedMirrored.flowId)).toBeUndefined(); |
| 223 | + expect(getTaskFlowById(activeManaged.flowId)).toMatchObject({ status: "blocked" }); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + it("finalizes cancel-requested blocked managed flows without active child tasks", async () => { |
| 228 | + await withTaskFlowMaintenanceStateDir(async () => { |
| 229 | + const flow = createManagedTaskFlow({ |
| 230 | + ownerKey: "agent:main:main", |
| 231 | + controllerId: "tests/task-flow-maintenance", |
| 232 | + goal: "Cancel blocked work", |
| 233 | + status: "running", |
| 234 | + }); |
| 235 | + const blocked = setFlowWaiting({ |
| 236 | + flowId: flow.flowId, |
| 237 | + expectedRevision: flow.revision, |
| 238 | + blockedSummary: "Waiting for an external approval", |
| 239 | + }); |
| 240 | + if (!blocked.applied) { |
| 241 | + throw new Error("Expected managed flow to enter its resumable blocked state"); |
| 242 | + } |
| 243 | + const cancelled = requestFlowCancel({ |
| 244 | + flowId: flow.flowId, |
| 245 | + expectedRevision: blocked.flow.revision, |
| 246 | + }); |
| 247 | + expect(cancelled.applied).toBe(true); |
| 248 | + |
| 249 | + expect(previewTaskFlowRegistryMaintenance()).toEqual({ reconciled: 1, pruned: 0 }); |
| 250 | + expect(await runTaskFlowRegistryMaintenance()).toEqual({ reconciled: 1, pruned: 0 }); |
| 251 | + expect(getTaskFlowById(flow.flowId)).toMatchObject({ status: "cancelled" }); |
| 252 | + expect(getTaskFlowById(flow.flowId)?.endedAt).toBeTypeOf("number"); |
| 253 | + }); |
| 254 | + }); |
| 255 | + |
146 | 256 | it("repairs terminal mirrored flows whose delivery updates outlived endedAt", async () => { |
147 | 257 | await withTaskFlowMaintenanceStateDir(async () => { |
148 | 258 | const flow = createFlowRecord({ |
|
0 commit comments