geneseed 4.32.0 → 5.0.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.
- package/CHANGELOG.md +101 -0
- package/DESIGN.md +2 -2
- package/README.md +6 -5
- package/SHIPPED.md +4 -3
- package/adapters/bob/README.md +6 -5
- package/adapters/claude-code/README.md +4 -3
- package/adapters/claude-code/skills/geneseed/SKILL.md +1 -1
- package/adapters/openclaude/README.md +4 -3
- package/adapters/opencode/GLOBAL-HARNESS-SPEC.md +3 -1
- package/adapters/opencode/HOW-OPENCODE-LOADS.md +2 -1
- package/adapters/opencode/README.md +18 -18
- package/adapters/opencode/plugins/geneseed-activity.js +57 -1
- package/adapters/opencode/plugins/geneseed-context.js +85 -18
- package/adapters/opencode/plugins/geneseed-guard.js +437 -76
- package/adapters/opencode/plugins/geneseed-learn.js +97 -23
- package/bin/geneseed-cli.mjs +17 -25
- package/bin/geneseed-hook.mjs +23 -8
- package/docs/DESIGN.md +175 -0
- package/docs/PRD.md +111 -0
- package/docs/README.md +3 -2
- package/docs/architecture.md +145 -0
- package/docs/concepts/agents.md +1 -1
- package/docs/concepts/collaboration.md +1 -1
- package/docs/concepts/enforcement-table.md +1 -1
- package/docs/concepts/exclusions.md +5 -5
- package/docs/concepts/footprint.md +1 -1
- package/docs/concepts/foreman-mode.md +2 -2
- package/docs/concepts/hooks.md +22 -8
- package/docs/concepts/host-matrix.md +2 -2
- package/docs/concepts/hosts.md +1 -1
- package/docs/concepts/kits.md +3 -1
- package/docs/concepts/ownership.md +1 -1
- package/docs/concepts/rules.md +15 -2
- package/docs/concepts/shared-tier.md +73 -0
- package/docs/concepts/skills.md +2 -2
- package/docs/concepts/themes.md +1 -1
- package/docs/extending.md +13 -12
- package/docs/guides/install-claude-code.md +1 -1
- package/docs/guides/install-opencode.md +1 -1
- package/docs/guides/migrate.md +8 -4
- package/docs/guides/project-context.md +2 -2
- package/docs/guides/setup-footprint.md +1 -1
- package/docs/guides/uninstall.md +2 -1
- package/docs/guides/upgrade-to-5.md +118 -0
- package/docs/guides/upgrade.md +21 -9
- package/docs/guides/wiki.md +3 -3
- package/docs/reference/env-context.md +5 -3
- package/docs/reference/environment.md +1 -1
- package/docs/reference/glossary.md +2 -0
- package/docs/reference/opencode-plugins.md +3 -3
- package/docs/reference/plugin-activity.md +1 -1
- package/docs/reference/plugin-context.md +1 -0
- package/docs/reference/plugin-guard.md +14 -6
- package/docs/reference/plugin-learn.md +13 -6
- package/docs/reference/troubleshoot-hooks.md +3 -3
- package/docs/reference/troubleshoot-update.md +14 -0
- package/docs/token-footprint.md +4 -3
- package/docs/understand/a-day.md +2 -2
- package/docs/understand/design-choices-install.md +2 -1
- package/docs/understand/enforced-vs-asked.md +1 -1
- package/docs/understand/machine-bob.md +2 -0
- package/docs/understand/machine-claude.md +7 -5
- package/docs/understand/machine-opencode.md +7 -5
- package/docs/understand/on-your-machine.md +4 -0
- package/docs/understand/take-it-out.md +1 -1
- package/docs/web-ui.md +135 -70
- package/harness.config.json +1 -1
- package/js/README.md +12 -7
- package/js/build/args.mjs +10 -8
- package/js/build/bundle.mjs +11 -13
- package/js/build/driver.mjs +66 -65
- package/js/build/emit-claude.mjs +22 -16
- package/js/build/emit-common.mjs +4 -5
- package/js/build/emit-opencode.mjs +22 -22
- package/js/build/fold.mjs +515 -0
- package/js/build/generate.mjs +41 -52
- package/js/build/render.mjs +115 -29
- package/js/build/rule-decide.mjs +273 -0
- package/js/build/source.mjs +4 -1
- package/js/build/stubs.mjs +70 -9
- package/js/build/version.mjs +10 -10
- package/js/cli-table.json +23 -83
- package/js/hosts/activity.mjs +2 -2
- package/js/hosts/geneseed-dir.mjs +1 -1
- package/js/hosts/hooks-context.mjs +389 -99
- package/js/hosts/hooks-learn.mjs +38 -7
- package/js/hosts/hooks-prims.mjs +4 -2
- package/js/hosts/hooks.mjs +114 -39
- package/js/hosts/hosts.mjs +150 -51
- package/js/hosts/installs.mjs +59 -171
- package/js/hosts/link.mjs +3 -5
- package/js/hosts/mcp.mjs +1 -1
- package/js/hosts/memory-files.mjs +219 -17
- package/js/hosts/ownership-gate.mjs +1 -1
- package/js/hosts/settings.mjs +18 -13
- package/js/hosts/shim.mjs +4 -33
- package/js/hosts/store-watch.mjs +191 -0
- package/js/inspect/checks-authoring.mjs +3 -3
- package/js/inspect/checks-build.mjs +45 -18
- package/js/inspect/checks-counts.mjs +4 -4
- package/js/inspect/checks-repo.mjs +13 -14
- package/js/inspect/cost.mjs +24 -2
- package/js/inspect/diff.mjs +123 -123
- package/js/inspect/doctor.mjs +22 -27
- package/js/inspect/excludes.mjs +181 -62
- package/js/inspect/inventory.mjs +22 -27
- package/js/inspect/registry.mjs +110 -32
- package/js/inspect/scan.mjs +5 -5
- package/js/inspect/status.mjs +141 -119
- package/js/inspect/validate.mjs +22 -24
- package/js/lib/fs.mjs +47 -50
- package/js/lib/json.mjs +10 -17
- package/js/lib/machine-dir.mjs +73 -0
- package/js/lib/paths.mjs +35 -33
- package/js/lib/proc.mjs +1 -1
- package/js/lib/prompt.mjs +19 -27
- package/js/lib/text.mjs +8 -12
- package/js/lib/udiff.mjs +11 -15
- package/js/loop/catalog.mjs +2 -3
- package/js/maintain/memory.mjs +17 -17
- package/js/maintain/migrate.mjs +120 -92
- package/js/maintain/setup.mjs +43 -32
- package/js/maintain/uninstall.mjs +43 -61
- package/js/maintain/update.mjs +200 -93
- package/js/ui/anim.mjs +21 -35
- package/js/ui/cli.mjs +36 -58
- package/js/ui/menu.mjs +22 -45
- package/js/ui/tui.mjs +27 -67
- package/js/web/actions.mjs +34 -34
- package/js/web/api.mjs +142 -79
- package/js/web/catalog.mjs +175 -46
- package/js/web/docs.mjs +33 -12
- package/js/web/doctor-worker.mjs +14 -0
- package/js/web/handler.mjs +16 -5
- package/js/web/routes.mjs +24 -22
- package/js/web/state.mjs +78 -19
- package/js/web/update-check.mjs +27 -12
- package/js/web/user-files.mjs +76 -104
- package/package.json +7 -4
- package/registry.json +48 -55
- package/src/AGENT.md.tmpl +13 -14
- package/src/agents/_self-improvement.md +1 -1
- package/src/agents/_template.md +1 -1
- package/src/agents/advocate.md +1 -1
- package/src/agents/architect.md +1 -1
- package/src/agents/developer.md +1 -1
- package/src/agents/devops-engineer.md +1 -1
- package/src/agents/docker-expert.md +1 -1
- package/src/agents/docs.md +1 -1
- package/src/agents/empiricist.md +1 -1
- package/src/agents/explorer.md +1 -1
- package/src/agents/framer.md +1 -1
- package/src/agents/historian.md +1 -1
- package/src/agents/kubernetes-specialist.md +1 -1
- package/src/agents/memory-curator.md +2 -2
- package/src/agents/operator.md +1 -1
- package/src/agents/performance.md +1 -1
- package/src/agents/pragmatist.md +1 -1
- package/src/agents/react-engineer.md +1 -1
- package/src/agents/react-reviewer.md +1 -1
- package/src/agents/researcher.md +1 -1
- package/src/agents/reviewer.md +1 -1
- package/src/agents/security.md +1 -1
- package/src/agents/skeptic.md +1 -1
- package/src/agents/spring-boot-engineer.md +1 -1
- package/src/agents/spring-reviewer.md +1 -1
- package/src/agents/spring-security.md +1 -1
- package/src/agents/steward.md +1 -1
- package/src/agents/tester.md +1 -1
- package/src/agents/user-advocate.md +1 -1
- package/src/agents/visionary.md +1 -1
- package/src/memory/README.md +1 -1
- package/src/skills/_self-improvement.md +2 -2
- package/src/skills/daydream/SKILL.md +2 -2
- package/src/skills/daydream/instructions.md +1 -1
- package/src/skills/geneseed.md +1 -1
- package/src/skills/learn-mode.md +1 -2
- package/src/skills/profile.md +2 -2
- package/src/skills/rule/approve.md +75 -0
- package/src/skills/rule/consolidate.md +3 -3
- package/src/skills/rule.md +14 -5
- package/src/skills/skill-forge.md +1 -1
- package/src/skills/teach.md +29 -12
- package/src/skills/wiki.md +1 -1
- package/themes/_TEMPLATE.json +1 -2
- package/themes/biker.json +1 -2
- package/themes/commentator.json +1 -2
- package/themes/cyberpunk.json +1 -2
- package/themes/gamer.json +1 -2
- package/themes/imperial.json +1 -2
- package/themes/joker.json +1 -2
- package/themes/marvin.json +1 -2
- package/themes/mean.json +1 -2
- package/themes/military.json +1 -2
- package/themes/neutral.json +1 -2
- package/themes/pirate.json +1 -2
- package/themes/sports.json +1 -2
- package/themes/verstappen.json +1 -2
- package/themes/wizard.json +1 -2
- package/web/dist/assets/Activity-Ci4PZco3.js +1 -0
- package/web/dist/assets/ActivityDetail-C8hroCND.js +1 -0
- package/web/dist/assets/AgentGlyph-k6o3gCM1.js +1 -0
- package/web/dist/assets/Bonsai-C1iBAt4v.js +1 -0
- package/web/dist/assets/{CopyButton-C5Li6awl.js → CopyButton-DJ3a4N88.js} +1 -1
- package/web/dist/assets/Dashboard-C_On4KXw.js +2 -0
- package/web/dist/assets/Docs-Kffdg5uV.js +5 -0
- package/web/dist/assets/GreenhouseView-k56_cBSF.js +1 -0
- package/web/dist/assets/Installs-DtEKLC9h.js +3 -0
- package/web/dist/assets/JournalView-RQnk5l7I.js +1 -0
- package/web/dist/assets/Kits-CXkEBpnf.js +1 -0
- package/web/dist/assets/Laws-DhhIVqmh.js +3 -0
- package/web/dist/assets/Library-CAUiEoJo.js +3 -0
- package/web/dist/assets/{Loops-CzitjxGV.js → Loops-LKD8mF4E.js} +2 -2
- package/web/dist/assets/{Markdown-C4S4tTA7.js → Markdown-0ORz7tAc.js} +21 -21
- package/web/dist/assets/OperatorHudView-CM5jNPwL.js +1 -0
- package/web/dist/assets/Personal-B6Uoly3C.js +2 -0
- package/web/dist/assets/{RailCats-D5weYPCS.js → RailCats-DQxxYBEP.js} +1 -1
- package/web/dist/assets/Settings-BTe6-wda.js +1 -0
- package/web/dist/assets/Tabs-CbdDWTp_.js +1 -0
- package/web/dist/assets/api-D_r0Jtla.js +1 -0
- package/web/dist/assets/controls-D4wmg_pl.js +1 -0
- package/web/dist/assets/index-HfGmYXab.js +3 -0
- package/web/dist/assets/index-ao3M-Fhm.css +1 -0
- package/web/dist/assets/motion-RlUl1osO.js +1 -0
- package/web/dist/assets/{sections-ChwaobfW.js → sections-CtFz7HFH.js} +1 -1
- package/web/dist/index.html +5 -5
- package/src/skills/quiz.md +0 -44
- package/web/dist/assets/Activity-Bjy64wHC.js +0 -1
- package/web/dist/assets/ActivityDetail-B0AaipP8.js +0 -1
- package/web/dist/assets/Bonsai-CJniRqaH.js +0 -1
- package/web/dist/assets/Dashboard-B-6DTmae.js +0 -2
- package/web/dist/assets/Docs-LRc30XL9.js +0 -5
- package/web/dist/assets/GreenhouseView-CcoYlaWo.js +0 -1
- package/web/dist/assets/Installs-dp1epTcE.js +0 -3
- package/web/dist/assets/JournalView-nFP4poCf.js +0 -1
- package/web/dist/assets/KitApply-DwyZUj57.js +0 -1
- package/web/dist/assets/Kits-C0IEdZXe.js +0 -1
- package/web/dist/assets/Laws-CkykmQWu.js +0 -3
- package/web/dist/assets/Library-Guh9HBZU.js +0 -2
- package/web/dist/assets/OperatorHudView-DLGNulIY.js +0 -1
- package/web/dist/assets/Personal-BMtberL8.js +0 -2
- package/web/dist/assets/Tabs-uYJqfuQt.js +0 -1
- package/web/dist/assets/api-BxjLd7Qd.js +0 -1
- package/web/dist/assets/index-DA7BiaE5.js +0 -3
- package/web/dist/assets/index-dbbDGFDq.css +0 -1
- package/web/dist/assets/motion-Bccx3zE8.js +0 -1
|
@@ -3,13 +3,17 @@
|
|
|
3
3
|
* docs. Runs once per session, so it is loaded only when the hook entry dispatches it; see
|
|
4
4
|
* `js/hosts/hooks.mjs`'s header for the contract every hook verb holds.
|
|
5
5
|
*/
|
|
6
|
-
import { readFileSync } from 'node:fs';
|
|
6
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { createHash } from 'node:crypto';
|
|
9
9
|
import { readText, printOut as out, printErr as err, withDiscardableStderr } from '../lib/fs.mjs';
|
|
10
|
+
import { readJsonc } from '../lib/json.mjs';
|
|
11
|
+
import { LEGACY_WIKI_SEED, WIKI_NAMES, sharedPaths } from '../lib/machine-dir.mjs';
|
|
10
12
|
import { normcase, toPlatformPath } from '../lib/paths.mjs';
|
|
11
13
|
import { relPosix } from '../lib/text.mjs';
|
|
12
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
SHARED_TIER_RE, carriersFor, globalHookStandingDown, hookProjectDir, isGlobalInstall, resolvePath, sovereignBypass,
|
|
16
|
+
} from './hosts.mjs';
|
|
13
17
|
import { isFile, isDir, sortPaths, listDir } from './hooks-prims.mjs';
|
|
14
18
|
import { gitRootOf } from './gitref.mjs';
|
|
15
19
|
import { doneLine } from './done-checks.mjs';
|
|
@@ -74,13 +78,6 @@ function fnTranslate(pat) {
|
|
|
74
78
|
return new RegExp(`^(?:${out})$`);
|
|
75
79
|
}
|
|
76
80
|
|
|
77
|
-
/**
|
|
78
|
-
* The host whose own root file counts as native — `--host` on the emitted command, `claude`
|
|
79
|
-
* when absent. Module state for the reason `./hooks.mjs`'s gates keep theirs: a hook process
|
|
80
|
-
* answers exactly one call, so it is set once at `cmdContext`'s entry.
|
|
81
|
-
*/
|
|
82
|
-
let HOST = 'claude';
|
|
83
|
-
|
|
84
81
|
// Kept in step with adapters/opencode/plugins/geneseed-context.js.
|
|
85
82
|
const EAGER_ROOT = ['AGENTS.md', 'AGENT.md', 'CLAUDE.md', '.cursorrules',
|
|
86
83
|
'README.md', 'README.adoc', 'CONTRIBUTING.md', 'CONTRIBUTING.adoc', 'user-rules.md', 'PROFILE.md'];
|
|
@@ -104,6 +101,28 @@ const NATIVE_ROOT = {
|
|
|
104
101
|
claude: ['CLAUDE.md'], bob: ['AGENTS.md'],
|
|
105
102
|
openclaude: ['AGENTS.md', 'CLAUDE.md'],
|
|
106
103
|
};
|
|
104
|
+
// An `@AGENTS.md` import in a CLAUDE.md: whitespace on both sides, so a `code span` is no import.
|
|
105
|
+
const AGENTS_IMPORT_RE = /(^|\s)@(\.\/)?AGENTS\.md(?=\s|$)/m;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Does Claude Code load this repo's AGENTS.md by itself? Yes when CLAUDE.md imports it (fenced
|
|
109
|
+
* blocks and HTML comments do not import), on any version. Yes when the repo has no CLAUDE.md, from 2.1.277 on.
|
|
110
|
+
*
|
|
111
|
+
* The version comes from `AI_AGENT`, which Claude Code sets for its hooks as
|
|
112
|
+
* `claude-code_2-1-302_harness` (CLI) or `claude-code_2-1-317_agent` (desktop). Observed 2026-11-05,
|
|
113
|
+
* and undocumented: when it is absent or reads otherwise the answer is no, and the hook keeps
|
|
114
|
+
* injecting, because a duplicate costs tokens and a miss loses the doc.
|
|
115
|
+
*/
|
|
116
|
+
function claudeLoadsAgents(root) {
|
|
117
|
+
let claudeMd = null;
|
|
118
|
+
try { claudeMd = readText(path.join(root, 'CLAUDE.md')); } catch { /* none at the root */ }
|
|
119
|
+
if (claudeMd !== null) return AGENTS_IMPORT_RE.test(claudeMd.replace(/```[\s\S]*?```|~~~[\s\S]*?~~~|<!--[\s\S]*?-->/g, ''));
|
|
120
|
+
if (isFile(path.join(root, '.claude', 'CLAUDE.md'))) return false;
|
|
121
|
+
const v = /^claude-code_(\d+)-(\d+)-(\d+)/.exec(process.env.AI_AGENT || '');
|
|
122
|
+
if (!v) return false;
|
|
123
|
+
const [major, minor, patch] = v.slice(1).map(Number);
|
|
124
|
+
return (major - 2 || minor - 1 || patch - 277) >= 0;
|
|
125
|
+
}
|
|
107
126
|
|
|
108
127
|
// The whole payload's budget, in characters. Claude Code does not hand a hook's stdout to the
|
|
109
128
|
// model past roughly 10k characters: it saves the output to a file and the model sees a 2 KB
|
|
@@ -111,39 +130,47 @@ const NATIVE_ROOT = {
|
|
|
111
130
|
// a 16.5 KB SessionStart output, both persisted); the exact threshold is not documented, hence
|
|
112
131
|
// the margin. The old 48 KB eager budget never bound — the host's cap always cut first.
|
|
113
132
|
// Priority inside the budget: session files (the user's own), then eager docs (whole, or
|
|
114
|
-
// listed lazy), then the lazy listing, folded per directory and cut with a count.
|
|
115
|
-
|
|
133
|
+
// listed lazy), then the lazy listing, folded per directory and cut with a count. Session files
|
|
134
|
+
// spend in `SESSION_FILES` order, so what the budget cuts first is what comes last.
|
|
135
|
+
export const OUTPUT_BUDGET = 9000;
|
|
116
136
|
// Kept back from the eager docs so the lazy listing always has room to say what exists.
|
|
117
137
|
const LAZY_RESERVE = 1500;
|
|
118
138
|
// A directory with this many undescribed lazy files is listed once, with its count.
|
|
119
139
|
const LAZY_FOLD_AT = 3;
|
|
140
|
+
// The OpenCode plugin's vault walk bounds: `MAX_DEPTH` and `MAX_FILES_SCANNED`.
|
|
141
|
+
const WIKI_MAX_DEPTH = 6;
|
|
142
|
+
const WIKI_MAX_FILES = 2000;
|
|
143
|
+
// Kept back so a listing can still say what exists: from a wiki's eager notes, and at the end of
|
|
144
|
+
// the payload from the line per session file the budget left out.
|
|
145
|
+
const LIST_RESERVE = 500;
|
|
120
146
|
// What a line costs against the budget: its characters plus a terminator counted as two,
|
|
121
147
|
// because stdout goes out as \r\n on Windows — a 144-line listing overran by 137 before this.
|
|
122
148
|
// Multi-line text pays one more per inner line break, for the same reason.
|
|
123
149
|
const cost = (s) => s.length + (s.match(/\n/g) || []).length + 2;
|
|
150
|
+
// How a listing marks an eager file the budget kept out; `renderPayload` reads it back in a wiki.
|
|
151
|
+
const OVER_BUDGET = ' (eager, but over the session budget — read on demand)';
|
|
124
152
|
|
|
125
153
|
// The files the root instruction file names for session start, relative to the HARNESS dir
|
|
126
154
|
// (`--root`: `.claude/`, `.bob/`, a global config dir) — not to the repo root discovery walks,
|
|
127
155
|
// which is why none of them reached a hooked host before 2026-10. Injected here so the read does
|
|
128
156
|
// not depend on the model remembering it. `context.json` is not in the list: it is a manifest,
|
|
129
157
|
// honoured by `resolveContextSets` (the harness dir is its last candidate). The OpenCode
|
|
130
|
-
// context plugin carries the same list.
|
|
131
|
-
const SESSION_FILES = ['user-rules.md', 'PROFILE.md', '
|
|
132
|
-
'notebook/NOTEBOOK.md'
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
const LEGACY_WIKI = 'wiki.jsonc';
|
|
158
|
+
// context plugin carries the same list, minus the wiki it renders in a block of its own.
|
|
159
|
+
const SESSION_FILES = ['user-rules.md', 'PROFILE.md', 'geneseed-wiki.jsonc', 'memory/MEMORY.md',
|
|
160
|
+
'notebook/NOTEBOOK.md'];
|
|
161
|
+
// One writer each: a non-seed host copy replaces the shared one. The others concatenate, shared
|
|
162
|
+
// first, so a host rule reads after the shared rule it narrows.
|
|
163
|
+
const WHOLE_FILES = new Set(['PROFILE.md', 'geneseed-wiki.jsonc']);
|
|
137
164
|
// SHA-256 of each seed body in `js/build/stubs.mjs` (`SESSION_SEEDS`), CRLF folded. A file still
|
|
138
165
|
// byte-identical to its seed says nothing, so it is skipped rather than injected as noise.
|
|
139
|
-
// Hashes, not an import: stubs.mjs pulls in the build's writers,
|
|
140
|
-
//
|
|
166
|
+
// Hashes, not an import: stubs.mjs pulls in the build's writers, which a session start has no
|
|
167
|
+
// use for. tests/unit/claude.test.mjs gates this set, and the plugin's copy, against the seeds.
|
|
141
168
|
const SEED_SHA256 = new Set([
|
|
142
169
|
'5c2e92fb1acde041e02d9ebf158460d8ff7a07cbc2b447859631604610130721', // user-rules.md
|
|
143
170
|
'29e012c3c4349ea62a9082cbd04bb25599a0ec280d62c33f6e10742809471816', // PROFILE.md
|
|
144
171
|
'3a82c78ccc5d745033c16c354f5eda9d79350c704e9da8d605d380fb1e108d7a', // geneseed-wiki.jsonc
|
|
145
172
|
// The wiki seed before the rename: the build renames an untouched one byte for byte.
|
|
146
|
-
|
|
173
|
+
LEGACY_WIKI_SEED, // wiki.jsonc (legacy)
|
|
147
174
|
'99c786049c260f6baf7aec68a5c0f59907861afe9b867da009440613bdddb907', // MEMORY.md
|
|
148
175
|
'9acb5c9d9cb5dac572104480f05b48cc144e676869bd60e86d8a15b1f6308184', // NOTEBOOK.md
|
|
149
176
|
]);
|
|
@@ -192,29 +219,215 @@ function runtimeFloorLine(root) {
|
|
|
192
219
|
return pkg && typeof pkg === 'object' && !Array.isArray(pkg) && pkg.engines === undefined ? FLOOR_LINE : null;
|
|
193
220
|
}
|
|
194
221
|
|
|
222
|
+
/** A session file's text, CRLF-folded and trimmed; null when missing, unreadable or still its seed. */
|
|
223
|
+
function sessionText(abs) {
|
|
224
|
+
if (!isFile(abs)) return null;
|
|
225
|
+
let text;
|
|
226
|
+
try { text = readFileSync(abs, 'utf8').replace(/\r\n/g, '\n'); } catch { return null; }
|
|
227
|
+
if (SEED_SHA256.has(createHash('sha256').update(text).digest('hex'))) return null;
|
|
228
|
+
return text.replace(/\n+$/, '');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The machine tier's copy of a session file; null for what stays per host (the notebook). */
|
|
232
|
+
function tierFile(rel) {
|
|
233
|
+
let tier;
|
|
234
|
+
try { tier = sharedPaths(); } catch { return null; } // no home: the host layer still loads
|
|
235
|
+
return {
|
|
236
|
+
'user-rules.md': tier.rules,
|
|
237
|
+
'PROFILE.md': tier.profile,
|
|
238
|
+
'geneseed-wiki.jsonc': tier.wiki,
|
|
239
|
+
'memory/MEMORY.md': path.join(tier.memory, 'MEMORY.md'),
|
|
240
|
+
}[rel] ?? null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The `host` a caller outside the hook passes for an install of `host`: what its emitted command
|
|
245
|
+
* says, and Claude's carries no `--host` (`claudeHookGroups`).
|
|
246
|
+
*/
|
|
247
|
+
export const emittedHost = (host) => (host === 'claude' ? null : host);
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Only a global install of THIS host reads the tier, as the OpenCode plugin's `readsTier` has it
|
|
251
|
+
* (it answers for OpenCode alone). The host is the one the emitted hook passed: an emit from before
|
|
252
|
+
* `--host` names none (null), and then any host's global qualifies. One that cannot tell stays on
|
|
253
|
+
* its own layer.
|
|
254
|
+
*/
|
|
255
|
+
const readsTier = (hookRoot, host) => isGlobalInstall(host, hookRoot);
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Does this PROJECT install read the machine tier? Its carrier says `Shared tier: on`, and no
|
|
259
|
+
* global hook is already injecting it: under `$GENESEED_STACK_GLOBAL` the global hook does not
|
|
260
|
+
* stand down (`globalHookStandingDown`), so a second tier here would be a double. Without the
|
|
261
|
+
* flag the global hook stands down for a wired project, and this hook is the only injector.
|
|
262
|
+
* A project with the option off reads no tier, as before. `carrier` is `installCarrier`'s text.
|
|
263
|
+
*/
|
|
264
|
+
function projectReadsTier(carrier) {
|
|
265
|
+
if (process.env.GENESEED_STACK_GLOBAL) return false;
|
|
266
|
+
return SHARED_TIER_RE.test(carrier);
|
|
267
|
+
}
|
|
268
|
+
|
|
195
269
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
* context, never a gate.
|
|
270
|
+
* How many items of the fold's ledger still wait for the user: a drafted rule, or a file with two
|
|
271
|
+
* versions. The fold writes them, and nothing else would say so. Unreadable counts as none: this is a
|
|
272
|
+
* reminder, and the fold itself reports a broken ledger.
|
|
200
273
|
*/
|
|
201
|
-
export function
|
|
274
|
+
export function pendingDrafts() {
|
|
275
|
+
try {
|
|
276
|
+
const { items } = JSON.parse(readFileSync(sharedPaths().foldState, 'utf8'));
|
|
277
|
+
return Object.values(items ?? {}).filter((i) => i?.state === 'pending').length;
|
|
278
|
+
} catch { return 0; }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** The session's reminder of them, or null. The OpenCode context plugin's `draftLine` is its twin. */
|
|
282
|
+
function draftLine() {
|
|
283
|
+
const n = pendingDrafts();
|
|
284
|
+
return n ? `[drafts] ${n} drafted item${n === 1 ? '' : 's'} in user-rules.draft.md wait${n === 1 ? 's' : ''} for the user's decision (the rule skill or the web console).` : null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The characters the tier would add to a session: every changed-from-seed file the hook injects
|
|
289
|
+
* from it. The `setup` prompt turns this into the token figure it shows beside the option.
|
|
290
|
+
*/
|
|
291
|
+
export function tierSessionChars() {
|
|
292
|
+
let n = 0;
|
|
293
|
+
for (const rel of SESSION_FILES) {
|
|
294
|
+
const abs = tierFile(rel);
|
|
295
|
+
const text = abs ? sessionText(abs) : null;
|
|
296
|
+
if (text !== null) n += text.length;
|
|
297
|
+
}
|
|
298
|
+
return n;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** A vault folder's `.md` notes, dot-folders skipped: a vault keeps its config and trash inside. */
|
|
302
|
+
function walkWiki(dir, depth = 1, acc = []) {
|
|
303
|
+
if (depth > WIKI_MAX_DEPTH) return acc;
|
|
304
|
+
let entries;
|
|
305
|
+
// `withFileTypes`, as the OpenCode plugin: a link or junction is neither a folder nor a note.
|
|
306
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return acc; }
|
|
307
|
+
for (const e of entries) {
|
|
308
|
+
if (acc.length >= WIKI_MAX_FILES) return acc;
|
|
309
|
+
const full = path.join(dir, e.name);
|
|
310
|
+
if (e.isDirectory()) {
|
|
311
|
+
if (!EXCLUDE_DIRS.has(e.name) && !e.name.startsWith('.')) walkWiki(full, depth + 1, acc);
|
|
312
|
+
} else if (e.isFile() && e.name.toLowerCase().endsWith('.md')) acc.push(full);
|
|
313
|
+
}
|
|
314
|
+
return acc;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The manifest's wikis, ready to render: the OpenCode plugin's `wikiSets`, with the same walk
|
|
319
|
+
* bounds and precedence. Folders apply first, so a file entry always overrides its folder's mode;
|
|
320
|
+
* `exclude` prunes; a wiki whose root is missing is skipped. Null for a file that does not parse.
|
|
321
|
+
*/
|
|
322
|
+
function wikiSets(text) {
|
|
323
|
+
const [data] = readJsonc(text);
|
|
324
|
+
if (data === null) return null;
|
|
325
|
+
const sets = [];
|
|
326
|
+
for (const w of Array.isArray(data.wikis) ? data.wikis : []) {
|
|
327
|
+
if (!w?.path || !isDir(w.path)) continue;
|
|
328
|
+
const entries = (Array.isArray(w.entries) ? w.entries : []).filter((e) => e?.path && typeof e.path === 'string').map((e) => {
|
|
329
|
+
const abs = path.isAbsolute(e.path) ? e.path : path.resolve(w.path, e.path);
|
|
330
|
+
return { abs, load: e.load === 'lazy' || e.load === 'exclude' ? e.load : 'eager',
|
|
331
|
+
description: e.description || '', dir: isDir(abs) };
|
|
332
|
+
});
|
|
333
|
+
const recs = new Map();
|
|
334
|
+
for (const folders of [true, false]) {
|
|
335
|
+
for (const e of entries.filter((x) => x.dir === folders)) {
|
|
336
|
+
for (const note of e.dir ? walkWiki(e.abs) : [e.abs]) {
|
|
337
|
+
if (e.load === 'exclude') { recs.delete(note); continue; }
|
|
338
|
+
const description = (e.dir ? '' : e.description) || recs.get(note)?.description || '';
|
|
339
|
+
recs.set(note, { path: note, load: e.load, description });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const notes = [...recs.values()].sort((a, b) => a.path.localeCompare(b.path));
|
|
344
|
+
sets.push({ name: w.name || path.basename(w.path), root: w.path, desc: w.description || '',
|
|
345
|
+
conventions: w.conventions || '', inbox: w.inbox || '',
|
|
346
|
+
protected: Array.isArray(w.protected) ? w.protected : [],
|
|
347
|
+
eager: notes.filter((n) => n.load === 'eager'), lazy: notes.filter((n) => n.load === 'lazy') });
|
|
348
|
+
}
|
|
349
|
+
return sets;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The wikis as the OpenCode plugin prints them, inside `room`: header lines, eager notes whole, and
|
|
354
|
+
* a lazy listing. A note that does not fit is listed instead, like an eager doc. Sized by `stat`
|
|
355
|
+
* before it is read, so a whole-vault eager entry costs no read past the budget.
|
|
356
|
+
*/
|
|
357
|
+
function renderWiki(wikis, room) {
|
|
358
|
+
const lines = [];
|
|
359
|
+
let left = room;
|
|
360
|
+
const add = (...ls) => { for (const l of ls) { lines.push(l); left -= cost(l); } };
|
|
361
|
+
for (const w of wikis) {
|
|
362
|
+
add(`--- wiki: ${w.name}${w.desc ? ` \u2014 ${w.desc}` : ''} (root: ${w.root}) ---`);
|
|
363
|
+
if (w.conventions) add(` conventions: ${w.conventions} (read before your first write there)`);
|
|
364
|
+
if (w.inbox) add(` inbox: ${w.inbox} (drop notes you cannot confidently file)`);
|
|
365
|
+
if (w.protected.length) add(` protected (never write): ${w.protected.join(', ')}`);
|
|
366
|
+
add('');
|
|
367
|
+
const demoted = [];
|
|
368
|
+
for (const e of w.eager) {
|
|
369
|
+
const head = `----- ${w.name}/${disp(e.path, w.root)}${e.description ? ` \u2014 ${e.description}` : ''} -----`;
|
|
370
|
+
let text = null;
|
|
371
|
+
try {
|
|
372
|
+
if (cost(head) + statSync(e.path).size + cost('') <= left - LIST_RESERVE) text = readText(e.path);
|
|
373
|
+
} catch (x) {
|
|
374
|
+
add(head, `[context] MISSING eager file: ${asOsError(x, e.path)}`, '');
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (text === null) demoted.push({ ...e, over: true });
|
|
378
|
+
else add(head, text.replace(/\n+$/, ''), '');
|
|
379
|
+
}
|
|
380
|
+
if (w.lazy.length || demoted.length) {
|
|
381
|
+
add('--- Lazy entries (load only when the task needs them) ---');
|
|
382
|
+
add(...lazyLines([...demoted, ...w.lazy], w.root, left - cost('')), '');
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return lines.join('\n').replace(/\n+$/, '');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* One session entry per layer. The `(shared)` and `(host: …)` labels appear only when the tier has
|
|
390
|
+
* a say in that file, so an install with an empty tier prints what it always printed. A wiki entry
|
|
391
|
+
* keeps its `wiki` sets, for a render sized to the room `cmdContext` finds.
|
|
392
|
+
*/
|
|
393
|
+
function sessionEntry(rel, abs, text, label) {
|
|
394
|
+
if (rel === 'geneseed-wiki.jsonc') {
|
|
395
|
+
// The user edits this file by hand, and a throw here would lose the whole payload, rules included.
|
|
396
|
+
try {
|
|
397
|
+
const wiki = wikiSets(text);
|
|
398
|
+
if (wiki !== null) return wiki.length ? { rel, abs, label, wiki, text: renderWiki(wiki, OUTPUT_BUDGET - LAZY_RESERVE) } : null;
|
|
399
|
+
} catch { /* reported below, as a manifest that does not parse */ }
|
|
400
|
+
return { rel, abs, label, text: `[context] ${abs} is not valid JSONC \u2014 no wiki loaded.` };
|
|
401
|
+
}
|
|
402
|
+
const nudge = rel === 'memory/MEMORY.md' ? memoryNudge(text) : '';
|
|
403
|
+
return { rel, abs, label, text: nudge ? `${text}\n${nudge}` : text };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* The session files in force for `hookRoot`, changed from their seed, in `SESSION_FILES` order:
|
|
408
|
+
* `{ rel, abs, text, label }` per layer. A global install, or a project built `--shared-tier on`,
|
|
409
|
+
* reads the machine tier first, then its own host layer. `$GENESEED_WIKI` overrides the wiki declaration on both, as it does on OpenCode.
|
|
410
|
+
* A missing or unreadable file is simply absent — this is best-effort context, never a gate.
|
|
411
|
+
* `host` is what the emitted hook passes (`emittedHost`), null for none, which reads as claude.
|
|
412
|
+
*/
|
|
413
|
+
export function sessionFiles(hookRoot, host = null, carrier = installCarrier(hookRoot, host)) {
|
|
414
|
+
const tier = readsTier(hookRoot, host) || projectReadsTier(carrier);
|
|
202
415
|
const found = [];
|
|
203
416
|
for (const rel of SESSION_FILES) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
417
|
+
const wiki = rel === 'geneseed-wiki.jsonc';
|
|
418
|
+
const pinned = wiki && process.env.GENESEED_WIKI && isFile(process.env.GENESEED_WIKI);
|
|
419
|
+
const hostAbs = pinned ? process.env.GENESEED_WIKI
|
|
420
|
+
: wiki ? WIKI_NAMES.map((n) => path.join(hookRoot, n)).find(isFile) ?? path.join(hookRoot, rel)
|
|
421
|
+
: path.join(hookRoot, rel);
|
|
422
|
+
const own = sessionText(hostAbs);
|
|
423
|
+
const sharedAbs = tier && !pinned ? tierFile(rel) : null;
|
|
424
|
+
const shared = sharedAbs ? sessionText(sharedAbs) : null;
|
|
425
|
+
if (shared !== null && !(own !== null && WHOLE_FILES.has(rel))) {
|
|
426
|
+
found.push(sessionEntry(rel, sharedAbs, shared, 'shared'));
|
|
208
427
|
}
|
|
209
|
-
if (
|
|
210
|
-
let text;
|
|
211
|
-
try { text = readFileSync(abs, 'utf8').replace(/\r\n/g, '\n'); } catch { continue; }
|
|
212
|
-
if (SEED_SHA256.has(createHash('sha256').update(text).digest('hex'))) continue;
|
|
213
|
-
text = text.replace(/\n+$/, '');
|
|
214
|
-
const nudge = rel.endsWith('MEMORY.md') ? memoryNudge(text) : '';
|
|
215
|
-
found.push({ rel, abs, text: nudge ? `${text}\n${nudge}` : text });
|
|
428
|
+
if (own !== null) found.push(sessionEntry(rel, hostAbs, own, shared === null ? '' : `host: ${host || 'claude'}`));
|
|
216
429
|
}
|
|
217
|
-
return found;
|
|
430
|
+
return found.filter(Boolean);
|
|
218
431
|
}
|
|
219
432
|
|
|
220
433
|
/** `_disp` — relative to the repo root when it sits under it, else verbatim. */
|
|
@@ -243,11 +456,12 @@ function rglobDocs(dir, acc = []) {
|
|
|
243
456
|
* Root entry docs are eager; other root docs (.md or .adoc), the doc trees and monorepo package
|
|
244
457
|
* READMEs are lazy.
|
|
245
458
|
*/
|
|
246
|
-
export function discoverContext(root, host =
|
|
459
|
+
export function discoverContext(root, host = 'claude') {
|
|
247
460
|
const eager = new Map();
|
|
248
461
|
const lazy = new Map();
|
|
249
462
|
const native = host === 'openclaude' && isFile(path.join(root, 'AGENTS.md'))
|
|
250
|
-
? ['AGENTS.md'] : NATIVE_ROOT[host] || [];
|
|
463
|
+
? ['AGENTS.md'] : [...(NATIVE_ROOT[host] || [])];
|
|
464
|
+
if (host === 'claude' && claudeLoadsAgents(root)) native.push('AGENTS.md');
|
|
251
465
|
for (const full of sortPaths(listDir(root).map((n) => path.join(root, n)))) {
|
|
252
466
|
if (!isFile(full)) continue;
|
|
253
467
|
const name = path.basename(full);
|
|
@@ -289,7 +503,7 @@ export function discoverContext(root, host = HOST) {
|
|
|
289
503
|
* these keys are absolute paths whose iteration order becomes the printed order.
|
|
290
504
|
* @returns {[Array<{path: string, description: string}>, Array<{path: string, description: string}>, string]}
|
|
291
505
|
*/
|
|
292
|
-
export function resolveContextSets(root, hookRoot = null) {
|
|
506
|
+
export function resolveContextSets(root, hookRoot = null, host = 'claude') {
|
|
293
507
|
let manifest = null;
|
|
294
508
|
const env = process.env.GENESEED_CONTEXT;
|
|
295
509
|
if (env && isFile(env)) {
|
|
@@ -305,7 +519,7 @@ export function resolveContextSets(root, hookRoot = null) {
|
|
|
305
519
|
}
|
|
306
520
|
|
|
307
521
|
if (manifest === null) {
|
|
308
|
-
const [e, l] = discoverContext(root);
|
|
522
|
+
const [e, l] = discoverContext(root, host);
|
|
309
523
|
return [e, l, `auto-discovery [${root}]`];
|
|
310
524
|
}
|
|
311
525
|
|
|
@@ -338,7 +552,7 @@ export function resolveContextSets(root, hookRoot = null) {
|
|
|
338
552
|
}
|
|
339
553
|
|
|
340
554
|
if (!entries.length && !extend) {
|
|
341
|
-
const [e, l] = discoverContext(root);
|
|
555
|
+
const [e, l] = discoverContext(root, host);
|
|
342
556
|
return [e, l, `auto-discovery [${root}] (empty ${path.basename(manifest)})`];
|
|
343
557
|
}
|
|
344
558
|
|
|
@@ -349,7 +563,7 @@ export function resolveContextSets(root, hookRoot = null) {
|
|
|
349
563
|
};
|
|
350
564
|
|
|
351
565
|
if (extend) {
|
|
352
|
-
const [de, dl] = discoverContext(root);
|
|
566
|
+
const [de, dl] = discoverContext(root, host);
|
|
353
567
|
for (const x of de) put(x.path, 'eager', '');
|
|
354
568
|
for (const x of dl) put(x.path, 'lazy', '');
|
|
355
569
|
}
|
|
@@ -417,13 +631,18 @@ function lazyLines(entries, root, room) {
|
|
|
417
631
|
lines.push(` - ${dir.replace(/\\/g, '/')}/ — ${byDir.get(dir)} docs`);
|
|
418
632
|
continue;
|
|
419
633
|
}
|
|
420
|
-
const why = e.over ?
|
|
634
|
+
const why = e.over ? OVER_BUDGET : '';
|
|
421
635
|
lines.push(` - ${shown}${e.description ? ` — ${e.description}` : ''}${why}`);
|
|
422
636
|
}
|
|
637
|
+
return fitLines(lines, room, (n) => ` - … ${n} more not listed (session budget)`);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** As many of `lines` as fit `room`, and past that one line saying how many did not. */
|
|
641
|
+
function fitLines(lines, room, tailOf) {
|
|
423
642
|
const kept = [];
|
|
424
643
|
let used = 0;
|
|
425
644
|
for (const [i, l] of lines.entries()) {
|
|
426
|
-
const tail =
|
|
645
|
+
const tail = tailOf(lines.length - i);
|
|
427
646
|
if (used + cost(l) + (i < lines.length - 1 ? cost(tail) : 0) > room) {
|
|
428
647
|
kept.push(tail);
|
|
429
648
|
break;
|
|
@@ -434,8 +653,123 @@ function lazyLines(entries, root, room) {
|
|
|
434
653
|
return kept;
|
|
435
654
|
}
|
|
436
655
|
|
|
656
|
+
/**
|
|
657
|
+
* The text of this host's own carrier, or '' (armed). A global install keeps it in its config dir;
|
|
658
|
+
* a project install keeps it in the repo, one level above the harness dir. The first candidate
|
|
659
|
+
* that exists answers, as `firstCarrier` does.
|
|
660
|
+
*/
|
|
661
|
+
function installCarrier(hookRoot, host) {
|
|
662
|
+
const base = readsTier(hookRoot, host) ? hookRoot : path.dirname(hookRoot);
|
|
663
|
+
// Last, a plain bundle or git-clone harness dir keeps its AGENT.md in the dir itself.
|
|
664
|
+
const found = [...carriersFor(host || 'claude').map((rel) => path.join(base, rel)), path.join(hookRoot, 'AGENT.md')];
|
|
665
|
+
for (const file of found) {
|
|
666
|
+
try { return readFileSync(file, 'utf8'); } catch { /* next candidate */ }
|
|
667
|
+
}
|
|
668
|
+
return '';
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* The payload, what it spent against `OUTPUT_BUDGET`, and whether the budget `cut` content from it:
|
|
673
|
+
* THE ONE WRITER of all three, so what the hook prints and what `sessionStartSize` reports cannot
|
|
674
|
+
* differ. Cut means a session file truncated or left out, a wiki note or an eager doc listed
|
|
675
|
+
* instead of injected; a lazy listing shortened is not, since it only names files. `heads` are the
|
|
676
|
+
* lines above the session block; `root` places `eager` and `lazy`. Reads, never prints.
|
|
677
|
+
*/
|
|
678
|
+
function renderPayload({ heads, session, eager, lazy, source, root }) {
|
|
679
|
+
const lines = [];
|
|
680
|
+
// `spent` counts every character pushed, headers included: the budget is the host's cap on
|
|
681
|
+
// the whole payload, not on the file bodies alone.
|
|
682
|
+
let spent = 0;
|
|
683
|
+
let cut = false;
|
|
684
|
+
const push = (...ls) => { for (const l of ls) { lines.push(l); spent += cost(l); } };
|
|
685
|
+
if (heads.length) push(...heads, '');
|
|
686
|
+
if (session.length) {
|
|
687
|
+
push('=== SESSION FILES \u2014 your harness files, injected at session start '
|
|
688
|
+
+ '(untouched seeds skipped) ===', '');
|
|
689
|
+
// Rules answer only to their own size: the lines above them never push one out, so they are
|
|
690
|
+
// capped when their layers together pass the share, and everything after them spends `spent`.
|
|
691
|
+
let rulesSpent = 0;
|
|
692
|
+
const left = [];
|
|
693
|
+
for (const s of session) {
|
|
694
|
+
const head = `----- ${s.rel}${s.label ? ` (${s.label})` : ''} -----`;
|
|
695
|
+
const rules = s.rel === 'user-rules.md';
|
|
696
|
+
const room = OUTPUT_BUDGET - LAZY_RESERVE - (rules ? rulesSpent : spent) - cost(head) - cost('');
|
|
697
|
+
const where = `${s.abs} on demand`;
|
|
698
|
+
if (room <= 0) {
|
|
699
|
+
left.push({ rel: s.rel, line: `[context] ${s.rel} not injected (session budget) \u2014 read ${where}` });
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
const text = capToRoom(s.wiki && cost(s.text) > room ? renderWiki(s.wiki, room) : s.text, room, where);
|
|
703
|
+
if (text !== s.text || (s.wiki && text.includes(OVER_BUDGET))) cut = true;
|
|
704
|
+
push(head, text, '');
|
|
705
|
+
if (rules) rulesSpent += cost(head) + cost(text) + cost('');
|
|
706
|
+
}
|
|
707
|
+
// Paths have no length limit, so these lines answer to the budget like any listing.
|
|
708
|
+
if (left.length) {
|
|
709
|
+
cut = true;
|
|
710
|
+
const room = OUTPUT_BUDGET - LIST_RESERVE - spent - cost('');
|
|
711
|
+
const tail = (n) => `[context] ${n} more session files not injected (session budget)`;
|
|
712
|
+
// The memory map first, and by its short name when its path cannot fit: a count line must
|
|
713
|
+
// never replace the one file C12 owes a line that names it.
|
|
714
|
+
const isMap = (l) => l.rel === 'memory/MEMORY.md';
|
|
715
|
+
const maps = left.filter(isMap);
|
|
716
|
+
const lines = [...maps, ...left.filter((l) => !isMap(l))].map((l) => l.line);
|
|
717
|
+
let shown = fitLines(lines, room, tail);
|
|
718
|
+
if (maps.length && shown[0] !== lines[0]) {
|
|
719
|
+
const short = [...new Set(maps.map((l) => `[context] ${l.rel} not injected (session budget)`))];
|
|
720
|
+
shown = fitLines([...short, ...lines.slice(maps.length)], room, tail);
|
|
721
|
+
}
|
|
722
|
+
push(...shown, '');
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
if (eager.length || lazy.length) {
|
|
726
|
+
push(`=== PROJECT CONTEXT \u2014 binding for this repo (via ${source}) ===`, '');
|
|
727
|
+
}
|
|
728
|
+
const demoted = [];
|
|
729
|
+
for (const entry of eager) {
|
|
730
|
+
const p = entry.path === undefined ? '' : entry.path;
|
|
731
|
+
const desc = entry.description === undefined ? '' : entry.description;
|
|
732
|
+
const target = path.isAbsolute(p) ? p : path.join(root, p);
|
|
733
|
+
const head = `----- ${disp(p, root)}${desc ? ` \u2014 ${desc}` : ''} -----`;
|
|
734
|
+
let text;
|
|
735
|
+
try {
|
|
736
|
+
text = readText(target).replace(/\n+$/, '');
|
|
737
|
+
} catch (e) {
|
|
738
|
+
push(head, `[context] MISSING eager file: ${asOsError(e, target)}`, '');
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
if (spent + cost(head) + cost(text) + cost('') > OUTPUT_BUDGET - LAZY_RESERVE) {
|
|
742
|
+
demoted.push({ ...entry, over: true });
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
push(head, text, '');
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
if (lazy.length || demoted.length) {
|
|
749
|
+
const title = '--- Lazy entries (load only when the task needs them) ---';
|
|
750
|
+
push(title);
|
|
751
|
+
push(...lazyLines([...lazy, ...demoted], root, OUTPUT_BUDGET - spent - cost('')), '');
|
|
752
|
+
}
|
|
753
|
+
return { text: `${lines.join('\n')}\n`, spent, cut: cut || demoted.length > 0 };
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* What the session-start hook of a GLOBAL install prints with no project around it, against the
|
|
758
|
+
* budget: `{ chars, budget, cut }`, for the console, which has no repo. `chars` is `spent`, in the
|
|
759
|
+
* budget's own unit (`cost`), so on Windows it is the output's length in characters. `cut` is told
|
|
760
|
+
* apart from `chars` because session files stop near 83% of the budget: a size cannot show it.
|
|
761
|
+
*
|
|
762
|
+
* The figure ignores `sovereignBypass`: the hook prints nothing in an excluded folder, but the
|
|
763
|
+
* exclusion depends on the session's cwd, and the console has no session. Reads files, writes nothing.
|
|
764
|
+
*/
|
|
765
|
+
export function sessionStartSize(hookRoot, host) {
|
|
766
|
+
const session = sessionFiles(hookRoot, emittedHost(host));
|
|
767
|
+
const { spent, cut } = renderPayload({ heads: [], session, eager: [], lazy: [], source: '', root: hookRoot });
|
|
768
|
+
return { chars: spent, budget: OUTPUT_BUDGET, cut };
|
|
769
|
+
}
|
|
770
|
+
|
|
437
771
|
export function cmdContext(args) {
|
|
438
|
-
|
|
772
|
+
const host = (args && args.host) || null;
|
|
439
773
|
// Discovery (and the stand-down) runs against the SESSION's project root, not the harness
|
|
440
774
|
// package dir: `$CLAUDE_PROJECT_DIR` when the host sets it (`hookProjectDir`), because
|
|
441
775
|
// SessionStart also fires on resume/compact/clear, after the agent may have `cd`'d away.
|
|
@@ -455,16 +789,17 @@ export function cmdContext(args) {
|
|
|
455
789
|
return 0;
|
|
456
790
|
}
|
|
457
791
|
if (hookRoot && sovereignBypass(hookRoot)) return 0;
|
|
458
|
-
// The RAW `args.host`,
|
|
792
|
+
// The RAW `args.host`, never defaulted to claude, which would key an older
|
|
459
793
|
// OpenClaude emit's `~/.openclaude` (no `--host`) on `.claude` instead of its folder name.
|
|
460
794
|
if (globalHookStandingDown(hookRoot, root, args.host, 'context')) return 0;
|
|
461
|
-
const
|
|
795
|
+
const carrier = hookRoot ? installCarrier(hookRoot, host) : '';
|
|
796
|
+
const session = hookRoot ? sessionFiles(hookRoot, host, carrier) : [];
|
|
462
797
|
// A session file discovery would also pick up (a `user-rules.md` at the repo root of an
|
|
463
798
|
// install whose harness dir IS the repo root) is injected once, in the session block.
|
|
464
799
|
const seen = new Set(session.map((s) => normcase(path.resolve(s.abs))));
|
|
465
800
|
const fresh = (e) => !seen.has(normcase(path.resolve(path.isAbsolute(e.path)
|
|
466
801
|
? e.path : path.join(root, e.path))));
|
|
467
|
-
let [eager, lazy, source] = resolveContextSets(root, hookRoot);
|
|
802
|
+
let [eager, lazy, source] = resolveContextSets(root, hookRoot, host || 'claude');
|
|
468
803
|
eager = eager.filter(fresh);
|
|
469
804
|
lazy = lazy.filter(fresh);
|
|
470
805
|
// The done gate's checks for this repo, and where they come from (`./hooks-done.mjs`). No git
|
|
@@ -472,15 +807,14 @@ export function cmdContext(args) {
|
|
|
472
807
|
// `--no-done-gate` carries `Done gate: off` in its AGENT.md and gets no line — the OpenCode
|
|
473
808
|
// context plugin's `doneGateOff` reads the same marker with the same pattern.
|
|
474
809
|
const gitRoot = gitRootOf(root);
|
|
475
|
-
let carrier = '';
|
|
476
|
-
try { carrier = hookRoot ? readFileSync(path.join(hookRoot, 'AGENT.md'), 'utf8') : ''; } catch { /* no carrier: armed */ }
|
|
477
810
|
const gateOff = (marker) => new RegExp(`^${marker}:[ \\t]*off[ \\t]*\\r?$`, 'm').test(carrier);
|
|
478
811
|
// Bob's Stop carries no transcript, so the gate never judges a turn there (`hooks-done.mjs`).
|
|
479
|
-
const done = gitRoot && !gateOff('Done gate') ? doneLine(gitRoot,
|
|
812
|
+
const done = gitRoot && !gateOff('Done gate') ? doneLine(gitRoot, host !== 'bob') : null;
|
|
480
813
|
// The ownership decline reminder (`./ownership-gate.mjs`), under the same contract: an install
|
|
481
814
|
// built `--no-ownership-gate` carries `Ownership gate: off` and gets no line.
|
|
482
815
|
const owned = gitRoot && !gateOff('Ownership gate') ? ownershipLine(gitRoot) : null;
|
|
483
|
-
const
|
|
816
|
+
const drafts = hookRoot && (readsTier(hookRoot, host) || projectReadsTier(carrier)) ? draftLine() : null;
|
|
817
|
+
const heads = [done, owned, gitRoot && runtimeFloorLine(gitRoot), drafts].filter(Boolean);
|
|
484
818
|
if (!session.length && !eager.length && !lazy.length) {
|
|
485
819
|
err(`[context] nothing to load for ${root} `
|
|
486
820
|
+ '(no docs discovered, no manifest entries).\n');
|
|
@@ -488,51 +822,7 @@ export function cmdContext(args) {
|
|
|
488
822
|
return 0;
|
|
489
823
|
}
|
|
490
824
|
|
|
491
|
-
const lines = [];
|
|
492
|
-
// `spent` counts every character pushed, headers included: the budget is the host's cap on
|
|
493
|
-
// the whole payload, not on the file bodies alone.
|
|
494
|
-
let spent = 0;
|
|
495
|
-
const push = (...ls) => { for (const l of ls) { lines.push(l); spent += cost(l); } };
|
|
496
|
-
if (heads.length) push(...heads, '');
|
|
497
|
-
if (session.length) {
|
|
498
|
-
push('=== SESSION FILES \u2014 your harness files, injected at session start '
|
|
499
|
-
+ '(untouched seeds skipped) ===', '');
|
|
500
|
-
for (const s of session) {
|
|
501
|
-
const head = `----- ${s.rel} -----`;
|
|
502
|
-
push(head, capToRoom(s.text, OUTPUT_BUDGET - LAZY_RESERVE - spent - cost(head) - cost(''),
|
|
503
|
-
`${s.abs} on demand`), '');
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
if (eager.length || lazy.length) {
|
|
507
|
-
push(`=== PROJECT CONTEXT \u2014 binding for this repo (via ${source}) ===`, '');
|
|
508
|
-
}
|
|
509
|
-
const demoted = [];
|
|
510
|
-
for (const entry of eager) {
|
|
511
|
-
const p = entry.path === undefined ? '' : entry.path;
|
|
512
|
-
const desc = entry.description === undefined ? '' : entry.description;
|
|
513
|
-
const target = path.isAbsolute(p) ? p : path.join(root, p);
|
|
514
|
-
const head = `----- ${disp(p, root)}${desc ? ` \u2014 ${desc}` : ''} -----`;
|
|
515
|
-
let text;
|
|
516
|
-
try {
|
|
517
|
-
text = readText(target).replace(/\n+$/, '');
|
|
518
|
-
} catch (e) {
|
|
519
|
-
push(head, `[context] MISSING eager file: ${asOsError(e, target)}`, '');
|
|
520
|
-
continue;
|
|
521
|
-
}
|
|
522
|
-
if (spent + cost(head) + cost(text) + cost('') > OUTPUT_BUDGET - LAZY_RESERVE) {
|
|
523
|
-
demoted.push({ ...entry, over: true });
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
push(head, text, '');
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (lazy.length || demoted.length) {
|
|
530
|
-
const title = '--- Lazy entries (load only when the task needs them) ---';
|
|
531
|
-
push(title);
|
|
532
|
-
push(...lazyLines([...lazy, ...demoted], root, OUTPUT_BUDGET - spent - cost('')), '');
|
|
533
|
-
}
|
|
534
|
-
|
|
535
825
|
// Claude Code takes a SessionStart hook's plain stdout as context; Bob reads it the same way.
|
|
536
|
-
out(
|
|
826
|
+
out(renderPayload({ heads, session, eager, lazy, source, root }).text);
|
|
537
827
|
return 0;
|
|
538
828
|
}
|