@markjaquith/agency 2.57.0 → 2.58.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/README.md +21 -6
- package/package.json +1 -1
- package/src/cli-parser.test.ts +8 -0
- package/src/cli-parser.ts +7 -1
- package/src/commands/archive.test.ts +39 -1
- package/src/commands/archive.ts +25 -2
- package/src/commands/restore.test.ts +8 -0
- package/src/services/ArchiveBulkService.test.ts +485 -0
- package/src/services/ArchiveService.test.ts +336 -2
- package/src/services/ArchiveService.ts +447 -29
- package/src/services/WorktreeService.test.ts +4 -0
- package/src/services/WorktreeService.ts +155 -13
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { chmod, mkdir, rm } 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 { GraphMutationService } from "./GraphMutationService"
|
|
9
|
+
import { PhaseService } from "./PhaseService"
|
|
10
|
+
import { TaskService } from "./TaskService"
|
|
11
|
+
import { WorktreeService } from "./WorktreeService"
|
|
12
|
+
|
|
13
|
+
const git = (args: readonly string[], cwd?: string) => {
|
|
14
|
+
const result = Bun.spawnSync(["git", ...args], { cwd })
|
|
15
|
+
if (result.exitCode !== 0) {
|
|
16
|
+
throw new Error(new TextDecoder().decode(result.stderr))
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("ArchiveService bulk task archive", () => {
|
|
21
|
+
let root: string
|
|
22
|
+
let source: string
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
root = await createTempDir()
|
|
26
|
+
await Bun.write(join(root, "agency.json"), '{"version":2}\n')
|
|
27
|
+
source = join(root, "source")
|
|
28
|
+
await mkdir(source, { recursive: true })
|
|
29
|
+
git(["init", "--initial-branch=main"], source)
|
|
30
|
+
git(["config", "user.email", "test@example.com"], source)
|
|
31
|
+
git(["config", "user.name", "Test"], source)
|
|
32
|
+
await Bun.write(join(source, "README.md"), "example\n")
|
|
33
|
+
git(["add", "README.md"], source)
|
|
34
|
+
git(["-c", "commit.gpgsign=false", "commit", "-m", "initial"], source)
|
|
35
|
+
await mkdir(join(root, "repos"), { recursive: true })
|
|
36
|
+
git(["clone", "--bare", source, join(root, "repos/agency")])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
afterEach(async () => cleanupTempDir(root))
|
|
40
|
+
|
|
41
|
+
const createTask = (id: string, epic?: string) =>
|
|
42
|
+
runTestEffect(
|
|
43
|
+
TaskService.pipe(
|
|
44
|
+
Effect.flatMap((service) =>
|
|
45
|
+
service.create(
|
|
46
|
+
{
|
|
47
|
+
id,
|
|
48
|
+
ticketUrl: null,
|
|
49
|
+
...(epic ? { epic } : {}),
|
|
50
|
+
repo: "agency",
|
|
51
|
+
branch: `task/${id}`,
|
|
52
|
+
base: "main",
|
|
53
|
+
},
|
|
54
|
+
root,
|
|
55
|
+
),
|
|
56
|
+
),
|
|
57
|
+
),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const dropTask = (id: string) =>
|
|
61
|
+
runTestEffect(
|
|
62
|
+
TaskService.pipe(
|
|
63
|
+
Effect.flatMap((service) => service.setStatus(id, "dropped", root)),
|
|
64
|
+
),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const archiveTasks = (dryRun = false) =>
|
|
68
|
+
runTestEffect(
|
|
69
|
+
ArchiveService.pipe(
|
|
70
|
+
Effect.flatMap((service) => service.archiveTasks(root, { dryRun })),
|
|
71
|
+
),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
test("returns sorted dispositions and treats an empty multi-phase task as non-terminal", async () => {
|
|
75
|
+
await createTask("z-dropped")
|
|
76
|
+
await dropTask("z-dropped")
|
|
77
|
+
await createTask("a-open")
|
|
78
|
+
await runTestEffect(
|
|
79
|
+
TaskService.pipe(
|
|
80
|
+
Effect.flatMap((service) =>
|
|
81
|
+
service.create(
|
|
82
|
+
{ id: "m-empty", ticketUrl: null, multiPhase: true },
|
|
83
|
+
root,
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const result = await archiveTasks(true)
|
|
90
|
+
|
|
91
|
+
expect(result.tasks.map((task) => task.id)).toEqual([
|
|
92
|
+
"a-open",
|
|
93
|
+
"m-empty",
|
|
94
|
+
"z-dropped",
|
|
95
|
+
])
|
|
96
|
+
expect(result.tasks).toMatchObject([
|
|
97
|
+
{
|
|
98
|
+
id: "a-open",
|
|
99
|
+
disposition: "skipped",
|
|
100
|
+
reason: { code: "non-terminal", details: ["status=open"] },
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
id: "m-empty",
|
|
104
|
+
disposition: "skipped",
|
|
105
|
+
reason: {
|
|
106
|
+
code: "non-terminal",
|
|
107
|
+
details: ["multi-phase task has no phases"],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
{ id: "z-dropped", disposition: "planned" },
|
|
111
|
+
])
|
|
112
|
+
expect(await Bun.file(join(root, "tasks/z-dropped/TASK.md")).exists()).toBe(
|
|
113
|
+
true,
|
|
114
|
+
)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test("uses aggregate phase status and archives only when every phase is terminal", async () => {
|
|
118
|
+
await runTestEffect(
|
|
119
|
+
TaskService.pipe(
|
|
120
|
+
Effect.flatMap((service) =>
|
|
121
|
+
service.create(
|
|
122
|
+
{ id: "multi", ticketUrl: null, multiPhase: true },
|
|
123
|
+
root,
|
|
124
|
+
),
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
for (const id of ["done", "open"]) {
|
|
129
|
+
await runTestEffect(
|
|
130
|
+
PhaseService.pipe(
|
|
131
|
+
Effect.flatMap((service) =>
|
|
132
|
+
service.create(
|
|
133
|
+
{
|
|
134
|
+
taskId: "multi",
|
|
135
|
+
id,
|
|
136
|
+
repo: "agency",
|
|
137
|
+
branch: `task/multi-${id}`,
|
|
138
|
+
base: "main",
|
|
139
|
+
},
|
|
140
|
+
root,
|
|
141
|
+
),
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
await runTestEffect(
|
|
147
|
+
PhaseService.pipe(
|
|
148
|
+
Effect.flatMap((service) =>
|
|
149
|
+
service.setStatus("multi", "done", "dropped", root),
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
const mixed = await archiveTasks(true)
|
|
155
|
+
expect(mixed.tasks[0]).toMatchObject({
|
|
156
|
+
disposition: "skipped",
|
|
157
|
+
reason: { code: "non-terminal" },
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
await runTestEffect(
|
|
161
|
+
PhaseService.pipe(
|
|
162
|
+
Effect.flatMap((service) =>
|
|
163
|
+
service.setStatus("multi", "open", "dropped", root),
|
|
164
|
+
),
|
|
165
|
+
),
|
|
166
|
+
)
|
|
167
|
+
const terminal = await archiveTasks()
|
|
168
|
+
expect(terminal.tasks[0]).toMatchObject({
|
|
169
|
+
id: "multi",
|
|
170
|
+
disposition: "archived",
|
|
171
|
+
})
|
|
172
|
+
expect(
|
|
173
|
+
await Bun.file(join(root, "archive/tasks/multi/TASK.md")).exists(),
|
|
174
|
+
).toBe(true)
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test("propagates retained-dependent skips to a fixed point", async () => {
|
|
178
|
+
await runTestEffect(
|
|
179
|
+
EpicService.pipe(
|
|
180
|
+
Effect.flatMap((service) =>
|
|
181
|
+
service.create(
|
|
182
|
+
"parent",
|
|
183
|
+
"https://example.com/epic",
|
|
184
|
+
[{ repo: "agency", ref: "main" }],
|
|
185
|
+
root,
|
|
186
|
+
),
|
|
187
|
+
),
|
|
188
|
+
),
|
|
189
|
+
)
|
|
190
|
+
for (const id of ["base", "middle", "retained"])
|
|
191
|
+
await createTask(id, "parent")
|
|
192
|
+
await runTestEffect(
|
|
193
|
+
GraphMutationService.pipe(
|
|
194
|
+
Effect.flatMap((service) =>
|
|
195
|
+
service.mutateTaskDependency("add", "middle", "base", root),
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
)
|
|
199
|
+
await runTestEffect(
|
|
200
|
+
GraphMutationService.pipe(
|
|
201
|
+
Effect.flatMap((service) =>
|
|
202
|
+
service.mutateTaskDependency("add", "retained", "middle", root),
|
|
203
|
+
),
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
await dropTask("base")
|
|
207
|
+
await dropTask("middle")
|
|
208
|
+
|
|
209
|
+
const result = await archiveTasks()
|
|
210
|
+
|
|
211
|
+
expect(result.tasks).toMatchObject([
|
|
212
|
+
{
|
|
213
|
+
id: "base",
|
|
214
|
+
disposition: "skipped",
|
|
215
|
+
reason: { code: "retained-dependent", details: ["middle"] },
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
id: "middle",
|
|
219
|
+
disposition: "skipped",
|
|
220
|
+
reason: { code: "retained-dependent", details: ["retained"] },
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
id: "retained",
|
|
224
|
+
disposition: "skipped",
|
|
225
|
+
reason: { code: "non-terminal" },
|
|
226
|
+
},
|
|
227
|
+
])
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test("archives an internal dependency cohort and updates its parent once without archiving it", async () => {
|
|
231
|
+
await runTestEffect(
|
|
232
|
+
EpicService.pipe(
|
|
233
|
+
Effect.flatMap((service) =>
|
|
234
|
+
service.create(
|
|
235
|
+
"parent",
|
|
236
|
+
"https://example.com/epic",
|
|
237
|
+
[{ repo: "agency", ref: "main" }],
|
|
238
|
+
root,
|
|
239
|
+
),
|
|
240
|
+
),
|
|
241
|
+
),
|
|
242
|
+
)
|
|
243
|
+
await createTask("base", "parent")
|
|
244
|
+
await createTask("dependent", "parent")
|
|
245
|
+
await runTestEffect(
|
|
246
|
+
GraphMutationService.pipe(
|
|
247
|
+
Effect.flatMap((service) =>
|
|
248
|
+
service.mutateTaskDependency("add", "dependent", "base", root),
|
|
249
|
+
),
|
|
250
|
+
),
|
|
251
|
+
)
|
|
252
|
+
await dropTask("base")
|
|
253
|
+
await dropTask("dependent")
|
|
254
|
+
|
|
255
|
+
const result = await archiveTasks()
|
|
256
|
+
|
|
257
|
+
expect(result.tasks.map((task) => task.disposition)).toEqual([
|
|
258
|
+
"archived",
|
|
259
|
+
"archived",
|
|
260
|
+
])
|
|
261
|
+
const parent = await runTestEffect(
|
|
262
|
+
EpicService.pipe(
|
|
263
|
+
Effect.flatMap((service) => service.show("parent", root)),
|
|
264
|
+
),
|
|
265
|
+
)
|
|
266
|
+
expect(parent.data.tasks).toEqual([])
|
|
267
|
+
expect(await Bun.file(join(root, "epics/parent/EPIC.md")).exists()).toBe(
|
|
268
|
+
true,
|
|
269
|
+
)
|
|
270
|
+
expect(await Bun.file(join(root, "archive/epics/parent")).exists()).toBe(
|
|
271
|
+
false,
|
|
272
|
+
)
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test("skips destination collisions and dirty managed worktrees", async () => {
|
|
276
|
+
await createTask("collision")
|
|
277
|
+
await dropTask("collision")
|
|
278
|
+
await mkdir(join(root, "archive/tasks/collision"), { recursive: true })
|
|
279
|
+
await createTask("dirty")
|
|
280
|
+
await dropTask("dirty")
|
|
281
|
+
const workspace = await runTestEffect(
|
|
282
|
+
WorktreeService.pipe(
|
|
283
|
+
Effect.flatMap((service) =>
|
|
284
|
+
service.materialize("dirty", undefined, root),
|
|
285
|
+
),
|
|
286
|
+
),
|
|
287
|
+
)
|
|
288
|
+
await Bun.write(join(workspace.writablePath!, "dirty.txt"), "keep\n")
|
|
289
|
+
|
|
290
|
+
const result = await archiveTasks()
|
|
291
|
+
|
|
292
|
+
expect(result.tasks).toMatchObject([
|
|
293
|
+
{
|
|
294
|
+
id: "collision",
|
|
295
|
+
disposition: "skipped",
|
|
296
|
+
reason: { code: "destination-exists" },
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
id: "dirty",
|
|
300
|
+
disposition: "skipped",
|
|
301
|
+
reason: { code: "dirty-worktree" },
|
|
302
|
+
},
|
|
303
|
+
])
|
|
304
|
+
expect(
|
|
305
|
+
await Bun.file(join(workspace.writablePath!, "dirty.txt")).exists(),
|
|
306
|
+
).toBe(true)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test("skips task- and phase-level active claims", async () => {
|
|
310
|
+
await createTask("claimed")
|
|
311
|
+
await dropTask("claimed")
|
|
312
|
+
const taskPath = join(root, "tasks/claimed/TASK.md")
|
|
313
|
+
await Bun.write(
|
|
314
|
+
taskPath,
|
|
315
|
+
(await Bun.file(taskPath).text()).replace(
|
|
316
|
+
"status: dropped\n",
|
|
317
|
+
`status: dropped
|
|
318
|
+
claim:
|
|
319
|
+
claimant: orchestrator
|
|
320
|
+
runner: opencode
|
|
321
|
+
sessionId: task-session
|
|
322
|
+
startedAt: 2026-08-07T00:00:00.000Z
|
|
323
|
+
targetRevision: ${"a".repeat(64)}
|
|
324
|
+
state: active
|
|
325
|
+
`,
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
await runTestEffect(
|
|
329
|
+
TaskService.pipe(
|
|
330
|
+
Effect.flatMap((service) =>
|
|
331
|
+
service.create(
|
|
332
|
+
{ id: "multi-claimed", ticketUrl: null, multiPhase: true },
|
|
333
|
+
root,
|
|
334
|
+
),
|
|
335
|
+
),
|
|
336
|
+
),
|
|
337
|
+
)
|
|
338
|
+
await runTestEffect(
|
|
339
|
+
PhaseService.pipe(
|
|
340
|
+
Effect.flatMap((service) =>
|
|
341
|
+
service.create(
|
|
342
|
+
{
|
|
343
|
+
taskId: "multi-claimed",
|
|
344
|
+
id: "phase",
|
|
345
|
+
repo: "agency",
|
|
346
|
+
branch: "task/multi-claimed",
|
|
347
|
+
base: "main",
|
|
348
|
+
},
|
|
349
|
+
root,
|
|
350
|
+
),
|
|
351
|
+
),
|
|
352
|
+
),
|
|
353
|
+
)
|
|
354
|
+
await runTestEffect(
|
|
355
|
+
PhaseService.pipe(
|
|
356
|
+
Effect.flatMap((service) =>
|
|
357
|
+
service.setStatus("multi-claimed", "phase", "dropped", root),
|
|
358
|
+
),
|
|
359
|
+
),
|
|
360
|
+
)
|
|
361
|
+
const phasePath = join(root, "tasks/multi-claimed/phases/phase/PHASE.md")
|
|
362
|
+
await Bun.write(
|
|
363
|
+
phasePath,
|
|
364
|
+
(await Bun.file(phasePath).text()).replace(
|
|
365
|
+
"status: dropped\n",
|
|
366
|
+
`status: dropped
|
|
367
|
+
claim:
|
|
368
|
+
claimant: orchestrator
|
|
369
|
+
runner: opencode
|
|
370
|
+
sessionId: phase-session
|
|
371
|
+
startedAt: 2026-08-07T00:00:00.000Z
|
|
372
|
+
targetRevision: ${"b".repeat(64)}
|
|
373
|
+
state: active
|
|
374
|
+
`,
|
|
375
|
+
),
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
const result = await archiveTasks(true)
|
|
379
|
+
|
|
380
|
+
expect(result.tasks).toMatchObject([
|
|
381
|
+
{
|
|
382
|
+
id: "claimed",
|
|
383
|
+
disposition: "skipped",
|
|
384
|
+
reason: { code: "active-claim", details: ["task:claimed"] },
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
id: "multi-claimed",
|
|
388
|
+
disposition: "skipped",
|
|
389
|
+
reason: {
|
|
390
|
+
code: "active-claim",
|
|
391
|
+
details: ["phase:multi-claimed/phase"],
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
])
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
test("skips a dirty jj workspace", async () => {
|
|
398
|
+
if (!Bun.which("jj")) return
|
|
399
|
+
const repository = join(root, "repos/agency")
|
|
400
|
+
await rm(repository, { recursive: true, force: true })
|
|
401
|
+
git(["clone", source, repository])
|
|
402
|
+
const initialized = Bun.spawnSync([
|
|
403
|
+
"jj",
|
|
404
|
+
"git",
|
|
405
|
+
"init",
|
|
406
|
+
"--colocate",
|
|
407
|
+
repository,
|
|
408
|
+
])
|
|
409
|
+
if (initialized.exitCode !== 0) {
|
|
410
|
+
throw new Error(new TextDecoder().decode(initialized.stderr))
|
|
411
|
+
}
|
|
412
|
+
await Bun.write(
|
|
413
|
+
join(root, "agency.json"),
|
|
414
|
+
JSON.stringify({ version: 2, vcs: "jj" }),
|
|
415
|
+
)
|
|
416
|
+
await createTask("jj-dirty")
|
|
417
|
+
await dropTask("jj-dirty")
|
|
418
|
+
const workspace = await runTestEffect(
|
|
419
|
+
WorktreeService.pipe(
|
|
420
|
+
Effect.flatMap((service) =>
|
|
421
|
+
service.materialize("jj-dirty", undefined, root),
|
|
422
|
+
),
|
|
423
|
+
),
|
|
424
|
+
)
|
|
425
|
+
await Bun.write(join(workspace.writablePath!, "dirty.txt"), "keep\n")
|
|
426
|
+
|
|
427
|
+
const result = await archiveTasks()
|
|
428
|
+
|
|
429
|
+
expect(result.tasks[0]).toMatchObject({
|
|
430
|
+
id: "jj-dirty",
|
|
431
|
+
disposition: "skipped",
|
|
432
|
+
reason: { code: "dirty-worktree" },
|
|
433
|
+
})
|
|
434
|
+
expect(
|
|
435
|
+
await Bun.file(join(workspace.writablePath!, "dirty.txt")).exists(),
|
|
436
|
+
).toBe(true)
|
|
437
|
+
})
|
|
438
|
+
|
|
439
|
+
test("rolls back the entire cohort when application fails", async () => {
|
|
440
|
+
await runTestEffect(
|
|
441
|
+
EpicService.pipe(
|
|
442
|
+
Effect.flatMap((service) =>
|
|
443
|
+
service.create(
|
|
444
|
+
"rollback-parent",
|
|
445
|
+
"https://example.com/epic",
|
|
446
|
+
[{ repo: "agency", ref: "main" }],
|
|
447
|
+
root,
|
|
448
|
+
),
|
|
449
|
+
),
|
|
450
|
+
),
|
|
451
|
+
)
|
|
452
|
+
for (const id of ["first", "second"]) {
|
|
453
|
+
await createTask(id, "rollback-parent")
|
|
454
|
+
await dropTask(id)
|
|
455
|
+
}
|
|
456
|
+
const archiveDirectory = join(root, "archive")
|
|
457
|
+
await mkdir(archiveDirectory, { mode: 0o500 })
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
await expect(archiveTasks()).rejects.toThrow("rolled back")
|
|
461
|
+
} finally {
|
|
462
|
+
await chmod(archiveDirectory, 0o700)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
for (const id of ["first", "second"]) {
|
|
466
|
+
expect(await Bun.file(join(root, `tasks/${id}/TASK.md`)).exists()).toBe(
|
|
467
|
+
true,
|
|
468
|
+
)
|
|
469
|
+
expect(
|
|
470
|
+
await Bun.file(
|
|
471
|
+
join(root, `tasks/${id}/.agency-lifecycle.json`),
|
|
472
|
+
).exists(),
|
|
473
|
+
).toBe(false)
|
|
474
|
+
}
|
|
475
|
+
const parent = await runTestEffect(
|
|
476
|
+
EpicService.pipe(
|
|
477
|
+
Effect.flatMap((service) => service.show("rollback-parent", root)),
|
|
478
|
+
),
|
|
479
|
+
)
|
|
480
|
+
expect(parent.data.tasks.map((task) => task.id)).toEqual([
|
|
481
|
+
"first",
|
|
482
|
+
"second",
|
|
483
|
+
])
|
|
484
|
+
})
|
|
485
|
+
})
|