Hmbown/CodeWhale · error · Error

Select an existing --trace ID when input contains multiple…

Error message

Select an existing --trace ID when input contains multiple traces.

What it means

After importing traces from the --input file, load() selects which trace to convert: if --trace=<id> was given it finds that ID; otherwise a single-trace file is auto-selected. If no trace is selected — the file contains multiple traces and no --trace was passed, or the given --trace ID doesn't exist — the script throws this error because conversion needs exactly one trace.

Solutions

  1. Pass --trace=<id> with an ID that actually exists in the input file (list IDs by importing the file and inspecting traces[].id).
  2. Copy-paste the trace ID exactly, including case, from the source trace metadata.
  3. Verify you're converting the intended input file — an ID from a different export won't match.
  4. Split the export so each file holds one trace, then convert without --trace.

Example fix

// before
node scripts/pet.mjs --input=multi-trace.jsonl --output=pet.jsonl
// after
node scripts/pet.mjs --input=multi-trace.jsonl --trace=tr_01J9XK... --output=pet.jsonl
Defensive patterns

Strategy: validation

Validate before calling

// confirm the ID exists before converting
const traces = importTrace(await readFile(inputFile, 'utf8'), inputFile, { privacy: 'metadata' });
if (traces.length > 1 && !traces.some(t => t.id === wantedTraceId)) {
  console.error('available IDs:', traces.map(t => t.id));
}

Try / catch

try {
  await runPetCli(['--input=' + f, '--trace=' + id, '--output=' + out]);
} catch (err) {
  if (err.message.startsWith('Select an existing --trace')) {
    console.error('list trace IDs in the file and pass --trace=<id> exactly');
  }
  throw err;
}

Prevention

When it happens

Trigger: --input file contains two or more traces and the command omits --trace; or --trace=<id> is passed but no imported trace has that exact ID (typo, stale ID, wrong file).

Common situations: Merged or concatenated trace exports containing several sessions; reusing a --trace ID from a previous recording against a new export; case-sensitive ID mismatch; assuming auto-selection works on multi-trace files.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/0a0623a680dd8d88. Report an issue: GitHub.

Appendix: source

Thrown at pet/scripts/pet.mjs:31

const option = name => args.find(a => a.startsWith(`--${name}=`))?.slice(name.length + 3);
if (args.includes('--help')) {
  console.log('node scripts/pet.mjs --input=trace.jsonl --output=pet.jsonl [--trace=ID] [--format=jsonl|tsv] [--watch]\nnode scripts/pet.mjs --runtime=http://127.0.0.1:7878 --thread=ID --output=pet.jsonl [--segment-buckets=216000] [--resume]\nUse --demo instead of --input for synthetic telemetry. Output must not already exist unless --resume is used for live recording. A resumed recorder preserves the previous segment and starts unknown at the same path.\nLive recording rotates at 216000 buckets or 64 MiB into OUTPUT.segment-NNNNNN.jsonl and continues at the same live path. All archives are retained.\nRuntime reads only the existing local event journal. Optional authentication comes from CODEWHALE_RUNTIME_TOKEN; never put a token in the URL. No agent or provider is started.');
  process.exit(0);
}
let output, recorder, monitor, timer, runtime;
try {
  for (const a of args) if (!['--demo', '--watch', '--resume'].includes(a) && !/^--(input|output|trace|format|runtime|thread|segment-buckets)=.+/.test(a)) throw new Error('Unknown or empty option. Use --help.');
  const input = option('input'), runtimeURL = option('runtime'), path = option('output'), format = option('format') ?? 'jsonl', live = args.includes('--watch') || !!runtimeURL;
  if (!path || [!!input, args.includes('--demo'), !!runtimeURL].filter(Boolean).length !== 1
    || !['jsonl', 'tsv'].includes(format) || live && format !== 'jsonl' || args.includes('--watch') && !input
    || !!runtimeURL !== !!option('thread') || option('trace') && !input || option('segment-buckets') && !live || args.includes('--resume') && !live)
    throw new Error('Choose one input source, an unused --output path (or --resume), and JSONL for live recording. Runtime requires --thread.');
  const load = async () => {
    if (!input) return { events: petDemoEvents(), duration: 80_000 };
    if ((await stat(input)).size > 64 * 1024 * 1024) throw new Error('Input exceeds 64 MiB.');
    const traces = importTrace(await readFile(input, 'utf8'), input, { privacy: 'metadata' });
    const trace = option('trace') ? traces.find(t => t.id === option('trace')) : traces.length === 1 ? traces[0] : undefined;
    if (!trace) throw new Error('Select an existing --trace ID when input contains multiple traces.');
    return trace;
  };
  let trace = runtimeURL ? undefined : await load(), buckets = compilePetTelemetry(trace?.events ?? [], trace?.duration ?? 0);
  if (live) recorder = await createPetRecorder(path, { resume: args.includes('--resume'), maxBuckets: option('segment-buckets') === undefined ? 216_000 : Number(option('segment-buckets')), report: text => console.error(text) });
  else output = await open(path, 'wx', 0o600);
  if (runtimeURL) runtime = await followRuntime({ baseUrl: runtimeURL, threadId: option('thread'),
    token: process.env.CODEWHALE_RUNTIME_TOKEN, report: text => console.error(text) });
  if (!live) {
    await output.writeFile(format === 'tsv' ? encodePetTSV(buckets) : encodePetJSONL(buckets));
    await output.close(); output = undefined;
    console.log(`Wrote ${buckets.length} pet buckets (${args.includes('--demo') ? 'demo' : 'trace replay'}).`);
  } else {
    // The driver owns wall time. The core only sees recorded relative timestamps.
    const started = performance.now(), startedWall = Date.now();
    const origin = trace && 'originTime' in trace && trace.originTime ? Date.parse(trace.originTime) : NaN;
    const offset = Number.isFinite(origin) ? Math.max(0, Date.now() - origin) : trace?.duration ?? 0;
    let dirty = false, running = false, sequence = 0, failed = false, stopping = false, lastBin = -1;
    const empty = compilePetTelemetry([])[0];

View on GitHub (pinned to 433685b202)