thincoder 0.12.59 → 0.12.60

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.
Files changed (127) hide show
  1. package/CHANGELOG.md +38 -3
  2. package/README.md +2 -2
  3. package/bin/thincoder.mjs +80 -19
  4. package/package.json +4 -3
  5. package/src/acp/bridge.mjs +7 -4
  6. package/src/advisor/messages.mjs +24 -4
  7. package/src/advisor/run.mjs +35 -33
  8. package/src/advisor.mjs +25 -6
  9. package/src/agent/completion.mjs +17 -11
  10. package/src/agent/dispatch.mjs +102 -19
  11. package/src/agent/helpers.mjs +36 -0
  12. package/src/agent/record-results.mjs +46 -10
  13. package/src/agent/run-stages.mjs +227 -0
  14. package/src/agent/setup-reminders.mjs +62 -0
  15. package/src/agent/setup.mjs +18 -2
  16. package/src/agent/spawn-child.mjs +29 -4
  17. package/src/agent-tools/advisor-async.mjs +456 -0
  18. package/src/agent-tools/advisor.mjs +110 -108
  19. package/src/agent-tools/async-settle.mjs +191 -0
  20. package/src/agent-tools/consult.mjs +121 -102
  21. package/src/agent-tools/design-token.mjs +104 -0
  22. package/src/agent-tools/eng.mjs +24 -29
  23. package/src/agent-tools/escalate-async.mjs +286 -0
  24. package/src/agent-tools/read-history.mjs +155 -31
  25. package/src/agent-tools/recent-changes.mjs +2 -1
  26. package/src/agent-tools/settings.mjs +7 -17
  27. package/src/agent-tools/subagent-actions.mjs +168 -130
  28. package/src/agent-tools/subagent-async.mjs +129 -174
  29. package/src/agent-tools/subagent-panel.mjs +153 -0
  30. package/src/agent-tools/subagent-run.mjs +202 -0
  31. package/src/agent-tools/subagent-scheduler.mjs +45 -21
  32. package/src/agent-tools/subagent-spawn.mjs +406 -0
  33. package/src/agent-tools/subagent.mjs +107 -555
  34. package/src/agent-tools/verify.mjs +118 -270
  35. package/src/agent.mjs +57 -190
  36. package/src/cli/distill-command.mjs +10 -4
  37. package/src/cli/make-agent.mjs +3 -1
  38. package/src/cli/memory-command.mjs +2 -1
  39. package/src/cli/permission.mjs +2 -2
  40. package/src/cli/setup-wizard.mjs +17 -12
  41. package/src/config.mjs +56 -8
  42. package/src/context.mjs +5 -147
  43. package/src/crash-reports.mjs +123 -0
  44. package/src/distill.mjs +11 -11
  45. package/src/explore-distill.mjs +155 -0
  46. package/src/memory/code-sync.mjs +2 -1
  47. package/src/memory/core.mjs +6 -193
  48. package/src/memory/delete.mjs +234 -0
  49. package/src/memory/docs.mjs +58 -48
  50. package/src/memory.mjs +3 -1
  51. package/src/peer-domains.mjs +265 -0
  52. package/src/peer-instances.mjs +231 -0
  53. package/src/prompt-overlays.mjs +25 -0
  54. package/src/prompts/advisor-design.md +9 -76
  55. package/src/prompts/advisor-round1.md +9 -68
  56. package/src/prompts/advisor-round2.md +7 -54
  57. package/src/prompts/advisor-round3.md +7 -54
  58. package/src/prompts/coder.md +7 -50
  59. package/src/prompts/consult-base.md +4 -24
  60. package/src/prompts/discipline.md +26 -44
  61. package/src/prompts/eng-coder.md +7 -32
  62. package/src/prompts/engineering-sub.md +3 -23
  63. package/src/prompts/engineering.md +53 -306
  64. package/src/prompts/explore.md +3 -12
  65. package/src/prompts/main.md +10 -32
  66. package/src/prompts/methodology-template.md +28 -48
  67. package/src/prompts/plan.md +2 -9
  68. package/src/prompts/system.md +16 -35
  69. package/src/provider/core.mjs +6 -67
  70. package/src/provider/errors.mjs +76 -0
  71. package/src/provider/retry.mjs +8 -45
  72. package/src/session-gc.mjs +214 -0
  73. package/src/session-guard.mjs +47 -0
  74. package/src/session-rename.mjs +38 -0
  75. package/src/session-slots.mjs +181 -58
  76. package/src/session.mjs +48 -89
  77. package/src/token-ttl.mjs +273 -0
  78. package/src/tools/checklist-sync.mjs +181 -0
  79. package/src/tools/checklist.mjs +52 -39
  80. package/src/tools/edit-batch.mjs +109 -10
  81. package/src/tools/edit-diff.mjs +110 -27
  82. package/src/tools/edit.md +17 -12
  83. package/src/tools/execute.mjs +31 -4
  84. package/src/tools/file.mjs +11 -6
  85. package/src/tools/git.mjs +14 -6
  86. package/src/tools/glob-dialect.mjs +130 -0
  87. package/src/tools/glob.md +3 -3
  88. package/src/tools/grep.md +1 -1
  89. package/src/tools/index.mjs +5 -6
  90. package/src/tools/ops.mjs +175 -3
  91. package/src/tools/patch.mjs +3 -3
  92. package/src/tools/question.md +3 -0
  93. package/src/tools/read.md +0 -1
  94. package/src/tools/shared.mjs +14 -13
  95. package/src/tools/system.mjs +44 -9
  96. package/src/tools/wait_for.md +22 -0
  97. package/src/tui/agent-turn.mjs +17 -228
  98. package/src/tui/cmd-config.mjs +48 -7
  99. package/src/tui/cmd-eng.mjs +20 -16
  100. package/src/tui/cmd-mcp.mjs +8 -2
  101. package/src/tui/cmd-new.mjs +3 -2
  102. package/src/tui/cmd-session.mjs +19 -4
  103. package/src/tui/cmd-think.mjs +10 -10
  104. package/src/tui/cmd-upgrade.mjs +19 -4
  105. package/src/tui/config-helpers.mjs +28 -16
  106. package/src/tui/distill-cmd.mjs +1 -1
  107. package/src/tui/index.mjs +3 -2
  108. package/src/tui/interaction.mjs +3 -3
  109. package/src/tui/mouse.mjs +7 -1
  110. package/src/tui/pickers.mjs +40 -22
  111. package/src/tui/render-segments.mjs +27 -10
  112. package/src/tui/startup.mjs +4 -0
  113. package/src/tui/subagent-blocks.mjs +95 -263
  114. package/src/tui/subagent-children.mjs +176 -0
  115. package/src/tui/subagent-freeze.mjs +172 -0
  116. package/src/tui/subagent-panel.mjs +61 -23
  117. package/src/tui/suspension-drive.mjs +351 -0
  118. package/src/tui/tool-args.mjs +3 -3
  119. package/src/tui/tool-display.mjs +142 -0
  120. package/src/tui/tool-events.mjs +37 -173
  121. package/src/tui/tui-lifecycle.mjs +29 -0
  122. package/src/tui/update-notice.mjs +4 -0
  123. package/src/tui/wizard.mjs +12 -6
  124. package/src/tools/pdf-parse-text.mjs +0 -497
  125. package/src/tools/pdf-parse-xref.mjs +0 -499
  126. package/src/tools/pdf.mjs +0 -155
  127. package/src/tools/read_pdf.md +0 -21
package/src/context.mjs CHANGED
@@ -287,7 +287,7 @@ export async function compressIfNeeded(agent, threshold, callbacks, extras = {},
287
287
  // Silent by design (D11): no onToken/onReasoning — the compaction process must not stream to the frontend.
288
288
  // signal propagates user cancellation (Ctrl+C) to the in-flight summary call.
289
289
  // Compression visibility (CONTEXT-COMPACTION.md §7 D-C1/D-C2): the frontend learns the compression
290
- // STARTED right before the LLM call ("Compressing context… / summarizing N messages" panel) — only
290
+ // STARTED right before the summary LLM call ("Compressing context… / summarizing N messages" panel) — only
291
291
  // the lifecycle is surfaced, never the summary body. N = the number of history messages being summarized.
292
292
  callbacks?.onCompressStart?.({ messages: middle.length })
293
293
  const startedAt = performance.now()
@@ -374,150 +374,8 @@ function shrinkOversized(agent, limit = OVERSIZE_CONTENT_LIMIT) {
374
374
  return shrunk
375
375
  }
376
376
 
377
- // ─── End-of-run exploration distillation (AGENT-LOOP §13 + CONTEXT-COMPACTION §5, 2026-08-23) ───
378
- // The main agent's machine line is flooded by inline step-by-step exploration (read/grep/...).
379
- // At run end we distill THIS run's exploration tool-results into one semantic summary note that
380
- // replaces them in the machine line, while agent._fullHistory (the human line) stays untouched.
377
+ // ─── End-of-run exploration distillation(2026-09-05 module-split:524 > 500 硬限——verbatim
378
+ // 迁至 explore-distill.mjs,语义零变——VS Code compact.mjs 同款联动;cross-repo parity 锚改指
379
+ // explore-distill.mjs——消费方 import 面不变(re-export))───────────────────────
381
380
 
382
- /** Read-only knowledge tools counted as "exploration" (execute writes files → never exploration). */
383
- export const EXPLORE_TOOLS = new Set([
384
- "read", "grep", "glob", "ls", "code_search", "doc_search", "repo_outline",
385
- ])
386
-
387
- /** Summary prompt for turning a burst of exploration results into a semantic summary. */
388
- export const EXPLORE_SUMMARY_PROMPT = `You are distilling exploration tool results. Summarize the following read-only codebase exploration into a compact semantic summary for the main agent's own context.
389
-
390
- Requirements:
391
- - Capture WHAT was discovered, WHERE (which files / directories / symbols), and the KEY CONCLUSIONS — do not list tool calls mechanically
392
- - Keep actionable facts the main agent needs to continue: code locations, function names, file paths, structure, and open questions the exploration raised
393
- - Drop raw tool-output noise, repeated lines, and verbatim file dumps — keep only what must be remembered
394
- - Be honest: mark anything not actually verified as "unverified"; do not present guesses as facts
395
- - Use bullet points; aim for information completeness, not a hard word limit
396
-
397
- Exploration log:
398
- `
399
-
400
- /** tool_calls name across both stored shapes ({function:{name}} and flat {name}). */
401
- function toolCallName(tc) {
402
- return tc?.function?.name ?? tc?.name ?? ""
403
- }
404
-
405
- /** Tool that produced a tool-result message (falls back to its owner assistant's tool_call). */
406
- function toolResultName(msg, ownerToolCalls) {
407
- if (typeof msg?.name === "string" && msg.name) return msg.name
408
- const owner = (ownerToolCalls ?? []).find((tc) => tc.id === msg?.tool_call_id)
409
- return owner ? toolCallName(owner) : ""
410
- }
411
-
412
- /**
413
- * Find the pure-exploration "assistant(tool_calls)→tool…" pair blocks added since `start`.
414
- * A block is explorable only when EVERY tool call AND every tool result in it is an exploration
415
- * tool — mixed blocks (read + edit in one turn) stay untouched, or we'd orphan the edit pairing.
416
- */
417
- function findExplorationBlocks(history, start) {
418
- const blocks = []
419
- let i = start
420
- while (i < history.length) {
421
- const m = history[i]
422
- if (m?.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
423
- let j = i + 1
424
- while (j < history.length && history[j]?.role === "tool") j++
425
- const toolMsgs = history.slice(i + 1, j)
426
- const allCallsExplore = m.tool_calls.every((tc) => EXPLORE_TOOLS.has(toolCallName(tc)))
427
- const allResultsExplore = toolMsgs.length > 0 && toolMsgs.every((t) => EXPLORE_TOOLS.has(toolResultName(t, m.tool_calls)))
428
- if (allCallsExplore && allResultsExplore) {
429
- blocks.push({ start: i, end: j, messages: history.slice(i, j), toolCount: toolMsgs.length })
430
- }
431
- i = j
432
- } else {
433
- i++
434
- }
435
- }
436
- return blocks
437
- }
438
-
439
- /** Serialize a batch of exploration messages for the summary LLM (same shape as compaction serialization). */
440
- function serializeExplorationMessages(messages) {
441
- const cap = 8000 // exploration results ARE the signal to distill — generous cap (quality-first, N1)
442
- return messages
443
- .map((m) => {
444
- const toolNote = m.tool_calls ? ` [called tools: ${m.tool_calls.map(toolCallName).join(", ")}]` : ""
445
- let text = ""
446
- if (typeof m.content === "string") text = m.content
447
- else if (Array.isArray(m.content)) text = m.content.filter((p) => p?.type === "text").map((p) => p.text ?? "").join(" ")
448
- return `[${m.role}]${toolNote} ${text.slice(0, cap)}`
449
- })
450
- .join("\n")
451
- }
452
-
453
- /**
454
- * Core (shared) distillation: replace this run's pure-exploration pair blocks with a single
455
- * "[Exploration summary]" note placed where the first block was. Returns a NEW history array,
456
- * or null when there is nothing to shrink (<3 exploration results / LLM failure). Pairing-safe:
457
- * whole assistant→tool blocks are removed, so no orphan tool_calls/tool can survive.
458
- */
459
- async function distillExplorations(history, start, provider, signal, agent, depth) {
460
- if (!Array.isArray(history) || history.length - start < 2) return null
461
- const blocks = findExplorationBlocks(history, start)
462
- const resultCount = blocks.reduce((n, b) => n + b.toolCount, 0)
463
- if (resultCount < 3) return null
464
-
465
- const serialized = blocks.map((b) => serializeExplorationMessages(b.messages)).join("\n")
466
-
467
- let summary
468
- try {
469
- // Silent by design (D11): thinking:null and no onToken/onReasoning — this internal
470
- // distillation must not stream to the frontend. signal propagates user cancellation.
471
- const resp = await chat({ ...provider, thinking: null, reasoningEffort: null }, {
472
- messages: [{ role: "user", content: EXPLORE_SUMMARY_PROMPT + serialized }],
473
- signal,
474
- // §18.6 D-TR4:轨迹元数据增补——kind=distill(探索蒸馏面——agent 元数据透出;
475
- // depth 经 summarizeRunExplorations 参数透传——agent.mjs 主作用域传入)
476
- logCtx: {
477
- stage: "distill", child: agent?._logId ?? null, kind: "distill",
478
- role: agent?._role ?? null, depth: depth ?? null,
479
- session: agent?._sessionStart ?? null, cwd: agent?.cwd ?? process.cwd(),
480
- traces: agent?.config?.traces?.enabled !== false,
481
- },
482
- })
483
- summary = resp?.content
484
- } catch {
485
- return null // N3: never block the run's return or lose history — original results stay
486
- }
487
- if (!summary) return null
488
-
489
- const drop = new Set()
490
- for (const b of blocks) for (let k = b.start; k < b.end; k++) drop.add(k)
491
- const note = { role: "user", content: "[Exploration summary]\n" + summary }
492
- const next = []
493
- let inserted = false
494
- for (let k = 0; k < history.length; k++) {
495
- if (drop.has(k)) {
496
- if (!inserted) { next.push(note); inserted = true }
497
- continue
498
- }
499
- next.push(history[k])
500
- }
501
- return next
502
- }
503
-
504
- /**
505
- * End-of-run exploration distillation (runAgent's final return). Shrinks the MACHINE line
506
- * (agent.history) only; agent._fullHistory is never touched. Triggers when this run added ≥3
507
- * exploration tool results; on LLM failure it silently keeps the original history (N3).
508
- * The distillation itself is silent and never streams (D11); `callbacks.onDistilled` fires
509
- * ONLY after the replacement actually lands (never on no-op/failure) — callers persist the
510
- * compressed session (SEND-STALL-DISTILL §2.3).
511
- */
512
- export async function summarizeRunExplorations(agent, callbacks, signal, depth = 0) {
513
- const next = await distillExplorations(agent.history, agent._runStartHistoryLen ?? 0, agent.provider, signal, agent, depth)
514
- if (!next) return
515
- agent.history = next
516
- // The machine line changed shape — the measured token baseline was for the pre-shrink context.
517
- // Invalidate so the next compaction check re-estimates instead of over-counting stale history.
518
- agent._lastPromptTokens = null
519
- agent._usageAtLen = null
520
- // The compressed machine line must reach the disk: the run's own save already happened,
521
- // so without this hook the async distill would leave the session un-compressed on exit.
522
- callbacks.onDistilled?.()
523
- }
381
+ export { summarizeRunExplorations, EXPLORE_TOOLS, EXPLORE_SUMMARY_PROMPT } from "./explore-distill.mjs"
@@ -0,0 +1,123 @@
1
+ /**
2
+ * crash-reports.mjs — R25(docs/design/ARCHITECTURE.md §R25——F-R25a/b/c)CLI 异常终止
3
+ * 捕获与留痕机制。
4
+ *
5
+ * 三类能力(与设计逐项对应):
6
+ * - prepareCrashReporting()(F-R25b):入口最前调用——mkdir 预建 ~/.thincoder/crash-reports/
7
+ * + process.report 代码内启用(reportOnFatalError + directory)——V8 OOM/原生 fatal
8
+ * 自动写 report.*.json。实现批实测(2026-09-07):目录缺失时 Node 对 fatal 静默不写
9
+ * 报告——预建是必要动作而非"零成本保险"。
10
+ * - writeCrashRecord()(F-R25a):JS 异常钩子同步落盘 crash-{ts}-{pid}.json(ts = epoch ms
11
+ * UTC + pid——跨进程同 ms 防覆盖——复审 #3)——权限 0600(config.json 先例)。
12
+ * - recentCrashHint()(F-R25c):启动扫描 24h 窗内记录(两类文件模式定死——评审 #8:
13
+ * crash-*.json 自写 + report.*.json Node fatal——mtime 判定)→ 返回提示文本或 null
14
+ * (无匹配不提示——负例)。
15
+ *
16
+ * 保留策略(评审 #6 + 复审 #4):>30 天淘汰——写时自清理(tool-results 先例『落盘目录
17
+ * 写时自清理』段同动机——Windows 磁盘清理不覆盖 ~/.thincoder)——清理搭车点 = F-R25a 写 +
18
+ * F-R25b 入口 mkdir + F-R25c 启动扫描(纯 fatal 序列 JS 不运行——下个进程入口 mkdir 时
19
+ * 清理)。
20
+ *
21
+ * 纪律:全部同步 API(崩溃路径无异步);调用方按执行序列各步独立 try/catch(复审 #2——
22
+ * 写失败/恢复失败不阻断后续步——exit 非 0 恒达)。
23
+ */
24
+ import { chmodSync, existsSync, mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs"
25
+ import { join } from "node:path"
26
+ import { configDir } from "./config.mjs"
27
+
28
+ /** crash-reports 运行时目录(~/.thincoder/crash-reports——非仓内——写时自清理)。 */
29
+ export function crashReportsDir() {
30
+ return join(configDir, "crash-reports")
31
+ }
32
+
33
+ /** 记录文件模式(评审 #8 定死):crash-*.json = 自写(F-R25a);report.*.json = Node
34
+ * fatal(默认命名 report.YYYYMMDD.HHMMSS.<pid>.<seq>.json——2026-09-07 实现批实测)。 */
35
+ function isCrashRecordName(name) {
36
+ return /^crash-.+\.json$/.test(name) || /^report\..+\.json$/.test(name)
37
+ }
38
+
39
+ /** >30 天淘汰(评审 #6)——写时自清理;搭车点 = 写 / 入口 mkdir / 启动扫描(复审 #4)。 */
40
+ function purgeOldCrashReports(dir) {
41
+ const cutoff = Date.now() - 30 * 24 * 3_600_000
42
+ let names
43
+ try { names = readdirSync(dir) } catch { return } // 目录不存在/不可读 → 无事可做
44
+ for (const name of names) {
45
+ if (!isCrashRecordName(name)) continue
46
+ try {
47
+ if (statSync(join(dir, name)).mtimeMs < cutoff) unlinkSync(join(dir, name))
48
+ } catch { /* 单个文件失败不影响其余 */ }
49
+ }
50
+ }
51
+
52
+ /**
53
+ * F-R25b:入口最前调用(一切重活前——缩编程期窗口)——预建目录 + process.report 启用。
54
+ * 任何失败不阻断启动(尽力面——record 路径自带降级)。返回目录路径。
55
+ */
56
+ export function prepareCrashReporting() {
57
+ const dir = crashReportsDir()
58
+ try {
59
+ mkdirSync(dir, { recursive: true })
60
+ // 代码内启用:shebang 入口无法携带启动参数(env 单参数限制 + execArgv 仅子进程——评审 #1)
61
+ process.report.directory = dir
62
+ process.report.reportOnFatalError = true
63
+ } catch { /* mkdir/启用失败不阻断启动——尽力面 */ }
64
+ purgeOldCrashReports(dir) // F-R25b 入口 mkdir 搭车清理(复审 #4——纯 fatal 序列也触发)
65
+ return dir
66
+ }
67
+
68
+ /**
69
+ * F-R25a:JS 异常钩子同步写诊断记录(内容:时间/类型/错误消息+堆栈/uptime/argv/cwd/
70
+ * 内存/node+版本——设计字段集)。
71
+ * @param {string} type uncaughtException | unhandledRejection
72
+ * @param {*} error 原始 error(unhandledRejection 可 reject 任意值——非 Error 兜底 String)
73
+ * @returns {string|null} 记录完整路径;写失败 → null(不抛——调用方步隔离——复审 #2)
74
+ */
75
+ export function writeCrashRecord({ type, error }) {
76
+ const dir = crashReportsDir()
77
+ try {
78
+ const record = {
79
+ time: new Date().toISOString(),
80
+ type,
81
+ message: error?.message ?? String(error),
82
+ stack: typeof error?.stack === "string" ? error.stack : null,
83
+ uptime: process.uptime(),
84
+ argv: process.argv,
85
+ cwd: process.cwd(),
86
+ memoryUsage: process.memoryUsage(),
87
+ node: process.version,
88
+ }
89
+ // ts = epoch ms UTC + pid——跨进程同 ms 防覆盖(复审 #3)
90
+ const file = join(dir, `crash-${Date.now()}-${process.pid}.json`)
91
+ writeFileSync(file, JSON.stringify(record, null, 2) + "\n", { encoding: "utf8", mode: 0o600 })
92
+ try { chmodSync(file, 0o600) } catch { /* Windows chmod 尽力而为——config.json 先例 */ }
93
+ purgeOldCrashReports(dir) // F-R25a 写时搭车清理(评审 #6)
94
+ return file
95
+ } catch {
96
+ return null // 写失败不阻断后续步
97
+ }
98
+ }
99
+
100
+ /**
101
+ * F-R25c:启动扫描——crash-reports 24h 窗内是否有记录(两类模式——mtime 判定)。
102
+ * @param {object} [opts] 可选 { dir }——目录注入(测试用);默认 crashReportsDir()
103
+ * @returns {string|null} 提示文本(含最新记录完整路径);无匹配 → null(负例——不提示)
104
+ */
105
+ export function recentCrashHint({ dir = crashReportsDir() } = {}) {
106
+ purgeOldCrashReports(dir) // F-R25c 启动扫描搭车清理(复审 #4)
107
+ if (!existsSync(dir)) return null
108
+ const cutoff = Date.now() - 24 * 3_600_000
109
+ let best = null // 窗内 mtime 最新记录
110
+ let names
111
+ try { names = readdirSync(dir) } catch { return null } // 目录不可读 → 静默无提示
112
+ for (const name of names) {
113
+ if (!isCrashRecordName(name)) continue
114
+ try {
115
+ const st = statSync(join(dir, name))
116
+ if (st.mtimeMs >= cutoff && (!best || st.mtimeMs > best.mtimeMs)) {
117
+ best = { path: join(dir, name), mtimeMs: st.mtimeMs }
118
+ }
119
+ } catch { /* 单文件 stat 失败跳过 */ }
120
+ }
121
+ if (!best) return null
122
+ return `上次运行异常终止(记录:${best.path})`
123
+ }
package/src/distill.mjs CHANGED
@@ -18,7 +18,7 @@ Output a JSON array (nothing else):
18
18
  "title": "Short title",
19
19
  "content": "Full content, self-contained — understandable without session context",
20
20
  "tags": ["tag1", "tag2"],
21
- "scope": "personal | project"
21
+ "layer": "personal | project"
22
22
  }
23
23
  ]
24
24
 
@@ -27,7 +27,7 @@ Extraction criteria:
27
27
  - decision: technical decisions made in the session and their rationale
28
28
  - pattern: debugging experiences, solutions, reusable workflows
29
29
  - rule: coding standards (caution! rules are usually best written manually; only extract rules explicitly established in the session)
30
- - scope: use "project" for project-specific knowledge; use "personal" for general or personal preferences
30
+ - layer: use "project" for project-specific knowledge; use "personal" for general or personal preferences
31
31
 
32
32
  Do NOT extract:
33
33
  - one-off task details ("changed line X in file Y today")
@@ -53,7 +53,7 @@ function tracesEnabledFromConfig() {
53
53
 
54
54
  /**
55
55
  * Extract candidates from a session transcript. transcript: plain-text session record.
56
- * Returns [{ type, title, content, tags, scope }], or [] on parse failure.
56
+ * Returns [{ type, title, content, tags, layer }], or [] on parse failure.
57
57
  * opts.traces(可选):§18.6 D-TR6 开关显式透传(测试隔离/未来调用方)——缺省回退
58
58
  * 磁盘配置(tracesEnabledFromConfig)——关 = chat() 出口不落盘。
59
59
  */
@@ -117,23 +117,23 @@ export function historyToTranscript(history, { maxChars = 30_000 } = {}) {
117
117
  /**
118
118
  * Write confirmed candidates to the specified layer.
119
119
  * opts: { projectDir, team: { dir } | null, author }
120
- * scope=team requires opts.team; project requires opts.projectDir.
120
+ * layer=team requires opts.team; project requires opts.projectDir.
121
121
  * Returns write result description.
122
122
  */
123
123
  export async function saveCandidate(memory, candidate, opts = {}) {
124
- const scope = candidate.scope ?? "personal"
124
+ const layer = candidate.layer ?? candidate.scope ?? "personal"
125
125
  // tags come from LLM output (untrusted): if not an array, stringify then split by comma/whitespace —
126
126
  // calling .split on a non-string would crash, and models often produce "a, b" comma strings
127
127
  const tags = Array.isArray(candidate.tags)
128
128
  ? candidate.tags.map((t) => String(t)).filter(Boolean)
129
129
  : String(candidate.tags ?? "").split(/[\s,]+/).filter(Boolean)
130
130
 
131
- if (scope === "personal") {
131
+ if (layer === "personal") {
132
132
  const id = await put(memory, { type: candidate.type, title: candidate.title, content: candidate.content, tags: tags.join(" ") })
133
133
  return `personal#${id}`
134
134
  }
135
- if (scope === "project") {
136
- if (!opts.projectDir) throw new Error("project scope unavailable — no project directory configured (set memory.projectDir in ~/.thincoder/config.json)")
135
+ if (layer === "project") {
136
+ if (!opts.projectDir) throw new Error("project layer unavailable — no project directory configured (set memory.projectDir in ~/.thincoder/config.json)")
137
137
  const filename = await putMarkdown(memory, {
138
138
  layer: "project", dir: opts.projectDir,
139
139
  type: candidate.type, title: candidate.title, content: candidate.content,
@@ -141,8 +141,8 @@ export async function saveCandidate(memory, candidate, opts = {}) {
141
141
  })
142
142
  return `project:${filename}`
143
143
  }
144
- if (scope === "team") {
145
- if (!opts.team?.dir) throw new Error("team scope not configured — configure memory.team in ~/.thincoder/config.json")
144
+ if (layer === "team") {
145
+ if (!opts.team?.dir) throw new Error("team layer not configured — configure memory.team in ~/.thincoder/config.json")
146
146
  const filename = await putMarkdown(memory, {
147
147
  layer: "team", dir: opts.team.dir,
148
148
  type: candidate.type, title: candidate.title, content: candidate.content,
@@ -151,5 +151,5 @@ export async function saveCandidate(memory, candidate, opts = {}) {
151
151
  await commitAndPush(opts.team.dir, filename, `memory: [${candidate.type}] ${candidate.title} (distilled)`)
152
152
  return `team:${filename}`
153
153
  }
154
- throw new Error(`unknown scope: ${scope}`)
154
+ throw new Error(`unknown layer: ${layer}`)
155
155
  }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * explore-distill.mjs — End-of-run exploration distillation (AGENT-LOOP §13 +
3
+ * CONTEXT-COMPACTION §5, 2026-08-23). 2026-09-05 module-split: moved verbatim out of
4
+ * context.mjs (524 > 500 hard limit). The main agent's machine line is flooded by inline
5
+ * step-by-step exploration (read/grep/...). At run end we distill THIS run's exploration
6
+ * tool-results into one semantic summary note that replaces them in the machine line,
7
+ * while agent._fullHistory (the human line) stays untouched. VS Code compact.mjs
8
+ * distillation mirrors this module (same-name file in thincoder-vscode/src, cross-repo
9
+ * parity anchors point here).
10
+ */
11
+
12
+ import { chat } from "./provider/index.mjs"
13
+
14
+ /** Read-only knowledge tools counted as "exploration" (execute writes files → never exploration). */
15
+ export const EXPLORE_TOOLS = new Set([
16
+ "read", "grep", "glob", "ls", "code_search", "doc_search", "repo_outline",
17
+ ])
18
+
19
+ /** Summary prompt for turning a burst of exploration results into a semantic summary. */
20
+ export const EXPLORE_SUMMARY_PROMPT = `You are distilling exploration tool results. Summarize the following read-only codebase exploration into a compact semantic summary for the main agent's own context.
21
+
22
+ Requirements:
23
+ - Capture WHAT was discovered, WHERE (which files / directories / symbols), and the KEY CONCLUSIONS — do not list tool calls mechanically
24
+ - Keep actionable facts the main agent needs to continue: code locations, function names, file paths, structure, and open questions the exploration raised
25
+ - Drop raw tool-output noise, repeated lines, and verbatim file dumps — keep only what must be remembered
26
+ - Be honest: mark anything not actually verified as "unverified"; do not present guesses as facts
27
+ - Use bullet points; aim for information completeness, not a hard word limit
28
+
29
+ Exploration log:
30
+ `
31
+
32
+ /** tool_calls name across both stored shapes ({function:{name}} and flat {name}). */
33
+ function toolCallName(tc) {
34
+ return tc?.function?.name ?? tc?.name ?? ""
35
+ }
36
+
37
+ /** Tool that produced a tool-result message (falls back to its owner assistant's tool_call). */
38
+ function toolResultName(msg, ownerToolCalls) {
39
+ if (typeof msg?.name === "string" && msg.name) return msg.name
40
+ const owner = (ownerToolCalls ?? []).find((tc) => tc.id === msg?.tool_call_id)
41
+ return owner ? toolCallName(owner) : ""
42
+ }
43
+
44
+ /**
45
+ * Find the pure-exploration "assistant(tool_calls)→tool…" pair blocks added since `start`.
46
+ * A block is explorable only when EVERY tool call AND every tool result in it is an exploration
47
+ * tool — mixed blocks (read + edit in one turn) stay untouched, or we'd orphan the edit pairing.
48
+ */
49
+ function findExplorationBlocks(history, start) {
50
+ const blocks = []
51
+ let i = start
52
+ while (i < history.length) {
53
+ const m = history[i]
54
+ if (m?.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
55
+ let j = i + 1
56
+ while (j < history.length && history[j]?.role === "tool") j++
57
+ const toolMsgs = history.slice(i + 1, j)
58
+ const allCallsExplore = m.tool_calls.every((tc) => EXPLORE_TOOLS.has(toolCallName(tc)))
59
+ const allResultsExplore = toolMsgs.length > 0 && toolMsgs.every((t) => EXPLORE_TOOLS.has(toolResultName(t, m.tool_calls)))
60
+ if (allCallsExplore && allResultsExplore) {
61
+ blocks.push({ start: i, end: j, messages: history.slice(i, j), toolCount: toolMsgs.length })
62
+ }
63
+ i = j
64
+ } else {
65
+ i++
66
+ }
67
+ }
68
+ return blocks
69
+ }
70
+
71
+ /** Serialize a batch of exploration messages for the summary LLM (same shape as compaction serialization). */
72
+ function serializeExplorationMessages(messages) {
73
+ const cap = 8000 // exploration results ARE the signal to distill — generous cap (quality-first, N1)
74
+ return messages
75
+ .map((m) => {
76
+ const toolNote = m.tool_calls ? ` [called tools: ${m.tool_calls.map(toolCallName).join(", ")}]` : ""
77
+ let text = ""
78
+ if (typeof m.content === "string") text = m.content
79
+ else if (Array.isArray(m.content)) text = m.content.filter((p) => p?.type === "text").map((p) => p.text ?? "").join(" ")
80
+ return `[${m.role}]${toolNote} ${text.slice(0, cap)}`
81
+ })
82
+ .join("\n")
83
+ }
84
+
85
+ /**
86
+ * Core (shared) distillation: replace this run's pure-exploration pair blocks with a single
87
+ * "[Exploration summary]" note placed where the first block was. Returns a NEW history array,
88
+ * or null when there is nothing to shrink (<3 exploration results / LLM failure). Pairing-safe:
89
+ * whole assistant→tool blocks are removed, so no orphan tool_calls/tool can survive.
90
+ */
91
+ async function distillExplorations(history, start, provider, signal, agent, depth) {
92
+ if (!Array.isArray(history) || history.length - start < 2) return null
93
+ const blocks = findExplorationBlocks(history, start)
94
+ const resultCount = blocks.reduce((n, b) => n + b.toolCount, 0)
95
+ if (resultCount < 3) return null
96
+
97
+ const serialized = blocks.map((b) => serializeExplorationMessages(b.messages)).join("\n")
98
+
99
+ let summary
100
+ try {
101
+ // Silent by design (D11): thinking:null and no onToken/onReasoning — this internal
102
+ // distillation must not stream to the frontend. signal propagates user cancellation.
103
+ const resp = await chat({ ...provider, thinking: null, reasoningEffort: null }, {
104
+ messages: [{ role: "user", content: EXPLORE_SUMMARY_PROMPT + serialized }],
105
+ signal,
106
+ // §18.6 D-TR4:轨迹元数据增补——kind=distill(探索蒸馏面——agent 元数据透出;
107
+ // depth 经 summarizeRunExplorations 参数透传——agent.mjs 主作用域传入)
108
+ logCtx: {
109
+ stage: "distill", child: agent?._logId ?? null, kind: "distill",
110
+ role: agent?._role ?? null, depth: depth ?? null,
111
+ session: agent?._sessionStart ?? null, cwd: agent?.cwd ?? process.cwd(),
112
+ traces: agent?.config?.traces?.enabled !== false,
113
+ },
114
+ })
115
+ summary = resp?.content
116
+ } catch {
117
+ return null // N3: never block the run's return or lose history — original results stay
118
+ }
119
+ if (!summary) return null
120
+
121
+ const drop = new Set()
122
+ for (const b of blocks) for (let k = b.start; k < b.end; k++) drop.add(k)
123
+ const note = { role: "user", content: "[Exploration summary]\n" + summary }
124
+ const next = []
125
+ let inserted = false
126
+ for (let k = 0; k < history.length; k++) {
127
+ if (drop.has(k)) {
128
+ if (!inserted) { next.push(note); inserted = true }
129
+ continue
130
+ }
131
+ next.push(history[k])
132
+ }
133
+ return next
134
+ }
135
+
136
+ /**
137
+ * End-of-run exploration distillation (runAgent's final return). Shrinks the MACHINE line
138
+ * (agent.history) only; agent._fullHistory is never touched. Triggers when this run added ≥3
139
+ * exploration tool results; on LLM failure it silently keeps the original history (N3).
140
+ * The distillation itself is silent and never streams (D11); `callbacks.onDistilled` fires
141
+ * ONLY after the replacement actually lands (never on no-op/failure) — callers persist the
142
+ * compressed session (SEND-STALL-DISTILL §2.3).
143
+ */
144
+ export async function summarizeRunExplorations(agent, callbacks, signal, depth = 0) {
145
+ const next = await distillExplorations(agent.history, agent._runStartHistoryLen ?? 0, agent.provider, signal, agent, depth)
146
+ if (!next) return
147
+ agent.history = next
148
+ // The machine line changed shape — the measured token baseline was for the pre-shrink context.
149
+ // Invalidate so the next compaction check re-estimates instead of over-counting stale history.
150
+ agent._lastPromptTokens = null
151
+ agent._usageAtLen = null
152
+ // The compressed machine line must reach the disk: the run's own save already happened,
153
+ // so without this hook the async distill would leave the session un-compressed on exit.
154
+ callbacks.onDistilled?.()
155
+ }
@@ -313,7 +313,8 @@ export function codeSearchTool(memory) {
313
313
  return {
314
314
  name: "code_search",
315
315
  description:
316
- "Search the project's source code for relevant code. Use this to find functions, classes, or code patterns across the codebase. Supports natural language queries and code snippets. Returns matching code chunks with file paths and line numbers. Prefer doc_search for the intended design (design docs, conventions); code_search for the implementation as written.",
316
+ "Search the project's source code for relevant code. Use this to find functions, classes, or code patterns across the codebase. Supports natural language queries and code snippets. Returns matching code chunks with file paths and line numbers. Prefer doc_search for the intended design (design docs, conventions); code_search for the implementation as written. " +
317
+ "For what was said in a session (conversation/chat history), use read_history.",
317
318
  parameters: {
318
319
  type: "object",
319
320
  properties: {