@pugi/cli 0.1.0-alpha.3 → 0.1.0-alpha.6
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/README.md +20 -0
- package/dist/commands/jobs.js +245 -0
- package/dist/core/agents/registry.js +69 -0
- package/dist/core/bash-classifier.js +1001 -0
- package/dist/core/context/builder.js +114 -0
- package/dist/core/context/compaction-events.js +99 -0
- package/dist/core/context/compaction.js +602 -0
- package/dist/core/context/invariants.js +250 -0
- package/dist/core/context/markdown-loader.js +270 -0
- package/dist/core/engine/compaction-hook.js +154 -0
- package/dist/core/engine/index.js +5 -0
- package/dist/core/engine/prompts.js +42 -0
- package/dist/core/engine/tool-bridge.js +159 -61
- package/dist/core/hooks.js +415 -0
- package/dist/core/jobs/registry.js +462 -0
- package/dist/core/mcp/client.js +316 -0
- package/dist/core/mcp/registry.js +171 -0
- package/dist/core/mcp/trust.js +91 -0
- package/dist/core/permission.js +221 -116
- package/dist/core/repl/cap-warning.js +91 -0
- package/dist/core/repl/session.js +399 -0
- package/dist/core/repl/slash-commands.js +116 -0
- package/dist/core/session.js +168 -0
- package/dist/core/subagents/dispatcher.js +258 -0
- package/dist/core/subagents/index.js +26 -0
- package/dist/core/subagents/spawn.js +86 -0
- package/dist/core/trust.js +109 -0
- package/dist/runtime/cli.js +158 -46
- package/dist/runtime/commands/budget.js +192 -0
- package/dist/runtime/commands/config.js +231 -0
- package/dist/runtime/commands/privacy.js +107 -0
- package/dist/runtime/commands/undo.js +329 -0
- package/dist/tools/bash.js +660 -0
- package/dist/tui/agent-tree.js +66 -0
- package/dist/tui/conversation-pane.js +45 -0
- package/dist/tui/input-box.js +91 -0
- package/dist/tui/login-picker.js +69 -0
- package/dist/tui/render.js +68 -0
- package/dist/tui/repl-render.js +218 -0
- package/dist/tui/repl.js +152 -0
- package/dist/tui/splash-data.js +61 -0
- package/dist/tui/splash.js +31 -0
- package/dist/tui/status-bar.js +58 -0
- package/package.json +11 -5
|
@@ -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
|