dsh-galatea 0.4.7 → 0.4.8

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 (3) hide show
  1. package/README.md +31 -4
  2. package/index.js +25 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  | 块 | 状态 |
14
14
  |---|---|
15
15
  | 能被 DSH 装起来 | ✅ 实测通过(dsh 0.2.2-rc.3) |
16
- | 发布到 npm | ✅ 0.4.6(GitHub Actions 自动发,不需要 token) |
16
+ | 发布到 npm | ✅ 0.4.8(GitHub Actions 自动发,不需要 token) |
17
17
  | 记忆:读写 Markdown | ✅ 15 条测试 |
18
18
  | 记忆:注入索引 | ✅ **已核实内容真的进了上下文**(不是在会话日志里看到「接上了没告警」) |
19
19
  | 记忆:`remember` / `recall` | ✅ 23 条测试 |
@@ -203,7 +203,7 @@
203
203
  (同理,日志里那句「有素材,这次没掷中」和「记忆里一条都没有」是两件事。
204
204
  前者什么都别做,后者去记一条。)
205
205
 
206
- ## 三条教训:粒度、上限、旧数据
206
+ ## 四条教训:粒度、上限、旧数据、清理的对象
207
207
 
208
208
  ### 一 · 天粒度和秒粒度不能比较
209
209
 
@@ -242,6 +242,33 @@ lastSpokeAt = 2026-11-15T09:45:59Z ← 真的时刻
242
242
  它也不会一直错下去:下一次真的开口就归零。
243
243
  但它提醒一件事:**修「某个计数器的累加条件」时,旧值也是新逻辑的输入。**
244
244
 
245
+ ### 四 · 清理函数写对了,但清的不是自己
246
+
247
+ `ctx.interval()` 内部是:
248
+
249
+ ```js
250
+ interval(callback, delay) {
251
+ return this.ctx.effect(() => {
252
+ const timer = setInterval(callback, delay)
253
+ return () => clearInterval(timer)
254
+ })
255
+ }
256
+ ```
257
+
258
+ 清理函数写得很对。问题在 `this.ctx` —— 那是 **TimerService 自己被注册时的 ctx**
259
+ (`service.d.ts` 的构造函数把入参存成 `this.ctx`),不是调用方的。
260
+ 而 `Context` 本身没有 `ctx` 属性,所以通过 `mixin('timer', ...)` 混到 Context 上的
261
+ 这个方法,方法体里的 `this` 只能是 service。
262
+
263
+ 于是定时器挂在 timer 服务的 fiber 上,而那个 fiber 在根上、不销毁。
264
+ **插件每重载一次,旧钟还在响一个。** 重载十次,十个钟同时写账本 ——
265
+ 而账本看起来只是在正常动,只是快了几倍,没有任何地方会报错。
266
+
267
+ 修法:自己的 `ctx.effect` 套一层,卸载时走自己的清理。
268
+
269
+ 这一条和上一条是同一类:**两样东西看起来配套,其实不是一对。**
270
+ 日期和时刻是「粒度不配对」,这里是「所有权不配对」。
271
+
245
272
  ## 一次教训:模块边界
246
273
 
247
274
  0.3.0 / 0.3.5 / 0.3.6 连着三次,坏的地方都在**两个文件交接处**:
@@ -278,7 +305,7 @@ $DSH_HOME/galatea/heartbeat-state.json # 心跳状态账本
278
305
  ## 本地开发
279
306
 
280
307
  ```bash
281
- npm test # 109 条,不依赖 DSH,纯 node
308
+ npm test # 110 条,不依赖 DSH,纯 node
282
309
  ```
283
310
 
284
311
  装了之后验证它真的加载了,只看一个东西:
@@ -299,7 +326,7 @@ dsh plugin add dsh-galatea
299
326
  或者在 profile 的 `package.json` 里手写:
300
327
 
301
328
  ```json
302
- "dependencies": { "dsh-galatea": "0.4.7" }
329
+ "dependencies": { "dsh-galatea": "0.4.8" }
303
330
  ```
304
331
 
305
332
  **版本号钉死,不要写 `^`。** 同一个版本号指向两个不同的包,以后就说不清你装的是哪一份。
package/index.js CHANGED
@@ -252,9 +252,27 @@ export function apply(ctx, config) {
252
252
  const TICK_MS = 60_000;
253
253
  if (ctx && typeof ctx.interval === 'function') {
254
254
  let lastRunAt = 0;
255
- // 用 ctx.interval,不用已 deprecated 的 ctx.setInterval —— 它返回 dispose,
256
- // 由当前 fiber 管着,宿主热重载时不会叠出第二个定时器。
257
- ctx.interval(() => {
255
+
256
+ // ⚠️ 必须包在**自己的** ctx.effect 里。
257
+ //
258
+ // 这里原来写的是「ctx.interval 返回 dispose,由当前 fiber 管着,
259
+ // 宿主热重载时不会叠出第二个定时器」。**那是假的,我从没验过就写上去了。**
260
+ //
261
+ // 翻 cordis-plugin-timer 才看见:
262
+ // interval(callback, delay) { return this.ctx.effect(() => { ... }) }
263
+ // 而这个 this.ctx 是 **TimerService 自己被注册时的 ctx**
264
+ // (service.d.ts 构造函数里把入参存成 this.ctx),
265
+ // 又因为 Context 本身没有 `ctx` 属性(context.d.ts 只有
266
+ // events/logger/reflect/registry),所以通过 `mixin('timer', ...)`
267
+ // 混到 Context 上的这个方法,方法体里的 this 只能是 service。
268
+ //
269
+ // ⟹ 定时器挂在 timer 服务的 fiber 上,那个 fiber 在根上、不销毁。
270
+ // ⟹ 插件每重载一次,旧钟还在响一个。重载十次,十个钟同时写账本 ——
271
+ // 而账本看起来只是在正常动,只是快了几倍,没有任何地方会报错。
272
+ //
273
+ // 用 ctx.effect 套住:这个 ctx 是**我们自己**的 fiber,卸载时会走清理。
274
+ ctx.effect(() => {
275
+ const stop = ctx.interval(() => {
258
276
  try {
259
277
  // 现读 —— volatile 字段唯一的正确用法。
260
278
  // 取不到(没配过)就回落到 0,也就是「关着」。
@@ -294,7 +312,10 @@ export function apply(ctx, config) {
294
312
  console.warn('[galatea] 自动心跳失败:', err && err.message);
295
313
  }
296
314
  }, TICK_MS);
297
- console.log('[galatea] 自动心跳的钟已装:每 60 秒醒一次,按设置里的 autoBeatMinutes 决定跑不跑(0 = 不跑)');
315
+ return () => stop();
316
+ }, 'galatea:auto-beat');
317
+
318
+ console.log('[galatea] 自动心跳的钟已装:每 60 秒醒一次,按设置里的 autoBeatMinutes 决定跑不跑(0 = 不跑)');
298
319
 
299
320
  // 一个很自然但会误会的组合:自动心跳间隔 < 最小间隔。
300
321
  // 它**不是错误** —— minGap 会把节奏压回每 minGap 分钟一次。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-galatea",
3
- "version": "0.4.7",
3
+ "version": "0.4.8",
4
4
  "description": "Galatea — memory, heartbeat and continuity for DeepSeek Harness agents.",
5
5
  "repository": {
6
6
  "type": "git",