(exploreTexts, answerGlobs = [], indent = ' ')
| 447 | * intersection from the agent's own answer instead. |
| 448 | */ |
| 449 | export function formatEnvelope(exploreTexts, answerGlobs = [], indent = ' ') { |
| 450 | // `tools/cache/**` -> /^tools\/cache\/.*$/ . Same semantics as probe-allocation. |
| 451 | // The `**` sentinel is written as an escape, never a literal NUL byte — a raw |
| 452 | // one makes git treat this whole script as binary and costs every future diff. |
| 453 | const glob2re = (glob) => { |
| 454 | const S = '\\u0000'; |
| 455 | const body = glob.replace(/[.+^${}()|[\]\\]/g, '\\$&') |
| 456 | .replace(/\*\*/g, S).replace(/\*/g, '[^/]*').replaceAll(S, '.*'); |
| 457 | return new RegExp(`^${body}$`); |
| 458 | }; |
| 459 | const answerRes = answerGlobs.map(glob2re); |
| 460 | const isAnswer = (p) => answerRes.some((re) => re.test(p)); |
| 461 | |
| 462 | // Share is over the sum of the per-file sections, i.e. of the source envelope |
| 463 | // the allocator divides. |
| 464 | const pooled = new Map(); |
| 465 | let envelope = 0; |
| 466 | for (const text of exploreTexts) { |
| 467 | for (const f of parseExploreCall(text).files) { |
| 468 | pooled.set(f.path, (pooled.get(f.path) ?? 0) + f.chars); |
| 469 | envelope += f.chars; |
| 470 | } |
| 471 | } |
| 472 | const ranked = [...pooled.entries()] |
| 473 | .map(([path, chars]) => ({ path, chars, share: envelope ? chars / envelope : 0, answer: isAnswer(path) })) |
| 474 | .sort((a, b) => b.chars - a.chars); |
| 475 | const answerChars = ranked.filter((r) => r.answer).reduce((s, r) => s + r.chars, 0); |
| 476 | const pct = (f) => `${(f * 100).toFixed(1)}%`; |
| 477 | |
| 478 | const out = []; |
| 479 | out.push(`${indent}Explore envelope: ${envelope.toLocaleString('en-US')} chars over ${exploreTexts.length} response(s)`); |
| 480 | if (answerGlobs.length) { |
| 481 | out.push(`${indent} answer-set share: ${pct(envelope ? answerChars / envelope : 0)} | top file answers: ${ranked[0]?.answer ?? false}`); |
| 482 | } |
| 483 | for (const f of ranked.slice(0, 12)) { |
| 484 | out.push(`${indent} ${f.answer ? '*' : ' '} ${pct(f.share).padStart(6)} ${String(f.chars).padStart(6)} ${f.path}`); |
| 485 | } |
| 486 | if (ranked.length > 12) out.push(`${indent} … ${ranked.length - 12} more files`); |
| 487 | return out.join('\n'); |
| 488 | } |
| 489 | |
| 490 | // --------------------------------------------------------------------------- |
| 491 | // Explore allocation efficiency (CG-9) |
no test coverage detected