@pugi/cli 0.1.0-beta.17 → 0.1.0-beta.19

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 (51) hide show
  1. package/dist/core/compact/auto-trigger.js +96 -0
  2. package/dist/core/compact/buffer-rewriter.js +115 -0
  3. package/dist/core/compact/summarizer.js +196 -0
  4. package/dist/core/compact/token-counter.js +108 -0
  5. package/dist/core/denial-tracking/index.js +8 -0
  6. package/dist/core/denial-tracking/state.js +264 -0
  7. package/dist/core/diagnostics/probe-runner.js +93 -0
  8. package/dist/core/diagnostics/probes/api.js +46 -0
  9. package/dist/core/diagnostics/probes/auth.js +86 -0
  10. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  11. package/dist/core/diagnostics/probes/config.js +72 -0
  12. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  13. package/dist/core/diagnostics/probes/disk.js +81 -0
  14. package/dist/core/diagnostics/probes/git.js +65 -0
  15. package/dist/core/diagnostics/probes/mcp.js +75 -0
  16. package/dist/core/diagnostics/probes/node.js +59 -0
  17. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  18. package/dist/core/diagnostics/probes/session.js +74 -0
  19. package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
  20. package/dist/core/diagnostics/probes/workspace.js +63 -0
  21. package/dist/core/diagnostics/types.js +70 -0
  22. package/dist/core/engine/native-pugi.js +20 -0
  23. package/dist/core/engine/strip-internal-fields.js +124 -0
  24. package/dist/core/engine/tool-bridge.js +251 -49
  25. package/dist/core/file-cache.js +113 -1
  26. package/dist/core/mcp/client.js +66 -6
  27. package/dist/core/mcp/registry.js +24 -2
  28. package/dist/core/permissions/gate.js +187 -0
  29. package/dist/core/permissions/index.js +18 -0
  30. package/dist/core/permissions/mode.js +102 -0
  31. package/dist/core/permissions/state.js +160 -0
  32. package/dist/core/permissions/tool-class.js +93 -0
  33. package/dist/core/repl/session.js +261 -9
  34. package/dist/core/repl/slash-commands.js +67 -4
  35. package/dist/runtime/cli.js +153 -58
  36. package/dist/runtime/commands/compact.js +296 -0
  37. package/dist/runtime/commands/doctor.js +369 -0
  38. package/dist/runtime/commands/mcp.js +290 -3
  39. package/dist/runtime/commands/permissions.js +87 -0
  40. package/dist/runtime/commands/status.js +178 -0
  41. package/dist/runtime/version.js +1 -1
  42. package/dist/tools/agent-tool.js +18 -4
  43. package/dist/tools/ask-user-question.js +213 -0
  44. package/dist/tools/file-tools.js +57 -14
  45. package/dist/tools/registry.js +7 -0
  46. package/dist/tui/ask-user-question-prompt.js +192 -0
  47. package/dist/tui/compact-banner.js +54 -0
  48. package/dist/tui/conversation-pane.js +68 -7
  49. package/dist/tui/doctor-table.js +31 -0
  50. package/dist/tui/status-table.js +7 -0
  51. package/package.json +2 -2
@@ -0,0 +1,57 @@
1
+ /**
2
+ * α7 L11 (2026-05-27) — DENIAL TRACKING probe for `pugi doctor`.
3
+ *
4
+ * Reports the current session's denial pressure: total denial count,
5
+ * unique (tool, args) patterns, and how many patterns have repeated
6
+ * past the reminder threshold. Operators read this to spot:
7
+ *
8
+ * - A hook script refusing more dispatches than expected (mis-
9
+ * configured `.pugi/hooks.json`).
10
+ * - Plan-mode runs where the model keeps trying mutating tools
11
+ * (a sign the prompt is not anchoring it correctly).
12
+ * - Stale-read loops indicating concurrent multi-agent writes.
13
+ *
14
+ * Status semantics:
15
+ *
16
+ * - `ok` when the tracker is empty OR carries denials but none have
17
+ * repeated past the threshold. Single denials are normal session
18
+ * hygiene; repeats are the signal.
19
+ * - `warn` when one or more patterns repeated >= the reminder
20
+ * threshold. The probe surfaces the count so the operator can act.
21
+ * - `skipped` when no tracker is wired (e.g. doctor invoked outside
22
+ * a live REPL session, top-level `pugi doctor`).
23
+ *
24
+ * Pure: takes a tracker snapshot — no I/O, no module-level state.
25
+ */
26
+ import { DENIAL_REMINDER_THRESHOLD, } from '../../denial-tracking/state.js';
27
+ export function probeDenialTracking(deps) {
28
+ if (!deps.tracker) {
29
+ return {
30
+ name: 'DENIAL TRACKING',
31
+ status: 'skipped',
32
+ detail: 'No live session — run `pugi doctor` from inside the REPL to see denials.',
33
+ };
34
+ }
35
+ const summary = deps.tracker.summary();
36
+ if (summary.totalDenials === 0) {
37
+ return {
38
+ name: 'DENIAL TRACKING',
39
+ status: 'ok',
40
+ detail: 'No tool denials this session.',
41
+ };
42
+ }
43
+ if (summary.repeatedPatterns === 0) {
44
+ return {
45
+ name: 'DENIAL TRACKING',
46
+ status: 'ok',
47
+ detail: `${summary.totalDenials} denial(s), ${summary.uniquePatterns} unique pattern(s), none repeated.`,
48
+ };
49
+ }
50
+ return {
51
+ name: 'DENIAL TRACKING',
52
+ status: 'warn',
53
+ detail: `${summary.totalDenials} denial(s), ${summary.repeatedPatterns} pattern(s) repeated >= ${DENIAL_REMINDER_THRESHOLD}.`,
54
+ remediation: 'Inspect via `/permissions denials` (when L6 lands) or check `.pugi/events.jsonl` for the latest refusals.',
55
+ };
56
+ }
57
+ //# sourceMappingURL=denial-tracking.js.map
@@ -0,0 +1,81 @@
1
+ /**
2
+ * DISK probe — warn the operator when the home partition is dangerously
3
+ * full. The session log, file cache, MCP working dirs, and the engine
4
+ * artifact bundle all land under `~/.pugi/`, so a full disk produces
5
+ * cryptic ENOSPC errors mid-dispatch. We catch that early.
6
+ *
7
+ * Implementation strategy: call `df -k` (POSIX-portable BSD tool with a
8
+ * stable column ordering) on the home dir and parse the available
9
+ * column. Bytes math + 1k blocks = simple integer arithmetic. We
10
+ * deliberately avoid `statvfs` because Node's stable surface for it
11
+ * (`fs.statfs` introduced in Node 18.15) returns BigInts that callers
12
+ * routinely mishandle on cross-platform builds.
13
+ *
14
+ * Thresholds:
15
+ * - `< 256 MiB` available → error (Pugi cannot do meaningful work)
16
+ * - `< 1 GiB` available → warn (operator should clear space soon)
17
+ * - otherwise → ok
18
+ */
19
+ const MIB = 1024 * 1024;
20
+ const GIB = MIB * 1024;
21
+ const ERROR_THRESHOLD_BYTES = 256 * MIB;
22
+ const WARN_THRESHOLD_BYTES = 1 * GIB;
23
+ export function probeDisk(ctx, deps) {
24
+ let freeBytes;
25
+ try {
26
+ freeBytes = deps.getFreeBytes(ctx.home);
27
+ }
28
+ catch (error) {
29
+ const message = error instanceof Error ? error.message : String(error);
30
+ return {
31
+ name: 'DISK',
32
+ status: 'warn',
33
+ detail: `Cannot determine free space on ${ctx.home}`,
34
+ remediation: `Inspection failed: ${message}`,
35
+ };
36
+ }
37
+ if (!Number.isFinite(freeBytes) || freeBytes < 0) {
38
+ return {
39
+ name: 'DISK',
40
+ status: 'warn',
41
+ detail: `df returned implausible value (${freeBytes}) for ${ctx.home}`,
42
+ };
43
+ }
44
+ const human = formatBytes(freeBytes);
45
+ if (freeBytes < ERROR_THRESHOLD_BYTES) {
46
+ return {
47
+ name: 'DISK',
48
+ status: 'error',
49
+ detail: `${human} free on home partition`,
50
+ remediation: 'Free disk space — Pugi writes ~/.pugi/sessions and cache files',
51
+ };
52
+ }
53
+ if (freeBytes < WARN_THRESHOLD_BYTES) {
54
+ return {
55
+ name: 'DISK',
56
+ status: 'warn',
57
+ detail: `${human} free on home partition`,
58
+ remediation: 'Consider clearing ~/.pugi/sessions older entries',
59
+ };
60
+ }
61
+ return {
62
+ name: 'DISK',
63
+ status: 'ok',
64
+ detail: `${human} free on home partition`,
65
+ };
66
+ }
67
+ /**
68
+ * Format bytes as `1.2GB` / `512MB` / `42KB`. Stays in IEC base-1024
69
+ * because that's what `df -k` returns and what operators reading the
70
+ * doctor table expect to see on their `df` follow-up.
71
+ */
72
+ export function formatBytes(bytes) {
73
+ if (bytes >= GIB)
74
+ return `${(bytes / GIB).toFixed(1)}GB`;
75
+ if (bytes >= MIB)
76
+ return `${Math.round(bytes / MIB)}MB`;
77
+ if (bytes >= 1024)
78
+ return `${Math.round(bytes / 1024)}KB`;
79
+ return `${bytes}B`;
80
+ }
81
+ //# sourceMappingURL=disk.js.map
@@ -0,0 +1,65 @@
1
+ /**
2
+ * GIT probe — verifies git is on PATH AND the current cwd is inside a
3
+ * git work tree. Reports the short HEAD sha + repo root for context.
4
+ *
5
+ * Pugi treats the workspace as a git project (worktree isolation,
6
+ * /codex review, /triple-review and the entire agent loop assume
7
+ * `git diff origin/main..HEAD` is meaningful). A workspace that is
8
+ * not a git work tree still works for read-only commands but the
9
+ * doctor surface flags this so the operator knows where the limits
10
+ * are.
11
+ */
12
+ export function probeGit(ctx, deps) {
13
+ let version;
14
+ try {
15
+ version = deps.resolveVersion();
16
+ }
17
+ catch (error) {
18
+ const message = error instanceof Error ? error.message : String(error);
19
+ return {
20
+ name: 'GIT',
21
+ status: 'warn',
22
+ detail: 'git not on PATH',
23
+ remediation: `Install git (error: ${message})`,
24
+ };
25
+ }
26
+ let inWorkTree = false;
27
+ try {
28
+ inWorkTree = deps.isInWorkTree(ctx.cwd);
29
+ }
30
+ catch {
31
+ inWorkTree = false;
32
+ }
33
+ if (!inWorkTree) {
34
+ return {
35
+ name: 'GIT',
36
+ status: 'warn',
37
+ detail: `${version} (cwd is not a git work tree)`,
38
+ remediation: 'Run `git init` to enable diff-based commands',
39
+ };
40
+ }
41
+ const sha = (() => {
42
+ try {
43
+ return deps.resolveHeadSha(ctx.cwd);
44
+ }
45
+ catch {
46
+ return null;
47
+ }
48
+ })();
49
+ const root = (() => {
50
+ try {
51
+ return deps.resolveRoot(ctx.cwd);
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ })();
57
+ const shaSuffix = sha ? ` @ ${sha.slice(0, 8)}` : '';
58
+ const rootSuffix = root ? ` (${root})` : '';
59
+ return {
60
+ name: 'GIT',
61
+ status: 'ok',
62
+ detail: `${version}${shaSuffix}${rootSuffix}`,
63
+ };
64
+ }
65
+ //# sourceMappingURL=git.js.map
@@ -0,0 +1,75 @@
1
+ /**
2
+ * MCP probe — reports the configured Model Context Protocol servers
3
+ * along with their trust + connection state.
4
+ *
5
+ * Sibling L13 (MCP server config) ships its own command surface; this
6
+ * probe consumes the existing `loadMcpRegistry` helper в a graceful
7
+ * try/catch so an unconfigured workspace (no `.pugi/mcp.json` OR an
8
+ * empty `servers: {}` map) lands as `ok` with detail "no servers
9
+ * configured" rather than a noisy failure row.
10
+ *
11
+ * Failure modes:
12
+ * - registry helper throws → `warn` with the error message;
13
+ * the table renderer surfaces the remediation hint;
14
+ * - one or more servers report `lastError` → `warn` summarising the
15
+ * count;
16
+ * - all configured servers connected cleanly → `ok` listing them.
17
+ */
18
+ export async function probeMcp(ctx, deps) {
19
+ let registry;
20
+ try {
21
+ // `connect: false` keeps the probe cheap — we only need the
22
+ // declared config + trust ledger entries, not live subprocess
23
+ // handshakes. The doctor sweep budget shouldn't be eaten by
24
+ // server-spawn latency.
25
+ registry = await deps.loadRegistry(ctx.cwd, { connect: false });
26
+ }
27
+ catch (error) {
28
+ const message = error instanceof Error ? error.message : String(error);
29
+ return {
30
+ name: 'MCP SERVERS',
31
+ status: 'warn',
32
+ detail: 'MCP registry not loadable (config or schema error)',
33
+ remediation: `Inspect .pugi/mcp.json: ${message}`,
34
+ };
35
+ }
36
+ const servers = Array.from(registry.servers.values());
37
+ // Best-effort shutdown — even with `connect: false` we still own the
38
+ // registry handle. Swallow errors so a clean-up failure never
39
+ // poisons the probe verdict.
40
+ await registry.shutdown().catch(() => { });
41
+ if (servers.length === 0) {
42
+ return {
43
+ name: 'MCP SERVERS',
44
+ status: 'ok',
45
+ detail: 'No MCP servers configured',
46
+ };
47
+ }
48
+ const labels = servers.map((server) => `${server.name}:${server.trust}`);
49
+ const trusted = servers.filter((server) => server.trust === 'trusted');
50
+ const pending = servers.filter((server) => server.trust === 'pending');
51
+ const denied = servers.filter((server) => server.trust === 'denied');
52
+ const failing = servers.filter((server) => typeof server.lastError === 'string' && server.lastError.length > 0);
53
+ if (failing.length > 0) {
54
+ return {
55
+ name: 'MCP SERVERS',
56
+ status: 'warn',
57
+ detail: `${failing.length}/${servers.length} server(s) reporting errors: ${labels.join(', ')}`,
58
+ remediation: `Inspect: \`pugi mcp list\``,
59
+ };
60
+ }
61
+ if (pending.length > 0) {
62
+ return {
63
+ name: 'MCP SERVERS',
64
+ status: 'warn',
65
+ detail: `${pending.length} pending trust decision(s): ${labels.join(', ')}`,
66
+ remediation: 'Run `pugi mcp trust <name>` or `pugi mcp deny <name>`',
67
+ };
68
+ }
69
+ return {
70
+ name: 'MCP SERVERS',
71
+ status: 'ok',
72
+ detail: `${trusted.length} trusted, ${denied.length} denied (${labels.join(', ')})`,
73
+ };
74
+ }
75
+ //# sourceMappingURL=mcp.js.map
@@ -0,0 +1,59 @@
1
+ /**
2
+ * NODE probe — verifies the running Node major version meets the
3
+ * `engines.node` floor declared in @pugi/cli's package.json.
4
+ *
5
+ * We bake the floor in as a constant rather than reading package.json
6
+ * at runtime because the published .tgz strips the file from a location
7
+ * the compiled bundle can reach (`--resolveJsonModule` is off for the
8
+ * CLI build). The lockstep is enforced by
9
+ * `scripts/check-version-lockstep.sh` in the publish pipeline.
10
+ */
11
+ /**
12
+ * Minimum supported Node major. Mirrors `engines.node` in
13
+ * apps/pugi-cli/package.json (`">=22.5.0"`).
14
+ */
15
+ export const MIN_NODE_MAJOR = 22;
16
+ export const MIN_NODE_MINOR = 5;
17
+ /**
18
+ * Parse a Node version string of the form `v<major>.<minor>.<patch>`.
19
+ * Returns null when the input doesn't match — the caller treats null
20
+ * as an error condition.
21
+ */
22
+ export function parseNodeVersion(version) {
23
+ const match = /^v(\d+)\.(\d+)\./.exec(version);
24
+ if (!match)
25
+ return null;
26
+ const major = Number(match[1]);
27
+ const minor = Number(match[2]);
28
+ if (!Number.isFinite(major) || !Number.isFinite(minor))
29
+ return null;
30
+ return { major, minor };
31
+ }
32
+ export function probeNode(input) {
33
+ const parsed = parseNodeVersion(input.version);
34
+ if (!parsed) {
35
+ return {
36
+ name: 'NODE',
37
+ status: 'error',
38
+ detail: `Unparseable Node version "${input.version}"`,
39
+ remediation: `Install Node >= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0 (current binary returns a non-semver version string)`,
40
+ };
41
+ }
42
+ const { major, minor } = parsed;
43
+ const passes = major > MIN_NODE_MAJOR ||
44
+ (major === MIN_NODE_MAJOR && minor >= MIN_NODE_MINOR);
45
+ if (passes) {
46
+ return {
47
+ name: 'NODE',
48
+ status: 'ok',
49
+ detail: `${input.version} (>= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0 required)`,
50
+ };
51
+ }
52
+ return {
53
+ name: 'NODE',
54
+ status: 'error',
55
+ detail: `${input.version} below floor >= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0`,
56
+ remediation: `Upgrade Node: \`nvm install ${MIN_NODE_MAJOR}\` or download from nodejs.org`,
57
+ };
58
+ }
59
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1,36 @@
1
+ /**
2
+ * PNPM probe — verifies pnpm is on PATH and reports its version. The
3
+ * customer-facing CLI doesn't strictly require pnpm at runtime
4
+ * (`@pugi/cli` is published as a regular npm package), but a missing
5
+ * pnpm means `pugi code` cannot run `pnpm test` / `pnpm typecheck`
6
+ * gates the agent loop emits. Surfacing this early prevents the
7
+ * agent from issuing a meaningless `command not found: pnpm` error
8
+ * three turns into a session.
9
+ */
10
+ export function probePnpm(deps) {
11
+ try {
12
+ const version = deps.resolveVersion();
13
+ if (!version) {
14
+ return {
15
+ name: 'PNPM',
16
+ status: 'warn',
17
+ detail: 'pnpm reported empty version string',
18
+ };
19
+ }
20
+ return {
21
+ name: 'PNPM',
22
+ status: 'ok',
23
+ detail: `pnpm ${version}`,
24
+ };
25
+ }
26
+ catch (error) {
27
+ const message = error instanceof Error ? error.message : String(error);
28
+ return {
29
+ name: 'PNPM',
30
+ status: 'warn',
31
+ detail: 'pnpm not on PATH — agent quality gates will be skipped',
32
+ remediation: `Install pnpm: \`npm i -g pnpm\` (error: ${message})`,
33
+ };
34
+ }
35
+ }
36
+ //# sourceMappingURL=pnpm.js.map
@@ -0,0 +1,74 @@
1
+ /**
2
+ * SESSION probe — reports the active session id + age when the doctor
3
+ * runs from inside the REPL OR finds a recent NDJSON session log in
4
+ * the workspace.
5
+ *
6
+ * The CLI command path has no live session context (each `pugi <cmd>`
7
+ * invocation is a fresh process), so we read `.pugi/events.jsonl` if
8
+ * present and report the most-recent event's age + total line count.
9
+ * Inside the REPL we pass an explicit `sessionId` so the probe
10
+ * surfaces the live state without re-reading disk.
11
+ *
12
+ * Absence of `.pugi/events.jsonl` is `skipped`, not an error — the
13
+ * operator may simply be running `pugi doctor` in a workspace that
14
+ * has not yet seen a dispatch.
15
+ */
16
+ export function probeSession(ctx, fs, deps) {
17
+ if (deps.liveSessionId) {
18
+ return {
19
+ name: 'SESSION',
20
+ status: 'ok',
21
+ detail: `session=${deps.liveSessionId} (live, REPL active)`,
22
+ };
23
+ }
24
+ const eventsPath = `${ctx.cwd}/.pugi/events.jsonl`;
25
+ if (!fs.existsSync(eventsPath)) {
26
+ return {
27
+ name: 'SESSION',
28
+ status: 'skipped',
29
+ detail: 'No prior dispatch logged in this workspace',
30
+ };
31
+ }
32
+ let stats;
33
+ try {
34
+ stats = fs.statSync(eventsPath);
35
+ }
36
+ catch (error) {
37
+ const message = error instanceof Error ? error.message : String(error);
38
+ return {
39
+ name: 'SESSION',
40
+ status: 'warn',
41
+ detail: `.pugi/events.jsonl present but unreadable`,
42
+ remediation: `Inspect: ${message}`,
43
+ };
44
+ }
45
+ const ageMs = Math.max(0, deps.now() - stats.mtimeMs);
46
+ const ageLabel = formatAge(ageMs);
47
+ // Counting lines is cheap on a small NDJSON file; a "huge" Pugi
48
+ // session is single-digit MB. We avoid loading binary blobs by
49
+ // simply walking the buffer count.
50
+ let lineCount = 0;
51
+ try {
52
+ const raw = fs.readFileSync(eventsPath, 'utf8');
53
+ lineCount = raw.split('\n').filter((line) => line.trim().length > 0).length;
54
+ }
55
+ catch {
56
+ lineCount = -1;
57
+ }
58
+ const linePart = lineCount >= 0 ? `, ${lineCount} event(s)` : '';
59
+ return {
60
+ name: 'SESSION',
61
+ status: 'ok',
62
+ detail: `last event ${ageLabel} ago${linePart}`,
63
+ };
64
+ }
65
+ export function formatAge(ms) {
66
+ if (ms < 60_000)
67
+ return `${Math.round(ms / 1000)}s`;
68
+ if (ms < 3_600_000)
69
+ return `${Math.round(ms / 60_000)}m`;
70
+ if (ms < 86_400_000)
71
+ return `${Math.round(ms / 3_600_000)}h`;
72
+ return `${Math.round(ms / 86_400_000)}d`;
73
+ }
74
+ //# sourceMappingURL=session.js.map