@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
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
* uninstall itself. The report always names leftovers instead of claiming
|
|
15
15
|
* "fully purged" when anything remains.
|
|
16
16
|
*/
|
|
17
|
-
import { existsSync, rmSync } from "node:fs";
|
|
18
|
-
import { join } from "node:path";
|
|
17
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { basename, join } from "node:path";
|
|
19
19
|
import { resolveHome, withHome } from "./home.js";
|
|
20
|
-
import { ALL_CLIENTS } from "../install/clients.js";
|
|
20
|
+
import { ALL_CLIENTS, clientConfigBackupPath, clientConfigHasFlairEntry, } from "../install/clients.js";
|
|
21
21
|
import { uninstallContinuityHooks, uninstallHook, harnessSupportsContinuity, hookBackupPath, hookSettingsPath, SUPPORTED_HARNESSES, } from "../hook-install.js";
|
|
22
22
|
import { disableScheduler as disableRemScheduler, LAUNCHD_LABEL as REM_LAUNCHD_LABEL, SYSTEMD_SERVICE_UNIT as REM_SYSTEMD_SERVICE, SYSTEMD_TIMER_UNIT as REM_SYSTEMD_TIMER, } from "../rem/scheduler.js";
|
|
23
23
|
import { disableScheduler as disableFederationScheduler, LAUNCHD_LABEL as FED_LAUNCHD_LABEL, SYSTEMD_SERVICE_UNIT as FED_SYSTEMD_SERVICE, SYSTEMD_TIMER_UNIT as FED_SYSTEMD_TIMER, } from "../federation/scheduler.js";
|
|
@@ -103,18 +103,248 @@ function sweepHookBackup(settingsPath, reportedBackup, homeDir, removed, leftove
|
|
|
103
103
|
rmPath(bak, removed, leftovers, displayUnderHome(homeDir, bak));
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* After a failed read: is the path genuinely absent? A dangling symlink also
|
|
108
|
+
* reads as ENOENT but is an entry purge could not check — not a missing one.
|
|
109
|
+
*/
|
|
110
|
+
function backupEntryAbsent(path) {
|
|
111
|
+
try {
|
|
112
|
+
lstatSync(path);
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
return err.code === "ENOENT";
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Client configs get the same sibling snapshot as hook settings: an unwire
|
|
121
|
+
* that writes an existing config copies its pre-write bytes to `<config>.bak`
|
|
122
|
+
* when that copy write succeeds. Purge removes a copy that
|
|
123
|
+
* `clientConfigHasFlairEntry()` identifies as carrying a Flair entry and
|
|
124
|
+
* reports the path like a hook backup. A copy with no Flair entry may be the
|
|
125
|
+
* user's own file and stays — as does one whose wiring could not be decided,
|
|
126
|
+
* which is reported as not checked (flair#2886).
|
|
127
|
+
*
|
|
128
|
+
* Call inside `withHome(homeDir, ...)`: `clientConfigBackupPath()` resolves `~`
|
|
129
|
+
* at call time, so the sweep must run with the target home in the environment,
|
|
130
|
+
* not the caller's (flair#2886).
|
|
131
|
+
*/
|
|
132
|
+
function sweepClientConfigBackup(clientId, homeDir, removed, leftovers) {
|
|
133
|
+
const backup = clientConfigBackupPath(clientId);
|
|
134
|
+
const display = displayUnderHome(homeDir, backup);
|
|
135
|
+
let bytes;
|
|
136
|
+
try {
|
|
137
|
+
bytes = readFileSync(backup);
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
const code = err.code;
|
|
141
|
+
if (code === "ENOENT" && backupEntryAbsent(backup))
|
|
142
|
+
return;
|
|
143
|
+
leftovers.push({
|
|
144
|
+
path: display,
|
|
145
|
+
kind: "unverified",
|
|
146
|
+
reason: `could not check for Flair wiring: could not read the file${code ? ` (${code})` : ""}`,
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const scan = clientConfigHasFlairEntry(clientId, bytes, display);
|
|
151
|
+
if (scan.status === "unknown") {
|
|
152
|
+
leftovers.push({
|
|
153
|
+
path: display,
|
|
154
|
+
kind: "unverified",
|
|
155
|
+
reason: `could not check for Flair wiring: ${scan.reason}`,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (scan.status === "present")
|
|
160
|
+
rmPath(backup, removed, leftovers, display);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* TEST-ONLY pause between choosing a hold directory name and creating it, so
|
|
164
|
+
* a fixture can occupy the name. Inert unless
|
|
165
|
+
* `FLAIR_TEST_PURGE_HOLD_BARRIER` names a directory; self-releases after 15 s.
|
|
166
|
+
*/
|
|
167
|
+
function holdSelectionBarrier(candidate) {
|
|
168
|
+
const barrierDir = process.env.FLAIR_TEST_PURGE_HOLD_BARRIER;
|
|
169
|
+
if (!barrierDir)
|
|
170
|
+
return;
|
|
171
|
+
try {
|
|
172
|
+
writeFileSync(join(barrierDir, `${process.pid}.hold-select`), candidate);
|
|
173
|
+
}
|
|
174
|
+
catch { /* inert */ }
|
|
175
|
+
const release = join(barrierDir, `${process.pid}.go`);
|
|
176
|
+
const deadline = Date.now() + 15_000;
|
|
177
|
+
while (!existsSync(release) && Date.now() < deadline) {
|
|
178
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Select a hold directory name and create it exclusively. `mkdir` without
|
|
183
|
+
* `recursive` fails with `EEXIST` when any entry already occupies the name,
|
|
184
|
+
* so a user file or directory there is never adopted or overwritten. The name
|
|
185
|
+
* carries this process's pid and a per-client sequence; the directory is
|
|
186
|
+
* created with requested mode 0700.
|
|
187
|
+
*/
|
|
188
|
+
function reserveHoldPath(backup, seq) {
|
|
189
|
+
for (let attempt = 0; attempt < 16; attempt++) {
|
|
190
|
+
const candidate = `${backup}.purge-hold-${process.pid}-${seq}${attempt === 0 ? "" : `-${attempt}`}`;
|
|
191
|
+
holdSelectionBarrier(candidate);
|
|
192
|
+
try {
|
|
193
|
+
mkdirSync(candidate, { mode: 0o700 });
|
|
194
|
+
return { dir: candidate, path: join(candidate, basename(backup)) };
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
if (err.code === "EEXIST")
|
|
198
|
+
continue;
|
|
199
|
+
throw err;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
throw new Error("no free hold path name after 16 attempts");
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A pre-existing `<config>.bak` is the user's copy until this purge decides
|
|
206
|
+
* otherwise, but the unwire's snapshot write renames over it. Move it aside
|
|
207
|
+
* first and put it back before the sweep, so its bytes — or, for a symlink,
|
|
208
|
+
* the link itself — survive the unwire. This covers a copy that cannot be
|
|
209
|
+
* read or parsed too: a failed read must never license its replacement. The
|
|
210
|
+
* hold directory is created exclusively, so a file or directory already at
|
|
211
|
+
* its name is never overwritten; when no free name can be reserved, or the
|
|
212
|
+
* rename fails, the client is reported and its unwire and sweep are skipped.
|
|
213
|
+
* Call inside `withHome(homeDir, ...)`.
|
|
214
|
+
*/
|
|
215
|
+
function holdClientConfigBackups(homeDir, leftovers) {
|
|
216
|
+
const held = [];
|
|
217
|
+
const skipped = new Set();
|
|
218
|
+
for (const client of ALL_CLIENTS) {
|
|
219
|
+
const backup = clientConfigBackupPath(client.id);
|
|
220
|
+
try {
|
|
221
|
+
lstatSync(backup);
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
if (err.code === "ENOENT")
|
|
225
|
+
continue;
|
|
226
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
227
|
+
leftovers.push({
|
|
228
|
+
path: displayUnderHome(homeDir, backup),
|
|
229
|
+
kind: "failed",
|
|
230
|
+
reason: `could not hold the backup across unwiring: ${reason}; skipped unwiring this client`,
|
|
231
|
+
});
|
|
232
|
+
skipped.add(client.id);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
let hold;
|
|
236
|
+
try {
|
|
237
|
+
hold = reserveHoldPath(backup, held.length);
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
241
|
+
leftovers.push({
|
|
242
|
+
path: displayUnderHome(homeDir, backup),
|
|
243
|
+
kind: "failed",
|
|
244
|
+
reason: `could not reserve a hold path: ${reason}; skipped unwiring this client`,
|
|
245
|
+
});
|
|
246
|
+
skipped.add(client.id);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
try {
|
|
250
|
+
renameSync(backup, hold.path);
|
|
251
|
+
held.push({ clientId: client.id, path: backup, heldPath: hold.path, heldDir: hold.dir, display: displayUnderHome(homeDir, backup) });
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
rmSync(hold.dir, { recursive: true, force: true });
|
|
255
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
256
|
+
leftovers.push({
|
|
257
|
+
path: displayUnderHome(homeDir, backup),
|
|
258
|
+
kind: "failed",
|
|
259
|
+
reason: `could not hold the backup across unwiring: ${reason}; skipped unwiring this client`,
|
|
260
|
+
});
|
|
261
|
+
skipped.add(client.id);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return { held, skipped };
|
|
265
|
+
}
|
|
266
|
+
/** Put a held entry back over the snapshot this purge's unwire attempts to
|
|
267
|
+
* write, then let the sweep decide it. A client whose restore fails is
|
|
268
|
+
* returned so its sweep is skipped; the REASON names the held copy, and
|
|
269
|
+
* after a failed restore that copy may remain only at the hold path. Call
|
|
270
|
+
* inside `withHome(homeDir, ...)`. */
|
|
271
|
+
export function restoreHeldClientConfigBackups(homeDir, held, leftovers) {
|
|
272
|
+
const unrestored = new Set();
|
|
273
|
+
for (const item of held) {
|
|
274
|
+
try {
|
|
275
|
+
rmSync(item.path, { force: true });
|
|
276
|
+
renameSync(item.heldPath, item.path);
|
|
277
|
+
}
|
|
278
|
+
catch (err) {
|
|
279
|
+
unrestored.add(item.clientId);
|
|
280
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
281
|
+
leftovers.push({
|
|
282
|
+
path: item.display,
|
|
283
|
+
kind: "failed",
|
|
284
|
+
reason: `could not restore the backup held across unwiring: ${reason}; skipped this backup's sweep; ` +
|
|
285
|
+
`the held copy remains at ${displayUnderHome(homeDir, item.heldPath)}`,
|
|
286
|
+
});
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
// Empty now; remove only the directory this purge created. Its loss is
|
|
290
|
+
// not a restore failure — the copy is already back at the config path.
|
|
291
|
+
try {
|
|
292
|
+
rmSync(item.heldDir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
catch { /* empty hold dir left */ }
|
|
295
|
+
}
|
|
296
|
+
return unrestored;
|
|
297
|
+
}
|
|
298
|
+
/** Restore held snapshots, then sweep eligible clients. A failed hold skips
|
|
299
|
+
* unwiring and leaves the original backup in place. A failed restore skips
|
|
300
|
+
* the sweep and reports the held path; the original backup path may be absent.
|
|
301
|
+
* An absent backup is not held, and its client proceeds (flair#2886). */
|
|
302
|
+
export function restoreAndSweepClientConfigBackups(homeDir, hold, removed, leftovers) {
|
|
303
|
+
const unrestored = restoreHeldClientConfigBackups(homeDir, hold.held, leftovers);
|
|
304
|
+
for (const client of ALL_CLIENTS) {
|
|
305
|
+
if (hold.skipped.has(client.id) || unrestored.has(client.id))
|
|
306
|
+
continue;
|
|
307
|
+
sweepClientConfigBackup(client.id, homeDir, removed, leftovers);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** Run client-config work with `HOME` set to the home being purged, and —
|
|
311
|
+
* when that home is not the caller's — with `PI_CODING_AGENT_DIR` ignored, so
|
|
312
|
+
* pi resolves under the target home instead of the caller's pi directory. */
|
|
313
|
+
function withClientConfigHome(homeDir, ignorePiDirOverride, fn) {
|
|
314
|
+
if (!ignorePiDirOverride) {
|
|
315
|
+
withHome(homeDir, fn);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const prevPiDir = process.env.PI_CODING_AGENT_DIR;
|
|
319
|
+
withHome(homeDir, () => {
|
|
320
|
+
delete process.env.PI_CODING_AGENT_DIR;
|
|
321
|
+
try {
|
|
322
|
+
fn();
|
|
323
|
+
}
|
|
324
|
+
finally {
|
|
325
|
+
if (prevPiDir !== undefined)
|
|
326
|
+
process.env.PI_CODING_AGENT_DIR = prevPiDir;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
106
330
|
/**
|
|
107
331
|
* Remove Flair-owned state under `homeDir`: the `~/.flair` tree (data, keys,
|
|
108
332
|
* admin-pass, backups, logs, snapshots, shims), REM + federation scheduler
|
|
109
|
-
* units on both platforms, SessionStart/continuity hooks,
|
|
110
|
-
* client wiring
|
|
111
|
-
* unless `omitNpmLeftover` is set.
|
|
333
|
+
* units on both platforms, SessionStart/continuity hooks, MCP/native
|
|
334
|
+
* client wiring, and the client-config snapshots left by unwiring. The npm
|
|
335
|
+
* package is listed as an intentional leftover unless `omitNpmLeftover` is set.
|
|
112
336
|
*/
|
|
113
337
|
export function purgeFlairInstall(opts = {}) {
|
|
114
|
-
const
|
|
338
|
+
const callerHome = resolveHome();
|
|
339
|
+
const homeDir = opts.homeDir ?? callerHome;
|
|
340
|
+
const ignorePiDirOverride = homeDir !== callerHome && process.env.PI_CODING_AGENT_DIR !== undefined;
|
|
115
341
|
const skipUnload = !!opts.skipSchedulerUnload;
|
|
116
342
|
const removed = [];
|
|
117
343
|
const leftovers = [];
|
|
344
|
+
const notes = [];
|
|
345
|
+
if (ignorePiDirOverride) {
|
|
346
|
+
notes.push(`ignored PI_CODING_AGENT_DIR while purging a different home: pi settings resolved under ${displayUnderHome(homeDir, join(homeDir, ".pi", "agent"))}`);
|
|
347
|
+
}
|
|
118
348
|
disableAndSweepScheduler(homeDir, skipUnload, "rem", removed, leftovers);
|
|
119
349
|
disableAndSweepScheduler(homeDir, skipUnload, "federation", removed, leftovers);
|
|
120
350
|
for (const harness of SUPPORTED_HARNESSES) {
|
|
@@ -141,7 +371,17 @@ export function purgeFlairInstall(opts = {}) {
|
|
|
141
371
|
for (const harness of SUPPORTED_HARNESSES) {
|
|
142
372
|
sweepHookBackup(hookSettingsPath(homeDir, harness), null, homeDir, removed, leftovers);
|
|
143
373
|
}
|
|
144
|
-
const
|
|
374
|
+
const hold = { held: [], skipped: new Set() };
|
|
375
|
+
withClientConfigHome(homeDir, ignorePiDirOverride, () => {
|
|
376
|
+
const found = holdClientConfigBackups(homeDir, leftovers);
|
|
377
|
+
hold.held.push(...found.held);
|
|
378
|
+
for (const id of found.skipped)
|
|
379
|
+
hold.skipped.add(id);
|
|
380
|
+
});
|
|
381
|
+
const unwireResults = [];
|
|
382
|
+
withClientConfigHome(homeDir, ignorePiDirOverride, () => {
|
|
383
|
+
unwireResults.push(...ALL_CLIENTS.filter((c) => !hold.skipped.has(c.id)).map((c) => c.unwire()));
|
|
384
|
+
});
|
|
145
385
|
for (const u of unwireResults) {
|
|
146
386
|
if (!u.ok) {
|
|
147
387
|
leftovers.push({ path: u.message, kind: "failed", reason: u.message });
|
|
@@ -150,6 +390,12 @@ export function purgeFlairInstall(opts = {}) {
|
|
|
150
390
|
removed.push(u.message);
|
|
151
391
|
}
|
|
152
392
|
}
|
|
393
|
+
// Restore and sweep under the target home. A failed hold skipped unwiring
|
|
394
|
+
// and left the original backup in place; a failed restore skips the sweep,
|
|
395
|
+
// reports the held path, and may leave the original backup path absent.
|
|
396
|
+
withClientConfigHome(homeDir, ignorePiDirOverride, () => {
|
|
397
|
+
restoreAndSweepClientConfigBackups(homeDir, hold, removed, leftovers);
|
|
398
|
+
});
|
|
153
399
|
const flairDir = join(homeDir, ".flair");
|
|
154
400
|
rmPath(flairDir, removed, leftovers, "~/.flair");
|
|
155
401
|
if (existsSync(flairDir)) {
|
|
@@ -167,19 +413,26 @@ export function purgeFlairInstall(opts = {}) {
|
|
|
167
413
|
remedy: `npm uninstall -g ${FLAIR_NPM_PACKAGE}`,
|
|
168
414
|
});
|
|
169
415
|
}
|
|
170
|
-
return { removed, leftovers };
|
|
416
|
+
return { removed, leftovers, notes };
|
|
171
417
|
}
|
|
172
418
|
export function formatPurgeReport(result) {
|
|
173
419
|
const lines = [];
|
|
420
|
+
for (const note of result.notes ?? []) {
|
|
421
|
+
lines.push(`ℹ️ ${note}`);
|
|
422
|
+
}
|
|
174
423
|
for (const item of result.removed) {
|
|
175
424
|
lines.push(`✅ Removed: ${item}`);
|
|
176
425
|
}
|
|
177
426
|
const failed = result.leftovers.filter((l) => l.kind === "failed");
|
|
427
|
+
const unverified = result.leftovers.filter((l) => l.kind === "unverified");
|
|
178
428
|
const intentional = result.leftovers.filter((l) => l.kind === "intentional");
|
|
179
429
|
for (const item of failed) {
|
|
180
430
|
lines.push(`⚠️ Left (failed): ${item.path} — ${item.reason}`);
|
|
181
431
|
}
|
|
182
|
-
|
|
432
|
+
for (const item of unverified) {
|
|
433
|
+
lines.push(`❓ Not checked: ${item.path} — ${item.reason}`);
|
|
434
|
+
}
|
|
435
|
+
if (failed.length === 0 && unverified.length === 0 && intentional.length === 0) {
|
|
183
436
|
lines.push("");
|
|
184
437
|
lines.push("🗑️ Flair fully purged");
|
|
185
438
|
return { lines, claimedFullyPurged: true };
|
|
@@ -198,5 +451,7 @@ export function formatPurgeReport(result) {
|
|
|
198
451
|
return { lines, claimedFullyPurged: false };
|
|
199
452
|
}
|
|
200
453
|
export function purgeHadFailures(result) {
|
|
201
|
-
|
|
454
|
+
// An unverified path means purge could not prove it was clean, so the run is
|
|
455
|
+
// not a success either (flair#2886).
|
|
456
|
+
return result.leftovers.some((l) => l.kind === "failed" || l.kind === "unverified");
|
|
202
457
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Classify the pre-install /Health attempt for the restart-failure decision. */
|
|
2
|
+
import { REFUSED_CODES } from "./connect-phase.js";
|
|
2
3
|
const DEFAULT_TIMEOUT_MS = 3000;
|
|
3
|
-
const REFUSED_TOKENS =
|
|
4
|
+
const REFUSED_TOKENS = REFUSED_CODES;
|
|
4
5
|
function namesRefusal(text) {
|
|
5
6
|
return REFUSED_TOKENS.some((token) => text.includes(token));
|
|
6
7
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* using-flair-skill.ts — the shipped `using-flair` skill text (flair#2141 S2).
|
|
3
|
+
*
|
|
4
|
+
* Normal `flair init` ensures this skill's Memory row and org assignment.
|
|
5
|
+
* Re-initializing an already-installed default local instance
|
|
6
|
+
* with `flair init --skip-start` defers that to a later `flair start`
|
|
7
|
+
* with the admin credential.
|
|
8
|
+
* Re-runs preserve edited text. An eligible agent's bootstrap may list it when
|
|
9
|
+
* the assignment wins same-name conflict resolution and fits the budget.
|
|
10
|
+
*
|
|
11
|
+
* ONE reviewed text, reconciled from the flair best-practices guidance and the
|
|
12
|
+
* cursor-flair skills (remember, bootstrap, coordinate, soul). It covers when to
|
|
13
|
+
* write and at what durability, provenance, recall habits, what not to store,
|
|
14
|
+
* identity, and where the Agent and Presence records describe the office.
|
|
15
|
+
*
|
|
16
|
+
* The text is code: it is reviewed like any other file, it carries a content
|
|
17
|
+
* hash, and the shipped-hash list below is the record of every version this
|
|
18
|
+
* repo has ever shipped. `src/lib/skill-seed.ts` (the decision and the write)
|
|
19
|
+
* reads them.
|
|
20
|
+
*
|
|
21
|
+
* Pure constants + one hash function, no Harper import, so the seed decision is
|
|
22
|
+
* unit-testable without a server.
|
|
23
|
+
*/
|
|
24
|
+
import { createHash } from "node:crypto";
|
|
25
|
+
/** The skill's Memory `metadata.name` and the org assignment's `skillName`. */
|
|
26
|
+
export const USING_FLAIR_SKILL_NAME = "using-flair";
|
|
27
|
+
/** The recall trigger — the "when to use" text a skill row embeds from. */
|
|
28
|
+
export const USING_FLAIR_SKILL_TRIGGER = "Use when you are about to store a durable decision, lesson, preference or fact, " +
|
|
29
|
+
"recall prior context at session start or after a gap, or look up who is in the office.";
|
|
30
|
+
/** The procedure, separating current Flair behaviour from recommended practice. */
|
|
31
|
+
export const USING_FLAIR_SKILL_CONTENT = [
|
|
32
|
+
"# Using Flair",
|
|
33
|
+
"",
|
|
34
|
+
"## Current Flair behavior",
|
|
35
|
+
"",
|
|
36
|
+
"### Durability and visibility",
|
|
37
|
+
"- Memory supports `permanent`, `persistent`, `standard` (the default), and `ephemeral` durability. Ephemeral memory expires on a timer (24 hours by default), not at the end of a session.",
|
|
38
|
+
"- permanent — routine maintenance never reaps or age-archives it (an expired validTo archives an eligible row; an acquired expiresAt never reaps it); it never decays; bootstrap considers the bootstrapping agent's own permanent memories before recent rows, subject to scope, expiry/closure and the token budget.",
|
|
39
|
+
"- persistent — routine maintenance never reaps or age-archives it (an expired validTo archives an eligible row; an acquired expiresAt never reaps it).",
|
|
40
|
+
"- standard — routine maintenance archives it once its validTo passes or, as a session note, after 30 days.",
|
|
41
|
+
"- ephemeral — routine maintenance reaps it once its TTL (24h by default) passes.",
|
|
42
|
+
"- No tier adds a flush, fsync, backup or replica acknowledgement: an explicit delete (owner or admin) or a store failure can end any of them.",
|
|
43
|
+
"- `permanent` and `persistent` default to `shared`; `standard` and `ephemeral` default to `private`.",
|
|
44
|
+
"",
|
|
45
|
+
"### Provenance",
|
|
46
|
+
"- `Memory.post` and `Memory.put` refuse a non-admin write whose `agentId` names another principal; admins bypass this mismatch check.",
|
|
47
|
+
"",
|
|
48
|
+
"### Identity and office records",
|
|
49
|
+
"- Your identity and standing instructions live in Soul. A bootstrap carries them when it is asked for soul (`includeSoul`) and when they fit its token budget.",
|
|
50
|
+
"- `Agent` holds registered agent records with `kind`, `role`, and `status` fields. A Basic administrator need not have an Agent row, and an Agent record is not a promise that the principal is reachable.",
|
|
51
|
+
"- Where a record exists, `Presence` uses `lastHeartbeatAt` to decide its `presenceStatus`. `activity` describes work; it is not a liveness verdict. A Presence record is not a guaranteed address.",
|
|
52
|
+
"",
|
|
53
|
+
"## Recommended practice",
|
|
54
|
+
"",
|
|
55
|
+
"### Writing and recall",
|
|
56
|
+
"- Store a decision, lesson, preference, or fact that should outlive this session as one concise `content` string.",
|
|
57
|
+
"- Use `permanent` for identity and long-lived facts, `persistent` for decisions and lessons, `standard` for ordinary working memory, and `ephemeral` for private scratch.",
|
|
58
|
+
"- Set `visibility: \"shared\"` explicitly to share an ordinary working fact with teammates.",
|
|
59
|
+
"- Write as yourself.",
|
|
60
|
+
"- Confirm a write with the returned id and a short preview.",
|
|
61
|
+
"- At session start, on resume, or after a long gap, load your bootstrap before answering from memory.",
|
|
62
|
+
"- On a task switch, search memory from two or three angles before acting.",
|
|
63
|
+
"- When a search surfaces a row you just wrote or a near-duplicate, update that row instead of adding another.",
|
|
64
|
+
"",
|
|
65
|
+
"### What not to store",
|
|
66
|
+
"- Never store secrets: API keys, passwords, tokens, or key-file contents.",
|
|
67
|
+
"- Never store another person's private data.",
|
|
68
|
+
"",
|
|
69
|
+
"### Identity",
|
|
70
|
+
"- Set identity and standing instructions deliberately in Soul rather than leaving them to memory.",
|
|
71
|
+
].join("\n");
|
|
72
|
+
/** sha256 of a skill row's `content` — the value the shipped-hash list holds. */
|
|
73
|
+
export function usingFlairSkillHash(content) {
|
|
74
|
+
return createHash("sha256").update(content, "utf8").digest("hex");
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The hashes of every `using-flair` text this repository has shipped. A stored
|
|
78
|
+
* row whose content hash is in this list holds text that matches a listed
|
|
79
|
+
* shipped version, so the installer may replace it; any other text is left
|
|
80
|
+
* alone. (A hash does not identify who wrote the text.) Append a new hash here
|
|
81
|
+
* whenever the text above changes.
|
|
82
|
+
*/
|
|
83
|
+
export const USING_FLAIR_SHIPPED_HASHES = Object.freeze([
|
|
84
|
+
usingFlairSkillHash(USING_FLAIR_SKILL_CONTENT),
|
|
85
|
+
]);
|
|
86
|
+
/** The current shipped content's hash. */
|
|
87
|
+
export const USING_FLAIR_CURRENT_HASH = usingFlairSkillHash(USING_FLAIR_SKILL_CONTENT);
|
|
@@ -18,6 +18,17 @@
|
|
|
18
18
|
* See test/unit/mcp-client-assertion.test.ts for a mirror of that
|
|
19
19
|
* verification, run against assertions this module produces.
|
|
20
20
|
*
|
|
21
|
+
* ── The audience form (RFC 7523bis transition switch) ───────────────────────
|
|
22
|
+
* RFC 7523bis makes the authorization server's issuer the sole `aud` and
|
|
23
|
+
* defines `typ: client-authentication+jwt`. The verifier's side of that
|
|
24
|
+
* (HarperFast/oauth #245) is merged upstream but NOT RELEASED, so the DEFAULT
|
|
25
|
+
* here stays the shape above and the issuer form is opt-in, via
|
|
26
|
+
* `FLAIR_MCP_CLIENT_ASSERTION_AUDIENCE` or `flair mcp token
|
|
27
|
+
* --assertion-audience`. The issuer form signs `aud` = the issuer read from
|
|
28
|
+
* the authorization server's metadata document — never derived from the
|
|
29
|
+
* token-endpoint URL — with `typ: "client-authentication+jwt"`. The default
|
|
30
|
+
* will be changed in a later PR after a compatible verifier release.
|
|
31
|
+
*
|
|
21
32
|
* ── oauth#161/#162/#163, shipped in @harperfast/oauth@2.2.0 ─────────────────
|
|
22
33
|
* The token-endpoint grant that CONSUMES this assertion
|
|
23
34
|
* (`grant_type=client_credentials`) shipped as HarperFast/oauth PR #170
|
|
@@ -34,10 +45,7 @@
|
|
|
34
45
|
* `sub`).
|
|
35
46
|
* - RFC 8707 `resource` parameter: accepted, exact-match, fail-closed,
|
|
36
47
|
* defaulting to the configured canonical resource — `buildTokenRequestForm`
|
|
37
|
-
* already carries an optional `resource` pass-through (see below)
|
|
38
|
-
* `defaultMcpResource()` supplies the configured canonical default
|
|
39
|
-
* (mirrors `resources/mcp-oauth-flag.ts`'s `mcpResource()` — the same
|
|
40
|
-
* `<issuer>/mcp` value the AS will exact-match against).
|
|
48
|
+
* already carries an optional `resource` pass-through (see below).
|
|
41
49
|
* - Issued token `sub` = `client_id`; access-token TTL default 300s
|
|
42
50
|
* (`mcp.clientCredentials.accessTokenTtl`); no `refresh_token` — agents
|
|
43
51
|
* re-mint on 401/near-expiry instead (see `getMcpAccessToken` below).
|
|
@@ -65,6 +73,99 @@ export const CLIENT_ASSERTION_TYPE_JWT_BEARER = "urn:ietf:params:oauth:client-as
|
|
|
65
73
|
* assertion signed with a longer window would be rejected server-side.
|
|
66
74
|
*/
|
|
67
75
|
export const MAX_ASSERTION_LIFETIME_SECONDS = 60;
|
|
76
|
+
/** The switch's accepted values, in the order the refusal message lists them. */
|
|
77
|
+
export const CLIENT_ASSERTION_AUDIENCE_FORMS = [
|
|
78
|
+
"token-endpoint",
|
|
79
|
+
"issuer",
|
|
80
|
+
];
|
|
81
|
+
/**
|
|
82
|
+
* The switch's environment variable (`flair mcp token --assertion-audience`
|
|
83
|
+
* overrides it). Default `"token-endpoint"`; an unrecognised value is refused,
|
|
84
|
+
* never treated as the default.
|
|
85
|
+
*/
|
|
86
|
+
export const CLIENT_ASSERTION_AUDIENCE_ENV = "FLAIR_MCP_CLIENT_ASSERTION_AUDIENCE";
|
|
87
|
+
/**
|
|
88
|
+
* The configured audience form, from an explicit flag value (else from
|
|
89
|
+
* CLIENT_ASSERTION_AUDIENCE_ENV). Unset or empty: `"token-endpoint"`.
|
|
90
|
+
*/
|
|
91
|
+
export function clientAssertionAudienceForm(raw = process.env[CLIENT_ASSERTION_AUDIENCE_ENV]) {
|
|
92
|
+
const value = (raw ?? "").trim().toLowerCase();
|
|
93
|
+
if (!value)
|
|
94
|
+
return "token-endpoint";
|
|
95
|
+
if (value === "token-endpoint" || value === "issuer")
|
|
96
|
+
return value;
|
|
97
|
+
throw new Error(`Unknown client-assertion audience form ${JSON.stringify(raw)}: ${CLIENT_ASSERTION_AUDIENCE_ENV} ` +
|
|
98
|
+
`(or --assertion-audience) must be ${CLIENT_ASSERTION_AUDIENCE_FORMS.map((f) => `"${f}"`).join(" or ")}.`);
|
|
99
|
+
}
|
|
100
|
+
/** The header `typ` of the token-endpoint audience form. */
|
|
101
|
+
export const CLIENT_ASSERTION_TYP_JWT = "JWT";
|
|
102
|
+
/** RFC 7523bis §4 explicit `typ`: the header of the issuer audience form. */
|
|
103
|
+
export const CLIENT_ASSERTION_TYP_CLIENT_AUTHENTICATION = "client-authentication+jwt";
|
|
104
|
+
/**
|
|
105
|
+
* The metadata document URL for an authorization server origin — the discovery
|
|
106
|
+
* convention src/lib/mcp-enable.ts already uses.
|
|
107
|
+
*/
|
|
108
|
+
export function oauthMetadataUrl(origin) {
|
|
109
|
+
return `${origin.replace(/\/+$/, "")}/.well-known/oauth-authorization-server`;
|
|
110
|
+
}
|
|
111
|
+
function isUsableIssuerUrl(value) {
|
|
112
|
+
try {
|
|
113
|
+
const url = new URL(value);
|
|
114
|
+
// RFC 8414 issuer identifiers cannot contain query or fragment components,
|
|
115
|
+
// including empty delimiters (`?` and `#`). URL.search/hash alone miss those.
|
|
116
|
+
return (url.protocol === "https:" || url.protocol === "http:") &&
|
|
117
|
+
!url.href.includes("?") && !url.href.includes("#");
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Read the `issuer` from the authorization server metadata document at
|
|
125
|
+
* `metadataUrl`. Refuses — naming the switch and the URL — when the document
|
|
126
|
+
* cannot be read, or carries no usable issuer.
|
|
127
|
+
*/
|
|
128
|
+
export async function fetchAuthorizationServerIssuer(metadataUrl, deps = {}) {
|
|
129
|
+
const setting = `${CLIENT_ASSERTION_AUDIENCE_ENV}=issuer (--assertion-audience issuer)`;
|
|
130
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
131
|
+
let res;
|
|
132
|
+
try {
|
|
133
|
+
res = await fetchImpl(metadataUrl, { signal: AbortSignal.timeout(15_000) });
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
throw new Error(`${setting}, but the metadata document at ${metadataUrl} could not be read: ${err?.message ?? err}`);
|
|
137
|
+
}
|
|
138
|
+
if (!res.ok)
|
|
139
|
+
throw new Error(`${setting}, but the metadata document at ${metadataUrl} returned HTTP ${res.status}`);
|
|
140
|
+
let body;
|
|
141
|
+
try {
|
|
142
|
+
body = await res.json();
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
throw new Error(`${setting}, but the metadata document at ${metadataUrl} did not return JSON`);
|
|
146
|
+
}
|
|
147
|
+
const issuer = body?.issuer;
|
|
148
|
+
if (typeof issuer !== "string" || !isUsableIssuerUrl(issuer)) {
|
|
149
|
+
throw new Error(`${setting}, but the metadata document at ${metadataUrl} has no usable issuer (got ${JSON.stringify(issuer)}).`);
|
|
150
|
+
}
|
|
151
|
+
return issuer;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Build the audience for a form. The token-endpoint form is today's shape. The
|
|
155
|
+
* issuer form reads the metadata document at `metadataUrl` and uses its
|
|
156
|
+
* `issuer` as the SOLE `aud` — never the token-endpoint URL, and never a value
|
|
157
|
+
* derived from it.
|
|
158
|
+
*/
|
|
159
|
+
export async function resolveClientAssertionAudience(params) {
|
|
160
|
+
if (params.form === "token-endpoint")
|
|
161
|
+
return { aud: params.tokenEndpoint, typ: CLIENT_ASSERTION_TYP_JWT };
|
|
162
|
+
if (!params.metadataUrl) {
|
|
163
|
+
throw new Error(`${CLIENT_ASSERTION_AUDIENCE_ENV}=issuer (--assertion-audience issuer) needs the authorization server's ` +
|
|
164
|
+
`metadata document: pass --issuer (or set FLAIR_MCP_ISSUER/FLAIR_PUBLIC_URL).`);
|
|
165
|
+
}
|
|
166
|
+
const issuer = await fetchAuthorizationServerIssuer(params.metadataUrl, params.fetchImpl ? { fetchImpl: params.fetchImpl } : {});
|
|
167
|
+
return { aud: issuer, typ: CLIENT_ASSERTION_TYP_CLIENT_AUTHENTICATION };
|
|
168
|
+
}
|
|
68
169
|
// ─── Key loading ────────────────────────────────────────────────────────────
|
|
69
170
|
/**
|
|
70
171
|
* Locate an agent's private key file. Search order mirrors `resolveKeyPath`
|
|
@@ -150,10 +251,13 @@ export function signClientAssertion(params) {
|
|
|
150
251
|
const iat = Math.floor(params.nowSeconds ?? Date.now() / 1000);
|
|
151
252
|
const exp = iat + expiresIn;
|
|
152
253
|
const jti = params.jti ?? randomUUID();
|
|
153
|
-
// header.typ is optional per RFC 7515 §4.1.9, but
|
|
154
|
-
// present
|
|
155
|
-
|
|
156
|
-
|
|
254
|
+
// header.typ is optional per RFC 7515 §4.1.9, but the verifier checks it
|
|
255
|
+
// when present — include it: "JWT" for the token-endpoint form (what the
|
|
256
|
+
// released verifier accepts when present), and "client-authentication+jwt" (RFC
|
|
257
|
+
// 7523bis) for the issuer form.
|
|
258
|
+
const audience = params.audience ?? { aud: tokenEndpoint, typ: CLIENT_ASSERTION_TYP_JWT };
|
|
259
|
+
const header = { alg: "EdDSA", typ: audience.typ };
|
|
260
|
+
const claims = { iss: clientId, sub: clientId, aud: audience.aud, exp, iat, jti };
|
|
157
261
|
const headerB64 = base64urlJson(header);
|
|
158
262
|
const payloadB64 = base64urlJson(claims);
|
|
159
263
|
const signingInput = `${headerB64}.${payloadB64}`;
|
|
@@ -319,6 +423,7 @@ export async function getMcpAccessToken(params) {
|
|
|
319
423
|
tokenEndpoint: params.tokenEndpoint,
|
|
320
424
|
privateKey: params.privateKey,
|
|
321
425
|
expiresInSeconds: params.expiresInSeconds,
|
|
426
|
+
audience: params.audience,
|
|
322
427
|
});
|
|
323
428
|
const form = buildTokenRequestForm({ clientId: params.clientId, assertion, resource: params.resource });
|
|
324
429
|
const token = await requestMcpAccessToken(form, params.tokenEndpoint, {
|
|
@@ -333,12 +438,7 @@ export async function getMcpAccessToken(params) {
|
|
|
333
438
|
}
|
|
334
439
|
// ─── Convenience defaults (this Flair instance's own /mcp + oauth surface) ──
|
|
335
440
|
//
|
|
336
|
-
//
|
|
337
|
-
// defaults. Duplicated here (rather than imported) because src/ (this CLI's
|
|
338
|
-
// build root, tsconfig.cli.json) and resources/ (tsconfig.json) are separate
|
|
339
|
-
// TypeScript compilation roots with no existing cross-import in this repo —
|
|
340
|
-
// keep both in sync if either changes. These are only DEFAULTS: every value
|
|
341
|
-
// is overridable via CLI flags, since an agent may authenticate to a
|
|
441
|
+
// Each default is overridable via CLI flags, since an agent may authenticate to a
|
|
342
442
|
// DIFFERENT Harper MCP server than the one running this CLI (identity is
|
|
343
443
|
// portable — the whole point of the design, see
|
|
344
444
|
// ~/ops/FLAIR-AGENT-AUTH-CONSUMER-SPEC.md "Wiring / usage").
|