Skip to content

feat(config): cwd-based workspaceRules routing#41

Open
zachlagden wants to merge 3 commits into
plastic-labs:mainfrom
zachlagden:feat/cwd-workspace-routing
Open

feat(config): cwd-based workspaceRules routing#41
zachlagden wants to merge 3 commits into
plastic-labs:mainfrom
zachlagden:feat/cwd-workspace-routing

Conversation

@zachlagden

@zachlagden zachlagden commented May 18, 2026

Copy link
Copy Markdown

Summary

Adds a workspaceRules array to the config file. When set, each rule maps a cwdPrefix (with ~ expanded to $HOME) to a workspace name. Rules are checked in order; the first matching prefix wins. When no rule matches — or when workspaceRules is unset — resolution falls through unchanged to the existing globalOverride / host-block / default chain, so behaviour for current users is byte-identical.

{
  "hosts": { "claude_code": { "workspace": "default", "aiPeer": "claude" } },
  "workspaceRules": [
    { "cwdPrefix": "~/code/work",     "workspace": "work" },
    { "cwdPrefix": "~/code/personal", "workspace": "personal" },
    { "cwdPrefix": "~/code",          "workspace": "experiments" }
  ]
}

Motivation

I run one Claude Code install across genuinely separate Honcho workspaces (work vs personal vs experiments) and want memory partitioned by domain so conclusions don't leak across contexts. Today the only workspace knobs are the flat workspace field, per-host blocks, and globalOverride — all of which resolve to a single workspace per process. Without cwd routing the only option is a wrapper script per project that exports a different config path, which is operationally clumsy.

How it relates to other open routing PRs

The other in-flight routing PRs all partition memory within a single workspace:

PR What it routes
#24 (xlyk) session name — glob keys into the existing sessions{} map
#32 (sasha-id) session name — per-repo strategy from git rev-parse --show-toplevel
#33 (sasha-id) host block / peer identity — HONCHO_PROFILE env var
#36 (sasha-id) session name — HONCHO_SESSION env var
this PR workspace — cwd-prefix → workspace map

These are complementary. HONCHO_PROFILE (#33) is the closest neighbour — it routes the peer identity, this routes the workspace. They can coexist (e.g. cwd→workspace, then HONCHO_PROFILE→aiPeer within it).

Implementation

  • Two new exports in plugins/honcho/src/config.ts: WorkspaceRule interface and pure helper resolveWorkspaceFromCwd(cwd, rules).
  • One opt-in workspaceRules?: WorkspaceRule[] field on HonchoFileConfig.
  • Hooked into resolveConfig at the top of the precedence chain — when resolveWorkspaceFromCwd returns a non-null value, it sets workspace and uses the host block's aiPeer (or default) for identity; otherwise the existing logic runs untouched.
  • ~ expanded to $HOME for cwdPrefix and trailing slashes normalised on both sides.
  • Prefix match is path-segment-aware: ~/code/work does NOT match ~/code/work-old (sibling directory).

Tests

plugins/honcho/src/config.test.ts — 10 unit tests covering:

  • no-rules / empty-rules short-circuit
  • exact and subdirectory matching
  • sibling-directory rejection (/work must not match /work-old or /workshop)
  • first-match ordering with overlapping prefixes
  • ~ expansion (with and without trailing path)
  • trailing-slash normalisation on both rule prefix and cwd
  • rule-order determinism among identical prefixes
$ bun test src/config.test.ts
 10 pass, 0 fail, 18 expect() calls, [44ms]

Behaviour for existing users

  • Config without workspaceRules: no-op, same code path as today.
  • Config with workspaceRules but cwd matches no rule: also no-op.
  • Only when both the field is set and a rule matches does new behaviour engage.

CHANGELOG updated with an [Unreleased] entry per CONTRIBUTING.md.

Summary by CodeRabbit

  • New Features

    • Added workspace routing via a new workspaceRules config: assign workspaces based on cwd prefixes, evaluated in order with first-match precedence over other resolution methods. Home (~) expansion is supported; if no rule matches, existing resolution behavior applies.
  • Bug Fixes

    • Normalized path separators and trailing slashes so rules work correctly on Windows and across platforms.

Review Change Stack

zachlagden and others added 2 commits May 18, 2026 07:59
Adds a new top-level config field `workspaceRules: WorkspaceRule[]` that
selects the active Honcho workspace based on the current working directory.
Useful when one Claude Code install spans multiple Honcho workspaces (e.g.
work / personal / experiments), and which workspace to use depends on which
project you're in.

Resolution order (highest priority first):
  1. workspaceRules (NEW) — first cwdPrefix match wins
  2. globalOverride (existing)
  3. hosts.<host>.workspace (existing)
  4. HONCHO_WORKSPACE env / raw.workspace / DEFAULT_WORKSPACE[host]

`cwdPrefix` supports leading `~` for $HOME. Matched as a prefix, not a glob.
When no rule matches, returns null and falls through to the existing chain,
so single-workspace setups are unaffected.

Example ~/.honcho/config.json:
  {
    "workspaceRules": [
      { "cwdPrefix": "~/work",     "workspace": "work"     },
      { "cwdPrefix": "~/personal", "workspace": "personal" },
      { "cwdPrefix": "~",          "workspace": "default"  }
    ]
  }

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
10 tests covering no-rules / empty-rules short-circuit, exact and
subdirectory matching, sibling-directory rejection, first-match
ordering, tilde expansion (with and without trailing path), trailing-
slash normalisation on both rule prefix and cwd, and rule-order
determinism among identical prefixes.
@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 70ae3ad8-183d-4cde-bcce-d68ebdb9c53b

📥 Commits

Reviewing files that changed from the base of the PR and between 35ab8cd and c7642b8.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • plugins/honcho/src/config.test.ts
  • plugins/honcho/src/config.ts
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • plugins/honcho/src/config.test.ts
  • plugins/honcho/src/config.ts

Walkthrough

Adds cwd-prefix-based workspace routing via a new WorkspaceRule config field and resolveWorkspaceFromCwd(). Rules expand ~, normalize paths (Windows-compatible), are evaluated in order with first-match precedence, and take priority in config resolution when matched; unmatched rules fall through.

Changes

Workspace routing via cwd prefix rules

Layer / File(s) Summary
Core routing logic and contract
plugins/honcho/src/config.ts
WorkspaceRule interface and exported resolveWorkspaceFromCwd() implement ~$HOME expansion, path normalization, and ordered first-match cwd-prefix matching; returns the matched workspace or null.
Config schema and resolution integration
plugins/honcho/src/config.ts
Adds optional workspaceRules to persisted config. resolveConfig() calls resolveWorkspaceFromCwd(process.cwd(), raw.workspaceRules) and, on match, sets workspace from the rule and derives aiPeer from host/raw/default fallbacks; non-matching cases continue existing resolution.
Tests and changelog
plugins/honcho/src/config.test.ts, CHANGELOG.md
Bun test suite covers null/empty rules, exact and subdirectory matches, sibling non-matches, first-match precedence, ~ expansion (including bare ~), trailing-slash normalization, and Windows backslash handling. CHANGELOG documents workspaceRules behavior and precedence.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Poem

🐰 I sniff the path where projects hide,
Tilde to home, I hop inside,
Rules in order, first one true,
Guide my paws — workspace found for you!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'feat(config): cwd-based workspaceRules routing' clearly and specifically describes the main feature addition: a new cwd-based workspace routing mechanism via the workspaceRules config field.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@plugins/honcho/src/config.ts`:
- Around line 38-42: The cwd-prefix matching currently compares normalized and
prefix using a hardcoded "/" which fails on Windows; update the logic inside the
function that computes normalized and prefix (variables normalized, prefix, and
the loop over rules with rule.cwdPrefix and expandHome) to be separator-agnostic
by normalizing path separators before trimming and comparing (e.g., replace
backslashes with forward slashes or use Node's path.sep-aware normalization) and
then perform the equality and startsWith checks against the normalized forms so
matches work on Windows too.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ea9c97a0-732d-4acb-815d-ab34725ef17e

📥 Commits

Reviewing files that changed from the base of the PR and between 0813ebd and 35ab8cd.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • plugins/honcho/src/config.test.ts
  • plugins/honcho/src/config.ts

Comment thread plugins/honcho/src/config.ts Outdated
CodeRabbit flagged that `resolveWorkspaceFromCwd` hardcoded "/" as the
path separator, so Windows cwds (which `process.cwd()` returns with `\`)
would miss valid subdirectory matches and fall through to the wrong
resolution branch. Normalise both `cwd` and the rule's expanded
`cwdPrefix` to forward slashes before trim + prefix comparison, and add
four Windows-cwd tests (forward-slash prefix, backslash prefix, sibling
rejection, trailing-backslash normalisation).

Refs: plastic-labs#41 (comment)
@zachlagden

Copy link
Copy Markdown
Author

Pushed c7642b8 to address the Windows path-separator finding from CodeRabbit.

Fix (plugins/honcho/src/config.ts)
Normalise \/ on both sides before trim + prefix comparison, so process.cwd() on Windows (C:\Users\alice\work\project) matches a rule whose cwdPrefix is written either Unix-style (C:/Users/alice/work) or Windows-style (C:\\Users\\alice\\work). The path-segment-boundary guarantee (sibling directories like work-old don't match work) carries over after normalisation.

Tests (plugins/honcho/src/config.test.ts) — 4 new, 14 total now passing:

  • Backslash cwd matches forward-slash prefix
  • Backslash cwd matches backslash prefix
  • Backslash sibling-directory rejection (C:\Users\alice\work-oldC:\Users\alice\work)
  • Trailing-backslash normalisation

CHANGELOG — added a second [Unreleased] bullet noting the Windows normalisation.

I left the 25%-docstring-coverage warning alone — the new exports (WorkspaceRule interface, resolveWorkspaceFromCwd, expandHome) all have JSDoc; the threshold is reading the whole config.ts module which has many older internal helpers without docstrings, and that feels out of scope for this PR.

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