ahmedelgabri/dotfiles
关于
~ 🍭 ~
开发工具实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
nix/pkgs/zh/README.md
原文zh
zh records and searches the shell commands that coding agents (Claude Code, Codex, pi) run.
These commands go in their own SQLite history next to .zsh_history, not in it. Thus they do not show in up-arrow, substring search, or inline suggestions. You can still search them with the Ctrl-R widget in config/zsh.d/zsh/config/extras.zsh.
| Caller | Subcommands used |
|---|---|
| Claude and Codex hooks | record |
pi agent-history extension |
record |
| Ctrl-R widget | list, show, forget |
Commands
| Command | What it does |
|---|---|
zh record AGENT |
Reads a Claude-Code-style hook payload from stdin and records its Bash command for AGENT. |
zh list [FILTERS] |
Prints NUL-separated id\ttime\tdir\tcmd rows, newest first. id is always empty. |
zh show [FILTERS] -- CMD |
Prints the newest record of CMD in the filters. |
zh forget CMD |
Deletes all records of CMD. If CMD starts with -, use zh forget -- CMD. |
zh stats [OPTIONS] |
Counts the most used command prefixes. See Stats over zsh history. |
zh import-atuin [PATH] |
Copies the commands that agents ran from an atuin database. |
list, show, and stats accept these filters. You can give each filter more than one time. stats always counts all rows, so --all has no effect there.
| Filter | Selects |
|---|---|
--dir DIR |
records from DIR and its subdirectories |
--repo DIR |
records from the jj workspace or Git worktree of DIR |
--agent NAME |
records from one agent |
--session ID |
records from one agent session |
--all |
all records: no deduplication, failed calls included |
-h and --help print help and exit with status 0, for zh and for each subcommand.
Database
The database is $ZDOTDIR/.zh.db. When ZDOTDIR is unset or empty, it is $HOME/.zh.db.
- The first write creates the file with mode 0600, in WAL mode, with all columns.
- If a
commandstable already exists,zhdoes not change it.zhdoes not migrate tables that do not have thesession,status, anddescriptioncolumns, because no such database is in use. - One table holds all records.
import-atuinattaches atuin's database while it runs and reads itshistorytable. The default path is${XDG_DATA_HOME:-~/.local/share}/atuin/history.db. A second run skips the rows that are already copied.
erDiagram
commands {
INTEGER id PK "rowid, insertion order"
INTEGER ts "Unix seconds; indexed (commands_ts)"
TEXT agent "claude, codex, pi, ..."
TEXT cwd "indexed (commands_cwd)"
TEXT cmd "the command, verbatim minus NULs"
TEXT session "hook session_id, default ''; indexed (commands_session)"
TEXT status "ran or failed, default 'ran'"
TEXT description "Claude's tool_input.description, default ''"
}
atuin_history["atuin.history (attached, read-only)"] {
INTEGER timestamp "nanoseconds, becomes ts / 1e9"
TEXT command "becomes cmd"
TEXT cwd "becomes cwd"
TEXT author "becomes agent (claude-code to claude)"
INTEGER author_kind "2 = agent; only these are copied"
INTEGER deleted_at "NULL rows only"
}
atuin_history ||..o{ commands : "import-atuin copies agent rows"
- All columns except
idareNOT NULL. statusisfailedonly for Claude'sPostToolUseFailurehook events. Codex and pi always recordran.listreadscommandsnewest first (ts DESC, id DESC). Without--all, it shows eachcmdone time and does not showfailedrows.statsgroups all rows in the filters by command prefix.
Recording
zhdoes not record a call when its command or its description matches the credential filter. The filter uses Rust'sregexwith simple case folding and Unicode word boundaries. Thus it records some shapes, such asPAßWORD=xor a zero-width joiner beforeTOKEN=.- A
cwd,session_id, ordescriptionthat is not a string is recorded as empty. The record does not fail. zhremoves NUL characters from all fields.
Stats over zsh history
zh stats counts the most used command prefixes in the agent records and in the interactive zsh history. It prints count\tfailed\tprefix for each prefix.
| Option | What it does |
|---|---|
--words N |
The number of words in a prefix. The default is 2. |
--limit N |
The number of prefixes to print. The default is 30. |
--source shell|agents|all |
The history to count. The default is all. |
--histfile PATH |
A different zsh history file. The default is ${ZDOTDIR:-$HOME}/.zsh_history. |
--since WHEN |
Only entries from WHEN. |
--until WHEN |
Only entries up to WHEN. |
Words
Words are runs of characters between whitespace, not shell syntax. Thus an env prefix or cd x && counts as words.
Sources and filters
- zsh history has no directory, agent, or session. Thus
--dir,--repo,--agent, and--sessionapply to agent records only. - Without
--source, these filters selectagents. - With
--source shellor--source all, these filters are a usage error. Thus a total never mixes filtered and unfiltered entries. --histfileis a usage error when only agent records are in scope (--source agents, or an agent-only filter).zhdoes not use$HISTFILE, becausezhruns from hooks, where it is unset.- A history file that does not exist counts as empty.
Time bounds
--since and --until apply to both sources. Both bounds include the full unit that you give:
| Form | Meaning |
|---|---|
YYYY-MM-DD |
a full local day |
"YYYY-MM-DD HH:MM" |
a full local minute |
12h, 3d, 2w |
hours, days, or weeks before now (fixed 1, 24, and 168 hours) |
- For example,
--since 2026-09-01 --until 2026-09-30is all of September. - A local time that a DST change skips is an error.
- A local time that a DST change repeats means its first occurrence.
- When you give a bound, entries without a timestamp are not counted.
Reading zsh history
zh reads the history file as zsh does:
- It restores metafied bytes.
- It joins continuation lines before it finds the headers.
- It undoes zsh's escapes for a trailing backslash and a leading colon.
A test makes real zsh write a history file, and then checks that each command reads back unchanged.
Limits of the shell counts
- Shell counts are the entries that zsh kept, not all executions.
HIST_IGNORE_DUPSand related options drop repeats, but the agent database keeps them. Thus compare counts in one source, not across sources. - zsh does not record exit status, so shell entries never count as
failed. - Shell entries go through the same credential filter as agent records before grouping. The filter does not catch all secrets, so the prefixes that
statsprints are only as clean as the filter.
Exit codes
| Code | When |
|---|---|
| 0 | Success, -h/--help, or output to a closed pipe (zh stops without a message). |
| 1 | A failure, or an argument error in a subcommand. |
| 64 | A missing or unknown subcommand. A bad value for an option that only stats has, such as --words, --limit, or --since. A stats usage error from Sources and filters. |
Argument error messages come from clap.
Output details
list shows a tab in the directory of a record as ␉. The stored directory does not change. This is necessary because the command starts after the third tab of each row.
Layout
| File | Role |
|---|---|
src/main.rs |
clap definitions, dispatch, exit codes |
src/db.rs |
database path, creation with mode 0600, busy timeout, schema creation |
src/record.rs |
hook payload parsing, credential filter, insert |
src/query.rs |
filters, list, show, forget, stats, import-atuin |
src/repo.rs |
jj/git root resolution for --repo |
src/zsh.rs |
zsh history file reader for stats |
src/when.rs |
--since/--until parsing and local-time resolution |
tests/cli.rs |
integration tests that run the built binary against real SQLite |
tests/fixtures/ |
synthetic database, atuin database, hook payloads, and the golden case list |
tests/golden/ |
the expected output of each case |
The unit tests are next to the code, in the tests module of each file.
Build and test
nix develop .#rust
cargo test
cargo clippy --all-targets -- -D warnings
cargo fmt --check
nix build .#zh and nix flake check build the package and run the same tests in the sandbox. The check inputs are:
gitandjujutsu, because the--repotests create repositorieszsh, because the history tests make it write a history file
Cargo.lock pins the dependencies, and the Nix build reads it directly. Thus there is no vendor hash to update.
Golden tests
tests/fixtures/cases.jsonlists the invocations.tests/golden/<name>.jsonholds the exit code, stdout, and stderr of each run. When a case setsrows, the file also holds the rows that the run leaves in the database. A Bash script made these outputs.zhkeeps the same subcommands, output bytes, exit codes, and SQLite schema as that script.- The integration test runs each case against the binary. It compares stdout and the stderr messages byte for byte.
- Argument errors and help are not in the golden files, because clap writes their text.
tests/cli.rschecks their exit codes separately.
The fixtures are synthetic. Never add real history to them.
探索相关项目
加载中…