@pugi/cli 0.1.0-beta.22 → 0.1.0-beta.24

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.
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Leak L27 (2026-05-27) — Auto-update channel + last-check persistence.
3
+ *
4
+ * Two pieces of disk state are managed here:
5
+ *
6
+ * 1. **Channel selection** — `~/.pugi/config.json::updateChannel`.
7
+ * Persisted across sessions so `pugi update` keeps polling the
8
+ * same track the operator opted into via `pugi update --channel
9
+ * <name>`. Mirrors the read/write pattern used by
10
+ * `core/permissions/state.ts::getGlobalDefaultMode` (passthrough
11
+ * schema, atomic tmp+rename, defensive parse).
12
+ *
13
+ * 2. **Last-check timestamp** — `~/.pugi/.last-update-check` (ISO
14
+ * string, single-line). Read by the cold-start banner gate so
15
+ * operators only see the "update available" hint once per
16
+ * `UPDATE_CHECK_INTERVAL_HOURS` (default 24h). Living on its own
17
+ * file (NOT a JSON object inside config.json) is intentional:
18
+ * the timestamp is a hot path — every CLI invocation touches it —
19
+ * and a single-line read+write is materially faster than the
20
+ * JSON parse + serialise of the broader config doc, with no
21
+ * schema coupling cost.
22
+ *
23
+ * Module contract:
24
+ *
25
+ * - Every file path resolver accepts a `homeDir` override so the
26
+ * test suite can drive the module through a per-test mkdtemp
27
+ * directory without polluting the real `~/.pugi/`.
28
+ *
29
+ * - Parse / read helpers NEVER throw on a malformed file. A
30
+ * corrupted JSON blob, a missing field, or an unreadable file all
31
+ * collapse to "no persisted value" so the next layer (the CLI
32
+ * flag or the hard default `beta`) takes over. A future-self
33
+ * debugging an update flow against a corrupt config never has the
34
+ * CLI crash on them.
35
+ *
36
+ * - Write helpers use the atomic tmp+rename idiom so a kill mid-
37
+ * write never produces a half-flushed JSON document. The
38
+ * timestamp file is small enough that POSIX `rename` is itself
39
+ * atomic in practice, but we keep the idiom uniform with the
40
+ * config write so reviewers do not have to context-switch.
41
+ */
42
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from 'node:fs';
43
+ import { homedir } from 'node:os';
44
+ import { resolve, dirname } from 'node:path';
45
+ import { z } from 'zod';
46
+ import { DEFAULT_UPDATE_CHANNEL, UPDATE_CHANNELS, } from './channels.js';
47
+ /**
48
+ * Default rate-limit window between registry probes. Operators see the
49
+ * cold-start banner at most once per window. Override per call via
50
+ * `shouldCheckForUpdate({ intervalHours })` — the cron-style scheduler
51
+ * passes 0 to force a check on every invocation, the doctor probe
52
+ * passes 24 to match the operator-visible cadence.
53
+ */
54
+ export const UPDATE_CHECK_INTERVAL_HOURS = 24;
55
+ /** Filename of the per-user channel + misc config. Mirrors L6 / L25. */
56
+ const CONFIG_FILE = '.pugi/config.json';
57
+ /** Filename of the standalone last-check ISO timestamp. */
58
+ const LAST_CHECK_FILE = '.pugi/.last-update-check';
59
+ /**
60
+ * Zod schema for the channel slice of `~/.pugi/config.json`. The
61
+ * passthrough lets sibling skills (L6 `defaultPermissionMode`, L25
62
+ * onboarding marker, etc.) coexist in the same JSON document without
63
+ * dropping their fields on a channel write.
64
+ */
65
+ const channelConfigSchema = z
66
+ .object({
67
+ updateChannel: z.enum(['stable', 'beta', 'canary']).optional(),
68
+ })
69
+ .partial()
70
+ .passthrough();
71
+ /**
72
+ * Resolve the absolute path of the per-user config file. Defaults to
73
+ * the real home dir, but every caller in the spec passes an explicit
74
+ * tmpdir so the persisted writes never escape the test sandbox.
75
+ */
76
+ export function configPath(homeDir = homedir()) {
77
+ return resolve(homeDir, CONFIG_FILE);
78
+ }
79
+ /**
80
+ * Resolve the absolute path of the single-line last-check file.
81
+ */
82
+ export function lastCheckPath(homeDir = homedir()) {
83
+ return resolve(homeDir, LAST_CHECK_FILE);
84
+ }
85
+ /**
86
+ * Read the persisted channel selection. Returns `null` when the
87
+ * config file is absent, the field is unset, or the file is unparse-
88
+ * able. The caller layers in the CLI flag + the hard default
89
+ * `DEFAULT_UPDATE_CHANNEL`.
90
+ *
91
+ * Defensive parse is intentional — a half-written config from a
92
+ * crashed session should never block `pugi update` from finishing the
93
+ * channel switch.
94
+ */
95
+ export function getUpdateChannel(homeDir = homedir()) {
96
+ const path = configPath(homeDir);
97
+ if (!existsSync(path))
98
+ return null;
99
+ try {
100
+ const raw = readFileSync(path, 'utf8');
101
+ const parsed = channelConfigSchema.parse(JSON.parse(raw));
102
+ return parsed.updateChannel ?? null;
103
+ }
104
+ catch {
105
+ return null;
106
+ }
107
+ }
108
+ /**
109
+ * Resolve the effective channel for an invocation. Resolution order:
110
+ *
111
+ * 1. `cliFlag` (when provided + parses to a known channel).
112
+ * 2. `~/.pugi/config.json::updateChannel`.
113
+ * 3. `DEFAULT_UPDATE_CHANNEL` (currently `beta`).
114
+ *
115
+ * An invalid `cliFlag` (e.g. `--channel yolo`) falls through to the
116
+ * next layer rather than crashing — the dispatcher already validates
117
+ * the flag up front and surfaces a deterministic error for unknown
118
+ * names. This helper exists for code paths (the doctor probe, the
119
+ * cold-start banner) where no CLI flag is in play and a silent fall-
120
+ * through is the correct behaviour.
121
+ */
122
+ export function resolveEffectiveChannel(options = {}) {
123
+ const cli = options.cliFlag;
124
+ if (cli && typeof cli === 'string') {
125
+ const trimmed = cli.trim().toLowerCase();
126
+ for (const channel of UPDATE_CHANNELS) {
127
+ if (channel === trimmed)
128
+ return channel;
129
+ }
130
+ }
131
+ const persisted = getUpdateChannel(options.homeDir ?? homedir());
132
+ if (persisted)
133
+ return persisted;
134
+ return DEFAULT_UPDATE_CHANNEL;
135
+ }
136
+ /**
137
+ * Persist the channel to `~/.pugi/config.json::updateChannel`. Creates
138
+ * `~/.pugi/` when missing; preserves any unrelated keys in the file
139
+ * (passthrough schema). Atomic tmp+rename so a kill mid-write never
140
+ * leaves the config half-flushed.
141
+ */
142
+ export function setUpdateChannel(channel, homeDir = homedir()) {
143
+ const path = configPath(homeDir);
144
+ mkdirSync(dirname(path), { recursive: true });
145
+ const existing = existsSync(path)
146
+ ? safeParseObject(readFileSync(path, 'utf8'))
147
+ : {};
148
+ const next = { ...existing, updateChannel: channel };
149
+ const tmpPath = `${path}.tmp`;
150
+ writeFileSync(tmpPath, `${JSON.stringify(next, null, 2)}\n`, {
151
+ encoding: 'utf8',
152
+ mode: 0o600,
153
+ });
154
+ renameSync(tmpPath, path);
155
+ }
156
+ /**
157
+ * Read the ISO timestamp of the most recent registry probe. Returns
158
+ * `null` when the file is absent or the contents do not parse as a
159
+ * valid Date. The caller treats `null` as "never checked" and runs an
160
+ * immediate probe.
161
+ */
162
+ export function readLastCheckedAt(homeDir = homedir()) {
163
+ const path = lastCheckPath(homeDir);
164
+ if (!existsSync(path))
165
+ return null;
166
+ try {
167
+ const raw = readFileSync(path, 'utf8').trim();
168
+ if (raw.length === 0)
169
+ return null;
170
+ const ts = Date.parse(raw);
171
+ if (!Number.isFinite(ts))
172
+ return null;
173
+ return new Date(ts);
174
+ }
175
+ catch {
176
+ return null;
177
+ }
178
+ }
179
+ /**
180
+ * Persist the timestamp of the most recent registry probe. Atomic
181
+ * tmp+rename for the same reasons as `setUpdateChannel` — the file is
182
+ * small but we keep the idiom uniform.
183
+ */
184
+ export function writeLastCheckedAt(when, homeDir = homedir()) {
185
+ const path = lastCheckPath(homeDir);
186
+ mkdirSync(dirname(path), { recursive: true });
187
+ const tmpPath = `${path}.tmp`;
188
+ writeFileSync(tmpPath, `${when.toISOString()}\n`, {
189
+ encoding: 'utf8',
190
+ mode: 0o600,
191
+ });
192
+ renameSync(tmpPath, path);
193
+ }
194
+ /**
195
+ * Decide whether the cold-start hint should run a fresh registry
196
+ * probe. Returns true when the last probe was more than
197
+ * `intervalHours` ago OR the timestamp file is missing entirely.
198
+ *
199
+ * Pass `intervalHours = 0` to force a probe on every call (used by
200
+ * the `pugi update --check` JSON surface where the operator is
201
+ * explicitly asking for a fresh result).
202
+ */
203
+ export function shouldCheckForUpdate(options = {}) {
204
+ const now = options.now ? options.now() : Date.now();
205
+ const intervalHours = options.intervalHours ?? UPDATE_CHECK_INTERVAL_HOURS;
206
+ if (intervalHours <= 0)
207
+ return true;
208
+ const last = readLastCheckedAt(options.homeDir ?? homedir());
209
+ if (!last)
210
+ return true;
211
+ const ageMs = now - last.getTime();
212
+ const windowMs = intervalHours * 60 * 60 * 1_000;
213
+ return ageMs >= windowMs;
214
+ }
215
+ /**
216
+ * Defensive helper — parse JSON to an object; non-object payloads
217
+ * (top-level array, primitive) collapse to an empty object so the
218
+ * channel-write merge does not surface a TypeError. Mirrors the
219
+ * `safeParseObject` in `core/permissions/state.ts` — duplicating the
220
+ * 10 lines is cheaper than threading a shared util module through
221
+ * two unrelated leak surfaces.
222
+ */
223
+ function safeParseObject(raw) {
224
+ try {
225
+ const parsed = JSON.parse(raw);
226
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
227
+ return parsed;
228
+ }
229
+ return {};
230
+ }
231
+ catch {
232
+ return {};
233
+ }
234
+ }
235
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1,89 @@
1
+ /**
2
+ * PUGI.md hierarchy probe — Leak L32 (2026-05-27).
3
+ *
4
+ * Surfaces how many ambient `PUGI.md` / `CLAUDE.md` files were
5
+ * discovered by the cwd → homedir walk-up. Operators triaging "why
6
+ * is the model not following my project conventions" can run
7
+ * `pugi doctor` and immediately see whether the hierarchy walk
8
+ * loaded the file they expected.
9
+ *
10
+ * Status semantics:
11
+ * - `skipped` when bare mode is active (the walk is deliberately
12
+ * disabled). The row still renders so the JSON schema stays
13
+ * stable for downstream consumers.
14
+ * - `skipped` when zero files were found. This is the default
15
+ * state on a clean machine and is NOT an error — most operators
16
+ * do not maintain a `~/PUGI.md`.
17
+ * - `ok` when one or more files were discovered. The detail names
18
+ * the closest file and the total count.
19
+ *
20
+ * Side effects:
21
+ * - One filesystem walk from cwd to homedir (bounded by the walker's
22
+ * depth cap). Each level performs at most 2 `existsSync` calls.
23
+ * Cost is single-digit ms even on cold cache; well inside the
24
+ * doctor probe wall-clock budget.
25
+ *
26
+ * Wired into `buildDefaultProbes` in `runtime/commands/doctor.ts`.
27
+ */
28
+ import { isBareMode } from '../../bare-mode/index.js';
29
+ import { walkUpPugiMd } from '../../pugi-md/walk-up.js';
30
+ export const PUGI_MD_DOCTOR_LABEL = 'PUGI.md HIERARCHY';
31
+ /** Detail string emitted when bare mode disables the walk. */
32
+ export const PUGI_MD_BARE_SKIP_DETAIL = 'skipped (--bare)';
33
+ /** Detail string emitted when the walk ran but found nothing. */
34
+ export const PUGI_MD_EMPTY_DETAIL = 'no PUGI.md / CLAUDE.md found in cwd → homedir';
35
+ export function probePugiMdHierarchy(input = {}) {
36
+ const env = input.env ?? process.env;
37
+ if (isBareMode(env)) {
38
+ return {
39
+ name: PUGI_MD_DOCTOR_LABEL,
40
+ status: 'skipped',
41
+ detail: PUGI_MD_BARE_SKIP_DETAIL,
42
+ };
43
+ }
44
+ const cwd = input.cwd ?? process.cwd();
45
+ const walk = input.walkImpl ?? ((c, o) => walkUpPugiMd(c, o));
46
+ let files;
47
+ try {
48
+ files = walk(cwd, input.homedir !== undefined ? { homedir: input.homedir } : {});
49
+ }
50
+ catch {
51
+ // Defensive: walker is wrapped in per-file try/catch already, so
52
+ // a throw here means a programmer error (bad input). Degrade to
53
+ // a `warn` row rather than crashing the doctor sweep.
54
+ return {
55
+ name: PUGI_MD_DOCTOR_LABEL,
56
+ status: 'warn',
57
+ detail: 'walk-up failed (see logs)',
58
+ };
59
+ }
60
+ if (files.length === 0) {
61
+ return {
62
+ name: PUGI_MD_DOCTOR_LABEL,
63
+ status: 'skipped',
64
+ detail: PUGI_MD_EMPTY_DETAIL,
65
+ };
66
+ }
67
+ // `files` is shallow-to-deep; the first entry is the closest to cwd.
68
+ // Operators reading the table care most about that one — it is the
69
+ // file that will most directly influence model behaviour. The
70
+ // additional count gives a quick "is the homedir / parent picked up
71
+ // too?" signal without listing every path.
72
+ const closest = files[0];
73
+ // closest is guaranteed non-undefined: files.length > 0 enforced
74
+ // by the early return above.
75
+ if (!closest) {
76
+ return {
77
+ name: PUGI_MD_DOCTOR_LABEL,
78
+ status: 'skipped',
79
+ detail: PUGI_MD_EMPTY_DETAIL,
80
+ };
81
+ }
82
+ const suffix = files.length === 1 ? '' : ` (+${files.length - 1} more)`;
83
+ return {
84
+ name: PUGI_MD_DOCTOR_LABEL,
85
+ status: 'ok',
86
+ detail: `${files.length} file${files.length === 1 ? '' : 's'}: ${closest.path}${suffix}`,
87
+ };
88
+ }
89
+ //# sourceMappingURL=pugi-md.js.map
@@ -18,6 +18,8 @@ import { buildContextPrefix, spliceContextPrefix } from './context-prefix.js';
18
18
  import { applyIntentMarker, classifyIntent } from './intent.js';
19
19
  import { loadTraversedMarkdown } from '../context/markdown-traverse.js';
20
20
  import { isBareMode } from '../bare-mode/index.js';
21
+ import { walkUpPugiMd } from '../pugi-md/walk-up.js';
22
+ import { renderAmbientContext } from '../pugi-md/context-injector.js';
21
23
  // α7 L11 (2026-05-27): per-session DenialTrackingState. One instance
22
24
  // per `run()` so denials cluster by (tool, args) within the same
23
25
  // command but do NOT leak across CLI invocations.
@@ -234,6 +236,30 @@ export class NativePugiEngineAdapter {
234
236
  // accurate; the REPL session retains the launch cwd for the
235
237
  // lifetime of the session which is what the operator expects.
236
238
  const cwdForTraverse = process.cwd();
239
+ // Leak L32 (2026-05-27): cwd → homedir walk-up that picks up every
240
+ // ambient `PUGI.md` (or `CLAUDE.md` as a fallback) the operator
241
+ // has placed above their workspace. This is the cross-project
242
+ // hierarchy walk — distinct from the workspace-bounded
243
+ // `loadTraversedMarkdown` below which only sees files INSIDE the
244
+ // workspace root. Render the concatenation once at session boot
245
+ // and prepend to the system prompt so the model treats the
246
+ // operator's personal guidance as ambient context for the whole
247
+ // session. `--bare` (Leak L22) skips this walk entirely.
248
+ let ambientContextBlock = '';
249
+ if (!isBareMode()) {
250
+ try {
251
+ const hierarchy = walkUpPugiMd(cwdForTraverse);
252
+ ambientContextBlock = renderAmbientContext(hierarchy);
253
+ }
254
+ catch {
255
+ // Pure FS surface — if it throws (programmer error in the
256
+ // walker, not a per-file fs error which is already swallowed
257
+ // inside) we drop ambient context for this session rather
258
+ // than crashing the engine loop. Doctor probe still surfaces
259
+ // the hierarchy state for operator triage.
260
+ ambientContextBlock = '';
261
+ }
262
+ }
237
263
  let traverseResult;
238
264
  // Leak L22 (2026-05-27): `--bare` skips the parent-dir PUGI.md /
239
265
  // AGENTS.md / CLAUDE.md / GEMINI.md walk-up. The engine sees only
@@ -548,7 +574,14 @@ export class NativePugiEngineAdapter {
548
574
  // pattern instead of re-issuing the same refused call.
549
575
  denialTracking,
550
576
  }),
551
- systemPrompt: systemPromptFor(kind),
577
+ // Leak L32 (2026-05-27): ambient `PUGI.md` hierarchy block
578
+ // prepended once at session boot. When the walk found
579
+ // nothing OR bare mode is on, `ambientContextBlock === ''`
580
+ // and the system prompt is unchanged — no leading blank
581
+ // line, no empty wrapper tag.
582
+ systemPrompt: ambientContextBlock
583
+ ? `${ambientContextBlock}\n\n${systemPromptFor(kind)}`
584
+ : systemPromptFor(kind),
552
585
  // β5a R5+R6+P1: per-turn `<context>` prefix + intent marker
553
586
  // applied above. Falls back to verbatim `task.prompt` when
554
587
  // both the prefix block is empty AND the intent classifier
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Aggregate byte cap on the full rendered block. 96 KB = 3 files at
3
+ * the per-file cap, which is enough for cwd + parent + homedir while
4
+ * leaving plenty of prompt budget for the rest of the system prompt.
5
+ * Anything beyond is replaced with a truncation marker.
6
+ */
7
+ export const MAX_INJECT_BYTES = 96 * 1024;
8
+ /**
9
+ * Marker line emitted when the aggregate cap is hit. Visible to the
10
+ * model so it knows ambient context was clipped; visible to the
11
+ * operator via the doctor probe so they can decide whether to trim
12
+ * their `PUGI.md` hierarchy.
13
+ */
14
+ export const TRUNCATION_MARKER = '<ambient-context-truncated reason="aggregate-cap" />';
15
+ /**
16
+ * Render a HierarchyFile array into the system-prompt block. Returns
17
+ * `''` when `files` is empty. Each file becomes one
18
+ * `<ambient-context source="..." level="...">...</ambient-context>`
19
+ * stanza separated by a single newline.
20
+ *
21
+ * Determinism: same input always produces byte-identical output.
22
+ */
23
+ export function renderAmbientContext(files) {
24
+ if (files.length === 0)
25
+ return '';
26
+ const stanzas = [];
27
+ let bytes = 0;
28
+ let truncated = false;
29
+ for (const file of files) {
30
+ const stanza = renderStanza(file);
31
+ const stanzaBytes = Buffer.byteLength(stanza, 'utf8') + 1; // newline join cost
32
+ if (bytes + stanzaBytes > MAX_INJECT_BYTES) {
33
+ truncated = true;
34
+ break;
35
+ }
36
+ stanzas.push(stanza);
37
+ bytes += stanzaBytes;
38
+ }
39
+ if (truncated)
40
+ stanzas.push(TRUNCATION_MARKER);
41
+ return stanzas.join('\n');
42
+ }
43
+ /**
44
+ * Build a single `<ambient-context>` stanza for one HierarchyFile.
45
+ * The `source` attribute carries the absolute path (after realpath)
46
+ * so the model can cite which file a piece of guidance came from
47
+ * when it explains its decisions to the operator.
48
+ */
49
+ function renderStanza(file) {
50
+ const sourceAttr = escapeAttr(file.path);
51
+ const levelAttr = String(file.level);
52
+ // No trailing newline inside `content` — the join adds one between
53
+ // stanzas. Trimming the file's trailing whitespace keeps the tag
54
+ // close to the content for readability when an engineer dumps the
55
+ // assembled prompt for debugging.
56
+ const trimmed = file.content.replace(/\s+$/g, '');
57
+ return [
58
+ `<ambient-context source="${sourceAttr}" level="${levelAttr}">`,
59
+ trimmed,
60
+ `</ambient-context>`,
61
+ ].join('\n');
62
+ }
63
+ /**
64
+ * Escape an XML attribute value. We expect operator-controlled paths
65
+ * (not adversarial input) but `&`, `"` and `<` are still possible in
66
+ * symlinked / unicode paths so we escape them defensively. The model
67
+ * has been trained to read this attribute as opaque metadata.
68
+ */
69
+ function escapeAttr(value) {
70
+ return value
71
+ .replace(/&/g, '&amp;')
72
+ .replace(/"/g, '&quot;')
73
+ .replace(/</g, '&lt;')
74
+ .replace(/>/g, '&gt;');
75
+ }
76
+ //# sourceMappingURL=context-injector.js.map
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Leak L32 (2026-05-27) — `PUGI.md` hierarchy walk-up to `$HOME`.
3
+ *
4
+ * Claude Code walks from `cwd` upward toward the user's homedir and
5
+ * concatenates every `CLAUDE.md` it finds at each intermediate level
6
+ * (deepest overrides shallowest). Pugi parity: same walk, looking for
7
+ * `PUGI.md` first at each level and accepting `CLAUDE.md` as a fallback
8
+ * — operators often have a leftover `~/CLAUDE.md` or a parent-dir
9
+ * `CLAUDE.md` from a previous Claude Code session and we want their
10
+ * ambient guidance picked up automatically without a migration step.
11
+ *
12
+ * Why this is a separate module from `core/context/markdown-traverse.ts`:
13
+ *
14
+ * - `markdown-traverse.ts` is the *workspace-bounded* walk (cwd → up
15
+ * to but NOT including `workspaceRoot`). It guards every read by
16
+ * `realpathSync` containment against the workspace root and
17
+ * refuses to escape — by design, because the per-dir markdown is
18
+ * part of the project's first-party context.
19
+ *
20
+ * - This module is the *home-bounded* walk (cwd → up to `homedir()`,
21
+ * OR until depth limit). It picks up the operator's personal /
22
+ * global guidance that lives ABOVE the workspace root. The two
23
+ * surfaces are complementary: workspace markdown encodes project
24
+ * conventions; this hierarchy walk encodes operator-level taste
25
+ * (preferred libraries, "always run prettier", style guides).
26
+ *
27
+ * Contract:
28
+ *
29
+ * - Walks from `cwd` upward. At each directory checks `PUGI.md`
30
+ * (preferred); when absent falls back to `CLAUDE.md`. Only ONE
31
+ * file per level is loaded — preferred wins.
32
+ * - Stops at `homedir()` INCLUSIVE — the file at `~/PUGI.md` or
33
+ * `~/CLAUDE.md` IS loaded (Claude Code parity: a `~/CLAUDE.md`
34
+ * applies to every project the operator opens).
35
+ * - Hard depth cap of `MAX_WALK_DEPTH` (20) directories regardless
36
+ * of how far cwd is from homedir; defense against symlinked or
37
+ * malicious cwd values.
38
+ * - Per-file byte cap `MAX_FILE_BYTES` (32 KB); over-cap files are
39
+ * truncated, not rejected, so a runaway `PUGI.md` does not break
40
+ * the prompt budget.
41
+ * - Returns shallow-to-deep order (cwd FIRST, homedir LAST). The
42
+ * caller is responsible for rendering precedence — Claude Code's
43
+ * rule is "deeper overrides shallower", which means the LAST
44
+ * entry in the rendered system prompt wins. Our order matches
45
+ * that convention so the context injector can splice directly.
46
+ *
47
+ * Safety:
48
+ *
49
+ * - No `realpath` on the directories themselves: the operator's
50
+ * cwd may live under a workspace symlink (common with macOS
51
+ * `/private/var/...`) and we want to honor what the operator
52
+ * sees in their shell. We DO resolve the candidate file via
53
+ * `realpathSync` before reading, but only to defeat
54
+ * symlinks-pointing-outside-homedir attacks; an off-tree symlink
55
+ * is skipped silently.
56
+ * - Catch + skip every fs error per file. The walk-up surface MUST
57
+ * NEVER break engine boot — missing read perms on a parent dir
58
+ * is the common case (e.g. `/etc` on a corp laptop) and the
59
+ * fallback is "no ambient context", not a crash.
60
+ *
61
+ * Pure module: no logging, no network, no fs writes.
62
+ */
63
+ import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
64
+ import { dirname, resolve } from 'node:path';
65
+ /**
66
+ * Hard ceiling on parent-dir traversal depth. 20 is generous — even
67
+ * deep monorepo layouts rarely sit more than 8-10 levels below the
68
+ * homedir on a developer's laptop. The cap exists so a misconfigured
69
+ * cwd (e.g. cwd outside the user's home filesystem entirely) cannot
70
+ * cause a multi-second fs scan of unrelated directories.
71
+ */
72
+ export const MAX_WALK_DEPTH = 20;
73
+ /**
74
+ * Per-file byte cap. 32 KB matches the per-dir markdown traverse
75
+ * aggregate budget — generous enough for a fully written-out
76
+ * project / personal `PUGI.md` (~8000 words) while keeping any one
77
+ * file from blowing the prompt budget on its own.
78
+ */
79
+ export const MAX_FILE_BYTES = 32 * 1024;
80
+ /**
81
+ * Filenames consulted at each level, in lookup order. `PUGI.md` is
82
+ * preferred — when both files coexist in a directory the Pugi-native
83
+ * file wins and the Claude Code shim is ignored. This is the same
84
+ * precedence used by `markdown-traverse.ts` for workspace-bounded
85
+ * walks; keeping the two surfaces consistent removes the "why does
86
+ * Pugi sometimes read CLAUDE.md and sometimes PUGI.md?" foot-gun.
87
+ */
88
+ export const HIERARCHY_SOURCES = ['PUGI.md', 'CLAUDE.md'];
89
+ /**
90
+ * Walk from `cwd` upward, collecting ambient `PUGI.md` / `CLAUDE.md`
91
+ * files at each level until we reach the homedir (inclusive) or the
92
+ * depth cap.
93
+ *
94
+ * Returns an array ordered shallowest-first (cwd → homedir). When no
95
+ * files are found, returns `[]`. When `cwd` is OUTSIDE the homedir
96
+ * tree (e.g. the operator runs `pugi` from `/tmp`), the walk still
97
+ * proceeds upward but stops the moment we reach a filesystem root
98
+ * without ever entering the homedir — useful for ops/admin invocations
99
+ * where there is genuinely no personal context to load.
100
+ */
101
+ export function walkUpPugiMd(cwd, opts = {}) {
102
+ const limit = clampLimit(opts.limit);
103
+ const home = opts.homedir;
104
+ let absCwd;
105
+ try {
106
+ absCwd = resolve(cwd);
107
+ }
108
+ catch {
109
+ return [];
110
+ }
111
+ const absHome = home ? resolve(home) : undefined;
112
+ const results = [];
113
+ let current = absCwd;
114
+ let level = 0;
115
+ const visited = new Set();
116
+ while (level <= limit) {
117
+ if (visited.has(current))
118
+ break; // pathological symlink loop guard
119
+ visited.add(current);
120
+ const found = tryLoadDirectory(current, level);
121
+ if (found)
122
+ results.push(found);
123
+ // Inclusive home boundary: load home if we are here, then stop.
124
+ if (absHome && current === absHome)
125
+ break;
126
+ const parent = dirname(current);
127
+ if (parent === current)
128
+ break; // hit filesystem root before homedir
129
+ current = parent;
130
+ level += 1;
131
+ }
132
+ return results;
133
+ }
134
+ /**
135
+ * Pick the first matching file in `dir`, read + cap it, and produce
136
+ * a HierarchyFile row. Returns `undefined` when no file in
137
+ * `HIERARCHY_SOURCES` exists or all reads error out (perms, symlink
138
+ * escape, etc.). NEVER throws — fs errors degrade to "no file at
139
+ * this level".
140
+ */
141
+ function tryLoadDirectory(dir, level) {
142
+ for (const source of HIERARCHY_SOURCES) {
143
+ const candidate = resolve(dir, source);
144
+ if (!existsSync(candidate))
145
+ continue;
146
+ // Realpath the FILE to defeat symlink-points-elsewhere attacks.
147
+ // We do not realpath the directory itself — operators often run
148
+ // pugi from inside a workspace symlink and the walk should honor
149
+ // the path they see.
150
+ let realPath;
151
+ try {
152
+ realPath = realpathSync(candidate);
153
+ }
154
+ catch {
155
+ // Broken symlink or perms issue on the link itself. Skip this
156
+ // file and try the next source in the same directory.
157
+ continue;
158
+ }
159
+ let rawBytes;
160
+ try {
161
+ rawBytes = statSync(realPath).size;
162
+ }
163
+ catch {
164
+ continue;
165
+ }
166
+ let content;
167
+ try {
168
+ content = readFileSync(realPath, 'utf8');
169
+ }
170
+ catch {
171
+ continue;
172
+ }
173
+ let truncated = false;
174
+ if (Buffer.byteLength(content, 'utf8') > MAX_FILE_BYTES) {
175
+ // Trim by character index sized to the byte cap. Mild over-trim
176
+ // on multi-byte boundaries is acceptable — we never under-trim
177
+ // (we'd exceed the cap) and the truncation is operator-visible
178
+ // via the `truncated` flag.
179
+ content = content.slice(0, MAX_FILE_BYTES);
180
+ truncated = true;
181
+ }
182
+ return {
183
+ path: realPath,
184
+ content,
185
+ level,
186
+ source,
187
+ truncated,
188
+ rawBytes,
189
+ };
190
+ }
191
+ return undefined;
192
+ }
193
+ /**
194
+ * Bound the limit to `[0, MAX_WALK_DEPTH]`. A negative or zero value
195
+ * still permits the cwd-level file to load (level 0 is always
196
+ * considered) — passing `limit: 0` means "current directory only".
197
+ */
198
+ function clampLimit(limit) {
199
+ if (typeof limit !== 'number' || !Number.isFinite(limit))
200
+ return MAX_WALK_DEPTH;
201
+ if (limit < 0)
202
+ return 0;
203
+ if (limit > MAX_WALK_DEPTH)
204
+ return MAX_WALK_DEPTH;
205
+ return Math.floor(limit);
206
+ }
207
+ //# sourceMappingURL=walk-up.js.map