lingshu-skill 3.85.0 → 3.88.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 +33 -0
- package/README.md +1 -1
- package/SKILL.md +1 -1
- package/cordis-plugin/lingshu-startup/context-pressure.js +80 -0
- package/cordis-plugin/lingshu-startup/plugin.js +8 -0
- package/docs//350/277/233/345/261/225/344/270/216/345/276/205/345/212/236.md +10 -0
- package/package.json +1 -1
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +7 -7
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +33 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/behavior_baseline.py +3 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +25 -4
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +64 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +10 -3
- package/scripts/behavior_baseline.py +3 -1
- package/scripts/cli.py +25 -4
- package/scripts/core.py +64 -0
- package/scripts/memory.py +10 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## v3.88(2026-11-23)
|
|
4
|
+
|
|
5
|
+
> 主题:**P9 水位联动审查**——发现 v3.86 只接通了"显示",注入降档仍读老机制(恒 full),本次打通联动。
|
|
6
|
+
|
|
7
|
+
- **`read_pressure()` 优先用 tokenMeter 实时水位**(P9 #54/55):v3.86 的 token 快照只接进 `_water_level`(显示),而 `build_injection` 的预算 ×0.7/×0.45、lazy/minimal 裁剪、水位守则提示全走 `read_pressure()`→老 `_pressure.json`(只在手工 check-context 时写,平时恒空/陈旧)→ 结果"显示 44% 但注入恒 full、快满也不缩包"。现在 `read_pressure()` 优先序:**手工新鲜值(check-context 显式 --used,我比你准)> tokenMeter 实时快照 > 陈旧老文件兜底**。下游三处消费者全部受益,不破坏老环境兼容。
|
|
8
|
+
- **水位守则提示 pct 随之变实时**(#56):minimal 档的"上下文已偏满(X%)"现在取 tokenMeter 值,不再是陈旧老文件。
|
|
9
|
+
- 实测:read_pressure 在 5%/75%/90% → full/lazy/minimal 档(source=token-meter);高水位注入包确实变短;手工新鲜 60% 优先于 token 5.4%。
|
|
10
|
+
|
|
11
|
+
验证:`tests/test_water_linkage.py` 2/2;六项检查 + 行为基线 + 全量测试全过。
|
|
12
|
+
|
|
13
|
+
## v3.87(2026-11-23)
|
|
14
|
+
|
|
15
|
+
> 主题:**修 v3.86 上线后暴露的水位百分比显示 bug**——采集器实测 44%,但 startup/status 显示 0%/60%。
|
|
16
|
+
|
|
17
|
+
- **根因**:`_water_level()` 的 `pct` 统一是 0-1 小数(0.44=44%),但两处显示位按 0-100 直接输出:`cli.py` 启动水位块 `int(0.43)=0`、`status` 的 `f"{pct:.0f}%"=0%`。此前一直是 stale 分支(显示 config 里的 0-100 原始值)掩盖了这个问题,token-meter 首次走到非 stale 分支才暴露。
|
|
18
|
+
- **修**:两处改成 `×100` 再取整。实测同一批命令内 collector=44.0% / water=0.44 / startup 输出"水位:44%(本会话实测)"三处一致。
|
|
19
|
+
- **说明**:排查中一度看到 60% 与 44% 并存,是采集器在命令间隙持续写入新值(会话在增长)造成的时序差,非 bug;同批对比已排除。
|
|
20
|
+
|
|
21
|
+
验证:六项检查 + 行为基线 + 全量测试全过;startup/status/check-context 三处显示一致。
|
|
22
|
+
|
|
23
|
+
## v3.86(2026-11-23)
|
|
24
|
+
|
|
25
|
+
> 主题:**给灵枢加"自己测上下文占用"的能力**——原来宿主注入的"估算约 14%(来源 chars)"偏低(真实 39%),而灵枢代码明确写着"没有实时上下文可测"。本次接通 DSH 官方 tokenMeter。
|
|
26
|
+
|
|
27
|
+
- **调研**(dsh-doc + DSH 安装源码实证):`@deepseek-ai/dsh-token-meter`(已装 v0.2.2-rc.5)以 `ctx.tokenMeter` 挂载(`super(ctx, "tokenMeter")`),公开 `contextPressure(session) -> {pressureTokens, contextWindow}`——provider 上报的真实 prompt 大小 + 路由窗口。这是用户可见参考值(非计费输入),正是水位显示所需。
|
|
28
|
+
- **采集器**(`cordis-plugin/lingshu-startup/context-pressure.js`):startup 插件在 `agent/pre-step` 每轮请求前调一次 `sampleContextPressure(ctx, agent)`,把 `{pct, pressure_tokens, context_window, ts, available}` 写到 `HOME/.context-pressure.json`。服务未挂载/字段缺省(首轮还没 usage)时写 `available=false`,全部异常吞掉(pre-step 监听器不能冒泡)。
|
|
29
|
+
- **Python 侧**(`core.token_context_pressure()`):读该快照,10 分钟以上视为陈旧、`available=false`/字段无效一律返回 None;`check-context` 不带 `--used` 时优先用它(标注"来源:DSH tokenMeter"),再退回 last_session_pct,最后才报错要求显式给值;`_water_level`(startup 水位提醒块/面板/status 的唯一数据源)同步改为优先 token-meter,标 `source=token-meter`、`stale=False`。
|
|
30
|
+
- **实测**:CLI `check-context` 不带参数自动读到模拟快照 39%(39000/100000 tokens);token-meter 快照可用/过期/不可用三种情况测试 3/3。
|
|
31
|
+
|
|
32
|
+
- **顺带修**:behavior_baseline.py 的时段归一化正则漏了“清晨/午间”(memory._time_context 5-11 点输出“清晨”),导致早上跑基线误报 6 处差异;补齐词表后行为基线 OK。
|
|
33
|
+
|
|
34
|
+
验证:`tests/test_context_pressure.py` 3/3;JS 两插件 node --check;六项检查 + 行为基线 + 全量测试(57/57)全过。
|
|
35
|
+
|
|
3
36
|
## v3.85(2026-11-22)
|
|
4
37
|
|
|
5
38
|
> 主题:**修 v3.84 发版自动化的 staged 轮询 bug**——publish 用 SKILL.md 的 X.Y 与 registry 的 X.Y.0 比较,永远匹配不上,导致发布成功后脚本空转 20 分钟;本次改从 package.json 读 X.Y.0。
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> 名字取自《黄帝内经》:**算力给思考,人格给灵魂,记忆给生命。**
|
|
4
4
|
|
|
5
|
-
> 当前版本 **v3.
|
|
5
|
+
> 当前版本 **v3.88**(变更见 [CHANGELOG.md](CHANGELOG.md))
|
|
6
6
|
|
|
7
7
|
> 🔁 **双仓库镜像**:GitHub(主)与 Gitee(镜像)保持同步——[gitee.com/htmcwy_0/lingshu-skill](https://gitee.com/htmcwy_0/lingshu-skill)。灵枢的更新检查会探测两边最新提交,**谁更新就自动从谁拉取**;任一边不可达(Gitee 匿名风控 / GitHub 限额)时走另一边兜底。
|
|
8
8
|
|
package/SKILL.md
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// 灵枢 · 上下文压力采集器
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:DSH 宿主注入的"上下文估算约 X%(来源:chars)"是字符粗估,
|
|
4
|
+
// 实测偏低(用户看到 14%、真实 39%)。DSH 官方有准确的 token 计量服务:
|
|
5
|
+
// ctx.tokenMeter.contextPressure(session) -> { pressureTokens, contextWindow }
|
|
6
|
+
// 但灵枢主体是 Python 进程(brain.py),拿不到 Cordis ctx。本模块在 DSH 插件层
|
|
7
|
+
// (JS,有 ctx)每步读一次准确压力,写到 HOME/.context-pressure.json,
|
|
8
|
+
// brain.py check-context / startup 注入再读这个文件——补上"没实时上下文可测"的缺口。
|
|
9
|
+
//
|
|
10
|
+
// 设计约束:
|
|
11
|
+
// - 只读 DSH 服务、只写 HOME 下一个小 JSON;绝不改 profile/注入文本;
|
|
12
|
+
// - 任何异常都吞掉(pre-step 是 wrapping 监听器,抛异常会破坏请求);
|
|
13
|
+
// - ctx.tokenMeter 不存在(旧版 DSH/未挂载)时静默跳过,写 availability=false 让 Python 侧知道。
|
|
14
|
+
|
|
15
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
16
|
+
import { homedir } from 'node:os'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
|
|
19
|
+
const asText = (v) => String(v ?? '').trim()
|
|
20
|
+
|
|
21
|
+
/** 记忆目录:BRAIN_MEMORY_HOME > ~/.brain-memory/.home 指针 > ~/.brain-memory。 */
|
|
22
|
+
function memoryHome() {
|
|
23
|
+
const env = asText(process.env.BRAIN_MEMORY_HOME)
|
|
24
|
+
if (env) return env
|
|
25
|
+
const base = join(homedir(), '.brain-memory')
|
|
26
|
+
try {
|
|
27
|
+
const ptr = asText(readFileSync(join(base, '.home'), 'utf8'))
|
|
28
|
+
if (ptr) return ptr
|
|
29
|
+
} catch { /* 没有指针文件就用默认目录 */ }
|
|
30
|
+
return base
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 采集一次上下文压力并落盘。由 startup 插件在 agent/pre-step 里调用。
|
|
35
|
+
* @param ctx Cordis 上下文(需有 tokenMeter 服务)
|
|
36
|
+
* @param agent 当前 agent(用于取 session)
|
|
37
|
+
*/
|
|
38
|
+
export function sampleContextPressure(ctx, agent) {
|
|
39
|
+
const out = {
|
|
40
|
+
ts: Math.floor(Date.now() / 1000),
|
|
41
|
+
available: false,
|
|
42
|
+
source: 'token-meter',
|
|
43
|
+
pressure_tokens: null,
|
|
44
|
+
context_window: null,
|
|
45
|
+
pct: null,
|
|
46
|
+
}
|
|
47
|
+
let file = ''
|
|
48
|
+
try {
|
|
49
|
+
const dir = memoryHome()
|
|
50
|
+
file = join(dir, '.context-pressure.json')
|
|
51
|
+
const meter = typeof ctx?.get === 'function' ? ctx.get('tokenMeter') : (ctx?.tokenMeter ?? null)
|
|
52
|
+
const session = agent?.session ?? null
|
|
53
|
+
if (!meter || typeof meter.contextPressure !== 'function' || !session) {
|
|
54
|
+
out.reason = 'tokenMeter 服务或 session 不可用(旧版 DSH 或未挂载)'
|
|
55
|
+
return write(file, out)
|
|
56
|
+
}
|
|
57
|
+
const snap = meter.contextPressure(session) || {}
|
|
58
|
+
const p = Number(snap.pressureTokens)
|
|
59
|
+
const w = Number(snap.contextWindow)
|
|
60
|
+
out.available = Number.isFinite(p) && Number.isFinite(w) && w > 0
|
|
61
|
+
if (out.available) {
|
|
62
|
+
out.pressure_tokens = Math.max(0, Math.round(p))
|
|
63
|
+
out.context_window = Math.round(w)
|
|
64
|
+
out.pct = Math.min(100, Math.max(0, Math.round((p / w) * 1000) / 10))
|
|
65
|
+
} else {
|
|
66
|
+
out.reason = 'provider 还没上报 usage / route 未声明窗口(字段缺省)'
|
|
67
|
+
}
|
|
68
|
+
} catch (e) {
|
|
69
|
+
out.reason = `采集失败:${e?.message || String(e)}`.slice(0, 200)
|
|
70
|
+
}
|
|
71
|
+
return write(file, out)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function write(file, out) {
|
|
75
|
+
try {
|
|
76
|
+
if (!file) return out
|
|
77
|
+
writeFileSync(file, JSON.stringify(out, null, 2) + '\n', 'utf8')
|
|
78
|
+
} catch { /* 写失败不影响请求 */ }
|
|
79
|
+
return out
|
|
80
|
+
}
|
|
@@ -25,6 +25,7 @@ import { homedir } from 'node:os'
|
|
|
25
25
|
import { delimiter, dirname, join } from 'node:path'
|
|
26
26
|
import { createHash, randomUUID } from 'node:crypto'
|
|
27
27
|
import { fileURLToPath } from 'node:url'
|
|
28
|
+
import { sampleContextPressure } from './context-pressure.js'
|
|
28
29
|
|
|
29
30
|
// 刻意零外部依赖:不 import @deepseek-ai/schemastery。原因——link: 安装时模块
|
|
30
31
|
// 真实路径留在技能目录,Node 从那里向上找 node_modules 一路到盘符根都找不到
|
|
@@ -328,6 +329,13 @@ export function apply(ctx, config) {
|
|
|
328
329
|
// 先 await next():让其他(wrapping)监听器先改完,保住它们的
|
|
329
330
|
// startsRequestSeries / batch 语义,再在结果上追加。
|
|
330
331
|
const decision = await next()
|
|
332
|
+
// v3.86:每轮请求前采一次 DSH 的准确 token 压力(ctx.tokenMeter),
|
|
333
|
+
// 写 HOME/.context-pressure.json 给 brain.py 读。必须在决策之后也不影响
|
|
334
|
+
// 注入语义;采集器自身吞掉所有异常,绝不让 pre-step 冒泡。
|
|
335
|
+
try {
|
|
336
|
+
const { agent: pa } = payload || {}
|
|
337
|
+
if (pa) sampleContextPressure(ctx, pa)
|
|
338
|
+
} catch { /* 采集失败不影响请求/注入 */ }
|
|
331
339
|
if (!decision || decision.kind !== 'enter') return decision
|
|
332
340
|
const { agent } = payload || {}
|
|
333
341
|
const text = agent && pending.get(agent)
|
|
@@ -349,6 +349,16 @@ startup 注入的记忆块与 cron 的瞬态提醒**同名**,于是 cron 的 p
|
|
|
349
349
|
|
|
350
350
|
52. **【中·安全】npm token 裸发漏脱敏**——`core._REDACT_PATTERNS` 无 npm 规则,裸 `npm_xxx` 被 capture 原文落盘。**已在 v3.82 修**(加 `npm_`+30 位具名规则)。历史残留(真库旧 token)已作废,不擅自删改记忆库。
|
|
351
351
|
|
|
352
|
+
### P8 · 上下文压力实测(2026-11-23)
|
|
353
|
+
|
|
354
|
+
53. **【已修 v3.86】上下文水位估算偏低**:宿主注入的"估算约 14%(来源 chars)"实测偏低(用户反馈真实 39%),而灵枢原代码明确"没有实时上下文可测"。接通 DSH 官方 `ctx.tokenMeter.contextPressure(session)`(已装 v0.2.2-rc.5,源码确认 `super(ctx,"tokenMeter")` 挂载),startup 插件每轮 pre-step 写 HOME/.context-pressure.json,Python 侧 `check-context`/`_water_level` 优先读它。实测 3/3。
|
|
355
|
+
|
|
356
|
+
### P9 · 水位联动审查(2026-11-23)
|
|
357
|
+
|
|
358
|
+
54. **【高·联动断】注入降档没用上 tokenMeter 实时水位**:`build_injection` 的 budget ×0.7/×0.45、lazy/minimal 裁剪、水位守则提示全读 `read_pressure()`→`_pressure.json`(老机制,靠手工 check-context 写);v3.86 的 tokenMeter 只接进了 `_water_level`(显示)。结果显示 44% 但注入恒 full 档——上下文快满也不缩包。**修**:`read_pressure()` 优先用 `token_context_pressure()`(换算 0-100 + tier),下游三处消费者全受益,老文件兜底兼容。 **【已在 v3.88 修】read_pressure 优先 token-meter,注入降档联动**
|
|
359
|
+
55. **【中·口径分裂】两套压力文件各说各话**:显示用 `.context-pressure.json`(0-1 小数、实时),降档用 `_pressure.json`(0-100、手工写),只在主动跑 check-context 时同步一次。**修**:同 #54 统一到 tokenMeter 优先。 **【已在 v3.88 修】统一口径(手工新鲜值优先,token其次)**
|
|
360
|
+
56. **【中·提示陈旧】水位守则的 pct 显示来自老文件**(`memory.py:4148`):minimal 档提示"上下文已偏满(X%)"用的是 `_pressure.get('pct')`(可能陈旧/None),不是实时值。**随 #54 一起修**。 **【已在 v3.88 修】守则提示 pct 随 read_pressure 变实时**
|
|
361
|
+
|
|
352
362
|
## 三、已知问题与风险
|
|
353
363
|
|
|
354
364
|
| # | 现象 | 影响 | 处置 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lingshu-skill",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.88.0",
|
|
4
4
|
"description": "Brain-inspired persistent memory, persona and growth system for AI coding agents. Local-only, zero-dependency Python. Requires Python 3.10+.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"PATCH_NOTES.md": "9296e524f743f358fd11115598398a754cfd69534526dde9c8ad85f14a418f89",
|
|
3
3
|
"README.md": "3d634d743b085eebd2cbb5dd3a00edfa728ed23947e20cc29e7c3b29087ec043",
|
|
4
|
-
"engine/CHANGELOG.md": "
|
|
5
|
-
"engine/README.md": "
|
|
4
|
+
"engine/CHANGELOG.md": "4addf07b56c10a68b175b7f01565a935a86cece8d52cad92c8b51879b2133653",
|
|
5
|
+
"engine/README.md": "db6f4e669ee52d262db322c5931d9b15fb0c673be13e29fb0ff9a66147bf92ca",
|
|
6
6
|
"engine/SKILL-lite.md": "04b87789232a0f7537c861c857888b4a0e947bcf92a461cbad5328af05361c58",
|
|
7
7
|
"engine/SKILL-tiny.md": "74e1955cc5bf4ec0ca28d52030f799f039283de27a52345a33ce783d6ee0c608",
|
|
8
|
-
"engine/SKILL.md": "
|
|
8
|
+
"engine/SKILL.md": "6a83aa8c5a2dcb701015096167f300534d9380bd6760c7f9e7e79f39855bb7e4",
|
|
9
9
|
"engine/assets/panel.html": "8b368f16f0c1447413456a16cb4666daa4df08983bd30d627af04ad2a0fbda26",
|
|
10
10
|
"engine/docs/commands.md": "15f57adbeeca393fe001831853d5b9a5dfb237ac666711fa5662000eb22b2c59",
|
|
11
11
|
"engine/docs/mechanics.md": "e8a6602a434ed5b78cac64a98884f65a6888eb48daa7a1c0be1f6c3984041b34",
|
|
@@ -13,15 +13,15 @@
|
|
|
13
13
|
"engine/docs/panel-api.md": "2cab01a7b824b402bca41d8739d36133265bb8622385876e1b499385aa1aaa99",
|
|
14
14
|
"engine/docs/recovery.md": "2e9443d8416048c72ebe5cbec03a437fb833e41f4cd6cc4b3c8ee14ea73a0e8e",
|
|
15
15
|
"engine/scripts/assembler.py": "084e1b0157feda76539f0acd57f813edc4fa4df65c7d418912ef874174622db6",
|
|
16
|
-
"engine/scripts/behavior_baseline.py": "
|
|
16
|
+
"engine/scripts/behavior_baseline.py": "2b91b597a7914c4bc65c6c19244941d4848d352f2434d3115e5dfdd6a09914ce",
|
|
17
17
|
"engine/scripts/bench.py": "036d427e52e3fb7a63479428ef616c02d35b2c7ab5cb2dd389a836934a7d4ff4",
|
|
18
18
|
"engine/scripts/brain.py": "659be7e951fa6d1505ea5a7c1a7aff7fb3cb182633573a68ae135ccaa4b7f05f",
|
|
19
19
|
"engine/scripts/check_pressure_sync.py": "46d53934f17c7b740d0ec9fdbef9d2653850f6e01e883eedd70ff9a1527e869a",
|
|
20
20
|
"engine/scripts/check_tag.py": "6f0b8209dc2deebb1f9ec90d429e1fad60c040a2ec014a6406d9d3498faafd7f",
|
|
21
21
|
"engine/scripts/checkpoint.py": "ec74e70e6ae1338ee3d7048c71b8297a1f60bd3c572a2e2843b8e9696bdf2d9a",
|
|
22
|
-
"engine/scripts/cli.py": "
|
|
22
|
+
"engine/scripts/cli.py": "297aa67ef468cca9444a4e387a69ca87d6e7d36475473532f42c51f090047a2c",
|
|
23
23
|
"engine/scripts/context_ab.py": "3d383f1fc16d328612825bbf731866fa427da0746490d027a0ddb719d210f1cb",
|
|
24
|
-
"engine/scripts/core.py": "
|
|
24
|
+
"engine/scripts/core.py": "08be3f2e57eebc29e9b096a75f953cc1ff5c1e6000b2f84f74f1c5c8f965f162",
|
|
25
25
|
"engine/scripts/dream.py": "e25749c32fe6cb3b3845c9541ef663dffe8efaa0d9bd251f611e1359208800b9",
|
|
26
26
|
"engine/scripts/emotion.py": "c2928217a856c9201291ebd63dee33a4a641ff30dcd79964738c24e7a9d94672",
|
|
27
27
|
"engine/scripts/failures.py": "3a16234a1871f62e3ba7b7b743110b4044fb7b715b4d0e924b8ebf1cf0be50d0",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"engine/scripts/mem_lesson.py": "6d9c26d4307dccb80f01d406e5253fe26f3540e0774b212f5fbd939d2ac442f0",
|
|
36
36
|
"engine/scripts/mem_score.py": "95dcecba5f4db7c0c16ebc415434e1cb58cc7dd8b8213360b4df36e2a90faf6e",
|
|
37
37
|
"engine/scripts/mem_topic.py": "c22f75c128cdd3b26ff845f1a71608401bc0dc8955f9f0e1988d7f0cd663d0d5",
|
|
38
|
-
"engine/scripts/memory.py": "
|
|
38
|
+
"engine/scripts/memory.py": "235791260369d5a95da7128fec024c66671821c00e59d1af030b1a450b42292c",
|
|
39
39
|
"engine/scripts/npm_prepack.py": "d62c491a6f9edfcab2dd465a995e72df879a61b4da26ae0fe76121c8adc77e02",
|
|
40
40
|
"engine/scripts/panel_build.py": "6394c1d74d203093e43b2106d7acca6e0ca9db61ca1cd6cd3a1616053653414b",
|
|
41
41
|
"engine/scripts/panel_cloud.py": "fff72888123d45bbf05ab329a06b2126f1c0b972c76283ecff32454bc4155c58",
|
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## v3.88(2026-11-23)
|
|
4
|
+
|
|
5
|
+
> 主题:**P9 水位联动审查**——发现 v3.86 只接通了"显示",注入降档仍读老机制(恒 full),本次打通联动。
|
|
6
|
+
|
|
7
|
+
- **`read_pressure()` 优先用 tokenMeter 实时水位**(P9 #54/55):v3.86 的 token 快照只接进 `_water_level`(显示),而 `build_injection` 的预算 ×0.7/×0.45、lazy/minimal 裁剪、水位守则提示全走 `read_pressure()`→老 `_pressure.json`(只在手工 check-context 时写,平时恒空/陈旧)→ 结果"显示 44% 但注入恒 full、快满也不缩包"。现在 `read_pressure()` 优先序:**手工新鲜值(check-context 显式 --used,我比你准)> tokenMeter 实时快照 > 陈旧老文件兜底**。下游三处消费者全部受益,不破坏老环境兼容。
|
|
8
|
+
- **水位守则提示 pct 随之变实时**(#56):minimal 档的"上下文已偏满(X%)"现在取 tokenMeter 值,不再是陈旧老文件。
|
|
9
|
+
- 实测:read_pressure 在 5%/75%/90% → full/lazy/minimal 档(source=token-meter);高水位注入包确实变短;手工新鲜 60% 优先于 token 5.4%。
|
|
10
|
+
|
|
11
|
+
验证:`tests/test_water_linkage.py` 2/2;六项检查 + 行为基线 + 全量测试全过。
|
|
12
|
+
|
|
13
|
+
## v3.87(2026-11-23)
|
|
14
|
+
|
|
15
|
+
> 主题:**修 v3.86 上线后暴露的水位百分比显示 bug**——采集器实测 44%,但 startup/status 显示 0%/60%。
|
|
16
|
+
|
|
17
|
+
- **根因**:`_water_level()` 的 `pct` 统一是 0-1 小数(0.44=44%),但两处显示位按 0-100 直接输出:`cli.py` 启动水位块 `int(0.43)=0`、`status` 的 `f"{pct:.0f}%"=0%`。此前一直是 stale 分支(显示 config 里的 0-100 原始值)掩盖了这个问题,token-meter 首次走到非 stale 分支才暴露。
|
|
18
|
+
- **修**:两处改成 `×100` 再取整。实测同一批命令内 collector=44.0% / water=0.44 / startup 输出"水位:44%(本会话实测)"三处一致。
|
|
19
|
+
- **说明**:排查中一度看到 60% 与 44% 并存,是采集器在命令间隙持续写入新值(会话在增长)造成的时序差,非 bug;同批对比已排除。
|
|
20
|
+
|
|
21
|
+
验证:六项检查 + 行为基线 + 全量测试全过;startup/status/check-context 三处显示一致。
|
|
22
|
+
|
|
23
|
+
## v3.86(2026-11-23)
|
|
24
|
+
|
|
25
|
+
> 主题:**给灵枢加"自己测上下文占用"的能力**——原来宿主注入的"估算约 14%(来源 chars)"偏低(真实 39%),而灵枢代码明确写着"没有实时上下文可测"。本次接通 DSH 官方 tokenMeter。
|
|
26
|
+
|
|
27
|
+
- **调研**(dsh-doc + DSH 安装源码实证):`@deepseek-ai/dsh-token-meter`(已装 v0.2.2-rc.5)以 `ctx.tokenMeter` 挂载(`super(ctx, "tokenMeter")`),公开 `contextPressure(session) -> {pressureTokens, contextWindow}`——provider 上报的真实 prompt 大小 + 路由窗口。这是用户可见参考值(非计费输入),正是水位显示所需。
|
|
28
|
+
- **采集器**(`cordis-plugin/lingshu-startup/context-pressure.js`):startup 插件在 `agent/pre-step` 每轮请求前调一次 `sampleContextPressure(ctx, agent)`,把 `{pct, pressure_tokens, context_window, ts, available}` 写到 `HOME/.context-pressure.json`。服务未挂载/字段缺省(首轮还没 usage)时写 `available=false`,全部异常吞掉(pre-step 监听器不能冒泡)。
|
|
29
|
+
- **Python 侧**(`core.token_context_pressure()`):读该快照,10 分钟以上视为陈旧、`available=false`/字段无效一律返回 None;`check-context` 不带 `--used` 时优先用它(标注"来源:DSH tokenMeter"),再退回 last_session_pct,最后才报错要求显式给值;`_water_level`(startup 水位提醒块/面板/status 的唯一数据源)同步改为优先 token-meter,标 `source=token-meter`、`stale=False`。
|
|
30
|
+
- **实测**:CLI `check-context` 不带参数自动读到模拟快照 39%(39000/100000 tokens);token-meter 快照可用/过期/不可用三种情况测试 3/3。
|
|
31
|
+
|
|
32
|
+
- **顺带修**:behavior_baseline.py 的时段归一化正则漏了“清晨/午间”(memory._time_context 5-11 点输出“清晨”),导致早上跑基线误报 6 处差异;补齐词表后行为基线 OK。
|
|
33
|
+
|
|
34
|
+
验证:`tests/test_context_pressure.py` 3/3;JS 两插件 node --check;六项检查 + 行为基线 + 全量测试(57/57)全过。
|
|
35
|
+
|
|
3
36
|
## v3.85(2026-11-22)
|
|
4
37
|
|
|
5
38
|
> 主题:**修 v3.84 发版自动化的 staged 轮询 bug**——publish 用 SKILL.md 的 X.Y 与 registry 的 X.Y.0 比较,永远匹配不上,导致发布成功后脚本空转 20 分钟;本次改从 package.json 读 X.Y.0。
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> 名字取自《黄帝内经》:**算力给思考,人格给灵魂,记忆给生命。**
|
|
4
4
|
|
|
5
|
-
> 当前版本 **v3.
|
|
5
|
+
> 当前版本 **v3.88**(变更见 [CHANGELOG.md](CHANGELOG.md))
|
|
6
6
|
|
|
7
7
|
> 🔁 **双仓库镜像**:GitHub(主)与 Gitee(镜像)保持同步——[gitee.com/htmcwy_0/lingshu-skill](https://gitee.com/htmcwy_0/lingshu-skill)。灵枢的更新检查会探测两边最新提交,**谁更新就自动从谁拉取**;任一边不可达(Gitee 匿名风控 / GitHub 限额)时走另一边兜底。
|
|
8
8
|
|
|
@@ -73,7 +73,9 @@ def _normalize(text: str) -> str:
|
|
|
73
73
|
text = re.sub(r"\b\d+(\.\d+)?\s*(ms|毫秒)\b", "<DUR>", text)
|
|
74
74
|
# 时段描述:`<DATETIME>,下午,正事多在此时(今天周末)。` / `<DATETIME>,深夜(…)——…。`
|
|
75
75
|
# 一律抹到句末句号(时段词后面可能是逗号,也可能是括号,格式随时段而变)。
|
|
76
|
-
|
|
76
|
+
# 词表必须与 memory._time_context 的时段段对齐——清晨(5-11)/午间(11-14)/下午/
|
|
77
|
+
# 夜晚/深夜等;之前漏了"清晨",导致早上 8 点录/查基线误报 6 处差异。
|
|
78
|
+
text = re.sub(r",(凌晨|清晨|早晨|上午|中午|午间|下午|傍晚|夜晚|深夜)[^\n]*?。",
|
|
77
79
|
",<TOD>。", text)
|
|
78
80
|
return text
|
|
79
81
|
|
|
@@ -777,7 +777,10 @@ def cmd_startup(query: str = "", budget: int | None = None,
|
|
|
777
777
|
# 水位提醒块:与 status 同一实现(memory._water_level),别两套口径。
|
|
778
778
|
try:
|
|
779
779
|
wl = memory._water_level()
|
|
780
|
-
pct
|
|
780
|
+
# _water_level 的 pct 统一是 0-1 小数(0.43=43%);stale 分支显示的是
|
|
781
|
+
# config 里的原始 0-100 值,两条口径别混。之前非 stale 分支直接 int(0.43)
|
|
782
|
+
# 得到 0%,token-meter 上线后首次走到这里才暴露。
|
|
783
|
+
pct = int(round(float(wl.get("pct", 0)) * 100))
|
|
781
784
|
if wl.get("stale"):
|
|
782
785
|
_last = wl.get("last_session_pct")
|
|
783
786
|
_age = wl.get("last_session_pct_age_hours")
|
|
@@ -1698,7 +1701,9 @@ def _main(argv=None) -> int:
|
|
|
1698
1701
|
p_forget.add_argument("id")
|
|
1699
1702
|
|
|
1700
1703
|
p_ctx = sub.add_parser("check-context", help="检查上下文水位:60%% 清点 / 75%% 做梦 / 90%% 封存")
|
|
1701
|
-
p_ctx.add_argument("--used", type=float,
|
|
1704
|
+
p_ctx.add_argument("--used", type=float, default=None,
|
|
1705
|
+
help="已用上下文百分比 0-100(缺省自动读 DSH tokenMeter 采集的 .context-pressure.json;"
|
|
1706
|
+
"读不到再退回最近一次手工记录)")
|
|
1702
1707
|
p_ctx.add_argument("--unsaved", type=float, default=0,
|
|
1703
1708
|
help="本轮还没落盘的估计字数(凭感觉估即可,用来提示'该记没记')")
|
|
1704
1709
|
p_ctx.add_argument("--handoff", action="store_true",
|
|
@@ -3665,7 +3670,7 @@ def _main(argv=None) -> int:
|
|
|
3665
3670
|
+ (f";上次记录 {wl['last_session_pct']}%"
|
|
3666
3671
|
+ (f",{wl['last_session_pct_age_hours']}h 前" if wl.get("last_session_pct_age_hours") is not None else "")
|
|
3667
3672
|
if wl.get("last_session_pct") is not None else "")
|
|
3668
|
-
+ ")") if wl.get("stale") else f"水位 {wl['pct']:.0f}%"
|
|
3673
|
+
+ ")") if wl.get("stale") else f"水位 {wl['pct'] * 100:.0f}%"
|
|
3669
3674
|
print(f"{_pct_txt}(清点 {th['review']:.0%} / 做梦 {th['dream']:.0%} / "
|
|
3670
3675
|
f"封存 {th['escape']:.0%})· 级别 {wl['level']}")
|
|
3671
3676
|
print(f" 各层条数:{wl['counts']}")
|
|
@@ -4405,7 +4410,23 @@ def _main(argv=None) -> int:
|
|
|
4405
4410
|
print(f"交接笔记已落盘:{rec.get('id', '')}(notes 里 kind=log,startup 会带出来)")
|
|
4406
4411
|
return 0
|
|
4407
4412
|
cfg = load_config()
|
|
4408
|
-
|
|
4413
|
+
# v3.86:--used 缺省时按优先级取真实水位——
|
|
4414
|
+
# 1) DSH tokenMeter 采集的 .context-pressure.json(准确、每轮更新);
|
|
4415
|
+
# 2) 最近一次 check-context 手工记下的 last_session_pct;
|
|
4416
|
+
# 3) 都没有才报错要求显式给 --used(不再瞎猜)。
|
|
4417
|
+
_tp = memory.token_context_pressure()
|
|
4418
|
+
if args.used is not None:
|
|
4419
|
+
used = float(args.used)
|
|
4420
|
+
elif _tp:
|
|
4421
|
+
used = _tp["pct"]
|
|
4422
|
+
print(f"[水位] 来源:DSH tokenMeter({_tp['pressure_tokens']}/{_tp['context_window']} tokens)")
|
|
4423
|
+
elif _cfg_num(cfg, "last_session_pct", -1) >= 0:
|
|
4424
|
+
used = _cfg_num(cfg, "last_session_pct")
|
|
4425
|
+
print("[水位] 来源:最近一次手工 check-context(DSH tokenMeter 未采集到)")
|
|
4426
|
+
else:
|
|
4427
|
+
print("[错误] 拿不到上下文水位:请先让 DSH 跑一轮(tokenMeter 会采集),"
|
|
4428
|
+
"或显式传 --used <百分比>", file=sys.stderr)
|
|
4429
|
+
return 1
|
|
4409
4430
|
# 记下本次水位:status --json / startup 水位提醒块的"最近一次 session 结束水位"
|
|
4410
4431
|
if not READONLY:
|
|
4411
4432
|
try:
|
|
@@ -1135,7 +1135,38 @@ def read_pressure(cfg: dict | None = None) -> dict | None:
|
|
|
1135
1135
|
返回形状:`{"pct": 78.0, "at": 1795240000, "stale": False, "tier": "lazy"|"minimal"|"full", ...}`
|
|
1136
1136
|
"""
|
|
1137
1137
|
try:
|
|
1138
|
+
# 先读老机制文件:手工 check-context --used 写的值若新鲜,应**优先于**
|
|
1139
|
+
# tokenMeter——用户显式给的数就是"我比你准"。平时它陈旧/不存在,才用 token。
|
|
1140
|
+
legacy = None
|
|
1138
1141
|
path = HOME / _PRESSURE_FILE
|
|
1142
|
+
if path.exists():
|
|
1143
|
+
try:
|
|
1144
|
+
raw = json.loads(path.read_text(encoding="utf-8-sig"))
|
|
1145
|
+
if isinstance(raw, dict):
|
|
1146
|
+
_lp, _la = raw.get("pct"), raw.get("at")
|
|
1147
|
+
if (not isinstance(_lp, bool) and isinstance(_lp, (int, float))
|
|
1148
|
+
and 0 <= float(_lp) <= 100
|
|
1149
|
+
and isinstance(_la, (int, float)) and not isinstance(_la, bool)
|
|
1150
|
+
and now_ts() - float(_la) <= _PRESSURE_TTL):
|
|
1151
|
+
legacy = {"pct": float(_lp), "at": float(_la),
|
|
1152
|
+
"source": str(raw.get("source") or "manual")}
|
|
1153
|
+
except Exception:
|
|
1154
|
+
pass
|
|
1155
|
+
tp = token_context_pressure()
|
|
1156
|
+
if legacy is not None:
|
|
1157
|
+
th = pressure_thresholds(cfg if cfg is not None else load_config())
|
|
1158
|
+
pct = legacy["pct"]
|
|
1159
|
+
tier = ("minimal" if pct >= th["minimal_at"]
|
|
1160
|
+
else "lazy" if pct >= th["lazy_at"] else "full")
|
|
1161
|
+
return {"pct": pct, "at": legacy["at"], "stale": False, "tier": tier,
|
|
1162
|
+
"thresholds": th, "source": legacy["source"]}
|
|
1163
|
+
if tp is not None:
|
|
1164
|
+
th = pressure_thresholds(cfg if cfg is not None else load_config())
|
|
1165
|
+
pct = float(tp["pct"])
|
|
1166
|
+
tier = ("minimal" if pct >= th["minimal_at"]
|
|
1167
|
+
else "lazy" if pct >= th["lazy_at"] else "full")
|
|
1168
|
+
return {"pct": pct, "at": tp["ts"], "stale": False, "tier": tier,
|
|
1169
|
+
"thresholds": th, "source": "token-meter"}
|
|
1139
1170
|
if not path.exists():
|
|
1140
1171
|
return None
|
|
1141
1172
|
raw = json.loads(path.read_text(encoding="utf-8-sig"))
|
|
@@ -1180,6 +1211,39 @@ def write_pressure(pct: float, source: str = "") -> None:
|
|
|
1180
1211
|
log_event("pressure", f"写上下文压力失败:{exc}", level="warn")
|
|
1181
1212
|
|
|
1182
1213
|
|
|
1214
|
+
# v3.86:DSH tokenMeter 采集的准确上下文压力(由 startup 插件写)。
|
|
1215
|
+
# 与 _pressure.json(灵枢自己按 check-context 记的降档档位)分开——
|
|
1216
|
+
# 这个文件是 DSH 官方 token 计量的只读快照,用来替代"没有实时上下文可测"的缺口。
|
|
1217
|
+
_TOKEN_PRESSURE_FILE = ".context-pressure.json"
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
def token_context_pressure(max_age_sec: int = 600) -> dict | None:
|
|
1221
|
+
"""读 DSH 插件写的 .context-pressure.json,返回 {pct, pressure_tokens, context_window, ts}。
|
|
1222
|
+
|
|
1223
|
+
找不到 / 过期 / 字段无效一律返回 None(调用方再退回旧逻辑)。
|
|
1224
|
+
max_age_sec 默认 600s:超过 10 分钟视为陈旧(会话可能已换),不采信。
|
|
1225
|
+
"""
|
|
1226
|
+
try:
|
|
1227
|
+
p = HOME / _TOKEN_PRESSURE_FILE
|
|
1228
|
+
if not p.is_file():
|
|
1229
|
+
return None
|
|
1230
|
+
d = json.loads(p.read_text(encoding="utf-8-sig"))
|
|
1231
|
+
if not isinstance(d, dict) or not d.get("available"):
|
|
1232
|
+
return None
|
|
1233
|
+
ts = _num(d.get("ts"), 0)
|
|
1234
|
+
if ts <= 0 or (now_ts() - ts) > max(0, int(max_age_sec)):
|
|
1235
|
+
return None
|
|
1236
|
+
pct = _num(d.get("pct"), -1)
|
|
1237
|
+
if not (0 <= pct <= 100):
|
|
1238
|
+
return None
|
|
1239
|
+
return {"pct": round(pct, 1),
|
|
1240
|
+
"pressure_tokens": int(_num(d.get("pressure_tokens"), 0)),
|
|
1241
|
+
"context_window": int(_num(d.get("context_window"), 0)),
|
|
1242
|
+
"ts": int(ts)}
|
|
1243
|
+
except Exception:
|
|
1244
|
+
return None
|
|
1245
|
+
|
|
1246
|
+
|
|
1183
1247
|
def clear_pressure() -> None:
|
|
1184
1248
|
"""清掉压力标记(回到满档)。"""
|
|
1185
1249
|
if READONLY:
|
|
@@ -1083,9 +1083,15 @@ def _water_level(used_pct: float | None = None) -> dict:
|
|
|
1083
1083
|
last_pct_at = _cfg_num(cfg, "last_session_pct_at", 0) # 那次记录的时间戳(v3.23)
|
|
1084
1084
|
|
|
1085
1085
|
used_given = used_pct is not None
|
|
1086
|
+
# v3.86:优先用 DSH tokenMeter 的准实时快照(每轮请求前采集),其次是显式
|
|
1087
|
+
# --used,最后才是 config 里的历史手工值。只有显式或 token-meter 算"非 stale"。
|
|
1088
|
+
_tp = None if used_given else token_context_pressure()
|
|
1086
1089
|
if used_given:
|
|
1087
1090
|
pct = float(used_pct)
|
|
1088
1091
|
source = "explicit"
|
|
1092
|
+
elif _tp:
|
|
1093
|
+
pct = _tp["pct"]
|
|
1094
|
+
source = "token-meter"
|
|
1089
1095
|
elif last_session_pct is not None:
|
|
1090
1096
|
pct = float(_cfg_num(cfg, "last_session_pct", 0))
|
|
1091
1097
|
source = "config" # 历史残留值,不是实时测量
|
|
@@ -1093,9 +1099,10 @@ def _water_level(used_pct: float | None = None) -> dict:
|
|
|
1093
1099
|
pct = 0.0
|
|
1094
1100
|
source = "default"
|
|
1095
1101
|
# v3.23:非实时输入一律标 stale —— 旧版把几天前的 last_session_pct 当"当前水位"返回,
|
|
1096
|
-
# 比没有更危险(看到 60% 会以为刚刚测过)。
|
|
1097
|
-
stale = not used_given
|
|
1098
|
-
age_hours = round((now -
|
|
1102
|
+
# 比没有更危险(看到 60% 会以为刚刚测过)。token-meter 是准实时,不算 stale。
|
|
1103
|
+
stale = not used_given and not _tp
|
|
1104
|
+
age_hours = round((now - _tp["ts"]) / 3600, 1) if _tp else (
|
|
1105
|
+
round((now - last_pct_at) / 3600, 1) if (stale and last_pct_at) else None)
|
|
1099
1106
|
if pct > 1.0:
|
|
1100
1107
|
pct = pct / 100.0 # 兼容 0-100 口径:--used 65 与 0.65 等价
|
|
1101
1108
|
review = _cfg_num(cfg, "review_threshold", 0.60)
|
|
@@ -73,7 +73,9 @@ def _normalize(text: str) -> str:
|
|
|
73
73
|
text = re.sub(r"\b\d+(\.\d+)?\s*(ms|毫秒)\b", "<DUR>", text)
|
|
74
74
|
# 时段描述:`<DATETIME>,下午,正事多在此时(今天周末)。` / `<DATETIME>,深夜(…)——…。`
|
|
75
75
|
# 一律抹到句末句号(时段词后面可能是逗号,也可能是括号,格式随时段而变)。
|
|
76
|
-
|
|
76
|
+
# 词表必须与 memory._time_context 的时段段对齐——清晨(5-11)/午间(11-14)/下午/
|
|
77
|
+
# 夜晚/深夜等;之前漏了"清晨",导致早上 8 点录/查基线误报 6 处差异。
|
|
78
|
+
text = re.sub(r",(凌晨|清晨|早晨|上午|中午|午间|下午|傍晚|夜晚|深夜)[^\n]*?。",
|
|
77
79
|
",<TOD>。", text)
|
|
78
80
|
return text
|
|
79
81
|
|
package/scripts/cli.py
CHANGED
|
@@ -777,7 +777,10 @@ def cmd_startup(query: str = "", budget: int | None = None,
|
|
|
777
777
|
# 水位提醒块:与 status 同一实现(memory._water_level),别两套口径。
|
|
778
778
|
try:
|
|
779
779
|
wl = memory._water_level()
|
|
780
|
-
pct
|
|
780
|
+
# _water_level 的 pct 统一是 0-1 小数(0.43=43%);stale 分支显示的是
|
|
781
|
+
# config 里的原始 0-100 值,两条口径别混。之前非 stale 分支直接 int(0.43)
|
|
782
|
+
# 得到 0%,token-meter 上线后首次走到这里才暴露。
|
|
783
|
+
pct = int(round(float(wl.get("pct", 0)) * 100))
|
|
781
784
|
if wl.get("stale"):
|
|
782
785
|
_last = wl.get("last_session_pct")
|
|
783
786
|
_age = wl.get("last_session_pct_age_hours")
|
|
@@ -1698,7 +1701,9 @@ def _main(argv=None) -> int:
|
|
|
1698
1701
|
p_forget.add_argument("id")
|
|
1699
1702
|
|
|
1700
1703
|
p_ctx = sub.add_parser("check-context", help="检查上下文水位:60%% 清点 / 75%% 做梦 / 90%% 封存")
|
|
1701
|
-
p_ctx.add_argument("--used", type=float,
|
|
1704
|
+
p_ctx.add_argument("--used", type=float, default=None,
|
|
1705
|
+
help="已用上下文百分比 0-100(缺省自动读 DSH tokenMeter 采集的 .context-pressure.json;"
|
|
1706
|
+
"读不到再退回最近一次手工记录)")
|
|
1702
1707
|
p_ctx.add_argument("--unsaved", type=float, default=0,
|
|
1703
1708
|
help="本轮还没落盘的估计字数(凭感觉估即可,用来提示'该记没记')")
|
|
1704
1709
|
p_ctx.add_argument("--handoff", action="store_true",
|
|
@@ -3665,7 +3670,7 @@ def _main(argv=None) -> int:
|
|
|
3665
3670
|
+ (f";上次记录 {wl['last_session_pct']}%"
|
|
3666
3671
|
+ (f",{wl['last_session_pct_age_hours']}h 前" if wl.get("last_session_pct_age_hours") is not None else "")
|
|
3667
3672
|
if wl.get("last_session_pct") is not None else "")
|
|
3668
|
-
+ ")") if wl.get("stale") else f"水位 {wl['pct']:.0f}%"
|
|
3673
|
+
+ ")") if wl.get("stale") else f"水位 {wl['pct'] * 100:.0f}%"
|
|
3669
3674
|
print(f"{_pct_txt}(清点 {th['review']:.0%} / 做梦 {th['dream']:.0%} / "
|
|
3670
3675
|
f"封存 {th['escape']:.0%})· 级别 {wl['level']}")
|
|
3671
3676
|
print(f" 各层条数:{wl['counts']}")
|
|
@@ -4405,7 +4410,23 @@ def _main(argv=None) -> int:
|
|
|
4405
4410
|
print(f"交接笔记已落盘:{rec.get('id', '')}(notes 里 kind=log,startup 会带出来)")
|
|
4406
4411
|
return 0
|
|
4407
4412
|
cfg = load_config()
|
|
4408
|
-
|
|
4413
|
+
# v3.86:--used 缺省时按优先级取真实水位——
|
|
4414
|
+
# 1) DSH tokenMeter 采集的 .context-pressure.json(准确、每轮更新);
|
|
4415
|
+
# 2) 最近一次 check-context 手工记下的 last_session_pct;
|
|
4416
|
+
# 3) 都没有才报错要求显式给 --used(不再瞎猜)。
|
|
4417
|
+
_tp = memory.token_context_pressure()
|
|
4418
|
+
if args.used is not None:
|
|
4419
|
+
used = float(args.used)
|
|
4420
|
+
elif _tp:
|
|
4421
|
+
used = _tp["pct"]
|
|
4422
|
+
print(f"[水位] 来源:DSH tokenMeter({_tp['pressure_tokens']}/{_tp['context_window']} tokens)")
|
|
4423
|
+
elif _cfg_num(cfg, "last_session_pct", -1) >= 0:
|
|
4424
|
+
used = _cfg_num(cfg, "last_session_pct")
|
|
4425
|
+
print("[水位] 来源:最近一次手工 check-context(DSH tokenMeter 未采集到)")
|
|
4426
|
+
else:
|
|
4427
|
+
print("[错误] 拿不到上下文水位:请先让 DSH 跑一轮(tokenMeter 会采集),"
|
|
4428
|
+
"或显式传 --used <百分比>", file=sys.stderr)
|
|
4429
|
+
return 1
|
|
4409
4430
|
# 记下本次水位:status --json / startup 水位提醒块的"最近一次 session 结束水位"
|
|
4410
4431
|
if not READONLY:
|
|
4411
4432
|
try:
|
package/scripts/core.py
CHANGED
|
@@ -1135,7 +1135,38 @@ def read_pressure(cfg: dict | None = None) -> dict | None:
|
|
|
1135
1135
|
返回形状:`{"pct": 78.0, "at": 1795240000, "stale": False, "tier": "lazy"|"minimal"|"full", ...}`
|
|
1136
1136
|
"""
|
|
1137
1137
|
try:
|
|
1138
|
+
# 先读老机制文件:手工 check-context --used 写的值若新鲜,应**优先于**
|
|
1139
|
+
# tokenMeter——用户显式给的数就是"我比你准"。平时它陈旧/不存在,才用 token。
|
|
1140
|
+
legacy = None
|
|
1138
1141
|
path = HOME / _PRESSURE_FILE
|
|
1142
|
+
if path.exists():
|
|
1143
|
+
try:
|
|
1144
|
+
raw = json.loads(path.read_text(encoding="utf-8-sig"))
|
|
1145
|
+
if isinstance(raw, dict):
|
|
1146
|
+
_lp, _la = raw.get("pct"), raw.get("at")
|
|
1147
|
+
if (not isinstance(_lp, bool) and isinstance(_lp, (int, float))
|
|
1148
|
+
and 0 <= float(_lp) <= 100
|
|
1149
|
+
and isinstance(_la, (int, float)) and not isinstance(_la, bool)
|
|
1150
|
+
and now_ts() - float(_la) <= _PRESSURE_TTL):
|
|
1151
|
+
legacy = {"pct": float(_lp), "at": float(_la),
|
|
1152
|
+
"source": str(raw.get("source") or "manual")}
|
|
1153
|
+
except Exception:
|
|
1154
|
+
pass
|
|
1155
|
+
tp = token_context_pressure()
|
|
1156
|
+
if legacy is not None:
|
|
1157
|
+
th = pressure_thresholds(cfg if cfg is not None else load_config())
|
|
1158
|
+
pct = legacy["pct"]
|
|
1159
|
+
tier = ("minimal" if pct >= th["minimal_at"]
|
|
1160
|
+
else "lazy" if pct >= th["lazy_at"] else "full")
|
|
1161
|
+
return {"pct": pct, "at": legacy["at"], "stale": False, "tier": tier,
|
|
1162
|
+
"thresholds": th, "source": legacy["source"]}
|
|
1163
|
+
if tp is not None:
|
|
1164
|
+
th = pressure_thresholds(cfg if cfg is not None else load_config())
|
|
1165
|
+
pct = float(tp["pct"])
|
|
1166
|
+
tier = ("minimal" if pct >= th["minimal_at"]
|
|
1167
|
+
else "lazy" if pct >= th["lazy_at"] else "full")
|
|
1168
|
+
return {"pct": pct, "at": tp["ts"], "stale": False, "tier": tier,
|
|
1169
|
+
"thresholds": th, "source": "token-meter"}
|
|
1139
1170
|
if not path.exists():
|
|
1140
1171
|
return None
|
|
1141
1172
|
raw = json.loads(path.read_text(encoding="utf-8-sig"))
|
|
@@ -1180,6 +1211,39 @@ def write_pressure(pct: float, source: str = "") -> None:
|
|
|
1180
1211
|
log_event("pressure", f"写上下文压力失败:{exc}", level="warn")
|
|
1181
1212
|
|
|
1182
1213
|
|
|
1214
|
+
# v3.86:DSH tokenMeter 采集的准确上下文压力(由 startup 插件写)。
|
|
1215
|
+
# 与 _pressure.json(灵枢自己按 check-context 记的降档档位)分开——
|
|
1216
|
+
# 这个文件是 DSH 官方 token 计量的只读快照,用来替代"没有实时上下文可测"的缺口。
|
|
1217
|
+
_TOKEN_PRESSURE_FILE = ".context-pressure.json"
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
def token_context_pressure(max_age_sec: int = 600) -> dict | None:
|
|
1221
|
+
"""读 DSH 插件写的 .context-pressure.json,返回 {pct, pressure_tokens, context_window, ts}。
|
|
1222
|
+
|
|
1223
|
+
找不到 / 过期 / 字段无效一律返回 None(调用方再退回旧逻辑)。
|
|
1224
|
+
max_age_sec 默认 600s:超过 10 分钟视为陈旧(会话可能已换),不采信。
|
|
1225
|
+
"""
|
|
1226
|
+
try:
|
|
1227
|
+
p = HOME / _TOKEN_PRESSURE_FILE
|
|
1228
|
+
if not p.is_file():
|
|
1229
|
+
return None
|
|
1230
|
+
d = json.loads(p.read_text(encoding="utf-8-sig"))
|
|
1231
|
+
if not isinstance(d, dict) or not d.get("available"):
|
|
1232
|
+
return None
|
|
1233
|
+
ts = _num(d.get("ts"), 0)
|
|
1234
|
+
if ts <= 0 or (now_ts() - ts) > max(0, int(max_age_sec)):
|
|
1235
|
+
return None
|
|
1236
|
+
pct = _num(d.get("pct"), -1)
|
|
1237
|
+
if not (0 <= pct <= 100):
|
|
1238
|
+
return None
|
|
1239
|
+
return {"pct": round(pct, 1),
|
|
1240
|
+
"pressure_tokens": int(_num(d.get("pressure_tokens"), 0)),
|
|
1241
|
+
"context_window": int(_num(d.get("context_window"), 0)),
|
|
1242
|
+
"ts": int(ts)}
|
|
1243
|
+
except Exception:
|
|
1244
|
+
return None
|
|
1245
|
+
|
|
1246
|
+
|
|
1183
1247
|
def clear_pressure() -> None:
|
|
1184
1248
|
"""清掉压力标记(回到满档)。"""
|
|
1185
1249
|
if READONLY:
|
package/scripts/memory.py
CHANGED
|
@@ -1083,9 +1083,15 @@ def _water_level(used_pct: float | None = None) -> dict:
|
|
|
1083
1083
|
last_pct_at = _cfg_num(cfg, "last_session_pct_at", 0) # 那次记录的时间戳(v3.23)
|
|
1084
1084
|
|
|
1085
1085
|
used_given = used_pct is not None
|
|
1086
|
+
# v3.86:优先用 DSH tokenMeter 的准实时快照(每轮请求前采集),其次是显式
|
|
1087
|
+
# --used,最后才是 config 里的历史手工值。只有显式或 token-meter 算"非 stale"。
|
|
1088
|
+
_tp = None if used_given else token_context_pressure()
|
|
1086
1089
|
if used_given:
|
|
1087
1090
|
pct = float(used_pct)
|
|
1088
1091
|
source = "explicit"
|
|
1092
|
+
elif _tp:
|
|
1093
|
+
pct = _tp["pct"]
|
|
1094
|
+
source = "token-meter"
|
|
1089
1095
|
elif last_session_pct is not None:
|
|
1090
1096
|
pct = float(_cfg_num(cfg, "last_session_pct", 0))
|
|
1091
1097
|
source = "config" # 历史残留值,不是实时测量
|
|
@@ -1093,9 +1099,10 @@ def _water_level(used_pct: float | None = None) -> dict:
|
|
|
1093
1099
|
pct = 0.0
|
|
1094
1100
|
source = "default"
|
|
1095
1101
|
# v3.23:非实时输入一律标 stale —— 旧版把几天前的 last_session_pct 当"当前水位"返回,
|
|
1096
|
-
# 比没有更危险(看到 60% 会以为刚刚测过)。
|
|
1097
|
-
stale = not used_given
|
|
1098
|
-
age_hours = round((now -
|
|
1102
|
+
# 比没有更危险(看到 60% 会以为刚刚测过)。token-meter 是准实时,不算 stale。
|
|
1103
|
+
stale = not used_given and not _tp
|
|
1104
|
+
age_hours = round((now - _tp["ts"]) / 3600, 1) if _tp else (
|
|
1105
|
+
round((now - last_pct_at) / 3600, 1) if (stale and last_pct_at) else None)
|
|
1099
1106
|
if pct > 1.0:
|
|
1100
1107
|
pct = pct / 100.0 # 兼容 0-100 口径:--used 65 与 0.65 等价
|
|
1101
1108
|
review = _cfg_num(cfg, "review_threshold", 0.60)
|