@cesarus777/opencode-cgp 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +131 -2
  3. package/dist/cli.js +1411 -0
  4. package/dist/core.js +1247 -0
  5. package/dist/index.js +2565 -0
  6. package/dist/rpc.js +332 -0
  7. package/dist/tui.js +2286 -0
  8. package/dist/types/adapter.d.ts +20 -0
  9. package/dist/types/chartreport.d.ts +14 -0
  10. package/dist/types/charts.d.ts +54 -0
  11. package/dist/types/config.d.ts +2 -0
  12. package/dist/types/core.d.ts +10 -0
  13. package/dist/types/estimate.d.ts +1 -0
  14. package/dist/types/family.d.ts +39 -0
  15. package/dist/types/history.d.ts +59 -0
  16. package/dist/types/historycharts.d.ts +17 -0
  17. package/dist/types/historyserver.d.ts +7 -0
  18. package/dist/types/index.d.ts +3 -0
  19. package/dist/types/monitor.d.ts +20 -0
  20. package/dist/types/performance.d.ts +57 -0
  21. package/dist/types/policy.d.ts +12 -0
  22. package/dist/types/recovery.d.ts +22 -0
  23. package/dist/types/rpc.d.ts +129 -0
  24. package/dist/types/scheduler.d.ts +18 -0
  25. package/dist/types/snapshot.d.ts +98 -0
  26. package/dist/types/snapshotserver.d.ts +7 -0
  27. package/dist/types/telemetry.d.ts +17 -0
  28. package/dist/types/tui/chartformat.d.ts +11 -0
  29. package/dist/types/tui/charts.d.ts +10 -0
  30. package/dist/types/tui/compact.d.ts +10 -0
  31. package/dist/types/tui/compactformat.d.ts +28 -0
  32. package/dist/types/tui/format.d.ts +8 -0
  33. package/dist/types/tui/inventory.d.ts +18 -0
  34. package/dist/types/tui/panel.d.ts +8 -0
  35. package/dist/types/tui/sidebar.d.ts +13 -0
  36. package/dist/types/tui/state.d.ts +30 -0
  37. package/dist/types/tui/transport.d.ts +40 -0
  38. package/dist/types/tui.d.ts +3 -0
  39. package/dist/types/types.d.ts +153 -0
  40. package/package.json +110 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cesarus777
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,132 @@
1
- # Temporary Holding Version
1
+ # OpenCode V2 Context Guard
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Configurable early compaction for root sessions and nested agents, using OpenCode's native durable compaction API. No model-limit changes, prompt injection, custom summaries, or task/workflow rewrites. Observe mode is the default.
4
+
5
+ Targets **`@opencode/plugin@2.0.29`**. The published SDK, official documentation, runner implementation, and upstream continuation test were inspected. The real 2.0.29 server and native TUI now pass [local verification with a synthetic provider](docs/validation/tui-2.0.29-2026-10-16.md); real-provider compaction and Superpowers validation remain unverified. See [compatibility](docs/compatibility.md).
6
+
7
+ ## Install
8
+
9
+ The intended npm distribution is **`@cesarus777/opencode-cgp`**. It becomes available after this repository's first publication. The unscoped `opencode-context-guard` name belongs to another project; do not install it expecting this plugin.
10
+
11
+ Minimum supported host: **OpenCode V2 2.0.29**. Only 2.0.29 has been tested; V1 is unsupported.
12
+
13
+ After this project's first publication, register the global package through the native manager:
14
+
15
+ ```sh
16
+ opencode plugin add @cesarus777/opencode-cgp@latest
17
+ ```
18
+
19
+ The CLI manages global package registration and installation. To register the plugin manually, add the following to **one** applicable `opencode.jsonc` instead. The host installs missing packages; a separate `npm install` in your project is unnecessary.
20
+
21
+ ```jsonc
22
+ {
23
+ "plugins": [
24
+ {
25
+ "package": "@cesarus777/opencode-cgp",
26
+ "options": {
27
+ "mode": "observe",
28
+ "defaults": {
29
+ "threshold": {
30
+ "type": "tokens",
31
+ "value": 131072,
32
+ },
33
+ },
34
+ },
35
+ },
36
+ ],
37
+ }
38
+ ```
39
+
40
+ Global configuration is `~/.config/opencode/opencode.jsonc` (or the OpenCode config directory selected by `XDG_CONFIG_HOME`). Project configuration is `./opencode.jsonc` or `./.opencode/opencode.jsonc`; relative plugin paths resolve from the containing configuration file. Plugin arrays accumulate across applicable configurations. Avoid duplicate registration through global/project configuration, discovery directories, or CLI registration. When adding options to a CLI-managed entry, edit that entry rather than adding another copy. The plugin never edits configuration itself.
41
+
42
+ Restart/reload OpenCode and look for `context-guard: observe; diagnostics ...`. Observe mode records policy decisions and never requests compaction; OpenCode's own automatic compaction remains active. Inspect diagnostics first, then set `mode` to `enforce` to admit native compaction at runtime step boundaries. The complete [example](examples/opencode.jsonc) includes model, role, and agent policies. **Last matching rule wins**; selectors within a rule are ANDed.
43
+
44
+ Pin the exact published version in the existing entry's `package` field or use the manager. Version `0.2.1` includes performance metrics, history charts, and CLI reports.
45
+
46
+ ```sh
47
+ opencode plugin add @cesarus777/opencode-cgp@0.2.1
48
+ ```
49
+
50
+ Manage the configured target with the [official V2 CLI](https://dev.opencode.ai/v2/docs/plugins):
51
+
52
+ ```sh
53
+ opencode plugin list
54
+ opencode plugin check
55
+ opencode plugin update @cesarus777/opencode-cgp@latest
56
+ opencode plugin remove @cesarus777/opencode-cgp@latest
57
+ ```
58
+
59
+ Use the actual configured target when it differs. Exact versions and full Git commit hashes stay pinned and are skipped by updates. To change a pin, edit the existing `package` field while preserving its options, or remove the old CLI target and add the desired version. Manually configured entries are removed by deleting that entry from its configuration file. Remote TUI clients also need the package registered in their client `cli.json`, in addition to server registration; see [client configuration](examples/cli.jsonc).
60
+
61
+ For development or unreleased Git revisions, V2 supports npm-compatible Git targets:
62
+
63
+ ```sh
64
+ opencode plugin add 'github:cesarus777/opencode-cgp#master'
65
+ ```
66
+
67
+ Replace `master` with a version tag or full commit hash to pin a revision. Git installs require Bun and development dependencies to run `prepare`; unlike npm artifacts they build from source. Prefer an exact published prerelease such as `@cesarus777/opencode-cgp@0.2.0-beta.1` after publication. Git preparation has not been verified with the real plugin manager; use a built local checkout if it fails.
68
+
69
+ Contributors can build and register a checkout without publishing:
70
+
71
+ ```sh
72
+ git clone https://github.com/cesarus777/opencode-cgp.git
73
+ cd opencode-cgp
74
+ bun install --frozen-lockfile
75
+ bun run typecheck
76
+ bun test
77
+ bun run build
78
+ bun run test:package
79
+ ```
80
+
81
+ Use `{"plugins":[{"package":"/absolute/path/to/opencode-cgp","options":{"mode":"observe"}}]}` in a disposable project's `opencode.jsonc`. Local paths are configured directly; `plugin add` does not accept local paths or tarballs. Root checkout wrappers continue to support the host's source loader. The npm package instead uses built JavaScript exports. See [build and release instructions](docs/releasing.md).
82
+
83
+ If loading fails, run `opencode plugin list` and `opencode plugin check`, confirm V2 2.0.29 and the correct configuration scope, and inspect OpenCode's startup logs. A missing `@opencode/plugin` or client module indicates an incomplete package installation: remove/re-add the configured package using the manager. TUI-only missing `solid-js`/OpenTUI modules usually indicate an unsupported host or loading the TUI outside its host runtime. These optional peers are supplied by the native TUI and must share its runtime instances; do not bundle private renderer copies. Standalone renderer tests need the pinned peers and public runtime preload described in [testing](docs/testing.md). Never reset persisted safety state merely to fix loading. For remote clients, check both client installation and server registration in the session's owning location.
84
+
85
+ ## Thresholds
86
+
87
+ | Type | Example | Trigger |
88
+ | ----------- | -------- | ------------------------------------------ |
89
+ | `tokens` | `131072` | Prompt tokens ≥ value |
90
+ | `ratio` | `0.25` | Prompt tokens ≥ ceil(usable input × value) |
91
+ | `remaining` | `32768` | Usable input − prompt tokens < value |
92
+
93
+ Usable input is `min(positive limit.input, limit.context − output reservation)` when both constraints are known. Without an explicit input limit, the context-minus-reservation budget is used. Reservation defaults to positive `limit.output`; `safety.outputReserveTokens` can explicitly override this accounting assumption. Unknown limits are not fabricated. Absolute thresholds still work without model metadata; relative decisions abstain. Model metadata is never mutated.
94
+
95
+ A provider measurement refers to its **completed request**, not the next assembled prompt. Prompt tokens include uncached input plus cache reads and writes; output and reasoning are separate. Approximate fallback measurements are visibly labeled. See [configuration and accounting](docs/configuration.md).
96
+
97
+ ## Native TUI
98
+
99
+ Open `/context-guard` or **Open Context Guard** in the command palette for current prompt occupancy, effective input capacity, trigger/headroom, measurement freshness, compaction phase and nested agent status. A native sidebar section appears where the host mounts its sidebar; the panel also works on child routes and narrow terminals. Estimates, unknown values, stale measurements and uncertain compaction remain explicit.
100
+
101
+ The TUI reads bounded snapshots over public OpenCode RPC, including when telemetry is disabled. Remote clients also need a client-side package registration in `cli.json`; see [the example](examples/cli.jsonc) and [controls, installation and bounds](docs/configuration.md#native-context-guard-view). It targets the inspected 2.0.29 API. The actual local host passes wide root, child and narrow grandchild panel checks. Use **Context Guard: open panel** above a child composer, where the host hides its usual prompt and palette shortcut. tmux, Neovim and cross-machine remote validation remain open.
102
+
103
+ Per-session live TPS, average model throughput, average session throughput and observed TTFT appear in the selected session and child rows. Live TPS and TTFT use `~` to mark estimates; completed rates use provider totals. Configurable local history defaults to seven days with a 64 MiB total cap, independently of diagnostic telemetry. See [metric definitions and history exports](docs/configuration.md#live-performance-and-retained-history).
104
+
105
+ Press **c** in the Context Guard panel for retained-history terminal charts; use **[ / ]** to change the time window and **Up / Down** to select an agent instance. Generate a self-contained interactive browser report with `bun run charts /absolute/history-directory --output report.html`. It includes filters, UTC timestamp tooltips, request statistics and SVG export. Installed packages expose the same command as `context-guard charts ...` (Bun required). See [chart controls and limits](docs/configuration.md#performance-charts).
106
+
107
+ ## Inspect
108
+
109
+ The startup message reports a location-specific JSONL file under `$XDG_STATE_HOME/opencode-context-guard/` (default `~/.local/state/opencode-context-guard/`). A custom absolute `telemetry.path` is supported. Use a distinct file per plugin location/process.
110
+
111
+ ```sh
112
+ bun run inspect /absolute/path/from/startup.jsonl --json
113
+ bun run inspect /absolute/path/from/startup.jsonl --history
114
+ ```
115
+
116
+ Latest retained session records show parent ID, agent/model, measurement scope/confidence, current and cumulative usage, model limits, effective threshold/percentage, policy, peak, compaction count, lifecycle state, failures, and timestamps. History includes degraded-capability and capacity events. Logs rotate at 8 MiB by default, retaining one previous file; this is a bounded diagnostic history, not an audit database. The CLI reads files and does not contact the model.
117
+
118
+ ## Safety and validation
119
+
120
+ Compaction runs at a runtime step boundary, including active subagent continuations. Admission is asynchronous: the plugin cannot promise a hard ceiling on the immediately following prompt or interrupt a long-running tool. It does not wait for an agent's task to finish.
121
+
122
+ Cooldown, hysteresis, deduplication, failure limits, persisted safety state, and completion-event tracking prevent repeated summaries. An accepted request is never labeled completed. Ambiguous admission, missing completion, and interrupted-plugin recovery suppress further automatic requests until explicit lifecycle evidence resolves uncertainty. Native manual compaction remains available. No private/experimental API is used.
123
+
124
+ ```sh
125
+ bun run typecheck
126
+ bun test
127
+ bun run test:live # explicitly SKIPs without the required server environment
128
+ ```
129
+
130
+ Tests cover the core, mocked V2 events for concurrent nested subagents, restart safety, telemetry rotation, and CLI reading. [Live test instructions](docs/testing.md) describe the optional disposable-session check against an existing V2 host and a local OpenAI-compatible endpoint; no model download is required.
131
+
132
+ Further reading: [API investigation](docs/reconnaissance.md), [architecture](docs/architecture.md), [Budget Controller extension](docs/budget-controller.md), [compatibility and limitations](docs/compatibility.md).