@markjaquith/agency 3.2.14 → 3.3.0

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.
Files changed (75) hide show
  1. package/README.md +20 -3
  2. package/cli-main.ts +1 -0
  3. package/package.json +7 -2
  4. package/src/cli-parser.ts +13 -0
  5. package/src/commands/work.ts +70 -2
  6. package/src/services/ReadinessService.ts +20 -0
  7. package/src/workbase/execution-contract.ts +10 -1
  8. package/src/workbase/schemas.ts +12 -0
  9. package/fixtures/protocol/orchestration-recipes.json +0 -53
  10. package/pi-extensions/agency.test.ts +0 -117
  11. package/src/check-commits.test.ts +0 -58
  12. package/src/cli-parser.test.ts +0 -945
  13. package/src/cli.test.ts +0 -1889
  14. package/src/commands/act.test.ts +0 -418
  15. package/src/commands/archive.test.ts +0 -236
  16. package/src/commands/context.test.ts +0 -711
  17. package/src/commands/description.test.ts +0 -103
  18. package/src/commands/doctor.test.ts +0 -185
  19. package/src/commands/epic.test.ts +0 -196
  20. package/src/commands/init.test.ts +0 -117
  21. package/src/commands/integration.test.ts +0 -165
  22. package/src/commands/pr.test.ts +0 -248
  23. package/src/commands/push.test.ts +0 -56
  24. package/src/commands/read-only.test.ts +0 -161
  25. package/src/commands/repo.test.ts +0 -199
  26. package/src/commands/restore.test.ts +0 -106
  27. package/src/commands/status.test.ts +0 -113
  28. package/src/commands/sync.test.ts +0 -200
  29. package/src/commands/task-phase.test.ts +0 -410
  30. package/src/commands/task.test.ts +0 -281
  31. package/src/commands/validate.test.ts +0 -186
  32. package/src/commands/work.test.ts +0 -1375
  33. package/src/commands/workbase.test.ts +0 -170
  34. package/src/graph-schema.test.ts +0 -124
  35. package/src/protocol.test.ts +0 -163
  36. package/src/readiness.test.ts +0 -105
  37. package/src/services/ArchiveBulkService.test.ts +0 -384
  38. package/src/services/ArchiveService.test.ts +0 -1092
  39. package/src/services/EpicService.test.ts +0 -74
  40. package/src/services/FileSystemService.test.ts +0 -81
  41. package/src/services/GraphMutationService.test.ts +0 -486
  42. package/src/services/GraphService.test.ts +0 -494
  43. package/src/services/IntegrationService.test.ts +0 -1091
  44. package/src/services/LifecycleTransaction.test.ts +0 -145
  45. package/src/services/PullRequestService.test.ts +0 -716
  46. package/src/services/PushService.test.ts +0 -408
  47. package/src/services/ReadinessService.test.ts +0 -290
  48. package/src/services/RepositoryService.test.ts +0 -789
  49. package/src/services/ReviewService.test.ts +0 -408
  50. package/src/services/SyncService.test.ts +0 -1377
  51. package/src/services/TaskPhaseService.test.ts +0 -852
  52. package/src/services/VersionControlService.test.ts +0 -62
  53. package/src/services/WorkbaseService.test.ts +0 -910
  54. package/src/services/WorktreeLock.test.ts +0 -205
  55. package/src/services/WorktreePerformance.test.ts +0 -71
  56. package/src/services/WorktreeService.test.ts +0 -2292
  57. package/src/test-utils.ts +0 -110
  58. package/src/usage-log.test.ts +0 -188
  59. package/src/utils/chooser.test.ts +0 -192
  60. package/src/utils/effect.test.ts +0 -30
  61. package/src/utils/interactive.pty.test.ts +0 -128
  62. package/src/utils/interactive.test.tsx +0 -860
  63. package/src/utils/process.test.ts +0 -132
  64. package/src/utils/progress.test.ts +0 -37
  65. package/src/work-view.test.ts +0 -151
  66. package/src/workbase/agent-command.test.ts +0 -128
  67. package/src/workbase/checkout-command.test.ts +0 -62
  68. package/src/workbase/delivery-command.test.ts +0 -180
  69. package/src/workbase/dependency-graph.test.ts +0 -50
  70. package/src/workbase/execution-contract.test.ts +0 -161
  71. package/src/workbase/frontmatter.test.ts +0 -67
  72. package/src/workbase/repository-reference.test.ts +0 -25
  73. package/src/workbase/schemas.test.ts +0 -543
  74. package/src/workbase/work-target.test.ts +0 -108
  75. package/src/workbase/worktree-command.test.ts +0 -61
@@ -1,1092 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, test } from "bun:test"
2
- import { Effect } from "effect"
3
- import { mkdir } from "node:fs/promises"
4
- import { join } from "node:path"
5
- import { cleanupTempDir, createTempDir, runTestEffect } from "../test-utils"
6
- import { ArchiveService } from "./ArchiveService"
7
- import { EpicService } from "./EpicService"
8
- import { PhaseService } from "./PhaseService"
9
- import { TaskService } from "./TaskService"
10
- import { WorkbaseService } from "./WorkbaseService"
11
- import { WorktreeService } from "./WorktreeService"
12
-
13
- const git = async (args: string[], cwd?: string) => {
14
- const process = Bun.spawn(["git", ...args], {
15
- cwd,
16
- stdout: "pipe",
17
- stderr: "pipe",
18
- })
19
- await process.exited
20
- if (process.exitCode !== 0) {
21
- throw new Error(await new Response(process.stderr).text())
22
- }
23
- }
24
-
25
- describe("ArchiveService", () => {
26
- let root: string
27
- const dropTask = (id: string) =>
28
- runTestEffect(
29
- TaskService.pipe(
30
- Effect.flatMap((service) => service.setStatus(id, "dropped", root)),
31
- ),
32
- )
33
- const dropPhase = (taskId: string, id: string) =>
34
- runTestEffect(
35
- PhaseService.pipe(
36
- Effect.flatMap((service) =>
37
- service.setStatus(taskId, id, "dropped", root),
38
- ),
39
- ),
40
- )
41
-
42
- beforeEach(async () => {
43
- root = await createTempDir()
44
- await Bun.write(join(root, "agency.json"), '{"version":2}\n')
45
- const source = join(root, "source")
46
- await mkdir(source, { recursive: true })
47
- await git(["init", "--initial-branch=main"], source)
48
- await git(["config", "user.email", "test@example.com"], source)
49
- await git(["config", "user.name", "Test"], source)
50
- await Bun.write(join(source, "README.md"), "example\n")
51
- await git(["add", "README.md"], source)
52
- await git(["-c", "commit.gpgsign=false", "commit", "-m", "initial"], source)
53
- await mkdir(join(root, "repos"), { recursive: true })
54
- await git(["clone", "--bare", source, join(root, "repos/agency")])
55
- })
56
-
57
- afterEach(async () => cleanupTempDir(root))
58
-
59
- test("requires terminal effective status for singular task archival", async () => {
60
- await runTestEffect(
61
- TaskService.pipe(
62
- Effect.flatMap((service) =>
63
- service.create(
64
- {
65
- id: "status-check",
66
- ticketUrl: null,
67
- repo: "agency",
68
- branch: "task/status-check",
69
- base: "main",
70
- },
71
- root,
72
- ),
73
- ),
74
- ),
75
- )
76
- await expect(
77
- runTestEffect(
78
- ArchiveService.pipe(
79
- Effect.flatMap((service) =>
80
- service.archiveTask("status-check", root),
81
- ),
82
- ),
83
- ),
84
- ).rejects.toThrow("status=open")
85
- await runTestEffect(
86
- TaskService.pipe(
87
- Effect.flatMap((service) =>
88
- service.setStatus("status-check", "working", root),
89
- ),
90
- ),
91
- )
92
- await expect(
93
- runTestEffect(
94
- ArchiveService.pipe(
95
- Effect.flatMap((service) =>
96
- service.archiveTask("status-check", root),
97
- ),
98
- ),
99
- ),
100
- ).rejects.toThrow("status=working")
101
- await runTestEffect(
102
- TaskService.pipe(
103
- Effect.flatMap((service) =>
104
- service.setStatus("status-check", "dropped", root),
105
- ),
106
- ),
107
- )
108
- const result = await runTestEffect(
109
- ArchiveService.pipe(
110
- Effect.flatMap((service) =>
111
- service.archiveTask("status-check", root, { dryRun: true }),
112
- ),
113
- ),
114
- )
115
- expect(result.dryRun).toBe(true)
116
-
117
- await runTestEffect(
118
- TaskService.pipe(
119
- Effect.flatMap((service) =>
120
- service.create(
121
- {
122
- id: "done-check",
123
- ticketUrl: null,
124
- repo: "agency",
125
- branch: "task/done-check",
126
- base: "main",
127
- },
128
- root,
129
- ),
130
- ),
131
- ),
132
- )
133
- const donePath = join(root, "tasks/done-check/TASK.md")
134
- await Bun.write(
135
- donePath,
136
- (await Bun.file(donePath).text()).replace("status: open", "status: done"),
137
- )
138
- const doneResult = await runTestEffect(
139
- ArchiveService.pipe(
140
- Effect.flatMap((service) =>
141
- service.archiveTask("done-check", root, { dryRun: true }),
142
- ),
143
- ),
144
- )
145
- expect(doneResult.dryRun).toBe(true)
146
- })
147
-
148
- test("rejects empty and partially terminal multi-phase tasks in singular archival", async () => {
149
- await runTestEffect(
150
- TaskService.pipe(
151
- Effect.flatMap((service) =>
152
- service.create(
153
- { id: "empty", ticketUrl: null, multiPhase: true },
154
- root,
155
- ),
156
- ),
157
- ),
158
- )
159
- await expect(
160
- runTestEffect(
161
- ArchiveService.pipe(
162
- Effect.flatMap((service) => service.archiveTask("empty", root)),
163
- ),
164
- ),
165
- ).rejects.toThrow("has no phases")
166
-
167
- for (const id of ["terminal", "open"]) {
168
- await runTestEffect(
169
- PhaseService.pipe(
170
- Effect.flatMap((service) =>
171
- service.create(
172
- {
173
- taskId: "empty",
174
- id,
175
- repo: "agency",
176
- branch: `task/empty-${id}`,
177
- base: "main",
178
- },
179
- root,
180
- ),
181
- ),
182
- ),
183
- )
184
- }
185
- await dropPhase("empty", "terminal")
186
- await expect(
187
- runTestEffect(
188
- ArchiveService.pipe(
189
- Effect.flatMap((service) => service.archiveTask("empty", root)),
190
- ),
191
- ),
192
- ).rejects.toThrow("phase:open:status=open")
193
- })
194
-
195
- test("archives a task after removing its worktree and preserves its branch", async () => {
196
- await runTestEffect(
197
- EpicService.pipe(
198
- Effect.flatMap((service) =>
199
- service.create(
200
- "parent",
201
- "https://example.com/epic",
202
- [{ repo: "agency", ref: "main" }],
203
- root,
204
- ),
205
- ),
206
- ),
207
- )
208
- await runTestEffect(
209
- TaskService.pipe(
210
- Effect.flatMap((service) =>
211
- service.create(
212
- {
213
- id: "child",
214
- ticketUrl: "https://example.com/task",
215
- epic: "parent",
216
- repo: "agency",
217
- branch: "task/child",
218
- base: "main",
219
- },
220
- root,
221
- ),
222
- ),
223
- ),
224
- )
225
- await runTestEffect(
226
- WorktreeService.pipe(
227
- Effect.flatMap((service) =>
228
- service.materialize("child", undefined, root),
229
- ),
230
- ),
231
- )
232
- await dropTask("child")
233
-
234
- const result = await runTestEffect(
235
- ArchiveService.pipe(
236
- Effect.flatMap((service) => service.archiveTask("child", root)),
237
- ),
238
- )
239
-
240
- expect(result.path).toBe(join(root, "archive/tasks/child"))
241
- expect(await Bun.file(join(root, "tasks/child/TASK.md")).exists()).toBe(
242
- false,
243
- )
244
- expect(await Bun.file(join(result.path, "TASK.md")).exists()).toBe(true)
245
- expect(await Bun.file(join(result.path, "code")).exists()).toBe(false)
246
- const epic = await runTestEffect(
247
- EpicService.pipe(
248
- Effect.flatMap((service) => service.show("parent", root)),
249
- ),
250
- )
251
- expect(epic.data.tasks).toEqual([])
252
- const report = await runTestEffect(
253
- WorkbaseService.pipe(Effect.flatMap((service) => service.validate(root))),
254
- )
255
- expect(report.valid).toBe(true)
256
- expect(
257
- Bun.spawnSync([
258
- "git",
259
- "-C",
260
- join(root, "repos/agency"),
261
- "show-ref",
262
- "--verify",
263
- "refs/heads/task/child",
264
- ]).exitCode,
265
- ).toBe(0)
266
- })
267
-
268
- test("archives a phase in the mirrored task hierarchy", async () => {
269
- await runTestEffect(
270
- TaskService.pipe(
271
- Effect.flatMap((service) =>
272
- service.create(
273
- {
274
- id: "multi",
275
- ticketUrl: "https://example.com/task",
276
- multiPhase: true,
277
- },
278
- root,
279
- ),
280
- ),
281
- ),
282
- )
283
- for (const id of ["first", "second"]) {
284
- await runTestEffect(
285
- PhaseService.pipe(
286
- Effect.flatMap((service) =>
287
- service.create(
288
- {
289
- taskId: "multi",
290
- id,
291
- repo: "agency",
292
- branch: `task/${id}`,
293
- base: "main",
294
- },
295
- root,
296
- ),
297
- ),
298
- ),
299
- )
300
- }
301
- await runTestEffect(
302
- WorktreeService.pipe(
303
- Effect.flatMap((service) =>
304
- service.materialize("multi", "second", root),
305
- ),
306
- ),
307
- )
308
-
309
- const result = await runTestEffect(
310
- ArchiveService.pipe(
311
- Effect.flatMap((service) =>
312
- service.archivePhase("multi", "second", root),
313
- ),
314
- ),
315
- )
316
-
317
- expect(result.path).toBe(join(root, "archive/tasks/multi/phases/second"))
318
- expect(await Bun.file(join(result.path, "PHASE.md")).exists()).toBe(true)
319
- expect(await Bun.file(join(result.path, "code")).exists()).toBe(false)
320
- const task = await runTestEffect(
321
- TaskService.pipe(
322
- Effect.flatMap((service) => service.show("multi", root)),
323
- ),
324
- )
325
- expect("phases" in task.data && task.data.phases).toEqual([{ id: "first" }])
326
- const report = await runTestEffect(
327
- WorkbaseService.pipe(Effect.flatMap((service) => service.validate(root))),
328
- )
329
- expect(report.valid).toBe(true)
330
- })
331
-
332
- test("archives an epic and all of its child task folders", async () => {
333
- await runTestEffect(
334
- EpicService.pipe(
335
- Effect.flatMap((service) =>
336
- service.create(
337
- "initiative",
338
- "https://example.com/epic",
339
- [{ repo: "agency", ref: "main" }],
340
- root,
341
- ),
342
- ),
343
- ),
344
- )
345
- await runTestEffect(
346
- TaskService.pipe(
347
- Effect.flatMap((service) =>
348
- service.create(
349
- {
350
- id: "delivery",
351
- ticketUrl: "https://example.com/task",
352
- epic: "initiative",
353
- repo: "agency",
354
- branch: "task/delivery",
355
- base: "main",
356
- },
357
- root,
358
- ),
359
- ),
360
- ),
361
- )
362
-
363
- const result = await runTestEffect(
364
- ArchiveService.pipe(
365
- Effect.flatMap((service) => service.archiveEpic("initiative", root)),
366
- ),
367
- )
368
-
369
- expect(result.affectedPaths).toEqual([
370
- join(root, "archive/tasks/delivery"),
371
- join(root, "archive/epics/initiative"),
372
- ])
373
- expect(
374
- await Bun.file(join(root, "archive/epics/initiative/EPIC.md")).exists(),
375
- ).toBe(true)
376
- expect(
377
- await Bun.file(join(root, "archive/tasks/delivery/TASK.md")).exists(),
378
- ).toBe(true)
379
- const report = await runTestEffect(
380
- WorkbaseService.pipe(Effect.flatMap((service) => service.validate(root))),
381
- )
382
- expect(report.valid).toBe(true)
383
- })
384
-
385
- test("rejects archiving an item required by an active sibling", async () => {
386
- await runTestEffect(
387
- TaskService.pipe(
388
- Effect.flatMap((service) =>
389
- service.create(
390
- {
391
- id: "multi",
392
- ticketUrl: "https://example.com/task",
393
- multiPhase: true,
394
- },
395
- root,
396
- ),
397
- ),
398
- ),
399
- )
400
- await runTestEffect(
401
- PhaseService.pipe(
402
- Effect.flatMap((service) =>
403
- service.create(
404
- {
405
- taskId: "multi",
406
- id: "first",
407
- repo: "agency",
408
- branch: "task/first",
409
- base: "main",
410
- },
411
- root,
412
- ),
413
- ),
414
- ),
415
- )
416
- await runTestEffect(
417
- PhaseService.pipe(
418
- Effect.flatMap((service) =>
419
- service.create(
420
- {
421
- taskId: "multi",
422
- id: "second",
423
- repo: "agency",
424
- branch: "task/second",
425
- base: "main",
426
- dependsOn: ["first"],
427
- },
428
- root,
429
- ),
430
- ),
431
- ),
432
- )
433
-
434
- await expect(
435
- runTestEffect(
436
- ArchiveService.pipe(
437
- Effect.flatMap((service) =>
438
- service.archivePhase("multi", "first", root),
439
- ),
440
- ),
441
- ),
442
- ).rejects.toThrow("phase 'second' depends on it")
443
- })
444
-
445
- test("does not move a task when its worktree is dirty", async () => {
446
- await runTestEffect(
447
- TaskService.pipe(
448
- Effect.flatMap((service) =>
449
- service.create(
450
- {
451
- id: "dirty",
452
- ticketUrl: "https://example.com/task",
453
- repo: "agency",
454
- branch: "task/dirty",
455
- base: "main",
456
- },
457
- root,
458
- ),
459
- ),
460
- ),
461
- )
462
- const workspace = await runTestEffect(
463
- WorktreeService.pipe(
464
- Effect.flatMap((service) =>
465
- service.materialize("dirty", undefined, root),
466
- ),
467
- ),
468
- )
469
- await Bun.write(join(workspace.writablePath!, "dirty.txt"), "keep me\n")
470
- await dropTask("dirty")
471
-
472
- await expect(
473
- runTestEffect(
474
- ArchiveService.pipe(
475
- Effect.flatMap((service) => service.archiveTask("dirty", root)),
476
- ),
477
- ),
478
- ).rejects.toThrow("Failed to remove worktree")
479
- expect(await Bun.file(join(root, "tasks/dirty/TASK.md")).exists()).toBe(
480
- true,
481
- )
482
- expect(await Bun.file(join(root, "archive/tasks/dirty")).exists()).toBe(
483
- false,
484
- )
485
- })
486
-
487
- test("dry-runs archive and restores without mutating files", async () => {
488
- await runTestEffect(
489
- TaskService.pipe(
490
- Effect.flatMap((service) =>
491
- service.create(
492
- {
493
- id: "preview",
494
- ticketUrl: null,
495
- repo: "agency",
496
- branch: "task/preview",
497
- base: "main",
498
- },
499
- root,
500
- ),
501
- ),
502
- ),
503
- )
504
- await dropTask("preview")
505
-
506
- const archivePreview = await runTestEffect(
507
- ArchiveService.pipe(
508
- Effect.flatMap((service) =>
509
- service.archiveTask("preview", root, { dryRun: true }),
510
- ),
511
- ),
512
- )
513
- expect(archivePreview.dryRun).toBe(true)
514
- expect(await Bun.file(join(root, "tasks/preview/TASK.md")).exists()).toBe(
515
- true,
516
- )
517
- expect(
518
- await Bun.file(join(root, "archive/tasks/preview/TASK.md")).exists(),
519
- ).toBe(false)
520
-
521
- await runTestEffect(
522
- ArchiveService.pipe(
523
- Effect.flatMap((service) => service.archiveTask("preview", root)),
524
- ),
525
- )
526
- const restorePreview = await runTestEffect(
527
- ArchiveService.pipe(
528
- Effect.flatMap((service) =>
529
- service.restoreTask("preview", root, { dryRun: true }),
530
- ),
531
- ),
532
- )
533
- expect(restorePreview.dryRun).toBe(true)
534
- expect(await Bun.file(join(root, "tasks/preview/TASK.md")).exists()).toBe(
535
- false,
536
- )
537
- expect(
538
- await Bun.file(join(root, "archive/tasks/preview/TASK.md")).exists(),
539
- ).toBe(true)
540
- })
541
-
542
- test("lists, filters, shows, and restores a task with provenance", async () => {
543
- await runTestEffect(
544
- EpicService.pipe(
545
- Effect.flatMap((service) =>
546
- service.create(
547
- "parent",
548
- "https://example.com/epic",
549
- [{ repo: "agency", ref: "main" }],
550
- root,
551
- ),
552
- ),
553
- ),
554
- )
555
- await runTestEffect(
556
- TaskService.pipe(
557
- Effect.flatMap((service) =>
558
- service.create(
559
- {
560
- id: "child",
561
- ticketUrl: null,
562
- epic: "parent",
563
- repo: "agency",
564
- branch: "task/child",
565
- base: "main",
566
- purpose: "implementation",
567
- handoff: {
568
- source: { kind: "task", taskId: "investigation" },
569
- sourceRevision: "a".repeat(64),
570
- },
571
- },
572
- root,
573
- ),
574
- ),
575
- ),
576
- )
577
- await dropTask("child")
578
- await runTestEffect(
579
- ArchiveService.pipe(
580
- Effect.flatMap((service) => service.archiveTask("child", root)),
581
- ),
582
- )
583
-
584
- const records = await runTestEffect(
585
- ArchiveService.pipe(
586
- Effect.flatMap((service) =>
587
- service.list(
588
- {
589
- kinds: ["task"],
590
- repositories: ["agency"],
591
- statuses: ["dropped"],
592
- },
593
- root,
594
- ),
595
- ),
596
- ),
597
- )
598
- expect(records.map((record) => record.id)).toEqual(["child"])
599
- const archived = await runTestEffect(
600
- ArchiveService.pipe(
601
- Effect.flatMap((service) =>
602
- service.show("task", "child", undefined, root),
603
- ),
604
- ),
605
- )
606
- expect(archived.provenance?.parent).toEqual({
607
- kind: "epic",
608
- id: "parent",
609
- declaration: { id: "child" },
610
- })
611
- expect(archived.provenance?.history[0]?.operation).toBe("archive")
612
-
613
- await runTestEffect(
614
- ArchiveService.pipe(
615
- Effect.flatMap((service) => service.restoreTask("child", root)),
616
- ),
617
- )
618
- const epic = await runTestEffect(
619
- EpicService.pipe(
620
- Effect.flatMap((service) => service.show("parent", root)),
621
- ),
622
- )
623
- expect(epic.data.tasks).toEqual([{ id: "child" }])
624
- const restoredTask = await runTestEffect(
625
- TaskService.pipe(
626
- Effect.flatMap((service) => service.show("child", root)),
627
- ),
628
- )
629
- expect(restoredTask.data).toMatchObject({
630
- purpose: "implementation",
631
- handoff: {
632
- source: { kind: "task", taskId: "investigation" },
633
- sourceRevision: "a".repeat(64),
634
- },
635
- })
636
- const provenance = await Bun.file(
637
- join(root, "tasks/child/.agency-lifecycle.json"),
638
- ).json()
639
- expect(
640
- provenance.history.map((item: { operation: string }) => item.operation),
641
- ).toEqual(["archive", "restore"])
642
- })
643
-
644
- test("preserves phase dependencies across archive and restore", async () => {
645
- await runTestEffect(
646
- TaskService.pipe(
647
- Effect.flatMap((service) =>
648
- service.create(
649
- { id: "multi", ticketUrl: null, multiPhase: true },
650
- root,
651
- ),
652
- ),
653
- ),
654
- )
655
- for (const [id, dependsOn] of [
656
- ["first", undefined],
657
- ["second", ["first"]],
658
- ] as const) {
659
- await runTestEffect(
660
- PhaseService.pipe(
661
- Effect.flatMap((service) =>
662
- service.create(
663
- {
664
- taskId: "multi",
665
- id,
666
- repo: "agency",
667
- branch: `task/${id}`,
668
- base: "main",
669
- ...(dependsOn ? { dependsOn } : {}),
670
- },
671
- root,
672
- ),
673
- ),
674
- ),
675
- )
676
- }
677
- await runTestEffect(
678
- ArchiveService.pipe(
679
- Effect.flatMap((service) =>
680
- service.archivePhase("multi", "second", root),
681
- ),
682
- ),
683
- )
684
- await expect(
685
- runTestEffect(
686
- PhaseService.pipe(
687
- Effect.flatMap((service) =>
688
- service.create(
689
- {
690
- taskId: "multi",
691
- id: "second",
692
- repo: "agency",
693
- branch: "task/recreated-second",
694
- base: "main",
695
- },
696
- root,
697
- ),
698
- ),
699
- ),
700
- ),
701
- ).rejects.toThrow("is archived; restore it")
702
- await runTestEffect(
703
- ArchiveService.pipe(
704
- Effect.flatMap((service) =>
705
- service.restorePhase("multi", "second", root),
706
- ),
707
- ),
708
- )
709
- const task = await runTestEffect(
710
- TaskService.pipe(
711
- Effect.flatMap((service) => service.show("multi", root)),
712
- ),
713
- )
714
- expect("phases" in task.data && task.data.phases).toEqual([
715
- { id: "first" },
716
- { id: "second", dependsOn: ["first"] },
717
- ])
718
- })
719
-
720
- test("preflights missing dependencies before restoring", async () => {
721
- await runTestEffect(
722
- TaskService.pipe(
723
- Effect.flatMap((service) =>
724
- service.create(
725
- { id: "multi", ticketUrl: null, multiPhase: true },
726
- root,
727
- ),
728
- ),
729
- ),
730
- )
731
- for (const [id, dependsOn] of [
732
- ["first", undefined],
733
- ["second", ["first"]],
734
- ] as const) {
735
- await runTestEffect(
736
- PhaseService.pipe(
737
- Effect.flatMap((service) =>
738
- service.create(
739
- {
740
- taskId: "multi",
741
- id,
742
- repo: "agency",
743
- branch: `task/${id}`,
744
- base: "main",
745
- ...(dependsOn ? { dependsOn } : {}),
746
- },
747
- root,
748
- ),
749
- ),
750
- ),
751
- )
752
- }
753
- await runTestEffect(
754
- ArchiveService.pipe(
755
- Effect.flatMap((service) =>
756
- service.archivePhase("multi", "second", root),
757
- ),
758
- ),
759
- )
760
- await runTestEffect(
761
- ArchiveService.pipe(
762
- Effect.flatMap((service) =>
763
- service.archivePhase("multi", "first", root),
764
- ),
765
- ),
766
- )
767
- await expect(
768
- runTestEffect(
769
- ArchiveService.pipe(
770
- Effect.flatMap((service) =>
771
- service.restorePhase("multi", "second", root),
772
- ),
773
- ),
774
- ),
775
- ).rejects.toThrow("dependency 'first'")
776
- expect(
777
- await Bun.file(
778
- join(root, "archive/tasks/multi/phases/second/PHASE.md"),
779
- ).exists(),
780
- ).toBe(true)
781
- })
782
-
783
- test("restores an epic and its task cohort", async () => {
784
- await runTestEffect(
785
- EpicService.pipe(
786
- Effect.flatMap((service) =>
787
- service.create(
788
- "initiative",
789
- "https://example.com/epic",
790
- [{ repo: "agency", ref: "main" }],
791
- root,
792
- ),
793
- ),
794
- ),
795
- )
796
- await runTestEffect(
797
- TaskService.pipe(
798
- Effect.flatMap((service) =>
799
- service.create(
800
- {
801
- id: "delivery",
802
- ticketUrl: null,
803
- epic: "initiative",
804
- repo: "agency",
805
- branch: "task/delivery",
806
- base: "main",
807
- },
808
- root,
809
- ),
810
- ),
811
- ),
812
- )
813
- await runTestEffect(
814
- ArchiveService.pipe(
815
- Effect.flatMap((service) => service.archiveEpic("initiative", root)),
816
- ),
817
- )
818
- await expect(
819
- runTestEffect(
820
- EpicService.pipe(
821
- Effect.flatMap((service) =>
822
- service.create(
823
- "initiative",
824
- "https://example.com/recreated",
825
- [{ repo: "agency", ref: "main" }],
826
- root,
827
- ),
828
- ),
829
- ),
830
- ),
831
- ).rejects.toThrow("is archived; restore it")
832
- await runTestEffect(
833
- ArchiveService.pipe(
834
- Effect.flatMap((service) => service.restoreEpic("initiative", root)),
835
- ),
836
- )
837
- expect(
838
- await Bun.file(join(root, "epics/initiative/EPIC.md")).exists(),
839
- ).toBe(true)
840
- expect(await Bun.file(join(root, "tasks/delivery/TASK.md")).exists()).toBe(
841
- true,
842
- )
843
- const report = await runTestEffect(
844
- WorkbaseService.pipe(Effect.flatMap((service) => service.validate(root))),
845
- )
846
- expect(report.valid).toBe(true)
847
- })
848
-
849
- test("rejects recreating archived entity IDs", async () => {
850
- await runTestEffect(
851
- TaskService.pipe(
852
- Effect.flatMap((service) =>
853
- service.create(
854
- {
855
- id: "reserved",
856
- ticketUrl: null,
857
- repo: "agency",
858
- branch: "task/reserved",
859
- base: "main",
860
- },
861
- root,
862
- ),
863
- ),
864
- ),
865
- )
866
- await dropTask("reserved")
867
- await runTestEffect(
868
- ArchiveService.pipe(
869
- Effect.flatMap((service) => service.archiveTask("reserved", root)),
870
- ),
871
- )
872
- await expect(
873
- runTestEffect(
874
- TaskService.pipe(
875
- Effect.flatMap((service) =>
876
- service.create(
877
- {
878
- id: "reserved",
879
- ticketUrl: null,
880
- repo: "agency",
881
- branch: "task/new-reserved",
882
- base: "main",
883
- },
884
- root,
885
- ),
886
- ),
887
- ),
888
- ),
889
- ).rejects.toThrow("is archived; explicit creation requires a different ID")
890
- })
891
-
892
- test("does not remove worktrees when another lifecycle mutation holds the lock", async () => {
893
- await runTestEffect(
894
- TaskService.pipe(
895
- Effect.flatMap((service) =>
896
- service.create(
897
- {
898
- id: "locked",
899
- ticketUrl: null,
900
- repo: "agency",
901
- branch: "task/locked",
902
- base: "main",
903
- },
904
- root,
905
- ),
906
- ),
907
- ),
908
- )
909
- const workspace = await runTestEffect(
910
- WorktreeService.pipe(
911
- Effect.flatMap((service) =>
912
- service.materialize("locked", undefined, root),
913
- ),
914
- ),
915
- )
916
- await dropTask("locked")
917
- await Bun.write(join(root, ".agency-archive.lock"), "held\n")
918
-
919
- await expect(
920
- runTestEffect(
921
- ArchiveService.pipe(
922
- Effect.flatMap((service) => service.archiveTask("locked", root)),
923
- ),
924
- ),
925
- ).rejects.toThrow("Another archive or restore operation")
926
- expect(
927
- await Bun.file(join(workspace.writablePath!, "README.md")).exists(),
928
- ).toBe(true)
929
- expect(await Bun.file(join(root, "tasks/locked/TASK.md")).exists()).toBe(
930
- true,
931
- )
932
- })
933
-
934
- test("rejects conflicting provenance and restore destinations", async () => {
935
- await runTestEffect(
936
- EpicService.pipe(
937
- Effect.flatMap((service) =>
938
- service.create(
939
- "parent",
940
- "https://example.com/epic",
941
- [{ repo: "agency", ref: "main" }],
942
- root,
943
- ),
944
- ),
945
- ),
946
- )
947
- await runTestEffect(
948
- TaskService.pipe(
949
- Effect.flatMap((service) =>
950
- service.create(
951
- {
952
- id: "child",
953
- ticketUrl: null,
954
- epic: "parent",
955
- repo: "agency",
956
- branch: "task/child",
957
- base: "main",
958
- },
959
- root,
960
- ),
961
- ),
962
- ),
963
- )
964
- await dropTask("child")
965
- await runTestEffect(
966
- ArchiveService.pipe(
967
- Effect.flatMap((service) => service.archiveTask("child", root)),
968
- ),
969
- )
970
- const manifestPath = join(
971
- root,
972
- "archive/tasks/child/.agency-lifecycle.json",
973
- )
974
- const manifest = await Bun.file(manifestPath).json()
975
- manifest.parent.declaration.id = "different"
976
- await Bun.write(manifestPath, JSON.stringify(manifest, null, 2) + "\n")
977
- await expect(
978
- runTestEffect(
979
- ArchiveService.pipe(
980
- Effect.flatMap((service) => service.restoreTask("child", root)),
981
- ),
982
- ),
983
- ).rejects.toThrow("conflicting parent declaration ID")
984
-
985
- manifest.parent.declaration.id = "child"
986
- await Bun.write(manifestPath, JSON.stringify(manifest, null, 2) + "\n")
987
- const archivedTaskPath = join(root, "archive/tasks/child/TASK.md")
988
- const archivedTask = await Bun.file(archivedTaskPath).text()
989
- await Bun.write(
990
- archivedTaskPath,
991
- archivedTask.replace("epic: parent\n", ""),
992
- )
993
- await expect(
994
- runTestEffect(
995
- ArchiveService.pipe(
996
- Effect.flatMap((service) => service.restoreTask("child", root)),
997
- ),
998
- ),
999
- ).rejects.toThrow("missing its epic backlink")
1000
- await Bun.write(archivedTaskPath, archivedTask)
1001
- await mkdir(join(root, "tasks/child"), { recursive: true })
1002
- await expect(
1003
- runTestEffect(
1004
- ArchiveService.pipe(
1005
- Effect.flatMap((service) => service.restoreTask("child", root)),
1006
- ),
1007
- ),
1008
- ).rejects.toThrow("Restore destination already exists")
1009
- expect(
1010
- await Bun.file(join(root, "archive/tasks/child/TASK.md")).exists(),
1011
- ).toBe(true)
1012
- })
1013
-
1014
- test("reserves IDs for incomplete archive directories", async () => {
1015
- await mkdir(join(root, "archive/epics/interrupted"), { recursive: true })
1016
- await expect(
1017
- runTestEffect(
1018
- EpicService.pipe(
1019
- Effect.flatMap((service) =>
1020
- service.create(
1021
- "interrupted",
1022
- "https://example.com/epic",
1023
- [{ repo: "agency", ref: "main" }],
1024
- root,
1025
- ),
1026
- ),
1027
- ),
1028
- ),
1029
- ).rejects.toThrow("is archived; restore it")
1030
- })
1031
-
1032
- test("preflights every phase worktree before removing any of them", async () => {
1033
- await runTestEffect(
1034
- TaskService.pipe(
1035
- Effect.flatMap((service) =>
1036
- service.create(
1037
- {
1038
- id: "multi-dirty",
1039
- ticketUrl: "https://example.com/task",
1040
- multiPhase: true,
1041
- },
1042
- root,
1043
- ),
1044
- ),
1045
- ),
1046
- )
1047
- for (const id of ["clean", "dirty"]) {
1048
- await runTestEffect(
1049
- PhaseService.pipe(
1050
- Effect.flatMap((service) =>
1051
- service.create(
1052
- {
1053
- taskId: "multi-dirty",
1054
- id,
1055
- repo: "agency",
1056
- branch: `task/${id}`,
1057
- base: "main",
1058
- },
1059
- root,
1060
- ),
1061
- ),
1062
- ),
1063
- )
1064
- await runTestEffect(
1065
- WorktreeService.pipe(
1066
- Effect.flatMap((service) =>
1067
- service.materialize("multi-dirty", id, root),
1068
- ),
1069
- ),
1070
- )
1071
- }
1072
- await Bun.write(
1073
- join(root, "tasks/multi-dirty/phases/dirty/code/agency/dirty.txt"),
1074
- "keep me\n",
1075
- )
1076
- await dropPhase("multi-dirty", "clean")
1077
- await dropPhase("multi-dirty", "dirty")
1078
-
1079
- await expect(
1080
- runTestEffect(
1081
- ArchiveService.pipe(
1082
- Effect.flatMap((service) => service.archiveTask("multi-dirty", root)),
1083
- ),
1084
- ),
1085
- ).rejects.toThrow("uncommitted changes")
1086
- expect(
1087
- await Bun.file(
1088
- join(root, "tasks/multi-dirty/phases/clean/code/agency/README.md"),
1089
- ).exists(),
1090
- ).toBe(true)
1091
- })
1092
- })