@fanchao8609/agent_brain_sync 0.4.0 → 0.5.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/install.js +16 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fanchao8609/agent_brain_sync",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "agent-brain-sync: 跨会话 AI 编码记忆 — hook 纯触发 + CLI/MCP 读写 .brain markdown 图谱, 防并发写保护。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/install.js CHANGED
@@ -262,6 +262,7 @@ const server = async ({ client, directory }) => {
262
262
  // 本会话是否真改过文件 (由 tool.execute.after 观测)
263
263
  let wroteFiles = false
264
264
  let nudged = false
265
+ let idleSeen = false
265
266
 
266
267
  async function logHook(evt) {
267
268
  try {
@@ -317,11 +318,16 @@ const server = async ({ client, directory }) => {
317
318
  return
318
319
  }
319
320
  if (type !== "session.idle") return // idle = 每轮结束, 不记日志(太吵), 只做收尾判定
321
+ const cwd = directory || process.cwd()
322
+ const brain = await findBrain(cwd)
323
+ // 可观测性: 首次 idle 无条件留一行痕(否则无法区分“事件没触发”与“被守卫拦下”)
324
+ if (!idleSeen) {
325
+ idleSeen = true
326
+ await logHook(\`session.idle:seen cwd=\${cwd} brain=\${brain || 'none'}\`)
327
+ }
320
328
  if (nudged || !wroteFiles) return
321
329
  const sessionID = event.properties && event.properties.sessionID
322
330
  if (!sessionID) return
323
- const cwd = directory || process.cwd()
324
- const brain = await findBrain(cwd)
325
331
  if (!brain) return // 无图谱=不在这项目沉淀, 不打扰
326
332
  if (await loggedToday(brain)) return // 今天已收尾过
327
333
  nudged = true
@@ -438,10 +444,18 @@ export default function absPiHook(pi: ExtensionAPI): void {
438
444
 
439
445
  // 收尾注入: 每个会话最多一次, 避免反复打扰。
440
446
  let teardownNudged = false
447
+ let agentEndSeen = false
441
448
  pi.on("agent_end", async (event: any, ctx: any) => {
442
449
  if (teardownNudged) return
443
450
  const cwd = (ctx && ctx.cwd) || process.cwd()
444
451
  const brain = await findBrain(cwd)
452
+ // 可观测性: 本会话首次 agent_end 无条件留一行痕。
453
+ // 没有它就无法区分三种状态: ①事件没触发 ②触发了但被守卫拦下 ③真注入了。
454
+ if (!agentEndSeen) {
455
+ agentEndSeen = true
456
+ // 带上 cwd 与命中的 brain —— 否则事后无法解释“为何这条 nudge 会出现”
457
+ await logHook(\`agent_end:seen cwd=\${cwd} brain=\${brain || 'none'}\`).catch(() => {})
458
+ }
445
459
  if (!brain) return // 无图谱=不在这项目沉淀, 不打扰
446
460
  if (!hasWriteWork(event?.messages ? collectToolResults(event.messages) : [])) return
447
461
  if (await loggedToday(brain)) return // 今天已收尾过