@tpsdev-ai/flair 0.58.0 → 0.62.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 +7 -6
- package/SECURITY.md +36 -0
- package/config.yaml +15 -1
- package/dist/bridges/builtins/mem0.js +63 -28
- 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/build-info.json +3 -3
- package/dist/cli.js +1030 -343
- package/dist/commands/agent.js +384 -45
- package/dist/commands/attention.js +4 -3
- package/dist/commands/backup.js +202 -34
- package/dist/commands/bridge.js +172 -119
- package/dist/commands/deploy.js +37 -4
- package/dist/commands/doctor.js +601 -163
- package/dist/commands/export.js +54 -19
- package/dist/commands/federation.js +141 -21
- package/dist/commands/hook.js +155 -2
- package/dist/commands/idp.js +14 -1
- package/dist/commands/import.js +7 -3
- package/dist/commands/init.js +462 -193
- package/dist/commands/integrity.js +388 -0
- package/dist/commands/keys.js +247 -58
- package/dist/commands/mcp.js +146 -53
- package/dist/commands/memory.js +171 -21
- package/dist/commands/migrate-harness-memory.js +3 -2
- package/dist/commands/orgevent.js +46 -37
- package/dist/commands/presence.js +4 -3
- package/dist/commands/principal.js +426 -48
- package/dist/commands/quality.js +10 -1
- package/dist/commands/reembed.js +141 -17
- package/dist/commands/relationship.js +2 -1
- package/dist/commands/rem.js +56 -18
- package/dist/commands/restore.js +101 -61
- package/dist/commands/search.js +6 -4
- package/dist/commands/service.js +151 -38
- package/dist/commands/soul.js +4 -3
- package/dist/commands/status.js +43 -5
- package/dist/commands/test.js +3 -2
- package/dist/commands/upgrade.js +24 -1
- package/dist/commands/workspace.js +6 -3
- package/dist/component-env.js +31 -41
- package/dist/deploy.js +77 -1
- package/dist/doctor-client.js +529 -92
- package/dist/engine-version-contract.js +1 -0
- package/dist/engine-version.js +23 -3
- package/dist/federation-verify.js +4 -3
- package/dist/hook-install.js +670 -6
- 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 +46 -0
- package/dist/lib/agent-roster.js +33 -0
- package/dist/lib/auth-resolve.js +49 -16
- package/dist/lib/capture-runtime.js +124 -0
- package/dist/lib/credential-status-rule.js +78 -0
- package/dist/lib/daemon-liveness.js +81 -8
- package/dist/lib/doctor-run.js +140 -35
- package/dist/lib/durability-copy.js +10 -0
- package/dist/lib/embedding-space-doctor.js +75 -0
- package/dist/lib/flair-agent-role.js +120 -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/launchd-management.js +27 -2
- 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 +1775 -338
- 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 +2 -0
- package/dist/lib/memory-id-policy.js +46 -0
- package/dist/lib/memory-integrity.js +309 -0
- package/dist/lib/memory-purge-response.js +20 -0
- package/dist/lib/ops-table-count.js +25 -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 +103 -7
- 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-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-seed-pending.js +24 -0
- package/dist/lib/skill-seed.js +365 -0
- package/dist/lib/stop-start-recovery.js +54 -0
- package/dist/lib/using-flair-skill.js +87 -0
- package/dist/mcp-client-assertion.js +112 -4
- package/dist/memory-id-checkpoint.js +326 -0
- package/dist/memory-node-compare.js +207 -0
- package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
- package/dist/rem/restore.js +6 -5
- package/dist/rem/runner.js +103 -44
- package/dist/resources/Agent.js +348 -8
- package/dist/resources/AgentKeyRegister.js +214 -0
- package/dist/resources/AgentKeyRegistration.js +28 -0
- package/dist/resources/AgentSeed.js +19 -4
- package/dist/resources/AutoPromoteCandidates.js +7 -1
- package/dist/resources/Credential.js +177 -2
- package/dist/resources/Federation.js +752 -132
- package/dist/resources/FederationRetry.js +140 -0
- package/dist/resources/InstructionVersion.js +220 -0
- package/dist/resources/Integration.js +300 -25
- package/dist/resources/Memory.js +2113 -604
- package/dist/resources/MemoryArchive.js +75 -42
- package/dist/resources/MemoryBootstrap.js +227 -64
- package/dist/resources/MemoryCandidate.js +21 -1
- package/dist/resources/MemoryDedupStats.js +30 -1
- package/dist/resources/MemoryFeed.js +327 -45
- package/dist/resources/MemoryGrant.js +21 -1
- package/dist/resources/MemoryMaintenance.js +129 -25
- package/dist/resources/MemoryPurge.js +232 -0
- package/dist/resources/MemoryReflect.js +7 -2
- package/dist/resources/MemoryReindex.js +24 -8
- package/dist/resources/Message.js +5 -3
- package/dist/resources/OAuth.js +186 -9
- package/dist/resources/OrgEvent.js +126 -3
- package/dist/resources/OrgSkillAssignment.js +209 -0
- package/dist/resources/Presence.js +144 -49
- package/dist/resources/PromoteMemoryCandidate.js +18 -7
- package/dist/resources/Relationship.js +23 -0
- package/dist/resources/SemanticSearch.js +63 -32
- package/dist/resources/Soul.js +189 -76
- package/dist/resources/TeamDirectory.js +35 -0
- package/dist/resources/WorkspaceState.js +21 -1
- package/dist/resources/XAA.js +212 -65
- package/dist/resources/agent-auth.js +40 -15
- package/dist/resources/agent-status-guard.js +63 -0
- package/dist/resources/auth-middleware.js +402 -162
- package/dist/resources/ed25519-auth.js +1 -1
- package/dist/resources/embedding-space-guard.js +366 -72
- package/dist/resources/engine-version-stamp-boot.js +53 -0
- package/dist/resources/federation-classify.js +94 -0
- package/dist/resources/federation-crypto.js +5 -5
- package/dist/resources/federation-retry-lib.js +135 -0
- package/dist/resources/flair-agent-role-boot.js +26 -0
- package/dist/resources/grant-visibility.js +135 -0
- package/dist/resources/harper-routing.js +83 -0
- package/dist/resources/health.js +130 -20
- package/dist/resources/host-source-citation.js +49 -0
- package/dist/resources/host-source-visibility.js +7 -6
- package/dist/resources/instance-create-lock.js +17 -0
- package/dist/resources/instance-identity.js +23 -0
- package/dist/resources/instruction-version-record.js +343 -0
- package/dist/resources/key-lock.js +31 -0
- package/dist/resources/mcp-handler.js +36 -8
- package/dist/resources/mcp-oauth-env-guard.js +36 -0
- package/dist/resources/mcp-oauth.js +26 -12
- package/dist/resources/mcp-tool-arguments.js +4 -6
- package/dist/resources/mcp-tools.js +150 -23
- package/dist/resources/memory-bootstrap-lib.js +5 -15
- package/dist/resources/memory-declared-attributes.js +3 -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 +88 -0
- package/dist/resources/memory-embedding-value.js +8 -0
- package/dist/resources/memory-host-source.js +27 -5
- package/dist/resources/memory-id-guard.js +75 -0
- package/dist/resources/memory-redaction.js +236 -0
- package/dist/resources/memory-supplied-vector.js +72 -0
- package/dist/resources/memory-write-lock.js +310 -0
- package/dist/resources/memory-write-response.js +139 -0
- package/dist/resources/migrations/data-dir.js +74 -16
- package/dist/resources/migrations/dir-safety.js +1 -1
- package/dist/resources/migrations/embedding-stamp.js +193 -79
- package/dist/resources/migrations/export.js +1 -0
- package/dist/resources/migrations/graph-heal.js +18 -25
- package/dist/resources/migrations/lock.js +7 -1
- package/dist/resources/migrations/snapshot.js +1 -0
- package/dist/resources/migrations/source-fields.js +5 -5
- package/dist/resources/migrations/synthetic-test-migration.js +14 -11
- package/dist/resources/migrations/visibility-backfill.js +29 -29
- package/dist/resources/multi-worker-guard.js +121 -0
- package/dist/resources/oauth-discovery.js +1 -0
- package/dist/resources/oauth-wellknown.js +8 -4
- package/dist/resources/object-body-guard.js +33 -0
- package/dist/resources/org-event-created-at.js +133 -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 +212 -0
- package/dist/resources/owner-scoped-writes.js +58 -0
- package/dist/resources/presence-receipt.js +236 -0
- package/dist/resources/promotion-stamp.js +47 -12
- package/dist/resources/record-owner-guard.js +74 -26
- package/dist/resources/record-types.js +17 -1
- package/dist/resources/relay-ops.js +78 -6
- package/dist/resources/replay-store.js +145 -21
- package/dist/resources/request-target.js +136 -0
- package/dist/resources/request-transaction.js +35 -0
- package/dist/resources/search-readiness.js +4 -1
- package/dist/resources/seed-ids.js +9 -0
- package/dist/resources/seed-reservation.js +117 -0
- package/dist/resources/semantic-retrieval-core.js +20 -9
- package/dist/resources/skill-manifest.js +190 -0
- package/dist/resources/skill-provenance.js +15 -10
- package/dist/resources/skill-subject.js +53 -0
- package/dist/resources/skill-version-write.js +432 -0
- package/dist/resources/skill-write-fault.js +17 -0
- package/dist/resources/skill-write-policy.js +67 -0
- package/dist/resources/skill-write.js +67 -7
- package/dist/resources/table-helpers.js +10 -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 +118 -0
- package/dist/resources/usage-recording.js +171 -63
- package/dist/resources/write-back.js +218 -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 +46 -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/flair-agent-role.js +120 -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/mcp-oauth-env-core.js +133 -0
- package/dist/src/lib/memory-attributes.js +2 -0
- package/dist/src/lib/memory-id-policy.js +46 -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/systemd-agent-id.js +38 -0
- package/docs/access-control.md +768 -0
- package/docs/api-reference.md +52 -13
- package/docs/auth.md +10 -12
- package/docs/bridges.md +11 -0
- package/docs/claude-code.md +34 -4
- package/docs/deepseek-harness.md +1 -1
- package/docs/deploying-on-fabric.md +70 -0
- package/docs/deployment.md +2 -0
- package/docs/embedding-in-a-harper-app.md +1 -1
- package/docs/federation.md +33 -2
- package/docs/hosted-on-fabric.md +43 -36
- package/docs/integrations.md +6 -6
- package/docs/mcp-clients.md +10 -5
- package/docs/n8n.md +17 -11
- package/docs/notes/mcp-agent-auth-consumer.md +12 -4
- package/docs/notes/mcp-oauth-model2.md +34 -5
- package/docs/quickstart-fabric.md +2 -2
- package/docs/quickstart.md +9 -1
- package/docs/releasing.md +34 -28
- package/docs/rem.md +8 -0
- package/docs/secrets-and-keys.md +19 -2
- package/docs/standalone-local.md +1 -0
- package/docs/supply-chain-policy.md +13 -3
- package/docs/upgrade.md +32 -6
- package/package.json +12 -6
- package/schemas/agent-key-registration.graphql +22 -0
- package/schemas/agent.graphql +3 -0
- package/schemas/federation.graphql +38 -1
- package/schemas/memory.graphql +115 -5
- package/schemas/oauth.graphql +27 -4
- package/schemas/schema.graphql +2 -0
- package/templates/launchd/start-flair-with-admin-pass.sh +89 -16
|
@@ -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
|
+
}
|