@thincoder/core 0.11.2 → 0.11.3
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.
- package/CHANGELOG.md +18 -0
- package/advisor/compaction.mjs +7 -5
- package/advisor/history.mjs +24 -11
- package/advisor/loop.mjs +13 -8
- package/advisor/messages.mjs +72 -8
- package/advisor/run.mjs +24 -13
- package/advisor.mjs +7 -3
- package/agent/chat-call.mjs +14 -9
- package/agent/completion.mjs +28 -40
- package/agent/continue-decision.mjs +14 -8
- package/agent/digest-account.mjs +133 -59
- package/agent/dispatch-run.mjs +1 -1
- package/agent/dispatch.mjs +2 -1
- package/agent/family-tools.mjs +12 -13
- package/agent/helpers.mjs +17 -72
- package/agent/image-heal.mjs +18 -4
- package/agent/post-turn.mjs +13 -26
- package/agent/record-results.mjs +21 -6
- package/agent/run-stages.mjs +72 -37
- package/agent/run-start.mjs +148 -113
- package/agent/setup-reminders.mjs +187 -91
- package/agent/setup.mjs +204 -138
- package/agent/spawn-child.mjs +2 -2
- package/agent/suspension.mjs +43 -15
- package/agent/timers.mjs +4 -1
- package/agent/turn-domain-text.mjs +105 -0
- package/agent/turn-loop.mjs +54 -22
- package/agent/turn-projection.mjs +127 -227
- package/agent/turn-tool-process.mjs +32 -37
- package/agent-tools/advisor-async.mjs +6 -3
- package/agent-tools/advisor-launch.mjs +5 -5
- package/agent-tools/advisor.mjs +1 -1
- package/agent-tools/async-discard.mjs +7 -2
- package/agent-tools/async-settle.mjs +18 -12
- package/agent-tools/bash-async.mjs +5 -2
- package/agent-tools/batch-closeout.mjs +12 -62
- package/agent-tools/batch-ledger-bind.mjs +19 -8
- package/agent-tools/batch-lifecycle.mjs +37 -11
- package/agent-tools/batch-read.mjs +151 -4
- package/agent-tools/batch-record-write.mjs +5 -5
- package/agent-tools/batch-segment-vocab.mjs +84 -4
- package/agent-tools/batch-skeleton.mjs +18 -14
- package/agent-tools/batch-supersede.mjs +1 -1
- package/agent-tools/batch-tier-gate.mjs +36 -119
- package/agent-tools/batch.mjs +30 -20
- package/agent-tools/browser-async.mjs +5 -2
- package/agent-tools/consult.mjs +10 -7
- package/agent-tools/context.mjs +47 -31
- package/agent-tools/digest-budget.mjs +10 -9
- package/agent-tools/eng.mjs +3 -4
- package/agent-tools/escalate-async.mjs +3 -3
- package/agent-tools/goal.mjs +5 -2
- package/agent-tools/judge-domain.mjs +347 -0
- package/agent-tools/manifest.mjs +2 -2
- package/agent-tools/parent-channel.mjs +6 -3
- package/agent-tools/plan.mjs +11 -10
- package/agent-tools/review-facts.mjs +9 -3
- package/agent-tools/skill.mjs +7 -8
- package/agent-tools/spawn-gates.mjs +5 -16
- package/agent-tools/ssh-session.mjs +6 -3
- package/agent-tools/ssh-transfer.mjs +1 -1
- package/agent-tools/subagent-actions.mjs +2 -2
- package/agent-tools/subagent-async.mjs +11 -8
- package/agent-tools/subagent-scheduler.mjs +9 -9
- package/agent-tools/subagent-spawn.mjs +2 -2
- package/agent-tools/subagent.mjs +5 -18
- package/agent-tools/task.mjs +0 -8
- package/agent.mjs +15 -15
- package/auto-think.mjs +5 -2
- package/compaction-work-state.mjs +230 -0
- package/compress-form.mjs +41 -6
- package/config-defaults.mjs +5 -2
- package/config.mjs +53 -17
- package/context-degrade.mjs +79 -48
- package/context-echo.mjs +6 -26
- package/context-push.mjs +328 -31
- package/context.mjs +203 -141
- package/conventions.mjs +1 -1
- package/declaration.mjs +95 -17
- package/escape.mjs +6 -5
- package/explore-distill.mjs +35 -8
- package/generate-title.mjs +10 -1
- package/knowledge/prompt-resources.md +5 -1
- package/ledger-tools.mjs +50 -50
- package/ledger-write-ops.mjs +43 -16
- package/memory/docs.mjs +51 -15
- package/memory/file-list.mjs +6 -0
- package/model-specs.mjs +87 -21
- package/origin-guard.mjs +190 -0
- package/package.json +1 -1
- package/prompt-files.mjs +33 -11
- package/prompt-overlays.mjs +12 -5
- package/prompts/advisor-design.md +55 -36
- package/prompts/advisor-round1.md +18 -7
- package/prompts/advisor-round2.md +18 -7
- package/prompts/advisor-round3.md +18 -7
- package/prompts/common.md +89 -51
- package/prompts/consult-base.md +2 -3
- package/prompts/discipline-engineering.md +474 -440
- package/prompts/discipline-normal.md +107 -31
- package/prompts/knowledge/business-gates.md +23 -15
- package/prompts/knowledge/decision-quality.md +16 -18
- package/prompts/knowledge/doc-lookup.md +22 -27
- package/prompts/knowledge/document-writing.md +31 -49
- package/prompts/knowledge/documentation-evaluation.md +37 -45
- package/prompts/knowledge/evidence-discipline.md +36 -55
- package/prompts/knowledge/normal-coding-workflow.md +27 -21
- package/prompts/knowledge/normal-delivery-collaboration.md +33 -17
- package/prompts/knowledge/normal-work-management.md +10 -7
- package/prompts/knowledge/product-security.md +22 -17
- package/prompts/knowledge/project-records.md +63 -52
- package/prompts/knowledge/tool-apply-patch.md +12 -13
- package/prompts/knowledge/tool-bash.md +13 -4
- package/prompts/knowledge/tool-batch-eng-coder.md +13 -6
- package/prompts/knowledge/tool-batch-eng-designer.md +14 -6
- package/prompts/knowledge/tool-batch-review.md +12 -6
- package/prompts/knowledge/tool-batch.md +76 -62
- package/prompts/knowledge/tool-browser.md +45 -34
- package/prompts/knowledge/tool-context.md +2 -2
- package/prompts/knowledge/tool-delete.md +5 -1
- package/prompts/knowledge/tool-doc-check.md +25 -24
- package/prompts/knowledge/tool-doc-graph-read.md +3 -3
- package/prompts/knowledge/tool-doc-graph.md +15 -15
- package/prompts/knowledge/tool-edit.md +26 -23
- package/prompts/knowledge/tool-execute.md +9 -4
- package/prompts/knowledge/tool-fetch.md +7 -1
- package/prompts/knowledge/tool-file-ops.md +6 -1
- package/prompts/knowledge/tool-git-read.md +3 -1
- package/prompts/knowledge/tool-git.md +1 -1
- package/prompts/knowledge/tool-glob.md +3 -1
- package/prompts/knowledge/tool-grep.md +5 -2
- package/prompts/knowledge/tool-hashline-edit.md +4 -2
- package/prompts/knowledge/tool-insert-after.md +3 -1
- package/prompts/knowledge/tool-ledger-read.md +6 -13
- package/prompts/knowledge/tool-ledger.md +14 -10
- package/prompts/knowledge/tool-ls.md +2 -0
- package/prompts/knowledge/tool-manifest.md +10 -7
- package/prompts/knowledge/tool-memory.md +15 -11
- package/prompts/knowledge/tool-operations.md +59 -71
- package/prompts/knowledge/tool-plan.md +1 -2
- package/prompts/knowledge/tool-process.md +1 -1
- package/prompts/knowledge/tool-read-history.md +6 -7
- package/prompts/knowledge/tool-read.md +1 -0
- package/prompts/knowledge/tool-settings.md +6 -7
- package/prompts/knowledge/tool-ssh.md +37 -15
- package/prompts/knowledge/tool-subagent.md +37 -39
- package/prompts/knowledge/tool-task.md +3 -1
- package/prompts/knowledge/tool-team-ledger.md +5 -1
- package/prompts/knowledge/tool-tree.md +2 -0
- package/prompts/knowledge/tool-wait-for.md +16 -16
- package/prompts/knowledge/tool-write.md +4 -1
- package/prompts/knowledge/upstream-protocol.md +39 -37
- package/prompts/persona-coder.md +1 -3
- package/prompts/persona-eng-coder.md +5 -5
- package/prompts/persona-eng-designer.md +34 -17
- package/prompts/persona-engineering.md +532 -501
- package/prompts/persona-explore.md +0 -2
- package/prompts/persona-normal.md +17 -9
- package/prompts/persona-plan.md +0 -2
- package/prompts/persona-tester.md +0 -2
- package/prompts/subagent-base.md +0 -2
- package/provider/anthropic.mjs +4 -2
- package/provider/core.mjs +5 -3
- package/provider/errors.mjs +41 -7
- package/provider/google.mjs +5 -3
- package/provider/reasoning-echo.mjs +16 -0
- package/provider/responses.mjs +4 -2
- package/provider/sse.mjs +11 -6
- package/rules.mjs +11 -10
- package/session-lifecycle.mjs +115 -81
- package/session-slot-write.mjs +1 -1
- package/session.mjs +23 -20
- package/shell-identity.mjs +129 -22
- package/skip-dirs.mjs +24 -0
- package/team-ledger.mjs +22 -22
- package/text-budget.mjs +1 -1
- package/think-off.mjs +27 -0
- package/token-ttl.mjs +1 -1
- package/token-window.mjs +278 -88
- package/tool-docs/bash.md +0 -2
- package/tool-docs/consult_start.md +2 -2
- package/tool-docs/consult_stop.md +2 -2
- package/tools/browser.mjs +8 -8
- package/tools/execute.mjs +2 -2
- package/tools/file.mjs +17 -11
- package/tools/git.mjs +12 -12
- package/tools/ops.mjs +2 -2
- package/tools/patch.mjs +1 -1
- package/tools/process-doc-mark.mjs +33 -4
- package/tools/repomap.mjs +25 -3
- package/tools/search.mjs +74 -26
- package/tools/shared.mjs +4 -3
- package/tools/ssh.mjs +5 -5
- package/tools/tree.mjs +32 -10
- package/tools/web-deepseek.mjs +1 -1
- package/tools/web.mjs +2 -2
- package/tools/write-gate-params.mjs +3 -3
- package/window-policy.mjs +118 -78
package/tools/tree.mjs
CHANGED
|
@@ -11,6 +11,8 @@ import { join, basename, extname } from "node:path"
|
|
|
11
11
|
// 两项旧值随之退场:`turbo`(无点,笔误——另三份都是 `.turbo`)、`bin`(`thincoder-cli/bin/**`
|
|
12
12
|
// 是 CLI 入口、产品代码,不该从任何面上被剪掉)。
|
|
13
13
|
import { SKIP_DIRS } from "../skip-dirs.mjs"
|
|
14
|
+
// 过程档默认排除 + 层标识(机制 = `docs/core/design/TOOLS.md` §7.1):只对**档结果**判,目录节点照留。
|
|
15
|
+
import { isProcessDocPath, docLayerName, processDocsReport } from "./process-doc-mark.mjs"
|
|
14
16
|
|
|
15
17
|
const MAX_ENTRIES = 200
|
|
16
18
|
const DEFAULT_DEPTH = 3
|
|
@@ -38,6 +40,7 @@ export const treeTool = {
|
|
|
38
40
|
properties: {
|
|
39
41
|
path: { type: "string", description: "Root directory (default cwd)" },
|
|
40
42
|
depth: { type: "integer", description: `Tree depth (default ${DEFAULT_DEPTH}, max 6)` },
|
|
43
|
+
includeProcessDocs: { type: "boolean", description: "Include process-layer docs (batch records, archived material) — omitted = excluded by default; true = include them" },
|
|
41
44
|
},
|
|
42
45
|
required: [],
|
|
43
46
|
},
|
|
@@ -51,13 +54,22 @@ export const treeTool = {
|
|
|
51
54
|
const d = Number(args.depth)
|
|
52
55
|
const maxDepth = Number.isInteger(d) && d > 0 ? Math.min(d, 6) : DEFAULT_DEPTH
|
|
53
56
|
const lines = [basename(root) + "/"]
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
// 被排除的档先移出 `items`,再算 `├──` ∥ `└──` 与 `state.count`:末项字形不落在被排除的条目上,
|
|
58
|
+
// 「…(更多项已省略)」也落在存活条目之后。`MAX_ENTRIES` 与 `truncate` 都不变。
|
|
59
|
+
const opts = {
|
|
60
|
+
includeProcessDocs: args.includeProcessDocs === true,
|
|
61
|
+
procCache: new Map(),
|
|
62
|
+
layerCache: new Map(),
|
|
63
|
+
}
|
|
64
|
+
const state = { count: 1, capped: false, excluded: 0, included: 0 }
|
|
65
|
+
await walk(root, 0, maxDepth, "", lines, state, opts)
|
|
66
|
+
const body = truncate(lines.join("\n"))
|
|
67
|
+
const n = opts.includeProcessDocs ? state.included : state.excluded
|
|
68
|
+
return n > 0 ? `${body}\n${processDocsReport(n, opts.includeProcessDocs)}` : body
|
|
57
69
|
},
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
async function walk(dir, depth, maxDepth, prefix, lines, state) {
|
|
72
|
+
async function walk(dir, depth, maxDepth, prefix, lines, state, opts) {
|
|
61
73
|
if (state.capped) return
|
|
62
74
|
let entries
|
|
63
75
|
try { entries = await readdir(dir, { withFileTypes: true }) } catch { return }
|
|
@@ -68,17 +80,27 @@ async function walk(dir, depth, maxDepth, prefix, lines, state) {
|
|
|
68
80
|
if (isDir) { if (SKIP_DIRS.has(e.name)) continue; items.push({ name: e.name, isDir: true }) }
|
|
69
81
|
else if (!BINARY_EXTS.has(extname(e.name).toLowerCase())) items.push({ name: e.name, isDir: false })
|
|
70
82
|
}
|
|
71
|
-
|
|
83
|
+
const kept = []
|
|
84
|
+
for (const it of items) {
|
|
85
|
+
if (it.isDir) { kept.push(it); continue }
|
|
86
|
+
const proc = isProcessDocPath(join(dir, it.name), opts.procCache)
|
|
87
|
+
if (proc && !opts.includeProcessDocs) { state.excluded += 1; continue }
|
|
88
|
+
if (proc) state.included += 1
|
|
89
|
+
kept.push(it)
|
|
90
|
+
}
|
|
91
|
+
kept.sort((a, b) => (a.isDir === b.isDir ? a.name.localeCompare(b.name) : a.isDir ? -1 : 1))
|
|
72
92
|
|
|
73
|
-
for (let i = 0; i <
|
|
93
|
+
for (let i = 0; i < kept.length; i++) {
|
|
74
94
|
if (state.capped) return
|
|
75
95
|
if (state.count >= MAX_ENTRIES) { state.capped = true; lines.push(prefix + "…(更多项已省略)"); return }
|
|
76
|
-
const { name, isDir } =
|
|
77
|
-
const isLast = i ===
|
|
78
|
-
|
|
96
|
+
const { name, isDir } = kept[i]
|
|
97
|
+
const isLast = i === kept.length - 1
|
|
98
|
+
// 层标识跟在条目名之后(本行字形是结构);目录行不带——目录不是档。
|
|
99
|
+
const text = isDir ? name + "/" : `${name} [${docLayerName(join(dir, name), opts.layerCache)}]`
|
|
100
|
+
lines.push(prefix + (isLast ? "└── " : "├── ") + text)
|
|
79
101
|
state.count++
|
|
80
102
|
if (isDir && depth + 1 < maxDepth) {
|
|
81
|
-
await walk(join(dir, name), depth + 1, maxDepth, prefix + (isLast ? " " : "│ "), lines, state)
|
|
103
|
+
await walk(join(dir, name), depth + 1, maxDepth, prefix + (isLast ? " " : "│ "), lines, state, opts)
|
|
82
104
|
}
|
|
83
105
|
}
|
|
84
106
|
}
|
package/tools/web-deepseek.mjs
CHANGED
|
@@ -54,7 +54,7 @@ export function backendMode(cfg, key) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/** First qualifying channel in stored order → `{ apiKey }` | null. Never throws. */
|
|
57
|
-
function deepseekChannel(cfg) {
|
|
57
|
+
export function deepseekChannel(cfg) {
|
|
58
58
|
const list = Array.isArray(cfg?.providersList) ? cfg.providersList
|
|
59
59
|
: Array.isArray(cfg?.providers) ? cfg.providers : []
|
|
60
60
|
for (const entry of list) {
|
package/tools/web.mjs
CHANGED
|
@@ -102,7 +102,7 @@ export const websearchTool = {
|
|
|
102
102
|
limit: { type: "number", description: "Max results (default 8, max 20)" },
|
|
103
103
|
engine: { type: "string", enum: ENGINE_NAMES, description: "Specific engine — \"bing\" (Bing). Omit to search all engines concurrently." },
|
|
104
104
|
page: { type: "number", description: "Page number for pagination (1-based, default 1). Only used when engine is specified." },
|
|
105
|
-
proxy: { type: "string", description: "http://host:port explicit proxy
|
|
105
|
+
proxy: { type: "string", description: "`http://host:port` explicit proxy. Use ONLY when passed; no proxy = direct. config.json's proxy is NOT auto-applied — pick per target (github and foreign sites usually need one, domestic ones do not)." },
|
|
106
106
|
},
|
|
107
107
|
required: ["query"],
|
|
108
108
|
},
|
|
@@ -207,7 +207,7 @@ export const fetchTool = {
|
|
|
207
207
|
type: "object",
|
|
208
208
|
properties: {
|
|
209
209
|
url: { type: "string", description: "http/https URL" },
|
|
210
|
-
proxy: { type: "string", description: "http://host:port explicit proxy
|
|
210
|
+
proxy: { type: "string", description: "`http://host:port` explicit proxy. Use ONLY when passed; no proxy = direct. config.json's proxy is NOT auto-applied — pick per target (github and foreign sites usually need one, domestic ones do not)." },
|
|
211
211
|
},
|
|
212
212
|
required: ["url"],
|
|
213
213
|
},
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
// 为什么不进 `required`(决策 35):`required` 是静态字面,而这十一把工具在两模式、子代理、出辖目标下共用;
|
|
18
18
|
// 静态必填会让普通模式读到一条不成立的强制(写锁仅工程模式),也让子代理读到一条没人核的要求。
|
|
19
19
|
// 机械强制在门(缺号即拒),schema 只负责让模型知道有这个参数;拒文因此是唯一教学面(决策 38)。
|
|
20
|
-
export const LEDGER_PARAM_DESC = '
|
|
20
|
+
export const LEDGER_PARAM_DESC = 'In-flight ledger row this call serves: `"#N"` or `"N"`'
|
|
21
21
|
/** `memory`:只有 `layer:"project"` 写项目内 `.thincoder/memory/**`(决策 39;`put` 缺省 `personal`)。 */
|
|
22
22
|
export const LEDGER_PARAM_DESC_MEMORY = LEDGER_PARAM_DESC
|
|
23
|
-
+ '
|
|
23
|
+
+ '; only write actions landing in the `project` layer need one. Pass `layer:"project"`, or omit `layer` on a single delete whose id starts `project:` — the prefix picks the layer. `personal` and `team` land in the user data directory and need none.'
|
|
24
24
|
/** 写门 `project` 参数(2026-11-03 项目声明面批 · 设计档 §2.3「工具 schema 面」/ 决策 40):项目根路径(带
|
|
25
25
|
* `PROJECT-MANIFEST.json` 的那个目录)—— 台账按项目根算键,所以「传项目」就是把这个根传进来,零映射表。
|
|
26
26
|
* 定位参数在场时以它为准,`project` 只补位;纯声明,不改执行目录(第二句因此是语义,不是「何时必带」)。
|
|
@@ -28,4 +28,4 @@ export const LEDGER_PARAM_DESC_MEMORY = LEDGER_PARAM_DESC
|
|
|
28
28
|
* 而「目标取不到项目根」真会发生(工作区根的文件)——那时 `project` 是唯一可用的判据点,schema 不给
|
|
29
29
|
* 这一格,门里的口子就永远触发不了(模型不知道有这个参数,第一次撞的还是拒文)。何时必带那条口径
|
|
30
30
|
* 在工程纪律段的写门参数一节,参数行只留身份。 */
|
|
31
|
-
export const PROJECT_PARAM_DESC = '
|
|
31
|
+
export const PROJECT_PARAM_DESC = 'Project root: the directory holding `PROJECT-MANIFEST.json` (absolute or cwd-relative). Declaration only — `workdir` changes cwd.'
|
package/window-policy.mjs
CHANGED
|
@@ -4,66 +4,76 @@
|
|
|
4
4
|
* 迁出:`config.mjs` 的预算族(`outputReserve` ∥ `usableWindow` ∥ `requestBudget` ∥
|
|
5
5
|
* `resolveCompactThreshold`)整族迁入本档;`config.mjs` 经 `export … from` 转口保既有导入面(先例 =
|
|
6
6
|
* `model-specs.mjs` 自 `config.mjs` 抽出的同形)。
|
|
7
|
-
* 新增:`
|
|
7
|
+
* 新增:`effectiveWindow` ∥ `policyBudget`——窗口只有一个事实来源(`providerSpec`
|
|
8
8
|
* 的五级链,`PROVIDER.md` §6.15),策略出口只有 `effectiveWindow` 一个(`CONTEXT-COMPACTION.md` §2.1 / §2.2)。
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* 缺参即拒(§2.3):`workMode` 无默认值,值不在 `WORK_MODES` 里就在函数入口拒绝并点名参数——默认值会把
|
|
13
|
-
* 「忘传」静默成一条不下上限的窗口,压缩线随之上移,人要等到账单才发现。
|
|
10
|
+
* 为什么要单出一层:策略值只在读数上现算,**不落在 provider ∥ config 对象上**——派生字段会被落盘 ∥
|
|
11
|
+
* 跨会话带走,改了配置要等下一次装配才生效,而它必须在下一处读数上就生效。
|
|
14
12
|
*
|
|
15
|
-
* 派生链(§2.2):`window` 是唯一输入。`reserve` 与窗口无关;`limit = window − reserve
|
|
16
|
-
* min(
|
|
17
|
-
* `threshold
|
|
18
|
-
*
|
|
13
|
+
* 派生链(§2.2):`window` 是唯一输入。`reserve` 与窗口无关;`limit = window − reserve`;
|
|
14
|
+
* `headroom = min(INJECT_HEADROOM_MAX_TOKENS, floor(INJECT_HEADROOM_RATIO × limit))`;
|
|
15
|
+
* `threshold = min(limit − headroom, floor(COMPACT_RATIO × window))`。两支都随窗口不减小 ⇒ 派生单调;
|
|
16
|
+
* `limit` 对窗口的斜率恒为 1,压缩线是两支中较小的一支。系统不给压缩线固定的值或固定上限——
|
|
17
|
+
* 要更紧的线由用户配 `agent.compactThreshold` 的显式正数,该值直接生效、不参与派生。
|
|
18
|
+
* 算式里没有静默兜底:派生不成立时按 §2.4 响一声、夹住、放行——判据照旧抛,接住它的是
|
|
19
|
+
* `budgetOf`(返回带 `contradiction` 的预算,两个派生值夹到 `BUDGET_FLOOR_TOKENS`)。
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
21
|
+
* 档位不是入参(§2.1 ∥ §2.3):tiny ∥ normal ∥ engineering 三档的有效窗口同值,档位只影响提示词、工具面与
|
|
22
|
+
* 注入开关。一个不影响任何输出的入参比一条缺省值更坏——它让每个调用点以为档位还在参与预算。
|
|
23
|
+
*
|
|
24
|
+
* 依赖面 = `model-specs.mjs`(`providerSpec` ∥ `providerSpecResolution`)——**零 import 环**(本档不 import
|
|
25
|
+
* `config.mjs`,只被它转口)。
|
|
22
26
|
*/
|
|
23
27
|
import { providerSpec, providerSpecResolution } from "./model-specs.mjs"
|
|
24
|
-
import { WORK_MODES } from "./config-migrate.mjs"
|
|
25
28
|
|
|
26
|
-
/**
|
|
27
|
-
|
|
29
|
+
/** 压缩线占窗口的比例(`CONTEXT-COMPACTION.md` §2.2):窗口用到六成即触发压缩。用户裁定的比率,
|
|
30
|
+
* 也是比率臂里唯一的乘子——压缩线没有第二个常量。数值在本档只出现这一处。 */
|
|
31
|
+
export const COMPACT_RATIO = 0.6
|
|
28
32
|
|
|
29
|
-
/**
|
|
33
|
+
/** 注入余量的绝对封顶(`CONTEXT-COMPACTION.md` §2.2):大窗口上余量封在这里。
|
|
30
34
|
* Headroom above the line absorbs injected context (directory tree, git context, outline, project instructions,
|
|
31
|
-
* memory/doc search — 30–50K). 这些量按会话注入、**不随窗口缩放**:1M 窗口上与 320K
|
|
32
|
-
* 所以正确的刻度是绝对量,不是窗口比例。
|
|
35
|
+
* memory/doc search — 30–50K). 这些量按会话注入、**不随窗口缩放**:1M 窗口上与 320K 窗口上它们占同样多的条目。
|
|
33
36
|
* 取上界 50000 的理由不对称:估小一档的后果是注入把 prompt 顶过硬上限而压缩线帮不上(守卫报 `context_overflow`),
|
|
34
|
-
*
|
|
35
|
-
export const
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
* 只夹 `threshold`,不夹 `limit`:`limit` 是物理线(窗口减预留),把它压到之下会在真实越窗之前就报错。 */
|
|
42
|
-
export const RETAIN_CAP_TOKENS = 262144
|
|
43
|
-
|
|
44
|
-
/** 模式入参的准入判据(§2.3 缺参即拒):无默认值。合法值域单源 = `WORK_MODES`(不在此重列字面)。 */
|
|
45
|
-
function assertWorkMode(workMode, fnName) {
|
|
46
|
-
if (!WORK_MODES.includes(workMode)) {
|
|
47
|
-
throw new Error(`${fnName}: workMode is required and must be one of ${WORK_MODES.join(" / ")} — got ${JSON.stringify(workMode)}`)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
37
|
+
* 估大一档只多付一次早压。它是**封顶**,不是余量本身——小窗口上余量按 `INJECT_HEADROOM_RATIO` 收缩。 */
|
|
38
|
+
export const INJECT_HEADROOM_MAX_TOKENS = 50000
|
|
39
|
+
|
|
40
|
+
/** 注入余量占输入上限的比例(`CONTEXT-COMPACTION.md` §2.2):`floor(0.25 × limit)` 与上一个常量取较小者。
|
|
41
|
+
* 小窗口正是每一千 token 都要用在请求上的那一档,按大窗口的绝对量付余量最经不起;大窗口上比例臂远大于
|
|
42
|
+
* 50,000,于是由封顶接管。 */
|
|
43
|
+
export const INJECT_HEADROOM_RATIO = 0.25
|
|
50
44
|
|
|
51
|
-
/**
|
|
52
|
-
*
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
/** 派生值的地板(`CONTEXT-COMPACTION.md` §2.4):派生不成立时 `limit` 与 `threshold` 同夹到这一格。
|
|
46
|
+
* 夹住不是兜底值代跑——地板让每一条路都当场判成装不下(`estPrompt > 1` 近乎恒真),没有一个请求因此发得出去。
|
|
47
|
+
* 取 1 不取 `INJECT_HEADROOM_MAX_TOKENS + 1`:小请求会过我们的守卫、被服务端 400,白付一次往返与自愈。 */
|
|
48
|
+
export const BUDGET_FLOOR_TOKENS = 1
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 配置计数值的**唯一**取值域谓词(`CONTEXT-COMPACTION.md` §1「保留窗与跨轮清理」∥ §2.2):
|
|
52
|
+
* 数字或数字串、有限非负整数 ⇒ 该数;其余(负数 ∥ 小数 ∥ 非数字 ∥ 布尔 ∥ 对象 ∥ `null` ∥ 空串)⇒ `null`。
|
|
53
|
+
* `agent.compactThreshold` 与 `agent.keepToolTurns` 共用它——三端输入面与装载期一律调这一份,另写一个
|
|
54
|
+
* 等价判断就是同一规则的第二个实现(改一处、别处不跟,正是不一致的老路)。
|
|
55
|
+
* 数字串按十进制整数文本收(`"1e3"` ∥ `"0x10"` ∥ `"+5"` 一律 `null`):配置里手写的这两种形是笔误,
|
|
56
|
+
* 静默转数会把「想写哪个量」藏起来。空串先返回 `null`——`Number("")` 是 0,不先判就变成一个合法的 0。
|
|
57
|
+
*/
|
|
58
|
+
export function nonNegativeIntValue(raw) {
|
|
59
|
+
if (typeof raw === "number") return Number.isFinite(raw) && Number.isInteger(raw) && raw >= 0 ? raw : null
|
|
60
|
+
if (typeof raw !== "string") return null
|
|
61
|
+
const text = raw.trim()
|
|
62
|
+
if (!/^\d+$/.test(text)) return null
|
|
63
|
+
const num = Number(text)
|
|
64
|
+
return Number.isFinite(num) && Number.isInteger(num) && num >= 0 ? num : null
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
/**
|
|
58
|
-
* 有效窗口(§2.1):本 run
|
|
59
|
-
*
|
|
68
|
+
* 有效窗口(§2.1):本 run 参与预算算式和一切上下文百分比分母的输入窗口。它**恒等于模型的真实窗口**,
|
|
69
|
+
* 档位不参与:tiny ∥ normal ∥ engineering 三档同口径。
|
|
70
|
+
* 模型事实仍只由 `providerSpec` 回答,本档不改它;渠道在配置里声明了 `providers[].context` 覆盖时,
|
|
71
|
+
* 五级链把覆盖值排在规格之前,本函数随之取覆盖值——覆盖是渠道事实,不是档位策略。
|
|
60
72
|
* 窗口派生的一切读数(threshold ∥ limit ∥ 尾部两式 ∥ 百分比分母)全从本函数取,端侧不另立算式。
|
|
61
73
|
* @param {object} provider 在用渠道条目(含 model)
|
|
62
|
-
* @param {"tiny"|"normal"|"engineering"} workMode 必填,无缺省
|
|
63
74
|
*/
|
|
64
|
-
export function effectiveWindow(provider
|
|
65
|
-
|
|
66
|
-
return capWindow(providerSpec(provider).context, workMode)
|
|
75
|
+
export function effectiveWindow(provider) {
|
|
76
|
+
return providerSpec(provider).context
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
// ─── 预算族(自 `config.mjs` 迁出;算式 = §2.2 的四行)────────────────────────
|
|
@@ -102,15 +112,21 @@ export function outputReserve(provider, spec) {
|
|
|
102
112
|
return Math.min(maxOutput, defaultOutput)
|
|
103
113
|
}
|
|
104
114
|
|
|
115
|
+
/** 派生链的减法一格(§2.2 的 `limit`):`usableWindow` 判它与 `budgetOf` 报差值读同一个量——
|
|
116
|
+
* 两处各写一次减法,矛盾态上就会报出两个差数。 */
|
|
117
|
+
function usableDelta(context, reserve) {
|
|
118
|
+
return (context ?? 0) - reserve
|
|
119
|
+
}
|
|
120
|
+
|
|
105
121
|
/**
|
|
106
122
|
* Usable input window = window − output reservation (§2.2 的 `limit`)。判据单点(§2.4):
|
|
107
|
-
* `reserve ≥ window` 时派生链没有解——没有任何输入装得下,`limit ≤ 0
|
|
108
|
-
*
|
|
109
|
-
*
|
|
123
|
+
* `reserve ≥ window` 时派生链没有解——没有任何输入装得下,`limit ≤ 0`。
|
|
124
|
+
* 这一格不静默兜底:判据照旧抛,报文点三个数(window ∥ reserve ∥ 差值)与三条出路;接住它的是 `budgetOf`
|
|
125
|
+
* (按 §2.4 响一声、夹住、放行),装载期因此不再终止进程。
|
|
110
126
|
*/
|
|
111
127
|
export function usableWindow(context, reserve) {
|
|
112
128
|
const ctx = context ?? 0
|
|
113
|
-
const usable = ctx
|
|
129
|
+
const usable = usableDelta(ctx, reserve)
|
|
114
130
|
if (usable <= 0) {
|
|
115
131
|
throw new Error(
|
|
116
132
|
`usableWindow: the output reservation ${reserve} leaves no input window — window ${ctx} − reserve ${reserve} = ${usable}. ` +
|
|
@@ -133,11 +149,34 @@ export function usableWindow(context, reserve) {
|
|
|
133
149
|
* (undefined ⇒ NaN — §6.17 guard); the guard reads limit instead.
|
|
134
150
|
* - reserve: what the request actually takes out of the window for output (see `outputReserve`).
|
|
135
151
|
* - limit: window − reserve — the input ceiling the server accepts (prompt + reserved output ≤ window).
|
|
136
|
-
* - threshold: min(limit −
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
152
|
+
* - threshold: min(limit − headroom, floor(COMPACT_RATIO × window)) — the compaction trigger line,
|
|
153
|
+
* where headroom = min(INJECT_HEADROOM_MAX_TOKENS, floor(INJECT_HEADROOM_RATIO × limit)).
|
|
154
|
+
* The line stays below `limit` (第一支恒不超过 `limit`), so the guard line never sits above the
|
|
155
|
+
* physical one — a request that fits would otherwise be refused before it ever overflows.
|
|
156
|
+
* 派生不成立(只剩 `reserve ≥ window` 一格)在这里接住(§2.4):判据的语音仍是 `usableWindow` 的抛,
|
|
157
|
+
* 本函数把它折成 `contradiction`(带 window ∥ reserve ∥ 差值),并把 `limit` ∥ `threshold` 夹到
|
|
158
|
+
* `BUDGET_FLOOR_TOKENS`——守卫面因此永远拿到一个正数 `limit`,而矛盾预算上每一条路都判装不下。
|
|
140
159
|
*/
|
|
160
|
+
|
|
161
|
+
/** 派生链的三行(§2.2):`limit` 与压缩线。两处无解只剩一格(`reserve ≥ window`),在这里抛——判据的语音不变,
|
|
162
|
+
* 接住它的是 `budgetOf`(§2.4)。抛出的错误带 `contradiction`:同一次算出的三个数,报文只从它取数,不另算一遍。 */
|
|
163
|
+
function deriveLines(window, reserve) {
|
|
164
|
+
let limit
|
|
165
|
+
try {
|
|
166
|
+
limit = usableWindow(window, reserve)
|
|
167
|
+
} catch (err) {
|
|
168
|
+
// 无解格:差值 = 同一次减法,报文只点这三个数(§2.4 的唯一形状)。
|
|
169
|
+
err.contradiction = { window, reserve, usable: usableDelta(window, reserve) }
|
|
170
|
+
throw err
|
|
171
|
+
}
|
|
172
|
+
// 压缩线的两支(§2.2)取较小者——按值接管,不按窗口区间分工:交界随 `reserve` 浮动,门槛写在算式里,
|
|
173
|
+
// 不写成一个窗口数。窗口来自规格表或 K 单位覆盖(≥ 1024),第二支因此恒正;`headroom` 只在
|
|
174
|
+
// `limit ≤ 3` 时为 0,那时第一支等于 `limit`——`threshold ≤ 0` 不构成第二条可达矛盾。
|
|
175
|
+
const headroom = Math.min(INJECT_HEADROOM_MAX_TOKENS, Math.floor(INJECT_HEADROOM_RATIO * limit))
|
|
176
|
+
const threshold = Math.min(limit - headroom, Math.floor(COMPACT_RATIO * window))
|
|
177
|
+
return { limit, threshold }
|
|
178
|
+
}
|
|
179
|
+
|
|
141
180
|
function budgetOf(provider, spec, window, windowSource) {
|
|
142
181
|
const explicitRaw = Number(provider?.maxTokens)
|
|
143
182
|
const explicit = Number.isFinite(explicitRaw) && explicitRaw > 0 ? explicitRaw : undefined
|
|
@@ -145,17 +184,17 @@ function budgetOf(provider, spec, window, windowSource) {
|
|
|
145
184
|
const maxTokens = explicit ?? (anthropicAlwaysSends ? spec?.maxOutput ?? 0 : undefined)
|
|
146
185
|
const maxTokensSource = explicit != null ? "explicit" : anthropicAlwaysSends ? "spec" : "absent"
|
|
147
186
|
const reserve = outputReserve(provider, spec)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
)
|
|
187
|
+
let lines
|
|
188
|
+
let contradiction = null
|
|
189
|
+
try {
|
|
190
|
+
lines = deriveLines(window, reserve)
|
|
191
|
+
} catch (err) {
|
|
192
|
+
if (!err?.contradiction) throw err // 只接派生不成立;别的错误照旧上传
|
|
193
|
+
contradiction = err.contradiction
|
|
194
|
+
lines = { limit: BUDGET_FLOOR_TOKENS, threshold: BUDGET_FLOOR_TOKENS }
|
|
157
195
|
}
|
|
158
|
-
|
|
196
|
+
const budget = { window, windowSource, maxTokens, maxTokensSource, reserve, limit: lines.limit, threshold: lines.threshold, contradiction }
|
|
197
|
+
return budget
|
|
159
198
|
}
|
|
160
199
|
|
|
161
200
|
/** Request budget — the MODEL-window exit (§2.2). Single source for the actually-sent max_tokens
|
|
@@ -175,29 +214,30 @@ export function requestBudget(provider, spec) {
|
|
|
175
214
|
/** Policy budget — the EFFECTIVE-window exit (§2.2): what the compaction threshold, the stats face
|
|
176
215
|
* and the pre-send guard read (window ∥ limit ∥ threshold).
|
|
177
216
|
* `maxTokens ∥ maxTokensSource ∥ reserve ∥ windowSource` 逐位转交 `requestBudget`(同一算式、同一次五级链
|
|
178
|
-
* 遍历),不另算;与它不同的只有窗口那一边 = `effectiveWindow
|
|
179
|
-
*
|
|
217
|
+
* 遍历),不另算;与它不同的只有窗口那一边 = `effectiveWindow`——有效窗口恒等于模型窗口(§2.1),
|
|
218
|
+
* 两个出口因此在窗口上同值,差别只剩各自读的那一行 provider。
|
|
219
|
+
* `windowSource` 说的是**模型窗口**的来源名,照抄而不另起一个来源名,是 §2.2 的裁定。
|
|
180
220
|
* @param {object} provider 在用渠道条目
|
|
181
221
|
* @param {object} [spec] 调用方已算过的 providerSpec(省一次查表;来源仍读五级链)
|
|
182
|
-
* @param {"tiny"|"normal"|"engineering"} workMode 必填,无缺省
|
|
183
222
|
*/
|
|
184
|
-
export function policyBudget(provider, spec
|
|
185
|
-
assertWorkMode(workMode, "policyBudget")
|
|
223
|
+
export function policyBudget(provider, spec) {
|
|
186
224
|
const resolution = providerSpecResolution(provider)
|
|
187
225
|
const resolved = spec ?? resolution.spec
|
|
188
|
-
return budgetOf(provider, resolved,
|
|
226
|
+
return budgetOf(provider, resolved, effectiveWindow(provider), resolution.source)
|
|
189
227
|
}
|
|
190
228
|
|
|
191
|
-
/** Derive compaction threshold; explicit
|
|
192
|
-
*
|
|
193
|
-
*
|
|
229
|
+
/** Derive compaction threshold; `explicit` 是配置文件里的原始值(可缺省、可为 0)。
|
|
230
|
+
* 「显式」的判据 = **有限正数**(CONTEXT-COMPACTION.md §2.2):过 `nonNegativeIntValue` 之后
|
|
231
|
+
* `v !== null && v > 0` ⇒ 该值覆盖派生线(`auto: false`);0 ∥ 空值 ∥ 非法值(手改落盘的负数、小数、
|
|
232
|
+
* 非数字)一律走 auto 支,`value = policyBudget().threshold`(§2.2 的两支取小,over the EFFECTIVE window,
|
|
233
|
+
* §2.1)。0 与缺省同义,都表示「不覆盖派生线」。
|
|
234
|
+
* 装载期(`config.mjs`)与三端输入面都调本函数/本谓词,不另写一份同形判断。
|
|
235
|
+
* Second param accepts EITHER a model name string (pure spec lookup —
|
|
194
236
|
* compat shape, no caller passes a string today) OR a provider object (providerSpec — the
|
|
195
|
-
* providers[].context override in K units is honored, PROVIDER.md §15 T-C2).
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
assertWorkMode(workMode, "resolveCompactThreshold")
|
|
200
|
-
if (explicit != null) return { value: explicit, auto: false }
|
|
237
|
+
* providers[].context override in K units is honored, PROVIDER.md §15 T-C2). */
|
|
238
|
+
export function resolveCompactThreshold(explicit, modelOrProvider) {
|
|
239
|
+
const v = nonNegativeIntValue(explicit)
|
|
240
|
+
if (v !== null && v > 0) return { value: v, auto: false }
|
|
201
241
|
const provider = typeof modelOrProvider === "string" ? { model: modelOrProvider } : (modelOrProvider ?? {})
|
|
202
|
-
return { value: policyBudget(provider
|
|
203
|
-
}
|
|
242
|
+
return { value: policyBudget(provider).threshold, auto: true }
|
|
243
|
+
}
|