@tpsdev-ai/flair 0.57.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/SECURITY.md +4 -1
- package/config.yaml +3 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1726 -376
- package/dist/commands/agent.js +160 -2
- package/dist/commands/doctor.js +177 -27
- package/dist/commands/federation.js +2 -1
- package/dist/commands/hook.js +10 -2
- package/dist/commands/init.js +150 -44
- package/dist/commands/mcp.js +53 -22
- package/dist/commands/memory.js +24 -2
- package/dist/commands/quality.js +26 -18
- package/dist/commands/service.js +137 -22
- package/dist/commands/status.js +114 -3
- package/dist/commands/upgrade.js +204 -84
- package/dist/doctor-client.js +50 -4
- package/dist/federation/scheduler.js +253 -4
- package/dist/install/clients.js +89 -23
- package/dist/install/global-bin-path.js +26 -6
- package/dist/lib/atomic-write.js +256 -0
- package/dist/lib/daemon-liveness.js +103 -0
- package/dist/lib/doctor-run.js +72 -0
- package/dist/lib/embed-gpu-doctor.js +49 -26
- package/dist/lib/federation-memory-attributes.js +15 -0
- package/dist/lib/flair-agent-role.js +116 -0
- package/dist/lib/init-admin-pass.js +4 -2
- package/dist/lib/init-listener-environ.js +38 -0
- package/dist/lib/init-occupied-listener.js +165 -0
- package/dist/lib/launchd-domain-preflight.js +448 -0
- package/dist/lib/launchd-management.js +179 -8
- package/dist/lib/launchd-repair.js +95 -0
- package/dist/lib/mcp-enable.js +667 -72
- package/dist/lib/memory-attributes.js +77 -0
- package/dist/lib/node-alias-path.js +95 -0
- package/dist/lib/npm-registry.js +119 -6
- package/dist/lib/owned-pins.js +20 -6
- package/dist/lib/process-start-time.js +32 -0
- package/dist/lib/service-repoint-apply.js +267 -0
- package/dist/lib/service-repoint.js +575 -0
- package/dist/lib/toml-string-array.js +104 -0
- package/dist/lib/tree-divergence.js +716 -0
- package/dist/lib/upgrade-plain-tree.js +18 -8
- package/dist/lib/upgrade-prior-liveness.js +69 -0
- package/dist/lib/upgrade-rollback.js +116 -0
- package/dist/rem/runner.js +11 -0
- package/dist/resources/Agent.js +54 -21
- package/dist/resources/AgentSeed.js +44 -1
- package/dist/resources/Federation.js +84 -54
- package/dist/resources/Memory.js +567 -72
- package/dist/resources/MemoryArchive.js +9 -0
- package/dist/resources/MemoryBootstrap.js +125 -29
- package/dist/resources/MemoryFeed.js +204 -3
- package/dist/resources/MemoryHostSource.js +90 -0
- package/dist/resources/MemoryMaintenance.js +121 -32
- package/dist/resources/MemoryReflect.js +4 -1
- package/dist/resources/MemoryReindex.js +7 -4
- package/dist/resources/MemoryUsage.js +53 -3
- package/dist/resources/OrgSkillAssignment.js +209 -0
- package/dist/resources/Presence.js +18 -10
- package/dist/resources/RecordUsage.js +30 -40
- package/dist/resources/Relationship.js +167 -78
- package/dist/resources/SemanticSearch.js +19 -6
- package/dist/resources/Soul.js +31 -19
- package/dist/resources/XAA.js +47 -14
- package/dist/resources/agent-auth.js +16 -10
- package/dist/resources/agent-status-guard.js +63 -0
- package/dist/resources/auth-middleware.js +137 -49
- package/dist/resources/auto-promote-lib.js +4 -4
- package/dist/resources/bm25-index-service.js +246 -32
- package/dist/resources/bm25-status.js +132 -0
- package/dist/resources/ed25519-auth.js +11 -54
- package/dist/resources/embed-gpu.js +262 -47
- package/dist/resources/embeddings-boot.js +52 -17
- package/dist/resources/federation-classify.js +34 -0
- package/dist/resources/federation-crypto.js +65 -19
- package/dist/resources/flair-agent-role-boot.js +26 -0
- package/dist/resources/health.js +67 -16
- package/dist/resources/host-pointer/registry.js +35 -0
- package/dist/resources/host-pointer-adapter.js +15 -0
- package/dist/resources/host-source-visibility.js +167 -0
- package/dist/resources/host-source.js +151 -0
- package/dist/resources/instance-identity.js +3 -2
- package/dist/resources/key-lock.js +31 -0
- package/dist/resources/mcp-handler.js +147 -51
- package/dist/resources/mcp-oauth.js +3 -6
- package/dist/resources/mcp-tools.js +47 -25
- package/dist/resources/memory-consolidate-lib.js +3 -1
- package/dist/resources/memory-declared-attributes.js +103 -0
- package/dist/resources/memory-feed-lib.js +5 -1
- package/dist/resources/memory-host-source.js +163 -0
- package/dist/resources/migrations/synthetic-test-migration.js +4 -1
- package/dist/resources/migrations/visibility-backfill.js +4 -1
- package/dist/resources/multi-worker-guard.js +121 -0
- package/dist/resources/oauth-discovery.js +1 -0
- package/dist/resources/oauth-wellknown.js +8 -4
- package/dist/resources/originator-instance.js +235 -0
- package/dist/resources/promotion-stamp.js +2 -0
- package/dist/resources/provenance.js +111 -11
- package/dist/resources/record-owner-guard.js +19 -0
- package/dist/resources/record-type-kit.js +9 -12
- package/dist/resources/record-types.js +22 -6
- package/dist/resources/relay-ops.js +28 -3
- package/dist/resources/replay-store.js +336 -0
- package/dist/resources/request-transaction.js +78 -0
- package/dist/resources/search-readiness.js +19 -21
- package/dist/resources/semantic-retrieval-core.js +5 -1
- package/dist/resources/skill-manifest.js +181 -0
- package/dist/resources/skill-provenance.js +15 -10
- package/dist/resources/table-patch-policy.js +109 -0
- package/dist/resources/table-patches.js +17 -0
- package/dist/resources/table-post-policy.js +138 -0
- package/dist/resources/table-posts.js +22 -0
- package/dist/resources/table-subscription-policy.js +135 -0
- package/dist/resources/table-subscriptions.js +19 -0
- package/dist/resources/tool-descriptors/index.js +15 -15
- package/dist/resources/trust-block.js +10 -3
- package/dist/resources/usage-recording.js +169 -41
- package/dist/src/lib/flair-agent-role.js +116 -0
- package/dist/src/lib/memory-attributes.js +77 -0
- package/docs/access-control.md +667 -0
- package/docs/api-reference.md +85 -17
- package/docs/auth.md +4 -0
- package/docs/claude-code.md +132 -0
- package/docs/deepseek-harness.md +1 -1
- package/docs/deployment.md +1 -1
- package/docs/federation.md +1 -1
- package/docs/integrations.md +6 -6
- package/docs/mcp-clients.md +16 -24
- package/docs/notes/mcp-oauth-model2.md +15 -1
- package/docs/quickstart.md +5 -0
- package/docs/releasing.md +152 -20
- package/docs/supply-chain-policy.md +10 -5
- package/docs/troubleshooting.md +11 -0
- package/docs/upgrade.md +187 -61
- package/package.json +10 -5
- package/schemas/agent.graphql +9 -6
- package/schemas/federation.graphql +2 -11
- package/schemas/memory.graphql +137 -17
- package/schemas/oauth.graphql +8 -4
- package/schemas/replay.graphql +18 -0
- package/templates/launchd/start-flair-with-admin-pass.sh +95 -0
- package/dist/resources/federation-nonce-store.js +0 -195
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Replay store shared by agent auth (TPS-Ed25519) and federation body
|
|
2
|
+
# signatures (flair#2061). One row per accepted nonce, written once by
|
|
3
|
+
# resources/replay-store.ts under a per-key lock, and read by every Harper
|
|
4
|
+
# worker thread of this instance:
|
|
5
|
+
# a:<agentId>:<nonce> agent auth
|
|
6
|
+
# f:<nonce> federation
|
|
7
|
+
#
|
|
8
|
+
# replicate: false — local to this instance.
|
|
9
|
+
# expiration: 120 — seconds a row is kept. Harper's expiration scan runs once
|
|
10
|
+
# per instance (on the last worker) and removes a row no earlier than this
|
|
11
|
+
# after it was written. It MUST equal REPLAY_RETENTION_S in
|
|
12
|
+
# resources/replay-store.ts and stay longer than twice every replay window
|
|
13
|
+
# (30 s each today); test/unit-isolated/replay-store.test.ts pins both.
|
|
14
|
+
# Internal bookkeeping only: no @export, never served.
|
|
15
|
+
type ReplayNonce @table(database: "flair", replicate: false, expiration: 120) {
|
|
16
|
+
id: ID @primaryKey # scope-prefixed key (see above)
|
|
17
|
+
seenAt: Long! # ms-epoch when recorded
|
|
18
|
+
}
|
|
@@ -25,6 +25,101 @@ ADMIN_PASS_FILE="$1"
|
|
|
25
25
|
NODE="$2"
|
|
26
26
|
HARPER_BIN="$3"
|
|
27
27
|
|
|
28
|
+
# flair#2056: is "$1", the command line `ps -o command=` reports (arguments
|
|
29
|
+
# joined by spaces), Harper-shaped? Its first word's basename is `node` or
|
|
30
|
+
# `bun`, and its second word is a Harper entry path:
|
|
31
|
+
# `…/node_modules/harper/dist/bin/harper.js`,
|
|
32
|
+
# `…/node_modules/@<scope>/harper/dist/bin/harper.js` or `dist/bin/harper.js`
|
|
33
|
+
# (what Harper's own restart forks). A second word starting with `-`, or a
|
|
34
|
+
# Harper path in a later word, does not match. Because ps joins arguments with
|
|
35
|
+
# spaces, this does not establish which argument is the script: an argument
|
|
36
|
+
# containing a space can produce a matching line. Nor does it show that the
|
|
37
|
+
# process serves anything. The CLI's check is isHarperProcessCommandLine in
|
|
38
|
+
# src/lib/daemon-liveness.ts.
|
|
39
|
+
is_harper_command() {
|
|
40
|
+
HC_LINE="$1"
|
|
41
|
+
HC_LINE="${HC_LINE#"${HC_LINE%%[! ]*}"}"
|
|
42
|
+
case "$HC_LINE" in *" "*) ;; *) return 1 ;; esac
|
|
43
|
+
HC_EXE="${HC_LINE%% *}"
|
|
44
|
+
HC_REST="${HC_LINE#* }"
|
|
45
|
+
HC_REST="${HC_REST#"${HC_REST%%[! ]*}"}"
|
|
46
|
+
HC_SCRIPT="${HC_REST%% *}"
|
|
47
|
+
case "${HC_EXE##*/}" in node|bun) ;; *) return 1 ;; esac
|
|
48
|
+
case "$HC_SCRIPT" in
|
|
49
|
+
-*) return 1 ;;
|
|
50
|
+
dist/bin/harper.js) return 0 ;;
|
|
51
|
+
*/harper/dist/bin/harper.js) ;;
|
|
52
|
+
*) return 1 ;;
|
|
53
|
+
esac
|
|
54
|
+
HC_DIR="${HC_SCRIPT%/harper/dist/bin/harper.js}"
|
|
55
|
+
case "$HC_DIR" in node_modules|*/node_modules) return 0 ;; esac
|
|
56
|
+
HC_SCOPE="${HC_DIR##*/}"
|
|
57
|
+
HC_DIR="${HC_DIR%/*}"
|
|
58
|
+
case "$HC_SCOPE" in @?*) ;; *) return 1 ;; esac
|
|
59
|
+
case "$HC_DIR" in node_modules|*/node_modules) return 0 ;; esac
|
|
60
|
+
return 1
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
# flair#2040 / flair#2056: do not start a second Harper on a data directory
|
|
64
|
+
# whose hdb.pid names a live process with a Harper-shaped command line (see
|
|
65
|
+
# is_harper_command above for what that does and does not establish). `kill -0`
|
|
66
|
+
# alone proves only that SOME process has the pid: if a direct process dies and
|
|
67
|
+
# leaves hdb.pid behind, another process can later get that pid, and a refusal
|
|
68
|
+
# on `kill -0` alone would make every KeepAlive retry exit 0 without ever
|
|
69
|
+
# starting Flair.
|
|
70
|
+
#
|
|
71
|
+
# So the launcher refuses only when the command line `ps -o command=` reports
|
|
72
|
+
# for the live pid is Harper-shaped. A flair#1454 sidecar
|
|
73
|
+
# (`flair-daemon.json`) is not required: an instance started by a pre-sidecar
|
|
74
|
+
# flair, or the instance this launcher execs (Harper writes no sidecar), has
|
|
75
|
+
# none. A sidecar disagrees when it names a different pid, or a startTimeMs more
|
|
76
|
+
# than 2000 ms from the start second `ps -o lstart=` reports (ps reports whole
|
|
77
|
+
# seconds, so a process started at 12.9 s is compared as 12 s). When the
|
|
78
|
+
# sidecar disagrees, the launcher does not refuse and continues its normal
|
|
79
|
+
# start.
|
|
80
|
+
#
|
|
81
|
+
# Exit 0 on the refusal: a deliberate no-op, not a crash. KeepAlive retries
|
|
82
|
+
# after its throttle interval; once that process has exited, a retry does not
|
|
83
|
+
# refuse.
|
|
84
|
+
if [ -n "${ROOTPATH:-}" ] && [ -f "$ROOTPATH/hdb.pid" ]; then
|
|
85
|
+
LIVE_PID="$(tr -cd '0-9' < "$ROOTPATH/hdb.pid" 2>/dev/null || true)"
|
|
86
|
+
if [ -n "$LIVE_PID" ] && [ "$LIVE_PID" != "$$" ] && [ "$LIVE_PID" -gt 1 ] 2>/dev/null && kill -0 "$LIVE_PID" 2>/dev/null; then
|
|
87
|
+
DISAGREE=0
|
|
88
|
+
SIDE="$ROOTPATH/flair-daemon.json"
|
|
89
|
+
if [ -f "$SIDE" ]; then
|
|
90
|
+
SIDE_PID="$(grep -o '"pid"[[:space:]]*:[[:space:]]*[0-9][0-9]*' "$SIDE" 2>/dev/null | grep -o '[0-9][0-9]*$' || true)"
|
|
91
|
+
SIDE_START="$(grep -o '"startTimeMs"[[:space:]]*:[[:space:]]*[0-9][0-9]*' "$SIDE" 2>/dev/null | grep -o '[0-9][0-9]*$' || true)"
|
|
92
|
+
# Shell arithmetic reads a leading 0 as octal: drop leading zeros, and
|
|
93
|
+
# leave the sidecar unused when nothing is left or more than 15 digits are.
|
|
94
|
+
SIDE_START="${SIDE_START#"${SIDE_START%%[!0]*}"}"
|
|
95
|
+
case "$SIDE_START" in ????????????????*) SIDE_START="" ;; esac
|
|
96
|
+
if [ -n "$SIDE_PID" ] && [ -n "$SIDE_START" ]; then
|
|
97
|
+
if [ "$SIDE_PID" != "$LIVE_PID" ]; then
|
|
98
|
+
DISAGREE=1
|
|
99
|
+
else
|
|
100
|
+
LSTART="$(ps -o lstart= -p "$LIVE_PID" 2>/dev/null || true)"
|
|
101
|
+
ACT_S=""
|
|
102
|
+
case "$(uname -s)" in
|
|
103
|
+
Darwin) if [ -n "$LSTART" ]; then ACT_S="$(date -j -f "%a %b %e %T %Y" "$LSTART" +%s 2>/dev/null || true)"; fi ;;
|
|
104
|
+
*) if [ -n "$LSTART" ]; then ACT_S="$(date -d "$LSTART" +%s 2>/dev/null || true)"; fi ;;
|
|
105
|
+
esac
|
|
106
|
+
if [ -n "$ACT_S" ]; then
|
|
107
|
+
# Milliseconds against the start second in milliseconds: a start
|
|
108
|
+
# second of 12 s against a sidecar of 14.5 s is 2500 ms.
|
|
109
|
+
DIFF_MS=$((SIDE_START - ACT_S * 1000))
|
|
110
|
+
if [ "$DIFF_MS" -lt 0 ]; then DIFF_MS=$((0 - DIFF_MS)); fi
|
|
111
|
+
if [ "$DIFF_MS" -gt 2000 ]; then DISAGREE=1; fi
|
|
112
|
+
fi
|
|
113
|
+
fi
|
|
114
|
+
fi
|
|
115
|
+
fi
|
|
116
|
+
if [ "$DISAGREE" = "0" ] && is_harper_command "$(ps -o command= -p "$LIVE_PID" 2>/dev/null || true)"; then
|
|
117
|
+
echo "start-flair-with-admin-pass: $ROOTPATH/hdb.pid names pid $LIVE_PID, whose command line as ps reports it is node or bun followed by a Harper entry path; not starting a second instance. launchd retries after its throttle interval." >&2
|
|
118
|
+
exit 0
|
|
119
|
+
fi
|
|
120
|
+
fi
|
|
121
|
+
fi
|
|
122
|
+
|
|
28
123
|
if [ ! -f "$ADMIN_PASS_FILE" ]; then
|
|
29
124
|
echo "start-flair-with-admin-pass: admin-pass file not found: $ADMIN_PASS_FILE" >&2
|
|
30
125
|
exit 1
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* federation-nonce-store.ts — persistent backing for the federation
|
|
3
|
-
* anti-replay NonceStore (federation-edge-hardening slice 4).
|
|
4
|
-
*
|
|
5
|
-
* The gap: `federationNonceStore` in Federation.ts was a bare in-memory Map
|
|
6
|
-
* (`createNonceStore()` in federation-crypto.ts). On process restart it's
|
|
7
|
-
* wiped, so a signed federation request captured within its ±30s freshness
|
|
8
|
-
* window (verifyBodySignatureFresh) could be replayed once, post-restart.
|
|
9
|
-
* This module closes that window by backing the SAME `NonceStore` interface
|
|
10
|
-
* with the `Nonce` Harper table, while keeping every method synchronous —
|
|
11
|
-
* `verifyBodySignatureFresh` and its 2 call sites in Federation.ts are
|
|
12
|
-
* UNCHANGED (they still call `.has()` / `.set()` / `.evict()` with no
|
|
13
|
-
* `await`), so the entire existing federation-crypto-replay test suite
|
|
14
|
-
* (all synchronous assertions) keeps working unmodified.
|
|
15
|
-
*
|
|
16
|
-
* How the sync interface stays sync against an inherently-async DB API:
|
|
17
|
-
* - `has()` / `evict()` read/mutate an in-memory Map (via the existing
|
|
18
|
-
* `createNonceStore()`), exactly as before — no DB round trip.
|
|
19
|
-
* - `set()` updates that same in-memory Map synchronously (so replay
|
|
20
|
-
* detection within THIS process's lifetime is unchanged), and fires an
|
|
21
|
-
* async `Nonce.put()` in the background (not awaited) to persist the
|
|
22
|
-
* nonce for the NEXT restart. A failed/slow persist never blocks or
|
|
23
|
-
* fails request handling — see the perf/durability tradeoff note below.
|
|
24
|
-
* - `hydrate()` is the new piece: an explicit async step that loads
|
|
25
|
-
* not-yet-expired rows from the `Nonce` table into the in-memory Map.
|
|
26
|
-
* Call it once, on startup, BEFORE the store starts guarding live
|
|
27
|
-
* traffic — that's what makes a nonce recorded by the PREVIOUS process
|
|
28
|
-
* visible to a FRESH store instance after a restart (the whole point).
|
|
29
|
-
*
|
|
30
|
-
* Known tradeoff (flagged, not fixed here — would require making NonceStore
|
|
31
|
-
* async, which ripples into ~40 synchronous call sites across
|
|
32
|
-
* federation-crypto-replay.test.ts + federation-sync-e2e.test.ts):
|
|
33
|
-
* `set()`'s persistence is fire-and-forget. A crash in the narrow gap
|
|
34
|
-
* between "response sent" and "async put() resolved" can still lose that
|
|
35
|
-
* one nonce — narrowing the replay window to that gap, not eliminating it
|
|
36
|
-
* outright. Defense-in-depth: the ±30s freshness check + signature
|
|
37
|
-
* verification are still the primary guards; this closes the common case
|
|
38
|
-
* (graceful restart / redeploy), not the crash-mid-request edge case.
|
|
39
|
-
*/
|
|
40
|
-
import { createNonceStore } from "./federation-crypto.js";
|
|
41
|
-
/** 2x the standard 30s freshness window — matches the in-memory `evict()` cutoff already used by verifyBodySignatureFresh. */
|
|
42
|
-
export const DEFAULT_RETENTION_MS = 60_000;
|
|
43
|
-
async function resolveDb(opts, cache) {
|
|
44
|
-
if (opts.db)
|
|
45
|
-
return opts.db;
|
|
46
|
-
if (!cache.db) {
|
|
47
|
-
cache.db = import("harper").then((h) => h.databases);
|
|
48
|
-
}
|
|
49
|
-
return cache.db;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Create a `NonceStore` backed by the `Nonce` Harper table.
|
|
53
|
-
*
|
|
54
|
-
* Wraps the existing in-memory `createNonceStore()` (unchanged behavior for
|
|
55
|
-
* `has()`/`set()`/`evict()` within this process) and adds:
|
|
56
|
-
* - background persistence of newly-set nonces (`set()`)
|
|
57
|
-
* - an explicit `hydrate()` to reload previously-persisted nonces (used
|
|
58
|
-
* at startup, and by tests simulating a restart with a fresh instance)
|
|
59
|
-
*/
|
|
60
|
-
export function createPersistentNonceStore(opts = {}) {
|
|
61
|
-
const memory = createNonceStore();
|
|
62
|
-
const dbCache = {};
|
|
63
|
-
return {
|
|
64
|
-
has(key) {
|
|
65
|
-
return memory.has(key);
|
|
66
|
-
},
|
|
67
|
-
evict(olderThan) {
|
|
68
|
-
memory.evict(olderThan);
|
|
69
|
-
},
|
|
70
|
-
set(key, value) {
|
|
71
|
-
memory.set(key, value);
|
|
72
|
-
// Fire-and-forget persistence — see module header for the tradeoff.
|
|
73
|
-
void (async () => {
|
|
74
|
-
try {
|
|
75
|
-
const db = await resolveDb(opts, dbCache);
|
|
76
|
-
await db.flair.Nonce.put({ id: key, seenAt: value });
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
console.error("[federation-nonce-store] failed to persist nonce (in-memory record already made — this process's replay guard is unaffected):", err?.message ?? err);
|
|
80
|
-
}
|
|
81
|
-
})();
|
|
82
|
-
},
|
|
83
|
-
async hydrate(hydrateOpts) {
|
|
84
|
-
const retentionMs = hydrateOpts?.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
85
|
-
const now = hydrateOpts?.now ?? Date.now();
|
|
86
|
-
const cutoff = now - retentionMs;
|
|
87
|
-
try {
|
|
88
|
-
const db = await resolveDb(opts, dbCache);
|
|
89
|
-
for await (const row of db.flair.Nonce.search()) {
|
|
90
|
-
if (row?.id != null && typeof row.seenAt === "number" && row.seenAt >= cutoff) {
|
|
91
|
-
memory.set(String(row.id), row.seenAt);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
catch (err) {
|
|
96
|
-
console.error("[federation-nonce-store] failed to hydrate from Nonce table (starting with an empty replay guard):", err?.message ?? err);
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
// ─── Eviction sweep — periodic Nonce table hygiene ─────────────────────────
|
|
102
|
-
//
|
|
103
|
-
// Mirrors federation-cleanup.ts's 5-min setInterval hub-pattern. Decoupled
|
|
104
|
-
// from the in-memory `evict()` above (which runs per-request and only trims
|
|
105
|
-
// the local Map): this sweep deletes rows from the `Nonce` TABLE itself so
|
|
106
|
-
// it doesn't grow unbounded across restarts. No native TTL in this repo —
|
|
107
|
-
// same app-managed-sweep idiom as PairingToken cleanup.
|
|
108
|
-
//
|
|
109
|
-
// Runs on every instance (hub AND spoke) — unlike PairingToken cleanup
|
|
110
|
-
// (hub-only, since only hubs issue pairing tokens), both FederationPair and
|
|
111
|
-
// FederationSync can be the RECEIVING side of a signed request on either
|
|
112
|
-
// role, so both roles accumulate rows in the Nonce table and both need the
|
|
113
|
-
// sweep.
|
|
114
|
-
const NONCE_CLEANUP_INTERVAL_MS = 300_000; // 5 minutes
|
|
115
|
-
let nonceCleanupTimer = null;
|
|
116
|
-
/**
|
|
117
|
-
* Core sweep logic — exposed for unit testing. Deletes `Nonce` rows with
|
|
118
|
-
* `seenAt` older than `retentionMs` (default: 2x the 30s freshness window).
|
|
119
|
-
*/
|
|
120
|
-
export async function runNonceCleanupTick(opts = {}) {
|
|
121
|
-
let db;
|
|
122
|
-
if (opts.db) {
|
|
123
|
-
db = opts.db;
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
const harper = await import("harper");
|
|
127
|
-
db = harper.databases;
|
|
128
|
-
}
|
|
129
|
-
const retentionMs = opts.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
130
|
-
const now = opts.now ?? Date.now();
|
|
131
|
-
const cutoff = now - retentionMs;
|
|
132
|
-
const toDelete = [];
|
|
133
|
-
let scanned = 0;
|
|
134
|
-
try {
|
|
135
|
-
for await (const row of db.flair.Nonce.search()) {
|
|
136
|
-
scanned++;
|
|
137
|
-
if (typeof row?.seenAt === "number" && row.seenAt < cutoff) {
|
|
138
|
-
toDelete.push(row.id);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
catch (err) {
|
|
143
|
-
console.error("[federation-nonce-store] failed to query Nonce records:", err?.message ?? err);
|
|
144
|
-
return { deleted: 0, scanned: 0 };
|
|
145
|
-
}
|
|
146
|
-
let deleted = 0;
|
|
147
|
-
for (const id of toDelete) {
|
|
148
|
-
try {
|
|
149
|
-
await db.flair.Nonce.delete(id);
|
|
150
|
-
deleted++;
|
|
151
|
-
}
|
|
152
|
-
catch (err) {
|
|
153
|
-
console.error("[federation-nonce-store] failed to delete expired nonce:", err?.message ?? err);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return { deleted, scanned };
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Initialise the periodic Nonce-table eviction sweep (5-min cadence).
|
|
160
|
-
*
|
|
161
|
-
* In test environments, callers pass a mock `db` via `opts` so this module
|
|
162
|
-
* never imports harper at the top level (mirrors
|
|
163
|
-
* federation-cleanup.ts's `initFederationCleanup`).
|
|
164
|
-
*/
|
|
165
|
-
export async function initNonceStoreCleanup(opts) {
|
|
166
|
-
const immediate = opts?.immediateTick ?? true;
|
|
167
|
-
let db;
|
|
168
|
-
if (opts?.db) {
|
|
169
|
-
db = opts.db;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
try {
|
|
173
|
-
const harper = await import("harper");
|
|
174
|
-
db = harper.databases;
|
|
175
|
-
}
|
|
176
|
-
catch (err) {
|
|
177
|
-
console.error("[federation-nonce-store] failed to load harper:", err?.message ?? err);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const retentionMs = opts?.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
182
|
-
console.log("[federation-nonce-store] starting nonce eviction sweep (5-min cadence)");
|
|
183
|
-
if (nonceCleanupTimer)
|
|
184
|
-
clearInterval(nonceCleanupTimer);
|
|
185
|
-
nonceCleanupTimer = setInterval(() => {
|
|
186
|
-
runNonceCleanupTick({ db, retentionMs }).catch((err) => {
|
|
187
|
-
console.error("[federation-nonce-store] cleanup tick error:", err?.message ?? err);
|
|
188
|
-
});
|
|
189
|
-
}, NONCE_CLEANUP_INTERVAL_MS);
|
|
190
|
-
if (immediate) {
|
|
191
|
-
runNonceCleanupTick({ db, retentionMs }).catch((err) => {
|
|
192
|
-
console.error("[federation-nonce-store] initial cleanup tick error:", err?.message ?? err);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
}
|