Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.8 KB
Raw Blame
#!/usr/bin/env node
/**
 * Render a scheduled task type's agent prompt end-to-end — shipped prompt
 * default → pre-step block substitution → `buildAgentPrompt` — and print the
 * byte size of every `## ` section, so a context audit starts from numbers
 * instead of guesses about which block is fat.
 *
 *   node scripts/measure-agent-prompt.js [taskType] [providerType] [--out <prefix>]
 *
 *   taskType      a DEFAULT_TASK_PROMPTS key (default: claim-issue)
 *   providerType  tui (light path, split system/user prompt), cli, or api
 *                 (full path: instruction files, memory, tools; default: tui)
 *   --out         also write <prefix>.user.md (and .system.md when split)
 *
 * Runs against the shipped defaults with placeholder app/reviewer values and
 * NO database (NODE_ENV=test + MEMORY_BACKEND=file are forced), so the numbers
 * are the prompt's own cost, not this install's. Set REPO=<path> to point the
 * api path's instruction-file walk at a real checkout.
 */
import { writeFileSync } from 'fs';

process.env.NODE_ENV = 'test';
process.env.MEMORY_BACKEND ||= 'file';

const args = process.argv.slice(2);
const outIndex = args.indexOf('--out');
const outPrefix = outIndex === -1 ? null : args[outIndex + 1];
// Splice rather than filter on index arithmetic: with no --out, outIndex + 1 is 0
// and an index filter would silently drop the first positional.
if (outIndex !== -1) args.splice(outIndex, 2);
const [taskType = 'claim-issue', providerType = 'tui'] = args;

const { getTaskPrompt } = await import('../server/services/taskPromptService.js');
const { buildImprovementTaskDescription } = await import('../server/services/cosTaskPreStepBlocks.js');
const { buildAgentPrompt } = await import('../server/services/agentPromptBuilder.js');
const { estimateTokens } = await import('../server/lib/contextBudget.js');

const app = { id: 'example-app', name: 'Example App', repoPath: process.env.REPO || '/repo/example' };
const template = await getTaskPrompt(taskType, { claimFlow: true });
const metadata = {
  app: app.id, appName: app.name, repoPath: app.repoPath, analysisType: taskType,
  autoGenerated: true, claimFlow: true, reviewers: ['antigravity', 'ollama'], useWorktree: false, openPR: false,
};
const description = await buildImprovementTaskDescription({
  promptTemplate: template, app, promptTaskType: taskType, metadata,
  // Every pre-step block renders empty, whatever placeholders the generator grows.
  blocks: new Proxy({}, { get: () => '' }),
});
const task = { id: 'task-measure', description, metadata, taskType: 'internal' };
const isApi = providerType === 'api';
const result = await buildAgentPrompt(task, {}, app.repoPath, null, undefined, {
  providerType,
  providerId: isApi ? 'example-api' : 'claude-code',
  providerCommand: isApi ? null : 'claude',
  providerModel: 'example-model',
  agentId: 'agent-measure',
  split: !isApi,
});
const { userPrompt, systemPrompt = '' } = typeof result === 'string' ? { userPrompt: result } : result;

const sections = (text) => text.split(/\n(?=## )/).map((s) => [s.split('\n')[0].slice(0, 72), s.length]);
const printSections = (label, text) => {
  console.log(`--- ${label} (${text.length}B, ~${estimateTokens(text)} tok)`);
  for (const [heading, size] of sections(text)) console.log(`${String(size).padStart(7)}  ${heading}`);
};

console.log(`${taskType} via ${providerType}: template=${template.length}B description=${description.length}B total≈${estimateTokens(userPrompt + systemPrompt)} tok`);
printSections('user prompt', userPrompt);
if (systemPrompt) printSections('system prompt', systemPrompt);
if (outPrefix) {
  writeFileSync(`${outPrefix}.user.md`, userPrompt);
  const written = [`${outPrefix}.user.md`];
  if (systemPrompt) {
    writeFileSync(`${outPrefix}.system.md`, systemPrompt);
    written.push(`${outPrefix}.system.md`);
  }
  console.log(`📝 Wrote ${written.join(' and ')}`);
}