@markjaquith/agency 1.9.4 → 1.9.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markjaquith/agency",
3
- "version": "1.9.4",
3
+ "version": "1.9.6",
4
4
  "description": "Manages personal agents files",
5
5
  "keywords": [
6
6
  "agents",
@@ -326,6 +326,36 @@ describe("emit command", () => {
326
326
  clearCapturedFilterRepoCalls()
327
327
  })
328
328
 
329
+ test("throws when emitted CLAUDE.md references AGENCY.md", async () => {
330
+ await checkoutBranch(tempDir, "main")
331
+ await createBranch(tempDir, "agency--claude-reference-test")
332
+
333
+ await Bun.write(
334
+ join(tempDir, "agency.json"),
335
+ JSON.stringify({
336
+ version: 1,
337
+ injectedFiles: ["AGENTS.md"],
338
+ template: "test",
339
+ createdAt: new Date().toISOString(),
340
+ }),
341
+ )
342
+ await Bun.write(
343
+ join(tempDir, "CLAUDE.md"),
344
+ "# Instructions\n\n@AGENCY.md\n",
345
+ )
346
+ await addAndCommit(
347
+ tempDir,
348
+ ["agency.json", "CLAUDE.md"],
349
+ "Add Claude agency reference",
350
+ )
351
+
352
+ await expect(
353
+ runTestEffectWithMockFilterRepo(emit({ silent: true })),
354
+ ).rejects.toThrow(
355
+ "CLAUDE.md on emitted branch claude-reference-test still references @AGENCY.md",
356
+ )
357
+ })
358
+
329
359
  test("constructs correct filter-repo arguments", async () => {
330
360
  // Set up fresh branch with agency.json
331
361
  await checkoutBranch(tempDir, "main")
@@ -248,9 +248,34 @@ export const emitCore = (gitRoot: string, options: EmitOptions) =>
248
248
  enabled: !options.silent && !verbose,
249
249
  })
250
250
 
251
+ yield* validateClaudeDoesNotReferenceAgency(gitRoot, emitBranchName)
252
+
251
253
  log(done(`Emitted ${highlight.branch(emitBranchName)}`))
252
254
  })
253
255
 
256
+ const validateClaudeDoesNotReferenceAgency = (
257
+ gitRoot: string,
258
+ emitBranchName: string,
259
+ ) =>
260
+ Effect.gen(function* () {
261
+ const git = yield* GitService
262
+ const claudeContents = yield* git.getFileAtRef(
263
+ gitRoot,
264
+ emitBranchName,
265
+ "CLAUDE.md",
266
+ )
267
+
268
+ if (!claudeContents?.includes("@AGENCY.md")) {
269
+ return
270
+ }
271
+
272
+ return yield* Effect.fail(
273
+ new Error(
274
+ `CLAUDE.md on emitted branch ${emitBranchName} still references @AGENCY.md. Remove the agency file reference before emitting.`,
275
+ ),
276
+ )
277
+ })
278
+
254
279
  const logFilterPreflight = (
255
280
  gitRoot: string,
256
281
  forkPoint: string,
@@ -0,0 +1,177 @@
1
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test"
2
+ import { chmod, mkdir } from "node:fs/promises"
3
+ import { join } from "node:path"
4
+ import { pr } from "./pr"
5
+ import {
6
+ cleanupTempDir,
7
+ createBranch,
8
+ createTempDir,
9
+ initGitRepo,
10
+ runTestEffect,
11
+ } from "../test-utils"
12
+
13
+ const restoreEnv = (key: string, value: string | undefined) => {
14
+ if (value === undefined) {
15
+ delete process.env[key]
16
+ return
17
+ }
18
+
19
+ process.env[key] = value
20
+ }
21
+
22
+ const readGhArgs = async (recordPath: string): Promise<string[]> => {
23
+ const file = Bun.file(recordPath)
24
+
25
+ if (!(await file.exists())) {
26
+ return []
27
+ }
28
+
29
+ const content = await file.text()
30
+ return content.trim().split("\n").filter(Boolean)
31
+ }
32
+
33
+ describe("pr command", () => {
34
+ let tempDir: string
35
+ let recordPath: string
36
+ let originalCwd: string
37
+ let originalPath: string | undefined
38
+ let originalAgencyConfigPath: string | undefined
39
+ let originalGhArgsFile: string | undefined
40
+
41
+ beforeEach(async () => {
42
+ tempDir = await createTempDir()
43
+ recordPath = join(tempDir, "gh-args.txt")
44
+ originalCwd = process.cwd()
45
+ originalPath = process.env.PATH
46
+ originalAgencyConfigPath = process.env.AGENCY_CONFIG_PATH
47
+ originalGhArgsFile = process.env.AGENCY_TEST_GH_ARGS_FILE
48
+
49
+ process.chdir(tempDir)
50
+ process.env.AGENCY_CONFIG_PATH = join(tempDir, "non-existent-config.json")
51
+ process.env.AGENCY_TEST_GH_ARGS_FILE = recordPath
52
+
53
+ const binDir = join(tempDir, "bin")
54
+ const ghPath = join(binDir, "gh")
55
+ await mkdir(binDir)
56
+ await Bun.write(
57
+ ghPath,
58
+ `#!/bin/sh
59
+ : > "$AGENCY_TEST_GH_ARGS_FILE"
60
+ for arg in "$@"; do
61
+ printf '%s\n' "$arg" >> "$AGENCY_TEST_GH_ARGS_FILE"
62
+ done
63
+ `,
64
+ )
65
+ await chmod(ghPath, 0o755)
66
+ process.env.PATH = `${binDir}:${originalPath ?? ""}`
67
+
68
+ await initGitRepo(tempDir)
69
+ })
70
+
71
+ afterEach(async () => {
72
+ process.chdir(originalCwd)
73
+ restoreEnv("PATH", originalPath)
74
+ restoreEnv("AGENCY_CONFIG_PATH", originalAgencyConfigPath)
75
+ restoreEnv("AGENCY_TEST_GH_ARGS_FILE", originalGhArgsFile)
76
+ await cleanupTempDir(tempDir)
77
+ })
78
+
79
+ test("passes through to gh pr unchanged outside agency context", async () => {
80
+ await createBranch(tempDir, "feature")
81
+
82
+ await runTestEffect(pr({ args: ["status"], silent: false }))
83
+
84
+ expect(await readGhArgs(recordPath)).toEqual(["pr", "status"])
85
+ })
86
+
87
+ test("passes through outside agency context with custom emit pattern", async () => {
88
+ const configPath = process.env.AGENCY_CONFIG_PATH
89
+
90
+ if (!configPath) {
91
+ throw new Error("AGENCY_CONFIG_PATH is not set")
92
+ }
93
+
94
+ await Bun.write(
95
+ configPath,
96
+ JSON.stringify({
97
+ sourceBranchPattern: "agency--%branch%",
98
+ emitBranch: "%branch%--PR",
99
+ }),
100
+ )
101
+ await createBranch(tempDir, "feature")
102
+
103
+ await runTestEffect(pr({ args: ["status"], silent: false }))
104
+
105
+ expect(await readGhArgs(recordPath)).toEqual(["pr", "status"])
106
+ })
107
+
108
+ test("appends emitted branch in agency context", async () => {
109
+ await createBranch(tempDir, "agency--feature")
110
+
111
+ await runTestEffect(pr({ args: ["view", "--web"], silent: false }))
112
+
113
+ expect(await readGhArgs(recordPath)).toEqual([
114
+ "pr",
115
+ "view",
116
+ "--web",
117
+ "feature",
118
+ ])
119
+ })
120
+
121
+ test("appends emitted branch with gh pr value flags when no selector is provided", async () => {
122
+ await createBranch(tempDir, "agency--feature")
123
+
124
+ await runTestEffect(
125
+ pr({
126
+ args: ["view", "--json", "number", "--jq", ".number"],
127
+ silent: false,
128
+ }),
129
+ )
130
+
131
+ expect(await readGhArgs(recordPath)).toEqual([
132
+ "pr",
133
+ "view",
134
+ "--json",
135
+ "number",
136
+ "--jq",
137
+ ".number",
138
+ "feature",
139
+ ])
140
+ })
141
+
142
+ test("does not append emitted branch when an explicit selector is provided", async () => {
143
+ await createBranch(tempDir, "agency--feature")
144
+
145
+ await runTestEffect(
146
+ pr({
147
+ args: [
148
+ "view",
149
+ "123",
150
+ "--json",
151
+ "statusCheckRollup",
152
+ "--jq",
153
+ ".statusCheckRollup[]",
154
+ ],
155
+ silent: false,
156
+ }),
157
+ )
158
+
159
+ expect(await readGhArgs(recordPath)).toEqual([
160
+ "pr",
161
+ "view",
162
+ "123",
163
+ "--json",
164
+ "statusCheckRollup",
165
+ "--jq",
166
+ ".statusCheckRollup[]",
167
+ ])
168
+ })
169
+
170
+ test("does not append emitted branch for subcommands without a selector", async () => {
171
+ await createBranch(tempDir, "agency--feature")
172
+
173
+ await runTestEffect(pr({ args: ["status"], silent: false }))
174
+
175
+ expect(await readGhArgs(recordPath)).toEqual(["pr", "status"])
176
+ })
177
+ })
@@ -2,7 +2,7 @@ import { Effect } from "effect"
2
2
  import type { BaseCommandOptions } from "../utils/command"
3
3
  import { GitService } from "../services/GitService"
4
4
  import { ConfigService } from "../services/ConfigService"
5
- import { resolveBranchPairWithAgencyJson } from "../utils/pr-branch"
5
+ import { resolveAgencyBranchPairWithAgencyJson } from "../utils/pr-branch"
6
6
  import { ensureGitRepo } from "../utils/effect"
7
7
 
8
8
  interface PrOptions extends BaseCommandOptions {
@@ -10,6 +10,120 @@ interface PrOptions extends BaseCommandOptions {
10
10
  args: string[]
11
11
  }
12
12
 
13
+ const PR_SUBCOMMANDS_WITH_OPTIONAL_SELECTOR = new Set([
14
+ "checkout",
15
+ "checks",
16
+ "close",
17
+ "comment",
18
+ "diff",
19
+ "edit",
20
+ "lock",
21
+ "merge",
22
+ "ready",
23
+ "reopen",
24
+ "review",
25
+ "unlock",
26
+ "update-branch",
27
+ "view",
28
+ ])
29
+
30
+ const GH_PR_FLAGS_WITH_VALUE = new Set([
31
+ "--add-assignee",
32
+ "--add-label",
33
+ "--add-project",
34
+ "--add-reviewer",
35
+ "--app",
36
+ "--assignee",
37
+ "--author",
38
+ "--base",
39
+ "--body",
40
+ "--body-file",
41
+ "--head",
42
+ "--hostname",
43
+ "--json",
44
+ "--jq",
45
+ "--label",
46
+ "--limit",
47
+ "--match-head-commit",
48
+ "--milestone",
49
+ "--project",
50
+ "--remove-assignee",
51
+ "--remove-label",
52
+ "--remove-project",
53
+ "--remove-reviewer",
54
+ "--repo",
55
+ "--reviewer",
56
+ "--search",
57
+ "--state",
58
+ "--template",
59
+ "--title",
60
+ ])
61
+
62
+ const GH_PR_SHORT_FLAGS_WITH_VALUE = new Set([
63
+ "-A",
64
+ "-B",
65
+ "-H",
66
+ "-L",
67
+ "-R",
68
+ "-a",
69
+ "-b",
70
+ "-l",
71
+ "-m",
72
+ "-p",
73
+ "-q",
74
+ "-r",
75
+ "-s",
76
+ "-t",
77
+ ])
78
+
79
+ const hasExplicitPrSelector = (args: readonly string[]): boolean => {
80
+ const subcommandArgs = args.slice(1)
81
+
82
+ for (let i = 0; i < subcommandArgs.length; i++) {
83
+ const arg = subcommandArgs[i]
84
+
85
+ if (!arg) {
86
+ continue
87
+ }
88
+
89
+ if (arg === "--") {
90
+ return subcommandArgs.slice(i + 1).some(Boolean)
91
+ }
92
+
93
+ if (arg.startsWith("--")) {
94
+ const flagName = arg.includes("=") ? arg.slice(0, arg.indexOf("=")) : arg
95
+
96
+ if (!arg.includes("=") && GH_PR_FLAGS_WITH_VALUE.has(flagName)) {
97
+ i++
98
+ }
99
+
100
+ continue
101
+ }
102
+
103
+ if (arg.startsWith("-") && arg.length > 1) {
104
+ if (GH_PR_SHORT_FLAGS_WITH_VALUE.has(arg)) {
105
+ i++
106
+ }
107
+
108
+ continue
109
+ }
110
+
111
+ return true
112
+ }
113
+
114
+ return false
115
+ }
116
+
117
+ const shouldAppendEmitBranch = (args: readonly string[]): boolean => {
118
+ const subcommand = args[0]
119
+
120
+ return (
121
+ typeof subcommand === "string" &&
122
+ PR_SUBCOMMANDS_WITH_OPTIONAL_SELECTOR.has(subcommand) &&
123
+ !hasExplicitPrSelector(args)
124
+ )
125
+ }
126
+
13
127
  export const pr = (options: PrOptions) =>
14
128
  Effect.gen(function* () {
15
129
  const git = yield* GitService
@@ -20,23 +134,27 @@ export const pr = (options: PrOptions) =>
20
134
  // Load config
21
135
  const config = yield* configService.loadConfig()
22
136
 
23
- // Get current branch and resolve the branch pair
137
+ // Get current branch and resolve the branch pair when agency context exists
24
138
  const currentBranch = yield* git.getCurrentBranch(gitRoot)
25
- const branches = yield* resolveBranchPairWithAgencyJson(
139
+ const branches = yield* resolveAgencyBranchPairWithAgencyJson(
26
140
  gitRoot,
27
141
  currentBranch,
28
142
  config.sourceBranchPattern,
29
143
  config.emitBranch,
30
144
  )
31
145
 
32
- // Build the gh pr command with the emit branch
33
- const ghArgs = ["gh", "pr", ...options.args, branches.emitBranch]
146
+ // Build the gh pr command with the emit branch only when gh accepts a selector.
147
+ const ghArgs =
148
+ branches && shouldAppendEmitBranch(options.args)
149
+ ? ["gh", "pr", ...options.args, branches.emitBranch]
150
+ : ["gh", "pr", ...options.args]
34
151
 
35
152
  // Run gh pr with stdio inherited so output goes directly to terminal
36
153
  const exitCode = yield* Effect.tryPromise({
37
154
  try: async () => {
38
155
  const proc = Bun.spawn(ghArgs, {
39
156
  cwd: gitRoot,
157
+ env: process.env,
40
158
  stdin: "inherit",
41
159
  stdout: "inherit",
42
160
  stderr: "inherit",
@@ -59,19 +177,20 @@ export const pr = (options: PrOptions) =>
59
177
  export const help = `
60
178
  Usage: agency pr <subcommand> [flags]
61
179
 
62
- Wrapper for 'gh pr' that automatically appends the emitted branch name.
180
+ Wrapper for 'gh pr' that automatically uses the emitted branch in agency context.
63
181
 
64
- This command passes all arguments to 'gh pr' with the emitted branch name
65
- appended, making it easy to work with PRs for your feature branch without
66
- needing to remember or type the emit branch name.
182
+ For gh pr subcommands that accept a PR selector, this command appends the
183
+ emitted branch name when you do not provide a selector. Outside agency context,
184
+ or when you provide a selector, it passes arguments through to 'gh pr' unchanged.
67
185
 
68
186
  Examples:
69
187
  agency pr view --web # gh pr view --web <emit-branch>
70
188
  agency pr checks # gh pr checks <emit-branch>
71
- agency pr status # gh pr status <emit-branch>
189
+ agency pr diff # gh pr diff <emit-branch>
72
190
 
73
191
  Notes:
74
192
  - Requires gh CLI to be installed and authenticated
75
193
  - Uses source and emit patterns from ~/.config/agency/agency.json
76
194
  - Respects emitBranch field in agency.json when present
195
+ - Falls through to gh pr unchanged outside agency context
77
196
  `
@@ -13,6 +13,7 @@ import {
13
13
  addAndCommit,
14
14
  renameBranch,
15
15
  runTestEffect,
16
+ runTestEffectWithMockFilterRepo,
16
17
  } from "../test-utils"
17
18
 
18
19
  async function setupAgencyJson(gitRoot: string): Promise<void> {
@@ -187,6 +188,22 @@ describe("push command", () => {
187
188
  })
188
189
 
189
190
  describe("error handling", () => {
191
+ test("throws when emitted CLAUDE.md references AGENCY.md", async () => {
192
+ await Bun.write(
193
+ join(tempDir, "CLAUDE.md"),
194
+ "# Instructions\n\n@AGENCY.md\n",
195
+ )
196
+ await addAndCommit(tempDir, "CLAUDE.md", "Add Claude agency reference")
197
+
198
+ await expect(
199
+ runTestEffectWithMockFilterRepo(
200
+ push({ baseBranch: "main", silent: true }),
201
+ ),
202
+ ).rejects.toThrow(
203
+ "Failed to create emit branch: CLAUDE.md on emitted branch feature still references @AGENCY.md",
204
+ )
205
+ })
206
+
190
207
  test("switches to source branch when run from emit branch", async () => {
191
208
  // First create the emit branch from source branch
192
209
  await runTestEffect(
@@ -439,3 +439,70 @@ export const resolveBranchPairWithAgencyJson = (
439
439
  // Strategy 4: Fall back to pattern-based resolution
440
440
  return resolveBranchPair(currentBranch, sourcePattern, emitPattern)
441
441
  })
442
+
443
+ /**
444
+ * Resolve branch pair only when the current branch is in agency context.
445
+ * Unlike resolveBranchPairWithAgencyJson, this intentionally does not treat
446
+ * arbitrary legacy branches as source branches.
447
+ */
448
+ export const resolveAgencyBranchPairWithAgencyJson = (
449
+ gitRoot: string,
450
+ currentBranch: string,
451
+ sourcePattern: string,
452
+ emitPattern: string,
453
+ ): Effect.Effect<BranchPair | null, never, GitService | FileSystemService> =>
454
+ Effect.gen(function* () {
455
+ const fromCurrentAgencyJson = yield* tryResolveFromCurrentAgencyJson(
456
+ gitRoot,
457
+ currentBranch,
458
+ )
459
+ if (fromCurrentAgencyJson) {
460
+ return fromCurrentAgencyJson
461
+ }
462
+
463
+ const fromOtherBranchAgencyJson =
464
+ yield* tryResolveFromOtherBranchAgencyJson(gitRoot, currentBranch)
465
+ if (fromOtherBranchAgencyJson) {
466
+ return fromOtherBranchAgencyJson
467
+ }
468
+
469
+ const fromPatternedSource = yield* tryResolveFromPatternedSourceBranch(
470
+ gitRoot,
471
+ currentBranch,
472
+ sourcePattern,
473
+ emitPattern,
474
+ )
475
+ if (fromPatternedSource) {
476
+ return fromPatternedSource
477
+ }
478
+
479
+ if (extractCleanBranch(currentBranch, sourcePattern)) {
480
+ return resolveBranchPair(currentBranch, sourcePattern, emitPattern)
481
+ }
482
+
483
+ if (emitPattern !== "%branch%") {
484
+ const cleanFromEmit = extractCleanFromEmit(currentBranch, emitPattern)
485
+
486
+ if (cleanFromEmit) {
487
+ const git = yield* GitService
488
+ const possibleSourceBranch = makeSourceBranchName(
489
+ cleanFromEmit,
490
+ sourcePattern,
491
+ )
492
+ const sourceExists = yield* pipe(
493
+ git.branchExists(gitRoot, possibleSourceBranch),
494
+ Effect.catchAll(() => Effect.succeed(false)),
495
+ )
496
+
497
+ if (sourceExists) {
498
+ return {
499
+ sourceBranch: possibleSourceBranch,
500
+ emitBranch: currentBranch,
501
+ isOnEmitBranch: true,
502
+ }
503
+ }
504
+ }
505
+ }
506
+
507
+ return null
508
+ })