Skip to content

refactor(config-state): structured JSON for logs; drop --hook/--branch filters - #2161

Merged
max-sixty merged 6 commits into
mainfrom
json-logs
Apr 13, 2026
Merged

max-sixty merged 6 commits into
mainfrom
json-logs

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Builds on #2156 which hoisted --format and added absolute path. Two follow-on changes:

1. Structured fields on hook_output entries

Hook-output log entries carried a slash-packed relative path in file — consumers had to split on / to filter by branch, source, or hook type. Expose first-class structured fields:

{
  "file": "main/user/post-start/server.log",
  "path": "/.../.git/wt/logs/main/user/post-start/server.log",
  "branch": "main",
  "source": "user",
  "hook_type": "post-start",
  "name": "server",
  "size": 1024,
  "modified_at": 1700000000
}

Internal-op entries (e.g., feature/internal/remove.log) normalize onto the same shape with source: "internal", hook_type: null, and the op name in name.

2. Drop --hook / --branch filters; use jq

With the structured fields, the old --hook=SPEC / --branch=X interface is a jq one-liner:

wt config state logs --format=json \
  | jq -r '.hook_output[]
           | select(.source == "user"
                    and .hook_type == "post-start"
                    and (.name | startswith("server")))
           | .path'

That subsumes the filter flags without the CLI plumbing, the HookLog::parse/to_spec roundtrip, or the error-shape tests that locked in a fragile source:hook-type:name grammar. logs and logs get now always list — one code path, one shape.

Breaking change: wt config state logs get --hook=... and --branch=... are removed. The jq pattern is documented in wt config state logs --help.

This was written by Claude Code on behalf of Maximilian Roos

max-sixty and others added 5 commits April 12, 2026 20:07
…s to logs JSON

Hook-output entries exposed a slash-packed relative path in `file` —
consumers had to split on `/` to filter by branch, source, or hook type.
Add first-class structured fields so `jq '.hook_output[] | select(.source
== "user" and .hook_type == "post-start")'` works directly:

    {
      "file": "main-vfz/user/post-start/server-abc.log",
      "path": "/.../.git/wt/logs/main-vfz/user/post-start/server-abc.log",
      "branch": "main-vfz",
      "source": "user",
      "hook_type": "post-start",
      "name": "server-abc",
      "size": 1024,
      "modified_at": 1700000000
    }

Internal-op entries (e.g., `feature/internal/remove.log`) normalize onto
the same shape with `source: "internal"`, `hook_type: null`, and the op
name in `name`, so one filter pattern covers both variants.

Values are the on-disk (sanitized) names — reversing the sanitization
would require matching against current git/config state, which is out of
scope for a pure log listing. Users with a configured hook named
`server` can match with `startswith("server")` against the sanitized
`server-abc` on disk.

Top-level shared files (command_log, diagnostic) retain their existing
shape — the structured fields apply only to per-branch hook output.

Co-Authored-By: Claude <[email protected]>
… with jq

With structured `branch`/`source`/`hook_type`/`name` fields on every
`hook_output` entry, path resolution by hook spec is a `jq` one-liner:

    wt config state logs --format=json \
      | jq -r '.hook_output[]
               | select(.source == "user"
                        and .hook_type == "post-start"
                        and (.name | startswith("server")))
               | .path'

That subsumes the old `--hook=SPEC` / `--branch=X` interface without the
CLI plumbing, the `HookLog::parse`/`to_spec` roundtrip, or the error-shape
tests that locked in a fragile `source:hook-type:name` grammar. `logs` and
`logs get` now always list; `--format=json` emits a single uniform shape
for interactive viewing and automation alike.

Breaking change: `wt config state logs get --hook=...` and `--branch=...`
are removed. Scripts using `tail -f "$(wt config state logs get
--hook=SPEC)"` should switch to the jq pipeline above; the jq examples
are in `wt config state logs --help`.

Co-Authored-By: Claude <[email protected]>
# Conflicts:
#	src/commands/process.rs
#	tests/integration_tests/config_state.rs
#	tests/snapshots/integration__integration_tests__config_state__logs_get_json_with_files.snap
#	tests/snapshots/integration__integration_tests__config_state__state_logs_get_hook_not_found.snap
# Conflicts:
#	tests/integration_tests/config_state.rs
#	tests/snapshots/integration__integration_tests__config_state__logs_get_json_with_files.snap
…tured logs JSON

Shows the jq pattern for resolving one hook's log path from the
structured `hook_output` entries in `wt config state logs --format=json`.
Handles both exact and hash-suffixed name matches, so the same alias
works whether the hook name needed sanitization or not.

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The documented jq one-liners for filtering by hook name have a precedence bug — | binds looser than and, so and .name | startswith("server") pipes the boolean result of the whole and chain into startswith, which errors with startswith() requires string inputs. Parenthesize (.name | startswith("server")) so only the name feeds the pipe. The same pattern is already used correctly in the PR body and in the extending.md hook-log alias recipe. Three occurrences in this file — fixing here will regenerate docs/content/config.md and skills/worktrunk/reference/config.md via the sync test.

Reproducer:

$ echo '[{"source":"user","hook_type":"post-start","name":"server-foo","path":"A"}]' \
  | jq -r '.[] | select(.source == "user" and .hook_type == "post-start" and .name | startswith("server")) | .path'
jq: error (at <stdin>:1): startswith() requires string inputs

Comment thread src/cli/config.rs Resolved
Comment thread src/cli/config.rs Resolved
Comment thread src/cli/config.rs Resolved
# Conflicts:
#	src/commands/config/state.rs
@max-sixty
max-sixty merged commit 50f9b82 into main Apr 13, 2026
24 checks passed
@max-sixty
max-sixty deleted the json-logs branch April 13, 2026 03:58
max-sixty added a commit that referenced this pull request Apr 13, 2026
#2169)

Two small polish changes to the `hook-log` alias recipe added in #2161:

- **Multi-line the alias body** so the 217-char pipeline becomes
readable in the rendered docs. The jq expression lives inside single
quotes, so literal newlines are safe; the outer shell pipeline reads
top-to-bottom without wrapping.
- **Drop the `startswith($name + "-")` branch.** It was meant to also
match the sanitized-with-hash form, but only covers the specific 3-char
suffix shape and would misfire on hook names that naturally end in
`-<3alnum>`. The limitation is documented instead: users with hook names
that required sanitization should pass the on-disk sanitized form.

A follow-up PR (dispatched separately) adds a `sanitize_hash` template
filter so the alias can pre-sanitize its input and match reliably across
all hook names.

> _This was written by Claude Code on behalf of Maximilian Roos_

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request Sep 26, 2026
…ipes wt rejects (#4279)

Two recipes on the Tips & Patterns page fail when copied. This PR fixes
both, adds a test that runs the database recipe straight from the page,
and makes the hook-output JSON report real branch names so the log
recipes can scope to the current branch.

- **Database per worktree**: the `set-vars` step passed three
`KEY=VALUE` pairs to one `wt config state vars set`, which accepts
exactly one (`error: unexpected argument 'port=…' found`). Because the
step failed, `vars.container` and `vars.port` were never set for the
`db` step or `pre-remove`. The step now runs three `vars set` calls
chained with `&&`. The recipe also stored `db_url`, and vars keys have
rejected underscores since #2285 (Git config variable names can't
contain them). It's now `db-url`, in the `vars get` example too. #1848
had moved this key from hyphens to underscores because Jinja parses
`vars.db-url` as subtraction. That doesn't apply here: the recipe reads
the key only with `wt config state vars get db-url`, never in a
template.
- **Monitor hook logs**: `wt config state logs get
--hook=user:post-start:server` and the `wtlog` alias built on it fail
with `unexpected argument '--hook'`, because #2161 removed that flag.
Its replacement, a `jq` filter over `wt config state logs
--format=json`, couldn't scope to the current branch. The JSON `branch`
field was the sanitized log directory (`feature/x` became
`feature-x-<hash>`), so the help example used `startswith` and `head -1`
and tailed whichever worktree's `server` log was newest.
- **Hook-output JSON `branch`**: the field now holds the real branch
name. It maps each log directory back through `sanitize_for_filename`
over the local branches, and is `null` when no local branch writes
there: a deleted branch, a detached HEAD, or a directory two branches
share. The recipes in the help and on the page match `[.branch, .source,
.hook_type, .name]` exactly. The extending page's `hook-log` alias
compared `.branch` with `{{ branch | sanitize_hash }}`, the on-disk
form, and now passes `{{ branch }}` to jq with `--arg`, like its other
values. The `wtlog` alias becomes a shell function taking the hook name.

`test_doc_example_database_set_vars_step_runs` pulls the `set-vars` step
from the page's TOML block, runs it as a `wt` alias with `wt` on `PATH`,
and checks the stored `port`. It fails on `main` with the argument error
above. After the first fix it also caught the `db_url` rejection.
`test_logs_get_json_branch_is_unsanitized` covers the branch mapping,
including the deleted and shared-directory cases. I ran the new recipe
by hand from a `feature/x` worktree next to an `other/y` worktree, and
it tailed only the `feature/x` log.

The `skills/` and `plugins/` copies of the page are regenerated by
`test_docs_are_in_sync`.

---------

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Maximilian Roos <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants