Skip to content

Commit dca966f

Browse files
committed
refactor: clean context tree architecture for v0.2.2
1 parent 829bb33 commit dca966f

78 files changed

Lines changed: 4615 additions & 3086 deletions

Some content is hidden

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

AGENTS.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Behavior implemented:
8080
- `agent:start`: pathless hook selected by scope when prompt references paths such as `@src/index.ts`.
8181
- `tool:read`: appends context bundle to read tool results.
8282
- `tool:edit` / `tool:write`: preflight injects context once, blocks initial mutation, then allows retry.
83-
- `session:spawn`: used by `/ct-new <path> [prompt]`.
83+
- `session:spawn`: schema/config concept for future config-driven session workflows; no slash command currently consumes it.
8484
- `subagent:spawn`: schema/config concept; runner interop still planned.
8585

8686
## Design principles
@@ -119,7 +119,6 @@ Implemented commands:
119119
/ct-cache-list
120120
/ct-cache-refresh <path>
121121
/ct-tui on|off
122-
/ct-new <path> [prompt]
123122
/ct-subagent <path> <task>
124123
```
125124

CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Version
66

77
## [Unreleased]
88

9+
## [0.2.2] - 2026-04-30
10+
11+
### Added
12+
13+
- Added user-global Context Tree config loading from `~/.pi/CONTEXT.json`, with `PI_CONTEXT_TREE_GLOBAL` override for tests and custom setups.
14+
15+
### Changed
16+
17+
- Cleaned up the code architecture by splitting runtime, command, bundle, TUI, and init-wizard responsibilities into focused modules.
18+
- Split `/ct-*` command registration into dedicated command groups for status, explain, cache, upgrade, init, and subagent concerns.
19+
- Updated public documentation to reflect the current architecture and command surface.
20+
21+
### Removed
22+
23+
- Removed `/ct-new`; session workflows are now left to Context Tree configuration and future config-driven integrations.
24+
925
## [0.2.1] - 2026-04-29
1026

1127
### Added
@@ -66,7 +82,8 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Version
6682

6783
- Synced `pnpm-lock.yaml` for reproducible installs.
6884

69-
[Unreleased]: https://github.com/ZEDIUM-Off/pi-context-tree/compare/v0.2.1...HEAD
85+
[Unreleased]: https://github.com/ZEDIUM-Off/pi-context-tree/compare/v0.2.2...HEAD
86+
[0.2.2]: https://github.com/ZEDIUM-Off/pi-context-tree/compare/v0.2.1...v0.2.2
7087
[0.2.1]: https://github.com/ZEDIUM-Off/pi-context-tree/compare/v0.2.0...v0.2.1
7188
[0.2.0]: https://github.com/ZEDIUM-Off/pi-context-tree/compare/v0.1.0...v0.2.0
7289
[0.1.0]: https://github.com/ZEDIUM-Off/pi-context-tree/releases/tag/v0.1.0

CONTEXT.json

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,16 @@
7070
},
7171
{
7272
"on": "agent:start",
73-
"inject": [
74-
"./src/context-schema.ts"
75-
]
73+
"inject": ["./src/context-schema.ts"]
7674
},
7775
{
7876
"on": "tool:read",
79-
"match": [
80-
"CONTEXT.json",
81-
"**/CONTEXT.json"
82-
],
83-
"inject": [
84-
"./src/context-schema.ts"
85-
]
77+
"match": ["CONTEXT.json", "**/CONTEXT.json"],
78+
"inject": ["./src/context-schema.ts"]
8679
},
8780
{
8881
"on": "tool:edit",
89-
"match": [
90-
"CONTEXT.json",
91-
"**/CONTEXT.json"
92-
],
82+
"match": ["CONTEXT.json", "**/CONTEXT.json"],
9383
"inject": [
9484
{
9585
"type": "file",
@@ -104,10 +94,7 @@
10494
},
10595
{
10696
"on": "tool:write",
107-
"match": [
108-
"CONTEXT.json",
109-
"**/CONTEXT.json"
110-
],
97+
"match": ["CONTEXT.json", "**/CONTEXT.json"],
11198
"inject": [
11299
{
113100
"type": "file",

CONTRIBUTING.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,18 @@ src/match.ts glob/operation matching
141141
src/normalize.ts inject/default normalization
142142
src/extract.ts section/line/marker extraction
143143
src/cache.ts URL cache
144-
src/bundle.ts bundle build/render/hash
144+
src/bundle.ts compatibility facade for bundle exports
145+
src/bundle/ explain/build/render/stability modules
145146
src/permissions.ts scope guard decisions
146-
src/tui.ts status/widget rendering
147-
src/sessions.ts scoped session command helpers
148-
src/subagents.ts pi-subagents interop
149-
src/commands.ts slash command handlers
150-
src/hooks.ts Pi event hooks
147+
src/tui.ts compatibility facade for TUI exports
148+
src/tui/ status/widget/detail rendering and summaries
149+
src/runtime/ Pi lifecycle and tool hooks
150+
src/commands/ slash command groups
151+
src/subagents.ts pi-subagents interop placeholder
151152
src/index.ts extension composition only
152153
```
153154

154-
Current code is not fully split yet; see `docs/architecture-review.md`.
155+
The runtime is split into focused modules; see `docs/architecture-review.md` for current boundaries and planned integration points.
155156

156157
## Pull request checklist
157158

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ hook + optional target path
1414
→ matching hooks[] entries
1515
→ mode-specific inline excerpts or references
1616
→ bounded context bundle
17-
→ Pi turn, read result, edit preflight, or scoped session
17+
→ Pi turn, read result, or edit preflight
1818
```
1919

2020
## Current status
@@ -27,13 +27,13 @@ Implemented MVP:
2727
- hook/match compatibility validation: path-aware hooks require `match[]`, pathless hooks forbid it;
2828
- hooks: `session:start`, `agent:start`, `tool:*`, `session:spawn`, `subagent:spawn`;
2929
- file and URL inject sources;
30+
- user-global injection config via `~/.pi/CONTEXT.json` or `PI_CONTEXT_TREE_GLOBAL`;
3031
- URL cache under `.pi/context-tree/cache/urls`;
3132
- markdown section extraction, line ranges, markers, and annotated segments;
3233
- bundle hashing and dedupe;
3334
- read-result context injection;
3435
- edit/write preflight injection;
3536
- self-read skip to avoid reinjecting the file being read;
36-
- scoped session command;
3737
- scope guard fallback;
3838
- structured TUI status/widget;
3939
- unit tests for schema, matching, extraction, cache, bundles, permissions.
@@ -46,7 +46,7 @@ Minimal example:
4646

4747
```json
4848
{
49-
"$schema": "https://raw.githubusercontent.com/ZEDIUM-Off/pi-context-tree/v0.2.0/schemas/context.schema.json",
49+
"$schema": "https://raw.githubusercontent.com/ZEDIUM-Off/pi-context-tree/v0.2.2/schemas/context.schema.json",
5050
"hooks": [
5151
{
5252
"on": "tool:read",
@@ -103,7 +103,6 @@ See [`docs/schema.md`](docs/schema.md) for full schema field behavior and best p
103103
/ct-tui on|off toggle Context Tree widget display only
104104
/ct-init [--resume] initialize editable Context Tree config for current codebase
105105
/ct-init-review <proposal> review agent proposal inside current init flow
106-
/ct-new <path> [prompt] create new Pi session seeded with session:spawn bundle
107106
/ct-subagent <path> <task> planned subagent handoff via subagent:spawn
108107
```
109108

@@ -146,6 +145,16 @@ Disable extension runtime:
146145
/ct-toggle off
147146
```
148147

148+
## Global config
149+
150+
Context Tree also loads an optional user-global config before project scopes:
151+
152+
```text
153+
~/.pi/CONTEXT.json
154+
```
155+
156+
Set `PI_CONTEXT_TREE_GLOBAL=/path/to/CONTEXT.json` to override the location. File sources in the global config resolve relative to the global config file; path-aware matches are evaluated against repository-relative target paths.
157+
149158
## Changelog
150159

151160
Release notes live in [`CHANGELOG.md`](CHANGELOG.md). GitHub releases mirror the same version sections.
@@ -219,7 +228,7 @@ For a Git URL, this clones or fast-forwards the target repository under `.test-w
219228

220229
This repo uses Context Tree to develop itself:
221230

222-
- root `CONTEXT.json` injects README only for scoped session context;
231+
- root `CONTEXT.json` injects README and package metadata at session startup;
223232
- `src/CONTEXT.json` injects Pi docs for extension runtime files and implementation-plan sections for core files;
224233
- `scripts/CONTEXT.json` injects the canonical schema for schema generation;
225234
- `test/CONTEXT.json` injects test strategy sections.

docs/architecture-review.md

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ Current implementation works, but several responsibilities still live together i
99
## Current state
1010

1111
```text
12-
src/context-schema.ts schema and inferred TS types
13-
src/context-tree.ts scan, match, normalize, extraction, URL cache, bundle rendering
12+
src/context-schema.ts explicit schema compatibility exports
13+
src/context-tree.ts explicit public API compatibility exports
14+
src/public-api.ts curated public API surface
15+
src/runtime/ Pi extension lifecycle and tool hooks
16+
src/commands/ slash command registration and handlers
17+
src/bundle/ explain/build/render/stability bundle modules
18+
src/tui/ TUI state summaries, widgets, detail panel, formatting
19+
src/init/ init wizard phase machine, prompts, submit/review flow
1420
src/permissions.ts scope guard decision logic
15-
src/index.ts Pi extension hooks, commands, TUI state, session command
1621
```
1722

18-
This is acceptable for MVP, but `context-tree.ts` and `index.ts` are too broad for long-term maintenance.
23+
The large legacy entrypoints have been split. `context-tree.ts` is now a compatibility facade over the curated `public-api.ts` surface rather than a broad export of internals.
1924

2025
## Target architecture
2126

@@ -39,25 +44,34 @@ src/cache.ts
3944
URL cache keying, TTL, stale fallback, metadata read/write.
4045
4146
src/bundle.ts
42-
Load sources, apply self-read skip, build bundle hash, render bundle markdown.
47+
Compatibility facade for public bundle exports.
48+
49+
src/bundle/
50+
Explain hooks, load sources, apply self-read skip, build bundle hash, render bundle markdown.
4351
4452
src/permissions.ts
4553
Pure scope guard decision engine.
4654
4755
src/tui.ts
48-
TUI state, compact widget rendering, status rendering, on-demand detail text.
56+
Compatibility facade for public TUI exports.
4957
50-
src/sessions.ts
51-
/context-tree new helpers, custom session entries/messages.
58+
src/tui/
59+
TUI state, compact widget rendering, status rendering, on-demand detail text.
5260
5361
src/subagents.ts
54-
Resolve subagent_spawn context and interop with pi-subagents.
62+
Resolve subagent:spawn context and interop with pi-subagents.
63+
64+
src/commands/register.ts
65+
Slash command composition only.
66+
67+
src/commands/*-commands.ts
68+
Slash command handlers grouped by status, explain, cache, upgrade, init, and subagent concerns.
5569
56-
src/commands.ts
57-
Slash command parsing and handlers.
70+
src/runtime/lifecycle.ts
71+
Pi lifecycle hooks: session_start, before_agent_start, turn_start.
5872
59-
src/hooks.ts
60-
Pi event hooks: before_agent_start, tool_call, tool_result, turn_start.
73+
src/runtime/tool-hooks.ts
74+
Pi tool hooks: tool_call, tool_result.
6175
6276
src/index.ts
6377
Extension composition only: register hooks and commands.
@@ -76,16 +90,15 @@ Move pure functions without behavior change:
7690
- URL cache → `cache.ts`
7791
- `buildBundle`, `renderBundle`, `formatExplain``bundle.ts`
7892

79-
Keep compatibility exports from `context-tree.ts` during transition.
93+
Keep `context-tree.ts` as a curated compatibility facade over `public-api.ts` during transition.
8094

8195
### Phase 2 — Pi integration split
8296

8397
Move side-effectful code:
8498

8599
- TUI state/rendering → `tui.ts`
86-
- command handlers → `commands.ts`
87-
- hook handlers → `hooks.ts`
88-
- session creation → `sessions.ts`
100+
- command handlers → grouped `commands/*-commands.ts` modules
101+
- hook handlers → `runtime/lifecycle.ts` and `runtime/tool-hooks.ts`
89102

90103
`index.ts` should become short.
91104

@@ -147,13 +160,14 @@ Implemented:
147160
- bundle hash/render;
148161
- read injection;
149162
- edit/write preflight;
150-
- scoped session creation;
163+
- user-global config loading via `~/.pi/CONTEXT.json` / `PI_CONTEXT_TREE_GLOBAL`;
151164
- basic scope guard fallback;
152165
- structured compact TUI with on-demand details;
153166
- tests for core behavior.
154167

155168
Partial or planned:
156169

170+
- `session:spawn` remains a schema hook for future config-driven session workflows; the old `/ct-new` command has been removed;
157171
- defaults cascade is currently basic, not deeply modeled/tested across all fields;
158172
- token budget is represented but not fully enforced;
159173
- `context` hook is not used yet;
@@ -165,9 +179,8 @@ Partial or planned:
165179

166180
## Release guidance
167181

168-
For a first public release, prefer transparency:
182+
For public releases, prefer transparency:
169183

170-
- call the release `0.1.0`;
171-
- describe current MVP clearly;
184+
- describe current runtime and command surface exactly;
172185
- mark subagents/guardrails/config-maintenance as planned;
173186
- keep architecture-review doc visible to contributors.

docs/implementation.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ Context Tree commands use `/ct-*` for discoverability:
252252
/ct-fetch <path> compile bundle and fetch/cache inline URLs
253253
/ct-cache-list show URL cache directory
254254
/ct-cache-refresh <path> refresh cached URL sources for target
255+
/ct-toggle on|off toggle entire Context Tree extension runtime
255256
/ct-tui on|off toggle Context Tree widget
256-
/ct-new <path> [prompt] create new Pi session seeded with session:spawn bundle
257+
/ct-init [--resume] initialize editable Context Tree config
258+
/ct-init-review <proposal> review agent proposal inside current init flow
257259
/ct-subagent <path> <task> planned subagent handoff via subagent:spawn
258260
```
259261

@@ -262,6 +264,7 @@ Context Tree commands use `/ct-*` for discoverability:
262264
- Prefer `mode: { "type": "ref" }` for broad docs.
263265
- Use `inline`, `sections`, `markers`, `lines`, or `segments` for invariants needed before edit/write.
264266
- Keep `session:start` deliberate: inline core startup docs only when always useful.
267+
- Keep user-global `~/.pi/CONTEXT.json` config small, reference-first, and portable across repositories.
265268
- Put domain rules near domain scope.
266269
- Use exclusions for tests/generated files.
267270
- Do not inject `AGENTS.md`; Pi already loads it.

docs/schema.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ hook + optional target path
4545

4646
Scope is implicit: `dirname(CONTEXT.json)`.
4747

48+
A user-global config can also be placed at:
49+
50+
```text
51+
~/.pi/CONTEXT.json
52+
```
53+
54+
It is loaded before project scopes for every repository. Its file sources resolve relative to that global config directory. Path-aware `match[]` patterns are evaluated against the target path relative to the current repository root. Set `PI_CONTEXT_TREE_GLOBAL=/path/to/CONTEXT.json` to override the default location, including in tests.
55+
4856
## stability
4957

5058
`stability` is a top-level scope signal for AI-assisted editing. It tells agents whether code under this scope is trusted reference, stable working code, active work, prototype, deprecated, or generated. It is not an edit policy engine; detailed rules still belong in hook-injected `.md` files.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pi-context-tree",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Pi extension for deterministic path-scoped contextualization via CONTEXT.json files.",
55
"repository": {
66
"type": "git",

schemas/context.schema.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,13 @@
835835
],
836836
"additionalProperties": false
837837
},
838-
"permissions": {},
838+
"permissions": {
839+
"type": "object",
840+
"properties": {
841+
"scopeGuard": {}
842+
},
843+
"additionalProperties": {}
844+
},
839845
"subagents": {}
840846
},
841847
"required": [

0 commit comments

Comments
 (0)