@animalabs/agent-framework 0.26.1 → 0.27.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.
- package/README.md +57 -7
- package/dist/src/agent.d.ts +263 -22
- package/dist/src/agent.d.ts.map +1 -1
- package/dist/src/agent.js +531 -54
- package/dist/src/agent.js.map +1 -1
- package/dist/src/api/server.d.ts.map +1 -1
- package/dist/src/api/server.js +1 -2
- package/dist/src/api/server.js.map +1 -1
- package/dist/src/auto-continue.d.ts +33 -0
- package/dist/src/auto-continue.d.ts.map +1 -0
- package/dist/src/auto-continue.js +35 -0
- package/dist/src/auto-continue.js.map +1 -0
- package/dist/src/code-execution/isolate/scripts.d.ts +11 -0
- package/dist/src/code-execution/isolate/scripts.d.ts.map +1 -1
- package/dist/src/code-execution/isolate/scripts.js +5 -1
- package/dist/src/code-execution/isolate/scripts.js.map +1 -1
- package/dist/src/framework.d.ts +829 -156
- package/dist/src/framework.d.ts.map +1 -1
- package/dist/src/framework.js +3906 -1008
- package/dist/src/framework.js.map +1 -1
- package/dist/src/gate/event-gate.d.ts +309 -46
- package/dist/src/gate/event-gate.d.ts.map +1 -1
- package/dist/src/gate/event-gate.js +2117 -395
- package/dist/src/gate/event-gate.js.map +1 -1
- package/dist/src/gate/index.d.ts +2 -2
- package/dist/src/gate/index.d.ts.map +1 -1
- package/dist/src/gate/index.js +1 -1
- package/dist/src/gate/index.js.map +1 -1
- package/dist/src/gate/recipe-reconcile.d.ts +103 -0
- package/dist/src/gate/recipe-reconcile.d.ts.map +1 -0
- package/dist/src/gate/recipe-reconcile.js +325 -0
- package/dist/src/gate/recipe-reconcile.js.map +1 -0
- package/dist/src/gate/types.d.ts +68 -8
- package/dist/src/gate/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +6 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcpl/channel-registry.d.ts +433 -113
- package/dist/src/mcpl/channel-registry.d.ts.map +1 -1
- package/dist/src/mcpl/channel-registry.js +1152 -434
- package/dist/src/mcpl/channel-registry.js.map +1 -1
- package/dist/src/mcpl/conversation-router.d.ts +16 -8
- package/dist/src/mcpl/conversation-router.d.ts.map +1 -1
- package/dist/src/mcpl/conversation-router.js +29 -15
- package/dist/src/mcpl/conversation-router.js.map +1 -1
- package/dist/src/mcpl/convert-block.d.ts.map +1 -1
- package/dist/src/mcpl/convert-block.js +14 -2
- package/dist/src/mcpl/convert-block.js.map +1 -1
- package/dist/src/mcpl/hook-orchestrator.d.ts +7 -1
- package/dist/src/mcpl/hook-orchestrator.d.ts.map +1 -1
- package/dist/src/mcpl/hook-orchestrator.js +32 -7
- package/dist/src/mcpl/hook-orchestrator.js.map +1 -1
- package/dist/src/mcpl/index.d.ts +5 -1
- package/dist/src/mcpl/index.d.ts.map +1 -1
- package/dist/src/mcpl/index.js +4 -0
- package/dist/src/mcpl/index.js.map +1 -1
- package/dist/src/mcpl/inference-router.d.ts +14 -2
- package/dist/src/mcpl/inference-router.d.ts.map +1 -1
- package/dist/src/mcpl/inference-router.js +73 -28
- package/dist/src/mcpl/inference-router.js.map +1 -1
- package/dist/src/mcpl/prose-grammar.d.ts +4 -0
- package/dist/src/mcpl/prose-grammar.d.ts.map +1 -1
- package/dist/src/mcpl/prose-grammar.js +6 -0
- package/dist/src/mcpl/prose-grammar.js.map +1 -1
- package/dist/src/mcpl/protocol-family.d.ts.map +1 -1
- package/dist/src/mcpl/protocol-family.js +3 -0
- package/dist/src/mcpl/protocol-family.js.map +1 -1
- package/dist/src/mcpl/push-coalescer.d.ts +20 -2
- package/dist/src/mcpl/push-coalescer.d.ts.map +1 -1
- package/dist/src/mcpl/push-coalescer.js +33 -11
- package/dist/src/mcpl/push-coalescer.js.map +1 -1
- package/dist/src/mcpl/reference-fetcher.d.ts +63 -42
- package/dist/src/mcpl/reference-fetcher.d.ts.map +1 -1
- package/dist/src/mcpl/reference-fetcher.js +177 -145
- package/dist/src/mcpl/reference-fetcher.js.map +1 -1
- package/dist/src/mcpl/references.d.ts +380 -46
- package/dist/src/mcpl/references.d.ts.map +1 -1
- package/dist/src/mcpl/references.js +737 -78
- package/dist/src/mcpl/references.js.map +1 -1
- package/dist/src/mcpl/server-connection.d.ts +82 -3
- package/dist/src/mcpl/server-connection.d.ts.map +1 -1
- package/dist/src/mcpl/server-connection.js +132 -7
- package/dist/src/mcpl/server-connection.js.map +1 -1
- package/dist/src/mcpl/tool-lifecycle.d.ts +11 -0
- package/dist/src/mcpl/tool-lifecycle.d.ts.map +1 -1
- package/dist/src/mcpl/tool-lifecycle.js +21 -0
- package/dist/src/mcpl/tool-lifecycle.js.map +1 -1
- package/dist/src/mcpl/tool-presence.d.ts +259 -0
- package/dist/src/mcpl/tool-presence.d.ts.map +1 -0
- package/dist/src/mcpl/tool-presence.js +628 -0
- package/dist/src/mcpl/tool-presence.js.map +1 -0
- package/dist/src/mcpl/tool-result-normalize.d.ts +9 -3
- package/dist/src/mcpl/tool-result-normalize.d.ts.map +1 -1
- package/dist/src/mcpl/tool-result-normalize.js +14 -6
- package/dist/src/mcpl/tool-result-normalize.js.map +1 -1
- package/dist/src/mcpl/tool-signature.d.ts +39 -0
- package/dist/src/mcpl/tool-signature.d.ts.map +1 -0
- package/dist/src/mcpl/tool-signature.js +292 -0
- package/dist/src/mcpl/tool-signature.js.map +1 -0
- package/dist/src/mcpl/toolset-host.d.ts +200 -0
- package/dist/src/mcpl/toolset-host.d.ts.map +1 -0
- package/dist/src/mcpl/toolset-host.js +621 -0
- package/dist/src/mcpl/toolset-host.js.map +1 -0
- package/dist/src/mcpl/toolsets-module.d.ts +80 -0
- package/dist/src/mcpl/toolsets-module.d.ts.map +1 -0
- package/dist/src/mcpl/toolsets-module.js +431 -0
- package/dist/src/mcpl/toolsets-module.js.map +1 -0
- package/dist/src/mcpl/transport.d.ts +2 -2
- package/dist/src/mcpl/transport.d.ts.map +1 -1
- package/dist/src/mcpl/transport.js +3 -2
- package/dist/src/mcpl/transport.js.map +1 -1
- package/dist/src/mcpl/types.d.ts +21 -1
- package/dist/src/mcpl/types.d.ts.map +1 -1
- package/dist/src/mcpl/types.js.map +1 -1
- package/dist/src/mcpl/visible-content.d.ts +7 -9
- package/dist/src/mcpl/visible-content.d.ts.map +1 -1
- package/dist/src/mcpl/visible-content.js +21 -9
- package/dist/src/mcpl/visible-content.js.map +1 -1
- package/dist/src/module-registry.d.ts +23 -8
- package/dist/src/module-registry.d.ts.map +1 -1
- package/dist/src/module-registry.js +68 -15
- package/dist/src/module-registry.js.map +1 -1
- package/dist/src/modules/discord/index.js +1 -1
- package/dist/src/modules/discord/index.js.map +1 -1
- package/dist/src/modules/history/index.d.ts +2 -1
- package/dist/src/modules/history/index.d.ts.map +1 -1
- package/dist/src/modules/history/index.js +51 -17
- package/dist/src/modules/history/index.js.map +1 -1
- package/dist/src/modules/history/semantic.d.ts +108 -6
- package/dist/src/modules/history/semantic.d.ts.map +1 -1
- package/dist/src/modules/history/semantic.js +273 -57
- package/dist/src/modules/history/semantic.js.map +1 -1
- package/dist/src/modules/workspace/direct-write.d.ts +181 -0
- package/dist/src/modules/workspace/direct-write.d.ts.map +1 -0
- package/dist/src/modules/workspace/direct-write.js +384 -0
- package/dist/src/modules/workspace/direct-write.js.map +1 -0
- package/dist/src/modules/workspace/edit-text.d.ts +26 -12
- package/dist/src/modules/workspace/edit-text.d.ts.map +1 -1
- package/dist/src/modules/workspace/edit-text.js +58 -28
- package/dist/src/modules/workspace/edit-text.js.map +1 -1
- package/dist/src/modules/workspace/effects.d.ts +10 -0
- package/dist/src/modules/workspace/effects.d.ts.map +1 -1
- package/dist/src/modules/workspace/effects.js +1 -1
- package/dist/src/modules/workspace/effects.js.map +1 -1
- package/dist/src/modules/workspace/index.d.ts +246 -27
- package/dist/src/modules/workspace/index.d.ts.map +1 -1
- package/dist/src/modules/workspace/index.js +1216 -236
- package/dist/src/modules/workspace/index.js.map +1 -1
- package/dist/src/modules/workspace/read-page.d.ts +41 -0
- package/dist/src/modules/workspace/read-page.d.ts.map +1 -0
- package/dist/src/modules/workspace/read-page.js +213 -0
- package/dist/src/modules/workspace/read-page.js.map +1 -0
- package/dist/src/modules/workspace/result-text.d.ts +67 -0
- package/dist/src/modules/workspace/result-text.d.ts.map +1 -0
- package/dist/src/modules/workspace/result-text.js +94 -0
- package/dist/src/modules/workspace/result-text.js.map +1 -0
- package/dist/src/modules/workspace/text.d.ts +29 -12
- package/dist/src/modules/workspace/text.d.ts.map +1 -1
- package/dist/src/modules/workspace/text.js +35 -33
- package/dist/src/modules/workspace/text.js.map +1 -1
- package/dist/src/modules/workspace/types.d.ts +41 -17
- package/dist/src/modules/workspace/types.d.ts.map +1 -1
- package/dist/src/modules/workspace/types.js.map +1 -1
- package/dist/src/sent-rows.d.ts +59 -0
- package/dist/src/sent-rows.d.ts.map +1 -0
- package/dist/src/sent-rows.js +145 -0
- package/dist/src/sent-rows.js.map +1 -0
- package/dist/src/silent-heartbeat.d.ts +17 -10
- package/dist/src/silent-heartbeat.d.ts.map +1 -1
- package/dist/src/silent-heartbeat.js.map +1 -1
- package/dist/src/stable-hash.d.ts +3 -0
- package/dist/src/stable-hash.d.ts.map +1 -0
- package/dist/src/stable-hash.js +15 -0
- package/dist/src/stable-hash.js.map +1 -0
- package/dist/src/stored-injections.d.ts +99 -0
- package/dist/src/stored-injections.d.ts.map +1 -0
- package/dist/src/stored-injections.js +106 -0
- package/dist/src/stored-injections.js.map +1 -0
- package/dist/src/text-as-stored.d.ts +4 -3
- package/dist/src/text-as-stored.d.ts.map +1 -1
- package/dist/src/text-as-stored.js +4 -3
- package/dist/src/text-as-stored.js.map +1 -1
- package/dist/src/thinking-drops.d.ts +81 -0
- package/dist/src/thinking-drops.d.ts.map +1 -0
- package/dist/src/thinking-drops.js +103 -0
- package/dist/src/thinking-drops.js.map +1 -0
- package/dist/src/tool-image-ledger.d.ts +9 -8
- package/dist/src/tool-image-ledger.d.ts.map +1 -1
- package/dist/src/tool-image-ledger.js +9 -8
- package/dist/src/tool-image-ledger.js.map +1 -1
- package/dist/src/tool-presentation.d.ts.map +1 -1
- package/dist/src/tool-presentation.js +3 -1
- package/dist/src/tool-presentation.js.map +1 -1
- package/dist/src/tool-result-guard.d.ts +4 -2
- package/dist/src/tool-result-guard.d.ts.map +1 -1
- package/dist/src/tool-result-guard.js +21 -6
- package/dist/src/tool-result-guard.js.map +1 -1
- package/dist/src/tool-result-history.d.ts +186 -21
- package/dist/src/tool-result-history.d.ts.map +1 -1
- package/dist/src/tool-result-history.js +428 -89
- package/dist/src/tool-result-history.js.map +1 -1
- package/dist/src/tune-out/coordinator.d.ts +17 -9
- package/dist/src/tune-out/coordinator.d.ts.map +1 -1
- package/dist/src/tune-out/coordinator.js +84 -37
- package/dist/src/tune-out/coordinator.js.map +1 -1
- package/dist/src/types/agent.d.ts +186 -7
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/events.d.ts +19 -2
- package/dist/src/types/events.d.ts.map +1 -1
- package/dist/src/types/framework.d.ts +24 -3
- package/dist/src/types/framework.d.ts.map +1 -1
- package/dist/src/types/module.d.ts +90 -7
- package/dist/src/types/module.d.ts.map +1 -1
- package/dist/src/types/trace.d.ts +57 -2
- package/dist/src/types/trace.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/agent.ts +593 -60
- package/src/api/server.ts +1 -2
- package/src/auto-continue.ts +42 -0
- package/src/code-execution/isolate/scripts.ts +11 -3
- package/src/framework.ts +4004 -1026
- package/src/gate/event-gate.ts +2315 -389
- package/src/gate/index.ts +2 -1
- package/src/gate/recipe-reconcile.ts +355 -0
- package/src/gate/types.ts +70 -8
- package/src/index.ts +7 -4
- package/src/mcpl/channel-registry.ts +1220 -467
- package/src/mcpl/conversation-router.ts +34 -16
- package/src/mcpl/convert-block.ts +14 -1
- package/src/mcpl/hook-orchestrator.ts +33 -6
- package/src/mcpl/index.ts +6 -1
- package/src/mcpl/inference-router.ts +79 -34
- package/src/mcpl/prose-grammar.ts +7 -0
- package/src/mcpl/protocol-family.ts +2 -0
- package/src/mcpl/push-coalescer.ts +51 -13
- package/src/mcpl/reference-fetcher.ts +201 -162
- package/src/mcpl/references.ts +852 -78
- package/src/mcpl/server-connection.ts +145 -7
- package/src/mcpl/tool-lifecycle.ts +21 -0
- package/src/mcpl/tool-presence.ts +650 -0
- package/src/mcpl/tool-result-normalize.ts +18 -6
- package/src/mcpl/tool-signature.ts +285 -0
- package/src/mcpl/toolset-host.ts +637 -0
- package/src/mcpl/toolsets-module.ts +451 -0
- package/src/mcpl/transport.ts +3 -2
- package/src/mcpl/types.ts +22 -1
- package/src/mcpl/visible-content.ts +22 -9
- package/src/module-registry.ts +77 -24
- package/src/modules/discord/index.ts +1 -1
- package/src/modules/history/index.ts +48 -18
- package/src/modules/history/semantic.ts +308 -54
- package/src/modules/workspace/direct-write.ts +425 -0
- package/src/modules/workspace/edit-text.ts +82 -35
- package/src/modules/workspace/effects.ts +1 -1
- package/src/modules/workspace/index.ts +1274 -241
- package/src/modules/workspace/read-page.ts +222 -0
- package/src/modules/workspace/result-text.ts +119 -0
- package/src/modules/workspace/text.ts +58 -32
- package/src/modules/workspace/types.ts +41 -17
- package/src/sent-rows.ts +162 -0
- package/src/silent-heartbeat.ts +17 -10
- package/src/stable-hash.ts +14 -0
- package/src/stored-injections.ts +131 -0
- package/src/text-as-stored.ts +4 -3
- package/src/thinking-drops.ts +141 -0
- package/src/tool-image-ledger.ts +9 -8
- package/src/tool-presentation.ts +3 -1
- package/src/tool-result-guard.ts +19 -6
- package/src/tool-result-history.ts +488 -88
- package/src/tune-out/coordinator.ts +80 -37
- package/src/types/agent.ts +189 -7
- package/src/types/events.ts +19 -2
- package/src/types/framework.ts +21 -3
- package/src/types/module.ts +86 -7
- package/src/types/trace.ts +58 -2
package/src/gate/event-gate.ts
CHANGED
|
@@ -11,9 +11,11 @@
|
|
|
11
11
|
* inference tokens, not whether the agent should know about the event.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import { existsSync, readFileSync,
|
|
15
|
-
import { dirname, join } from 'node:path';
|
|
14
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
15
|
+
import { basename, dirname, join } from 'node:path';
|
|
16
16
|
import { GateScript } from './gate-script.js';
|
|
17
|
+
import { canonical, duplicateName, lastRecipePath, noChanges, reconcileLine, reconcileWithRecipe, ruleName, type WrittenConfig } from './recipe-reconcile.js';
|
|
18
|
+
import { closestNames, isNearMiss } from '../closest-names.js';
|
|
17
19
|
import { MAX_TIMER_MS } from '../deadline-timer.js';
|
|
18
20
|
import { safeSlice } from '../safe-slice.js';
|
|
19
21
|
|
|
@@ -24,6 +26,8 @@ import type {
|
|
|
24
26
|
GatePolicyMatch,
|
|
25
27
|
GateBehavior,
|
|
26
28
|
GateDecision,
|
|
29
|
+
GateEntryRef,
|
|
30
|
+
WakeLine,
|
|
27
31
|
GateEventInfo,
|
|
28
32
|
GatePolicyStats,
|
|
29
33
|
GateStatus,
|
|
@@ -32,7 +36,7 @@ import type {
|
|
|
32
36
|
} from './types.js';
|
|
33
37
|
|
|
34
38
|
// Re-export types consumers need
|
|
35
|
-
export type { GateConfig, GateOptions, GateDecision, GateEventInfo } from './types.js';
|
|
39
|
+
export type { GateConfig, GateOptions, GateDecision, GateEntryRef, GateEventInfo, WakeLine } from './types.js';
|
|
36
40
|
|
|
37
41
|
// ---------------------------------------------------------------------------
|
|
38
42
|
// Constants
|
|
@@ -60,6 +64,21 @@ const MIN_DEBOUNCE_MS = 100;
|
|
|
60
64
|
/** Maximum debounce value (ms). */
|
|
61
65
|
const MAX_DEBOUNCE_MS = 300_000;
|
|
62
66
|
|
|
67
|
+
/** Whether a check made at `last` is still fresh at `now`, less than the
|
|
68
|
+
* reload throttle ago. A clock stepped backwards since makes it stale, so
|
|
69
|
+
* the next check runs rather than wait for the clock to catch up. */
|
|
70
|
+
function checkedRecently(now: number, last: number): boolean {
|
|
71
|
+
const elapsed = now - last;
|
|
72
|
+
return elapsed >= 0 && elapsed < RELOAD_THROTTLE_MS;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A read that failed because nothing is at the path. Any other failure
|
|
76
|
+
* (EACCES on a directory, ELOOP, ENOTDIR) is a file that's there but can't
|
|
77
|
+
* be read. */
|
|
78
|
+
function isMissing(err: unknown): boolean {
|
|
79
|
+
return (err as NodeJS.ErrnoException | null)?.code === 'ENOENT';
|
|
80
|
+
}
|
|
81
|
+
|
|
63
82
|
// ---------------------------------------------------------------------------
|
|
64
83
|
// Default config
|
|
65
84
|
// ---------------------------------------------------------------------------
|
|
@@ -104,12 +123,25 @@ interface PendingEvent {
|
|
|
104
123
|
* whose `channelId` is the adapter's raw id (a bare Discord snowflake for a
|
|
105
124
|
* DM) — unroutable. Absent when the host could not resolve one. */
|
|
106
125
|
routeChannelId?: string;
|
|
126
|
+
/** The event's own identity among what the framework stored at arrival:
|
|
127
|
+
* a channel message's id, a push event's event id (isNewestEntry). */
|
|
128
|
+
messageId?: string;
|
|
129
|
+
eventId?: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Where the host put a message the gate wrote: the agent whose window it
|
|
133
|
+
* went to and, when it was held for that agent's live turn, the id it waits
|
|
134
|
+
* under in the host's queue. */
|
|
135
|
+
export interface WritePlacement {
|
|
136
|
+
agent?: string;
|
|
137
|
+
deferredId?: string;
|
|
107
138
|
}
|
|
108
139
|
|
|
109
140
|
/**
|
|
110
141
|
* Where a gate-requested wake came from, handed to the framework alongside the
|
|
111
142
|
* reason so the turn's InferenceRequest can carry it (host telemetry stamps
|
|
112
|
-
* "who woke the agent" from it). Ids only, never content or display names
|
|
143
|
+
* "who woke the agent" from it). Ids only, never content or display names,
|
|
144
|
+
* except `line`, which is the batch's own text for the agent's window.
|
|
113
145
|
*/
|
|
114
146
|
export interface WakeProvenance {
|
|
115
147
|
/** Composite channel id of the chosen event — telemetry only, never a
|
|
@@ -131,9 +163,20 @@ export interface WakeProvenance {
|
|
|
131
163
|
* (2026-09-30: a DM reply published into a guild channel). Ambient-only
|
|
132
164
|
* batches still set nothing and keep the legacy fallback. */
|
|
133
165
|
routeChannelId?: string;
|
|
166
|
+
/** The server of `routeChannelId`'s channel, beside it whenever the chosen
|
|
167
|
+
* event named its server: channel ids are unique only within one
|
|
168
|
+
* server's connection (MCPL §14.2). */
|
|
169
|
+
routeServerId?: string;
|
|
134
170
|
/** Timestamp (ms) of the chosen event, so a consumer coalescing several
|
|
135
171
|
* requests can order by event recency, not by flush order. */
|
|
136
172
|
at?: number;
|
|
173
|
+
/** The batch's `[Gate: …]` line, when the gate handed it to the wake rather
|
|
174
|
+
* than wrote it: a WakeLine, the parts its text is rendered from when it's
|
|
175
|
+
* written (wakeLineText), decided by a fresh start of the wake's turn as
|
|
176
|
+
* WakeLine says. Unlike the fields above, this is content for the agent's
|
|
177
|
+
* window, not telemetry: it carries the channel's name as the agent is
|
|
178
|
+
* shown it. */
|
|
179
|
+
line?: WakeLine;
|
|
137
180
|
}
|
|
138
181
|
|
|
139
182
|
interface DebounceState {
|
|
@@ -236,11 +279,39 @@ export function wakeProvenance(events: PendingEvent[]): WakeProvenance | undefin
|
|
|
236
279
|
...(channelId ? { channelId } : {}),
|
|
237
280
|
...(pick.authorId ? { counterparty: `${ns}:user:${pick.authorId}` } : {}),
|
|
238
281
|
...(pick.addressed ? { addressed: true } : {}),
|
|
239
|
-
...(pick.addressed && pick.routeChannelId
|
|
282
|
+
...(pick.addressed && pick.routeChannelId
|
|
283
|
+
? { routeChannelId: pick.routeChannelId, ...(pick.serverId ? { routeServerId: pick.serverId } : {}) }
|
|
284
|
+
: {}),
|
|
240
285
|
at: pick.timestamp,
|
|
241
286
|
};
|
|
242
287
|
}
|
|
243
288
|
|
|
289
|
+
/** A `[Gate: …]` line's reference to messages already in the window: how
|
|
290
|
+
* many, in which channel, and how long ago the oldest arrived, as of `now`. */
|
|
291
|
+
function referenceLine(count: number, channel: string, oldest: number, now: number): string {
|
|
292
|
+
const age = Math.round((now - oldest) / 1000);
|
|
293
|
+
return `- ${count} message${count > 1 ? 's' : ''} in ${channel} ` +
|
|
294
|
+
`(oldest ~${age}s ago) — already shown above at arrival; this is a ` +
|
|
295
|
+
`batched wake, not new content`;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** A handed batched-wake line's text (WakeLine), as it reads when written at
|
|
299
|
+
* `now`: the gate's words, its message's age counted to then. */
|
|
300
|
+
export function wakeLineText(line: WakeLine, now: number): string {
|
|
301
|
+
return `[Gate: 1 event matched]\n\n${referenceLine(1, line.shown, line.at, now)}`;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The event's own stored entry, as the framework is asked about it. */
|
|
305
|
+
function entryRef(event: PendingEvent): GateEntryRef {
|
|
306
|
+
return {
|
|
307
|
+
eventType: event.eventType,
|
|
308
|
+
...(event.serverId !== undefined ? { serverId: event.serverId } : {}),
|
|
309
|
+
...(event.channelId !== undefined ? { channelId: event.channelId } : {}),
|
|
310
|
+
...(event.messageId !== undefined ? { messageId: event.messageId } : {}),
|
|
311
|
+
...(event.eventId !== undefined ? { eventId: event.eventId } : {}),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
244
315
|
/**
|
|
245
316
|
* Default route-channel resolution when the host supplies none: a
|
|
246
317
|
* channel-incoming event's channel id is already the registered composite id;
|
|
@@ -277,169 +348,1316 @@ function behaviorFingerprint(b: import('./types.js').GateBehavior): string {
|
|
|
277
348
|
}
|
|
278
349
|
|
|
279
350
|
// ---------------------------------------------------------------------------
|
|
280
|
-
//
|
|
351
|
+
// Reading rules
|
|
281
352
|
// ---------------------------------------------------------------------------
|
|
282
353
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
354
|
+
// A rule is read as written when it can be, and every problem it has is named.
|
|
355
|
+
// gate.json and the host's starting rules are what a deployment already runs,
|
|
356
|
+
// so a rule with a problem is read the way earlier releases read it, and no
|
|
357
|
+
// event that woke the agent there goes quiet: a behavior the gate doesn't know
|
|
358
|
+
// is read as "always"; a field of the wrong type, one slip from one the gate
|
|
359
|
+
// reads there, or one the gate reads at another level of the rule (a match's
|
|
360
|
+
// "tagsAny" beside "name"), as left out, its problem naming where it's read;
|
|
361
|
+
// and a number out of range the way the starting rules ran it, but for a
|
|
362
|
+
// debounce, read at its nearest bound, which can send a batch later or sooner
|
|
363
|
+
// but drops no event in it, and a refill interval of 0, read as refilling at
|
|
364
|
+
// once. A rule is left out only where earlier releases refused it. One that
|
|
365
|
+
// skips or defers is read past a problem too, holding back what they held back:
|
|
366
|
+
// left out, the events it held back would go on to the rules after it, where a
|
|
367
|
+
// rate limit, a sample or a debounce could spend them and then hold back an
|
|
368
|
+
// event that woke the agent before. Under a default that holds events back, a
|
|
369
|
+
// file that leaves out a rule that could wake the agent isn't used, since that
|
|
370
|
+
// would hold back what the rule was written to wake on; earlier releases didn't
|
|
371
|
+
// use it either. A change through addPolicy, whose caller is there to fix it,
|
|
372
|
+
// refuses any problem, and nothing is saved. A field name that is no slip of
|
|
373
|
+
// one the gate reads there, and that it reads at no other level, changes
|
|
374
|
+
// nothing, a slip of another level's field included: it loads, and a note names
|
|
375
|
+
// it with the fields its place holds. In a rule, a name holding a field's whole
|
|
376
|
+
// name is such a name beside that field, an extra that explains it, and a slip
|
|
377
|
+
// of the field where it's given in the field's place (fieldNames); at
|
|
378
|
+
// gate.json's top, where it can be a label of the file, it's such a name either
|
|
379
|
+
// way. A file earlier releases loaded loads as they read it: a slip of
|
|
380
|
+
// "policies", a list of rules parked under a name the gate doesn't read, a
|
|
381
|
+
// rule's fields at gate.json's top and a "policies" that isn't a list each do
|
|
382
|
+
// nothing, and a note names each, so where "policies" writes no rules the file
|
|
383
|
+
// has none, and under "skip" or "defer" it holds back every event, as it did. A
|
|
384
|
+
// "policies" holding nothing writes no rules, as one left out does. A null is a
|
|
385
|
+
// field left out, and so is "", with a note: earlier releases read most fields
|
|
386
|
+
// that way, and none narrows anything by them. Where they refused a rule or a
|
|
387
|
+
// file for a null or a "" (a passthrough, a behavior's kind, gate.json's
|
|
388
|
+
// default), it now loads without it.
|
|
389
|
+
|
|
390
|
+
const CONFIG_FIELDS = ['default', 'policies'];
|
|
391
|
+
const POLICY_FIELDS = ['name', 'match', 'behavior', 'resets', 'passthrough'];
|
|
392
|
+
const MATCH_FIELDS = [
|
|
393
|
+
'scope', 'source', 'channel', 'mount', 'pathGlob', 'tagsAny', 'tagsAll', 'tagsNone', 'metadataTrue', 'filter',
|
|
394
|
+
];
|
|
395
|
+
const FILTER_FIELDS = ['type', 'pattern'];
|
|
396
|
+
const BEHAVIOR_KINDS = ['debounce', 'rate_limit', 'passive_sample'] as const;
|
|
397
|
+
const RATE_LIMIT_FIELDS = ['tokens', 'refillIntervalMs', 'keyBy'];
|
|
398
|
+
const PASSIVE_SAMPLE_FIELDS = ['every', 'keyBy'];
|
|
399
|
+
|
|
400
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
401
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** A value that sets nothing: null, "", an empty list, or an object holding only such values. */
|
|
405
|
+
function holdsNothing(value: unknown): boolean {
|
|
406
|
+
return value === null || value === '' || (Array.isArray(value) && value.length === 0)
|
|
407
|
+
|| (isPlainObject(value) && Object.values(value).every(holdsNothing));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Whether a failed write left its file as it was: writeFileSync reports a
|
|
412
|
+
* failure once it has the file open, so it may have changed it, as its write
|
|
413
|
+
* or its close.
|
|
414
|
+
*/
|
|
415
|
+
function beforeWriting(err: unknown): boolean {
|
|
416
|
+
const syscall = (err as NodeJS.ErrnoException).syscall;
|
|
417
|
+
return syscall !== 'write' && syscall !== 'close';
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* A change's refusal where gate.json has no rule list it could edit without
|
|
422
|
+
* writing over what it holds: a list, JSON that isn't an object, or a
|
|
423
|
+
* "policies" that isn't a list and holds something. `runs` is what the gate
|
|
424
|
+
* runs meanwhile (rulesRun).
|
|
425
|
+
*/
|
|
426
|
+
function noListRefusal(noList: string, runs: string): string {
|
|
427
|
+
return `${noList}, so it has no list of rules a change could edit without writing over what it holds, and nothing `
|
|
428
|
+
+ `was saved. Meanwhile the gate runs ${runs}. Until gate.json is an object whose "policies" is a list holding its `
|
|
429
|
+
+ 'rules, a change is refused the same way.';
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* A change's refusal where gate.json is there but couldn't be used as a file
|
|
434
|
+
* a change edits: it couldn't be read, or it isn't JSON (`notJson`). Nothing is
|
|
435
|
+
* written over it, so what it holds, often an operator's unfinished edit, stays
|
|
436
|
+
* as it is. `runs` is what the gate runs meanwhile (rulesRun).
|
|
437
|
+
*/
|
|
438
|
+
function unusableRefusal(unusable: { error: string; notJson?: true }, runs: string): string {
|
|
439
|
+
if (!unusable.notJson) {
|
|
440
|
+
return `gate.json couldn't be read (${unusable.error}), so a change can't edit it, and nothing was written over it. `
|
|
441
|
+
+ `Meanwhile the gate runs ${runs}. Whoever runs this host can make gate.json readable; until then, a change is `
|
|
442
|
+
+ 'refused the same way.';
|
|
443
|
+
}
|
|
444
|
+
return `gate.json isn't JSON the gate can read (${unusable.error}), so a change can't edit its rules without writing `
|
|
445
|
+
+ `over what it holds, and nothing was saved. Meanwhile the gate runs ${runs}. Until gate.json is JSON, a change is `
|
|
446
|
+
+ 'refused the same way.';
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* A change's refusal at a start that didn't use gate.json, which has no rules
|
|
451
|
+
* of its own (start-without): a change puts the rules gate.json then holds in
|
|
452
|
+
* force, so it would take out of force what the start runs in its place.
|
|
453
|
+
* `runs` is what the gate runs meanwhile (rulesRun); gate_status's entries
|
|
454
|
+
* (notUsedClause, describeStart) say which rules keep gate.json out of use
|
|
455
|
+
* and each fix.
|
|
456
|
+
*/
|
|
457
|
+
function notUsedRefusal(runs: string): string {
|
|
458
|
+
return "gate.json isn't used at this start, which runs as one without it does, and a change would put gate.json's rules in "
|
|
459
|
+
+ `force in place of what that runs, so nothing was saved. Meanwhile the gate runs ${runs}. gate_status says what keeps `
|
|
460
|
+
+ 'gate.json out of use and what would put it in use, such as giving it rules under "policies", or fixing the starting '
|
|
461
|
+
+ 'rules, which whoever runs this host can do, for the next start; until then, a change is refused the same way.';
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* A value as a message quotes it: its JSON, cut short when long, never inside a
|
|
466
|
+
* surrogate pair, since the message reaches the agent's context.
|
|
467
|
+
*/
|
|
468
|
+
export function shownValue(value: unknown): string {
|
|
469
|
+
if (value === undefined) return 'missing';
|
|
470
|
+
// JSON writes NaN and the infinities as null.
|
|
471
|
+
if (typeof value === 'number' && !Number.isFinite(value)) return String(value);
|
|
472
|
+
let json: string;
|
|
473
|
+
try {
|
|
474
|
+
json = JSON.stringify(value) ?? String(value);
|
|
475
|
+
} catch {
|
|
476
|
+
json = String(value);
|
|
477
|
+
}
|
|
478
|
+
return json.length > 80 ? `${safeSlice(json, 0, 77)}...` : json;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** Entries of a list as a message quotes them. */
|
|
482
|
+
function shownValues(values: readonly unknown[]): string {
|
|
483
|
+
return values.map(shownValue).join(', ');
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** `a`, `a and b`, `a, b and c` (or "or"). */
|
|
487
|
+
function joinedList(items: readonly string[], joiner: 'and' | 'or' = 'and'): string {
|
|
488
|
+
return items.length < 2 ? items.join('') : `${items.slice(0, -1).join(', ')} ${joiner} ${items[items.length - 1]}`;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/** `"a"`, `"a" and "b"`, `"a", "b" and "c"` (or "or"). */
|
|
492
|
+
function quotedList(names: readonly string[], joiner: 'and' | 'or' = 'and'): string {
|
|
493
|
+
return joinedList(names.map((n) => `"${n}"`), joiner);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** 1st, 2nd, 3rd, 4th, 11th, 21st. */
|
|
497
|
+
function ordinal(n: number): string {
|
|
498
|
+
const tens = n % 100;
|
|
499
|
+
const suffix = tens >= 11 && tens <= 13 ? 'th' : (['th', 'st', 'nd', 'rd'][n % 10] ?? 'th');
|
|
500
|
+
return `${n}${suffix}`;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** A message part as one sentence, for joining several. */
|
|
504
|
+
function sentence(text: string): string {
|
|
505
|
+
return /[.?!]$/.test(text) ? text : `${text}.`;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** How a note speaks of what it checks: a rule's fields, or a tool's parameters. */
|
|
509
|
+
const RULE_FIELDS_TERMS = { unread: "which the gate doesn't read", listed: 'Its fields are' };
|
|
510
|
+
const TOOL_PARAMETERS_TERMS = { unread: "which it doesn't take", listed: 'Its parameters are' };
|
|
511
|
+
|
|
512
|
+
/** A field name one slip from one or more the gate reads in its place. */
|
|
513
|
+
interface Slip {
|
|
514
|
+
key: string;
|
|
515
|
+
/** Every field it could be meant for, in closestNames' order: `tags` is part of "tagsAny", "tagsAll" and "tagsNone". */
|
|
516
|
+
meant: string[];
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* An object's field names that the gate doesn't read in its place: slips of
|
|
521
|
+
* one it does, and the rest. A name is a slip of each field isNearMiss finds
|
|
522
|
+
* it close to and, where `held`, of each whose whole name it holds, ignoring
|
|
523
|
+
* case, that the object doesn't give: sent in that field's place, it's the
|
|
524
|
+
* field under another name (`matchTags` with no `match`), where beside it,
|
|
525
|
+
* it's an extra. The object is one withoutEmpty read, so a field given as
|
|
526
|
+
* null or "" isn't given.
|
|
527
|
+
*/
|
|
528
|
+
function fieldNames(
|
|
529
|
+
obj: Record<string, unknown>, fields: readonly string[], held = true,
|
|
530
|
+
): { slips: Slip[]; unread: string[] } {
|
|
531
|
+
const slips: Slip[] = [];
|
|
532
|
+
const unread: string[] = [];
|
|
533
|
+
for (const key of Object.keys(obj)) {
|
|
534
|
+
if (obj[key] === undefined || fields.includes(key)) continue;
|
|
535
|
+
// Every field the gate reads has four characters or more, so holding one
|
|
536
|
+
// needs no length of its own here. A field of two characters or fewer
|
|
537
|
+
// would be held by names that have nothing to do with it ("valid" holds
|
|
538
|
+
// "id"), and holding it would need the three characters isNearMiss asks
|
|
539
|
+
// of a part.
|
|
540
|
+
const holds = (field: string): boolean => key.toLowerCase().includes(field.toLowerCase());
|
|
541
|
+
const near = fields.filter((field) => isNearMiss(key, field) || (held && !Object.hasOwn(obj, field) && holds(field)));
|
|
542
|
+
if (near.length === 0) {
|
|
543
|
+
unread.push(key);
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
// closestNames keeps every name decided on above, in its order.
|
|
547
|
+
slips.push({ key, meant: closestNames(key, near, near.length) });
|
|
548
|
+
}
|
|
549
|
+
return { slips, unread };
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** `"tags" (did you mean "tagsAny", "tagsAll" or "tagsNone"?)` */
|
|
553
|
+
function slipShown(slip: Slip): string {
|
|
554
|
+
return `"${slip.key}" (did you mean ${quotedList(slip.meant, 'or')}?)`;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** Names an object has that are fields read at another level, with where that is (`homes`). */
|
|
558
|
+
interface Misplaced {
|
|
559
|
+
keys: string[];
|
|
560
|
+
homes: string[];
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* An object's names that do nothing in its place, split into those that are
|
|
565
|
+
* fields read elsewhere, grouped by where (`homesOf`), and the rest.
|
|
566
|
+
*/
|
|
567
|
+
function placedElsewhere(
|
|
568
|
+
unread: readonly string[], homesOf: (key: string) => string[],
|
|
569
|
+
): { misplaced: Misplaced[]; rest: string[] } {
|
|
570
|
+
const misplaced: Misplaced[] = [];
|
|
571
|
+
const rest: string[] = [];
|
|
572
|
+
for (const key of unread) {
|
|
573
|
+
const homes = homesOf(key);
|
|
574
|
+
if (homes.length === 0) {
|
|
575
|
+
rest.push(key);
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
const same = misplaced.find((m) => m.homes.join() === homes.join());
|
|
579
|
+
if (same) same.keys.push(key);
|
|
580
|
+
else misplaced.push({ keys: [key], homes });
|
|
581
|
+
}
|
|
582
|
+
return { misplaced, rest };
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** `"tagsAny", a field of "match"`; `"tagsAny" and "source", fields of "match"` */
|
|
586
|
+
function misplacedShown(m: Misplaced): string {
|
|
587
|
+
return `${quotedList(m.keys)}, ${m.keys.length === 1 ? 'a field' : 'fields'} of ${joinedList(m.homes)}`;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The levels of a gate.json the gate reads fields at, each as a message names
|
|
592
|
+
* it, `file` naming the file itself.
|
|
593
|
+
*/
|
|
594
|
+
function fieldLevels(file: string): ReadonlyArray<{ name: string; fields: readonly string[] }> {
|
|
595
|
+
return [
|
|
596
|
+
{ name: file, fields: CONFIG_FIELDS },
|
|
597
|
+
{ name: 'a rule', fields: POLICY_FIELDS },
|
|
598
|
+
{ name: '"match"', fields: MATCH_FIELDS },
|
|
599
|
+
{ name: '"match.filter"', fields: FILTER_FIELDS },
|
|
600
|
+
{ name: '"behavior"', fields: BEHAVIOR_KINDS },
|
|
601
|
+
{ name: '"behavior.rate_limit"', fields: RATE_LIMIT_FIELDS },
|
|
602
|
+
{ name: '"behavior.passive_sample"', fields: PASSIVE_SAMPLE_FIELDS },
|
|
603
|
+
];
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/** Where a name is a field the gate reads, at a level other than the one whose fields are `here`. */
|
|
607
|
+
function homesOf(file: string, here: readonly string[]): (key: string) => string[] {
|
|
608
|
+
return (key) => fieldLevels(file)
|
|
609
|
+
.filter((level) => level.fields !== here && level.fields.includes(key))
|
|
610
|
+
.map((level) => level.name);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** Where each field of an object schema's nested objects is taken: `tagsAny` in "match". */
|
|
614
|
+
function nestedFields(properties: Readonly<Record<string, unknown>>, path = ''): Map<string, string[]> {
|
|
615
|
+
const homes = new Map<string, string[]>();
|
|
616
|
+
const add = (field: string, where: string) => homes.set(field, [...(homes.get(field) ?? []), where]);
|
|
617
|
+
for (const [name, schema] of Object.entries(properties)) {
|
|
618
|
+
if (!isPlainObject(schema) || !isPlainObject(schema.properties)) continue;
|
|
619
|
+
const where = path ? `${path}.${name}` : name;
|
|
620
|
+
for (const field of Object.keys(schema.properties)) add(field, `"${where}"`);
|
|
621
|
+
for (const [field, inner] of nestedFields(schema.properties, where)) for (const at of inner) add(field, at);
|
|
622
|
+
}
|
|
623
|
+
return homes;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** A note naming the fields an object has that do nothing in its place, with the fields that do. */
|
|
627
|
+
function noteUnread(
|
|
628
|
+
where: string, unread: readonly string[], fields: readonly string[], notes: string[], terms = RULE_FIELDS_TERMS,
|
|
629
|
+
): void {
|
|
630
|
+
if (unread.length === 0) return;
|
|
631
|
+
notes.push(
|
|
632
|
+
`${where} has ${quotedList(unread)}, ${terms.unread}, so ${unread.length === 1 ? 'it does' : 'they do'} `
|
|
633
|
+
+ `nothing. ${terms.listed} ${quotedList(fields)}.`,
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* A tool's input, its parameters given by its schema's `properties`, read as a
|
|
639
|
+
* rule's fields are: a null or "" parameter is left out (a note names each "",
|
|
640
|
+
* with what leaving it out does where `means` says), a slip of a parameter
|
|
641
|
+
* (fieldNames) is refused, naming the one meant, and so is a field of one of
|
|
642
|
+
* its object parameters given on its own, naming that parameter; any other
|
|
643
|
+
* name is noted, or named in that refusal. Returns the input without what was
|
|
644
|
+
* left out.
|
|
645
|
+
*/
|
|
646
|
+
export function checkToolInput(
|
|
647
|
+
tool: string, input: Record<string, unknown>, properties: Readonly<Record<string, unknown>>, notes: string[],
|
|
648
|
+
means: Readonly<Record<string, string>> = {},
|
|
649
|
+
): Record<string, unknown> {
|
|
650
|
+
const params = Object.keys(properties);
|
|
651
|
+
const given = withoutEmpty(tool, input, notes, means);
|
|
652
|
+
const { slips, unread } = fieldNames(given, params);
|
|
653
|
+
const homes = nestedFields(properties);
|
|
654
|
+
const { misplaced, rest } = placedElsewhere(unread, (key) => homes.get(key) ?? []);
|
|
655
|
+
if (slips.length > 0 || misplaced.length > 0) {
|
|
656
|
+
// Every name it doesn't take is named, so the call that follows the refusal doesn't count on one.
|
|
657
|
+
const has = [
|
|
658
|
+
...(slips.length > 0 ? [`${slips.map(slipShown).join(' and ')}, ${TOOL_PARAMETERS_TERMS.unread}`] : []),
|
|
659
|
+
...misplaced.map((m) => `${misplacedShown(m)}, ${TOOL_PARAMETERS_TERMS.unread} here`),
|
|
660
|
+
...(rest.length > 0 ? [`${quotedList(rest)}, ${TOOL_PARAMETERS_TERMS.unread}`] : []),
|
|
661
|
+
];
|
|
662
|
+
// Where a field has one home, show it there, as the call would give it. A
|
|
663
|
+
// value shownValue cuts short isn't one the call could give, so then the
|
|
664
|
+
// parameter is named alone.
|
|
665
|
+
const moves = misplaced.filter((m) => m.homes.length === 1).map((m) => {
|
|
666
|
+
const it = m.keys.length === 1 ? 'it' : 'them';
|
|
667
|
+
const shown = m.keys.map((key) => [key, shownValue(given[key])] as const);
|
|
668
|
+
return shown.every(([key, value]) => value === JSON.stringify(given[key]))
|
|
669
|
+
? `Put ${it} in ${m.homes[0]}: {${shown.map(([key, value]) => `${JSON.stringify(key)}: ${value}`).join(', ')}}.`
|
|
670
|
+
: `Put ${it} in ${m.homes[0]}.`;
|
|
671
|
+
});
|
|
672
|
+
throw new Error(
|
|
673
|
+
`${tool} has ${has.join(', and ')}, so nothing was saved. ${moves.map((move) => `${move} `).join('')}`
|
|
674
|
+
+ `${TOOL_PARAMETERS_TERMS.listed} ${quotedList(params)}.`,
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
noteUnread(tool, rest, params, notes, TOOL_PARAMETERS_TERMS);
|
|
678
|
+
return given;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* An object's fields as the gate reads them: a null or "" is a field left out,
|
|
683
|
+
* and a note names each "", with what leaving it out does where that's more
|
|
684
|
+
* than setting no constraint (`means`, by field). Every other field stays an
|
|
685
|
+
* own field of the copy, "__proto__" included, which JSON gives as one: set by
|
|
686
|
+
* assignment, it would make its value the copy's prototype, whose fields the
|
|
687
|
+
* gate would then read as the object's own.
|
|
688
|
+
*/
|
|
689
|
+
function withoutEmpty(
|
|
690
|
+
where: string, obj: Record<string, unknown>, notes: string[], means: Readonly<Record<string, string>> = {},
|
|
691
|
+
): Record<string, unknown> {
|
|
692
|
+
const kept: Array<[string, unknown]> = [];
|
|
693
|
+
const empty: string[] = [];
|
|
694
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
695
|
+
if (value === null || value === undefined) continue;
|
|
696
|
+
if (value === '') empty.push(key);
|
|
697
|
+
else kept.push([key, value]);
|
|
698
|
+
}
|
|
699
|
+
const out: Record<string, unknown> = Object.fromEntries(kept);
|
|
700
|
+
if (empty.length > 0) {
|
|
701
|
+
const meant = empty.flatMap((key) => {
|
|
702
|
+
// An own field only: a name such as "constructor" would find Object.prototype's.
|
|
703
|
+
const what = Object.hasOwn(means, key) ? means[key] : undefined;
|
|
704
|
+
return what ? [what] : [];
|
|
705
|
+
});
|
|
706
|
+
notes.push(
|
|
707
|
+
`${where} has ${quotedList(empty)} as "", so ${empty.length === 1 ? "it's" : "they're"} left out`
|
|
708
|
+
+ `${meant.length > 0 ? `: ${meant.join(', and ')}` : ''}.`,
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
return out;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** What leaving out a rule's field does, where that's more than setting no constraint. */
|
|
715
|
+
const RULE_LEFT_OUT = { behavior: 'a rule without one has the behavior "always"', match: 'a rule without one matches every event' };
|
|
716
|
+
/** The same for gate.json's own fields, under the default it then has. */
|
|
717
|
+
function configLeftOut(defaultBehavior: unknown): Record<string, string> {
|
|
718
|
+
return {
|
|
719
|
+
default: 'its default is "always"',
|
|
720
|
+
policies: defaultBehavior === 'skip' || defaultBehavior === 'defer'
|
|
721
|
+
? `it has no rules, so under its default, "${defaultBehavior}", every event is held back`
|
|
722
|
+
: 'it has no rules, so every event gets the default, "always"',
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/** A problem a rule has. */
|
|
727
|
+
interface RuleProblem {
|
|
728
|
+
/** What's wrong: the field, what it holds, and what's needed. */
|
|
729
|
+
text: string;
|
|
730
|
+
/** How the rule is read past it ("so …"), the way earlier releases read it.
|
|
731
|
+
* Absent where they refused the rule, which is then left out. */
|
|
732
|
+
read?: string;
|
|
733
|
+
/** The reading only narrows what the rule matches (an empty scope entry),
|
|
734
|
+
* so a rule that holds events back holds back no more for it. */
|
|
735
|
+
narrows?: boolean;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/** A rule as the gate reads it. */
|
|
739
|
+
interface RuleReading {
|
|
740
|
+
/** How messages name it: `Policy "x"`, or its place when it has no name. */
|
|
741
|
+
where: string;
|
|
742
|
+
/** The rule as read, unless a problem has no reading. */
|
|
743
|
+
policy?: GatePolicy;
|
|
744
|
+
/** It wakes on none of what it matches, even where it can't be read: it
|
|
745
|
+
* skips or defers, or it's a rate limit whose tokens are a number not over
|
|
746
|
+
* 0. Left out, such a rule holds back nothing it was written to wake on,
|
|
747
|
+
* though what it matches goes on to the rules after it, as for any rule
|
|
748
|
+
* left out. */
|
|
749
|
+
quiet: boolean;
|
|
750
|
+
/** Every problem it has, in the order its fields are read. */
|
|
751
|
+
problems: RuleProblem[];
|
|
752
|
+
/** Its fields that do nothing, and those given as "", each a sentence. */
|
|
753
|
+
notes: string[];
|
|
754
|
+
/** Set where it would be in force, and nothing left in its match narrows it. */
|
|
755
|
+
matchesAll?: boolean;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* A rule's notes, ending with what a match that narrows nothing does. A
|
|
760
|
+
* refusal says it of the rule read without what's wrong in its match, as
|
|
761
|
+
* earlier releases read it, since that's why what's wrong there matters.
|
|
762
|
+
*/
|
|
763
|
+
function notesOf(reading: RuleReading, refused = false): string[] {
|
|
764
|
+
if (!reading.matchesAll) return reading.notes;
|
|
765
|
+
return [...reading.notes, refused
|
|
766
|
+
? "Read without what's wrong in its match, as earlier releases read it, the rule would match every event."
|
|
767
|
+
: `${reading.where}: nothing left in match narrows the rule, so it matches every event.`];
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/** Where a rule's fields are read into: the rule's name in messages, its problems, and its notes. */
|
|
771
|
+
interface Reader {
|
|
772
|
+
where: string;
|
|
773
|
+
/** How messages name the file the rule is in, for a field of the file's own put in the rule. */
|
|
774
|
+
file: string;
|
|
775
|
+
problems: RuleProblem[];
|
|
776
|
+
notes: string[];
|
|
777
|
+
/** Set where its rate limit's tokens are a number not over 0, so it wakes on none of what it matches. */
|
|
778
|
+
noTokens?: boolean;
|
|
779
|
+
/** Set where its match is left with nothing that narrows it, once something in it was named. */
|
|
780
|
+
matchesAll?: boolean;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/** How a slip is read: as a field left out, which does nothing. */
|
|
784
|
+
const SLIP_READ = 'so it does nothing';
|
|
785
|
+
/** How a field of the wrong type is read: as one left out. */
|
|
786
|
+
const DROPPED = 'so the rule is read without it';
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* An object's field names, at `level` of the rule (such as "match"): a slip of
|
|
790
|
+
* a field is a problem, read as the field left out, and so is a field the gate
|
|
791
|
+
* reads at another level, named with where that is (`misplacedReadOf` says
|
|
792
|
+
* how the rule then reads, where that's more than the field doing nothing);
|
|
793
|
+
* any other name the gate doesn't read is noted.
|
|
794
|
+
*/
|
|
795
|
+
function readFieldNames(
|
|
796
|
+
r: Reader, level: string | null, obj: Record<string, unknown>, fields: readonly string[],
|
|
797
|
+
readOf: (slip: Slip) => string = () => SLIP_READ,
|
|
798
|
+
misplacedReadOf: (m: Misplaced) => string | undefined = () => undefined,
|
|
799
|
+
): void {
|
|
800
|
+
const { slips, unread } = fieldNames(obj, fields);
|
|
801
|
+
const { misplaced, rest } = placedElsewhere(unread, homesOf(r.file, fields));
|
|
802
|
+
noteUnread(level ? `${r.where}: ${level}` : r.where, rest, fields, r.notes);
|
|
803
|
+
for (const slip of slips) {
|
|
804
|
+
r.problems.push({ text: `${level ?? 'it'} has ${slipShown(slip)}, which the gate doesn't read`, read: readOf(slip) });
|
|
805
|
+
}
|
|
806
|
+
for (const m of misplaced) {
|
|
807
|
+
r.problems.push({
|
|
808
|
+
text: `${level ?? 'it'} has ${misplacedShown(m)}, which the gate doesn't read here`,
|
|
809
|
+
read: misplacedReadOf(m) ?? (m.keys.length === 1 ? SLIP_READ : 'so they do nothing'),
|
|
810
|
+
});
|
|
286
811
|
}
|
|
812
|
+
}
|
|
287
813
|
|
|
288
|
-
|
|
814
|
+
/** "so that entry is dropped", "so those entries are dropped". */
|
|
815
|
+
function droppedEntries(count: number): string {
|
|
816
|
+
return count === 1 ? 'so that entry is dropped' : 'so those entries are dropped';
|
|
817
|
+
}
|
|
289
818
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
819
|
+
/**
|
|
820
|
+
* A rule, read as written, or past each problem it has. `unnamed` names a rule
|
|
821
|
+
* without a name in messages, and `file` the file it's in.
|
|
822
|
+
*/
|
|
823
|
+
function readRule(raw: unknown, unnamed: string, file = 'gate.json'): RuleReading {
|
|
824
|
+
if (!isPlainObject(raw)) {
|
|
825
|
+
return {
|
|
826
|
+
where: unnamed,
|
|
827
|
+
problems: [{ text: `it's ${shownValue(raw)}, where a rule is an object, such as {"name": "my-rule", "behavior": "always"}` }],
|
|
828
|
+
notes: [],
|
|
829
|
+
quiet: false,
|
|
830
|
+
};
|
|
293
831
|
}
|
|
832
|
+
const name = nameOf(raw);
|
|
833
|
+
const r: Reader = { where: name ? `Policy "${name}"` : unnamed, file, problems: [], notes: [] };
|
|
834
|
+
const obj = withoutEmpty(r.where, raw, r.notes, RULE_LEFT_OUT);
|
|
835
|
+
// Field names first, so a slip of one is named before what its absence does.
|
|
836
|
+
readFieldNames(r, null, obj, POLICY_FIELDS, (slip) => {
|
|
837
|
+
if (slip.meant.includes('behavior') && obj.behavior === undefined) return 'so the rule\'s behavior is "always"';
|
|
838
|
+
if (slip.meant.includes('match') && obj.match === undefined) return 'so the rule matches every event';
|
|
839
|
+
return SLIP_READ;
|
|
840
|
+
}, (m) => {
|
|
841
|
+
const of = (fields: readonly string[]) => m.keys.some((key) => fields.includes(key));
|
|
842
|
+
if (obj.match === undefined && (of(MATCH_FIELDS) || of(FILTER_FIELDS))) return 'so the rule matches every event';
|
|
843
|
+
if (obj.behavior === undefined && (of(BEHAVIOR_KINDS) || of(RATE_LIMIT_FIELDS) || of(PASSIVE_SAMPLE_FIELDS))) {
|
|
844
|
+
return 'so the rule\'s behavior is "always"';
|
|
845
|
+
}
|
|
846
|
+
return undefined;
|
|
847
|
+
});
|
|
848
|
+
if (!name) {
|
|
849
|
+
r.problems.push({
|
|
850
|
+
text: obj.name === undefined ? 'it has no "name"' : `name is ${shownValue(obj.name)}, where a non-empty string is needed`,
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
const { behavior, kind } = readBehavior(r, obj.behavior);
|
|
854
|
+
const match = readMatch(r, obj.match);
|
|
855
|
+
const resets = readResets(r, obj.resets);
|
|
856
|
+
const passthrough = readPassthrough(r, obj.passthrough, behavior, kind);
|
|
857
|
+
const reading: RuleReading = {
|
|
858
|
+
where: r.where, problems: r.problems, notes: r.notes, quiet: kind === 'skip' || kind === 'defer' || r.noTokens === true,
|
|
859
|
+
};
|
|
860
|
+
if (name && behavior && r.problems.every((p) => p.read !== undefined)) {
|
|
861
|
+
const policy: GatePolicy = { name, match, behavior };
|
|
862
|
+
if (resets) policy.resets = resets;
|
|
863
|
+
if (passthrough) policy.passthrough = true;
|
|
864
|
+
reading.policy = policy;
|
|
865
|
+
if (r.matchesAll) reading.matchesAll = true;
|
|
866
|
+
}
|
|
867
|
+
return reading;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
type BehaviorKind = 'always' | 'defer' | 'skip' | (typeof BEHAVIOR_KINDS)[number];
|
|
871
|
+
|
|
872
|
+
const BEHAVIORS_NEEDED = '"always", "defer" or "skip", or an object holding one of "debounce", "rate_limit" and "passive_sample"';
|
|
294
873
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
874
|
+
/**
|
|
875
|
+
* A rule's behavior: "always" when left out; one of the three names; or an
|
|
876
|
+
* object holding one of the timing kinds. Anything else but a list or an
|
|
877
|
+
* object was read as "always", a rule that wakes on every event it matches,
|
|
878
|
+
* and is read so; a list, or an object holding none of the kinds, was
|
|
879
|
+
* refused, and the rule is left out. Of
|
|
880
|
+
* two kinds or more, "debounce", then "rate_limit", then "passive_sample" was
|
|
881
|
+
* the one read, whatever their order, and is read so. Each other kind is read
|
|
882
|
+
* on its own as well, so that a refusal names its problems too, and a load
|
|
883
|
+
* reads past them, since it isn't read either way. `kind` is the behavior's
|
|
884
|
+
* kind even where it can't be read, for passthrough's check.
|
|
885
|
+
*/
|
|
886
|
+
function readBehavior(r: Reader, value: unknown): { behavior?: GateBehavior | undefined; kind?: BehaviorKind | undefined } {
|
|
887
|
+
if (value === undefined) return { behavior: 'always', kind: 'always' };
|
|
888
|
+
if (value === 'always' || value === 'defer' || value === 'skip') return { behavior: value, kind: value };
|
|
889
|
+
if (!isPlainObject(value)) {
|
|
890
|
+
if (Array.isArray(value)) {
|
|
891
|
+
r.problems.push({ text: `behavior is ${shownValue(value)}, where ${BEHAVIORS_NEEDED} is needed` });
|
|
892
|
+
return {};
|
|
299
893
|
}
|
|
894
|
+
r.problems.push({ text: `behavior is ${shownValue(value)}, where ${BEHAVIORS_NEEDED} is needed`, read: 'so it\'s read as "always"' });
|
|
895
|
+
return { behavior: 'always', kind: 'always' };
|
|
896
|
+
}
|
|
897
|
+
const b = withoutEmpty(`${r.where}: behavior`, value, r.notes);
|
|
898
|
+
readFieldNames(r, 'behavior', b, BEHAVIOR_KINDS);
|
|
899
|
+
const kinds = BEHAVIOR_KINDS.filter((k) => b[k] !== undefined);
|
|
900
|
+
const kind = kinds[0];
|
|
901
|
+
if (kind === undefined) {
|
|
902
|
+
r.problems.push({ text: 'behavior holds none of "debounce", "rate_limit" and "passive_sample"' });
|
|
903
|
+
return {};
|
|
904
|
+
}
|
|
905
|
+
if (kinds.length > 1) {
|
|
906
|
+
r.problems.push({ text: `behavior holds ${quotedList(kinds)}, and a rule has one behavior`, read: `so only its "${kind}" is read` });
|
|
907
|
+
}
|
|
908
|
+
const behavior = READ_KIND[kind](r, b[kind]);
|
|
909
|
+
for (const other of kinds.slice(1)) {
|
|
910
|
+
const unread: Reader = { where: r.where, file: r.file, problems: [], notes: [] };
|
|
911
|
+
READ_KIND[other](unread, b[other]);
|
|
912
|
+
for (const p of unread.problems) r.problems.push({ text: p.text, read: `which goes unread with the rest of "${other}"` });
|
|
913
|
+
}
|
|
914
|
+
return { behavior, kind };
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* A debounce: a number of milliseconds. One out of the gate's range is read
|
|
919
|
+
* at its nearest bound. Earlier releases refused it in gate.json and ran it
|
|
920
|
+
* as written in the starting rules, where one over 2,147,483,647 ms ran after
|
|
921
|
+
* 1 ms, the most a timer takes; read at the bound, a batch goes out later or
|
|
922
|
+
* sooner than it did there, and every event in it is still delivered.
|
|
923
|
+
*/
|
|
924
|
+
function readDebounce(r: Reader, ms: unknown): GateBehavior | undefined {
|
|
925
|
+
if (typeof ms !== 'number' || !Number.isFinite(ms)) {
|
|
926
|
+
r.problems.push({ text: `behavior.debounce is ${shownValue(ms)}, where a number of milliseconds is needed, such as 60000` });
|
|
927
|
+
return undefined;
|
|
928
|
+
}
|
|
929
|
+
if (ms < MIN_DEBOUNCE_MS) {
|
|
930
|
+
r.problems.push({
|
|
931
|
+
text: `behavior.debounce is ${ms} ms, under the ${MIN_DEBOUNCE_MS} ms minimum`, read: `so it's read as ${MIN_DEBOUNCE_MS} ms`,
|
|
932
|
+
});
|
|
933
|
+
return { debounce: MIN_DEBOUNCE_MS };
|
|
300
934
|
}
|
|
935
|
+
if (ms > MAX_DEBOUNCE_MS) {
|
|
936
|
+
r.problems.push({
|
|
937
|
+
text: `behavior.debounce is ${ms} ms, over the ${MAX_DEBOUNCE_MS} ms maximum`, read: `so it's read as ${MAX_DEBOUNCE_MS} ms`,
|
|
938
|
+
});
|
|
939
|
+
return { debounce: MAX_DEBOUNCE_MS };
|
|
940
|
+
}
|
|
941
|
+
return { debounce: ms };
|
|
942
|
+
}
|
|
301
943
|
|
|
302
|
-
|
|
944
|
+
/**
|
|
945
|
+
* A rate limit. A refill interval under 0 ran as a bucket that never refills,
|
|
946
|
+
* waking on its first tokens and then on none of what the rule matched, and
|
|
947
|
+
* is read so. One of 0 refilled the bucket once and then never, its clock left
|
|
948
|
+
* at NaN; it's read as refilling at once, every time, so the rule limits
|
|
949
|
+
* nothing, which holds back nothing it woke the agent on (applyRateLimit). A
|
|
950
|
+
* rule whose tokens aren't a positive number is left out, as earlier releases'
|
|
951
|
+
* check refused it. Tokens that are a number not over 0, which ran unchecked
|
|
952
|
+
* from the starting rules, woke the agent on nothing the rule matched, so that
|
|
953
|
+
* rule is quiet (noTokens): leaving it out holds back nothing it was written
|
|
954
|
+
* to wake on, though what it matches goes on to the rules after it, as for any
|
|
955
|
+
* rule left out.
|
|
956
|
+
*/
|
|
957
|
+
function readRateLimit(r: Reader, value: unknown): GateBehavior | undefined {
|
|
958
|
+
if (!isPlainObject(value)) {
|
|
959
|
+
r.problems.push({
|
|
960
|
+
text: `behavior.rate_limit is ${shownValue(value)}, where an object is needed, such as {"tokens": 3, "refillIntervalMs": 60000}`,
|
|
961
|
+
});
|
|
962
|
+
return undefined;
|
|
963
|
+
}
|
|
964
|
+
const conf = withoutEmpty(`${r.where}: behavior.rate_limit`, value, r.notes, { keyBy: 'every event the rule matches shares one bucket' });
|
|
965
|
+
readFieldNames(r, 'behavior.rate_limit', conf, RATE_LIMIT_FIELDS);
|
|
966
|
+
const tokens = conf.tokens;
|
|
967
|
+
const refill = conf.refillIntervalMs;
|
|
968
|
+
const tokensUsable = typeof tokens === 'number' && Number.isFinite(tokens) && tokens > 0;
|
|
969
|
+
if (typeof tokens === 'number' && !(tokens > 0)) r.noTokens = true;
|
|
970
|
+
if (!tokensUsable) {
|
|
971
|
+
r.problems.push({ text: `behavior.rate_limit.tokens is ${shownValue(tokens)}, where a positive number is needed` });
|
|
972
|
+
}
|
|
973
|
+
const refillUsable = typeof refill === 'number' && Number.isFinite(refill);
|
|
974
|
+
if (!refillUsable) {
|
|
975
|
+
r.problems.push({
|
|
976
|
+
text: `behavior.rate_limit.refillIntervalMs is ${shownValue(refill)}, where a positive number of milliseconds is needed`,
|
|
977
|
+
});
|
|
978
|
+
} else if (refill <= 0) {
|
|
979
|
+
r.problems.push({
|
|
980
|
+
text: `behavior.rate_limit.refillIntervalMs is ${refill}, where a positive number of milliseconds is needed`,
|
|
981
|
+
read: refill === 0
|
|
982
|
+
? 'so its tokens refill at once, and it limits nothing it matches'
|
|
983
|
+
: "so its tokens never refill, and once they're spent it wakes the agent on nothing it matches",
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
const keyBy = readKeyBy(r, 'rate_limit', conf.keyBy);
|
|
987
|
+
if (!tokensUsable || !refillUsable) return undefined;
|
|
988
|
+
const out: { tokens: number; refillIntervalMs: number; keyBy?: string } = { tokens, refillIntervalMs: refill };
|
|
989
|
+
if (keyBy !== undefined) out.keyBy = keyBy;
|
|
990
|
+
return { rate_limit: out };
|
|
303
991
|
}
|
|
304
992
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
993
|
+
/**
|
|
994
|
+
* A sample. A step at or under 0 sampled every event, as 1 does, and a
|
|
995
|
+
* fraction its next whole number, since the count was compared with it, so
|
|
996
|
+
* each is read that way.
|
|
997
|
+
*/
|
|
998
|
+
function readPassiveSample(r: Reader, value: unknown): GateBehavior | undefined {
|
|
999
|
+
if (!isPlainObject(value)) {
|
|
1000
|
+
r.problems.push({ text: `behavior.passive_sample is ${shownValue(value)}, where an object is needed, such as {"every": 10}` });
|
|
1001
|
+
return undefined;
|
|
1002
|
+
}
|
|
1003
|
+
const conf = withoutEmpty(`${r.where}: behavior.passive_sample`, value, r.notes, { keyBy: 'every event the rule matches shares one count' });
|
|
1004
|
+
readFieldNames(r, 'behavior.passive_sample', conf, PASSIVE_SAMPLE_FIELDS);
|
|
1005
|
+
const given = conf.every;
|
|
1006
|
+
let every: number | undefined;
|
|
1007
|
+
if (typeof given !== 'number' || !Number.isFinite(given)) {
|
|
1008
|
+
r.problems.push({ text: `behavior.passive_sample.every is ${shownValue(given)}, where a whole number of 1 or more is needed` });
|
|
1009
|
+
} else if (Number.isInteger(given) && given > 0) {
|
|
1010
|
+
every = given;
|
|
1011
|
+
} else {
|
|
1012
|
+
every = given <= 0 ? 1 : Math.ceil(given);
|
|
1013
|
+
r.problems.push({
|
|
1014
|
+
text: `behavior.passive_sample.every is ${given}, ${given <= 0 ? 'under 1' : 'not a whole number'}`,
|
|
1015
|
+
read: `so it's read as ${every}`,
|
|
1016
|
+
});
|
|
308
1017
|
}
|
|
1018
|
+
const keyBy = readKeyBy(r, 'passive_sample', conf.keyBy);
|
|
1019
|
+
if (every === undefined) return undefined;
|
|
1020
|
+
const out: { every: number; keyBy?: string } = { every };
|
|
1021
|
+
if (keyBy !== undefined) out.keyBy = keyBy;
|
|
1022
|
+
return { passive_sample: out };
|
|
1023
|
+
}
|
|
309
1024
|
|
|
310
|
-
|
|
1025
|
+
/** How each kind of behavior object is read. */
|
|
1026
|
+
const READ_KIND: Readonly<Record<(typeof BEHAVIOR_KINDS)[number], (r: Reader, value: unknown) => GateBehavior | undefined>> = {
|
|
1027
|
+
debounce: readDebounce,
|
|
1028
|
+
rate_limit: readRateLimit,
|
|
1029
|
+
passive_sample: readPassiveSample,
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
/** A timing kind's keyBy: a field name. One of another type was read as none. */
|
|
1033
|
+
function readKeyBy(r: Reader, kind: 'rate_limit' | 'passive_sample', value: unknown): string | undefined {
|
|
1034
|
+
if (value === undefined) return undefined;
|
|
1035
|
+
if (typeof value === 'string') return value;
|
|
1036
|
+
r.problems.push({
|
|
1037
|
+
text: `behavior.${kind}.keyBy is ${shownValue(value)}, where a field name is needed, such as "channelId"`,
|
|
1038
|
+
read: `so it does nothing, and every event the rule matches shares one ${kind === 'rate_limit' ? 'bucket' : 'count'}`,
|
|
1039
|
+
});
|
|
1040
|
+
return undefined;
|
|
1041
|
+
}
|
|
311
1042
|
|
|
312
|
-
|
|
313
|
-
|
|
1043
|
+
/**
|
|
1044
|
+
* A rule's match: every field optional, and one left out matches anything. A
|
|
1045
|
+
* field of the wrong type, or an entry of one in a list, was dropped, which
|
|
1046
|
+
* widened the rule, and is read so; an empty entry in a list, dropped, but for
|
|
1047
|
+
* one in `scope`, which was kept and narrows the rule, since no event's type is
|
|
1048
|
+
* "". A match that isn't an object matched every event, and so does one left
|
|
1049
|
+
* with nothing that narrows it, which readRule notes, for a rule it can read,
|
|
1050
|
+
* where something in the match was named: dropped, left out or unread.
|
|
1051
|
+
*/
|
|
1052
|
+
function readMatch(r: Reader, value: unknown): GatePolicyMatch {
|
|
1053
|
+
const match: GatePolicyMatch = {};
|
|
1054
|
+
if (value === undefined) return match;
|
|
1055
|
+
if (!isPlainObject(value)) {
|
|
1056
|
+
r.problems.push({
|
|
1057
|
+
text: `match is ${shownValue(value)}, where an object is needed, such as {"source": "discord"}`,
|
|
1058
|
+
read: 'so the rule matches every event',
|
|
1059
|
+
});
|
|
1060
|
+
return match;
|
|
314
1061
|
}
|
|
1062
|
+
const named = r.problems.length + r.notes.length;
|
|
1063
|
+
const m = withoutEmpty(`${r.where}: match`, value, r.notes);
|
|
1064
|
+
readFieldNames(r, 'match', m, MATCH_FIELDS);
|
|
315
1065
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
throw new Error(`Policy "${obj.name}": debounce must be <= ${MAX_DEBOUNCE_MS}ms, got ${b.debounce}`);
|
|
331
|
-
}
|
|
332
|
-
behavior = { debounce: b.debounce };
|
|
333
|
-
} else if ('rate_limit' in b) {
|
|
334
|
-
const rl = b.rate_limit;
|
|
335
|
-
if (!rl || typeof rl !== 'object') {
|
|
336
|
-
throw new Error(`Policy "${obj.name}": rate_limit must be an object`);
|
|
337
|
-
}
|
|
338
|
-
const conf = rl as Record<string, unknown>;
|
|
339
|
-
if (typeof conf.tokens !== 'number' || !Number.isFinite(conf.tokens) || conf.tokens <= 0) {
|
|
340
|
-
throw new Error(`Policy "${obj.name}": rate_limit.tokens must be a positive number`);
|
|
341
|
-
}
|
|
342
|
-
if (typeof conf.refillIntervalMs !== 'number' || !Number.isFinite(conf.refillIntervalMs) || conf.refillIntervalMs <= 0) {
|
|
343
|
-
throw new Error(`Policy "${obj.name}": rate_limit.refillIntervalMs must be a positive number`);
|
|
344
|
-
}
|
|
345
|
-
const out: { tokens: number; refillIntervalMs: number; keyBy?: string } = {
|
|
346
|
-
tokens: conf.tokens,
|
|
347
|
-
refillIntervalMs: conf.refillIntervalMs,
|
|
348
|
-
};
|
|
349
|
-
if (typeof conf.keyBy === 'string' && conf.keyBy.length > 0) {
|
|
350
|
-
out.keyBy = conf.keyBy;
|
|
1066
|
+
if (m.scope !== undefined) {
|
|
1067
|
+
if (!Array.isArray(m.scope)) {
|
|
1068
|
+
r.problems.push({
|
|
1069
|
+
text: `match.scope is ${shownValue(m.scope)}, where a list of event types is needed, such as ["mcpl:channel-incoming"]`,
|
|
1070
|
+
read: 'so the rule matches every type of event',
|
|
1071
|
+
});
|
|
1072
|
+
} else {
|
|
1073
|
+
const types = m.scope.filter((s): s is string => typeof s === 'string');
|
|
1074
|
+
const dropped = m.scope.filter((s) => typeof s !== 'string');
|
|
1075
|
+
if (dropped.length > 0) {
|
|
1076
|
+
r.problems.push({
|
|
1077
|
+
text: `match.scope holds ${shownValues(dropped)}, which ${dropped.length === 1 ? "isn't an event type" : "aren't event types"}`,
|
|
1078
|
+
read: types.length > 0 ? droppedEntries(dropped.length) : `${droppedEntries(dropped.length)}, and the rule matches every type of event`,
|
|
1079
|
+
});
|
|
351
1080
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
1081
|
+
if (types.includes('')) {
|
|
1082
|
+
r.problems.push({
|
|
1083
|
+
text: 'match.scope holds "", and no event\'s type is ""',
|
|
1084
|
+
read: types.every((t) => t === '') ? 'so the rule matches no event' : 'so that entry matches no event',
|
|
1085
|
+
narrows: true,
|
|
1086
|
+
});
|
|
357
1087
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
1088
|
+
match.scope = types;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
for (const [field, example] of [
|
|
1092
|
+
['source', '"discord"'], ['channel', '"discord:*:12345"'], ['mount', '"project"'], ['pathGlob', '"notes/*.md"'],
|
|
1093
|
+
] as const) {
|
|
1094
|
+
const given = m[field];
|
|
1095
|
+
if (given === undefined) continue;
|
|
1096
|
+
if (typeof given === 'string') match[field] = given;
|
|
1097
|
+
else r.problems.push({ text: `match.${field} is ${shownValue(given)}, where a string is needed, such as ${example}`, read: DROPPED });
|
|
1098
|
+
}
|
|
1099
|
+
for (const [field, example] of [
|
|
1100
|
+
['metadataTrue', '["mentioned"]'], ['tagsAny', '["chat:ambient"]'], ['tagsAll', '["chat:ambient"]'], ['tagsNone', '["chat:ambient"]'],
|
|
1101
|
+
] as const) {
|
|
1102
|
+
const given = m[field];
|
|
1103
|
+
if (given === undefined) continue;
|
|
1104
|
+
if (!Array.isArray(given)) {
|
|
1105
|
+
r.problems.push({
|
|
1106
|
+
text: `match.${field} is ${shownValue(given)}, where a list of non-empty strings is needed, such as ${example}`, read: DROPPED,
|
|
1107
|
+
});
|
|
1108
|
+
continue;
|
|
1109
|
+
}
|
|
1110
|
+
const kept = given.filter((s): s is string => typeof s === 'string' && s.length > 0);
|
|
1111
|
+
const dropped = given.filter((s) => typeof s !== 'string' || s.length === 0);
|
|
1112
|
+
if (dropped.length > 0) {
|
|
1113
|
+
r.problems.push({
|
|
1114
|
+
text: `match.${field} holds ${shownValues(dropped)}, where each entry is a non-empty string`,
|
|
1115
|
+
read: kept.length > 0 ? droppedEntries(dropped.length) : `${droppedEntries(dropped.length)}, and with none left the list narrows nothing`,
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
if (kept.length > 0) match[field] = kept;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
if (m.filter !== undefined) {
|
|
1122
|
+
if (!isPlainObject(m.filter)) {
|
|
1123
|
+
r.problems.push({
|
|
1124
|
+
text: `match.filter is ${shownValue(m.filter)}, where an object is needed, such as {"type": "text", "pattern": "release"}`,
|
|
1125
|
+
read: DROPPED,
|
|
1126
|
+
});
|
|
1127
|
+
} else {
|
|
1128
|
+
// An empty pattern matches every event's content, so a filter with one
|
|
1129
|
+
// narrows nothing, as before, and it's left out, its other problems named.
|
|
1130
|
+
const emptyPattern = m.filter.pattern === '';
|
|
1131
|
+
const f = withoutEmpty(`${r.where}: match.filter`, emptyPattern ? { ...m.filter, pattern: undefined } : m.filter, r.notes);
|
|
1132
|
+
readFieldNames(r, 'match.filter', f, FILTER_FIELDS);
|
|
1133
|
+
const without = 'so the rule is read without its filter';
|
|
1134
|
+
const type = f.type === 'text' || f.type === 'regex' ? f.type : undefined;
|
|
1135
|
+
if (type === undefined) {
|
|
1136
|
+
r.problems.push({ text: `match.filter.type is ${shownValue(f.type)}, where "text" or "regex" is needed`, read: without });
|
|
361
1137
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
1138
|
+
if (emptyPattern) {
|
|
1139
|
+
r.notes.push(`${r.where}: match.filter's pattern is "", so the filter narrows nothing, and it's left out.`);
|
|
1140
|
+
} else if (typeof f.pattern !== 'string') {
|
|
1141
|
+
r.problems.push({
|
|
1142
|
+
text: `match.filter.pattern is ${shownValue(f.pattern)}, where the text or regular expression to look for is needed`,
|
|
1143
|
+
read: without,
|
|
1144
|
+
});
|
|
1145
|
+
} else if (type === 'text') {
|
|
1146
|
+
match.filter = { type, pattern: f.pattern };
|
|
1147
|
+
} else if (type === 'regex') {
|
|
1148
|
+
try {
|
|
1149
|
+
new RegExp(f.pattern);
|
|
1150
|
+
match.filter = { type, pattern: f.pattern };
|
|
1151
|
+
} catch (e) {
|
|
1152
|
+
r.problems.push({ text: `match.filter.pattern isn't a regular expression the gate can read (${errorText(e)})` });
|
|
1153
|
+
}
|
|
365
1154
|
}
|
|
366
|
-
behavior = { passive_sample: out };
|
|
367
|
-
} else {
|
|
368
|
-
throw new Error(`Policy "${obj.name}": unknown behavior — expected always | defer | skip | { debounce } | { rate_limit } | { passive_sample }`);
|
|
369
1155
|
}
|
|
370
|
-
} else {
|
|
371
|
-
behavior = 'always';
|
|
372
1156
|
}
|
|
373
1157
|
|
|
374
|
-
//
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
1158
|
+
// Left with nothing that narrows it once something in it was named, it
|
|
1159
|
+
// matches every event. Empty lists and nulls alone always meant no
|
|
1160
|
+
// constraint, and name nothing.
|
|
1161
|
+
const narrows = Object.entries(match).some(([field, v]) => !(field === 'scope' && Array.isArray(v) && v.length === 0));
|
|
1162
|
+
if (!narrows && r.problems.length + r.notes.length > named) r.matchesAll = true;
|
|
1163
|
+
return match;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
/** A rule's resets: names of rules whose state it clears when it fires. An entry that isn't a name was dropped. */
|
|
1167
|
+
function readResets(r: Reader, value: unknown): string[] | undefined {
|
|
1168
|
+
if (value === undefined) return undefined;
|
|
1169
|
+
if (!Array.isArray(value)) {
|
|
1170
|
+
r.problems.push({
|
|
1171
|
+
text: `resets is ${shownValue(value)}, where a list of rule names is needed, such as ["other-rule"]`,
|
|
1172
|
+
read: 'so the rule resets no other',
|
|
1173
|
+
});
|
|
1174
|
+
return undefined;
|
|
1175
|
+
}
|
|
1176
|
+
// Unknown names are ignored at runtime, so reload races don't blow up.
|
|
1177
|
+
const names = value.filter((s): s is string => typeof s === 'string' && s.length > 0);
|
|
1178
|
+
const dropped = value.filter((s) => typeof s !== 'string' || s.length === 0);
|
|
1179
|
+
if (dropped.length > 0) {
|
|
1180
|
+
r.problems.push({
|
|
1181
|
+
text: `resets holds ${shownValues(dropped)}, where each entry is a rule's name`,
|
|
1182
|
+
read: names.length > 0 ? droppedEntries(dropped.length) : `${droppedEntries(dropped.length)}, and the rule resets no other`,
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
return names.length > 0 ? names : undefined;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* A rule's passthrough. It's meaningful only on the counting behaviors, where
|
|
1190
|
+
* "matched but didn't fire" is a distinct outcome that can fall through. On
|
|
1191
|
+
* always, defer, skip or a debounce it would be a no-op at best and a
|
|
1192
|
+
* misleading promise at worst, so it's refused rather than ignored.
|
|
1193
|
+
*/
|
|
1194
|
+
function readPassthrough(
|
|
1195
|
+
r: Reader, value: unknown, behavior: GateBehavior | undefined, kind: BehaviorKind | undefined,
|
|
1196
|
+
): boolean {
|
|
1197
|
+
if (value === undefined || value === false) return false;
|
|
1198
|
+
if (value !== true) {
|
|
1199
|
+
r.problems.push({ text: `passthrough is ${shownValue(value)}, where true or false is needed` });
|
|
1200
|
+
return false;
|
|
1201
|
+
}
|
|
1202
|
+
if (kind === undefined || kind === 'rate_limit' || kind === 'passive_sample') return true;
|
|
1203
|
+
r.problems.push({
|
|
1204
|
+
text: `passthrough is only valid with rate_limit or passive_sample behaviors — for "${behavior ? formatBehavior(behavior) : kind}" `
|
|
1205
|
+
+ 'there is no non-firing match to fall through on',
|
|
1206
|
+
});
|
|
1207
|
+
return false;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
/** What a load or a start does with a rule. */
|
|
1211
|
+
type RuleOutcome = 'as-written' | 'read' | 'left-out';
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* A rule with no problem is in force as written. One with a problem is read
|
|
1215
|
+
* past it, the way earlier releases read it, so it wakes the agent wherever
|
|
1216
|
+
* they did and holds back what they held back, a rule that skips or defers
|
|
1217
|
+
* included. It's left out only where they refused it.
|
|
1218
|
+
*/
|
|
1219
|
+
function ruleOutcome(reading: RuleReading): RuleOutcome {
|
|
1220
|
+
if (reading.problems.length === 0) return 'as-written';
|
|
1221
|
+
return reading.policy === undefined ? 'left-out' : 'read';
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/** What a load or a start says of a rule: how it's read past its problems, or that it's left out, and its notes. */
|
|
1225
|
+
function readingSentences(reading: RuleReading, outcome: RuleOutcome): string[] {
|
|
1226
|
+
const said: string[] = [];
|
|
1227
|
+
if (outcome === 'read') {
|
|
1228
|
+
// Read past a problem that can widen it, a rule that holds events back holds back what it now matches.
|
|
1229
|
+
const behavior = reading.policy?.behavior;
|
|
1230
|
+
const holds = (behavior === 'skip' || behavior === 'defer') && reading.problems.some((p) => !p.narrows)
|
|
1231
|
+
? ` Read so, it ${behavior === 'skip' ? 'skips' : 'defers'} every event it now matches, as earlier releases did.`
|
|
1232
|
+
: '';
|
|
1233
|
+
said.push(`${reading.where}: ${reading.problems.map((p) => `${p.text}, ${p.read}`).join('; ')}.${holds}`);
|
|
1234
|
+
} else if (outcome === 'left-out') {
|
|
1235
|
+
said.push(`${reading.where} is left out: ${reading.problems.map((p) => p.text).join('; ')}.`);
|
|
1236
|
+
}
|
|
1237
|
+
return [...said, ...notesOf(reading)];
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/** addPolicy's refusal of a rule with problems: every one of them, and its notes. */
|
|
1241
|
+
function refusalOf(reading: RuleReading): string {
|
|
1242
|
+
return [`${reading.where} wasn't saved: ${reading.problems.map((p) => p.text).join('; ')}.`, ...notesOf(reading, true)].join(' ');
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
/** gate.json, or the host's starting rules, as the gate reads them. */
|
|
1246
|
+
type ConfigReading =
|
|
1247
|
+
/** Not used: why, and what a change would edit, since one that parses is
|
|
1248
|
+
* edited as written, whether it's used or not (rulesForChange); where it's
|
|
1249
|
+
* the rules left out, each one's name, or undefined for one without. */
|
|
1250
|
+
| { refused: string; listed: Listed; silenced?: Array<string | undefined> }
|
|
1251
|
+
| {
|
|
1252
|
+
/** What's in force from it. */
|
|
1253
|
+
config: GateConfig;
|
|
1254
|
+
/** Each rule as given, as read, and what's done with it. */
|
|
1255
|
+
rules: Array<{ raw: unknown; reading: RuleReading; outcome: RuleOutcome }>;
|
|
1256
|
+
/** What the gate says of it, each a sentence: its own problems and notes, then each rule's. */
|
|
1257
|
+
sentences: string[];
|
|
1258
|
+
/** Its own fields as loaded, and its rule list, each rule as written, for
|
|
1259
|
+
* a write to edit by name; or, for one with no rule list to edit (a list,
|
|
1260
|
+
* or a "policies" that isn't one and holds something), what it is instead. */
|
|
1261
|
+
listed: Listed;
|
|
1262
|
+
};
|
|
1263
|
+
|
|
1264
|
+
/** A gate.json's own fields and its rule list, as written, or why it has none a write could edit. */
|
|
1265
|
+
type Listed = { top: Record<string, unknown>; rules: unknown[] } | { noList: string };
|
|
1266
|
+
|
|
1267
|
+
/** A rule's name as written, when it has a usable one. */
|
|
1268
|
+
function nameOf(rule: unknown): string | undefined {
|
|
1269
|
+
return isPlainObject(rule) && typeof rule.name === 'string' && rule.name ? rule.name : undefined;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/** What a load of gate.json says, from what its reading says of it: nothing, when that's nothing. */
|
|
1273
|
+
function loadNote(sentences: readonly string[]): string | null {
|
|
1274
|
+
return sentences.length > 0 ? `gate.json loaded. ${sentences.join(' ')}` : null;
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
/** The names of the rules a gate.json holds as written: none, for one with no rule list. */
|
|
1278
|
+
function heldNames(listed: Listed): Set<string> {
|
|
1279
|
+
return new Set('noList' in listed ? [] : listed.rules.map(nameOf).filter((name): name is string => name !== undefined));
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* gate.json, or the host's starting rules, read (`file` names which in
|
|
1284
|
+
* messages). A file earlier releases loaded loads as they read it, with a
|
|
1285
|
+
* note naming what they didn't read: a list is read as no rules under the
|
|
1286
|
+
* default "always", and a slip of "policies", a name the gate doesn't read
|
|
1287
|
+
* (a list of rules parked under one included), a field of a rule at the
|
|
1288
|
+
* file's top and a "policies" that isn't a list each as nothing, so where
|
|
1289
|
+
* "policies" writes no rules the file has none, and its default decides every
|
|
1290
|
+
* event. A "policies" holding nothing, an empty list or one that isn't a
|
|
1291
|
+
* list, writes no rules, as one left out does. Earlier releases refused it
|
|
1292
|
+
* whole where it isn't an object, or its default isn't one of the three, and
|
|
1293
|
+
* it isn't used. Each rule is read on its own. Under a default that holds
|
|
1294
|
+
* events back, a rule left out that could wake the agent would hold back what
|
|
1295
|
+
* it was written to wake on, so the file isn't used, as earlier releases,
|
|
1296
|
+
* which refused such a rule and so the file, didn't use it. `start` is what a
|
|
1297
|
+
* later start ran over a gate.json with no rules of its own, for its note: the
|
|
1298
|
+
* host's starting rules beside one with no rule list, or, where gate.json
|
|
1299
|
+
* isn't used at that start, the clause saying why and what runs instead
|
|
1300
|
+
* (notUsedClause), which the sentence saying it has no rules carries.
|
|
1301
|
+
*/
|
|
1302
|
+
function readConfig(
|
|
1303
|
+
raw: unknown, file: 'gate.json' | 'the starting config', start?: 'beside' | { notUsed: string },
|
|
1304
|
+
): ConfigReading {
|
|
1305
|
+
const File = file === 'gate.json' ? file : 'The starting config';
|
|
1306
|
+
if (Array.isArray(raw)) {
|
|
1307
|
+
return {
|
|
1308
|
+
config: { policies: [], default: 'always' },
|
|
1309
|
+
rules: [],
|
|
1310
|
+
sentences: [
|
|
1311
|
+
`${File} is a list, where an object is needed, such as {"policies": [{"name": "my-rule", "behavior": "always"}]}, `
|
|
1312
|
+
+ (start === 'beside'
|
|
1313
|
+
? 'so none of it is read, and the gate runs the host\'s starting rules beside it, under the default, "always".'
|
|
1314
|
+
: 'so none of it is read, and every event gets the default, "always".'),
|
|
1315
|
+
],
|
|
1316
|
+
listed: { noList: `${File} is a list` },
|
|
391
1317
|
};
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
1318
|
+
}
|
|
1319
|
+
if (!isPlainObject(raw)) return { refused: `${file} must be a JSON object`, listed: { noList: `${File} is ${shownValue(raw)}` } };
|
|
1320
|
+
|
|
1321
|
+
const sentences: string[] = [];
|
|
1322
|
+
// At a start that doesn't use it, what having no rules does is that.
|
|
1323
|
+
const notUsedWhy = typeof start === 'object' ? start.notUsed : undefined;
|
|
1324
|
+
const notUsed = notUsedWhy === undefined ? undefined : `it has no rules, so ${notUsedWhy}`;
|
|
1325
|
+
const obj = withoutEmpty(file, raw, sentences, { ...configLeftOut(raw.default), ...(notUsed ? { policies: notUsed } : {}) });
|
|
1326
|
+
// Here a name holding "default" or "policies" isn't a slip of it: at the
|
|
1327
|
+
// top, such a name can be a label of the file ("policiesUpdated").
|
|
1328
|
+
const names = fieldNames(obj, CONFIG_FIELDS, false);
|
|
1329
|
+
const { misplaced, rest } = placedElsewhere(names.unread, homesOf(file, CONFIG_FIELDS));
|
|
1330
|
+
noteUnread(file, rest, CONFIG_FIELDS, sentences);
|
|
1331
|
+
for (const m of misplaced) {
|
|
1332
|
+
sentences.push(
|
|
1333
|
+
`${file} has ${misplacedShown(m)}, which the gate doesn't read here, so ${m.keys.length === 1 ? 'it does' : 'they do'} nothing.`,
|
|
1334
|
+
);
|
|
1335
|
+
}
|
|
1336
|
+
// A "policies" holding nothing writes no rules, as one left out does.
|
|
1337
|
+
const noPolicies = obj.policies === undefined || holdsNothing(obj.policies);
|
|
1338
|
+
// What a change edits, used or not: its own fields and rule list as written
|
|
1339
|
+
// (a list left out, null or "", or one holding nothing, is an empty one),
|
|
1340
|
+
// beside whatever else it holds; or, where "policies" isn't a list and holds
|
|
1341
|
+
// something, what it is instead, since a list there would write over it.
|
|
1342
|
+
const listed: Listed = noPolicies || Array.isArray(obj.policies)
|
|
1343
|
+
? { top: raw, rules: Array.isArray(obj.policies) ? obj.policies : [] }
|
|
1344
|
+
: { noList: `${File}'s "policies" is ${shownValue(obj.policies)}` };
|
|
1345
|
+
const given = obj.default;
|
|
1346
|
+
if (given !== undefined && given !== 'always' && given !== 'defer' && given !== 'skip') {
|
|
1347
|
+
return { refused: `${file} "default" must be "always", "defer", or "skip", got: ${shownValue(given)}`, listed };
|
|
1348
|
+
}
|
|
1349
|
+
const defaultBehavior = given ?? 'always';
|
|
1350
|
+
// Beside a start that runs the host's starting rules, since it has no list to add them to, they're what its default
|
|
1351
|
+
// meets; at one that doesn't use it, what runs in its place is.
|
|
1352
|
+
const noRules = start === 'beside'
|
|
1353
|
+
? `it has no rules, so the gate runs the host's starting rules beside it, under its default, "${defaultBehavior}"`
|
|
1354
|
+
: notUsed ?? configLeftOut(defaultBehavior).policies;
|
|
1355
|
+
for (const slip of names.slips) {
|
|
1356
|
+
let read = SLIP_READ;
|
|
1357
|
+
// Where "policies" writes no rules, what a slip of it holds leaves the file with none.
|
|
1358
|
+
if (slip.meant.includes('policies') && noPolicies) read = `and ${noRules}`;
|
|
1359
|
+
else if (slip.meant.includes('default') && obj.default === undefined) read = 'so its default is "always"';
|
|
1360
|
+
sentences.push(`${File} has ${slipShown(slip)}, which the gate doesn't read, ${read}.`);
|
|
1361
|
+
}
|
|
1362
|
+
const policies: unknown[] = Array.isArray(obj.policies) ? obj.policies : [];
|
|
1363
|
+
if (!Array.isArray(obj.policies) && obj.policies !== undefined) {
|
|
1364
|
+
sentences.push(
|
|
1365
|
+
`${File}'s "policies" is ${shownValue(obj.policies)}, where a list of rules is needed, such as `
|
|
1366
|
+
+ `[{"name": "my-rule", "behavior": "always"}], and ${noRules}.`,
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
const rules = policies.map((rule, i) => {
|
|
1371
|
+
const reading = readRule(rule, `The ${ordinal(i + 1)} policy`, file);
|
|
1372
|
+
return { raw: rule, reading, outcome: ruleOutcome(reading) };
|
|
1373
|
+
});
|
|
1374
|
+
if (defaultBehavior !== 'always') {
|
|
1375
|
+
const silenced = rules.filter((rule) => rule.outcome === 'left-out' && !rule.reading.quiet);
|
|
1376
|
+
if (silenced.length > 0) {
|
|
1377
|
+
const one = silenced.length === 1;
|
|
1378
|
+
return {
|
|
1379
|
+
refused: [
|
|
1380
|
+
`${file} leaves out ${one ? 'a rule' : 'rules'} that could wake the agent, and under its default, "${defaultBehavior}", `
|
|
1381
|
+
+ `what ${one ? 'it was' : 'they were'} written to wake it on would be held back.`,
|
|
1382
|
+
...silenced.flatMap((rule) => readingSentences(rule.reading, rule.outcome)),
|
|
1383
|
+
].join(' '),
|
|
1384
|
+
listed,
|
|
1385
|
+
silenced: silenced.map((rule) => nameOf(rule.raw)),
|
|
1386
|
+
};
|
|
405
1387
|
}
|
|
406
1388
|
}
|
|
1389
|
+
for (const rule of rules) sentences.push(...readingSentences(rule.reading, rule.outcome));
|
|
1390
|
+
// A list with no rules, or none the gate reads, where no sentence above said it has none.
|
|
1391
|
+
if (notUsed !== undefined && !sentences.some((said) => said.includes(notUsed))) {
|
|
1392
|
+
sentences.push(`${File} has no rules${rules.length > 0 ? ' but ones the gate leaves out' : ''}, so ${notUsedWhy}.`);
|
|
1393
|
+
}
|
|
1394
|
+
return {
|
|
1395
|
+
config: {
|
|
1396
|
+
policies: rules.filter((rule) => rule.outcome !== 'left-out').map((rule) => rule.reading.policy as GatePolicy),
|
|
1397
|
+
default: defaultBehavior,
|
|
1398
|
+
},
|
|
1399
|
+
rules,
|
|
1400
|
+
sentences,
|
|
1401
|
+
listed,
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
407
1404
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
1405
|
+
/** One of the host's starting rules: as given, with its name when it has one, and as read. */
|
|
1406
|
+
interface StartingRule { name: string | undefined; raw: unknown; policy: GatePolicy | undefined; quiet: boolean }
|
|
1407
|
+
|
|
1408
|
+
/** The host's starting rules (initialConfig), read as gate.json is, for the seed and the reconcile. */
|
|
1409
|
+
interface StartingRules {
|
|
1410
|
+
/** Why none of them is used, when that's so. */
|
|
1411
|
+
refusedWhole?: string;
|
|
1412
|
+
/** What they put in force when gate.json can't be loaded: each rule not left out, as read. */
|
|
1413
|
+
config?: GateConfig;
|
|
1414
|
+
/** With `config`, the starting config as given, which a first start writes,
|
|
1415
|
+
* since gate.json is read the same way. */
|
|
1416
|
+
given?: unknown;
|
|
1417
|
+
/** Each starting rule as given, with its name when it has one, for the reconcile to add by name, and as read:
|
|
1418
|
+
* the rule, unless it's left out, and whether it wakes on none of what it matches (RuleReading.quiet). */
|
|
1419
|
+
rules: StartingRule[];
|
|
1420
|
+
/** What the gate says of them, each a sentence. */
|
|
1421
|
+
sentences: string[];
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
function readStartingRules(raw: unknown): StartingRules {
|
|
1425
|
+
const read = readConfig(raw, 'the starting config');
|
|
1426
|
+
if ('refused' in read) return { refusedWhole: read.refused, rules: [], sentences: [] };
|
|
1427
|
+
return {
|
|
1428
|
+
config: read.config,
|
|
1429
|
+
given: raw,
|
|
1430
|
+
rules: read.rules.map((rule) => ({ name: nameOf(rule.raw), raw: rule.raw, policy: rule.reading.policy, quiet: rule.reading.quiet })),
|
|
1431
|
+
sentences: read.sentences,
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* The recipe the gate last applied (lastRecipePath), as written, or null when
|
|
1437
|
+
* it has none on record. It's read as the host's starting rules are, so a
|
|
1438
|
+
* recipe recorded as given reads back as the start read it. A record that
|
|
1439
|
+
* exists and isn't one the gate wrote (it can't be read, isn't JSON, is
|
|
1440
|
+
* refused as the starting rules are, or names a rule twice) gives null with
|
|
1441
|
+
* the reason, which quotes nothing from the file but a rule's name: a start
|
|
1442
|
+
* then removes and redefines nothing, as the first does, and says why.
|
|
1443
|
+
*/
|
|
1444
|
+
function readLastRecipe(path: string): { recipe: WrittenConfig | null; problem?: string } {
|
|
1445
|
+
if (!existsSync(path)) return { recipe: null };
|
|
1446
|
+
let raw: unknown;
|
|
1447
|
+
try {
|
|
1448
|
+
raw = parseGateJson(readFileSync(path, 'utf-8'));
|
|
1449
|
+
} catch (err) {
|
|
1450
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
1451
|
+
return { recipe: null, problem: code ? `reading it failed with ${code}` : "it isn't JSON" };
|
|
1452
|
+
}
|
|
1453
|
+
const read = readStartingRules(raw);
|
|
1454
|
+
if (read.refusedWhole !== undefined) return { recipe: null, problem: "it isn't read as the starting rules are" };
|
|
1455
|
+
const rules = read.rules.map((rule) => rule.raw);
|
|
1456
|
+
const twice = duplicateName(rules);
|
|
1457
|
+
if (twice !== undefined) return { recipe: null, problem: `it names the rule "${twice}" twice` };
|
|
1458
|
+
return { recipe: { rules, default: isPlainObject(raw) ? raw.default : undefined } };
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/**
|
|
1462
|
+
* A rule's meaning, for the start's merge: the rule as the gate reads it,
|
|
1463
|
+
* where it reads, and as written where it doesn't. So a rule `wake_add_rule`
|
|
1464
|
+
* wrote as read agrees with the same rule written more briefly.
|
|
1465
|
+
*/
|
|
1466
|
+
function ruleMeaning(rule: unknown): string {
|
|
1467
|
+
const reading = readRule(rule, 'A policy');
|
|
1468
|
+
return reading.policy ? `read ${canonical(reading.policy)}` : `written ${canonical(rule)}`;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/** What a start's write of the host's starting rules to gate.json did. */
|
|
1472
|
+
type StartWrite =
|
|
1473
|
+
| { kind: 'none' }
|
|
1474
|
+
/** A first start's write of the starting rules threw: at its open, or partway. */
|
|
1475
|
+
| { kind: 'seed-failed'; error: string; atOpen: boolean }
|
|
1476
|
+
/**
|
|
1477
|
+
* A later start's write to gate.json threw, or couldn't be made; `config` is
|
|
1478
|
+
* what it was writing: the rules it read plus the ones it was adding
|
|
1479
|
+
* (`names`), with the merge's other changes (`changing`: the rules it was
|
|
1480
|
+
* removing, redefining or moving, and "default").
|
|
1481
|
+
*/
|
|
1482
|
+
| {
|
|
1483
|
+
kind: 'reconcile-failed'; names: string[]; changing?: string[]; error: string; atOpen: boolean; config: GateConfig;
|
|
1484
|
+
/** The text it was writing, whose reading is `config`, and what a load of it says, when the write itself failed. */
|
|
1485
|
+
text?: string;
|
|
1486
|
+
note?: string | null;
|
|
1487
|
+
sentences?: readonly string[];
|
|
1488
|
+
}
|
|
1489
|
+
/** A later start over a gate.json with no rule list to add the ones it lacks to: `config` is gate.json's default with them, which the gate runs beside it, unsaved. */
|
|
1490
|
+
| { kind: 'reconcile-unsaved'; names: string[]; error: string; config: GateConfig }
|
|
1491
|
+
/** A later start over a gate.json with no rules of its own (no rule list, an empty one, or one holding only rules the
|
|
1492
|
+
* gate leaves out) whose default, `default`, holds events back, where the host's starting rules can't all be put in
|
|
1493
|
+
* force beside it or in it: some that could wake the agent are left out (`leftOut`, by name, and `nameless`, a count of
|
|
1494
|
+
* those with no name) or kept out by gate.json's rules of their names, which the gate leaves out (`heldOut`); or
|
|
1495
|
+
* they're refused whole under their own default. gate.json isn't used at that start, which runs as one without it
|
|
1496
|
+
* does. */
|
|
1497
|
+
| {
|
|
1498
|
+
kind: 'start-without'; default: NonNullable<GateConfig['default']>;
|
|
1499
|
+
leftOut?: string[]; nameless?: number; heldOut?: string[];
|
|
1500
|
+
};
|
|
1501
|
+
|
|
1502
|
+
/**
|
|
1503
|
+
* What gate_status says about the start, which stays until the next one: the
|
|
1504
|
+
* host's starting rules that couldn't be used and what the start did instead,
|
|
1505
|
+
* how it read them past problems, and rules a reconcile couldn't add. Worded
|
|
1506
|
+
* about the start, so later writes and reloads
|
|
1507
|
+
* leave it true; configSource says what's in force now. `held` is the names
|
|
1508
|
+
* of the rules gate.json holds as written, when it loaded.
|
|
1509
|
+
*/
|
|
1510
|
+
function describeStart(starting: StartingRules, write: StartWrite, held: ReadonlySet<string> | null): string | null {
|
|
1511
|
+
const parts: string[] = [];
|
|
1512
|
+
if (starting.refusedWhole !== undefined) {
|
|
1513
|
+
// gate.json's rules: as loaded, or as read before a write of the start's
|
|
1514
|
+
// own left it unable to load; or none, where gate.json wasn't used either.
|
|
1515
|
+
const instead = write.kind === 'start-without'
|
|
1516
|
+
? `gate.json, which has no rules the gate reads, wasn't used either: under its default, "${write.default}", it would hold back every `
|
|
1517
|
+
+ 'event, so the gate started with no rules, as a start without gate.json does. Giving gate.json rules under '
|
|
1518
|
+
+ '"policies", or fixing the starting rules, puts rules in force (a fix to the starting rules, at the next start).'
|
|
1519
|
+
: held !== null || write.kind === 'reconcile-failed'
|
|
1520
|
+
? "The gate started with gate.json's rules."
|
|
1521
|
+
: 'The gate started with no rules, as on a host without starting rules.';
|
|
1522
|
+
parts.push(`At start, the host's starting rules weren't used: ${sentence(starting.refusedWhole)} ${instead}`);
|
|
1523
|
+
}
|
|
1524
|
+
if (starting.sentences.length > 0) parts.push(`In the host's starting rules, ${starting.sentences.join(' ')}`);
|
|
1525
|
+
// A reconcile whose write failed and left gate.json loading: the rules it
|
|
1526
|
+
// was adding didn't reach it, unless the write landed before failing.
|
|
1527
|
+
if (write.kind === 'reconcile-failed' && held) {
|
|
1528
|
+
if (!write.names.every((name) => held.has(name))) {
|
|
1529
|
+
parts.push(
|
|
1530
|
+
`At start, the host's starting rules ${quotedList(write.names)} couldn't be added to gate.json (${write.error}), `
|
|
1531
|
+
+ "so they weren't put in force. "
|
|
1532
|
+
+ (write.text !== undefined
|
|
1533
|
+
? 'Whoever runs this host can fix what kept them from being written.'
|
|
1534
|
+
: 'A start adds them once what kept them out is fixed.'),
|
|
1535
|
+
);
|
|
1536
|
+
}
|
|
1537
|
+
// The merge's other changes: removals, redefinitions, moves and the default.
|
|
1538
|
+
if (write.changing?.length) {
|
|
1539
|
+
parts.push(
|
|
1540
|
+
`At start, the recipe's changes to gate.json (${quotedList(write.changing)}) `
|
|
1541
|
+
+ (write.atOpen
|
|
1542
|
+
? `weren't saved (${write.error}), so the gate started with gate.json's rules as they were. `
|
|
1543
|
+
: `may not all have been saved (${write.error}): the gate started with gate.json as the write left it. `)
|
|
1544
|
+
+ (write.text !== undefined
|
|
1545
|
+
? 'Whoever runs this host can fix what kept them from being written.'
|
|
1546
|
+
: 'A start applies them once what kept them out is fixed.'),
|
|
1547
|
+
);
|
|
436
1548
|
}
|
|
437
1549
|
}
|
|
1550
|
+
return parts.length > 0 ? parts.join(' ') : null;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
/**
|
|
1554
|
+
* Why gate.json, which has no rules of its own, isn't used at a start that runs
|
|
1555
|
+
* as one without it (start-without), and what runs in its place: the clause
|
|
1556
|
+
* after "it has no rules, so" in its note.
|
|
1557
|
+
*/
|
|
1558
|
+
function notUsedClause(write: Extract<StartWrite, { kind: 'start-without' }>, starting: StartingRules): string {
|
|
1559
|
+
const back = 'Giving gate.json rules under "policies"';
|
|
1560
|
+
const out = outOfForce(write);
|
|
1561
|
+
if (out) {
|
|
1562
|
+
return `it isn't used at this start: under its default, "${write.default}", ${out.said}, so the gate runs the starting `
|
|
1563
|
+
+ `rules under their own default, "${starting.config?.default ?? 'always'}", as a start without gate.json does. ${back}, or ${out.fix}, puts it in use`
|
|
1564
|
+
+ (out.nextStart ? ' (a fix to the starting rules, at the next start)' : '');
|
|
1565
|
+
}
|
|
1566
|
+
return `under its default, "${write.default}", it would hold back every event: it isn't used at this start, and the gate runs `
|
|
1567
|
+
+ `no rules, as a start without gate.json does, so every event gets the default, "always". ${back}, or fixing the starting rules, puts rules in force`
|
|
1568
|
+
+ ' (a fix to the starting rules, at the next start)';
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
/**
|
|
1572
|
+
* The starting rules a start that doesn't use gate.json couldn't put in force
|
|
1573
|
+
* beside it or in it, which could wake the agent, and what would put them in
|
|
1574
|
+
* force: null where the starting rules were refused whole.
|
|
1575
|
+
*/
|
|
1576
|
+
function outOfForce(write: Extract<StartWrite, { kind: 'start-without' }>): { said: string; fix: string; nextStart: boolean } | null {
|
|
1577
|
+
const nameless = write.nameless ?? 0;
|
|
1578
|
+
const left = [...(write.leftOut ?? []).map((name) => `"${name}"`), ...(nameless === 0 ? [] : [nameless === 1 ? 'one with no name' : `${nameless} with no name`])];
|
|
1579
|
+
const held = write.heldOut ?? [];
|
|
1580
|
+
const said: string[] = [];
|
|
1581
|
+
const fix: string[] = [];
|
|
1582
|
+
if (left.length > 0) {
|
|
1583
|
+
said.push(`the host's starting rules would leave out ${joinedList(left)}, which could wake the agent`);
|
|
1584
|
+
fix.push(`fixing ${joinedList(left)} in the starting rules`);
|
|
1585
|
+
}
|
|
1586
|
+
if (held.length > 0) {
|
|
1587
|
+
said.push(`the host's ${quotedList(held)}, which could wake the agent, would be kept out by gate.json's `
|
|
1588
|
+
+ `${held.length === 1 ? 'rule of that name' : 'rules of those names'}, which the gate leaves out`);
|
|
1589
|
+
fix.push(`fixing gate.json's ${quotedList(held)}`);
|
|
1590
|
+
}
|
|
1591
|
+
// While gate.json has no rules of its own, every one of them has to be put
|
|
1592
|
+
// in force for it to be used, so where both kinds keep it out, both fixes
|
|
1593
|
+
// are needed; giving gate.json rules of its own is the other way, which the
|
|
1594
|
+
// entry gives first.
|
|
1595
|
+
// The starting rules are read at a start, so a fix to them puts gate.json in
|
|
1596
|
+
// use from the next one.
|
|
1597
|
+
return said.length > 0
|
|
1598
|
+
? { said: said.join(', and '), fix: fix.length > 1 ? `both ${fix.join(', and ')}` : fix[0]!, nextStart: left.length > 0 }
|
|
1599
|
+
: null;
|
|
1600
|
+
}
|
|
438
1601
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
1602
|
+
/**
|
|
1603
|
+
* What gate_status says when the start left rules in force unsaved: a first
|
|
1604
|
+
* start's seed that failed, a reconcile whose write left gate.json unable to
|
|
1605
|
+
* load (`loaded` false), or, over a gate.json with no rule list (`loaded`
|
|
1606
|
+
* true), the starting rules it lacks, run beside it, or, over one with no rules
|
|
1607
|
+
* of its own, the starting rules run under their own default where gate.json
|
|
1608
|
+
* isn't used. It goes once gate.json is written, or a reload applies its text
|
|
1609
|
+
* or, but where the start ran rules beside or in place of such a gate.json,
|
|
1610
|
+
* finds it holding the text in force.
|
|
1611
|
+
*/
|
|
1612
|
+
function describeUnsaved(starting: StartingRules, write: StartWrite, loaded: boolean): string | null {
|
|
1613
|
+
if (write.kind === 'reconcile-unsaved') {
|
|
1614
|
+
if (!loaded) return null;
|
|
1615
|
+
return `At start, the host's starting rules ${quotedList(write.names)} couldn't be added to gate.json (${write.error}). `
|
|
1616
|
+
+ `The gate runs them under gate.json's default, "${write.config.default ?? 'always'}", as if gate.json held them, but `
|
|
1617
|
+
+ "they aren't saved in it: once gate.json changes and loads, what it holds is in force instead. Once gate.json has a "
|
|
1618
|
+
+ 'list of rules under "policies", a start adds them to it, as to any gate.json with a list.';
|
|
1619
|
+
}
|
|
1620
|
+
if (write.kind === 'start-without') {
|
|
1621
|
+
// Starting rules refused whole put none in force, and describeStart says so.
|
|
1622
|
+
const out = outOfForce(write);
|
|
1623
|
+
if (!out) return null;
|
|
1624
|
+
return `At start, gate.json, which has no rules the gate reads, wasn't used: under its default, "${write.default}", ${out.said}. The gate `
|
|
1625
|
+
+ `runs the starting rules under their own default, "${starting.config?.default ?? 'always'}", as a start without gate.json `
|
|
1626
|
+
+ 'does, but they aren\'t saved in it: once gate.json changes and loads, what it holds is in force instead. Giving '
|
|
1627
|
+
+ `gate.json rules under "policies", or ${out.fix}, puts it in use`
|
|
1628
|
+
+ `${out.nextStart ? ' (a fix to the starting rules, at the next start)' : ''}.`;
|
|
1629
|
+
}
|
|
1630
|
+
if (loaded) return null;
|
|
1631
|
+
if (write.kind === 'seed-failed') {
|
|
1632
|
+
return write.atOpen
|
|
1633
|
+
? `At start, gate.json couldn't be created (${write.error}), so the host's starting rules are in force but aren't saved in it. `
|
|
1634
|
+
+ 'Whoever runs this host can fix what keeps it from being written.'
|
|
1635
|
+
: `At start, writing the host's starting rules to gate.json failed partway (${write.error}), so gate.json may be left `
|
|
1636
|
+
+ "part-written. The starting rules are in force but aren't saved in it, and a restart before gate.json is fixed starts "
|
|
1637
|
+
+ 'from them too. Whoever runs this host can fix what kept it from being written, and gate.json.';
|
|
1638
|
+
}
|
|
1639
|
+
if (write.kind === 'reconcile-failed') {
|
|
1640
|
+
// A write of the recipe's merge that did more than add rules, or one that only added them.
|
|
1641
|
+
const merging = (write.changing?.length ?? 0) > 0;
|
|
1642
|
+
const changes = quotedList([...write.names, ...(write.changing ?? [])]);
|
|
1643
|
+
const what = merging
|
|
1644
|
+
? write.atOpen
|
|
1645
|
+
? `the recipe's changes to gate.json (${changes}) couldn't be written (${write.error}), and gate.json couldn't be loaded after that`
|
|
1646
|
+
: `writing the recipe's changes to gate.json (${changes}) failed partway (${write.error}), so gate.json may be left part-written`
|
|
1647
|
+
: write.atOpen
|
|
1648
|
+
? `the host's starting rules ${quotedList(write.names)} couldn't be added to gate.json (${write.error}), and gate.json `
|
|
1649
|
+
+ "couldn't be loaded after that"
|
|
1650
|
+
: `adding the host's starting rules ${quotedList(write.names)} to gate.json failed partway (${write.error}), so gate.json `
|
|
1651
|
+
+ 'may be left part-written';
|
|
1652
|
+
// What those rules hold that the gate reads past or leaves out, since
|
|
1653
|
+
// gate.json's own entry is now its load error.
|
|
1654
|
+
const read = write.sentences?.length ? `, read as a load reads them: ${write.sentences.join(' ')}` : '.';
|
|
1655
|
+
const runs = merging ? 'the rules that write held' : "the rules it read from gate.json and the ones it was adding";
|
|
1656
|
+
return `At start, ${what}. The gate runs ${runs}, which aren't saved${read} `
|
|
1657
|
+
+ `A restart before gate.json is fixed starts from ${starting.config ? "the host's starting rules" : 'no rules'}. `
|
|
1658
|
+
+ 'Whoever runs this host can fix what kept it from being written, and gate.json.';
|
|
1659
|
+
}
|
|
1660
|
+
return null;
|
|
443
1661
|
}
|
|
444
1662
|
|
|
445
1663
|
// ---------------------------------------------------------------------------
|
|
@@ -544,6 +1762,45 @@ export function formatShadowWarning(w: ShadowWarning): string {
|
|
|
544
1762
|
+ `(first match wins — every event "${w.later}" would match is consumed by "${w.earlier}" first)`;
|
|
545
1763
|
}
|
|
546
1764
|
|
|
1765
|
+
/** The text of a thrown value, for a log line. It never throws itself, whatever was thrown. */
|
|
1766
|
+
function errorText(err: unknown): string {
|
|
1767
|
+
try {
|
|
1768
|
+
return err instanceof Error ? err.message : String(err);
|
|
1769
|
+
} catch {
|
|
1770
|
+
return 'a value that could not be printed';
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
/**
|
|
1775
|
+
* Run a caller's callback once the change it's told of is made and traced. A
|
|
1776
|
+
* throw from it is logged, not thrown, so the call answers as the change it
|
|
1777
|
+
* made: told the change had failed, an agent could make it a second time.
|
|
1778
|
+
*/
|
|
1779
|
+
function afterChange(name: string, callback: () => void): void {
|
|
1780
|
+
try {
|
|
1781
|
+
callback();
|
|
1782
|
+
} catch (err) {
|
|
1783
|
+
console.error(`[gate] ${name} threw once the change was made, and the change stands: ${errorText(err)}`);
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
/** The privileged-users file's error when some entries of its list aren't user ids. */
|
|
1788
|
+
function skippedPrivilegedIds(skipped: unknown[], loaded: number): string {
|
|
1789
|
+
const one = skipped.length === 1;
|
|
1790
|
+
const shown = skipped.slice(0, 5).map((x) => {
|
|
1791
|
+
if (typeof x === 'number' && Number.isInteger(x)) {
|
|
1792
|
+
return `${x} (a number this large can't be read exactly: write the id as a string, in quotes)`;
|
|
1793
|
+
}
|
|
1794
|
+
const json = JSON.stringify(x) ?? String(x);
|
|
1795
|
+
return json.length > 40 ? `${safeSlice(json, 0, 39)}…` : json;
|
|
1796
|
+
});
|
|
1797
|
+
const more = skipped.length > shown.length ? `, and ${skipped.length - shown.length} more` : '';
|
|
1798
|
+
const rest = loaded === 0 ? 'no id loaded, so no one is privileged' : `${loaded} ${loaded === 1 ? 'id' : 'ids'} did load`;
|
|
1799
|
+
return `${skipped.length} ${one ? 'entry' : 'entries'} in the privileged-users file ${one ? "isn't a user id" : "aren't user ids"}, `
|
|
1800
|
+
+ `so ${one ? 'it was' : 'they were'} skipped: ${shown.join(', ')}${more}. A user id is a non-empty string, or a safe integer `
|
|
1801
|
+
+ `(a whole number within 2^53 - 1 of zero); ${rest}.`;
|
|
1802
|
+
}
|
|
1803
|
+
|
|
547
1804
|
// ---------------------------------------------------------------------------
|
|
548
1805
|
// EventGate
|
|
549
1806
|
// ---------------------------------------------------------------------------
|
|
@@ -554,11 +1811,37 @@ export class EventGate {
|
|
|
554
1811
|
/** Optional agent-authored programmable gate (gate.js). */
|
|
555
1812
|
private gateScript: GateScript;
|
|
556
1813
|
private compiledPolicies: CompiledPolicy[] = [];
|
|
557
|
-
|
|
1814
|
+
/** The text of gate.json whose rules are in force, from a load or a write
|
|
1815
|
+
* (or the start's reconcile, whose write failed); or, while what's in force
|
|
1816
|
+
* is what a start ran beside gate.json or in its place (configSource not
|
|
1817
|
+
* "file"), gate.json's text as that start found it.
|
|
1818
|
+
* A reload parses the file again only when its text differs; a file that
|
|
1819
|
+
* holds it again after failing to load (a chmod that makes it readable,
|
|
1820
|
+
* an edit undone) needs nothing applied, only its error cleared. */
|
|
1821
|
+
private configText: string | undefined;
|
|
1822
|
+
/** The notes on fields the text in force has that the gate doesn't read, if any. */
|
|
1823
|
+
private configNote: string | null = null;
|
|
558
1824
|
private lastReloadCheck: number = 0;
|
|
559
1825
|
private configSource: 'file' | 'initial' | 'default' = 'default';
|
|
560
1826
|
private lastReloadTimestamp: number | null = null;
|
|
561
|
-
|
|
1827
|
+
/** The current problem with each file the gate reads, one each, replaced
|
|
1828
|
+
* whenever that file is read again (gate_status `errors`). */
|
|
1829
|
+
private configError: string | null = null;
|
|
1830
|
+
/** Whether gate.json's error is one that keeps what it holds out of force
|
|
1831
|
+
* (configUnreadable), so gate_status says what's in force instead. */
|
|
1832
|
+
private configUnused = false;
|
|
1833
|
+
private privilegedError: string | null = null;
|
|
1834
|
+
/** What the start did with the host's starting rules that it couldn't use
|
|
1835
|
+
* or add (describeStart), kept apart from gate.json's own error, until the
|
|
1836
|
+
* next start. */
|
|
1837
|
+
private startNotice: string | null = null;
|
|
1838
|
+
/** The rules in force that the start couldn't save (describeUnsaved),
|
|
1839
|
+
* until gate.json is written or a reload applies its text. */
|
|
1840
|
+
private startUnsaved: string | null = null;
|
|
1841
|
+
/** What the start's merge of the recipe couldn't do (seedOrReconcile), said with startNotice until the next start. */
|
|
1842
|
+
private startMergeNote: string | null = null;
|
|
1843
|
+
/** Whether the start's unsaved rules (startUnsaved) are a merge's that did more than add rules. */
|
|
1844
|
+
private startUnsavedMerged = false;
|
|
562
1845
|
|
|
563
1846
|
// Debounce state
|
|
564
1847
|
private debounceTimers = new Map<string, DebounceState>();
|
|
@@ -618,7 +1901,9 @@ export class EventGate {
|
|
|
618
1901
|
// privilegedUsersPath on change.
|
|
619
1902
|
private privilegedUsersPath: string | undefined;
|
|
620
1903
|
private privilegedUserIds = new Set<string>();
|
|
621
|
-
|
|
1904
|
+
/** The privileged-users file's text as last read, whether or not it loaded;
|
|
1905
|
+
* undefined when it was missing or its last read failed. */
|
|
1906
|
+
private privilegedText: string | undefined;
|
|
622
1907
|
private lastPrivilegedCheck = 0;
|
|
623
1908
|
|
|
624
1909
|
// Dependency-injected callbacks
|
|
@@ -629,10 +1914,14 @@ export class EventGate {
|
|
|
629
1914
|
metadata?: Record<string, unknown>,
|
|
630
1915
|
/** Registry agent to deliver into; absent = primary (historical). */
|
|
631
1916
|
forAgent?: string,
|
|
1917
|
+
/** Out-param: where the message landed, for the wake that goes with it. */
|
|
1918
|
+
placement?: WritePlacement,
|
|
632
1919
|
) => unknown;
|
|
633
|
-
private requestInferenceFn: (agentName: string, reason: string, source: string, provenance?: WakeProvenance) => void;
|
|
1920
|
+
private requestInferenceFn: (agentName: string, reason: string, source: string, provenance?: WakeProvenance, after?: WritePlacement) => void;
|
|
634
1921
|
private getAgentNamesFn: () => string[];
|
|
635
1922
|
private resolveRouteChannelFn: (info: GateEventInfo) => string | undefined;
|
|
1923
|
+
private channelShownFn?: (channel: { serverId: string; channelId: string }) => string;
|
|
1924
|
+
private isNewestEntryFn?: (entry: GateEntryRef) => boolean;
|
|
636
1925
|
/** Clock injection — keeps the new rate_limit / passive_sample paths
|
|
637
1926
|
* testable without monkey-patching Date.now globally. */
|
|
638
1927
|
private now: () => number;
|
|
@@ -647,17 +1936,37 @@ export class EventGate {
|
|
|
647
1936
|
content: Array<{ type: 'text'; text: string }>,
|
|
648
1937
|
metadata?: Record<string, unknown>,
|
|
649
1938
|
forAgent?: string,
|
|
1939
|
+
placement?: WritePlacement,
|
|
650
1940
|
) => unknown;
|
|
651
|
-
|
|
1941
|
+
/** `after`: the placement of the message the wake is for, when it has
|
|
1942
|
+
* one, so the host can drop the wake once a request that carried the
|
|
1943
|
+
* message is answered. */
|
|
1944
|
+
requestInference: (agentName: string, reason: string, source: string, provenance?: WakeProvenance, after?: WritePlacement) => void;
|
|
652
1945
|
getAgentNames: () => string[];
|
|
653
1946
|
/** Optional clock — defaults to Date.now. Tests inject for deterministic time. */
|
|
654
1947
|
now?: () => number;
|
|
655
1948
|
/** Per-event timeout (ms) for the optional gate.js script. Default 50. */
|
|
656
1949
|
scriptTimeoutMs?: number;
|
|
657
1950
|
/** Map an event to the registered channel it arrived on (the host knows
|
|
658
|
-
* how push-event origins compose into channel ids).
|
|
659
|
-
*
|
|
1951
|
+
* how push-event origins compose into channel ids). The gate routes a
|
|
1952
|
+
* batched wake by it, tests a policy's `match.channel` against it when
|
|
1953
|
+
* the event's own id misses, and keys `keyBy: "channelId"` counts on it.
|
|
1954
|
+
* A resolver that throws counts as returning nothing. Default: a
|
|
1955
|
+
* channel-incoming event's own channel id; nothing for push events, so
|
|
1956
|
+
* a push then matches and counts by its own id alone. */
|
|
660
1957
|
resolveRouteChannel?: (info: GateEventInfo) => string | undefined;
|
|
1958
|
+
/** How the agent is shown a channel, its server's and its id (the
|
|
1959
|
+
* framework's ChannelRegistry.nameShown): a batched wake's summary names
|
|
1960
|
+
* each channel so, since two servers' channels can share an id. Default:
|
|
1961
|
+
* the id. */
|
|
1962
|
+
channelShown?: (channel: { serverId: string; channelId: string }) => string;
|
|
1963
|
+
/** Whether an MCPL event's own context entry is still the newest entry in
|
|
1964
|
+
* the window a batched wake's line would be written to, with nothing
|
|
1965
|
+
* waiting to be written there. A batched wake for that one message
|
|
1966
|
+
* doesn't write its line when it fires: it hands the line to the wake
|
|
1967
|
+
* (WakeProvenance.line), for a fresh start of the wake's turn to decide
|
|
1968
|
+
* (WakeLine). Absent: every batched wake writes its line, as before. */
|
|
1969
|
+
isNewestEntry?: (entry: GateEntryRef) => boolean;
|
|
661
1970
|
}) {
|
|
662
1971
|
this.configPath = opts.configPath;
|
|
663
1972
|
this.privilegedUsersPath = opts.privilegedUsersPath;
|
|
@@ -666,52 +1975,77 @@ export class EventGate {
|
|
|
666
1975
|
this.requestInferenceFn = opts.requestInference;
|
|
667
1976
|
this.getAgentNamesFn = opts.getAgentNames;
|
|
668
1977
|
this.resolveRouteChannelFn = opts.resolveRouteChannel ?? defaultRouteChannel;
|
|
1978
|
+
this.channelShownFn = opts.channelShown;
|
|
1979
|
+
this.isNewestEntryFn = opts.isNewestEntry;
|
|
669
1980
|
this.now = opts.now ?? (() => Date.now());
|
|
670
1981
|
this.loadPrivileged();
|
|
671
1982
|
|
|
672
|
-
// Seed or reconcile the on-disk config
|
|
1983
|
+
// Seed or reconcile the on-disk config from the host's starting rules
|
|
1984
|
+
// (`initialConfig`), which are read as gate.json is, rule by rule: a rule
|
|
1985
|
+
// with a problem is read past it, or left out (readConfig).
|
|
673
1986
|
//
|
|
674
|
-
// First start: write `initialConfig`
|
|
675
|
-
//
|
|
676
|
-
//
|
|
677
|
-
//
|
|
678
|
-
//
|
|
679
|
-
//
|
|
680
|
-
//
|
|
681
|
-
//
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
1987
|
+
// First start: write `initialConfig` as given, since gate.json is read the
|
|
1988
|
+
// same way, and record it as the recipe last applied, beside gate.json.
|
|
1989
|
+
// Later starts: merge the recipe into gate.json against that record,
|
|
1990
|
+
// three ways and as written (seedOrReconcile), so a rule the recipe
|
|
1991
|
+
// dropped, renamed, moved or redefined follows it, while a rule edited at
|
|
1992
|
+
// run time or by `workspace--edit _config/gate.json`, and one no recipe
|
|
1993
|
+
// declared, stays. `default` follows the same rule: an unedited one takes
|
|
1994
|
+
// the recipe's.
|
|
1995
|
+
const initial = opts.initialConfig;
|
|
1996
|
+
const starting = initial !== undefined ? readStartingRules(initial) : undefined;
|
|
1997
|
+
const write: StartWrite = starting ? this.seedOrReconcile(starting) : { kind: 'none' };
|
|
1998
|
+
|
|
1999
|
+
// Load config: gate.json; without it, the starting rules as read, unless
|
|
2000
|
+
// none of them is used; and otherwise no rules, as a host without any has. When a
|
|
2001
|
+
// reconcile's own write left gate.json unable to load, the gate runs what
|
|
2002
|
+
// that write held, the rules it read as the recipe's merge changed them,
|
|
2003
|
+
// since a failed write of ours shouldn't take rules out of force. Over a gate.json
|
|
2004
|
+
// with no rule list to add them to, it runs gate.json's default with the
|
|
2005
|
+
// starting rules it lacks, as earlier releases did once they had written
|
|
2006
|
+
// them over it, where seedOrReconcile gives them (reconcile-unsaved). Where
|
|
2007
|
+
// gate.json has no rules of its own and they can't run beside it or be
|
|
2008
|
+
// added to it (start-without), earlier releases couldn't load what they
|
|
2009
|
+
// wrote and ran the starting rules from memory, so gate.json isn't used,
|
|
2010
|
+
// and the start runs as one without it does.
|
|
2011
|
+
const read = this.readConfigFile();
|
|
2012
|
+
if (read.state === 'loaded') {
|
|
2013
|
+
// Beside a gate.json with no rule list, the host's starting rules it
|
|
2014
|
+
// lacks, and its note says they run beside it, or, in place of one with
|
|
2015
|
+
// no rules of its own, those a start without it runs, and its note says
|
|
2016
|
+
// why.
|
|
2017
|
+
const beside = write.kind === 'reconcile-unsaved';
|
|
2018
|
+
const without = write.kind === 'start-without' ? write : null;
|
|
2019
|
+
this.config = beside ? write.config : without ? (starting?.config ?? { ...DEFAULT_CONFIG }) : read.config;
|
|
2020
|
+
this.configText = read.text;
|
|
2021
|
+
const start = beside ? 'beside' : without && starting ? { notUsed: notUsedClause(without, starting) } : undefined;
|
|
2022
|
+
const asStarted = start ? readConfig(parseGateJson(read.text), 'gate.json', start) : null;
|
|
2023
|
+
this.configNote = asStarted && 'sentences' in asStarted ? loadNote(asStarted.sentences) : read.note;
|
|
2024
|
+
this.configError = this.configNote;
|
|
2025
|
+
this.configSource = beside || starting?.config && without ? 'initial' : without ? 'default' : 'file';
|
|
2026
|
+
if (this.configNote) console.error(`[gate] ${this.configNote}`);
|
|
2027
|
+
} else if (write.kind === 'reconcile-failed') {
|
|
2028
|
+
this.config = write.config;
|
|
2029
|
+
// A change over the part-written gate.json is refused; once gate.json is
|
|
2030
|
+
// gone, this text is what a change edits, so every rule it holds keeps its name.
|
|
2031
|
+
this.configText = write.text;
|
|
2032
|
+
this.configNote = write.note ?? null;
|
|
2033
|
+
this.configSource = 'file';
|
|
2034
|
+
} else {
|
|
2035
|
+
this.config = starting?.config ?? { ...DEFAULT_CONFIG };
|
|
2036
|
+
this.configSource = starting?.config ? 'initial' : 'default';
|
|
2037
|
+
}
|
|
2038
|
+
// What the start did is kept apart from gate.json's load error, so the
|
|
2039
|
+
// load above can't clear it when it succeeds.
|
|
2040
|
+
if (starting) {
|
|
2041
|
+
const described = describeStart(starting, write, read.state === 'loaded' ? heldNames(read.listed) : null);
|
|
2042
|
+
this.startNotice = [described, this.startMergeNote].filter((part): part is string => part !== null).join(' ') || null;
|
|
2043
|
+
this.startUnsaved = describeUnsaved(starting, write, read.state === 'loaded');
|
|
2044
|
+
this.startUnsavedMerged = write.kind === 'reconcile-failed' && (write.changing?.length ?? 0) > 0;
|
|
2045
|
+
}
|
|
2046
|
+
for (const said of [this.startNotice, this.startUnsaved]) {
|
|
2047
|
+
if (said) console.error(`[gate] ${said}`);
|
|
711
2048
|
}
|
|
712
|
-
|
|
713
|
-
// Load config
|
|
714
|
-
this.config = this.loadConfig() ?? opts.initialConfig ?? { ...DEFAULT_CONFIG };
|
|
715
2049
|
this.compiledPolicies = this.compilePolicies(this.config);
|
|
716
2050
|
this.refreshShadowWarnings('startup');
|
|
717
2051
|
|
|
@@ -728,29 +2062,377 @@ export class EventGate {
|
|
|
728
2062
|
// Config loading
|
|
729
2063
|
// =========================================================================
|
|
730
2064
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
2065
|
+
/**
|
|
2066
|
+
* Write the starting rules to gate.json, as given, when it's absent, and
|
|
2067
|
+
* record them as the recipe last applied (lastRecipePath); or, when it
|
|
2068
|
+
* loads, merge them into its own rule list against that record, three ways
|
|
2069
|
+
* and as written (reconcileWithRecipe), and record them. A rule as the last
|
|
2070
|
+
* recipe left it follows the new one, a rule edited since keeps its edit, a
|
|
2071
|
+
* rule no recipe declared stays, and `default` follows the same rule. A
|
|
2072
|
+
* rule gate.json holds holds its name, whether the gate reads it or leaves
|
|
2073
|
+
* it out. gate.json is written before the record, so a stop between the two
|
|
2074
|
+
* leaves a state the next start reads rightly: the other order would make
|
|
2075
|
+
* the old recipe's rules read as edits. Nothing is recorded where gate.json
|
|
2076
|
+
* isn't written as the merge asks, or a name is given twice. Where
|
|
2077
|
+
* gate.json has no rules of its own and, under its default, one of them that
|
|
2078
|
+
* could wake the agent can't be put in force (left out, with no name, or kept
|
|
2079
|
+
* out by a rule of gate.json's of its name that the gate leaves out), or
|
|
2080
|
+
* they're refused whole and that default holds events back, gate.json isn't
|
|
2081
|
+
* used at this start, which runs as one without it does. Otherwise, where
|
|
2082
|
+
* gate.json has no rule list to add them to, it writes nothing, and the gate
|
|
2083
|
+
* runs beside it those of them it reads. Says what it did when that wasn't
|
|
2084
|
+
* simply done or not needed.
|
|
2085
|
+
*/
|
|
2086
|
+
private seedOrReconcile(starting: StartingRules): StartWrite {
|
|
2087
|
+
const read = this.readConfigFile();
|
|
2088
|
+
if (read.state === 'absent') {
|
|
2089
|
+
if (!starting.config) return { kind: 'none' };
|
|
2090
|
+
try {
|
|
2091
|
+
mkdirSync(dirname(this.configPath), { recursive: true });
|
|
2092
|
+
writeFileSync(this.configPath, JSON.stringify(starting.given, null, 2) + '\n');
|
|
2093
|
+
} catch (err) {
|
|
2094
|
+
return { kind: 'seed-failed', error: errorText(err), atOpen: beforeWriting(err) };
|
|
2095
|
+
}
|
|
2096
|
+
const twice = duplicateName(starting.rules.map((rule) => rule.raw));
|
|
2097
|
+
if (twice !== undefined) {
|
|
2098
|
+
this.startMergeNote = `At start, the recipe names the rule "${twice}" twice, so gate.json was seeded with it as given and it `
|
|
2099
|
+
+ "wasn't recorded as the recipe last applied: a later start merges the recipe by name, once gate.json and the recipe "
|
|
2100
|
+
+ 'name each rule once.';
|
|
2101
|
+
} else {
|
|
2102
|
+
this.recordRecipe(lastRecipePath(this.configPath), starting.given, 'gate.json was seeded with the recipe');
|
|
2103
|
+
}
|
|
2104
|
+
return { kind: 'none' };
|
|
2105
|
+
}
|
|
2106
|
+
if (read.state !== 'loaded') return { kind: 'none' };
|
|
2107
|
+
const held = heldNames(read.listed);
|
|
2108
|
+
const fileDefault = read.config.default ?? 'always';
|
|
2109
|
+
// No rule list, an empty one, or one holding only rules the gate leaves out.
|
|
2110
|
+
const noRules = read.config.policies.length === 0;
|
|
2111
|
+
// Starting rules refused whole put none beside or in a gate.json with no
|
|
2112
|
+
// rules of its own, which holds events back. Earlier releases wrote them
|
|
2113
|
+
// over it or into it, couldn't load what they wrote, and ran them from
|
|
2114
|
+
// memory, so gate.json isn't used at this start either.
|
|
2115
|
+
if (noRules && starting.refusedWhole !== undefined && fileDefault !== 'always') {
|
|
2116
|
+
return { kind: 'start-without', default: fileDefault };
|
|
734
2117
|
}
|
|
2118
|
+
// Nor can a start put in force, beside or in a gate.json with no rules of
|
|
2119
|
+
// its own, a starting rule a load leaves out, one with no name, which it
|
|
2120
|
+
// can't add by name, or one whose name gate.json holds in a rule the gate
|
|
2121
|
+
// leaves out, which keeps the host's rule of that name out. Where, under
|
|
2122
|
+
// gate.json's default, one of them could wake the agent, earlier releases
|
|
2123
|
+
// ran the starting rules from memory: they couldn't load what they wrote
|
|
2124
|
+
// over gate.json or into it, or, refusing its rule, didn't load it at all.
|
|
2125
|
+
// So gate.json isn't used at this start, which runs as one without it does.
|
|
2126
|
+
if (noRules && fileDefault !== 'always') {
|
|
2127
|
+
const out = starting.rules.filter((rule) => !rule.quiet && (rule.policy === undefined || (rule.name !== undefined && held.has(rule.name))));
|
|
2128
|
+
if (out.length > 0) {
|
|
2129
|
+
const named = out.flatMap((rule) => (rule.name === undefined ? [] : [rule.name]));
|
|
2130
|
+
return {
|
|
2131
|
+
kind: 'start-without',
|
|
2132
|
+
default: fileDefault,
|
|
2133
|
+
leftOut: named.filter((name) => !held.has(name)),
|
|
2134
|
+
nameless: out.length - named.length,
|
|
2135
|
+
heldOut: named.filter((name) => held.has(name)),
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
if ('noList' in read.listed) {
|
|
2140
|
+
const missing = starting.rules.filter((rule): rule is StartingRule & { name: string } => rule.name !== undefined && !held.has(rule.name));
|
|
2141
|
+
if (missing.length === 0) return { kind: 'none' };
|
|
2142
|
+
const names = missing.map((rule) => rule.name);
|
|
2143
|
+
// Adding them would write over what gate.json holds. Earlier releases
|
|
2144
|
+
// wrote them over it and ran them under its default, so the gate runs
|
|
2145
|
+
// them so, unsaved, beside it.
|
|
2146
|
+
const error = `${read.listed.noList}, so there's no list of rules to add them to without writing over what it holds`;
|
|
2147
|
+
// The ones it runs: a starting rule left out is named as the starting rules are read.
|
|
2148
|
+
const running = missing.flatMap((rule) => (rule.policy === undefined ? [] : [rule.policy]));
|
|
2149
|
+
if (running.length === 0) return { kind: 'reconcile-failed', names, atOpen: true, config: read.config, error };
|
|
2150
|
+
return { kind: 'reconcile-unsaved', names: running.map((policy) => policy.name), error, config: { policies: running, default: fileDefault } };
|
|
2151
|
+
}
|
|
2152
|
+
// Starting rules refused whole have nothing to merge or record.
|
|
2153
|
+
if (starting.refusedWhole !== undefined) return { kind: 'none' };
|
|
2154
|
+
// The merge, as written: gate.json's rule list and default against the
|
|
2155
|
+
// recipe's, three ways, with the recipe last applied.
|
|
2156
|
+
const recordPath = lastRecipePath(this.configPath);
|
|
2157
|
+
const last = readLastRecipe(recordPath);
|
|
2158
|
+
const given = isPlainObject(starting.given) ? starting.given : {};
|
|
2159
|
+
let merged: ReturnType<typeof reconcileWithRecipe>;
|
|
2160
|
+
let appended = false;
|
|
2161
|
+
let appendedNote = '';
|
|
735
2162
|
try {
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
return config;
|
|
2163
|
+
merged = reconcileWithRecipe(
|
|
2164
|
+
{ rules: read.listed.rules, default: read.listed.top.default },
|
|
2165
|
+
{ rules: starting.rules.map((rule) => rule.raw), default: given.default },
|
|
2166
|
+
last.recipe,
|
|
2167
|
+
ruleMeaning,
|
|
2168
|
+
);
|
|
743
2169
|
} catch (err) {
|
|
744
|
-
|
|
745
|
-
|
|
2170
|
+
// A name given twice: a merge by name can't say which rule was meant.
|
|
2171
|
+
// So the start adds the rules gate.json lacks, after its own, as
|
|
2172
|
+
// earlier releases did, and removes, redefines and records nothing.
|
|
2173
|
+
const missing = starting.rules.filter((rule): rule is StartingRule & { name: string } => rule.name !== undefined && !held.has(rule.name));
|
|
2174
|
+
const lacked = [...new Set(missing.map((rule) => rule.name))];
|
|
2175
|
+
appended = true;
|
|
2176
|
+
// What it did is said once its write is made (appendedNote), and a
|
|
2177
|
+
// write that fails is said as any start's is.
|
|
2178
|
+
const why = `At start, ${errorText(err)}, and the merge with the recipe needs one rule per name, so it didn't run`;
|
|
2179
|
+
const remedy = 'Once gate.json and the recipe name each rule once, a start merges the recipe.';
|
|
2180
|
+
this.startMergeNote = `${why}. ${remedy}`;
|
|
2181
|
+
appendedNote = `${why}: `
|
|
2182
|
+
+ (lacked.length > 0
|
|
2183
|
+
? `the start added the rules gate.json lacked (${quotedList(lacked)}), after its own, and removed, redefined and recorded nothing. `
|
|
2184
|
+
: 'gate.json is as it was, and nothing was recorded. ')
|
|
2185
|
+
+ remedy;
|
|
2186
|
+
merged = {
|
|
2187
|
+
rules: [...read.listed.rules, ...missing.map((rule) => rule.raw)],
|
|
2188
|
+
default: read.listed.top.default,
|
|
2189
|
+
defaultChanged: false,
|
|
2190
|
+
changes: { ...noChanges(), added: missing.map((rule) => rule.name) },
|
|
2191
|
+
};
|
|
2192
|
+
}
|
|
2193
|
+
const { changes } = merged;
|
|
2194
|
+
if (last.problem !== undefined) changes.recordUnreadable = last.problem;
|
|
2195
|
+
const top = { ...read.listed.top };
|
|
2196
|
+
if (merged.defaultChanged) {
|
|
2197
|
+
if (merged.default === undefined) delete top.default;
|
|
2198
|
+
else top.default = merged.default;
|
|
2199
|
+
}
|
|
2200
|
+
const reconciled = { ...top, policies: merged.rules };
|
|
2201
|
+
const names = [...changes.added, ...changes.restored];
|
|
2202
|
+
const changing = [...changes.removed, ...changes.updated, ...changes.moved, ...(changes.defaultChange ? ['default'] : [])];
|
|
2203
|
+
const written = canonical(reconciled) !== canonical({ ...read.listed.top, policies: read.listed.rules });
|
|
2204
|
+
if (written) {
|
|
2205
|
+
// What the write holds, as a load reads it, for a write that fails partway.
|
|
2206
|
+
const holds = readConfig(reconciled, 'gate.json');
|
|
2207
|
+
if ('refused' in holds) {
|
|
2208
|
+
// A write of ours doesn't take gate.json's own rules out of force:
|
|
2209
|
+
// nothing is written or recorded, so it loads as it did. (With no
|
|
2210
|
+
// rules of its own, gate.json isn't used at this start, above.)
|
|
2211
|
+
const error = 'with them, gate.json would leave out a rule that could wake the agent, and under its default, '
|
|
2212
|
+
+ `"${changes.defaultChange?.to ?? read.config.default ?? 'always'}", it wouldn't be used`;
|
|
2213
|
+
return { kind: 'reconcile-failed', names, changing, atOpen: true, config: read.config, error };
|
|
2214
|
+
}
|
|
2215
|
+
const text = JSON.stringify(reconciled, null, 2) + '\n';
|
|
2216
|
+
try {
|
|
2217
|
+
writeFileSync(this.configPath, text);
|
|
2218
|
+
} catch (err) {
|
|
2219
|
+
return {
|
|
2220
|
+
kind: 'reconcile-failed',
|
|
2221
|
+
names,
|
|
2222
|
+
changing,
|
|
2223
|
+
error: errorText(err),
|
|
2224
|
+
atOpen: beforeWriting(err),
|
|
2225
|
+
config: holds.config,
|
|
2226
|
+
text,
|
|
2227
|
+
note: loadNote(holds.sentences),
|
|
2228
|
+
sentences: holds.sentences,
|
|
2229
|
+
};
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
if (appended) this.startMergeNote = appendedNote;
|
|
2233
|
+
// Said before the record is written, so a failed record write still
|
|
2234
|
+
// leaves what the start did to gate.json named. A start that only added
|
|
2235
|
+
// rules, since the merge couldn't run, is said in startNotice instead.
|
|
2236
|
+
const line = appended ? null : reconcileLine(changes);
|
|
2237
|
+
if (line) console.error(line);
|
|
2238
|
+
if (line || names.length > 0) {
|
|
2239
|
+
this.emitTrace({
|
|
2240
|
+
type: 'gate:config-reconciled',
|
|
2241
|
+
configPath: this.configPath,
|
|
2242
|
+
...changes,
|
|
2243
|
+
// Every name the start added to gate.json, as an append's trace gave.
|
|
2244
|
+
addedPolicies: names,
|
|
2245
|
+
timestamp: Date.now(),
|
|
2246
|
+
});
|
|
2247
|
+
}
|
|
2248
|
+
if (!appended && (!last.recipe || canonical({ rules: last.recipe.rules, default: last.recipe.default }) !== canonical({ rules: starting.rules.map((rule) => rule.raw), default: given.default }))) {
|
|
2249
|
+
this.recordRecipe(recordPath, starting.given, written ? 'gate.json holds the recipe merged into it' : 'the merge left gate.json as it was');
|
|
2250
|
+
}
|
|
2251
|
+
return { kind: 'none' };
|
|
2252
|
+
}
|
|
2253
|
+
|
|
2254
|
+
/**
|
|
2255
|
+
* Record the starting config, as given, as the recipe last applied, after
|
|
2256
|
+
* gate.json holds what the start merged (`holds` says what that is). A
|
|
2257
|
+
* write that fails is said with the start: the next start merges against
|
|
2258
|
+
* the record as the write left it.
|
|
2259
|
+
*/
|
|
2260
|
+
private recordRecipe(recordPath: string, given: unknown, holds: string): void {
|
|
2261
|
+
try {
|
|
2262
|
+
writeFileSync(recordPath, JSON.stringify(given, null, 2) + '\n');
|
|
2263
|
+
} catch (err) {
|
|
2264
|
+
this.startMergeNote = `At start, ${holds}, and the gate runs it, but recording the recipe in ${basename(recordPath)} failed `
|
|
2265
|
+
+ `(${errorText(err)}), so the next start merges against the record as this write left it. Whoever runs this host can `
|
|
2266
|
+
+ 'fix what kept it from being written.';
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
/**
|
|
2271
|
+
* gate.json as it is on disk: absent; loaded; refused, when it's JSON that
|
|
2272
|
+
* readConfig doesn't use; or unreadable, when it can't be read or isn't
|
|
2273
|
+
* JSON (`notJson`, where it could be read). A loaded file comes with its note,
|
|
2274
|
+
* which names what it holds that does nothing or is read past a problem, and
|
|
2275
|
+
* a loaded or refused one with its own fields and rule list as written, for a
|
|
2276
|
+
* write to edit (Listed).
|
|
2277
|
+
* Reading applies nothing: whoever applies a loaded config records its text
|
|
2278
|
+
* as `configText`. With `unlessUnchanged`, a file whose text is
|
|
2279
|
+
* `configText` comes back `unchanged`, unparsed, since the rules in force
|
|
2280
|
+
* came from it, or are what a start ran beside it or in its place.
|
|
2281
|
+
*/
|
|
2282
|
+
private readConfigFile(opts?: { unlessUnchanged?: boolean }):
|
|
2283
|
+
| { state: 'absent' }
|
|
2284
|
+
| { state: 'unchanged' }
|
|
2285
|
+
| { state: 'loaded'; config: GateConfig; text: string; note: string | null; listed: Listed }
|
|
2286
|
+
| { state: 'refused'; error: string; listed: Listed; silenced?: Array<string | undefined> }
|
|
2287
|
+
| { state: 'unreadable'; error: string; notJson?: true } {
|
|
2288
|
+
let raw: string;
|
|
2289
|
+
try {
|
|
2290
|
+
raw = readFileSync(this.configPath, 'utf-8');
|
|
2291
|
+
} catch (err) {
|
|
2292
|
+
if (!isMissing(err)) return this.configUnreadable(err);
|
|
2293
|
+
// No file is no problem with it: the rules in force stay.
|
|
2294
|
+
this.configError = null;
|
|
2295
|
+
this.configUnused = false;
|
|
2296
|
+
return { state: 'absent' };
|
|
2297
|
+
}
|
|
2298
|
+
// A file holding the text in force has that text's notes as its error,
|
|
2299
|
+
// whatever a failed read or a look at other text since left there: beside
|
|
2300
|
+
// the host's starting rules, they say those run beside it.
|
|
2301
|
+
const inForce = raw === this.configText;
|
|
2302
|
+
if (opts?.unlessUnchanged && inForce) {
|
|
2303
|
+
this.configError = this.configNote;
|
|
2304
|
+
this.configUnused = false;
|
|
2305
|
+
return { state: 'unchanged' };
|
|
2306
|
+
}
|
|
2307
|
+
let parsed: unknown;
|
|
2308
|
+
try {
|
|
2309
|
+
parsed = parseGateJson(raw);
|
|
2310
|
+
} catch (err) {
|
|
2311
|
+
return { ...this.configUnreadable(err), notJson: true };
|
|
2312
|
+
}
|
|
2313
|
+
const read = readConfig(parsed, 'gate.json');
|
|
2314
|
+
// JSON the gate doesn't use is still the operator's file, edited as written.
|
|
2315
|
+
if ('refused' in read) {
|
|
2316
|
+
return { ...this.configUnreadable(new Error(read.refused)), state: 'refused', listed: read.listed, silenced: read.silenced };
|
|
2317
|
+
}
|
|
2318
|
+
// A field the gate doesn't read does nothing, and a rule is read past a
|
|
2319
|
+
// problem or left out, so the file loads, and each is named.
|
|
2320
|
+
const note = loadNote(read.sentences);
|
|
2321
|
+
this.configError = inForce ? this.configNote : note;
|
|
2322
|
+
this.configUnused = false;
|
|
2323
|
+
return { state: 'loaded', config: read.config, text: raw, note, listed: read.listed };
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
/**
|
|
2327
|
+
* gate.json's current error, replacing the last one, and traced and logged
|
|
2328
|
+
* when it's new, so an edit that doesn't load is in the log once.
|
|
2329
|
+
*/
|
|
2330
|
+
private configUnreadable(err: unknown): { state: 'unreadable'; error: string } {
|
|
2331
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
2332
|
+
if (msg !== this.configError) {
|
|
2333
|
+
console.error(`[gate] ${this.configPath} couldn't be used (${msg}), so what it holds isn't in force`);
|
|
746
2334
|
this.emitTrace({
|
|
747
2335
|
type: 'gate:config-error',
|
|
748
2336
|
error: msg,
|
|
749
2337
|
configPath: this.configPath,
|
|
750
2338
|
timestamp: Date.now(),
|
|
751
2339
|
});
|
|
752
|
-
return null; // Caller keeps previous config
|
|
753
2340
|
}
|
|
2341
|
+
this.configError = msg;
|
|
2342
|
+
this.configUnused = true;
|
|
2343
|
+
return { state: 'unreadable', error: msg };
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
/**
|
|
2347
|
+
* gate.json's error as gate_status shows it: one that keeps what the file
|
|
2348
|
+
* holds out of force says what's in force instead, so it reads on its own.
|
|
2349
|
+
*/
|
|
2350
|
+
private configErrorShown(): string | null {
|
|
2351
|
+
if (this.configError === null || !this.configUnused) return this.configError;
|
|
2352
|
+
return `${sentence(this.configError)} So what gate.json holds isn't in force, and the gate runs ${this.rulesRun()}.`;
|
|
2353
|
+
}
|
|
2354
|
+
|
|
2355
|
+
/** What the gate runs, by where its rules came from, for a text that says so: "the host's starting rules". */
|
|
2356
|
+
private rulesRun(): string {
|
|
2357
|
+
if (this.config.policies.length === 0) return `no rules: every event gets the default, "${this.config.default ?? 'always'}"`;
|
|
2358
|
+
// Rules from a gate.json text, with the start's unsaved: only a start
|
|
2359
|
+
// whose reconcile write left gate.json unable to load runs those, since a
|
|
2360
|
+
// write of gate.json that lands, a reload that finds it holding a text
|
|
2361
|
+
// that loads, or, while gate.json's rules are in force, one that finds it
|
|
2362
|
+
// holding the text in force, clears startUnsaved.
|
|
2363
|
+
return this.configSource === 'file'
|
|
2364
|
+
? this.startUnsaved !== null
|
|
2365
|
+
? this.startUnsavedMerged
|
|
2366
|
+
? 'the rules its start was writing to gate.json'
|
|
2367
|
+
: 'the rules it read from gate.json at start and the ones it was adding'
|
|
2368
|
+
: 'the rules of the last gate.json it used'
|
|
2369
|
+
: "the host's starting rules";
|
|
2370
|
+
}
|
|
2371
|
+
|
|
2372
|
+
/**
|
|
2373
|
+
* What a policy change edits: gate.json's own fields and rule list as
|
|
2374
|
+
* written, each rule as written, so a write keeps every rule it doesn't
|
|
2375
|
+
* change, problems and unknown fields included, whether gate.json loads or
|
|
2376
|
+
* is JSON the gate doesn't use. Over one it doesn't use, writeConfig writes
|
|
2377
|
+
* only what loads, so a change is refused, naming what keeps the file out
|
|
2378
|
+
* and what would bring it into force (`remedy`), unless it mends that, and
|
|
2379
|
+
* the file stays as its operator wrote it. When
|
|
2380
|
+
* gate.json is absent, the same of the text whose rules are in force, so a
|
|
2381
|
+
* rule it leaves out keeps its name; or the rules in force, where no text
|
|
2382
|
+
* is, it has no rule list, or what's in force is what a start ran beside or
|
|
2383
|
+
* in place of it. A change is refused (`refused`) for a gate.json that can't
|
|
2384
|
+
* be read or isn't JSON (unusableRefusal), whose rules can't be edited
|
|
2385
|
+
* without writing over what it holds, and for one with no rule list a write
|
|
2386
|
+
* could edit without that (a list, a "policies" that isn't one and holds
|
|
2387
|
+
* something, or JSON that isn't an object): so a removal or an anchor is
|
|
2388
|
+
* refused too, rather than looked for in rules that may not be what the
|
|
2389
|
+
* file holds. It's refused as well for a gate.json that holds the text a
|
|
2390
|
+
* start didn't use (start-without), since what the change wrote would put
|
|
2391
|
+
* its rules in force in place of what the start runs.
|
|
2392
|
+
*/
|
|
2393
|
+
private rulesForChange():
|
|
2394
|
+
| { top: Record<string, unknown>; rules: unknown[]; remedy?: string }
|
|
2395
|
+
| { refused: string } {
|
|
2396
|
+
const read = this.readConfigFile();
|
|
2397
|
+
if (read.state === 'unreadable') return { refused: unusableRefusal(read, this.rulesRun()) };
|
|
2398
|
+
if (read.state === 'loaded' || read.state === 'refused') {
|
|
2399
|
+
if ('noList' in read.listed) return { refused: noListRefusal(read.listed.noList, this.rulesRun()) };
|
|
2400
|
+
// Rules not gate.json's in force while it holds the text in force: what a
|
|
2401
|
+
// start ran in place of one with no rules of its own, until it changes.
|
|
2402
|
+
if (read.state === 'loaded' && read.text === this.configText && this.configSource !== 'file') {
|
|
2403
|
+
return { refused: notUsedRefusal(this.rulesRun()) };
|
|
2404
|
+
}
|
|
2405
|
+
const edit = { top: read.listed.top, rules: [...read.listed.rules] };
|
|
2406
|
+
if (read.state === 'loaded') return edit;
|
|
2407
|
+
// What would bring it into force, for a change that doesn't. A change
|
|
2408
|
+
// finds its rule by name: a replacement takes the first rule with it,
|
|
2409
|
+
// and a removal every one.
|
|
2410
|
+
const names = [...new Set(read.silenced ?? [])];
|
|
2411
|
+
const only = names.length === 1 ? names[0] : undefined;
|
|
2412
|
+
const sharing = only === undefined ? 0 : edit.rules.filter((rule) => nameOf(rule) === only).length;
|
|
2413
|
+
// A removal that would leave no rules in force is refused (writeConfig),
|
|
2414
|
+
// so it isn't offered.
|
|
2415
|
+
const rest = readConfig({ ...edit.top, policies: edit.rules.filter((rule) => nameOf(rule) !== only) }, 'gate.json');
|
|
2416
|
+
const removes = !('refused' in rest) && rest.config.policies.length > 0;
|
|
2417
|
+
return {
|
|
2418
|
+
...edit,
|
|
2419
|
+
remedy: sharing === 1
|
|
2420
|
+
? `${removes ? 'Replacing or removing' : 'Replacing'} "${only}" by name would bring gate.json into force.`
|
|
2421
|
+
: sharing > 1
|
|
2422
|
+
? `gate.json has ${sharing} rules named "${only}", and a change by that name replaces the first of them`
|
|
2423
|
+
+ `${removes ? ' or removes them all' : ''}.`
|
|
2424
|
+
: 'No change through the gate can bring it into force, so gate.json has to be fixed by an edit of its own; '
|
|
2425
|
+
+ 'until then, a change is refused the same way.',
|
|
2426
|
+
};
|
|
2427
|
+
}
|
|
2428
|
+
// While the rules in force aren't gate.json's, configText is the text a
|
|
2429
|
+
// start ran them beside or in place of, so its rules aren't the ones in force.
|
|
2430
|
+
const text = this.configText !== undefined && this.configSource === 'file'
|
|
2431
|
+
? readConfig(parseGateJson(this.configText), 'gate.json')
|
|
2432
|
+
: undefined;
|
|
2433
|
+
return text && !('refused' in text) && !('noList' in text.listed)
|
|
2434
|
+
? { top: text.listed.top, rules: [...text.listed.rules] }
|
|
2435
|
+
: { top: { policies: [], default: this.config.default ?? 'always' }, rules: [...this.config.policies] as unknown[] };
|
|
754
2436
|
}
|
|
755
2437
|
|
|
756
2438
|
private compilePolicies(config: GateConfig): CompiledPolicy[] {
|
|
@@ -778,54 +2460,67 @@ export class EventGate {
|
|
|
778
2460
|
});
|
|
779
2461
|
}
|
|
780
2462
|
|
|
2463
|
+
/**
|
|
2464
|
+
* Clear the state of each rule `was` held in force that `next` leaves out or
|
|
2465
|
+
* gives another behavior: a reload calls it before it puts `next` in force,
|
|
2466
|
+
* a write after. Without this, a rule that came back, or whose tokens 100
|
|
2467
|
+
* became 5 (or keyBy switched, or every 10 became 3), would run on the
|
|
2468
|
+
* buckets and counters of the rule it was. A rule left out loses its stats
|
|
2469
|
+
* (matchCount, lastMatchTimestamp) too; a rule kept keeps them, which are
|
|
2470
|
+
* history, not live state, and one kept with the same behavior keeps all its
|
|
2471
|
+
* state, its denial and fire counts included. Rules that share a name share
|
|
2472
|
+
* its state, so a name keeps it only where its rules, in order, keep their
|
|
2473
|
+
* behaviors. Each pending debounce batch is sent last, as its timer would
|
|
2474
|
+
* have sent it, once every rule's state is cleared; one whose delivery throws
|
|
2475
|
+
* is named in the log and dropped (fireDebounce), and the rest are still
|
|
2476
|
+
* sent, so the write or reload that cleared it has done what it was for, its
|
|
2477
|
+
* caller hears that it did, and no state is left behind.
|
|
2478
|
+
*/
|
|
2479
|
+
private clearStateFor(was: GateConfig, next: GateConfig): { removed: string[]; cleared: string[] } {
|
|
2480
|
+
const behaviorsByName = (config: GateConfig): Map<string, string> => {
|
|
2481
|
+
const byName = new Map<string, string[]>();
|
|
2482
|
+
for (const p of config.policies) byName.set(p.name, [...(byName.get(p.name) ?? []), behaviorFingerprint(p.behavior)]);
|
|
2483
|
+
return new Map([...byName].map(([name, behaviors]) => [name, JSON.stringify(behaviors)]));
|
|
2484
|
+
};
|
|
2485
|
+
const before = behaviorsByName(was);
|
|
2486
|
+
const after = behaviorsByName(next);
|
|
2487
|
+
const removed = [...before.keys()].filter((name) => !after.has(name));
|
|
2488
|
+
for (const name of removed) {
|
|
2489
|
+
this.clearPolicyState(name);
|
|
2490
|
+
this.stats.delete(name);
|
|
2491
|
+
}
|
|
2492
|
+
const cleared = [...after.keys()].filter((name) => before.has(name) && before.get(name) !== after.get(name));
|
|
2493
|
+
for (const name of cleared) this.clearPolicyState(name);
|
|
2494
|
+
for (const name of [...removed, ...cleared]) this.fireDebounce(name);
|
|
2495
|
+
return { removed, cleared };
|
|
2496
|
+
}
|
|
2497
|
+
|
|
781
2498
|
private reloadIfChanged(): void {
|
|
782
2499
|
const now = Date.now();
|
|
783
|
-
if (now
|
|
2500
|
+
if (checkedRecently(now, this.lastReloadCheck)) return;
|
|
784
2501
|
this.lastReloadCheck = now;
|
|
785
2502
|
|
|
786
2503
|
try {
|
|
787
|
-
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
if (
|
|
793
|
-
|
|
794
|
-
// Capture old policies' behavior fingerprints before any state
|
|
795
|
-
// mutation — needed to detect kept policies whose parameters changed.
|
|
796
|
-
const oldBehaviorByName = new Map<string, string>();
|
|
797
|
-
for (const p of this.config.policies) {
|
|
798
|
-
oldBehaviorByName.set(p.name, behaviorFingerprint(p.behavior));
|
|
799
|
-
}
|
|
2504
|
+
// Missing, unchanged or unreadable: the rules in force stay.
|
|
2505
|
+
const read = this.readConfigFile({ unlessUnchanged: true });
|
|
2506
|
+
// gate.json holds the text in force, so none the start ran is unsaved,
|
|
2507
|
+
// unless what's in force is what the start ran beside it or in its place.
|
|
2508
|
+
if (read.state === 'unchanged' && this.configSource === 'file') this.startUnsaved = null;
|
|
2509
|
+
if (read.state !== 'loaded') return;
|
|
2510
|
+
const newConfig = read.config;
|
|
800
2511
|
|
|
801
|
-
|
|
802
|
-
const newPolicyNames = new Set(newConfig.policies.map(p => p.name));
|
|
803
|
-
const removedPolicies: string[] = [];
|
|
804
|
-
for (const oldName of oldBehaviorByName.keys()) {
|
|
805
|
-
if (!newPolicyNames.has(oldName)) {
|
|
806
|
-
this.clearPolicyState(oldName, { deliverPendingDebounce: true });
|
|
807
|
-
this.stats.delete(oldName);
|
|
808
|
-
removedPolicies.push(oldName);
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
// Clean up state for KEPT policies whose behavior parameters changed.
|
|
813
|
-
// Without this, a tokens 100→5 edit (or keyBy switch, or every 10→3,
|
|
814
|
-
// etc.) wouldn't take effect until existing buckets/counters drained.
|
|
815
|
-
// Stats are preserved — matchCount is historical, not live state.
|
|
816
|
-
const stateClearedPolicies: string[] = [];
|
|
817
|
-
for (const newPolicy of newConfig.policies) {
|
|
818
|
-
const oldHash = oldBehaviorByName.get(newPolicy.name);
|
|
819
|
-
if (oldHash === undefined) continue; // new policy, no state to clear
|
|
820
|
-
const newHash = behaviorFingerprint(newPolicy.behavior);
|
|
821
|
-
if (oldHash !== newHash) {
|
|
822
|
-
this.clearPolicyState(newPolicy.name, { deliverPendingDebounce: true });
|
|
823
|
-
stateClearedPolicies.push(newPolicy.name);
|
|
824
|
-
}
|
|
825
|
-
}
|
|
2512
|
+
const { removed: removedPolicies, cleared: stateClearedPolicies } = this.clearStateFor(this.config, newConfig);
|
|
826
2513
|
|
|
827
2514
|
this.config = newConfig;
|
|
828
2515
|
this.compiledPolicies = this.compilePolicies(newConfig);
|
|
2516
|
+
// Recorded only now that its rules are in force, so a reload that
|
|
2517
|
+
// throws before here is tried again at the next check.
|
|
2518
|
+
this.configText = read.text;
|
|
2519
|
+
this.configNote = read.note;
|
|
2520
|
+
this.configSource = 'file';
|
|
2521
|
+
if (read.note) console.error(`[gate] ${read.note}`);
|
|
2522
|
+
// The rules in force are now gate.json's, so none the start ran is unsaved.
|
|
2523
|
+
this.startUnsaved = null;
|
|
829
2524
|
this.lastReloadTimestamp = now;
|
|
830
2525
|
this.refreshShadowWarnings('reload');
|
|
831
2526
|
|
|
@@ -839,7 +2534,7 @@ export class EventGate {
|
|
|
839
2534
|
timestamp: now,
|
|
840
2535
|
});
|
|
841
2536
|
} catch {
|
|
842
|
-
//
|
|
2537
|
+
// The rules in force stay, and the next check tries again.
|
|
843
2538
|
}
|
|
844
2539
|
}
|
|
845
2540
|
|
|
@@ -850,55 +2545,87 @@ export class EventGate {
|
|
|
850
2545
|
/**
|
|
851
2546
|
* Add or replace (upsert) a single policy in gate.json and apply it live.
|
|
852
2547
|
*
|
|
853
|
-
* The raw policy is
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
2548
|
+
* The raw policy is read with the SAME `readRule` used when loading gate.json,
|
|
2549
|
+
* and since its caller is there to fix it, any problem it has throws, naming
|
|
2550
|
+
* every one (nothing is written), and the caller surfaces it as a tool error,
|
|
2551
|
+
* where a load reads past it (readConfig). On success the policy is persisted
|
|
2552
|
+
* to gate.json (so it survives restart and is visible to `workspace--edit _config/gate.json`)
|
|
857
2553
|
* and the in-memory config is swapped in immediately, so the rule takes effect
|
|
858
|
-
* without waiting for the throttled
|
|
2554
|
+
* without waiting for the throttled reload.
|
|
859
2555
|
*
|
|
860
2556
|
* The freshest on-disk config is re-read first, so a concurrent operator edit
|
|
861
|
-
* isn't clobbered
|
|
862
|
-
*
|
|
863
|
-
*
|
|
864
|
-
*
|
|
865
|
-
*
|
|
866
|
-
*
|
|
867
|
-
*
|
|
868
|
-
*
|
|
2557
|
+
* isn't clobbered, and the change edits gate.json's own rule list by name:
|
|
2558
|
+
* every rule it doesn't change is written back as it was written, problems and
|
|
2559
|
+
* fields the gate doesn't read included, and so is everything else gate.json
|
|
2560
|
+
* holds, such as a list of rules parked under another name, beside which a
|
|
2561
|
+
* change adds a "policies" list where there's none (rulesForChange). Over a
|
|
2562
|
+
* gate.json the gate doesn't use, only a change after which it loads is
|
|
2563
|
+
* written, such as one replacing or removing the rule that keeps it out, and
|
|
2564
|
+
* that leaves it rules in force, since it holds events back by default; any
|
|
2565
|
+
* other is refused, naming what does, and the file stays as written. When
|
|
2566
|
+
* gate.json is absent, the change edits the text whose rules are in force the
|
|
2567
|
+
* same way, or the rules in force where there's none. A gate.json that can't
|
|
2568
|
+
* be read or isn't JSON is refused (unusableRefusal), and stays as it is. A policy with the
|
|
2569
|
+
* same `name` is replaced IN PLACE when no `position` is given (order
|
|
2570
|
+
* preserved); WITH a `position` it is moved there — so re-issuing a rule is
|
|
2571
|
+
* also how you reposition it. New policies append by default; `'prepend'` puts
|
|
2572
|
+
* one at the very front (ahead of EVERYTHING, including addressed-message wake
|
|
2573
|
+
* rules — see the shadow lint), and the anchored forms `{before: name}` /
|
|
2574
|
+
* `{after: name}` insert relative to an existing rule, which is almost always
|
|
2575
|
+
* the safer way to order a sampler or debounce against a specific catch-all.
|
|
2576
|
+
* Fields of the policy the gate reads nowhere, and that aren't a slip of one
|
|
2577
|
+
* it does, are left out of what's written, and named to `onIgnoredFields` once
|
|
2578
|
+
* the change is written and traced. A throw from the callback is logged,
|
|
2579
|
+
* and the change stands (afterChange). A field it reads at another level is a
|
|
2580
|
+
* problem, like a slip.
|
|
2581
|
+
*
|
|
2582
|
+
* Throws, writing nothing, for an invalid policy, a placement it can't make,
|
|
2583
|
+
* a gate.json that can't be read or isn't JSON, one with no rule list it
|
|
2584
|
+
* could edit without writing over what it holds, one a start didn't use,
|
|
2585
|
+
* which runs as one without it does, while it's unchanged, or one the gate
|
|
2586
|
+
* doesn't use that the change doesn't mend or would leave with no rules in
|
|
2587
|
+
* force; and throws when the write fails, which may leave gate.json
|
|
2588
|
+
* part-written.
|
|
869
2589
|
*/
|
|
870
2590
|
addPolicy(
|
|
871
2591
|
rawPolicy: unknown,
|
|
872
|
-
options?: {
|
|
2592
|
+
options?: {
|
|
2593
|
+
position?: 'append' | 'prepend' | { before: string } | { after: string };
|
|
2594
|
+
onIgnoredFields?: (notes: string[]) => void;
|
|
2595
|
+
},
|
|
873
2596
|
): GatePolicy {
|
|
874
|
-
const
|
|
875
|
-
|
|
876
|
-
const
|
|
877
|
-
const
|
|
2597
|
+
const reading = readRule(rawPolicy, 'A policy');
|
|
2598
|
+
if (reading.problems.length > 0 || !reading.policy) throw new Error(refusalOf(reading));
|
|
2599
|
+
const policy = reading.policy;
|
|
2600
|
+
const ignored = notesOf(reading);
|
|
2601
|
+
const edit = this.rulesForChange();
|
|
2602
|
+
if ('refused' in edit) throw new Error(edit.refused);
|
|
2603
|
+
const { top, rules, remedy } = edit;
|
|
2604
|
+
const idx = rules.findIndex((rule) => nameOf(rule) === policy.name);
|
|
878
2605
|
const position = options?.position;
|
|
879
2606
|
|
|
880
2607
|
if (idx >= 0 && position === undefined) {
|
|
881
|
-
|
|
2608
|
+
rules[idx] = policy; // replace in place, order preserved
|
|
882
2609
|
} else {
|
|
883
|
-
if (idx >= 0)
|
|
2610
|
+
if (idx >= 0) rules.splice(idx, 1); // repositioning a known rule
|
|
884
2611
|
if (position === 'prepend') {
|
|
885
|
-
|
|
2612
|
+
rules.unshift(policy);
|
|
886
2613
|
} else if (position !== undefined && typeof position === 'object') {
|
|
887
2614
|
const anchorName = 'before' in position ? position.before : position.after;
|
|
888
|
-
const anchorIdx =
|
|
2615
|
+
const anchorIdx = rules.findIndex((rule) => nameOf(rule) === anchorName);
|
|
889
2616
|
if (anchorIdx < 0) {
|
|
890
2617
|
throw new Error(
|
|
891
2618
|
`addPolicy: no policy named "${anchorName}" to anchor on — `
|
|
892
|
-
+ `have: [${
|
|
2619
|
+
+ `have: [${rules.map(nameOf).filter((name) => name !== undefined).join(', ')}]`,
|
|
893
2620
|
);
|
|
894
2621
|
}
|
|
895
|
-
|
|
2622
|
+
rules.splice('before' in position ? anchorIdx : anchorIdx + 1, 0, policy);
|
|
896
2623
|
} else {
|
|
897
|
-
|
|
2624
|
+
rules.push(policy);
|
|
898
2625
|
}
|
|
899
2626
|
}
|
|
900
2627
|
|
|
901
|
-
this.writeConfig(
|
|
2628
|
+
this.writeConfig(top, rules, remedy);
|
|
902
2629
|
this.emitTrace({
|
|
903
2630
|
type: 'gate:policy-added',
|
|
904
2631
|
configPath: this.configPath,
|
|
@@ -906,26 +2633,43 @@ export class EventGate {
|
|
|
906
2633
|
replaced: idx >= 0,
|
|
907
2634
|
timestamp: Date.now(),
|
|
908
2635
|
});
|
|
2636
|
+
if (ignored.length > 0) {
|
|
2637
|
+
// A caller that doesn't take the notes still gets them said, in the log.
|
|
2638
|
+
const onIgnoredFields = options?.onIgnoredFields;
|
|
2639
|
+
if (onIgnoredFields) afterChange('onIgnoredFields', () => onIgnoredFields(ignored));
|
|
2640
|
+
else console.error(`[gate] ${ignored.join(' ')}`);
|
|
2641
|
+
}
|
|
909
2642
|
return policy;
|
|
910
2643
|
}
|
|
911
2644
|
|
|
912
2645
|
/**
|
|
913
2646
|
* Remove a policy by name from gate.json and clear its live state. Any pending
|
|
914
|
-
* debounce batch is
|
|
915
|
-
* so buffered events aren't silently dropped
|
|
916
|
-
*
|
|
917
|
-
* (see addPolicy).
|
|
2647
|
+
* debounce batch is sent once the removal is written and in force, as its
|
|
2648
|
+
* timer would have sent it, so buffered events aren't silently dropped
|
|
2649
|
+
* (clearStateFor). Freshest on-disk config is re-read first, and every other
|
|
2650
|
+
* rule is written back as it was written (see addPolicy). A name that isn't
|
|
2651
|
+
* a non-empty string names no rule, so it returns false, writing nothing,
|
|
2652
|
+
* whatever gate.json holds. Otherwise it throws, writing nothing, for a
|
|
2653
|
+
* gate.json that can't be read or isn't JSON, one with no rule list it could
|
|
2654
|
+
* edit, or one a start didn't use, as addPolicy does, and returns false,
|
|
2655
|
+
* writing nothing, if no policy of that name exists, gate.json's own rules
|
|
2656
|
+
* included, so one the gate leaves out can be removed by name too. For a name
|
|
2657
|
+
* it holds, it throws, writing nothing, for a gate.json the gate doesn't use
|
|
2658
|
+
* that the removal doesn't mend or would leave with no rules in force, and
|
|
2659
|
+
* throws when the write fails, which may
|
|
2660
|
+
* leave gate.json part-written; the rules in force then stay as they were,
|
|
2661
|
+
* with their state.
|
|
918
2662
|
*/
|
|
919
2663
|
removePolicy(name: string): boolean {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
this.writeConfig(
|
|
2664
|
+
// nameOf gives undefined for a rule with no name, so a call from JavaScript
|
|
2665
|
+
// with no name would otherwise remove every such rule.
|
|
2666
|
+
if (typeof name !== 'string' || name === '') return false;
|
|
2667
|
+
const edit = this.rulesForChange();
|
|
2668
|
+
if ('refused' in edit) throw new Error(edit.refused);
|
|
2669
|
+
if (!edit.rules.some((rule) => nameOf(rule) === name)) return false;
|
|
2670
|
+
// The write clears the rule's state once it has succeeded (writeConfig),
|
|
2671
|
+
// so a refusal or a failed write leaves the rule in force with its state.
|
|
2672
|
+
this.writeConfig(edit.top, edit.rules.filter((rule) => nameOf(rule) !== name), edit.remedy);
|
|
929
2673
|
this.emitTrace({
|
|
930
2674
|
type: 'gate:policy-removed',
|
|
931
2675
|
configPath: this.configPath,
|
|
@@ -936,34 +2680,78 @@ export class EventGate {
|
|
|
936
2680
|
}
|
|
937
2681
|
|
|
938
2682
|
/**
|
|
939
|
-
* List the current policy names
|
|
2683
|
+
* List the current policy names: those of a newer gate.json that loads (the
|
|
2684
|
+
* freshest on-disk view), and otherwise the rules in force, which beside a
|
|
2685
|
+
* gate.json with no rule list are the host's starting rules. Cheap helper for
|
|
940
2686
|
* tools/UX that want to show what's active without the full status payload.
|
|
941
2687
|
*/
|
|
942
2688
|
listPolicyNames(): string[] {
|
|
943
|
-
const
|
|
2689
|
+
const read = this.readConfigFile({ unlessUnchanged: true });
|
|
2690
|
+
const current = read.state === 'loaded' ? read.config : this.config;
|
|
944
2691
|
return current.policies.map((p) => p.name);
|
|
945
2692
|
}
|
|
946
2693
|
|
|
947
2694
|
/**
|
|
948
|
-
* Persist
|
|
949
|
-
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
2695
|
+
* Persist gate.json and apply it in memory together: `top`, its own fields,
|
|
2696
|
+
* with `rules` as its rule list. Only what loads is written: a change to a
|
|
2697
|
+
* gate.json the gate doesn't use, for which `remedy` is given, what would
|
|
2698
|
+
* bring it into force, throws, naming that and `remedy`, and writes nothing,
|
|
2699
|
+
* where the change doesn't mend what keeps it out, or where it would leave
|
|
2700
|
+
* gate.json with no rules in force: such a gate.json holds events back by
|
|
2701
|
+
* default, so putting it in force would hold back every event, those the
|
|
2702
|
+
* rules in force wake on included. The rules in force are then what the text
|
|
2703
|
+
* written reads as, as at a load, so what it still holds that the gate reads
|
|
2704
|
+
* past or leaves out stays named; the text is kept as `configText`, so
|
|
2705
|
+
* `reloadIfChanged` doesn't parse it again. Once the text is written and its
|
|
2706
|
+
* rules are in force, a rule that was in force that it leaves out or gives
|
|
2707
|
+
* another behavior loses its state (clearStateFor), last, since a pending
|
|
2708
|
+
* batch it sends can't be taken back, and a write that fails leaves the
|
|
2709
|
+
* rules in force with their state; a batch that can't be delivered is
|
|
2710
|
+
* logged, not thrown. A write that fails throws, saying whether gate.json was
|
|
2711
|
+
* changed: one that fails before writing leaves it as it was, and one that
|
|
2712
|
+
* fails after its open may leave it part-written (beforeWriting). Callers
|
|
2713
|
+
* surface it as a tool error.
|
|
952
2714
|
*/
|
|
953
|
-
private writeConfig(
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
2715
|
+
private writeConfig(top: Record<string, unknown>, rules: unknown[], remedy?: string): void {
|
|
2716
|
+
const text = JSON.stringify({ ...top, policies: rules }, null, 2) + '\n';
|
|
2717
|
+
const read = readConfig(JSON.parse(text), 'gate.json');
|
|
2718
|
+
if ('refused' in read) {
|
|
2719
|
+
throw new Error(
|
|
2720
|
+
`Nothing was saved: with this change, gate.json wouldn't be used. ${sentence(read.refused)}${remedy ? ` ${remedy}` : ''}`,
|
|
2721
|
+
);
|
|
2722
|
+
}
|
|
2723
|
+
// One the gate doesn't use, and that a change can mend, holds back what a
|
|
2724
|
+
// rule it leaves out would wake on, so its default holds events back.
|
|
2725
|
+
if (remedy !== undefined && read.config.policies.length === 0) {
|
|
2726
|
+
throw new Error(
|
|
2727
|
+
'Nothing was saved: with this change, gate.json would have no rules the gate reads, and its default would hold back every '
|
|
2728
|
+
+ `event. Meanwhile the gate runs ${this.rulesRun()}. ${remedy}`,
|
|
2729
|
+
);
|
|
2730
|
+
}
|
|
2731
|
+
try {
|
|
2732
|
+
mkdirSync(dirname(this.configPath), { recursive: true });
|
|
2733
|
+
writeFileSync(this.configPath, text);
|
|
2734
|
+
} catch (err) {
|
|
2735
|
+
throw new Error(beforeWriting(err)
|
|
2736
|
+
? `gate.json couldn't be written (${errorText(err)}), so nothing was saved: the rules in force and gate.json stay `
|
|
2737
|
+
+ 'as they were. Whoever runs this host can fix what keeps it from being written.'
|
|
2738
|
+
: `Writing gate.json failed partway (${errorText(err)}), so the change isn't in force: the rules in force stay as `
|
|
2739
|
+
+ 'they were. gate.json may hold the whole change, which the gate reads at its next reload; or it may hold part of it '
|
|
2740
|
+
+ 'or nothing, which the gate can\'t read, and then they stay until gate.json is fixed. '
|
|
2741
|
+
+ 'Whoever runs this host can fix what kept it from being written, and check what gate.json holds.', { cause: err });
|
|
2742
|
+
}
|
|
2743
|
+
const was = this.config;
|
|
2744
|
+
this.configText = text;
|
|
2745
|
+
this.configNote = loadNote(read.sentences);
|
|
2746
|
+
this.config = read.config;
|
|
2747
|
+
this.compiledPolicies = this.compilePolicies(read.config);
|
|
958
2748
|
this.refreshShadowWarnings('policy-mutation');
|
|
959
2749
|
this.configSource = 'file';
|
|
960
|
-
this.
|
|
2750
|
+
this.configError = this.configNote;
|
|
2751
|
+
this.configUnused = false;
|
|
2752
|
+
this.startUnsaved = null;
|
|
961
2753
|
this.lastReloadTimestamp = Date.now();
|
|
962
|
-
|
|
963
|
-
this.configMtime = statSync(this.configPath).mtimeMs;
|
|
964
|
-
} catch {
|
|
965
|
-
// A failed stat only risks one redundant reload; keep the old mtime.
|
|
966
|
-
}
|
|
2754
|
+
this.clearStateFor(was, read.config);
|
|
967
2755
|
}
|
|
968
2756
|
|
|
969
2757
|
// =========================================================================
|
|
@@ -984,9 +2772,16 @@ export class EventGate {
|
|
|
984
2772
|
return false;
|
|
985
2773
|
}
|
|
986
2774
|
|
|
987
|
-
// Channel check
|
|
2775
|
+
// Channel check: the event's own channel id, or the registered id the
|
|
2776
|
+
// gate resolves for its route. A push event carries the adapter's raw id
|
|
2777
|
+
// (`origin.channelId`, or none), and a channels/incoming message the
|
|
2778
|
+
// registered one, so a pattern in the registered form matches every
|
|
2779
|
+
// event in its channel whose push names the channel (by `mcplChannelId`,
|
|
2780
|
+
// or a Discord origin's guild and channel): its messages on both paths,
|
|
2781
|
+
// and its reactions, edits, voice reports and other pushes.
|
|
988
2782
|
if (compiled.channelRegex && !compiled.channelRegex.test(info.channelId)) {
|
|
989
|
-
|
|
2783
|
+
const registered = this.routeChannelFor(info);
|
|
2784
|
+
if (registered === undefined || !compiled.channelRegex.test(registered)) return false;
|
|
990
2785
|
}
|
|
991
2786
|
|
|
992
2787
|
// Mount check (workspace fs events)
|
|
@@ -1057,10 +2852,9 @@ export class EventGate {
|
|
|
1057
2852
|
// marker requires too: a `source` in an event's metadata is the server's
|
|
1058
2853
|
// own word, which any server can give.
|
|
1059
2854
|
if (this.sleepUntil > this.now()) {
|
|
1060
|
-
this.reloadPrivilegedIfChanged();
|
|
1061
2855
|
const isHeartbeat = info.serverId === 'heartbeat';
|
|
1062
2856
|
const authorId = this.extractAuthorId(info.metadata);
|
|
1063
|
-
const privileged =
|
|
2857
|
+
const privileged = this.isPrivilegedUser(authorId);
|
|
1064
2858
|
if (!isHeartbeat && !privileged) {
|
|
1065
2859
|
this.sleepSuppressed++;
|
|
1066
2860
|
this.totalEvaluations++;
|
|
@@ -1190,10 +2984,17 @@ export class EventGate {
|
|
|
1190
2984
|
// rate_limit + passive_sample + resets
|
|
1191
2985
|
// =========================================================================
|
|
1192
2986
|
|
|
1193
|
-
/** Resolve the partition key for keyBy-style behaviors.
|
|
1194
|
-
|
|
2987
|
+
/** Resolve the partition key for keyBy-style behaviors. `keyBy:
|
|
2988
|
+
* "channelId"` takes the registered id the gate resolves for the event when
|
|
2989
|
+
* it has one, as channel matching does, so a channel's push events count
|
|
2990
|
+
* with its channels/incoming messages rather than under their raw id. */
|
|
2991
|
+
private resolveKey(info: GateEventInfo, keyBy: string | undefined): string {
|
|
1195
2992
|
if (!keyBy) return SHARED_BUCKET_KEY;
|
|
1196
|
-
|
|
2993
|
+
if (keyBy === 'channelId') {
|
|
2994
|
+
const registered = this.routeChannelFor(info);
|
|
2995
|
+
if (registered !== undefined) return registered;
|
|
2996
|
+
}
|
|
2997
|
+
const value = info.metadata?.[keyBy];
|
|
1197
2998
|
if (value === undefined || value === null || value === '') return SHARED_BUCKET_KEY;
|
|
1198
2999
|
return String(value);
|
|
1199
3000
|
}
|
|
@@ -1203,7 +3004,7 @@ export class EventGate {
|
|
|
1203
3004
|
info: GateEventInfo,
|
|
1204
3005
|
config: { tokens: number; refillIntervalMs: number; keyBy?: string },
|
|
1205
3006
|
): GateDecision {
|
|
1206
|
-
const key = this.resolveKey(info
|
|
3007
|
+
const key = this.resolveKey(info, config.keyBy);
|
|
1207
3008
|
let buckets = this.rateLimitBuckets.get(policy.name);
|
|
1208
3009
|
if (!buckets) {
|
|
1209
3010
|
buckets = new Map();
|
|
@@ -1217,7 +3018,14 @@ export class EventGate {
|
|
|
1217
3018
|
buckets.set(key, bucket);
|
|
1218
3019
|
} else if (bucket.tokens < config.tokens) {
|
|
1219
3020
|
const elapsed = now - bucket.lastRefill;
|
|
1220
|
-
|
|
3021
|
+
// A refill interval of 0, read from gate.json or the starting rules
|
|
3022
|
+
// (readRateLimit), refills at once; divided by, it refilled the bucket
|
|
3023
|
+
// once and left its clock at NaN. Under 0, no number of whole intervals
|
|
3024
|
+
// has passed, so it never refills.
|
|
3025
|
+
if (config.refillIntervalMs === 0) {
|
|
3026
|
+
bucket.tokens = config.tokens;
|
|
3027
|
+
bucket.lastRefill = now;
|
|
3028
|
+
} else if (elapsed > 0) {
|
|
1221
3029
|
const tokensToAdd = Math.floor(elapsed / config.refillIntervalMs);
|
|
1222
3030
|
if (tokensToAdd > 0) {
|
|
1223
3031
|
bucket.tokens = Math.min(config.tokens, bucket.tokens + tokensToAdd);
|
|
@@ -1245,7 +3053,7 @@ export class EventGate {
|
|
|
1245
3053
|
info: GateEventInfo,
|
|
1246
3054
|
config: { every: number; keyBy?: string },
|
|
1247
3055
|
): GateDecision {
|
|
1248
|
-
const key = this.resolveKey(info
|
|
3056
|
+
const key = this.resolveKey(info, config.keyBy);
|
|
1249
3057
|
let counters = this.passiveSampleCounters.get(policy.name);
|
|
1250
3058
|
if (!counters) {
|
|
1251
3059
|
counters = new Map();
|
|
@@ -1277,24 +3085,16 @@ export class EventGate {
|
|
|
1277
3085
|
}
|
|
1278
3086
|
|
|
1279
3087
|
/**
|
|
1280
|
-
* Clear
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
*
|
|
1284
|
-
*
|
|
3088
|
+
* Clear one policy's live state (rate-limit buckets, passive-sample
|
|
3089
|
+
* counters, denial / fire counters) and stop its debounce timer, leaving
|
|
3090
|
+
* its pending batch for fireDebounce to send, as clearStateFor does once
|
|
3091
|
+
* every policy's state is cleared. It leaves stats (matchCount,
|
|
3092
|
+
* lastMatchTimestamp), which are history; clearStateFor deletes a removed
|
|
3093
|
+
* policy's.
|
|
1285
3094
|
*/
|
|
1286
|
-
private clearPolicyState(
|
|
1287
|
-
name: string,
|
|
1288
|
-
options: { deliverPendingDebounce: boolean },
|
|
1289
|
-
): void {
|
|
3095
|
+
private clearPolicyState(name: string): void {
|
|
1290
3096
|
const debounce = this.debounceTimers.get(name);
|
|
1291
|
-
if (debounce)
|
|
1292
|
-
clearTimeout(debounce.timer);
|
|
1293
|
-
if (options.deliverPendingDebounce && debounce.events.length > 0) {
|
|
1294
|
-
this.deliverEvents(debounce.events);
|
|
1295
|
-
}
|
|
1296
|
-
this.debounceTimers.delete(name);
|
|
1297
|
-
}
|
|
3097
|
+
if (debounce) clearTimeout(debounce.timer);
|
|
1298
3098
|
this.rateLimitBuckets.delete(name);
|
|
1299
3099
|
this.passiveSampleCounters.delete(name);
|
|
1300
3100
|
this.rateLimitDenied.delete(name);
|
|
@@ -1318,11 +3118,14 @@ export class EventGate {
|
|
|
1318
3118
|
if ('debounce' in b) return false; // batches; never triggers inline
|
|
1319
3119
|
if ('rate_limit' in b) {
|
|
1320
3120
|
const cfg = b.rate_limit;
|
|
1321
|
-
const key = this.resolveKey(info
|
|
3121
|
+
const key = this.resolveKey(info, cfg.keyBy);
|
|
1322
3122
|
const bucket = this.rateLimitBuckets.get(policy.name)?.get(key);
|
|
1323
3123
|
if (!bucket) return true; // fresh bucket starts full
|
|
3124
|
+
// As applyRateLimit refills: at once, for an interval of 0, and never
|
|
3125
|
+
// for one under 0.
|
|
3126
|
+
if (cfg.refillIntervalMs === 0) return true;
|
|
1324
3127
|
let tokens = bucket.tokens;
|
|
1325
|
-
if (tokens < cfg.tokens) {
|
|
3128
|
+
if (tokens < cfg.tokens && cfg.refillIntervalMs > 0) {
|
|
1326
3129
|
const elapsed = Math.max(0, this.now() - bucket.lastRefill);
|
|
1327
3130
|
tokens = Math.min(cfg.tokens, tokens + Math.floor(elapsed / cfg.refillIntervalMs));
|
|
1328
3131
|
}
|
|
@@ -1330,7 +3133,7 @@ export class EventGate {
|
|
|
1330
3133
|
}
|
|
1331
3134
|
if ('passive_sample' in b) {
|
|
1332
3135
|
const cfg = b.passive_sample;
|
|
1333
|
-
const key = this.resolveKey(info
|
|
3136
|
+
const key = this.resolveKey(info, cfg.keyBy);
|
|
1334
3137
|
const count = this.passiveSampleCounters.get(policy.name)?.get(key) ?? 0;
|
|
1335
3138
|
return count + 1 >= cfg.every;
|
|
1336
3139
|
}
|
|
@@ -1357,16 +3160,8 @@ export class EventGate {
|
|
|
1357
3160
|
return { trigger, policyName: null, behavior: this.config.default ?? 'always' };
|
|
1358
3161
|
}
|
|
1359
3162
|
|
|
1360
|
-
/**
|
|
1361
|
-
|
|
1362
|
-
* rule wins each and whether it would wake. Backs the before/after table in
|
|
1363
|
-
* the wake_add_rule result — the point where a rule change's real semantics
|
|
1364
|
-
* must become legible (af#105; the Mythos stand-back-density incident would
|
|
1365
|
-
* have been visible as `dm: discord-direct-address → stand-back-density`).
|
|
1366
|
-
* Probes are representative shapes, not an exhaustive event space — rules
|
|
1367
|
-
* keyed on specific channels/mounts may not match any probe.
|
|
1368
|
-
*/
|
|
1369
|
-
probeTable(): GateProbeRow[] {
|
|
3163
|
+
/** The canonical synthetic events the probe table runs: chat shapes as MCPL delivers them, and a heartbeat. */
|
|
3164
|
+
private probeEvents(): Array<{ label: string; info: GateEventInfo }> {
|
|
1370
3165
|
const chat = (
|
|
1371
3166
|
label: string,
|
|
1372
3167
|
eventType: string,
|
|
@@ -1412,8 +3207,34 @@ export class EventGate {
|
|
|
1412
3207
|
} as GateEventInfo,
|
|
1413
3208
|
},
|
|
1414
3209
|
];
|
|
3210
|
+
return probes;
|
|
3211
|
+
}
|
|
1415
3212
|
|
|
1416
|
-
|
|
3213
|
+
/**
|
|
3214
|
+
* The rules no probe event matches, such as one scoped to an event type
|
|
3215
|
+
* the probes don't model (`external-message`, how a module's messages are
|
|
3216
|
+
* gated): the probe table can't show what they do, so a result that shows
|
|
3217
|
+
* the table names them rather than let its rows read as their effect.
|
|
3218
|
+
*/
|
|
3219
|
+
probeUnreached(): string[] {
|
|
3220
|
+
const events = this.probeEvents().map(({ info }) => info);
|
|
3221
|
+
return this.compiledPolicies
|
|
3222
|
+
.filter((compiled) => !events.some((info) => this.compiledMatches(compiled, info)))
|
|
3223
|
+
.map((compiled) => compiled.policy.name);
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
/**
|
|
3227
|
+
* Probe a canonical set of synthetic chat/heartbeat events and report which
|
|
3228
|
+
* rule wins each and whether it would wake. Backs the before/after table in
|
|
3229
|
+
* the wake_add_rule result — the point where a rule change's real semantics
|
|
3230
|
+
* must become legible (af#105; the Mythos stand-back-density incident would
|
|
3231
|
+
* have been visible as `dm: discord-direct-address → stand-back-density`).
|
|
3232
|
+
* Probes are representative shapes, not an exhaustive event space — rules
|
|
3233
|
+
* keyed on specific channels/mounts, or on other event types, may not match
|
|
3234
|
+
* any probe; `probeUnreached` names those.
|
|
3235
|
+
*/
|
|
3236
|
+
probeTable(): GateProbeRow[] {
|
|
3237
|
+
return this.probeEvents().map(({ label, info }) => {
|
|
1417
3238
|
const d = this.probe(info);
|
|
1418
3239
|
return {
|
|
1419
3240
|
probe: label,
|
|
@@ -1514,6 +3335,8 @@ export class EventGate {
|
|
|
1514
3335
|
serverId: info.serverId || undefined,
|
|
1515
3336
|
addressed: Array.isArray(info.tags) && info.tags.includes('chat:addressed'),
|
|
1516
3337
|
routeChannelId: this.routeChannelFor(info),
|
|
3338
|
+
...(typeof info.metadata?.messageId === 'string' ? { messageId: info.metadata.messageId } : {}),
|
|
3339
|
+
...(typeof info.metadata?.eventId === 'string' ? { eventId: info.metadata.eventId } : {}),
|
|
1517
3340
|
};
|
|
1518
3341
|
|
|
1519
3342
|
const existing = this.debounceTimers.get(policy.name);
|
|
@@ -1546,7 +3369,20 @@ export class EventGate {
|
|
|
1546
3369
|
return;
|
|
1547
3370
|
}
|
|
1548
3371
|
|
|
1549
|
-
|
|
3372
|
+
// A delivery that throws drops the batch, named in the log: thrown from
|
|
3373
|
+
// its timer, it would be uncaught, and stop the process, and thrown to a
|
|
3374
|
+
// write or reload that sends it (clearStateFor), it would report a change
|
|
3375
|
+
// that was made as failed.
|
|
3376
|
+
try {
|
|
3377
|
+
this.deliverEvents(events);
|
|
3378
|
+
} catch (err) {
|
|
3379
|
+
const one = events.length === 1;
|
|
3380
|
+
console.error(
|
|
3381
|
+
`[gate] Policy "${policyName}": its pending batch of ${events.length} ${one ? 'event' : 'events'} couldn't be delivered `
|
|
3382
|
+
+ `(${errorText(err)}), so the batch is dropped`,
|
|
3383
|
+
);
|
|
3384
|
+
return;
|
|
3385
|
+
}
|
|
1550
3386
|
|
|
1551
3387
|
this.emitTrace({
|
|
1552
3388
|
type: 'gate:debounce-delivered',
|
|
@@ -1574,40 +3410,68 @@ export class EventGate {
|
|
|
1574
3410
|
const referenced = events.filter(e => e.inContext);
|
|
1575
3411
|
|
|
1576
3412
|
const lines: string[] = quoted.map(e => `- [${e.policyName}] (${e.eventType}): ${e.content}`);
|
|
3413
|
+
// How each referenced channel is named in the line, in order.
|
|
3414
|
+
const shown: string[] = [];
|
|
1577
3415
|
if (referenced.length > 0) {
|
|
1578
|
-
|
|
3416
|
+
// By channel, its server and its id: two servers' channels can share
|
|
3417
|
+
// an id.
|
|
3418
|
+
const byChannel = new Map<string, { name: string; count: number; oldest: number; label?: string }>();
|
|
1579
3419
|
for (const e of referenced) {
|
|
1580
3420
|
// The registered id when known, so this line names the channel the
|
|
1581
3421
|
// same way routing notices and send tools do (a push event's own
|
|
1582
|
-
// channelId is the adapter's raw id)
|
|
1583
|
-
|
|
1584
|
-
|
|
3422
|
+
// channelId is the adapter's raw id), shown as the agent names it
|
|
3423
|
+
// back where the event names its server.
|
|
3424
|
+
// Only a registered id is a channel name; an adapter's raw id is
|
|
3425
|
+
// shown as it is.
|
|
3426
|
+
const id = e.routeChannelId ?? e.channelId;
|
|
3427
|
+
const name = id === undefined
|
|
3428
|
+
? '(unknown channel)'
|
|
3429
|
+
: e.routeChannelId !== undefined && e.serverId !== undefined && this.channelShownFn
|
|
3430
|
+
? this.channelShownFn({ serverId: e.serverId, channelId: e.routeChannelId })
|
|
3431
|
+
: id;
|
|
3432
|
+
const key = JSON.stringify([e.serverId ?? null, id ?? null]);
|
|
3433
|
+
const entry = byChannel.get(key) ?? { name, count: 0, oldest: e.timestamp, label: e.channelLabel };
|
|
1585
3434
|
entry.count += 1;
|
|
1586
3435
|
entry.oldest = Math.min(entry.oldest, e.timestamp);
|
|
1587
3436
|
entry.label = entry.label ?? e.channelLabel;
|
|
1588
3437
|
byChannel.set(key, entry);
|
|
1589
3438
|
}
|
|
1590
|
-
for (const
|
|
3439
|
+
for (const { name, count, oldest, label } of byChannel.values()) {
|
|
1591
3440
|
// Prefer the human-readable name; keep the id parenthesized so the
|
|
1592
3441
|
// agent can still address tools that want the composite id. A name
|
|
1593
3442
|
// may carry its own `#`, so one is added only where the name lacks
|
|
1594
3443
|
// it, as at the other places that add a `#`.
|
|
1595
|
-
const channel = label ? `${label.startsWith('#') ? label : `#${label}`} (${
|
|
1596
|
-
|
|
1597
|
-
lines.push(
|
|
1598
|
-
`- ${count} message${count > 1 ? 's' : ''} in ${channel} ` +
|
|
1599
|
-
`(oldest ~${age}s ago) — already shown above at arrival; this is a ` +
|
|
1600
|
-
`batched wake, not new content`,
|
|
1601
|
-
);
|
|
3444
|
+
const channel = label ? `${label.startsWith('#') ? label : `#${label}`} (${name})` : name;
|
|
3445
|
+
shown.push(channel);
|
|
3446
|
+
lines.push(referenceLine(count, channel, oldest, Date.now()));
|
|
1602
3447
|
}
|
|
1603
3448
|
}
|
|
1604
3449
|
|
|
3450
|
+
// The line says which messages a wake is for. For one message that is
|
|
3451
|
+
// still the newest thing in the window it would be written to, it says
|
|
3452
|
+
// nothing the message right above doesn't. But the turn compiles later,
|
|
3453
|
+
// and what lands in between puts the message above other entries, so the
|
|
3454
|
+
// gate hands the line to the wake (WakeProvenance.line) and a fresh start
|
|
3455
|
+
// of the wake's turn decides it (WakeLine). Several messages, one with
|
|
3456
|
+
// anything after it, or content the line quotes, keep the line now.
|
|
1605
3457
|
const text = `[Gate: ${events.length} event${events.length > 1 ? 's' : ''} matched]\n\n${lines.join('\n')}`;
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
3458
|
+
const only = events.length === 1 && events[0].inContext ? events[0] : undefined;
|
|
3459
|
+
let line: WakeLine | undefined;
|
|
3460
|
+
if (only && this.newestEntry(only)) {
|
|
3461
|
+
const shownChannel = only.routeChannelId ?? only.channelId;
|
|
3462
|
+
line = {
|
|
3463
|
+
entry: entryRef(only),
|
|
3464
|
+
policyNames,
|
|
3465
|
+
shown: shown[0],
|
|
3466
|
+
at: only.timestamp,
|
|
3467
|
+
...(shownChannel !== undefined ? { channelId: shownChannel } : {}),
|
|
3468
|
+
};
|
|
3469
|
+
} else {
|
|
3470
|
+
this.addMessageFn('user', [{ type: 'text', text }], {
|
|
3471
|
+
source: 'gate:debounce',
|
|
3472
|
+
policies: policyNames,
|
|
3473
|
+
});
|
|
3474
|
+
}
|
|
1611
3475
|
|
|
1612
3476
|
// Provenance of the batched wake (who/where woke the agent): the newest
|
|
1613
3477
|
// ADDRESSED event first, else the newest event naming a channel or an
|
|
@@ -1615,13 +3479,26 @@ export class EventGate {
|
|
|
1615
3479
|
// event addressed the agent and its registered channel is known,
|
|
1616
3480
|
// `routeChannelId` names the turn's speech locus too — the reply goes
|
|
1617
3481
|
// back to whoever addressed the agent, not to whichever channel last saw
|
|
1618
|
-
// any traffic. Ambient-only batches
|
|
1619
|
-
|
|
3482
|
+
// any traffic. Ambient-only batches set no locus. A batch whose line the
|
|
3483
|
+
// gate left to the turn's start carries it too (`line`), one object
|
|
3484
|
+
// shared by every agent the batch wakes, so it is written once.
|
|
3485
|
+
const chosen = wakeProvenance(events);
|
|
3486
|
+
const provenance = line ? { ...chosen, line } : chosen;
|
|
1620
3487
|
for (const agentName of this.getAgentNamesFn()) {
|
|
1621
3488
|
this.requestInferenceFn(agentName, 'gate:debounce', 'gate', provenance);
|
|
1622
3489
|
}
|
|
1623
3490
|
}
|
|
1624
3491
|
|
|
3492
|
+
/** isNewestEntry for one event; a callback fault keeps the line. */
|
|
3493
|
+
private newestEntry(event: PendingEvent): boolean {
|
|
3494
|
+
if (!this.isNewestEntryFn) return false;
|
|
3495
|
+
try {
|
|
3496
|
+
return this.isNewestEntryFn(entryRef(event)) === true;
|
|
3497
|
+
} catch {
|
|
3498
|
+
return false;
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
|
|
1625
3502
|
// =========================================================================
|
|
1626
3503
|
// Inference lifecycle
|
|
1627
3504
|
// =========================================================================
|
|
@@ -1673,8 +3550,20 @@ export class EventGate {
|
|
|
1673
3550
|
if (this.quiesced) return;
|
|
1674
3551
|
if (this.inferring.size === 0 && this.inferenceBuffer.length > 0) {
|
|
1675
3552
|
const events = this.inferenceBuffer.splice(0);
|
|
1676
|
-
// Defer to avoid re-entrancy inside trace callbacks
|
|
1677
|
-
|
|
3553
|
+
// Defer to avoid re-entrancy inside trace callbacks. A delivery that
|
|
3554
|
+
// throws drops what was held, named in the log, as fireDebounce drops a
|
|
3555
|
+
// batch: thrown in the microtask, it would be uncaught.
|
|
3556
|
+
queueMicrotask(() => {
|
|
3557
|
+
try {
|
|
3558
|
+
this.deliverEvents(events);
|
|
3559
|
+
} catch (err) {
|
|
3560
|
+
const one = events.length === 1;
|
|
3561
|
+
console.error(
|
|
3562
|
+
`[gate] ${events.length} ${one ? 'event' : 'events'} held for an agent's turn or the host's quiesce couldn't be `
|
|
3563
|
+
+ `delivered (${errorText(err)}), so ${one ? "it's" : "they're"} dropped`,
|
|
3564
|
+
);
|
|
3565
|
+
}
|
|
3566
|
+
});
|
|
1678
3567
|
}
|
|
1679
3568
|
}
|
|
1680
3569
|
|
|
@@ -1782,10 +3671,9 @@ export class EventGate {
|
|
|
1782
3671
|
|
|
1783
3672
|
/**
|
|
1784
3673
|
* `agentName` is gone, as a conversation fork's end or an ephemeral run's
|
|
1785
|
-
* removes one: its pending self-wake is cancelled,
|
|
1786
|
-
*
|
|
1787
|
-
*
|
|
1788
|
-
* exists. A sleep another agent set, or one set with no agent, stays.
|
|
3674
|
+
* removes one: its pending self-wake is cancelled, rather than wake a name
|
|
3675
|
+
* that no longer exists. It holds no sleep: neither can set one (the
|
|
3676
|
+
* framework refuses their `sleep`).
|
|
1789
3677
|
*/
|
|
1790
3678
|
agentGone(agentName: string): void {
|
|
1791
3679
|
const selfWake = this.selfWakeTimers.get(agentName);
|
|
@@ -1793,7 +3681,6 @@ export class EventGate {
|
|
|
1793
3681
|
clearTimeout(selfWake);
|
|
1794
3682
|
this.selfWakeTimers.delete(agentName);
|
|
1795
3683
|
}
|
|
1796
|
-
if (this.sleepUntil !== 0 && this.sleepAgent === agentName) this.clearSleep();
|
|
1797
3684
|
}
|
|
1798
3685
|
|
|
1799
3686
|
// =========================================================================
|
|
@@ -1832,15 +3719,21 @@ export class EventGate {
|
|
|
1832
3719
|
const nowIso = new Date(this.now()).toISOString().slice(0, 19) + 'Z';
|
|
1833
3720
|
// Deliver the notice into the WAKING agent's window — a self-wake
|
|
1834
3721
|
// armed by a non-primary agent (e.g. a subconscious cadence) must not
|
|
1835
|
-
// narrate itself into the primary's context.
|
|
3722
|
+
// narrate itself into the primary's context. Its wake goes with it: an
|
|
3723
|
+
// agent that heard the notice during a turn was awake when its timer
|
|
3724
|
+
// fired, and the host drops the wake once a request that carried the
|
|
3725
|
+
// notice is answered.
|
|
3726
|
+
const placement: WritePlacement = {};
|
|
1836
3727
|
this.addMessageFn('user', [{
|
|
1837
3728
|
type: 'text',
|
|
1838
3729
|
text: `[self-wake] your ${source} timer (${Math.round(ms / 1000)}s) elapsed — now ${nowIso}`,
|
|
1839
|
-
}], { source: 'gate:self-wake' }, agentName);
|
|
3730
|
+
}], { source: 'gate:self-wake' }, agentName, placement);
|
|
1840
3731
|
this.requestInferenceFn(
|
|
1841
3732
|
agentName,
|
|
1842
3733
|
`self-scheduled wake (${source}, ${Math.round(ms / 1000)}s)`,
|
|
1843
3734
|
'self-wake',
|
|
3735
|
+
undefined,
|
|
3736
|
+
placement,
|
|
1844
3737
|
);
|
|
1845
3738
|
}, ms);
|
|
1846
3739
|
this.selfWakeTimers.set(agentName, timer);
|
|
@@ -1918,7 +3811,6 @@ export class EventGate {
|
|
|
1918
3811
|
return null;
|
|
1919
3812
|
}
|
|
1920
3813
|
|
|
1921
|
-
/** Load the privileged-users file (bare array or { userIds: [...] }). */
|
|
1922
3814
|
/**
|
|
1923
3815
|
* Whether an author id is on the privileged-users list (hot-reloaded).
|
|
1924
3816
|
* Shared by sleep bypass and tune-out wake classification (#77: wakes are
|
|
@@ -1926,45 +3818,74 @@ export class EventGate {
|
|
|
1926
3818
|
*/
|
|
1927
3819
|
isPrivilegedUser(authorId: string | null | undefined): boolean {
|
|
1928
3820
|
if (!authorId) return false;
|
|
1929
|
-
this.
|
|
3821
|
+
this.reloadPrivilegedIfChanged();
|
|
1930
3822
|
return this.privilegedUserIds.has(authorId);
|
|
1931
3823
|
}
|
|
1932
3824
|
|
|
1933
|
-
|
|
3825
|
+
/**
|
|
3826
|
+
* Read the privileged-users file: a list of user ids, or `{"userIds": [...]}`,
|
|
3827
|
+
* each a non-empty string or a safe integer (a whole number within 2^53 - 1 of zero). An absent file
|
|
3828
|
+
* means no one is privileged. A file that can't be read, isn't JSON, or
|
|
3829
|
+
* holds no list leaves the list that last loaded in this process in effect
|
|
3830
|
+
* (none, at startup), with its error in gate_status until the file is read
|
|
3831
|
+
* again. In a list, an entry that isn't a user id is skipped, and named in
|
|
3832
|
+
* the file's error, and the rest load. With `unlessUnchanged`, a file whose
|
|
3833
|
+
* text is the text last read isn't parsed again.
|
|
3834
|
+
*/
|
|
3835
|
+
private loadPrivileged(opts?: { unlessUnchanged?: boolean }): void {
|
|
1934
3836
|
if (!this.privilegedUsersPath) return;
|
|
3837
|
+
const failed = (why: string): string => `Failed to load privileged-users file: ${why}`;
|
|
3838
|
+
let raw: string;
|
|
1935
3839
|
try {
|
|
1936
|
-
|
|
3840
|
+
raw = readFileSync(this.privilegedUsersPath, 'utf8');
|
|
3841
|
+
} catch (err) {
|
|
3842
|
+
// Read again next time: a file can become readable with its text
|
|
3843
|
+
// unchanged, as a chmod makes it.
|
|
3844
|
+
this.privilegedText = undefined;
|
|
3845
|
+
if (isMissing(err)) {
|
|
1937
3846
|
this.privilegedUserIds = new Set();
|
|
1938
|
-
this.
|
|
1939
|
-
|
|
3847
|
+
this.privilegedError = null;
|
|
3848
|
+
} else {
|
|
3849
|
+
this.privilegedError = failed(err instanceof Error ? err.message : String(err));
|
|
1940
3850
|
}
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
3851
|
+
return;
|
|
3852
|
+
}
|
|
3853
|
+
if (opts?.unlessUnchanged && raw === this.privilegedText) return;
|
|
3854
|
+
this.privilegedText = raw;
|
|
3855
|
+
let ids: unknown;
|
|
3856
|
+
try {
|
|
3857
|
+
const parsed: unknown = parseGateJson(raw);
|
|
3858
|
+
ids = Array.isArray(parsed) ? parsed : (parsed as { userIds?: unknown } | null)?.userIds;
|
|
1945
3859
|
} catch (err) {
|
|
1946
|
-
this.
|
|
1947
|
-
|
|
1948
|
-
|
|
3860
|
+
this.privilegedError = failed(err instanceof Error ? err.message : String(err));
|
|
3861
|
+
return;
|
|
3862
|
+
}
|
|
3863
|
+
// A file that parses but holds no list hasn't loaded either: it used to
|
|
3864
|
+
// mean no one is privileged, without a word.
|
|
3865
|
+
if (!Array.isArray(ids)) {
|
|
3866
|
+
this.privilegedError = failed('expected a list of user ids, or {"userIds": [...]}');
|
|
3867
|
+
return;
|
|
3868
|
+
}
|
|
3869
|
+
const loaded = new Set<string>();
|
|
3870
|
+
const skipped: unknown[] = [];
|
|
3871
|
+
for (const id of ids) {
|
|
3872
|
+
if (typeof id === 'string' && id !== '') loaded.add(id);
|
|
3873
|
+
// JSON.parse has already rounded an integer past 2^53, and the rounded
|
|
3874
|
+
// value can be another user's id, so it's skipped rather than loaded.
|
|
3875
|
+
else if (typeof id === 'number' && Number.isSafeInteger(id)) loaded.add(String(id));
|
|
3876
|
+
else skipped.push(id);
|
|
1949
3877
|
}
|
|
3878
|
+
this.privilegedUserIds = loaded;
|
|
3879
|
+
this.privilegedError = skipped.length > 0 ? skippedPrivilegedIds(skipped, loaded.size) : null;
|
|
1950
3880
|
}
|
|
1951
3881
|
|
|
1952
|
-
/**
|
|
3882
|
+
/** At most once a second, the privileged-users file read again, and loaded when its text has changed. */
|
|
1953
3883
|
private reloadPrivilegedIfChanged(): void {
|
|
1954
3884
|
if (!this.privilegedUsersPath) return;
|
|
1955
3885
|
const now = this.now();
|
|
1956
|
-
if (now
|
|
3886
|
+
if (checkedRecently(now, this.lastPrivilegedCheck)) return;
|
|
1957
3887
|
this.lastPrivilegedCheck = now;
|
|
1958
|
-
|
|
1959
|
-
if (!existsSync(this.privilegedUsersPath)) {
|
|
1960
|
-
if (this.privilegedUserIds.size > 0) this.privilegedUserIds = new Set();
|
|
1961
|
-
return;
|
|
1962
|
-
}
|
|
1963
|
-
const mtime = statSync(this.privilegedUsersPath).mtimeMs;
|
|
1964
|
-
if (mtime !== this.privilegedMtime) this.loadPrivileged();
|
|
1965
|
-
} catch {
|
|
1966
|
-
/* leave the last-known set in place on a transient stat/read error */
|
|
1967
|
-
}
|
|
3888
|
+
this.loadPrivileged({ unlessUnchanged: true });
|
|
1968
3889
|
}
|
|
1969
3890
|
|
|
1970
3891
|
getToolDefinition(): ToolDefinition {
|
|
@@ -1978,6 +3899,10 @@ export class EventGate {
|
|
|
1978
3899
|
}
|
|
1979
3900
|
|
|
1980
3901
|
async handleToolCall(): Promise<{ success: boolean; data?: GateStatus; error?: string }> {
|
|
3902
|
+
// What's in force now: each file read again if a check is due, as the
|
|
3903
|
+
// next event would.
|
|
3904
|
+
this.reloadIfChanged();
|
|
3905
|
+
this.reloadPrivilegedIfChanged();
|
|
1981
3906
|
const status = this.getStatus();
|
|
1982
3907
|
return { success: true, data: status };
|
|
1983
3908
|
}
|
|
@@ -2036,7 +3961,8 @@ export class EventGate {
|
|
|
2036
3961
|
default: this.config.default ?? 'always',
|
|
2037
3962
|
script: this.gateScript.status(),
|
|
2038
3963
|
policies,
|
|
2039
|
-
errors: [
|
|
3964
|
+
errors: [this.configErrorShown(), this.startNotice, this.startUnsaved, this.privilegedError]
|
|
3965
|
+
.filter((e): e is string => e !== null),
|
|
2040
3966
|
shadowWarnings: this.shadowWarningsCache.map(formatShadowWarning),
|
|
2041
3967
|
totalEvaluations: this.totalEvaluations,
|
|
2042
3968
|
defaultDecisions: {
|