Skip to content

refactor(herdr-core): split leaf modules out of the herdr and herdr-core clients - #4394

Merged
devswha merged 3 commits into
mainfrom
tui/2a-leaf-splits
Nov 21, 2026
Merged

devswha merged 3 commits into
mainfrom
tui/2a-leaf-splits

Conversation

@devswha

@devswha devswha commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Change

Leaf modules split out of the herdr and herdr-core clients, so code that needs one small piece (a socket path, the line reader, the test-session rules, a worktree record) can import it without the whole client. No behaviour changes.

  • server/herdr-core/hc-connect.ts: starting hc connect and the variables kept out of a started core (spawnHcConnect, SERVER_ENV), out of connection.ts.
  • server/herdr-core/core-socket.ts: where the core of an environment listens (coreSocket, plainSession) and the user-core guard (userCore), out of discovery.ts and connection.ts.
  • server/herdr/line-reader.ts (makeLineReader), server/herdr/test-session.ts (liveSessionsRefused, sameSocket) and server/herdr/worktree-info.ts (the WorktreeInfo type), out of server/herdr/client.ts.
  • Every importer now names the new file.

Move-only. git diff --color-moved=zebra --color-moved-ws=allow-indentation-change origin/main...HEAD: 386 of the 497 changed lines are moved code. The other 111 are import lines, export on userCore (now imported across files), the header comments and imports of the five new files, three one-line comments inside moved blocks, and makeLineReader's doc comment, which now also says what happens to an oversized line and which test imports it.

The 111 changed lines that git does not mark as moved
scripts/relay-offline-reasons-regression.ts
-import { liveSessionsRefused } from "../server/herdr/client.ts";
+import { liveSessionsRefused } from "../server/herdr/test-session.ts";

server/conversation.ts
-import { coreSocket } from "./herdr-core/discovery.ts";
+import { coreSocket } from "./herdr-core/core-socket.ts";

server/herdr-core/adapter.ts
-import { LINE_BYTES, makeLineReader } from "../herdr/client.ts";
+import { LINE_BYTES } from "../herdr/client.ts";
+import { makeLineReader } from "../herdr/line-reader.ts";
-import type { WorktreeInfo } from "../herdr/client.ts";
+import type { WorktreeInfo } from "../herdr/worktree-info.ts";

server/herdr-core/connection.test.ts
-import { spawnHcConnect } from "./connection.ts";
+import { spawnHcConnect } from "./hc-connect.ts";

server/herdr-core/connection.ts
-import { liveSessionsRefused, sameSocket } from "../herdr/client.ts";
-import { coreSocket } from "./discovery.ts";
-function userCore(env: Record<string, string | undefined>): boolean {
-

server/herdr-core/core-socket.ts
+/**
+ * Where the core of this environment listens, as hc works it out, and whether its socket file is
+ * there. It reads hc's config.toml and the home of the user and nothing more, so a client that
+ * only talks to a core can import it without the rest of the server; it never connects.
+ */
+
+import { statSync } from "node:fs";
+import { join } from "node:path";
+import { sameSocket } from "../herdr/test-session.ts";
+import { userHome } from "../user-home.ts";
+
+type Env = Record<string, string | undefined>;
+
+export function userCore(env: Record<string, string | undefined>): boolean {

server/herdr-core/discovery.test.ts
-import { aclReader, coreSocket, discoverHc, findHc, harmlessGroups, herdrCoreFacts, macAclGrantsWrite, ownedByUser, plainSession, privateGroup, PROBE_TIMEOUT_MS, trustRefusal } from "./discovery.ts";
+import { coreSocket, plainSession } from "./core-socket.ts";
+import { aclReader, discoverHc, findHc, harmlessGroups, herdrCoreFacts, macAclGrantsWrite, ownedByUser, privateGroup, PROBE_TIMEOUT_MS, trustRefusal } from "./discovery.ts";

server/herdr-core/discovery.ts
+import { coreSocket, plainSession } from "./core-socket.ts";

server/herdr-core/hc-connect.ts
+/**
+ * Starting `hc connect` as a child process, the one place this side runs hc: the transport a
+ * `HcConnection` (connection.ts) reads and writes. It refuses a test process that would reach the
+ * user's own core, and keeps this server's own variables out of the environment of a core that
+ * `hc connect` starts. connection.ts itself stays free of the server's other code.
+ */
+
+import { liveSessionsRefused } from "../herdr/test-session.ts";
+import { HcError, type HcTransport } from "./connection.ts";
+import { userCore } from "./core-socket.ts";
+  // runtime.ts hands out this path that cannot exist while a moved hc is found again
+      // a child that just exited takes no more: its end is reported through `exited`
+        /* the line ends on `exited` */

server/herdr-core/pin-label.ts
-import { LINE_BYTES, makeLineReader } from "../herdr/client.ts";
+import { LINE_BYTES } from "../herdr/client.ts";
+import { makeLineReader } from "../herdr/line-reader.ts";

server/herdr-core/runtime-choice.ts
-import { coreSocket, discoverHc, type FoundHc } from "./discovery.ts";
+import { coreSocket } from "./core-socket.ts";
+import { discoverHc, type FoundHc } from "./discovery.ts";

server/herdr-core/runtime.ts
-import { HcConnection, spawnHcConnect } from "./connection.ts";
+import { HcConnection } from "./connection.ts";
+import { spawnHcConnect } from "./hc-connect.ts";
-import { BUNDLED_PROBE_TIMEOUT_MS, coreSocket, findHc, type FoundHc } from "./discovery.ts";
+import { coreSocket } from "./core-socket.ts";
+import { BUNDLED_PROBE_TIMEOUT_MS, findHc, type FoundHc } from "./discovery.ts";

server/herdr-core/terminal-env.ts
-import { SERVER_ENV, type HcConnection } from "./connection.ts";
+import type { HcConnection } from "./connection.ts";
+import { SERVER_ENV } from "./hc-connect.ts";

server/herdr-core/worktrees.ts
-import type { WorktreeInfo } from "../herdr/client.ts";
+import type { WorktreeInfo } from "../herdr/worktree-info.ts";

server/herdr/client.ts
+import { join } from "node:path";
+import { makeLineReader } from "./line-reader.ts";
+import type { WorktreeInfo } from "./worktree-info.ts";
+import { liveSessionsRefused, sameSocket } from "./test-session.ts";
-
-/**
- * once its line is complete, which keeps a UTF-8 character split between reads whole. A chunk is split, and its partial tail kept,
- * is still delivered, and the first throw is rethrown after the last.
-
-

server/herdr/line-reader.ts
+/**
+ * The reader that splits herdr's and hc's byte streams into newline-terminated lines of bounded
+ * size. It imports nothing, so any client of those streams can use it.
+ */
+
+/**
+ * Splits incoming bytes into newline-terminated lines of at most `limit` bytes each.
+ * once its line is complete, which keeps a UTF-8 character split between reads whole. A line
+ * that passes `limit` before its newline is reported once through `onOversize`, its bytes are
+ * dropped, and the reader ignores everything after it. A chunk is split, and its partial tail kept,
+ * is still delivered, and the first throw is rethrown after the last. Exported for
+ * server/herdr/seam.test.ts.

server/herdr/live-guard.test.ts
-import { spawnHcConnect } from "../herdr-core/connection.ts";
-import { coreSocket } from "../herdr-core/discovery.ts";
+import { coreSocket } from "../herdr-core/core-socket.ts";
+import { spawnHcConnect } from "../herdr-core/hc-connect.ts";

server/herdr/seam.test.ts
-  agentGet, focusFromMove, HerdrError, herdrRpc, LARGE_REPLY_BYTES, LINE_BYTES, makeLineReader, paneFocus, paneMoveBeside, paneProcessInfo, paneRead, paneSwap, paneSwapWith, sessionSnapshot,
+  agentGet, focusFromMove, HerdrError, herdrRpc, LARGE_REPLY_BYTES, LINE_BYTES, paneFocus, paneMoveBeside, paneProcessInfo, paneRead, paneSwap, paneSwapWith, sessionSnapshot,
+import { makeLineReader } from "./line-reader.ts";

server/herdr/test-session.ts
+/**
+ * The test-session rules: whether this process asked for an isolated herdr (HERDR_TEST_SESSION),
+ * and one spelling of a socket path to compare against the user's own. Both the herdr client and
+ * the herdr-core connection use them, so neither has to import the other.
+ */
+
+

server/herdr/worktree-info.ts
+/**
+ * herdr's worktree record, in a file that imports nothing, so code that only names the type
+ * (server/herdr-core/worktrees.ts) does not import the whole client.
+ */
+
+/** herdr's view of one git checkout: `worktree.list` entries, and what create/open hand back. */

server/runtime-move-service.contract.test.ts
-import { HcConnection, spawnHcConnect } from "./herdr-core/connection.ts";
+import { HcConnection } from "./herdr-core/connection.ts";
+import { spawnHcConnect } from "./herdr-core/hc-connect.ts";

server/runtime-move-service.ts
-import { HcConnection, spawnHcConnect } from "./herdr-core/connection.ts";
+import { HcConnection } from "./herdr-core/connection.ts";
+import { spawnHcConnect } from "./herdr-core/hc-connect.ts";

Validation

  • bun run check fast at 69795e2f2, on the heavy host (hosthatch), as it runs there: exit 0. Workflow syntax, generated types, generated themes, typecheck and build ok; unit tests 21502 pass, 0 fail, 45 skip in 1097 files.
  • bun run typecheck and the 13 unit files the change touches, one process each: 593 pass, 0 fail.
  • The integration-lane files it changes, in the CI runner image with herdr 0.9.3's isolated session: server/herdr/live-guard.test.ts 3 pass, server/herdr/seam.test.ts 34 pass; server/runtime-move-service.contract.test.ts skips its 7 cases there (they need hc as well as herdr).
  • The same three commits in the dry run of the whole series on main e20d6967d: typecheck and unit pass at this slice and at every later one.
  • To wait for: GitHub's herdr-core live job (this moves spawnHcConnect, which the live suite uses).
  • Skipped: bun run check full; the browser lanes cover nothing this moves.
  • No changelog fragment: users notice nothing.

Slice: 2a

…test-session leaves

Move-only: no symbol renamed, no signature or statement changed. spawnHcConnect, its
userCore guard and SERVER_ENV move from herdr-core/connection.ts to hc-connect.ts;
coreSocket, its types and helpers and plainSession move from herdr-core/discovery.ts to
core-socket.ts; makeLineReader moves from herdr/client.ts to herdr/line-reader.ts; the
test-session helpers liveSessionsRefused and sameSocket move from herdr/client.ts to
herdr/test-session.ts. The old files re-export nothing: every importer, tests included
(import lines only), points at the new path. No moved function opens a socket, so the
unix-connect allow-list in server/conventions.test.ts is unchanged. connection.ts now
imports neither herdr/client.ts nor herdr-core/discovery.ts.

Slice: 2a
Move-only: userCore leaves hc-connect.ts for core-socket.ts, a leaf, so the socket transport can share the rule that keeps a test process off the user's own core. hc-connect.ts imports it back.

Slice: 2a
…f file

server/herdr-core/worktrees.ts named only this type from server/herdr/client.ts, so every file that imports worktrees.ts (snapshot.ts, and through it the core model the terminal screen will use) reached the whole client. The interface moves unchanged to server/herdr/worktree-info.ts, which imports nothing; the client, worktrees.ts and the adapter import it from there. Move-only.

Slice: 2a
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Connection Handling
    • Core connections now use consistent socket discovery across configured, runtime, and default locations.
    • Live-session safeguards and clearer handling of unavailable or moved cores are maintained when launching connections.
  • Reliability
    • Session identifiers and socket paths are validated and normalized, improving consistency across platforms.
    • Oversized input lines are detected, and complete lines are processed reliably even when data arrives in fragments.

Herdr socket discovery, connection launching, and shared client helpers move into dedicated modules. Callers and tests update their imports to use the extracted modules.

Changes: Herdr module separation

Layer / File(s) Summary
Extract shared Herdr helpers
server/herdr/test-session.ts, server/herdr/line-reader.ts, server/herdr/worktree-info.ts, server/herdr/client.ts, server/herdr-core/adapter.ts, server/herdr-core/pin-label.ts, server/herdr-core/worktrees.ts, server/herdr/seam.test.ts, scripts/relay-offline-reasons-regression.ts
Test-session checks, socket comparison, line framing, and the WorktreeInfo interface move to dedicated modules. Existing callers and tests import them from those modules.
Separate core socket discovery
server/herdr-core/core-socket.ts, server/herdr-core/discovery.ts, server/herdr-core/discovery.test.ts, server/herdr-core/runtime-choice.ts, server/herdr-core/runtime.ts, server/conversation.ts
Socket selection, presence checks, and session validation move from discovery.ts to core-socket.ts. Discovery and its callers and tests import the extracted functions.
Separate hc connect launching
server/herdr-core/hc-connect.ts, server/herdr-core/connection.ts, server/herdr-core/runtime.ts, server/herdr-core/terminal-env.ts, server/runtime-move-service.ts, server/herdr-core/connection.test.ts, server/herdr/live-guard.test.ts, server/runtime-move-service.contract.test.ts
spawnHcConnect and SERVER_ENV move from connection.ts to hc-connect.ts. Runtime callers and tests update their imports.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Suggested reviewers: nahwan-kim

Merge Risk: Minimal · up to 69795

This is a move-only refactor that splits small helpers into standalone modules and updates imports. No behavior change or concrete regression is identified, and the risk of merging is minimal. The pending live job is the only open verification item.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: splitting leaf modules out of the herdr and herdr-core clients. The raw summary confirms the moved helpers (spawnHcConnect, coreSocket, makeLineReader, liveSessionsRef…
Description check Passed The description follows the template. It has a Change section that explains the split, the move-only nature, and the affected files. It has a Validation section that lists the checks run (bun run chec…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: splitting leaf modules out of the herdr and herdr-core clients. The raw summary confirms the moved helpers (spawnHcConnect, coreSocket, makeLineReader, liveSessionsRefused, WorktreeInfo) into new modules. The title is specific and accurate.

Full details: Description check

Explanation

The description follows the template. It has a Change section that explains the split, the move-only nature, and the affected files. It has a Validation section that lists the checks run (bun run check fast, typecheck, targeted test files), the results, and the skipped items (check full, pending live job). The template's changelog note is addressed (no fragment, users notice nothing). The description is thorough and specific.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 059ff50 and 69795e2.

Files selected for processing: 22

  • scripts/relay-offline-reasons-regression.ts
  • server/conversation.ts
  • server/herdr-core/adapter.ts
  • server/herdr-core/connection.test.ts
  • server/herdr-core/connection.ts
  • server/herdr-core/core-socket.ts
  • server/herdr-core/discovery.test.ts
  • server/herdr-core/discovery.ts
  • server/herdr-core/hc-connect.ts
  • server/herdr-core/pin-label.ts
  • server/herdr-core/runtime-choice.ts
  • server/herdr-core/runtime.ts
  • server/herdr-core/terminal-env.ts
  • server/herdr-core/worktrees.ts
  • server/herdr/client.ts
  • server/herdr/line-reader.ts
  • server/herdr/live-guard.test.ts
  • server/herdr/seam.test.ts
  • server/herdr/test-session.ts
  • server/herdr/worktree-info.ts
  • server/runtime-move-service.contract.test.ts
  • server/runtime-move-service.ts
Files with no reviewable changes: 1

  • server/herdr-core/connection.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.

@devswha
devswha merged commit 31853b5 into main Nov 21, 2026
24 checks passed
@devswha

devswha commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Merged through pr-merge at head 69795e2f2. Seen before the merge was asked for: required checks green, no unresolved thread, no conflict, no hold, main not red, no release in progress. CodeRabbit reviewed this head.

reviews: dori review CLEAR at 69795e2; omo gate reviewer (GPT-6 Astra) CLEAR at 69795e2; CodeRabbit reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant