@blockrun/franklin 3.15.19 → 3.15.21

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.
@@ -3,17 +3,25 @@ import path from 'node:path';
3
3
  import chalk from 'chalk';
4
4
  import { BLOCKRUN_DIR } from '../config.js';
5
5
  const LOG_FILE = path.join(BLOCKRUN_DIR, 'franklin-debug.log');
6
+ const ARCHIVE_LOG_FILE = path.join(BLOCKRUN_DIR, 'franklin-debug.log.1');
6
7
  const LEGACY_LOG_FILE = path.join(BLOCKRUN_DIR, 'runcode-debug.log');
7
- const MAX_LOG_SIZE = 10 * 1024 * 1024; // 10MB auto-rotate threshold
8
8
  export function logsCommand(options) {
9
9
  if (options.clear) {
10
+ let cleared = false;
10
11
  try {
11
12
  fs.unlinkSync(LOG_FILE);
12
- console.log(chalk.green('Logs cleared.'));
13
+ cleared = true;
13
14
  }
14
- catch {
15
- console.log(chalk.dim('No log file to clear.'));
15
+ catch { /* may not exist */ }
16
+ try {
17
+ fs.unlinkSync(ARCHIVE_LOG_FILE);
18
+ cleared = true;
16
19
  }
20
+ catch { /* may not exist */ }
21
+ if (cleared)
22
+ console.log(chalk.green('Logs cleared.'));
23
+ else
24
+ console.log(chalk.dim('No log file to clear.'));
17
25
  return;
18
26
  }
19
27
  // Migrate legacy log file
@@ -23,23 +31,17 @@ export function logsCommand(options) {
23
31
  }
24
32
  catch { /* best effort */ }
25
33
  }
26
- if (!fs.existsSync(LOG_FILE)) {
34
+ // Logger now self-rotates on write (in src/logger.ts). The previous
35
+ // in-place "slice off the first half" rotation here was destructive
36
+ // — every invocation that crossed 10 MB silently dropped half the
37
+ // history. With self-rotation in place this command no longer needs
38
+ // to mutate the file at all; it just stitches the archive + live
39
+ // log for display.
40
+ if (!fs.existsSync(LOG_FILE) && !fs.existsSync(ARCHIVE_LOG_FILE)) {
27
41
  console.log(chalk.dim('No logs yet. Start franklin with --debug to enable logging:'));
28
42
  console.log(chalk.bold(' franklin start --debug'));
29
43
  return;
30
44
  }
31
- // Auto-rotate: if file is over threshold, keep only last half
32
- try {
33
- const stat = fs.statSync(LOG_FILE);
34
- if (stat.size > MAX_LOG_SIZE) {
35
- const content = fs.readFileSync(LOG_FILE, 'utf-8');
36
- const lines = content.split('\n');
37
- const half = lines.slice(Math.floor(lines.length / 2));
38
- fs.writeFileSync(LOG_FILE, half.join('\n'));
39
- console.log(chalk.dim(`(Rotated log — was ${(stat.size / 1024 / 1024).toFixed(1)}MB)`));
40
- }
41
- }
42
- catch { /* ignore rotation errors */ }
43
45
  const parsed = parseInt(options.lines || '50', 10);
44
46
  const tailLines = isNaN(parsed) ? 50 : Math.max(1, Math.min(10000, parsed));
45
47
  if (options.follow) {
@@ -78,8 +80,15 @@ export function logsCommand(options) {
78
80
  }
79
81
  function printLastLines(n) {
80
82
  try {
81
- const content = fs.readFileSync(LOG_FILE, 'utf-8');
82
- const lines = content.split('\n').filter(Boolean);
83
+ // Logger self-rotates to franklin-debug.log.1 when the live log
84
+ // crosses 10MB. Stitch the archive on first so requests for "last N"
85
+ // can span the rotation boundary — without this, immediately after
86
+ // a rotation `franklin logs --lines 1000` would show only whatever
87
+ // lines have been written since rotation, even though the archive
88
+ // is sitting right next to it.
89
+ const archive = fs.existsSync(ARCHIVE_LOG_FILE) ? fs.readFileSync(ARCHIVE_LOG_FILE, 'utf-8') : '';
90
+ const live = fs.existsSync(LOG_FILE) ? fs.readFileSync(LOG_FILE, 'utf-8') : '';
91
+ const lines = (archive + live).split('\n').filter(Boolean);
83
92
  const start = Math.max(0, lines.length - n);
84
93
  const slice = lines.slice(start);
85
94
  if (start > 0) {
package/dist/logger.js CHANGED
@@ -14,6 +14,20 @@ import fs from 'node:fs';
14
14
  import path from 'node:path';
15
15
  import { BLOCKRUN_DIR } from './config.js';
16
16
  const LOG_FILE = path.join(BLOCKRUN_DIR, 'franklin-debug.log');
17
+ const ARCHIVE_FILE = path.join(BLOCKRUN_DIR, 'franklin-debug.log.1');
18
+ // Self-rotation threshold. When the live log crosses this size on a
19
+ // write, rename it to franklin-debug.log.1 (overwriting any previous
20
+ // archive) and start fresh. Non-destructive: one full archive of the
21
+ // most recent ROTATE_AT_BYTES is always retained, so users can still
22
+ // read history across the rotation. Earlier behavior (only triggered
23
+ // by `franklin logs`, sliced the file in half in-place) lost history
24
+ // outright and only ran if the user happened to invoke `franklin logs`.
25
+ const ROTATE_AT_BYTES = 10 * 1024 * 1024; // 10 MB
26
+ // Probe every N writes to amortize the stat() — average debug entry is
27
+ // ~80 bytes, so 1000 writes (~80 KB worth) between checks keeps the
28
+ // overhead negligible while still catching a runaway log within seconds.
29
+ const ROTATE_PROBE_EVERY_N_WRITES = 1000;
30
+ let writesSinceRotateProbe = 0;
17
31
  // Strip ANSI escapes + carriage returns so the log stays grep-able.
18
32
  const ANSI_RE = /\x1b\[[0-9;]*m|\x1b\][^\x07]*\x07|\r/g;
19
33
  let debugMode = false;
@@ -36,9 +50,42 @@ function ensureDir() {
36
50
  }
37
51
  catch { /* readonly mount / disk full — keep trying so a remount recovers */ }
38
52
  }
53
+ function maybeRotate() {
54
+ try {
55
+ if (!fs.existsSync(LOG_FILE))
56
+ return;
57
+ const { size } = fs.statSync(LOG_FILE);
58
+ if (size < ROTATE_AT_BYTES)
59
+ return;
60
+ // renameSync overwrites an existing target on POSIX, which is what
61
+ // we want — single archive, always the most recent rotation. On
62
+ // Windows the rename can EEXIST; in that case unlink the archive
63
+ // first and retry. If even that fails, fall through silently rather
64
+ // than leaving the log file in a half-rotated state.
65
+ try {
66
+ fs.renameSync(LOG_FILE, ARCHIVE_FILE);
67
+ }
68
+ catch {
69
+ try {
70
+ fs.unlinkSync(ARCHIVE_FILE);
71
+ }
72
+ catch { /* may not exist */ }
73
+ try {
74
+ fs.renameSync(LOG_FILE, ARCHIVE_FILE);
75
+ }
76
+ catch { /* give up */ }
77
+ }
78
+ }
79
+ catch { /* best effort */ }
80
+ }
39
81
  function writeFile(level, msg) {
40
82
  ensureDir();
41
83
  try {
84
+ writesSinceRotateProbe++;
85
+ if (writesSinceRotateProbe >= ROTATE_PROBE_EVERY_N_WRITES) {
86
+ writesSinceRotateProbe = 0;
87
+ maybeRotate();
88
+ }
42
89
  const clean = msg.replace(ANSI_RE, '');
43
90
  fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] [${level.toUpperCase()}] ${clean}\n`);
44
91
  }
@@ -2,19 +2,7 @@
2
2
  * Fallback chain for Franklin
3
3
  * Automatically switches to backup models when primary fails (429, 5xx, etc.)
4
4
  */
5
- import fs from 'node:fs';
6
- import os from 'node:os';
7
- import path from 'node:path';
8
- const LOG_FILE = path.join(os.homedir(), '.blockrun', 'franklin-debug.log');
9
- // eslint-disable-next-line no-control-regex
10
- const ANSI_RE = /\x1B\[[0-9;]*[A-Za-z]|\x1B\][^\x07]*\x07|\x1B[()][A-B]|\r/g;
11
- function appendLog(msg) {
12
- try {
13
- fs.mkdirSync(path.dirname(LOG_FILE), { recursive: true });
14
- fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${msg.replace(ANSI_RE, '')}\n`);
15
- }
16
- catch { /* ignore */ }
17
- }
5
+ import { logger } from '../logger.js';
18
6
  export const DEFAULT_FALLBACK_CONFIG = {
19
7
  chain: [
20
8
  'deepseek/deepseek-chat', // Direct fallback — cheap & reliable
@@ -93,7 +81,7 @@ export async function fetchWithFallback(url, init, originalBody, config = DEFAUL
93
81
  if (nextModel && onFallback) {
94
82
  const errMsg = err instanceof Error ? err.message : 'Network error';
95
83
  onFallback(model, 0, nextModel);
96
- appendLog(`[franklin] [fallback] ${model} network error: ${errMsg}`);
84
+ logger.warn(`[franklin] [fallback] ${model} network error: ${errMsg}`);
97
85
  }
98
86
  if (i < config.chain.length - 1) {
99
87
  await sleep(config.retryDelayMs);
@@ -1,7 +1,4 @@
1
1
  import http from 'node:http';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import os from 'node:os';
5
2
  import { getOrCreateWallet, getOrCreateSolanaWallet, createPaymentPayload, createSolanaPaymentPayload, parsePaymentRequired, extractPaymentDetails, solanaKeyToBytes, SOLANA_NETWORK, } from '@blockrun/llm';
6
3
  import { recordUsage } from '../stats/tracker.js';
7
4
  import { appendAudit } from '../stats/audit.js';
@@ -12,34 +9,13 @@ import { VERSION } from '../config.js';
12
9
  // User-Agent for backend requests
13
10
  const USER_AGENT = `franklin/${VERSION}`;
14
11
  const X_FRANKLIN_VERSION = VERSION;
15
- const LOG_FILE = path.join(os.homedir(), '.blockrun', 'franklin-debug.log');
16
- // Strip ANSI escape codes so log file doesn't distort terminal on replay
17
- function stripAnsi(str) {
18
- // eslint-disable-next-line no-control-regex
19
- return str.replace(/\x1B\[[0-9;]*[A-Za-z]|\x1B\][^\x07]*\x07|\x1B[()][A-B]|\r/g, '');
20
- }
21
- function debug(options, ...args) {
22
- if (!options.debug)
23
- return;
24
- const msg = `[${new Date().toISOString()}] ${stripAnsi(args.map(String).join(' '))}\n`;
25
- try {
26
- fs.mkdirSync(path.dirname(LOG_FILE), { recursive: true });
27
- fs.appendFileSync(LOG_FILE, msg);
28
- }
29
- catch {
30
- /* ignore */
31
- }
32
- }
33
- function log(...args) {
34
- const msg = `[franklin] ${args.map(String).join(' ')}`;
35
- // Do NOT print to stdout — the terminal is owned by the parent process (stdio: inherit).
36
- // Use `franklin logs` to read runtime messages.
37
- try {
38
- fs.mkdirSync(path.dirname(LOG_FILE), { recursive: true });
39
- fs.appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${stripAnsi(msg)}\n`);
40
- }
41
- catch { /* ignore */ }
42
- }
12
+ // Logging here goes through the unified logger introduced in 3.15.11
13
+ // (timestamp + [LEVEL] tag, self-rotating at 10 MB, optional stderr
14
+ // mirror in debug mode). The previous per-module debug()/log() helpers
15
+ // duplicated the file path, ANSI strip regex (with a slightly different
16
+ // pattern!), and timestamp format — bug fixes never propagated. They
17
+ // were the last holdouts after the agent loop was migrated.
18
+ import { logger, setDebugMode } from '../logger.js';
43
19
  const DEFAULT_MAX_TOKENS = 4096;
44
20
  // 180s budget for *time-to-headers* — reasoning-class models (zai/glm-*,
45
21
  // nemotron *-reasoning, deepseek-r*, gpt-5-codex, anthropic extended-thinking)
@@ -249,6 +225,10 @@ function withinRateLimit() {
249
225
  return true;
250
226
  }
251
227
  export function createProxy(options) {
228
+ // Wire stderr-mirroring of unified logger output to the proxy's debug
229
+ // flag — same pattern as interactiveSession in agent/loop. File writes
230
+ // happen regardless; only the live stderr mirror is gated.
231
+ setDebugMode(!!options.debug);
252
232
  const chain = options.chain || 'base';
253
233
  let currentModel = options.modelOverride || DEFAULT_MODEL;
254
234
  const fallbackEnabled = options.fallbackEnabled !== false; // Default true
@@ -307,19 +287,22 @@ export function createProxy(options) {
307
287
  let requestModel = currentModel || options.modelOverride || 'unknown';
308
288
  let usedFallback = false;
309
289
  try {
310
- debug(options, `request: ${req.method} ${req.url} currentModel=${currentModel || 'none'}`);
290
+ if (options.debug)
291
+ logger.debug(`[franklin] request: ${req.method} ${req.url} currentModel=${currentModel || 'none'}`);
311
292
  if (body) {
312
293
  try {
313
294
  const parsed = JSON.parse(body);
314
295
  // Intercept "use <model>" commands for in-session model switching
315
296
  if (parsed.messages) {
316
297
  const last = parsed.messages[parsed.messages.length - 1];
317
- debug(options, `last msg role=${last?.role} content-type=${typeof last?.content} content=${JSON.stringify(last?.content).slice(0, 200)}`);
298
+ if (options.debug)
299
+ logger.debug(`[franklin] last msg role=${last?.role} content-type=${typeof last?.content} content=${JSON.stringify(last?.content).slice(0, 200)}`);
318
300
  }
319
301
  const switchCmd = detectModelSwitch(parsed);
320
302
  if (switchCmd) {
321
303
  currentModel = switchCmd;
322
- debug(options, `model switched to: ${currentModel}`);
304
+ if (options.debug)
305
+ logger.debug(`[franklin] model switched to: ${currentModel}`);
323
306
  const fakeResponse = {
324
307
  id: `msg_franklin_${Date.now()}`,
325
308
  type: 'message',
@@ -374,7 +357,7 @@ export function createProxy(options) {
374
357
  const routing = routeRequest(promptText, routingProfile);
375
358
  parsed.model = routing.model;
376
359
  requestModel = routing.model;
377
- log(`🧠 Smart routing: ${routingProfile} → ${routing.tier} → ${routing.model} ` +
360
+ logger.info(`[franklin] 🧠 Smart routing: ${routingProfile} → ${routing.tier} → ${routing.model} ` +
378
361
  `(${(routing.savings * 100).toFixed(0)}% savings) [${routing.signals.join(', ')}]`);
379
362
  }
380
363
  {
@@ -392,8 +375,8 @@ export function createProxy(options) {
392
375
  ? Math.max(lastOut * 2, DEFAULT_MAX_TOKENS)
393
376
  : DEFAULT_MAX_TOKENS;
394
377
  parsed.max_tokens = Math.min(adaptive, modelCap);
395
- if (original !== parsed.max_tokens) {
396
- debug(options, `max_tokens: ${original || 'unset'} → ${parsed.max_tokens} (last output: ${lastOut || 'none'})`);
378
+ if (original !== parsed.max_tokens && options.debug) {
379
+ logger.debug(`[franklin] max_tokens: ${original || 'unset'} → ${parsed.max_tokens} (last output: ${lastOut || 'none'})`);
397
380
  }
398
381
  }
399
382
  body = JSON.stringify(parsed);
@@ -430,7 +413,7 @@ export function createProxy(options) {
430
413
  body = JSON.stringify(parsed);
431
414
  }
432
415
  catch { /* body not JSON, skip */ }
433
- log(`⚠️ Safety net: resolved unrouted ${virtualName} → ${requestModel}`);
416
+ logger.warn(`[franklin] ⚠️ Safety net: resolved unrouted ${virtualName} → ${requestModel}`);
434
417
  }
435
418
  }
436
419
  // Build request init
@@ -456,7 +439,7 @@ export function createProxy(options) {
456
439
  solanaWallet,
457
440
  timeoutMs: requestTimeoutMs,
458
441
  }, (failedModel, status, nextModel) => {
459
- log(`⚠️ ${failedModel} returned ${status}, falling back to ${nextModel}`);
442
+ logger.warn(`[franklin] ⚠️ ${failedModel} returned ${status}, falling back to ${nextModel}`);
460
443
  });
461
444
  response = result.response;
462
445
  finalModel = result.modelUsed;
@@ -464,7 +447,7 @@ export function createProxy(options) {
464
447
  body = result.bodyUsed;
465
448
  usedFallback = result.fallbackUsed;
466
449
  if (usedFallback) {
467
- log(`↺ Fallback successful: using ${finalModel}`);
450
+ logger.info(`[franklin] Fallback successful: using ${finalModel}`);
468
451
  }
469
452
  }
470
453
  else {
@@ -516,7 +499,7 @@ export function createProxy(options) {
516
499
  }
517
500
  res.writeHead(response.status, { 'Content-Type': 'application/json' });
518
501
  res.end(errorBody);
519
- log(`⚠️ ${response.status} from backend for ${finalModel}`);
502
+ logger.warn(`[franklin] ⚠️ ${response.status} from backend for ${finalModel}`);
520
503
  return;
521
504
  }
522
505
  res.writeHead(response.status, responseHeaders);
@@ -531,7 +514,7 @@ export function createProxy(options) {
531
514
  const pump = async () => {
532
515
  while (true) {
533
516
  if (Date.now() > streamDeadline) {
534
- log('⚠️ Stream timeout after 5 minutes');
517
+ logger.warn('[franklin] ⚠️ Stream timeout after 5 minutes');
535
518
  try {
536
519
  reader.cancel();
537
520
  }
@@ -576,7 +559,8 @@ export function createProxy(options) {
576
559
  fallback: usedFallback,
577
560
  source: 'proxy',
578
561
  });
579
- debug(options, `recorded: model=${finalModel} in=${inputTokens} out=${outputTokens} cost=$${cost.toFixed(4)} fallback=${usedFallback}`);
562
+ if (options.debug)
563
+ logger.debug(`[franklin] recorded: model=${finalModel} in=${inputTokens} out=${outputTokens} cost=$${cost.toFixed(4)} fallback=${usedFallback}`);
580
564
  }
581
565
  }
582
566
  res.end();
@@ -590,7 +574,7 @@ export function createProxy(options) {
590
574
  }
591
575
  };
592
576
  pump().catch((err) => {
593
- log(`❌ Stream error: ${err instanceof Error ? err.message : String(err)}`);
577
+ logger.error(`[franklin] Stream error: ${err instanceof Error ? err.message : String(err)}`);
594
578
  res.end();
595
579
  });
596
580
  }
@@ -615,7 +599,8 @@ export function createProxy(options) {
615
599
  fallback: usedFallback,
616
600
  source: 'proxy',
617
601
  });
618
- debug(options, `recorded: model=${finalModel} in=${inputTokens} out=${outputTokens} cost=$${cost.toFixed(4)} fallback=${usedFallback}`);
602
+ if (options.debug)
603
+ logger.debug(`[franklin] recorded: model=${finalModel} in=${inputTokens} out=${outputTokens} cost=$${cost.toFixed(4)} fallback=${usedFallback}`);
619
604
  }
620
605
  }
621
606
  catch {
@@ -626,7 +611,7 @@ export function createProxy(options) {
626
611
  }
627
612
  catch (error) {
628
613
  const msg = error instanceof Error ? error.message : 'Proxy error';
629
- log(`❌ Error: ${msg}`);
614
+ logger.error(`[franklin] Error: ${msg}`);
630
615
  res.writeHead(502, { 'Content-Type': 'application/json' });
631
616
  res.end(JSON.stringify({
632
617
  type: 'error',
@@ -693,7 +678,7 @@ async function fetchWithPaymentFallback(url, init, originalBody, config, payment
693
678
  if (nextModel && onFallback) {
694
679
  onFallback(model, 0, nextModel);
695
680
  }
696
- log(`[fallback] ${model} request error: ${err instanceof Error ? err.message : String(err)}`);
681
+ logger.warn(`[franklin] [fallback] ${model} request error: ${err instanceof Error ? err.message : String(err)}`);
697
682
  if (i < config.chain.length - 1) {
698
683
  await sleep(config.retryDelayMs);
699
684
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockrun/franklin",
3
- "version": "3.15.19",
3
+ "version": "3.15.21",
4
4
  "description": "Franklin — The AI agent with a wallet. Spends USDC autonomously to get real work done. Pay per action, no subscriptions.",
5
5
  "type": "module",
6
6
  "exports": {