8000 feat(heartbeat): every command that works before it prints says so by dominikwozniak · Pull Request #90 · 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 .changeset/great-pandas-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"dw-mc": minor
---

Every command that works before it can print now says so, on one line it rewrites and wipes when the
work is done. `dw-mc status`, `dw-mc` and `dw-mc sweep` count the repositories they search and then the
pull requests they read; a command that reads a pull request's guards live says which one it is reading;
a clone says whether it is arriving for the first time or being fetched, and a worktree says it is being
cut; `dw-mc cleanup` says it is measuring the disk before it says what that weighs.

Piped or redirected, every command prints exactly what it printed before. The line is drawn only where
there is a terminal with a width to draw in.
4 changes: 4 additions & 0 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ _Avoid_: icon, badge, glyph
How a command that reports rather than tabulates writes: a heading, an indented body, and a blank line before the next one. What each command says is its own; the shape is not.
_Avoid_: section, panel, group

**Heartbeat**:
The one line a command rewrites while it works: a spinner, how far it has got, how long it has taken. Gone the moment the work is, so what stays on the screen is the report. What it counts is the command's; the clock and the line are not.
_Avoid_: spinner, loader, progress bar

### Housekeeping

**Clone**:
Expand Down
4 changes: 3 additions & 1 deletion docs/adr/0007-what-the-screen-says.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Colour carries state, dim carries context, and prose is never coloured

Mission control writes lines, not screens. There are no frames, no boxes and no full-screen redrawing beyond the spinner a review already turns: a line survives a pipe, a paste into a pull request and a terminal I resized, and a frame does not.
Mission control writes lines, not screens. There are no frames, no boxes and no full-screen redrawing beyond the one line a heartbeat rewrites: a line survives a pipe, a paste into a pull request and a terminal I resized, and a frame does not.

**Work that takes seconds says so.** Every command that reaches GitHub, a clone or the disk before it can print turns a heartbeat while it does, because a terminal that shows nothing for five seconds is one I stop trusting. It is one line, rewritten in place and gone the moment the work is, so what the screen keeps is the report and never the trail of what it took to get there. What the line counts belongs to the command — a review counts tools, a sweep counts pull requests, a command reading two guards counts nothing — and the clock and the rewriting belong to the heartbeat. Where there is no screen to measure, nothing is drawn: a pipe reads exactly what it read before there was a heartbeat at all.

What a line is allowed to say in more than words is fixed here, because a rule invented per command is four looks.

Expand Down
40 changes: 38 additions & 2 deletions src/adapters/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest"
import { ConfigProvider, Effect, Layer, Path } from "effect"

import { rebaseInPlace, rebaseOnto, standingWorktree, withWorktree } from "#adapters/git.ts"
import { layerScripted } from "#adapters/picker.ts"
import { fakeHandle, layerFake } from "#adapters/spawner.ts"

const state = "/home/dw/.local/state/dw-mc"
Expand Down Expand Up @@ -94,8 +95,14 @@ const git = (options: {
return Effect.succeed(fakeHandle({}))
})

const machine = (spawner: Layer.Layer<never> | ReturnType<typeof git>) =>
Layer.mergeAll(ConfigProvider.layer(ConfigProvider.fromEnvRecord({ HOME: "/home/dw" })), Path.layer, spawner)
const machine = (spawner: Layer.Layer<never> | ReturnType<typeof git>, drawn?: Array<string>) =>
Layer.mergeAll(
ConfigProvider.layer(ConfigProvider.fromEnvRecord({ HOME: "/home/dw" })),
Path.layer,
// Cutting a worktree says so on the screen, so there is a screen to say it on.
layerScripted([], drawn),
spawner
)

describe("the tool's own clone and its throwaway worktree", () => {
it.effect("cuts the worktree at the pull request's head and takes it down after", () => {
Expand All @@ -117,6 +124,35 @@ describe("the tool's own clone and its throwaway worktree", () => {
}).pipe(Effect.provide(machine(git({ spawned }))))
})

it.effect("says a first clone apart from every fetch after it", () => {
const spawned: Array<string> = []
const fresh: Array<string> = []
const warm: Array<string> = []

return Effect.gen(function* () {
yield* Effect.provide(
withWorktree("dominikwozniak/dw-mc", 28, () => Effect.void),
machine(git({ spawned }), fresh)
)
yield* Effect.provide(
withWorktree("dominikwozniak/dw-mc", 28, () => Effect.void),
machine(git({ spawned: [], cloned: true }), warm)
)

// A first clone of a large repository is minutes, and the line is what
// explains why: a `cloning` that sits there has not hung.
assert.include(fresh.join("\n"), "cloning dominikwozniak/dw-mc")
assert.include(fresh.join("\n"), "fetching dominikwozniak/dw-mc")
assert.notInclude(warm.join("\n"), "cloning")
assert.include(warm.join("\n"), "fetching dominikwozniak/dw-mc")

// Cutting the checkout is its own wait, and its own words.
assert.include(warm.join("\n"), "cutting a worktree of dominikwozniak/dw-mc")
// Neither leaves anything on the screen behind it.
assert.match(warm.at(-1) ?? "", /^\r +\r$/)
})
})

it.effect("brings the base branch up to date with the pull request's head", () => {
const spawned: Array<string> = []

Expand Down
58 changes: 41 additions & 17 deletions src/adapters/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Effect, Path, Result, Schema } from "effect"

import type { Reads } from "#adapters/heartbeat.ts"
import { beating } from "#adapters/heartbeat.ts"
import { capture } from "#adapters/spawner.ts"
import type { Cut, Session } from "#adapters/store.ts"
import { clonesIn, stateDirectory, under } from "#adapters/store.ts"
Expand Down Expand Up @@ -57,28 +59,46 @@ export class WorktreeHeld extends Schema.TaggedError<WorktreeHeld>()("WorktreeHe
* The head comes from the pull request's ref rather than from what a sweep last
* saw, so what is cut is the commit the run really reads.
*/
/**
* How the heartbeat of a cut reads.
*
* The stages are named apart because a first clone and a hundredth fetch take
* wildly different times, and the line is what explains the difference: a
* `cloning` that sits there for two minutes is a large repository arriving
* once, not a tool that has hung.
*/
const cutting =
(what: string, repo: string): Reads =>
(since) =>
`${what} ${repo} · ${since}`

const whereToCut = Effect.fn("git.whereToCut")(function* (repo: string, number: number, cut: Cut) {
const path = yield* Path.Path
const state = yield* stateDirectory
const clone = path.join(state, clonesIn, `${repo}.git`)

const bare = yield* Effect.orElseSucceed(git(["-C", clone, "rev-parse", "--is-bare-repository"]), () => "")
if (bare !== "true") {
yield* git(["clone", "--bare", "--filter=blob:none", `https://github.com/${repo}.git`, clone])
}

const pullRef = `refs/dw-mc/pr/${number}`
yield* git([
"-C",
clone,
"fetch",
"--no-tags",
"--force",
"origin",
`+refs/pull/${number}/head:${pullRef}`,
"+refs/heads/*:refs/heads/*"
])
const head = yield* git(["-C", clone, "rev-parse", pullRef])

const head = yield* beating(cutting(bare === "true" ? "fetching" : "cloning", repo), (says) =>
Effect.gen(function* () {
if (bare !== "true") {
yield* git(["clone", "--bare", "--filter=blob:none", `https://github.com/${repo}.git`, clone])
yield* says(cutting("fetching", repo))
}
yield* git([
"-C",
clone,
"fetch",
"--no-tags",
"--force",
"origin",
`+refs/pull/${number}/head:${pullRef}`,
"+refs/heads/*:refs/heads/*"
])
return yield* git(["-C", clone, "rev-parse", pullRef])
})
)
return { clone, head, directory: path.join(state, cut, repo, String(number)) }
})

Expand All @@ -105,7 +125,9 @@ export const withWorktree = Effect.fn("git.withWorktree")(function* <A, E, R>(
const remove = Effect.ignore(git(["-C", clone, "worktree", "remove", "--force", directory]))

return yield* Effect.acquireUseRelease(
Effect.flatMap(remove, () => git(["-C", clone, "worktree", "add", "--detach", directory, head])),
beating(cutting("cutting a worktree of", repo), () =>
Effect.flatMap(remove, () => git(["-C", clone, "worktree", "add", "--detach", directory, head]))
),
() => use({ directory, head }),
() => remove
)
Expand Down Expand Up @@ -215,7 +237,9 @@ export const standingWorktree = Effect.fn("git.standingWorktree")(function* (
if (session === "rebase") {
yield* reuseResolutions(clone)
}
yield* git(["-C", clone, "worktree", "add", "-B", branch, directory, head])
yield* beating(cutting("cutting a worktree of", repo), () =>
git(["-C", clone, "worktree", "add", "-B", branch, directory, head])
)

// What makes `git push` inside the session land on the pull request: the
// branch tracks the pull request's, and a push follows the upstream's name
Expand Down
120 changes: 120 additions & 0 deletions src/adapters/heartbeat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { assert, describe, it } from "@effect/vitest"
import { Console, Duration, Effect } from "effect"
import { TestClock } from "effect/testing"

import type { Reads, Says } from "#adapters/heartbeat.ts"
import { beating } from "#adapters/heartbeat.ts"
import { layerScripted } from "#adapters/picker.ts"

/** What a run has reached for so far, as the command that counts would hold it. */
interface Doing {
readonly tools: number
readonly subagents: number
}

/** How the line reads, as the command that counts would word it. */
const reads =
(doing: Doing): Reads =>
(since) =>
`reviewing · ${doing.tools} tools · ${doing.subagents} subagents · ${since}`

/** Collects what was printed a line at a time, beside what was drawn in place. */
const recording = (printed: Array<string>) => {
const console_: Console.Console = Object.assign(Object.create(console), {
log: (...args: ReadonlyArray<unknown>) => printed.push(args.join(" ")),
error: () => {}
})
return Effect.provideService(Console.Console, console_)
}

/** A run that reaches for three tools, one of them a subagent, then finishes. */
const reaching = (says: Says) =>
Effect.gen(function* () {
let doing: Doing = { tools: 0, subagents: 0 }
const string) => {
doing = { tools: doing.tools + 1, subagents: doing.subagents + (tool === "Agent" ? 1 : 0) }
return says(reads(doing), ` · ${tool}`)
}
yield* onTool("Bash")
yield* onTool("Agent")
yield* onTool("Bash")
yield* TestClock.adjust(Duration.seconds(75))
return "reviewed"
})

/** Work that counts nothing at all, which is every command that reads two guards. */
const reading = () => Effect.as(TestClock.adjust(Duration.seconds(3)), "read")

const from = reads({ tools: 0, subagents: 0 })

describe("beating", () => {
it.effect("keeps one line and says how far the work has got", () => {
const drawn: Array<string> = []
const printed: Array<string> = []

return Effect.gen(function* () {
assert.strictEqual(yield* beating(from, reaching), "reviewed")

const screen = drawn.join("\n")
assert.include(screen, "reviewing · 3 tools · 1 subagents · 1m15s")
assert.deepStrictEqual(printed, [])
// What the work leaves on the screen is the report, not its own heartbeat.
assert.match(drawn.at(-1) ?? "", /^\r +\r$/)
}).pipe(Effect.provide(layerScripted([], drawn)), recording(printed))
})

it.effect("beats for work that counts nothing, on the clock alone", () => {
const drawn: Array<string> = []
const printed: Array<string> = []

return Effect.gen(function* () {
assert.strictEqual(yield* beating(() => "reading GitHub", reading), "read")

assert.include(drawn.join("\n"), "reading GitHub")
assert.deepStrictEqual(printed, [])
assert.match(drawn.at(-1) ?? "", /^\r +\r$/)
}).pipe(Effect.provide(layerScripted([], drawn)), recording(printed))
})

it.effect("writes the asides out a line at a time where there is no screen to draw on", () => {
const drawn: Array<string> = []
const printed: Array<string> = []

return Effect.gen(function* () {
assert.strictEqual(yield* beating(from, reaching), "reviewed")

assert.deepStrictEqual(printed, [" · Bash", " · Agent", " · Bash"])
assert.deepStrictEqual(drawn, [])
}).pipe(Effect.provide(layerScripted([], drawn, 0)), recording(printed))
})

it.effect("says nothing at all where there is no screen and the work has no aside", () => {
const drawn: Array<string> = []
const printed: Array<string> = []

return Effect.gen(function* () {
// A sweep gives no aside, so a piped `dw-mc status` prints what it always did.
const silent = (says: Says) =>
Effect.as(
says(() => "sweeping"),
"swept"
)
assert.strictEqual(yield* beating(from, silent), "swept")

assert.deepStrictEqual(printed, [])
assert.deepStrictEqual(drawn, [])
}).pipe(Effect.provide(layerScripted([], drawn, 0)), recording(printed))
})

it.effect("takes the heartbeat down when the work fails, and says nothing of its own", () => {
const drawn: Array<string> = []
const printed: Array<string> = []

return Effect.gen(function* () {
const error = yield* Effect.flip(beating(from, () => Effect.fail("the run gave up" as const)))

assert.strictEqual(error, "the run gave up")
assert.match(drawn.at(-1) ?? "", /^\r +\r$/)
}).pipe(Effect.provide(layerScripted([], drawn)), recording(printed))
})
})
Loading
Loading
0