@reinsjs/brain 0.3.0 → 0.4.0
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 +12 -0
- package/dist/index.d.ts +14 -14
- package/dist/index.js +3 -12
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @reinsjs/brain
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 6b390d0: perception: the default render no longer reports session tokens used or unfolded turns (both stay in `meta.reading` and are available to a custom `render`). Session tokens counted cache reads, so a fresh session crossed several tiers within a few steps and each crossing appended another note the model could not act on. The compactions line keeps its "what was folded" note. compact: the default rules no longer name the perception note's title, so they hold when the host renders its own status notes or does not install perception.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- fb032c1: core: the projection's threshold fallback now compares the calibrated context size — the estimate plus the fixed overhead the estimate cannot see (system prompt, tool definitions, thinking), measured from the last `budget_usage` — against `contextLimit - reserveTokens`. Before, it compared the raw estimate, so perception could report "≥85%, the harness auto-folds at 85%" while no fold happened, and a system prompt plus tool table larger than the reserve could overflow the window before the fallback ever fired. `contextOverheadOf` moves to core (brain still re-exports it) and returns 0 when the last usage record yields a non-finite value, so a malformed `budget_usage` cannot fail the run at start; `ProjectionStats.overheadTokens` is new; `project({ budget: { overheadTokens: 0 } })` restores the old comparison. The fallback now fires earlier by that overhead, and `reserveTokens` is effectively the output reserve.
|
|
12
|
+
- Updated dependencies [fb032c1]
|
|
13
|
+
- @reinsjs/core@0.4.0
|
|
14
|
+
|
|
3
15
|
## 0.3.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Tool, TurnContext, MaybePromise, Socket, Event, CompactionPayload, FoldedToolResult, ToolContext, ContentPart, EventDraft, ToolReferencePart, MemoryStore, CoreEventOf, SkillSource } from '@reinsjs/core';
|
|
2
|
+
export { contextOverheadOf } from '@reinsjs/core';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* approval —— 审批与权限模块(技术方案 §9.7,B7)。
|
|
@@ -408,7 +409,7 @@ declare const COMPACT_TOOL_NAME = "compact";
|
|
|
408
409
|
declare const RECALL_TOOL_NAME = "recall";
|
|
409
410
|
declare const COMPACT_TOOL_DESCRIPTION: string;
|
|
410
411
|
declare const RECALL_TOOL_DESCRIPTION: string;
|
|
411
|
-
declare const COMPACT_RULES = "## Managing your context window\nYou have a `compact` tool that folds earlier conversation into a summary you write.
|
|
412
|
+
declare const COMPACT_RULES = "## Managing your context window\nYou have a `compact` tool that folds earlier conversation into a summary you write. Status notes from the harness, if present, tell you how full the window is.\n- Compact when a sub-task is finished or the trajectory has converged and the folded details are no longer needed verbatim. Do it at a natural pause, not while acting on something.\n- Do not compact in the middle of a derivation, while a problem is still unresolved, or when you feel stuck: you would lose exactly the details you still need.\n- The summary replaces everything it folds, including earlier summaries. Carry forward the user's goals and constraints, decisions made and why, the current state, and what comes next. List each fact that must survive in `keep`. The most recent user message and pinned notes survive verbatim on their own; check the pins still say what you need.\n- Keep exact values, not just conclusions: identifiers, status and enum fields you verified, numbers, dates, error codes. \"Checked, all ENABLE\" loses the field values themselves.\n- Nothing folded is lost for good: under each summary the harness lists the folded tool results as `seq N tool(arguments)`, and `recall({ seq })` brings one back verbatim. When asked about a detail your summary did not keep, recall the result that contained it instead of guessing or saying it is gone.\n- If you never compact, the harness folds the oldest turns mechanically once the window nears its limit. Its summary is worse than yours, but it lists the folded tool results the same way.";
|
|
412
413
|
|
|
413
414
|
/**
|
|
414
415
|
* handoff —— 会话交接模块(技术方案 §9.5,B5)。
|
|
@@ -913,17 +914,12 @@ interface ReadingThresholds {
|
|
|
913
914
|
declare function countModelTurns(events: readonly Event[]): number;
|
|
914
915
|
/** 累加 budget_usage:输入、缓存读、缓存写、输出全算,反映的是"花了多少",不是"上下文多大" */
|
|
915
916
|
declare function sumSessionTokens(timeline: readonly Event[]): number;
|
|
916
|
-
|
|
917
|
-
* 估算与真实之间的固定开销(B8,关闭 §17 的 B2 实测发现)。
|
|
918
|
-
*
|
|
919
|
-
* 投影只数模型可见事件的正文,系统提示、工具表、thinking 签名块等都不在内,真实 input 可高出一倍。
|
|
920
|
-
* 这些多出来的部分在一个 run 里基本是常量(系统提示与工具表整轮不变),所以用**加法**校准而不是比例:
|
|
921
|
-
* 比例会随历史变长把误差放大(100k 时翻倍成 200k),加法只补那块固定的开销。
|
|
922
|
-
* 数据来自日志最后一条同时带 tokens 与 contextEstimate 的 budget_usage:真实上下文 = input + cacheRead + cacheWrite。
|
|
923
|
-
*/
|
|
924
|
-
declare function contextOverheadOf(timeline: readonly Event[]): number;
|
|
917
|
+
|
|
925
918
|
interface ReadingOptions {
|
|
926
|
-
/**
|
|
919
|
+
/**
|
|
920
|
+
* 用上一请求的真实用量校准上下文估算。缺省 true,与 core 阈值兜底同一口径;
|
|
921
|
+
* 关掉则只按投影估算(偏低),此时读数会比兜底实际触发点低一截
|
|
922
|
+
*/
|
|
927
923
|
calibrate?: boolean;
|
|
928
924
|
}
|
|
929
925
|
declare function readPerception(ctx: TurnContext, thresholds: ReadingThresholds, limits?: PerceptionLimits, opts?: ReadingOptions): PerceptionReading;
|
|
@@ -948,9 +944,9 @@ declare function readPerception(ctx: TurnContext, thresholds: ReadingThresholds,
|
|
|
948
944
|
interface PerceptionOptions {
|
|
949
945
|
/** 上下文使用率档位边界(占窗口比例)。缺省 [0.5, 0.7, 0.85] → <50% / 50%–70% / 70%–85% / ≥85% */
|
|
950
946
|
usageTiers?: readonly number[];
|
|
951
|
-
/** 未折叠模型轮数档位边界。缺省 [5, 15, 40] → ≤5 / 6–15 / 16–40 / >40 */
|
|
947
|
+
/** 未折叠模型轮数档位边界。缺省 [5, 15, 40] → ≤5 / 6–15 / 16–40 / >40。只进读数,缺省 render 不报 */
|
|
952
948
|
turnTiers?: readonly number[];
|
|
953
|
-
/** 会话累计 token 档位边界。缺省 [1e4, 5e4, 2e5, 1e6] */
|
|
949
|
+
/** 会话累计 token 档位边界。缺省 [1e4, 5e4, 2e5, 1e6]。只进读数,缺省 render 不报 */
|
|
954
950
|
tokenTiers?: readonly number[];
|
|
955
951
|
/** 外溢结果条数档位边界(0 单独一档)。缺省 [3, 10] → 0 / 1–3 / 4–10 / >10 */
|
|
956
952
|
spillTiers?: readonly number[];
|
|
@@ -974,6 +970,10 @@ declare function perception(opts?: PerceptionOptions): Socket;
|
|
|
974
970
|
* 读数 → 给模型看的文字。只陈述事实,不下指令:什么时候整理、要不要交接是模型的判断(宪法一),
|
|
975
971
|
* 规则提示属于 compact 模块(B2)且放在稳定的系统提示里,不在这条每次变档才出现的说明里。
|
|
976
972
|
*
|
|
973
|
+
* 只报对模型做决定有用的量。"会话累计 token"与"未折叠轮数"留在读数里(meta 与自定义 render 可用),缺省文字不报
|
|
974
|
+
* (2026-10-30 OnSet 反馈):累计 token 含缓存读,上下文一大每步都加一整份,新会话几步就连跨几档;轮数档位开头也连跳。
|
|
975
|
+
* 每跨一档文字就变、就多一条说明,而模型拿这两个数做不了任何判断 —— 上下文多满看使用率,花了多少看预算余量。
|
|
976
|
+
*
|
|
977
977
|
* 用英文写:这段话进的是模型上下文而不是给人看的日志,英文 token 更省,且各家模型都熟。
|
|
978
978
|
* 宿主要换措辞或语言,给 perception() 传自己的 render。
|
|
979
979
|
*/
|
|
@@ -1409,4 +1409,4 @@ declare function parseFetchBlobArgs(raw: unknown): FetchBlobArgs;
|
|
|
1409
1409
|
declare function isTextMime(mime: string): boolean;
|
|
1410
1410
|
declare function spill(opts?: SpillOptions): Socket;
|
|
1411
1411
|
|
|
1412
|
-
export { APPROVAL_EXPIRY_RULE, APPROVAL_POLICY_IDS, APPROVAL_RULES, APPROVAL_SOCKET_NAME, type ApprovalOptions, BUDGET_SOCKET_NAME, type BudgetDimension, type BudgetHit, type BudgetLimits, type BudgetOptions, COMPACT_INPUT_SCHEMA, COMPACT_RULES, COMPACT_SOCKET_NAME, COMPACT_TOOL_DESCRIPTION, COMPACT_TOOL_NAME, type CompactArgs, type CompactOptions, type CompactPlan, DEFAULT_HANDOFF_REASON, DEFAULT_LAZY_SUMMARY_CHARS, DEFAULT_MAX_CONSECUTIVE_COMPACTIONS, DEFAULT_MAX_FILE_BYTES, DEFAULT_MAX_PIN_TEXT_LENGTH, DEFAULT_MAX_RESULT_TOKENS, DEFAULT_MAX_SUMMARY_CHARS, DEFAULT_MAX_VIEW_CHARS, DEFAULT_PERCEPTION_THRESHOLDS, DEFAULT_PIN_OVERSHOOT_TOLERANCE, DEFAULT_PREVIEW_CHARS, DEFAULT_PREVIEW_LINES, DEFAULT_SKILLS_ROOT, DEFAULT_SKILL_READ_CHARS, DEFAULT_TOOL_FIND_MAX, type ExpiredApproval, FETCH_BLOB_INPUT_SCHEMA, FETCH_BLOB_TOOL_DESCRIPTION, FETCH_BLOB_TOOL_NAME, type FetchBlobArgs, HANDOFF_INPUT_SCHEMA, HANDOFF_RULES, HANDOFF_SOCKET_NAME, HANDOFF_TOOL_DESCRIPTION, HANDOFF_TOOL_NAME, type HandoffArgs, type HandoffOptions, type InlineSkill, type InlineSkillsOptions, LAZY_TOOLS_SOCKET_NAME, LAZY_TOOL_RULES, type LazyToolMenu, type LazyToolMenuEntry, type LazyToolsOptions, type LimitDimension, type LoadedSkill, MANIFEST_DEFAULT_EXCLUDE, MANIFEST_HEADING, MAX_MEMORY_PATH_LENGTH, MAX_SKILL_DESCRIPTION_CHARS, MEMORY_COMMANDS, MEMORY_INPUT_SCHEMA, MEMORY_ROOT, MEMORY_RULES, MEMORY_SOCKET_NAME, MEMORY_TOOL_DESCRIPTION, MEMORY_TOOL_NAME, type ManifestOptions, type MemoryCommand, type MemoryFs, type MemoryLimits, type MemoryOp, type MemoryOptions, type MemoryOutcome, PERCEPTION_SOCKET_NAME, PINS_SOCKET_NAME, PIN_INPUT_SCHEMA, PIN_RULES, PIN_TOOL_DESCRIPTION, PIN_TOOL_NAME, type ParsedSkill, type PerceptionLimits, type PerceptionOptions, type PerceptionReading, type PinArgs, type PinMeta, type PinNote, type PinSpec, type PinsOptions, type PlanOptions, type PolicyCall, type PolicyOutcome, type PolicyRule, type PolicyRuleSpec, type PolicyVerdict, type Preview, type PreviewOptions, RECALL_INPUT_SCHEMA, RECALL_TOOL_DESCRIPTION, RECALL_TOOL_NAME, type ReadingOptions, type ReadingThresholds, type RecallArgs, SKILLS_SOCKET_NAME, SKILL_FILE_NAME, SKILL_NAME_RE, SKILL_READ_INPUT_SCHEMA, SKILL_READ_TOOL_DESCRIPTION, SKILL_READ_TOOL_NAME, SKILL_RULES, SPILL_BLOB_MIME, SPILL_RULES, SPILL_SOCKET_NAME, type SkillFrontmatter, type SkillMenu, type SkillMenuEntry, type SkillReadInput, type SkillsOptions, type SpillOptions, TOOL_FIND_INPUT_SCHEMA, TOOL_FIND_TOOL_DESCRIPTION, TOOL_FIND_TOOL_NAME, type TextMeasure, type TextTokenEstimator, type Tier, type ToolFindInput, approval, assertAscending, assertLimits, assertSkillsRoot, bindMemoryFs, budget, budgetUsedOf, byteLength, carriedPins, checkBudget, clipEndByTokens, compact, compactNumber, composeHandoffNote,
|
|
1412
|
+
export { APPROVAL_EXPIRY_RULE, APPROVAL_POLICY_IDS, APPROVAL_RULES, APPROVAL_SOCKET_NAME, type ApprovalOptions, BUDGET_SOCKET_NAME, type BudgetDimension, type BudgetHit, type BudgetLimits, type BudgetOptions, COMPACT_INPUT_SCHEMA, COMPACT_RULES, COMPACT_SOCKET_NAME, COMPACT_TOOL_DESCRIPTION, COMPACT_TOOL_NAME, type CompactArgs, type CompactOptions, type CompactPlan, DEFAULT_HANDOFF_REASON, DEFAULT_LAZY_SUMMARY_CHARS, DEFAULT_MAX_CONSECUTIVE_COMPACTIONS, DEFAULT_MAX_FILE_BYTES, DEFAULT_MAX_PIN_TEXT_LENGTH, DEFAULT_MAX_RESULT_TOKENS, DEFAULT_MAX_SUMMARY_CHARS, DEFAULT_MAX_VIEW_CHARS, DEFAULT_PERCEPTION_THRESHOLDS, DEFAULT_PIN_OVERSHOOT_TOLERANCE, DEFAULT_PREVIEW_CHARS, DEFAULT_PREVIEW_LINES, DEFAULT_SKILLS_ROOT, DEFAULT_SKILL_READ_CHARS, DEFAULT_TOOL_FIND_MAX, type ExpiredApproval, FETCH_BLOB_INPUT_SCHEMA, FETCH_BLOB_TOOL_DESCRIPTION, FETCH_BLOB_TOOL_NAME, type FetchBlobArgs, HANDOFF_INPUT_SCHEMA, HANDOFF_RULES, HANDOFF_SOCKET_NAME, HANDOFF_TOOL_DESCRIPTION, HANDOFF_TOOL_NAME, type HandoffArgs, type HandoffOptions, type InlineSkill, type InlineSkillsOptions, LAZY_TOOLS_SOCKET_NAME, LAZY_TOOL_RULES, type LazyToolMenu, type LazyToolMenuEntry, type LazyToolsOptions, type LimitDimension, type LoadedSkill, MANIFEST_DEFAULT_EXCLUDE, MANIFEST_HEADING, MAX_MEMORY_PATH_LENGTH, MAX_SKILL_DESCRIPTION_CHARS, MEMORY_COMMANDS, MEMORY_INPUT_SCHEMA, MEMORY_ROOT, MEMORY_RULES, MEMORY_SOCKET_NAME, MEMORY_TOOL_DESCRIPTION, MEMORY_TOOL_NAME, type ManifestOptions, type MemoryCommand, type MemoryFs, type MemoryLimits, type MemoryOp, type MemoryOptions, type MemoryOutcome, PERCEPTION_SOCKET_NAME, PINS_SOCKET_NAME, PIN_INPUT_SCHEMA, PIN_RULES, PIN_TOOL_DESCRIPTION, PIN_TOOL_NAME, type ParsedSkill, type PerceptionLimits, type PerceptionOptions, type PerceptionReading, type PinArgs, type PinMeta, type PinNote, type PinSpec, type PinsOptions, type PlanOptions, type PolicyCall, type PolicyOutcome, type PolicyRule, type PolicyRuleSpec, type PolicyVerdict, type Preview, type PreviewOptions, RECALL_INPUT_SCHEMA, RECALL_TOOL_DESCRIPTION, RECALL_TOOL_NAME, type ReadingOptions, type ReadingThresholds, type RecallArgs, SKILLS_SOCKET_NAME, SKILL_FILE_NAME, SKILL_NAME_RE, SKILL_READ_INPUT_SCHEMA, SKILL_READ_TOOL_DESCRIPTION, SKILL_READ_TOOL_NAME, SKILL_RULES, SPILL_BLOB_MIME, SPILL_RULES, SPILL_SOCKET_NAME, type SkillFrontmatter, type SkillMenu, type SkillMenuEntry, type SkillReadInput, type SkillsOptions, type SpillOptions, TOOL_FIND_INPUT_SCHEMA, TOOL_FIND_TOOL_DESCRIPTION, TOOL_FIND_TOOL_NAME, type TextMeasure, type TextTokenEstimator, type Tier, type ToolFindInput, approval, assertAscending, assertLimits, assertSkillsRoot, bindMemoryFs, budget, budgetUsedOf, byteLength, carriedPins, checkBudget, clipEndByTokens, compact, compactNumber, composeHandoffNote, countLines, countModelTurns, countTier, defaultBudgetNote, defaultSummary, defaultTextTokens, evaluatePolicy, executeMemoryCommand, findExpiredApproval, findModelPin, fmt, globToRegExp, handoff, humanSize, inlineSkills, isModelCompaction, isPinNoteEvent, isTextMime, isUnder, lastHostPin, lastVisiblePerceptionNote, lastVisibleUserMessage, lazyMenuOf, lazyTools, loadSkillMenu, measureText, memory, normalizeRule, parseCompactArgs, parseFetchBlobArgs, parseHandoffArgs, parseMemoryCommand, parsePinArgs, parseRecallArgs, parseSkillMarkdown, parseSkillReadInput, parseToolFindInput, percent, perception, pinInputSchema, pinMetaOf, pinToolDescription, pins, planCompaction, previewOf, rangeTier, readPerception, recallResult, recallTool, renderCompactionSummary, renderLazyToolMenu, renderLoadedTool, renderPerception, renderSkillMenu, renderToolFindResult, resolveMemoryPath, revealedLazyTools, segmentTimelineByTurn, skills, spill, sumSessionTokens, summarizeTool, toolFindResultBound, toolReferenceOf, trailingCompactionRun, unfinishedHandoffArgs };
|
package/dist/index.js
CHANGED
|
@@ -333,7 +333,7 @@ var RECALL_TOOL_DESCRIPTION = [
|
|
|
333
333
|
"Use it when you need a value that the summary only mentioned or left out; each recall costs a turn and stays in context afterwards."
|
|
334
334
|
].join(" ");
|
|
335
335
|
var COMPACT_RULES = `## Managing your context window
|
|
336
|
-
You have a \`${COMPACT_TOOL_NAME}\` tool that folds earlier conversation into a summary you write.
|
|
336
|
+
You have a \`${COMPACT_TOOL_NAME}\` tool that folds earlier conversation into a summary you write. Status notes from the harness, if present, tell you how full the window is.
|
|
337
337
|
- Compact when a sub-task is finished or the trajectory has converged and the folded details are no longer needed verbatim. Do it at a natural pause, not while acting on something.
|
|
338
338
|
- Do not compact in the middle of a derivation, while a problem is still unresolved, or when you feel stuck: you would lose exactly the details you still need.
|
|
339
339
|
- The summary replaces everything it folds, including earlier summaries. Carry forward the user's goals and constraints, decisions made and why, the current state, and what comes next. List each fact that must survive in \`keep\`. The most recent user message and pinned notes survive verbatim on their own; check the pins still say what you need.
|
|
@@ -1687,7 +1687,7 @@ function memory(opts = {}) {
|
|
|
1687
1687
|
}
|
|
1688
1688
|
|
|
1689
1689
|
// src/perception/reading.ts
|
|
1690
|
-
import {
|
|
1690
|
+
import { contextOverheadOf } from "@reinsjs/core";
|
|
1691
1691
|
|
|
1692
1692
|
// src/perception/tiers.ts
|
|
1693
1693
|
function assertAscending(name, bounds) {
|
|
@@ -1753,14 +1753,6 @@ function sumSessionTokens(timeline) {
|
|
|
1753
1753
|
}
|
|
1754
1754
|
return n;
|
|
1755
1755
|
}
|
|
1756
|
-
function contextOverheadOf(timeline) {
|
|
1757
|
-
for (let i = timeline.length - 1; i >= 0; i--) {
|
|
1758
|
-
const e = timeline[i];
|
|
1759
|
-
if (e?.type !== "core.budget_usage" || e.payload.contextEstimate === void 0) continue;
|
|
1760
|
-
return Math.max(0, contextTokensOf2(e.payload.tokens) - e.payload.contextEstimate);
|
|
1761
|
-
}
|
|
1762
|
-
return 0;
|
|
1763
|
-
}
|
|
1764
1756
|
function remainingOf(budget2, contextTokens, limits) {
|
|
1765
1757
|
const used = {
|
|
1766
1758
|
contextTokens,
|
|
@@ -1820,8 +1812,7 @@ function renderPerception(r) {
|
|
|
1820
1812
|
const lines = [
|
|
1821
1813
|
"Runtime context status (from the harness, not from the user):",
|
|
1822
1814
|
`- Context window used: ${r.contextUsage.label} (the harness auto-folds the oldest turns at ${r.autoFoldAt})`,
|
|
1823
|
-
`-
|
|
1824
|
-
`- Session tokens used: ${r.sessionTokens.label}`
|
|
1815
|
+
`- Compactions so far: ${r.compactions} ${foldedNote(r.compactions)}`
|
|
1825
1816
|
];
|
|
1826
1817
|
if (r.budgetRemaining) {
|
|
1827
1818
|
lines.push(`- Run budget remaining: ${r.budgetRemaining.label} (tightest: ${r.budgetRemaining.tightest})`);
|