8000 feat: report a review run's findings as validated JSON by dominikwozniak · Pull Request #30 · dominikwozniak/dw-mc · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ _Avoid_: reviewer, provider, model
One problem a review run reports, at a file and line, with a severity of error, warning or info. An error is a blocking finding: it withholds the stamp until the head changes.
_Avoid_: issue, comment, violation

**Outcome**:
What a review run came to: the verdict and findings it reported, or the failure it reached instead. Every recorded run has exactly one.
_Avoid_: result, status

**Failure**:
A review run that reached no verdict: the runner exited badly, ran out of patience, or answered in a shape that does not validate. Recorded as what it is, never as a clean verdict.
_Avoid_: error, crash

**Re-run rule**:
When a review run is repeated on a pull request that has already had one: only where files outside the repository's `docs_only` globs changed since that run. A force flag overrides it.
_Avoid_: cache, debounce

**Fix session**:
An interactive agent session mission control opens for me in a fresh worktree, carrying the findings I selected and my notes on them. It is never the review run that produced the findings; the re-review of its result is a new review run.
_Avoid_: auto-fix, repair run
48 changes: 47 additions & 1 deletion src/adapters/gh.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { assert, describe, it } from "@effect/vitest"
import { Effect, PlatformError } from "effect"

import { currentRepo, mergeabilityOf, prComments, requireAuth, reviewDecisionOf, viewer } from "#adapters/gh.ts"
import {
comparedFiles,
currentRepo,
mergeabilityOf,
prComments,
requireAuth,
reviewDecisionOf,
viewer
} from "#adapters/gh.ts"
import { fakeHandle, layerFake } from "#adapters/spawner.ts"

/** A spawner that answers every program the same way, and records the argv. */
Expand Down Expand Up @@ -155,3 +163,41 @@ describe("prComments", () => {
}).pipe(Effect.provide(answered))
})
})

describe("comparedFiles", () => {
const base = "284d599022a55d4dcae74b31b9a49a0f50061014"
const head = "9c1f0b7a1d1e4a2c8b3f5d6e7a8b9c0d1e2f3a4b"

it.effect("asks GitHub what changed between two commits", () => {
const spawned: Array<ReadonlyArray<string>> = []
const compared = answering(spawned, {
stdout: JSON.stringify({ files: [{ filename: "README.md" }, { filename: "src/cli/review.ts" }] })
})

return Effect.gen(function* () {
const files = yield* comparedFiles("dominikwozniak/dw-mc", base, head)

assert.deepStrictEqual(files, ["README.md", "src/cli/review.ts"])
assert.deepStrictEqual(spawned, [["gh", "api", `repos/dominikwozniak/dw-mc/compare/${base}...${head}`]])
}).pipe(Effect.provide(compared))
})

it.effect("reads a comparison with nothing between its two commits", () => {
const empty = answering([], { stdout: JSON.stringify({ status: "identical" }) })

return Effect.gen(function* () {
assert.deepStrictEqual(yield* comparedFiles("dominikwozniak/dw-mc", base, base), [])
}).pipe(Effect.provide(empty))
})

it.effect("says what it could not read rather than reporting no change", () => {
const gone = answering([], { exitCode: 1, stderr: "gh: No commit found for SHA\n" })

return Effect.gen(function* () {
const error = yield* Effect.flip(comparedFiles("dominikwozniak/dw-mc", base, head))

assert.strictEqual(error._tag, "GhReadFailed")
assert.include(error.message, "No commit found")
}).pipe(Effect.provide(gone))
})
})
24 changes: 24 additions & 0 deletions src/adapters/gh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ export const prReviews = Effect.fnUntraced(function* (repo: string, number: numb
)
})

const Compare = Schema.fromJsonString(
Schema.Struct({ files: Schema.optionalKey(Schema.Array(Schema.Struct({ filename: Schema.St 10BC8 ring }))) })
)

/**
* The repository paths that changed between two commits.
*
* GitHub compares them rather than git, because the commit a run was recorded
* against is not one the tool's own clone is promised to still have: a force
* push moves the pull request's ref and the old commit goes with it, where
* GitHub keeps both sides of the comparison. A comparison of a commit with
* itself reports no files at all, and so does one of two commits with nothing
* between them, which is why the key is optional.
*
* `base...head` measures from where the two commits last agreed, so two heads
* on one branch report what was pushed between them, and a branch rebased since
* reports its whole diff. The second is the right answer for a caller deciding
* whether the code has moved: after a rebase it has, all of it.
*/
export const comparedFiles = Effect.fnUntraced(function* (repo: string, base: string, head: string) {
const compare = yield* readJson("api compare", "gh", ["api", `repos/${repo}/compare/${base}...${head}`], Compare)
return (compare.files ?? []).map((file) => file.filename)
})

const Commits = Schema.fromJsonString(
Schema.Struct({
commits: Schema.Array(
Expand Down
132 changes: 130 additions & 2 deletions src/adapters/runner.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { assert, describe, it } from "@effect/vitest"
import { Effect } from "effect"
import { Duration, Effect, Fiber } from "effect"
import { TestClock } from "effect/testing"
import type { ChildProcess } from "effect/unstable/process"

import { builtinReview } from "#adapters/runner.ts"
import { builtinFindings, builtinReview } from "#adapters/runner.ts"
import { fakeHandle, layerFake } from "#adapters/spawner.ts"

const session = "befb6186-5471-4b26-b680-e8ca49df25ac"
Expand Down Expand Up @@ -177,3 +178,130 @@ describe("the built-in runner", () => {
}).pipe(Effect.provide(claude({ spawned, stdout: transcript(gaveUp) })))
})
})

describe("the built-in runner's second turn", () => {
const schema = `{"type":"object","properties":{"verdict":{"type":"string"}}}`
const found = {
verdict: "findings",
findings: [{ file: "src/cli/review.ts", line: 88, severity: "error", summary: "The run is never recorded." }]
}

/** What `claude --output-format json --json-schema` really prints: one result object. */
const answer = (fields: Record<string, unknown>) =>
JSON.stringify({
type: "result",
subtype: "success",
is_error: false,
session_id: session,
result: JSON.stringify(found),
num_turns: 2,
...fields
})

const reporting = (spawned: Array<ChildProcess.StandardCommand>) =>
builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema }).pipe(
Effect.provide(claude({ spawned, stdout: answer({ structured_output: found }) }))
)

it.effect("resumes the first turn's session and brings back what the runner validated", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
const output = yield* reporting(spawned)

assert.deepStrictEqual(output, found)
assert.deepStrictEqual(spawned[0]?.args.slice(0, 3), ["-p", "--resume", session])
assert.deepStrictEqual(spawned[0]?.args.slice(-4), ["--output-format", "json", "--json-schema", schema])
})
})

it.effect("asks for the findings of the review it just gave", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
yield* reporting(spawned)

assert.include(spawned[0]?.args[3] ?? "", "structured outp 8 ut" FCBE )
})
})

it.effect("reports in the worktree the review ran in", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
yield* reporting(spawned)

assert.strictEqual(spawned[0]?.options.cwd, "/worktree")
})
})

it.effect("a turn that exits non-zero is a failure", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
const error = yield* Effect.flip(
builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema })
)

assert.strictEqual(error._tag, "RunnerFailed")
assert.include(error.message, "No conversation found")
}).pipe(Effect.provide(claude({ spawned, stderr: "No conversation found\n", exitCode: 1 })))
})

it.effect("a turn that validated nothing is a failure, never a clean verdict", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
const error = yield* Effect.flip(
builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema })
)

assert.strictEqual(error._tag, "RunnerFailed")
assert.include(error.message, "no structured output")
}).pipe(Effect.provide(claude({ spawned, stdout: answer({}) })))
})

it.effect("a turn that answered with something this cannot read is a failure", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
const error = yield* Effect.flip(
builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema })
)

assert.strictEqual(error._tag, "RunnerFailed")
assert.include(error.message, "no result")
}).pipe(Effect.provide(claude({ spawned, stdout: "Command completed" })))
})

it.effect("a turn the runner itself calls an error is a failure", () => {
const spawned: Array<ChildProcess.StandardCommand> = []

return Effect.gen(function* () {
const error = yield* Effect.flip(
builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema })
)

assert.strictEqual(error._tag, "RunnerFailed")
assert.include(error.message, "error_during_execution")
}).pipe(
Effect.provide(
claude({ spawned, stdout: answer({ subtype: "error_during_execution", is_error: true, result: "gave up" }) })
)
)
})

it.effect("a turn that never comes back is a failure rather than a command that hangs", () =>
Effect.gen(function* () {
const turn = yield* Effect.forkChild(
Effect.flip(builtinFindings({ directory: "/worktree", sessionId: session, jsonSchema: schema }))
)

yield* TestClock.adjust(Duration.minutes(6))

const error = yield* Fiber.join(turn)
assert.strictEqual(error._tag, "RunnerFailed")
assert.include(error.message, "did not come back")
}).pipe(Effect.provide(layerFake(() => Effect.never)))
)
})
Loading
0