aegiscode 7.0.26 → 7.0.28

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.
Files changed (118) hide show
  1. package/SOURCE-OFFER.txt +9 -0
  2. package/package.json +4 -7
  3. package/scripts/check.mjs +1 -52
  4. package/scripts/demo.mjs +1 -94
  5. package/scripts/predist.mjs +1 -75
  6. package/scripts/runtests.mjs +1 -56
  7. package/scripts/seal-gate.mjs +1 -74
  8. package/src/agents.js +1 -139
  9. package/src/app.js +1 -2957
  10. package/src/art.js +1 -289
  11. package/src/chatflow.js +1 -3023
  12. package/src/checkpoint.js +1 -90
  13. package/src/clipboard.js +1 -424
  14. package/src/cloudsync.js +1 -528
  15. package/src/commands.js +1 -4089
  16. package/src/config.js +1 -581
  17. package/src/connect.js +1 -349
  18. package/src/credentials.js +1 -33
  19. package/src/deps.js +1 -232
  20. package/src/devrun.js +1 -187
  21. package/src/diff.js +1 -333
  22. package/src/diffview.js +1 -296
  23. package/src/engine.js +1 -297
  24. package/src/entitlement.js +1 -355
  25. package/src/events.js +1 -707
  26. package/src/export.js +1 -67
  27. package/src/filepreview.js +1 -153
  28. package/src/fingerprint.js +1 -592
  29. package/src/format.js +1 -259
  30. package/src/fuzzy.js +1 -119
  31. package/src/history.js +1 -423
  32. package/src/init.js +1 -175
  33. package/src/input.js +1 -139
  34. package/src/keys.js +1 -183
  35. package/src/markdown.js +1 -306
  36. package/src/models.js +1 -507
  37. package/src/naming.js +1 -57
  38. package/src/overlays.js +1 -462
  39. package/src/panels.js +1 -1418
  40. package/src/ping.js +1 -226
  41. package/src/presence.js +1 -238
  42. package/src/render.js +1 -516
  43. package/src/screen.js +1 -518
  44. package/src/screens.js +1 -896
  45. package/src/secret.js +1 -59
  46. package/src/securefs.js +1 -359
  47. package/src/settings.js +1 -155
  48. package/src/shared.js +1 -52
  49. package/src/sharedpaths.js +1 -53
  50. package/src/signup.js +1 -156
  51. package/src/summarize.js +1 -95
  52. package/src/sysdiag.js +1 -585
  53. package/src/system.js +1 -40
  54. package/src/theme.js +1 -570
  55. package/src/title.js +1 -220
  56. package/src/todos.js +1 -115
  57. package/src/tokens.js +1 -263
  58. package/src/topup.js +1 -210
  59. package/src/update.js +1 -191
  60. package/vendor/client/aegis.js +1 -2441
  61. package/vendor/client/attest.js +1 -518
  62. package/vendor/client/authorship-key.js +1 -410
  63. package/vendor/client/auto-provision.js +1 -134
  64. package/vendor/client/brain-catalog.js +1 -268
  65. package/vendor/client/byok-catalog.js +1 -199
  66. package/vendor/client/catalog-client.js +1 -487
  67. package/vendor/client/conversation-export.js +1 -177
  68. package/vendor/client/credentials.js +1 -494
  69. package/vendor/client/device-fingerprint.js +1 -141
  70. package/vendor/client/drive-connect.js +1 -110
  71. package/vendor/client/drive-upload.js +1 -234
  72. package/vendor/client/env-file.js +1 -1273
  73. package/vendor/client/fingerprint.js +1 -1064
  74. package/vendor/client/foreign-memory.js +1 -669
  75. package/vendor/client/google-signin.js +1 -133
  76. package/vendor/client/hooks.js +1 -430
  77. package/vendor/client/model-discovery.js +1 -373
  78. package/vendor/client/naming.js +1 -283
  79. package/vendor/client/platform.js +1 -937
  80. package/vendor/client/prefs.js +1 -351
  81. package/vendor/client/presence-constants.js +1 -107
  82. package/vendor/client/presence.js +1 -438
  83. package/vendor/client/record-vault.js +3 -1055
  84. package/vendor/client/session-store.js +1 -575
  85. package/vendor/client/topup.js +1 -297
  86. package/vendor/client/touch.js +1 -458
  87. package/vendor/client/update.js +1 -763
  88. package/vendor/client/user-models.js +1 -381
  89. package/vendor/desktop/lib/local/agents.js +1 -105
  90. package/vendor/desktop/lib/local/attachments.js +1 -276
  91. package/vendor/desktop/lib/local/autonomous.js +1 -1045
  92. package/vendor/desktop/lib/local/claude-pro.js +1 -1028
  93. package/vendor/desktop/lib/local/context.js +1 -125
  94. package/vendor/desktop/lib/local/engine.js +1 -3666
  95. package/vendor/desktop/lib/local/entitlement.js +1 -660
  96. package/vendor/desktop/lib/local/exec-plan.js +1 -269
  97. package/vendor/desktop/lib/local/git-scope.js +1 -228
  98. package/vendor/desktop/lib/local/integrity.js +1 -163
  99. package/vendor/desktop/lib/local/local.js +1 -2145
  100. package/vendor/desktop/lib/local/prompt.js +1 -280
  101. package/vendor/desktop/lib/local/queue.js +1 -560
  102. package/vendor/desktop/lib/local/session-rounds.js +1 -238
  103. package/vendor/desktop/lib/local/shell.js +1 -712
  104. package/vendor/desktop/lib/local/spill.js +1 -75
  105. package/vendor/desktop/lib/local/tools.js +1 -1328
  106. package/vendor/desktop/lib/local/turn-guard.js +1 -300
  107. package/vendor/desktop/lib/local/worktree-lock.js +1 -235
  108. package/vendor/desktop/lib/settings.js +1 -890
  109. package/vendor/desktop/main.js +1 -4890
  110. package/vendor/desktop/renderer/usage.js +1 -1239
  111. package/vendor/hosts/install.js +1 -638
  112. package/vendor/hosts/openai-shim.js +1 -515
  113. package/vendor/hosts/spec.js +1 -164
  114. package/vendor/hosts/targets.js +1 -346
  115. package/vendor/mcp/server.js +1 -221
  116. package/vendor/mcp/tools.js +1 -408
  117. package/vendor/one/src/child.js +1 -285
  118. package/vendor/one/src/permissions.js +1 -383
@@ -1,460 +1,3 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Copyright (C) 2026 Niklas Borneklint — AEGIS Code (aegiscode) / Aegiscloud.
3
- // AEGIS Code (aegiscode) — source: https://github.com/aegisinfo/aegiscode-plugin
4
- // Conveying a modified copy, or running one as a network service, owes the
5
- // corresponding source under this licence (§5/§13) — see LICENSING.md.
6
- 'use strict';
7
-
8
- /**
9
- * VENDORED — do not edit. Generated by tools/vendor-fingerprint.mjs.
10
- *
11
- * source: aegiscodex 079f445 — src/touch.js
12
- * rewrite: ESM -> CJS, module syntax only. No constant, purpose string,
13
- * attested field list or digest differs from the engine's.
14
- * verify: node --test test/fingerprint-interop.test.mjs
15
- * refresh: node tools/vendor-fingerprint.mjs
16
- */
17
- /**
18
- * touch.js — the thumb axis of φ(α): gesture dynamics from the trackpad.
19
- *
20
- * ## What this is, and what it is not
21
- *
22
- * It is NOT a fingerprint. This machine has no fingerprint sensor: `fprintd-list`
23
- * reports "No devices available", `lsusb` shows no imaging device, and the
24
- * trackpad (Apple `05ac:0253`, bcm5974) has no imager. A trackpad cannot image a
25
- * ridge pattern, so nothing here should ever be labelled `fingerprint`.
26
- *
27
- * It is also not the trackpad's *touch* data. The kernel does expose contact size
28
- * and force for this device (`ABS_MT_TOUCH_MAJOR`, pressure), but only through
29
- * `/dev/input/eventN`, which is `root:input` and this user is not in `input`.
30
- * Reachable without privilege through X, the trackpad exposes exactly four
31
- * valuators — `Rel X`, `Rel Y`, `Rel Horiz Scroll`, `Rel Vert Scroll`. There is
32
- * no absolute contact geometry in that list, and there is no `Touch` class, so
33
- * the pad cannot say where a finger is, how big it is, or how hard it presses.
34
- *
35
- * What is left — and it is real — is *how the thumb moves*: the timing, shape and
36
- * smoothness of a stroke. That is a behavioural biometric, the same family as
37
- * keystroke dynamics. It is stable enough to distinguish habitual users on one
38
- * device, and it is a much weaker claim than a fingerprint: it varies with
39
- * fatigue, moisture, posture and what else is on the pad, and it is *learned*,
40
- * not measured. `compareTouch` therefore returns a graded score, never a
41
- * boolean identity, and `THRESHOLD` is documented as a default rather than a
42
- * truth.
43
- *
44
- * ## Why the deltas are raw
45
- *
46
- * `Rel X`/`Rel Y` in a `Motion` event have already been through the X pointer
47
- * acceleration curve, which multiplies fast movement more than slow movement.
48
- * Speed is one of the features below, so acceleration would make the feature
49
- * partly a function of the acceleration profile rather than of the thumb.
50
- * `RawMotion` valuators are the device's own deltas, before acceleration, and
51
- * that is what `parseXi2` prefers when both are present.
52
- *
53
- * ## Why nothing here is a timestamp from X
54
- *
55
- * `xinput test-xi2` prints no event time, so the sampler stamps arrival time at
56
- * read. That makes every duration a *lower bound* with reader jitter on top.
57
- * Features are therefore built to survive tens of milliseconds of noise: they
58
- * are binned with explicit tolerances, and the median across strokes is what
59
- * gets hashed, so one mistimed stroke cannot swing the result.
60
- *
61
- * Pure: node:crypto only, no I/O, no spawn. The capture tool
62
- * (`tools/aegis-touchpad.mjs`) owns the process side and is the only file in
63
- * this axis allowed to touch it.
64
- */
65
-
66
- const crypto = require('node:crypto');
67
-
68
- /** Enough strokes for a median to mean something, few enough to ask of a thumb. */
69
- const MIN_STROKES = 3;
70
- const MAX_STROKES = 64;
71
- const MAX_SAMPLES = 20000;
72
-
73
- /** A gap this long with no motion ends a stroke. Motion bursts are all X gives us. */
74
- const DEFAULT_IDLE_GAP_MS = 140;
75
-
76
- /** Bump when the axis set or a tolerance changes: signatures are not portable across it. */
77
- const TOUCH_VERSION = 1;
78
-
79
- const SIG_LENGTH = 16;
80
-
81
- /**
82
- * One axis per habitual trait. `mode` decides how a value is binned:
83
- * 'linear' — absolute tolerance in the axis's own units
84
- * 'log' — multiplicative tolerance, for axes whose *scale* is device-specific
85
- * `tol` is the half-bin width; a value is quantised to `round(v / tol)`.
86
- */
87
- const AXES = Object.freeze([
88
- { name: 'samples', mode: 'linear', tol: 3 }, // how many events the stroke took
89
- { name: 'durationMs', mode: 'linear', tol: 150 }, // how long the thumb was moving
90
- { name: 'straightness', mode: 'linear', tol: 0.15 }, // net / path; 1 = dead straight
91
- { name: 'verticalBias', mode: 'linear', tol: 0.15 }, // |dy| / path
92
- { name: 'pauseRatio', mode: 'linear', tol: 0.15 }, // fraction of zero-delta samples
93
- { name: 'dtCv', mode: 'linear', tol: 0.15 }, // jitter of the sampling interval
94
- { name: 'turnMean', mode: 'linear', tol: 0.12 }, // mean turn per sample, radians
95
- { name: 'speedSkew', mode: 'linear', tol: 0.5 }, // peak / mean - 1
96
- { name: 'reversalRate', mode: 'linear', tol: 0.1 }, // direction changes per sample
97
- { name: 'pathPerSample', mode: 'log', tol: 1.25 }, // device-scale, so multiplicative
98
- ]);
99
-
100
- /**
101
- * Mean bin distance at or below which two profiles are called a match. One whole
102
- * bin of slop on every axis on average is a deliberately loose default: this is a
103
- * behavioural axis, and a tight threshold would report "different thumb" for the
104
- * same thumb on a cold hand. Tune per device; do not read it as an identity test.
105
- */
106
- const THRESHOLD = 1.0;
107
-
108
- const AXIS_NAMES = Object.freeze(AXES.map((a) => a.name));
109
-
110
- // ---------------------------------------------------------------------------
111
- // parsing
112
- // ---------------------------------------------------------------------------
113
-
114
- /**
115
- * Parse `xinput test-xi2 --root` output into motion samples.
116
- *
117
- * Two shapes are accepted. The block form is what XI2 prints:
118
- *
119
- * EVENT type 17 (RawMotion)
120
- * device: 9 (9)
121
- * valuators:
122
- * 0: -1.00 (0.00)
123
- * 1: 6.00 (0.00)
124
- *
125
- * and the flat form is what the older `xinput test <id>` prints:
126
- *
127
- * motion id 9 [9, 3] 0: -1 1: 0
128
- *
129
- * The leading device table that `test-xi2` prints at startup is skipped. Events
130
- * are stamped with arrival time: `now` is injectable so tests are deterministic.
131
- */
132
- function parseXi2(text, { now = () => Date.now(), device = null } = {}) {
133
- const samples = [];
134
- let current = null;
135
-
136
- const flush = () => {
137
- if (!current) return;
138
- const dv = current.valuators;
139
- if (dv[0] !== undefined || dv[1] !== undefined) {
140
- samples.push({
141
- type: current.type,
142
- device: current.device,
143
- dx: Number(dv[0] || 0),
144
- dy: Number(dv[1] || 0),
145
- t: current.t,
146
- });
147
- }
148
- current = null;
149
- };
150
-
151
- for (const rawLine of String(text).split('\n')) {
152
- const line = rawLine.replace(/\u001b\[[0-9;]*m/g, '');
153
-
154
- const ev = /^\s*EVENT\s+type\s+(\d+)\s*(?:\(([^)]*)\))?/.exec(line);
155
- if (ev) {
156
- flush();
157
- current = { type: Number(ev[1]), name: ev[2] || '', device: null, valuators: {}, t: now() };
158
- continue;
159
- }
160
-
161
- const dev = /^\s*device:\s*(\d+)/.exec(line);
162
- if (dev && current) {
163
- current.device = Number(dev[1]);
164
- continue;
165
- }
166
-
167
- if (current) {
168
- // " 0: -1.00 (0.00)" — index: value (raw value)
169
- const v = /^\s*(\d+):\s*(-?[\d.]+)\s*(?:\((-?[\d.]+)\))?\s*$/.exec(line);
170
- if (v) {
171
- current.valuators[Number(v[1])] = Number(v[2]);
172
- continue;
173
- }
174
- continue;
175
- }
176
-
177
- // flat form
178
- const flat = /^\s*(?:motion|raw motion)\s+id\s+(\d+)\s+\[[^\]]*\]\s*(.*)$/.exec(line);
179
- if (flat) {
180
- const dv = {};
181
- for (const pair of flat[2].matchAll(/(\d+):\s*(-?\d+)/g)) dv[Number(pair[1])] = Number(pair[2]);
182
- if (dv[0] !== undefined || dv[1] !== undefined) {
183
- samples.push({ type: 0, device: Number(flat[1]), dx: Number(dv[0] || 0), dy: Number(dv[1] || 0), t: now() });
184
- }
185
- }
186
- }
187
- flush();
188
-
189
- const filtered = device === null ? samples : samples.filter((s) => s.device === device);
190
- return filtered.slice(0, MAX_SAMPLES);
191
- }
192
-
193
- // ---------------------------------------------------------------------------
194
- // segmentation
195
- // ---------------------------------------------------------------------------
196
-
197
- /**
198
- * Split samples into strokes: a stroke is a run of samples with no gap longer
199
- * than `idleGapMs`. X gives us no finger-down/finger-up through the relative
200
- * pointer path, so a silence gap is the only boundary available — which is why
201
- * `aegis-touchpad.mjs` asks for *separate, deliberate* strokes with the thumb
202
- * lifted in between rather than one continuous scribble.
203
- */
204
- function segmentStrokes(samples, { idleGapMs = DEFAULT_IDLE_GAP_MS } = {}) {
205
- const strokes = [];
206
- let current = [];
207
- for (const s of samples) {
208
- if (current.length) {
209
- const gap = s.t - current[current.length - 1].t;
210
- if (gap > idleGapMs) {
211
- strokes.push(current);
212
- current = [];
213
- }
214
- }
215
- current.push(s);
216
- }
217
- if (current.length) strokes.push(current);
218
- return strokes.filter((s) => s.length > 1).slice(0, MAX_STROKES);
219
- }
220
-
221
- // ---------------------------------------------------------------------------
222
- // features
223
- // ---------------------------------------------------------------------------
224
-
225
- const mean = (xs) => (xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : 0);
226
-
227
- /**
228
- * Ten traits from one stroke. Every ratio is guarded against a zero denominator
229
- * and returns 0 rather than NaN: a NaN would poison canonicalisation downstream,
230
- * and a stroke that moved nowhere is better described as "no trait" than as a
231
- * crash.
232
- */
233
- function strokeFeatures(stroke) {
234
- const n = stroke.length;
235
- let path = 0;
236
- let dx = 0;
237
- let dy = 0;
238
- let pauses = 0;
239
- let turns = 0;
240
- let reversals = 0;
241
-
242
- const speeds = [];
243
- const dts = [];
244
-
245
- let prevSignX = 0;
246
- let prevSignY = 0;
247
- let prevAngle = null;
248
-
249
- for (let i = 0; i < n; i++) {
250
- const s = stroke[i];
251
- const mag = Math.hypot(s.dx, s.dy);
252
- path += mag;
253
- dx += s.dx;
254
- dy += s.dy;
255
- if (mag === 0) pauses++;
256
-
257
- if (i > 0) {
258
- const dt = Math.max(1, stroke[i].t - stroke[i - 1].t);
259
- dts.push(dt);
260
- speeds.push(mag / dt);
261
- const angle = Math.atan2(s.dy, s.dx);
262
- if (prevAngle !== null) {
263
- let d = angle - prevAngle;
264
- while (d > Math.PI) d -= 2 * Math.PI;
265
- while (d < -Math.PI) d += 2 * Math.PI;
266
- turns += Math.abs(d);
267
- }
268
- prevAngle = angle;
269
- }
270
-
271
- const sx = Math.sign(s.dx);
272
- const sy = Math.sign(s.dy);
273
- if (sx && prevSignX && sx !== prevSignX) reversals++;
274
- if (sy && prevSignY && sy !== prevSignY) reversals++;
275
- if (sx) prevSignX = sx;
276
- if (sy) prevSignY = sy;
277
- }
278
-
279
- const net = Math.hypot(dx, dy);
280
- const duration = Math.max(1, stroke[n - 1].t - stroke[0].t);
281
- const meanSpeed = mean(speeds);
282
- const dtMean = mean(dts);
283
- const dtVar = mean(dts.map((d) => (d - dtMean) ** 2));
284
-
285
- return {
286
- samples: n,
287
- durationMs: duration,
288
- straightness: path ? net / path : 0,
289
- verticalBias: path ? Math.abs(dy) / path : 0,
290
- pauseRatio: pauses / n,
291
- dtCv: dtMean ? Math.sqrt(dtVar) / dtMean : 0,
292
- turnMean: n > 1 ? turns / (n - 1) : 0,
293
- speedSkew: meanSpeed ? Math.max(...speeds) / meanSpeed - 1 : 0,
294
- reversalRate: reversals / n,
295
- pathPerSample: path / n,
296
- };
297
- }
298
-
299
- // ---------------------------------------------------------------------------
300
- // quantisation
301
- // ---------------------------------------------------------------------------
302
-
303
- /** Bin one value. `log` mode is symmetric in ratio, so 2x and 0.5x are equidistant. */
304
- function binValue(axis, value) {
305
- const v = Number.isFinite(value) ? value : 0;
306
- if (axis.mode === 'log') {
307
- if (v <= 0) return 0;
308
- return Math.round(Math.log(v) / Math.log(axis.tol));
309
- }
310
- return Math.round(v / axis.tol);
311
- }
312
-
313
- /** Distance between two bins, in bin units, on one axis. */
314
- function binDistance(axis, a, b) {
315
- if (axis.mode === 'log') {
316
- const ratio = Math.log(axis.tol);
317
- const ra = a * ratio;
318
- const rb = b * ratio;
319
- return Math.abs(ra - rb);
320
- }
321
- return Math.abs(a - b);
322
- }
323
-
324
- /** Quantise a feature object to integers, axis by axis. Unknown axes are ignored. */
325
- function quantise(features) {
326
- const out = {};
327
- for (const axis of AXES) out[axis.name] = binValue(axis, features[axis.name]);
328
- return out;
329
- }
330
-
331
- /** Median across strokes per axis: order-independent, and robust to one bad stroke. */
332
- function medians(featureList) {
333
- const out = {};
334
- for (const axis of AXES) {
335
- const xs = featureList.map((f) => Number(f[axis.name]) || 0).sort((a, b) => a - b);
336
- const mid = Math.floor(xs.length / 2);
337
- const med = xs.length % 2 ? xs[mid] : (xs[mid - 1] + xs[mid]) / 2;
338
- out[axis.name] = Number.isFinite(med) ? med : 0;
339
- }
340
- return out;
341
- }
342
-
343
- /**
344
- * The signature body: versioned, one `axis=bin` line per trait, fixed order.
345
- * Exported shape is a hash, but this is the thing hashed, and it is what a
346
- * verifier recomputes.
347
- */
348
- function signatureBody(med) {
349
- const bins = quantise(med);
350
- return TOUCH_VERSION + '\n' + AXIS_NAMES.map((k) => `${k}=${bins[k]}`).join('\n');
351
- }
352
-
353
- /**
354
- * 16 hex over the median-binned trait vector, recomputed from the medians.
355
- *
356
- * This is the function `src/fingerprint.js` calls, and it takes medians rather
357
- * than a stored hash on purpose: a profile file's own `signature` field is then
358
- * never trusted, only recomputed. Editing `medians` in that file changes the
359
- * signature and the record's attestation breaks, which is the same property the
360
- * rest of φ(α) has.
361
- *
362
- * `strokeCount` is what makes absence honest — see rule 3.
363
- *
364
- * @returns {string|null} null below `MIN_STROKES`: the median of one stroke is
365
- * just that stroke, and one gesture is not evidence of a habit.
366
- */
367
- function signatureFromMedians(med, strokeCount) {
368
- if (!med || Number(strokeCount) < MIN_STROKES) return null;
369
- return crypto.createHash('sha256').update(signatureBody(med), 'utf8').digest('hex').slice(0, SIG_LENGTH);
370
- }
371
-
372
- /**
373
- * The thumb signature over a list of stroke features.
374
- *
375
- * Median across strokes makes this order-independent — the same thumb producing
376
- * the same habits in a different order fingerprints identically, matching the
377
- * rule the behavioural axis already follows.
378
- */
379
- function touchSignature(featureList) {
380
- const usable = (Array.isArray(featureList) ? featureList : [])
381
- .filter((f) => f && Number.isFinite(Number(f.samples)) && Number(f.samples) > 1)
382
- .slice(0, MAX_STROKES);
383
- if (usable.length < MIN_STROKES) return null;
384
- return signatureFromMedians(medians(usable), usable.length);
385
- }
386
-
387
- // ---------------------------------------------------------------------------
388
- // profile + comparison
389
- // ---------------------------------------------------------------------------
390
-
391
- /**
392
- * A full touch profile: the signature, the medians it came from, and the axis
393
- * table with tolerances — so a verifier can recompute rather than trust, and a
394
- * reader can see which axes were even in play.
395
- */
396
- function profile(samples, opts = {}) {
397
- const strokes = segmentStrokes(samples, opts);
398
- const featureList = strokes.map(strokeFeatures);
399
- const signature = touchSignature(featureList);
400
- const med = featureList.length ? medians(featureList) : null;
401
-
402
- return Object.freeze({
403
- version: TOUCH_VERSION,
404
- kind: 'gesture-dynamics',
405
- notice:
406
- 'not a fingerprint: this machine has no fingerprint sensor, and the trackpad ' +
407
- 'exposes no contact geometry through X. These are habits of motion, learned not measured.',
408
- signature,
409
- samples: featureList.length,
410
- strokes: strokes.length,
411
- medians: med ? Object.freeze({ ...med }) : null,
412
- bins: med ? Object.freeze(quantise(med)) : null,
413
- axes: AXES,
414
- });
415
- }
416
-
417
- /**
418
- * Graded comparison, never a bare boolean.
419
- *
420
- * @returns {{score:number, withinTolerance:boolean, distance:number, worst:Array, comparable:boolean}}
421
- * `comparable` is false when either side has no signature — two absences are
422
- * not a match, and saying so is the point.
423
- */
424
- function compareTouch(a, b, { threshold = THRESHOLD } = {}) {
425
- const empty = { score: 0, withinTolerance: false, distance: Infinity, worst: [], comparable: false };
426
- if (!a || !b) return empty;
427
- const ba = a.bins || (a.medians ? quantise(a.medians) : null);
428
- const bb = b.bins || (b.medians ? quantise(b.medians) : null);
429
- if (!ba || !bb) return empty;
430
-
431
- const worst = [];
432
- let total = 0;
433
- let count = 0;
434
- for (const axis of AXES) {
435
- if (ba[axis.name] === undefined || bb[axis.name] === undefined) continue;
436
- const d = binDistance(axis, ba[axis.name], bb[axis.name]);
437
- total += d;
438
- count++;
439
- worst.push({ axis: axis.name, bins: d, a: ba[axis.name], b: bb[axis.name] });
440
- }
441
- if (!count) return empty;
442
- const distance = total / count;
443
- worst.sort((x, y) => y.bins - x.bins);
444
- return {
445
- score: Number((1 / (1 + distance)).toFixed(4)),
446
- withinTolerance: distance <= threshold,
447
- distance: Number(distance.toFixed(4)),
448
- worst: worst.slice(0, 3),
449
- comparable: true,
450
- };
451
- }
452
-
453
-
454
-
455
- module.exports = {
456
- parseXi2, segmentStrokes, strokeFeatures, quantise, medians, touchSignature,
457
- signatureBody, signatureFromMedians, profile, compareTouch, binValue, binDistance,
458
- AXES, AXIS_NAMES, TOUCH_VERSION, SIG_LENGTH, MIN_STROKES, MAX_STROKES,
459
- DEFAULT_IDLE_GAP_MS, THRESHOLD,
460
- };
3
+ "use strict";const crypto=require("node:crypto"),MIN_STROKES=3,MAX_STROKES=64,MAX_SAMPLES=2e4,DEFAULT_IDLE_GAP_MS=140,TOUCH_VERSION=1,SIG_LENGTH=16,AXES=Object.freeze([{name:"samples",mode:"linear",tol:3},{name:"durationMs",mode:"linear",tol:150},{name:"straightness",mode:"linear",tol:.15},{name:"verticalBias",mode:"linear",tol:.15},{name:"pauseRatio",mode:"linear",tol:.15},{name:"dtCv",mode:"linear",tol:.15},{name:"turnMean",mode:"linear",tol:.12},{name:"speedSkew",mode:"linear",tol:.5},{name:"reversalRate",mode:"linear",tol:.1},{name:"pathPerSample",mode:"log",tol:1.25}]),THRESHOLD=1,AXIS_NAMES=Object.freeze(AXES.map((e=>e.name)));function parseXi2(e,{now:t=(()=>Date.now()),device:n=null}={}){const s=[];let a=null;const o=()=>{if(!a)return;const e=a.valuators;void 0===e[0]&&void 0===e[1]||s.push({type:a.type,device:a.device,dx:Number(e[0]||0),dy:Number(e[1]||0),t:a.t}),a=null};for(const n of String(e).split("\n")){const e=n.replace(/\u001b\[[0-9;]*m/g,""),r=/^\s*EVENT\s+type\s+(\d+)\s*(?:\(([^)]*)\))?/.exec(e);if(r){o(),a={type:Number(r[1]),name:r[2]||"",device:null,valuators:{},t:t()};continue}const i=/^\s*device:\s*(\d+)/.exec(e);if(i&&a){a.device=Number(i[1]);continue}if(a){const t=/^\s*(\d+):\s*(-?[\d.]+)\s*(?:\((-?[\d.]+)\))?\s*$/.exec(e);if(t){a.valuators[Number(t[1])]=Number(t[2]);continue}continue}const u=/^\s*(?:motion|raw motion)\s+id\s+(\d+)\s+\[[^\]]*\]\s*(.*)$/.exec(e);if(u){const e={};for(const t of u[2].matchAll(/(\d+):\s*(-?\d+)/g))e[Number(t[1])]=Number(t[2]);void 0===e[0]&&void 0===e[1]||s.push({type:0,device:Number(u[1]),dx:Number(e[0]||0),dy:Number(e[1]||0),t:t()})}}return o(),(null===n?s:s.filter((e=>e.device===n))).slice(0,2e4)}function segmentStrokes(e,{idleGapMs:t=DEFAULT_IDLE_GAP_MS}={}){const n=[];let s=[];for(const a of e)s.length&&a.t-s[s.length-1].t>t&&(n.push(s),s=[]),s.push(a);return s.length&&n.push(s),n.filter((e=>e.length>1)).slice(0,MAX_STROKES)}const mean=e=>e.length?e.reduce(((e,t)=>e+t),0)/e.length:0;function strokeFeatures(e){const t=e.length;let n=0,s=0,a=0,o=0,r=0,i=0;const u=[],l=[];let c=0,m=0,d=null;for(let h=0;h<t;h++){const t=e[h],S=Math.hypot(t.dx,t.dy);if(n+=S,s+=t.dx,a+=t.dy,0===S&&o++,h>0){const n=Math.max(1,e[h].t-e[h-1].t);l.push(n),u.push(S/n);const s=Math.atan2(t.dy,t.dx);if(null!==d){let e=s-d;for(;e>Math.PI;)e-=2*Math.PI;for(;e<-Math.PI;)e+=2*Math.PI;r+=Math.abs(e)}d=s}const f=Math.sign(t.dx),p=Math.sign(t.dy);f&&c&&f!==c&&i++,p&&m&&p!==m&&i++,f&&(c=f),p&&(m=p)}const h=Math.hypot(s,a),S=Math.max(1,e[t-1].t-e[0].t),f=mean(u),p=mean(l),M=mean(l.map((e=>(e-p)**2)));return{samples:t,durationMs:S,straightness:n?h/n:0,verticalBias:n?Math.abs(a)/n:0,pauseRatio:o/t,dtCv:p?Math.sqrt(M)/p:0,turnMean:t>1?r/(t-1):0,speedSkew:f?Math.max(...u)/f-1:0,reversalRate:i/t,pathPerSample:n/t}}function binValue(e,t){const n=Number.isFinite(t)?t:0;return"log"===e.mode?n<=0?0:Math.round(Math.log(n)/Math.log(e.tol)):Math.round(n/e.tol)}function binDistance(e,t,n){if("log"===e.mode){const s=Math.log(e.tol),a=t*s,o=n*s;return Math.abs(a-o)}return Math.abs(t-n)}function quantise(e){const t={};for(const n of AXES)t[n.name]=binValue(n,e[n.name]);return t}function medians(e){const t={};for(const n of AXES){const s=e.map((e=>Number(e[n.name])||0)).sort(((e,t)=>e-t)),a=Math.floor(s.length/2),o=s.length%2?s[a]:(s[a-1]+s[a])/2;t[n.name]=Number.isFinite(o)?o:0}return t}function signatureBody(e){const t=quantise(e);return TOUCH_VERSION+"\n"+AXIS_NAMES.map((e=>`${e}=${t[e]}`)).join("\n")}function signatureFromMedians(e,t){return!e||Number(t)<MIN_STROKES?null:crypto.createHash("sha256").update(signatureBody(e),"utf8").digest("hex").slice(0,SIG_LENGTH)}function touchSignature(e){const t=(Array.isArray(e)?e:[]).filter((e=>e&&Number.isFinite(Number(e.samples))&&Number(e.samples)>1)).slice(0,MAX_STROKES);return t.length<MIN_STROKES?null:signatureFromMedians(medians(t),t.length)}function profile(e,t={}){const n=segmentStrokes(e,t),s=n.map(strokeFeatures),a=touchSignature(s),o=s.length?medians(s):null;return Object.freeze({version:TOUCH_VERSION,kind:"gesture-dynamics",notice:"not a fingerprint: this machine has no fingerprint sensor, and the trackpad exposes no contact geometry through X. These are habits of motion, learned not measured.",signature:a,samples:s.length,strokes:n.length,medians:o?Object.freeze({...o}):null,bins:o?Object.freeze(quantise(o)):null,axes:AXES})}function compareTouch(e,t,{threshold:n=1}={}){const s={score:0,withinTolerance:!1,distance:1/0,worst:[],comparable:!1};if(!e||!t)return s;const a=e.bins||(e.medians?quantise(e.medians):null),o=t.bins||(t.medians?quantise(t.medians):null);if(!a||!o)return s;const r=[];let i=0,u=0;for(const e of AXES){if(void 0===a[e.name]||void 0===o[e.name])continue;const t=binDistance(e,a[e.name],o[e.name]);i+=t,u++,r.push({axis:e.name,bins:t,a:a[e.name],b:o[e.name]})}if(!u)return s;const l=i/u;return r.sort(((e,t)=>t.bins-e.bins)),{score:Number((1/(1+l)).toFixed(4)),withinTolerance:l<=n,distance:Number(l.toFixed(4)),worst:r.slice(0,3),comparable:!0}}module.exports={parseXi2:parseXi2,segmentStrokes:segmentStrokes,strokeFeatures:strokeFeatures,quantise:quantise,medians:medians,touchSignature:touchSignature,signatureBody:signatureBody,signatureFromMedians:signatureFromMedians,profile:profile,compareTouch:compareTouch,binValue:binValue,binDistance:binDistance,AXES:AXES,AXIS_NAMES:AXIS_NAMES,TOUCH_VERSION:TOUCH_VERSION,SIG_LENGTH:SIG_LENGTH,MIN_STROKES:MIN_STROKES,MAX_STROKES:MAX_STROKES,DEFAULT_IDLE_GAP_MS:DEFAULT_IDLE_GAP_MS,THRESHOLD:1};