@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
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { writeJournalScope as active } from "./write-journal-context.js";
|
|
2
|
+
export { currentWriteJournal } from "./write-journal-context.js";
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { databases } from "harper";
|
|
5
|
+
import { isMemoryHostSourceStore, loadStoredPointer, storedPointerIds } from "./memory-host-source.js";
|
|
6
|
+
import { restorePointerRowIfAbsent } from "./host-pointer-adapter.js";
|
|
7
|
+
import { txnPausePoint } from "./txn-pause-point.js";
|
|
8
|
+
function materialize(value, seen = new WeakMap()) {
|
|
9
|
+
if (value == null || typeof value !== "object")
|
|
10
|
+
return value;
|
|
11
|
+
if (seen.has(value))
|
|
12
|
+
return seen.get(value);
|
|
13
|
+
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value instanceof Date || value instanceof RegExp) {
|
|
14
|
+
return structuredClone(value);
|
|
15
|
+
}
|
|
16
|
+
if (value instanceof Map) {
|
|
17
|
+
const result = new Map();
|
|
18
|
+
seen.set(value, result);
|
|
19
|
+
for (const [key, item] of value)
|
|
20
|
+
result.set(materialize(key, seen), materialize(item, seen));
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
if (value instanceof Set) {
|
|
24
|
+
const result = new Set();
|
|
25
|
+
seen.set(value, result);
|
|
26
|
+
for (const item of value)
|
|
27
|
+
result.add(materialize(item, seen));
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
const source = Object.keys(value).length === 0 && typeof value.toJSON === "function" ? value.toJSON() : value;
|
|
31
|
+
const result = Array.isArray(source) ? [] : {};
|
|
32
|
+
seen.set(value, result);
|
|
33
|
+
if (source !== value)
|
|
34
|
+
seen.set(source, result);
|
|
35
|
+
if (Array.isArray(source))
|
|
36
|
+
result.length = source.length;
|
|
37
|
+
for (const key of Object.keys(source)) {
|
|
38
|
+
Object.defineProperty(result, key, { value: materialize(source[key], seen), enumerable: true, writable: true, configurable: true });
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
const copy = (value) => value == null ? undefined : materialize(value);
|
|
43
|
+
const equal = (a, b) => isDeepStrictEqual(copy(a), copy(b));
|
|
44
|
+
function undoable(entry, current) {
|
|
45
|
+
if (!equal(current, entry.after))
|
|
46
|
+
return false;
|
|
47
|
+
if (entry.before !== undefined || !entry.after?.tags?.includes("skill"))
|
|
48
|
+
return true;
|
|
49
|
+
return current && current.archived !== true && !current.validTo &&
|
|
50
|
+
(current.expiresAt == null || (typeof current.expiresAt === "string" && Date.parse(current.expiresAt) > Date.now()));
|
|
51
|
+
}
|
|
52
|
+
/** Captures every addWrite in the owned transaction, including caller hooks. */
|
|
53
|
+
export class WriteJournal {
|
|
54
|
+
entries = [];
|
|
55
|
+
pointerChanged = false;
|
|
56
|
+
compensationAttempted = false;
|
|
57
|
+
staged = new Set();
|
|
58
|
+
transactions = new Set();
|
|
59
|
+
owners = new WeakMap();
|
|
60
|
+
assertCoverage() {
|
|
61
|
+
const recorded = new Set(this.entries.map((entry) => entry.operation));
|
|
62
|
+
for (const txn of this.transactions)
|
|
63
|
+
for (const operation of txn.writes ?? []) {
|
|
64
|
+
if (operation && !recorded.has(operation))
|
|
65
|
+
throw new Error("write journal: staged write has no inverse");
|
|
66
|
+
}
|
|
67
|
+
for (const operation of this.staged) {
|
|
68
|
+
if (!recorded.has(operation))
|
|
69
|
+
throw new Error("write journal: staged write has no inverse");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
run(fn) {
|
|
73
|
+
return active.run(this, fn);
|
|
74
|
+
}
|
|
75
|
+
/** The Memory row ids this journal's writes touched, sorted. Undo holds
|
|
76
|
+
* these ids' per-id write lock around its checks and writes (flair#2950). */
|
|
77
|
+
memoryWriteIds() {
|
|
78
|
+
const store = databases?.flair?.Memory?.primaryStore;
|
|
79
|
+
if (!store)
|
|
80
|
+
return [];
|
|
81
|
+
const ids = new Set();
|
|
82
|
+
for (const entry of this.entries) {
|
|
83
|
+
if (entry.operation?.store === store && entry.operation.key != null) {
|
|
84
|
+
ids.add(String(entry.operation.key));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return [...ids].sort();
|
|
88
|
+
}
|
|
89
|
+
async transaction(ctx, fn) {
|
|
90
|
+
let callbackCompleted = false;
|
|
91
|
+
const releases = [];
|
|
92
|
+
const links = new Set();
|
|
93
|
+
const attach = (txn) => {
|
|
94
|
+
if (!txn || links.has(txn))
|
|
95
|
+
return;
|
|
96
|
+
if (typeof txn.addWrite !== "function")
|
|
97
|
+
throw new Error("write journal: transaction staging unavailable");
|
|
98
|
+
links.add(txn);
|
|
99
|
+
this.transactions.add(txn);
|
|
100
|
+
const addWrite = txn.addWrite;
|
|
101
|
+
const ownAddWrite = Object.getOwnPropertyDescriptor(txn, "addWrite");
|
|
102
|
+
txn.addWrite = (operation) => {
|
|
103
|
+
const entry = { operation, transaction: txn, before: copy(operation.entry?.value), applied: false };
|
|
104
|
+
this.entries.push(entry);
|
|
105
|
+
this.staged.add(operation);
|
|
106
|
+
this.owners.set(operation, txn);
|
|
107
|
+
const commit = operation.commit;
|
|
108
|
+
if (typeof commit !== "function" || !operation.store || operation.key == null) {
|
|
109
|
+
throw new Error("write journal: unsupported staged write");
|
|
110
|
+
}
|
|
111
|
+
operation.commit = (...args) => {
|
|
112
|
+
let prior = operation.priorWrite;
|
|
113
|
+
while (prior && !prior.stagedEntry)
|
|
114
|
+
prior = prior.priorWrite;
|
|
115
|
+
entry.before = copy((prior?.stagedEntry ?? args[1])?.value);
|
|
116
|
+
const result = commit.apply(operation, args);
|
|
117
|
+
if (result?.then)
|
|
118
|
+
throw new Error("write journal: asynchronous commit unsupported");
|
|
119
|
+
entry.applied = operation.stagedEntry !== undefined;
|
|
120
|
+
entry.after = copy(operation.stagedEntry?.value);
|
|
121
|
+
entry.version = args[0];
|
|
122
|
+
return result;
|
|
123
|
+
};
|
|
124
|
+
return addWrite.call(txn, operation);
|
|
125
|
+
};
|
|
126
|
+
const ownNext = Object.getOwnPropertyDescriptor(txn, "next");
|
|
127
|
+
let next = txn.next;
|
|
128
|
+
Object.defineProperty(txn, "next", {
|
|
129
|
+
configurable: true,
|
|
130
|
+
enumerable: ownNext?.enumerable ?? true,
|
|
131
|
+
get: () => next,
|
|
132
|
+
set: (value) => { attach(value); next = value; },
|
|
133
|
+
});
|
|
134
|
+
releases.push(() => {
|
|
135
|
+
if (ownAddWrite)
|
|
136
|
+
Object.defineProperty(txn, "addWrite", ownAddWrite);
|
|
137
|
+
else
|
|
138
|
+
delete txn.addWrite;
|
|
139
|
+
Object.defineProperty(txn, "next", { configurable: true, enumerable: ownNext?.enumerable ?? true, writable: true, value: next });
|
|
140
|
+
});
|
|
141
|
+
attach(next);
|
|
142
|
+
};
|
|
143
|
+
const capture = () => attach(ctx?.transaction);
|
|
144
|
+
try {
|
|
145
|
+
const result = await fn(capture, () => { callbackCompleted = true; });
|
|
146
|
+
this.assertCoverage();
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
if (!callbackCompleted) {
|
|
151
|
+
for (const txn of links)
|
|
152
|
+
this.transactions.delete(txn);
|
|
153
|
+
for (let index = this.entries.length - 1; index >= 0; index--) {
|
|
154
|
+
if (links.has(this.entries[index].transaction))
|
|
155
|
+
this.entries.splice(index, 1);
|
|
156
|
+
}
|
|
157
|
+
for (const operation of this.staged) {
|
|
158
|
+
if (links.has(this.owners.get(operation)))
|
|
159
|
+
this.staged.delete(operation);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
throw err;
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
for (const release of releases.reverse())
|
|
166
|
+
release();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async undo(ctx, transact) {
|
|
170
|
+
return active.run(null, async () => {
|
|
171
|
+
this.assertCoverage();
|
|
172
|
+
let confirmed = true;
|
|
173
|
+
const undone = new Map();
|
|
174
|
+
const versions = new Map();
|
|
175
|
+
for (const entry of [...this.entries].reverse()) {
|
|
176
|
+
if (!entry.applied)
|
|
177
|
+
continue;
|
|
178
|
+
const { operation } = entry;
|
|
179
|
+
const table = Object.values(databases).flatMap((db) => Object.values(db))
|
|
180
|
+
.find((t) => t?.primaryStore === operation.store);
|
|
181
|
+
const raw = Object.getPrototypeOf(table ?? {});
|
|
182
|
+
if (!table || !raw?.get || !raw?.put || !raw?.delete) {
|
|
183
|
+
confirmed = false;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const pointer = isMemoryHostSourceStore(operation.store);
|
|
187
|
+
const read = async (shared) => {
|
|
188
|
+
if (!pointer)
|
|
189
|
+
return raw.get.call(table, operation.key, shared);
|
|
190
|
+
await storedPointerIds([String(operation.key)], shared);
|
|
191
|
+
return loadStoredPointer(String(operation.key), shared);
|
|
192
|
+
};
|
|
193
|
+
const version = table === databases.flair.InstructionVersion;
|
|
194
|
+
const successor = table === databases.flair.Memory && entry.before === undefined && entry.after?.tags?.includes("skill");
|
|
195
|
+
const restore = pointer && entry.before !== undefined && entry.after === undefined;
|
|
196
|
+
const pause = async (pending) => { if (pending)
|
|
197
|
+
await pending; };
|
|
198
|
+
if (successor)
|
|
199
|
+
await pause(txnPausePoint("skill-update-successor-rollback-start"));
|
|
200
|
+
if (restore)
|
|
201
|
+
await pause(txnPausePoint("skill-update-pointer-restore-start"));
|
|
202
|
+
try {
|
|
203
|
+
const current = await read({});
|
|
204
|
+
if (!undoable(entry, current)) {
|
|
205
|
+
if (pointer && current?.hostSource != null)
|
|
206
|
+
this.pointerChanged = true;
|
|
207
|
+
confirmed = false;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const journal = this;
|
|
211
|
+
let undoVersion;
|
|
212
|
+
const expectedVersion = versions.get(operation.store)?.get(operation.key) ?? entry.version;
|
|
213
|
+
await transact(ctx, async (shared) => {
|
|
214
|
+
const stored = await read(shared);
|
|
215
|
+
if (!undoable(entry, stored))
|
|
216
|
+
throw new Error("write journal: undo target changed");
|
|
217
|
+
const guard = {
|
|
218
|
+
store: operation.store, key: operation.key, reloadCommitBase: true, entry: undefined,
|
|
219
|
+
commit(time, value) {
|
|
220
|
+
undoVersion = time;
|
|
221
|
+
if (!undoable(entry, value?.value) || (value && value.version !== expectedVersion)) {
|
|
222
|
+
if (pointer && value?.value?.hostSource != null)
|
|
223
|
+
journal.pointerChanged = true;
|
|
224
|
+
throw new Error("write journal: undo target changed");
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
shared.transaction.addWrite(guard);
|
|
229
|
+
if (guard.entry === undefined)
|
|
230
|
+
guard.entry = null;
|
|
231
|
+
if (entry.before === undefined)
|
|
232
|
+
await raw.delete.call(table, operation.key, shared);
|
|
233
|
+
else if (restore)
|
|
234
|
+
await restorePointerRowIfAbsent(copy(entry.before), shared);
|
|
235
|
+
else
|
|
236
|
+
await raw.put.call(table, copy(entry.before), shared);
|
|
237
|
+
if (version)
|
|
238
|
+
await pause(txnPausePoint("skill-journal-version-rollback-staged"));
|
|
239
|
+
if (restore)
|
|
240
|
+
await pause(txnPausePoint("skill-update-pointer-restore-staged"));
|
|
241
|
+
});
|
|
242
|
+
if (version)
|
|
243
|
+
await pause(txnPausePoint("skill-journal-version-rollback-committed"));
|
|
244
|
+
if (successor)
|
|
245
|
+
await pause(txnPausePoint("skill-update-successor-rollback-committed"));
|
|
246
|
+
if (!equal(await read({}), entry.before))
|
|
247
|
+
confirmed = false;
|
|
248
|
+
const committedEntry = operation.store.getEntry(operation.key, { uncachedRead: true });
|
|
249
|
+
if (committedEntry && committedEntry.version !== undoVersion)
|
|
250
|
+
confirmed = false;
|
|
251
|
+
let byKey = undone.get(operation.store);
|
|
252
|
+
if (!byKey)
|
|
253
|
+
undone.set(operation.store, byKey = new Map());
|
|
254
|
+
byKey.set(operation.key, { read, entry });
|
|
255
|
+
let byVersion = versions.get(operation.store);
|
|
256
|
+
if (!byVersion)
|
|
257
|
+
versions.set(operation.store, byVersion = new Map());
|
|
258
|
+
byVersion.set(operation.key, undoVersion);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
confirmed = false;
|
|
262
|
+
try {
|
|
263
|
+
const current = await read({});
|
|
264
|
+
if (pointer && current?.hostSource != null && !equal(current, entry.before))
|
|
265
|
+
this.pointerChanged = true;
|
|
266
|
+
}
|
|
267
|
+
catch { }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
for (const byKey of undone.values())
|
|
271
|
+
for (const { read, entry } of byKey.values()) {
|
|
272
|
+
try {
|
|
273
|
+
const current = await read({});
|
|
274
|
+
const stored = entry.operation.store.getEntry(entry.operation.key, { uncachedRead: true });
|
|
275
|
+
const version = versions.get(entry.operation.store)?.get(entry.operation.key);
|
|
276
|
+
if (!equal(current, entry.before) || (stored && stored.version !== version)) {
|
|
277
|
+
if (isMemoryHostSourceStore(entry.operation.store) && current?.hostSource != null)
|
|
278
|
+
this.pointerChanged = true;
|
|
279
|
+
confirmed = false;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
confirmed = false;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return confirmed;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
package/dist/src/keystore.js
CHANGED
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
* or an auto-generated random passphrase stored at ~/.flair/keys/.passphrase
|
|
9
9
|
* (mode 0600). Never falls back to guessable data.
|
|
10
10
|
*/
|
|
11
|
-
import { existsSync, mkdirSync, readFileSync,
|
|
11
|
+
import { closeSync, constants, existsSync, fstatSync, linkSync, mkdirSync, openSync, readFileSync, unlinkSync } from "node:fs";
|
|
12
12
|
import { join } from "node:path";
|
|
13
13
|
import { resolveHome } from "./lib/home.js";
|
|
14
|
+
import { agentKeyPath } from "./lib/key-dir.js";
|
|
14
15
|
import { writeFilesAtomically } from "./lib/atomic-write.js";
|
|
15
16
|
import { randomBytes, createCipheriv, createDecipheriv, hkdfSync, } from "node:crypto";
|
|
16
17
|
// ─── Helpers ────────────────────────────────────────────────────────────────
|
|
@@ -28,7 +29,7 @@ function keysDir() {
|
|
|
28
29
|
function keyPath(instanceId) {
|
|
29
30
|
// Sanitize instanceId to prevent directory traversal
|
|
30
31
|
const safe = instanceId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
31
|
-
return
|
|
32
|
+
return agentKeyPath(keysDir(), safe);
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Path to the auto-generated passphrase file.
|
|
@@ -37,20 +38,54 @@ function keyPath(instanceId) {
|
|
|
37
38
|
function passphrasePath() {
|
|
38
39
|
return join(keysDir(), ".passphrase");
|
|
39
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Read the auto-generated passphrase through one no-follow descriptor, after
|
|
43
|
+
* refusing a file wider than owner-only. On Windows the mode check is skipped
|
|
44
|
+
* and the file is read by path.
|
|
45
|
+
*/
|
|
46
|
+
function readPassphraseFile(pp, hooks) {
|
|
47
|
+
const read = hooks.read ?? ((source) => readFileSync(source, "utf-8"));
|
|
48
|
+
const checked = (source) => {
|
|
49
|
+
const passphrase = read(source).trim();
|
|
50
|
+
if (!passphrase)
|
|
51
|
+
throw new Error(`passphrase file ${pp} is empty`);
|
|
52
|
+
return passphrase;
|
|
53
|
+
};
|
|
54
|
+
if ((hooks.platform ?? process.platform) === "win32") {
|
|
55
|
+
return checked(pp);
|
|
56
|
+
}
|
|
57
|
+
const fd = openSync(pp, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
58
|
+
try {
|
|
59
|
+
const st = (hooks.fstat ?? fstatSync)(fd);
|
|
60
|
+
if ((st.mode & 0o077) !== 0) {
|
|
61
|
+
throw new Error(`passphrase file ${pp} is accessible to other users (mode ${(st.mode & 0o777).toString(8).padStart(3, "0")}); ` +
|
|
62
|
+
`run chmod 600 ${pp} to make it owner-only`);
|
|
63
|
+
}
|
|
64
|
+
return checked(fd);
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
closeSync(fd);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
40
70
|
/**
|
|
41
71
|
* Get or create the keystore passphrase.
|
|
42
72
|
* Priority: FLAIR_KEY_PASSPHRASE env var > auto-generated file.
|
|
43
73
|
* Never falls back to guessable data (hostname, username, etc.).
|
|
44
74
|
*/
|
|
45
|
-
function getPassphrase() {
|
|
75
|
+
function getPassphrase(hooks = {}) {
|
|
46
76
|
// Explicit env var takes priority
|
|
47
77
|
if (process.env.FLAIR_KEY_PASSPHRASE) {
|
|
48
78
|
return process.env.FLAIR_KEY_PASSPHRASE;
|
|
49
79
|
}
|
|
50
80
|
const pp = passphrasePath();
|
|
51
|
-
// Read existing auto-generated passphrase
|
|
52
|
-
|
|
53
|
-
|
|
81
|
+
// Read existing auto-generated passphrase; a missing file falls through to
|
|
82
|
+
// generation, and any other open/read failure propagates.
|
|
83
|
+
try {
|
|
84
|
+
return readPassphraseFile(pp, hooks);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (error?.code !== "ENOENT")
|
|
88
|
+
throw error;
|
|
54
89
|
}
|
|
55
90
|
// Generate a cryptographically random passphrase and persist it
|
|
56
91
|
const dir = keysDir();
|
|
@@ -58,8 +93,40 @@ function getPassphrase() {
|
|
|
58
93
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
59
94
|
}
|
|
60
95
|
const generated = randomBytes(32).toString("base64url");
|
|
61
|
-
|
|
62
|
-
|
|
96
|
+
const staged = join(dir, `.passphrase.${process.pid}.${randomBytes(8).toString("hex")}.tmp`);
|
|
97
|
+
writeFilesAtomically([{ path: staged, content: generated, mode: 0o600 }]);
|
|
98
|
+
let passphrase = generated;
|
|
99
|
+
let failure;
|
|
100
|
+
try {
|
|
101
|
+
hooks.beforePublish?.();
|
|
102
|
+
// Link the complete file without replacing a competing passphrase; reread on conflict.
|
|
103
|
+
try {
|
|
104
|
+
(hooks.link ?? linkSync)(staged, pp);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
const code = error?.code;
|
|
108
|
+
if (code !== "EEXIST") {
|
|
109
|
+
throw new Error(`could not publish the generated passphrase at ${pp} (${code}: ${error.message}); ` +
|
|
110
|
+
"if the keys dir's volume does not support hard links, set FLAIR_KEY_PASSPHRASE or use a keys dir on a volume that does");
|
|
111
|
+
}
|
|
112
|
+
passphrase = readPassphraseFile(pp, hooks);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
failure = { error };
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
unlinkSync(staged);
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (error?.code !== "ENOENT") {
|
|
123
|
+
const cleanup = `could not remove staged passphrase ${staged} (${error?.code}: ${error.message})`;
|
|
124
|
+
throw new Error(`${failure ? failure.error.message : `passphrase at ${pp}`}; ${cleanup}`, { cause: failure ? failure.error : error });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (failure)
|
|
128
|
+
throw failure.error;
|
|
129
|
+
return passphrase;
|
|
63
130
|
}
|
|
64
131
|
/**
|
|
65
132
|
* Derive a 256-bit encryption key using HKDF.
|
|
@@ -116,7 +183,7 @@ class FileKeyStore {
|
|
|
116
183
|
}
|
|
117
184
|
const p = keyPath(instanceId);
|
|
118
185
|
const encrypted = encryptSeed(seed);
|
|
119
|
-
|
|
186
|
+
writeFilesAtomically([{ path: p, content: encrypted, mode: 0o600 }]);
|
|
120
187
|
}
|
|
121
188
|
}
|
|
122
189
|
/** Singleton keystore instance. */
|
|
@@ -193,4 +260,4 @@ export function recordSeedOwner(instanceId, dataDir) {
|
|
|
193
260
|
]);
|
|
194
261
|
}
|
|
195
262
|
// Export helpers for testing
|
|
196
|
-
export { encryptSeed, decryptSeed, deriveKey, keyPath, keysDir };
|
|
263
|
+
export { encryptSeed, decryptSeed, deriveKey, getPassphrase, keyPath, keysDir };
|
|
@@ -29,6 +29,18 @@ export const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
|
29
29
|
* refuse with. The CLI and JIT-principal writers refuse with the same message
|
|
30
30
|
* text but not this code. */
|
|
31
31
|
export const AGENT_ID_ERROR = "invalid_agent_id";
|
|
32
|
+
/** The remedy for an id that cannot be used as a JSON integer: a BigInt, which
|
|
33
|
+
* cannot be JSON-serialized, or an integral number outside the safe-integer
|
|
34
|
+
* range, whose value can no longer be trusted to round-trip. */
|
|
35
|
+
export const AGENT_ID_STRING_REMEDY = "send the agent id as a string";
|
|
36
|
+
/** True for an id Harper delivered from a JSON integer whose value JavaScript
|
|
37
|
+
* cannot trust to round-trip. A BigInt is never an agent ID; an unsafe number
|
|
38
|
+
* cannot be trusted when compared or stored, so isValidAgentId refuses it
|
|
39
|
+
* too. */
|
|
40
|
+
export function isUnsafeAgentIdWireValue(id) {
|
|
41
|
+
return (typeof id === "bigint" ||
|
|
42
|
+
(typeof id === "number" && Number.isInteger(id) && !Number.isSafeInteger(id)));
|
|
43
|
+
}
|
|
32
44
|
/**
|
|
33
45
|
* Is `id` an agent ID this rule accepts? A non-string, non-number value —
|
|
34
46
|
* including `null` — is never valid. A write path must distinguish an OMITTED
|
|
@@ -38,9 +50,39 @@ export const AGENT_ID_ERROR = "invalid_agent_id";
|
|
|
38
50
|
export function isValidAgentId(id) {
|
|
39
51
|
if (typeof id !== "string" && typeof id !== "number")
|
|
40
52
|
return false;
|
|
53
|
+
if (typeof id === "number" && !Number.isSafeInteger(id))
|
|
54
|
+
return false;
|
|
41
55
|
return AGENT_ID_PATTERN.test(String(id));
|
|
42
56
|
}
|
|
43
|
-
/** A human-readable refusal
|
|
57
|
+
/** A human-readable refusal. A BigInt or an unsafe integer gets the string
|
|
58
|
+
* remedy alone (no rule). Every other value gets
|
|
59
|
+
* "invalid agent id <shown>: <rule>", where a Symbol or a non-finite number
|
|
60
|
+
* or undefined is shown as String(id) and anything else as JSON.stringify(id); after
|
|
61
|
+
* those branches, if Array.isArray (for a revoked Proxy) or JSON.stringify
|
|
62
|
+
* throws, or JSON.stringify returns undefined, the value is reported by type:
|
|
63
|
+
* "invalid agent id (<type> that cannot be serialized): <rule>"
|
|
64
|
+
* instead. */
|
|
44
65
|
export function invalidAgentIdMessage(id) {
|
|
45
|
-
|
|
66
|
+
if (isUnsafeAgentIdWireValue(id))
|
|
67
|
+
return AGENT_ID_STRING_REMEDY;
|
|
68
|
+
const rule = `an agent id must match ${AGENT_ID_RULE} (1-64 characters: A-Z, a-z, 0-9, _ or -)`;
|
|
69
|
+
let shown;
|
|
70
|
+
let isArray = false;
|
|
71
|
+
if (id === undefined || typeof id === "symbol" || (typeof id === "number" && !Number.isFinite(id))) {
|
|
72
|
+
shown = String(id);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
try {
|
|
76
|
+
isArray = Array.isArray(id);
|
|
77
|
+
shown = JSON.stringify(id);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Array.isArray (for a revoked Proxy) or JSON.stringify can throw; report by type below.
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (shown === undefined) {
|
|
84
|
+
const type = isArray ? "an array" : typeof id === "object" ? "an object" : `a ${typeof id}`;
|
|
85
|
+
return `invalid agent id (${type} that cannot be serialized): ${rule}`;
|
|
86
|
+
}
|
|
87
|
+
return `invalid agent id ${shown}: ${rule}`;
|
|
46
88
|
}
|
|
@@ -47,7 +47,7 @@ function resolveHooks(h) {
|
|
|
47
47
|
return {
|
|
48
48
|
open: h.open ?? ((p, flags, mode) => openSync(p, flags, mode)),
|
|
49
49
|
write: h.write ?? ((fd, data) => {
|
|
50
|
-
const buf = Buffer.from(data, "utf-8");
|
|
50
|
+
const buf = typeof data === "string" ? Buffer.from(data, "utf-8") : Buffer.from(data);
|
|
51
51
|
let off = 0;
|
|
52
52
|
while (off < buf.length)
|
|
53
53
|
off += writeSync(fd, buf, off, buf.length - off);
|
|
@@ -148,7 +148,7 @@ function stage(fs, target, content, mode) {
|
|
|
148
148
|
return tmp;
|
|
149
149
|
}
|
|
150
150
|
/** Best-effort directory fsync so the renames themselves are durable. */
|
|
151
|
-
function fsyncDir(dir) {
|
|
151
|
+
export function fsyncDir(dir) {
|
|
152
152
|
try {
|
|
153
153
|
const fd = openSync(dir, "r");
|
|
154
154
|
try {
|
|
@@ -184,7 +184,7 @@ export function writeFilesAtomically(entries, hooks = {}) {
|
|
|
184
184
|
if (!fs.exists(e.path))
|
|
185
185
|
return { path: e.path, existed: false };
|
|
186
186
|
// A planned-from target is restored from the bytes it was re-checked to hold.
|
|
187
|
-
const content = e.expect ? e.expect.content : fs.
|
|
187
|
+
const content = e.expect ? Buffer.from(e.expect.content, "utf-8") : fs.readBytes(e.path);
|
|
188
188
|
return { path: e.path, existed: true, content, mode: fs.modeOf(e.path) };
|
|
189
189
|
});
|
|
190
190
|
// 2. Stage every new file. Nothing visible has changed yet.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* federation-retry-row.ts — a federation retry row's key, outcome vocabulary
|
|
3
|
+
* and sender-convention check, in one module.
|
|
4
|
+
*
|
|
5
|
+
* Pure, DB-free and dependency-free: a resource imports it directly
|
|
6
|
+
* (resources/federation-retry-lib.ts re-exports it), and a src module that a
|
|
7
|
+
* resource imports must load no other module (test/unit/resource-src-purity-
|
|
8
|
+
* 1775.test.ts). The CLI build (src/) and the Harper resources share this one
|
|
9
|
+
* copy. The receiver's admin/read resources and the generic-Memory sender both
|
|
10
|
+
* classify rows with retryRowProblem: the retry selector excludes a malformed
|
|
11
|
+
* row, while an eligible cursor-selected source is still sent and its
|
|
12
|
+
* malformed retry row is left as stored. The reason vocabulary differs: the
|
|
13
|
+
* caller passes the predicate
|
|
14
|
+
* its own path uses (`isSkipReason` plus local reasons for a Harper resource;
|
|
15
|
+
* the sender's stricter pending-row list).
|
|
16
|
+
*/
|
|
17
|
+
// ─── Key ───
|
|
18
|
+
/**
|
|
19
|
+
* Encode the (peerId, table, recordId) tuple as the row's primary key.
|
|
20
|
+
*
|
|
21
|
+
* A JSON array join, never a bare concatenation: a naive `${peerId}${table}
|
|
22
|
+
* ${recordId}` collides (peer `a` + table `bc` is peer `ab` + table `c`), and
|
|
23
|
+
* peer and record ids are arbitrary strings off the wire, including legacy
|
|
24
|
+
* `.content`-suffixed ids. JSON quotes and escapes each component, so distinct
|
|
25
|
+
* tuples encode to distinct strings and the components round-trip exactly.
|
|
26
|
+
*
|
|
27
|
+
* Every component must be a non-empty string; a missing component is a caller
|
|
28
|
+
* bug, not a row to write, so this THROWS rather than mint a malformed key.
|
|
29
|
+
*/
|
|
30
|
+
export function retryEntryKey(peerId, table, recordId) {
|
|
31
|
+
for (const [name, value] of [
|
|
32
|
+
["peerId", peerId],
|
|
33
|
+
["table", table],
|
|
34
|
+
["recordId", recordId],
|
|
35
|
+
]) {
|
|
36
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
37
|
+
throw new Error(`federation retry: ${name} must be a non-empty string`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return JSON.stringify([peerId, table, recordId]);
|
|
41
|
+
}
|
|
42
|
+
/** Decode {@link retryEntryKey}'s output, or null when `id` is not a retry key. */
|
|
43
|
+
export function parseRetryEntryKey(id) {
|
|
44
|
+
let parsed;
|
|
45
|
+
try {
|
|
46
|
+
parsed = JSON.parse(id);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(parsed) || parsed.length !== 3)
|
|
52
|
+
return null;
|
|
53
|
+
const [peerId, table, recordId] = parsed;
|
|
54
|
+
if (typeof peerId !== "string" || typeof table !== "string" || typeof recordId !== "string")
|
|
55
|
+
return null;
|
|
56
|
+
if (peerId.length === 0 || table.length === 0 || recordId.length === 0)
|
|
57
|
+
return null;
|
|
58
|
+
return { peerId, table, recordId };
|
|
59
|
+
}
|
|
60
|
+
export const RETRY_RESOLUTIONS = new Set([
|
|
61
|
+
"merged",
|
|
62
|
+
"refused_terminal",
|
|
63
|
+
"source_deleted",
|
|
64
|
+
"source_ineligible",
|
|
65
|
+
"peer_unavailable",
|
|
66
|
+
"retry_exhausted",
|
|
67
|
+
]);
|
|
68
|
+
export const RETRY_BOUNDS = new Set(["attempts", "age", "both"]);
|
|
69
|
+
export function isRetryResolution(value) {
|
|
70
|
+
return typeof value === "string" && RETRY_RESOLUTIONS.has(value);
|
|
71
|
+
}
|
|
72
|
+
export function isRetryBound(value) {
|
|
73
|
+
return typeof value === "string" && RETRY_BOUNDS.has(value);
|
|
74
|
+
}
|
|
75
|
+
export function retryRowProblem(row, isKnownReason) {
|
|
76
|
+
let expectedId = null;
|
|
77
|
+
try {
|
|
78
|
+
expectedId = retryEntryKey(row.peerId, row.table, row.recordId);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
expectedId = null;
|
|
82
|
+
}
|
|
83
|
+
if (expectedId === null || row.id !== expectedId)
|
|
84
|
+
return "id_mismatch";
|
|
85
|
+
if (row.state !== "pending" && row.state !== "terminal")
|
|
86
|
+
return "unknown_state";
|
|
87
|
+
if (row.lastReason != null && !isKnownReason(row.lastReason))
|
|
88
|
+
return "unknown_reason";
|
|
89
|
+
if (row.state === "pending") {
|
|
90
|
+
if (row.resolution != null || row.bound != null)
|
|
91
|
+
return "outcome_mismatch";
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (!isRetryResolution(row.resolution))
|
|
95
|
+
return "outcome_mismatch";
|
|
96
|
+
const exhausted = row.resolution === "retry_exhausted";
|
|
97
|
+
if (exhausted ? !isRetryBound(row.bound) : row.bound != null)
|
|
98
|
+
return "outcome_mismatch";
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* instruction-event-contract.ts — the shared reserved-event predicate
|
|
3
|
+
* (flair#2139 slice 3a). Kept in `src/lib` so both the OrgEvent resource and
|
|
4
|
+
* the `flair doctor` check import one definition without crossing the CLI
|
|
5
|
+
* build's rootDir.
|
|
6
|
+
*/
|
|
7
|
+
export const RESERVED_EVENT_KINDS = Object.freeze(["soul.changed", "skill.changed"]);
|
|
8
|
+
export const RESERVED_EVENT_ID_PREFIX = "instruction-change:";
|
|
9
|
+
/** Whether a record carries a reserved kind or id prefix. Non-records are not reserved. */
|
|
10
|
+
export function isReservedEventRecord(record) {
|
|
11
|
+
if (record == null || typeof record !== "object" || Array.isArray(record))
|
|
12
|
+
return false;
|
|
13
|
+
const { kind, id } = record;
|
|
14
|
+
if (typeof kind === "string" && RESERVED_EVENT_KINDS.includes(kind))
|
|
15
|
+
return true;
|
|
16
|
+
return typeof id === "string" && id.startsWith(RESERVED_EVENT_ID_PREFIX);
|
|
17
|
+
}
|