@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,235 @@
|
|
|
1
|
+
import { databases } from "harper";
|
|
2
|
+
import { localInstanceId } from "./instance-identity.js";
|
|
3
|
+
/**
|
|
4
|
+
* originator-instance.ts — the single resource-layer delegate that enforces the
|
|
5
|
+
* server-stamped `originatorInstanceId` contract on every REST write path
|
|
6
|
+
* (flair#1965).
|
|
7
|
+
*
|
|
8
|
+
* ─── The contract ────────────────────────────────────────────────────────────
|
|
9
|
+
*
|
|
10
|
+
* `originatorInstanceId` names the federation instance that AUTHORED a record
|
|
11
|
+
* (schemas/memory.graphql, agent.graphql): it is stamped server-side and is
|
|
12
|
+
* never client-writable. Before this module each of Memory/Soul/Agent/
|
|
13
|
+
* Relationship only stamped it when the request body's value was null
|
|
14
|
+
* (`content.originatorInstanceId == null`), so a caller could supply or change
|
|
15
|
+
* it on create AND on update. The rule enforced now is:
|
|
16
|
+
*
|
|
17
|
+
* - CREATE (`post()`, and a `put()`/`patch()` that lands on a row with no
|
|
18
|
+
* stored counterpart): the local instance id is stamped; ANY request-body
|
|
19
|
+
* value is ignored.
|
|
20
|
+
* - UPDATE (`put()`/`patch()` over an existing row): the STORED value
|
|
21
|
+
* stands. A body value neither replaces nor clears it, and omitting the
|
|
22
|
+
* field leaves it exactly as stored (a legacy row with no value is left
|
|
23
|
+
* without one — the field is additive, never invented on update).
|
|
24
|
+
* - FEDERATION MERGE: unchanged. The trusted apply path
|
|
25
|
+
* (resources/Federation.ts's `FederationSync.post()`) merges a peer's row
|
|
26
|
+
* through the RAW table handle — `(databases as any).flair.Memory.put(
|
|
27
|
+
* mergedData)`, i.e. Harper's static table-level put — never through a
|
|
28
|
+
* resource's post()/put()/patch(). The originator's own value carried in
|
|
29
|
+
* `record.data` (selected by `mergeRecord`'s LWW, or the inbound row for a
|
|
30
|
+
* new one) is therefore preserved verbatim. `_originatorInstanceId` (the
|
|
31
|
+
* underscore form) is the separate RECEIVER-side bookkeeping stamp that
|
|
32
|
+
* same path writes.
|
|
33
|
+
*
|
|
34
|
+
* The wire field is server-stamped on EVERY application write. The federation
|
|
35
|
+
* merge is the one path that carries a peer's value, and it is not
|
|
36
|
+
* "body-less": `POST /FederationSync` IS REST-reachable and parses a request
|
|
37
|
+
* body, but that body is trusted only because the handler verifies a batch
|
|
38
|
+
* signature against the sending PAIRED PEER's pinned public key (and each
|
|
39
|
+
* record's own signature against its claimed originator's pinned key) before
|
|
40
|
+
* the raw-table merge. No unauthenticated caller reaches the raw write, and the
|
|
41
|
+
* merge never reads a request-body field into the stored `originatorInstanceId`
|
|
42
|
+
* — it takes the value from the already-authenticated `record.data`.
|
|
43
|
+
*
|
|
44
|
+
* `localInstanceId()` resolves to null on an instance that has never been
|
|
45
|
+
* federation-bootstrapped (no single Instance row yet) — the field is nullable
|
|
46
|
+
* by design, so a create stamps null rather than inventing an id.
|
|
47
|
+
*/
|
|
48
|
+
function isPlainObject(value) {
|
|
49
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
/** A stored row is a plain object; a `Response` (a refusal the caller's own
|
|
52
|
+
* get() produced) or anything else is not. */
|
|
53
|
+
function isStoredRow(value) {
|
|
54
|
+
return isPlainObject(value) && !(value instanceof Response);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* CREATE path — stamp this instance's own federation id, ignoring any value the
|
|
58
|
+
* request body carried. Used by `post()` (always a create) and by a
|
|
59
|
+
* `put()`/`patch()` whose target id has no stored row yet.
|
|
60
|
+
*/
|
|
61
|
+
export async function stampOriginatorOnCreate(content) {
|
|
62
|
+
if (!isPlainObject(content))
|
|
63
|
+
return;
|
|
64
|
+
content.originatorInstanceId = await localInstanceId();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* UPDATE path — the STORED value stands. A body value is dropped (neither
|
|
68
|
+
* replaces nor clears it); when the stored row carries no value the field is
|
|
69
|
+
* left off the write so a legacy row stays un-stamped rather than gaining a
|
|
70
|
+
* fresh local id.
|
|
71
|
+
*
|
|
72
|
+
* `stored` is the row this write is over. For a `put()` (which REPLACES the
|
|
73
|
+
* row) the stored value is re-asserted explicitly; for a merging `patch()` the
|
|
74
|
+
* field is simply removed from the body so the stored value survives. Both use
|
|
75
|
+
* this one function so the two verbs cannot drift.
|
|
76
|
+
*/
|
|
77
|
+
export function keepStoredOriginator(content, stored) {
|
|
78
|
+
if (!isPlainObject(content))
|
|
79
|
+
return;
|
|
80
|
+
if (isPlainObject(stored) && stored.originatorInstanceId !== undefined) {
|
|
81
|
+
content.originatorInstanceId = stored.originatorInstanceId;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
delete content.originatorInstanceId;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Apply the full create/update rule: `stored` is the row this write is over
|
|
89
|
+
* (null/undefined ⇒ a create). This is the one call a `put()` or `patch()` needs
|
|
90
|
+
* after it has resolved the pre-existing row.
|
|
91
|
+
*/
|
|
92
|
+
export async function applyOriginatorInstanceId(content, stored) {
|
|
93
|
+
if (isStoredRow(stored)) {
|
|
94
|
+
keepStoredOriginator(content, stored);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
await stampOriginatorOnCreate(content);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* PATCH path — a patch MERGES into the stored row (Harper partial update), so a
|
|
102
|
+
* body value can be dropped and the stored value stands untouched.
|
|
103
|
+
*/
|
|
104
|
+
export function dropClientOriginator(content) {
|
|
105
|
+
if (isPlainObject(content))
|
|
106
|
+
delete content.originatorInstanceId;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* ─── Federation bookkeeping (the underscore receiver stamps) ────────────────
|
|
110
|
+
*
|
|
111
|
+
* `_originatorInstanceId`, `_syncedFrom` and `_syncedAt` are the RECEIVER-side
|
|
112
|
+
* bookkeeping written by resources/Federation.ts's merge (through the RAW table
|
|
113
|
+
* handle) on every synced row, and rendered by resources/AdminMemory.ts's
|
|
114
|
+
* admin provenance pane. They are on the Memory writer whitelist so the merge
|
|
115
|
+
* can write them, but they are NOT client-settable: a write body must never
|
|
116
|
+
* pass one through (a forged value would dress a local row up as a synced one
|
|
117
|
+
* in the admin pane). The federation merge writes them AFTER its whitelist
|
|
118
|
+
* pass, so it is unaffected by the application-side strip below.
|
|
119
|
+
*/
|
|
120
|
+
const FEDERATION_BOOKKEEPING_FIELDS = ["_originatorInstanceId", "_syncedFrom", "_syncedAt"];
|
|
121
|
+
/** CREATE path — drop any client-supplied receiver bookkeeping. */
|
|
122
|
+
export function dropClientFederationBookkeeping(content) {
|
|
123
|
+
if (!isPlainObject(content))
|
|
124
|
+
return;
|
|
125
|
+
for (const field of FEDERATION_BOOKKEEPING_FIELDS)
|
|
126
|
+
delete content[field];
|
|
127
|
+
}
|
|
128
|
+
/** UPDATE path (full replace) — the STORED receiver stamps stand. */
|
|
129
|
+
export function keepStoredFederationBookkeeping(content, stored) {
|
|
130
|
+
if (!isPlainObject(content))
|
|
131
|
+
return;
|
|
132
|
+
for (const field of FEDERATION_BOOKKEEPING_FIELDS) {
|
|
133
|
+
if (isPlainObject(stored) && stored[field] !== undefined) {
|
|
134
|
+
content[field] = stored[field];
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
delete content[field];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Apply the create/update rule for the receiver bookkeeping: `stored` ⇒ keep its
|
|
143
|
+
* values (re-asserted onto a full-replace put); no stored row ⇒ a client body
|
|
144
|
+
* may not set them.
|
|
145
|
+
*/
|
|
146
|
+
export function applyFederationBookkeeping(content, stored) {
|
|
147
|
+
if (isStoredRow(stored)) {
|
|
148
|
+
keepStoredFederationBookkeeping(content, stored);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
dropClientFederationBookkeeping(content);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** The URL-bound target id of a resource instance: `getId()` on a real Harper
|
|
155
|
+
* resource. Only a scalar id is usable; anything else (no method, a thrown
|
|
156
|
+
* call, a non-scalar id) means "no URL target to consult". */
|
|
157
|
+
function urlTargetId(resource) {
|
|
158
|
+
try {
|
|
159
|
+
const getId = resource?.getId;
|
|
160
|
+
const id = typeof getId === "function" ? getId.call(resource) : undefined;
|
|
161
|
+
if (typeof id === "string" || typeof id === "number")
|
|
162
|
+
return id;
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
/* getId() threw — fall back to the body id below */
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
/** A 400 for a body `id` that disagrees with the address the write was sent to:
|
|
170
|
+
* Harper writes to the URL-bound target and rewrites the row's primary key to
|
|
171
|
+
* it, so a mismatching body id names a row this write does NOT land on. */
|
|
172
|
+
function idTargetMismatchDenial(bodyId, targetId) {
|
|
173
|
+
return new Response(JSON.stringify({
|
|
174
|
+
error: "id_target_mismatch",
|
|
175
|
+
message: `the request body id ${JSON.stringify(bodyId)} does not match the address it was sent to (${JSON.stringify(targetId)})`,
|
|
176
|
+
}), { status: 400, headers: { "content-type": "application/json" } });
|
|
177
|
+
}
|
|
178
|
+
/** A 500 for a stored-row lookup that FAILED. A lookup failure is never read as
|
|
179
|
+
* "no stored row": treating it as a create would let an existing row be
|
|
180
|
+
* re-stamped when the read fails but the write succeeds. */
|
|
181
|
+
function storedRowLookupFailure(tableName, err) {
|
|
182
|
+
// Constant format string + a structured data object: a template literal here
|
|
183
|
+
// would let anything interpolated into the format position forge the log
|
|
184
|
+
// (semgrep javascript.lang.security.audit.unsafe-formatstring).
|
|
185
|
+
console.error("originator-instance: the stored row could not be read, so the write was refused", { table: tableName, err });
|
|
186
|
+
return new Response(JSON.stringify({
|
|
187
|
+
error: "stored_row_lookup_failed",
|
|
188
|
+
message: "the existing record could not be read, so the write was refused",
|
|
189
|
+
}), { status: 500, headers: { "content-type": "application/json" } });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Resolve the stored row a `put()`/`patch()` is over, for the create/update
|
|
193
|
+
* decision.
|
|
194
|
+
*
|
|
195
|
+
* The authoritative id is the URL-BOUND target (`resource.getId()`), NEVER the
|
|
196
|
+
* request body's `id`: Harper writes to the URL target and rewrites the stored
|
|
197
|
+
* primary key to it (dist/resources/Table.js `_writeUpdate`), so a body id is
|
|
198
|
+
* not the row this write lands on. Reading the stored row by a body id let a
|
|
199
|
+
* caller borrow another row's stored originator — or, via a nonexistent id,
|
|
200
|
+
* force a "create" that re-stamped an existing row.
|
|
201
|
+
*
|
|
202
|
+
* Rules:
|
|
203
|
+
* - a body `id` that disagrees with the URL target id is REFUSED;
|
|
204
|
+
* - the stored row is read by the URL target id (via the raw table reader,
|
|
205
|
+
* falling back to the resource's own bound-record read when the table
|
|
206
|
+
* exposes no static reader — an in-process harness);
|
|
207
|
+
* - a read FAILURE refuses the write — it is never read as "no stored row".
|
|
208
|
+
*
|
|
209
|
+
* An in-process call with no URL-bound id (a direct `new Resource().put(body)`)
|
|
210
|
+
* falls back to the body `id`, which IS the write key on that path.
|
|
211
|
+
*/
|
|
212
|
+
export async function resolveStoredRow(resource, tableName, content, getUrlBound) {
|
|
213
|
+
const targetId = urlTargetId(resource);
|
|
214
|
+
const bodyId = isPlainObject(content) && content.id != null ? content.id : undefined;
|
|
215
|
+
if (targetId != null && bodyId !== undefined && String(bodyId) !== String(targetId)) {
|
|
216
|
+
return { row: null, denial: idTargetMismatchDenial(bodyId, targetId) };
|
|
217
|
+
}
|
|
218
|
+
const id = targetId ?? bodyId;
|
|
219
|
+
if (id == null)
|
|
220
|
+
return { row: null };
|
|
221
|
+
try {
|
|
222
|
+
const table = databases.flair?.[tableName];
|
|
223
|
+
if (table && typeof table.get === "function") {
|
|
224
|
+
const row = await table.get(id);
|
|
225
|
+
return { row: isStoredRow(row) ? row : null };
|
|
226
|
+
}
|
|
227
|
+
// No static table reader (an in-process harness): read the resource's own
|
|
228
|
+
// URL-bound record instead — the row this write lands on.
|
|
229
|
+
const bound = await getUrlBound();
|
|
230
|
+
return { row: isStoredRow(bound) ? bound : null };
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
return { row: null, denial: storedRowLookupFailure(tableName, err) };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { withDetachedTxn } from "./table-helpers.js";
|
|
2
2
|
import { databases } from "harper";
|
|
3
|
+
import { stripUndeclaredMemoryAttributes } from "./memory-declared-attributes.js";
|
|
3
4
|
import { noteMemoryUpsert } from "./bm25-index-service.js";
|
|
4
5
|
// Called only after the promotion workflow has authorized and written a Memory
|
|
5
6
|
// through its resource (safety, embedding, ownership and provenance still run).
|
|
@@ -12,6 +13,7 @@ export async function stampMemoryPromotion(id, reviewerId, decidedAt, enumeratio
|
|
|
12
13
|
if (!stored)
|
|
13
14
|
throw new Error(`Promotion memory ${id} was not written`);
|
|
14
15
|
const row = { ...stored, promotionStatus: "approved", promotedBy: reviewerId, promotedAt: decidedAt };
|
|
16
|
+
stripUndeclaredMemoryAttributes(row);
|
|
15
17
|
await withDetachedTxn(enumerationContext, () => table.put(row));
|
|
16
18
|
noteMemoryUpsert(row);
|
|
17
19
|
}
|
|
@@ -38,8 +38,8 @@ function sanitizeClaim(value) {
|
|
|
38
38
|
* CLAIMS. Deliberately minimal — verified fields only:
|
|
39
39
|
*
|
|
40
40
|
* { v: 1,
|
|
41
|
-
* verified: { agentId: <string|null>, timestamp: <ISO string> },
|
|
42
|
-
* claimed?: { model?: <string>, client?: <string> } }
|
|
41
|
+
* verified: { agentId: <string|null>, timestamp: <ISO string>, receivedAt: <ISO string> },
|
|
42
|
+
* claimed?: { createdAt?: <string>, model?: <string>, client?: <string> } }
|
|
43
43
|
*
|
|
44
44
|
* - `verified.agentId` comes from the ALREADY-RESOLVED auth verdict
|
|
45
45
|
* (resolveAgentAuth) — never from anything the caller can forge on the
|
|
@@ -48,10 +48,31 @@ function sanitizeClaim(value) {
|
|
|
48
48
|
* with no per-agent identity to attribute) stamps `null` rather than
|
|
49
49
|
* throwing; `kind: "anonymous"` never reaches here — every write path
|
|
50
50
|
* already 401s it before this point.
|
|
51
|
-
* - `verified.timestamp`
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
51
|
+
* - `verified.timestamp` is stamped from the SERVER clock at write time
|
|
52
|
+
* (flair#1960) — the SAME single clock read as `verified.receivedAt`, because
|
|
53
|
+
* both assert the server's write instant (a second read could differ by
|
|
54
|
+
* milliseconds and make the two fields disagree). Never client-suppliable:
|
|
55
|
+
* the caller's `createdAt` is a CLAIM and is recorded separately under
|
|
56
|
+
* `claimed.createdAt` (below), never here. Every field under `verified` is
|
|
57
|
+
* server-derived.
|
|
58
|
+
* The clock is an injectable `() => Date` (defaulting to the wall clock) so
|
|
59
|
+
* the "one read" contract is TESTABLE: a counting/advancing fake clock fails
|
|
60
|
+
* the test deterministically if a second read is ever added (flair#1960 r2 —
|
|
61
|
+
* comparing two wall-clock reads only catches a drift of >0 ms, and two reads
|
|
62
|
+
* almost always land in the SAME millisecond, so the old equality-only test
|
|
63
|
+
* CAUGHT a second read only ~7 times in 10,000 — it MISSED it the other
|
|
64
|
+
* ~9,993). Production callers never pass one.
|
|
65
|
+
* - The host pointer (`hostSource`) is NOT provenance (flair#1940 A5): it lives
|
|
66
|
+
* in its own `MemoryHostSource` table, is never part of this `{ v, verified,
|
|
67
|
+
* claimed }` blob, and is written on the Memory write path in the same
|
|
68
|
+
* transaction as the Memory row (t1/t2). `provenance` therefore stays a field
|
|
69
|
+
* the server controls and is never client-writable.
|
|
70
|
+
* - `claimed.createdAt` (flair#1960) is the caller's claim on the record's
|
|
71
|
+
* creation time — the `createdAt` the write carries, sanitized like the
|
|
72
|
+
* other claims (string-only, control-char strip, trim, 200-char cap,
|
|
73
|
+
* drop-if-empty). It is a CLAIM, never verified: the server's own write
|
|
74
|
+
* instant lives in `verified.timestamp`. The record's own `createdAt` field
|
|
75
|
+
* still carries the caller's value unchanged.
|
|
55
76
|
* - `claimed.model` is an OPTIONAL, UNVERIFIED passthrough: included only
|
|
56
77
|
* when the incoming write payload itself already carries a non-empty
|
|
57
78
|
* string `model` field (sanitized via sanitizeClaim above). Never
|
|
@@ -71,8 +92,9 @@ function sanitizeClaim(value) {
|
|
|
71
92
|
* OAuth path, the caller is required to source this from the verified
|
|
72
93
|
* `client_id` token claim, never the user-controlled `client_name` — see
|
|
73
94
|
* resources/mcp-handler.ts's handleToolCall for that stamp site.
|
|
74
|
-
* - The `claimed` key is omitted entirely (not stamped as `{}`) when
|
|
75
|
-
* `model` and `client` are absent.
|
|
95
|
+
* - The `claimed` key is omitted entirely (not stamped as `{}`) when
|
|
96
|
+
* `createdAt`, `model` and `client` are ALL absent after sanitization (e.g.
|
|
97
|
+
* an empty or all-control-chars `createdAt` with no model/client claims).
|
|
76
98
|
*
|
|
77
99
|
* Originally introduced in resources/Memory.ts (Memory.post()/Memory.put());
|
|
78
100
|
* extracted here so Relationship.ts (and any future write path) can reuse the
|
|
@@ -81,18 +103,33 @@ function sanitizeClaim(value) {
|
|
|
81
103
|
* buildProvenance as-is," which this module makes literal (one function, one
|
|
82
104
|
* shape, imported by every writer) instead of a copy that could drift.
|
|
83
105
|
*/
|
|
84
|
-
export function buildProvenance(auth, createdAt, content) {
|
|
106
|
+
export function buildProvenance(auth, createdAt, content, clock = () => new Date()) {
|
|
107
|
+
// ONE clock read per write, shared by BOTH server-derived timestamps
|
|
108
|
+
// (flair#1960): `verified.timestamp` and `verified.receivedAt` assert the
|
|
109
|
+
// same server write instant, so a single `clock()` call keeps them identical
|
|
110
|
+
// instead of letting a second read drift them apart by milliseconds.
|
|
111
|
+
const serverNow = clock().toISOString();
|
|
85
112
|
const provenance = {
|
|
86
113
|
v: 1,
|
|
87
114
|
verified: {
|
|
88
115
|
agentId: auth.kind === "agent" ? auth.agentId : null,
|
|
89
|
-
|
|
116
|
+
// flair#1960: the server's write instant — NEVER the caller's `createdAt`.
|
|
117
|
+
timestamp: serverNow,
|
|
118
|
+
// flair#1940 A4: the server's RECEIPT time — same clock read as above.
|
|
119
|
+
receivedAt: serverNow,
|
|
90
120
|
},
|
|
91
121
|
};
|
|
122
|
+
// The caller's `createdAt` is their CLAIM on the record's creation time; it is
|
|
123
|
+
// recorded under `claimed` (sanitized like the other claims) so a reader can
|
|
124
|
+
// compare the claim against the server's `verified.timestamp`. The record's
|
|
125
|
+
// own `createdAt` field keeps the claim unchanged (flair#1960).
|
|
126
|
+
const claimedCreatedAt = sanitizeClaim(createdAt);
|
|
92
127
|
const model = sanitizeClaim(content?.model);
|
|
93
128
|
const client = sanitizeClaim(content?.claimedClient);
|
|
94
|
-
if (model !== undefined || client !== undefined) {
|
|
129
|
+
if (claimedCreatedAt !== undefined || model !== undefined || client !== undefined) {
|
|
95
130
|
provenance.claimed = {};
|
|
131
|
+
if (claimedCreatedAt !== undefined)
|
|
132
|
+
provenance.claimed.createdAt = claimedCreatedAt;
|
|
96
133
|
if (model !== undefined)
|
|
97
134
|
provenance.claimed.model = model;
|
|
98
135
|
if (client !== undefined)
|
|
@@ -100,3 +137,66 @@ export function buildProvenance(auth, createdAt, content) {
|
|
|
100
137
|
}
|
|
101
138
|
return JSON.stringify(provenance);
|
|
102
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* The fields whose change makes a memory write SEMANTIC — the text the record
|
|
142
|
+
* actually says, plus its creation claim. A PATCH that changes one of these is
|
|
143
|
+
* re-authoring content (and, for `createdAt`, re-authoring the creation claim),
|
|
144
|
+
* so it MUST re-stamp provenance from the resolved auth and the server clock
|
|
145
|
+
* rather than carry a previously stored `verified.*` value forward (flair#1960
|
|
146
|
+
* r2). `createdAt` is included (flair#1960 r4): a createdAt-only PATCH is
|
|
147
|
+
* allowed and reaches `super.patch`, so if it were NOT semantic the row's
|
|
148
|
+
* `createdAt` could change while `provenance.claimed.createdAt` kept the old
|
|
149
|
+
* value. Making it semantic means a changed creation claim re-stamps, and
|
|
150
|
+
* `claimed.createdAt` follows the row.
|
|
151
|
+
*/
|
|
152
|
+
export const MEMORY_SEMANTIC_FIELDS = Object.freeze(["content", "subject", "summary", "createdAt"]);
|
|
153
|
+
/**
|
|
154
|
+
* The fields whose change makes a relationship write SEMANTIC — its identity
|
|
155
|
+
* (what it links), plus its creation claim. Same rule as MEMORY_SEMANTIC_FIELDS:
|
|
156
|
+
* a semantic PATCH re-stamps provenance; a metadata-only PATCH
|
|
157
|
+
* (confidence/source/etc.) leaves the stored, previously-stamped blob in place.
|
|
158
|
+
* `createdAt` is included (flair#1960 r4) for the same reason: a changed
|
|
159
|
+
* creation claim is a re-authored claim, so the PATCH re-stamps and
|
|
160
|
+
* `claimed.createdAt` follows the row.
|
|
161
|
+
*/
|
|
162
|
+
export const RELATIONSHIP_SEMANTIC_FIELDS = Object.freeze(["subject", "predicate", "object", "createdAt"]);
|
|
163
|
+
/**
|
|
164
|
+
* True when `content` changes at least one of `fields` relative to the STORED
|
|
165
|
+
* record — the trigger for a provenance re-stamp on the update (PATCH) path.
|
|
166
|
+
*
|
|
167
|
+
* A value that is ABSENT from the write body is never a change (Harper PATCH
|
|
168
|
+
* merges; a missing key means "leave the stored value"), so this only looks at
|
|
169
|
+
* keys actually present in `content`. A non-object body, or a body whose target
|
|
170
|
+
* is not a string/primitive change, is not semantic.
|
|
171
|
+
*/
|
|
172
|
+
export function isSemanticPatch(content, existing, fields) {
|
|
173
|
+
if (!content || typeof content !== "object" || Array.isArray(content))
|
|
174
|
+
return false;
|
|
175
|
+
if (!existing || typeof existing !== "object")
|
|
176
|
+
return false;
|
|
177
|
+
for (const field of fields) {
|
|
178
|
+
if (!Object.prototype.hasOwnProperty.call(content, field))
|
|
179
|
+
continue;
|
|
180
|
+
if (content[field] !== existing[field])
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The response a PATCH returns when its stored-row READ FAILS (flair#1960 r3).
|
|
187
|
+
*
|
|
188
|
+
* A read ERROR is not the same as "no stored row" (a `null`/`undefined`
|
|
189
|
+
* result). Both PATCH paths used to coalesce a thrown read into `null`; the
|
|
190
|
+
* semantic predicate then returns `false` for `null`, and the patch falls
|
|
191
|
+
* through to `super.patch()` as if it were a METADATA-ONLY write — which keeps
|
|
192
|
+
* a legacy stored blob (including a caller-chosen `verified.timestamp`) in
|
|
193
|
+
* place. Without the stored record there is no way to tell a semantic PATCH
|
|
194
|
+
* from a metadata-only one, so the only safe outcome is to REFUSE the write
|
|
195
|
+
* rather than fail open. 500: a server-side read fault, fail-closed.
|
|
196
|
+
*/
|
|
197
|
+
export function storedRowReadFailedResponse(table) {
|
|
198
|
+
return new Response(JSON.stringify({
|
|
199
|
+
error: "stored_row_read_failed",
|
|
200
|
+
message: `cannot read the stored ${table} row; refusing the write`,
|
|
201
|
+
}), { status: 500, headers: { "content-type": "application/json" } });
|
|
202
|
+
}
|
|
@@ -117,6 +117,10 @@ export const OWNER_GUARD_EXEMPT = Object.freeze({
|
|
|
117
117
|
// resources/Agent.ts's shared write-authorization helper, which both its
|
|
118
118
|
// put() and its patch() route through.
|
|
119
119
|
Agent: "self-ownership by primary key; enforced in resources/Agent.ts for every verb",
|
|
120
|
+
// flair#1940 A1': MemoryHostSource refuses REST writes. Application pointer
|
|
121
|
+
// writes stamp `authorId` from the authenticated principal; direct resource
|
|
122
|
+
// reads permit admins and trusted internal callers.
|
|
123
|
+
MemoryHostSource: "REST writes refused; application writes stamp authorId; direct reads limited to admins and trusted internal callers",
|
|
120
124
|
});
|
|
121
125
|
/** The verbs that can mutate a record, and therefore need the rule applied. */
|
|
122
126
|
export const MUTATING_METHODS = Object.freeze(["POST", "PUT", "PATCH", "DELETE"]);
|
|
@@ -175,6 +179,21 @@ export function isForbiddenOwnerMutation(record, ownerField, callerAgentId) {
|
|
|
175
179
|
return true;
|
|
176
180
|
return owner !== callerAgentId;
|
|
177
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* The refusal for a write that names a stored record owned by another
|
|
184
|
+
* principal: 403 with a body that names only the table.
|
|
185
|
+
*
|
|
186
|
+
* One constructor for every place the rule is enforced — the auth middleware
|
|
187
|
+
* for `/<Table>/<id>` routes, and resources/relay-ops.ts for a Message POST to
|
|
188
|
+
* the collection that reuses an existing message id — so a refused caller gets
|
|
189
|
+
* the same response on either route, and it carries nothing from the stored
|
|
190
|
+
* row.
|
|
191
|
+
*/
|
|
192
|
+
export function ownerMutationRefusal(table) {
|
|
193
|
+
return new Response(JSON.stringify({
|
|
194
|
+
error: `forbidden: cannot modify ${table} owned by another principal`,
|
|
195
|
+
}), { status: 403, headers: { "Content-Type": "application/json" } });
|
|
196
|
+
}
|
|
178
197
|
/**
|
|
179
198
|
* Decide whether a caller may CHANGE the owner field of an already-stored record.
|
|
180
199
|
*
|
|
@@ -218,19 +218,16 @@ export function makeByIdReadGate(readScope) {
|
|
|
218
218
|
return superGet(target);
|
|
219
219
|
}
|
|
220
220
|
// Non-admin agent: scoped per the table's own read-scope model. The scope
|
|
221
|
-
// is evaluated on the
|
|
222
|
-
//
|
|
223
|
-
//
|
|
221
|
+
// is evaluated on the UNSELECTED row: the gate passes super.get a fresh plain
|
|
222
|
+
// object it builds itself, carrying only the id (or nothing, when the target
|
|
223
|
+
// has no id), and checks the row that returns. In Harper's default instance
|
|
224
|
+
// mode the resource is loaded by id before get runs, and get returns that
|
|
225
|
+
// whole record when the target carries no select or property. The caller's
|
|
226
|
+
// target, and anything its class does when constructed, never shapes the row
|
|
227
|
+
// the decision sees. A target that asks for a selection or a single property
|
|
228
|
+
// gets that shaped result only once the unselected row passes.
|
|
224
229
|
const shaped = typeof target === "object" && target !== null && (target.select != null || target.property != null);
|
|
225
|
-
|
|
226
|
-
if (shaped) {
|
|
227
|
-
const Ctor = target.constructor;
|
|
228
|
-
if (typeof Ctor !== "function")
|
|
229
|
-
return NOT_FOUND(); // fail closed: cannot re-read unshaped
|
|
230
|
-
unshaped = new Ctor();
|
|
231
|
-
unshaped.id = targetId;
|
|
232
|
-
}
|
|
233
|
-
const record = await superGet(unshaped);
|
|
230
|
+
const record = await superGet(targetId != null ? { id: targetId } : {});
|
|
234
231
|
if (!record) {
|
|
235
232
|
// Row did not load: either it genuinely does not exist, OR — the #1181
|
|
236
233
|
// failure — a by-id read reached here on an unloaded instance and
|
|
@@ -101,9 +101,9 @@
|
|
|
101
101
|
* explicitly deferred by Flint's post-slice-1 comment on #520 ("Sherlock
|
|
102
102
|
* flagging stamp-default → stamp-strict convergence... Registry slice
|
|
103
103
|
* follows" — convergence is a FUTURE decision, not made here). Each
|
|
104
|
-
* sub-field is optional because
|
|
105
|
-
* `stampAttribution` on both methods
|
|
106
|
-
*
|
|
104
|
+
* sub-field is optional because a table's write path need not call
|
|
105
|
+
* `stampAttribution` on both methods; an absent sub-field is not a
|
|
106
|
+
* placeholder.
|
|
107
107
|
* The stamped attribute name is always `ownerField` above — no type uses a
|
|
108
108
|
* different field for attribution vs. ownership scoping.
|
|
109
109
|
*
|
|
@@ -291,9 +291,9 @@ export const RECORD_TYPES = {
|
|
|
291
291
|
ownerField: "agentId",
|
|
292
292
|
identity: "gated",
|
|
293
293
|
readScope: "owner-only",
|
|
294
|
-
//
|
|
295
|
-
//
|
|
296
|
-
attribution: { put: "stamp-strict" },
|
|
294
|
+
// post() (a collection create) and put() (upsert) share one preparation
|
|
295
|
+
// (Relationship.ts's prepareRelationshipWrite), with the same mode.
|
|
296
|
+
attribution: { post: "stamp-strict", put: "stamp-strict" },
|
|
297
297
|
provenance: true,
|
|
298
298
|
remEligible: false,
|
|
299
299
|
federation: "included",
|
|
@@ -363,6 +363,22 @@ export const RECORD_TYPES = {
|
|
|
363
363
|
// same reasoning, not by omission.
|
|
364
364
|
federation: "excluded",
|
|
365
365
|
},
|
|
366
|
+
// OrgSkillAssignment — flair#2141 S1. Org-scope skill assignments, readable
|
|
367
|
+
// by any verified agent. resources/OrgSkillAssignment.ts gates each write
|
|
368
|
+
// verb itself on the operator or internal source (no record-type-kit
|
|
369
|
+
// attribution idiom). No principal owns a row: `ownerField` names the
|
|
370
|
+
// server-stamped `writer`, which no read scope or attribution mode uses.
|
|
371
|
+
// Instance-local.
|
|
372
|
+
OrgSkillAssignment: {
|
|
373
|
+
table: "OrgSkillAssignment",
|
|
374
|
+
ownerField: "writer",
|
|
375
|
+
identity: "gated",
|
|
376
|
+
readScope: "none",
|
|
377
|
+
attribution: {},
|
|
378
|
+
provenance: false,
|
|
379
|
+
remEligible: false,
|
|
380
|
+
federation: "excluded",
|
|
381
|
+
},
|
|
366
382
|
};
|
|
367
383
|
// ─── Composite MCP tools (the second, and only other, reviewed chokepoint) ─
|
|
368
384
|
//
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* All authorization decisions are made here against an already-resolved auth
|
|
10
10
|
* verdict; the resource layer only resolves the verdict and forwards the call.
|
|
11
11
|
*/
|
|
12
|
+
import { isForbiddenOwnerMutation, ownerMutationRefusal, OWNER_FIELDS } from "./record-owner-guard.js";
|
|
12
13
|
import { capDecision, computeContentHash, isUnconsumed, LOCAL_ORG_SENTINEL, reconcileState, sweepDecision, verifyMessageSignature, } from "./relay-lib.js";
|
|
13
14
|
// ─── HTTP responses (Response is a web global, not a Harper import) ──────────
|
|
14
15
|
const json = (status, obj) => new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json" } });
|
|
@@ -70,7 +71,11 @@ function acceptedView(row) {
|
|
|
70
71
|
* per-sender sub-cap, and delivers. Returns the accepted envelope — the SAME
|
|
71
72
|
* shape regardless of whether `to` exists (no existence oracle): the only
|
|
72
73
|
* principal ever looked up is the sender (needed for its public key), which is
|
|
73
|
-
* the authenticated caller and therefore always present.
|
|
74
|
+
* the authenticated caller and therefore always present. A send that reuses a
|
|
75
|
+
* stored message id is answered with that message's accepted view only when
|
|
76
|
+
* it has the same sender; any other sender gets the owner-guard 403
|
|
77
|
+
* (record-owner-guard.ts's ownerMutationRefusal), which carries no field of
|
|
78
|
+
* the stored message.
|
|
74
79
|
*/
|
|
75
80
|
export async function relaySend(deps, auth, content) {
|
|
76
81
|
if (auth.kind === "anonymous")
|
|
@@ -120,9 +125,29 @@ export async function relaySend(deps, auth, content) {
|
|
|
120
125
|
// Retry-dedup: an identical resend — same primary id, or the same content
|
|
121
126
|
// hash from this sender to this recipient — returns the already-accepted
|
|
122
127
|
// envelope. No second row, and (critically) no second charge against the cap.
|
|
123
|
-
|
|
124
|
-
|
|
128
|
+
//
|
|
129
|
+
// The same-id answer is for the ORIGINAL SENDER only. A stored message is
|
|
130
|
+
// owned by its `from` (OWNER_FIELDS.Message), and this send's `from` is the
|
|
131
|
+
// verified signer. When the id is held by another sender, the send is
|
|
132
|
+
// refused with the owner-guard refusal — the same response the auth
|
|
133
|
+
// middleware gives a write to `/Message/<id>` for a message the caller does
|
|
134
|
+
// not own — and nothing from the stored row is returned. The rule is about
|
|
135
|
+
// the message, not the caller's role: an admin sending as X gets the same
|
|
136
|
+
// refusal for an id held by Y. The lookup fails CLOSED: if the stored row
|
|
137
|
+
// cannot be read, the send is refused the same way, never written over it.
|
|
138
|
+
let existingById;
|
|
139
|
+
try {
|
|
140
|
+
existingById = (await Promise.resolve(deps.messages.get(String(content.id)))) ?? null;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return ownerMutationRefusal("Message");
|
|
144
|
+
}
|
|
145
|
+
if (existingById) {
|
|
146
|
+
if (isForbiddenOwnerMutation(existingById, OWNER_FIELDS.Message, String(content.from))) {
|
|
147
|
+
return ownerMutationRefusal("Message");
|
|
148
|
+
}
|
|
125
149
|
return acceptedView(existingById);
|
|
150
|
+
}
|
|
126
151
|
// The recipient's inbox — queried by the @indexed `to`, never the whole table
|
|
127
152
|
// (Sherlock P0). Serves both dedup (same content from this sender) and the cap.
|
|
128
153
|
const recipientRows = await queryRows(deps.messages, [{ attribute: "to", value: content.to }]);
|