feat(dsh): 门禁账本插件接入 + 角色绑定跨重启还原
二、门禁与唤醒(新插件 dsh-plugin-team-ledger 0.2.0)
宿主 Agent Teams 缺两块能力,补在独立仓
D:/Code/AI/dsh-plugin-team-ledger:
- 状态存放:<工作区>/.dsh/team/gate.jsonl 追加账本,记验收
清单、实测证据、认领历史,跨重启存活;canonical JSON +
fsync,坏尾行截断恢复。
- 唤醒模型:门禁推进后按事件计划唤醒下一责任人,借活体
Lead 作 caller 走宿主 sendMessage;无定时器;30 分钟滑动
窗口限 4 次防复活风暴。
- 验收独立性由机器强制:执行人/历史认领者不能 accept,
checklist 逐项 passed=true 缺一不可。
- team_member_revive(Lead 专用):成员模型路由死亡后的
复活通道——可换 provider/model,再经 subagents.sendMessage
冷恢复唤醒,绕过宿主 roster 的 phase=active 门禁。
二、角色绑定跨重启还原(roles fork 0.5.2)
persona 与工具白名单只有运行中 agent 的内存态,宿主重启后
冷恢复的成员既无 persona 也无白名单——实测出现 doc-writer
成员带全量工具面(qq_*/ImageGen),属静默权限放大。改为
注入成功时持久化 childId -> {roleId, cwd} 到
~/.dsh/role-inject-bindings.json,并在 agent/created 时重放。
追加 team_member_rebind 补绑定,覆盖 0.5.2 之前已存在的成员。
三、配套
- .dsh/roles 八个角色白名单加入门禁工具;AGENTS.md 委派策略
补门禁/复活/重绑三套操作口径。
- docs/环境与部署/子Agent角色自动注入实现说明.md 新增
§5.1,记录绑定机制、存量成员缺口与「成员自省不可全信」的
台架教训。
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Print the sys+tools budget of ONE session log, plus which role plugin owns
|
||||
* the delegation tool. Read-only: it decompresses the session log and reports.
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/inspect-session-budget.mjs <session-dir>
|
||||
* node scripts/inspect-session-budget.mjs --project <project-dir> [--newest]
|
||||
*
|
||||
* `--project` picks the newest session directory under
|
||||
* ~/.dsh/sessions/<slug-of-project-dir>/.
|
||||
*
|
||||
* Read-only: it never writes. Requires Node >= 22.15 for the multi-frame zstd
|
||||
* decoder (`node:zlib` gained zstd around then); the plugin itself needs >= 20.
|
||||
*/
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import { homedir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { zstdDecompressSync } from 'node:zlib'
|
||||
|
||||
function sessionRootFor(projectDir) {
|
||||
const slug = `--${projectDir.replace(/^\/+|\/+$/g, '').replaceAll('/', '-')}--`
|
||||
return join(homedir(), '.dsh', 'sessions', slug)
|
||||
}
|
||||
|
||||
function newestSessionDir(root) {
|
||||
const entries = readdirSync(root)
|
||||
.map((name) => join(root, name))
|
||||
.filter((path) => {
|
||||
try {
|
||||
return statSync(path).isDirectory()
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
})
|
||||
if (entries.length === 0) throw new Error(`no session directories under ${root}`)
|
||||
return entries.sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs)[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate the byte ranges of every complete Zstandard frame. Session logs are
|
||||
* appended frame by frame, and Node's one-shot decoder only reads the first,
|
||||
* so the ranges are scanned here (same frame-header walk the host uses).
|
||||
*/
|
||||
function scanZstdFrames(buffer) {
|
||||
const frames = []
|
||||
let offset = 0
|
||||
while (offset < buffer.length) {
|
||||
const start = offset
|
||||
if (buffer.length - offset < 4) break
|
||||
if (buffer.readUInt32LE(offset) !== 0xfd2fb528) break
|
||||
offset += 4
|
||||
if (offset === buffer.length) break
|
||||
const descriptor = buffer.readUInt8(offset)
|
||||
offset += 1
|
||||
const contentSizeFlag = descriptor >>> 6
|
||||
const singleSegment = (descriptor & 32) !== 0
|
||||
const checksum = (descriptor & 4) !== 0
|
||||
const dictionaryFlag = descriptor & 3
|
||||
const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag
|
||||
const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag
|
||||
offset += (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes
|
||||
let complete = true
|
||||
for (;;) {
|
||||
if (buffer.length - offset < 3) {
|
||||
complete = false
|
||||
break
|
||||
}
|
||||
const blockHeader = buffer.readUIntLE(offset, 3)
|
||||
offset += 3
|
||||
const lastBlock = (blockHeader & 1) !== 0
|
||||
const blockType = (blockHeader >>> 1) & 3
|
||||
const blockSize = blockHeader >>> 3
|
||||
offset += blockType === 1 ? 1 : blockSize
|
||||
if (buffer.length - offset < 0) {
|
||||
complete = false
|
||||
break
|
||||
}
|
||||
if (lastBlock) break
|
||||
}
|
||||
if (!complete) break
|
||||
if (checksum) offset += 4
|
||||
frames.push({ start, end: offset })
|
||||
}
|
||||
return frames
|
||||
}
|
||||
|
||||
/** Session logs are appended as concatenated zstd frames; decode every frame. */
|
||||
async function readLog(sessionDir) {
|
||||
const path = join(sessionDir, 'session.v3.jsonl.zstd')
|
||||
const raw = readFileSync(path)
|
||||
let text
|
||||
if (raw.subarray(0, 4).toString('hex') === '28b52ffd') {
|
||||
const frames = scanZstdFrames(raw)
|
||||
text = frames.map((frame) => zstdDecompressSync(raw.subarray(frame.start, frame.end)).toString('utf8')).join('')
|
||||
} else {
|
||||
text = raw.toString('utf8')
|
||||
}
|
||||
return text.split('\n').filter(Boolean).map((line) => JSON.parse(line))
|
||||
}
|
||||
|
||||
function textOf(node) {
|
||||
if (node === null || typeof node !== 'object') return undefined
|
||||
if (node.type === 'text' && typeof node.text === 'string') return node.text
|
||||
for (const value of Object.values(node)) {
|
||||
const found = textOf(value)
|
||||
if (found !== undefined) return found
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const argv = process.argv.slice(2)
|
||||
/**
|
||||
* Read one flag's value, refusing a missing one. A flag whose value is absent —
|
||||
* or is itself the next flag — used to flow through as `undefined`, which threw
|
||||
* a bare TypeError for `--project` and silently searched for the literal string
|
||||
* "undefined" for `--grep`.
|
||||
*/
|
||||
function flagValue(flag) {
|
||||
const at = argv.indexOf(flag)
|
||||
if (at < 0) return undefined
|
||||
const value = argv[at + 1]
|
||||
if (value === undefined || value.startsWith('--')) {
|
||||
console.error(`${flag} requires a value`)
|
||||
process.exit(2)
|
||||
}
|
||||
return value
|
||||
}
|
||||
let sessionDir
|
||||
const projectDir = flagValue('--project')
|
||||
if (projectDir !== undefined) {
|
||||
sessionDir = newestSessionDir(sessionRootFor(projectDir))
|
||||
} else if (argv[0] !== undefined && !argv[0].startsWith('--')) {
|
||||
sessionDir = argv[0]
|
||||
} else {
|
||||
console.error('usage: inspect-session-budget.mjs <session-dir> | --project <project-dir>')
|
||||
process.exit(2)
|
||||
}
|
||||
|
||||
const events = await readLog(sessionDir)
|
||||
const header = [...events].reverse().find((event) => event.type === 'request/header')
|
||||
const system = [...events].reverse().find((event) => event.type === 'system/message')
|
||||
const descriptor = events.find((event) => event.type === 'subagent/descriptor')
|
||||
const meta = events.find((event) => event.type === 'session')
|
||||
|
||||
console.log(`session : ${sessionDir}`)
|
||||
if (meta?.data?.cwd !== undefined) console.log(`cwd : ${meta.data.cwd}`)
|
||||
if (descriptor !== undefined) console.log(`subagent : provider=${descriptor.data.provider} mode=${descriptor.data.mode} label=${JSON.stringify(descriptor.data.label)}`)
|
||||
|
||||
const systemText = system === undefined ? undefined : textOf(system.data?.message ?? system.data)
|
||||
if (systemText !== undefined) {
|
||||
console.log(`system prompt: ${systemText.length} chars`)
|
||||
const catalog = systemText.includes('Roles come from role files')
|
||||
console.log(`role catalog : ${catalog ? 'present' : 'absent'}`)
|
||||
const needle = flagValue('--grep')
|
||||
if (needle !== undefined) {
|
||||
const at = systemText.indexOf(needle)
|
||||
console.log(`contains ${JSON.stringify(needle)}: ${at >= 0 ? 'YES' : 'no'}`)
|
||||
if (at >= 0) console.log(` …${systemText.slice(Math.max(0, at - 60), at + needle.length + 60).replaceAll('\n', ' / ')}…`)
|
||||
}
|
||||
if (systemText.includes('Check the [exit code: N] marker')) console.log('bash guidance: present')
|
||||
if (systemText.includes('Use the read tool — not shell commands like cat')) console.log('read guidance: present')
|
||||
}
|
||||
|
||||
const tools = header?.data?.header?.tools ?? []
|
||||
const sizes = tools
|
||||
.map((tool) => ({ name: tool.name, chars: JSON.stringify(tool).length }))
|
||||
.sort((left, right) => right.chars - left.chars)
|
||||
const total = sizes.reduce((sum, entry) => sum + entry.chars, 0)
|
||||
console.log(`tools : ${tools.length} (${total} schema chars)`)
|
||||
if (argv.includes('--all')) {
|
||||
console.log(` ${sizes.map((entry) => `${entry.name}(${entry.chars})`).join(', ')}`)
|
||||
}
|
||||
const role = tools.find((tool) => tool.name === 'subagent_role')
|
||||
if (role !== undefined) {
|
||||
// Matches BOTH of this plugin's wordings (a fork transport drops the
|
||||
// "self-contained" clause) while still rejecting the official `subagent`
|
||||
// tool, which says "to a subagent" rather than "to a role defined by a role file".
|
||||
const owner = /^Delegate a (?:self-contained )?task to a role defined by a role file/.test(String(role.description))
|
||||
? 'this plugin'
|
||||
: 'another registration of the same name'
|
||||
console.log(`subagent_role: ${JSON.stringify(role).length} chars — ${owner}`)
|
||||
}
|
||||
console.log('largest tools:')
|
||||
for (const entry of sizes.slice(0, 8)) console.log(` ${String(entry.chars).padStart(6)} ${entry.name}`)
|
||||
Reference in New Issue
Block a user