@markjaquith/agency 2.10.0 → 2.11.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.
package/src/cli.test.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { afterEach, describe, expect, test } from "bun:test"
2
- import { access, realpath } from "node:fs/promises"
2
+ import { access, mkdir, realpath } from "node:fs/promises"
3
3
  import { join } from "node:path"
4
4
  import { cleanupTempDir, createTempDir } from "./test-utils"
5
5
 
@@ -159,6 +159,7 @@ describe("CLI", () => {
159
159
  ["status", "Usage: agency status"],
160
160
  ["validate", "Usage: agency validate"],
161
161
  ["context", "Usage: agency context"],
162
+ ["graph", "Usage: agency graph"],
162
163
  ] as const) {
163
164
  const result = await runCli([command, "--help"])
164
165
  expect(result.exitCode).toBe(0)
@@ -221,6 +222,69 @@ describe("CLI", () => {
221
222
  ).toEqual([await realpath(root)])
222
223
  })
223
224
 
225
+ test("exports equivalent JSON and JSONL graph contracts", async () => {
226
+ const root = await createTempDir()
227
+ tempDirs.push(root)
228
+ expect((await runCli(["init", root])).exitCode).toBe(0)
229
+ await mkdir(join(root, "repos/agency"), { recursive: true })
230
+ await mkdir(join(root, "tasks/example"), { recursive: true })
231
+ await Bun.write(
232
+ join(root, "tasks/example/TASK.md"),
233
+ `---
234
+ ticketUrl: null
235
+ repo: agency
236
+ branch: feat/example
237
+ base: main
238
+ pr: null
239
+ status: open
240
+ ---
241
+
242
+ # Example
243
+ `,
244
+ )
245
+
246
+ const json = parseJson(
247
+ await runCli(
248
+ ["graph", "--json", "--include", "bodies", "--kind", "task"],
249
+ root,
250
+ ),
251
+ )
252
+ expect(json).toMatchObject({
253
+ version: 1,
254
+ includes: ["bodies"],
255
+ nodes: [
256
+ { id: "task:example", body: expect.stringContaining("# Example") },
257
+ ],
258
+ edges: [],
259
+ })
260
+
261
+ const streamed = await runCli(
262
+ ["graph", "--jsonl", "--include", "bodies", "--kind", "task"],
263
+ root,
264
+ )
265
+ expect(streamed.exitCode).toBe(0)
266
+ expect(streamed.stderr).toBe("")
267
+ const records = streamed.stdout
268
+ .trim()
269
+ .split("\n")
270
+ .map((line) => JSON.parse(line))
271
+ expect(records.map((record) => record.type)).toEqual([
272
+ "meta",
273
+ "node",
274
+ "end",
275
+ ])
276
+ const reconstructed = {
277
+ ...records[0].graph,
278
+ nodes: records
279
+ .filter((record) => record.type === "node")
280
+ .map((record) => record.node),
281
+ edges: records
282
+ .filter((record) => record.type === "edge")
283
+ .map((record) => record.edge),
284
+ }
285
+ expect(reconstructed).toEqual(json)
286
+ })
287
+
224
288
  test("lets JSON override silent and disables interactive task input", async () => {
225
289
  const root = await createTempDir()
226
290
  tempDirs.push(root)
@@ -243,6 +307,24 @@ describe("CLI", () => {
243
307
 
244
308
  const version = await runCli(["status", "--version", "--json"])
245
309
  expect(parseJson(version)).toEqual({ version: "0.0.0-development" })
310
+
311
+ const jsonlHelp = await runCli(["graph", "--help", "--jsonl"])
312
+ expect(parseJson(jsonlHelp)).toContain("Usage: agency graph")
313
+
314
+ const invalidJsonl = await runCli([
315
+ "graph",
316
+ "--jsonl",
317
+ "--include",
318
+ "secrets",
319
+ ])
320
+ expect(invalidJsonl.exitCode).toBe(1)
321
+ expect(invalidJsonl.stderr).toBe("")
322
+ expect(invalidJsonl.stdout.trim().split("\n")).toHaveLength(1)
323
+ expect(JSON.parse(invalidJsonl.stdout)).toMatchObject({
324
+ version: 1,
325
+ ok: false,
326
+ error: { code: "CLI_USAGE" },
327
+ })
246
328
  })
247
329
 
248
330
  test("runs a multi-phase domain workflow through subprocesses", async () => {
@@ -0,0 +1,103 @@
1
+ import { Effect } from "effect"
2
+ import {
3
+ graphJsonlRecords,
4
+ type GraphInclude,
5
+ type GraphNodeKind,
6
+ } from "../graph-schema"
7
+ import { GraphService } from "../services/GraphService"
8
+ import type { WorkStatus } from "../workbase/schemas"
9
+ import type { BaseCommandOptions } from "../utils/command"
10
+ import { createLoggers } from "../utils/effect"
11
+
12
+ interface GraphCommandOptions extends BaseCommandOptions {
13
+ readonly json?: boolean
14
+ readonly jsonl?: boolean
15
+ readonly ready?: boolean
16
+ readonly blocked?: boolean
17
+ readonly statuses?: readonly string[]
18
+ readonly repositories?: readonly string[]
19
+ readonly kinds?: readonly string[]
20
+ readonly include?: readonly string[]
21
+ }
22
+
23
+ const allowedStatuses = new Set<WorkStatus>([
24
+ "open",
25
+ "working",
26
+ "delegated",
27
+ "done",
28
+ "dropped",
29
+ ])
30
+ const allowedKinds = new Set<GraphNodeKind>([
31
+ "epic",
32
+ "task",
33
+ "phase",
34
+ "repository",
35
+ "execution-unit",
36
+ ])
37
+ const allowedIncludes = new Set<GraphInclude>([
38
+ "bodies",
39
+ "workspace",
40
+ "git",
41
+ "pr",
42
+ ])
43
+
44
+ const validated = <T extends string>(
45
+ label: string,
46
+ values: readonly string[] | undefined,
47
+ allowed: ReadonlySet<T>,
48
+ ): T[] =>
49
+ (values ?? []).map((value) => {
50
+ if (!allowed.has(value as T)) {
51
+ throw new Error(
52
+ `Invalid --${label} value '${value}'. Expected one of: ${[...allowed].join(", ")}`,
53
+ )
54
+ }
55
+ return value as T
56
+ })
57
+
58
+ export const graph = (options: GraphCommandOptions = {}) =>
59
+ Effect.gen(function* () {
60
+ const service = yield* GraphService
61
+ const { log } = createLoggers(options)
62
+ const statuses = yield* Effect.sync(() =>
63
+ validated("status", options.statuses, allowedStatuses),
64
+ )
65
+ const kinds = yield* Effect.sync(() =>
66
+ validated("kind", options.kinds, allowedKinds),
67
+ )
68
+ const include = yield* Effect.sync(() =>
69
+ validated("include", options.include, allowedIncludes),
70
+ )
71
+ const result = yield* service.get({
72
+ cwd: options.cwd,
73
+ ready: options.ready,
74
+ blocked: options.blocked,
75
+ statuses,
76
+ repositories: options.repositories,
77
+ kinds,
78
+ include,
79
+ })
80
+ if (options.jsonl) {
81
+ for (const record of graphJsonlRecords(result)) {
82
+ process.stdout.write(`${JSON.stringify(record)}\n`)
83
+ }
84
+ return
85
+ }
86
+ log(JSON.stringify(result, null, 2))
87
+ })
88
+
89
+ export const help = `
90
+ Usage: agency graph [options]
91
+
92
+ Export the workbase as a deterministic, versioned graph.
93
+
94
+ Options:
95
+ --json Output one versioned machine result
96
+ --jsonl Stream versioned graph records
97
+ --ready Include only ready nodes
98
+ --blocked Include only blocked nodes
99
+ --status <status> Filter by status (repeatable)
100
+ --repository <alias> Filter by repository (repeatable)
101
+ --kind <kind> Filter by entity kind (repeatable)
102
+ --include <layer> Include bodies, workspace, git, or pr (repeatable)
103
+ `
@@ -12,6 +12,7 @@ import { status } from "./status"
12
12
  import { task } from "./task"
13
13
  import { validate } from "./validate"
14
14
  import { context } from "./context"
15
+ import { graph } from "./graph"
15
16
 
16
17
  const write = async (root: string, path: string, content: string) => {
17
18
  const fullPath = join(root, path)
@@ -143,6 +144,7 @@ status: open
143
144
  silent: true,
144
145
  }),
145
146
  )
147
+ await runTestEffect(graph({ cwd: root, silent: true }))
146
148
 
147
149
  expect(await Bun.file(join(root, "AGENTS.md")).exists()).toBe(false)
148
150
  expect(
@@ -0,0 +1,61 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import jsonSchema from "../schemas/agency-graph-v1.schema.json"
3
+ import { graphJsonlRecords, type AgencyGraph } from "./graph-schema"
4
+
5
+ describe("graph contract", () => {
6
+ test("publishes the v1 JSON Schema", () => {
7
+ expect(jsonSchema).toMatchObject({
8
+ $schema: "https://json-schema.org/draft/2020-12/schema",
9
+ title: "Agency workbase graph v1",
10
+ properties: { version: { const: 1 } },
11
+ })
12
+ expect(jsonSchema.properties.filters).toMatchObject({
13
+ additionalProperties: false,
14
+ required: ["ready", "blocked", "statuses", "repositories", "kinds"],
15
+ })
16
+ expect(jsonSchema.$defs.node.allOf).toHaveLength(5)
17
+ expect(jsonSchema.$defs.node.allOf[3]?.then?.properties).toMatchObject({
18
+ status: { type: "null" },
19
+ readiness: { type: "null" },
20
+ aggregate: { type: "null" },
21
+ })
22
+ })
23
+
24
+ test("streams records that reconstruct graph semantics", () => {
25
+ const graph = {
26
+ version: 1,
27
+ workbase: { version: 2 },
28
+ filters: {
29
+ ready: null,
30
+ blocked: null,
31
+ statuses: [],
32
+ repositories: [],
33
+ kinds: [],
34
+ },
35
+ includes: [],
36
+ nodes: [],
37
+ edges: [],
38
+ summary: {
39
+ status: "open",
40
+ total: 0,
41
+ open: 0,
42
+ working: 0,
43
+ delegated: 0,
44
+ done: 0,
45
+ dropped: 0,
46
+ terminal: 0,
47
+ },
48
+ validation: { valid: true, issues: [] },
49
+ } satisfies AgencyGraph
50
+ const records = [...graphJsonlRecords(graph)]
51
+ const { nodes: _nodes, edges: _edges, ...metadata } = graph
52
+ expect(records).toEqual([
53
+ {
54
+ version: 1,
55
+ type: "meta",
56
+ graph: metadata,
57
+ },
58
+ { version: 1, type: "end", nodeCount: 0, edgeCount: 0 },
59
+ ])
60
+ })
61
+ })
@@ -0,0 +1,255 @@
1
+ import { Schema } from "@effect/schema"
2
+ import {
3
+ EpicFrontmatter,
4
+ PhaseFrontmatter,
5
+ TaskFrontmatter,
6
+ WorkStatus,
7
+ } from "./workbase/schemas"
8
+
9
+ export const GRAPH_VERSION = 1 as const
10
+
11
+ export const GraphNodeKind = Schema.Literal(
12
+ "epic",
13
+ "task",
14
+ "phase",
15
+ "repository",
16
+ "execution-unit",
17
+ )
18
+
19
+ export const GraphEdgeKind = Schema.Literal(
20
+ "owns",
21
+ "depends_on",
22
+ "writes",
23
+ "references",
24
+ )
25
+
26
+ export const GraphInclude = Schema.Literal("bodies", "workspace", "git", "pr")
27
+
28
+ export const GraphBlocker = Schema.Struct({
29
+ kind: Schema.Literal("dependency", "validation", "status"),
30
+ id: Schema.String,
31
+ status: Schema.optional(WorkStatus),
32
+ reason: Schema.String,
33
+ })
34
+
35
+ export const GraphProgress = Schema.Struct({
36
+ status: WorkStatus,
37
+ total: Schema.Number,
38
+ open: Schema.Number,
39
+ working: Schema.Number,
40
+ delegated: Schema.Number,
41
+ done: Schema.Number,
42
+ dropped: Schema.Number,
43
+ terminal: Schema.Number,
44
+ })
45
+
46
+ export const GraphReadiness = Schema.Struct({
47
+ ready: Schema.Boolean,
48
+ blocked: Schema.Boolean,
49
+ blockers: Schema.Array(GraphBlocker),
50
+ })
51
+
52
+ export const GraphWorkbase = Schema.Struct({
53
+ version: Schema.Literal(2),
54
+ root: Schema.optional(Schema.String),
55
+ })
56
+
57
+ export const GraphFilters = Schema.Struct({
58
+ ready: Schema.NullOr(Schema.Boolean),
59
+ blocked: Schema.NullOr(Schema.Boolean),
60
+ statuses: Schema.Array(WorkStatus),
61
+ repositories: Schema.Array(Schema.String),
62
+ kinds: Schema.Array(GraphNodeKind),
63
+ })
64
+
65
+ const DocumentHash = Schema.Struct({ sha256: Schema.String })
66
+ export const GraphEpicData = Schema.extend(EpicFrontmatter, DocumentHash)
67
+ export const GraphTaskData = Schema.extend(TaskFrontmatter, DocumentHash)
68
+ export const GraphPhaseData = Schema.extend(PhaseFrontmatter, DocumentHash)
69
+ export const GraphExecutionData = Schema.extend(
70
+ PhaseFrontmatter,
71
+ Schema.Struct({
72
+ taskId: Schema.String,
73
+ phaseId: Schema.optional(Schema.String),
74
+ ticketUrl: Schema.optional(Schema.NullOr(Schema.String)),
75
+ epic: Schema.optional(Schema.String),
76
+ }),
77
+ )
78
+
79
+ export const GraphDocumentWorkspace = Schema.Struct({
80
+ documentPath: Schema.String,
81
+ directory: Schema.String,
82
+ })
83
+ export const GraphRepositoryWorkspace = Schema.Struct({
84
+ path: Schema.String,
85
+ target: Schema.NullOr(Schema.String),
86
+ })
87
+ export const GraphExecutionWorkspace = Schema.Struct({
88
+ codePath: Schema.String,
89
+ checkoutPath: Schema.String,
90
+ materialized: Schema.Boolean,
91
+ })
92
+ export const GraphRepositoryGit = Schema.Struct({
93
+ kind: Schema.NullOr(Schema.Literal("bare", "repository")),
94
+ remote: Schema.NullOr(Schema.String),
95
+ head: Schema.NullOr(Schema.String),
96
+ branch: Schema.NullOr(Schema.String),
97
+ })
98
+ export const GraphExecutionGit = Schema.Struct({
99
+ branch: Schema.String,
100
+ base: Schema.String,
101
+ branchCommit: Schema.NullOr(Schema.String),
102
+ baseCommit: Schema.NullOr(Schema.String),
103
+ checkoutCommit: Schema.NullOr(Schema.String),
104
+ checkoutBranch: Schema.NullOr(Schema.String),
105
+ dirty: Schema.NullOr(Schema.Boolean),
106
+ })
107
+ export const GraphPr = Schema.Union(
108
+ Schema.Struct({ url: Schema.Null, state: Schema.Literal("none") }),
109
+ Schema.Struct({ url: Schema.String, state: Schema.Literal("unavailable") }),
110
+ Schema.Struct({
111
+ recordedUrl: Schema.String,
112
+ number: Schema.Number,
113
+ state: Schema.String,
114
+ title: Schema.String,
115
+ isDraft: Schema.Boolean,
116
+ headRefName: Schema.String,
117
+ baseRefName: Schema.String,
118
+ url: Schema.String,
119
+ }),
120
+ )
121
+
122
+ const NodeIdentity = {
123
+ id: Schema.String,
124
+ key: Schema.String,
125
+ dependents: Schema.Array(Schema.String),
126
+ repositories: Schema.Array(Schema.String),
127
+ }
128
+ const StatefulNode = {
129
+ ...NodeIdentity,
130
+ status: WorkStatus,
131
+ readiness: GraphReadiness,
132
+ aggregate: GraphProgress,
133
+ }
134
+ const DocumentNode = {
135
+ ...StatefulNode,
136
+ body: Schema.optional(Schema.String),
137
+ workspace: Schema.optional(GraphDocumentWorkspace),
138
+ }
139
+
140
+ export const GraphNode = Schema.Union(
141
+ Schema.Struct({
142
+ ...DocumentNode,
143
+ kind: Schema.Literal("epic"),
144
+ data: GraphEpicData,
145
+ }),
146
+ Schema.Struct({
147
+ ...DocumentNode,
148
+ kind: Schema.Literal("task"),
149
+ data: GraphTaskData,
150
+ }),
151
+ Schema.Struct({
152
+ ...DocumentNode,
153
+ kind: Schema.Literal("phase"),
154
+ data: GraphPhaseData,
155
+ }),
156
+ Schema.Struct({
157
+ ...NodeIdentity,
158
+ kind: Schema.Literal("repository"),
159
+ status: Schema.Null,
160
+ readiness: Schema.Null,
161
+ aggregate: Schema.Null,
162
+ data: Schema.Struct({ alias: Schema.String }),
163
+ workspace: Schema.optional(GraphRepositoryWorkspace),
164
+ git: Schema.optional(GraphRepositoryGit),
165
+ }),
166
+ Schema.Struct({
167
+ ...StatefulNode,
168
+ kind: Schema.Literal("execution-unit"),
169
+ data: GraphExecutionData,
170
+ workspace: Schema.optional(GraphExecutionWorkspace),
171
+ git: Schema.optional(GraphExecutionGit),
172
+ pr: Schema.optional(GraphPr),
173
+ }),
174
+ )
175
+
176
+ export const GraphEdge = Schema.Struct({
177
+ id: Schema.String,
178
+ kind: GraphEdgeKind,
179
+ from: Schema.String,
180
+ to: Schema.String,
181
+ })
182
+
183
+ export const AgencyGraph = Schema.Struct({
184
+ version: Schema.Literal(GRAPH_VERSION),
185
+ workbase: GraphWorkbase,
186
+ filters: GraphFilters,
187
+ includes: Schema.Array(GraphInclude),
188
+ nodes: Schema.Array(GraphNode),
189
+ edges: Schema.Array(GraphEdge),
190
+ summary: GraphProgress,
191
+ validation: Schema.Struct({
192
+ valid: Schema.Boolean,
193
+ issues: Schema.Array(
194
+ Schema.Struct({ path: Schema.String, message: Schema.String }),
195
+ ),
196
+ }),
197
+ })
198
+
199
+ export type GraphNodeKind = Schema.Schema.Type<typeof GraphNodeKind>
200
+ export type GraphEdgeKind = Schema.Schema.Type<typeof GraphEdgeKind>
201
+ export type GraphInclude = Schema.Schema.Type<typeof GraphInclude>
202
+ export type GraphBlocker = Schema.Schema.Type<typeof GraphBlocker>
203
+ export type GraphProgress = Schema.Schema.Type<typeof GraphProgress>
204
+ export type GraphReadiness = Schema.Schema.Type<typeof GraphReadiness>
205
+ export type GraphExecutionWorkspace = Schema.Schema.Type<
206
+ typeof GraphExecutionWorkspace
207
+ >
208
+ export type GraphExecutionGit = Schema.Schema.Type<typeof GraphExecutionGit>
209
+ export type GraphRepositoryGit = Schema.Schema.Type<typeof GraphRepositoryGit>
210
+ export type GraphPr = Schema.Schema.Type<typeof GraphPr>
211
+ export type GraphNode = Schema.Schema.Type<typeof GraphNode>
212
+ export type GraphEdge = Schema.Schema.Type<typeof GraphEdge>
213
+ export type AgencyGraph = Schema.Schema.Type<typeof AgencyGraph>
214
+
215
+ export type GraphJsonlRecord =
216
+ | {
217
+ readonly version: typeof GRAPH_VERSION
218
+ readonly type: "meta"
219
+ readonly graph: Omit<AgencyGraph, "nodes" | "edges">
220
+ }
221
+ | {
222
+ readonly version: typeof GRAPH_VERSION
223
+ readonly type: "node"
224
+ readonly node: GraphNode
225
+ }
226
+ | {
227
+ readonly version: typeof GRAPH_VERSION
228
+ readonly type: "edge"
229
+ readonly edge: GraphEdge
230
+ }
231
+ | {
232
+ readonly version: typeof GRAPH_VERSION
233
+ readonly type: "end"
234
+ readonly nodeCount: number
235
+ readonly edgeCount: number
236
+ }
237
+
238
+ export function* graphJsonlRecords(
239
+ graph: AgencyGraph,
240
+ ): Generator<GraphJsonlRecord> {
241
+ const { nodes, edges, ...metadata } = graph
242
+ yield { version: GRAPH_VERSION, type: "meta", graph: metadata }
243
+ for (const node of nodes) {
244
+ yield { version: GRAPH_VERSION, type: "node", node }
245
+ }
246
+ for (const edge of edges) {
247
+ yield { version: GRAPH_VERSION, type: "edge", edge }
248
+ }
249
+ yield {
250
+ version: GRAPH_VERSION,
251
+ type: "end",
252
+ nodeCount: nodes.length,
253
+ edgeCount: edges.length,
254
+ }
255
+ }
package/src/protocol.ts CHANGED
@@ -90,6 +90,11 @@ const errorMetadata: Readonly<Record<string, ErrorMetadata>> = {
90
90
  remediation:
91
91
  "Run the command from an Agency entity or provide a valid target.",
92
92
  },
93
+ GraphError: {
94
+ code: "GRAPH_ERROR",
95
+ retryable: false,
96
+ remediation: "Correct the workbase graph data or filters and retry.",
97
+ },
93
98
  ProcessError: { code: "PROCESS_ERROR", retryable: true },
94
99
  ProtocolOutputError: {
95
100
  code: "PROTOCOL_OUTPUT_ERROR",