@pugi/cli 0.1.0-alpha.10

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 (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -0
  3. package/bin/run.js +2 -0
  4. package/dist/commands/jobs.js +245 -0
  5. package/dist/core/agents/loader.js +104 -0
  6. package/dist/core/agents/registry.js +69 -0
  7. package/dist/core/auto-open-browser.js +128 -0
  8. package/dist/core/bash-classifier.js +1001 -0
  9. package/dist/core/clipboard.js +70 -0
  10. package/dist/core/context/builder.js +114 -0
  11. package/dist/core/context/compaction-events.js +99 -0
  12. package/dist/core/context/compaction.js +602 -0
  13. package/dist/core/context/invariants.js +250 -0
  14. package/dist/core/context/markdown-loader.js +270 -0
  15. package/dist/core/credentials.js +355 -0
  16. package/dist/core/engine/adapter-runner.js +8 -0
  17. package/dist/core/engine/anvil-client.js +156 -0
  18. package/dist/core/engine/compaction-hook.js +154 -0
  19. package/dist/core/engine/index.js +12 -0
  20. package/dist/core/engine/native-pugi.js +369 -0
  21. package/dist/core/engine/noop.js +27 -0
  22. package/dist/core/engine/prompts.js +118 -0
  23. package/dist/core/engine/tool-bridge.js +313 -0
  24. package/dist/core/file-cache.js +29 -0
  25. package/dist/core/hooks.js +415 -0
  26. package/dist/core/index-store.js +260 -0
  27. package/dist/core/jobs/registry.js +462 -0
  28. package/dist/core/mcp/client.js +316 -0
  29. package/dist/core/mcp/registry.js +171 -0
  30. package/dist/core/mcp/trust.js +91 -0
  31. package/dist/core/path-security.js +63 -0
  32. package/dist/core/permission.js +309 -0
  33. package/dist/core/repl/cap-warning.js +91 -0
  34. package/dist/core/repl/clipboard-read.js +174 -0
  35. package/dist/core/repl/history-search.js +175 -0
  36. package/dist/core/repl/history.js +172 -0
  37. package/dist/core/repl/kill-ring.js +138 -0
  38. package/dist/core/repl/session.js +618 -0
  39. package/dist/core/repl/slash-commands.js +227 -0
  40. package/dist/core/repl/workspace-context.js +113 -0
  41. package/dist/core/session.js +258 -0
  42. package/dist/core/settings.js +59 -0
  43. package/dist/core/skills/loader.js +454 -0
  44. package/dist/core/skills/sources.js +480 -0
  45. package/dist/core/skills/trust.js +172 -0
  46. package/dist/core/subagents/dispatcher.js +258 -0
  47. package/dist/core/subagents/index.js +26 -0
  48. package/dist/core/subagents/spawn.js +86 -0
  49. package/dist/core/trust.js +109 -0
  50. package/dist/index.js +8 -0
  51. package/dist/runtime/cli.js +3405 -0
  52. package/dist/runtime/commands/agents.js +385 -0
  53. package/dist/runtime/commands/budget.js +192 -0
  54. package/dist/runtime/commands/config.js +231 -0
  55. package/dist/runtime/commands/privacy.js +107 -0
  56. package/dist/runtime/commands/skills.js +401 -0
  57. package/dist/runtime/commands/undo.js +329 -0
  58. package/dist/runtime/update-check.js +294 -0
  59. package/dist/tools/bash.js +660 -0
  60. package/dist/tools/file-tools.js +346 -0
  61. package/dist/tools/registry.js +25 -0
  62. package/dist/tools/web-fetch.js +535 -0
  63. package/dist/tui/agent-tree.js +66 -0
  64. package/dist/tui/conversation-pane.js +45 -0
  65. package/dist/tui/device-flow.js +142 -0
  66. package/dist/tui/input-box.js +474 -0
  67. package/dist/tui/login-picker.js +69 -0
  68. package/dist/tui/render.js +125 -0
  69. package/dist/tui/repl-render.js +240 -0
  70. package/dist/tui/repl-splash-art.js +64 -0
  71. package/dist/tui/repl-splash.js +111 -0
  72. package/dist/tui/repl.js +214 -0
  73. package/dist/tui/slash-palette.js +106 -0
  74. package/dist/tui/splash-data.js +61 -0
  75. package/dist/tui/splash.js +31 -0
  76. package/dist/tui/status-bar.js +71 -0
  77. package/dist/tui/update-banner.js +8 -0
  78. package/dist/tui/workspace-context.js +105 -0
  79. package/package.json +71 -0
@@ -0,0 +1,70 @@
1
+ import { spawn } from 'node:child_process';
2
+ /**
3
+ * Write `text` to the platform clipboard. Best-effort, async.
4
+ */
5
+ export async function writeClipboard(text, deps = {}) {
6
+ const platform = deps.platform ?? process.platform;
7
+ const spawnWrite = deps.spawnWrite ?? defaultSpawnWrite;
8
+ if (platform === 'darwin') {
9
+ const ok = await spawnWrite('pbcopy', [], text);
10
+ return { copied: ok };
11
+ }
12
+ if (platform === 'win32') {
13
+ // clip.exe is shipped with every Windows >= XP. UTF-16LE would be
14
+ // ideal, but a UTF-8 BOM-less write covers the ASCII subset that
15
+ // device-flow URLs use (no non-ASCII characters reach this path).
16
+ const ok = await spawnWrite('clip', [], text);
17
+ return { copied: ok };
18
+ }
19
+ if (platform === 'linux' || platform === 'freebsd' || platform === 'openbsd') {
20
+ // Wayland-native sessions usually have wl-copy and not xclip. Try
21
+ // wl-copy first if WAYLAND_DISPLAY is set, otherwise prefer xclip.
22
+ const order = process.env.WAYLAND_DISPLAY
23
+ ? ['wl-copy', 'xclip', 'xsel']
24
+ : ['xclip', 'wl-copy', 'xsel'];
25
+ for (const tool of order) {
26
+ const args = tool === 'xclip' ? ['-selection', 'clipboard'] : tool === 'xsel' ? ['--clipboard', '--input'] : [];
27
+ const ok = await spawnWrite(tool, args, text);
28
+ if (ok)
29
+ return { copied: true };
30
+ }
31
+ return { copied: false };
32
+ }
33
+ return { copied: false };
34
+ }
35
+ /**
36
+ * Real spawn. Writes `text` to the child's stdin and resolves with
37
+ * `true` only when the child exits 0. Errors (missing binary,
38
+ * permission denied, non-zero exit) flip the result to `false`.
39
+ */
40
+ function defaultSpawnWrite(cmd, args, text) {
41
+ return new Promise((resolve) => {
42
+ let settled = false;
43
+ const settle = (value) => {
44
+ if (settled)
45
+ return;
46
+ settled = true;
47
+ resolve(value);
48
+ };
49
+ try {
50
+ const options = {
51
+ stdio: ['pipe', 'ignore', 'ignore'],
52
+ shell: false,
53
+ };
54
+ const child = spawn(cmd, args.slice(), options);
55
+ child.on('error', () => settle(false));
56
+ child.on('close', (code) => settle(code === 0));
57
+ const stdin = child.stdin;
58
+ if (!stdin) {
59
+ settle(false);
60
+ return;
61
+ }
62
+ stdin.on('error', () => settle(false));
63
+ stdin.end(text, 'utf8');
64
+ }
65
+ catch {
66
+ settle(false);
67
+ }
68
+ });
69
+ }
70
+ //# sourceMappingURL=clipboard.js.map
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Context builder — separates static and dynamic blocks so the model's
3
+ * prompt cache stays warm across turns.
4
+ *
5
+ * Per `docs/research/pugi-cli-corpus/patterns/context-compaction.md` §6,
6
+ * static and dynamic context live in different sections:
7
+ *
8
+ * STATIC DYNAMIC
9
+ * - system instructions - transcript turns (recent)
10
+ * - tool schemas (sorted) - session memory ref
11
+ * - safety rules - open task graph snapshot
12
+ * - PUGI.md + AGENTS.md
13
+ *
14
+ * Static blocks hash deterministically; identical session starts emit
15
+ * identical static prefixes, hitting the model provider's prompt cache.
16
+ * Dynamic blocks rebuild every turn — compaction touches dynamic only.
17
+ *
18
+ * This module is pure: no fs, no network. Markdown loading happens in
19
+ * `markdown-loader.ts` and is passed in. Hash inputs are sorted to keep
20
+ * the static hash stable under map-iteration-order changes.
21
+ */
22
+ import { createHash } from 'node:crypto';
23
+ /**
24
+ * Build a `BuiltContext` from input. Pure function: same input always
25
+ * produces byte-identical static blocks (and therefore byte-identical
26
+ * static hashes). Dynamic blocks are returned as-is in transcript order;
27
+ * compaction reshuffles dynamic only.
28
+ *
29
+ * Marked async to match the codebase convention even though the body is
30
+ * fully sync. Callers chain `await buildContext(...)` so a future move
31
+ * to async markdown reloading or RAG injection does not break callers.
32
+ */
33
+ export async function buildContext(input) {
34
+ const blocks = [];
35
+ // 1. Tools first — alphabetically sorted by name, per pattern card §6.
36
+ const sortedTools = [...input.tools].sort((a, b) => a.name.localeCompare(b.name));
37
+ const toolBundle = sortedTools.map((t) => `${t.name}:${t.schema}`).join('\n');
38
+ blocks.push({
39
+ kind: 'tool_schema',
40
+ name: 'tools',
41
+ content: toolBundle,
42
+ bytes: Buffer.byteLength(toolBundle, 'utf8'),
43
+ });
44
+ // 2. Instructions — single deterministic block.
45
+ const instructionsContent = normalizeNewlines(input.instructions);
46
+ blocks.push({
47
+ kind: 'instructions',
48
+ name: 'instructions',
49
+ content: instructionsContent,
50
+ bytes: Buffer.byteLength(instructionsContent, 'utf8'),
51
+ });
52
+ // 3. Safety rules — optional, hashed into the instructions block via
53
+ // concatenation when present so consumers can read both as one.
54
+ if (input.safetyRules) {
55
+ const safety = normalizeNewlines(input.safetyRules);
56
+ blocks.push({
57
+ kind: 'safety_rules',
58
+ name: 'safety',
59
+ content: safety,
60
+ bytes: Buffer.byteLength(safety, 'utf8'),
61
+ });
62
+ }
63
+ // 4. PUGI.md / AGENTS.md — loaded markdown in workspace-root order.
64
+ for (const md of input.markdown) {
65
+ blocks.push({
66
+ kind: md.source === 'PUGI.md' ? 'pugi_md' : 'agents_md',
67
+ name: md.source,
68
+ content: md.content,
69
+ bytes: Buffer.byteLength(md.content, 'utf8'),
70
+ });
71
+ }
72
+ const staticBytes = blocks.reduce((sum, b) => sum + b.bytes, 0);
73
+ const instructionsBlock = blocks.find((b) => b.kind === 'instructions');
74
+ const toolBlock = blocks.find((b) => b.kind === 'tool_schema');
75
+ const mdBlocks = blocks.filter((b) => b.kind === 'pugi_md' || b.kind === 'agents_md');
76
+ const instructionsHash = sha256(instructionsBlock?.content ?? '');
77
+ const toolSchemaHash = sha256(toolBundle);
78
+ const pugiMdHash = mdBlocks.length > 0 ? sha256(mdBlocks.map((b) => b.content).join('\n---\n')) : undefined;
79
+ const dynamicBytes = input.transcript.reduce((sum, t) => sum + Buffer.byteLength(t.content, 'utf8'), 0) +
80
+ (input.sessionMemory?.bytes ?? 0) +
81
+ estimateTaskGraphBytes(input.openTaskGraph);
82
+ const totalBytes = staticBytes + dynamicBytes;
83
+ return {
84
+ static: {
85
+ instructionsHash,
86
+ toolSchemaHash,
87
+ pugiMdHash,
88
+ blocks,
89
+ bytes: staticBytes,
90
+ },
91
+ dynamic: {
92
+ transcriptTurns: input.transcript,
93
+ sessionMemory: input.sessionMemory,
94
+ openTaskGraph: input.openTaskGraph,
95
+ bytes: dynamicBytes,
96
+ },
97
+ totalBytes,
98
+ estimatedTokens: Math.ceil(totalBytes / 4),
99
+ };
100
+ }
101
+ function sha256(input) {
102
+ return createHash('sha256').update(input, 'utf8').digest('hex');
103
+ }
104
+ function normalizeNewlines(input) {
105
+ // CRLF normalization plus trailing-newline strip so identical source
106
+ // emitted by different OS shells hashes identically.
107
+ return input.replace(/\r\n/g, '\n').replace(/\n+$/, '');
108
+ }
109
+ function estimateTaskGraphBytes(graph) {
110
+ if (!graph)
111
+ return 0;
112
+ return Buffer.byteLength(JSON.stringify(graph), 'utf8');
113
+ }
114
+ //# sourceMappingURL=builder.js.map
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Compaction event emitter — appends `compaction.*` events to the
3
+ * session's events.jsonl using the shared AuditEvent discriminated union
4
+ * in `@pugi/sdk/audit-trace.ts`.
5
+ *
6
+ * Until PR #307 these events were written with the ad-hoc shape
7
+ * `{ id, ts, sessionId, ... }` and the comment in this header claimed
8
+ * "we keep `session.ts` untouched and write our events through a
9
+ * dedicated helper that bypasses the AuditEvent schema". The downside,
10
+ * caught by Claude review on PR #307, is that every consumer that
11
+ * calls `auditEventSchema.parse(line)` (cabinet UI, replay tooling,
12
+ * `core/index-store.ts`) would throw on every compaction event, and
13
+ * `safeParse` consumers would silently drop them.
14
+ *
15
+ * Fix: extend the discriminated union with four compaction event
16
+ * shapes and emit using the `timestamp` (full word) field name to
17
+ * match every other event already in the schema. The events now
18
+ * round-trip through `auditEventSchema.parse` without loss.
19
+ *
20
+ * Events emitted:
21
+ *
22
+ * compaction.started { sessionId, tier, trigger }
23
+ * compaction.completed { sessionId, tier, bytesReclaimed,
24
+ * newContextSize, artifactsCreated }
25
+ * compaction.skipped { sessionId, tier, reason }
26
+ * compaction.invariant_violated { sessionId, invariant, evidence,
27
+ * artifactRef? }
28
+ *
29
+ * All four are appended in mode 0o600 and respect the
30
+ * `session.enabled` flag (no-op when `.pugi/` is absent).
31
+ */
32
+ import { appendFileSync } from 'node:fs';
33
+ import { randomUUID } from 'node:crypto';
34
+ export function emitCompactionStarted(session, tier, trigger) {
35
+ if (!session.enabled)
36
+ return;
37
+ appendCompactionEvent(session, {
38
+ type: 'compaction.started',
39
+ id: randomUUID(),
40
+ timestamp: nowIso(),
41
+ sessionId: session.id,
42
+ tier,
43
+ trigger,
44
+ });
45
+ }
46
+ export function emitCompactionCompleted(session, tier, bytesReclaimed, newContextSize, artifactsCreated) {
47
+ if (!session.enabled)
48
+ return;
49
+ appendCompactionEvent(session, {
50
+ type: 'compaction.completed',
51
+ id: randomUUID(),
52
+ timestamp: nowIso(),
53
+ sessionId: session.id,
54
+ tier,
55
+ bytesReclaimed,
56
+ newContextSize,
57
+ artifactsCreated: artifactsCreated.map((a) => ({
58
+ sha256: a.sha256,
59
+ size: a.size,
60
+ producedBy: a.producedBy,
61
+ })),
62
+ });
63
+ }
64
+ export function emitCompactionSkipped(session, tier, reason) {
65
+ if (!session.enabled)
66
+ return;
67
+ appendCompactionEvent(session, {
68
+ type: 'compaction.skipped',
69
+ id: randomUUID(),
70
+ timestamp: nowIso(),
71
+ sessionId: session.id,
72
+ tier,
73
+ reason,
74
+ });
75
+ }
76
+ export function emitCompactionInvariantViolated(session, violation) {
77
+ if (!session.enabled)
78
+ return;
79
+ const event = {
80
+ type: 'compaction.invariant_violated',
81
+ id: randomUUID(),
82
+ timestamp: nowIso(),
83
+ sessionId: session.id,
84
+ invariant: violation.invariant,
85
+ evidence: violation.evidence,
86
+ ...(violation.artifactRef !== undefined ? { artifactRef: violation.artifactRef } : {}),
87
+ };
88
+ appendCompactionEvent(session, event);
89
+ }
90
+ function appendCompactionEvent(session, event) {
91
+ appendFileSync(session.eventsPath, `${JSON.stringify(event)}\n`, {
92
+ encoding: 'utf8',
93
+ mode: 0o600,
94
+ });
95
+ }
96
+ function nowIso() {
97
+ return new Date().toISOString();
98
+ }
99
+ //# sourceMappingURL=compaction-events.js.map