Repository navigation
Add Hindsight Memory gateway hook / 新增 Hindsight Memory 网关记忆插件 - #876
Sam-dancing-nu1 wants to merge 5 commits into
Conversation
Automatic Hindsight recall before each request, optional capture (off by default), reuses an existing bank, and skips agents with native Hindsight injection by default to prevent double injection.
yetone
left a comment
There was a problem hiding this comment.
感谢贡献。代码是原创(Apache-2.0),只连接用户自己的 Hindsight 服务器,记忆捕获默认关闭,注入的文字也没有提到 magpie,这些都没问题。不过目前合并不了,原因如下:
-
(阻塞)每个请求都会召回一次,并把结果插到 system prompt 前面。 工具循环里的每一步也都会召回,所以:
- system 前缀每次都变,prompt cache 全部失效,长对话的费用会成倍增加;
- 每个请求最多多等约 4.8 s(README 里也写了)。
请参照仓库里已有的记忆 hook(
packages/powercontext、packages/nowledge-mem):- 用
ctx.query取查询,一轮对话只召回一次,工具循环中不再召回; - 召回块附加到该轮的 user 消息上,不要放进 system;
- 同一轮的块用缓存保持不变(sticky),这样后续请求的前缀能命中缓存。
-
README 中强制启用的示例
"agents":["Hermes Agent"]不会生效。ctx.agent的值是 agent id,例如hermes、dsh、claude,不是显示名。 -
ctx.report只接受这几个代码:ready、empty、unbound、unauthorized、unavailable、invalid、skipped。recalled、skipped-agent、captured等会被宿主忽略,请改用上面列出的代码。 -
README 的安装说明指向的是
github:Sam-dancing-nu1/magpie-hindsight和magpie-hindsight。 应改为本仓库的包名@magpie-community/gateway-hindsight,在 magpie 的插件页安装。 -
index.js导出的_internal看起来只是给测试用的。 测试可以直接 importlib/*.mjs,建议不要把它放进公开入口。
我运行过的检查(在另一台 macOS 机器上,HOME 和 TMPDIR 都用了沙盒目录):
packages/hindsight下运行bun test:15 pass;bun scripts/check.mjs hindsight:✓。
|
Thanks. I read every changed line and ran the package's tests on a separate machine (bun 1.4.2, temp HOME, PR merged onto main f379bf9):
|
- Recall once per user turn, attach to that user message (never system), replay the same block through the tool loop to keep prompt cache alive - Prefer ctx.query (host-stripped) over raw user text - Remote hindsight_api now requires allowRemote, and plain HTTP also requires allowInsecureHttp; fail closed with no fetch otherwise - Report only host-recognized codes (ready/empty/skipped/unavailable/invalid) - README: install via @magpie-community/gateway-hindsight, agent ids (hermes/dsh), privacy disclosure for recall and capture payloads - registry summary documents the 8 KB recall payload and capture payload - Remove atomcode-state.json and the test-only index.js export - Unify gatewayTimeoutMs default to 5000 - Add GUI settings page (options form, connection test, status)
中文两轮的 7+5 条意见已逐项处理(
验证: EnglishAll 12 points from both rounds are addressed in
Verification: 19/19 unit tests + 5/5 page tests pass; |
yetone
left a comment
There was a problem hiding this comment.
Re-reviewed at f3c9e53 (every changed line, GUI page included). Turn-sticky recall on the user message, ctx.query, the remote/plain-http opt-in and the disclosure are good, and the GUI page is clean (all dynamic text goes through textContent, the service page interpolates only its hex token, Host is checked, the probe only GETs /health on the gated URL). Still blocking:
atomcode-state.jsonis still in the PR (repo root, +10). The reply says it was removed; it is in every commit including f3c9e53. Drop it.packages/hindsight/package.json:2name ismagpie-hindsight, whileregistry.jsonlists@magpie-community/gateway-hindsight.scripts/publish.shpublishes the package.json name, so merging would publish an unscopedmagpie-hindsightand the registry entry would never resolve. Use"name": "@magpie-community/gateway-hindsight", and pointrepository/homepage/bugs(lines 8-15) atmagpie-community/pluginswith"directory": "packages/hindsight", aspackages/powercontextandpackages/nowledge-memdo. README:1/13/42 then say the same name.- Capture is lost for every turn that uses a tool.
lib/plugin.mjs:45-46setsctx.state.hindsightonly on the request that recalls; the sticky/injected paths (34-41) return without it, soonResponseof the turn's last request (the one withfinish_reason: "stop") has no state and retains nothing. Repro (chat,capture: true): request 1 = user message, replytool_calls; request 2 = same user message + assistant tool call + tool result, replystop "done"→ only the recall call is made, no/memoriesPOST. Set the turn's state (config + query) on every accepted request of the turn, and add this as a test. - The origin guard has no test. Deleting
if (ctx.origin !== "local")(lib/plugin.mjs:19) leaves all 24 tests green. Add a test thatremote,gateway-keyand a missing origin make no fetch.
Smaller, please fix in the same push:
lib/protocol.mjs:19-68(latestUserText,alreadyInjected,injectSystem) is no longer used by the hook; remove it and its tests. README:121 and :251 still say the tests cover "system-prompt adapters".page/service.mjs:71,94:json(result.code)dropsbankandms, so the test page always showsbank: hermesand— ms. Return them.page/settings.js:42-44checks only the plain-http opt-in; a non-loopback https URL saves withoutallowRemoteand the hook then reportsinvalidon every request. Mirrorconfiguration()(requireallowRemotefor any non-loopback host).- README:64 and :194 still describe the default skip as "Hermes Agent / DeepSeek Harness"; say
hermes/dshthere.test/plugin.test.mjs:12usesagent: "Claude Code"; use an id (claude). - Nothing tests the
injected(turn)path (lib/plugin.mjs:41); removing it is also green.
What I ran (macOS, bun 1.4.2, node 22.22.0, temp HOME/TMPDIR, PR head f3c9e53 on main b228cc6): bun test packages/hindsight 24 pass; bun scripts/check.mjs ok; package node --test 24 pass and scripts/check.mjs ok. Broken by hand: removing the allowRemote check, the allowInsecureHttp check, the per-turn cache lookup (double recall), the sticky replay on resent requests, history replay, the cache write and ctx.query preference each turn a test red. Removing the origin guard and the injected guard leave everything green (points 4 and 9). The tool-loop capture repro in point 3 was run against the unmodified PR code.
中文
新增 Hindsight Memory 网关插件(
@magpie-community/gateway-hindsight):为 Magpie 网关后的 Agent 在每次模型请求前自动召回 Hindsight 记忆并注入 system prompt,可选采集当前轮对话(默认关闭)。要点:
hermes),无需插件专用库;API 地址、bank、召回 token 预算、Agent 过滤均可配置。agents为空),需要时可在白名单中强制开启。验证:
bun scripts/check.mjs全仓通过。capture时无新增采集文档。来源仓库(持续维护):https://github.com/Sam-dancing-nu1/magpie-hindsight
AI 参与声明:本项目在 AI 辅助下完成(Hermes Agent),包括实现、测试与文档。
English
Add Hindsight Memory (
@magpie-community/gateway-hindsight): automatic Hindsight recall before each model request for agents behind Magpie, with optional capture of the completed turn (off by default).hermes); API URL, bank, recall budget and agent allow-list are configurable.agentslist; naming them force-enables.Verification: 15/15 unit tests pass (Node 22);
bun scripts/check.mjspasses across the repo. Local end-to-end: the model answered with an account name knowable only from the recalled bank memory; withcaptureomitted, no documents were retained.Source repository: https://github.com/Sam-dancing-nu1/magpie-hindsight
AI disclosure: built with AI assistance (Hermes Agent), including implementation, tests and documentation.