@chatwarden/core 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +173 -3
- package/dist/src/ai/agreement.d.ts +46 -0
- package/dist/src/ai/agreement.d.ts.map +1 -0
- package/dist/src/ai/agreement.js +71 -0
- package/dist/src/ai/agreement.js.map +1 -0
- package/dist/src/ai/breaker.d.ts +31 -0
- package/dist/src/ai/breaker.d.ts.map +1 -0
- package/dist/src/ai/breaker.js +62 -0
- package/dist/src/ai/breaker.js.map +1 -0
- package/dist/src/ai/budget.d.ts +26 -0
- package/dist/src/ai/budget.d.ts.map +1 -0
- package/dist/src/ai/budget.js +57 -0
- package/dist/src/ai/budget.js.map +1 -0
- package/dist/src/ai/case.d.ts +37 -0
- package/dist/src/ai/case.d.ts.map +1 -0
- package/dist/src/ai/case.js +30 -0
- package/dist/src/ai/case.js.map +1 -0
- package/dist/src/ai/casebook.d.ts +59 -0
- package/dist/src/ai/casebook.d.ts.map +1 -0
- package/dist/src/ai/casebook.js +47 -0
- package/dist/src/ai/casebook.js.map +1 -0
- package/dist/src/ai/ceilings.d.ts +42 -0
- package/dist/src/ai/ceilings.d.ts.map +1 -0
- package/dist/src/ai/ceilings.js +59 -0
- package/dist/src/ai/ceilings.js.map +1 -0
- package/dist/src/ai/fake.d.ts +41 -0
- package/dist/src/ai/fake.d.ts.map +1 -0
- package/dist/src/ai/fake.js +75 -0
- package/dist/src/ai/fake.js.map +1 -0
- package/dist/src/ai/hash.d.ts +15 -0
- package/dist/src/ai/hash.d.ts.map +1 -0
- package/dist/src/ai/hash.js +22 -0
- package/dist/src/ai/hash.js.map +1 -0
- package/dist/src/ai/inert.d.ts +5 -0
- package/dist/src/ai/inert.d.ts.map +1 -0
- package/dist/src/ai/inert.js +22 -0
- package/dist/src/ai/inert.js.map +1 -0
- package/dist/src/ai/injection.d.ts +8 -0
- package/dist/src/ai/injection.d.ts.map +1 -0
- package/dist/src/ai/injection.js +30 -0
- package/dist/src/ai/injection.js.map +1 -0
- package/dist/src/ai/lines.d.ts +64 -0
- package/dist/src/ai/lines.d.ts.map +1 -0
- package/dist/src/ai/lines.js +172 -0
- package/dist/src/ai/lines.js.map +1 -0
- package/dist/src/ai/prompt.d.ts +48 -0
- package/dist/src/ai/prompt.d.ts.map +1 -0
- package/dist/src/ai/prompt.js +196 -0
- package/dist/src/ai/prompt.js.map +1 -0
- package/dist/src/ai/redact.d.ts +29 -0
- package/dist/src/ai/redact.d.ts.map +1 -0
- package/dist/src/ai/redact.js +160 -0
- package/dist/src/ai/redact.js.map +1 -0
- package/dist/src/ai/schema.d.ts +6 -0
- package/dist/src/ai/schema.d.ts.map +1 -0
- package/dist/src/ai/schema.js +50 -0
- package/dist/src/ai/schema.js.map +1 -0
- package/dist/src/ai/service.d.ts +332 -0
- package/dist/src/ai/service.d.ts.map +1 -0
- package/dist/src/ai/service.js +751 -0
- package/dist/src/ai/service.js.map +1 -0
- package/dist/src/ai/types.d.ts +196 -0
- package/dist/src/ai/types.d.ts.map +1 -0
- package/dist/src/ai/types.js +23 -0
- package/dist/src/ai/types.js.map +1 -0
- package/dist/src/ai/validate.d.ts +30 -0
- package/dist/src/ai/validate.d.ts.map +1 -0
- package/dist/src/ai/validate.js +160 -0
- package/dist/src/ai/validate.js.map +1 -0
- package/dist/src/ai/worker.d.ts +76 -0
- package/dist/src/ai/worker.d.ts.map +1 -0
- package/dist/src/ai/worker.js +293 -0
- package/dist/src/ai/worker.js.map +1 -0
- package/dist/src/audit.d.ts +12 -3
- package/dist/src/audit.d.ts.map +1 -1
- package/dist/src/audit.js +30 -4
- package/dist/src/audit.js.map +1 -1
- package/dist/src/chat.d.ts +39 -1
- package/dist/src/chat.d.ts.map +1 -1
- package/dist/src/chat.js +85 -6
- package/dist/src/chat.js.map +1 -1
- package/dist/src/chatwarden.d.ts +84 -1
- package/dist/src/chatwarden.d.ts.map +1 -1
- package/dist/src/chatwarden.js +168 -6
- package/dist/src/chatwarden.js.map +1 -1
- package/dist/src/decide.d.ts +36 -1
- package/dist/src/decide.d.ts.map +1 -1
- package/dist/src/decide.js +62 -2
- package/dist/src/decide.js.map +1 -1
- package/dist/src/documents.d.ts +1 -1
- package/dist/src/documents.d.ts.map +1 -1
- package/dist/src/documents.js +6 -1
- package/dist/src/documents.js.map +1 -1
- package/dist/src/freeze.d.ts +3 -0
- package/dist/src/freeze.d.ts.map +1 -0
- package/dist/src/freeze.js +10 -0
- package/dist/src/freeze.js.map +1 -0
- package/dist/src/index.d.ts +37 -8
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +23 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/ladder.d.ts +28 -0
- package/dist/src/ladder.d.ts.map +1 -1
- package/dist/src/ladder.js +37 -1
- package/dist/src/ladder.js.map +1 -1
- package/dist/src/pace.d.ts +66 -0
- package/dist/src/pace.d.ts.map +1 -0
- package/dist/src/pace.js +111 -0
- package/dist/src/pace.js.map +1 -0
- package/dist/src/pacing.d.ts +63 -0
- package/dist/src/pacing.d.ts.map +1 -0
- package/dist/src/pacing.js +198 -0
- package/dist/src/pacing.js.map +1 -0
- package/dist/src/policy.d.ts +57 -1
- package/dist/src/policy.d.ts.map +1 -1
- package/dist/src/policy.js +67 -4
- package/dist/src/policy.js.map +1 -1
- package/dist/src/profile.d.ts +64 -8
- package/dist/src/profile.d.ts.map +1 -1
- package/dist/src/profile.js +98 -27
- package/dist/src/profile.js.map +1 -1
- package/dist/src/profileError.d.ts +10 -0
- package/dist/src/profileError.d.ts.map +1 -0
- package/dist/src/profileError.js +17 -0
- package/dist/src/profileError.js.map +1 -0
- package/dist/src/retention.d.ts +28 -0
- package/dist/src/retention.d.ts.map +1 -1
- package/dist/src/retention.js +42 -10
- package/dist/src/retention.js.map +1 -1
- package/dist/src/signalCounts.d.ts +37 -0
- package/dist/src/signalCounts.d.ts.map +1 -0
- package/dist/src/signalCounts.js +117 -0
- package/dist/src/signalCounts.js.map +1 -0
- package/dist/src/signalInert.d.ts +10 -0
- package/dist/src/signalInert.d.ts.map +1 -0
- package/dist/src/signalInert.js +28 -0
- package/dist/src/signalInert.js.map +1 -0
- package/dist/src/signalService.d.ts +101 -0
- package/dist/src/signalService.d.ts.map +1 -0
- package/dist/src/signalService.js +383 -0
- package/dist/src/signalService.js.map +1 -0
- package/dist/src/signals.d.ts +186 -0
- package/dist/src/signals.d.ts.map +1 -0
- package/dist/src/signals.js +361 -0
- package/dist/src/signals.js.map +1 -0
- package/dist/src/subjects.d.ts +2 -0
- package/dist/src/subjects.d.ts.map +1 -1
- package/dist/src/subjects.js +4 -0
- package/dist/src/subjects.js.map +1 -1
- package/dist/src/team.d.ts +27 -0
- package/dist/src/team.d.ts.map +1 -1
- package/dist/src/team.js +92 -4
- package/dist/src/team.js.map +1 -1
- package/dist/src/types.d.ts +324 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/package.json +2 -2
- package/src/ai/agreement.ts +106 -0
- package/src/ai/breaker.ts +73 -0
- package/src/ai/budget.ts +65 -0
- package/src/ai/case.ts +74 -0
- package/src/ai/casebook.ts +75 -0
- package/src/ai/ceilings.ts +68 -0
- package/src/ai/fake.ts +105 -0
- package/src/ai/hash.ts +23 -0
- package/src/ai/inert.ts +26 -0
- package/src/ai/injection.ts +32 -0
- package/src/ai/lines.ts +216 -0
- package/src/ai/prompt.ts +223 -0
- package/src/ai/redact.ts +174 -0
- package/src/ai/schema.ts +51 -0
- package/src/ai/service.ts +961 -0
- package/src/ai/types.ts +182 -0
- package/src/ai/validate.ts +173 -0
- package/src/ai/worker.ts +372 -0
- package/src/audit.ts +31 -5
- package/src/chat.ts +109 -8
- package/src/chatwarden.ts +248 -8
- package/src/decide.ts +97 -4
- package/src/documents.ts +7 -2
- package/src/freeze.ts +8 -0
- package/src/index.ts +91 -2
- package/src/ladder.ts +59 -1
- package/src/pace.ts +139 -0
- package/src/pacing.ts +242 -0
- package/src/policy.ts +107 -7
- package/src/profile.ts +149 -27
- package/src/profileError.ts +17 -0
- package/src/retention.ts +63 -10
- package/src/signalCounts.ts +133 -0
- package/src/signalInert.ts +31 -0
- package/src/signalService.ts +406 -0
- package/src/signals.ts +489 -0
- package/src/subjects.ts +5 -0
- package/src/team.ts +93 -5
- package/src/types.ts +358 -2
package/src/ai/worker.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import type { AiCallUsage, AiErrorKind, AiReadRow, Clock, ReportRecord, Store } from '../types.js';
|
|
2
|
+
import { nextUtcMidnight, startOfUtcDay } from './budget.js';
|
|
3
|
+
import type { CallWindow } from './budget.js';
|
|
4
|
+
import type { Breaker } from './breaker.js';
|
|
5
|
+
import type { Prepared } from './case.js';
|
|
6
|
+
import { usedExampleIds } from './prompt.js';
|
|
7
|
+
import { validateAIOutput } from './validate.js';
|
|
8
|
+
import type { AbortSignalLike, AIExampleInput, AIRead, AIReadInput, AIReadResult, AISignal, AIUsage, ModerationAI } from './types.js';
|
|
9
|
+
|
|
10
|
+
// One read, start to finish (design section 4). The job reserves the right to call (one atomic write on the store, so two processes
|
|
11
|
+
// make one call), checks the budget, the breaker and the rate, calls the adapter under a deadline, validates the answer in core,
|
|
12
|
+
// maps line refs back to line ids and stores the result. Every outcome is written; nothing here can fail a report, a queue read or a
|
|
13
|
+
// decision, and nothing here can act on a person.
|
|
14
|
+
|
|
15
|
+
/** A call is allowed this long beyond its deadline before another process may take the read over. */
|
|
16
|
+
export const LEASE_SLACK_MS = 15_000;
|
|
17
|
+
/** How many calls a read gets in all (the first and two retries). */
|
|
18
|
+
export const MAX_ATTEMPTS = 3;
|
|
19
|
+
/** The wait after the first failed attempt, then after the second. */
|
|
20
|
+
export const RETRY_AFTER_MS: readonly number[] = [60_000, 600_000];
|
|
21
|
+
/** A job that lost a race for the breaker's probe or the call window tries again after this. */
|
|
22
|
+
const RACE_RETRY_MS = 60_000;
|
|
23
|
+
/** What one call is counted as costing while it is in flight, until a real cost is known: 0.1 cent. */
|
|
24
|
+
const DEFAULT_ESTIMATE_MICROS = 1000;
|
|
25
|
+
|
|
26
|
+
/** The store with the AI methods present (checked once, at creation). */
|
|
27
|
+
export type AiStore = Store &
|
|
28
|
+
Required<
|
|
29
|
+
Pick<
|
|
30
|
+
Store,
|
|
31
|
+
| 'reserveAiRead'
|
|
32
|
+
| 'completeAiRead'
|
|
33
|
+
| 'failAiRead'
|
|
34
|
+
| 'loadAiRead'
|
|
35
|
+
| 'loadAiReads'
|
|
36
|
+
| 'loadAiReadById'
|
|
37
|
+
| 'loadAiReadFor'
|
|
38
|
+
| 'listAiRetries'
|
|
39
|
+
| 'aiSpendSince'
|
|
40
|
+
| 'insertAiOutcome'
|
|
41
|
+
| 'loadAiOutcome'
|
|
42
|
+
| 'markAiOutcome'
|
|
43
|
+
| 'aiExamples'
|
|
44
|
+
| 'aiTeachableCount'
|
|
45
|
+
| 'aiStats'
|
|
46
|
+
>
|
|
47
|
+
>;
|
|
48
|
+
|
|
49
|
+
/** The counters of this process: calls in flight, the last cost, tokens for the cache hit rate, invalid answers by code. */
|
|
50
|
+
export class Meter {
|
|
51
|
+
inflight = 0;
|
|
52
|
+
lastCostMicros = 0;
|
|
53
|
+
inputTokens = 0;
|
|
54
|
+
cacheReadTokens = 0;
|
|
55
|
+
cacheWriteTokens = 0;
|
|
56
|
+
readonly invalid: Record<string, number> = Object.create(null) as Record<string, number>;
|
|
57
|
+
|
|
58
|
+
estimate(): number {
|
|
59
|
+
return Math.max(DEFAULT_ESTIMATE_MICROS, this.lastCostMicros);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
usage(u: AiCallUsage): void {
|
|
63
|
+
this.lastCostMicros = u.costMicros;
|
|
64
|
+
this.inputTokens += u.inputTokens;
|
|
65
|
+
this.cacheReadTokens += u.cacheReadTokens;
|
|
66
|
+
this.cacheWriteTokens += u.cacheWriteTokens;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
invalidCode(code: string): void {
|
|
70
|
+
this.invalid[code] = (this.invalid[code] ?? 0) + 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The share of input tokens served from the provider's cache, or null before any call used tokens. */
|
|
74
|
+
cacheHitRate(): number | null {
|
|
75
|
+
const total = this.inputTokens + this.cacheReadTokens + this.cacheWriteTokens;
|
|
76
|
+
return total === 0 ? null : this.cacheReadTokens / total;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** What the worker needs from the service around it. */
|
|
81
|
+
export interface WorkerEnv {
|
|
82
|
+
readonly store: AiStore;
|
|
83
|
+
readonly clock: Clock;
|
|
84
|
+
readonly adapter: ModerationAI;
|
|
85
|
+
readonly timeoutMs: number;
|
|
86
|
+
readonly capMicros: number;
|
|
87
|
+
readonly breaker: Breaker;
|
|
88
|
+
readonly window: CallWindow;
|
|
89
|
+
readonly meter: Meter;
|
|
90
|
+
level(): number;
|
|
91
|
+
/** The owner's most calls in a UTC day (`profile.ai.dailyCalls`), read live. */
|
|
92
|
+
dailyCalls(): number;
|
|
93
|
+
closed(): boolean;
|
|
94
|
+
prepare(report: ReportRecord): Prepared;
|
|
95
|
+
/** Casebook examples for this case, newest first; empty when there are none, learning is off or something fails. */
|
|
96
|
+
examplesFor(report: ReportRecord, prepared: Prepared): AIExampleInput[];
|
|
97
|
+
/** The signal counts about the reported person (`feeds.ai`): a snapshot for this read; empty when the feed is off or nothing qualifies. Fenced by the caller. */
|
|
98
|
+
signalsFor(report: ReportRecord): AISignal[];
|
|
99
|
+
track(controller: Abortable): void;
|
|
100
|
+
untrack(controller: Abortable): void;
|
|
101
|
+
/** The `ai.read` audit row for a terminal outcome. Fenced by the caller. */
|
|
102
|
+
auditRead(reportId: string): void;
|
|
103
|
+
warn(event: string, detail?: Record<string, string | number | boolean>): void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const errName = (err: unknown): string => {
|
|
107
|
+
try {
|
|
108
|
+
return err instanceof Error ? err.name : typeof err;
|
|
109
|
+
} catch {
|
|
110
|
+
return 'unknown';
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
type CallKind = 'retryable' | 'fatal' | 'declined' | 'timeout';
|
|
115
|
+
const CALL_KINDS: readonly string[] = ['retryable', 'fatal', 'declined', 'timeout'];
|
|
116
|
+
|
|
117
|
+
/** The kind of an error an adapter threw. A copy of core's AIError is recognised by shape; anything else is retryable. */
|
|
118
|
+
function kindOf(err: unknown, ours: boolean): CallKind {
|
|
119
|
+
try {
|
|
120
|
+
const kind = (err as { name?: unknown; kind?: unknown } | null)?.kind;
|
|
121
|
+
const name = (err as { name?: unknown } | null)?.name;
|
|
122
|
+
if (name === 'AIError' && typeof kind === 'string' && CALL_KINDS.includes(kind)) return kind as CallKind;
|
|
123
|
+
} catch {
|
|
124
|
+
// A hostile error object: fall through.
|
|
125
|
+
}
|
|
126
|
+
return ours ? 'timeout' : 'retryable';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** What the worker needs of an `AbortController` to give a call up. */
|
|
130
|
+
export interface Abortable {
|
|
131
|
+
abort(): void;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `promise`, or a timeout error as soon as the signal aborts, whichever comes first. The loser is never left unhandled. */
|
|
135
|
+
function raceAbort<T>(promise: Promise<T>, signal: AbortSignalLike): Promise<T> {
|
|
136
|
+
return new Promise<T>((resolve, reject) => {
|
|
137
|
+
const onAbort = (): void => reject(Object.assign(new Error('aborted'), { name: 'AIError', kind: 'timeout' }));
|
|
138
|
+
if (signal.aborted) onAbort();
|
|
139
|
+
else signal.addEventListener('abort', onAbort, { once: true });
|
|
140
|
+
promise.then(
|
|
141
|
+
(v) => {
|
|
142
|
+
signal.removeEventListener('abort', onAbort);
|
|
143
|
+
resolve(v);
|
|
144
|
+
},
|
|
145
|
+
(e: unknown) => {
|
|
146
|
+
signal.removeEventListener('abort', onAbort);
|
|
147
|
+
reject(e);
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const count = (v: unknown): number => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? Math.floor(v) : 0);
|
|
154
|
+
|
|
155
|
+
/** The usage an adapter reported, made safe to store: a model name of at most 100 characters, whole non-negative numbers. */
|
|
156
|
+
function safeUsage(u: Partial<AIUsage> | undefined, latencyMs: number): AiCallUsage {
|
|
157
|
+
return {
|
|
158
|
+
model: typeof u?.model === 'string' && u.model !== '' ? u.model.slice(0, 100) : 'unknown',
|
|
159
|
+
inputTokens: count(u?.inputTokens),
|
|
160
|
+
outputTokens: count(u?.outputTokens),
|
|
161
|
+
cacheReadTokens: count(u?.cacheReadTokens),
|
|
162
|
+
cacheWriteTokens: count(u?.cacheWriteTokens),
|
|
163
|
+
costMicros: typeof u?.costMicros === 'number' && Number.isFinite(u.costMicros) && u.costMicros > 0 ? Math.round(u.costMicros) : 0,
|
|
164
|
+
latencyMs: Math.max(0, Math.floor(latencyMs)),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Until when the sweeper should leave a report alone, given the row a reserve found: null (nothing to wait for) for a finished read. */
|
|
169
|
+
export function parkOf(row: AiReadRow, now: number): number | null {
|
|
170
|
+
switch (row.status) {
|
|
171
|
+
case 'ok':
|
|
172
|
+
return null;
|
|
173
|
+
case 'pending':
|
|
174
|
+
return row.leaseUntil ?? now + RACE_RETRY_MS;
|
|
175
|
+
case 'failed':
|
|
176
|
+
case 'skipped':
|
|
177
|
+
return row.retryAt ?? Number.POSITIVE_INFINITY;
|
|
178
|
+
default:
|
|
179
|
+
// declined, invalid: final for this input.
|
|
180
|
+
return Number.POSITIVE_INFINITY;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A row that ended without an answer and will not be tried again by itself (declined, invalid twice, fatal, given up): the only kind of
|
|
186
|
+
* row a moderator's explicit "read again" may retake, once (the row's `retaken` marker). A row waiting for its own retry is not final, and
|
|
187
|
+
* neither is a skipped one: it made no call and gave its attempt back, so an explicit ask simply takes it (no marker).
|
|
188
|
+
*/
|
|
189
|
+
export function isFinalRow(row: AiReadRow): boolean {
|
|
190
|
+
return row.status === 'declined' || row.status === 'invalid' || (row.status === 'failed' && row.retryAt === null);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** How a job ended. `park` is the time (epoch ms, possibly Infinity) before which the sweeper should not queue the report again, or null when there is nothing to wait for. */
|
|
194
|
+
export interface ReadEnd {
|
|
195
|
+
park: number | null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const NOTHING: ReadEnd = { park: null };
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Read one report. `opts.retake` is a moderator's explicit ask: a FINAL row (see {@link isFinalRow}) that has not been retaken is taken over once
|
|
202
|
+
* more (and the call is charged and audited like any other). Throws only on a store failure; the caller fences it.
|
|
203
|
+
*/
|
|
204
|
+
export async function runRead(env: WorkerEnv, reportId: string, opts?: { retake?: boolean }): Promise<ReadEnd> {
|
|
205
|
+
if (env.level() === 0 || env.closed()) return NOTHING;
|
|
206
|
+
const report = env.store.loadReport(reportId);
|
|
207
|
+
if (report === null || report.status !== 'open') return NOTHING;
|
|
208
|
+
|
|
209
|
+
// Cheap refusals first, before anything is written: an unavailable breaker (open, or half-open with its probe out) or a full
|
|
210
|
+
// minute leaves no row behind.
|
|
211
|
+
const now = env.clock.now();
|
|
212
|
+
if (!env.breaker.available() || !env.window.peek(now)) return NOTHING;
|
|
213
|
+
|
|
214
|
+
const prepared = env.prepare(report);
|
|
215
|
+
const reserved = env.store.reserveAiRead(
|
|
216
|
+
{ reportId, inputHash: prepared.hash, promptVersion: prepared.input.promptVersion, adapter: env.adapter.id },
|
|
217
|
+
now + env.timeoutMs + LEASE_SLACK_MS,
|
|
218
|
+
now,
|
|
219
|
+
opts?.retake === true ? { retake: true } : undefined,
|
|
220
|
+
);
|
|
221
|
+
if (!reserved.reserved) {
|
|
222
|
+
return { park: parkOf(reserved.existing, now) };
|
|
223
|
+
}
|
|
224
|
+
const { id, lease } = reserved;
|
|
225
|
+
|
|
226
|
+
// The daily caps. Money: what is stored for today, plus what the calls in flight here are estimated to cost. Calls: the owner's
|
|
227
|
+
// ceiling, which holds even for an adapter that reports no cost (the count includes this reservation).
|
|
228
|
+
const today = env.store.aiSpendSince(startOfUtcDay(now));
|
|
229
|
+
if (today.micros + env.meter.inflight * env.meter.estimate() >= env.capMicros || today.calls > env.dailyCalls()) {
|
|
230
|
+
const resume = nextUtcMidnight(now);
|
|
231
|
+
env.store.failAiRead(id, lease, 'skipped', 'budget', resume);
|
|
232
|
+
return { park: resume };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const gate = env.breaker.gate();
|
|
236
|
+
if (gate === 'no' || !env.window.tryTake(now)) {
|
|
237
|
+
if (gate === 'probe') env.breaker.release();
|
|
238
|
+
const again = now + RACE_RETRY_MS;
|
|
239
|
+
env.store.failAiRead(id, lease, 'skipped', 'retryable', again);
|
|
240
|
+
return { park: again };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
let examples: AIExampleInput[] = [];
|
|
244
|
+
try {
|
|
245
|
+
examples = env.examplesFor(report, prepared);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
env.warn('ai.examples_failed', { error: errName(err) });
|
|
248
|
+
}
|
|
249
|
+
let signals: AISignal[] = [];
|
|
250
|
+
try {
|
|
251
|
+
signals = env.signalsFor(report);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
env.warn('ai.signals_failed', { error: errName(err) });
|
|
254
|
+
}
|
|
255
|
+
let input: AIReadInput = prepared.input;
|
|
256
|
+
let exampleIds: string[] = [];
|
|
257
|
+
try {
|
|
258
|
+
if (examples.length > 0 || signals.length > 0) {
|
|
259
|
+
input = prepared.build(examples, signals);
|
|
260
|
+
exampleIds = usedExampleIds(examples);
|
|
261
|
+
}
|
|
262
|
+
} catch (err) {
|
|
263
|
+
env.warn('ai.examples_failed', { error: errName(err) });
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// The call, under a hard deadline.
|
|
267
|
+
const controller = new AbortController();
|
|
268
|
+
env.track(controller);
|
|
269
|
+
let timedOut = false;
|
|
270
|
+
const timer = setTimeout(() => {
|
|
271
|
+
timedOut = true;
|
|
272
|
+
controller.abort();
|
|
273
|
+
}, env.timeoutMs);
|
|
274
|
+
(timer as { unref?: () => void }).unref?.();
|
|
275
|
+
env.meter.inflight += 1;
|
|
276
|
+
const startedAt = env.clock.now();
|
|
277
|
+
let result: AIReadResult | undefined;
|
|
278
|
+
let thrown: unknown;
|
|
279
|
+
let failed = false;
|
|
280
|
+
try {
|
|
281
|
+
result = await raceAbort(Promise.resolve(env.adapter.read(input, { signal: controller.signal })), controller.signal);
|
|
282
|
+
} catch (err) {
|
|
283
|
+
failed = true;
|
|
284
|
+
thrown = err;
|
|
285
|
+
} finally {
|
|
286
|
+
clearTimeout(timer);
|
|
287
|
+
env.untrack(controller);
|
|
288
|
+
env.meter.inflight -= 1;
|
|
289
|
+
}
|
|
290
|
+
// The service is closing: the store may already be going away. The lease runs out and the read is taken up later.
|
|
291
|
+
if (env.closed()) return NOTHING;
|
|
292
|
+
const latency = env.clock.now() - startedAt;
|
|
293
|
+
|
|
294
|
+
if (failed) return settleFailure(env, { id, lease, reportId, hash: prepared.hash, kind: kindOf(thrown, timedOut) });
|
|
295
|
+
|
|
296
|
+
const usage = safeUsage((result as { usage?: Partial<AIUsage> } | null)?.usage, latency);
|
|
297
|
+
env.meter.usage(usage);
|
|
298
|
+
|
|
299
|
+
let verdict: ReturnType<typeof validateAIOutput>;
|
|
300
|
+
try {
|
|
301
|
+
verdict = validateAIOutput((result as { output?: unknown } | null)?.output, {
|
|
302
|
+
sent: prepared.refs,
|
|
303
|
+
activeRules: prepared.activeRules,
|
|
304
|
+
ownRefs: prepared.ownRefs,
|
|
305
|
+
});
|
|
306
|
+
} catch {
|
|
307
|
+
verdict = { ok: false, code: 'unreadable' };
|
|
308
|
+
}
|
|
309
|
+
if (!verdict.ok) {
|
|
310
|
+
// Paid for, and counted in the spend, although there is no read. A model's malformed or truncated answer is not deterministic,
|
|
311
|
+
// so the first one is tried again once (a minute later); a second is final.
|
|
312
|
+
env.meter.invalidCode(verdict.code);
|
|
313
|
+
env.breaker.success();
|
|
314
|
+
if (lease < 2) {
|
|
315
|
+
const retryAt = env.clock.now() + (RETRY_AFTER_MS[0] ?? RACE_RETRY_MS);
|
|
316
|
+
env.store.failAiRead(id, lease, 'failed', 'invalid', retryAt, usage);
|
|
317
|
+
return { park: retryAt };
|
|
318
|
+
}
|
|
319
|
+
if (env.store.failAiRead(id, lease, 'invalid', 'invalid', null, usage)) env.auditRead(reportId);
|
|
320
|
+
return { park: Number.POSITIVE_INFINITY };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const toIds = (refs: readonly string[]): string[] => refs.flatMap((ref) => {
|
|
324
|
+
const lineId = prepared.idOf.get(ref);
|
|
325
|
+
return lineId === undefined ? [] : [lineId];
|
|
326
|
+
});
|
|
327
|
+
const injected = verdict.read.injectionSuspected || prepared.injection;
|
|
328
|
+
const rec = verdict.read.recommendation;
|
|
329
|
+
const read: AIRead = {
|
|
330
|
+
...verdict.read,
|
|
331
|
+
evidence: toIds(verdict.read.evidence),
|
|
332
|
+
// A line that tried to instruct the AI makes the whole read low confidence, whatever the model said.
|
|
333
|
+
confidence: injected ? 'low' : verdict.read.confidence,
|
|
334
|
+
injectionSuspected: injected,
|
|
335
|
+
recommendation: rec === null ? null : { ...rec, evidence: toIds(rec.evidence) },
|
|
336
|
+
};
|
|
337
|
+
env.breaker.success();
|
|
338
|
+
const stored = env.store.completeAiRead(id, lease, { ...usage, read, exampleIds, signals: input.signals ?? [] });
|
|
339
|
+
if (!stored) {
|
|
340
|
+
env.warn('ai.read_superseded');
|
|
341
|
+
return NOTHING;
|
|
342
|
+
}
|
|
343
|
+
env.auditRead(reportId);
|
|
344
|
+
return NOTHING;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** An adapter failure: retry on the schedule, or give up (and say so once). */
|
|
348
|
+
function settleFailure(env: WorkerEnv, f: { id: string; lease: number; reportId: string; hash: string; kind: CallKind }): ReadEnd {
|
|
349
|
+
const { id, lease, reportId, kind } = f;
|
|
350
|
+
const final: ReadEnd = { park: Number.POSITIVE_INFINITY };
|
|
351
|
+
if (kind === 'declined') {
|
|
352
|
+
// A refusal is an answer: stored, not retried, and the provider is up.
|
|
353
|
+
env.breaker.success();
|
|
354
|
+
if (env.store.failAiRead(id, lease, 'declined', 'declined', null)) env.auditRead(reportId);
|
|
355
|
+
return final;
|
|
356
|
+
}
|
|
357
|
+
const errorKind: AiErrorKind = kind;
|
|
358
|
+
if (kind === 'fatal') {
|
|
359
|
+
env.breaker.failure(true);
|
|
360
|
+
if (env.store.failAiRead(id, lease, 'failed', errorKind, null)) env.auditRead(reportId);
|
|
361
|
+
return final;
|
|
362
|
+
}
|
|
363
|
+
env.breaker.failure();
|
|
364
|
+
if (lease >= MAX_ATTEMPTS) {
|
|
365
|
+
// Three calls in all: given up.
|
|
366
|
+
if (env.store.failAiRead(id, lease, 'failed', errorKind, null)) env.auditRead(reportId);
|
|
367
|
+
return final;
|
|
368
|
+
}
|
|
369
|
+
const retryAt = env.clock.now() + (RETRY_AFTER_MS[lease - 1] ?? RETRY_AFTER_MS[RETRY_AFTER_MS.length - 1] ?? RACE_RETRY_MS);
|
|
370
|
+
env.store.failAiRead(id, lease, 'failed', errorKind, retryAt);
|
|
371
|
+
return { park: retryAt };
|
|
372
|
+
}
|
package/src/audit.ts
CHANGED
|
@@ -11,7 +11,11 @@ export type Role = (typeof ROLES)[number];
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* The closed list of things the audit trail records. The `term.*` and `exception.*` names also cover the rows the
|
|
14
|
-
* term editor (`LiveTerms`) wrote before the trail existed.
|
|
14
|
+
* term editor (`LiveTerms`) wrote before the trail existed. The list only ever grows at the end.
|
|
15
|
+
*
|
|
16
|
+
* The three `ai.*` actions target a report and carry no detail: `ai.read` is written by the AI itself (an `ai:` actor, no role)
|
|
17
|
+
* when a read finishes, `ai.rerun` by a moderator who asked for another read, `ai.outcome` by a moderator whose decision was
|
|
18
|
+
* recorded against a read.
|
|
15
19
|
*/
|
|
16
20
|
export const AUDIT_ACTIONS = [
|
|
17
21
|
'report.view',
|
|
@@ -37,6 +41,9 @@ export const AUDIT_ACTIONS = [
|
|
|
37
41
|
'policy.reset',
|
|
38
42
|
'report.dismiss_case',
|
|
39
43
|
'report.dismiss_reporter',
|
|
44
|
+
'ai.read',
|
|
45
|
+
'ai.rerun',
|
|
46
|
+
'ai.outcome',
|
|
40
47
|
] as const;
|
|
41
48
|
export type AuditAction = (typeof AUDIT_ACTIONS)[number];
|
|
42
49
|
|
|
@@ -53,6 +60,12 @@ export interface AuditDetail {
|
|
|
53
60
|
/** The longest reason a policy audit row keeps. */
|
|
54
61
|
export const AUDIT_REASON_MAX = 2000;
|
|
55
62
|
|
|
63
|
+
/** The actions that point at a report (and only at one), and carry nothing else. */
|
|
64
|
+
const AI_ACTIONS: readonly string[] = ['ai.read', 'ai.rerun', 'ai.outcome'];
|
|
65
|
+
|
|
66
|
+
/** An actor that is the AI itself, not a person: `ai:` and the adapter's id (`ai:anthropic:claude-haiku-5-5`). */
|
|
67
|
+
const AI_ACTOR_PREFIX = 'ai:';
|
|
68
|
+
|
|
56
69
|
/** The actions whose row carries an {@link AuditDetail}, and the only ones that may. */
|
|
57
70
|
const DETAIL_ACTIONS: readonly string[] = ['policy.update', 'policy.reset'];
|
|
58
71
|
|
|
@@ -77,7 +90,8 @@ export interface AuditRecordInput {
|
|
|
77
90
|
* them as they are.)
|
|
78
91
|
*/
|
|
79
92
|
actor: string;
|
|
80
|
-
role
|
|
93
|
+
/** The staff member's role. `null` only for the AI's own actor (`ai:<adapter id>`), which records `ai.read` and nothing else. */
|
|
94
|
+
role: Role | null;
|
|
81
95
|
action: AuditAction;
|
|
82
96
|
target?: AuditTarget;
|
|
83
97
|
/** Required for `policy.update` and `policy.reset` (and refused for every other action). */
|
|
@@ -204,7 +218,14 @@ function parse(input: unknown): AuditRecordInput | null {
|
|
|
204
218
|
if (typeof input !== 'object' || input === null) return null;
|
|
205
219
|
const { actor, role, action, target, detail } = input as Record<string, unknown>;
|
|
206
220
|
if (typeof actor !== 'string' || !ACTOR_RE.test(actor)) return null;
|
|
207
|
-
if (!
|
|
221
|
+
if (!isAction(action)) return null;
|
|
222
|
+
// The AI is the one actor with no role: `ai:<adapter id>` records `ai.read`, and only it does. A person always has a role.
|
|
223
|
+
if (actor.startsWith(AI_ACTOR_PREFIX)) {
|
|
224
|
+
if (actor.length === AI_ACTOR_PREFIX.length || role !== null || action !== 'ai.read') return null;
|
|
225
|
+
} else if (!isRole(role) || action === 'ai.read') {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const roleOut: Role | null = actor.startsWith(AI_ACTOR_PREFIX) ? null : (role as Role);
|
|
208
229
|
// A policy row must say what it changed; no other row may carry anything but ids.
|
|
209
230
|
let parsedDetail: AuditDetail | undefined;
|
|
210
231
|
if (DETAIL_ACTIONS.includes(action)) {
|
|
@@ -215,11 +236,12 @@ function parse(input: unknown): AuditRecordInput | null {
|
|
|
215
236
|
return null;
|
|
216
237
|
}
|
|
217
238
|
const extra = parsedDetail === undefined ? {} : { detail: parsedDetail };
|
|
218
|
-
if (target === undefined) return { actor, role, action, ...extra };
|
|
239
|
+
if (target === undefined) return AI_ACTIONS.includes(action) ? null : { actor, role: roleOut, action, ...extra };
|
|
219
240
|
if (typeof target !== 'object' || target === null) return null;
|
|
220
241
|
const { type, ref } = target as Record<string, unknown>;
|
|
221
242
|
if (!isTargetType(type) || typeof ref !== 'string' || !REF_RE.test(ref)) return null;
|
|
222
|
-
|
|
243
|
+
if (AI_ACTIONS.includes(action) && type !== 'report') return null;
|
|
244
|
+
return { actor, role: roleOut, action, target: { type, ref }, ...extra };
|
|
223
245
|
}
|
|
224
246
|
|
|
225
247
|
/**
|
|
@@ -241,6 +263,10 @@ export class AuditService {
|
|
|
241
263
|
/**
|
|
242
264
|
* Write one entry, now. Synchronous and fenced: it never throws, and returns false (nothing written, one log line
|
|
243
265
|
* carrying the event name and at most the error class) for an input outside the closed lists or a store failure.
|
|
266
|
+
*
|
|
267
|
+
* A person always has a `role`. The AI is the one actor without: pass `role: null` explicitly (`undefined` is refused) with an actor
|
|
268
|
+
* that begins `ai:` (and is longer than that), for `ai.read` only. So a staff ref or login that begins `ai:` is refused (false), and
|
|
269
|
+
* so is `ai.read` from anyone else. The three `ai.*` actions need a `report` target and take no detail.
|
|
244
270
|
*/
|
|
245
271
|
record(input: AuditRecordInput): boolean {
|
|
246
272
|
try {
|
package/src/chat.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import type { EvidenceBuffer, RoomRoster } from './evidence.js';
|
|
2
|
-
import { safeWarn, ThrottledWarn } from './log.js';
|
|
2
|
+
import { errorClass, safeWarn, ThrottledWarn } from './log.js';
|
|
3
3
|
import type { MuteService } from './mutes.js';
|
|
4
4
|
import type { NoticeService } from './notices.js';
|
|
5
|
+
import type { PaceReason, ResolvedPace } from './pace.js';
|
|
6
|
+
import { createPacer } from './pacing.js';
|
|
7
|
+
import type { PaceKey, Pacer } from './pacing.js';
|
|
5
8
|
import type { Limits } from './limits.js';
|
|
6
9
|
import { slidingWindow } from './rateLimit.js';
|
|
7
10
|
import type { SlidingWindow } from './rateLimit.js';
|
|
@@ -28,11 +31,14 @@ import type {
|
|
|
28
31
|
// 2. the seat's occupant, then the gate hook
|
|
29
32
|
// 3. active restriction -> 'restricted' (chat, ban, or the in-memory automatic one)
|
|
30
33
|
// 4. length > 200 -> 'length'
|
|
31
|
-
// 5.
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
+
// 5. pacing, when configured -> 'slow_mode' | 'flood' | 'duplicate' (read only: a refusal writes nothing and
|
|
35
|
+
// spends nothing; it carries `retryAfterMs`)
|
|
36
|
+
// 6. rate, 3 per 5 s per room:seat -> 'rate' (spent even when the line is then blocked)
|
|
37
|
+
// 7. pacing commit -- only now does the line count toward the next pacing check
|
|
38
|
+
// 8. matcher hit -> buffered as blocked, one strike, maybe an automatic timeout, 'blocked'
|
|
39
|
+
// 9. profanity, by the profile's mode -> allow: untouched; star: masked; refuse: 'profanity' (never a strike,
|
|
34
40
|
// never buffered, never a notice: swearing is not a rule violation)
|
|
35
|
-
//
|
|
41
|
+
// 10. buffer the delivered line -> its lineId
|
|
36
42
|
// It never throws. Everything on this path reads in-memory state; the only store writes are the strike
|
|
37
43
|
// (inside StrikeService) and the automatic timeout, and a failure of either is logged and absorbed.
|
|
38
44
|
|
|
@@ -64,8 +70,12 @@ interface BlockedLine {
|
|
|
64
70
|
* automatic-filter rule (`autoRestrictRule`, CR 1 by default) for `blocked`, and for `restricted` the sanction's
|
|
65
71
|
* own rule (omitted when it is `'uncovered'` or unknown; the in-memory fallback cites the automatic-filter rule). `noticeId` is the player's notice that explains a restriction, when one
|
|
66
72
|
* exists. Never an identifier, a rule's private note or the matched term. `'empty'` means stay silent.
|
|
73
|
+
*
|
|
74
|
+
* `'slow_mode'`, `'flood'` and `'duplicate'` are pacing refusals and only ever appear once pacing is configured (`profile.pace`, a
|
|
75
|
+
* stored layer or `cw.rooms.setPace`). They carry `retryAfterMs` and nothing else: a pause, not a sanction, so no rule, notice or end
|
|
76
|
+
* time, and never the line.
|
|
67
77
|
*/
|
|
68
|
-
export type RefuseReason = 'empty' | 'length' | 'rate' | 'gate' | 'restricted' | 'blocked' | 'profanity';
|
|
78
|
+
export type RefuseReason = 'empty' | 'length' | 'rate' | 'gate' | 'restricted' | 'blocked' | 'profanity' | PaceReason;
|
|
69
79
|
|
|
70
80
|
export type ChatResult =
|
|
71
81
|
| { deliver: { lineId: string; text: string } }
|
|
@@ -76,6 +86,8 @@ export type ChatResult =
|
|
|
76
86
|
byModerator?: boolean;
|
|
77
87
|
rule?: number;
|
|
78
88
|
noticeId?: string;
|
|
89
|
+
/** Present for `slow_mode`, `flood` and `duplicate`: when the same player may try again, in ms from now (a whole number, at least 1). @experimental */
|
|
90
|
+
retryAfterMs?: number;
|
|
79
91
|
};
|
|
80
92
|
};
|
|
81
93
|
|
|
@@ -136,13 +148,32 @@ export interface ChatServiceDeps {
|
|
|
136
148
|
* `roomId:seatId`. The service trusts the numbers; the facade validates them.
|
|
137
149
|
*/
|
|
138
150
|
limits?: { maxLength: Limits['chatMaxLength']; rate: Limits['chatRate'] };
|
|
151
|
+
/**
|
|
152
|
+
* Pacing: slow mode, a burst limit and repeat refusal. `settings` is read on every line (so a saved change applies at once) and
|
|
153
|
+
* answers every check off for a room that is not paced: such a line builds no key and makes no `canonical` call, and keeps no state.
|
|
154
|
+
* `canonical` folds a merged subject into the person the state is kept under. `onFlood` is told once per burst window when a known person trips the burst
|
|
155
|
+
* limit; a throw from it is logged by class and absorbed. Without `pace` nothing is paced.
|
|
156
|
+
* @experimental
|
|
157
|
+
*/
|
|
158
|
+
pace?: {
|
|
159
|
+
settings: (roomId: string) => ResolvedPace;
|
|
160
|
+
canonical: (subject: SubjectId) => SubjectId;
|
|
161
|
+
onFlood?: (info: { roomId: string; subject: SubjectId }) => void;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** What pacing says about a line: a refusal, or the key to commit under; neither when nothing is paced. */
|
|
166
|
+
interface Paced {
|
|
167
|
+
refusal: ChatResult | null;
|
|
168
|
+
key: PaceKey | null;
|
|
139
169
|
}
|
|
170
|
+
const NOT_PACED: Paced = Object.freeze({ refusal: null, key: null });
|
|
140
171
|
|
|
141
172
|
const errName = (err: unknown): string => (err instanceof Error ? err.name : typeof err);
|
|
142
173
|
|
|
143
174
|
const refuse = (
|
|
144
175
|
reason: RefuseReason,
|
|
145
|
-
extra?: { until?: number | null; byModerator?: boolean; rule?: number; noticeId?: string },
|
|
176
|
+
extra?: { until?: number | null; byModerator?: boolean; rule?: number; noticeId?: string; retryAfterMs?: number },
|
|
146
177
|
): ChatResult => ({ refuse: extra === undefined ? { reason } : { reason, ...extra } });
|
|
147
178
|
|
|
148
179
|
interface Restriction {
|
|
@@ -203,6 +234,10 @@ export class ChatService {
|
|
|
203
234
|
private readonly rate: SlidingWindow;
|
|
204
235
|
/** The typing lookup runs at keystroke rate: its failure line is limited to one a minute. */
|
|
205
236
|
private readonly typingWarn: ThrottledWarn;
|
|
237
|
+
/** Pacing runs on every line: a failure of it is limited to one log line a minute. */
|
|
238
|
+
private readonly paceWarn: ThrottledWarn;
|
|
239
|
+
private readonly pacer: Pacer | undefined;
|
|
240
|
+
private readonly pace: ChatServiceDeps['pace'];
|
|
206
241
|
|
|
207
242
|
/**
|
|
208
243
|
* Automatic restrictions that could not be stored (no subject to put a row on, or the write failed), by
|
|
@@ -229,6 +264,9 @@ export class ChatService {
|
|
|
229
264
|
this.onAutoRestricted = deps.onAutoRestricted;
|
|
230
265
|
this.logger = deps.logger;
|
|
231
266
|
this.typingWarn = new ThrottledWarn(deps.logger, deps.clock);
|
|
267
|
+
this.paceWarn = new ThrottledWarn(deps.logger, deps.clock);
|
|
268
|
+
this.pace = deps.pace;
|
|
269
|
+
this.pacer = deps.pace === undefined ? undefined : createPacer({ clock: deps.clock, settings: deps.pace.settings });
|
|
232
270
|
this.notices = deps.notices;
|
|
233
271
|
this.autoRestrictRule = deps.autoRestrictRule ?? DEFAULT_AUTO_RESTRICT_RULE;
|
|
234
272
|
this.maxLength = deps.limits?.maxLength ?? MAX_LEN;
|
|
@@ -309,6 +347,11 @@ export class ChatService {
|
|
|
309
347
|
}
|
|
310
348
|
}
|
|
311
349
|
|
|
350
|
+
/** Forget a room's pacing state (the room is closing). */
|
|
351
|
+
dropPaceRoom(roomId: string): void {
|
|
352
|
+
this.pacer?.dropRoom(roomId);
|
|
353
|
+
}
|
|
354
|
+
|
|
312
355
|
// --- internals -----------------------------------------------------------
|
|
313
356
|
|
|
314
357
|
private run(roomId: string, seatId: string, text: string, opts?: { audience?: string[] }): ChatResult {
|
|
@@ -335,9 +378,20 @@ export class ChatService {
|
|
|
335
378
|
}
|
|
336
379
|
|
|
337
380
|
if (trimmed.length > this.maxLength) return refuse('length');
|
|
381
|
+
|
|
382
|
+
// Pacing asks before `rate` so the specific, retryable message wins over the blunt one; it only reads, so a refusal spends nothing.
|
|
383
|
+
// With every check off it builds nothing (no key, no canonical lookup) and there is nothing to commit.
|
|
384
|
+
const paced = this.pacer === undefined ? NOT_PACED : this.paceCheck(this.pacer, roomId, seatId, subject, trimmed);
|
|
385
|
+
if (paced.refusal !== null) return paced.refusal;
|
|
338
386
|
// Spent even when the line is then blocked, so a flood of slurs is throttled like any flood and cannot
|
|
339
387
|
// probe the term list faster than ordinary chat.
|
|
340
|
-
if (!this.rate.check(rateKey))
|
|
388
|
+
if (!this.rate.check(rateKey)) {
|
|
389
|
+
// The line passed pacing but never commits: the pacer must not keep its text.
|
|
390
|
+
this.paceDiscard(this.pacer);
|
|
391
|
+
return refuse('rate');
|
|
392
|
+
}
|
|
393
|
+
// Past `rate` the line has happened as far as pacing is concerned (a blocked or profane line has spent its slot too).
|
|
394
|
+
if (this.pacer !== undefined && paced.key !== null) this.paceCommit(this.pacer, paced.key, trimmed);
|
|
341
395
|
|
|
342
396
|
const audience = opts?.audience === undefined ? null : [...opts.audience];
|
|
343
397
|
|
|
@@ -389,6 +443,53 @@ export class ChatService {
|
|
|
389
443
|
return { deliver: { lineId, text: delivered } };
|
|
390
444
|
}
|
|
391
445
|
|
|
446
|
+
/**
|
|
447
|
+
* Ask pacing about a line: a refusal to return, or the key to commit under once `rate` passes. Everything that can throw is in the
|
|
448
|
+
* fence (the settings, the canonical lookup, the pacer), and it fails open: a broken pacer must not silence chat, so the line goes on
|
|
449
|
+
* with nothing to commit and the class is logged (at most once a minute).
|
|
450
|
+
*/
|
|
451
|
+
private paceCheck(pacer: Pacer, roomId: string, seatId: string, subject: SubjectId | null, text: string): Paced {
|
|
452
|
+
try {
|
|
453
|
+
if (!pacer.active(roomId)) return NOT_PACED;
|
|
454
|
+
const key: PaceKey = { roomId, who: subject === null ? `seat:${seatId}` : (this.pace?.canonical(subject) ?? subject) };
|
|
455
|
+
const verdict = pacer.check(key, text);
|
|
456
|
+
if (verdict.ok) return { refusal: null, key };
|
|
457
|
+
if (verdict.reason === 'flood' && verdict.signalFlood && subject !== null) this.tellFlood(roomId, key.who);
|
|
458
|
+
return { refusal: refuse(verdict.reason, { retryAfterMs: verdict.retryAfterMs }), key: null };
|
|
459
|
+
} catch (err) {
|
|
460
|
+
this.paceWarn.warn('chatwarden.pace_failed', { error: errorClass(err) });
|
|
461
|
+
return NOT_PACED;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
private paceDiscard(pacer: Pacer | undefined): void {
|
|
466
|
+
try {
|
|
467
|
+
pacer?.discard();
|
|
468
|
+
} catch (err) {
|
|
469
|
+
this.paceWarn.warn('chatwarden.pace_failed', { error: errorClass(err) });
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private paceCommit(pacer: Pacer, key: PaceKey, text: string): void {
|
|
474
|
+
try {
|
|
475
|
+
pacer.commit(key, text);
|
|
476
|
+
} catch (err) {
|
|
477
|
+
this.paceWarn.warn('chatwarden.pace_failed', { error: errorClass(err) });
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** Tell the facade a known person tripped the burst limit. Never changes the verdict: a throw or a rejection is logged by class and dropped. */
|
|
482
|
+
private tellFlood(roomId: string, subject: SubjectId): void {
|
|
483
|
+
const onFlood = this.pace?.onFlood;
|
|
484
|
+
if (onFlood === undefined) return;
|
|
485
|
+
try {
|
|
486
|
+
const out: unknown = onFlood({ roomId, subject });
|
|
487
|
+
if (isThenable(out)) out.then(undefined, (err: unknown) => this.paceWarn.warn('chatwarden.pace_failed', { error: errorClass(err) }));
|
|
488
|
+
} catch (err) {
|
|
489
|
+
this.paceWarn.warn('chatwarden.pace_failed', { error: errorClass(err) });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
392
493
|
/** The gate hook; a throw is a refusal (it is access control, so it fails closed). */
|
|
393
494
|
private gateAllows(roomId: string, subject: SubjectId | null): boolean {
|
|
394
495
|
if (this.gate === undefined) return true;
|