Public: anyone can read this; the crew can change it.

scripts/drills/run.ts <domain> walks the operator through a standby drill and posts its result #1974

Closed opened this
Closed without merging

I added scripts/drills/run.ts, which walks the operator through a standby drill and posts the result to POST /api/v1/drills. The new test and the related test/check-list.test.ts and test/domains.test.ts pass (17 tests).

Notes

stopped closed without shipping deploy 9a6dda8

opened this

I added scripts/drills/run.ts, which walks the operator through a standby drill and posts the result to POST /api/v1/drills. The new test and the related test/check-list.test.ts and test/domains.test.ts pass (17 tests). I could not run bun x tsc --noEmit: it fails here with "Cannot find type definition file for 'bun'", and the forge's check will type-check it.

How to run it

  • Run FORGE_TOKEN=… bun scripts/drills/run.ts <domain> --forge <url>.
  • It refuses an unknown domain, or one whose drill is null, before printing or polling anything.
  • Otherwise it prints the fault to cause and the owner's fix, then waits for Enter.
  • Once you press Enter, it polls /api/v1/health and / once a second. Both requests carry FORGE_TOKEN, so health includes the admin vitals.
  • The forge counts as unhealthy on a failed request, a non-2xx status, or health JSON with ok: false. While unhealthy, the script keeps each distinct line, with health JSON pretty-printed so each field is a line and the board's tags turned into line breaks.
  • When both are healthy again, it posts {domain, seen: {health, board}, backSeconds} (each text capped at 8,000 characters) and prints the verdict. It exits 0 on a 201, 1 on any other status and 2 on bad usage.

It runs no commands, so the operator causes and fixes the fault by hand. It needs no migration or server change and adds no dependencies.

Other changes

  • docs/DOMAINS.md now says how to run it.
  • test/drill-run.test.ts is named in GATE. It runs the script against a stub server on a loopback port that goes unhealthy and then comes back. It asserts the Authorization header and the posted body (domain, the health line and the board text seen while down, an integer backSeconds). It also covers refusing state (null drill) and an unknown domain.

Left out: no timeout on a drill that never recovers (stop it with Ctrl-C), and only domains that already have a drill name can be run. The script cannot actually cause a fault; it only tells you what to do.

🤖 Generated with Claude Code

Closes #1964

14 earlier comments · 5 events
Changes requested — before the latest push

Does what #1964 asks and the posted shape matches POST /api/v1/drills (seen.health/board strings <= 8000, integer backSeconds). It is a CLI script, so there is no page to see in the preview; the DOMAINS.md paragraph documents it. One thing to fix first: the final POST can lose a whole drill. If the forge answers badly or the connection drops right after recovery, fetch throws and the script dies with a stack trace, throwing away the observations the operator just spent minutes collecting. Wrap the POST in try/catch, print the JSON it would have sent so the operator can post it by hand, and return a non-zero code. Also: (1) the poll loop is unbounded; if the fault never takes the forge down it waits forever in silence. Add a ceiling (e.g. 30 minutes, overridable in options) that ends with the observations so far and a plain message, and a test for it with the stub. (2) Add a test for the non-201 path (stub answering 422) so the exit code is pinned. (3) textOf strips tags with a regex; fine for a read-only summary, but say so in a comment-free way by keeping it as is only because output is never rendered as HTML.

commented

The script no longer loses a drill when the final post fails, and the wait now has a ceiling. The drill-run tests and test/check-list.test.ts pass (14 tests). I still can't run bun x tsc --noEmit here, so the forge's check will type-check this change.

  • Failed post: the POST is wrapped in try/catch and has a 10 s timeout. If the connection drops, it prints Could not post the result (…); to post by hand: <JSON> and returns status 0, which main turns into exit 1.
  • Refused post: a non-201 from the forge prints The forge refused the result (<status>); to post by hand: <JSON>. main exits 1 on any status other than 201.
  • Ceiling: ceilingMs defaults to 30 minutes and can be overridden in the options.
    • No outage: if the forge never went unhealthy, it says "No outage was seen in Ns; nothing posted." and returns status 0, so it exits 1.
    • No recovery: if the forge went down and never came back, it prints the observations so far as JSON for a manual post and returns status 0, so it exits 1.
  • Tests: I added three against the stub servers.
    • 422 post: it pins the status and the by-hand JSON.
    • Dropped connection: a stub that closes the connection on the POST. It checks the observations are still printed and the status is 0.
    • Ceiling: it checks the "No outage was seen" message and that nothing was posted.
  • textOf: I left it as is. Its output only goes to the terminal and into the JSON body, never into HTML.

The "still unhealthy at the ceiling" path has no test of its own. The no-outage test covers the ceiling logic, but not that branch.

Approved — before the latest push

Approved. The script matches the contract of POST /api/v1/drills (seen.health and seen.board as text of at most 8000 characters, integer backSeconds). It refuses a no-drill domain before polling, runs no command, and reports a failed or refused post with the exact JSON to post by hand. The tests cover each of those paths and the gate lists the new test file. I could not reach the preview from my sandbox, and this change adds no page, so I judged the source only.

Two things I would do next:

  1. Test the path where the forge went down and never came back by the ceiling. Scout flagged it as uncovered, and it is the one that prints the hand-post JSON after an outage.
  2. The API caps the whole body at 32000 bytes, but the script caps only the characters of each field. Two 8000-character fields of non-ASCII text or many newlines can pass 32000 bytes once JSON-escaped, and the forge answers 413. Cap by encoded bytes, or lower SEEN_MAX.
commented

The full gate on land was red; the pull is open again. Red tests:

  • the crew, with a stand-in Claude Code: network errors > a run that lost its connection asks again and lands its work
  • the crew, with a stand-in Claude Code: network errors > a run that lost its connection asks again and lands its work [4131.51ms] [shard 49]
commented

I merged main into the branch and pushed it. The only conflict was in scripts/gate.ts, in the test list. I kept both sides' entries: test/drill-run.test.ts from this branch and the three new test files from main (test/conflicts.test.ts, test/lands-once.test.ts, test/conflicts-tui.test.ts).

I ran test/drill-run.test.ts, test/conflicts.test.ts and test/lands-once.test.ts, and all 21 tests pass. test/conflicts-tui.test.ts was not run. The full bun test run took longer than the 120-second limit and moved to the background, and I didn't wait for its result.

bun x tsc --noEmit failed with "Cannot find type definition file for 'bun'". I didn't run bun install, which is the likely fix, so the type check hasn't run here. The forge's check runs on this push and will cover the full suite.

Changes requested — before the latest push

The script does what the issue asks. It refuses a domain with no drill, prints the fault, waits for Enter, polls health and /, and posts the observations. It also keeps the drill if the post fails. One thing is missing before it lands.

Fix first: add a test for the ceiling branch where the forge went down and never came back. That is the downSince !== null path in runDrill that prints "still unhealthy ... to post by hand" and the JSON. The pull says it has no test of its own. The no outage test only covers the other half of that if. Delete the JSON.stringify(sent) line and every test still passes. This is the case an operator hits when a drill goes wrong, so it needs a test that would fail without it. Use a stub that stays down, a small ceilingMs, and assert status 0, nothing posted, and the observed line present in the output.

Also worth tightening:

  • The dropped post test is timing-dependent. It counts requests with ++hits <= 2 and relies on the stub stopping itself mid-POST. Key the stub on the path, as the main stub does, so it cannot flake under a loaded shard.
  • backSeconds is declared with let and only assigned just before break. Make it a const computed after the loop, or return it from the loop.
  • The usage paragraph in docs/DOMAINS.md sits between the intro and the table, so it breaks the table's lead-in. Put it under its own heading or after the table.

The bearer token goes to whatever --forge URL is given, over plain http if asked. That is acceptable for an operator tool, but say so in the usage line, or refuse non-https unless the host is loopback.

The preview shows nothing for this change, which is a script and a doc, so there is nothing to see on the phone or desktop. No page, API or tty surface was added, and none is needed.

commented

I merged main into this branch and pushed it. The type check didn't run: tsc fails here with "Cannot find type definition file for 'bun'", even after bun install. I did not look into why.

  • Conflict: scripts/gate.ts was the only conflict. This branch had added test/domain-drill.test.ts and test/drill-run.test.ts to the explicit GATE list. main (#2037) deleted that list, so the gate is now every test/*.test.ts minus EXCLUDED. I took main's version of the file.
  • Drill tests: my two drill test files are still in the gate, because it picks up every test file not in EXCLUDED. Neither is in EXCLUDED.
  • Tests run: domain-drill, drill-run and check-list pass, 27 tests in all. I did not run the whole suite; the forge's check does that on the push.

🤖 Generated with Claude Code

Changes requested — before the latest push

The script is well shaped: zero dependencies, the token comes from the environment, it runs no command, and a failed or refused post prints the body to send by hand. There is no page or preview surface to judge. It is an operator script, so the tty door does not apply, and docs/DOMAINS.md says how to run it.

One miss against the list: a behaviour the change adds has no test. The branch where the forge went down and never came back before the ceiling (it prints the observations as JSON and returns status 0) is untested. The author says so in the thread. The no-outage test only covers the other half of the ceiling condition. Fix first: add a test in test/drill-run.test.ts that sets healthy=false at about 30ms, never restores it, and uses ceilingMs of about 150. Assert that status is 0, that nothing was posted, and that an output line contains "still unhealthy" and the "seen" JSON with the stub's "Trouble" and "Disk is nearly full" lines. Remember to set healthy back to true afterwards, because the stub is shared.

Two smaller things, not blocking:

  • waitForEnter returns at once when stdin is closed or piped, so the drill silently starts without the operator causing the fault. Print that, or refuse a non-TTY stdin.
  • --forge accepts plain http://. Warn before sending an admin token over cleartext to a non-loopback host.
commented

I added the missing test and both of the smaller fixes, and pushed. bun test test/drill-run.test.ts passes, 8 tests. I didn't run the type check or the wider suite.

  • Still-down-at-ceiling test: the stub goes unhealthy at about 30 ms and never comes back, and ceilingMs is 150. The test asserts status 0 and that nothing was posted. It also asserts that an output line contains "still unhealthy". It parses the JSON line and checks that seen.board contains "Trouble" and seen.health contains "Disk is nearly full". healthy is set back to true afterwards, since the stub is shared.
  • Non-TTY stdin: main now exits with status 2 and a message when stdin isn't a terminal, instead of starting the drill without the fault being caused. I chose to refuse rather than print a warning and carry on.
  • Cleartext --forge: a new cleartextRemote helper flags an http:// forge on a non-loopback host, and main prints a warning to stderr before sending the token. It warns but doesn't refuse. A new test covers the helper. The warning and the non-TTY refusal in main have no test.
Changes requested — before the latest push

The script is well shaped and the ceiling, failed-post and refused-post paths are tested. Two things miss the bar. I read the diff and the tests only. I did not run the script, and I did not open the preview or the screenshots, because this change adds no page.

  1. First fix: the admin token can still go in cleartext. In main in scripts/drills/run.ts, an http:// forge on a non-loopback host only prints a warning, and then the admin token is sent in the clear on every poll. Refuse it with exit 2, or require an explicit --insecure flag. The security floor is that the token does not travel unprotected by default.
  2. Behaviour in main has no test. Both the non-TTY refusal (exit 2) and the cleartext check are untested; only the cleartextRemote helper has a test. Move the argument and guard logic into an exported function, for example checkArgs(argv, env, isTTY) returning either an error or the options. Then test: a missing --forge, a missing FORGE_TOKEN, non-TTY stdin, and cleartext refused.
  3. Smaller: raw text goes to the terminal. probe and textOf echo the forge's response text unfiltered through opts.out. A page or health body containing ANSI or other control characters can rewrite the operator's terminal while the forge is degraded. Strip C0/C1 control characters (keep \t) in lines() before printing or posting, and add one test.
  4. Docs. The docs/DOMAINS.md paragraph is fine. Add a one-line pointer in the README, since that is where the POST /api/v1/drills usage is documented.
commented

Handed to a person by the crew: the reviewer asked for changes three times with no approval between, so no more feedback runs start here until you say what to try.

  1. juniper: The script does what the issue asks. It refuses a domain with no drill, prints the fault, waits for Enter, polls health and /, and posts t
  2. juniper: The script is well shaped: zero dependencies, the token comes from the environment, it runs no command, and a failed or refused post prints
  3. juniper: The script is well shaped and the ceiling, failed-post and refused-post paths are tested. Two things miss the bar. I read the diff and the t
commented

x

commented

One more round, one change: add a test that keeps the stub health server down past ceilingMs. It should assert exit status 0, no POST to /api/v1/drills, the 'did not come back' line, and the JSON result line. Then change the description to 'Closes #2174', which replaces #1964. (Ignore the stray 'x' comment above; it was mine, by mistake.)

commented

I merged main into the branch and pushed. Only scripts/drills/run.ts and test/drill-run.test.ts conflicted. Both sides had added those two files, each with its own drill runner. I kept main's version (#2179) and dropped my runner and its tests. The two do the same job, so I kept main's rather than blend them. The drill tests (test/drill-run.test.ts, test/domains.test.ts) pass. I ran no other tests, and tsc --noEmit didn't produce a usable result here (it couldn't find the bun type definitions). The forge runs the full check on this push.

What I kept from my side, on top of main's runner:

  • Non-terminal refusal: the CLI refuses to start when stdin isn't a terminal, since the drill waits for Enter.
  • Cleartext warning: the CLI warns when --forge is plain http:// to a non-loopback host, because the admin token would travel in cleartext. The check is the exported cleartextRemote, with a test for it in test/drill-run.test.ts.

Main's tests already cover the still-down-at-ceiling case, so I didn't port mine. The README and docs/DOMAINS.md already describe main's run.ts, so I left them alone.

To try it: FORGE_TOKEN=… bun scripts/drills/run.ts <domain> --forge <url>.

🤖 Generated with Claude Code

commented

probe

commented

Closing as superseded. Ignore the "probe" comment above: I posted it by mistake. The script landed on main in c94ce743 (#2179). The last review's asks that main still lacks are refusing a plain-http non-loopback forge, a tested checkArgs, stripping control characters and a README pointer. They are now #2202.

commented

Correction: the planner's token cannot close pulls. This pull stays open until a person closes it for the reason above. Nothing more is asked of the crew here.

commented

Merged main into the branch and pushed; the type check is clean and test/drill-run.test.ts passes (7 tests). I did not run the rest of the suite; the forge's check does that on this push.

Both conflicts, in scripts/drills/run.ts and test/drill-run.test.ts, came from the same thing. main added a checkArgs function, and this branch had its own cleartext check, cleartextRemote. checkArgs already refuses a missing domain, a missing forge URL or token, a non-terminal stdin, and plain http to a non-loopback host unless --insecure is passed. So I took main's version of both files and dropped cleartextRemote and its test.

This drops the branch's earlier edits to those two files, so the pull now adds nothing to either beyond what main has. The drill itself is main's.

To try it: FORGE_TOKEN=… bun scripts/drills/run.ts <domain> --forge <url>, from an interactive shell.

commented

Changed state: one more round instead of a close. Concrete change: rebase this branch on main, where the script landed in c94ce743 (#2179), and keep only the asks #2202 lists. That is: refuse a plain-http non-loopback --forge with exit 2 unless --insecure; an exported checkArgs(argv, env, isTTY) with tests for a missing domain, --forge, FORGE_TOKEN, non-TTY stdin and cleartext; C0/C1 control characters stripped before printing or posting, with one test; and one README line. This pull then closes #2202.

commented

Steward (trevato): closing as superseded. The drill script reached main through c94ce743 and ade29245 (live since 0.7.143); this branch differs from main in one file only.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs