@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/team.ts
CHANGED
|
@@ -38,6 +38,11 @@ const NEXT_PAGE = 200;
|
|
|
38
38
|
|
|
39
39
|
/** With `rankDownHabitual`, candidates are ranked this many at a time (see `next`). */
|
|
40
40
|
const RANK_SLICE = 20;
|
|
41
|
+
/** The most "handed to this moderator" markers `next` keeps (the oldest go). */
|
|
42
|
+
const SERVED_MAX = 1000;
|
|
43
|
+
|
|
44
|
+
/** With `signalBoost`, take-next looks at most this many people (oldest first) for an overdue or boosted case. @experimental */
|
|
45
|
+
export const SIGNAL_BOOST_SCAN_MAX = 200;
|
|
41
46
|
|
|
42
47
|
/** A claim as staff see it. Staff only: `by` is a staff actor. */
|
|
43
48
|
export interface CaseClaim {
|
|
@@ -192,8 +197,16 @@ export interface TeamDeps {
|
|
|
192
197
|
reasons?: () => readonly ReportReason[];
|
|
193
198
|
/** How each person's reports have turned out (see `cw.reports.reporterAccuracy`). Without it, `rankDownHabitual` ranks nobody down. */
|
|
194
199
|
accuracy?: (people: readonly SubjectId[]) => Map<SubjectId, ReporterAccuracy>;
|
|
200
|
+
/**
|
|
201
|
+
* Which of these people meet the queue signal feed (`cw.signals.boosted`), as canonical ids. Only asked when `next` is given
|
|
202
|
+
* `signalBoost`, once per page of candidates. Without it nobody is boosted. A throw reads as nobody boosted.
|
|
203
|
+
*/
|
|
204
|
+
boosted?: (people: readonly SubjectId[]) => Set<SubjectId>;
|
|
195
205
|
}
|
|
196
206
|
|
|
207
|
+
/** The key of a lapsed-claim marker: the moderator and the person, apart by a character neither can hold. */
|
|
208
|
+
const markerKey = (actor: string, person: SubjectId): string => `${actor}\u0000${person}`;
|
|
209
|
+
|
|
197
210
|
/** Claims, take next and viewing. The facade exposes it as `cw.cases`. */
|
|
198
211
|
export class CaseDesk {
|
|
199
212
|
private readonly store: Store;
|
|
@@ -201,7 +214,10 @@ export class CaseDesk {
|
|
|
201
214
|
private readonly clock: Clock;
|
|
202
215
|
private readonly reasons: () => readonly ReportReason[];
|
|
203
216
|
private readonly accuracy: NonNullable<TeamDeps['accuracy']>;
|
|
217
|
+
private readonly boosted: TeamDeps['boosted'];
|
|
204
218
|
private readonly presence: ViewingPresence;
|
|
219
|
+
/** `moderator, person` -> when the claim `next` handed that moderator ran out (this process only). See `lapsedByMe`. */
|
|
220
|
+
private readonly served = new Map<string, number>();
|
|
205
221
|
|
|
206
222
|
constructor(deps: TeamDeps) {
|
|
207
223
|
this.store = deps.store;
|
|
@@ -209,6 +225,7 @@ export class CaseDesk {
|
|
|
209
225
|
this.clock = deps.clock;
|
|
210
226
|
this.reasons = deps.reasons ?? (() => []);
|
|
211
227
|
this.accuracy = deps.accuracy ?? (() => new Map());
|
|
228
|
+
this.boosted = deps.boosted;
|
|
212
229
|
this.presence = new ViewingPresence(deps.clock);
|
|
213
230
|
}
|
|
214
231
|
|
|
@@ -243,6 +260,8 @@ export class CaseDesk {
|
|
|
243
260
|
}
|
|
244
261
|
const now = this.clock.now();
|
|
245
262
|
const res = this.store.claimCase(canonical, actor, now, now + CLAIM_TTL_MS, opts.takeOver === true ? 'takeover' : 'claim');
|
|
263
|
+
// The holder keeping the claim alive (or taking it again) is not one who let it lapse.
|
|
264
|
+
if (res.won) this.served.delete(markerKey(actor, canonical));
|
|
246
265
|
return res.won
|
|
247
266
|
? { ok: true, claim: claimOf(res.claim), tookOverFrom: res.from }
|
|
248
267
|
: { ok: false, error: 'claimed', claim: claimOf(res.claim) };
|
|
@@ -254,6 +273,8 @@ export class CaseDesk {
|
|
|
254
273
|
release(person: SubjectId, actor: string): boolean {
|
|
255
274
|
needActor(actor);
|
|
256
275
|
const canonical = this.subjects.canonical(needPerson(person));
|
|
276
|
+
// Released on purpose: not a claim that ran out (`lapsedByMe`).
|
|
277
|
+
this.served.delete(markerKey(actor, canonical));
|
|
257
278
|
return this.store.transaction(() => this.store.releaseClaim(canonical, actor, this.clock.now()));
|
|
258
279
|
}
|
|
259
280
|
|
|
@@ -268,6 +289,8 @@ export class CaseDesk {
|
|
|
268
289
|
const canonical = this.subjects.canonical(needPerson(person));
|
|
269
290
|
return this.store.transaction(() => {
|
|
270
291
|
if (this.store.listOpenReportsByPerson(this.subjects.aliasesOf(canonical), 1).length > 0) return false;
|
|
292
|
+
// Decided and done: the claim ending is not "let it lapse".
|
|
293
|
+
this.served.delete(markerKey(actor, canonical));
|
|
271
294
|
return this.store.releaseClaim(canonical, actor, this.clock.now());
|
|
272
295
|
});
|
|
273
296
|
}
|
|
@@ -283,8 +306,20 @@ export class CaseDesk {
|
|
|
283
306
|
* reporter, or no accuracy data, keeps the case normal). The first normal case in oldest-first order is claimed; ranked-down
|
|
284
307
|
* ones are remembered in order and, when the scan ends (end of the queue, or {@link RANK_DOWN_SCAN_MAX} reports read), the
|
|
285
308
|
* oldest of them is claimed. Nothing is skipped for good: a queue holding only habitual cases still serves them, oldest first.
|
|
309
|
+
*
|
|
310
|
+
* With `signalBoost` (default false: behaviour unchanged), the scan stays oldest first but a case is claimed only when it is OVERDUE (its
|
|
311
|
+
* oldest open report has waited {@link REPORTER_RANK_LAPSE_MS}) or BOOSTED (the person meets the queue signal feed, `TeamDeps.boosted`);
|
|
312
|
+
* the first such case wins. When the queue ends, or {@link SIGNAL_BOOST_SCAN_MAX} people have been looked at, with neither, the oldest
|
|
313
|
+
* free case is claimed as it would be without the filter. A boosted case is never ranked down as a habitual reporter's. Because the
|
|
314
|
+
* queue is oldest first an overdue case is always met before anything newer, so boosted cases cannot starve an old one past 24 hours.
|
|
315
|
+
* Nothing leaves the queue.
|
|
316
|
+
*
|
|
317
|
+
* Also with `signalBoost`: a case `next` handed this moderator whose claim then ran out (they did not decide it, and did not hold it)
|
|
318
|
+
* is served to them after every other free case for one claim window ({@link CLAIM_TTL_MS}) from the moment it ran out, so a boosted or
|
|
319
|
+
* overdue case does not come straight back to the person who let it lapse. Other moderators are not affected, it is still served when it
|
|
320
|
+
* is all that is left, and it is kept in this process only (a restart forgets it).
|
|
286
321
|
*/
|
|
287
|
-
next(actor: string, filter: { category?: ReportCategory; reason?: string | null; rankDownHabitual?: boolean } = {}): NextCase | null {
|
|
322
|
+
next(actor: string, filter: { category?: ReportCategory; reason?: string | null; rankDownHabitual?: boolean; signalBoost?: boolean } = {}): NextCase | null {
|
|
288
323
|
needActor(actor);
|
|
289
324
|
const category = filter.category;
|
|
290
325
|
if (category !== undefined && !REPORT_CATEGORIES.includes(category)) throw new RangeError('cases: unknown category');
|
|
@@ -294,9 +329,19 @@ export class CaseDesk {
|
|
|
294
329
|
const rank = filter.rankDownHabitual;
|
|
295
330
|
if (rank !== undefined && typeof rank !== 'boolean') throw new RangeError('cases: rankDownHabitual must be a boolean');
|
|
296
331
|
const rankDown = rank === true;
|
|
332
|
+
const boost = filter.signalBoost;
|
|
333
|
+
if (boost !== undefined && typeof boost !== 'boolean') throw new RangeError('cases: signalBoost must be a boolean');
|
|
334
|
+
const boostMode = boost === true;
|
|
297
335
|
const seen = new Set<SubjectId>();
|
|
298
336
|
// Ranked-down people in the order the scan met them (oldest first).
|
|
299
337
|
const deferred: SubjectId[] = [];
|
|
338
|
+
// With signalBoost: free people who are neither overdue nor boosted, in scan order, claimed when the scan ends (before the ranked-down).
|
|
339
|
+
const ordinary: SubjectId[] = [];
|
|
340
|
+
// With signalBoost: free people whose claim this moderator let lapse in the last claim window, claimed last of all.
|
|
341
|
+
const lapsed: SubjectId[] = [];
|
|
342
|
+
// With signalBoost: when each person's oldest open report was filed (the first one the scan met), and how many people were looked at.
|
|
343
|
+
const oldestAt = new Map<SubjectId, number>();
|
|
344
|
+
let scanned = 0;
|
|
300
345
|
let read = 0;
|
|
301
346
|
let after: { createdAt: number; id: string } | undefined;
|
|
302
347
|
for (;;) {
|
|
@@ -315,33 +360,53 @@ export class CaseDesk {
|
|
|
315
360
|
if (seen.has(person)) continue;
|
|
316
361
|
seen.add(person);
|
|
317
362
|
candidates.push(person);
|
|
363
|
+
if (boostMode) oldestAt.set(person, report.createdAt);
|
|
364
|
+
}
|
|
365
|
+
if (boostMode) {
|
|
366
|
+
// At most SIGNAL_BOOST_SCAN_MAX people in all, held ones included: they are looked at too.
|
|
367
|
+
candidates.length = Math.max(0, Math.min(candidates.length, SIGNAL_BOOST_SCAN_MAX - scanned));
|
|
368
|
+
scanned += candidates.length;
|
|
318
369
|
}
|
|
319
370
|
const held = new Set(this.store.loadClaims(candidates, this.clock.now()).map((c) => c.person));
|
|
320
371
|
const free = candidates.filter((p) => !held.has(p));
|
|
372
|
+
// One grouped read for the page.
|
|
373
|
+
const boosted = boostMode ? this.boostedAmong(free) : new Set<SubjectId>();
|
|
374
|
+
const now = this.clock.now();
|
|
321
375
|
// Ranked lazily, a slice at a time: the first ordinary case ends the scan, so "next, next, next" does not read the open
|
|
322
376
|
// reports of a whole page of candidates to claim the first one.
|
|
323
377
|
for (let i = 0; i < free.length; i += RANK_SLICE) {
|
|
324
378
|
const slice = free.slice(i, i + RANK_SLICE);
|
|
325
379
|
const down = rankDown ? this.rankedDown(slice) : new Set<SubjectId>();
|
|
326
380
|
for (const person of slice) {
|
|
327
|
-
if (
|
|
381
|
+
if (boostMode && this.lapsedByMe(actor, person, now)) {
|
|
382
|
+
lapsed.push(person);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const isBoosted = boosted.has(person);
|
|
386
|
+
if (down.has(person) && !isBoosted) {
|
|
328
387
|
deferred.push(person);
|
|
329
388
|
continue;
|
|
330
389
|
}
|
|
390
|
+
if (boostMode && !isBoosted && now - (oldestAt.get(person) ?? now) < REPORTER_RANK_LAPSE_MS) {
|
|
391
|
+
ordinary.push(person);
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
331
394
|
const won = this.tryClaim(person, actor);
|
|
332
395
|
if (won !== null) return won;
|
|
333
396
|
}
|
|
334
397
|
}
|
|
335
398
|
const last = page[page.length - 1];
|
|
336
399
|
if (page.length < NEXT_PAGE || last === undefined) break;
|
|
400
|
+
if (boostMode && scanned >= SIGNAL_BOOST_SCAN_MAX) break;
|
|
337
401
|
// Enough read to have looked for a normal case: serve the oldest ranked-down one rather than scan the whole queue.
|
|
338
402
|
if (rankDown && read >= RANK_DOWN_SCAN_MAX && deferred.length > 0) break;
|
|
339
403
|
after = { createdAt: last.createdAt, id: last.id };
|
|
340
404
|
}
|
|
341
|
-
|
|
405
|
+
const rest = [...ordinary, ...deferred, ...lapsed];
|
|
406
|
+
if (rest.length === 0) return null;
|
|
342
407
|
// A rival may have taken one meanwhile: one read of the claims, so a held case costs no write.
|
|
343
|
-
const heldNow = new Set(this.store.loadClaims(
|
|
344
|
-
for (const person of
|
|
408
|
+
const heldNow = new Set(this.store.loadClaims(rest, this.clock.now()).map((c) => c.person));
|
|
409
|
+
for (const person of rest) {
|
|
345
410
|
if (heldNow.has(person)) continue;
|
|
346
411
|
const won = this.tryClaim(person, actor);
|
|
347
412
|
if (won !== null) return won;
|
|
@@ -349,6 +414,25 @@ export class CaseDesk {
|
|
|
349
414
|
return null;
|
|
350
415
|
}
|
|
351
416
|
|
|
417
|
+
/** Did a claim `next` handed this moderator on this person run out within the last claim window? Forgets markers older than that. */
|
|
418
|
+
private lapsedByMe(actor: string, person: SubjectId, now: number): boolean {
|
|
419
|
+
const key = markerKey(actor, person);
|
|
420
|
+
const until = this.served.get(key);
|
|
421
|
+
if (until === undefined) return false;
|
|
422
|
+
if (now - until >= CLAIM_TTL_MS) this.served.delete(key);
|
|
423
|
+
return until <= now && now - until < CLAIM_TTL_MS;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Who of these people the queue signal feed boosts (canonical ids). Nobody without the dependency, or when it throws. */
|
|
427
|
+
private boostedAmong(people: readonly SubjectId[]): Set<SubjectId> {
|
|
428
|
+
if (this.boosted === undefined || people.length === 0) return new Set();
|
|
429
|
+
try {
|
|
430
|
+
return this.boosted(people);
|
|
431
|
+
} catch {
|
|
432
|
+
return new Set();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
352
436
|
/** One atomic 'free' claim of a person's case, opened on their newest open report; null when a rival won or the case closed. */
|
|
353
437
|
private tryClaim(person: SubjectId, actor: string): NextCase | null {
|
|
354
438
|
return retryBusy(() =>
|
|
@@ -356,6 +440,10 @@ export class CaseDesk {
|
|
|
356
440
|
const now = this.clock.now();
|
|
357
441
|
const res = this.store.claimCase(person, actor, now, now + CLAIM_TTL_MS, 'free');
|
|
358
442
|
if (!res.won) return null;
|
|
443
|
+
const key = markerKey(actor, person);
|
|
444
|
+
this.served.delete(key);
|
|
445
|
+
this.served.set(key, res.claim.until);
|
|
446
|
+
if (this.served.size > SERVED_MAX) this.served.delete(this.served.keys().next().value!);
|
|
359
447
|
const newest = this.store.listOpenReportsByPerson(this.subjects.aliasesOf(person), 1)[0];
|
|
360
448
|
if (newest === undefined) {
|
|
361
449
|
// The case closed under us: do not leave a claim on nothing.
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Core types for @chatwarden/core. Organized by section; later tasks append
|
|
2
2
|
// their own sections below.
|
|
3
3
|
|
|
4
|
+
import type { AIExample, AIRead, AISignal } from './ai/types.js';
|
|
4
5
|
import type { AuditListQuery, AuditRow } from './audit.js';
|
|
5
6
|
import type { PolicyLayer } from './profile.js';
|
|
6
7
|
import type { RuleRef } from './rules.js';
|
|
@@ -229,6 +230,214 @@ export interface ReporterOutcomeRow {
|
|
|
229
230
|
badFaith: number;
|
|
230
231
|
}
|
|
231
232
|
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
// AI reads and the casebook (AI levels 1 and 2). Rows of the optional AI Store methods.
|
|
235
|
+
// ---------------------------------------------------------------------------
|
|
236
|
+
|
|
237
|
+
/** Where an AI read stands. `pending` is owned by a lease; `skipped` means the daily limit (or the breaker) kept the call from being made. */
|
|
238
|
+
export type AiReadStatus = 'pending' | 'ok' | 'declined' | 'invalid' | 'failed' | 'skipped';
|
|
239
|
+
|
|
240
|
+
/** Why a read did not finish. `budget` belongs to a `skipped` read; `declined` and `invalid` to the statuses of the same name. */
|
|
241
|
+
export type AiErrorKind = 'retryable' | 'fatal' | 'declined' | 'timeout' | 'invalid' | 'budget';
|
|
242
|
+
|
|
243
|
+
/** A read about to be made: the input the lease is taken for. The store adds the id, the attempt count and the time. */
|
|
244
|
+
export interface AiReadPending {
|
|
245
|
+
reportId: string;
|
|
246
|
+
/** The input hash (design section 6); with `reportId` it is the UNIQUE key. */
|
|
247
|
+
inputHash: string;
|
|
248
|
+
promptVersion: number;
|
|
249
|
+
/** `ModerationAI.id` (never a key). */
|
|
250
|
+
adapter: string;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** What one provider call used and cost: stored on a finished read, and on a failed one that still cost something. */
|
|
254
|
+
export interface AiCallUsage {
|
|
255
|
+
model: string;
|
|
256
|
+
inputTokens: number;
|
|
257
|
+
outputTokens: number;
|
|
258
|
+
cacheReadTokens: number;
|
|
259
|
+
cacheWriteTokens: number;
|
|
260
|
+
/** Integer micro-dollars. */
|
|
261
|
+
costMicros: number;
|
|
262
|
+
latencyMs: number;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The validated answer and the usage of the call that produced it. */
|
|
266
|
+
export interface AiReadDone extends AiCallUsage {
|
|
267
|
+
read: AIRead;
|
|
268
|
+
/**
|
|
269
|
+
* The ids of the casebook examples that were in the prompt (`AIStoredRead.exampleIds`): the examples are not part of the input
|
|
270
|
+
* hash, so the read records which ones it saw. Ids only; none by default.
|
|
271
|
+
*/
|
|
272
|
+
exampleIds?: readonly string[];
|
|
273
|
+
/**
|
|
274
|
+
* The signal counts about the reported person the prompt carried (`feeds.ai`): exactly what was sent, label, polarity, distinct and window
|
|
275
|
+
* only. Stored with the read (`signals_json`); none (or an empty list) is stored as null.
|
|
276
|
+
*/
|
|
277
|
+
signals?: readonly AISignal[];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* One stored read, whatever its state. `read` is the validated answer, set only for `ok`; `leaseUntil` only while `pending`;
|
|
282
|
+
* `retryAt` for a `failed` or `skipped` read that may be tried again. It holds no chat text but the one-line summary and no
|
|
283
|
+
* identity.
|
|
284
|
+
*/
|
|
285
|
+
export interface AiReadRow {
|
|
286
|
+
id: string;
|
|
287
|
+
reportId: string;
|
|
288
|
+
inputHash: string;
|
|
289
|
+
status: AiReadStatus;
|
|
290
|
+
/** Calls started for this row: 1 when reserved, +1 each time a lease or a retry is taken over. */
|
|
291
|
+
attempts: number;
|
|
292
|
+
retryAt: number | null;
|
|
293
|
+
leaseUntil: number | null;
|
|
294
|
+
errorKind: AiErrorKind | null;
|
|
295
|
+
adapter: string;
|
|
296
|
+
model: string | null;
|
|
297
|
+
promptVersion: number;
|
|
298
|
+
read: AIRead | null;
|
|
299
|
+
/** The ids of the casebook examples this read saw (empty when none, and for every row but an `ok` one). */
|
|
300
|
+
exampleIds: readonly string[];
|
|
301
|
+
/** The signal counts this read was shown (see {@link AiReadDone.signals}); null when none were sent, for every row but an `ok` one, and for a read made before signals existed. */
|
|
302
|
+
signals: readonly AISignal[] | null;
|
|
303
|
+
inputTokens: number | null;
|
|
304
|
+
outputTokens: number | null;
|
|
305
|
+
cacheReadTokens: number | null;
|
|
306
|
+
cacheWriteTokens: number | null;
|
|
307
|
+
/**
|
|
308
|
+
* The cost of the attempts that finished as this row's owner, summed. Tokens are summed too; `model` and `latencyMs` are the latest
|
|
309
|
+
* attempt's. A stale owner's paid call is booked only to the spend ledger, so this can be lower than the ledger: the daily cap reads the ledger.
|
|
310
|
+
*/
|
|
311
|
+
costMicros: number;
|
|
312
|
+
latencyMs: number | null;
|
|
313
|
+
/** When the row was first reserved (a take-over keeps it). Spend is booked per attempt, not by this. */
|
|
314
|
+
createdAt: number;
|
|
315
|
+
/**
|
|
316
|
+
* True once a moderator's explicit "read again" has retaken this row (see `reserveAiRead` `opts.retake`). The explicit retake
|
|
317
|
+
* happens once per row, for good: it is a fact of the row, so a restart, a second process or any pruning cannot allow another.
|
|
318
|
+
*/
|
|
319
|
+
retaken: boolean;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* One signal a game recorded about a player (flood control and signals, design 5.8). Staff-side data: it holds ids and numbers only, no
|
|
324
|
+
* text, handle or seat. `sourceKey` is `b:<canonical by>`, `r:<roomId>` or `d:<UTC day>`; `(kind, subject, sourceKey, capBucket)` is unique,
|
|
325
|
+
* which is the per-source cap.
|
|
326
|
+
* @experimental
|
|
327
|
+
*/
|
|
328
|
+
export interface SignalRow {
|
|
329
|
+
id: string;
|
|
330
|
+
kind: string;
|
|
331
|
+
subject: SubjectId;
|
|
332
|
+
/** 'b:<canonical by>' | 'r:<roomId>' | 'd:<UTC day>' */
|
|
333
|
+
sourceKey: string;
|
|
334
|
+
bySubject: SubjectId | null;
|
|
335
|
+
roomId: string | null;
|
|
336
|
+
at: number;
|
|
337
|
+
/** The effective weight, stored at record time (0 or more). */
|
|
338
|
+
weight: number;
|
|
339
|
+
/** floor(at / perByWindowMs): the cap bucket. */
|
|
340
|
+
capBucket: number;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* What `listSignalSources` answers: one row per source of a kind about a subject. Rows are bounded by distinct sources, not by volume.
|
|
345
|
+
* @experimental
|
|
346
|
+
*/
|
|
347
|
+
export interface SignalSourceRow {
|
|
348
|
+
subject: SubjectId;
|
|
349
|
+
kind: string;
|
|
350
|
+
sourceKey: string;
|
|
351
|
+
/** The source's `by` (the smallest, when rows of one source name more than one); null for a room or day source. */
|
|
352
|
+
bySubject: SubjectId | null;
|
|
353
|
+
/** The highest weight among the source's rows in range. */
|
|
354
|
+
weight: number;
|
|
355
|
+
firstAt: number;
|
|
356
|
+
lastAt: number;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* The answer to a reserve: this caller owns the read now (`id`), or a row already exists for that report and input. `lease` is the
|
|
361
|
+
* caller's fence token (the row's `attempts` after this reserve): `completeAiRead` and `failAiRead` must be given it, and do nothing
|
|
362
|
+
* once another caller has taken the row over.
|
|
363
|
+
*/
|
|
364
|
+
export type AiReserveResult = { reserved: true; id: string; lease: number } | { reserved: false; existing: AiReadRow };
|
|
365
|
+
|
|
366
|
+
/** The human's verdict, and the AI's recommendation, in the casebook's three classes. */
|
|
367
|
+
export type AiVerdict = 'upheld' | 'noted' | 'no_action';
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* The finer facts of one outcome (design section 11): whether the verdict and the rule matched the AI, how many cited lines
|
|
371
|
+
* matched, whether the human step was the same, milder or harsher than the pre-fill (`none` when there was no pre-fill), and
|
|
372
|
+
* whether it was a one-click accept. Booleans, a count and a word: no text can be written into a row that outlives its report.
|
|
373
|
+
*/
|
|
374
|
+
export interface AiOutcomeDetail {
|
|
375
|
+
verdict: boolean;
|
|
376
|
+
rule: boolean;
|
|
377
|
+
lines: number;
|
|
378
|
+
outcome: 'same' | 'milder' | 'harsher' | 'none';
|
|
379
|
+
oneClick: boolean;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* One casebook row to insert: a decided report that had an AI read, computed by the server in the transaction of the decision.
|
|
384
|
+
* Ids, counts and the human's final choices; the only chat text is `example`, pseudonymised, kept on its own shorter clock.
|
|
385
|
+
*/
|
|
386
|
+
export interface AiOutcomeInsert {
|
|
387
|
+
reportId: string;
|
|
388
|
+
/** The read the moderator was shown, or null when there was none. */
|
|
389
|
+
readId: string | null;
|
|
390
|
+
decidedAt: number;
|
|
391
|
+
/** The AI level in force when decided (1 or 2). */
|
|
392
|
+
level: number;
|
|
393
|
+
/** The human's final verdict. */
|
|
394
|
+
verdict: AiVerdict;
|
|
395
|
+
/** The human's final rule on the reported person (`'3'`, `'uncovered'`), or null. */
|
|
396
|
+
rule: string | null;
|
|
397
|
+
/** The conduct level the human settled on, when the form had one. */
|
|
398
|
+
severity: ConductLevel | null;
|
|
399
|
+
/** What the AI recommended, or null (level 1, or `unclear`). */
|
|
400
|
+
aiVerdict: AiVerdict | null;
|
|
401
|
+
agreement: 'agree' | 'override' | 'none';
|
|
402
|
+
detail: AiOutcomeDetail;
|
|
403
|
+
/** The report was dismissed as bad faith: excludes the row from examples. */
|
|
404
|
+
badFaith: boolean;
|
|
405
|
+
/** Up to 6 pseudonymised lines, or null for none. A store stores null for a `badFaith` row: it can never teach. */
|
|
406
|
+
example: AIExample | null;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** A stored outcome: what was inserted, and the two marks a later action sets. */
|
|
410
|
+
export interface AiOutcomeRow extends AiOutcomeInsert {
|
|
411
|
+
/** Set by a granted appeal or a take-back. Excludes the row from examples (and drops its example text); counts as an AI false positive. */
|
|
412
|
+
reversed: boolean;
|
|
413
|
+
/** A senior's mark. Excludes the row from examples. */
|
|
414
|
+
dontTeach: boolean;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** One teachable casebook example, newest first from {@link Store.aiExamples}. The human's final decision is the label. */
|
|
418
|
+
export interface AiExampleRow {
|
|
419
|
+
reportId: string;
|
|
420
|
+
decidedAt: number;
|
|
421
|
+
agreement: 'agree' | 'override';
|
|
422
|
+
example: AIExample;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The casebook counts for one rule (the human's final rule; `null` for none), for decisions at or after a time. `decided` is every
|
|
427
|
+
* outcome; `falsePositives` those where the AI recommended `upheld` or `noted` and the human settled on `no_action`, or the
|
|
428
|
+
* decision was later reversed.
|
|
429
|
+
*/
|
|
430
|
+
export interface AiStatsRow {
|
|
431
|
+
rule: string | null;
|
|
432
|
+
decided: number;
|
|
433
|
+
agree: number;
|
|
434
|
+
override: number;
|
|
435
|
+
none: number;
|
|
436
|
+
falsePositives: number;
|
|
437
|
+
/** Decisions that were a one-click accept of the AI's pre-fill (the outcome detail's `oneClick`). */
|
|
438
|
+
oneClick: number;
|
|
439
|
+
}
|
|
440
|
+
|
|
232
441
|
/**
|
|
233
442
|
* One decided report a person filed that carried a reason, with what its decision did: for the reporter's accuracy. `reason` is
|
|
234
443
|
* the report's stored `reportedReason`; `target` the reported subject as stored; `actions` one entry per sanction group the
|
|
@@ -709,7 +918,7 @@ export interface Store {
|
|
|
709
918
|
* Reports that are still `open`, OLDEST first (`createdAt` ascending, then `id` ascending, so a tie is stable), whole
|
|
710
919
|
* records. `category` filters; `after` keeps only reports newer than that instant (exclusive), and with `afterId` also
|
|
711
920
|
* the ones filed AT that instant whose id sorts above it (keyset paging: pass the last row's `createdAt` and `id`).
|
|
712
|
-
* `limit` is clamped to 1..200 (a fraction is floored). The scan behind "take next".
|
|
921
|
+
* `limit` is clamped to 1..200 (a fraction is floored); a limit that is not finite, or is below 1, counts as 1. The scan behind "take next".
|
|
713
922
|
*/
|
|
714
923
|
listOpenReports(q: OpenReportQuery): ReportRecord[];
|
|
715
924
|
|
|
@@ -858,6 +1067,152 @@ export interface Store {
|
|
|
858
1067
|
*/
|
|
859
1068
|
savePolicyLayer?(layer: PolicyLayer, expectedVersion: number): number | null;
|
|
860
1069
|
|
|
1070
|
+
// AI reads and the casebook (AI levels 1 and 2, design section 5). OPTIONAL: a store without them works as before, and
|
|
1071
|
+
// `createChatwarden({ ai })` leaves the AI inert with one (and logs it once). SYNCHRONOUS on purpose (like claims); they run inside `transaction`.
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* OPTIONAL. Take the right to make one read, as ONE atomic conditional write: of two callers (two processes on one database
|
|
1075
|
+
* included) for the same `reportId` and `inputHash`, exactly one gets `{ reserved: true }`; the other gets the existing row. A new
|
|
1076
|
+
* row is `pending`, owned until `leaseUntil`, with `attempts` 1. An existing row is taken over (same id, `attempts` + 1, a new
|
|
1077
|
+
* lease, `retryAt` and `errorKind` cleared, `createdAt` kept) only when it is `pending` and its lease has ended (`leaseUntil <= now`),
|
|
1078
|
+
* or `failed` or `skipped` with a `retryAt` that has come (`retryAt <= now`), or `ok` with a stored answer that cannot be read. An
|
|
1079
|
+
* `ok` (readable), `declined` or `invalid` row, a `failed` or `skipped` one with no `retryAt`, and a live lease are returned as
|
|
1080
|
+
* `existing` (a copy). A win carries the fence token `lease` (= `attempts`), and opens one spend-ledger entry at `now` (one call
|
|
1081
|
+
* started); `aiSpendSince` reads that ledger, so every attempt is booked to the time it was made. Does not move the change token.
|
|
1082
|
+
* With `opts.retake` (a moderator's explicit "read again") a FINAL row (`declined`, `invalid`, or `failed` with no `retryAt`) is
|
|
1083
|
+
* taken over too, once: the take-over sets the row's `retaken` marker and a row already marked is never retaken again. A `failed` row
|
|
1084
|
+
* still waiting for its own retry time (`retryAt` set), an `ok` read and a live lease never are, so the three automatic attempts
|
|
1085
|
+
* hold; the one explicit retake is the single extra attempt (a given-up row's is its fourth). A `skipped` row made no call and gave its
|
|
1086
|
+
* attempt back, so an explicit ask takes it at once, whatever its `retryAt`, without using the marker.
|
|
1087
|
+
*/
|
|
1088
|
+
reserveAiRead?(row: AiReadPending, leaseUntil: number, now: number, opts?: { retake?: boolean }): AiReserveResult;
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* OPTIONAL. Store the validated read and the usage on a `pending` row: status `ok`, the lease cleared; cost and tokens are added to
|
|
1092
|
+
* the row, and the cost to this attempt's ledger entry. `lease` is the token `reserveAiRead` returned. True only when this call did
|
|
1093
|
+
* it; false (nothing changed) for an unknown row, one that is not `pending`, or one whose `attempts` is no longer `lease` (another
|
|
1094
|
+
* caller took it over: a stale owner can neither finish nor fail the new owner's row). A stale owner's call was still paid for: its
|
|
1095
|
+
* `result` cost is booked to its own attempt's ledger entry (once), and nothing else is touched. Moves the change token only on true.
|
|
1096
|
+
*/
|
|
1097
|
+
completeAiRead?(id: string, lease: number, result: AiReadDone): boolean;
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* OPTIONAL. End a `pending` row without a read: `failed`, `declined`, `invalid` or `skipped`, with the kind of failure and, for
|
|
1101
|
+
* a retry, the time it may be taken again (`retryAt`; null is final). The lease is cleared. `usage`, when the provider still
|
|
1102
|
+
* charged for the call (a refusal, an invalid answer, a failure after tokens were billed), is added to the row and to this attempt's
|
|
1103
|
+
* ledger entry, so it counts toward `aiSpendSince`. A `skipped` read was never a call: its ledger entry is removed and the attempt is given back (a skip does not use up the three-attempt budget). `lease` fences as
|
|
1104
|
+
* in `completeAiRead`, including the booking of a stale owner's `usage`. True when this call did it; false for an unknown row, one that
|
|
1105
|
+
* is not `pending`, or a stale `lease`. Does not move the change token.
|
|
1106
|
+
*/
|
|
1107
|
+
failAiRead?(
|
|
1108
|
+
id: string,
|
|
1109
|
+
lease: number,
|
|
1110
|
+
status: 'failed' | 'declined' | 'invalid' | 'skipped',
|
|
1111
|
+
errorKind: AiErrorKind,
|
|
1112
|
+
retryAt: number | null,
|
|
1113
|
+
usage?: AiCallUsage,
|
|
1114
|
+
): boolean;
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* OPTIONAL. The newest `ok` read of the report (`createdAt` descending, then `id` descending), whatever its input hash; null
|
|
1118
|
+
* when there is none. A row whose stored answer cannot be read is skipped. Pending, failed, declined, invalid and skipped rows
|
|
1119
|
+
* are never returned.
|
|
1120
|
+
*/
|
|
1121
|
+
loadAiRead?(reportId: string): AiReadRow | null;
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* OPTIONAL. The newest `ok` read of each of these reports (one row per report that has one), in the order the ids first appear.
|
|
1125
|
+
* At most the first 200 distinct ids are looked at; unknown ids give nothing.
|
|
1126
|
+
*/
|
|
1127
|
+
loadAiReads?(reportIds: readonly string[]): AiReadRow[];
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* OPTIONAL. One read by its id, whatever its state (a copy), or null for an unknown id. The decision's echo names the read the
|
|
1131
|
+
* moderator was shown; the caller checks that it belongs to the report and is `ok`.
|
|
1132
|
+
*/
|
|
1133
|
+
loadAiReadById?(id: string): AiReadRow | null;
|
|
1134
|
+
|
|
1135
|
+
/**
|
|
1136
|
+
* OPTIONAL. The row for this report and this input hash (the pair is unique), whatever its state (a copy), or null. "Read again"
|
|
1137
|
+
* looks at it to tell a final row, which an explicit ask may retake once (`retaken`), from one still waiting for its own retry.
|
|
1138
|
+
*/
|
|
1139
|
+
loadAiReadFor?(reportId: string, inputHash: string): AiReadRow | null;
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* OPTIONAL. The rows the sweeper should pick up at `now`, oldest first (`createdAt`, then `id`), at most `limit` (clamped to
|
|
1143
|
+
* 1..200): `failed` or `skipped` rows whose `retryAt` has come (`retryAt <= now`), and `pending` rows whose lease has ended
|
|
1144
|
+
* (`leaseUntil <= now`: the process that held them died). Rows of reports that have been decided are left out: nobody will read them. `limit` is clamped to 1..200 (a fraction is floored); one that is omitted, not finite or below 1 is the default of 20 (what the sweeper enqueues at a time).
|
|
1145
|
+
*/
|
|
1146
|
+
listAiRetries?(now: number, limit: number | undefined): AiReadRow[];
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* OPTIONAL. What the attempts made at or after `since` cost: the sum of their `costMicros` (a failed attempt with usage included),
|
|
1150
|
+
* and the number of calls (every attempt but a skipped one, a `pending` one included). Each attempt counts at the time it was
|
|
1151
|
+
* reserved, so a retry that crosses UTC midnight books each attempt to its own day. The daily cap is this with `since` = the UTC
|
|
1152
|
+
* midnight.
|
|
1153
|
+
*/
|
|
1154
|
+
aiSpendSince?(since: number): { micros: number; calls: number };
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* OPTIONAL. Record the outcome of a decided report. False, nothing written, when the report already has one. A `badFaith` row is
|
|
1158
|
+
* stored without its example (it can never teach). Does not move the change token (the decision it belongs to does).
|
|
1159
|
+
*/
|
|
1160
|
+
insertAiOutcome?(row: AiOutcomeInsert): boolean;
|
|
1161
|
+
|
|
1162
|
+
/** OPTIONAL. The report's outcome, or null. Returns a copy; an unreadable `detail` reads as `{}` and an unreadable example as null. */
|
|
1163
|
+
loadAiOutcome?(reportId: string): AiOutcomeRow | null;
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* OPTIONAL. Set the marks on an outcome (a mark is never cleared) and drop its example text in the same write: a reversed or marked
|
|
1167
|
+
* row can never teach, so its chat text goes now. False for an unknown report. Moves the change token when the report has an outcome.
|
|
1168
|
+
*/
|
|
1169
|
+
markAiOutcome?(reportId: string, patch: { reversed?: true; dontTeach?: true }): boolean;
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* OPTIONAL. Teachable examples with this human rule (`'3'`, `'uncovered'`) and conduct level, decided at or after `since`, newest
|
|
1173
|
+
* first (`decidedAt` descending, then `reportId` descending), at most `limit`. Teachable: an example is
|
|
1174
|
+
* stored, the AI recommended something (`agree` or `override`, not `none`), and the row is not `reversed`, `dontTeach` or `badFaith`.
|
|
1175
|
+
* `limit` is clamped to 1..200 (a fraction is floored); one that is omitted, not finite or below 1 is the default of 2 (the prompt takes at most two
|
|
1176
|
+
* examples).
|
|
1177
|
+
*/
|
|
1178
|
+
aiExamples?(rule: string, severity: string, limit: number | undefined, since: number): AiExampleRow[];
|
|
1179
|
+
|
|
1180
|
+
/** OPTIONAL. How many teachable examples (see `aiExamples`) were decided at or after `since`. */
|
|
1181
|
+
aiTeachableCount?(since: number): number;
|
|
1182
|
+
|
|
1183
|
+
/**
|
|
1184
|
+
* OPTIONAL. The casebook counts per rule for decisions at or after `since`: numbered rules in number order, then `'uncovered'`,
|
|
1185
|
+
* then no rule. A rule with no decision is left out.
|
|
1186
|
+
*/
|
|
1187
|
+
aiStats?(since: number): AiStatsRow[];
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* OPTIONAL. Retention for the AI rows, all strict (`<`): reads created before `readsBefore` (any status), outcomes decided before
|
|
1191
|
+
* `outcomesBefore`, and the example of every outcome decided before `examplesBefore` (set to none; the counts stay); spend-ledger
|
|
1192
|
+
* entries made before `readsBefore` go with the reads. Returns how many reads and outcomes were deleted. `purgeReports` deletes the reads of the reports it deletes itself.
|
|
1193
|
+
*/
|
|
1194
|
+
purgeAi?(readsBefore: number, outcomesBefore: number, examplesBefore: number): { reads: number; outcomes: number };
|
|
1195
|
+
|
|
1196
|
+
// Signals (flood control and signals, design 5.8). OPTIONAL: a store without them cannot keep signals, and `createChatwarden` refuses a
|
|
1197
|
+
// profile that declares kinds or feeds with one. SYNCHRONOUS on purpose (like claims); they run inside `transaction`. They do not move
|
|
1198
|
+
// the change token.
|
|
1199
|
+
|
|
1200
|
+
/**
|
|
1201
|
+
* OPTIONAL. Insert one signal as ONE atomic statement: false, and nothing written, when a row with the same `(kind, subject, sourceKey,
|
|
1202
|
+
* capBucket)` exists (the per-source cap; of two callers, two processes on one database included, exactly one writes) or the id is
|
|
1203
|
+
* taken. True when a row was written.
|
|
1204
|
+
*/
|
|
1205
|
+
insertSignal?(row: SignalRow): boolean;
|
|
1206
|
+
|
|
1207
|
+
/**
|
|
1208
|
+
* OPTIONAL. One row per `(subject, kind, sourceKey)` that has a row at or after `since`, for these subjects (a person's canonical id and
|
|
1209
|
+
* aliases): the highest weight, the earliest and the latest `at` among the rows in range. No subjects: `[]`. Returns copies.
|
|
1210
|
+
*/
|
|
1211
|
+
listSignalSources?(subjects: readonly SubjectId[], since: number): SignalSourceRow[];
|
|
1212
|
+
|
|
1213
|
+
/** OPTIONAL. Delete rows with `at < before` (a row exactly on the line is kept); returns how many. */
|
|
1214
|
+
purgeSignals?(before: number): number;
|
|
1215
|
+
|
|
861
1216
|
// Staff audit trail. SYNCHRONOUS on purpose (like sanctions). The trail also holds the rows `closeGap` and the term
|
|
862
1217
|
// editor write; none of these methods ever returns the json columns those carry (they may hold term text).
|
|
863
1218
|
|
|
@@ -890,7 +1245,8 @@ export interface Store {
|
|
|
890
1245
|
/**
|
|
891
1246
|
* Delete reports with `createdAt < before`, except a bad-faith dismissal still inside its window
|
|
892
1247
|
* (`bad_faith = 1` and `actionedAt >= keepBadFaithSince`): the reporter's suspension reads it. Also deletes the
|
|
893
|
-
* recorded decisions of reports that no longer exist
|
|
1248
|
+
* recorded decisions of reports that no longer exist, and the AI reads of the reports it deletes (not their casebook outcomes: those
|
|
1249
|
+
* have their own clock, see `purgeAi`). Returns how many reports it deleted.
|
|
894
1250
|
*/
|
|
895
1251
|
purgeReports(before: number, keepBadFaithSince: number): number;
|
|
896
1252
|
|