opencode-model-router 1.4.0 → 1.6.0

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.
@@ -1,5 +1,11 @@
1
1
  import { newGuardState } from "./guards";
2
2
  import type { GuardState, GuardPolicy } from "./guards";
3
+ import { DEFAULT_IDLE_TTL_MS } from "../router/idle-sweep";
4
+
5
+ export interface GuardStoreOptions {
6
+ /** Injectable clock (tests). Defaults to Date.now. */
7
+ now?: () => number;
8
+ }
3
9
 
4
10
  /**
5
11
  * Per-plugin-instance store of guard state, keyed by sessionID. Mirrors the
@@ -7,12 +13,29 @@ import type { GuardState, GuardPolicy } from "./guards";
7
13
  * singletons, so concurrent subagent sessions never share mutable state (M7).
8
14
  * Also holds a per-session "pending note" used by advisory mode to defer a
9
15
  * banner from the before-hook to the after-hook (where output is mutable).
16
+ *
17
+ * Idle-TTL: every mutating entry point refreshes a per-session lastTouch stamp;
18
+ * `sweep()` evicts sessions idle for at least ttlMs. No timers.
10
19
  */
11
- export function createGuardStore() {
20
+ export function createGuardStore(options: GuardStoreOptions = {}) {
21
+ const now = options.now ?? Date.now;
12
22
  const states = new Map<string, GuardState>();
13
23
  const pendingNotes = new Map<string, string>();
24
+ const lastTouch = new Map<string, number>();
25
+
26
+ function touch(sessionID: string): void {
27
+ lastTouch.set(sessionID, now());
28
+ }
29
+
30
+ function evict(sessionID: string): void {
31
+ states.delete(sessionID);
32
+ pendingNotes.delete(sessionID);
33
+ lastTouch.delete(sessionID);
34
+ }
35
+
14
36
  return {
15
37
  ensure(sessionID: string, policy: GuardPolicy): GuardState {
38
+ touch(sessionID);
16
39
  let s = states.get(sessionID);
17
40
  if (!s) {
18
41
  s = newGuardState(policy);
@@ -24,16 +47,36 @@ export function createGuardStore() {
24
47
  return states.get(sessionID);
25
48
  },
26
49
  setPendingNote(sessionID: string, note: string): void {
50
+ touch(sessionID);
27
51
  pendingNotes.set(sessionID, note);
28
52
  },
29
53
  takePendingNote(sessionID: string): string | undefined {
54
+ touch(sessionID);
30
55
  const n = pendingNotes.get(sessionID);
31
56
  if (n !== undefined) pendingNotes.delete(sessionID);
32
57
  return n;
33
58
  },
59
+ /**
60
+ * Start a new dispatch round for an existing session: reset the
61
+ * per-dispatch tool-call count while preserving cumulative totals,
62
+ * redundancy fingerprints and deliverable state. No-op for sessions
63
+ * that were never guarded (nothing to reset).
64
+ */
65
+ beginDispatch(sessionID: string): void {
66
+ const s = states.get(sessionID);
67
+ if (!s) return;
68
+ s.toolCallCount = 0;
69
+ s.dispatches += 1;
70
+ touch(sessionID);
71
+ },
34
72
  clear(sessionID: string): void {
35
- states.delete(sessionID);
36
- pendingNotes.delete(sessionID);
73
+ evict(sessionID);
74
+ },
75
+ /** Evict every session idle for >= ttlMs. Future stamps are never evicted. */
76
+ sweep(nowMs: number = now(), ttlMs: number = DEFAULT_IDLE_TTL_MS): void {
77
+ for (const [sessionID, stamp] of [...lastTouch.entries()]) {
78
+ if (nowMs - stamp >= ttlMs) evict(sessionID);
79
+ }
37
80
  },
38
81
  };
39
82
  }