@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
package/dist/resources/Agent.js
CHANGED
|
@@ -1,8 +1,84 @@
|
|
|
1
1
|
import { databases } from "harper";
|
|
2
|
-
import { resolveAgentAuth, allowVerified, allowAdmin
|
|
2
|
+
import { resolveAgentAuth, allowVerified, allowAdmin } from "./agent-auth.js";
|
|
3
3
|
import { agentRecordIsAdmin, reconcileAdminFields } from "./agent-admin.js";
|
|
4
4
|
import { admitPrincipalWrite, statusWriteRefusal } from "./agent-status-guard.js";
|
|
5
|
-
import { applyOriginatorInstanceId, resolveStoredRow, stampOriginatorOnCreate } from "./originator-instance.js";
|
|
5
|
+
import { applyFederationBookkeeping, applyOriginatorInstanceId, dropClientFederationBookkeeping, resolveStoredRow, stampOriginatorOnCreate } from "./originator-instance.js";
|
|
6
|
+
import { currentInstanceId, localInstanceId } from "./instance-identity.js";
|
|
7
|
+
import { AGENT_ID_ERROR, invalidAgentIdMessage, isValidAgentId } from "../src/lib/agent-id-rule.js";
|
|
8
|
+
import { AGENT_HOME_STAMP_FIELD, AGENT_PRINCIPAL_WRITE_BACK_FIELD, AGENT_PRINCIPAL_WRITE_BACK_NO_ROW_ERROR, isForeignAgentHome, isHomeLessAgentRow, isSyncOriginatedAgentRow, pickPrincipalWriteBackFields } from "../src/lib/agent-home-fields.js";
|
|
9
|
+
import { withOwnedTransaction } from "./request-transaction.js";
|
|
10
|
+
import { txnPausePoint } from "./txn-pause-point.js";
|
|
11
|
+
/** The resource's URL-bound target id (`getId()`), or null when it has none
|
|
12
|
+
* (an in-process call on a collection resource) or the id is not a scalar. */
|
|
13
|
+
function urlBoundId(resource) {
|
|
14
|
+
try {
|
|
15
|
+
const getId = resource?.getId;
|
|
16
|
+
const id = typeof getId === "function" ? getId.call(resource) : undefined;
|
|
17
|
+
if (typeof id === "string" || typeof id === "number")
|
|
18
|
+
return id;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
/* getId() threw — no URL-bound target */
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The id this write lands on: the URL-bound target (`getId()`) when there is
|
|
27
|
+
* one, otherwise the body `id` (an in-process call, where the body id IS the
|
|
28
|
+
* write key). `present` is true only when an id was SUPPLIED — a body carrying
|
|
29
|
+
* an `id` key whose value is `null` is present, while a body with no `id` key
|
|
30
|
+
* is absent. A non-scalar target is ignored.
|
|
31
|
+
*/
|
|
32
|
+
function writeTargetId(resource, content) {
|
|
33
|
+
const bound = urlBoundId(resource);
|
|
34
|
+
if (bound !== null)
|
|
35
|
+
return { present: true, value: bound };
|
|
36
|
+
if (content != null && typeof content === "object" && "id" in content && content.id !== undefined) {
|
|
37
|
+
return { present: true, value: content.id };
|
|
38
|
+
}
|
|
39
|
+
return { present: false, value: undefined };
|
|
40
|
+
}
|
|
41
|
+
/** A 400 refusing a SUPPLIED agent id outside the shared rule, before anything
|
|
42
|
+
* is written. An ABSENT id is allowed: a collection POST with no `id` key lets
|
|
43
|
+
* Harper generate one (a UUID, which matches the rule), and a PUT/PATCH with
|
|
44
|
+
* neither a URL target nor a body id has no id to validate. A body that
|
|
45
|
+
* supplies `id: null` is present and refused — null is not an id. */
|
|
46
|
+
function agentIdDenial(target) {
|
|
47
|
+
if (!target.present)
|
|
48
|
+
return null;
|
|
49
|
+
if (isValidAgentId(target.value))
|
|
50
|
+
return null;
|
|
51
|
+
return new Response(JSON.stringify({ error: AGENT_ID_ERROR, message: invalidAgentIdMessage(target.value) }), { status: 400, headers: { "content-type": "application/json" } });
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Thrown INSIDE the guarded home stamp's owned transaction to ABORT it when the
|
|
55
|
+
* confirmation read finds the committed row no longer eligible (it gained a home
|
|
56
|
+
* or provenance) or gone. Aborting discards the staged write; the caller maps the
|
|
57
|
+
* reason to the response. See Agent.applyGuardedHomeStamp (flair#2472).
|
|
58
|
+
*/
|
|
59
|
+
class AgentHomeStampAborted extends Error {
|
|
60
|
+
reason;
|
|
61
|
+
constructor(reason) {
|
|
62
|
+
super(`agent home stamp aborted: ${reason}`);
|
|
63
|
+
this.reason = reason;
|
|
64
|
+
this.name = "AgentHomeStampAborted";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Thrown INSIDE the guarded principal write-back's owned transaction to ABORT it
|
|
69
|
+
* when the confirmation read finds the committed row no longer eligible: its home
|
|
70
|
+
* differs from the resolved local ID at that read, or the row is gone. Aborting
|
|
71
|
+
* discards the staged write; the caller maps the reason to the response. See
|
|
72
|
+
* Agent.applyGuardedPrincipalWriteBack (flair#2473).
|
|
73
|
+
*/
|
|
74
|
+
class PrincipalWriteBackAborted extends Error {
|
|
75
|
+
reason;
|
|
76
|
+
constructor(reason) {
|
|
77
|
+
super(`agent principal write-back aborted: ${reason}`);
|
|
78
|
+
this.reason = reason;
|
|
79
|
+
this.name = "PrincipalWriteBackAborted";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
6
82
|
/**
|
|
7
83
|
* Agent resource — serves as the Principal table in 1.0.
|
|
8
84
|
*
|
|
@@ -21,14 +97,21 @@ import { applyOriginatorInstanceId, resolveStoredRow, stampOriginatorOnCreate }
|
|
|
21
97
|
*/
|
|
22
98
|
export class Agent extends databases.flair.Agent {
|
|
23
99
|
// Self-authorize now that the global gate is non-rejecting. Verified agents read
|
|
24
|
-
// the principal table for discovery
|
|
25
|
-
//
|
|
26
|
-
//
|
|
100
|
+
// the principal table for discovery. An agent that is not an administrator
|
|
101
|
+
// updates only its OWN record, with PATCH (the patch handler enforces
|
|
102
|
+
// ownership); an Agent PUT is an administrator operation (put() refuses any
|
|
103
|
+
// other caller). Creating/deleting principals is admin-only (flair_agent
|
|
104
|
+
// grant: insert=false, delete=false). Anonymous denied throughout.
|
|
27
105
|
allowRead() { return allowVerified(this.getContext?.()); }
|
|
28
106
|
allowCreate() { return allowAdmin(this.getContext?.()); }
|
|
29
107
|
allowUpdate() { return allowVerified(this.getContext?.()); }
|
|
30
108
|
allowDelete() { return allowAdmin(this.getContext?.()); }
|
|
31
109
|
async post(content, context) {
|
|
110
|
+
// flair#2359 — the ONE agent-ID rule, before any field is defaulted or
|
|
111
|
+
// written. A create must never store an id AgentSeed would refuse.
|
|
112
|
+
const idDenial = agentIdDenial(writeTargetId(this, content));
|
|
113
|
+
if (idDenial)
|
|
114
|
+
return idDenial;
|
|
32
115
|
const now = new Date().toISOString();
|
|
33
116
|
// Backward compat: set type for legacy code
|
|
34
117
|
content.type ||= "agent";
|
|
@@ -57,6 +140,7 @@ export class Agent extends databases.flair.Agent {
|
|
|
57
140
|
// contract (create/update rule; the federation merge path is the raw
|
|
58
141
|
// table writer and never consults a body).
|
|
59
142
|
await stampOriginatorOnCreate(content);
|
|
143
|
+
dropClientFederationBookkeeping(content);
|
|
60
144
|
return super.post(content, context);
|
|
61
145
|
}
|
|
62
146
|
/**
|
|
@@ -65,6 +149,9 @@ export class Agent extends databases.flair.Agent {
|
|
|
65
149
|
* It lives in one place because it previously lived only in put(), and PATCH
|
|
66
150
|
* does not route through put() — so every rule written here was enforced on
|
|
67
151
|
* one verb and not the other. Returns a Response to send, or null to proceed.
|
|
152
|
+
* put() refuses a caller that is not an administrator before calling this
|
|
153
|
+
* (an Agent PUT is an administrator operation), so the per-record rules
|
|
154
|
+
* below decide a non-admin PATCH.
|
|
68
155
|
*
|
|
69
156
|
* Three rules:
|
|
70
157
|
* 1. Only an admin principal may modify a principal OTHER than itself.
|
|
@@ -114,15 +201,19 @@ export class Agent extends databases.flair.Agent {
|
|
|
114
201
|
status: 403, headers: { "content-type": "application/json" },
|
|
115
202
|
});
|
|
116
203
|
}
|
|
117
|
-
// 2. Only admin principals can change admin status.
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
//
|
|
204
|
+
// 2. Only admin principals can change admin status. Both sides of the
|
|
205
|
+
// comparison read the AUTHORITY, `role` (resources/agent-admin.ts), never
|
|
206
|
+
// the `admin` mirror: the role of the row read above, against the role
|
|
207
|
+
// that row would have once the body is merged onto it and reconciled
|
|
208
|
+
// (reconcileAdminFields turns `admin: true` into `role: "admin"`), which is
|
|
209
|
+
// how patch() reconciles. An ordinary self-update that names neither field
|
|
210
|
+
// is unaffected, and a write whose reconciled role keeps the read row's
|
|
211
|
+
// authority is admitted. For a change an administrator commits after this
|
|
212
|
+
// read, see patch().
|
|
121
213
|
const touchesPrivilegeFields = content != null && typeof content === "object" && ("role" in content || "admin" in content);
|
|
122
214
|
if (touchesPrivilegeFields) {
|
|
123
|
-
const
|
|
124
|
-
const wouldBeAdmin = agentRecordIsAdmin(
|
|
125
|
-
const isAdminNow = agentRecordIsAdmin(existing) || (existing?.admin === true);
|
|
215
|
+
const isAdminNow = agentRecordIsAdmin(existing);
|
|
216
|
+
const wouldBeAdmin = agentRecordIsAdmin(reconcileAdminFields({ ...(existing ?? {}), ...content }));
|
|
126
217
|
if (wouldBeAdmin !== isAdminNow) {
|
|
127
218
|
return new Response(JSON.stringify({ error: "only admin principals can change a principal's admin status" }), {
|
|
128
219
|
status: 403, headers: { "content-type": "application/json" },
|
|
@@ -132,30 +223,116 @@ export class Agent extends databases.flair.Agent {
|
|
|
132
223
|
return null;
|
|
133
224
|
}
|
|
134
225
|
async put(content) {
|
|
226
|
+
// flair#2359 — refuse an id outside the shared rule before any read or
|
|
227
|
+
// write.
|
|
228
|
+
const idDenial = agentIdDenial(writeTargetId(this, content));
|
|
229
|
+
if (idDenial)
|
|
230
|
+
return idDenial;
|
|
231
|
+
// An Agent PUT is an administrator operation (flair#2696). It is a full
|
|
232
|
+
// replacement. The server sets these fields, whatever the body says: the
|
|
233
|
+
// id is the URL's target (a trusted in-process call with no URL target uses
|
|
234
|
+
// its body id); createdAt, publicKey, originatorInstanceId and
|
|
235
|
+
// the receiver federation fields (_originatorInstanceId, _syncedFrom,
|
|
236
|
+
// _syncedAt) are carried from the stored row; updatedAt is set here; and
|
|
237
|
+
// role and admin are reconciled (an admin role or `admin: true` sets both,
|
|
238
|
+
// otherwise admin is false). Every other field comes from the body, so an
|
|
239
|
+
// ordinary field the body omits (`status` and `role` included) is not kept.
|
|
240
|
+
// An agent that is not an administrator updates its own record with PATCH,
|
|
241
|
+
// which merges the body into the stored row. Only an administrator or a
|
|
242
|
+
// trusted internal call goes past this point; anonymous and unrecognized
|
|
243
|
+
// verdicts are refused (by Harper's allowUpdate()/allowCreate() checks or
|
|
244
|
+
// by authorizePrincipalWrite). "Administrator" is the caller verdict
|
|
245
|
+
// authorizePrincipalWrite uses too: for a signed request, the decision the
|
|
246
|
+
// request gate took from the Agent row it verified the signature against
|
|
247
|
+
// (agentIsAdmin in resources/agent-auth.ts: role "admin" on that row, or an
|
|
248
|
+
// id FLAIR_ADMIN_AGENTS names).
|
|
249
|
+
if (admitPrincipalWrite(await resolveAgentAuth(this.getContext?.())) === "non-admin") {
|
|
250
|
+
return new Response(JSON.stringify({
|
|
251
|
+
error: "agent_put_admin_only",
|
|
252
|
+
message: "an Agent PUT is an administrator operation; to update your own agent record, use PATCH",
|
|
253
|
+
}), { status: 403, headers: { "content-type": "application/json" } });
|
|
254
|
+
}
|
|
135
255
|
const denial = await this.authorizePrincipalWrite(content);
|
|
136
256
|
if (denial)
|
|
137
257
|
return denial;
|
|
138
|
-
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
// UPDATE keeps the stored value (a body value never replaces or clears it).
|
|
150
|
-
// The row is resolved by the URL-BOUND target id, never a body `id` (Harper
|
|
151
|
-
// writes to the URL target); a mismatch or a failed read refuses the write.
|
|
258
|
+
// A PUT updates an EXISTING row (flair#2696). Harper's put is an upsert; an
|
|
259
|
+
// Agent PUT does not create one. 404 is the repo's "not found / use the
|
|
260
|
+
// other route" status (resources/table-patch-policy.ts, resources/Memory.ts's
|
|
261
|
+
// reindex_row_not_found), and the message names POST /AgentSeed, a creation
|
|
262
|
+
// path.
|
|
263
|
+
const requiresExisting = () => new Response(JSON.stringify({
|
|
264
|
+
error: "agent_put_requires_existing",
|
|
265
|
+
message: "an Agent PUT updates an existing agent; to create an agent, use POST /AgentSeed",
|
|
266
|
+
}), { status: 404, headers: { "content-type": "application/json" } });
|
|
267
|
+
// The stored row, resolved by the write's target id; a body/URL mismatch or
|
|
268
|
+
// a failed read refuses the write, and a missing row is refused.
|
|
152
269
|
const resolvedOriginRow = await resolveStoredRow(this, "Agent", content, () => super.get());
|
|
153
270
|
if (resolvedOriginRow.denial)
|
|
154
271
|
return resolvedOriginRow.denial;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
272
|
+
if (!resolvedOriginRow.row)
|
|
273
|
+
return requiresExisting();
|
|
274
|
+
// Test-only: inert unless the fault-injection env opt-in is set and armed.
|
|
275
|
+
// Holds the PUT after the stored-row read above, before the owned
|
|
276
|
+
// transaction below, so a deletion or a key rotation can commit in between
|
|
277
|
+
// (test/integration/agent-put-race-2696.test.ts).
|
|
278
|
+
const prePause = txnPausePoint("agent-put-pre");
|
|
279
|
+
if (prePause)
|
|
280
|
+
await prePause;
|
|
281
|
+
// The replacement is built and written inside a FRESH OWNED transaction
|
|
282
|
+
// (withOwnedTransaction detaches the request's), from a re-read of the row
|
|
283
|
+
// in that transaction, never from the read above, which a deletion or a key
|
|
284
|
+
// rotation may have overtaken. Harper stamps a transaction's writes with one
|
|
285
|
+
// time, taken at its first read (here, the re-read), and applies the writes
|
|
286
|
+
// to a row in the order of those times:
|
|
287
|
+
// - a deletion or rotation committed before the re-read is SEEN by it: a
|
|
288
|
+
// row the re-read finds absent is refused (the PUT never re-creates it),
|
|
289
|
+
// and the immutable fields are taken from the re-read row
|
|
290
|
+
// (test/integration/agent-put-race-2696.test.ts commits both here);
|
|
291
|
+
// - one whose own transaction takes its time after the re-read carries a
|
|
292
|
+
// later time, so Harper applies it after this write (a deletion stays
|
|
293
|
+
// deleted, a rotated key stands).
|
|
294
|
+
// Not covered: a write whose own transaction took its time before the
|
|
295
|
+
// re-read and commits after it. There is no compare-and-set; per-id
|
|
296
|
+
// serialization is tracked in flair#2478.
|
|
297
|
+
const outcome = await withOwnedTransaction(this.getContext?.(), async (c) => {
|
|
298
|
+
const current = await resolveStoredRow(this, "Agent", content, () => super.get(), c);
|
|
299
|
+
if (current.denial)
|
|
300
|
+
return { kind: "denied", denial: current.denial };
|
|
301
|
+
const stored = current.row;
|
|
302
|
+
if (!stored)
|
|
303
|
+
return { kind: "gone" };
|
|
304
|
+
content.updatedAt = new Date().toISOString();
|
|
305
|
+
// Immutable fields: a full replacement carries the STORED values, ignoring
|
|
306
|
+
// any client-supplied value, the way Integration.put carries its stored
|
|
307
|
+
// createdAt forward. A key is rotated by `flair agent rotate-key`, an
|
|
308
|
+
// `update` through Harper's operations API, not by this PUT.
|
|
309
|
+
content.createdAt = stored.createdAt;
|
|
310
|
+
content.publicKey = stored.publicKey;
|
|
311
|
+
// Keep the two admin fields agreeing on disk — see resources/agent-admin.ts.
|
|
312
|
+
// Only an administrator or a trusted internal call reaches here (see the
|
|
313
|
+
// refusal at the top of put()), so this normalises an authorized intent.
|
|
314
|
+
reconcileAdminFields(content);
|
|
315
|
+
// Write-time originatorInstanceId — see post() above /
|
|
316
|
+
// resources/originator-instance.ts. An UPDATE keeps the stored value (a
|
|
317
|
+
// body value never replaces or clears it).
|
|
318
|
+
await applyOriginatorInstanceId(content, stored);
|
|
319
|
+
applyFederationBookkeeping(content, stored);
|
|
320
|
+
// The resource's write joins the owned transaction: Harper resolves an
|
|
321
|
+
// instance write's transaction from its context at write time. A resource
|
|
322
|
+
// bound to a URL target writes to that target. An in-process call on a
|
|
323
|
+
// collection resource has no URL target, so the write names the body id
|
|
324
|
+
// (the id resolveStoredRow read the row by) as its target explicitly.
|
|
325
|
+
const bound = urlBoundId(this);
|
|
326
|
+
const result = bound !== null
|
|
327
|
+
? await super.put(content)
|
|
328
|
+
: await super.put(writeTargetId(this, content).value, content);
|
|
329
|
+
return { kind: "written", result };
|
|
330
|
+
});
|
|
331
|
+
if (outcome.kind === "denied")
|
|
332
|
+
return outcome.denial;
|
|
333
|
+
if (outcome.kind === "gone")
|
|
334
|
+
return requiresExisting();
|
|
335
|
+
return outcome.result;
|
|
159
336
|
}
|
|
160
337
|
/**
|
|
161
338
|
* PATCH → Harper's partial update (`Resource.patch(data, query)`; see
|
|
@@ -174,9 +351,59 @@ export class Agent extends databases.flair.Agent {
|
|
|
174
351
|
* Both verbs now share authorizePrincipalWrite().
|
|
175
352
|
*/
|
|
176
353
|
async patch(content, query) {
|
|
354
|
+
// flair#2359 — refuse an id outside the shared rule before any read or
|
|
355
|
+
// write.
|
|
356
|
+
const idDenial = agentIdDenial(writeTargetId(this, content));
|
|
357
|
+
if (idDenial)
|
|
358
|
+
return idDenial;
|
|
177
359
|
const denial = await this.authorizePrincipalWrite(content);
|
|
178
360
|
if (denial)
|
|
179
361
|
return denial;
|
|
362
|
+
// flair#2472 — the guarded home stamp, the write `flair agent stamp-home
|
|
363
|
+
// --apply` sends. This is NOT an ordinary merge: it decides and writes
|
|
364
|
+
// inside a FRESH OWNED transaction (see applyGuardedHomeStamp), never from
|
|
365
|
+
// the request's pinned read snapshot. Handled before the generic merge so
|
|
366
|
+
// it does not stamp a row that its owned read or its confirmation read sees
|
|
367
|
+
// with a home or provenance, and does not re-create a row either read finds
|
|
368
|
+
// absent.
|
|
369
|
+
if (content != null && typeof content === "object" && content[AGENT_HOME_STAMP_FIELD] === true) {
|
|
370
|
+
delete content[AGENT_HOME_STAMP_FIELD];
|
|
371
|
+
return this.applyGuardedHomeStamp(content);
|
|
372
|
+
}
|
|
373
|
+
// flair#2473 — the guarded write-back `flair principal add` sends when its
|
|
374
|
+
// create-only insert was SKIPPED: the row exists, so the record is written
|
|
375
|
+
// back WITHOUT the home field. It runs in a FRESH OWNED transaction (see
|
|
376
|
+
// applyGuardedPrincipalWriteBack) and refuses when its owned read or its
|
|
377
|
+
// confirmation read sees a stored home differing from the resolved local ID.
|
|
378
|
+
//
|
|
379
|
+
// Any admitted administrator or trusted internal caller can send the flag.
|
|
380
|
+
// It writes only the allow-list in applyGuardedPrincipalWriteBack.
|
|
381
|
+
// Admitted non-admin callers follow the ordinary PATCH path, which strips
|
|
382
|
+
// a client-supplied home and federation bookkeeping.
|
|
383
|
+
if (content != null && typeof content === "object" && content[AGENT_PRINCIPAL_WRITE_BACK_FIELD] === true) {
|
|
384
|
+
delete content[AGENT_PRINCIPAL_WRITE_BACK_FIELD];
|
|
385
|
+
const admission = admitPrincipalWrite(await resolveAgentAuth(this.getContext?.()));
|
|
386
|
+
if (admission === "admin" || admission === "internal") {
|
|
387
|
+
return this.applyGuardedPrincipalWriteBack(content);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// Test-only: inert unless the fault-injection env opt-in is set and armed.
|
|
391
|
+
// Holds an ordinary PATCH after authorizePrincipalWrite's read and decision,
|
|
392
|
+
// before the merge below, so an administrator's change can commit in
|
|
393
|
+
// between (test/integration/agent-patch-race-2696.test.ts).
|
|
394
|
+
const prePause = txnPausePoint("agent-patch-pre");
|
|
395
|
+
if (prePause)
|
|
396
|
+
await prePause;
|
|
397
|
+
// The ordinary PATCH reads and writes in the request's transaction. Harper
|
|
398
|
+
// stamps a transaction's writes with one time, taken at its first read
|
|
399
|
+
// (before the pause above), and applies the writes to a row in the order of
|
|
400
|
+
// those times. So a change an administrator commits after that read, from a
|
|
401
|
+
// transaction that takes its time after it, is applied after this PATCH: a
|
|
402
|
+
// role the administrator sets stands over the role reconciled here, and
|
|
403
|
+
// `status`, which a non-admin PATCH cannot name, is not written by it at
|
|
404
|
+
// all. Not covered: a write whose transaction took its time before this
|
|
405
|
+
// request's first read and commits after it (per-id serialization is
|
|
406
|
+
// tracked in flair#2478).
|
|
180
407
|
if (content != null && typeof content === "object") {
|
|
181
408
|
// A partial update must not be able to leave the record's two admin
|
|
182
409
|
// fields disagreeing, so reconcile against the MERGED result rather than
|
|
@@ -201,10 +428,213 @@ export class Agent extends databases.flair.Agent {
|
|
|
201
428
|
if (resolvedOriginRow.denial)
|
|
202
429
|
return resolvedOriginRow.denial;
|
|
203
430
|
await applyOriginatorInstanceId(content, resolvedOriginRow.row);
|
|
431
|
+
applyFederationBookkeeping(content, resolvedOriginRow.row);
|
|
204
432
|
content.updatedAt = new Date().toISOString();
|
|
205
433
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
434
|
+
return super.patch(content, query);
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The guarded home stamp (flair#2472), the ONE write `flair agent stamp-home
|
|
438
|
+
* --apply` sends. It is decided and applied inside a FRESH OWNED transaction,
|
|
439
|
+
* never from the request's pinned read snapshot.
|
|
440
|
+
*
|
|
441
|
+
* Harper loads `/Agent/:id` before this override runs, so the request context
|
|
442
|
+
* already carries this row in its read snapshot. A decision made from that
|
|
443
|
+
* snapshot is stale: a federation merge or another creator that commits in
|
|
444
|
+
* between is invisible. `withOwnedTransaction` detaches the request's
|
|
445
|
+
* transaction for this call and reads the row in a scope this call OWNS.
|
|
446
|
+
*
|
|
447
|
+
* The rules, evaluated inside that owned transaction:
|
|
448
|
+
* - this instance's own id — never the request body's — is the value stamped.
|
|
449
|
+
* It is read from the CURRENT `Instance` table at the start of the owned
|
|
450
|
+
* transaction (`currentInstanceId`, a fresh read every call — never the
|
|
451
|
+
* process-cached `localInstanceId`, which keeps a successful id after the
|
|
452
|
+
* table changes). If it could not confirm a single canonical Instance row,
|
|
453
|
+
* the write is REFUSED by name
|
|
454
|
+
* (`agent_home_stamp_no_instance`, 409) and nothing is staged;
|
|
455
|
+
* - a row the owned read finds absent is refused (404, `agent_home_stamp_no_row`);
|
|
456
|
+
* a stamp NEVER creates, so a row whose deletion that read sees is not
|
|
457
|
+
* re-created;
|
|
458
|
+
* - a row that is not home-less, or that carries federation-sync provenance,
|
|
459
|
+
* is reported `stamped: false` (a 200, nothing written, no `updatedAt` change);
|
|
460
|
+
* - otherwise the update is STAGED, then the COMMITTED row is re-read in an
|
|
461
|
+
* explicit fresh context (never this transaction's snapshot or its staged
|
|
462
|
+
* write). A row that confirmation read sees with a home or provenance, or
|
|
463
|
+
* sees as gone, ABORTS this owned transaction and discards the staged write.
|
|
464
|
+
*
|
|
465
|
+
* The guarded writes do not use compare-and-set: a home or provenance committed, or a deletion
|
|
466
|
+
* or an `Instance` table change made, after the read that checked it and before
|
|
467
|
+
* this transaction's commit is still not seen (the residual window; per-id
|
|
468
|
+
* serialization is tracked in flair#2478).
|
|
469
|
+
*
|
|
470
|
+
* The CLI reports a row `stamped` only when this resource confirms it applied
|
|
471
|
+
* the stamp (`stamped: true`): a read-back that merely shows this instance's
|
|
472
|
+
* home is not enough — another writer may have set it.
|
|
473
|
+
*/
|
|
474
|
+
async applyGuardedHomeStamp(content) {
|
|
475
|
+
const admission = admitPrincipalWrite(await resolveAgentAuth(this.getContext?.()));
|
|
476
|
+
if (admission !== "admin" && admission !== "internal") {
|
|
477
|
+
return new Response(JSON.stringify({ error: "agent_home_stamp_forbidden", message: "the home stamp is an administrator or trusted-internal write" }), { status: 403, headers: { "content-type": "application/json" } });
|
|
478
|
+
}
|
|
479
|
+
const target = writeTargetId(this, content);
|
|
480
|
+
const id = target.present && (typeof target.value === "string" || typeof target.value === "number")
|
|
481
|
+
? String(target.value)
|
|
482
|
+
: null;
|
|
483
|
+
const noRow = () => new Response(JSON.stringify({ error: "agent_home_stamp_no_row", message: "could not confirm a stored agent row to stamp" }), { status: 404, headers: { "content-type": "application/json" } });
|
|
484
|
+
const ineligible = () => new Response(JSON.stringify({ id, stamped: false, reason: "ineligible" }), { status: 200, headers: { "content-type": "application/json" } });
|
|
485
|
+
if (id === null)
|
|
486
|
+
return noRow();
|
|
487
|
+
// Could not confirm a single canonical Instance row: refuse with nothing staged.
|
|
488
|
+
const noInstance = () => new Response(JSON.stringify({ error: "agent_home_stamp_no_instance", message: "could not confirm a single canonical Instance row to stamp as the home; the row was not written" }), { status: 409, headers: { "content-type": "application/json" } });
|
|
489
|
+
const table = databases.flair?.Agent;
|
|
490
|
+
const readRow = async (context) => {
|
|
491
|
+
if (!table || typeof table.get !== "function")
|
|
492
|
+
return null;
|
|
493
|
+
const row = await table.get(id, context);
|
|
494
|
+
return row && typeof row === "object" && !(row instanceof Response) ? row : null;
|
|
495
|
+
};
|
|
496
|
+
const eligible = (row) => isHomeLessAgentRow(row) && !isSyncOriginatedAgentRow(row);
|
|
497
|
+
const updatedAt = new Date().toISOString();
|
|
498
|
+
let outcome;
|
|
499
|
+
try {
|
|
500
|
+
outcome = await withOwnedTransaction(this.getContext?.(), async (c) => {
|
|
501
|
+
// The value to stamp: this instance's id from the Instance table AS IT IS
|
|
502
|
+
// NOW (a fresh read, never the process cache), resolved BEFORE the
|
|
503
|
+
// eligibility read (flair#2478 pattern) so nothing awaits between that
|
|
504
|
+
// read and the staging, where a writer could commit unseen by the
|
|
505
|
+
// confirmation read.
|
|
506
|
+
const home = await currentInstanceId();
|
|
507
|
+
if (home === null)
|
|
508
|
+
return { kind: "no-instance" };
|
|
509
|
+
const stampBody = { originatorInstanceId: home, updatedAt };
|
|
510
|
+
// The row as this call OWNS it — a scope the request's snapshot does not pin.
|
|
511
|
+
const stored = await readRow(c);
|
|
512
|
+
if (stored === null)
|
|
513
|
+
return { kind: "no-row" };
|
|
514
|
+
if (!eligible(stored))
|
|
515
|
+
return { kind: "ineligible" };
|
|
516
|
+
if (!table || typeof table.update !== "function")
|
|
517
|
+
return { kind: "no-row" };
|
|
518
|
+
// STAGE the write of the home and updatedAt in this transaction's own scope.
|
|
519
|
+
await table.update(id, stampBody, c);
|
|
520
|
+
// Test-only: inert unless the fault-injection env opt-in is set and armed.
|
|
521
|
+
// The name is a DIRECT string literal at the call site (flair#2382's
|
|
522
|
+
// checker scans resources/ and src/ and resolves only literals); it must
|
|
523
|
+
// stay equal to AGENT_HOME_STAMP_PAUSE_POINT, which the integration test
|
|
524
|
+
// arms (test/integration/agent-home-2433.test.ts).
|
|
525
|
+
const pause = txnPausePoint("agent-home-stamp");
|
|
526
|
+
if (pause)
|
|
527
|
+
await pause;
|
|
528
|
+
// CONFIRM the COMMITTED row (an explicit fresh context, never this
|
|
529
|
+
// transaction's snapshot or its staged write) is still eligible. A row
|
|
530
|
+
// that gained a home or provenance, or that is gone, ABORTS this owned
|
|
531
|
+
// transaction: the staged write is discarded and the caller is told.
|
|
532
|
+
const committed = await readRow({});
|
|
533
|
+
if (committed === null)
|
|
534
|
+
throw new AgentHomeStampAborted("no-row");
|
|
535
|
+
if (!eligible(committed))
|
|
536
|
+
throw new AgentHomeStampAborted("ineligible");
|
|
537
|
+
return { kind: "applied" };
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
catch (err) {
|
|
541
|
+
if (err instanceof AgentHomeStampAborted)
|
|
542
|
+
return err.reason === "no-row" ? noRow() : ineligible();
|
|
543
|
+
throw err;
|
|
544
|
+
}
|
|
545
|
+
if (outcome.kind === "no-instance")
|
|
546
|
+
return noInstance();
|
|
547
|
+
if (outcome.kind === "no-row")
|
|
548
|
+
return noRow();
|
|
549
|
+
if (outcome.kind === "ineligible")
|
|
550
|
+
return ineligible();
|
|
551
|
+
return new Response(JSON.stringify({ id, stamped: true }), { status: 200, headers: { "content-type": "application/json" } });
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* The guarded write-back (flair#2473), the ONE write `flair principal add`
|
|
555
|
+
* sends when its create-only `insert` was SKIPPED because the row already
|
|
556
|
+
* exists. It runs inside a FRESH OWNED transaction, never from the request's
|
|
557
|
+
* pinned read snapshot, and it refuses by name in two cases:
|
|
558
|
+
*
|
|
559
|
+
* - the row is ABSENT (at the owned read before staging, or at the
|
|
560
|
+
* confirmation read): Harper's `table.update` would INSERT a missing row,
|
|
561
|
+
* so a deletion either read sees leaves the table without the row rather
|
|
562
|
+
* than re-creating it home-less (404, `agent_principal_write_back_no_row`);
|
|
563
|
+
* - a non-empty string stored home differs from the resolved local ID
|
|
564
|
+
* (`localInstanceId()`, cached after its first success, or null):
|
|
565
|
+
* the write is refused (`originator_instance_immutable`).
|
|
566
|
+
*
|
|
567
|
+
* The body this path writes is the EXPLICIT ALLOW-LIST
|
|
568
|
+
* (`pickPrincipalWriteBackFields`) plus the write key — never a home and never
|
|
569
|
+
* a federation stamp, whatever the request body carried — so an admitted caller
|
|
570
|
+
* cannot use the flag to change a stored home.
|
|
571
|
+
*
|
|
572
|
+
* The update is STAGED first, then the COMMITTED row is re-read in an explicit
|
|
573
|
+
* fresh context. If that read finds no row or a non-empty home differing from
|
|
574
|
+
* the resolved local ID, it ABORTS this owned transaction and discards the
|
|
575
|
+
* staged write. The guarded writes do not use compare-and-set: a home that changes, or a
|
|
576
|
+
* deletion, after the confirmation and before the commit is still not seen (the
|
|
577
|
+
* residual window; per-id serialization is tracked in flair#2478).
|
|
578
|
+
*/
|
|
579
|
+
async applyGuardedPrincipalWriteBack(content) {
|
|
580
|
+
const immutable = () => new Response(JSON.stringify({ error: "originator_instance_immutable", message: "originator_instance_immutable: the stored home differs from the resolved local ID; refusing to change it" }), { status: 409, headers: { "content-type": "application/json" } });
|
|
581
|
+
const unavailable = () => new Response(JSON.stringify({ error: "originator_instance_immutable", message: "could not confirm an eligible agent row for write-back; refusing to change it" }), { status: 409, headers: { "content-type": "application/json" } });
|
|
582
|
+
const noRow = () => new Response(JSON.stringify({ error: AGENT_PRINCIPAL_WRITE_BACK_NO_ROW_ERROR, message: `${AGENT_PRINCIPAL_WRITE_BACK_NO_ROW_ERROR}: could not confirm a stored agent row; the write-back was refused and did not re-create it` }), { status: 404, headers: { "content-type": "application/json" } });
|
|
583
|
+
const target = writeTargetId(this, content);
|
|
584
|
+
const id = target.present && (typeof target.value === "string" || typeof target.value === "number")
|
|
585
|
+
? String(target.value)
|
|
586
|
+
: null;
|
|
587
|
+
if (id === null)
|
|
588
|
+
return unavailable();
|
|
589
|
+
// Resolve everything that can yield FIRST (flair#2478 pattern) — this
|
|
590
|
+
// instance's own id — so nothing awaits between the eligibility read and the
|
|
591
|
+
// staging. ONLY the allow-listed fields are written back; the body carries no
|
|
592
|
+
// home field, so the stored home stands.
|
|
593
|
+
const home = await localInstanceId();
|
|
594
|
+
const table = databases.flair?.Agent;
|
|
595
|
+
const body = { ...pickPrincipalWriteBackFields(content), id };
|
|
596
|
+
let outcome;
|
|
597
|
+
try {
|
|
598
|
+
outcome = await withOwnedTransaction(this.getContext?.(), async (c) => {
|
|
599
|
+
if (!table || typeof table.get !== "function" || typeof table.update !== "function") {
|
|
600
|
+
return { kind: "unavailable" };
|
|
601
|
+
}
|
|
602
|
+
const stored = await table.get(id, c);
|
|
603
|
+
const storedRow = stored && typeof stored === "object" && !(stored instanceof Response) ? stored : null;
|
|
604
|
+
// Refuse an ABSENT row BEFORE staging: `table.update` would CREATE it, and
|
|
605
|
+
// this write-back must not re-create a row this read finds absent.
|
|
606
|
+
if (storedRow === null)
|
|
607
|
+
return { kind: "no-row" };
|
|
608
|
+
if (isForeignAgentHome(storedRow, home))
|
|
609
|
+
return { kind: "refused" };
|
|
610
|
+
// STAGE the write-back in this transaction's own scope.
|
|
611
|
+
await table.update(id, body, c);
|
|
612
|
+
// Test-only: inert unless the fault-injection env opt-in is set and armed.
|
|
613
|
+
const pause = txnPausePoint("agent-principal-write-back");
|
|
614
|
+
if (pause)
|
|
615
|
+
await pause;
|
|
616
|
+
// CONFIRM the COMMITTED row is present and has no non-empty home differing
|
|
617
|
+
// from the resolved local ID; otherwise abort and discard the staged write.
|
|
618
|
+
const committed = await table.get(id, {});
|
|
619
|
+
const committedRow = committed && typeof committed === "object" && !(committed instanceof Response) ? committed : null;
|
|
620
|
+
if (committedRow === null)
|
|
621
|
+
throw new PrincipalWriteBackAborted("no-row");
|
|
622
|
+
if (isForeignAgentHome(committedRow, home))
|
|
623
|
+
throw new PrincipalWriteBackAborted("foreign");
|
|
624
|
+
return { kind: "applied" };
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
catch (err) {
|
|
628
|
+
if (err instanceof PrincipalWriteBackAborted)
|
|
629
|
+
return err.reason === "no-row" ? noRow() : immutable();
|
|
630
|
+
throw err;
|
|
631
|
+
}
|
|
632
|
+
if (outcome.kind === "no-row")
|
|
633
|
+
return noRow();
|
|
634
|
+
if (outcome.kind === "unavailable")
|
|
635
|
+
return unavailable();
|
|
636
|
+
if (outcome.kind === "refused")
|
|
637
|
+
return immutable();
|
|
638
|
+
return new Response(JSON.stringify({ id, writtenBack: true }), { status: 200, headers: { "content-type": "application/json" } });
|
|
209
639
|
}
|
|
210
640
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Resource, databases } from "harper";
|
|
2
2
|
import { selectPublicDescription, selectPublicSkills } from "./agentcard-fields.js";
|
|
3
|
+
import { refuseRewrittenAgentId } from "./path-id-rewrite.js";
|
|
3
4
|
export class AgentCard extends Resource {
|
|
4
5
|
// Public discovery metadata (per A2A spec, agent cards are intentionally
|
|
5
6
|
// public). Self-gates as public so it survives the global gate's removal — and
|
|
@@ -14,6 +15,9 @@ export class AgentCard extends Resource {
|
|
|
14
15
|
if (!agentId) {
|
|
15
16
|
return new Response(JSON.stringify({ error: "agentId required in path: GET /AgentCard/{agentId}" }), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
16
17
|
}
|
|
18
|
+
const rewritten = refuseRewrittenAgentId(this.getContext?.(), agentId);
|
|
19
|
+
if (rewritten)
|
|
20
|
+
return rewritten;
|
|
17
21
|
const agent = await databases.flair.Agent.get(agentId).catch(() => null);
|
|
18
22
|
if (!agent) {
|
|
19
23
|
return new Response(JSON.stringify({ error: "agent_not_found", agentId }), {
|