@tpsdev-ai/flair 0.59.0 → 0.63.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 +10 -10
- package/SECURITY.md +49 -5
- package/config.yaml +23 -12
- package/dist/bridges/builtins/mem0.js +63 -28
- package/dist/bridges/runtime/execute.js +1 -0
- package/dist/bridges/runtime/import-runner.js +12 -0
- package/dist/bridges/runtime/load-plugin.js +42 -13
- package/dist/bridges/runtime/yaml-loader.js +4 -2
- package/dist/bridges/types.js +1 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1054 -367
- package/dist/commands/agent.js +233 -90
- package/dist/commands/attention.js +4 -3
- package/dist/commands/backup-approval.js +218 -0
- package/dist/commands/backup.js +211 -39
- package/dist/commands/bootstrap.js +3 -1
- package/dist/commands/bridge.js +172 -119
- package/dist/commands/deploy.js +71 -8
- package/dist/commands/doctor.js +711 -146
- package/dist/commands/export.js +57 -23
- package/dist/commands/federation.js +787 -82
- package/dist/commands/grant.js +5 -6
- package/dist/commands/help.js +25 -0
- package/dist/commands/hook.js +155 -2
- package/dist/commands/idp.js +27 -19
- package/dist/commands/import.js +136 -20
- package/dist/commands/init.js +469 -200
- package/dist/commands/integrity.js +387 -0
- package/dist/commands/keys.js +247 -58
- package/dist/commands/mcp.js +108 -52
- package/dist/commands/memory.js +203 -35
- package/dist/commands/migrate-harness-memory.js +3 -2
- package/dist/commands/orgevent.js +51 -47
- package/dist/commands/presence.js +4 -3
- package/dist/commands/principal.js +428 -62
- package/dist/commands/quality.js +13 -1
- package/dist/commands/reembed.js +141 -17
- package/dist/commands/relationship.js +2 -1
- package/dist/commands/rem.js +537 -80
- package/dist/commands/restore.js +413 -65
- package/dist/commands/search.js +6 -4
- package/dist/commands/service.js +235 -40
- package/dist/commands/soul.js +4 -9
- package/dist/commands/status.js +94 -38
- package/dist/commands/test.js +7 -3
- package/dist/commands/upgrade.js +77 -23
- package/dist/commands/workspace.js +13 -11
- package/dist/component-env.js +31 -41
- package/dist/deploy.js +136 -4
- package/dist/doctor-client.js +534 -92
- package/dist/engine-version-contract.js +1 -0
- package/dist/engine-version.js +23 -3
- package/dist/fabric-upgrade.js +12 -2
- package/dist/federation/presence-send.js +139 -0
- package/dist/federation-verify.js +273 -180
- package/dist/hook-install.js +670 -6
- package/dist/install/clients.js +171 -35
- package/dist/keystore.js +72 -0
- package/dist/lib/action-recall-runtime.js +328 -0
- package/dist/lib/agent-home-fields.js +132 -0
- package/dist/lib/agent-home.js +427 -0
- package/dist/lib/agent-id-rule.js +88 -0
- package/dist/lib/agent-roster.js +33 -0
- package/dist/lib/atomic-write.js +1 -1
- package/dist/lib/auth-resolve.js +1069 -60
- package/dist/lib/backup-approval-store.js +612 -0
- package/dist/lib/backup-artifact.js +343 -0
- package/dist/lib/capture-runtime.js +124 -0
- package/dist/lib/config-critical-section.js +212 -25
- package/dist/lib/connect-phase.js +33 -0
- package/dist/lib/credential-status-rule.js +78 -0
- package/dist/lib/daemon-liveness.js +43 -11
- package/dist/lib/doctor-run.js +75 -37
- package/dist/lib/durability-copy.js +10 -0
- package/dist/lib/embedding-space-doctor.js +75 -0
- 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/flair-agent-role.js +4 -0
- package/dist/lib/hub-url.js +12 -0
- package/dist/lib/init-admin-pass.js +88 -12
- package/dist/lib/init-listener-environ.js +1 -2
- package/dist/lib/init-occupied-listener.js +15 -111
- package/dist/lib/init-spawn-attribution.js +73 -0
- package/dist/lib/init-tcp-probe.js +33 -0
- package/dist/lib/instance-identity-row.js +20 -0
- package/dist/lib/instruction-event-contract.js +17 -0
- package/dist/lib/instruction-event-doctor.js +102 -0
- package/dist/lib/launchd-management.js +38 -3
- package/dist/lib/listener-table.js +268 -0
- package/dist/lib/local-delete-instance.js +27 -0
- package/dist/lib/local-instance-pair.js +256 -0
- package/dist/lib/mcp-enable.js +1182 -341
- package/dist/lib/mcp-oauth-env-core.js +133 -0
- package/dist/lib/mcp-oauth-env.js +125 -0
- package/dist/lib/mcp-spec.js +17 -1
- package/dist/lib/memory-attributes.js +4 -0
- package/dist/lib/memory-id-policy.js +46 -0
- package/dist/lib/memory-integrity.js +309 -0
- package/dist/lib/memory-provider-timeout.js +33 -0
- package/dist/lib/memory-purge-response.js +20 -0
- package/dist/lib/mqtt-config.js +132 -0
- package/dist/lib/mqtt-listener-doctor.js +104 -0
- package/dist/lib/open-live-row.js +20 -0
- package/dist/lib/ops-table-count.js +25 -0
- package/dist/lib/ops-url.js +52 -0
- package/dist/lib/owned-pins.js +67 -10
- package/dist/lib/peer-status.js +27 -0
- package/dist/lib/principal-status.js +29 -0
- package/dist/lib/process-start-time.js +76 -12
- package/dist/lib/promotion-content-hash.js +20 -0
- package/dist/lib/proto-safe-record.js +42 -0
- package/dist/lib/record-id-path.js +25 -0
- package/dist/lib/reembed-server-support.js +66 -0
- package/dist/lib/restore-archive.js +760 -0
- package/dist/lib/restore-verify.js +104 -0
- package/dist/lib/scheduler-platform.js +10 -6
- package/dist/lib/secret-keys.js +25 -0
- package/dist/lib/secrets-push.js +50 -22
- package/dist/lib/skill-assignment-preflight.js +159 -0
- package/dist/lib/skill-seed-pending.js +24 -0
- package/dist/lib/skill-seed.js +448 -0
- package/dist/lib/skill-tombstone-doctor.js +133 -0
- package/dist/lib/soul-scope-fields.js +26 -0
- package/dist/lib/stop-start-recovery.js +54 -0
- package/dist/lib/uninstall-purge.js +266 -11
- package/dist/lib/upgrade-prior-liveness.js +2 -1
- package/dist/lib/using-flair-skill.js +87 -0
- package/dist/mcp-client-assertion.js +114 -14
- package/dist/memory-id-checkpoint.js +347 -0
- package/dist/memory-node-compare.js +207 -0
- package/dist/mqtt-off-deploy.js +550 -0
- package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
- package/dist/rem/promote-policy.js +3 -3
- package/dist/rem/restore.js +303 -26
- package/dist/rem/runner.js +317 -96
- 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 +463 -33
- package/dist/resources/AgentCard.js +4 -0
- package/dist/resources/AgentKeyRegister.js +248 -0
- package/dist/resources/AgentKeyRegistration.js +28 -0
- package/dist/resources/AgentReadPosition.js +7 -0
- package/dist/resources/AgentSeed.js +238 -107
- package/dist/resources/AutoPromoteCandidates.js +413 -78
- package/dist/resources/Credential.js +183 -10
- package/dist/resources/Federation.js +915 -145
- package/dist/resources/FederationRetry.js +140 -0
- package/dist/resources/InstructionVersion.js +220 -0
- package/dist/resources/Integration.js +300 -25
- package/dist/resources/MCPClientMetadata.js +6 -3
- package/dist/resources/Memory.js +3300 -758
- package/dist/resources/MemoryArchive.js +171 -53
- package/dist/resources/MemoryBootstrap.js +122 -61
- package/dist/resources/MemoryCandidate.js +37 -3
- package/dist/resources/MemoryConsolidate.js +2 -2
- package/dist/resources/MemoryDedupStats.js +30 -1
- package/dist/resources/MemoryFeed.js +545 -50
- package/dist/resources/MemoryGrant.js +21 -1
- package/dist/resources/MemoryMaintenance.js +368 -46
- package/dist/resources/MemoryPurge.js +348 -0
- package/dist/resources/MemoryReflect.js +71 -12
- package/dist/resources/MemoryReindex.js +111 -17
- package/dist/resources/Message.js +16 -5
- package/dist/resources/OAuth.js +342 -48
- package/dist/resources/OAuthClient.js +1 -1
- package/dist/resources/OrgEvent.js +405 -19
- 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 +166 -51
- package/dist/resources/PromoteMemoryCandidate.js +24 -7
- package/dist/resources/RecordUsage.js +2 -4
- package/dist/resources/Relationship.js +23 -0
- package/dist/resources/SemanticSearch.js +63 -32
- package/dist/resources/Soul.js +191 -76
- package/dist/resources/SoulFeed.js +2 -1
- package/dist/resources/TeamDirectory.js +35 -0
- package/dist/resources/WorkspaceLatest.js +4 -0
- package/dist/resources/WorkspaceState.js +21 -1
- package/dist/resources/XAA.js +207 -63
- 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 +138 -83
- 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 +534 -171
- 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/ed25519-auth.js +1 -1
- package/dist/resources/embedding-space-guard.js +366 -72
- package/dist/resources/embeddings-provider.js +25 -0
- package/dist/resources/engine-version-stamp-boot.js +53 -0
- package/dist/resources/federation-classify.js +161 -0
- package/dist/resources/federation-crypto.js +5 -5
- package/dist/resources/federation-retry-lib.js +60 -0
- package/dist/resources/grant-visibility.js +135 -0
- package/dist/resources/harper-routing.js +83 -0
- package/dist/resources/health.js +163 -22
- package/dist/resources/host-pointer/registry.js +29 -0
- package/dist/resources/host-pointer-adapter.js +1 -0
- package/dist/resources/host-source-citation.js +49 -0
- package/dist/resources/host-source-visibility.js +7 -6
- package/dist/resources/in-process.js +24 -22
- package/dist/resources/instance-create-lock.js +17 -0
- package/dist/resources/instance-identity.js +23 -0
- package/dist/resources/instruction-version-record.js +587 -0
- package/dist/resources/internal-authority.js +13 -0
- package/dist/resources/key-lock.js +3 -3
- package/dist/resources/mcp-handler.js +141 -19
- package/dist/resources/mcp-oauth-env-guard.js +36 -0
- package/dist/resources/mcp-oauth-flag.js +12 -14
- package/dist/resources/mcp-oauth.js +450 -56
- package/dist/resources/mcp-tool-arguments.js +4 -6
- package/dist/resources/mcp-tools.js +187 -40
- package/dist/resources/memory-bootstrap-lib.js +5 -15
- package/dist/resources/memory-declared-attributes.js +4 -1
- package/dist/resources/memory-deletion-history.js +55 -0
- package/dist/resources/memory-durability.js +54 -0
- package/dist/resources/memory-embed-hash.js +144 -0
- package/dist/resources/memory-embedding-value.js +8 -0
- package/dist/resources/memory-host-source.js +32 -7
- package/dist/resources/memory-id-guard.js +75 -0
- package/dist/resources/memory-redaction.js +236 -0
- package/dist/resources/memory-reflect-lib.js +1 -1
- package/dist/resources/memory-supplied-vector.js +72 -0
- package/dist/resources/memory-write-lock.js +489 -0
- package/dist/resources/memory-write-response.js +139 -0
- 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 +90 -19
- package/dist/resources/migrations/dir-safety.js +61 -3
- package/dist/resources/migrations/embedding-stamp.js +462 -82
- package/dist/resources/migrations/export.js +1 -0
- package/dist/resources/migrations/graph-heal.js +27 -27
- package/dist/resources/migrations/ledger.js +5 -2
- package/dist/resources/migrations/lock.js +870 -77
- package/dist/resources/migrations/progress.js +71 -10
- package/dist/resources/migrations/runner.js +212 -18
- package/dist/resources/migrations/snapshot.js +1 -0
- package/dist/resources/migrations/source-fields.js +5 -5
- package/dist/resources/migrations/state.js +36 -1
- package/dist/resources/migrations/status.js +2 -13
- package/dist/resources/migrations/synthetic-test-migration.js +81 -12
- package/dist/resources/migrations/types.js +26 -1
- package/dist/resources/migrations/visibility-backfill.js +113 -72
- package/dist/resources/oauth-discovery.js +0 -5
- package/dist/resources/object-body-guard.js +40 -0
- package/dist/resources/org-event-created-at.js +133 -0
- package/dist/resources/org-event-reserved.js +92 -0
- package/dist/resources/org-event-write-fault.js +19 -0
- package/dist/resources/originator-instance.js +39 -7
- package/dist/resources/owner-delete-recheck.js +78 -0
- package/dist/resources/owner-field-guard.js +39 -5
- package/dist/resources/owner-scoped-write-policy.js +256 -0
- package/dist/resources/owner-scoped-writes.js +89 -0
- package/dist/resources/path-id-rewrite.js +76 -0
- package/dist/resources/presence-internal.js +5 -5
- package/dist/resources/presence-receipt.js +236 -0
- package/dist/resources/promotion-binding.js +43 -0
- package/dist/resources/promotion-stamp.js +143 -17
- package/dist/resources/protocol-context.js +189 -0
- package/dist/resources/record-owner-guard.js +74 -26
- package/dist/resources/record-type-kit.js +2 -4
- package/dist/resources/record-types.js +1 -1
- package/dist/resources/relay-ops.js +78 -6
- package/dist/resources/replay-store.js +96 -19
- package/dist/resources/request-target.js +163 -0
- package/dist/resources/request-transaction.js +46 -1
- package/dist/resources/search-readiness.js +4 -1
- package/dist/resources/seed-ids.js +9 -0
- package/dist/resources/seed-reservation.js +116 -0
- package/dist/resources/semantic-retrieval-core.js +27 -15
- package/dist/resources/server-row-validation.js +71 -0
- package/dist/resources/skill-manifest.js +24 -14
- package/dist/resources/skill-subject.js +53 -0
- package/dist/resources/skill-version-write.js +789 -0
- package/dist/resources/skill-write-fault.js +20 -0
- package/dist/resources/skill-write-policy.js +68 -0
- package/dist/resources/skill-write.js +67 -7
- package/dist/resources/soul-write-policy.js +26 -7
- package/dist/resources/table-helpers.js +10 -7
- package/dist/resources/table-subscription-policy.js +518 -85
- package/dist/resources/table-subscriptions.js +20 -7
- package/dist/resources/team-directory.js +248 -0
- package/dist/resources/tool-descriptors/index.js +69 -8
- package/dist/resources/txn-pause-point.js +235 -0
- package/dist/resources/usage-recording.js +186 -70
- package/dist/resources/write-back.js +383 -0
- package/dist/resources/write-journal-context.js +5 -0
- package/dist/resources/write-journal.js +289 -0
- package/dist/src/engine-version-contract.js +1 -0
- package/dist/src/keystore.js +72 -0
- package/dist/src/lib/agent-home-fields.js +132 -0
- package/dist/src/lib/agent-id-rule.js +88 -0
- package/dist/src/lib/agent-key-register.js +80 -0
- package/dist/src/lib/atomic-write.js +256 -0
- package/dist/src/lib/credential-status-rule.js +78 -0
- package/dist/src/lib/expired-by-agent.js +37 -0
- package/dist/src/lib/federation-retry-row.js +101 -0
- package/dist/src/lib/flair-agent-role.js +4 -0
- package/dist/src/lib/idp-mapping-rule.js +81 -0
- package/dist/src/lib/instance-identity-row.js +20 -0
- package/dist/src/lib/instruction-event-contract.js +17 -0
- package/dist/src/lib/mcp-oauth-env-core.js +133 -0
- package/dist/src/lib/memory-attributes.js +4 -0
- package/dist/src/lib/memory-id-policy.js +46 -0
- package/dist/src/lib/open-live-row.js +20 -0
- package/dist/src/lib/principal-status.js +29 -0
- package/dist/src/lib/proto-safe-record.js +42 -0
- package/dist/src/lib/reembed-server-support.js +66 -0
- package/dist/src/lib/scheduler-platform.js +10 -6
- package/dist/src/lib/soul-scope-fields.js +26 -0
- package/dist/src/lib/systemd-agent-id.js +38 -0
- package/dist/src/rem/promote-policy.js +3 -3
- package/dist/src/rem/scheduler.js +21 -4
- package/docs/access-control.md +164 -54
- package/docs/api-reference.md +165 -32
- package/docs/auth.md +46 -30
- package/docs/bridges.md +11 -0
- package/docs/claude-code.md +37 -7
- package/docs/deploying-on-fabric.md +178 -0
- package/docs/deployment.md +19 -0
- package/docs/embedding-in-a-harper-app.md +17 -15
- package/docs/entity-vocabulary.md +3 -6
- package/docs/federation.md +61 -3
- package/docs/hosted-on-fabric.md +43 -36
- package/docs/integrations.md +14 -14
- package/docs/mcp-clients.md +12 -7
- package/docs/n8n.md +17 -11
- package/docs/notes/mcp-agent-auth-consumer.md +16 -13
- package/docs/notes/mcp-oauth-model2.md +40 -11
- package/docs/openclaw.md +1 -1
- package/docs/quickstart-fabric.md +4 -4
- package/docs/quickstart.md +12 -4
- package/docs/releasing.md +25 -19
- package/docs/rem.md +32 -3
- package/docs/secrets-and-keys.md +21 -4
- package/docs/standalone-local.md +30 -2
- package/docs/supply-chain-policy.md +34 -16
- package/docs/the-team.md +2 -2
- package/docs/upgrade.md +46 -6
- package/package.json +24 -12
- package/schemas/agent-key-registration.graphql +22 -0
- package/schemas/agent.graphql +6 -3
- package/schemas/federation.graphql +41 -1
- package/schemas/memory.graphql +91 -7
- package/schemas/oauth.graphql +19 -0
- package/schemas/schema.graphql +2 -0
- 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,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory-embed-hash.ts — the text hash the server stores with a vector it
|
|
3
|
+
* computes, and the expected hash a stored value is compared with (flair#2465,
|
|
4
|
+
* part 2).
|
|
5
|
+
*
|
|
6
|
+
* A row's embedding is only meaningful for the text it was embedded from. This
|
|
7
|
+
* module defines that text's hash with the SAME function that defines a Memory
|
|
8
|
+
* row's `contentHash` (`memory-feed-lib.ts`'s `computeContentHash`), applied to
|
|
9
|
+
* the field the row actually embeds from (`skill-write.ts`'s `skillEmbedText`:
|
|
10
|
+
* `trigger` for a skill-tagged row with a non-empty trigger, else `content`).
|
|
11
|
+
*
|
|
12
|
+
* Three values are kept apart:
|
|
13
|
+
* - the EXPECTED hash of a row's current text ({@link embeddedTextHash});
|
|
14
|
+
* - a NEWLY COMPUTED hash: the expected hash the server stores as
|
|
15
|
+
* `embeddedContentHash` when it computes a vector on a guarded path (an
|
|
16
|
+
* ordinary Memory POST/PUT/PATCH embed, the re-embed PATCH, the stamp
|
|
17
|
+
* migration's `.content`-suffix fallback, the auto-embed backfill);
|
|
18
|
+
* - a RETAINED hash: a preserved stored value that a write keeps without
|
|
19
|
+
* computing a vector (a PUT or PATCH that carries a vector or stores one
|
|
20
|
+
* accepted from the caller, a skill successor that carries the stored
|
|
21
|
+
* vector). It may describe an
|
|
22
|
+
* earlier server-computed vector, not necessarily the vector stored now.
|
|
23
|
+
* A value received through federation is an unverified peer assertion.
|
|
24
|
+
* Exceptions: a row with an empty owner gets no hash (null) even from a
|
|
25
|
+
* server-computed embed, and a vector accepted and stored from the caller gets
|
|
26
|
+
* no newly computed hash (a POST or PUT create stores null; an admin PATCH
|
|
27
|
+
* create drops a submitted value and can leave the field absent; an update
|
|
28
|
+
* retains the stored hash beside it). When redaction changes the text, the
|
|
29
|
+
* supplied vector is discarded instead, and the write embeds the redacted text
|
|
30
|
+
* and stores a newly computed hash when the engine returns a vector.
|
|
31
|
+
*
|
|
32
|
+
* The embedding-stamp migration treats an embedding as current when its
|
|
33
|
+
* `embeddingModel` is current-space AND the stored hash is absent or equals the
|
|
34
|
+
* expected hash. A stored hash that differs from the expected hash re-queues
|
|
35
|
+
* the row; an absent hash does not.
|
|
36
|
+
*
|
|
37
|
+
* Deliberately a plain function: importing `memory-feed-lib.js` /
|
|
38
|
+
* `skill-write.js` (both Harper-free) keeps this usable from a migration's unit
|
|
39
|
+
* test with no live runtime.
|
|
40
|
+
*/
|
|
41
|
+
import { computeContentHash } from "./memory-feed-lib.js";
|
|
42
|
+
import { skillEmbedText } from "./skill-write.js";
|
|
43
|
+
/**
|
|
44
|
+
* `computeContentHash(agentId, text)`. Requires a non-empty string `agentId`
|
|
45
|
+
* (the row's owner) and non-empty `text`; otherwise `undefined`, and a writer
|
|
46
|
+
* stores null.
|
|
47
|
+
*/
|
|
48
|
+
export function hashEmbeddedText(agentId, text) {
|
|
49
|
+
if (typeof agentId !== "string" || agentId.length === 0)
|
|
50
|
+
return undefined;
|
|
51
|
+
if (typeof text !== "string" || text.length === 0)
|
|
52
|
+
return undefined;
|
|
53
|
+
return computeContentHash(agentId, text);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The EXPECTED hash of a row's current embedding text: {@link hashEmbeddedText}
|
|
57
|
+
* of the row's owner and `skillEmbedText(row)`.
|
|
58
|
+
*/
|
|
59
|
+
export function embeddedTextHash(row) {
|
|
60
|
+
if (!row || typeof row !== "object")
|
|
61
|
+
return undefined;
|
|
62
|
+
return hashEmbeddedText(row.agentId, skillEmbedText(row));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The hash half of "is a row's stored embedding current?" (the caller applies
|
|
66
|
+
* the `embeddingModel` half, `isCurrentSpaceStamp`). True when the row stores
|
|
67
|
+
* no `embeddedContentHash`, or the stored hash equals the expected hash of the
|
|
68
|
+
* row's current embedding text.
|
|
69
|
+
*
|
|
70
|
+
* Only an actually absent hash (null or undefined) bypasses the comparison
|
|
71
|
+
* here; a matching hash passes it. Either way the caller still requires a
|
|
72
|
+
* current-space model. A row can lack a hash because it was written before
|
|
73
|
+
* this field existed, was created through POST or PUT with a vector accepted
|
|
74
|
+
* from the caller or through an admin PATCH create, or was written by a path
|
|
75
|
+
* that stores none; a server-computed embed stores none for a row with an
|
|
76
|
+
* empty owner or no text. A row with an empty owner or no text can still hold
|
|
77
|
+
* a retained or federation-received hash: that value is compared like any
|
|
78
|
+
* other and, with no expected hash to equal, fails the comparison. See the
|
|
79
|
+
* migration's module doc for the cost of re-queueing rows with no hash
|
|
80
|
+
* instead.
|
|
81
|
+
*/
|
|
82
|
+
export function embeddedHashMatches(row) {
|
|
83
|
+
const recorded = row?.embeddedContentHash;
|
|
84
|
+
if (recorded === undefined || recorded === null)
|
|
85
|
+
return true; // no stored hash — comparison bypassed
|
|
86
|
+
const expected = embeddedTextHash(row);
|
|
87
|
+
return typeof expected === "string" && recorded === expected;
|
|
88
|
+
}
|
|
89
|
+
/** Relative element-wise tolerance for {@link holdsOwnStaleVector}. The staged
|
|
90
|
+
* vector is the embedding provider's output as `getEmbedding` returns it; no
|
|
91
|
+
* code here bounds or normalizes its components, so the comparison scales
|
|
92
|
+
* with magnitude: two finite components match when
|
|
93
|
+
* `|stored - staged| <= 1e-6 * max(1, |stored|, |staged|)`.
|
|
94
|
+
* - At magnitude 1 and above the relative term applies. Rounding a
|
|
95
|
+
* normal-range float32 value changes it by at most 2^-24 (about 6e-8) of
|
|
96
|
+
* its magnitude, so a stored copy that went through float32 matches.
|
|
97
|
+
* - Below magnitude 1 the absolute floor of 1e-6 applies, and that floor is
|
|
98
|
+
* what accepts the round trip there: a subnormal float32 value such as
|
|
99
|
+
* 1e-45 rounds with a large relative error (about 40%) but an absolute
|
|
100
|
+
* error far inside 1e-6.
|
|
101
|
+
* - A non-finite component on either side never matches: a value beyond
|
|
102
|
+
* float32 range rounds to Infinity, and NaN equals nothing. */
|
|
103
|
+
const STAGED_VECTOR_RELATIVE_TOLERANCE = 1e-6;
|
|
104
|
+
/** One component of {@link holdsOwnStaleVector}'s comparison: both components
|
|
105
|
+
* finite, and within the tolerance above. */
|
|
106
|
+
function matchesStagedComponent(stored, staged) {
|
|
107
|
+
if (!Number.isFinite(stored) || !Number.isFinite(staged))
|
|
108
|
+
return false;
|
|
109
|
+
return Math.abs(stored - staged) <= STAGED_VECTOR_RELATIVE_TOLERANCE * Math.max(1, Math.abs(stored), Math.abs(staged));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* True when the row's stored vector matches `staged.embedding` element by
|
|
113
|
+
* element within {@link STAGED_VECTOR_RELATIVE_TOLERANCE} while the row's current
|
|
114
|
+
* embedding text (`skillEmbedText`) is not `staged.text`. A match means the row
|
|
115
|
+
* holds a vector equal, within that tolerance, to the one this writer computed
|
|
116
|
+
* for `staged.text`, a text the row no longer has, so the caller re-embeds the
|
|
117
|
+
* current text. It is evidence that the stored vector matches this writer's
|
|
118
|
+
* computation, not proof of which writer stored it, and it assumes nothing
|
|
119
|
+
* about how far apart the vectors of different texts are. The stored
|
|
120
|
+
* `embeddedContentHash` is not consulted: it is null for a row with an empty
|
|
121
|
+
* owner and so would match any text. Returns false when nothing was staged,
|
|
122
|
+
* when the row's text is still `staged.text`, or when the stored vector does
|
|
123
|
+
* not match. A non-match says nothing about the vector's provenance; it only
|
|
124
|
+
* means this staged-vector exception does not apply, and the caller's normal
|
|
125
|
+
* eligibility decides whether the row is written: the embedding backfill writes
|
|
126
|
+
* a missing vector or one of at most 100 entries and skips a longer one; the
|
|
127
|
+
* embedding-stamp migration re-embeds a row whose stamp is not current or whose
|
|
128
|
+
* stored hash does not match its text, whatever vector it holds.
|
|
129
|
+
*/
|
|
130
|
+
export function holdsOwnStaleVector(row, staged) {
|
|
131
|
+
if (!staged || !row || typeof row !== "object")
|
|
132
|
+
return false;
|
|
133
|
+
if (skillEmbedText(row) === staged.text)
|
|
134
|
+
return false;
|
|
135
|
+
const stored = row.embedding;
|
|
136
|
+
if (stored == null || typeof stored.length !== "number" || stored.length !== staged.embedding.length)
|
|
137
|
+
return false;
|
|
138
|
+
for (let i = 0; i < staged.embedding.length; i++) {
|
|
139
|
+
const value = stored[i];
|
|
140
|
+
if (typeof value !== "number" || !matchesStagedComponent(value, staged.embedding[i]))
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function isEmptyEmbedding(value) {
|
|
2
|
+
return (Array.isArray(value) && value.length === 0) ||
|
|
3
|
+
(ArrayBuffer.isView(value) && value.byteLength === 0);
|
|
4
|
+
}
|
|
5
|
+
export function normalizeEmptyEmbedding(row) {
|
|
6
|
+
if (isEmptyEmbedding(row.embedding))
|
|
7
|
+
row.embedding = null;
|
|
8
|
+
}
|
|
@@ -65,6 +65,9 @@ export function buildPointerRow(args) {
|
|
|
65
65
|
function pointerTable() {
|
|
66
66
|
return databases.flair?.[MEMORY_HOST_SOURCE_TABLE];
|
|
67
67
|
}
|
|
68
|
+
export function isMemoryHostSourceStore(store) {
|
|
69
|
+
return store != null && pointerTable()?.primaryStore === store;
|
|
70
|
+
}
|
|
68
71
|
/**
|
|
69
72
|
* flair#1940 A1'' item 6 (adjudication B, hardened in round 5/adjudication 0e)
|
|
70
73
|
* — is an incoming hostSource an ECHO of the STORED pointer? Preservation (do
|
|
@@ -94,8 +97,13 @@ export function isPointerEchoOf(input, storedPointer, writerAuthorId) {
|
|
|
94
97
|
/**
|
|
95
98
|
* Read the pointer rows for a WHOLE result set in ONE batched query (A1'
|
|
96
99
|
* item 4). Empty input issues no query. Returns memoryId -> row.
|
|
100
|
+
*
|
|
101
|
+
* `context` is optional: the reader join omits it. A caller that must observe
|
|
102
|
+
* a delete committed earlier in the SAME request passes its own transaction
|
|
103
|
+
* context, because a contextless read joins the request's ambient transaction
|
|
104
|
+
* and reads that transaction's pinned read snapshot (see storedPointerIds).
|
|
97
105
|
*/
|
|
98
|
-
export async function loadPointerRows(ids) {
|
|
106
|
+
export async function loadPointerRows(ids, context) {
|
|
99
107
|
const wanted = [...new Set(ids.filter((id) => typeof id === "string" && id.length > 0))];
|
|
100
108
|
const map = new Map();
|
|
101
109
|
if (wanted.length === 0)
|
|
@@ -108,18 +116,35 @@ export async function loadPointerRows(ids) {
|
|
|
108
116
|
const query = wanted.length === 1
|
|
109
117
|
? { conditions: [{ attribute: "memoryId", comparator: "equals", value: wanted[0] }] }
|
|
110
118
|
: { operator: "or", conditions: wanted.map((id) => ({ attribute: "memoryId", comparator: "equals", value: id })) };
|
|
111
|
-
const result = table.search(query);
|
|
119
|
+
const result = table.search(query, context);
|
|
112
120
|
for await (const row of result) {
|
|
113
121
|
if (row && typeof row.memoryId === "string")
|
|
114
122
|
map.set(row.memoryId, row);
|
|
115
123
|
}
|
|
116
124
|
return map;
|
|
117
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* The ids among `ids` that still have a stored pointer row: the check POST
|
|
128
|
+
* /MemoryPurge runs after its pointer deletes commit. Unlike loadPointerRows
|
|
129
|
+
* (the reader join, which reads a missing table as "no pointers"), it throws
|
|
130
|
+
* when the table cannot be searched, so a failed check never reads as "no
|
|
131
|
+
* pointer row left".
|
|
132
|
+
*
|
|
133
|
+
* The caller passes a transaction context it owns. A contextless read resolves
|
|
134
|
+
* the request's ambient operation transaction and reads that transaction's
|
|
135
|
+
* read snapshot, which was pinned before the purge's pointer delete committed,
|
|
136
|
+
* so it would report a deleted pointer row as still present.
|
|
137
|
+
*/
|
|
138
|
+
export async function storedPointerIds(ids, context) {
|
|
139
|
+
if (typeof pointerTable()?.search !== "function") {
|
|
140
|
+
throw new Error(`${MEMORY_HOST_SOURCE_TABLE} table unavailable`);
|
|
141
|
+
}
|
|
142
|
+
return new Set((await loadPointerRows(ids, context)).keys());
|
|
143
|
+
}
|
|
118
144
|
/**
|
|
119
145
|
* flair#1940 A1-iv item 2 — the ONE reader helper. `Memory.get()`,
|
|
120
|
-
* `Memory.search()
|
|
121
|
-
* helper.
|
|
122
|
-
* in this slice. It fetches the pointer rows for the WHOLE result set in ONE
|
|
146
|
+
* `Memory.search()`, `SemanticSearch` and bootstrap render pointers through the
|
|
147
|
+
* pointer helper. It fetches the pointer rows for the WHOLE result set in ONE
|
|
123
148
|
* batched query and applies the join, so no reader can forget the join and no
|
|
124
149
|
* module needs to touch the MemoryHostSource table itself. `readerAgentId` is
|
|
125
150
|
* the non-admin reader; an admin/operator read is the named exception (see
|
|
@@ -149,8 +174,8 @@ export async function projectRowsThroughPointers(rows, readerAgentId) {
|
|
|
149
174
|
}
|
|
150
175
|
/** Read ONE stored pointer row (the write path's echo check). Kept here so the
|
|
151
176
|
* MemoryHostSource table is touched ONLY by this module. */
|
|
152
|
-
export async function loadStoredPointer(memoryId) {
|
|
153
|
-
return (await loadPointerRows([memoryId])).get(memoryId) ?? null;
|
|
177
|
+
export async function loadStoredPointer(memoryId, context) {
|
|
178
|
+
return (await loadPointerRows([memoryId], context)).get(memoryId) ?? null;
|
|
154
179
|
}
|
|
155
180
|
/** The Memory attributes that would carry a pointer into an inbound federated
|
|
156
181
|
* row (A1' item 5). A record carrying any of these is refused, not merged. */
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory-id-guard.ts — the resource-layer refusal for a Memory id that ends in
|
|
3
|
+
* the `.content` property suffix (flair#2199).
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
import { MEMORY_CONTENT_SELECTOR_SUFFIX, endsWithContentSelectorSuffix, decodeMemoryIdSegment } from "../src/lib/memory-id-policy.js";
|
|
7
|
+
import { DECLARED_MEMORY_ATTRIBUTES } from "./memory-declared-attributes.js";
|
|
8
|
+
const DECLARED = new Set(DECLARED_MEMORY_ATTRIBUTES);
|
|
9
|
+
/** The named error for a Memory write naming an id that ends in `.content`. */
|
|
10
|
+
export const CONTENT_SUFFIX_ID_ERROR = "memory_id_content_suffix";
|
|
11
|
+
/** The named 400 for one offending id. */
|
|
12
|
+
export function contentSuffixIdDenial(id) {
|
|
13
|
+
return new Response(JSON.stringify({
|
|
14
|
+
error: CONTENT_SUFFIX_ID_ERROR,
|
|
15
|
+
message: `Memory ids must not end in "${MEMORY_CONTENT_SELECTOR_SUFFIX}".`,
|
|
16
|
+
}), { status: 400, headers: { "content-type": "application/json" } });
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Refuse a write when any of its named ids (the URL-bound id and/or a body id)
|
|
20
|
+
* ends in the `.content` suffix.
|
|
21
|
+
*/
|
|
22
|
+
export function refuseContentSuffixId(ids, target) {
|
|
23
|
+
const rawId = typeof target?.pathname === "string"
|
|
24
|
+
? decodeMemoryIdSegment(target.pathname.split("/").at(-1) ?? "")
|
|
25
|
+
: undefined;
|
|
26
|
+
const bad = [...ids, rawId].find((id) => endsWithContentSelectorSuffix(id));
|
|
27
|
+
return bad === undefined ? null : contentSuffixIdDenial(String(bad));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The record id a Memory reference resolves to, the way Harper resolves a
|
|
31
|
+
* by-id path: decode it, then drop a trailing `.<declared attribute>` selector
|
|
32
|
+
* (Harper splits at the FIRST dot and treats the remainder as a property ONLY
|
|
33
|
+
* when it names a declared attribute). A reference that carries no such
|
|
34
|
+
* selector resolves to its decoded form; a non-string resolves to undefined.
|
|
35
|
+
* Memory.ts resolves a `supersedes` reference with this once, before anything
|
|
36
|
+
* reads it, and uses the result for the reserved-id check, the authorization
|
|
37
|
+
* read (non-admin agents only), the stored reference and the close
|
|
38
|
+
* (flair#2307).
|
|
39
|
+
*/
|
|
40
|
+
export function resolveMemoryReferenceId(ref) {
|
|
41
|
+
if (typeof ref !== "string")
|
|
42
|
+
return undefined;
|
|
43
|
+
const decoded = decodeMemoryIdSegment(ref);
|
|
44
|
+
const dot = decoded.indexOf(".");
|
|
45
|
+
if (dot > -1 && DECLARED.has(decoded.slice(dot + 1)))
|
|
46
|
+
return decoded.slice(0, dot);
|
|
47
|
+
return decoded;
|
|
48
|
+
}
|
|
49
|
+
/** The named error for a `supersedes` target whose read failed (flair#2307). */
|
|
50
|
+
export const SUPERSEDES_TARGET_UNREADABLE_ERROR = "supersedes_target_unreadable";
|
|
51
|
+
/** The named error for a `supersedes` target that does not exist (flair#2307). */
|
|
52
|
+
export const SUPERSEDES_TARGET_MISSING_ERROR = "supersedes_target_missing";
|
|
53
|
+
/**
|
|
54
|
+
* Refuse a write whose `supersedes` target could not be read: the predecessor
|
|
55
|
+
* read every write with a `supersedes` makes (prepareSkillBody in
|
|
56
|
+
* resources/skill-version-write.ts), or, for a non-admin agent, the
|
|
57
|
+
* authorization read. A client error from the read (an id Harper cannot look
|
|
58
|
+
* up) answers 400; anything else 503, since a retry can succeed. Nothing has
|
|
59
|
+
* been written when this is returned.
|
|
60
|
+
*/
|
|
61
|
+
export function supersedesTargetUnreadable(err) {
|
|
62
|
+
const code = err?.statusCode;
|
|
63
|
+
const status = typeof code === "number" && code >= 400 && code < 500 ? 400 : 503;
|
|
64
|
+
return new Response(JSON.stringify({
|
|
65
|
+
error: SUPERSEDES_TARGET_UNREADABLE_ERROR,
|
|
66
|
+
message: "the record named by `supersedes` could not be read; nothing was written",
|
|
67
|
+
}), { status, headers: { "content-type": "application/json" } });
|
|
68
|
+
}
|
|
69
|
+
/** Refuse a write whose `supersedes` names a record that does not exist. */
|
|
70
|
+
export function supersedesTargetMissing() {
|
|
71
|
+
return new Response(JSON.stringify({
|
|
72
|
+
error: SUPERSEDES_TARGET_MISSING_ERROR,
|
|
73
|
+
message: "the record named by `supersedes` does not exist; nothing was written",
|
|
74
|
+
}), { status: 409, headers: { "content-type": "application/json" } });
|
|
75
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* memory-redaction.ts — server-side credential redaction for explicit Memory
|
|
3
|
+
* writes (flair#2407).
|
|
4
|
+
*
|
|
5
|
+
* ONE redactor. The automatic-capture path and the action-recall cache call
|
|
6
|
+
* `redactSecrets` (packages/flair-mcp/src/secret-redaction.ts), which delegates
|
|
7
|
+
* to `redactSecretsWithCount`; this module calls `redactSecretsWithCount`
|
|
8
|
+
* directly, so the server keeps no second pattern list.
|
|
9
|
+
*
|
|
10
|
+
* Memory text fields are redacted on write. Skill writes refuse metadata with
|
|
11
|
+
* duplicate top-level keys; otherwise name/description and string tags are
|
|
12
|
+
* redacted (flair#2575).
|
|
13
|
+
*
|
|
14
|
+
* Memory post/put/patch and the feed ingest (resources/MemoryFeed.ts) call
|
|
15
|
+
* redactMemoryWrite() on the write body before the row is persisted; post and
|
|
16
|
+
* put call it before the dedup gate and the stored embedding are computed.
|
|
17
|
+
* post, put and the feed ingest call redactSkillWrite() on the EFFECTIVE row
|
|
18
|
+
* after skill-tag inheritance (prepareSkillBody) and before persistence, so an
|
|
19
|
+
* omitted-tag PUT on a stored skill, a superseding POST and a feed ingest
|
|
20
|
+
* redact the skill fields of the row they write.
|
|
21
|
+
* When the count is above zero, a Memory post/put/patch response also carries
|
|
22
|
+
* `stored`: the stored (post-redaction) values of the fields that write's
|
|
23
|
+
* request supplied (flair#2561) — the REDACTED_MEMORY_FIELDS it supplied as
|
|
24
|
+
* strings (suppliedMemoryTextFields) and, for a skill-tagged row, the skill
|
|
25
|
+
* fields it supplied (suppliedSkillTextFields, flair#2575). The supplied
|
|
26
|
+
* fields are read from the body as the request sent it, before a stored row or
|
|
27
|
+
* a skill predecessor is merged into it (suppliedStoredTextFields,
|
|
28
|
+
* storedMemoryText). The feed ingest's response holds, of the row it stored or
|
|
29
|
+
* matched, only named primitive fields, plus `redactedValues` when present
|
|
30
|
+
* (resources/memory-write-response.ts).
|
|
31
|
+
* A skill PUT whose redaction replaced a value always writes its redacted
|
|
32
|
+
* effective row, so each field `stored` reports is what the row holds: the
|
|
33
|
+
* in-place (reserved seed) PUT's unchanged-row check never skips it.
|
|
34
|
+
* put() skips the call for the shipped-skill seed write (isOperatorSeedPut): a
|
|
35
|
+
* PUT of the reserved seed skill id by a verified Basic administrator
|
|
36
|
+
* (src/lib/skill-seed.ts). The seed compares the stored text to its source
|
|
37
|
+
* text, so it must round-trip unchanged. The call does not otherwise depend on
|
|
38
|
+
* the caller: an admin Ed25519 key or an OAuth principal is an agent
|
|
39
|
+
* (resources/skill-write-policy.ts) and is redacted. put()'s `_reindex` re-PUT
|
|
40
|
+
* returns before the call; it refuses a change to these fields.
|
|
41
|
+
*
|
|
42
|
+
* The FederationSync receive path does not call this module: the redactor
|
|
43
|
+
* leaves incoming federated content alone. Federation checks a record's
|
|
44
|
+
* signature, when it carries one, on the incoming record before it adds
|
|
45
|
+
* receiver bookkeeping to the row.
|
|
46
|
+
*/
|
|
47
|
+
import { redactSecretsWithCount } from "../packages/flair-mcp/src/secret-redaction.js";
|
|
48
|
+
import { isReservedSeedId } from "./seed-reservation.js";
|
|
49
|
+
import { isSkillWrite, MEMORY_EMBEDDING_INPUT_FIELDS } from "./skill-write.js";
|
|
50
|
+
import { parseSkillMetadata } from "./skill-provenance.js";
|
|
51
|
+
import { skillWriteSource } from "./skill-write-policy.js";
|
|
52
|
+
/** The Memory text fields redacted on a write. */
|
|
53
|
+
export const REDACTED_MEMORY_FIELDS = Object.freeze(["content", "summary", "trigger"]);
|
|
54
|
+
/** Metadata fields selected after duplicate-key validation. */
|
|
55
|
+
export const REDACTED_SKILL_FIELDS = Object.freeze(["name", "description"]);
|
|
56
|
+
/**
|
|
57
|
+
* flair#2561: the string-valued text fields a write request supplies: each of
|
|
58
|
+
* the REDACTED_MEMORY_FIELDS that holds a string in the request body. A field
|
|
59
|
+
* the body sends as null or as a non-string is not in the list. A write reads
|
|
60
|
+
* them from the body as the request sent it, before a stored row or a skill
|
|
61
|
+
* predecessor is merged into that body, so the list names only text fields the
|
|
62
|
+
* request itself carried.
|
|
63
|
+
*/
|
|
64
|
+
export function suppliedMemoryTextFields(body) {
|
|
65
|
+
if (!body || typeof body !== "object" || Array.isArray(body))
|
|
66
|
+
return [];
|
|
67
|
+
return REDACTED_MEMORY_FIELDS.filter((field) => typeof body[field] === "string");
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* flair#2561, flair#2575: the skill fields a write request supplies: `name` and
|
|
71
|
+
* `description` when the body's own `metadata` holds a string under that key,
|
|
72
|
+
* and `tags` when the body carries a `tags` array. Read, like
|
|
73
|
+
* suppliedMemoryTextFields, from the body as the request sent it, before a
|
|
74
|
+
* stored row or a skill predecessor is merged into it, so a field the row
|
|
75
|
+
* inherits (an omitted `tags` array, or a predecessor's `metadata`) is not in
|
|
76
|
+
* the list.
|
|
77
|
+
*/
|
|
78
|
+
export function suppliedSkillTextFields(body) {
|
|
79
|
+
if (!body || typeof body !== "object" || Array.isArray(body))
|
|
80
|
+
return [];
|
|
81
|
+
const record = body;
|
|
82
|
+
const meta = parseSkillMetadata(record.metadata);
|
|
83
|
+
const supplied = REDACTED_SKILL_FIELDS.filter((field) => typeof meta[field] === "string");
|
|
84
|
+
if (Array.isArray(record.tags))
|
|
85
|
+
supplied.push("tags");
|
|
86
|
+
return supplied;
|
|
87
|
+
}
|
|
88
|
+
/** The fields a Memory POST or PUT request supplies that its `stored` can
|
|
89
|
+
* report: suppliedMemoryTextFields plus suppliedSkillTextFields. */
|
|
90
|
+
export function suppliedStoredTextFields(body) {
|
|
91
|
+
return [...suppliedMemoryTextFields(body), ...suppliedSkillTextFields(body)];
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* flair#2467, flair#2561, flair#2575: the values on `row` of the `supplied`
|
|
95
|
+
* fields, copied; a supplied field that holds no value on `row` is left out. A
|
|
96
|
+
* write calls it on the row it stages, after redaction, with the fields its own
|
|
97
|
+
* request supplied (suppliedMemoryTextFields, suppliedStoredTextFields), so the
|
|
98
|
+
* copy holds the stored (post-redaction) value of each of those fields, changed
|
|
99
|
+
* or not, and no other field of the row: a field the row holds from a merged
|
|
100
|
+
* stored row or a skill predecessor is not in it. For a skill-tagged row the
|
|
101
|
+
* supplied skill fields are the `metadata` `name`/`description` (parsed out of
|
|
102
|
+
* the row) and the accepted `tags` entries. When the write replaced any value,
|
|
103
|
+
* its response carries the copy as `stored` next to `redactedValues`; the
|
|
104
|
+
* writer never has to read the row back, so a later write cannot change what
|
|
105
|
+
* this one reports.
|
|
106
|
+
*/
|
|
107
|
+
export function storedMemoryText(row, supplied) {
|
|
108
|
+
const stored = {};
|
|
109
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
110
|
+
return stored;
|
|
111
|
+
const record = row;
|
|
112
|
+
for (const field of REDACTED_MEMORY_FIELDS) {
|
|
113
|
+
if (!supplied.includes(field))
|
|
114
|
+
continue;
|
|
115
|
+
const value = record[field];
|
|
116
|
+
if (typeof value === "string")
|
|
117
|
+
stored[field] = value;
|
|
118
|
+
}
|
|
119
|
+
if (isSkillWrite(record)) {
|
|
120
|
+
const meta = parseSkillMetadata(record.metadata);
|
|
121
|
+
for (const field of REDACTED_SKILL_FIELDS) {
|
|
122
|
+
if (supplied.includes(field) && typeof meta[field] === "string")
|
|
123
|
+
stored[field] = meta[field];
|
|
124
|
+
}
|
|
125
|
+
// Report the entries the schema accepts (`[String]`: a string or null), so
|
|
126
|
+
// `stored.tags` is what the row carries, not only its string entries.
|
|
127
|
+
if (supplied.includes("tags") && Array.isArray(record.tags)) {
|
|
128
|
+
stored.tags = record.tags.filter((tag) => typeof tag === "string" || tag === null);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return stored;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Whether a Memory PUT is the shipped-skill seed write: the caller is a
|
|
135
|
+
* verified Basic administrator (skillWriteSource "operator") and every id the
|
|
136
|
+
* write names (the URL id and any body id) is the reserved seed skill id.
|
|
137
|
+
*/
|
|
138
|
+
export function isOperatorSeedPut(context, auth, ids) {
|
|
139
|
+
const named = ids.filter((id) => id !== undefined && id !== null);
|
|
140
|
+
if (named.length === 0 || !named.every((id) => isReservedSeedId("Memory", id)))
|
|
141
|
+
return false;
|
|
142
|
+
return skillWriteSource(context, auth) === "operator";
|
|
143
|
+
}
|
|
144
|
+
/** The redacted fields an embedding can be computed from, across row kinds:
|
|
145
|
+
* `trigger` for a skill-tagged row with a non-empty trigger, `content` for every
|
|
146
|
+
* other row (MEMORY_EMBEDDING_INPUT_FIELDS, next to embedTextField/skillEmbedText
|
|
147
|
+
* in resources/skill-write.ts). Redaction sees the write body, not the stored
|
|
148
|
+
* row's kind, so it uses the whole set. */
|
|
149
|
+
const EMBEDDING_SOURCE_FIELDS = new Set(MEMORY_EMBEDDING_INPUT_FIELDS);
|
|
150
|
+
/**
|
|
151
|
+
* Redact REDACTED_MEMORY_FIELDS of a write body IN PLACE. When the redaction
|
|
152
|
+
* changes `content` or `trigger`, the fields an embedding is computed from, a
|
|
153
|
+
* caller-supplied `embedding` and `embeddingModel` are removed from the body,
|
|
154
|
+
* so the supplied vector is not stored. For a non-object input it changes
|
|
155
|
+
* nothing and returns `{ count: 0, embeddingDiscarded: false }`.
|
|
156
|
+
*/
|
|
157
|
+
export function redactMemoryWrite(content) {
|
|
158
|
+
const none = { count: 0, embeddingDiscarded: false };
|
|
159
|
+
if (!content || typeof content !== "object" || Array.isArray(content))
|
|
160
|
+
return none;
|
|
161
|
+
const row = content;
|
|
162
|
+
let count = 0;
|
|
163
|
+
let embedSourceChanged = false;
|
|
164
|
+
for (const field of REDACTED_MEMORY_FIELDS) {
|
|
165
|
+
const value = row[field];
|
|
166
|
+
if (typeof value !== "string")
|
|
167
|
+
continue;
|
|
168
|
+
const redacted = redactSecretsWithCount(value);
|
|
169
|
+
if (redacted.text !== value) {
|
|
170
|
+
row[field] = redacted.text;
|
|
171
|
+
count += redacted.count;
|
|
172
|
+
if (EMBEDDING_SOURCE_FIELDS.has(field))
|
|
173
|
+
embedSourceChanged = true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (count === 0)
|
|
177
|
+
return none;
|
|
178
|
+
const supplied = row.embedding != null || row.embeddingModel != null;
|
|
179
|
+
if (!supplied || !embedSourceChanged)
|
|
180
|
+
return { count, embeddingDiscarded: false };
|
|
181
|
+
delete row.embedding;
|
|
182
|
+
delete row.embeddingModel;
|
|
183
|
+
return { count, embeddingDiscarded: true };
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Redact name/description and string tags after prepareSkillBody has refused
|
|
187
|
+
* metadata with duplicate top-level keys and resolved the effective skill tag.
|
|
188
|
+
* Returns the number of values replaced.
|
|
189
|
+
*/
|
|
190
|
+
export function redactSkillWrite(row) {
|
|
191
|
+
if (!row || typeof row !== "object" || Array.isArray(row))
|
|
192
|
+
return 0;
|
|
193
|
+
const record = row;
|
|
194
|
+
return isSkillWrite(record) ? redactSkillFields(record) : 0;
|
|
195
|
+
}
|
|
196
|
+
/** Redact validated skill fields in place. */
|
|
197
|
+
function redactSkillFields(row) {
|
|
198
|
+
let count = 0;
|
|
199
|
+
if (Array.isArray(row.tags)) {
|
|
200
|
+
row.tags = row.tags.map((entry) => {
|
|
201
|
+
if (typeof entry !== "string")
|
|
202
|
+
return entry;
|
|
203
|
+
const redacted = redactSecretsWithCount(entry);
|
|
204
|
+
if (redacted.text === entry)
|
|
205
|
+
return entry;
|
|
206
|
+
count += redacted.count;
|
|
207
|
+
return redacted.text;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
count += redactSkillMetadata(row);
|
|
211
|
+
return count;
|
|
212
|
+
}
|
|
213
|
+
/** Serialize validated metadata only when name or description changes. */
|
|
214
|
+
function redactSkillMetadata(row) {
|
|
215
|
+
const original = row.metadata;
|
|
216
|
+
const asString = typeof original === "string";
|
|
217
|
+
const asObject = original !== null && typeof original === "object" && !Array.isArray(original);
|
|
218
|
+
if (!asString && !asObject)
|
|
219
|
+
return 0;
|
|
220
|
+
const parsed = parseSkillMetadata(original);
|
|
221
|
+
let count = 0;
|
|
222
|
+
for (const field of REDACTED_SKILL_FIELDS) {
|
|
223
|
+
const value = parsed[field];
|
|
224
|
+
if (typeof value !== "string")
|
|
225
|
+
continue;
|
|
226
|
+
const redacted = redactSecretsWithCount(value);
|
|
227
|
+
if (redacted.text !== value) {
|
|
228
|
+
parsed[field] = redacted.text;
|
|
229
|
+
count += redacted.count;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (count === 0)
|
|
233
|
+
return 0;
|
|
234
|
+
row.metadata = asString ? JSON.stringify(parsed) : parsed;
|
|
235
|
+
return count;
|
|
236
|
+
}
|
|
@@ -168,7 +168,7 @@ export function resolveCandidateVisibilityRuling(candidate) {
|
|
|
168
168
|
//
|
|
169
169
|
// Passed as `responseFormat: { schema: CANDIDATES_SCHEMA }` to models.generate()
|
|
170
170
|
// so backends that honor structured output (Ollama, OpenAI — verified against
|
|
171
|
-
//
|
|
171
|
+
// harper 5.1.17's bundled backends) return conformant
|
|
172
172
|
// JSON directly. Not every backend enforces it (Anthropic's Messages API has
|
|
173
173
|
// no equivalent and Harper documents that it silently ignores the option) —
|
|
174
174
|
// this module never trusts the backend to have enforced the schema; every
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Non-admin Memory writes discard caller-supplied vectors and model stamps.
|
|
3
|
+
* A vector or stamp counts as caller-supplied only when it differs from the
|
|
4
|
+
* stored row's value; one re-sent unchanged is carried and kept as stored.
|
|
5
|
+
* Embedding is computed when the resulting write needs one.
|
|
6
|
+
* Administrator and trusted in-process writes and federation-sync records are unchanged.
|
|
7
|
+
* Empty-vector normalization and the re-embed request protocol are unchanged;
|
|
8
|
+
* an ordinary non-admin write strips a supplied stamp even alongside a null or empty embedding.
|
|
9
|
+
*/
|
|
10
|
+
import { isDeepStrictEqual } from "node:util";
|
|
11
|
+
/** Presence check after empty-embedding normalization, not vector validation. */
|
|
12
|
+
export function hasSuppliedEmbedding(value) {
|
|
13
|
+
return value !== null && value !== undefined;
|
|
14
|
+
}
|
|
15
|
+
/** True when the caller's object body supplies an `embeddingModel` stamp (any
|
|
16
|
+
* value, `null` included). Captured before a stored row is merged in, so a
|
|
17
|
+
* stamp carried from a stored row is never mistaken for one the caller set. */
|
|
18
|
+
export function suppliesEmbeddingModel(content) {
|
|
19
|
+
return !!content && typeof content === "object" && !Array.isArray(content) &&
|
|
20
|
+
Object.prototype.hasOwnProperty.call(content, "embeddingModel");
|
|
21
|
+
}
|
|
22
|
+
function sameVector(a, b) {
|
|
23
|
+
const plain = (v) => (ArrayBuffer.isView(v) ? Array.from(v) : v);
|
|
24
|
+
return isDeepStrictEqual(plain(a), plain(b));
|
|
25
|
+
}
|
|
26
|
+
/** What the caller's own body supplies: a non-null `embedding` and/or an
|
|
27
|
+
* `embeddingModel` that DIFFERS from the stored row's. With no stored row
|
|
28
|
+
* (a create) everything present is supplied. Captured before a stored row is
|
|
29
|
+
* merged into the body. */
|
|
30
|
+
export function suppliedVectorFlags(content, stored) {
|
|
31
|
+
const body = content && typeof content === "object" && !Array.isArray(content) ? content : null;
|
|
32
|
+
const vector = hasSuppliedEmbedding(body?.embedding) && !(stored && sameVector(body.embedding, stored.embedding));
|
|
33
|
+
const stamp = suppliesEmbeddingModel(content) && !(stored && isDeepStrictEqual(body.embeddingModel, stored.embeddingModel));
|
|
34
|
+
return { vector, stamp };
|
|
35
|
+
}
|
|
36
|
+
/** True for actors exempt from discarding caller-supplied vectors:
|
|
37
|
+
* administrators and trusted in-process callers. A stored vector carried by
|
|
38
|
+
* a non-admin skill update can remain. */
|
|
39
|
+
export function keepsSuppliedVector(auth) {
|
|
40
|
+
return auth.kind === "internal" || (auth.kind === "agent" && auth.isAdmin);
|
|
41
|
+
}
|
|
42
|
+
/** Discard a non-admin write's caller-supplied vector and/or model stamp, in
|
|
43
|
+
* place. `supplied` comes from `suppliedVectorFlags`: what the CALLER's body
|
|
44
|
+
* held that differs from the stored row, before any stored row was merged in,
|
|
45
|
+
* so a vector prepareSkillBody carried from a stored row is left alone. A
|
|
46
|
+
* carried vector equal to `stored`'s keeps the stored stamp. A non-object or
|
|
47
|
+
* array body (no single record) is left untouched. Returns true when a field
|
|
48
|
+
* was removed. */
|
|
49
|
+
export function discardSuppliedVector(content, auth, supplied, stored) {
|
|
50
|
+
if (!content || typeof content !== "object" || Array.isArray(content))
|
|
51
|
+
return false;
|
|
52
|
+
if (keepsSuppliedVector(auth))
|
|
53
|
+
return false;
|
|
54
|
+
const carriedVector = !supplied.vector && hasSuppliedEmbedding(content.embedding);
|
|
55
|
+
let discarded = false;
|
|
56
|
+
if (supplied.vector && hasSuppliedEmbedding(content.embedding)) {
|
|
57
|
+
discarded = Object.prototype.hasOwnProperty.call(content, "embedding");
|
|
58
|
+
delete content.embedding;
|
|
59
|
+
}
|
|
60
|
+
if ((supplied.vector || supplied.stamp) && !carriedVector) {
|
|
61
|
+
discarded = Object.prototype.hasOwnProperty.call(content, "embeddingModel") || discarded;
|
|
62
|
+
delete content.embeddingModel;
|
|
63
|
+
}
|
|
64
|
+
else if (supplied.stamp && stored && sameVector(content.embedding, stored.embedding)) {
|
|
65
|
+
if (Object.prototype.hasOwnProperty.call(stored, "embeddingModel"))
|
|
66
|
+
content.embeddingModel = stored.embeddingModel;
|
|
67
|
+
else
|
|
68
|
+
delete content.embeddingModel;
|
|
69
|
+
discarded = true;
|
|
70
|
+
}
|
|
71
|
+
return discarded;
|
|
72
|
+
}
|