@iceinvein/agent-skills 0.24.0 → 0.26.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 (55) hide show
  1. package/README.md +4 -3
  2. package/dist/cli/index.js +775 -236
  3. package/package.json +2 -1
  4. package/skills/index.json +2 -2
  5. package/skills/magpie/README.md +14 -0
  6. package/skills/magpie/SKILL.md +42 -23
  7. package/skills/magpie/bin/magpie.ts +91 -3
  8. package/skills/magpie/evals/README.md +1 -1
  9. package/skills/magpie/evals/post-folds-selection-events/fixture.sh +35 -0
  10. package/skills/magpie/evals/post-folds-selection-events/graders/selection-read-from-magpie-view.md +6 -0
  11. package/skills/magpie/evals/post-folds-selection-events/prompt.md +2 -2
  12. package/skills/magpie/evals/rules.json +2 -2
  13. package/skills/magpie/package.json +1 -1
  14. package/skills/magpie/references/critic.md +2 -2
  15. package/skills/magpie/references/peer-review.md +7 -7
  16. package/skills/magpie/references/proof.md +2 -2
  17. package/skills/magpie/scripts/__tests__/helper.test.ts +300 -0
  18. package/skills/magpie/scripts/__tests__/labels.test.ts +46 -1
  19. package/skills/magpie/scripts/__tests__/mark-cmd.test.ts +129 -0
  20. package/skills/magpie/scripts/__tests__/peer-run.test.ts +231 -0
  21. package/skills/magpie/scripts/__tests__/post-cmd.test.ts +298 -1
  22. package/skills/magpie/scripts/__tests__/preflight.test.ts +22 -1
  23. package/skills/magpie/scripts/__tests__/render-cmd.test.ts +15 -1
  24. package/skills/magpie/scripts/__tests__/render-findings.test.ts +13 -0
  25. package/skills/magpie/scripts/__tests__/server.test.ts +96 -1
  26. package/skills/magpie/scripts/__tests__/setup-cmd.test.ts +14 -0
  27. package/skills/magpie/scripts/__tests__/skill-lint.test.ts +68 -4
  28. package/skills/magpie/scripts/__tests__/view-cmd.test.ts +219 -0
  29. package/skills/magpie/scripts/helper.js +44 -4
  30. package/skills/magpie/scripts/labels.ts +11 -0
  31. package/skills/magpie/scripts/mark-cmd.ts +56 -0
  32. package/skills/magpie/scripts/peer-run.ts +146 -0
  33. package/skills/magpie/scripts/post-cmd.ts +75 -5
  34. package/skills/magpie/scripts/preflight.ts +20 -20
  35. package/skills/magpie/scripts/render-cmd.ts +3 -1
  36. package/skills/magpie/scripts/render-findings.ts +3 -1
  37. package/skills/magpie/scripts/server.ts +31 -2
  38. package/skills/magpie/scripts/setup-cmd.ts +7 -1
  39. package/skills/magpie/scripts/view-cmd.ts +143 -0
  40. package/skills/magpie/skill.json +1 -1
  41. package/skills/sluice/SKILL.md +21 -6
  42. package/skills/sluice/references/deep-channel.md +35 -25
  43. package/skills/sluice/references/finish.md +2 -2
  44. package/skills/sluice/references/intent.md +1 -1
  45. package/skills/sluice/references/status.md +10 -4
  46. package/skills/sluice/references/test-first.md +12 -0
  47. package/skills/sluice/scripts/board-index.sh +59 -37
  48. package/skills/sluice/scripts/board.sh +72 -40
  49. package/skills/sluice/scripts/host.sh +60 -0
  50. package/skills/sluice/scripts/run-stats.sh +41 -3
  51. package/skills/sluice/scripts/session-start.sh +75 -17
  52. package/skills/sluice/scripts/status.sh +9 -5
  53. package/skills/sluice/scripts/stop-guard.sh +101 -37
  54. package/skills/sluice/scripts/transcript.sh +221 -0
  55. package/skills/sluice/skill.json +1 -1
@@ -0,0 +1,231 @@
1
+ import { expect, test } from 'bun:test'
2
+ import { existsSync } from 'node:fs'
3
+ import { chmod, mkdtemp, readFile, writeFile } from 'node:fs/promises'
4
+ import { tmpdir } from 'node:os'
5
+ import { join } from 'node:path'
6
+ import { killProviderGroup, peerArgv, peerConfigFromEnv, runPeer } from '../peer-run.ts'
7
+
8
+ const CLI = new URL('../../bin/magpie.ts', import.meta.url).pathname
9
+ const P = 'review these findings'
10
+
11
+ async function fakeProvider(body: string): Promise<string> {
12
+ const dir = await mkdtemp(join(tmpdir(), 'magpie-peer-bin-'))
13
+ const bin = join(dir, 'fake-codex')
14
+ await writeFile(bin, `#!/bin/sh\n${body}\n`)
15
+ await chmod(bin, 0o755)
16
+ return bin
17
+ }
18
+
19
+ // A killed grandchild is reparented and reaped asynchronously, so for a moment it
20
+ // can linger as exiting (E) or a zombie (Z); both count as stopped, since it runs nothing.
21
+ function isRunning(pid: number): boolean {
22
+ const stat = Bun.spawnSync(['ps', '-o', 'stat=', '-p', String(pid)]).stdout.toString().trim()
23
+ return stat !== '' && !stat.startsWith('Z') && !stat.startsWith('E')
24
+ }
25
+
26
+ async function runDirWith(files: Record<string, string>): Promise<string> {
27
+ const runDir = await mkdtemp(join(tmpdir(), 'magpie-peer-run-'))
28
+ for (const [name, text] of Object.entries(files)) await writeFile(join(runDir, name), text)
29
+ return runDir
30
+ }
31
+
32
+ test('codex preset runs exec with the model and reads the prompt from stdin', () => {
33
+ expect(peerArgv('codex', 'codex', 'm1', P)).toEqual({
34
+ argv: ['codex', 'exec', '-m', 'm1', '-'],
35
+ stdin: P,
36
+ })
37
+ })
38
+
39
+ test('codex preset without a model leaves out the -m pair', () => {
40
+ expect(peerArgv('codex', 'codex', null, P)).toEqual({ argv: ['codex', 'exec', '-'], stdin: P })
41
+ })
42
+
43
+ test('claude preset runs print mode with the model and reads the prompt from stdin', () => {
44
+ expect(peerArgv('claude', 'claude', 'm2', P)).toEqual({
45
+ argv: ['claude', '-p', '--model', 'm2'],
46
+ stdin: P,
47
+ })
48
+ })
49
+
50
+ test('agy preset passes the prompt as an argument and nothing on stdin', () => {
51
+ expect(peerArgv('agy', 'agy', 'm3', P)).toEqual({
52
+ argv: ['agy', '-p', P, '--model', 'm3'],
53
+ stdin: null,
54
+ })
55
+ })
56
+
57
+ test('an empty environment configures codex with no model, 600s and the codex binary', () => {
58
+ expect(peerConfigFromEnv({})).toEqual({ provider: 'codex', model: null, timeoutSec: 600, bin: 'codex' })
59
+ })
60
+
61
+ test('MAGPIE_PEER, MAGPIE_PEER_MODEL and the provider bin variable pick the peer', () => {
62
+ expect(
63
+ peerConfigFromEnv({
64
+ MAGPIE_PEER: 'agy',
65
+ MAGPIE_PEER_MODEL: 'm9',
66
+ MAGPIE_PEER_TIMEOUT: '30',
67
+ MAGPIE_AGY_BIN: '/opt/agy',
68
+ MAGPIE_CODEX_BIN: '/opt/codex',
69
+ }),
70
+ ).toEqual({ provider: 'agy', model: 'm9', timeoutSec: 30, bin: '/opt/agy' })
71
+ })
72
+
73
+ test('an unknown MAGPIE_PEER throws naming the known providers', () => {
74
+ expect(() => peerConfigFromEnv({ MAGPIE_PEER: 'gpt' })).toThrow('codex, claude, agy')
75
+ })
76
+
77
+ test('a non-positive MAGPIE_PEER_TIMEOUT throws', () => {
78
+ expect(() => peerConfigFromEnv({ MAGPIE_PEER_TIMEOUT: '0' })).toThrow('MAGPIE_PEER_TIMEOUT')
79
+ })
80
+
81
+ test('a non-numeric MAGPIE_PEER_TIMEOUT throws', () => {
82
+ expect(() => peerConfigFromEnv({ MAGPIE_PEER_TIMEOUT: 'soon' })).toThrow('MAGPIE_PEER_TIMEOUT')
83
+ })
84
+
85
+ test('runPeer writes the provider stdout to peer.out', async () => {
86
+ const bin = await fakeProvider('cat')
87
+ const runDir = await runDirWith({ 'peer-prompt.md': 'single batch prompt' })
88
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin })
89
+ expect(result).toEqual({ ok: true, outPath: join(runDir, 'peer.out') })
90
+ expect(await readFile(join(runDir, 'peer.out'), 'utf8')).toBe('single batch prompt')
91
+ })
92
+
93
+ test('peer-run --batch 2 reads peer-prompt-2.md and writes peer-2.out', async () => {
94
+ const bin = await fakeProvider('cat')
95
+ const runDir = await runDirWith({ 'peer-prompt.md': 'wrong', 'peer-prompt-2.md': 'second batch' })
96
+ const proc = Bun.spawn(['bun', CLI, 'peer-run', runDir, '--batch', '2'], {
97
+ env: { ...process.env, MAGPIE_PEER: 'codex', MAGPIE_CODEX_BIN: bin },
98
+ stdout: 'pipe',
99
+ stderr: 'pipe',
100
+ })
101
+ expect(await proc.exited).toBe(0)
102
+ expect(await readFile(join(runDir, 'peer-2.out'), 'utf8')).toBe('second batch')
103
+ })
104
+
105
+ test('a provider exiting non-zero yields its first stderr line as the error', async () => {
106
+ const bin = await fakeProvider('echo boom >&2\necho later >&2\nexit 3')
107
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
108
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin })
109
+ expect(result.ok).toBe(false)
110
+ expect(!result.ok && result.error).toContain('boom')
111
+ expect(!result.ok && result.error).not.toContain('later')
112
+ })
113
+
114
+ test('peer-run exits 1 printing the provider stderr line', async () => {
115
+ const bin = await fakeProvider('echo boom >&2\nexit 3')
116
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
117
+ const proc = Bun.spawn(['bun', CLI, 'peer-run', runDir], {
118
+ env: { ...process.env, MAGPIE_PEER: 'codex', MAGPIE_CODEX_BIN: bin },
119
+ stdout: 'pipe',
120
+ stderr: 'pipe',
121
+ })
122
+ expect(await proc.exited).toBe(1)
123
+ expect(await new Response(proc.stderr).text()).toContain('boom')
124
+ })
125
+
126
+ test('a timed-out provider is killed along with the processes it started', async () => {
127
+ const pidFile = join(await mkdtemp(join(tmpdir(), 'magpie-peer-pid-')), 'child.pid')
128
+ const bin = await fakeProvider(`sleep 30 &\necho $! > ${pidFile}\nwait`)
129
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
130
+ await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '1' })
131
+ const childPid = Number(await readFile(pidFile, 'utf8'))
132
+ expect(isRunning(childPid)).toBe(false)
133
+ })
134
+
135
+ test('a provider running past MAGPIE_PEER_TIMEOUT is reported as timed out', async () => {
136
+ const bin = await fakeProvider('sleep 5')
137
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
138
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '1' })
139
+ expect(result.ok).toBe(false)
140
+ expect(!result.ok && result.error).toContain('timed out after 1s')
141
+ })
142
+
143
+ test('SIGTERM to peer-run kills the provider group and exits 143', async () => {
144
+ const pidFile = join(await mkdtemp(join(tmpdir(), 'magpie-peer-pid-')), 'child.pid')
145
+ const bin = await fakeProvider(`sleep 30 &\necho $! > ${pidFile}\nwait`)
146
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
147
+ const proc = Bun.spawn(['bun', CLI, 'peer-run', runDir], {
148
+ env: { ...process.env, MAGPIE_PEER: 'codex', MAGPIE_CODEX_BIN: bin },
149
+ stdout: 'pipe',
150
+ stderr: 'pipe',
151
+ })
152
+ const deadline = Date.now() + 5000
153
+ while (!existsSync(pidFile) || (await readFile(pidFile, 'utf8')).trim() === '') {
154
+ if (Date.now() > deadline) throw new Error('fake provider never wrote its child pid')
155
+ await Bun.sleep(20)
156
+ }
157
+ const childPid = Number(await readFile(pidFile, 'utf8'))
158
+ proc.kill('SIGTERM')
159
+ expect(await proc.exited).toBe(143)
160
+ expect(isRunning(childPid)).toBe(false)
161
+ })
162
+
163
+ test('SIGHUP to peer-run kills the provider group and exits 129', async () => {
164
+ const pidFile = join(await mkdtemp(join(tmpdir(), 'magpie-peer-pid-')), 'child.pid')
165
+ const bin = await fakeProvider(`sleep 30 &\necho $! > ${pidFile}\nwait`)
166
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
167
+ const proc = Bun.spawn(['bun', CLI, 'peer-run', runDir], {
168
+ env: { ...process.env, MAGPIE_PEER: 'codex', MAGPIE_CODEX_BIN: bin },
169
+ stdout: 'pipe',
170
+ stderr: 'pipe',
171
+ })
172
+ const deadline = Date.now() + 5000
173
+ while (!existsSync(pidFile) || (await readFile(pidFile, 'utf8')).trim() === '') {
174
+ if (Date.now() > deadline) throw new Error('fake provider never wrote its child pid')
175
+ await Bun.sleep(20)
176
+ }
177
+ const childPid = Number(await readFile(pidFile, 'utf8'))
178
+ proc.kill('SIGHUP')
179
+ expect(await proc.exited).toBe(129)
180
+ expect(isRunning(childPid)).toBe(false)
181
+ })
182
+
183
+ test('killing a provider group that has already exited does not throw', async () => {
184
+ const gone = Bun.spawn(['true'], { detached: true })
185
+ await gone.exited
186
+ expect(() => killProviderGroup(gone.pid)).not.toThrow()
187
+ })
188
+
189
+ test('a provider that exits 0 but leaves a child holding its output returns its output within the time limit', async () => {
190
+ const bin = await fakeProvider('sleep 20 &\necho OUT\nexit 0')
191
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
192
+ const started = Date.now()
193
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '2' })
194
+ expect(Date.now() - started).toBeLessThan(3000)
195
+ expect(result).toEqual({ ok: true, outPath: join(runDir, 'peer.out') })
196
+ expect(await readFile(join(runDir, 'peer.out'), 'utf8')).toBe('OUT\n')
197
+ }, 30000)
198
+
199
+ test('a provider that exits 0 does not leave the child it started running', async () => {
200
+ const pidFile = join(await mkdtemp(join(tmpdir(), 'magpie-peer-pid-')), 'child.pid')
201
+ const bin = await fakeProvider(`sleep 20 >/dev/null 2>&1 &\necho $! > ${pidFile}\nexit 0`)
202
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
203
+ await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '2' })
204
+ const childPid = Number(await readFile(pidFile, 'utf8'))
205
+ expect(isRunning(childPid)).toBe(false)
206
+ }, 30000)
207
+
208
+ test('a provider that exits non-zero but leaves a child holding its stderr fails within the time limit', async () => {
209
+ const bin = await fakeProvider('sleep 20 &\necho boom >&2\nexit 4')
210
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
211
+ const started = Date.now()
212
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '2' })
213
+ expect(Date.now() - started).toBeLessThan(3000)
214
+ expect(result).toEqual({ ok: false, error: 'boom' })
215
+ }, 30000)
216
+
217
+ test('a child that leaves the provider group and holds its output still ends the run at the time limit', async () => {
218
+ const pidFile = join(await mkdtemp(join(tmpdir(), 'magpie-peer-pid-')), 'child.pid')
219
+ const bin = await fakeProvider(
220
+ // The provider waits for the pid file so the child has left the group before the provider exits.
221
+ `perl -e 'setpgrp(0, 0); open(my $f, ">", $ARGV[0]); print $f $$; close $f; exec "sleep", "20"' ${pidFile} &\nwhile [ ! -s ${pidFile} ]; do sleep 0.05; done\necho OUT\nexit 0`,
222
+ )
223
+ const runDir = await runDirWith({ 'peer-prompt.md': 'p' })
224
+ const started = Date.now()
225
+ const result = await runPeer(runDir, null, { MAGPIE_CODEX_BIN: bin, MAGPIE_PEER_TIMEOUT: '2' })
226
+ const elapsed = Date.now() - started
227
+ // setpgrp made the child its own group leader, so its pid names its group.
228
+ killProviderGroup(Number(await readFile(pidFile, 'utf8')))
229
+ expect(elapsed).toBeLessThan(3000)
230
+ expect(result).toEqual({ ok: false, error: 'timed out after 2s' })
231
+ }, 30000)
@@ -1,5 +1,6 @@
1
1
  import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
2
- import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
2
+ import { existsSync } from 'node:fs'
3
+ import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
3
4
  import { tmpdir } from 'node:os'
4
5
  import { join } from 'node:path'
5
6
  import {
@@ -11,6 +12,7 @@ import {
11
12
  parseRepoFromUrl,
12
13
  postFindingsAsReview,
13
14
  runPost,
15
+ runPostReview,
14
16
  } from '../post-cmd.ts'
15
17
 
16
18
  let runDir: string
@@ -680,3 +682,298 @@ test('formatInlineBody leaves a not-proven note out of the posted comment', () =
680
682
  } as never)
681
683
  expect(body).not.toContain('needs a live database')
682
684
  })
685
+
686
+ // ---------------------------------------------------------------------------
687
+ // post --review on the example-pr fixture
688
+ // ---------------------------------------------------------------------------
689
+
690
+ const FIXTURE = new URL('../../fixtures/example-pr', import.meta.url).pathname
691
+ const CLI = new URL('../../bin/magpie.ts', import.meta.url).pathname
692
+
693
+ async function fixtureRunDir(): Promise<string> {
694
+ const dir = await mkdtemp(join(tmpdir(), 'magpie-post-fixture-'))
695
+ await cp(FIXTURE, dir, { recursive: true })
696
+ return dir
697
+ }
698
+
699
+ /** A gh that records each review payload it is sent, one per line, and answers review 501. */
700
+ async function recordingGh(dir: string): Promise<{ bin: string; calls: () => Promise<string[]> }> {
701
+ const bin = join(dir, 'recording-gh.sh')
702
+ const callsPath = join(dir, 'gh-calls')
703
+ await writeFile(
704
+ bin,
705
+ `#!/usr/bin/env bash\ncat >> '${callsPath}'\necho >> '${callsPath}'\necho '{"id": 501}'\n`,
706
+ )
707
+ await Bun.spawn(['chmod', '+x', bin]).exited
708
+ return {
709
+ bin,
710
+ calls: async () => {
711
+ if (!existsSync(callsPath)) return []
712
+ return (await readFile(callsPath, 'utf8')).split('\n').filter(Boolean)
713
+ },
714
+ }
715
+ }
716
+
717
+ async function failingGh(dir: string): Promise<string> {
718
+ const bin = join(dir, 'failing-gh.sh')
719
+ await writeFile(
720
+ bin,
721
+ '#!/usr/bin/env bash\ncat >/dev/null\necho "HTTP 502: bad gateway" >&2\nexit 1\n',
722
+ )
723
+ await Bun.spawn(['chmod', '+x', bin]).exited
724
+ return bin
725
+ }
726
+
727
+ async function runCli(
728
+ args: string[],
729
+ ghBin: string,
730
+ ): Promise<{ exit: number; stdout: string; stderr: string }> {
731
+ const proc = Bun.spawn(['bun', CLI, 'post', ...args], {
732
+ stdout: 'pipe',
733
+ stderr: 'pipe',
734
+ env: { ...process.env, MAGPIE_GH_BIN: ghBin },
735
+ })
736
+ const [stdout, stderr, exit] = await Promise.all([
737
+ new Response(proc.stdout).text(),
738
+ new Response(proc.stderr).text(),
739
+ proc.exited,
740
+ ])
741
+ return { exit, stdout, stderr }
742
+ }
743
+
744
+ describe('postFindingsAsReview with ids already posted', () => {
745
+ let dir: string
746
+ beforeEach(async () => {
747
+ dir = await fixtureRunDir()
748
+ })
749
+ afterEach(async () => {
750
+ await rm(dir, { recursive: true, force: true })
751
+ })
752
+
753
+ test('sends a review holding only the ids not yet posted', async () => {
754
+ const gh = await recordingGh(dir)
755
+ await postFindingsAsReview({
756
+ runDir: dir,
757
+ findingIds: ['security-1', 'bugs-1'],
758
+ prNumber: 1337,
759
+ headSha: '9f3a8c0211dbb5fe7a82a2c1b08e0a45c2d1ee01',
760
+ repo: 'example/repo',
761
+ ghBin: gh.bin,
762
+ })
763
+ const calls = await gh.calls()
764
+ expect(calls).toHaveLength(1)
765
+ const payload = JSON.parse(calls[0] as string) as {
766
+ body: string
767
+ comments: Array<{ path: string; line: number }>
768
+ }
769
+ expect(payload.comments.map((c) => ({ path: c.path, line: c.line }))).toEqual([
770
+ { path: 'src/cache/invalidation.ts', line: 142 },
771
+ ])
772
+ expect(payload.body).not.toContain('Session token written to Redis')
773
+ })
774
+
775
+ test('reports an id already posted as already-posted', async () => {
776
+ const gh = await recordingGh(dir)
777
+ const r = await postFindingsAsReview({
778
+ runDir: dir,
779
+ findingIds: ['security-1', 'bugs-1'],
780
+ prNumber: 1337,
781
+ headSha: '9f3a8c0211dbb5fe7a82a2c1b08e0a45c2d1ee01',
782
+ repo: 'example/repo',
783
+ ghBin: gh.bin,
784
+ })
785
+ expect(r.comments).toEqual([
786
+ { id: 'security-1', status: 'already-posted' },
787
+ { id: 'bugs-1', status: 'posted' },
788
+ ])
789
+ })
790
+
791
+ test('posting the same id twice invokes gh once', async () => {
792
+ const gh = await recordingGh(dir)
793
+ const input = {
794
+ runDir: dir,
795
+ findingIds: ['bugs-1'],
796
+ prNumber: 1337,
797
+ headSha: '9f3a8c0211dbb5fe7a82a2c1b08e0a45c2d1ee01',
798
+ repo: 'example/repo',
799
+ ghBin: gh.bin,
800
+ }
801
+ await postFindingsAsReview(input)
802
+ const second = await postFindingsAsReview(input)
803
+ expect(await gh.calls()).toHaveLength(1)
804
+ expect(second.reviewId).toBeNull()
805
+ expect(second.comments).toEqual([{ id: 'bugs-1', status: 'already-posted' }])
806
+ })
807
+ })
808
+
809
+ describe('runPostReview', () => {
810
+ let dir: string
811
+ beforeEach(async () => {
812
+ dir = await fixtureRunDir()
813
+ })
814
+ afterEach(async () => {
815
+ await rm(dir, { recursive: true, force: true })
816
+ })
817
+
818
+ test('posts the review to the PR named in pr.json and returns its id', async () => {
819
+ const gh = await recordingGh(dir)
820
+ const r = await runPostReview({ runDir: dir, findingIds: ['bugs-1'], ghBin: gh.bin })
821
+ expect(r).toEqual({
822
+ ok: true,
823
+ reviewId: '501',
824
+ results: [{ id: 'bugs-1', status: 'posted' }],
825
+ })
826
+ })
827
+
828
+ test('fails with an error when pr.json has no head SHA', async () => {
829
+ const prJson = JSON.parse(await readFile(join(dir, 'pr.json'), 'utf8'))
830
+ delete prJson.headRefOid
831
+ await writeFile(join(dir, 'pr.json'), JSON.stringify(prJson))
832
+ const gh = await recordingGh(dir)
833
+ const r = await runPostReview({ runDir: dir, findingIds: ['bugs-1'], ghBin: gh.bin })
834
+ expect(r.ok).toBe(false)
835
+ expect(r.reviewId).toBeNull()
836
+ expect(r.error).toContain('headSha')
837
+ expect(await gh.calls()).toHaveLength(0)
838
+ })
839
+
840
+ test('is not ok when gh fails to create the review', async () => {
841
+ const r = await runPostReview({
842
+ runDir: dir,
843
+ findingIds: ['bugs-1'],
844
+ ghBin: await failingGh(dir),
845
+ })
846
+ expect(r.ok).toBe(false)
847
+ expect(r.reviewId).toBeNull()
848
+ expect(r.results).toEqual([
849
+ { id: 'bugs-1', status: 'failed', message: 'HTTP 502: bad gateway' },
850
+ ])
851
+ })
852
+
853
+ test('reports an id that is no finding as unknown-id and sends no review', async () => {
854
+ const gh = await recordingGh(dir)
855
+ const r = await runPostReview({ runDir: dir, findingIds: ['no-such-finding'], ghBin: gh.bin })
856
+ expect(r).toEqual({
857
+ ok: true,
858
+ reviewId: null,
859
+ results: [{ id: 'no-such-finding', status: 'unknown-id' }],
860
+ })
861
+ expect(await gh.calls()).toHaveLength(0)
862
+ })
863
+
864
+ test('never records an id that is no finding in post-status.json', async () => {
865
+ const gh = await recordingGh(dir)
866
+ await runPostReview({ runDir: dir, findingIds: ['no-such-finding', 'bugs-1'], ghBin: gh.bin })
867
+ const status = JSON.parse(await readFile(join(dir, 'post-status.json'), 'utf8'))
868
+ expect(status['no-such-finding']).toBeUndefined()
869
+ expect(status['bugs-1']).toBe('posted')
870
+ })
871
+
872
+ test('posts an id given twice as one inline comment and reports it once', async () => {
873
+ const gh = await recordingGh(dir)
874
+ const r = await runPostReview({ runDir: dir, findingIds: ['bugs-1', 'bugs-1'], ghBin: gh.bin })
875
+ const calls = await gh.calls()
876
+ const payload = JSON.parse(calls[0] as string) as { comments: unknown[] }
877
+ expect(calls).toHaveLength(1)
878
+ expect(payload.comments).toHaveLength(1)
879
+ expect(r.results).toEqual([{ id: 'bugs-1', status: 'posted' }])
880
+ })
881
+
882
+ test('is ok with no review when every id was already posted', async () => {
883
+ const gh = await recordingGh(dir)
884
+ const r = await runPostReview({
885
+ runDir: dir,
886
+ findingIds: ['security-1', 'arch-1'],
887
+ ghBin: gh.bin,
888
+ })
889
+ expect(r).toEqual({
890
+ ok: true,
891
+ reviewId: null,
892
+ results: [
893
+ { id: 'security-1', status: 'already-posted' },
894
+ { id: 'arch-1', status: 'already-posted' },
895
+ ],
896
+ })
897
+ expect(await gh.calls()).toHaveLength(0)
898
+ })
899
+ })
900
+
901
+ describe('magpie post CLI', () => {
902
+ let dir: string
903
+ beforeEach(async () => {
904
+ dir = await fixtureRunDir()
905
+ })
906
+ afterEach(async () => {
907
+ await rm(dir, { recursive: true, force: true })
908
+ })
909
+
910
+ test('post --review prints the review id and per-id results as JSON', async () => {
911
+ const gh = await recordingGh(dir)
912
+ const r = await runCli([dir, '--ids', 'security-1,bugs-1', '--review'], gh.bin)
913
+ expect(r.exit).toBe(0)
914
+ expect(JSON.parse(r.stdout)).toEqual({
915
+ ok: true,
916
+ reviewId: '501',
917
+ results: [
918
+ { id: 'security-1', status: 'already-posted' },
919
+ { id: 'bugs-1', status: 'posted' },
920
+ ],
921
+ })
922
+ })
923
+
924
+ test('post --review exits 2 when pr.json has no PR number', async () => {
925
+ const prJson = JSON.parse(await readFile(join(dir, 'pr.json'), 'utf8'))
926
+ delete prJson.number
927
+ await writeFile(join(dir, 'pr.json'), JSON.stringify(prJson))
928
+ const gh = await recordingGh(dir)
929
+ const r = await runCli([dir, '--ids', 'bugs-1', '--review'], gh.bin)
930
+ expect(r.exit).toBe(2)
931
+ expect(r.stderr).toStartWith('post: ')
932
+ })
933
+
934
+ test('post --review exits 1 when gh fails to create the review', async () => {
935
+ const r = await runCli([dir, '--ids', 'bugs-1', '--review'], await failingGh(dir))
936
+ expect(r.exit).toBe(1)
937
+ expect(JSON.parse(r.stdout).ok).toBe(false)
938
+ })
939
+
940
+ test('post --review exits 0 when every id was already posted', async () => {
941
+ const gh = await recordingGh(dir)
942
+ const r = await runCli([dir, '--ids', 'security-1', '--review'], gh.bin)
943
+ expect(r.exit).toBe(0)
944
+ expect(JSON.parse(r.stdout)).toEqual({
945
+ ok: true,
946
+ reviewId: null,
947
+ results: [{ id: 'security-1', status: 'already-posted' }],
948
+ })
949
+ })
950
+
951
+ test('post --review --via one records via one on the post log entries', async () => {
952
+ const gh = await recordingGh(dir)
953
+ await runCli([dir, '--ids', 'bugs-1', '--review', '--via', 'one'], gh.bin)
954
+ const ok = (await logEntries(dir)).filter((e) => e.stage === 'post' && e.status === 'ok')
955
+ expect(ok.map((e) => ({ id: e.id, via: e.via }))).toEqual([{ id: 'bugs-1', via: 'one' }])
956
+ })
957
+
958
+ test('post --via selected records via selected on the comment route', async () => {
959
+ const gh = await recordingGh(dir)
960
+ await runCli([dir, '--ids', 'bugs-1', '--via', 'selected'], gh.bin)
961
+ const ok = (await logEntries(dir)).filter((e) => e.status === 'ok' && e.id === 'bugs-1')
962
+ expect(ok.map((e) => e.via)).toEqual(['selected'])
963
+ })
964
+
965
+ test('post without --via records via cli on the comment route', async () => {
966
+ const gh = await recordingGh(dir)
967
+ await runCli([dir, '--ids', 'bugs-1'], gh.bin)
968
+ const ok = (await logEntries(dir)).filter((e) => e.status === 'ok' && e.id === 'bugs-1')
969
+ expect(ok.map((e) => e.via)).toEqual(['cli'])
970
+ })
971
+
972
+ test('post with an unknown --via exits 2 and posts nothing', async () => {
973
+ const gh = await recordingGh(dir)
974
+ const r = await runCli([dir, '--ids', 'bugs-1', '--review', '--via', 'bogus'], gh.bin)
975
+ expect(r.exit).toBe(2)
976
+ expect(r.stderr).toBe('post: invalid --via bogus (want recommended|selected|one|cli)\n')
977
+ expect(await gh.calls()).toHaveLength(0)
978
+ })
979
+ })
@@ -1,5 +1,5 @@
1
1
  import { expect, test } from 'bun:test'
2
- import { preflight } from '../preflight.ts'
2
+ import { defaultDeps, preflight, renderInstallHint } from '../preflight.ts'
3
3
 
4
4
  test('returns ok when all binaries resolve', async () => {
5
5
  const result = await preflight({
@@ -43,3 +43,24 @@ test('renderInstallHint produces a single-line message per missing tool', () =>
43
43
  expect(out).toContain('codex:')
44
44
  expect(out.split('\n').length).toBeGreaterThanOrEqual(2)
45
45
  })
46
+
47
+ test('the configured peer provider is the optional dependency', async () => {
48
+ const result = await preflight(
49
+ defaultDeps({ MAGPIE_PEER: 'claude', MAGPIE_CLAUDE_BIN: 'no-such-claude-bin-789' }),
50
+ )
51
+ expect(result.ok).toBe(true)
52
+ expect(result.missing).toEqual([])
53
+ expect(result.missingOptional).toEqual(['claude'])
54
+ })
55
+
56
+ test('renderInstallHint gives agy an install line rather than the not-on-PATH fallback', () => {
57
+ const hint = renderInstallHint(['agy'])
58
+ expect(hint).toStartWith('agy: install')
59
+ expect(hint).not.toContain('not found on PATH')
60
+ })
61
+
62
+ test('with no MAGPIE_PEER the codex binary is the optional dependency', async () => {
63
+ const result = await preflight(defaultDeps({ MAGPIE_CODEX_BIN: 'no-such-codex-bin' }))
64
+ expect(result.ok).toBe(true)
65
+ expect(result.missingOptional).toEqual(['codex'])
66
+ })
@@ -1,5 +1,5 @@
1
1
  import { afterEach, beforeEach, expect, test } from 'bun:test'
2
- import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'
2
+ import { cp, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join } from 'node:path'
5
5
  import { runRender } from '../render-cmd.ts'
@@ -334,3 +334,17 @@ for (const bad of ['0', '-2', '2.5', 'ten']) {
334
334
  expect(await readdir(join(runDir, 'screen'))).toEqual([])
335
335
  })
336
336
  }
337
+
338
+ test('render findings ticks the findings the run selected in state/events', async () => {
339
+ await cp(new URL('../../fixtures/example-pr', import.meta.url).pathname, runDir, {
340
+ recursive: true,
341
+ })
342
+ await writeFile(
343
+ join(runDir, 'state', 'events'),
344
+ `${JSON.stringify({ type: 'select', findingId: 'bugs-1', timestamp: 1 })}\n`,
345
+ )
346
+ expect(await runRender(runDir, 'findings')).toBe(0)
347
+ const html = await readFile(join(runDir, 'screen', 'findings.html'), 'utf8')
348
+ expect(html).toMatch(/data-finding-id="bugs-1" checked aria-label/)
349
+ expect(html).not.toMatch(/data-finding-id="perf-2" checked/)
350
+ })
@@ -64,6 +64,19 @@ test('empty findings renders the page shell with an empty-state message', () =>
64
64
  expect(html).toContain('No findings')
65
65
  })
66
66
 
67
+ test('a finding in selectedIds renders its checkbox checked and the others unchecked', () => {
68
+ const html = renderFindingsHtml({
69
+ findings: SAMPLE_FINDINGS,
70
+ postStatus: {},
71
+ selectedIds: new Set(['a']),
72
+ dismissed: new Map(),
73
+ topN: DEFAULT_TOP_N,
74
+ highlighter: hl,
75
+ })
76
+ expect(html).toMatch(/data-finding-id="a" checked aria-label/)
77
+ expect(html).not.toMatch(/data-finding-id="b" checked/)
78
+ })
79
+
67
80
  test('emits the two-tab segmented control with Files and All Issues buttons', () => {
68
81
  const html = renderFindingsHtml({
69
82
  findings: SAMPLE_FINDINGS,