dsh-client-auto-continue 0.11.6 → 0.11.7

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.
package/README.md CHANGED
@@ -59,13 +59,13 @@ It watches the live event streams and reacts to:
59
59
  | `turn/end` → `max-tokens` | Output token ceiling reached |
60
60
  | `host/agent-error` | Agent failure with no turn position (only network/timeout-class messages auto-resume) |
61
61
 
62
- **Never auto-continues:** user-aborted turns (`aborted`) or policy rejections (`blocked`); live `interrupted` turn-ends too — that marker is only written by crash repair when the host reloads, so orphaned turns are recovered by the startup scan, not the live path; sessions the host already resumed itself; running sessions or sessions with queued messages; subagent sessions; anything inside the cooldown / consecutive-cap windows (configurable in the settings card, below).
62
+ **Never auto-continues:** user-aborted turns (`aborted`) or policy rejections (`blocked`); live `interrupted` turn-ends too — that marker is only written by crash repair when the host reloads, so orphaned turns are recovered by the startup scan, not the live path; sessions the host already resumed itself; running sessions; subagent sessions; anything inside the cooldown / consecutive-cap windows (configurable in the settings card, below). If an interrupted session already has queued turns, the continuation runs first and the existing turns retain their order behind it.
63
63
 
64
64
  ---
65
65
 
66
66
  ## How It Works
67
67
 
68
- The host-side engine subscribes to the session event firehose inside the dsh host process — exactly one engine, regardless of how many tabs are open (the duplicate-send class of bugs cannot exist by construction). On an interruption it waits a **grace period** (default 3 s) — if the host starts a new turn by itself (`turn/start`), the auto-continue is cancelled — then sends the configured text through the agent registry (`agent.followup`, the same queue the Send button uses).
68
+ The host-side engine subscribes to the session event firehose inside the dsh host process — exactly one engine, regardless of how many tabs are open (the duplicate-send class of bugs cannot exist by construction). On an interruption it waits a **grace period** (default 3 s) — if the host starts a new turn by itself (`turn/start`), the auto-continue is cancelled — then sends the configured text through the agent registry (`agent.followup`, the same queue the Send button uses). When that queue already contains turns, the engine promotes only its newly inserted continuation before the host wakes the agent; it does not remove or reorder the queued user turns.
69
69
 
70
70
  On host boot it also scans the live sessions: a session whose last turn ended with a non-human reason **within the scan window** (default 15 minutes), with no later `turn/start` or user message, gets resumed automatically too (e.g. the host crashed while the browser was closed — the agent-loop resumes the session and the engine picks it up).
71
71
 
package/README.zh.md CHANGED
@@ -59,13 +59,13 @@
59
59
  | `turn/end` → `max-tokens` | 达到输出 token 上限 |
60
60
  | `host/agent-error` | 无回合位置的 Agent 失败(仅网络/超时类消息自动续跑) |
61
61
 
62
- **绝不自动继续:** 用户主动停止(`aborted`)或策略拒绝(`blocked`); 实时流里的 `interrupted` 同样不自动继续——该标记只在宿主重载时由崩溃修复写入, 孤儿回合由启动扫描恢复, 不走实时路径; 宿主已自行恢复的会话; 正在运行或已有排队消息的会话; 子代理会话; 处于冷却期 / 连续次数上限内的会话(可在设置卡片中调整, 见下)。
62
+ **绝不自动继续:** 用户主动停止(`aborted`)或策略拒绝(`blocked`); 实时流里的 `interrupted` 同样不自动继续——该标记只在宿主重载时由崩溃修复写入, 孤儿回合由启动扫描恢复, 不走实时路径; 宿主已自行恢复的会话; 正在运行的会话; 子代理会话; 处于冷却期 / 连续次数上限内的会话(可在设置卡片中调整, 见下)。若中断会话已有排队消息, 续跑消息会先执行, 原有消息随后按原顺序处理。
63
63
 
64
64
  ---
65
65
 
66
66
  ## 工作原理
67
67
 
68
- 宿主侧引擎在 dsh 宿主进程内订阅会话事件 firehose——**全局只有一个引擎**, 无论开多少个标签页(重复发送这类 bug 从构造上不可能存在)。检测到中断后先等待一个**宽限期**(默认 3 秒)——若宿主自行开启了新回合(`turn/start`), 自动继续即取消——然后经 agent 注册表(`agent.followup`, 与「发送」按钮同一个排队通道)发送配置的文本。
68
+ 宿主侧引擎在 dsh 宿主进程内订阅会话事件 firehose——**全局只有一个引擎**, 无论开多少个标签页(重复发送这类 bug 从构造上不可能存在)。检测到中断后先等待一个**宽限期**(默认 3 秒)——若宿主自行开启了新回合(`turn/start`), 自动继续即取消——然后经 agent 注册表(`agent.followup`, 与「发送」按钮同一个排队通道)发送配置的文本。若队列已有消息, 引擎会在宿主唤醒 agent 之前只把本次续跑消息移到队首, 不删除或重排用户消息。
69
69
 
70
70
  宿主启动时还会扫描存活会话: 最后一个回合在**扫描时间窗**(默认 15 分钟)内以非人为原因结束、且其后没有新回合或用户消息的会话, 会被自动续跑(例如浏览器关闭期间宿主崩溃——agent-loop 恢复会话后引擎接着接手)。
71
71
 
package/lib/index.js CHANGED
@@ -660,6 +660,7 @@ var AutoContinueRunner = class {
660
660
  this.notices = [];
661
661
  this.noticeListeners = /* @__PURE__ */ new Set();
662
662
  this.stateListeners = /* @__PURE__ */ new Set();
663
+ this.prioritizedFollowups = /* @__PURE__ */ new Set();
663
664
  this.disposed = false;
664
665
  this.disposeSessionEvents = ctx.on("session/event", (session, event) => {
665
666
  try {
@@ -670,6 +671,30 @@ var AutoContinueRunner = class {
670
671
  );
671
672
  }
672
673
  });
674
+ this.disposeInboxEvents = ctx.on("agent/inbox/inserted", ({ agent, message }) => {
675
+ if (!this.prioritizedFollowups.delete(message.id)) return;
676
+ let removed = false;
677
+ try {
678
+ const index = agent.inbox.nextTurn.findIndex((pending) => pending.id === message.id);
679
+ if (index <= 0) return;
680
+ removed = agent.inbox.remove(message.id);
681
+ if (!removed) return;
682
+ agent.inbox.prepend("next-turn", message);
683
+ } catch (error) {
684
+ if (removed) {
685
+ try {
686
+ agent.inbox.append("next-turn", message);
687
+ } catch (rollbackError) {
688
+ console.error(
689
+ `[auto-continue] 恢复续跑消息失败 ${agent.id}: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`
690
+ );
691
+ }
692
+ }
693
+ console.error(
694
+ `[auto-continue] 调整续跑消息顺序失败 ${agent.id}: ${error instanceof Error ? error.message : String(error)}`
695
+ );
696
+ }
697
+ });
673
698
  const config = this.getConfig();
674
699
  if (config.scanOnBoot) {
675
700
  void this.bootScanLoop();
@@ -734,6 +759,8 @@ var AutoContinueRunner = class {
734
759
  if (this.disposed) return;
735
760
  this.disposed = true;
736
761
  this.disposeSessionEvents();
762
+ this.disposeInboxEvents();
763
+ this.prioritizedFollowups.clear();
737
764
  for (const state of this.states.values()) {
738
765
  if (state.pendingTimer !== void 0) clearTimeout(state.pendingTimer);
739
766
  if (state.loopRetryTimer !== void 0) clearTimeout(state.loopRetryTimer);
@@ -1230,11 +1257,14 @@ var AutoContinueRunner = class {
1230
1257
  source: { kind: "user" }
1231
1258
  });
1232
1259
  trackPendingEcho(state, message.id);
1260
+ if (agent.inbox.nextTurn.length > 0) this.prioritizedFollowups.add(message.id);
1233
1261
  try {
1234
1262
  agent.followup(message);
1235
1263
  } catch (error) {
1236
1264
  forgetPendingEcho(state, message.id);
1237
1265
  throw error;
1266
+ } finally {
1267
+ this.prioritizedFollowups.delete(message.id);
1238
1268
  }
1239
1269
  const now = Date.now();
1240
1270
  state.consecutive += 1;
@@ -36,7 +36,9 @@ export declare class AutoContinueRunner {
36
36
  private readonly notices;
37
37
  private readonly noticeListeners;
38
38
  private readonly stateListeners;
39
+ private readonly prioritizedFollowups;
39
40
  private readonly disposeSessionEvents;
41
+ private readonly disposeInboxEvents;
40
42
  private disposed;
41
43
  /**
42
44
  * @param ctx - host plugin context (agents registry, session events, settings).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-client-auto-continue",
3
3
  "description": "DSH Web UI plugin: automatically sends a localized continue prompt when a request is interrupted by network errors or other non-human causes",
4
- "version": "0.11.6",
4
+ "version": "0.11.7",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/types/index.d.ts",
@@ -197,7 +197,9 @@ export class AutoContinueRunner {
197
197
  private readonly notices: HostNotice[] = [];
198
198
  private readonly noticeListeners = new Set<() => void>();
199
199
  private readonly stateListeners = new Set<() => void>();
200
+ private readonly prioritizedFollowups = new Set<string>();
200
201
  private readonly disposeSessionEvents: () => void;
202
+ private readonly disposeInboxEvents: () => void;
201
203
  private disposed = false;
202
204
 
203
205
  /**
@@ -220,6 +222,37 @@ export class AutoContinueRunner {
220
222
  );
221
223
  }
222
224
  });
225
+ // followup() appends before it wakes the driver. Move only this plugin's marked
226
+ // message during the synchronous insertion event, while the driver still cannot claim it.
227
+ this.disposeInboxEvents = ctx.on('agent/inbox/inserted', ({ agent, message }) => {
228
+ if (!this.prioritizedFollowups.delete(message.id)) return;
229
+ let removed = false;
230
+ try {
231
+ const index = agent.inbox.nextTurn.findIndex(pending => pending.id === message.id);
232
+ if (index <= 0) return;
233
+ removed = agent.inbox.remove(message.id);
234
+ if (!removed) return;
235
+ agent.inbox.prepend('next-turn', message);
236
+ } catch (error) {
237
+ // Preserve the continuation even if a host-side queue mutation rejects the move.
238
+ if (removed) {
239
+ try {
240
+ agent.inbox.append('next-turn', message);
241
+ } catch (rollbackError) {
242
+ console.error(
243
+ `[auto-continue] 恢复续跑消息失败 ${agent.id}: ${
244
+ rollbackError instanceof Error ? rollbackError.message : String(rollbackError)
245
+ }`,
246
+ );
247
+ }
248
+ }
249
+ console.error(
250
+ `[auto-continue] 调整续跑消息顺序失败 ${agent.id}: ${
251
+ error instanceof Error ? error.message : String(error)
252
+ }`,
253
+ );
254
+ }
255
+ });
223
256
  const config = this.getConfig();
224
257
  if (config.scanOnBoot) {
225
258
  void this.bootScanLoop();
@@ -294,6 +327,8 @@ export class AutoContinueRunner {
294
327
  if (this.disposed) return;
295
328
  this.disposed = true;
296
329
  this.disposeSessionEvents();
330
+ this.disposeInboxEvents();
331
+ this.prioritizedFollowups.clear();
297
332
  for (const state of this.states.values()) {
298
333
  if (state.pendingTimer !== undefined) clearTimeout(state.pendingTimer);
299
334
  if (state.loopRetryTimer !== undefined) clearTimeout(state.loopRetryTimer);
@@ -874,7 +909,7 @@ export class AutoContinueRunner {
874
909
  ? config.continueTextMaxTokens
875
910
  : config.continueText;
876
911
  const text = this.buildContinueText(config, state, template);
877
- // 发送: agent.followup 是排队语义(运行中会排入 inbox, 不会打断), 天然安全
912
+ // 发送: followup 负责唤醒; 同步 inbox 插入事件会在唤醒前把本消息移到已有队列之前。
878
913
  const agent = this.ctx.agents.get(sessionId);
879
914
  if (agent === undefined) {
880
915
  this.log(`跳过 ${sessionId}(${reason}): 无 live agent`);
@@ -888,11 +923,14 @@ export class AutoContinueRunner {
888
923
  });
889
924
  // `followup` may publish the matching session event synchronously.
890
925
  trackPendingEcho(state, message.id);
926
+ if (agent.inbox.nextTurn.length > 0) this.prioritizedFollowups.add(message.id);
891
927
  try {
892
928
  agent.followup(message);
893
929
  } catch (error) {
894
930
  forgetPendingEcho(state, message.id);
895
931
  throw error;
932
+ } finally {
933
+ this.prioritizedFollowups.delete(message.id);
896
934
  }
897
935
  const now = Date.now();
898
936
  state.consecutive += 1;