@agent-custody/receipts 0.16.11 → 0.16.13
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 +23 -5
- package/dist/admission.d.ts +62 -0
- package/dist/admission.js +101 -0
- package/dist/check.js +42 -3
- package/dist/cli.js +179 -7
- package/dist/config.d.ts +6 -1
- package/dist/config.js +21 -2
- package/dist/door-app.js +1 -1
- package/dist/door-core.d.ts +81 -3
- package/dist/door-core.js +113 -13
- package/dist/door-error-rates.d.ts +102 -0
- package/dist/door-error-rates.js +178 -0
- package/dist/door-form.d.ts +7 -2
- package/dist/door-form.js +52 -4
- package/dist/door-measure.d.ts +153 -0
- package/dist/door-measure.js +325 -0
- package/dist/door-signals.d.ts +414 -0
- package/dist/door-signals.js +555 -0
- package/dist/door-test-set.d.ts +43 -0
- package/dist/door-test-set.js +117 -0
- package/dist/door-worker.d.ts +20 -1
- package/dist/door-worker.js +30 -2
- package/dist/door.js +18 -2
- package/dist/erc8004-link.d.ts +24 -0
- package/dist/erc8004-link.js +11 -0
- package/dist/erc8004.d.ts +26 -0
- package/dist/erc8004.js +208 -0
- package/dist/index.d.ts +17 -3
- package/dist/index.js +9 -2
- package/dist/lock.d.ts +5 -0
- package/dist/lock.js +1 -1
- package/dist/log-store.d.ts +3 -1
- package/dist/log-store.js +2 -2
- package/dist/passport.d.ts +53 -0
- package/dist/passport.js +87 -0
- package/dist/portal.d.ts +14 -0
- package/dist/portal.js +102 -0
- package/dist/registry.d.ts +145 -0
- package/dist/registry.js +294 -0
- package/dist/web-bot-auth.d.ts +9 -3
- package/dist/web-bot-auth.js +31 -4
- package/docs/admission-lists.md +89 -0
- package/docs/agent-registry.md +173 -0
- package/docs/door-error-rates.md +490 -0
- package/docs/door-languages.md +3 -1
- package/docs/door-middleware.md +6 -1
- package/docs/door-quickstart.md +2 -2
- package/docs/door-signals.md +112 -2
- package/docs/door-worker.md +14 -1
- package/docs/door.md +37 -6
- package/docs/github-action.md +2 -2
- package/docs/tutorials.md +7 -0
- package/docs/usage.md +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { BROWSER_HEADERS, MAX_CLIENT_HINT, NAVIGATE_DESTS, WRITE_HEADERS, FLAG_SIGNALS, FORM_FAST_MIN_KEYS, FORM_FAST_MS_PER_KEY, HISTORY_MAX_ARRIVALS, HISTORY_WINDOW_MS, KNOWN_TOOL_JA4, MAX_FORM_FILL_MS, MAX_FORM_REPORT, MAX_HEADER_NAMES, MAX_HEADER_NAME_LENGTH, MAX_TLS_VALUE, MAX_USER_AGENT, SEC_CH_UA_SINCE, SEC_FETCH_HEADERS, SEC_FETCH_SINCE, STATE_CHANGING, TIMING_MIN_GAPS, TIMING_REGULAR_MIN_MEDIAN_MS, TIMING_REGULAR_SPREAD_PERCENT, TLS13_SINCE, createHistoryTracker, flagOf, signalsOf, } from "./door-signals.js";
|
|
2
|
+
export const LABELS = ["human", "automated"];
|
|
3
|
+
/** The questions about `class` that `door error-rates` scores (src/door-error-rates.ts). */
|
|
4
|
+
export const LABEL_QUESTIONS = ["agent", "verified"];
|
|
5
|
+
/** What a label can be good for: a question about `class`, or a flag. `validFor` holds these names in both label formats. */
|
|
6
|
+
export const LABEL_USES = [...LABEL_QUESTIONS, ...FLAG_SIGNALS];
|
|
7
|
+
/**
|
|
8
|
+
* Every exported constant of door-signals.ts that a flag reads. A result measured with other values does not describe
|
|
9
|
+
* this door. The rules that read a User-Agent (`statedClient`, the embedded-Chromium forms) are code and not constants:
|
|
10
|
+
* a change to them needs a new measure too, and this record does not show it.
|
|
11
|
+
*/
|
|
12
|
+
export function thresholds() {
|
|
13
|
+
return {
|
|
14
|
+
HISTORY_WINDOW_MS, HISTORY_MAX_ARRIVALS, TIMING_MIN_GAPS, TIMING_REGULAR_SPREAD_PERCENT, TIMING_REGULAR_MIN_MEDIAN_MS, FORM_FAST_MIN_KEYS, FORM_FAST_MS_PER_KEY,
|
|
15
|
+
MAX_FORM_REPORT, MAX_FORM_FILL_MS, MAX_HEADER_NAMES, MAX_HEADER_NAME_LENGTH, MAX_USER_AGENT, MAX_TLS_VALUE, STATE_CHANGING: [...STATE_CHANGING].sort(),
|
|
16
|
+
BROWSER_HEADERS, WRITE_HEADERS, SEC_FETCH_HEADERS, NAVIGATE_DESTS: [...NAVIGATE_DESTS].sort(), MAX_CLIENT_HINT, SEC_FETCH_SINCE, SEC_CH_UA_SINCE, TLS13_SINCE, KNOWN_TOOL_JA4: Object.fromEntries([...KNOWN_TOOL_JA4].sort(([a], [b]) => (a < b ? -1 : 1))),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const Z = 1.959963984540054;
|
|
20
|
+
/** The rate `flagged / n` and its 95% Wilson score interval, as integers per 10,000. The interval is rounded outward. */
|
|
21
|
+
export function rateOf(flagged, n) {
|
|
22
|
+
if (n === 0)
|
|
23
|
+
return { n, flagged, rate: null, low: null, high: null };
|
|
24
|
+
const p = flagged / n;
|
|
25
|
+
const z2 = Z * Z;
|
|
26
|
+
const centre = (p + z2 / (2 * n)) / (1 + z2 / n);
|
|
27
|
+
const half = (Z * Math.sqrt((p * (1 - p)) / n + z2 / (4 * n * n))) / (1 + z2 / n);
|
|
28
|
+
const round = (x) => Math.round(x * 1e6) / 1e6;
|
|
29
|
+
return {
|
|
30
|
+
n,
|
|
31
|
+
flagged,
|
|
32
|
+
rate: Math.round((flagged * 10_000) / n),
|
|
33
|
+
low: flagged === 0 ? 0 : Math.max(0, Math.floor(round((centre - half) * 10_000))),
|
|
34
|
+
high: flagged === n ? 10_000 : Math.min(10_000, Math.ceil(round((centre + half) * 10_000))),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The flags that apply to an arrival, from its signals. The header flag applies only when the record has the header
|
|
39
|
+
* names. The client-hints flag applies only when the arrival sent `sec-ch-ua`, and the Fetch Metadata flag only when it
|
|
40
|
+
* sent a `sec-fetch-*` header: without them the flag is false by construction. The TLS flags apply only when a well-formed JA4 arrived. The form flags apply only to a well-formed form report.
|
|
41
|
+
*/
|
|
42
|
+
export function flagValues(signals, hasHeaders) {
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const flag of FLAG_SIGNALS) {
|
|
45
|
+
const value = flagOf(signals, flag);
|
|
46
|
+
if (value === undefined)
|
|
47
|
+
continue;
|
|
48
|
+
if (flag === "fingerprint.headers.mismatch" && !hasHeaders)
|
|
49
|
+
continue;
|
|
50
|
+
if (flag === "fingerprint.headers.hintsMismatch" && signals.fingerprint.headers.hints === undefined)
|
|
51
|
+
continue;
|
|
52
|
+
const nav = signals.behaviour.navigation;
|
|
53
|
+
if (flag === "fingerprint.headers.fetchInvalid" && nav.site === undefined && nav.mode === undefined && nav.dest === undefined)
|
|
54
|
+
continue;
|
|
55
|
+
if (flag.startsWith("fingerprint.tls.") && signals.fingerprint.tls?.ja4 === undefined)
|
|
56
|
+
continue;
|
|
57
|
+
if (flag.startsWith("behaviour.form.") && signals.behaviour.form?.report !== "claimed")
|
|
58
|
+
continue;
|
|
59
|
+
out[flag] = value;
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
64
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/;
|
|
65
|
+
/** True when the text can be the name of a set, a session, or a label source. */
|
|
66
|
+
export const isName = (v) => typeof v === "string" && NAME.test(v);
|
|
67
|
+
const optional = (o, where, fields, type) => {
|
|
68
|
+
for (const f of fields)
|
|
69
|
+
if (o[f] !== undefined && (typeof o[f] !== type || (type === "number" && !Number.isFinite(o[f]))))
|
|
70
|
+
throw new Error(`${where}${f} is a ${type}`);
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* A labelled record from one line of a set. Throws with the reason when the line is not one. Every field of the
|
|
74
|
+
* observation has its type checked here, so the replay never meets a number where the door's code reads text. The
|
|
75
|
+
* method is in upper case, as the door records it.
|
|
76
|
+
*/
|
|
77
|
+
export function parseLabelledRecord(line) {
|
|
78
|
+
const r = JSON.parse(line);
|
|
79
|
+
if (!isObject(r))
|
|
80
|
+
throw new Error("a record is a JSON object");
|
|
81
|
+
for (const field of ["set", "session", "labelSource"])
|
|
82
|
+
if (!isName(r[field]))
|
|
83
|
+
throw new Error(`${field} is a name of letters, digits, and . _ : / -`);
|
|
84
|
+
if (r.label !== "human" && r.label !== "automated")
|
|
85
|
+
throw new Error('label is "human" or "automated"');
|
|
86
|
+
if (r.validFor !== undefined && !(Array.isArray(r.validFor) && r.validFor.every((f) => LABEL_USES.includes(f))))
|
|
87
|
+
throw new Error("validFor is a list of flags and questions");
|
|
88
|
+
if (r.automated !== undefined && r.automated !== (r.label === "automated"))
|
|
89
|
+
throw new Error("automated is true for the label automated and false for the label human");
|
|
90
|
+
if ((r.observation === undefined) === (r.flags === undefined))
|
|
91
|
+
throw new Error("a record has observation or flags, and not both");
|
|
92
|
+
if (r.observation !== undefined) {
|
|
93
|
+
const o = r.observation;
|
|
94
|
+
if (!isObject(o) || !Number.isFinite(o.at) || typeof o.method !== "string" || typeof o.path !== "string")
|
|
95
|
+
throw new Error("observation needs at (a number), method, and path");
|
|
96
|
+
if (!/^[A-Z]{1,16}$/.test(o.method))
|
|
97
|
+
throw new Error("observation.method is in upper case, as the door records it");
|
|
98
|
+
if (o.referer !== "same-origin" && o.referer !== "cross-origin" && o.referer !== "absent")
|
|
99
|
+
throw new Error('observation.referer is "same-origin", "cross-origin", or "absent"');
|
|
100
|
+
if (o.origin !== undefined && o.origin !== "same-origin" && o.origin !== "cross-origin" && o.origin !== "absent")
|
|
101
|
+
throw new Error('observation.origin is "same-origin", "cross-origin", or "absent"');
|
|
102
|
+
optional(o, "observation.", ["userAgent", "accept", "form", "clientHints", "http"], "string");
|
|
103
|
+
if (o.network !== undefined) {
|
|
104
|
+
if (!isObject(o.network))
|
|
105
|
+
throw new Error("observation.network is an object");
|
|
106
|
+
optional(o.network, "observation.network.", ["asn"], "number");
|
|
107
|
+
optional(o.network, "observation.network.", ["organization"], "string");
|
|
108
|
+
}
|
|
109
|
+
optional(o, "observation.", ["secure"], "boolean");
|
|
110
|
+
if (!isObject(o.secFetch))
|
|
111
|
+
throw new Error("observation.secFetch is an object");
|
|
112
|
+
optional(o.secFetch, "observation.secFetch.", ["site", "mode", "dest", "user"], "string");
|
|
113
|
+
if (o.headers !== undefined && (!isObject(o.headers) || !Array.isArray(o.headers.names) || !o.headers.names.every((n) => typeof n === "string") || typeof o.headers.ordered !== "boolean"))
|
|
114
|
+
throw new Error("observation.headers is { names, ordered }, and each name is a string");
|
|
115
|
+
if (o.tls !== undefined) {
|
|
116
|
+
if (!isObject(o.tls))
|
|
117
|
+
throw new Error("observation.tls is an object");
|
|
118
|
+
optional(o.tls, "observation.tls.", ["ja4", "negotiated", "cipher", "extensionsSha1"], "string");
|
|
119
|
+
optional(o.tls, "observation.tls.", ["helloLength"], "number");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (!isObject(r.flags))
|
|
124
|
+
throw new Error("flags is an object");
|
|
125
|
+
for (const [flag, value] of Object.entries(r.flags)) {
|
|
126
|
+
if (!FLAG_SIGNALS.includes(flag))
|
|
127
|
+
throw new Error(`${flag} is not a flag`);
|
|
128
|
+
if (typeof value !== "boolean")
|
|
129
|
+
throw new Error(`flags.${flag} is true or false`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return r;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Counts labelled records as they come, so a large set is never held whole. Each session has its own history, as one
|
|
136
|
+
* source has at the door, and its records must come in time order. The memory grows with the number of sessions.
|
|
137
|
+
*/
|
|
138
|
+
export function createMeasure() {
|
|
139
|
+
const sets = new Map();
|
|
140
|
+
return {
|
|
141
|
+
add(record) {
|
|
142
|
+
let set = sets.get(record.set);
|
|
143
|
+
if (!set) {
|
|
144
|
+
set = { sessions: new Map(), labelSources: { human: new Map(), automated: new Map() }, arrivals: { human: new Map(), automated: new Map() }, count: { human: 0, automated: 0 } };
|
|
145
|
+
sets.set(record.set, set);
|
|
146
|
+
}
|
|
147
|
+
let session = set.sessions.get(record.session);
|
|
148
|
+
if (!session) {
|
|
149
|
+
session = { label: record.label, last: -Infinity, applies: new Set(), flagged: new Set() };
|
|
150
|
+
set.sessions.set(record.session, session);
|
|
151
|
+
}
|
|
152
|
+
if (session.label !== record.label)
|
|
153
|
+
throw new Error(`session ${record.session} of ${record.set} has two labels`);
|
|
154
|
+
let values;
|
|
155
|
+
if (record.observation) {
|
|
156
|
+
const o = record.observation;
|
|
157
|
+
if (o.at < session.last)
|
|
158
|
+
throw new Error(`session ${record.session} of ${record.set} goes back in time: sort its records by observation.at`);
|
|
159
|
+
session.last = o.at;
|
|
160
|
+
session.tracker ??= createHistoryTracker({ maxSources: 1 });
|
|
161
|
+
values = flagValues(signalsOf(o, session.tracker.record("session", o)), !!o.headers);
|
|
162
|
+
}
|
|
163
|
+
else
|
|
164
|
+
values = record.flags ?? {};
|
|
165
|
+
set.count[record.label]++;
|
|
166
|
+
const sources = set.labelSources[record.label];
|
|
167
|
+
sources.set(record.labelSource, (sources.get(record.labelSource) ?? 0) + 1);
|
|
168
|
+
for (const flag of FLAG_SIGNALS) {
|
|
169
|
+
const value = values[flag];
|
|
170
|
+
if (value === undefined || (record.validFor && !record.validFor.includes(flag)))
|
|
171
|
+
continue;
|
|
172
|
+
const cell = set.arrivals[record.label].get(flag) ?? { n: 0, flagged: 0 };
|
|
173
|
+
set.arrivals[record.label].set(flag, cell);
|
|
174
|
+
cell.n++;
|
|
175
|
+
session.applies.add(flag);
|
|
176
|
+
if (value) {
|
|
177
|
+
cell.flagged++;
|
|
178
|
+
session.flagged.add(flag);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
result() {
|
|
183
|
+
const names = [...sets.keys()].sort();
|
|
184
|
+
return {
|
|
185
|
+
format: "agent-custody/door-measure/v1",
|
|
186
|
+
per: 10000,
|
|
187
|
+
confidencePercent: 95,
|
|
188
|
+
thresholds: thresholds(),
|
|
189
|
+
sets: names.map((name) => {
|
|
190
|
+
const set = sets.get(name);
|
|
191
|
+
const side = (label, flag) => {
|
|
192
|
+
const cell = set.arrivals[label].get(flag) ?? { n: 0, flagged: 0 };
|
|
193
|
+
let n = 0;
|
|
194
|
+
let flagged = 0;
|
|
195
|
+
for (const s of set.sessions.values()) {
|
|
196
|
+
if (s.label !== label || !s.applies.has(flag))
|
|
197
|
+
continue;
|
|
198
|
+
n++;
|
|
199
|
+
if (s.flagged.has(flag))
|
|
200
|
+
flagged++;
|
|
201
|
+
}
|
|
202
|
+
return { arrivals: rateOf(cell.flagged, cell.n), sessions: rateOf(flagged, n) };
|
|
203
|
+
};
|
|
204
|
+
const sessions = { human: 0, automated: 0 };
|
|
205
|
+
for (const s of set.sessions.values())
|
|
206
|
+
sessions[s.label]++;
|
|
207
|
+
const sources = (label) => Object.fromEntries([...set.labelSources[label]].sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
208
|
+
return {
|
|
209
|
+
set: name,
|
|
210
|
+
arrivals: { ...set.count },
|
|
211
|
+
sessions,
|
|
212
|
+
labelSources: { human: sources("human"), automated: sources("automated") },
|
|
213
|
+
flags: FLAG_SIGNALS.map((flag) => ({ flag, human: side("human", flag), automated: side("automated", flag) })),
|
|
214
|
+
};
|
|
215
|
+
}),
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** The measure of a set that fits in memory. The records of each session are sorted by their recorded time first. */
|
|
221
|
+
export function measure(records) {
|
|
222
|
+
const all = [...records];
|
|
223
|
+
const order = new Map(all.map((r, i) => [r, i]));
|
|
224
|
+
all.sort((a, b) => (a.observation?.at ?? 0) - (b.observation?.at ?? 0) || order.get(a) - order.get(b));
|
|
225
|
+
const m = createMeasure();
|
|
226
|
+
for (const r of all)
|
|
227
|
+
m.add(r);
|
|
228
|
+
return m.result();
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* An integer per 10,000 as a percentage with one decimal. `down` and `up` round to the printed digit in that direction:
|
|
232
|
+
* a bound of an interval is printed outward, so the printed interval is never narrower than the computed one.
|
|
233
|
+
*/
|
|
234
|
+
export function percent(per10k, round = "nearest") {
|
|
235
|
+
const tenths = round === "down" ? Math.floor(per10k / 10) : round === "up" ? Math.ceil(per10k / 10) : Math.round(per10k / 10);
|
|
236
|
+
return `${(tenths / 10).toFixed(1)}%`;
|
|
237
|
+
}
|
|
238
|
+
/** The rate of a measured count as a percentage. A count above zero that rounds to 0.0% is `<0.1%`, so that it does not read as zero. */
|
|
239
|
+
export function ratePercent(r) {
|
|
240
|
+
return r.flagged > 0 && r.rate < 5 ? "<0.1%" : percent(r.rate);
|
|
241
|
+
}
|
|
242
|
+
/** The interval of a measured count as text, each bound printed outward: `0.2% to 3.7%`. */
|
|
243
|
+
export function intervalText(r) {
|
|
244
|
+
return `${percent(r.low, "down")} to ${percent(r.high, "up")}`;
|
|
245
|
+
}
|
|
246
|
+
/** One rate as text: `3/200 = 1.5% (0.5% to 4.4%)`, or `not measured` when the flag applies to nothing. */
|
|
247
|
+
export function formatRate(r) {
|
|
248
|
+
if (r.rate === null)
|
|
249
|
+
return "not measured";
|
|
250
|
+
return `${r.flagged}/${r.n} = ${ratePercent(r)} (${intervalText(r)})`;
|
|
251
|
+
}
|
|
252
|
+
/** The result as text: for each set, each flag, with both sides per session and per arrival. */
|
|
253
|
+
export function formatMeasure(result) {
|
|
254
|
+
const lines = [];
|
|
255
|
+
for (const set of result.sets) {
|
|
256
|
+
lines.push(`${set.set}: ${set.arrivals.human} human arrivals in ${set.sessions.human} sessions, ${set.arrivals.automated} automated arrivals in ${set.sessions.automated} sessions`);
|
|
257
|
+
for (const label of LABELS)
|
|
258
|
+
for (const [source, n] of Object.entries(set.labelSources[label]))
|
|
259
|
+
lines.push(` ${label} by ${source}: ${n}`);
|
|
260
|
+
for (const f of set.flags) {
|
|
261
|
+
if (f.human.arrivals.n + f.automated.arrivals.n === 0) {
|
|
262
|
+
lines.push(` ${f.flag}: not measured`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
lines.push(` ${f.flag}`);
|
|
266
|
+
lines.push(` false-positive rate (humans flagged; lower is better) per session: ${formatRate(f.human.sessions)}; per arrival: ${formatRate(f.human.arrivals)}`);
|
|
267
|
+
lines.push(` detection rate (automated flagged; higher is better) per session: ${formatRate(f.automated.sessions)}; per arrival: ${formatRate(f.automated.arrivals)}`);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (!result.sets.length)
|
|
271
|
+
lines.push("no labelled record");
|
|
272
|
+
lines.push("Each interval is a 95% Wilson interval. The arrivals of one session are not independent, so the interval per session is the one to read.");
|
|
273
|
+
return lines.join("\n");
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* A labelled set from a door's own verified receipts. An arrival that passed the challenge is `human`
|
|
277
|
+
* (`challenge-passed`); an arrival of a verified agent is `automated` (`verified-agent`); the rest have no label and
|
|
278
|
+
* are left out, as is a verified agent that passed the challenge. A record holds the label and the flags only. The session is a number: the door's source (the agent, else
|
|
279
|
+
* the address), cut where it is silent for `HISTORY_WINDOW_MS`. No path, address, agent name, header, or time leaves.
|
|
280
|
+
*
|
|
281
|
+
* Each record also holds the label in the fields of `door error-rates`: `kind`, `automated`, `genuine`, and `source`.
|
|
282
|
+
* `genuine` is always null: a receipt does not say whether an agent is the agent that it names, except through the
|
|
283
|
+
* door's own check, and that check is what `door error-rates` scores. The label of a verified agent is the door's own
|
|
284
|
+
* word (`source: "door"`), so its `validFor` holds the flags only and no question about `class` counts it. With
|
|
285
|
+
* `receiptIds` a record has its `receiptId`, which `door error-rates --labels` needs. The id names one receipt of the
|
|
286
|
+
* operator, so it is off unless asked for.
|
|
287
|
+
*/
|
|
288
|
+
export function labelsFromReceipts(predicates, set, opts = {}) {
|
|
289
|
+
if (!isName(set))
|
|
290
|
+
throw new Error("the set name is letters, digits, and . _ : / -, as door measure reads it");
|
|
291
|
+
const out = { records: [], read: 0, noSignals: 0, unlabelled: 0 };
|
|
292
|
+
const door = [...predicates].filter((p) => p.issuer.kind === "door").sort((a, b) => (a.timestamp < b.timestamp ? -1 : a.timestamp > b.timestamp ? 1 : 0));
|
|
293
|
+
const sessions = new Map();
|
|
294
|
+
let next = 1;
|
|
295
|
+
for (const p of door) {
|
|
296
|
+
out.read++;
|
|
297
|
+
const arrival = p.facts.arrival?.value;
|
|
298
|
+
if (!arrival?.fingerprint || !arrival.behaviour) {
|
|
299
|
+
out.noSignals++;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const source = arrival.class === "verified" ? `agent:${arrival.subject}` : `remote:${arrival.remote}`;
|
|
303
|
+
const at = Date.parse(p.timestamp);
|
|
304
|
+
let session = sessions.get(source);
|
|
305
|
+
if (!session || at - session.last > HISTORY_WINDOW_MS)
|
|
306
|
+
sessions.set(source, (session = { id: next++, last: at }));
|
|
307
|
+
session.last = at;
|
|
308
|
+
const passed = arrival.challenge?.outcome === "passed";
|
|
309
|
+
const verified = arrival.class === "verified";
|
|
310
|
+
// a verified agent that passed the challenge has both labels, so it has none
|
|
311
|
+
const label = passed === verified
|
|
312
|
+
? null
|
|
313
|
+
: passed
|
|
314
|
+
? { label: "human", labelSource: "challenge-passed", kind: "challenge-passed", automated: false, genuine: null, source: "challenge" }
|
|
315
|
+
: { label: "automated", labelSource: "verified-agent", kind: "verified-agent", automated: true, genuine: null, source: "door", validFor: [...FLAG_SIGNALS] };
|
|
316
|
+
if (!label) {
|
|
317
|
+
out.unlabelled++;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
// the door's fact does not say whether it received the header set; `count` is present only when it did
|
|
321
|
+
const flags = flagValues({ fingerprint: arrival.fingerprint, behaviour: arrival.behaviour }, arrival.fingerprint.headers.count !== undefined);
|
|
322
|
+
out.records.push({ ...(opts.receiptIds ? { receiptId: p.receiptId } : {}), set, session: `s${session.id}`, ...label, flags });
|
|
323
|
+
}
|
|
324
|
+
return out;
|
|
325
|
+
}
|