8000 refactor(cli): the preamble and the exit code, once each by dominikwozniak · Pull Request #94 · 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
13 changes: 5 additions & 8 deletions src/cli/comments.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Console, DateTime, Effect, Option } from "effect"
import { Console, DateTime, Effect } from "effect"
import { Command, Flag } from "effect/unstable/cli"

import type { ConfigFile } from "#adapters/config.ts"
import { read as readConfig } from "#adapters/config.ts"
import type { Thread } from "#adapters/conversation.ts"
import { prConversation } from "#adapters/conversation.ts"
import type { Paint } from "#adapters/paint.ts"
import { Paint as PaintService } from "#adapters/paint.ts"
import { named, prArgument, reading, swept } from "#cli/pr.ts"
import { asUserError, userFacing } from "#cli/exit.ts"
import { forPr, prArgument, reading, swept } from "#cli/pr.ts"
import { heading } from "#cli/row.ts"
import { asUserError, userFacing } from "#cli/sweep.ts"
import type { Facts } from "#domain/bucket.ts"
import { place, unanswered } from "#domain/bucket.ts"
import type { Shown } from "#domain/comments.ts"
Expand Down Expand Up @@ -111,8 +109,7 @@ export const comments = Command.make(
{ pr: prArgument, all: allFlag },
Effect.fn("comments")(
function* ({ all, pr }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const { number, repo } = yield* forPr(pr)

const facts = yield* swept(repo, number)
const paint = yield* PaintService
Expand All @@ -130,6 +127,6 @@ export const comments = Command.make(
yield* Console.log("")
yield* Effect.forEach(lines(view, paint), (line) => Console.log(line))
},
Effect.catchTag(["ConfigMalformed", ...userFacing], asUserError)
Effect.catchTag(userFacing, asUserError)
)
).pipe(Command.withDescription("Print the conversation on one pull request, and what is waiting on me in it"))
29 changes: 29 additions & 0 deletions src/cli/exit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Effect } from "effect"
import { CliError } from "effect/unstable/cli"

/**
* The failures a command owes me a sentence for rather than a stack.
*
* Every one of them is a machine or a file that needs fixing, and the message
* says what to fix. Anything not named here is a fault of the tool's own, and a
* stack is what I want to see for those.
*/
export const userFacing = ["ConfigMalformed", "GhUnavailable", "GhReadFailed", "GhUnreadable"] as const

/** The same, for a command that also runs `git` against the tool's own clone. */
export const userFacingAndGit = [...userFacing, "GitFailed"] as const

/**
* The same, for a command that cuts a standing worktree and opens an agent
* session in it.
*
* `dw-mc fix` and `dw-mc resolve` are the two, and they fail the same ways
* because they do the same thing to different findings: a worktree that holds
* work of mine and an agent that would not run are the session's failures, not
* either command's.
*/
export const userFacingAndSession = [...userFacing, "GitFailed", "WorktreeHeld", "AgentFailed"] as const

/** Turns one of those into the sentence the CLI prints, and the exit code it leaves. */
export const asUserError = (cause: unknown): Effect.Effect<never, CliError.UserError> =>
Effect.fail(new CliError.UserError({ cause }))
30 changes: 5 additions & 25 deletions src/cli/findings.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Console, Effect, Option, Schema } from "effect"
import { Console, Effect, Schema } from "effect"
import { CliError, Command, Flag } from "effect/unstable/cli"

import type { ConfigFile } from "#adapters/config.ts"
import { read as readConfig, settingsFor } from "#adapters/config.ts"
import { named, prArgument } from "#cli/pr.ts"
import { asUserError } from "#cli/exit.ts"
import { currentRun, forPr, prArgument } from "#cli/pr.ts"
import { rule } from "#cli/row.ts"
import { asUserError } from "#cli/sweep.ts"
import { count, table } from "#cli/table.ts"
import type { Findings } from "#domain/findings.ts"
import { blocking, Findings as FindingsSchema } from "#domain/findings.ts"
import type { ReviewRun } from "#domain/review.ts"
import { lastRun, reportedBy, short } from "#domain/review.ts"
import { reportedBy, short } from "#domain/review.ts"
import type { Severity } from "#terms/review.ts"

/** The findings as the JSON the schema defines, rather than as this file spells it. */
Expand Down Expand Up @@ -44,22 +42,6 @@ export const lines = (found: Findings): ReadonlyArray<string> =>
rule
)

/**
* The review run whose findings are the current ones, or the sentence saying
* there are none.
*
* The last run on the pull request is what "current" means here, and it is read
* off the state directory rather than worked out from GitHub: this command is
* one I run inside a fix session, where another round trip to GitHub buys
* nothing the run it is about to fix does not already say.
*/
export const currentRun = Effect.fn("findings.currentRun")(function* (repo: string, number: number) {
const run = yield* lastRun(repo, number)
return Option.isSome(run)
? run.value
: yield* asUserError(`No review run on ${repo}#${number}. Run dw-mc review ${number} first.`)
})

/**
* What the run reported, or the sentence saying it reported nothing at all.
*
Expand Down Expand Up @@ -96,9 +78,7 @@ export const findings = Command.make(
{ pr: prArgument, json: jsonFlag },
Effect.fn("findings")(
function* ({ json, pr }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const settings = settingsFor(file, repo)
const { number, repo, settings } = yield* forPr(pr)

const run = yield* currentRun(repo, number)
const found = yield* whatItFound(run)
Expand Down
26 changes: 8 additions & 18 deletions src/cli/fix.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Console, Effect, Option } from "effect"
import { CliError, Command, Flag } from "effect/unstable/cli"
import { Command, Flag } from "effect/unstable/cli"

import { steeredSession } from "#adapters/claude.ts"
import type { ConfigFile } from "#adapters/config.ts"
import { launcherOf, read as readConfig, settingsFor } from "#adapters/config.ts"
import { prView } from "#adapters/gh.ts"
import { standingWorktree } from "#adapters/git.ts"
import { choose, note, width } from "#adapters/picker.ts"
import { currentRun, header, lines, whatItFound } from "#cli/findings.ts"
import { named, prArgument, reading } from "#cli/pr.ts"
import { asUserError, userFacing } from "#cli/sweep.ts"
import { asUserError, userFacingAndSession } from "#cli/exit.ts"
import { header, lines, whatItFound } from "#cli/findings.ts"
import { currentRun, forPr, prArgument, reading, refuse } from "#cli/pr.ts"
import { truncate } from "#cli/table.ts"
import type { Finding, Findings } from "#domain/findings.ts"
import type { Chosen } from "#domain/fix.ts"
Expand Down Expand Up @@ -58,12 +56,6 @@ const noted = Effect.fn("fix.noted")(function* (picked: ReadonlyArray<Finding>)
return chosen
})

/** The domain's word on a head that has moved, as the command's own failure. */
const fixable = (number: number, run: string, now: string) => {
const stale = staleAt(number, run, now)
return stale === null ? Effect.void : Effect.fail(new CliError.UserError({ cause: stale }))
}

/**
* A fix session: the findings I picked, in an agent session I steer.
*
Expand All @@ -84,9 +76,7 @@ export const fix = Command.make(
{ pr: prArgument, commit: commitFlag, print: printFlag },
Effect.fn("fix")(
function* ({ commit, pr, print }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const settings = settingsFor(file, repo)
const { number, repo, settings, launcher } = yield* forPr(pr)

const run = yield* currentRun(repo, number)
const found = yield* whatItFound(run)
Expand All @@ -96,7 +86,7 @@ export const fix = Command.make(
}

const view = yield* reading(`${repo}#${number}`, prView(repo, number))
yield* fixable(number, run.head, view.headRefOid)
yield* refuse(staleAt(number, run.head, view.headRefOid))

const picked = yield* choose("Which findings does the session carry?", choicesOf(found, yield* width))
const chosen = yield* Effect.catchTag(noted(Option.getOrElse(picked, () => [])), "QuitError", () =>
Expand All @@ -123,7 +113,7 @@ export const fix = Command.make(
yield* Console.log(` ${worktree.directory}, pushing to ${view.headRefName}`)

const ended = yield* steeredSession({
launcher: launcherOf(file),
launcher,
directory: worktree.directory,
prompt: yield* promptFor({ repo, number, head: worktree.head, findings: chosen }, commits)
})
Expand All @@ -135,6 +125,6 @@ export const fix = Command.make(
)
yield* Console.log(`Once you have pushed, dw-mc review ${number} reviews the new head as a new run.`)
},
Effect.catchTag([...userFacing, "GitFailed", "WorktreeHeld", "AgentFailed"], asUserError)
Effect.catchTag(userFacingAndSession, asUserError)
)
).pipe(Command.withDescription("Pick findings from the current review run and open a fix session on them"))
2 changes: 1 addition & 1 deletion src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ConfigFile, SettingsPatch } from "#adapters/config.ts"
import { builtIn, ConfigStore, encode, merge, read, withDefaults, withRepo, write } from "#adapters/config.ts"
import { currentRepo, requireAuth } from "#adapters/gh.ts"
import { stateDirectory } from "#adapters/store.ts"
import { asUserError } from "#cli/sweep.ts"
import { asUserError } from "#cli/exit.ts"
import type { Effort } from "#terms/review.ts"

const effortFlag = Flag.Literals("effort", ["low", "medium", "high", "xhigh", "max"]).pipe(
Expand Down
12 changes: 4 additions & 8 deletions src/cli/merge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Console, Effect, Option } from "effect"
import { Console, Effect } from "effect"
import { Command } from "effect/unstable/cli"

import { rollupState } from "#adapters/ci.ts"
import type { ConfigFile } from "#adapters/config.ts"
import { read as readConfig, settingsFor } from "#adapters/config.ts"
import { mergeabilityOf, mergePr, prView, reviewDecisionOf, viewer } from "#adapters/gh.ts"
import { named, prArgument, reading, refuse } from "#cli/pr.ts"
import { asUserError, userFacing } from "#cli/sweep.ts"
import { asUserError, userFacing } from "#cli/exit.ts"
import { forPr, prArgument, reading, refuse } from "#cli/pr.ts"
import { decide } from "#domain/merge.ts"
import { reviewedAt, short } from "#domain/review.ts"
import { withdrawnAt } from "#domain/stamp.ts"
Expand Down Expand Up @@ -37,9 +35,7 @@ export const merge = Command.make(
{ pr: prArgument },
Effect.fn("merge")(
function* ({ pr }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const settings = settingsFor(file, repo)
const { number, repo, settings } = yield* forPr(pr)

const [view, me] = yield* reading(`${repo}#${number}`, Effect.all([prView(repo, number), viewer]))

Expand Down
3 changes: 2 additions & 1 deletion src/cli/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { read as readConfig, settingsFor } from "#adapters/config.ts"
import { Paint, ink, plain } from "#adapters/paint.ts"
import { confirm, pick, width } from "#adapters/picker.ts"
import { prKey } from "#adapters/store.ts"
import { asUserError, userFacing } from "#cli/exit.ts"
import { cells, rule } from "#cli/row.ts"
import { asUserError, printTroubles, sweeping, userFacing } from "#cli/sweep.ts"
import { printTroubles, sweeping } from "#cli/sweep.ts"
import { table, truncate, visible } from "#cli/table.ts"
import type { Facts } from "#domain/bucket.ts"
import { group } from "#domain/bucket.ts"
Expand Down
42 changes: 42 additions & 0 deletions src/cli/pr.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Effect, Option } from "effect"
import { Argument, CliError } from "effect/unstable/cli"

import type { ConfigFile } from "#adapters/config.ts"
import { launcherOf, read as readConfig, settingsFor } from "#adapters/config.ts"
import { beating } from "#adapters/heartbeat.ts"
import { prKey, remembered, storeFor } from "#adapters/store.ts"
import { Facts } from "#domain/bucket.ts"
import type { Reference } from "#domain/reference.ts"
import { resolve } from "#domain/reference.ts"
import { lastRun } from "#domain/review.ts"

/** The pull request a command acts on, named the way I actually type it. */
export const prArgument = Argument.String("pr").pipe(
Expand Down Expand Up @@ -33,6 +36,26 @@ export const named = (pr: string, registered: ReadonlyArray<string>) => {
: Effect.fail(new CliError.UserError({ cause: whyNothingNamed(reference) }))
}

/**
* What a command that acts on one pull request opens with: which pull request
* it is, and what the configuration says about its repository.
*
* Nine commands ask the file the same three questions before they do anything
* else, and asking them here is what keeps the answers the same: which
* repositories are registered decides what a bare `28` may name, and a command
* that read the file its own way would resolve a different pull request from
* the one beside it.
*
* `settings` and `launcher` come back whether or not this command wants them,
* because both are a merge of records already in hand and neither reads
* anything. The file itself does not, so nothing downstream keeps a copy of it.
*/
export const forPr = Effect.fn("pr.forPr")(function* (pr: string) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
return { repo, number, settings: settingsFor(file, repo), launcher: launcherOf(file) }
})

/**
* A domain guard's word, as the command's own failure.
*
Expand Down Expand Up @@ -66,6 +89,25 @@ export const swept = Effect.fn("pr.swept")(function* (repo: string, number: numb
return facts.value
})

/**
* The review run whose findings are the current ones, or the sentence saying
* there are none.
*
* The last run on the pull request is what "current" means here, and it is read
* off the state directory rather than worked out from GitHub: the commands that
* ask are ones I run inside a fix session, where another round trip to GitHub
* buys nothing the run it is about to fix does not already say.
*/
export const currentRun = Effect.fn("pr.currentRun")(function* (repo: string, number: number) {
const run = yield* lastRun(repo, number)
if (Option.isNone(run)) {
return yield* new CliError.UserError({
cause: `No review run on ${repo}#${number}. Run dw-mc review ${number} first.`
})
}
return run.value
})

/**
* The guard reads of one command, under a heartbeat.
*
Expand Down
14 changes: 5 additions & 9 deletions src/cli/rebase.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Console, Effect, Option } from "effect"
import { Console, Effect } from "effect"
import { Command } from "effect/unstable/cli"

import { rollupState } from "#adapters/ci.ts"
import type { ConfigFile } from "#adapters/config.ts"
import { read as readConfig, settingsFor } from "#adapters/config.ts"
import { openPrs, prView, viewer } from "#adapters/gh.ts"
import { rebaseOnto } from "#adapters/git.ts"
import { named, prArgument, reading, refuse } from "#cli/pr.ts"
import { asUserError, userFacing } from "#cli/sweep.ts"
import { asUserError, userFacingAndGit } from "#cli/exit.ts"
import { forPr, prArgument, reading, refuse } from "#cli/pr.ts"
import { count } from "#cli/table.ts"
import { decide, recordConflict, stackOf } from "#domain/rebase.ts"
import { short } from "#domain/review.ts"
Expand Down Expand Up @@ -45,9 +43,7 @@ export const rebase = Command.make(
{ pr: prArgument },
Effect.fn("rebase")(
function* ({ pr }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const settings = settingsFor(file, repo)
const { number, repo, settings } = yield* forPr(pr)

const [view, open, me] = yield* reading(
`${repo}#${number}`,
Expand Down Expand Up @@ -102,6 +98,6 @@ export const rebase = Command.make(
`rebased ${count(done.behind, "commit")} of ${view.baseRefName} and pushed with a lease`
)
},
Effect.catchTag([...userFacing, "GitFailed"], asUserError)
Effect.catchTag(userFacingAndGit, asUserError)
)
).pipe(Command.withDescription("Rebase one branch onto its base and push it with a lease"))
12 changes: 4 additions & 8 deletions src/cli/rerun.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Console, Effect, Option } from "effect"
import { Console, Effect } from "effect"
import { Command } from "effect/unstable/cli"

import { failedRuns, rerunFailed, rollupState } from "#adapters/ci.ts"
import type { ConfigFile } from "#adapters/config.ts"
import { read as readConfig, settingsFor } from "#adapters/config.ts"
import { prView, viewer } from "#adapters/gh.ts"
import { named, prArgument, reading, refuse } from "#cli/pr.ts"
import { asUserError, userFacing } from "#cli/sweep.ts"
import { asUserError, userFacing } from "#cli/exit.ts"
import { forPr, prArgument, reading, refuse } from "#cli/pr.ts"
import { count } from "#cli/table.ts"
import { flakyReason } from "#domain/flaky.ts"
import { decide, recordRerun, refusedUnclassified, rerunFor } from "#domain/rerun.ts"
Expand Down Expand Up @@ -35,9 +33,7 @@ export const rerun = Command.make(
{ pr: prArgument },
Effect.fn("rerun")(
function* ({ pr }) {
const file: ConfigFile = Option.getOrElse(yield* readConfig, (): ConfigFile => ({}))
const { number, repo } = yield* named(pr, Object.keys(file.repos ?? {}).toSorted())
const settings = settingsFor(file, repo)
const { number, repo, settings } = yield* forPr(pr)

const [view, me] = yield* reading(`${repo}#${number}`, Effect.all([prView(repo, number), viewer]))

Expand Down
Loading
Loading
0