Skip to content

Commit a182bc8

Browse files
committed
feat(codex): read saved exec JSON usage via CODEX_HOME
Merge the current Rust-based main branch into the Codex exec JSON PR and keep the public configuration model aligned with Claude-style data paths. Saved codex exec --json output is now discovered through comma-separated CODEX_HOME entries. Entries that contain sessions/ are treated as normal Codex homes, while entries without sessions/ are read directly as JSONL directories, so a command such as CODEX_HOME="/Users/ryoppippi/.codex,/Users/ryoppippi/codex-exec-logs" covers both sources without adding a separate JSON option or CODEX_EXEC_LOG_DIR. The Rust Codex loader now handles standalone exec usage payloads, nested result usage/model fields, stringified token counts, and missing timestamps by falling back to the source file modification time. Documentation was updated to describe only CODEX_HOME-based discovery.
2 parents c22ba12 + 71de950 commit a182bc8

206 files changed

Lines changed: 18676 additions & 31145 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/skills/ast-grep/SKILL.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
name: ast-grep
3+
description: Guides ccusage structural code searches with ast-grep. Use when finding syntax patterns, validating migrations, or writing AST-based search commands.
4+
---
5+
6+
# ast-grep
7+
8+
Use ast-grep when a search needs syntax structure rather than plain text. Prefer `rg` for simple text search.
9+
10+
## Tooling
11+
12+
This repo provides `ast-grep` through `flake.nix`. Run commands from the Nix dev shell, or use `direnv exec .` when the shell is not already active:
13+
14+
```sh
15+
direnv exec . ast-grep run --lang ts --pattern '/$P/' --json=stream apps packages
16+
```
17+
18+
If `direnv` is unavailable and this is a one-off search, use comma as a fallback:
19+
20+
```sh
21+
, ast-grep run --lang ts --pattern '/$P/' --json=stream apps packages
22+
```
23+
24+
## Workflow
25+
26+
1. Describe the syntax shape you need to find.
27+
2. Start with a small pattern and test it against the repo.
28+
3. Use `--debug-query` when the pattern does not match the AST shape you expected.
29+
4. Only move to YAML rules when `run --pattern` cannot express the search.
30+
31+
## Commands
32+
33+
For quick pattern searches:
34+
35+
```sh
36+
ast-grep run --lang ts --pattern 'console.log($ARG)' apps packages
37+
```
38+
39+
For JSON output that scripts can consume:
40+
41+
```sh
42+
ast-grep run --lang ts --pattern '/$P/' --json=stream apps packages
43+
```
44+
45+
## Rule Tips
46+
47+
- Use `run --pattern` for simple single-node matches.
48+
- Use `scan --rule` or `scan --inline-rules` for relational rules such as `inside` or `has`.
49+
- Add `stopBy: end` to relational rules so ast-grep searches the full direction.
50+
- Use `--debug-query=ast`, `--debug-query=cst`, or `--debug-query=pattern` when a rule does not match the code shape you expected.

‎.agents/skills/ccusage-agent-sources/SKILL.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Keep agent-specific code there. Split files by responsibility when the implement
6060
- `pricing.ts` or `pricing-macro.ts` - agent-specific pricing candidates, bundled pricing, or provider filters.
6161
- `types.ts` - source-local types when they are not shared outside the adapter.
6262

63+
For Rust work, use the `ccusage-rust` skill and mirror the same responsibility boundaries under `rust/crates/ccusage/src/adapter/<agent>/` where practical: `mod.rs`, `paths.rs`, `parser.rs`, `loader.rs`, and `report.rs`.
64+
6365
When moving an existing loader into an adapter, update internal imports to the adapter path instead of adding compatibility re-export shims. Keep old root-level modules only when they are part of the package's declared public exports or are dedicated bundled worker entries. In `apps/ccusage`, `src/data-loader.ts` is a worker/build entry for the optimized Claude `data-loader` chunk from PR #984; do not put new source logic there.
6466

6567
Use shared ccusage foundation for rendering, table layout, logging, date formatting, progress, pricing fetcher lifecycle, JSONL walking, worker gating, worker spawn/result ordering, and aggregation wherever the source data permits. Agent adapters should mainly own source-specific log discovery, parsing, token mapping, model mapping, and source-specific metadata.

‎.agents/skills/ccusage-development/SKILL.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Tools are managed by `flake.nix` and `package.json`. Use `comma` or `nix run` fo
4848

4949
- Use TypeScript strict-mode patterns already present in the package.
5050
- Prefer `satisfies` and `as const satisfies` over unsafe `as` assertions; use the `typescript-style` skill for details.
51+
- Use the `ccusage-rust` skill before editing `rust/crates/**`, native packaging behavior, Rust pricing embedding, or Rust performance work.
5152
- Use `.ts` extensions for local imports.
5253
- Use Node path utilities for file paths.
5354
- Use `logger.ts` instead of `console.log`.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
name: ccusage-rust
3+
description: Guides ccusage Rust implementation work. Use when editing rust/crates, native packaging, Rust performance, parser/module layout, pricing embedding, or Rust/TypeScript parity.
4+
paths:
5+
- 'rust/**/*.rs'
6+
- 'rust/**/*.toml'
7+
- 'rust/**/build.rs'
8+
globs: 'rust/**/*.rs,rust/**/*.toml,rust/**/build.rs'
9+
---
10+
11+
# ccusage Rust
12+
13+
Use this skill for the native Rust CLI under `rust/crates/ccusage` and `rust/crates/ccusage-terminal`.
14+
15+
## Source Parity
16+
17+
Rust must match the TypeScript implementation on `origin/main` unless the user explicitly scopes a behavior change. Before implementing or refactoring an agent, inspect the corresponding TypeScript adapter:
18+
19+
```sh
20+
git ls-tree -r --name-only origin/main apps/ccusage/src/adapter
21+
git show origin/main:apps/ccusage/src/adapter/<agent>/index.ts
22+
git show origin/main:apps/ccusage/src/adapter/<agent>/parser.ts
23+
git show origin/main:apps/ccusage/src/adapter/<agent>/paths.ts
24+
```
25+
26+
Preserve report semantics, JSON fields, table columns, progress/spinner text, agent grouping, date filtering, `--offline`, `CLAUDE_CONFIG_DIR`, and source-specific environment variables.
27+
28+
## Module Layout
29+
30+
Do not keep growing `main.rs` or single large adapter files. Mirror the TypeScript responsibility boundaries where practical:
31+
32+
- `adapter/<agent>/mod.rs` - public adapter surface and command wiring.
33+
- `adapter/<agent>/paths.rs` - environment variables, defaults, and path discovery.
34+
- `adapter/<agent>/parser.rs` - raw record parsing and token/model mapping.
35+
- `adapter/<agent>/loader.rs` - file walking, SQLite reads, dedupe, and date filtering entry points.
36+
- `adapter/<agent>/report.rs` - JSON/table row shaping when agent-specific.
37+
- shared modules stay in `types.rs`, `summary.rs`, `output.rs`, `pricing.rs`, `progress.rs`, and `date_utils.rs`.
38+
39+
Keep public `pub(crate)` surfaces narrow. Prefer moving tests with the code they exercise instead of leaving all Rust tests in `main.rs`.
40+
41+
When splitting large Rust modules or removing duplication, use the `reduce-similarities` skill, which runs `similarity-rs` for `.rs` files.
42+
43+
## Pricing Embedding
44+
45+
TypeScript uses build/macro-time pricing snapshots. Rust should not rely on a manually edited `claude-pricing.json` as the only embedded source.
46+
47+
When changing pricing:
48+
49+
- Prefer a `build.rs` step that fetches LiteLLM `model_prices_and_context_window.json` into `OUT_DIR`.
50+
- Keep a checked-in fallback snapshot so offline builds and network failures still work.
51+
- Load the generated build-time snapshot first, then fallback pricing, then runtime fetch when not `--offline`.
52+
- Add tests for embedded/offline pricing and context limits.
53+
54+
## Performance
55+
56+
Read the local Rust Performance Book clone before non-trivial optimization:
57+
58+
```text
59+
/Users/ryoppippi/ghq/github.com/nnethercote/perf-book/src/profiling.md
60+
/Users/ryoppippi/ghq/github.com/nnethercote/perf-book/src/io.md
61+
/Users/ryoppippi/ghq/github.com/nnethercote/perf-book/src/heap-allocations.md
62+
/Users/ryoppippi/ghq/github.com/nnethercote/perf-book/src/parallelism.md
63+
/Users/ryoppippi/ghq/github.com/nnethercote/perf-book/src/type-sizes.md
64+
```
65+
66+
Profile before committing an optimization. Validate with end-to-end `hyperfine` and JSON/table parity, not only microbenchmarks.
67+
68+
For ccusage workloads, check:
69+
70+
- I/O count and buffering before CPU-only tweaks.
71+
- Avoid unnecessary `String` allocation and cloning on hot paths; prefer borrowed `&str`, `Arc<str>`, or typed summaries where ownership is needed.
72+
- Avoid returning large intermediate object vectors when worker-side aggregation or typed transfer payloads can preserve output.
73+
- Use parallelism only when it improves end-to-end command time on real fixture shapes.
74+
- Keep binary size visible when adding dependencies or enabling features.
75+
76+
## Validation
77+
78+
Use the `ccusage-testing` skill for Rust test commands. For perf or parity work, compare against `origin/main` TypeScript output for a stable fixture window before changing behavior.

‎.agents/skills/ccusage-testing/SKILL.md‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
---
22
name: ccusage-testing
3-
description: Guides ccusage tests. Use when adding or fixing in-source Vitest, fs-fixture data, CLI snapshots, Claude model pricing, or LiteLLM compatibility.
3+
description: Guides ccusage tests. Use when adding or fixing in-source Vitest, Rust cargo tests, fs-fixture data, CLI snapshots, Claude model pricing, or LiteLLM compatibility.
44
---
55

66
# ccusage Testing
77

88
Use the `tdd` skill for logic changes and general test readability rules, including the guidance to avoid over-DRYing tests when duplication improves clarity. Use the `fs-fixture` skill when creating filesystem fixtures. This skill adds ccusage-specific Vitest, fixture, model, and pricing rules.
99

10+
## Rust Tests
11+
12+
Use `direnv exec .` when the current shell does not already expose the Rust toolchain:
13+
14+
```sh
15+
direnv exec . cargo test --manifest-path rust/Cargo.toml --workspace
16+
direnv exec . cargo test --manifest-path rust/Cargo.toml --workspace <test_name>
17+
direnv exec . cargo test --manifest-path rust/Cargo.toml --workspace -- --ignored
18+
```
19+
20+
For repo-wide validation, prefer the package script because it runs Vitest and Rust tests together:
21+
22+
```sh
23+
direnv exec . pnpm run test
24+
```
25+
26+
Put Rust unit tests near the module they exercise with `#[cfg(test)] mod tests`. When splitting a large module, move its tests with the code instead of leaving unrelated tests in `main.rs`.
27+
28+
Use fixture-backed Rust tests for parser, path discovery, SQLite loading, dedupe, aggregation, pricing, and CLI output parity. For TDD syntax and focused cargo test examples, read `../tdd/references/rust-examples.md`.
29+
1030
## Vitest Pattern
1131

1232
- Tests live beside implementation in `if (import.meta.vitest != null)` blocks.
@@ -27,7 +47,8 @@ Utility functions should include concise JSDoc describing their purpose and focu
2747

2848
- Avoid `try`/`catch` in tests for expected failures. Use `expect(...).toThrow()`, `await expect(...).rejects`, or explicit Result failure assertions.
2949
- Avoid `if` branches inside test bodies. Split behaviors into separate tests or use `it.each` for table-driven cases.
30-
- Do not over-DRY tests. Keep repeated setup inline when it makes the behavior easier to read.
50+
- Tests do not need to be DRY. Prefer repeated, explicit setup in each test when it makes the behavior easier to read.
51+
- Do not hoist one-off values out of tests. Write literals and direct setup values inline in the test body when sharing them would make the behavior harder to read.
3152

3253
For concrete good and bad examples, read `../tdd/references/vitest-examples.md`.
3354

‎.agents/skills/commit/SKILL.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ Use standard git history commands to understand intent before committing. Prefer
7777
- Potential impacts
7878
- Wrap at 72 characters
7979

80+
**Subject should name the artifact or behavior changed**:
81+
82+
- Prefer concrete subjects that make sense when read alone in a commit list.
83+
- Avoid vague review-process subjects such as `chore: address review feedback`,
84+
`chore: apply comments`, or `fix: update per CodeRabbit`.
85+
- Put reviewer context in the body, not the subject. For example, use
86+
`docs(skills): clarify arkregex newline guidance` with a body explaining that
87+
it addresses CodeRabbit feedback.
88+
8089
## Quality Checks
8190

8291
- Can this be reverted without breaking other functionality?
Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,84 @@
11
---
22
name: reduce-similarities
3-
description: Detects duplicate JavaScript and TypeScript code with similarity-ts. Use when checking .js, .jsx, .ts, or .tsx files for refactoring opportunities.
3+
description: Detect duplicate Rust code using AST-based similarity analysis. Use when working with .rs files and looking for code duplication or refactoring opportunities.
44
argument-hint: '[path] [--threshold 0.85] [--print]'
5-
allowed-tools: Bash(similarity-ts *) Read Grep Glob
6-
paths: '**/*.ts,**/*.tsx,**/*.js,**/*.jsx'
5+
allowed-tools: Bash(similarity-rs *) Read Grep Glob
6+
paths: '**/*.rs'
77
---
88

9-
# TypeScript/JavaScript Code Similarity Detection
9+
# Rust Code Similarity Detection
1010

1111
## What to do
1212

13-
Run `similarity-ts` on the target project to detect duplicate functions and types, then analyze results and propose refactoring.
13+
Run `similarity-rs` on the target Rust project to detect duplicate functions, struct/enum definitions, and impl methods, then analyze results and propose refactoring.
1414

15-
similarity-ts is installed via nix. check out flake.nix for details.
15+
If `similarity-rs` is not installed:
16+
17+
```bash
18+
similarity-rs
19+
20+
# or
21+
22+
direnv exec . similarity-rs
23+
```
1624

1725
## Step 1: Run similarity analysis
1826

1927
```bash
20-
similarity-ts $ARGUMENTS
28+
similarity-rs $ARGUMENTS
2129
```
2230

2331
If no arguments given:
2432

2533
```bash
26-
similarity-ts . --threshold 0.85 --min-tokens 25
34+
similarity-rs . --threshold 0.85 --min-lines 5
2735
```
2836

29-
For type-level duplicates (interfaces, type aliases):
37+
For struct/enum similarity:
3038

3139
```bash
32-
similarity-ts . --threshold 0.85 --experimental-types
40+
similarity-rs . --threshold 0.85 --experimental-types
3341
```
3442

3543
## Step 2: Analyze results
3644

3745
### High-priority
3846

39-
- **100% similarity**: Extract shared function
40-
- **95-100%**: Parameterize the small difference
41-
- **Duplicate types/interfaces**: Consolidate into a single definition and re-export
47+
- **100% similarity**: Exact duplicates -> extract shared function or use generics
48+
- **95-100%**: Same algorithm on different types -> generic function with trait bounds
49+
- **Duplicate impl methods**: Same logic across types -> trait with default implementation
4250

4351
### Medium-priority
4452

45-
- **85-95%**: Extract common pattern, especially for API handlers and data processing
46-
- **Similar type literals**: Shared interface with optional fields
53+
- **85-95%**: Similar match arms or error handling -> macro or shared helper
54+
- **Parallel struct definitions**: Identical fields -> shared base or generic struct
4755

4856
### Acceptable
4957

50-
- **Short utility functions** (< 5 lines) that naturally share structure
51-
- **Overloaded variants** that differ by type parameter
58+
- **Short `new()` constructors** with field initialization
59+
- **Simple `From`/`Into` implementations**
60+
- **Derive-equivalent implementations**
5261

5362
## Step 3: Propose refactoring
5463

55-
For each high-priority pair, show before/after code with concrete implementation.
64+
For each high-priority pair, show before/after code with Rust idioms.
5665

5766
## Key Options
5867

59-
| Option | Description |
60-
| ------------------------------- | -------------------------------------------------------- |
61-
| `--threshold <0-1>` | Similarity threshold (default: 0.85) |
62-
| `--min-tokens <n>` | Skip functions with fewer AST nodes (recommended: 20-30) |
63-
| `--print` | Show actual code snippets |
64-
| `--experimental-types` | Enable type/interface similarity detection |
65-
| `--experimental-overlap` | Enable partial overlap detection |
66-
| `--extensions <ext>` | File extensions to check (comma-separated) |
67-
| `--filter-function <name>` | Filter by function name |
68-
| `--filter-function-body <text>` | Filter by function body content |
69-
| `--fail-on-duplicates` | Exit code 1 if duplicates found |
70-
71-
## Common TS/JS refactoring patterns
72-
73-
- **Data processing loops** with different field names -> generic mapper
74-
- **API handlers** with similar request/response logic -> shared middleware
75-
- **Validation functions** -> schema-based validation
76-
- **Duplicate interfaces** -> shared base interface with extensions
77-
- **Similar type literals** -> extract named type
68+
| Option | Description |
69+
| -------------------------- | ------------------------------------------------ |
70+
| `--threshold <0-1>` | Similarity threshold (default: 0.85) |
71+
| `--min-lines <n>` | Skip functions shorter than n lines (default: 3) |
72+
| `--min-tokens <n>` | Skip functions with fewer AST nodes |
73+
| `--print` | Show actual code snippets |
74+
| `--experimental-types` | Enable struct/enum similarity detection |
75+
| `--filter-function <name>` | Filter by function name |
76+
| `--fail-on-duplicates` | Exit code 1 if duplicates found |
77+
78+
## Common Rust refactoring patterns
79+
80+
- **Type-specific functions** -> generic `fn<T: Trait>` with trait bounds
81+
- **Duplicate impl blocks** across types -> trait with default methods
82+
- **Repeated match patterns** -> macro_rules! or helper function
83+
- **Parallel X/Y/Z functions** -> enum parameter or tuple-based abstraction
84+
- **Similar error handling** -> shared Result combinator or `?` chain

‎.agents/skills/skill-creator/SKILL.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ Follow Anthropic's skill authoring guidance from the Agent Skills best practices
3535
- Avoid vague descriptions such as "helps with docs" or "processes files".
3636
- Keep metadata concise because skill names and descriptions are always loaded; Anthropic's size guidance treats frontmatter as roughly 100 words.
3737

38+
Optional file routing fields:
39+
40+
- Use `paths` for Claude-style file matching. It may be a comma-separated glob string or a YAML list.
41+
- Add `globs` as a compatibility hint when a skill should trigger for file types across agent runtimes.
42+
- For cross-agent repo-local skills that should apply to TypeScript or JavaScript, include both:
43+
44+
```yaml
45+
paths:
46+
- '**/*.ts'
47+
- '**/*.tsx'
48+
- '**/*.js'
49+
- '**/*.jsx'
50+
globs: '*.ts,*.tsx,*.js,*.jsx'
51+
```
52+
53+
- Do not rely only on path metadata for Codex-style discovery; keep the `description` explicit about the file types and actions that should trigger the skill.
54+
3855
For this repo, prefer one or two short sentences, usually around 20-35 words. Do not compress a description so far that it becomes only a label; the agent still needs enough trigger context to choose the skill reliably.
3956

4057
Good pattern:

‎.agents/skills/tdd/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Prefer running only the tests affected by your changes during the Red-Green-Refa
5858
**Runner-specific guidance** — Read the relevant example file alongside this skill for detailed test modifiers, idioms, and runner-specific tips:
5959

6060
- **Vitest**: See `references/vitest-examples.md` — test modifiers (`it.todo`, `it.skip`, `it.fails`, etc.), `--changed` flag
61-
- **Rust (cargo test)**: Use focused runs like `cargo test <name>` and `cargo test -- --ignored`
61+
- **Rust (cargo test)**: See `references/rust-examples.md` — `#[ignore]`, `#[should_panic]`, filtering, doc tests
6262
- **Zig (zig test)**: Use `zig test <file> --test-filter <name>` for focused runs
6363

6464
For other runners, adapt the general patterns:

0 commit comments

Comments
 (0)