@tpsdev-ai/flair 0.62.0 → 0.63.1
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 +14 -13
- package/SECURITY.md +73 -43
- package/config.yaml +11 -11
- package/dist/bridges/runtime/execute.js +1 -0
- package/dist/bridges/runtime/import-runner.js +4 -0
- package/dist/bridges/types.js +1 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +309 -205
- package/dist/commands/agent.js +325 -137
- package/dist/commands/attention.js +2 -0
- package/dist/commands/backup-approval.js +218 -0
- package/dist/commands/backup.js +37 -33
- package/dist/commands/bootstrap.js +5 -1
- package/dist/commands/bridge.js +10 -0
- package/dist/commands/deploy.js +37 -7
- package/dist/commands/doctor.js +206 -42
- package/dist/commands/export.js +144 -15
- package/dist/commands/federation.js +653 -68
- package/dist/commands/grant.js +5 -6
- package/dist/commands/help.js +25 -0
- package/dist/commands/idp.js +14 -19
- package/dist/commands/import.js +167 -34
- package/dist/commands/init.js +380 -164
- package/dist/commands/integrity.js +5 -6
- package/dist/commands/keys.js +12 -8
- package/dist/commands/mcp.js +157 -79
- package/dist/commands/memory.js +38 -17
- package/dist/commands/migrate-harness-memory.js +9 -3
- package/dist/commands/orgevent.js +5 -10
- package/dist/commands/principal.js +32 -39
- package/dist/commands/quality.js +26 -3
- package/dist/commands/reembed.js +468 -111
- package/dist/commands/rem.js +484 -65
- package/dist/commands/restore.js +352 -44
- package/dist/commands/search.js +2 -0
- package/dist/commands/service.js +109 -2
- package/dist/commands/soul.js +0 -6
- package/dist/commands/status.js +165 -124
- package/dist/commands/test.js +4 -1
- package/dist/commands/upgrade.js +53 -22
- package/dist/commands/workspace.js +7 -8
- package/dist/deploy.js +59 -3
- package/dist/doctor-client.js +9 -25
- package/dist/fabric-upgrade.js +12 -2
- package/dist/federation/presence-send.js +139 -0
- package/dist/federation/scheduler.js +148 -12
- package/dist/federation-verify.js +269 -177
- package/dist/install/clients.js +171 -35
- package/dist/keystore.js +77 -10
- package/dist/lib/agent-id-rule.js +44 -2
- package/dist/lib/agent-key-file.js +509 -0
- package/dist/lib/agent-rotation-lock.js +249 -0
- package/dist/lib/atomic-write.js +3 -3
- package/dist/lib/auth-resolve.js +1040 -64
- package/dist/lib/backup-approval-store.js +612 -0
- package/dist/lib/backup-artifact.js +343 -0
- package/dist/lib/config-critical-section.js +212 -25
- package/dist/lib/connect-phase.js +33 -0
- package/dist/lib/daemon-liveness.js +2 -1
- package/dist/lib/federation-pair-access.js +4 -0
- package/dist/lib/federation-retry-queue.js +283 -0
- package/dist/lib/federation-retry-row.js +101 -0
- package/dist/lib/instruction-event-contract.js +17 -0
- package/dist/lib/instruction-event-doctor.js +102 -0
- package/dist/lib/key-dir.js +203 -0
- package/dist/lib/launchd-management.js +21 -6
- package/dist/lib/local-instance-pair.js +8 -1
- package/dist/lib/mcp-enable.js +14 -15
- package/dist/lib/memory-attributes.js +2 -0
- package/dist/lib/memory-provider-timeout.js +33 -0
- package/dist/lib/mqtt-config.js +132 -0
- package/dist/lib/mqtt-listener-doctor.js +104 -0
- package/dist/lib/node-alias-path.js +50 -2
- package/dist/lib/open-live-row.js +20 -0
- package/dist/lib/ops-url.js +52 -0
- package/dist/lib/promotion-content-hash.js +20 -0
- package/dist/lib/remote-admin-pass.js +157 -0
- package/dist/lib/restore-archive.js +760 -0
- package/dist/lib/restore-verify.js +1 -1
- package/dist/lib/scheduler-platform.js +13 -0
- package/dist/lib/service-repoint.js +13 -4
- package/dist/lib/skill-assignment-preflight.js +159 -0
- package/dist/lib/skill-seed.js +125 -10
- package/dist/lib/skill-tombstone-doctor.js +133 -0
- package/dist/lib/soul-scope-fields.js +26 -0
- package/dist/lib/terminal-text.js +32 -0
- package/dist/lib/uninstall-purge.js +266 -11
- package/dist/lib/upgrade-prior-liveness.js +2 -1
- package/dist/mcp-client-assertion.js +7 -14
- package/dist/memory-id-checkpoint.js +28 -7
- package/dist/mqtt-off-deploy.js +550 -0
- package/dist/rem/promote-policy.js +3 -3
- package/dist/rem/restore.js +302 -26
- package/dist/rem/runner.js +220 -58
- package/dist/rem/scheduler.js +21 -4
- package/dist/resources/A2AAdapter.js +131 -178
- package/dist/resources/AdminMemory.js +2 -0
- package/dist/resources/AdminMemoryCandidates.js +116 -0
- package/dist/resources/Agent.js +155 -45
- package/dist/resources/AgentCard.js +4 -0
- package/dist/resources/AgentKeyRegister.js +56 -22
- package/dist/resources/AgentReadPosition.js +7 -0
- package/dist/resources/AgentSeed.js +232 -107
- package/dist/resources/AutoPromoteCandidates.js +413 -84
- package/dist/resources/Credential.js +6 -8
- package/dist/resources/Federation.js +219 -57
- package/dist/resources/FederationRetry.js +4 -4
- package/dist/resources/InstructionVersion.js +1 -1
- package/dist/resources/MCPClientMetadata.js +6 -3
- package/dist/resources/Memory.js +1348 -271
- package/dist/resources/MemoryArchive.js +149 -64
- package/dist/resources/MemoryBootstrap.js +12 -22
- package/dist/resources/MemoryCandidate.js +16 -2
- package/dist/resources/MemoryConsolidate.js +2 -2
- package/dist/resources/MemoryFeed.js +360 -147
- package/dist/resources/MemoryMaintenance.js +290 -72
- package/dist/resources/MemoryPurge.js +138 -22
- package/dist/resources/MemoryReflect.js +68 -14
- package/dist/resources/MemoryReindex.js +91 -13
- package/dist/resources/Message.js +11 -2
- package/dist/resources/OAuth.js +156 -39
- package/dist/resources/OAuthClient.js +1 -1
- package/dist/resources/OrgEvent.js +292 -29
- package/dist/resources/OrgEventCatchup.js +7 -0
- package/dist/resources/OrgEventMaintenance.js +48 -10
- package/dist/resources/PairingToken.js +1 -1
- package/dist/resources/Presence.js +31 -11
- package/dist/resources/PromoteMemoryCandidate.js +8 -2
- package/dist/resources/RecordUsage.js +2 -4
- package/dist/resources/Soul.js +2 -0
- package/dist/resources/SoulFeed.js +2 -1
- package/dist/resources/WorkspaceLatest.js +4 -0
- package/dist/resources/XAA.js +36 -10
- package/dist/resources/a2a-stream.js +313 -0
- package/dist/resources/a2a-url.js +4 -5
- package/dist/resources/agent-admin.js +10 -1
- package/dist/resources/agent-auth.js +98 -68
- package/dist/resources/agent-read-position-lib.js +4 -1
- package/dist/resources/agent-status-guard.js +9 -7
- package/dist/resources/auth-middleware.js +166 -42
- package/dist/resources/authority-field-guard.js +9 -11
- package/dist/resources/auto-promote-lib.js +8 -5
- package/dist/resources/basic-auth-header.js +21 -0
- package/dist/resources/bm25-index-service.js +4 -5
- package/dist/resources/dedup-cluster.js +6 -1
- package/dist/resources/embeddings-provider.js +25 -0
- package/dist/resources/federation-classify.js +89 -0
- package/dist/resources/federation-retry-lib.js +6 -81
- package/dist/resources/health.js +75 -19
- package/dist/resources/host-pointer/registry.js +29 -0
- package/dist/resources/host-pointer-adapter.js +1 -0
- package/dist/resources/in-process-api.js +74 -11
- package/dist/resources/in-process.js +24 -22
- package/dist/resources/instruction-version-record.js +251 -7
- package/dist/resources/internal-authority.js +13 -0
- package/dist/resources/mcp-client-metadata-fields.js +4 -0
- package/dist/resources/mcp-handler.js +146 -13
- package/dist/resources/mcp-oauth-flag.js +12 -14
- package/dist/resources/mcp-oauth.js +446 -53
- package/dist/resources/mcp-tool-scopes.js +69 -0
- package/dist/resources/mcp-tools.js +70 -22
- package/dist/resources/memory-declared-attributes.js +1 -0
- package/dist/resources/memory-embed-hash.js +56 -0
- package/dist/resources/memory-host-source.js +5 -2
- package/dist/resources/memory-reflect-lib.js +1 -1
- package/dist/resources/memory-write-lock.js +202 -23
- package/dist/resources/memory-write-response.js +1 -1
- package/dist/resources/memory-write-transaction.js +243 -0
- package/dist/resources/migration-boot.js +17 -12
- package/dist/resources/migrations/boot-progress.js +25 -0
- package/dist/resources/migrations/data-dir.js +16 -3
- package/dist/resources/migrations/dir-safety.js +60 -2
- package/dist/resources/migrations/embedding-stamp.js +315 -49
- package/dist/resources/migrations/graph-heal.js +9 -2
- package/dist/resources/migrations/ledger.js +5 -2
- package/dist/resources/migrations/lock.js +867 -80
- package/dist/resources/migrations/progress.js +71 -10
- package/dist/resources/migrations/runner.js +212 -18
- package/dist/resources/migrations/state.js +36 -1
- package/dist/resources/migrations/status.js +2 -13
- package/dist/resources/migrations/synthetic-test-migration.js +78 -12
- package/dist/resources/migrations/types.js +26 -1
- package/dist/resources/migrations/visibility-backfill.js +111 -70
- package/dist/resources/oauth-discovery.js +0 -5
- package/dist/resources/object-body-guard.js +12 -5
- package/dist/resources/org-event-reserved.js +92 -0
- package/dist/resources/org-event-write-fault.js +19 -0
- package/dist/resources/owner-field-guard.js +1 -1
- package/dist/resources/owner-scoped-write-policy.js +65 -21
- package/dist/resources/owner-scoped-writes.js +37 -6
- package/dist/resources/path-id-rewrite.js +76 -0
- package/dist/resources/presence-internal.js +5 -5
- package/dist/resources/promotion-binding.js +43 -0
- package/dist/resources/promotion-stamp.js +124 -33
- package/dist/resources/protocol-context.js +189 -0
- package/dist/resources/record-type-kit.js +2 -4
- package/dist/resources/request-target.js +28 -1
- package/dist/resources/request-transaction.js +11 -1
- package/dist/resources/seed-reservation.js +1 -2
- package/dist/resources/semantic-retrieval-core.js +7 -6
- package/dist/resources/server-row-validation.js +71 -0
- package/dist/resources/skill-manifest.js +4 -3
- package/dist/resources/skill-version-write.js +400 -27
- package/dist/resources/skill-write-fault.js +6 -3
- package/dist/resources/skill-write-policy.js +2 -1
- package/dist/resources/soul-write-policy.js +26 -7
- package/dist/resources/table-helpers.js +1 -1
- package/dist/resources/table-subscription-policy.js +518 -85
- package/dist/resources/table-subscriptions.js +20 -7
- package/dist/resources/txn-pause-point.js +122 -5
- package/dist/resources/usage-recording.js +54 -46
- package/dist/resources/write-back.js +176 -11
- package/dist/resources/write-journal-context.js +5 -0
- package/dist/resources/write-journal.js +289 -0
- package/dist/src/keystore.js +77 -10
- package/dist/src/lib/agent-id-rule.js +44 -2
- package/dist/src/lib/atomic-write.js +3 -3
- package/dist/src/lib/federation-retry-row.js +101 -0
- package/dist/src/lib/instruction-event-contract.js +17 -0
- package/dist/src/lib/key-dir.js +203 -0
- package/dist/src/lib/memory-attributes.js +2 -0
- package/dist/src/lib/open-live-row.js +20 -0
- package/dist/src/lib/scheduler-platform.js +13 -0
- package/dist/src/lib/soul-scope-fields.js +26 -0
- package/dist/src/rem/promote-policy.js +3 -3
- package/dist/src/rem/scheduler.js +21 -4
- package/docs/access-control.md +39 -21
- package/docs/api-reference.md +161 -52
- package/docs/auth.md +69 -38
- package/docs/claude-code.md +3 -3
- package/docs/deploying-on-fabric.md +118 -2
- package/docs/deployment.md +17 -0
- package/docs/embedding-in-a-harper-app.md +23 -21
- package/docs/entity-vocabulary.md +7 -11
- package/docs/federation.md +29 -2
- package/docs/hosted-on-fabric.md +1 -1
- package/docs/integrations.md +11 -11
- package/docs/mcp-clients.md +7 -5
- package/docs/notes/mcp-agent-auth-consumer.md +4 -9
- package/docs/notes/mcp-oauth-model2.md +6 -6
- package/docs/openclaw.md +1 -1
- package/docs/quickstart-fabric.md +2 -2
- package/docs/quickstart.md +3 -3
- package/docs/releasing.md +13 -13
- package/docs/rem.md +24 -3
- package/docs/secrets-and-keys.md +9 -14
- package/docs/standalone-local.md +29 -2
- package/docs/supply-chain-policy.md +28 -20
- package/docs/the-team.md +2 -2
- package/docs/upgrade.md +29 -5
- package/package.json +18 -11
- package/schemas/agent.graphql +3 -3
- package/schemas/federation.graphql +10 -7
- package/schemas/memory.graphql +7 -2
- package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
- package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
- package/dist/resources/soul-adk-guard.js +0 -68
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
/** Workflow verdicts can only be stamped by their trusted raw-table paths.
|
|
2
2
|
* Keep unchanged echoes compatible with full-row clients, and preserve omitted
|
|
3
|
-
* stamps on PUT; omission must not erase a verdict. A content change
|
|
4
|
-
*
|
|
3
|
+
* stamps on PUT; omission must not erase a verdict. A Memory content change
|
|
4
|
+
* drops Memory's stamps; other tables keep theirs. No admin/body bypass. */
|
|
5
5
|
export const AUTHORITY_FIELDS = {
|
|
6
|
-
Memory: ["promotionStatus", "promotedAt", "promotedBy"],
|
|
6
|
+
Memory: ["promotionStatus", "promotedAt", "promotedBy", "promotionContentHash", "writeBackWriteId"],
|
|
7
|
+
MemoryCandidate: ["promotedRowId", "promotedRowInstanceToken"],
|
|
7
8
|
};
|
|
8
9
|
export async function guardAuthorityFields(getExisting, content, table) {
|
|
9
10
|
const existing = await getExisting();
|
|
10
11
|
for (const field of AUTHORITY_FIELDS[table]) {
|
|
11
12
|
if (Object.hasOwn(content, field) && content[field] !== existing?.[field]) {
|
|
12
|
-
return new Response(JSON.stringify({ error: `forbidden: ${field} is set by the promotion workflow` }), {
|
|
13
|
+
return new Response(JSON.stringify({ error: `forbidden: ${field} is set by ${field === "writeBackWriteId" ? "write-back" : "the promotion workflow"}` }), {
|
|
13
14
|
status: 403, headers: { "content-type": "application/json" },
|
|
14
15
|
});
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
if (existing && Object.hasOwn(content, "content") && content.content !== existing.content) {
|
|
18
|
+
// Memory's verdict and content binding attest to its content, so a Memory
|
|
19
|
+
// content change drops them. Null the columns (do not merely delete) so PATCH
|
|
20
|
+
// and PUT both drop them. Other tables' fields are preserved below.
|
|
21
|
+
if (table === "Memory" && existing && Object.hasOwn(content, "content") && content.content !== existing.content) {
|
|
21
22
|
for (const field of AUTHORITY_FIELDS[table])
|
|
22
23
|
content[field] = null;
|
|
23
24
|
return null;
|
|
@@ -28,9 +29,6 @@ export async function guardAuthorityFields(getExisting, content, table) {
|
|
|
28
29
|
}
|
|
29
30
|
return null;
|
|
30
31
|
}
|
|
31
|
-
/** Drop workflow stamps from a request-origin raw write. Unconditional: even
|
|
32
|
-
* stamps the guard would restore onto an omitted-field update must not land
|
|
33
|
-
* through ingest paths that are not a promotion-stamp site. */
|
|
34
32
|
export function stripAuthorityFields(record, table) {
|
|
35
33
|
for (const field of AUTHORITY_FIELDS[table])
|
|
36
34
|
delete record[field];
|
|
@@ -69,7 +69,10 @@ export const AUTO_PROMOTE_RATIONALE = "auto-promoted from ADK session distillati
|
|
|
69
69
|
* runs once per nightly cycle, not on every write, and each promotion is a
|
|
70
70
|
* bounded DB write (plus at most one embedding compute on the Memory.put path),
|
|
71
71
|
* so this caps the blast radius of a single cycle rather than throttling a hot
|
|
72
|
-
* path.
|
|
72
|
+
* path. AutoPromoteCandidates counts every eligible candidate it attempts a
|
|
73
|
+
* Memory write for, whatever the outcome (a rejected or thrown attempt
|
|
74
|
+
* included), plus those it takes a recorded row for (flair#2714). Overflow
|
|
75
|
+
* stays `pending` and is swept on subsequent cycles.
|
|
73
76
|
*/
|
|
74
77
|
export const DEFAULT_MAX_AUTO_PROMOTE_PER_CYCLE = 200;
|
|
75
78
|
// ─── Structural-truncation signal (flair#1756 slice 2) ───────────────────────
|
|
@@ -77,10 +80,10 @@ export const DEFAULT_MAX_AUTO_PROMOTE_PER_CYCLE = 200;
|
|
|
77
80
|
// src/rem/promote-policy.ts. This gate imports structuralImbalance from it; the
|
|
78
81
|
// CLI flag path (src/commands/rem.ts) uses hasTerminalPunctuation from the same
|
|
79
82
|
// module. A server file importing a PURE helper from src/ is established practice
|
|
80
|
-
// in this repo (resources/PromoteMemoryCandidate.ts
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
//
|
|
83
|
+
// in this repo (resources/PromoteMemoryCandidate.ts imports that module); what
|
|
84
|
+
// does not survive npm packaging is a CLI file importing FROM resources/. One
|
|
85
|
+
// definition lets the CLI and this gate share a single implementation rather
|
|
86
|
+
// than a hand-kept pair.
|
|
84
87
|
//
|
|
85
88
|
// Slice 1 handled a generation the backend LABELLED incomplete (finishReason
|
|
86
89
|
// `length` / `content_filter`). This handles the other case: a claim reaches
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Basic `Authorization` header check the auth middleware uses
|
|
3
|
+
* (flair#2764), in one place so the CLI's copy in src/lib/auth-resolve.ts can
|
|
4
|
+
* be tested against it. It is a prefix test only: `Basic` in any case (scheme
|
|
5
|
+
* names are case-insensitive, RFC 9110) followed by a space. Nothing after
|
|
6
|
+
* that space is validated: parseBasicAuthHeader decodes all of it with
|
|
7
|
+
* `Buffer.from(..., "base64")`, which does not reject characters outside the
|
|
8
|
+
* base64 alphabet, so further spaces or any other text pass the check.
|
|
9
|
+
*/
|
|
10
|
+
export const BASIC_AUTH_HEADER_RE = /^Basic /i;
|
|
11
|
+
/** The user and password a Basic header carries, or null for any other header. */
|
|
12
|
+
export function parseBasicAuthHeader(header) {
|
|
13
|
+
if (!BASIC_AUTH_HEADER_RE.test(header))
|
|
14
|
+
return null;
|
|
15
|
+
const decoded = Buffer.from(header.slice(6), "base64").toString("utf-8");
|
|
16
|
+
const colonIdx = decoded.indexOf(":");
|
|
17
|
+
return {
|
|
18
|
+
user: colonIdx >= 0 ? decoded.slice(0, colonIdx) : decoded,
|
|
19
|
+
pass: colonIdx >= 0 ? decoded.slice(colonIdx + 1) : "",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -53,10 +53,8 @@
|
|
|
53
53
|
//
|
|
54
54
|
// 2. SYNCHRONOUS HOOKS at flair's own write surface (`noteMemoryUpsert` /
|
|
55
55
|
// `noteMemoryDelete`) give READ-YOUR-WRITE after owned transactions commit.
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
// asynchronous; other hooked writes avoid that delay. Both mechanisms are
|
|
59
|
-
// idempotent by id, so seeing a committed write twice is a no-op.
|
|
56
|
+
// Both mechanisms are idempotent by id, so seeing a committed write twice
|
|
57
|
+
// is a no-op.
|
|
60
58
|
//
|
|
61
59
|
// If the feed cannot be established, or delivers an event shape we do not
|
|
62
60
|
// understand (Harper emits a bare `reload` marker when a base copy / resync is
|
|
@@ -83,6 +81,7 @@
|
|
|
83
81
|
import { threadId } from "node:worker_threads";
|
|
84
82
|
import { databases } from "harper";
|
|
85
83
|
import { withDetachedTxn } from "./table-helpers.js";
|
|
84
|
+
import { subscribeInProcess } from "./table-subscription-policy.js";
|
|
86
85
|
import { Bm25Index, INDEX_SELECT } from "./bm25-index.js";
|
|
87
86
|
import { BM25_BOOT_WARM_SKIPPED_PREFIX, formatBm25IndexSummary, readThreadsCount } from "./bm25-status.js";
|
|
88
87
|
import { retrievalMode } from "./bm25.js";
|
|
@@ -258,7 +257,7 @@ async function startFeed(ctx) {
|
|
|
258
257
|
if (feedStarted)
|
|
259
258
|
return;
|
|
260
259
|
feedStarted = true;
|
|
261
|
-
const subscription = await withDetachedTxn(ctx, () => databases.flair.Memory
|
|
260
|
+
const subscription = await withDetachedTxn(ctx, () => subscribeInProcess(databases.flair.Memory, { omitCurrent: true }));
|
|
262
261
|
// Deliberately not awaited: the consumer runs for the life of the process.
|
|
263
262
|
(async () => {
|
|
264
263
|
try {
|
|
@@ -41,7 +41,12 @@ export const DEDUP_CLUSTER_ANN_K_DEFAULT = 10;
|
|
|
41
41
|
* foreseeable future, and the brief explicitly asked to prefer the full ANN
|
|
42
42
|
* sweep over sampling. When the instance exceeds this, the sweep covers the
|
|
43
43
|
* most-recently-created N memories (deterministic, documented) rather than
|
|
44
|
-
* a random sample.
|
|
44
|
+
* a random sample. The CLI's `POST /MemoryDedupStats` shares the REM work
|
|
45
|
+
* deadline (1,800 s by default, `REM_WORK_REQUEST_DEADLINE_MS` in
|
|
46
|
+
* `src/commands/rem.ts`) which is a sizing estimate
|
|
47
|
+
* from one observation, not a throughput claim: 20,000 x ~57 ms = ~1,140 s.
|
|
48
|
+
* The route also scans every Memory row before it selects the capped ANN
|
|
49
|
+
* set. */
|
|
45
50
|
export const DEDUP_CLUSTER_MAX_MEMORIES_DEFAULT = 20_000;
|
|
46
51
|
/** Server-side stat artifact home (Slice 1c Part 2 — smallest-surface
|
|
47
52
|
* storage choice). A single small JSON file under the SERVER's own
|
|
@@ -107,6 +107,28 @@
|
|
|
107
107
|
* still a separate, deliberate step from shipping this code — see the PR
|
|
108
108
|
* description for that distinction.
|
|
109
109
|
*/
|
|
110
|
+
import { TEST_FAULT_INJECTION_ENV } from "./txn-pause-point.js";
|
|
111
|
+
/**
|
|
112
|
+
* Test-only provider delay: the first call in this process whose text contains
|
|
113
|
+
* `FLAIR_TEST_EMBED_PROVIDER_DELAY_MATCH` waits
|
|
114
|
+
* `FLAIR_TEST_EMBED_PROVIDER_DELAY_MS` before reaching the backend. The delay
|
|
115
|
+
* happens only when `FLAIR_ENABLE_TEST_FAULT_INJECTION=1` and both settings are set.
|
|
116
|
+
*/
|
|
117
|
+
const TEST_EMBED_DELAY_MS_ENV = "FLAIR_TEST_EMBED_PROVIDER_DELAY_MS";
|
|
118
|
+
const TEST_EMBED_DELAY_MATCH_ENV = "FLAIR_TEST_EMBED_PROVIDER_DELAY_MATCH";
|
|
119
|
+
let testProviderDelayUsed = false;
|
|
120
|
+
function testProviderDelayMs(text) {
|
|
121
|
+
if (testProviderDelayUsed)
|
|
122
|
+
return undefined;
|
|
123
|
+
if (process.env[TEST_FAULT_INJECTION_ENV] !== "1")
|
|
124
|
+
return undefined;
|
|
125
|
+
const match = process.env[TEST_EMBED_DELAY_MATCH_ENV];
|
|
126
|
+
const ms = Number(process.env[TEST_EMBED_DELAY_MS_ENV]);
|
|
127
|
+
if (!match || !text.includes(match) || !Number.isFinite(ms) || ms <= 0)
|
|
128
|
+
return undefined;
|
|
129
|
+
testProviderDelayUsed = true;
|
|
130
|
+
return ms;
|
|
131
|
+
}
|
|
110
132
|
const VALID_INPUT_TYPES = new Set(["document", "query"]);
|
|
111
133
|
/**
|
|
112
134
|
* THE GATE (flair#504 Phase 2 — flipped ON). A module-level constant, not an
|
|
@@ -320,6 +342,9 @@ function ensureProbeStarted() {
|
|
|
320
342
|
export async function getEmbedding(text, inputType) {
|
|
321
343
|
ensureProbeStarted();
|
|
322
344
|
try {
|
|
345
|
+
const providerDelay = testProviderDelayMs(text);
|
|
346
|
+
if (providerDelay !== undefined)
|
|
347
|
+
await new Promise((resolve) => setTimeout(resolve, providerDelay));
|
|
323
348
|
const models = await getModelsApi();
|
|
324
349
|
const [vec] = await models.embed(text, buildEmbedOptions(inputType));
|
|
325
350
|
if (!vec)
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* in SyncLog.skippedReasons so operators can grep for them.
|
|
7
7
|
*/
|
|
8
8
|
import { effectiveAgentStatus } from "./agent-status-guard.js";
|
|
9
|
+
import { ADMIN_ROLE } from "./agent-admin.js";
|
|
9
10
|
/**
|
|
10
11
|
* Every SkipReason's retry class (flair#2455 design rev 2, K1/S3).
|
|
11
12
|
*
|
|
@@ -32,9 +33,12 @@ export const SKIP_REASON_CLASS = {
|
|
|
32
33
|
pointer_not_federated: "non_retryable",
|
|
33
34
|
agent_status_not_federated: "non_retryable",
|
|
34
35
|
agent_pending_key_not_federated: "non_retryable",
|
|
36
|
+
agent_identity_not_from_home: "non_retryable",
|
|
37
|
+
agent_admin_id_not_federated: "non_retryable",
|
|
35
38
|
id_mismatch: "non_retryable",
|
|
36
39
|
invalid_expiry: "non_retryable",
|
|
37
40
|
merge_target_changed: "retryable",
|
|
41
|
+
merge_target_busy: "retryable",
|
|
38
42
|
merge_error: "retryable",
|
|
39
43
|
invalid_agent_id: "non_retryable",
|
|
40
44
|
skill_not_federated: "non_retryable",
|
|
@@ -245,3 +249,88 @@ export function inboundChangesExistingPendingKey(record, local) {
|
|
|
245
249
|
return false;
|
|
246
250
|
return Object.prototype.hasOwnProperty.call(data, "publicKey") && data.publicKey !== local.publicKey;
|
|
247
251
|
}
|
|
252
|
+
const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
253
|
+
/** A missing, null or empty home all mean "no recorded home". */
|
|
254
|
+
function sameAgentHome(a, b) {
|
|
255
|
+
const none = (v) => v === null || v === undefined || v === "";
|
|
256
|
+
return (none(a) && none(b)) || a === b;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* flair#2665: whether `senderInstanceId` (the batch-verified sender, not a
|
|
260
|
+
* field of the record) is the stored Agent row's home (`originatorInstanceId`).
|
|
261
|
+
* A row with no recorded home has no sender that matches.
|
|
262
|
+
*/
|
|
263
|
+
export function senderIsAgentHome(local, senderInstanceId) {
|
|
264
|
+
const home = local?.originatorInstanceId;
|
|
265
|
+
return typeof home === "string" && home.length > 0 && home === senderInstanceId;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* flair#2665: true for an inbound Agent record that would change an existing
|
|
269
|
+
* local row's `publicKey` or home (`originatorInstanceId`), regardless of
|
|
270
|
+
* timestamps.
|
|
271
|
+
*/
|
|
272
|
+
export function inboundChangesAgentIdentity(record, local) {
|
|
273
|
+
if (record.table !== "Agent" || local == null)
|
|
274
|
+
return false;
|
|
275
|
+
const data = record.data;
|
|
276
|
+
if (!data || typeof data !== "object")
|
|
277
|
+
return false;
|
|
278
|
+
if (hasOwn(data, "publicKey") && data.publicKey !== local.publicKey)
|
|
279
|
+
return true;
|
|
280
|
+
return hasOwn(data, "originatorInstanceId") && !sameAgentHome(data.originatorInstanceId, local.originatorInstanceId);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* flair#2665: the refusal for an inbound Agent record that would change an
|
|
284
|
+
* existing row's `publicKey` or home when the batch-verified sender is not that
|
|
285
|
+
* row's stored home, else null.
|
|
286
|
+
*/
|
|
287
|
+
export function agentIdentityRefusal(record, local, senderInstanceId) {
|
|
288
|
+
if (!inboundChangesAgentIdentity(record, local))
|
|
289
|
+
return null;
|
|
290
|
+
return senderIsAgentHome(local, senderInstanceId) ? null : "agent_identity_not_from_home";
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* flair#2665: the refusal for an inbound Agent record that would create a row
|
|
294
|
+
* (`local` is null) whose id is in `configuredAdminIds`, else null. The caller
|
|
295
|
+
* passes resources/agent-admin.ts's configuredAdminAgentIds(), the set
|
|
296
|
+
* resources/agent-auth.ts's agentIsAdmin() admits by id.
|
|
297
|
+
*/
|
|
298
|
+
export function agentAdminIdCreateRefusal(record, local, configuredAdminIds) {
|
|
299
|
+
if (record.table !== "Agent" || local != null)
|
|
300
|
+
return null;
|
|
301
|
+
return configuredAdminIds.has(record.id) ? "agent_admin_id_not_federated" : null;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* flair#2665: admin state (`role` equal to ADMIN_ROLE, `admin: true`) is the
|
|
305
|
+
* receiving instance's own. Applied to the row a federation merge is
|
|
306
|
+
* about to write for an Agent record (mutates and returns `merged`):
|
|
307
|
+
* - a NEW row (`stored` is null) is written without admin state: a `role` equal to
|
|
308
|
+
* ADMIN_ROLE is removed and an `admin: true` mirror becomes false;
|
|
309
|
+
* - an EXISTING row keeps its stored `role` when the merged one would change
|
|
310
|
+
* whether the row is an administrator, and keeps its stored `admin` mirror
|
|
311
|
+
* when the merged one's `admin: true` state differs from the stored one's.
|
|
312
|
+
* A `role` label follows the merge when the stored and incoming roles are both
|
|
313
|
+
* non-admin.
|
|
314
|
+
*/
|
|
315
|
+
export function keepReceiverAgentAuthority(merged, stored) {
|
|
316
|
+
if (stored == null) {
|
|
317
|
+
if (merged.role === ADMIN_ROLE)
|
|
318
|
+
delete merged.role;
|
|
319
|
+
if (merged.admin === true)
|
|
320
|
+
merged.admin = false;
|
|
321
|
+
return merged;
|
|
322
|
+
}
|
|
323
|
+
if ((merged.role === ADMIN_ROLE) !== (stored.role === ADMIN_ROLE)) {
|
|
324
|
+
if (hasOwn(stored, "role"))
|
|
325
|
+
merged.role = stored.role;
|
|
326
|
+
else
|
|
327
|
+
delete merged.role;
|
|
328
|
+
}
|
|
329
|
+
if ((merged.admin === true) !== (stored.admin === true)) {
|
|
330
|
+
if (hasOwn(stored, "admin"))
|
|
331
|
+
merged.admin = stored.admin;
|
|
332
|
+
else
|
|
333
|
+
delete merged.admin;
|
|
334
|
+
}
|
|
335
|
+
return merged;
|
|
336
|
+
}
|
|
@@ -8,26 +8,14 @@
|
|
|
8
8
|
* in resources/FederationRetry.ts.
|
|
9
9
|
*/
|
|
10
10
|
import { isSkipReason } from "./federation-classify.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"source_deleted",
|
|
15
|
-
"source_ineligible",
|
|
16
|
-
"peer_unavailable",
|
|
17
|
-
"retry_exhausted",
|
|
18
|
-
]);
|
|
19
|
-
export const RETRY_BOUNDS = new Set(["attempts", "age", "both"]);
|
|
11
|
+
import { RETRY_BOUNDS, RETRY_RESOLUTIONS, isRetryBound, isRetryResolution, parseRetryEntryKey, retryEntryKey, retryRowProblem as retryRowProblemShared, } from "../src/lib/federation-retry-row.js";
|
|
12
|
+
export { parseRetryEntryKey, retryEntryKey };
|
|
13
|
+
export { RETRY_BOUNDS, RETRY_RESOLUTIONS, isRetryBound, isRetryResolution };
|
|
20
14
|
export const LOCAL_RETRY_REASONS = new Set([
|
|
21
15
|
"transport_failure",
|
|
22
16
|
"invalid_response",
|
|
23
17
|
"retry_unknown",
|
|
24
18
|
]);
|
|
25
|
-
export function isRetryResolution(value) {
|
|
26
|
-
return typeof value === "string" && RETRY_RESOLUTIONS.has(value);
|
|
27
|
-
}
|
|
28
|
-
export function isRetryBound(value) {
|
|
29
|
-
return typeof value === "string" && RETRY_BOUNDS.has(value);
|
|
30
|
-
}
|
|
31
19
|
/**
|
|
32
20
|
* Every field the `FederationRetry` schema declares (schemas/federation.graphql),
|
|
33
21
|
* in schema order. The table is `@sealed`, so Harper refuses a write that
|
|
@@ -64,72 +52,9 @@ export function pickRetryRowFields(row) {
|
|
|
64
52
|
return out;
|
|
65
53
|
}
|
|
66
54
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* A JSON array join, never a bare concatenation: a naive `${peerId}${table}
|
|
70
|
-
* ${recordId}` collides (peer `a` + table `bc` is peer `ab` + table `c`), and
|
|
71
|
-
* peer and record ids are arbitrary strings off the wire, including legacy
|
|
72
|
-
* `.content`-suffixed ids. JSON quotes and escapes each component, so distinct
|
|
73
|
-
* tuples encode to distinct strings and the components round-trip exactly.
|
|
74
|
-
*
|
|
75
|
-
* Every component must be a non-empty string; a missing component is a caller
|
|
76
|
-
* bug, not a row to write, so this THROWS rather than mint a malformed key.
|
|
55
|
+
* See {@link retryRowProblemShared}: the shared convention check, with the
|
|
56
|
+
* receiver's full reason vocabulary.
|
|
77
57
|
*/
|
|
78
|
-
export function retryEntryKey(peerId, table, recordId) {
|
|
79
|
-
for (const [name, value] of [
|
|
80
|
-
["peerId", peerId],
|
|
81
|
-
["table", table],
|
|
82
|
-
["recordId", recordId],
|
|
83
|
-
]) {
|
|
84
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
85
|
-
throw new Error(`federation retry: ${name} must be a non-empty string`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return JSON.stringify([peerId, table, recordId]);
|
|
89
|
-
}
|
|
90
|
-
/** Decode {@link retryEntryKey}'s output, or null when `id` is not a retry key. */
|
|
91
|
-
export function parseRetryEntryKey(id) {
|
|
92
|
-
let parsed;
|
|
93
|
-
try {
|
|
94
|
-
parsed = JSON.parse(id);
|
|
95
|
-
}
|
|
96
|
-
catch {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
if (!Array.isArray(parsed) || parsed.length !== 3)
|
|
100
|
-
return null;
|
|
101
|
-
const [peerId, table, recordId] = parsed;
|
|
102
|
-
if (typeof peerId !== "string" || typeof table !== "string" || typeof recordId !== "string")
|
|
103
|
-
return null;
|
|
104
|
-
if (peerId.length === 0 || table.length === 0 || recordId.length === 0)
|
|
105
|
-
return null;
|
|
106
|
-
return { peerId, table, recordId };
|
|
107
|
-
}
|
|
108
58
|
export function retryRowProblem(row) {
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
expectedId = retryEntryKey(row.peerId, row.table, row.recordId);
|
|
112
|
-
}
|
|
113
|
-
catch {
|
|
114
|
-
expectedId = null;
|
|
115
|
-
}
|
|
116
|
-
if (expectedId === null || row.id !== expectedId)
|
|
117
|
-
return "id_mismatch";
|
|
118
|
-
if (row.state !== "pending" && row.state !== "terminal")
|
|
119
|
-
return "unknown_state";
|
|
120
|
-
if (row.lastReason != null && !isSkipReason(row.lastReason) && !LOCAL_RETRY_REASONS.has(row.lastReason)) {
|
|
121
|
-
return "unknown_reason";
|
|
122
|
-
}
|
|
123
|
-
if (row.state === "pending") {
|
|
124
|
-
if (row.resolution != null || row.bound != null)
|
|
125
|
-
return "outcome_mismatch";
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
if (!isRetryResolution(row.resolution))
|
|
129
|
-
return "outcome_mismatch";
|
|
130
|
-
const exhausted = row.resolution === "retry_exhausted";
|
|
131
|
-
if (exhausted ? !isRetryBound(row.bound) : row.bound != null)
|
|
132
|
-
return "outcome_mismatch";
|
|
133
|
-
}
|
|
134
|
-
return null;
|
|
59
|
+
return retryRowProblemShared(row, (value) => isSkipReason(value) || LOCAL_RETRY_REASONS.has(value));
|
|
135
60
|
}
|
package/dist/resources/health.js
CHANGED
|
@@ -203,6 +203,10 @@ export class HealthDetail extends Resource {
|
|
|
203
203
|
// corpus is one space and still outstanding against getModelId().
|
|
204
204
|
let mixedEmbeddingSpaces = false;
|
|
205
205
|
let memoryScanOk = false;
|
|
206
|
+
// Set only when the Memory scan finished without throwing. A failed or
|
|
207
|
+
// partial scan leaves it false, and the unregistered-author list is then
|
|
208
|
+
// reported unavailable (null); no other field reads this flag.
|
|
209
|
+
let memoryScanComplete = false;
|
|
206
210
|
let memoryModelCounts = {};
|
|
207
211
|
// flair#1326: same search-ready signal as public /Health. HealthDetail
|
|
208
212
|
// stays HTTP 200 (it is a stats dump, not a traffic gate); the field
|
|
@@ -335,6 +339,7 @@ export class HealthDetail extends Resource {
|
|
|
335
339
|
memoryModelCounts = modelCounts;
|
|
336
340
|
if (distinctSpaces.size > 1)
|
|
337
341
|
mixedEmbeddingSpaces = true;
|
|
342
|
+
memoryScanComplete = true;
|
|
338
343
|
}
|
|
339
344
|
catch {
|
|
340
345
|
stats.memories = null;
|
|
@@ -347,38 +352,80 @@ export class HealthDetail extends Resource {
|
|
|
347
352
|
const blank = (id) => ({
|
|
348
353
|
id, memoryCount: 0, hashFallback: 0, writes24h: 0, lastWriteAt: null, usageCount: 0,
|
|
349
354
|
});
|
|
355
|
+
// The count, the names and the table all come from one map keyed by
|
|
356
|
+
// Agent id, so the admin total, names and row set describe the same
|
|
357
|
+
// agents. A Memory row whose author has no Agent record — the
|
|
358
|
+
// operator's `skill:using-flair` seed when the operator id was never
|
|
359
|
+
// registered — is kept in a separate list, never added to `perAgent`
|
|
360
|
+
// as if it were an agent (flair#3051).
|
|
350
361
|
const perAgentMap = new Map();
|
|
351
362
|
for (const a of agents) {
|
|
352
363
|
if (a.id)
|
|
353
364
|
perAgentMap.set(a.id, blank(a.id));
|
|
354
365
|
}
|
|
366
|
+
const unregisteredMap = new Map();
|
|
355
367
|
const cutoff24h = nowMs - 24 * 3600 * 1000;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
if (!
|
|
369
|
-
row.
|
|
368
|
+
// Only a completed Memory scan yields counts. A scan that threw part-way
|
|
369
|
+
// leaves a partial `memoriesList`; counting it would report partial
|
|
370
|
+
// numbers as facts, so the loop is skipped and every row below reports
|
|
371
|
+
// its Memory-derived values as unknown.
|
|
372
|
+
if (memoryScanComplete) {
|
|
373
|
+
for (const m of memoriesList) {
|
|
374
|
+
if (!m.agentId)
|
|
375
|
+
continue;
|
|
376
|
+
const known = perAgentMap.get(m.agentId);
|
|
377
|
+
const row = known ?? unregisteredMap.get(m.agentId) ?? blank(m.agentId);
|
|
378
|
+
row.memoryCount++;
|
|
379
|
+
row.usageCount += (m.usageCount ?? 0);
|
|
380
|
+
if (!m.embeddingModel || m.embeddingModel === "hash-512d")
|
|
381
|
+
row.hashFallback++;
|
|
382
|
+
if (m.createdAt) {
|
|
383
|
+
const ts = new Date(m.createdAt).getTime();
|
|
384
|
+
if (ts >= cutoff24h)
|
|
385
|
+
row.writes24h++;
|
|
386
|
+
if (!row.lastWriteAt || ts > new Date(row.lastWriteAt).getTime()) {
|
|
387
|
+
row.lastWriteAt = m.createdAt;
|
|
388
|
+
}
|
|
370
389
|
}
|
|
390
|
+
if (known)
|
|
391
|
+
perAgentMap.set(m.agentId, row);
|
|
392
|
+
else
|
|
393
|
+
unregisteredMap.set(m.agentId, row);
|
|
371
394
|
}
|
|
372
|
-
perAgentMap.set(m.agentId, row);
|
|
373
395
|
}
|
|
374
|
-
const
|
|
396
|
+
const agentIds = Array.from(perAgentMap.keys());
|
|
397
|
+
const perAgentFull = memoryScanComplete
|
|
398
|
+
? Array.from(perAgentMap.values()).sort((a, b) => b.memoryCount - a.memoryCount)
|
|
399
|
+
: agentIds.map((id) => ({
|
|
400
|
+
id,
|
|
401
|
+
memoryCount: null,
|
|
402
|
+
hashFallback: null,
|
|
403
|
+
writes24h: null,
|
|
404
|
+
lastWriteAt: null,
|
|
405
|
+
usageCount: null,
|
|
406
|
+
}));
|
|
375
407
|
const perAgent = isAdmin
|
|
376
408
|
? perAgentFull
|
|
377
|
-
: perAgentFull.filter((r) => r.id === callerAgent || r.memoryCount > 0);
|
|
409
|
+
: perAgentFull.filter((r) => r.id === callerAgent || (r.memoryCount ?? 0) > 0);
|
|
410
|
+
const unregisteredMemoryAuthors = Array.from(unregisteredMap.values())
|
|
411
|
+
.sort((a, b) => b.memoryCount - a.memoryCount)
|
|
412
|
+
.map((r) => ({ id: r.id, memoryCount: r.memoryCount }));
|
|
378
413
|
stats.agents = {
|
|
379
|
-
count:
|
|
380
|
-
names: isAdmin ?
|
|
414
|
+
count: perAgentMap.size,
|
|
415
|
+
names: isAdmin ? agentIds : undefined,
|
|
381
416
|
perAgent,
|
|
417
|
+
// A non-admin gets the de-duplicated Agent count — one per distinct
|
|
418
|
+
// truthy Agent id, so a duplicate collapses and an id-less row is
|
|
419
|
+
// not counted — and only the perAgent rows it may see: its own row
|
|
420
|
+
// and rows with memories. unregisteredMemoryAuthors is undefined for
|
|
421
|
+
// a non-admin. An admin gets that list only after a complete Memory
|
|
422
|
+
// scan; null means the scan did not finish, so the answer is unknown
|
|
423
|
+
// rather than empty or partial. The same rule covers each row's
|
|
424
|
+
// Memory-derived values: they are null (unknown), never zero or a
|
|
425
|
+
// partial count, when the scan did not complete.
|
|
426
|
+
unregisteredMemoryAuthors: isAdmin
|
|
427
|
+
? (memoryScanComplete ? unregisteredMemoryAuthors : null)
|
|
428
|
+
: undefined,
|
|
382
429
|
};
|
|
383
430
|
}
|
|
384
431
|
catch {
|
|
@@ -847,6 +894,9 @@ export class HealthDetail extends Resource {
|
|
|
847
894
|
lastCycleError: redactReason(rawSnapshot.lastCycleError),
|
|
848
895
|
migrations: rawSnapshot.migrations.map((m) => ({
|
|
849
896
|
...m,
|
|
897
|
+
// Row ids are operator detail, like the state-file path below:
|
|
898
|
+
// verified non-admin callers get the count only (flair#2840).
|
|
899
|
+
unconfirmedIds: undefined,
|
|
850
900
|
reason: m.state === "completed" && m.reason
|
|
851
901
|
? "recorded complete; not re-verified this boot"
|
|
852
902
|
: redactReason(m.reason),
|
|
@@ -888,6 +938,12 @@ export class HealthDetail extends Resource {
|
|
|
888
938
|
if (m.state === "halted" || m.state === "failed") {
|
|
889
939
|
warnings.push({ level: "warn", message: `migration '${m.id}' ${m.state}${m.reason ? `: ${m.reason}` : ""} — see \`flair doctor\`` });
|
|
890
940
|
}
|
|
941
|
+
else if (m.state === "completed" && (m.rowsUnconfirmed ?? 0) > 0) {
|
|
942
|
+
warnings.push({
|
|
943
|
+
level: "warn",
|
|
944
|
+
message: `migration '${m.id}' completed with ${m.rowsUnconfirmed} row(s) it could not confirm`,
|
|
945
|
+
});
|
|
946
|
+
}
|
|
891
947
|
}
|
|
892
948
|
// The boot trigger sets `scheduled` synchronously at module load, so
|
|
893
949
|
// `idle` here means resources/migration-boot.js never loaded — the
|
|
@@ -33,3 +33,32 @@ export const productionHostPointerAdapter = {
|
|
|
33
33
|
export function currentHostPointerAdapter() {
|
|
34
34
|
return productionHostPointerAdapter;
|
|
35
35
|
}
|
|
36
|
+
export class PointerAlreadyStoredError extends Error {
|
|
37
|
+
pointer;
|
|
38
|
+
constructor(pointer) {
|
|
39
|
+
super("MemoryHostSource replacement already stored");
|
|
40
|
+
this.pointer = pointer;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Guard the table put with Harper's commit-time entry and conflict retry. */
|
|
44
|
+
export async function restorePointerRowIfAbsent(row, ctx) {
|
|
45
|
+
const store = pointerTable()?.primaryStore;
|
|
46
|
+
if (!store || typeof ctx?.transaction?.addWrite !== "function") {
|
|
47
|
+
throw new Error("MemoryHostSource conditional restore unavailable");
|
|
48
|
+
}
|
|
49
|
+
const guard = {
|
|
50
|
+
key: row.memoryId,
|
|
51
|
+
store,
|
|
52
|
+
reloadCommitBase: true,
|
|
53
|
+
entry: undefined,
|
|
54
|
+
commit(_time, entry) {
|
|
55
|
+
if (entry?.value)
|
|
56
|
+
throw new PointerAlreadyStoredError(entry.value);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
ctx.transaction.addWrite(guard);
|
|
60
|
+
// Distinguish a read absence from an entry Harper's save() has not read yet.
|
|
61
|
+
if (guard.entry === undefined)
|
|
62
|
+
guard.entry = null;
|
|
63
|
+
await currentHostPointerAdapter().putPointerRow(row, ctx);
|
|
64
|
+
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* Production behaviour is the real table.
|
|
8
8
|
*/
|
|
9
9
|
import { currentHostPointerAdapter } from "./host-pointer/registry.js";
|
|
10
|
+
export { PointerAlreadyStoredError, restorePointerRowIfAbsent } from "./host-pointer/registry.js";
|
|
10
11
|
export function putPointerRow(row, ctx) {
|
|
11
12
|
return currentHostPointerAdapter().putPointerRow(row, ctx);
|
|
12
13
|
}
|