vod-devkit 0.3.0 → 0.4.2

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.
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env python3
2
+ """D-pending-1 无通道场景 pending 反馈队列(2026-11-09,方案 A 四条执行细节定稿)。
3
+
4
+ 背景:宿主 VOD 插件未安装时,vod-feedback-collect skill 兜底把「用户已确认
5
+ 意愿的反馈」落盘为 pending,等插件/通道恢复后由 submit_voices 顺手 flush。
6
+ 此前「暂存」只存在于会话输出(全盘 grep 零落盘实锤),会话结束=有记录的丢失。
7
+
8
+ 四条定稿口径:
9
+ 1. 幂等键 = sha1(content + confirmAt + source):**条目级去重**(同一条 flush
10
+ 两次可识别);**内容级去重由引擎 uk_voice_id 负责**(voiceId=f(source,
11
+ content,nonce),同内容重提=skipped)——两 层分工,禁止混用。
12
+ 2. flush 清理 = 重写 + 原子 rename(tmp+os.replace):文件里永远只有真正
13
+ 待提交的条目;失败的条目保留 status=pending,不因尝试过就标已处理。
14
+ flush 同步执行但失败只记日志、不回滚主提交(文件个位数行,毫秒级;
15
+ 异步留作扩展点,当前规模不值得引入并发写竞态)。
16
+ 3. echo 两场景(skill 层口径,本模块只承载 echoShown 标记):能拿到用户
17
+ 输入原文 → 落盘前比对不一致以原文为准;拿不到 → echoShown=true,
18
+ 承认是概率性防线非确定性闸非安全边界。
19
+ 4. 生命周期:createdAt 超过 EXPIRE_DAYS(30) 的条目 flush 时标记 expired
20
+ 并从文件剔除(不搞定时任务);单次 flush 上限 CAP=100 条(按 createdAt
21
+ 最早优先),文件过大是已知扩展点。
22
+
23
+ 双通道消息分离:用户侧只见「已暂存,恢复后自动补交」,路径/profile/
24
+ 幂等键/flush 结果全部只进日志。
25
+ """
26
+ import datetime
27
+ import hashlib
28
+ import json
29
+ import os
30
+ from pathlib import Path
31
+ from typing import Any
32
+
33
+ PENDING_DIRNAME = ".vod-devkit"
34
+ PENDING_FILENAME = "pending-feedback.jsonl"
35
+ CAP = 100
36
+ EXPIRE_DAYS = 30
37
+ STATUS_PENDING = "pending"
38
+ STATUS_DONE = "done"
39
+ STATUS_EXPIRED = "expired"
40
+
41
+
42
+ def default_path() -> Path:
43
+ return Path(os.path.expanduser("~")) / PENDING_DIRNAME / PENDING_FILENAME
44
+
45
+
46
+ def entry_key(content: str, confirm_at: str, source: str) -> str:
47
+ """条目级幂等键(内容级去重由引擎 uk_voice_id 负责,见模块 docstring)。"""
48
+ return hashlib.sha1(
49
+ (str(content) + "\x00" + str(confirm_at) + "\x00" + str(source)).encode("utf-8")
50
+ ).hexdigest()
51
+
52
+
53
+ def make_entry(content: str, tags: list | None, confirm_at: str, source: str,
54
+ echo_shown: bool = False, original_lines: list | None = None) -> dict:
55
+ now = datetime.datetime.now().isoformat(timespec="seconds")
56
+ return {
57
+ "idempotencyKey": entry_key(content, confirm_at, source),
58
+ "content": str(content),
59
+ "tags": list(tags or []),
60
+ "confirmAt": str(confirm_at),
61
+ "createdAt": now,
62
+ "source": str(source),
63
+ "echoShown": bool(echo_shown),
64
+ "status": STATUS_PENDING,
65
+ **({"originalLines": [str(x) for x in original_lines]}
66
+ if original_lines else {}),
67
+ }
68
+
69
+
70
+ def canonical_content(entry: dict) -> str:
71
+ """权威 content:带 originalLines 的条目以分行原文 `\n` 拼接为真源
72
+ (S73 判定例的技术锁——模型拼接时新增的标点/连接词在此被机械剥除,
73
+ 实锤:模型把两行无标点原话拼成「差劲。使用…不流畅。」)。无
74
+ originalLines 的条目回落 entry 自身 content(行为不变)。"""
75
+ lines = entry.get("originalLines")
76
+ if isinstance(lines, list) and lines:
77
+ return "\n".join(str(x) for x in lines)
78
+ return str(entry.get("content", ""))
79
+
80
+
81
+ def load_entries(path: Path) -> list[dict]:
82
+ """读全量(坏行容忍跳过——手工写入/中断不炸 flush)。"""
83
+ if not path.exists():
84
+ return []
85
+ out = []
86
+ for line in path.read_text(encoding="utf-8").splitlines():
87
+ line = line.strip()
88
+ if not line:
89
+ continue
90
+ try:
91
+ obj = json.loads(line)
92
+ if isinstance(obj, dict):
93
+ out.append(obj)
94
+ except (ValueError, TypeError):
95
+ continue
96
+ return out
97
+
98
+
99
+ def append_entry(path: Path, entry: dict) -> None:
100
+ """追加一行(原子性由「单行一次 write+flush」保证;坏行由 load 容忍)。"""
101
+ path.parent.mkdir(parents=True, exist_ok=True)
102
+ with open(path, "a", encoding="utf-8") as f:
103
+ f.write(json.dumps(entry, ensure_ascii=False) + "\n")
104
+ f.flush()
105
+ os.fsync(f.fileno())
106
+
107
+
108
+ def save_entries(path: Path, entries: list[dict]) -> None:
109
+ """重写 + 原子 rename(D-pending-1 口径 2:tmp + os.replace)。"""
110
+ path.parent.mkdir(parents=True, exist_ok=True)
111
+ tmp = path.with_suffix(path.suffix + ".tmp")
112
+ with open(tmp, "w", encoding="utf-8") as f:
113
+ for e in entries:
114
+ f.write(json.dumps(e, ensure_ascii=False) + "\n")
115
+ f.flush()
116
+ os.fsync(f.fileno())
117
+ os.replace(tmp, path)
118
+
119
+
120
+ def plan_flush(entries: list[dict], now: datetime.datetime | None = None,
121
+ cap: int = CAP, expire_days: int = EXPIRE_DAYS) -> dict[str, Any]:
122
+ """flush 计划(纯函数):to_flush(pending、未过期、最早优先、上限 cap)、
123
+ expired(超期条目,调用方剔除+告知)、kept(留在文件里的其余条目)。"""
124
+ now = now or datetime.datetime.now()
125
+ deadline = now - datetime.timedelta(days=expire_days)
126
+ pending, expired = [], []
127
+ for e in entries:
128
+ created = str(e.get("createdAt", ""))[:19]
129
+ try:
130
+ created_dt = datetime.datetime.fromisoformat(created)
131
+ except ValueError:
132
+ created_dt = now # 无 createdAt 的手工行不误杀
133
+ if e.get("status") == STATUS_PENDING and created_dt < deadline:
134
+ expired.append(e)
135
+ elif e.get("status") == STATUS_PENDING:
136
+ pending.append(e)
137
+ pending.sort(key=lambda e: str(e.get("createdAt", "")) or "9999-12-31T23:59:59")
138
+ return {"to_flush": pending[:cap], "expired": expired,
139
+ "kept": [e for e in pending[cap:]]}
package/vod_mcp/policy.py CHANGED
@@ -23,7 +23,7 @@ def _resolve_ref(policy: dict, value):
23
23
  return value
24
24
 
25
25
 
26
- def _match_tool_rule(rule: dict, tool: str, params: dict) -> bool:
26
+ def _match_tool_rule(policy: dict, rule: dict, tool: str, params: dict) -> bool:
27
27
  m = rule["match"]
28
28
  if m.get("tool") != tool:
29
29
  return False
@@ -36,7 +36,7 @@ def _match_tool_rule(rule: dict, tool: str, params: dict) -> bool:
36
36
  if "equals" in m:
37
37
  return actual == m["equals"]
38
38
  if "gt" in m:
39
- limit = _resolve_ref(load_policy(), m["gt"])
39
+ limit = _resolve_ref(policy, m["gt"])
40
40
  try:
41
41
  if isinstance(actual, list):
42
42
  return len(actual) > int(limit)
@@ -44,7 +44,7 @@ def _match_tool_rule(rule: dict, tool: str, params: dict) -> bool:
44
44
  except (TypeError, ValueError):
45
45
  return False
46
46
  if "notIn" in m:
47
- whitelist = _resolve_ref(load_policy(), m["notIn"])
47
+ whitelist = _resolve_ref(policy, m["notIn"])
48
48
  if not isinstance(whitelist, (list, tuple)):
49
49
  return False
50
50
  return actual not in whitelist
@@ -56,7 +56,7 @@ def check_tool_call(policy: dict, tool: str, params: dict) -> list:
56
56
  hits = []
57
57
  for rule in policy.get("rules", []):
58
58
  if "tool" in rule.get("match", {}):
59
- if _match_tool_rule(rule, tool, params):
59
+ if _match_tool_rule(policy, rule, tool, params):
60
60
  hits.append(rule)
61
61
  return hits
62
62
 
package/vod_mcp/tools.py CHANGED
@@ -30,12 +30,17 @@ _FOLLOWUP_DECLINES = frozenset({
30
30
 
31
31
  def _followup_fields(voice: dict) -> dict:
32
32
  """D-followup-impl-2 单源提取(submit 主路径与 draft 暂存共用,禁双边漂移):
33
- 仅 trim 非空且非拒答词才含键;引擎同口径截 2000;内容永不回流 content。"""
33
+ 仅 trim 非空且非拒答词才含键;引擎同口径截 2000;内容永不回流 content。
34
+ followupStatus(评审八点 #3 引擎侧承载的插件上报):三态白名单归一
35
+ (strip+lower),白名单外/缺失省键——引擎侧降 NULL 不拒收,插件侧同口径双保险。"""
34
36
  out = {}
35
37
  for _f in ("reproSteps", "expectedBehavior"):
36
38
  _val = str(voice.get(_f) or "").strip()
37
39
  if _val and _val.lower() not in _FOLLOWUP_DECLINES:
38
40
  out[_f] = _val[:2000]
41
+ _st = str(voice.get("followupStatus") or "").strip().lower()
42
+ if _st in ("not_asked", "declined", "answered"):
43
+ out["followupStatus"] = _st
39
44
  return out
40
45
 
41
46
  _CONTRACT_PATH = Path(__file__).resolve().parent.parent / "contract" / "tools.schema.json"
@@ -221,6 +226,13 @@ def _install_nonce() -> str:
221
226
  if not n:
222
227
  import uuid
223
228
  n = uuid.uuid4().hex[:8]
229
+ # 首装 nonce 必须同步回写内存态 _RT(D-dev-nonce-sync,2026-11-09 真机实锤):
230
+ # _RT 仅模块导入时加载一次,_save_runtime 只落盘——不同步则同进程内每次调用
231
+ # 生成新 nonce:领令牌(设备 A)与提交(设备 B)设备绑定校验失败,重试仍
232
+ # 新 nonce(设备 C)再 401——全新安装首次提交必失败冒给用户(存量安装
233
+ # runtime.json 已有 nonce 不受影响)。同步后领令牌/提交同设备,401→领令牌
234
+ # →重试全程进程内无感。
235
+ _RT["install_nonce"] = n
224
236
  try:
225
237
  _save_runtime({"install_nonce": n})
226
238
  except Exception:
@@ -238,6 +250,15 @@ def _voice_id(source: str, content: str, nonce: str) -> str:
238
250
  return f"ext_{source}_{hashlib.sha1((content + nonce).encode('utf-8')).hexdigest()[:12]}"
239
251
 
240
252
 
253
+ def _legacy_key_hint() -> str:
254
+ """旧版残留凭据自愈提示(评审拍板项):出错时给可操作路径,不发公告靠用户撞。
255
+ 含当前插件版本与重装命令——重装会刷新 runtime/清掉旧 key 残留。"""
256
+ version = getattr(__import__("vod_mcp"), "__version__", "unknown")
257
+ return ("若为旧版安装残留凭据:更新插件(当前 v" + version + ")并重装即可清除——"
258
+ "npx vod-devkit@latest install --target <宿主> --product <产品名>;"
259
+ "或移除 VOD_JAVA_API_KEY 环境变量走匿名档")
260
+
261
+
241
262
  def _explicit_key() -> bool:
242
263
  """当前 JAVA_KEY 是否为显式配置(正式/宿主 key)而非本会话自动领取的匿名令牌。
243
264
 
@@ -251,6 +272,133 @@ def _explicit_key() -> bool:
251
272
  return bool(JAVA_KEY) and str(_RT.get("token_tier") or "") != "anonymous"
252
273
 
253
274
 
275
+ # ── D-pending-1:无通道期 pending 反馈 flush(方案 A,2026-11-09)──
276
+
277
+ def _flush_pending(product):
278
+ """补提交 ~/.vod-devkit/pending-feedback.jsonl 里 status=pending 的条目。
279
+
280
+ 口径(D-pending-1 定稿):同步执行但失败只记日志、绝不回滚主提交;
281
+ 幂等键=条目级(sha1(content+confirmAt+source)),内容级去重由引擎
282
+ uk_voice_id 承担(同内容重提=skipped);重写+原子 rename,失败条目
283
+ 保留 pending;单次上限 100 条最早优先,超 30 天标记 expired 剔除。
284
+ 双通道消息分离:本函数结果只进工具内部诊断(result.pendingFlush),
285
+ 用户侧话术由 skill 层承担(已暂存/自动补交,禁止出现路径)。
286
+ """
287
+ from vod_mcp import pending as _pq
288
+ path = _pq.default_path()
289
+ entries = _pq.load_entries(path)
290
+ if not entries:
291
+ return {"scanned": 0}
292
+ plan = _pq.plan_flush(entries)
293
+ kept = list(plan["kept"])
294
+ flushed, failed, corrected = 0, 0, 0
295
+ for e in plan["to_flush"]:
296
+ content = _pq.canonical_content(e) # D-pending-1 补强:originalLines 权威拼接,剥模型加的标点
297
+ if content != str(e.get("content", "")):
298
+ corrected += 1
299
+ _log("pending flush: content 与 originalLines 拼接不一致,以 originalLines 为准(剥除模型拼接改动)")
300
+ hv = {"title": str(content)[:60], "content": str(content)}
301
+ if e.get("tags"):
302
+ hv["tags"] = e["tags"]
303
+ body = {"voices": [hv]}
304
+ if product:
305
+ body["product"] = product
306
+ try:
307
+ out = _http("POST", _SUBMIT_HOARDING, body, base=JAVA_BASE, key=JAVA_KEY)
308
+ ids = (out or {}).get("voiceIds") or ((out or {}).get("data") or {}).get("voiceIds")
309
+ if ids is None:
310
+ failed += 1
311
+ kept.append(e)
312
+ _log("pending flush: 回执未含 voiceIds,条目保留")
313
+ else:
314
+ flushed += 1 # 含幂等 skipped(同内容重提=引擎 skipped,零重复)
315
+ except Exception as err:
316
+ failed += 1
317
+ kept.append(e)
318
+ _log(f"pending flush failed (kept pending): {err}")
319
+ _pq.save_entries(path, kept)
320
+ return {"scanned": len(entries), "flushed": flushed,
321
+ "failed": failed, "expired": len(plan["expired"]),
322
+ "corrected": corrected}
323
+
324
+
325
+
326
+ def _collect_voice_items(params: dict) -> tuple[list, list]:
327
+ """校验段:content 非空+≤2000 字(契约 maxLength,防超大 payload)。
328
+ 返回 (items[(index, voice, content)], rejected 明细)。"""
329
+ items, rejected = [], []
330
+ for i, v in enumerate(params.get("voices", [])):
331
+ content = str(v.get("content", "")).strip()
332
+ if not content:
333
+ rejected.append({"index": i, "reason": "content 为空"})
334
+ elif len(content) > 2000:
335
+ rejected.append({"index": i, "reason": f"content 超 2000 字({len(content)})"})
336
+ else:
337
+ items.append((i, v, content))
338
+ return items, rejected
339
+
340
+
341
+ def _echo_plan(items: list, vids: list) -> dict:
342
+ """回显段(方案 A,2026-11-06 拍板):提交回执强制携带原话回显。
343
+ 检测型防线——宿主模型改写用户原话(实锤:"体验不好"→"不友好")
344
+ 在回执处当场可见;agent 必须原样转述,改写露馅。"""
345
+ ok = [(vid, items[min(i, len(items) - 1)][2])
346
+ for i, vid in enumerate(vids) if vid]
347
+ return {
348
+ "requiresUserEcho": bool(ok),
349
+ "instruction": ("请向用户原样转述已收录内容:每条以「已收录原话:」开头,"
350
+ "后接 content 全文。禁止改写、概括、增删字词。"),
351
+ "contents": [{"voiceId": vid, "content": c} for vid, c in ok],
352
+ }
353
+
354
+
355
+ def _ledger_submit(items: list, vids: list, tids: list, product: str | None,
356
+ extra: dict | None = None) -> None:
357
+ """账本段:回执 voiceIds/taskIds 逐条落本地账本(层级①查询的源)。"""
358
+ import time as _time
359
+ entries = [{"voiceId": vid, "taskId": tid,
360
+ "title": items[min(i, len(items) - 1)][2][:40],
361
+ "product": product, "at": _time.strftime("%Y-%m-%dT%H:%M:%S")}
362
+ for i, (vid, tid) in enumerate(zip(vids, tids))]
363
+ if extra:
364
+ for e in entries:
365
+ e.update(extra) # 两段式:stage=committed + draftId 关联(验收③)
366
+ _ledger_append(entries)
367
+
368
+
369
+ def _hoard_voice(v: dict, content: str) -> dict:
370
+ """载荷段:单条囤积端点 voice 载荷(title/content + tags/userType +
371
+ D-followup-impl-2 追问补充透传:单源 _followup_fields,拒答词集兜底)。"""
372
+ hv = {"title": content[:60], "content": content}
373
+ if v.get("tags"): # 采集层三分类(problem_type:值)→正式入库
374
+ hv["tags"] = v["tags"]
375
+ if v.get("userType"):
376
+ hv["userType"] = v["userType"]
377
+ hv.update(_followup_fields(v))
378
+ return hv
379
+
380
+
381
+ def _raise_explicit_key_rejected(key: str) -> None:
382
+ """显式 key(安装注入/正式档)被引擎 401 拒绝 → fail fast:
383
+ 不取匿名令牌、不覆盖配置——静默降级匿名档=配置错误被吞
384
+ (归因错位比报错更糟,同 AIShell 错归病根)。囤积/legacy 两路径同口径。"""
385
+ raise ToolError(
386
+ f"注入的 key 被引擎拒绝(指纹 {_fingerprint(key)}):"
387
+ "请核对 key 是否过期/被吊销,或移除 key 走匿名档。"
388
+ + _legacy_key_hint(),
389
+ code=-32012)
390
+
391
+
392
+ def _acquire_anon_token_or_raise() -> str:
393
+ """缺 key/匿名令牌失效 → 领零字段匿名令牌;端点不可用则如实报错。"""
394
+ token = _fetch_anon_token()
395
+ if not token:
396
+ raise ToolError(
397
+ "未配置接入 key 且匿名令牌端点不可用。引导:"
398
+ + ";".join(g["label"] for g in _KEY_GUIDANCE), code=-32012)
399
+ return token
400
+
401
+
254
402
  def submit_voices(params: dict, client_info: dict | None = None,
255
403
  ledger_extra: dict | None = None) -> dict:
256
404
  """V16 切换:主路径=囤积端点 POST /external/voices(Bearer,批式,不触发分析);
@@ -266,46 +414,12 @@ def submit_voices(params: dict, client_info: dict | None = None,
266
414
  import uuid
267
415
  host = (client_info or {}).get("name") or "unknown-host"
268
416
  product, ignored_runtime = _resolve_product(params)
269
- accepted, voice_ids, task_ids, rejected = 0, [], [], []
270
417
  global JAVA_KEY
271
- voices_in = params.get("voices", [])
272
- items = [] # (index, voice_map, content)
273
- for i, v in enumerate(voices_in):
274
- content = str(v.get("content", "")).strip()
275
- if not content:
276
- rejected.append({"index": i, "reason": "content 为空"})
277
- continue
278
- if len(content) > 2000: # 契约 maxLength 2000:内容边界(防超大 payload)
279
- rejected.append({"index": i, "reason": f"content 超 2000 字({len(content)})"})
280
- continue
281
- items.append((i, v, content))
418
+ accepted, voice_ids, task_ids = 0, [], []
419
+ items, rejected = _collect_voice_items(params)
282
420
  if not items:
283
421
  return {"accepted": 0, "voiceIds": [], "taskIds": [], "rejected": rejected}
284
422
 
285
- def _echo(vids):
286
- """方案 A(2026-11-06 拍板):提交回执强制携带原话回显。
287
- 检测型防线——宿主模型改写用户原话(实锤:"体验不好"→"不友好")
288
- 在回执处当场可见;agent 必须原样转述,改写露馅。"""
289
- ok = [(vid, items[min(i, len(items) - 1)][2])
290
- for i, vid in enumerate(vids) if vid]
291
- return {
292
- "requiresUserEcho": bool(ok),
293
- "instruction": ("请向用户原样转述已收录内容:每条以「已收录原话:」开头,"
294
- "后接 content 全文。禁止改写、概括、增删字词。"),
295
- "contents": [{"voiceId": vid, "content": c} for vid, c in ok],
296
- }
297
-
298
- def _ledger(vids, tids):
299
- import time as _time
300
- entries = [{"voiceId": vid, "taskId": tid,
301
- "title": items[min(i, len(items) - 1)][2][:40],
302
- "product": product, "at": _time.strftime("%Y-%m-%dT%H:%M:%S")}
303
- for i, (vid, tid) in enumerate(zip(vids, tids))]
304
- if ledger_extra:
305
- for e in entries:
306
- e.update(ledger_extra) # 两段式:stage=committed + draftId 关联(验收③)
307
- _ledger_append(entries)
308
-
309
423
  def _legacy_submit(i, v, content):
310
424
  """过渡回落:老链单条提交(提交即分析语义,仅 404 触发)。"""
311
425
  nonce = uuid.uuid4().hex[:8] # 调用级:重试复用,跨调用不重复
@@ -324,20 +438,7 @@ def submit_voices(params: dict, client_info: dict | None = None,
324
438
  return vid, (out.get("data") or {}).get("taskId")
325
439
 
326
440
  # ---- 主路径:囤积端点批式提交 ----
327
- hoard_voices = []
328
- for i, v, content in items:
329
- hv = {"title": content[:60], "content": content}
330
- tags = v.get("tags") # 采集层三分类(problem_type:值)→【类型名】标题前缀正式入库
331
- if tags:
332
- hv["tags"] = tags
333
- ut = v.get("userType")
334
- if ut:
335
- hv["userType"] = ut
336
- # D-followup-impl-2 追问补充透传:单源 _followup_fields(拒答词集兜底+
337
- # 引擎同口径截 2000+禁回流 content;未追问/答"没有"=键集合零漂移)
338
- hv.update(_followup_fields(v))
339
- hoard_voices.append(hv)
340
- body = {"voices": hoard_voices}
441
+ body = {"voices": [_hoard_voice(v, content) for _, v, content in items]}
341
442
  if product: # 无归因不带 product(引擎 UNKNOWN 档),不本地拦截
342
443
  body["product"] = product
343
444
  out = None
@@ -349,20 +450,9 @@ def submit_voices(params: dict, client_info: dict | None = None,
349
450
  fallback = True # 囤积端点未部署 → 落到下方 legacy 回落
350
451
  elif not JAVA_KEY or ex.code == -32012:
351
452
  if _explicit_key():
352
- # 显式 key(安装注入/正式档)被引擎 401 拒绝 → fail fast:
353
- # 不取匿名令牌、不覆盖配置——静默降级匿名档=配置错误被吞
354
- #(归因错位比报错更糟,同 AIShell 错归病根)
355
- raise ToolError(
356
- f"注入的 key 被引擎拒绝(指纹 {_fingerprint(JAVA_KEY)}):"
357
- "请核对 key 是否过期/被吊销,或移除 key 走匿名档(删除 "
358
- "VOD_JAVA_API_KEY 环境变量与 runtime.json java_key)",
359
- code=-32012)
453
+ _raise_explicit_key_rejected(JAVA_KEY)
360
454
  # 缺 key/匿名令牌失效 → 零字段匿名令牌 → 落 runtime.json → 重试一次
361
- token = _fetch_anon_token()
362
- if not token:
363
- raise ToolError(
364
- "未配置接入 key 且匿名令牌端点不可用。引导:" +
365
- ";".join(g["label"] for g in _KEY_GUIDANCE), code=-32012)
455
+ token = _acquire_anon_token_or_raise()
366
456
  JAVA_KEY = token
367
457
  _save_runtime({"java_key": token, "token_tier": "anonymous"})
368
458
  out = _http("POST", _SUBMIT_HOARDING, body, base=JAVA_BASE, key=JAVA_KEY)
@@ -382,22 +472,23 @@ def submit_voices(params: dict, client_info: dict | None = None,
382
472
  # 回执缺失/整批被拒 → 不得兜底计成功(实锤缺陷:空 voiceId 记成功
383
473
  # 且宿主模型据此谎报"已提交")。accepted 以真实回执为准。
384
474
  accepted = 0
385
- voice_ids.extend([""] * len(hoard_voices))
475
+ voice_ids.extend([""] * len(items))
386
476
  if not engine_rejected:
387
477
  engine_rejected = [{"reason": "引擎回执未含 voiceIds(提交结果未知),请勿视为已收录"}]
388
- task_ids.extend([None] * len(hoard_voices)) # 囤积语义:无即时任务
478
+ task_ids.extend([None] * len(items)) # 囤积语义:无即时任务
389
479
  result = {"accepted": accepted, "voiceIds": voice_ids,
390
480
  "taskIds": task_ids, "rejected": rejected + engine_rejected,
391
481
  "analysis": "deferred", # 囤积语义明示(V6 运营批量消费)
392
482
  "path": "hoarding",
393
- "echo": _echo(voice_ids)}
483
+ "echo": _echo_plan(items, voice_ids)}
394
484
  if out.get("productRegistered") is not None:
395
485
  result["productRegistered"] = out.get("productRegistered")
396
486
  # D-attrib-A 可观测:模型试图改归因留痕,不静默吞
397
487
  result["productSource"] = "install_binding"
398
488
  if ignored_runtime:
399
489
  result["ignoredRuntimeProduct"] = ignored_runtime
400
- _ledger(voice_ids, task_ids)
490
+ _ledger_submit(items, voice_ids, task_ids, product, ledger_extra)
491
+ result["pendingFlush"] = _flush_pending(product)
401
492
  return result
402
493
  # ---- 过渡回落:老链单条提交(仅囤积端点 404 触发) ----
403
494
  for i, v, content in items:
@@ -414,46 +505,25 @@ def submit_voices(params: dict, client_info: dict | None = None,
414
505
  task_ids.append(None)
415
506
  elif (not JAVA_KEY or e.code == -32012) and not voice_ids:
416
507
  if _explicit_key():
417
- # 显式 key 被拒 → fail fast(同囤积主路径口径,防静默降级)
418
- raise ToolError(
419
- f"注入的 key 被引擎拒绝(指纹 {_fingerprint(JAVA_KEY)}):"
420
- "请核对 key 是否过期/被吊销,或移除 key 走匿名档", code=-32012)
508
+ _raise_explicit_key_rejected(JAVA_KEY)
421
509
  # 零字段匿名令牌:缺 key/401 → 自动取令牌 → 落 runtime.json → 重试一次
422
- token = _fetch_anon_token()
423
- if token:
424
- JAVA_KEY = token
425
- _save_runtime({"java_key": token, "token_tier": "anonymous"})
426
- vid, tid = _legacy_submit(i, v, content)
427
- accepted += 1
428
- voice_ids.append(vid)
429
- task_ids.append(tid)
430
- else:
431
- raise ToolError(
432
- "未配置接入 key 且匿名令牌端点不可用。引导:" +
433
- ";".join(g["label"] for g in _KEY_GUIDANCE), code=-32012)
510
+ token = _acquire_anon_token_or_raise()
511
+ JAVA_KEY = token
512
+ _save_runtime({"java_key": token, "token_tier": "anonymous"})
513
+ vid, tid = _legacy_submit(i, v, content)
514
+ accepted += 1
515
+ voice_ids.append(vid)
516
+ task_ids.append(tid)
434
517
  else:
435
518
  rejected.append({"index": i, "reason": msg[:120]})
436
519
  result = {"accepted": accepted, "voiceIds": voice_ids,
437
520
  "taskIds": task_ids, "rejected": rejected,
438
521
  "path": "legacy-fallback",
439
- "echo": _echo(voice_ids)}
440
- _ledger(voice_ids, task_ids)
522
+ "echo": _echo_plan(items, voice_ids)}
523
+ _ledger_submit(items, voice_ids, task_ids, product, ledger_extra)
524
+ result["pendingFlush"] = _flush_pending(product)
441
525
  return result
442
526
 
443
- # 本地账本落账(层级①查询的源)
444
- import time as _time
445
- _contents = [str(v.get("content", ""))[:40]
446
- for v in params.get("voices", []) if str(v.get("content", "")).strip()]
447
- ledger_entries = [
448
- {"voiceId": vid, "taskId": tid,
449
- "title": (_contents[i] if i < len(_contents) else ""),
450
- "product": product, "at": _time.strftime("%Y-%m-%dT%H:%M:%S")}
451
- for i, (vid, tid) in enumerate(zip(voice_ids, task_ids))
452
- ]
453
- _ledger_append(ledger_entries)
454
- return {"accepted": accepted, "voiceIds": voice_ids,
455
- "taskIds": task_ids, "rejected": rejected}
456
-
457
527
 
458
528
  def _fingerprint(key: str) -> str | None:
459
529
  import hashlib
@@ -479,6 +549,12 @@ def probe_key(key: str | None = None, timeout: int = 8) -> dict:
479
549
  except ToolError as ex:
480
550
  code = ex.code
481
551
  if code == 401 or code == -32012:
552
+ # 匿名令牌 401 ≠ 配置错误:报"过期待重领"而非"rejected",
553
+ # 避免 runtime 残留过期匿名令牌时被误读为正式 key 失效
554
+ # (显式 key 被拒仍报 rejected——fail fast 口径不变)
555
+ if not _explicit_key():
556
+ return {"ok": False, "reason": "expired-anonymous", "fingerprint": fp,
557
+ "note": "匿名令牌已过期:下次提交自动重领,无需操作"}
482
558
  return {"ok": False, "reason": "rejected", "fingerprint": fp}
483
559
  return {"ok": None, "reason": "unreachable", "fingerprint": fp}
484
560
 
@@ -504,6 +580,10 @@ def auth_status(params: dict, client_info: dict | None = None) -> dict:
504
580
  if JAVA_KEY:
505
581
  # 主动探针(devkit auth init 真调 IAM 的廉价版):用户问"key 好没好"有确定答案
506
582
  out["keyCheck"] = probe_key()
583
+ if _explicit_key():
584
+ # 旧版残留凭据检测位(评审拍板项):显式 key 存活即在位提示自愈路径,
585
+ # 不等提交撞 401 才发现(codex 0.2.3 残留 key 挡匿名链的实锤教训)
586
+ out["legacyKeyHint"] = _legacy_key_hint()
507
587
  if not JAVA_KEY:
508
588
  out["guidance"] = _KEY_GUIDANCE
509
589
  out["tokenReady"] = "首次提交时将自动申请匿名令牌(零字段,日限额低)"
@@ -595,23 +675,33 @@ def _ensure_first_boot(rt_path: Path | None = None) -> dict:
595
675
  return {"action": "stamped"}
596
676
 
597
677
 
678
+ def _uptime_seconds() -> float:
679
+ """本机 uptime(秒)。独立函数:测试可 monkeypatch,消除对宿主机器的依赖
680
+ (实锤:test_clone_image_detection_warns 曾硬编码 boot_age=7 天与真机
681
+ /proc/uptime 实值比较,uptime>7 天的机器必红——环境相关测试缺陷)。"""
682
+ return float(open("/proc/uptime").read().split()[0])
683
+
684
+
598
685
  def _warn_if_cloned(cfg: dict, p: Path) -> None:
599
686
  """preset 克隆镜像检测(D-key-guard-3①延伸,仅告警不阻断):
600
687
  firstBootAt 距今显著超过本机 uptime(>1h 容差)→ 本机状态大概率来自
601
688
  母机镜像克隆——所有克隆机共享同一 device 锚点/账本,声量与配额会收敛。
602
689
  判据:firstBootAt 的"年龄" > uptime+容差 ⇒ 首启发生在"这台机器"存在之前。"""
603
690
  import time as _t
691
+ if _distribution() != "preset":
692
+ return
604
693
  try:
605
- if _distribution() != "preset":
606
- return
607
694
  boot = _t.mktime(_t.strptime(cfg["firstBootAt"], "%Y-%m-%dT%H:%M:%S"))
608
- uptime_s = float(open("/proc/uptime").read().split()[0])
609
- if _t.time() - boot > uptime_s + 3600:
610
- _warn("疑似镜像克隆机:firstBootAt 早于本机 uptime——克隆分发未清 "
611
- "runtime.json,所有克隆机共享同一设备锚点/账本。请在镜像冻结前 "
612
- "uninstall 或删除 runtime.json(装机 checklist ②)")
613
- except Exception:
614
- pass
695
+ uptime_s = _uptime_seconds()
696
+ except (KeyError, ValueError, OSError) as ex:
697
+ # 收窄捕获(原 except Exception: pass 吞掉真实异常,告警失效不可见):
698
+ # 只吞解析类异常(缺字段/格式坏/读不到 /proc),其余如实上抛
699
+ _log(f"clone 检测跳过(无法解析 firstBootAt/uptime):{ex}")
700
+ return
701
+ if _t.time() - boot > uptime_s + 3600:
702
+ _warn("疑似镜像克隆机:firstBootAt 早于本机 uptime——克隆分发未清 "
703
+ "runtime.json,所有克隆机共享同一设备锚点/账本。请在镜像冻结前 "
704
+ "uninstall 或删除 runtime.json(装机 checklist ②)")
615
705
 
616
706
 
617
707
  def _save_runtime(update: dict, replace: bool = False) -> None:
@@ -709,6 +799,11 @@ def _drafts_purge(store: dict) -> dict:
709
799
  if v.get("expiresAt", 0) > now}
710
800
 
711
801
 
802
+ def _drafts_load() -> dict:
803
+ """草稿库现读 + 过期惰性清除(draft/commit 单源共用,禁双边漂移)。"""
804
+ return _drafts_purge(_runtime_cfg().get(_DRAFT_KEY) or {})
805
+
806
+
712
807
  def draft_voices(params: dict, client_info: dict | None = None) -> dict:
713
808
  """两段式第一步:校验+暂存草稿+签发一次性令牌(不提交任何数据)。"""
714
809
  import time as _time
@@ -744,10 +839,14 @@ def draft_voices(params: dict, client_info: dict | None = None) -> dict:
744
839
  payload_sha = hashlib.sha256(
745
840
  json.dumps(payload, sort_keys=True, ensure_ascii=False).encode()).hexdigest()
746
841
 
747
- fresh = _runtime_cfg()
748
- store = _drafts_purge(fresh.get(_DRAFT_KEY) or {})
842
+ store = _drafts_load()
749
843
  store[draft_id] = {
750
844
  "payload": payload, "payloadSha256": payload_sha, "tokenHash": _sha256(token),
845
+ # D-resume-commit-1:token 明文随草稿落盘(runtime.json 0600,与 java_key
846
+ # 同级保护)。token 的威胁模型是"防载荷改写"而非"同意凭证"——落盘不放大
847
+ # 风险,换断回合可续:agent 回合中断后凭 draftId 即可恢复提交(此前
848
+ # approvalToken 只存在于 draft 响应文本,回合断即丢失,只能重开草稿)
849
+ "token": token,
751
850
  "product": product, "createdAt": _time.strftime("%Y-%m-%dT%H:%M:%S"),
752
851
  "expiresAt": _time.time() + _DRAFT_TTL,
753
852
  }
@@ -770,19 +869,22 @@ def draft_voices(params: dict, client_info: dict | None = None) -> dict:
770
869
 
771
870
 
772
871
  def commit_voices(params: dict, client_info: dict | None = None) -> dict:
773
- """两段式第二步:令牌校验→单次消费→复用 submit_voices 既有提交链。"""
872
+ """两段式第二步:令牌校验→单次消费→复用 submit_voices 既有提交链。
873
+
874
+ D-resume-commit-1:approvalToken/payloadSha256 可省略——draftId 命中本地
875
+ 未过期草稿时自动恢复(断回合续提交)。显式传入仍走原校验(传了假 sha
876
+ 照样拒),恢复路径的完整性由签发时落盘的 sha/token 保证,语义不放宽。
877
+ """
774
878
  draft_id = str(params.get("draftId", ""))
775
- token = str(params.get("approvalToken", ""))
776
- payload_sha = str(params.get("payloadSha256", ""))
777
- if not draft_id or not token or not payload_sha:
778
- raise ToolError("draftId/approvalToken/payloadSha256 为必填(取自 vod_draft_voices 返回原样透传)",
779
- code=-32602)
879
+ if not draft_id:
880
+ raise ToolError("draftId 为必填(取自 vod_draft_voices 返回原样透传)", code=-32602)
780
881
 
781
- fresh = _runtime_cfg()
782
- store = _drafts_purge(fresh.get(_DRAFT_KEY) or {})
882
+ store = _drafts_load()
783
883
  draft = store.get(draft_id)
784
884
  if draft is None:
785
885
  raise ToolError("草稿不存在或已过期(10 分钟 TTL),请重新 vod_draft_voices", code=-32011)
886
+ token = str(params.get("approvalToken", "")) or str(draft.get("token", ""))
887
+ payload_sha = str(params.get("payloadSha256", "")) or str(draft["payloadSha256"])
786
888
  if draft["tokenHash"] != _sha256(token):
787
889
  raise ToolError("审批令牌不匹配(draft 返回的 approvalToken 原样透传)", code=-32011)
788
890
  if draft["payloadSha256"] != payload_sha: