Files
dsh_ai1net_server/.workbuddy/待落地/verify-stop-hook.py
T
admin ce8e6ceed9 chore(工作区): 纳入版本控制基线(回收 411 MB 过程产物)
回收 411 MB(470 M → 58.8 M),全部经回收站,可恢复:
- 待清理/(146.2 M,含 relay 分片 128 M 与 42 项过程目录)
- tmp/(32.4 M,按接续棒命名的过程临时区)
- .workbuddy/tmp/(39.5 M)
- 4 份 workbuddy.db 冗余副本(101 M,09-23 事故的坏副本 / 抢救产物)
- tmp/im16/gw/centrifugo 二进制(63.9 M,可重下)+ 缓存残留

入库范围:常驻规则(CODEBUDDY.md / README.md / state.py)、在途接续入口与
接续包、docs/、交付物/、交接单/、归档/、scripts/、.codebuddy/、
.workbuddy/memory/;共 398 件,其中 >60 KB 的 26 件全为文档。

排除(.gitignore):tmp/、待清理/、运行态日志与缓存、*.db 与 DB 备份整目录、
打包二进制(*.tar.gz / *.tgz)、记忆修复前备份。
2026-09-24 07:51:03 +08:00

78 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""R5 预验证:临时装 Stop 钩子 → headless 跑一次(**不重启桌面**)→ 自动卸回 → 出结论。
为什么安全:hooks 是**应用启动时快照** ⇒ 正在跑的桌面会话看不到这次改动,
只有新起的进程(本例 = headless)才会加载它。全程自包含:无论成败都会卸回。
"""
import io
import json
import os
import shutil
import subprocess
import time
WS = r'E:/ProgramData/AI技能/aliyun-dsh-server'
S = r'E:/ProgramData/.workbuddy/settings.json'
CLI = r'C:/Users/Administrator/AppData/Local/Programs/WorkBuddy/resources/app.asar.unpacked/cli/dist/codebuddy-headless.js'
NODE = r'E:/ProgramData/.workbuddy/binaries/node/versions/22.22.2-3/node.exe'
PY = r'E:/ProgramData/.workbuddy/binaries/python/versions/3.13.12/python.exe'
SCRIPT = WS + '/dsh-server-docs/scripts/stop-dialog-guard.py'
LOG = WS + '/.workbuddy/stop-dialog-guard.log'
ENTRY = {"hooks": [{"type": "command",
"command": '"%s" -S -E "%s"' % (PY, SCRIPT),
"timeout": 10}]}
def hooks_state():
d = json.load(io.open(S, encoding='utf-8'))
return {k: len(v) for k, v in d.get('hooks', {}).items()}
def main():
print('[1] 装之前 hooks:', hooks_state(), flush=True)
bak = S + '.bak-stopverify2-' + time.strftime('%H%M%S')
shutil.copy2(S, bak)
d = json.load(io.open(S, encoding='utf-8'))
d['hooks']['Stop'] = [ENTRY]
io.open(S, 'w', encoding='utf-8', newline='\n').write(json.dumps(d, ensure_ascii=False, indent=1))
print('[2] 已临时装 Stop:', hooks_state(), '(备份', os.path.basename(bak), ')', flush=True)
if os.path.exists(LOG):
os.remove(LOG)
env = dict(os.environ)
env['DSH_SG_DEBUG'] = '1'
env['CODEBUDDY_PROJECT_DIR'] = WS
prompt = '请只用这一句话回复我,不要做任何其他事、不要读文件:要我接着做吗?'
out, err, rc = '', '', -1
try:
r = subprocess.run([NODE, CLI, '-p', prompt, '--output-format', 'stream-json'],
cwd=WS, env=env, capture_output=True, text=True, timeout=420)
out, err, rc = r.stdout, r.stderr, r.returncode
except Exception as e:
err = 'EXC: %s' % e
print('[3] headless rc=%s|stdout %d 字节|stderr %d 字节' % (rc, len(out), len(err)), flush=True)
invoked = os.path.exists(LOG)
logtxt = io.open(LOG, encoding='utf-8').read() if invoked else ''
injected = ('征询句' in out) or ('征询句' in err)
turns = out.count('"assistant"')
print('[4] 结论:', flush=True)
print(' 宿主是否调用 Stop 钩子 =', '是(有 debug 日志)' if invoked else '未观测到', flush=True)
if logtxt:
print(' debug 日志:', logtxt.strip().split('\n')[0][:180], flush=True)
print(' 是否采纳 continue:false(reason 被注入)=', '是' if injected else '未观测到', flush=True)
print(' assistant 出现次数(>1 = 被拦后继续了一轮)=', turns, flush=True)
if rc != 0:
print(' stderr 末尾:', (err or '')[-300:].replace('\n', ' '), flush=True)
d2 = json.load(io.open(S, encoding='utf-8'))
if 'Stop' in d2.get('hooks', {}):
del d2['hooks']['Stop']
io.open(S, 'w', encoding='utf-8', newline='\n').write(json.dumps(d2, ensure_ascii=False, indent=1))
print('[5] 已卸回:', hooks_state(), flush=True)
if __name__ == '__main__':
main()