compact-agent 1.27.2 → 1.28.0

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/bin/crowcoder.js CHANGED
@@ -2,10 +2,9 @@
2
2
  // In-process best-effort: override process.emitWarning to drop DEP0040.
3
3
  // Catches the warning when it's emitted late (after this runs), but does NOT
4
4
  // catch warnings fired during Node's ESM bootstrap (before any user code).
5
- // For a fully clean stderr, invoke Crowcoder via:
5
+ // For a fully clean stderr, invoke compact-agent via:
6
6
  // node --no-deprecation bin/crowcoder.js
7
- // NODE_OPTIONS=--no-deprecation crowcoder
8
- // oag chat # already passes --no-deprecation
7
+ // NODE_OPTIONS=--no-deprecation compact-agent
9
8
  (() => {
10
9
  const orig = process.emitWarning;
11
10
  process.emitWarning = function patched(warning, ...rest) {
@@ -16,4 +15,50 @@
16
15
  return orig.call(this, warning, ...rest);
17
16
  };
18
17
  })();
18
+
19
+ // ── --debug [level] flag ───────────────────────────────────
20
+ // Parse here at the entry point so that the env var is set BEFORE
21
+ // any module-level code in dist/index.js reads it. The debug
22
+ // instrumentation in src/debug.ts checks $COMPACT_AGENT_DEBUG at
23
+ // init time; setting it from argv keeps the implementation in one
24
+ // place (env var as single source of truth).
25
+ //
26
+ // Accepted forms:
27
+ // --debug → info
28
+ // --debug=trace → trace
29
+ // --debug trace → trace
30
+ // --debug=on → info (alias)
31
+ // --debug=off → off
32
+ //
33
+ // Invalid levels fall back to 'info' with a one-line stderr notice.
34
+ (() => {
35
+ const argv = process.argv;
36
+ const VALID = new Set(['off', 'info', 'debug', 'trace', 'on']);
37
+ for (let i = 2; i < argv.length; i++) {
38
+ const a = argv[i];
39
+ if (a === '--debug') {
40
+ const next = argv[i + 1];
41
+ if (next && VALID.has(next.toLowerCase())) {
42
+ process.env.COMPACT_AGENT_DEBUG = next.toLowerCase() === 'on' ? 'info' : next.toLowerCase();
43
+ argv.splice(i, 2);
44
+ } else {
45
+ process.env.COMPACT_AGENT_DEBUG = 'info';
46
+ argv.splice(i, 1);
47
+ }
48
+ break;
49
+ }
50
+ if (a && a.startsWith('--debug=')) {
51
+ const lvl = a.slice('--debug='.length).toLowerCase();
52
+ if (VALID.has(lvl)) {
53
+ process.env.COMPACT_AGENT_DEBUG = lvl === 'on' ? 'info' : lvl;
54
+ } else {
55
+ process.stderr.write(`[compact-agent] unknown --debug level "${lvl}"; defaulting to 'info'.\n`);
56
+ process.env.COMPACT_AGENT_DEBUG = 'info';
57
+ }
58
+ argv.splice(i, 1);
59
+ break;
60
+ }
61
+ }
62
+ })();
63
+
19
64
  import('../dist/index.js');
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Debug instrumentation.
3
+ *
4
+ * Writes NDJSON event records to ~/.compact-agent/debug/<sessionId>.jsonl
5
+ * for offline analysis. Designed so that with `level === 'off'` (the
6
+ * default), every emit() call is a single boolean check + early return —
7
+ * zero file I/O and negligible CPU on the hot path.
8
+ *
9
+ * Levels (each includes everything below it):
10
+ *
11
+ * off — no logging (default; for end users)
12
+ * info — high-signal milestones: session start/end, API request/
13
+ * response summary, tool call summary, hook block, stream
14
+ * loop detection, mode/perm/model change
15
+ * debug — info + per-tool argument previews, per-chunk stream stats,
16
+ * per-keypress trace at REPL, slash command dispatch
17
+ * trace — debug + raw API payloads (truncated), full tool I/O,
18
+ * internal state transitions
19
+ *
20
+ * Toggling:
21
+ *
22
+ * - CLI flag: compact-agent --debug [level]
23
+ * - Env var: COMPACT_AGENT_DEBUG=trace
24
+ * - Slash cmd: /debug on [level] /debug off /debug tail [n]
25
+ *
26
+ * The event log is append-only. Each session gets its own file so
27
+ * concurrent compact-agent instances don't fight over a single sink.
28
+ * Files are subject to the same 24h GC window as the gateguard state.
29
+ *
30
+ * Design notes:
31
+ *
32
+ * - We deliberately don't use console.error or any third-party logger.
33
+ * console.error pollutes the user's REPL view; a third-party logger
34
+ * adds startup time + a transitive dep. Direct fs.appendFileSync to
35
+ * an NDJSON file is the lowest-overhead path that's also trivially
36
+ * consumable by jq / Python / another agent.
37
+ *
38
+ * - We DO accept the cost of synchronous appendFileSync — debug
39
+ * instrumentation is rarely on the hot path of perf-sensitive code,
40
+ * and the cost of one syscall per event (only when level >= info)
41
+ * is dwarfed by the cost of the API call the event describes.
42
+ *
43
+ * - Stack traces are NOT captured automatically. Each emit() takes an
44
+ * explicit `data` object — callers include whatever they need.
45
+ * Auto-traces would balloon the log file for limited extra value.
46
+ */
47
+ export type DebugLevel = 'off' | 'info' | 'debug' | 'trace';
48
+ /**
49
+ * Initialize debug at session start. Resolves level from (in priority order):
50
+ * 1. explicit `level` arg (passed in from --debug flag parsing)
51
+ * 2. $COMPACT_AGENT_DEBUG env var
52
+ * 3. existing state.level (typically 'off')
53
+ *
54
+ * Sets the log file path under ~/.compact-agent/debug/<sessionId>.jsonl
55
+ * but doesn't touch the filesystem until the first emit (lazy create).
56
+ */
57
+ export declare function initDebug(sessionId: string, explicitLevel?: DebugLevel | string): void;
58
+ /**
59
+ * Emit a debug event. The fastest path is the early-return when the
60
+ * configured level is below the event's level — a single integer compare.
61
+ *
62
+ * `data` is shallow-cloned to a JSON-safe form. Functions, BigInts, and
63
+ * circular refs are stripped via the replacer to keep emit cheap and
64
+ * crash-free regardless of what callers throw in.
65
+ */
66
+ export declare function emit(eventLevel: Exclude<DebugLevel, 'off'>, eventName: string, data?: Record<string, unknown>): void;
67
+ /**
68
+ * Public read-only state snapshot for /debug status output.
69
+ */
70
+ export declare function getDebugStatus(): {
71
+ level: DebugLevel;
72
+ logPath: string | null;
73
+ eventCount: number;
74
+ uptimeMs: number;
75
+ };
76
+ /**
77
+ * Change the active debug level at runtime. Called from /debug on/off.
78
+ * If transitioning OFF → non-off, we ensure the log dir exists; if
79
+ * transitioning to OFF we leave the file alone (it's append-only and
80
+ * read by external tools).
81
+ */
82
+ export declare function setDebugLevel(level: DebugLevel): void;
83
+ /**
84
+ * Read the last N events from the current session's log. Used by
85
+ * /debug tail. Returns lines as JSON strings (one per event); the
86
+ * caller can pretty-print or pass through to jq.
87
+ */
88
+ export declare function tailDebug(n: number): string[];
89
+ /**
90
+ * True if any non-off level is active. Use this to gate expensive
91
+ * data preparation that you don't want to run when debug is off:
92
+ *
93
+ * if (isDebugActive()) {
94
+ * emit('debug', 'event-name', { detail: computeExpensiveDetail() });
95
+ * }
96
+ */
97
+ export declare function isDebugActive(): boolean;
package/dist/debug.js ADDED
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Debug instrumentation.
3
+ *
4
+ * Writes NDJSON event records to ~/.compact-agent/debug/<sessionId>.jsonl
5
+ * for offline analysis. Designed so that with `level === 'off'` (the
6
+ * default), every emit() call is a single boolean check + early return —
7
+ * zero file I/O and negligible CPU on the hot path.
8
+ *
9
+ * Levels (each includes everything below it):
10
+ *
11
+ * off — no logging (default; for end users)
12
+ * info — high-signal milestones: session start/end, API request/
13
+ * response summary, tool call summary, hook block, stream
14
+ * loop detection, mode/perm/model change
15
+ * debug — info + per-tool argument previews, per-chunk stream stats,
16
+ * per-keypress trace at REPL, slash command dispatch
17
+ * trace — debug + raw API payloads (truncated), full tool I/O,
18
+ * internal state transitions
19
+ *
20
+ * Toggling:
21
+ *
22
+ * - CLI flag: compact-agent --debug [level]
23
+ * - Env var: COMPACT_AGENT_DEBUG=trace
24
+ * - Slash cmd: /debug on [level] /debug off /debug tail [n]
25
+ *
26
+ * The event log is append-only. Each session gets its own file so
27
+ * concurrent compact-agent instances don't fight over a single sink.
28
+ * Files are subject to the same 24h GC window as the gateguard state.
29
+ *
30
+ * Design notes:
31
+ *
32
+ * - We deliberately don't use console.error or any third-party logger.
33
+ * console.error pollutes the user's REPL view; a third-party logger
34
+ * adds startup time + a transitive dep. Direct fs.appendFileSync to
35
+ * an NDJSON file is the lowest-overhead path that's also trivially
36
+ * consumable by jq / Python / another agent.
37
+ *
38
+ * - We DO accept the cost of synchronous appendFileSync — debug
39
+ * instrumentation is rarely on the hot path of perf-sensitive code,
40
+ * and the cost of one syscall per event (only when level >= info)
41
+ * is dwarfed by the cost of the API call the event describes.
42
+ *
43
+ * - Stack traces are NOT captured automatically. Each emit() takes an
44
+ * explicit `data` object — callers include whatever they need.
45
+ * Auto-traces would balloon the log file for limited extra value.
46
+ */
47
+ import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync } from 'node:fs';
48
+ import { join } from 'node:path';
49
+ import { getHomeStateDir } from './config.js';
50
+ const LEVEL_ORDER = {
51
+ off: 0,
52
+ info: 1,
53
+ debug: 2,
54
+ trace: 3,
55
+ };
56
+ const state = {
57
+ level: 'off',
58
+ sessionId: null,
59
+ logPath: null,
60
+ eventCount: 0,
61
+ startedAt: Date.now(),
62
+ };
63
+ /**
64
+ * Initialize debug at session start. Resolves level from (in priority order):
65
+ * 1. explicit `level` arg (passed in from --debug flag parsing)
66
+ * 2. $COMPACT_AGENT_DEBUG env var
67
+ * 3. existing state.level (typically 'off')
68
+ *
69
+ * Sets the log file path under ~/.compact-agent/debug/<sessionId>.jsonl
70
+ * but doesn't touch the filesystem until the first emit (lazy create).
71
+ */
72
+ export function initDebug(sessionId, explicitLevel) {
73
+ state.sessionId = sessionId;
74
+ const envLevel = process.env.COMPACT_AGENT_DEBUG;
75
+ const requested = (explicitLevel || envLevel || state.level || 'off').toLowerCase();
76
+ state.level = isValidLevel(requested) ? requested : 'off';
77
+ if (state.level !== 'off') {
78
+ const dir = join(getHomeStateDir(), 'debug');
79
+ state.logPath = join(dir, `${sessionId}.jsonl`);
80
+ try {
81
+ mkdirSync(dir, { recursive: true });
82
+ }
83
+ catch { /* if we can't create the dir, emit() will fail-safe */ }
84
+ // GC: drop any debug files older than 24h.
85
+ try {
86
+ const cutoff = Date.now() - 24 * 60 * 60 * 1000;
87
+ for (const name of readdirSync(dir)) {
88
+ const p = join(dir, name);
89
+ try {
90
+ const s = statSync(p);
91
+ if (s.mtimeMs < cutoff)
92
+ unlinkSync(p);
93
+ }
94
+ catch { /* noop */ }
95
+ }
96
+ }
97
+ catch { /* noop */ }
98
+ emit('info', 'debug.init', { level: state.level, sessionId });
99
+ }
100
+ }
101
+ function isValidLevel(s) {
102
+ return s === 'off' || s === 'info' || s === 'debug' || s === 'trace';
103
+ }
104
+ /**
105
+ * Emit a debug event. The fastest path is the early-return when the
106
+ * configured level is below the event's level — a single integer compare.
107
+ *
108
+ * `data` is shallow-cloned to a JSON-safe form. Functions, BigInts, and
109
+ * circular refs are stripped via the replacer to keep emit cheap and
110
+ * crash-free regardless of what callers throw in.
111
+ */
112
+ export function emit(eventLevel, eventName, data) {
113
+ if (LEVEL_ORDER[state.level] < LEVEL_ORDER[eventLevel])
114
+ return;
115
+ if (!state.logPath)
116
+ return;
117
+ const record = {
118
+ t: new Date().toISOString(),
119
+ rel: Date.now() - state.startedAt,
120
+ lvl: eventLevel,
121
+ ev: eventName,
122
+ ...(data ? { data: safeClone(data) } : {}),
123
+ };
124
+ try {
125
+ appendFileSync(state.logPath, JSON.stringify(record) + '\n', 'utf-8');
126
+ state.eventCount++;
127
+ }
128
+ catch {
129
+ // If the log file write fails, we don't want to spam errors into
130
+ // the REPL — silently drop the event. The user can re-enable via
131
+ // /debug if the underlying issue is fixed.
132
+ }
133
+ }
134
+ /**
135
+ * JSON.stringify replacer that prevents crashes on circular refs,
136
+ * BigInt, functions, and overly-large strings. Truncates strings to
137
+ * 8KB so a single huge prompt or tool output doesn't bloat the log.
138
+ */
139
+ function safeClone(input) {
140
+ const seen = new WeakSet();
141
+ return JSON.parse(JSON.stringify(input, (_key, value) => {
142
+ if (typeof value === 'bigint')
143
+ return value.toString() + 'n';
144
+ if (typeof value === 'function')
145
+ return '[function]';
146
+ if (typeof value === 'string' && value.length > 8192) {
147
+ return value.slice(0, 8192) + `…[truncated ${value.length - 8192}b]`;
148
+ }
149
+ if (value && typeof value === 'object') {
150
+ if (seen.has(value))
151
+ return '[circular]';
152
+ seen.add(value);
153
+ }
154
+ return value;
155
+ }));
156
+ }
157
+ /**
158
+ * Public read-only state snapshot for /debug status output.
159
+ */
160
+ export function getDebugStatus() {
161
+ return {
162
+ level: state.level,
163
+ logPath: state.logPath,
164
+ eventCount: state.eventCount,
165
+ uptimeMs: Date.now() - state.startedAt,
166
+ };
167
+ }
168
+ /**
169
+ * Change the active debug level at runtime. Called from /debug on/off.
170
+ * If transitioning OFF → non-off, we ensure the log dir exists; if
171
+ * transitioning to OFF we leave the file alone (it's append-only and
172
+ * read by external tools).
173
+ */
174
+ export function setDebugLevel(level) {
175
+ const old = state.level;
176
+ state.level = level;
177
+ if (level !== 'off' && !state.logPath && state.sessionId) {
178
+ const dir = join(getHomeStateDir(), 'debug');
179
+ state.logPath = join(dir, `${state.sessionId}.jsonl`);
180
+ try {
181
+ mkdirSync(dir, { recursive: true });
182
+ }
183
+ catch { /* noop */ }
184
+ }
185
+ if (level !== 'off' && old !== level) {
186
+ emit('info', 'debug.level-change', { from: old, to: level });
187
+ }
188
+ }
189
+ /**
190
+ * Read the last N events from the current session's log. Used by
191
+ * /debug tail. Returns lines as JSON strings (one per event); the
192
+ * caller can pretty-print or pass through to jq.
193
+ */
194
+ export function tailDebug(n) {
195
+ if (!state.logPath || !existsSync(state.logPath))
196
+ return [];
197
+ try {
198
+ const raw = readFileSync(state.logPath, 'utf-8');
199
+ const lines = raw.split('\n').filter((l) => l.trim().length > 0);
200
+ return lines.slice(-Math.max(1, n));
201
+ }
202
+ catch {
203
+ return [];
204
+ }
205
+ }
206
+ /**
207
+ * True if any non-off level is active. Use this to gate expensive
208
+ * data preparation that you don't want to run when debug is off:
209
+ *
210
+ * if (isDebugActive()) {
211
+ * emit('debug', 'event-name', { detail: computeExpensiveDetail() });
212
+ * }
213
+ */
214
+ export function isDebugActive() {
215
+ return state.level !== 'off';
216
+ }
217
+ //# sourceMappingURL=debug.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.js","sourceRoot":"","sources":["../src/debug.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,OAAO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AAM,GAAG,GAAG,YAAY,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACjD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACjE,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC;AAC/B,CAAC"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { readFileSync as fsReadFileSync, writeFileSync as fsWriteFileSync, unlin
5
5
  import { tmpdir } from 'node:os';
6
6
  import { join as pathJoin } from 'node:path';
7
7
  import { spawnSync } from 'node:child_process';
8
+ import { initDebug, emit as dbgEmit, setDebugLevel, getDebugStatus, tailDebug } from './debug.js';
8
9
  import chalk from 'chalk';
9
10
  import { loadConfig, saveConfig, configExists, getConfigDir } from './config.js';
10
11
  import { resetClient } from './api.js';
@@ -544,6 +545,72 @@ export function handleSlashCommand(input, config, messages, session, mode) {
544
545
  console.log(chalk.dim(` Sending ${result.length} chars from editor…`));
545
546
  return { handled: true, injectPrompt: result };
546
547
  }
548
+ // ── Debug — toggle instrumentation + tail event log ──
549
+ // Surface for the NDJSON debug stream written to
550
+ // ~/.compact-agent/debug/<sessionId>.jsonl. Used by reviewers
551
+ // driving the agent + by users diagnosing their own issues.
552
+ //
553
+ // /debug show current level + log path + event count
554
+ // /debug on [level] turn instrumentation on (default level: info)
555
+ // /debug off turn instrumentation off (existing log file is kept)
556
+ // /debug tail [N] print the last N events (default 20) inline
557
+ case '/debug': {
558
+ const parts = args.trim().split(/\s+/).filter(Boolean);
559
+ const sub = (parts[0] || '').toLowerCase();
560
+ if (!sub) {
561
+ const s = getDebugStatus();
562
+ console.log(chalk.dim(` Debug level: ${s.level}`));
563
+ console.log(chalk.dim(` Log file: ${s.logPath || '(no log — level is off)'}`));
564
+ console.log(chalk.dim(` Events: ${s.eventCount} this session`));
565
+ console.log(chalk.dim(` Uptime: ${(s.uptimeMs / 1000).toFixed(1)}s`));
566
+ console.log('');
567
+ console.log(chalk.dim(` /debug on [info|debug|trace]`));
568
+ console.log(chalk.dim(` /debug off`));
569
+ console.log(chalk.dim(` /debug tail [N]`));
570
+ return { handled: true };
571
+ }
572
+ if (sub === 'off') {
573
+ setDebugLevel('off');
574
+ console.log(chalk.green(' Debug: off'));
575
+ return { handled: true };
576
+ }
577
+ if (sub === 'on') {
578
+ const lvl = (parts[1] || 'info').toLowerCase();
579
+ if (lvl !== 'info' && lvl !== 'debug' && lvl !== 'trace') {
580
+ console.log(chalk.yellow(` Unknown level "${lvl}". Use info, debug, or trace.`));
581
+ return { handled: true };
582
+ }
583
+ setDebugLevel(lvl);
584
+ const s = getDebugStatus();
585
+ console.log(chalk.green(` Debug: ${lvl} — writing to ${s.logPath}`));
586
+ return { handled: true };
587
+ }
588
+ if (sub === 'tail') {
589
+ const n = parseInt(parts[1] || '20', 10);
590
+ const lines = tailDebug(Number.isFinite(n) && n > 0 ? n : 20);
591
+ if (lines.length === 0) {
592
+ console.log(chalk.dim(' (no events — turn on with /debug on)'));
593
+ return { handled: true };
594
+ }
595
+ console.log(chalk.dim(` Last ${lines.length} debug events:`));
596
+ for (const ln of lines) {
597
+ try {
598
+ const rec = JSON.parse(ln);
599
+ const ts = String(rec.rel || 0).padStart(6) + 'ms';
600
+ const lvl = (rec.lvl || '').toUpperCase().padEnd(5);
601
+ const ev = rec.ev || '';
602
+ const d = rec.data ? ' ' + JSON.stringify(rec.data).slice(0, 120) : '';
603
+ console.log(chalk.dim(` ${ts} ${lvl} ${ev}${d}`));
604
+ }
605
+ catch {
606
+ console.log(chalk.dim(` ${ln.slice(0, 200)}`));
607
+ }
608
+ }
609
+ return { handled: true };
610
+ }
611
+ console.log(chalk.yellow(` Unknown /debug subcommand "${sub}". Try: /debug, /debug on, /debug off, /debug tail`));
612
+ return { handled: true };
613
+ }
547
614
  // ── History ───────────────────────────────────────
548
615
  case '/history': {
549
616
  const stats = getCompactionStats(messages);
@@ -2203,6 +2270,19 @@ async function main() {
2203
2270
  // Create session
2204
2271
  const mode = { current: 'dev' };
2205
2272
  const session = createSession(process.cwd(), config.model, config.provider, mode.current);
2273
+ // ── Debug instrumentation ─────────────────────────────────
2274
+ // Initialize early so subsequent emit() calls land in the right file.
2275
+ // Reads $COMPACT_AGENT_DEBUG which bin/crowcoder.js may have set from
2276
+ // the --debug CLI flag. Level 'off' is a no-op; non-off opens an
2277
+ // NDJSON log at ~/.compact-agent/debug/<sessionId>.jsonl.
2278
+ initDebug(session.id);
2279
+ dbgEmit('info', 'session.start', {
2280
+ cwd: process.cwd(),
2281
+ model: config.model,
2282
+ provider: config.provider,
2283
+ mode: mode.current,
2284
+ permissionMode: config.permissionMode,
2285
+ });
2206
2286
  const messages = [];
2207
2287
  // Session start hook + memory persistence
2208
2288
  await runHooks({ event: 'SessionStart', sessionId: session.id, cwd: process.cwd(), permissionMode: config.permissionMode });
@@ -2868,6 +2948,8 @@ async function main() {
2868
2948
  }
2869
2949
  // Slash commands
2870
2950
  if (trimmed.startsWith('/')) {
2951
+ // Truncate arg preview so debug logs don't blow up on /editor seeds etc.
2952
+ dbgEmit('debug', 'slash.dispatch', { input: trimmed.slice(0, 200) });
2871
2953
  const result = handleSlashCommand(trimmed, config, messages, session, mode);
2872
2954
  if (result.shouldExit)
2873
2955
  break;