@tpsdev-ai/flair 0.59.0 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/SECURITY.md +49 -5
- package/config.yaml +23 -12
- package/dist/bridges/builtins/mem0.js +63 -28
- package/dist/bridges/runtime/execute.js +1 -0
- package/dist/bridges/runtime/import-runner.js +12 -0
- package/dist/bridges/runtime/load-plugin.js +42 -13
- package/dist/bridges/runtime/yaml-loader.js +4 -2
- package/dist/bridges/types.js +1 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1054 -367
- package/dist/commands/agent.js +233 -90
- package/dist/commands/attention.js +4 -3
- package/dist/commands/backup-approval.js +218 -0
- package/dist/commands/backup.js +211 -39
- package/dist/commands/bootstrap.js +3 -1
- package/dist/commands/bridge.js +172 -119
- package/dist/commands/deploy.js +71 -8
- package/dist/commands/doctor.js +711 -146
- package/dist/commands/export.js +57 -23
- package/dist/commands/federation.js +787 -82
- package/dist/commands/grant.js +5 -6
- package/dist/commands/help.js +25 -0
- package/dist/commands/hook.js +155 -2
- package/dist/commands/idp.js +27 -19
- package/dist/commands/import.js +136 -20
- package/dist/commands/init.js +469 -200
- package/dist/commands/integrity.js +387 -0
- package/dist/commands/keys.js +247 -58
- package/dist/commands/mcp.js +108 -52
- package/dist/commands/memory.js +203 -35
- package/dist/commands/migrate-harness-memory.js +3 -2
- package/dist/commands/orgevent.js +51 -47
- package/dist/commands/presence.js +4 -3
- package/dist/commands/principal.js +428 -62
- package/dist/commands/quality.js +13 -1
- package/dist/commands/reembed.js +141 -17
- package/dist/commands/relationship.js +2 -1
- package/dist/commands/rem.js +537 -80
- package/dist/commands/restore.js +413 -65
- package/dist/commands/search.js +6 -4
- package/dist/commands/service.js +235 -40
- package/dist/commands/soul.js +4 -9
- package/dist/commands/status.js +94 -38
- package/dist/commands/test.js +7 -3
- package/dist/commands/upgrade.js +77 -23
- package/dist/commands/workspace.js +13 -11
- package/dist/component-env.js +31 -41
- package/dist/deploy.js +136 -4
- package/dist/doctor-client.js +534 -92
- package/dist/engine-version-contract.js +1 -0
- package/dist/engine-version.js +23 -3
- package/dist/fabric-upgrade.js +12 -2
- package/dist/federation/presence-send.js +139 -0
- package/dist/federation-verify.js +273 -180
- package/dist/hook-install.js +670 -6
- package/dist/install/clients.js +171 -35
- package/dist/keystore.js +72 -0
- package/dist/lib/action-recall-runtime.js +328 -0
- package/dist/lib/agent-home-fields.js +132 -0
- package/dist/lib/agent-home.js +427 -0
- package/dist/lib/agent-id-rule.js +88 -0
- package/dist/lib/agent-roster.js +33 -0
- package/dist/lib/atomic-write.js +1 -1
- package/dist/lib/auth-resolve.js +1069 -60
- package/dist/lib/backup-approval-store.js +612 -0
- package/dist/lib/backup-artifact.js +343 -0
- package/dist/lib/capture-runtime.js +124 -0
- package/dist/lib/config-critical-section.js +212 -25
- package/dist/lib/connect-phase.js +33 -0
- package/dist/lib/credential-status-rule.js +78 -0
- package/dist/lib/daemon-liveness.js +43 -11
- package/dist/lib/doctor-run.js +75 -37
- package/dist/lib/durability-copy.js +10 -0
- package/dist/lib/embedding-space-doctor.js +75 -0
- package/dist/lib/federation-pair-access.js +4 -0
- package/dist/lib/federation-retry-queue.js +283 -0
- package/dist/lib/federation-retry-row.js +101 -0
- package/dist/lib/flair-agent-role.js +4 -0
- package/dist/lib/hub-url.js +12 -0
- package/dist/lib/init-admin-pass.js +88 -12
- package/dist/lib/init-listener-environ.js +1 -2
- package/dist/lib/init-occupied-listener.js +15 -111
- package/dist/lib/init-spawn-attribution.js +73 -0
- package/dist/lib/init-tcp-probe.js +33 -0
- package/dist/lib/instance-identity-row.js +20 -0
- package/dist/lib/instruction-event-contract.js +17 -0
- package/dist/lib/instruction-event-doctor.js +102 -0
- package/dist/lib/launchd-management.js +38 -3
- package/dist/lib/listener-table.js +268 -0
- package/dist/lib/local-delete-instance.js +27 -0
- package/dist/lib/local-instance-pair.js +256 -0
- package/dist/lib/mcp-enable.js +1182 -341
- package/dist/lib/mcp-oauth-env-core.js +133 -0
- package/dist/lib/mcp-oauth-env.js +125 -0
- package/dist/lib/mcp-spec.js +17 -1
- package/dist/lib/memory-attributes.js +4 -0
- package/dist/lib/memory-id-policy.js +46 -0
- package/dist/lib/memory-integrity.js +309 -0
- package/dist/lib/memory-provider-timeout.js +33 -0
- package/dist/lib/memory-purge-response.js +20 -0
- package/dist/lib/mqtt-config.js +132 -0
- package/dist/lib/mqtt-listener-doctor.js +104 -0
- package/dist/lib/open-live-row.js +20 -0
- package/dist/lib/ops-table-count.js +25 -0
- package/dist/lib/ops-url.js +52 -0
- package/dist/lib/owned-pins.js +67 -10
- package/dist/lib/peer-status.js +27 -0
- package/dist/lib/principal-status.js +29 -0
- package/dist/lib/process-start-time.js +76 -12
- package/dist/lib/promotion-content-hash.js +20 -0
- package/dist/lib/proto-safe-record.js +42 -0
- package/dist/lib/record-id-path.js +25 -0
- package/dist/lib/reembed-server-support.js +66 -0
- package/dist/lib/restore-archive.js +760 -0
- package/dist/lib/restore-verify.js +104 -0
- package/dist/lib/scheduler-platform.js +10 -6
- package/dist/lib/secret-keys.js +25 -0
- package/dist/lib/secrets-push.js +50 -22
- package/dist/lib/skill-assignment-preflight.js +159 -0
- package/dist/lib/skill-seed-pending.js +24 -0
- package/dist/lib/skill-seed.js +448 -0
- package/dist/lib/skill-tombstone-doctor.js +133 -0
- package/dist/lib/soul-scope-fields.js +26 -0
- package/dist/lib/stop-start-recovery.js +54 -0
- package/dist/lib/uninstall-purge.js +266 -11
- package/dist/lib/upgrade-prior-liveness.js +2 -1
- package/dist/lib/using-flair-skill.js +87 -0
- package/dist/mcp-client-assertion.js +114 -14
- package/dist/memory-id-checkpoint.js +347 -0
- package/dist/memory-node-compare.js +207 -0
- package/dist/mqtt-off-deploy.js +550 -0
- package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
- package/dist/rem/promote-policy.js +3 -3
- package/dist/rem/restore.js +303 -26
- package/dist/rem/runner.js +317 -96
- package/dist/rem/scheduler.js +21 -4
- package/dist/resources/A2AAdapter.js +131 -178
- package/dist/resources/AdminMemory.js +2 -0
- package/dist/resources/AdminMemoryCandidates.js +116 -0
- package/dist/resources/Agent.js +463 -33
- package/dist/resources/AgentCard.js +4 -0
- package/dist/resources/AgentKeyRegister.js +248 -0
- package/dist/resources/AgentKeyRegistration.js +28 -0
- package/dist/resources/AgentReadPosition.js +7 -0
- package/dist/resources/AgentSeed.js +238 -107
- package/dist/resources/AutoPromoteCandidates.js +413 -78
- package/dist/resources/Credential.js +183 -10
- package/dist/resources/Federation.js +915 -145
- package/dist/resources/FederationRetry.js +140 -0
- package/dist/resources/InstructionVersion.js +220 -0
- package/dist/resources/Integration.js +300 -25
- package/dist/resources/MCPClientMetadata.js +6 -3
- package/dist/resources/Memory.js +3300 -758
- package/dist/resources/MemoryArchive.js +171 -53
- package/dist/resources/MemoryBootstrap.js +122 -61
- package/dist/resources/MemoryCandidate.js +37 -3
- package/dist/resources/MemoryConsolidate.js +2 -2
- package/dist/resources/MemoryDedupStats.js +30 -1
- package/dist/resources/MemoryFeed.js +545 -50
- package/dist/resources/MemoryGrant.js +21 -1
- package/dist/resources/MemoryMaintenance.js +368 -46
- package/dist/resources/MemoryPurge.js +348 -0
- package/dist/resources/MemoryReflect.js +71 -12
- package/dist/resources/MemoryReindex.js +111 -17
- package/dist/resources/Message.js +16 -5
- package/dist/resources/OAuth.js +342 -48
- package/dist/resources/OAuthClient.js +1 -1
- package/dist/resources/OrgEvent.js +405 -19
- package/dist/resources/OrgEventCatchup.js +7 -0
- package/dist/resources/OrgEventMaintenance.js +48 -10
- package/dist/resources/PairingToken.js +1 -1
- package/dist/resources/Presence.js +166 -51
- package/dist/resources/PromoteMemoryCandidate.js +24 -7
- package/dist/resources/RecordUsage.js +2 -4
- package/dist/resources/Relationship.js +23 -0
- package/dist/resources/SemanticSearch.js +63 -32
- package/dist/resources/Soul.js +191 -76
- package/dist/resources/SoulFeed.js +2 -1
- package/dist/resources/TeamDirectory.js +35 -0
- package/dist/resources/WorkspaceLatest.js +4 -0
- package/dist/resources/WorkspaceState.js +21 -1
- package/dist/resources/XAA.js +207 -63
- package/dist/resources/a2a-stream.js +313 -0
- package/dist/resources/a2a-url.js +4 -5
- package/dist/resources/agent-admin.js +10 -1
- package/dist/resources/agent-auth.js +138 -83
- package/dist/resources/agent-read-position-lib.js +4 -1
- package/dist/resources/agent-status-guard.js +9 -7
- package/dist/resources/auth-middleware.js +534 -171
- package/dist/resources/authority-field-guard.js +9 -11
- package/dist/resources/auto-promote-lib.js +8 -5
- package/dist/resources/basic-auth-header.js +21 -0
- package/dist/resources/bm25-index-service.js +4 -5
- package/dist/resources/dedup-cluster.js +6 -1
- package/dist/resources/ed25519-auth.js +1 -1
- package/dist/resources/embedding-space-guard.js +366 -72
- package/dist/resources/embeddings-provider.js +25 -0
- package/dist/resources/engine-version-stamp-boot.js +53 -0
- package/dist/resources/federation-classify.js +161 -0
- package/dist/resources/federation-crypto.js +5 -5
- package/dist/resources/federation-retry-lib.js +60 -0
- package/dist/resources/grant-visibility.js +135 -0
- package/dist/resources/harper-routing.js +83 -0
- package/dist/resources/health.js +163 -22
- package/dist/resources/host-pointer/registry.js +29 -0
- package/dist/resources/host-pointer-adapter.js +1 -0
- package/dist/resources/host-source-citation.js +49 -0
- package/dist/resources/host-source-visibility.js +7 -6
- package/dist/resources/in-process.js +24 -22
- package/dist/resources/instance-create-lock.js +17 -0
- package/dist/resources/instance-identity.js +23 -0
- package/dist/resources/instruction-version-record.js +587 -0
- package/dist/resources/internal-authority.js +13 -0
- package/dist/resources/key-lock.js +3 -3
- package/dist/resources/mcp-handler.js +141 -19
- package/dist/resources/mcp-oauth-env-guard.js +36 -0
- package/dist/resources/mcp-oauth-flag.js +12 -14
- package/dist/resources/mcp-oauth.js +450 -56
- package/dist/resources/mcp-tool-arguments.js +4 -6
- package/dist/resources/mcp-tools.js +187 -40
- package/dist/resources/memory-bootstrap-lib.js +5 -15
- package/dist/resources/memory-declared-attributes.js +4 -1
- package/dist/resources/memory-deletion-history.js +55 -0
- package/dist/resources/memory-durability.js +54 -0
- package/dist/resources/memory-embed-hash.js +144 -0
- package/dist/resources/memory-embedding-value.js +8 -0
- package/dist/resources/memory-host-source.js +32 -7
- package/dist/resources/memory-id-guard.js +75 -0
- package/dist/resources/memory-redaction.js +236 -0
- package/dist/resources/memory-reflect-lib.js +1 -1
- package/dist/resources/memory-supplied-vector.js +72 -0
- package/dist/resources/memory-write-lock.js +489 -0
- package/dist/resources/memory-write-response.js +139 -0
- package/dist/resources/memory-write-transaction.js +243 -0
- package/dist/resources/migration-boot.js +17 -12
- package/dist/resources/migrations/boot-progress.js +25 -0
- package/dist/resources/migrations/data-dir.js +90 -19
- package/dist/resources/migrations/dir-safety.js +61 -3
- package/dist/resources/migrations/embedding-stamp.js +462 -82
- package/dist/resources/migrations/export.js +1 -0
- package/dist/resources/migrations/graph-heal.js +27 -27
- package/dist/resources/migrations/ledger.js +5 -2
- package/dist/resources/migrations/lock.js +870 -77
- package/dist/resources/migrations/progress.js +71 -10
- package/dist/resources/migrations/runner.js +212 -18
- package/dist/resources/migrations/snapshot.js +1 -0
- package/dist/resources/migrations/source-fields.js +5 -5
- package/dist/resources/migrations/state.js +36 -1
- package/dist/resources/migrations/status.js +2 -13
- package/dist/resources/migrations/synthetic-test-migration.js +81 -12
- package/dist/resources/migrations/types.js +26 -1
- package/dist/resources/migrations/visibility-backfill.js +113 -72
- package/dist/resources/oauth-discovery.js +0 -5
- package/dist/resources/object-body-guard.js +40 -0
- package/dist/resources/org-event-created-at.js +133 -0
- package/dist/resources/org-event-reserved.js +92 -0
- package/dist/resources/org-event-write-fault.js +19 -0
- package/dist/resources/originator-instance.js +39 -7
- package/dist/resources/owner-delete-recheck.js +78 -0
- package/dist/resources/owner-field-guard.js +39 -5
- package/dist/resources/owner-scoped-write-policy.js +256 -0
- package/dist/resources/owner-scoped-writes.js +89 -0
- package/dist/resources/path-id-rewrite.js +76 -0
- package/dist/resources/presence-internal.js +5 -5
- package/dist/resources/presence-receipt.js +236 -0
- package/dist/resources/promotion-binding.js +43 -0
- package/dist/resources/promotion-stamp.js +143 -17
- package/dist/resources/protocol-context.js +189 -0
- package/dist/resources/record-owner-guard.js +74 -26
- package/dist/resources/record-type-kit.js +2 -4
- package/dist/resources/record-types.js +1 -1
- package/dist/resources/relay-ops.js +78 -6
- package/dist/resources/replay-store.js +96 -19
- package/dist/resources/request-target.js +163 -0
- package/dist/resources/request-transaction.js +46 -1
- package/dist/resources/search-readiness.js +4 -1
- package/dist/resources/seed-ids.js +9 -0
- package/dist/resources/seed-reservation.js +116 -0
- package/dist/resources/semantic-retrieval-core.js +27 -15
- package/dist/resources/server-row-validation.js +71 -0
- package/dist/resources/skill-manifest.js +24 -14
- package/dist/resources/skill-subject.js +53 -0
- package/dist/resources/skill-version-write.js +789 -0
- package/dist/resources/skill-write-fault.js +20 -0
- package/dist/resources/skill-write-policy.js +68 -0
- package/dist/resources/skill-write.js +67 -7
- package/dist/resources/soul-write-policy.js +26 -7
- package/dist/resources/table-helpers.js +10 -7
- package/dist/resources/table-subscription-policy.js +518 -85
- package/dist/resources/table-subscriptions.js +20 -7
- package/dist/resources/team-directory.js +248 -0
- package/dist/resources/tool-descriptors/index.js +69 -8
- package/dist/resources/txn-pause-point.js +235 -0
- package/dist/resources/usage-recording.js +186 -70
- package/dist/resources/write-back.js +383 -0
- package/dist/resources/write-journal-context.js +5 -0
- package/dist/resources/write-journal.js +289 -0
- package/dist/src/engine-version-contract.js +1 -0
- package/dist/src/keystore.js +72 -0
- package/dist/src/lib/agent-home-fields.js +132 -0
- package/dist/src/lib/agent-id-rule.js +88 -0
- package/dist/src/lib/agent-key-register.js +80 -0
- package/dist/src/lib/atomic-write.js +256 -0
- package/dist/src/lib/credential-status-rule.js +78 -0
- package/dist/src/lib/expired-by-agent.js +37 -0
- package/dist/src/lib/federation-retry-row.js +101 -0
- package/dist/src/lib/flair-agent-role.js +4 -0
- package/dist/src/lib/idp-mapping-rule.js +81 -0
- package/dist/src/lib/instance-identity-row.js +20 -0
- package/dist/src/lib/instruction-event-contract.js +17 -0
- package/dist/src/lib/mcp-oauth-env-core.js +133 -0
- package/dist/src/lib/memory-attributes.js +4 -0
- package/dist/src/lib/memory-id-policy.js +46 -0
- package/dist/src/lib/open-live-row.js +20 -0
- package/dist/src/lib/principal-status.js +29 -0
- package/dist/src/lib/proto-safe-record.js +42 -0
- package/dist/src/lib/reembed-server-support.js +66 -0
- package/dist/src/lib/scheduler-platform.js +10 -6
- package/dist/src/lib/soul-scope-fields.js +26 -0
- package/dist/src/lib/systemd-agent-id.js +38 -0
- package/dist/src/rem/promote-policy.js +3 -3
- package/dist/src/rem/scheduler.js +21 -4
- package/docs/access-control.md +164 -54
- package/docs/api-reference.md +165 -32
- package/docs/auth.md +46 -30
- package/docs/bridges.md +11 -0
- package/docs/claude-code.md +37 -7
- package/docs/deploying-on-fabric.md +178 -0
- package/docs/deployment.md +19 -0
- package/docs/embedding-in-a-harper-app.md +17 -15
- package/docs/entity-vocabulary.md +3 -6
- package/docs/federation.md +61 -3
- package/docs/hosted-on-fabric.md +43 -36
- package/docs/integrations.md +14 -14
- package/docs/mcp-clients.md +12 -7
- package/docs/n8n.md +17 -11
- package/docs/notes/mcp-agent-auth-consumer.md +16 -13
- package/docs/notes/mcp-oauth-model2.md +40 -11
- package/docs/openclaw.md +1 -1
- package/docs/quickstart-fabric.md +4 -4
- package/docs/quickstart.md +12 -4
- package/docs/releasing.md +25 -19
- package/docs/rem.md +32 -3
- package/docs/secrets-and-keys.md +21 -4
- package/docs/standalone-local.md +30 -2
- package/docs/supply-chain-policy.md +34 -16
- package/docs/the-team.md +2 -2
- package/docs/upgrade.md +46 -6
- package/package.json +24 -12
- package/schemas/agent-key-registration.graphql +22 -0
- package/schemas/agent.graphql +6 -3
- package/schemas/federation.graphql +41 -1
- package/schemas/memory.graphql +91 -7
- package/schemas/oauth.graphql +19 -0
- package/schemas/schema.graphql +2 -0
- package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
- package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
- package/dist/resources/soul-adk-guard.js +0 -68
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* listener-table.ts — the processes the OS lists as LISTENING on a TCP port
|
|
3
|
+
* (flair#2526). The answer comes from the kernel's socket table, never from
|
|
4
|
+
* anything a listener says about itself and never from a PID file.
|
|
5
|
+
*
|
|
6
|
+
* Per platform:
|
|
7
|
+
* - Linux: the LISTEN rows (state `0A`) of `/proc/net/tcp` and
|
|
8
|
+
* `/proc/net/tcp6` whose local port is `port` name socket inodes; the
|
|
9
|
+
* processes holding those inodes are the ones whose `/proc/<pid>/fd/*` link
|
|
10
|
+
* reads `socket:[<inode>]`. No external tool.
|
|
11
|
+
* - every other platform (macOS included): `lsof -nP -iTCP:<port>
|
|
12
|
+
* -sTCP:LISTEN -Fp`, whose `p<pid>` lines name the processes.
|
|
13
|
+
*
|
|
14
|
+
* Its limit: both read only what this CLI can see. Linux `/proc/net` lists
|
|
15
|
+
* this process's network namespace, and `/proc/<pid>/fd` of a process this
|
|
16
|
+
* user may not inspect is unreadable; macOS lsof lists only processes this
|
|
17
|
+
* user may inspect. A listener outside that view (in another network
|
|
18
|
+
* namespace or container, or owned by a user this CLI may not inspect) is
|
|
19
|
+
* absent from the table or has no owner the table can name, and a port
|
|
20
|
+
* reached through a forward is held by the forwarder (docker-proxy, ssh -L).
|
|
21
|
+
* Callers refuse a listener they cannot see, and a forwarder whose observed
|
|
22
|
+
* command line does not match Harper's. A CLI inside the same container, or
|
|
23
|
+
* allowed to inspect that user's processes, sees the listener.
|
|
24
|
+
*/
|
|
25
|
+
import { execFileSync } from "node:child_process";
|
|
26
|
+
import { readdirSync, readFileSync, readlinkSync } from "node:fs";
|
|
27
|
+
import { parseProcStatStartTime } from "./daemon-liveness.js";
|
|
28
|
+
/** The lsof command line the non-Linux reader runs for `port`. */
|
|
29
|
+
export function lsofListenerArgs(port) {
|
|
30
|
+
return ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-Fp"];
|
|
31
|
+
}
|
|
32
|
+
/** What to do when the table does not show this CLI the listener, or shows a forwarder. */
|
|
33
|
+
export const LISTENER_VISIBILITY_REMEDY = "Run this command where it can see Harper's own listener: on the host (or inside the container) where Harper "
|
|
34
|
+
+ "runs, as a user allowed to inspect Harper's process. A port held by a forwarder whose command line does not "
|
|
35
|
+
+ "match Harper's (such as docker-proxy or ssh -L) is refused.";
|
|
36
|
+
/**
|
|
37
|
+
* Parse `lsof -Fp` output into the distinct listening pids, sorted. Only `p`
|
|
38
|
+
* lines carry a pid; lsof's other field lines (`f<fd>` and the like) are
|
|
39
|
+
* ignored. A `p` line that is not a positive integer makes the whole output
|
|
40
|
+
* unusable (null): the caller refuses rather than guessing.
|
|
41
|
+
*/
|
|
42
|
+
export function parseLsofListenerPids(output) {
|
|
43
|
+
const pids = new Set();
|
|
44
|
+
for (const raw of output.split("\n")) {
|
|
45
|
+
const line = raw.trim();
|
|
46
|
+
if (!line.startsWith("p"))
|
|
47
|
+
continue;
|
|
48
|
+
const pid = Number(line.slice(1));
|
|
49
|
+
if (!/^p[0-9]+$/.test(line) || !Number.isSafeInteger(pid) || pid <= 0)
|
|
50
|
+
return null;
|
|
51
|
+
pids.add(pid);
|
|
52
|
+
}
|
|
53
|
+
return [...pids].sort((a, b) => a - b);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Parse one `/proc/net/tcp` or `/proc/net/tcp6` table into the socket inodes
|
|
57
|
+
* LISTENING (state `0A`) on `port`. The local address is `<hex addr>:<hex
|
|
58
|
+
* port>`; inode 0 (no owning socket) is skipped. A data row with fewer than 10
|
|
59
|
+
* columns makes the table unusable (null).
|
|
60
|
+
*/
|
|
61
|
+
export function parseProcNetTcpListenInodes(table, port) {
|
|
62
|
+
const inodes = [];
|
|
63
|
+
const rows = table.split("\n").slice(1);
|
|
64
|
+
for (const row of rows) {
|
|
65
|
+
const cols = row.trim().split(/\s+/);
|
|
66
|
+
if (cols.length === 1 && cols[0] === "")
|
|
67
|
+
continue;
|
|
68
|
+
if (cols.length < 10)
|
|
69
|
+
return null;
|
|
70
|
+
const local = cols[1];
|
|
71
|
+
const colon = local.lastIndexOf(":");
|
|
72
|
+
if (colon < 0)
|
|
73
|
+
return null;
|
|
74
|
+
const localPort = Number.parseInt(local.slice(colon + 1), 16);
|
|
75
|
+
if (cols[3] !== "0A" || localPort !== port)
|
|
76
|
+
continue;
|
|
77
|
+
if (!/^[0-9]+$/.test(cols[9]))
|
|
78
|
+
return null;
|
|
79
|
+
if (cols[9] !== "0")
|
|
80
|
+
inodes.push(cols[9]);
|
|
81
|
+
}
|
|
82
|
+
return inodes;
|
|
83
|
+
}
|
|
84
|
+
const DEFAULT_IO = {
|
|
85
|
+
platform: process.platform,
|
|
86
|
+
runLsof: (args) => execFileSync("lsof", args, { encoding: "utf-8", timeout: 5_000, killSignal: "SIGKILL", stdio: ["ignore", "pipe", "ignore"] }),
|
|
87
|
+
runPs: (args) => execFileSync("ps", args, {
|
|
88
|
+
encoding: "utf-8",
|
|
89
|
+
timeout: 5_000,
|
|
90
|
+
killSignal: "SIGKILL",
|
|
91
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
92
|
+
env: { ...process.env, LC_ALL: "C" },
|
|
93
|
+
}),
|
|
94
|
+
readFile: (path) => readFileSync(path, "utf-8"),
|
|
95
|
+
readDir: (path) => readdirSync(path),
|
|
96
|
+
readLink: (path) => readlinkSync(path),
|
|
97
|
+
};
|
|
98
|
+
/** The processes the OS lists as LISTENING on TCP `port` (see the module comment). */
|
|
99
|
+
export function readTcpListenerPids(port, io = {}) {
|
|
100
|
+
const full = { ...DEFAULT_IO, ...io };
|
|
101
|
+
return full.platform === "linux" ? readFromProc(port, full) : readFromLsof(port, full);
|
|
102
|
+
}
|
|
103
|
+
function readFromLsof(port, io) {
|
|
104
|
+
const command = `lsof ${lsofListenerArgs(port).join(" ")}`;
|
|
105
|
+
let output;
|
|
106
|
+
try {
|
|
107
|
+
output = io.runLsof(lsofListenerArgs(port));
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
if (err?.code === "ENOENT") {
|
|
111
|
+
return {
|
|
112
|
+
kind: "unavailable",
|
|
113
|
+
reason: `lsof is not installed or not on PATH, and this check reads the listener table with \`${command}\``,
|
|
114
|
+
remedy: "Install lsof or put it on PATH (macOS ships it as /usr/sbin/lsof), then re-run this command on the host where Harper runs.",
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// lsof exits 1 with no output when no socket matches.
|
|
118
|
+
if (err?.status === 1 && String(err?.stdout ?? "").trim() === "")
|
|
119
|
+
return { kind: "listeners", pids: [] };
|
|
120
|
+
const how = err?.signal ? `was stopped by ${err.signal}` : `exited ${err?.status ?? err?.code ?? "abnormally"}`;
|
|
121
|
+
return {
|
|
122
|
+
kind: "unavailable",
|
|
123
|
+
reason: `\`${command}\` ${how}`,
|
|
124
|
+
remedy: `Run \`${command}\` yourself to see why it fails, fix that, and re-run this command.`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const pids = parseLsofListenerPids(output);
|
|
128
|
+
if (pids === null) {
|
|
129
|
+
return { kind: "unavailable", reason: `\`${command}\` printed a pid line that is not a process id`, remedy: `Run \`${command}\` yourself and check its output.` };
|
|
130
|
+
}
|
|
131
|
+
return { kind: "listeners", pids };
|
|
132
|
+
}
|
|
133
|
+
function readFromProc(port, io) {
|
|
134
|
+
const inodes = new Set();
|
|
135
|
+
for (const table of ["/proc/net/tcp", "/proc/net/tcp6"]) {
|
|
136
|
+
let text;
|
|
137
|
+
try {
|
|
138
|
+
text = io.readFile(table);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
// A host with IPv6 disabled has no tcp6 table; the IPv4 table is required.
|
|
142
|
+
if (table === "/proc/net/tcp6" && err?.code === "ENOENT")
|
|
143
|
+
continue;
|
|
144
|
+
return {
|
|
145
|
+
kind: "unavailable",
|
|
146
|
+
reason: `this check reads the listener table from ${table}, which cannot be read (${err?.code ?? err?.message})`,
|
|
147
|
+
remedy: "Run this command on a Linux host with /proc mounted, as the user that runs Harper.",
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const found = parseProcNetTcpListenInodes(text, port);
|
|
151
|
+
if (found === null) {
|
|
152
|
+
return { kind: "unavailable", reason: `${table} has a row this check cannot parse`, remedy: `Check ${table}; this check reads its LISTEN rows.` };
|
|
153
|
+
}
|
|
154
|
+
for (const inode of found)
|
|
155
|
+
inodes.add(inode);
|
|
156
|
+
}
|
|
157
|
+
if (inodes.size === 0)
|
|
158
|
+
return { kind: "listeners", pids: [] };
|
|
159
|
+
const owners = new Map();
|
|
160
|
+
let entries;
|
|
161
|
+
try {
|
|
162
|
+
entries = io.readDir("/proc");
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
return {
|
|
166
|
+
kind: "unavailable",
|
|
167
|
+
reason: `this check finds socket owners under /proc, which cannot be listed (${err?.code ?? err?.message})`,
|
|
168
|
+
remedy: "Run this command on a Linux host with /proc mounted, as the user that runs Harper.",
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
for (const entry of entries) {
|
|
172
|
+
if (!/^[0-9]+$/.test(entry))
|
|
173
|
+
continue;
|
|
174
|
+
let fds;
|
|
175
|
+
try {
|
|
176
|
+
fds = io.readDir(`/proc/${entry}/fd`);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
continue; // a process this user may not inspect, or one that exited: its sockets stay unowned below
|
|
180
|
+
}
|
|
181
|
+
for (const fd of fds) {
|
|
182
|
+
let target;
|
|
183
|
+
try {
|
|
184
|
+
target = io.readLink(`/proc/${entry}/fd/${fd}`);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const match = /^socket:\[([0-9]+)\]$/.exec(target);
|
|
190
|
+
if (!match || !inodes.has(match[1]))
|
|
191
|
+
continue;
|
|
192
|
+
const set = owners.get(match[1]) ?? new Set();
|
|
193
|
+
set.add(Number(entry));
|
|
194
|
+
owners.set(match[1], set);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const unowned = [...inodes].filter((inode) => !owners.has(inode));
|
|
198
|
+
if (unowned.length > 0) {
|
|
199
|
+
return {
|
|
200
|
+
kind: "unavailable",
|
|
201
|
+
reason: `TCP port ${port} has a listening socket (inode ${unowned.join(", ")}) whose owner could not be found or inspected in /proc/<pid>/fd (the process may have exited, or this user may not inspect it)`,
|
|
202
|
+
remedy: LISTENER_VISIBILITY_REMEDY,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const pids = new Set();
|
|
206
|
+
for (const set of owners.values())
|
|
207
|
+
for (const pid of set)
|
|
208
|
+
pids.add(pid);
|
|
209
|
+
return { kind: "listeners", pids: [...pids].sort((a, b) => a - b) };
|
|
210
|
+
}
|
|
211
|
+
/** The ps command line the non-Linux birth reader runs for `pid`. */
|
|
212
|
+
export function psStartArgs(pid) {
|
|
213
|
+
return ["-o", "lstart=", "-p", String(pid)];
|
|
214
|
+
}
|
|
215
|
+
/** `ps -o lstart=` under `LC_ALL=C`: `Tue Oct 13 06:46:06 2026`, with a space-padded day. */
|
|
216
|
+
const LSTART = /^[A-Z][a-z]{2} [A-Z][a-z]{2} [ 0-9][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}$/;
|
|
217
|
+
/** Parse `ps -o lstart=` output into a start time, or null when it is not one. */
|
|
218
|
+
export function parsePsLstart(output) {
|
|
219
|
+
const line = output.trim();
|
|
220
|
+
return LSTART.test(line) ? line : null;
|
|
221
|
+
}
|
|
222
|
+
/** The birth identity of `pid` (see {@link BirthRead}). */
|
|
223
|
+
export function readProcessBirth(pid, io = {}) {
|
|
224
|
+
const full = { ...DEFAULT_IO, ...io };
|
|
225
|
+
if (full.platform === "linux") {
|
|
226
|
+
const path = `/proc/${pid}/stat`;
|
|
227
|
+
let stat;
|
|
228
|
+
try {
|
|
229
|
+
stat = full.readFile(path);
|
|
230
|
+
}
|
|
231
|
+
catch (err) {
|
|
232
|
+
return {
|
|
233
|
+
kind: "unavailable",
|
|
234
|
+
reason: `the start time of process ${pid} cannot be read from ${path} (${err?.code ?? err?.message}); it may have exited`,
|
|
235
|
+
remedy: "Re-run this command once the instance is running and not restarting.",
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
const ticks = parseProcStatStartTime(stat);
|
|
239
|
+
if (ticks === null) {
|
|
240
|
+
return { kind: "unavailable", reason: `${path} has no start time this check can read`, remedy: `Check ${path}; this check reads its field 22.` };
|
|
241
|
+
}
|
|
242
|
+
return { kind: "birth", id: `${pid}@${ticks}` };
|
|
243
|
+
}
|
|
244
|
+
const command = `ps ${psStartArgs(pid).join(" ")}`;
|
|
245
|
+
let output;
|
|
246
|
+
try {
|
|
247
|
+
output = full.runPs(psStartArgs(pid));
|
|
248
|
+
}
|
|
249
|
+
catch (err) {
|
|
250
|
+
if (err?.code === "ENOENT") {
|
|
251
|
+
return {
|
|
252
|
+
kind: "unavailable",
|
|
253
|
+
reason: `ps is not installed or not on PATH, and this check reads a process's start time with \`${command}\``,
|
|
254
|
+
remedy: "Put ps on PATH (macOS ships it as /bin/ps), then re-run this command.",
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
kind: "unavailable",
|
|
259
|
+
reason: `\`${command}\` found no start time (exit ${err?.status ?? err?.code ?? "abnormal"}); process ${pid} may have exited`,
|
|
260
|
+
remedy: "Re-run this command once the instance is running and not restarting.",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const started = parsePsLstart(output);
|
|
264
|
+
if (started === null) {
|
|
265
|
+
return { kind: "unavailable", reason: `\`${command}\` printed a start time this check cannot read`, remedy: `Run \`${command}\` yourself and check its output.` };
|
|
266
|
+
}
|
|
267
|
+
return { kind: "birth", id: `${pid}@${started}` };
|
|
268
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { proveLocalInstancePair } from "./local-instance-pair.js";
|
|
2
|
+
export async function resolveLocalDeleteInstance(opts, resolveOpsPort, authorization) {
|
|
3
|
+
const source = opts.opsPort !== undefined ? "--ops-port" : process.env.FLAIR_OPS_PORT ? "FLAIR_OPS_PORT" : "ops endpoint";
|
|
4
|
+
const opsPort = resolveOpsPort(opts);
|
|
5
|
+
const opsUrl = `http://127.0.0.1:${opsPort}/`;
|
|
6
|
+
// The pair check is shared with `flair doctor` (local-instance-pair.ts). It
|
|
7
|
+
// runs before this command sends anything to the HTTP port.
|
|
8
|
+
const pair = await proveLocalInstancePair(opsPort, authorization);
|
|
9
|
+
if (pair.kind !== "proven") {
|
|
10
|
+
throw new Error(`${source} ${opsPort}: the local HTTP/ops pair could not be proven (${pair.detail}); refusing deletion. ` +
|
|
11
|
+
(pair.remedy ?? deleteRemedy(source, opsPort, pair.status)));
|
|
12
|
+
}
|
|
13
|
+
const httpPort = pair.httpPort;
|
|
14
|
+
if (opts.port !== undefined && Number(opts.port) !== httpPort) {
|
|
15
|
+
throw new Error(`--port ${opts.port} does not match ${source} ${opsPort}'s HTTP endpoint ${httpPort}; refusing deletion. ` +
|
|
16
|
+
`Pass --port ${httpPort}, or omit --port, or name the ops port of the instance that serves ${opts.port}.`);
|
|
17
|
+
}
|
|
18
|
+
return { opsUrl, baseUrl: `http://127.0.0.1:${httpPort}` };
|
|
19
|
+
}
|
|
20
|
+
/** The remedy a delete refusal names when the pair check carries none. */
|
|
21
|
+
function deleteRemedy(source, opsPort, status) {
|
|
22
|
+
if (status === 401 || status === 403) {
|
|
23
|
+
return `Check that the admin credential this command uses is the one for the instance on ${source} ${opsPort}.`;
|
|
24
|
+
}
|
|
25
|
+
return "Pass --ops-port with the ops port of the instance to delete from (or set FLAIR_OPS_PORT); if that instance's "
|
|
26
|
+
+ "configuration records ports it does not serve, correct it and restart the instance.";
|
|
27
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* local-instance-pair.ts — the ONE check that pairs an operations API endpoint
|
|
3
|
+
* with an HTTP port on this host.
|
|
4
|
+
*
|
|
5
|
+
* Shared by the local delete paths (`memory hygiene --apply`, `agent remove`,
|
|
6
|
+
* via src/lib/local-delete-instance.ts, flair#2225) and by `flair doctor`'s
|
|
7
|
+
* instance reads and credentialed HTTP requests (src/cli.ts
|
|
8
|
+
* `verifyDoctorRosterInstance`, flair#2483/#2520/#2526), so the rule is
|
|
9
|
+
* written once.
|
|
10
|
+
*
|
|
11
|
+
* What it checks:
|
|
12
|
+
* 1. it asks the loopback ops endpoint on `opsPort` for Harper's
|
|
13
|
+
* `get_configuration`, and continues only when that configuration
|
|
14
|
+
* - records a usable `http.port`,
|
|
15
|
+
* - binds that HTTP port on loopback or on all interfaces (127.0.0.1,
|
|
16
|
+
* localhost, 0.0.0.0, [::], or a bare port), and
|
|
17
|
+
* - records `operationsApi.network.port` equal to `opsPort`, the port asked;
|
|
18
|
+
* 2. it reads the OS listener table (src/lib/listener-table.ts: `/proc` on
|
|
19
|
+
* Linux, `lsof` elsewhere) for `opsPort` and for that HTTP port, requires
|
|
20
|
+
* exactly one listening process on each, and reads each one's birth
|
|
21
|
+
* identity (pid plus start time: `/proc/<pid>/stat` on Linux, `ps -o
|
|
22
|
+
* lstart=` elsewhere) (flair#2526);
|
|
23
|
+
* 3. it requires each listener to have a recognized Harper command line
|
|
24
|
+
* (`isHarperProcessCommandLine`), so a forwarder whose observed command
|
|
25
|
+
* line does not match Harper's (such as docker-proxy or ssh -L) is refused;
|
|
26
|
+
* 4. it requires the two listeners to be one pid;
|
|
27
|
+
* 5. it reads both listeners and their birth identities again and requires
|
|
28
|
+
* them unchanged, so a process replaced between reads under a reused pid
|
|
29
|
+
* is refused when its start time differs. On macOS the start time has
|
|
30
|
+
* one-second resolution, so a replacement started within the same second
|
|
31
|
+
* is not told apart (a finer birth identity is a follow-up).
|
|
32
|
+
* Only then does it report the configured HTTP port. `flair doctor` requires it
|
|
33
|
+
* to equal the HTTP port doctor probed. The delete paths compare it with
|
|
34
|
+
* `--port` only when `--port` is given; otherwise they address that port.
|
|
35
|
+
*
|
|
36
|
+
* Nothing is sent to the HTTP port: the admin credential goes only to the ops
|
|
37
|
+
* endpoint the caller named. Both callers send a credential to the HTTP port
|
|
38
|
+
* only after this check proves the pair. Doctor has one exception: when no
|
|
39
|
+
* usable admin credential can run this check, and
|
|
40
|
+
* {@link proveLocalHarperListener} passes for that port's own listener, it
|
|
41
|
+
* sends that port requests with no Authorization header or an Ed25519
|
|
42
|
+
* signature, never a Basic or Bearer header
|
|
43
|
+
* (src/commands/doctor.ts `decideDoctorPairGate`, the shared resolver's
|
|
44
|
+
* `signedOnly` transport).
|
|
45
|
+
* No PID file and no process id an endpoint reports about itself is evidence
|
|
46
|
+
* (Harper's own `system_information` pid is read from its `hdb.pid`).
|
|
47
|
+
*
|
|
48
|
+
* Every other outcome is `unproven`, with a detail naming which condition
|
|
49
|
+
* failed and, for the listener-table conditions except two different pids, a
|
|
50
|
+
* `remedy`; callers supply their own remedy otherwise.
|
|
51
|
+
*
|
|
52
|
+
* Its limits: the listener table shows only what this CLI can see, so a Harper
|
|
53
|
+
* listener outside that view (another network namespace or container, or a
|
|
54
|
+
* user this CLI may not inspect) is refused, and so is a port held by a
|
|
55
|
+
* forwarder whose observed command line does not match Harper's, such as
|
|
56
|
+
* docker-proxy or ssh -L. The command-line check reads what `ps`/`/proc`
|
|
57
|
+
* report; it does not authenticate the program, so a forwarder started with a
|
|
58
|
+
* Harper-shaped command line is not told apart. The macOS start time is
|
|
59
|
+
* recorded to the second.
|
|
60
|
+
*/
|
|
61
|
+
import { harperPortValue } from "./harper-port-value.js";
|
|
62
|
+
import { isHarperProcessCommandLine } from "./daemon-liveness.js";
|
|
63
|
+
import { readProcessBirth, readTcpListenerPids, LISTENER_VISIBILITY_REMEDY } from "./listener-table.js";
|
|
64
|
+
import { defaultReadProcessCmdline } from "./upgrade-exec-path.js";
|
|
65
|
+
/** The host halves of an `http.port` binding that bind on loopback or on all interfaces. */
|
|
66
|
+
const LOCAL_HTTP_BIND_HOSTS = ["127.0.0.1", "localhost", "0.0.0.0", "[::]", ""];
|
|
67
|
+
const DEFAULT_OS_READS = {
|
|
68
|
+
listeners: (port) => readTcpListenerPids(port),
|
|
69
|
+
birth: (pid) => readProcessBirth(pid),
|
|
70
|
+
commandLine: defaultReadProcessCmdline,
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Test seam for callers that cannot pass `deps` (the CLI commands run
|
|
74
|
+
* in-process by test/unit-isolated). Production never sets it.
|
|
75
|
+
*/
|
|
76
|
+
let osReadsForTests = null;
|
|
77
|
+
export function setInstancePairOsReadsForTests(reads) {
|
|
78
|
+
osReadsForTests = reads;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Ask the loopback ops endpoint on `opsPort` for its configuration, then prove
|
|
82
|
+
* from the OS listener table that one process with a recognized Harper command
|
|
83
|
+
* line listens on both that ops port and the configured HTTP port (see the
|
|
84
|
+
* module comment). `authorization`
|
|
85
|
+
* is the full `Authorization` header value; it is sent only to `opsPort`.
|
|
86
|
+
* `status` is the HTTP status of a non-2xx answer, else null.
|
|
87
|
+
*/
|
|
88
|
+
export async function proveLocalInstancePair(opsPort, authorization, deps = {}) {
|
|
89
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
90
|
+
const os = { ...DEFAULT_OS_READS, ...osReadsForTests, ...deps.osReads };
|
|
91
|
+
const unproven = (detail, status = null, remedy) => remedy === undefined ? { kind: "unproven", status, detail } : { kind: "unproven", status, detail, remedy };
|
|
92
|
+
// 1. The ops endpoint's own configuration.
|
|
93
|
+
let response;
|
|
94
|
+
try {
|
|
95
|
+
response = await fetchImpl(`http://127.0.0.1:${opsPort}/`, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers: { "Content-Type": "application/json", Authorization: authorization },
|
|
98
|
+
body: JSON.stringify({ operation: "get_configuration" }),
|
|
99
|
+
signal: AbortSignal.timeout(10_000),
|
|
100
|
+
redirect: "error",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return unproven("the configuration request got no answer");
|
|
105
|
+
}
|
|
106
|
+
if (!response.ok)
|
|
107
|
+
return unproven(`the configuration request answered HTTP ${response.status}`, response.status);
|
|
108
|
+
let config;
|
|
109
|
+
try {
|
|
110
|
+
config = await response.json();
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return unproven("the configuration answer is not JSON");
|
|
114
|
+
}
|
|
115
|
+
const httpPort = harperPortValue(config?.http?.port);
|
|
116
|
+
if (httpPort === null)
|
|
117
|
+
return unproven("its configuration records no usable HTTP port");
|
|
118
|
+
const configuredOpsPort = harperPortValue(config?.operationsApi?.network?.port);
|
|
119
|
+
if (configuredOpsPort !== opsPort) {
|
|
120
|
+
return unproven(`its configuration records ops port ${configuredOpsPort ?? "none"}, not ${opsPort}`);
|
|
121
|
+
}
|
|
122
|
+
const binding = String(config?.http?.port ?? "");
|
|
123
|
+
const separator = binding.lastIndexOf(":");
|
|
124
|
+
const host = separator < 0 ? "127.0.0.1" : binding.slice(0, separator);
|
|
125
|
+
if (!LOCAL_HTTP_BIND_HOSTS.includes(host)) {
|
|
126
|
+
return unproven(`its configuration binds HTTP to ${host}, not loopback or all interfaces`);
|
|
127
|
+
}
|
|
128
|
+
// 2. One process listens on each port; note each one's birth identity.
|
|
129
|
+
const first = observeListeners(os, opsPort, httpPort);
|
|
130
|
+
if (!("ops" in first))
|
|
131
|
+
return first;
|
|
132
|
+
// 3. Each has a recognized Harper command line, not a forwarder's.
|
|
133
|
+
const notHarper = harperListenerRefusal(os, first.ops.pid, "ops", opsPort)
|
|
134
|
+
?? (first.http.pid === first.ops.pid ? null : harperListenerRefusal(os, first.http.pid, "HTTP", httpPort));
|
|
135
|
+
if (notHarper !== null)
|
|
136
|
+
return notHarper;
|
|
137
|
+
// 4. They are one pid.
|
|
138
|
+
if (first.ops.pid !== first.http.pid) {
|
|
139
|
+
return unproven(`the process listening on ops port ${opsPort} is ${first.ops.pid} and the process listening on HTTP port ` +
|
|
140
|
+
`${httpPort} is ${first.http.pid}, so they are not the same running instance`);
|
|
141
|
+
}
|
|
142
|
+
// 5. Nothing changed since step 2: the same pid and birth on both ports.
|
|
143
|
+
const second = observeListeners(os, opsPort, httpPort);
|
|
144
|
+
if (!("ops" in second))
|
|
145
|
+
return second;
|
|
146
|
+
for (const [label, port, before, after] of [
|
|
147
|
+
["ops", opsPort, first.ops, second.ops],
|
|
148
|
+
["HTTP", httpPort, first.http, second.http],
|
|
149
|
+
]) {
|
|
150
|
+
if (before.birth !== after.birth) {
|
|
151
|
+
return unproven(`the process listening on ${label} port ${port} changed between two reads (${before.birth}, then ${after.birth}), ` +
|
|
152
|
+
`so it is not proven to be the process whose command line was checked`, null, RESTARTING_REMEDY);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { kind: "proven", httpPort };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The credential-free half of the check, for one HTTP port (flair#2526): one
|
|
159
|
+
* process listens on `httpPort`, its command line matches a recognized Harper
|
|
160
|
+
* pattern, and its pid and start time are unchanged on a second read (to the
|
|
161
|
+
* second on macOS). Nothing is sent to any port. The command-line match does
|
|
162
|
+
* not authenticate the program, and the check says nothing about which ops
|
|
163
|
+
* endpoint the listener pairs with: `flair doctor` uses it only when the full
|
|
164
|
+
* check could not read the ops configuration for want of a usable admin
|
|
165
|
+
* credential, and then sends that HTTP port requests with no Authorization
|
|
166
|
+
* header or an Ed25519 signature; never Basic or Bearer.
|
|
167
|
+
*/
|
|
168
|
+
export function proveLocalHarperListener(httpPort, deps = {}) {
|
|
169
|
+
const os = { ...DEFAULT_OS_READS, ...osReadsForTests, ...deps.osReads };
|
|
170
|
+
const first = observeListener(os, "HTTP", httpPort);
|
|
171
|
+
if (!("birth" in first))
|
|
172
|
+
return first;
|
|
173
|
+
const notHarper = harperListenerRefusal(os, first.pid, "HTTP", httpPort);
|
|
174
|
+
if (notHarper !== null)
|
|
175
|
+
return notHarper;
|
|
176
|
+
const second = observeListener(os, "HTTP", httpPort);
|
|
177
|
+
if (!("birth" in second))
|
|
178
|
+
return second;
|
|
179
|
+
if (first.birth !== second.birth) {
|
|
180
|
+
return {
|
|
181
|
+
kind: "unproven",
|
|
182
|
+
status: null,
|
|
183
|
+
detail: `the process listening on HTTP port ${httpPort} changed between two reads (${first.birth}, then ${second.birth}), ` +
|
|
184
|
+
`so it is not proven to be the process whose command line was checked`,
|
|
185
|
+
remedy: RESTARTING_REMEDY,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return { kind: "proven", httpPort };
|
|
189
|
+
}
|
|
190
|
+
/** What to do when a listener changed under the check. */
|
|
191
|
+
const RESTARTING_REMEDY = "Re-run this command once the instance is running and not restarting.";
|
|
192
|
+
/** Read the sole listener on each port and its birth identity, or an `unproven` proof naming why not. */
|
|
193
|
+
function observeListeners(os, opsPort, httpPort) {
|
|
194
|
+
const ops = observeListener(os, "ops", opsPort);
|
|
195
|
+
if (!("birth" in ops))
|
|
196
|
+
return ops;
|
|
197
|
+
const http = observeListener(os, "HTTP", httpPort);
|
|
198
|
+
if (!("birth" in http))
|
|
199
|
+
return http;
|
|
200
|
+
return { ops, http };
|
|
201
|
+
}
|
|
202
|
+
function observeListener(os, label, port) {
|
|
203
|
+
const sole = soleListener(os.listeners(port), label, port);
|
|
204
|
+
if (!("pid" in sole))
|
|
205
|
+
return sole;
|
|
206
|
+
const birth = os.birth(sole.pid);
|
|
207
|
+
if (birth.kind === "unavailable") {
|
|
208
|
+
return { kind: "unproven", status: null, detail: `the process listening on ${label} port ${port} has no readable birth identity: ${birth.reason}`, remedy: birth.remedy };
|
|
209
|
+
}
|
|
210
|
+
return { pid: sole.pid, birth: birth.id };
|
|
211
|
+
}
|
|
212
|
+
/** An `unproven` proof when `pid`'s command line cannot be read or is not a recognized Harper one, else null. */
|
|
213
|
+
function harperListenerRefusal(os, pid, label, port) {
|
|
214
|
+
const commandLine = os.commandLine(pid);
|
|
215
|
+
if (commandLine === null) {
|
|
216
|
+
return { kind: "unproven", status: null, detail: `the command line of process ${pid}, listening on ${label} port ${port}, cannot be read`, remedy: LISTENER_VISIBILITY_REMEDY };
|
|
217
|
+
}
|
|
218
|
+
if (isHarperProcessCommandLine(commandLine))
|
|
219
|
+
return null;
|
|
220
|
+
return {
|
|
221
|
+
kind: "unproven",
|
|
222
|
+
status: null,
|
|
223
|
+
detail: `process ${pid} (${programName(commandLine)}), listening on ${label} port ${port}, does not have a recognized Harper command line, as when a port forwarder (docker-proxy, ssh -L) holds that port`,
|
|
224
|
+
remedy: LISTENER_VISIBILITY_REMEDY,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/** The one process listening on `port`, or an `unproven` proof naming why not. */
|
|
228
|
+
function soleListener(read, label, port) {
|
|
229
|
+
if (read.kind === "unavailable") {
|
|
230
|
+
return { kind: "unproven", status: null, detail: `the listener table for ${label} port ${port} cannot be read: ${read.reason}`, remedy: read.remedy };
|
|
231
|
+
}
|
|
232
|
+
if (read.pids.length === 0) {
|
|
233
|
+
return {
|
|
234
|
+
kind: "unproven",
|
|
235
|
+
status: null,
|
|
236
|
+
detail: `the listener table shows no process this CLI can see listening on ${label} port ${port}`,
|
|
237
|
+
remedy: label === "HTTP"
|
|
238
|
+
? `Start the instance that should serve HTTP port ${port}, or correct its configuration and restart it. ${LISTENER_VISIBILITY_REMEDY}`
|
|
239
|
+
: LISTENER_VISIBILITY_REMEDY,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (read.pids.length > 1) {
|
|
243
|
+
return {
|
|
244
|
+
kind: "unproven",
|
|
245
|
+
status: null,
|
|
246
|
+
detail: `processes ${read.pids.join(", ")} all listen on ${label} port ${port}, so the one serving it cannot be named`,
|
|
247
|
+
remedy: `Stop every listener on ${label} port ${port} but Harper's own, then re-run this command.`,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return { pid: read.pids[0] };
|
|
251
|
+
}
|
|
252
|
+
/** The basename of a command line's first argument (never the arguments). */
|
|
253
|
+
function programName(commandLine) {
|
|
254
|
+
const first = commandLine.includes("\u0000") ? commandLine.split("\u0000")[0] : commandLine.trim().split(/\s+/)[0];
|
|
255
|
+
return first.split("/").pop() || "unknown program";
|
|
256
|
+
}
|