@thincoder/core 0.11.3 → 0.11.5
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 +23 -0
- package/advisor/compaction.mjs +40 -17
- package/advisor/loop.mjs +14 -6
- package/advisor/notice.mjs +1 -1
- package/advisor/timeline.mjs +4 -4
- package/agent/completion.mjs +19 -0
- package/agent/dispatch-gates.mjs +2 -0
- package/agent/dispatch.mjs +2 -2
- package/agent/family-tools.mjs +13 -9
- package/agent/helpers.mjs +10 -6
- package/agent/planning-step.mjs +453 -0
- package/agent/post-turn.mjs +70 -2
- package/agent/run-stages.mjs +22 -50
- package/agent/run-start.mjs +17 -0
- package/agent/setup-reminders.mjs +71 -8
- package/agent/setup.mjs +8 -39
- package/agent/spawn-child.mjs +6 -2
- package/agent/task-lifecycle.mjs +146 -0
- package/agent/turn-loop.mjs +73 -11
- package/agent-tools/batch-closeout.mjs +21 -34
- package/agent-tools/batch-gate.mjs +7 -5
- package/agent-tools/batch-ledger-bind.mjs +21 -1
- package/agent-tools/batch-lifecycle.mjs +7 -7
- package/agent-tools/batch-record-write.mjs +237 -280
- package/agent-tools/batch-supersede.mjs +3 -3
- package/agent-tools/batch.mjs +42 -9
- package/agent-tools/context.mjs +0 -13
- package/agent-tools/escalate-async.mjs +7 -1
- package/agent-tools/parent-channel.mjs +7 -6
- package/agent-tools/plan.mjs +103 -67
- package/agent-tools/review-facts.mjs +3 -3
- package/agent-tools/settings.mjs +2 -0
- package/agent-tools/spawn-gates.mjs +37 -11
- package/agent-tools/ssh-session.mjs +68 -25
- package/agent-tools/ssh-transfer.mjs +7 -2
- package/agent-tools/ssh-transport.mjs +2 -2
- package/agent-tools/ssh-vault.mjs +2 -2
- package/agent-tools/step.mjs +462 -0
- package/agent-tools/subagent-actions.mjs +6 -1
- package/agent-tools/subagent-run.mjs +7 -0
- package/agent-tools/subagent-spawn.mjs +1 -1
- package/agent-tools/subagent.mjs +17 -11
- package/agent-tools/task.mjs +178 -26
- package/agent-tools.mjs +1 -0
- package/agent.mjs +2 -0
- package/config-defaults.mjs +17 -1
- package/config-migrate.mjs +8 -5
- package/config.mjs +7 -0
- package/context.mjs +5 -4
- package/declaration.mjs +4 -4
- package/generate-title.mjs +0 -6
- package/i18n.mjs +4 -0
- package/ledger-cmd.mjs +11 -1
- package/ledger-tools.mjs +15 -4
- package/manifest-schema.mjs +1 -0
- package/model-context.mjs +34 -11
- package/model-specs.mjs +5 -2
- package/package.json +1 -1
- package/prompt-files.mjs +48 -11
- package/prompt-overlays.mjs +79 -8
- package/prompts/advisor-design.md +253 -206
- package/prompts/advisor-round1.md +151 -145
- package/prompts/advisor-round2.md +136 -130
- package/prompts/advisor-round3.md +140 -134
- package/prompts/common.md +244 -96
- package/prompts/consult-base.md +29 -30
- package/prompts/discipline-engineering.md +73 -519
- package/prompts/discipline-normal.md +28 -136
- package/prompts/knowledge/batch-before-write.md +8 -0
- package/prompts/knowledge/batch-cycle-register.md +15 -0
- package/prompts/knowledge/batch-progress-ledger.md +29 -0
- package/prompts/knowledge/business-gates.md +24 -24
- package/prompts/knowledge/decision-quality.md +8 -19
- package/prompts/knowledge/doc-discipline-d-rules.md +63 -0
- package/prompts/knowledge/doc-lookup.md +25 -24
- package/prompts/knowledge/document-writing.md +11 -32
- package/prompts/knowledge/documentation-evaluation.md +42 -42
- package/prompts/knowledge/eng-dispatch-ops.md +34 -0
- package/prompts/knowledge/eng-ledger-ops.md +26 -0
- package/prompts/knowledge/evidence-discipline.md +10 -32
- package/prompts/knowledge/normal-batch-discipline.md +13 -0
- package/prompts/knowledge/normal-coding-workflow.md +97 -99
- package/prompts/knowledge/normal-delivery-collaboration.md +107 -112
- package/prompts/knowledge/normal-work-management.md +44 -44
- package/prompts/knowledge/product-security.md +7 -26
- package/prompts/knowledge/project-records.md +69 -64
- package/prompts/knowledge/tool-advisor.md +17 -17
- package/prompts/knowledge/tool-apply-patch.md +14 -14
- package/prompts/knowledge/tool-bash.md +32 -31
- package/prompts/knowledge/tool-batch-eng-coder.md +19 -13
- package/prompts/knowledge/tool-batch-eng-designer.md +21 -14
- package/prompts/knowledge/tool-batch-review.md +18 -12
- package/prompts/knowledge/tool-batch.md +93 -85
- package/prompts/knowledge/tool-browser.md +59 -59
- package/prompts/knowledge/tool-code-search.md +2 -2
- package/prompts/knowledge/tool-consult-start.md +6 -6
- package/prompts/knowledge/tool-consult-stop.md +3 -3
- package/prompts/knowledge/tool-context.md +4 -4
- package/prompts/knowledge/tool-delete.md +16 -16
- package/prompts/knowledge/tool-doc-check.md +39 -39
- package/prompts/knowledge/tool-doc-graph-read.md +11 -11
- package/prompts/knowledge/tool-doc-graph.md +48 -48
- package/prompts/knowledge/tool-doc-search.md +3 -3
- package/prompts/knowledge/tool-edit.md +30 -30
- package/prompts/knowledge/tool-eng.md +1 -1
- package/prompts/knowledge/tool-execute.md +16 -16
- package/prompts/knowledge/tool-fetch.md +15 -15
- package/prompts/knowledge/tool-file-ops.md +15 -15
- package/prompts/knowledge/tool-get-current-time.md +3 -3
- package/prompts/knowledge/tool-git-read.md +15 -15
- package/prompts/knowledge/tool-git.md +26 -26
- package/prompts/knowledge/tool-glob.md +14 -14
- package/prompts/knowledge/tool-goal.md +5 -5
- package/prompts/knowledge/tool-grep.md +19 -18
- package/prompts/knowledge/tool-hashline-edit.md +13 -13
- package/prompts/knowledge/tool-insert-after.md +14 -14
- package/prompts/knowledge/tool-instance-list.md +3 -3
- package/prompts/knowledge/tool-instance-send.md +5 -5
- package/prompts/knowledge/tool-knowledge.md +12 -12
- package/prompts/knowledge/tool-ledger-read.md +20 -20
- package/prompts/knowledge/tool-ledger.md +44 -44
- package/prompts/knowledge/tool-lint.md +10 -10
- package/prompts/knowledge/tool-ls.md +11 -11
- package/prompts/knowledge/tool-lsp.md +8 -8
- package/prompts/knowledge/tool-main-history.md +1 -1
- package/prompts/knowledge/tool-manifest-read.md +7 -7
- package/prompts/knowledge/tool-manifest.md +43 -32
- package/prompts/knowledge/tool-memory-read.md +12 -12
- package/prompts/knowledge/tool-memory.md +14 -14
- package/prompts/knowledge/tool-notify-parent.md +10 -10
- package/prompts/knowledge/tool-operations.md +88 -88
- package/prompts/knowledge/tool-peek.md +10 -0
- package/prompts/knowledge/tool-peer-instances.md +1 -1
- package/prompts/knowledge/tool-plan.md +6 -3
- package/prompts/knowledge/tool-process-read.md +8 -8
- package/prompts/knowledge/tool-process.md +11 -11
- package/prompts/knowledge/tool-question.md +11 -13
- package/prompts/knowledge/tool-read-history-batch.md +13 -13
- package/prompts/knowledge/tool-read-history.md +19 -19
- package/prompts/knowledge/tool-read-image.md +8 -8
- package/prompts/knowledge/tool-read.md +17 -17
- package/prompts/knowledge/tool-recent-changes.md +4 -4
- package/prompts/knowledge/tool-repo-outline.md +2 -2
- package/prompts/knowledge/tool-settings-read.md +5 -7
- package/prompts/knowledge/tool-settings.md +10 -10
- package/prompts/knowledge/tool-skill.md +4 -4
- package/prompts/knowledge/tool-ssh.md +66 -66
- package/prompts/knowledge/tool-step.md +26 -0
- package/prompts/knowledge/tool-subagent.md +52 -50
- package/prompts/knowledge/tool-task.md +14 -11
- package/prompts/knowledge/tool-team-ledger-read.md +7 -7
- package/prompts/knowledge/tool-team-ledger.md +13 -13
- package/prompts/knowledge/tool-timer.md +14 -14
- package/prompts/knowledge/tool-tree.md +11 -11
- package/prompts/knowledge/tool-verify.md +11 -11
- package/prompts/knowledge/tool-wait-for.md +20 -20
- package/prompts/knowledge/tool-websearch.md +16 -14
- package/prompts/knowledge/tool-write.md +10 -10
- package/prompts/knowledge/upstream-protocol.md +37 -38
- package/prompts/persona-coder.md +22 -22
- package/prompts/persona-eng-coder.md +87 -81
- package/prompts/persona-eng-designer.md +173 -153
- package/prompts/persona-engineering.md +53 -554
- package/prompts/persona-explore.md +18 -15
- package/prompts/persona-normal.md +62 -63
- package/prompts/persona-plan.md +36 -32
- package/prompts/persona-tester.md +39 -39
- package/prompts/planning-next-moves.md +9 -0
- package/prompts/subagent-base.md +4 -16
- package/provider/errors.mjs +0 -24
- package/provider/list-models.mjs +20 -2
- package/session-lifecycle.mjs +4 -3
- package/tool-args.mjs +25 -0
- package/tool-docs/peek.md +3 -0
- package/tool-docs/plan.md +1 -1
- package/tool-docs/question.md +1 -1
- package/tool-docs/step.md +3 -0
- package/tools/edit-diff.mjs +4 -4
- package/tools/index.mjs +3 -2
- package/tools/peek.mjs +447 -0
- package/tools/question.mjs +87 -9
- package/tools/ssh.mjs +1 -1
- package/window-policy.mjs +107 -80
package/window-policy.mjs
CHANGED
|
@@ -10,13 +10,14 @@
|
|
|
10
10
|
* 为什么要单出一层:策略值只在读数上现算,**不落在 provider ∥ config 对象上**——派生字段会被落盘 ∥
|
|
11
11
|
* 跨会话带走,改了配置要等下一次装配才生效,而它必须在下一处读数上就生效。
|
|
12
12
|
*
|
|
13
|
-
* 派生链(§2.2):`window` 是唯一输入。`
|
|
14
|
-
* `
|
|
15
|
-
* `
|
|
16
|
-
* `limit` 对窗口的斜率恒为 1
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* `budgetOf
|
|
13
|
+
* 派生链(§2.2):`window` 是唯一输入。`limit = window − reserve`;
|
|
14
|
+
* `threshold = max(COMPACT_FLOOR_TOKENS, floor(COMPACT_RATIO × window))`(`compactLineOf`,两处共用);
|
|
15
|
+
* `reserve` 封在 `window − threshold` 以下(见 `budgetOf`)⇒ `limit ≥ threshold` 由构造保证。
|
|
16
|
+
* 压缩线不从 `limit` 反推、不取 min ⇒ 派生单调;比例臂主导的段上 `limit` 对窗口的斜率恒为 1。
|
|
17
|
+
* 系统不另设被百分比约束的固定上限——要更紧的线由用户配 `agent.compactThreshold` 的显式正数,
|
|
18
|
+
* 该值直接生效、不参与派生。
|
|
19
|
+
* `budgetOf` 不再产出「矛盾预算」,`contradiction` 座位已退役(§2.4 随之取消)。
|
|
20
|
+
* 已知边界:窗口 ≤ `COMPACT_FLOOR_TOKENS` 时压缩线落在窗口之外,自动压缩不成立,只能靠守卫报错。
|
|
20
21
|
*
|
|
21
22
|
* 档位不是入参(§2.1 ∥ §2.3):tiny ∥ normal ∥ engineering 三档的有效窗口同值,档位只影响提示词、工具面与
|
|
22
23
|
* 注入开关。一个不影响任何输出的入参比一条缺省值更坏——它让每个调用点以为档位还在参与预算。
|
|
@@ -25,26 +26,30 @@
|
|
|
25
26
|
* `config.mjs`,只被它转口)。
|
|
26
27
|
*/
|
|
27
28
|
import { providerSpec, providerSpecResolution } from "./model-specs.mjs"
|
|
29
|
+
import { listedOutputTokens, warnContextOnce } from "./model-context.mjs"
|
|
30
|
+
|
|
31
|
+
/** 压缩线占窗口的比例(`CONTEXT-COMPACTION.md` §2.2):`threshold = max(COMPACT_FLOOR_TOKENS, floor(COMPACT_RATIO × window))`。
|
|
32
|
+
* 它是压缩线本身,不是 `min()` 的一支——`limit` 只由派生链同行供出,不回头收缩这条线。 */
|
|
28
33
|
|
|
29
|
-
/** 压缩线占窗口的比例(`CONTEXT-COMPACTION.md` §2.2):窗口用到六成即触发压缩。用户裁定的比率,
|
|
30
|
-
* 也是比率臂里唯一的乘子——压缩线没有第二个常量。数值在本档只出现这一处。 */
|
|
31
34
|
export const COMPACT_RATIO = 0.6
|
|
32
35
|
|
|
33
|
-
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
36
|
+
/** 压缩线的绝对下限(`CONTEXT-COMPACTION.md` §2.2):小窗口上 `0.6 × window` 用不完可用的输入,于是按这个数压。
|
|
37
|
+
* 128,000 窗口上 `0.6 × 128,000 = 76,800`,而 `limit = 96,000`——比例臂先到,用户裁定改用满输入再压,
|
|
38
|
+
* 于是取两者较大者。这条线**可以等于 `limit`**(128K 就是这样):那时注入没留余量,注入一来就顶线,
|
|
39
|
+
* 守卫与压缩在同一处接手,代价是每轮都可能触发一次压缩。大窗口上比例臂远大于这个数,由比例接管。 */
|
|
40
|
+
export const COMPACT_FLOOR_TOKENS = 96000
|
|
41
|
+
|
|
42
|
+
/** 注入余量的绝对封顶(`CONTEXT-COMPACTION.md` §2.2)· `existing but unused:` 压缩线改为 `max(96,000, floor(0.6 × window))`
|
|
43
|
+
* 之后,注入余量不再进预算算式——余量现在由压缩线的位置隐含(`limit − threshold`),不是一个可调常量。
|
|
44
|
+
* 两个常量只为 `config.mjs` 的转口面保留,不被任何算式读取;调它们看不到任何效果。 */
|
|
38
45
|
export const INJECT_HEADROOM_MAX_TOKENS = 50000
|
|
39
46
|
|
|
40
|
-
/** 注入余量占输入上限的比例(`CONTEXT-COMPACTION.md` §2.2
|
|
41
|
-
* 小窗口正是每一千 token 都要用在请求上的那一档,按大窗口的绝对量付余量最经不起;大窗口上比例臂远大于
|
|
42
|
-
* 50,000,于是由封顶接管。 */
|
|
47
|
+
/** 注入余量占输入上限的比例(`CONTEXT-COMPACTION.md` §2.2)· `existing but unused:` 见上一个常量的注记。 */
|
|
43
48
|
export const INJECT_HEADROOM_RATIO = 0.25
|
|
44
49
|
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* 取 1
|
|
50
|
+
/** `reserve` 封顶的正数地板(`CONTEXT-COMPACTION.md` §2.2):封顶 = `max(BUDGET_FLOOR_TOKENS, window − threshold)`。
|
|
51
|
+
* 窗口 ≤ `COMPACT_FLOOR_TOKENS` 时 `window − threshold` 落到非正数,这个 `max` 把它抬回 1——那是唯一用到它的路径。
|
|
52
|
+
* 取 1 不取更大的数:封顶取负会让 `limit > window`,而取 1 至少保住一个合法的预算对象。 */
|
|
48
53
|
export const BUDGET_FLOOR_TOKENS = 1
|
|
49
54
|
|
|
50
55
|
/**
|
|
@@ -87,23 +92,44 @@ const DEFAULT_OUTPUT_PLAIN = 8_192
|
|
|
87
92
|
const DEFAULT_OUTPUT_THINKING = 65_536
|
|
88
93
|
const DEFAULT_OUTPUT_MAX_EFFORT = 131_072
|
|
89
94
|
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
95
|
+
/** Output reservation counted against the window (`CONTEXT-COMPACTION.md` §2.2.1): what THIS request
|
|
96
|
+
* actually takes out of the window for output. Priority is a table, fixed:
|
|
97
|
+
* 1. explicit `provider.maxTokens` ⇒ that value verbatim (the transport sends it as max_tokens).
|
|
98
|
+
* 2. provider's own listed output ceiling (`max_output_tokens` ∥ `max_completion_tokens` via
|
|
99
|
+
* `listedOutputTokens`) ⇒ that value verbatim. The endpoint is the only one that knows what it will
|
|
100
|
+
* accept; a static table only knows the model family's spec ceiling.
|
|
101
|
+
* 3. `format === "anthropic"` ⇒ `spec.maxOutput` (that face always sends max_tokens ⇒ spec 档).
|
|
102
|
+
* 4. absent 档(未显式给 `provider.maxTokens`,端点也未报)⇒ `min(spec.maxOutput, 服务端默认输出)`.
|
|
103
|
+
* 三档的返回值都被 `budgetOf` 按 `window − threshold` 再封一次顶(§2.2):不封顶时
|
|
104
|
+
* `limit < threshold`,压缩线永远够不着。封顶后计数与实发同走封顶值——一处压一处不压,压缩线就按
|
|
105
|
+
* 一条不存在的窗口算(教训见 2026-10-18 overflow 400:`budget counted 250_000, 393_216 actually sent`)。
|
|
99
106
|
* 分支只有这一处:`budgetOf` 照本函数的返回值取数,不另判一次档。
|
|
100
107
|
*/
|
|
108
|
+
|
|
109
|
+
/** `provider.maxTokens` 的单一归一(有限正数才算显式):`outputReserve` 与 `budgetOf` 共用,
|
|
110
|
+
* 两处各写一种取值判,explicit 档的 reserve 就会与实际发出的 maxTokens 分成两个数。 */
|
|
111
|
+
function explicitMaxTokensOf(provider) {
|
|
112
|
+
const raw = Number(provider?.maxTokens)
|
|
113
|
+
return Number.isFinite(raw) && raw > 0 ? raw : undefined
|
|
114
|
+
}
|
|
115
|
+
|
|
101
116
|
export function outputReserve(provider, spec) {
|
|
102
|
-
|
|
103
|
-
// 就会与实际发出的 maxTokens 分成两个数。
|
|
104
|
-
const rawExplicit = Number(provider?.maxTokens)
|
|
105
|
-
const explicit = Number.isFinite(rawExplicit) && rawExplicit > 0 ? rawExplicit : undefined
|
|
117
|
+
const explicit = explicitMaxTokensOf(provider)
|
|
106
118
|
if (explicit !== undefined) return explicit
|
|
119
|
+
// provider 自报的输出上限:端自己是唯一知道它吃什么的那一方,排在静态表档之前。
|
|
120
|
+
// 异常大值照用 + 告警一次,不截断——与 `model-specs.mjs` 的 `listed-over-4x` 同口径(截断 = 静默改写真事实)。
|
|
121
|
+
const reported = listedOutputTokens(provider)
|
|
122
|
+
if (reported !== null) {
|
|
123
|
+
const staticValue = spec?.maxOutput ?? 0
|
|
124
|
+
if (staticValue > 0 && reported > staticValue * 4) {
|
|
125
|
+
const endpoint = String(provider?.baseURL ?? "").replace(/\/+$/, "")
|
|
126
|
+
warnContextOnce(
|
|
127
|
+
`listed-output-over-4x\0${endpoint}\0${provider?.model ?? ""}\0${reported}`,
|
|
128
|
+
`[config] model "${provider?.model ?? ""}" on ${endpoint} reports an output ceiling of ${reported}, over 4x the spec value ${staticValue} — using the reported value as-is (not truncated)`
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
return reported
|
|
132
|
+
}
|
|
107
133
|
const maxOutput = spec?.maxOutput ?? 0
|
|
108
134
|
if (provider?.format === "anthropic") return maxOutput
|
|
109
135
|
const defaultOutput = provider?.reasoningEffort === "max"
|
|
@@ -113,16 +139,15 @@ export function outputReserve(provider, spec) {
|
|
|
113
139
|
}
|
|
114
140
|
|
|
115
141
|
/** 派生链的减法一格(§2.2 的 `limit`):`usableWindow` 判它与 `budgetOf` 报差值读同一个量——
|
|
116
|
-
*
|
|
142
|
+
* 两处各写一次减法,报文与判定就会报出两个差数。 */
|
|
117
143
|
function usableDelta(context, reserve) {
|
|
118
144
|
return (context ?? 0) - reserve
|
|
119
145
|
}
|
|
120
146
|
|
|
121
147
|
/**
|
|
122
|
-
* Usable input window = window − output reservation (§2.2 的 `limit`)
|
|
123
|
-
* `
|
|
124
|
-
*
|
|
125
|
-
* (按 §2.4 响一声、夹住、放行),装载期因此不再终止进程。
|
|
148
|
+
* Usable input window = window − output reservation (§2.2 的 `limit`)。`reserve` 在 `budgetOf` 里被封在
|
|
149
|
+
* `window − threshold` 以下,而窗口取自模型规格或渠道覆盖(恒 ≥ 2),`usable ≤ 0` 因此不可达。
|
|
150
|
+
* 判据留着作断言:真到这里就说明封顶被绕过了,那时抛出比静默兜底便宜。
|
|
126
151
|
*/
|
|
127
152
|
export function usableWindow(context, reserve) {
|
|
128
153
|
const ctx = context ?? 0
|
|
@@ -143,57 +168,59 @@ export function usableWindow(context, reserve) {
|
|
|
143
168
|
* - window / windowSource: the input window this budget was computed on, plus its source name
|
|
144
169
|
* (closed set { modelContext | provider-override | list | spec | default }, passed through
|
|
145
170
|
* verbatim — no class merging).
|
|
146
|
-
* - maxTokens / maxTokensSource: the value THIS transport writes into the request body
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
171
|
+
* - maxTokens / maxTokensSource: the value THIS transport writes into the request body, and where it came
|
|
172
|
+
* from — closed set { explicit | reported | spec | absent }:
|
|
173
|
+
* explicit = `provider.maxTokens` (最高)
|
|
174
|
+
* reported = the endpoint's own listed ceiling (`max_output_tokens` ∥ `max_completion_tokens`)
|
|
175
|
+
* spec = anthropic face, which always sends max_tokens
|
|
176
|
+
* absent = `min(spec.maxOutput, server default output)`
|
|
177
|
+
* All four faces send it now, then withheld at the reserve ceiling. Never a guard criterion; the guard
|
|
178
|
+
* reads limit instead.
|
|
150
179
|
* - reserve: what the request actually takes out of the window for output (see `outputReserve`).
|
|
151
180
|
* - limit: window − reserve — the input ceiling the server accepts (prompt + reserved output ≤ window).
|
|
152
|
-
* - threshold:
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
|
|
157
|
-
*
|
|
158
|
-
* `
|
|
181
|
+
* - threshold: max(COMPACT_FLOOR_TOKENS, floor(COMPACT_RATIO × window)) — the compaction trigger line,
|
|
182
|
+
* computed from window alone. It is the line itself, not a min() branch. Because `budgetOf`
|
|
183
|
+
* clamps `reserve` at `window − threshold`, `limit ≥ threshold` holds and the guard line (limit)
|
|
184
|
+
* and the compaction line (threshold) agree by construction — a fitting request is never refused first.
|
|
185
|
+
|
|
186
|
+
* `reserve` 已被 `budgetOf` 封在 `window − threshold` 以下,`usableWindow` 的抛在这里不可达;
|
|
187
|
+
* 派生链因此只产出正数 `limit` 与 `threshold`,不再有「矛盾预算」这一格(§2.4 已取消)。
|
|
188
|
+
* 已知边界:窗口 ≤ `COMPACT_FLOOR_TOKENS` 时封顶落到 `BUDGET_FLOOR_TOKENS`,`limit < threshold`,
|
|
189
|
+
* 自动压缩不成立——那不是矛盾态,而是压缩线在窗口之外,只能靠 `enforceContextFit` 报错。
|
|
159
190
|
*/
|
|
160
191
|
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
192
|
+
/** 压缩线(§2.2 的唯一算式):比例臂与绝对下限取较大者。
|
|
193
|
+
* 两处共用它——`deriveLines` 出 `threshold`,`budgetOf` 出 `reserve` 的封顶;抄第二份就是同一条规则的两个实现。 */
|
|
194
|
+
function compactLineOf(window) {
|
|
195
|
+
return Math.max(COMPACT_FLOOR_TOKENS, Math.floor(COMPACT_RATIO * window))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** 派生链的三行(§2.2):`limit` 与压缩线。`reserve` 由 `budgetOf` 封顶后,`usableWindow` 的抛不可达。 */
|
|
163
199
|
function deriveLines(window, reserve) {
|
|
164
|
-
|
|
165
|
-
|
|
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))
|
|
200
|
+
const limit = usableWindow(window, reserve)
|
|
201
|
+
const threshold = compactLineOf(window)
|
|
177
202
|
return { limit, threshold }
|
|
178
203
|
}
|
|
179
204
|
|
|
180
205
|
function budgetOf(provider, spec, window, windowSource) {
|
|
181
|
-
const
|
|
182
|
-
const explicit = Number.isFinite(explicitRaw) && explicitRaw > 0 ? explicitRaw : undefined
|
|
206
|
+
const explicit = explicitMaxTokensOf(provider)
|
|
183
207
|
const anthropicAlwaysSends = provider?.format === "anthropic"
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const
|
|
208
|
+
// 来源名是闭集四值之一 { explicit | reported | spec | absent }(`windowSource` 同一个来源名体系):端自报命中时报 "reported"——
|
|
209
|
+
// 消费面(`agent-tools/context.mjs` 的状态行)读的是这个字符串,报错了读到的人会去查「为什么 absent 档会有 131,072」。
|
|
210
|
+
const reportedOutput = listedOutputTokens(provider)
|
|
211
|
+
const maxTokensSource = explicit != null ? "explicit" : reportedOutput !== null ? "reported" : anthropicAlwaysSends ? "spec" : "absent"
|
|
212
|
+
// 输出预留封顶(§2.2 派生线的可满足条件):`limit = window − reserve`。若 `reserve > window − threshold`,
|
|
213
|
+
// 则 `limit < threshold`,守卫 ②(`fixedFloor > limit`)会在压缩线之前开火——那条线永远够不着。
|
|
214
|
+
// 封顶不是策略选择,是「到这个数必须压」可满足的唯一算术条件。
|
|
215
|
+
// 128,000 窗:`window − max(96,000, 76,800) = 32,000`,正好等于该档 `maxOutput` ⇒ 不触发封顶,`limit = threshold`。
|
|
216
|
+
const reserveCeiling = Math.max(BUDGET_FLOOR_TOKENS, window - compactLineOf(window))
|
|
217
|
+
// 计数与实发是同一个数(§2.2)。absent 档也发出封顶后的预留:一个字节都不发时服务端按它自己的默认
|
|
218
|
+
// 预留(通常高于四成窗口),而我们的 `limit` 与压缩线仍按封顶后的值算 ⇒ 压缩线落在服务端不接受的位置。
|
|
219
|
+
const reserve = Math.min(outputReserve(provider, spec), reserveCeiling)
|
|
220
|
+
const maxTokens = reserve
|
|
221
|
+
|
|
222
|
+
const lines = deriveLines(window, reserve)
|
|
223
|
+
const budget = { window, windowSource, maxTokens, maxTokensSource, reserve, limit: lines.limit, threshold: lines.threshold }
|
|
197
224
|
return budget
|
|
198
225
|
}
|
|
199
226
|
|
|
@@ -229,7 +256,7 @@ export function policyBudget(provider, spec) {
|
|
|
229
256
|
/** Derive compaction threshold; `explicit` 是配置文件里的原始值(可缺省、可为 0)。
|
|
230
257
|
* 「显式」的判据 = **有限正数**(CONTEXT-COMPACTION.md §2.2):过 `nonNegativeIntValue` 之后
|
|
231
258
|
* `v !== null && v > 0` ⇒ 该值覆盖派生线(`auto: false`);0 ∥ 空值 ∥ 非法值(手改落盘的负数、小数、
|
|
232
|
-
* 非数字)一律走 auto 支,`value = policyBudget().threshold`(§2.2
|
|
259
|
+
* 非数字)一律走 auto 支,`value = policyBudget().threshold`(§2.2 的两臂取大,over the EFFECTIVE window,
|
|
233
260
|
* §2.1)。0 与缺省同义,都表示「不覆盖派生线」。
|
|
234
261
|
* 装载期(`config.mjs`)与三端输入面都调本函数/本谓词,不另写一份同形判断。
|
|
235
262
|
* Second param accepts EITHER a model name string (pure spec lookup —
|