@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,10 +1,13 @@
|
|
|
1
|
+
import { WriteJournal, currentWriteJournal } from "./write-journal.js";
|
|
2
|
+
import { withKeyLock } from "./key-lock.js";
|
|
3
|
+
import { withMemoryWriteLock } from "./memory-write-lock.js";
|
|
1
4
|
import { normalizeEmptyEmbedding } from "./memory-embedding-value.js";
|
|
2
5
|
/** Skill writes share the ["flair-instruction-version", "skill"] lock. */
|
|
3
6
|
import { databases } from "harper";
|
|
4
7
|
import { storageResult } from "./memory-write-response.js";
|
|
5
8
|
import { randomUUID } from "node:crypto";
|
|
6
9
|
import { isDeepStrictEqual } from "node:util";
|
|
7
|
-
import { recordVersion, readHead, skillRefReadable, SKILL_SUBJECT_TYPE } from "./instruction-version-record.js";
|
|
10
|
+
import { recordVersion, readHead, readVersionRecord, readVersionRecordAfter, skillRefReadable, SKILL_SUBJECT_TYPE } from "./instruction-version-record.js";
|
|
8
11
|
import { SKILL_TAG, enforceSkillDurability, skillEmbedText, skillMetadataHasDuplicateKey } from "./skill-write.js";
|
|
9
12
|
import { stripUndeclaredMemoryAttributes } from "./memory-declared-attributes.js";
|
|
10
13
|
import { skillWriteSource } from "./skill-write-policy.js";
|
|
@@ -13,9 +16,11 @@ import { FORBIDDEN } from "./record-type-kit.js";
|
|
|
13
16
|
import { PRIVATE_VISIBILITY, SHARED_VISIBILITY } from "./memory-visibility.js";
|
|
14
17
|
import { maybeThrowSkillWriteFault } from "./skill-write-fault.js";
|
|
15
18
|
import { supersedesTargetUnreadable } from "./memory-id-guard.js";
|
|
16
|
-
import { grantedCloseRevokedRefusal, writeGrantHeld } from "./write-back.js";
|
|
19
|
+
import { grantedCloseRevokedRefusal, sameStoredRow, storedRowChangedRefusal, writeGrantHeld } from "./write-back.js";
|
|
20
|
+
import { isJoinableTransaction, withOwnedTransaction } from "./request-transaction.js";
|
|
17
21
|
import { txnPausePoint } from "./txn-pause-point.js";
|
|
18
22
|
import { committedVisibilityDiffers, grantedSuccessorVisibilityDenial, predecessorVisibilityChangedRefusal } from "./grant-visibility.js";
|
|
23
|
+
import { isOpenLiveRow } from "../src/lib/open-live-row.js";
|
|
19
24
|
/** Durability-keyed default visibility (mirrors Memory's write default). */
|
|
20
25
|
function defaultVisibilityForDurability(durability) {
|
|
21
26
|
return durability === "permanent" || durability === "persistent" ? "shared" : "private";
|
|
@@ -114,17 +119,6 @@ export function buildSkillSuccessorRow(args) {
|
|
|
114
119
|
function okResponse() {
|
|
115
120
|
return new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "Content-Type": "application/json" } });
|
|
116
121
|
}
|
|
117
|
-
function isOpenLiveRow(row) {
|
|
118
|
-
if (!row || typeof row !== "object")
|
|
119
|
-
return false;
|
|
120
|
-
if (row.archived === true)
|
|
121
|
-
return false;
|
|
122
|
-
if (row.expiresAt != null && (typeof row.expiresAt !== "string" || !(Date.parse(row.expiresAt) > Date.now())))
|
|
123
|
-
return false;
|
|
124
|
-
if (typeof row.validTo === "string" && row.validTo.length > 0)
|
|
125
|
-
return false;
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
122
|
/** Is this row a skill-tagged Memory row? */
|
|
129
123
|
export function rowIsSkill(row) {
|
|
130
124
|
return !!row && Array.isArray(row.tags) && row.tags.includes(SKILL_TAG);
|
|
@@ -198,7 +192,7 @@ export async function prepareSkillBody(content, stored, auth) {
|
|
|
198
192
|
delete effective.id;
|
|
199
193
|
return { content: effective, predecessor };
|
|
200
194
|
}
|
|
201
|
-
/**
|
|
195
|
+
/** Return the sole live head, or null if none; refuse ambiguity. */
|
|
202
196
|
export async function resolveSkillHead(subjectId, addressedId, shared) {
|
|
203
197
|
const table = databases.flair?.Memory;
|
|
204
198
|
if (!table || typeof table.search !== "function")
|
|
@@ -257,6 +251,22 @@ export function grantedSkillClose(closed, ctx, auth) {
|
|
|
257
251
|
return null;
|
|
258
252
|
return { granteeId: auth.agentId, ownerId: closed.agentId };
|
|
259
253
|
}
|
|
254
|
+
/** The fields the embedding backfill's incremental update stages
|
|
255
|
+
* (resources/auth-middleware.ts); a committed target read may differ from the
|
|
256
|
+
* request snapshot by these without refusing the POST (flair#3170). */
|
|
257
|
+
const BACKFILL_WRITTEN_FIELDS = new Set(["embedding", "embeddingModel", "embeddedContentHash"]);
|
|
258
|
+
/** Does a committed target row differ from the request snapshot outside the
|
|
259
|
+
* embedding backfill's fields? Such a change refuses the POST with 409
|
|
260
|
+
* skill_target_changed (flair#3170). */
|
|
261
|
+
export function skillTargetSnapshotChanged(snapshot, committed) {
|
|
262
|
+
for (const field of new Set([...Object.keys(snapshot), ...Object.keys(committed)])) {
|
|
263
|
+
if (BACKFILL_WRITTEN_FIELDS.has(field))
|
|
264
|
+
continue;
|
|
265
|
+
if (!isDeepStrictEqual(snapshot[field], committed[field]))
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
260
270
|
export async function validateSkillSnapshots(stored, predecessor, targetId, shared) {
|
|
261
271
|
for (const [id, expected] of [[targetId, stored], [predecessor?.id, predecessor]]) {
|
|
262
272
|
if (!id)
|
|
@@ -275,6 +285,11 @@ export async function validateSkillSnapshots(stored, predecessor, targetId, shar
|
|
|
275
285
|
export const defaultSkillHooks = {
|
|
276
286
|
createSuccessor: async (successor, shared) => {
|
|
277
287
|
normalizeEmptyEmbedding(successor);
|
|
288
|
+
// Not pre-validated: this row carries the caller's fields without that
|
|
289
|
+
// coercion, so a defect in it — a server-stamped field included — stays a
|
|
290
|
+
// caller error (400) at commit. Pre-validation covers the A2A,
|
|
291
|
+
// agent-key-register and instruction-version writer events and the
|
|
292
|
+
// migration rows (resources/server-row-validation.ts).
|
|
278
293
|
const result = storageResult(await databases.flair.Memory.put(successor, shared));
|
|
279
294
|
if (result instanceof Response)
|
|
280
295
|
return result;
|
|
@@ -283,6 +298,8 @@ export const defaultSkillHooks = {
|
|
|
283
298
|
closePredecessor: async (predecessor, patch, shared) => {
|
|
284
299
|
const closed = { ...predecessor, ...patch };
|
|
285
300
|
normalizeEmptyEmbedding(closed);
|
|
301
|
+
// Not pre-validated: a close carries the caller's row; its defects stay a
|
|
302
|
+
// caller error at commit (see the successor note above).
|
|
286
303
|
const result = storageResult(await databases.flair.Memory.put(closed, shared));
|
|
287
304
|
if (result instanceof Response)
|
|
288
305
|
return result;
|
|
@@ -293,20 +310,17 @@ export const defaultSkillHooks = {
|
|
|
293
310
|
* close or a delete's head close. A grant-authorized cross-agent close stands on
|
|
294
311
|
* the grant read at authorization time; re-read it on committed state HERE,
|
|
295
312
|
* after the close is staged, through the shared write-back helper the non-skill
|
|
296
|
-
* close uses (Memory.ts closeSupersededRecord, flair#2333 H10).
|
|
297
|
-
* 409 when the grant is gone — the caller returns it, so the whole transaction
|
|
298
|
-
* rolls back: no close, no successor, no version — else null. A plan with no
|
|
299
|
-
* grant (same-agent, operator, internal) reads nothing.
|
|
313
|
+
* close uses (Memory.ts closeSupersededRecord, flair#2333 H10).
|
|
300
314
|
*
|
|
301
315
|
* For a grant-authorized supersede, `successor` is the planned successor row.
|
|
302
316
|
* Before the grant re-check, the predecessor's COMMITTED row (an empty-context
|
|
303
317
|
* read, as for the grant) must read with the successor's visibility: a write
|
|
304
318
|
* grant does not include changing a record's visibility (grant-visibility.ts).
|
|
305
|
-
* A mismatch returns the named 409 predecessor_visibility_changed
|
|
306
|
-
*
|
|
319
|
+
* A mismatch returns the named 409 predecessor_visibility_changed.
|
|
320
|
+
* As with the grant, a change committed
|
|
307
321
|
* after this re-read and before the transaction commits is not seen by it.
|
|
308
322
|
*/
|
|
309
|
-
async function afterCloseStaged(granted, supersede) {
|
|
323
|
+
export async function afterCloseStaged(granted, supersede) {
|
|
310
324
|
// Test-only: inert unless armed — here the close write is staged, so a
|
|
311
325
|
// competing grant revocation or row change and the committed-state re-checks
|
|
312
326
|
// below can be exercised in either order (the skill-path counterpart of Memory's "granted-close-staged").
|
|
@@ -323,9 +337,280 @@ async function afterCloseStaged(granted, supersede) {
|
|
|
323
337
|
const held = await writeGrantHeld(databases.flair.MemoryGrant, granted.granteeId, granted.ownerId, {});
|
|
324
338
|
return held ? null : grantedCloseRevokedRefusal();
|
|
325
339
|
}
|
|
340
|
+
/** Attempts at re-applying a skill update's close whose committed write did not land (flair#2645). */
|
|
341
|
+
const SKILL_UPDATE_CLOSE_ATTEMPTS = 3;
|
|
342
|
+
/** No caller-held Memory ids. */
|
|
343
|
+
const NO_HELD_IDS = new Set();
|
|
344
|
+
/**
|
|
345
|
+
* Acquire missing Memory ids in sorted order. Inline compensation skips
|
|
346
|
+
* caller-held ids; deferred confirmation and compensation reacquire after
|
|
347
|
+
* release (flair#2950).
|
|
348
|
+
* Returns `locked: false` when a missing id's lock could not be taken.
|
|
349
|
+
*/
|
|
350
|
+
async function withSkillMemoryLocks(ids, held, fn) {
|
|
351
|
+
const missing = [...new Set(ids.map((id) => String(id)))].filter((id) => !held.has(id)).sort();
|
|
352
|
+
if (missing.length === 0)
|
|
353
|
+
return { locked: true, value: await fn() };
|
|
354
|
+
const outcome = await withMemoryWriteLock(databases?.flair?.Memory?.primaryStore, missing, fn, {
|
|
355
|
+
phase: "skill-version-write",
|
|
356
|
+
});
|
|
357
|
+
return outcome.kind === "done" ? { locked: true, value: outcome.value } : { locked: false };
|
|
358
|
+
}
|
|
359
|
+
/** The named 500 when the skill update cannot be confirmed. */
|
|
360
|
+
function skillUpdateUnconfirmedFailure() {
|
|
361
|
+
return Response.json({
|
|
362
|
+
error: "skill_update_unconfirmed",
|
|
363
|
+
message: "the skill update could not be confirmed",
|
|
364
|
+
}, { status: 500 });
|
|
365
|
+
}
|
|
366
|
+
/** The named 409 for an update whose close still is not visible after the bounded re-applies. */
|
|
367
|
+
function skillUpdateNotClosedRefusal(attempts) {
|
|
368
|
+
return Response.json({
|
|
369
|
+
error: "skill_update_not_closed",
|
|
370
|
+
message: `the skill version close was not applied after ${attempts} attempts; not updated`,
|
|
371
|
+
}, { status: 409 });
|
|
372
|
+
}
|
|
373
|
+
/** The named 409 when neither a sole live successor nor ordered supersession is confirmed. */
|
|
374
|
+
function skillUpdateNotConfirmedRefusal() {
|
|
375
|
+
return Response.json({
|
|
376
|
+
error: "skill_update_not_confirmed",
|
|
377
|
+
message: "the committed store confirms neither this update's successor as the only live head nor its closure with this request's version record present, the record with the next sequence number linked to it by hash, and that later version deleting the successor or its row naming the successor as superseded",
|
|
378
|
+
}, { status: 409 });
|
|
379
|
+
}
|
|
380
|
+
/** The named 500 when compensation cannot be confirmed. */
|
|
381
|
+
function skillUpdateCompensationFailure(message) {
|
|
382
|
+
return Response.json({ error: "skill_update_compensation_failed", message }, { status: 500 });
|
|
383
|
+
}
|
|
384
|
+
async function compensateUnlandedSkillUpdate(args) {
|
|
385
|
+
args.journal.compensationAttempted = true;
|
|
386
|
+
try {
|
|
387
|
+
const table = databases.flair?.InstructionVersion;
|
|
388
|
+
// Memory id locks before the instruction-version lock (the global order),
|
|
389
|
+
// so the undo's Memory writes are serialized with the other per-id-locked
|
|
390
|
+
// writers of those ids, not just the version append (flair#2950).
|
|
391
|
+
const locked = await withSkillMemoryLocks(args.journal.memoryWriteIds(), args.held ?? NO_HELD_IDS, async () => withKeyLock(table?.primaryStore, ["flair-instruction-version", "skill"], async () => {
|
|
392
|
+
const appended = [...args.journal.entries].reverse().find((entry) => entry.after?.subjectType === "skill");
|
|
393
|
+
if (appended) {
|
|
394
|
+
const head = await readHead(SKILL_SUBJECT_TYPE, appended.after.subjectId, {});
|
|
395
|
+
if (head && String(head.id) !== String(appended.after.id))
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
return args.journal.undo(args.ctx, withOwnedTransaction);
|
|
399
|
+
}, 200, 10));
|
|
400
|
+
if (!locked.locked)
|
|
401
|
+
throw new Error("the Memory write lock could not be acquired for the skill write's undo");
|
|
402
|
+
const result = locked.value;
|
|
403
|
+
if (result.kind === "done" && result.value)
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
console.error("skill-version-write: update undo could not be confirmed", err);
|
|
408
|
+
}
|
|
409
|
+
return skillUpdateCompensationFailure("the skill write's undo could not be confirmed; check its outcome, because its version and event may persist" +
|
|
410
|
+
(args.journal.pointerChanged ? "; a different host-pointer row was left in place" : ""));
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* flair#2645 — confirm a closed successor only when this request's update
|
|
414
|
+
* record is present, the record with the next sequence number links to it by
|
|
415
|
+
* hash, and that later version either deletes this successor (its `rowId`) or
|
|
416
|
+
* has a row that names this successor as superseded.
|
|
417
|
+
*/
|
|
418
|
+
async function confirmSupersededInOrder(args) {
|
|
419
|
+
if (!args.appendedVersionId)
|
|
420
|
+
return skillUpdateNotConfirmedRefusal();
|
|
421
|
+
let appended;
|
|
422
|
+
let following;
|
|
423
|
+
try {
|
|
424
|
+
appended = await readVersionRecord(SKILL_SUBJECT_TYPE, args.subjectId, args.appendedVersionId, {});
|
|
425
|
+
following = appended
|
|
426
|
+
? await readVersionRecordAfter(SKILL_SUBJECT_TYPE, args.subjectId, appended.version, {})
|
|
427
|
+
: null;
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
return skillUpdateUnconfirmedFailure();
|
|
431
|
+
}
|
|
432
|
+
if (!appended || appended.kind !== "update" || String(appended.memoryId) !== String(args.successor.id)) {
|
|
433
|
+
return skillUpdateNotConfirmedRefusal();
|
|
434
|
+
}
|
|
435
|
+
if (!following || following.previousVersionHash !== appended.recordHash)
|
|
436
|
+
return skillUpdateNotConfirmedRefusal();
|
|
437
|
+
if (following.kind === "delete") {
|
|
438
|
+
return String(following.rowId ?? "") === String(args.successor.id) ? null : skillUpdateNotConfirmedRefusal();
|
|
439
|
+
}
|
|
440
|
+
if (following.kind !== "update")
|
|
441
|
+
return skillUpdateNotConfirmedRefusal();
|
|
442
|
+
const followingId = typeof following.memoryId === "string" ? following.memoryId : null;
|
|
443
|
+
if (!followingId)
|
|
444
|
+
return skillUpdateNotConfirmedRefusal();
|
|
445
|
+
let followingRow;
|
|
446
|
+
try {
|
|
447
|
+
followingRow = await args.memory.get(followingId, {});
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
return skillUpdateUnconfirmedFailure();
|
|
451
|
+
}
|
|
452
|
+
if (!followingRow || String(followingRow.supersedes ?? "") !== String(args.successor.id)) {
|
|
453
|
+
return skillUpdateNotConfirmedRefusal();
|
|
454
|
+
}
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* flair#2645 — confirm a skill close in a fresh committed read, then answer
|
|
459
|
+
* success only when the committed store shows it. Harper can resolve the
|
|
460
|
+
* close's commit while skipping the queued full put (it loses to a newer write
|
|
461
|
+
* of the same row) and still commit the request's other writes. A close that
|
|
462
|
+
* did not land is re-applied from a fresh read, re-checking the row's identity
|
|
463
|
+
* and the caller's authority. Exhausted retries run compensation before the
|
|
464
|
+
* named refusal; unconfirmed compensation returns a named 500.
|
|
465
|
+
* Returns null only when the close and any caller-supplied effect are confirmed.
|
|
466
|
+
*/
|
|
467
|
+
export async function confirmSkillClose(args) {
|
|
468
|
+
const journal = currentWriteJournal() ?? new WriteJournal();
|
|
469
|
+
return journal.run(async () => {
|
|
470
|
+
const result = await confirmSkillCloseInner(args, journal);
|
|
471
|
+
if (result && !journal.compensationAttempted && journal.entries.some((entry) => entry.applied)) {
|
|
472
|
+
return (await compensateUnlandedSkillUpdate({ ctx: args.ctx, journal, held: args.held })) ?? result;
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
async function confirmSkillCloseInner(args, journal) {
|
|
478
|
+
const { ctx, table, expected, closePatch, authorize, afterStaged, confirmEffect, refusal, unconfirmed } = args;
|
|
479
|
+
const compensate = () => compensateUnlandedSkillUpdate({ ctx, journal, held: args.held });
|
|
480
|
+
const attempts = args.attempts ?? SKILL_UPDATE_CLOSE_ATTEMPTS;
|
|
481
|
+
const id = String(expected.id);
|
|
482
|
+
for (let attempt = 1;; attempt++) {
|
|
483
|
+
let committed;
|
|
484
|
+
try {
|
|
485
|
+
committed = await table.get(id, {});
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
return unconfirmed();
|
|
489
|
+
}
|
|
490
|
+
if (!committed) {
|
|
491
|
+
const denied = args.missing?.() ?? storedRowChangedRefusal("Memory");
|
|
492
|
+
return (await compensate()) ?? denied;
|
|
493
|
+
}
|
|
494
|
+
if (!sameStoredRow(committed, expected)) {
|
|
495
|
+
// Replaced between the reads: not the row the caller meant.
|
|
496
|
+
return (await compensate()) ?? storedRowChangedRefusal("Memory");
|
|
497
|
+
}
|
|
498
|
+
if (typeof committed.validTo === "string" && committed.validTo.length > 0) {
|
|
499
|
+
const denied = confirmEffect ? await confirmEffect() : null;
|
|
500
|
+
return denied ? (await compensate()) ?? denied : null;
|
|
501
|
+
}
|
|
502
|
+
if (attempt > attempts)
|
|
503
|
+
break;
|
|
504
|
+
let applied;
|
|
505
|
+
try {
|
|
506
|
+
// Hold the row's id across the retry's fresh transaction (flair#2950).
|
|
507
|
+
const locked = await withSkillMemoryLocks([id], args.held ?? NO_HELD_IDS, () => withOwnedTransaction(ctx, async (c) => {
|
|
508
|
+
const stored = await table.get(id, c);
|
|
509
|
+
if (!stored)
|
|
510
|
+
return "staged";
|
|
511
|
+
if (!sameStoredRow(stored, expected))
|
|
512
|
+
return storedRowChangedRefusal("Memory");
|
|
513
|
+
const denied = await authorize(stored, c);
|
|
514
|
+
if (denied)
|
|
515
|
+
return denied;
|
|
516
|
+
const closed = { ...stored, ...closePatch };
|
|
517
|
+
normalizeEmptyEmbedding(closed);
|
|
518
|
+
const result = storageResult(await table.put(closed, c));
|
|
519
|
+
if (result instanceof Response)
|
|
520
|
+
throw result;
|
|
521
|
+
const revoked = await afterStaged?.(stored, attempt);
|
|
522
|
+
if (revoked)
|
|
523
|
+
throw revoked;
|
|
524
|
+
return "staged";
|
|
525
|
+
}));
|
|
526
|
+
if (!locked.locked)
|
|
527
|
+
return unconfirmed();
|
|
528
|
+
applied = locked.value;
|
|
529
|
+
}
|
|
530
|
+
catch (err) {
|
|
531
|
+
if (err instanceof Response)
|
|
532
|
+
applied = err;
|
|
533
|
+
else
|
|
534
|
+
return unconfirmed();
|
|
535
|
+
}
|
|
536
|
+
if (applied instanceof Response)
|
|
537
|
+
return (await compensate()) ?? applied;
|
|
538
|
+
}
|
|
539
|
+
return (await compensate()) ?? refusal();
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* flair#2645 — confirm the predecessor close and either this request's
|
|
543
|
+
* successor as the subject's only live head or its ordered supersession while closed.
|
|
544
|
+
*/
|
|
545
|
+
async function confirmSkillUpdate(args) {
|
|
546
|
+
const { ctx, subjectId, predecessor, closePatch, successor, grantedClose, authorize } = args;
|
|
547
|
+
const table = databases.flair?.Memory;
|
|
548
|
+
if (!table || typeof table.get !== "function" || typeof table.put !== "function") {
|
|
549
|
+
return skillUpdateUnconfirmedFailure();
|
|
550
|
+
}
|
|
551
|
+
const confirmsSingleHead = async () => {
|
|
552
|
+
let successorCommitted;
|
|
553
|
+
try {
|
|
554
|
+
successorCommitted = await table.get(String(successor.id), {});
|
|
555
|
+
}
|
|
556
|
+
catch {
|
|
557
|
+
return skillUpdateUnconfirmedFailure();
|
|
558
|
+
}
|
|
559
|
+
if (!successorCommitted || !sameStoredRow(successorCommitted, successor)) {
|
|
560
|
+
return skillUpdateNotConfirmedRefusal();
|
|
561
|
+
}
|
|
562
|
+
if (!isOpenLiveRow(successorCommitted)) {
|
|
563
|
+
if (typeof successorCommitted.validTo !== "string" || successorCommitted.validTo.length === 0) {
|
|
564
|
+
return skillUpdateNotConfirmedRefusal();
|
|
565
|
+
}
|
|
566
|
+
// The successor is closed; confirm the ordered supersession.
|
|
567
|
+
return confirmSupersededInOrder({
|
|
568
|
+
subjectId,
|
|
569
|
+
successor,
|
|
570
|
+
appendedVersionId: args.appendedVersionId,
|
|
571
|
+
memory: table,
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
if (typeof table.search !== "function")
|
|
575
|
+
return skillUpdateUnconfirmedFailure();
|
|
576
|
+
const live = [];
|
|
577
|
+
try {
|
|
578
|
+
for await (const row of table.search({ conditions: [
|
|
579
|
+
{ attribute: "skillSubjectId", comparator: "equals", value: subjectId },
|
|
580
|
+
] }, {})) {
|
|
581
|
+
if (isOpenLiveRow(row))
|
|
582
|
+
live.push(String(row.id));
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
catch {
|
|
586
|
+
return skillUpdateUnconfirmedFailure();
|
|
587
|
+
}
|
|
588
|
+
return live.length === 1 && live[0] === String(successor.id) ? null : skillUpdateNotConfirmedRefusal();
|
|
589
|
+
};
|
|
590
|
+
return confirmSkillClose({
|
|
591
|
+
ctx,
|
|
592
|
+
table,
|
|
593
|
+
expected: predecessor,
|
|
594
|
+
closePatch,
|
|
595
|
+
authorize,
|
|
596
|
+
afterStaged: async (_row, attempt) => {
|
|
597
|
+
const pause = attempt === 1 ? txnPausePoint("skill-update-retry-first")
|
|
598
|
+
: attempt === 2 ? txnPausePoint("skill-update-retry-second")
|
|
599
|
+
: txnPausePoint("skill-update-retry-third");
|
|
600
|
+
if (pause)
|
|
601
|
+
await pause;
|
|
602
|
+
return afterCloseStaged(grantedClose, { predecessorId: String(predecessor.id), successor });
|
|
603
|
+
},
|
|
604
|
+
confirmEffect: confirmsSingleHead,
|
|
605
|
+
missing: () => storedRowChangedRefusal("Memory"),
|
|
606
|
+
refusal: () => skillUpdateNotClosedRefusal(SKILL_UPDATE_CLOSE_ATTEMPTS),
|
|
607
|
+
unconfirmed: skillUpdateUnconfirmedFailure,
|
|
608
|
+
held: args.held,
|
|
609
|
+
});
|
|
610
|
+
}
|
|
326
611
|
/**
|
|
327
612
|
* Run one skill version write atomically. The append helper owns the lock, the
|
|
328
|
-
*
|
|
613
|
+
* version row; this function performs the Memory-side
|
|
329
614
|
* successor/close/pointer writes inside that same transaction.
|
|
330
615
|
*/
|
|
331
616
|
export async function runSkillVersionWrite(args) {
|
|
@@ -334,8 +619,33 @@ export async function runSkillVersionWrite(args) {
|
|
|
334
619
|
// request context, which would surface as HTTP 201. The path a skill write
|
|
335
620
|
// replaces returned 200, so restore the flag's prior value on the way out.
|
|
336
621
|
const restoreCreated = captureCreatedFlag(args.ctx);
|
|
622
|
+
const held = new Set((args.heldMemoryIds ?? []).map((id) => String(id)));
|
|
337
623
|
try {
|
|
338
|
-
|
|
624
|
+
const journal = new WriteJournal();
|
|
625
|
+
const result = await journal.run(async () => {
|
|
626
|
+
const r = await runSkillVersionWriteInner(args);
|
|
627
|
+
if (!r.ok && !journal.compensationAttempted && journal.entries.some((entry) => entry.applied)) {
|
|
628
|
+
const failure = await compensateUnlandedSkillUpdate({ ctx: args.ctx, journal, held });
|
|
629
|
+
if (failure)
|
|
630
|
+
return { ok: false, response: failure };
|
|
631
|
+
}
|
|
632
|
+
return r;
|
|
633
|
+
});
|
|
634
|
+
if (result.ok && result.confirm) {
|
|
635
|
+
const confirm = result.confirm;
|
|
636
|
+
return {
|
|
637
|
+
...result,
|
|
638
|
+
confirm: async () => {
|
|
639
|
+
const denial = await confirm();
|
|
640
|
+
if (!denial)
|
|
641
|
+
return null;
|
|
642
|
+
// The caller released the write's Memory locks before this ran.
|
|
643
|
+
const failure = await compensateUnlandedSkillUpdate({ ctx: args.ctx, journal });
|
|
644
|
+
return failure ?? denial;
|
|
645
|
+
},
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
return result;
|
|
339
649
|
}
|
|
340
650
|
finally {
|
|
341
651
|
restoreCreated();
|
|
@@ -355,18 +665,42 @@ export function captureCreatedFlag(ctx) {
|
|
|
355
665
|
}
|
|
356
666
|
};
|
|
357
667
|
}
|
|
668
|
+
/** A plan whose success {@link confirmSkillUpdate} checks after the commit. */
|
|
669
|
+
function supersedesPredecessor(plan) {
|
|
670
|
+
return plan.kind === "update" && !!plan.predecessor;
|
|
671
|
+
}
|
|
672
|
+
/** A joined attempt's signal that its plan needs a commit this write owns. */
|
|
673
|
+
const OWNED_COMMIT_REQUIRED = Symbol("skill-version-write.owned-commit-required");
|
|
358
674
|
async function runSkillVersionWriteInner(args) {
|
|
675
|
+
const joined = args.joinCallerTransaction === true && isJoinableTransaction(args.ctx);
|
|
676
|
+
const first = await runSkillVersionAttempt(args, joined);
|
|
677
|
+
if (first !== OWNED_COMMIT_REQUIRED)
|
|
678
|
+
return first;
|
|
679
|
+
// flair#2645: the update is confirmed against the committed store after its
|
|
680
|
+
// commit, which a joined write leaves to the caller. The joined attempt
|
|
681
|
+
// stopped before staging a write; plan again from a fresh head, owned.
|
|
682
|
+
const owned = await runSkillVersionAttempt(args, false);
|
|
683
|
+
if (owned === OWNED_COMMIT_REQUIRED)
|
|
684
|
+
throw new Error("skill-version-write: an owned attempt cannot require an owned commit");
|
|
685
|
+
return owned;
|
|
686
|
+
}
|
|
687
|
+
async function runSkillVersionAttempt(args, joined) {
|
|
359
688
|
let plan = null;
|
|
360
689
|
let owner = "";
|
|
361
|
-
|
|
690
|
+
let ownedCommitRequired = false;
|
|
691
|
+
const outcome = await recordVersion(args.ctx, {
|
|
362
692
|
subjectType: "skill",
|
|
363
693
|
prepare: async (shared) => {
|
|
364
694
|
const head = await args.head(shared);
|
|
365
|
-
const built = await args.plan(head, shared);
|
|
695
|
+
const built = await args.plan(head, shared, { joined });
|
|
366
696
|
if (built === null)
|
|
367
697
|
return null;
|
|
368
698
|
if (built instanceof Response)
|
|
369
699
|
return built;
|
|
700
|
+
if (joined && supersedesPredecessor(built)) {
|
|
701
|
+
ownedCommitRequired = true;
|
|
702
|
+
return skillUpdateUnconfirmedFailure();
|
|
703
|
+
}
|
|
370
704
|
// A write grant does not include changing a record's visibility;
|
|
371
705
|
// visibility is set by the owner or an admin (grant-visibility.ts). The
|
|
372
706
|
// plan is checked here and again on committed state once the close is
|
|
@@ -405,7 +739,7 @@ async function runSkillVersionWriteInner(args) {
|
|
|
405
739
|
return revoked;
|
|
406
740
|
}
|
|
407
741
|
else {
|
|
408
|
-
maybeThrowSkillWriteFault("successor", owner);
|
|
742
|
+
maybeThrowSkillWriteFault("successor", owner, writePlan.successor);
|
|
409
743
|
const created = await args.hooks.createSuccessor(writePlan.successor, shared);
|
|
410
744
|
if (created instanceof Response)
|
|
411
745
|
return storageResult(created);
|
|
@@ -428,5 +762,44 @@ async function runSkillVersionWriteInner(args) {
|
|
|
428
762
|
if (pointerDenial)
|
|
429
763
|
return pointerDenial;
|
|
430
764
|
return okResponse();
|
|
431
|
-
});
|
|
765
|
+
}, joined);
|
|
766
|
+
if (ownedCommitRequired)
|
|
767
|
+
return OWNED_COMMIT_REQUIRED;
|
|
768
|
+
if (!outcome.ok || plan === null)
|
|
769
|
+
return outcome;
|
|
770
|
+
const writePlan = plan;
|
|
771
|
+
if (!supersedesPredecessor(writePlan))
|
|
772
|
+
return outcome;
|
|
773
|
+
const confirm = async () => {
|
|
774
|
+
// Test-only: inert unless armed. Pauses between the committed write and the
|
|
775
|
+
// confirmation read, so a later update of the same subject can complete.
|
|
776
|
+
// The pause stays outside the confirmation's lock acquisition.
|
|
777
|
+
const confirmPause = txnPausePoint("skill-update-confirm");
|
|
778
|
+
if (confirmPause)
|
|
779
|
+
await confirmPause;
|
|
780
|
+
const authorize = args.hooks.authorizeClose ?? (async () => skillUpdateUnconfirmedFailure());
|
|
781
|
+
// A deferred confirmation runs after the verb released its Memory locks; an
|
|
782
|
+
// inline one runs under the locks the caller still holds (flair#2950).
|
|
783
|
+
const held = args.deferConfirmation ? NO_HELD_IDS : new Set((args.heldMemoryIds ?? []).map(String));
|
|
784
|
+
// flair#2645: the owned transaction has committed, but its success is not
|
|
785
|
+
// proof that the queued close was applied. Confirm against the committed
|
|
786
|
+
// store before answering success.
|
|
787
|
+
return await confirmSkillUpdate({
|
|
788
|
+
ctx: args.ctx,
|
|
789
|
+
subjectId: args.subjectId,
|
|
790
|
+
predecessor: writePlan.predecessor,
|
|
791
|
+
closePatch: writePlan.closePatch,
|
|
792
|
+
successor: writePlan.successor,
|
|
793
|
+
grantedClose: writePlan.grantedClose,
|
|
794
|
+
authorize,
|
|
795
|
+
held,
|
|
796
|
+
appendedVersionId: outcome.version || null,
|
|
797
|
+
});
|
|
798
|
+
};
|
|
799
|
+
if (args.deferConfirmation)
|
|
800
|
+
return { ...outcome, confirm };
|
|
801
|
+
const confirmation = await confirm();
|
|
802
|
+
if (confirmation)
|
|
803
|
+
return { ok: false, response: confirmation };
|
|
804
|
+
return outcome;
|
|
432
805
|
}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* skill-write-fault.ts — the injection point for a per-step fault in the
|
|
3
3
|
* transactional skill writer (flair#2139 S2): successor write, predecessor
|
|
4
|
-
* close, pointer write, version append.
|
|
4
|
+
* close, pointer write, version append, instruction event.
|
|
5
5
|
*
|
|
6
6
|
* The hook is null unless setSkillWriteFaultHook installs one. Nothing under
|
|
7
7
|
* resources/ or src/ calls it; the real-Harper suite installs it from
|
|
8
8
|
* test/fixtures/skill-write-fault-2139/probe.js, which is not in the package.
|
|
9
|
+
* The optional third argument is the exact row about to be staged, so a test
|
|
10
|
+
* fixture can hand the writer a row its table's validator must refuse
|
|
11
|
+
* (flair#2997); the hook returns void and the writer does not read the row back.
|
|
9
12
|
*/
|
|
10
13
|
let hook = null;
|
|
11
14
|
export function setSkillWriteFaultHook(next) {
|
|
12
15
|
hook = next;
|
|
13
16
|
}
|
|
14
17
|
/** Run the installed hook for `step`; a no-op when none is installed. */
|
|
15
|
-
export function maybeThrowSkillWriteFault(step, agentId) {
|
|
16
|
-
hook?.(step, agentId);
|
|
18
|
+
export function maybeThrowSkillWriteFault(step, agentId, record) {
|
|
19
|
+
hook?.(step, agentId, record);
|
|
17
20
|
}
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
* never from a body field or connector label. No Harper imports.
|
|
25
25
|
*/
|
|
26
26
|
import { resolveAgentAuth } from "./agent-auth.js";
|
|
27
|
+
import { isDeliberateInternalVerdict } from "./internal-authority.js";
|
|
27
28
|
import { FORBIDDEN, UNAUTH } from "./record-type-kit.js";
|
|
28
29
|
/**
|
|
29
30
|
* The credential class of a skill write, or null when the caller may not write.
|
|
@@ -33,7 +34,7 @@ import { FORBIDDEN, UNAUTH } from "./record-type-kit.js";
|
|
|
33
34
|
export function skillWriteSource(context, auth) {
|
|
34
35
|
const request = context?.request ?? context;
|
|
35
36
|
// A deliberate internal call: no HTTP request at all and the marker set.
|
|
36
|
-
if (
|
|
37
|
+
if (isDeliberateInternalVerdict(context, auth))
|
|
37
38
|
return "internal";
|
|
38
39
|
const header = request?.headers?.get?.("authorization") ?? request?.headers?.asObject?.authorization ?? "";
|
|
39
40
|
// Role is not source: only verified Basic admin auth is the operator class.
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { resolveAgentAuth } from "./agent-auth.js";
|
|
2
|
+
import { isDeliberateInternalVerdict } from "./internal-authority.js";
|
|
2
3
|
import { FORBIDDEN, UNAUTH } from "./record-type-kit.js";
|
|
3
4
|
import { buildProvenance } from "./provenance.js";
|
|
4
5
|
import { databases } from "harper";
|
|
5
|
-
import {
|
|
6
|
+
import { SOUL_SCOPE_FIELDS_REFUSAL, suppliesSoulScopeFields } from "../src/lib/soul-scope-fields.js";
|
|
6
7
|
// Role is not source: an admin agent key or delegated OAuth identity is still
|
|
7
8
|
// a runtime credential. Only verified Basic admin auth enters the operator path.
|
|
8
9
|
// Callers cannot choose this class via body fields or connector-supplied labels.
|
|
9
10
|
export function soulWriteSource(context, auth) {
|
|
10
11
|
const request = context?.request ?? context;
|
|
11
12
|
// Internal path needs a deliberate __flairInternal marker; a contextless call is refused.
|
|
12
|
-
if (
|
|
13
|
+
if (isDeliberateInternalVerdict(context, auth))
|
|
13
14
|
return "internal";
|
|
14
15
|
const header = request?.headers?.get?.("authorization") ?? request?.headers?.asObject?.authorization ?? "";
|
|
15
16
|
if (auth.kind === "agent" && auth.isAdmin && /^Basic\s/i.test(header))
|
|
@@ -23,6 +24,15 @@ export async function authorizeSoulWrite(context) {
|
|
|
23
24
|
: source ? null : FORBIDDEN("soul_write_requires_operator: use authenticated operator credentials");
|
|
24
25
|
return { auth, source, denied };
|
|
25
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether this context's soul write would pass source authorization: the
|
|
29
|
+
* decision {@link authorizeSoulWrite} returns, so a surface that must decide
|
|
30
|
+
* BEFORE a write (the /mcp `tools/list` gate) asks the write's
|
|
31
|
+
* source-authorization question instead of restating the rule.
|
|
32
|
+
*/
|
|
33
|
+
export async function soulWriteAllowed(context) {
|
|
34
|
+
return (await authorizeSoulWrite(context)).denied === null;
|
|
35
|
+
}
|
|
26
36
|
export function soulProvenance(auth, source, now) {
|
|
27
37
|
const provenance = JSON.parse(buildProvenance(auth, now, {}));
|
|
28
38
|
provenance.verified.sourceClass = source;
|
|
@@ -53,11 +63,20 @@ export async function refuseLearnedSoulWrite(content) {
|
|
|
53
63
|
}
|
|
54
64
|
return null;
|
|
55
65
|
}
|
|
56
|
-
/**
|
|
57
|
-
*
|
|
66
|
+
/** Refuse a write that would store a non-empty `scopeTag` or a non-empty
|
|
67
|
+
* `tags` array: Soul has no dedicated or enforced per-user scope field (flair#1540). */
|
|
68
|
+
export function refuseSoulScopeFields(content) {
|
|
69
|
+
return suppliesSoulScopeFields(content) ? FORBIDDEN(SOUL_SCOPE_FIELDS_REFUSAL) : null;
|
|
70
|
+
}
|
|
71
|
+
/** Content guards after source authorization: the vendor-neutral scope-field
|
|
72
|
+
* refusal, then the generic learned-content backstop. New connectors need no
|
|
73
|
+
* Soul-side branch.
|
|
74
|
+
*
|
|
75
|
+
* `content` is the row this write would store: POST and PUT pass the request
|
|
76
|
+
* body, PATCH the merged row, so a stored row's scope fields decide a PATCH. */
|
|
58
77
|
export async function refuseSoulWriteContent(content) {
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
61
|
-
return
|
|
78
|
+
const scope = refuseSoulScopeFields(content);
|
|
79
|
+
if (scope)
|
|
80
|
+
return scope;
|
|
62
81
|
return refuseLearnedSoulWrite(content);
|
|
63
82
|
}
|
|
@@ -17,7 +17,7 @@ export async function patchRecord(table, id, patch, opts = {}) {
|
|
|
17
17
|
if (!existing)
|
|
18
18
|
throw new Error(`Record ${id} not found`);
|
|
19
19
|
// The lastReflected stamp updates only the patch's fields.
|
|
20
|
-
return { update: patch };
|
|
20
|
+
return { idempotent: true, update: patch };
|
|
21
21
|
}, { ...opts, label: opts.label ?? "patchRecord" });
|
|
22
22
|
}
|
|
23
23
|
/**
|