@tpsdev-ai/flair 0.62.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.
Files changed (232) hide show
  1. package/README.md +6 -6
  2. package/SECURITY.md +18 -10
  3. package/config.yaml +11 -11
  4. package/dist/bridges/runtime/execute.js +1 -0
  5. package/dist/bridges/types.js +1 -0
  6. package/dist/build-info.json +3 -3
  7. package/dist/cli.js +66 -122
  8. package/dist/commands/agent.js +27 -65
  9. package/dist/commands/backup-approval.js +218 -0
  10. package/dist/commands/backup.js +37 -33
  11. package/dist/commands/bootstrap.js +3 -1
  12. package/dist/commands/deploy.js +37 -7
  13. package/dist/commands/doctor.js +119 -0
  14. package/dist/commands/export.js +3 -4
  15. package/dist/commands/federation.js +653 -68
  16. package/dist/commands/grant.js +5 -6
  17. package/dist/commands/help.js +25 -0
  18. package/dist/commands/idp.js +14 -19
  19. package/dist/commands/import.js +131 -19
  20. package/dist/commands/init.js +7 -7
  21. package/dist/commands/integrity.js +5 -6
  22. package/dist/commands/mcp.js +11 -17
  23. package/dist/commands/memory.js +33 -15
  24. package/dist/commands/orgevent.js +5 -10
  25. package/dist/commands/principal.js +13 -25
  26. package/dist/commands/quality.js +3 -0
  27. package/dist/commands/rem.js +484 -65
  28. package/dist/commands/restore.js +352 -44
  29. package/dist/commands/service.js +84 -2
  30. package/dist/commands/soul.js +0 -6
  31. package/dist/commands/status.js +51 -33
  32. package/dist/commands/test.js +4 -1
  33. package/dist/commands/upgrade.js +53 -22
  34. package/dist/commands/workspace.js +7 -8
  35. package/dist/deploy.js +59 -3
  36. package/dist/doctor-client.js +7 -2
  37. package/dist/fabric-upgrade.js +12 -2
  38. package/dist/federation/presence-send.js +139 -0
  39. package/dist/federation-verify.js +269 -177
  40. package/dist/install/clients.js +171 -35
  41. package/dist/lib/agent-id-rule.js +44 -2
  42. package/dist/lib/atomic-write.js +1 -1
  43. package/dist/lib/auth-resolve.js +1028 -52
  44. package/dist/lib/backup-approval-store.js +612 -0
  45. package/dist/lib/backup-artifact.js +343 -0
  46. package/dist/lib/config-critical-section.js +212 -25
  47. package/dist/lib/connect-phase.js +33 -0
  48. package/dist/lib/daemon-liveness.js +2 -1
  49. package/dist/lib/federation-pair-access.js +4 -0
  50. package/dist/lib/federation-retry-queue.js +283 -0
  51. package/dist/lib/federation-retry-row.js +101 -0
  52. package/dist/lib/instruction-event-contract.js +17 -0
  53. package/dist/lib/instruction-event-doctor.js +102 -0
  54. package/dist/lib/launchd-management.js +14 -3
  55. package/dist/lib/mcp-enable.js +14 -15
  56. package/dist/lib/memory-attributes.js +2 -0
  57. package/dist/lib/memory-provider-timeout.js +33 -0
  58. package/dist/lib/mqtt-config.js +132 -0
  59. package/dist/lib/mqtt-listener-doctor.js +104 -0
  60. package/dist/lib/open-live-row.js +20 -0
  61. package/dist/lib/ops-url.js +52 -0
  62. package/dist/lib/promotion-content-hash.js +20 -0
  63. package/dist/lib/restore-archive.js +760 -0
  64. package/dist/lib/restore-verify.js +1 -1
  65. package/dist/lib/skill-assignment-preflight.js +159 -0
  66. package/dist/lib/skill-seed.js +91 -8
  67. package/dist/lib/skill-tombstone-doctor.js +133 -0
  68. package/dist/lib/soul-scope-fields.js +26 -0
  69. package/dist/lib/uninstall-purge.js +266 -11
  70. package/dist/lib/upgrade-prior-liveness.js +2 -1
  71. package/dist/mcp-client-assertion.js +2 -10
  72. package/dist/memory-id-checkpoint.js +28 -7
  73. package/dist/mqtt-off-deploy.js +550 -0
  74. package/dist/rem/promote-policy.js +3 -3
  75. package/dist/rem/restore.js +302 -26
  76. package/dist/rem/runner.js +220 -58
  77. package/dist/rem/scheduler.js +21 -4
  78. package/dist/resources/A2AAdapter.js +131 -178
  79. package/dist/resources/AdminMemory.js +2 -0
  80. package/dist/resources/AdminMemoryCandidates.js +116 -0
  81. package/dist/resources/Agent.js +155 -45
  82. package/dist/resources/AgentCard.js +4 -0
  83. package/dist/resources/AgentKeyRegister.js +56 -22
  84. package/dist/resources/AgentReadPosition.js +7 -0
  85. package/dist/resources/AgentSeed.js +232 -107
  86. package/dist/resources/AutoPromoteCandidates.js +413 -84
  87. package/dist/resources/Credential.js +6 -8
  88. package/dist/resources/Federation.js +219 -57
  89. package/dist/resources/FederationRetry.js +4 -4
  90. package/dist/resources/InstructionVersion.js +1 -1
  91. package/dist/resources/MCPClientMetadata.js +6 -3
  92. package/dist/resources/Memory.js +1303 -270
  93. package/dist/resources/MemoryArchive.js +149 -64
  94. package/dist/resources/MemoryBootstrap.js +12 -22
  95. package/dist/resources/MemoryCandidate.js +16 -2
  96. package/dist/resources/MemoryConsolidate.js +2 -2
  97. package/dist/resources/MemoryFeed.js +360 -147
  98. package/dist/resources/MemoryMaintenance.js +290 -72
  99. package/dist/resources/MemoryPurge.js +138 -22
  100. package/dist/resources/MemoryReflect.js +68 -14
  101. package/dist/resources/MemoryReindex.js +91 -13
  102. package/dist/resources/Message.js +11 -2
  103. package/dist/resources/OAuth.js +156 -39
  104. package/dist/resources/OAuthClient.js +1 -1
  105. package/dist/resources/OrgEvent.js +292 -29
  106. package/dist/resources/OrgEventCatchup.js +7 -0
  107. package/dist/resources/OrgEventMaintenance.js +48 -10
  108. package/dist/resources/PairingToken.js +1 -1
  109. package/dist/resources/Presence.js +31 -11
  110. package/dist/resources/PromoteMemoryCandidate.js +8 -2
  111. package/dist/resources/RecordUsage.js +2 -4
  112. package/dist/resources/Soul.js +2 -0
  113. package/dist/resources/SoulFeed.js +2 -1
  114. package/dist/resources/WorkspaceLatest.js +4 -0
  115. package/dist/resources/XAA.js +36 -10
  116. package/dist/resources/a2a-stream.js +313 -0
  117. package/dist/resources/a2a-url.js +4 -5
  118. package/dist/resources/agent-admin.js +10 -1
  119. package/dist/resources/agent-auth.js +98 -68
  120. package/dist/resources/agent-read-position-lib.js +4 -1
  121. package/dist/resources/agent-status-guard.js +9 -7
  122. package/dist/resources/auth-middleware.js +166 -42
  123. package/dist/resources/authority-field-guard.js +9 -11
  124. package/dist/resources/auto-promote-lib.js +8 -5
  125. package/dist/resources/basic-auth-header.js +21 -0
  126. package/dist/resources/bm25-index-service.js +4 -5
  127. package/dist/resources/dedup-cluster.js +6 -1
  128. package/dist/resources/embeddings-provider.js +25 -0
  129. package/dist/resources/federation-classify.js +89 -0
  130. package/dist/resources/federation-retry-lib.js +6 -81
  131. package/dist/resources/health.js +43 -4
  132. package/dist/resources/host-pointer/registry.js +29 -0
  133. package/dist/resources/host-pointer-adapter.js +1 -0
  134. package/dist/resources/in-process.js +24 -22
  135. package/dist/resources/instruction-version-record.js +251 -7
  136. package/dist/resources/internal-authority.js +13 -0
  137. package/dist/resources/mcp-handler.js +106 -12
  138. package/dist/resources/mcp-oauth-flag.js +12 -14
  139. package/dist/resources/mcp-oauth.js +430 -53
  140. package/dist/resources/mcp-tools.js +60 -22
  141. package/dist/resources/memory-declared-attributes.js +1 -0
  142. package/dist/resources/memory-embed-hash.js +56 -0
  143. package/dist/resources/memory-host-source.js +5 -2
  144. package/dist/resources/memory-reflect-lib.js +1 -1
  145. package/dist/resources/memory-write-lock.js +202 -23
  146. package/dist/resources/memory-write-response.js +1 -1
  147. package/dist/resources/memory-write-transaction.js +243 -0
  148. package/dist/resources/migration-boot.js +17 -12
  149. package/dist/resources/migrations/boot-progress.js +25 -0
  150. package/dist/resources/migrations/data-dir.js +16 -3
  151. package/dist/resources/migrations/dir-safety.js +60 -2
  152. package/dist/resources/migrations/embedding-stamp.js +315 -49
  153. package/dist/resources/migrations/graph-heal.js +9 -2
  154. package/dist/resources/migrations/ledger.js +5 -2
  155. package/dist/resources/migrations/lock.js +867 -80
  156. package/dist/resources/migrations/progress.js +71 -10
  157. package/dist/resources/migrations/runner.js +212 -18
  158. package/dist/resources/migrations/state.js +36 -1
  159. package/dist/resources/migrations/status.js +2 -13
  160. package/dist/resources/migrations/synthetic-test-migration.js +78 -12
  161. package/dist/resources/migrations/types.js +26 -1
  162. package/dist/resources/migrations/visibility-backfill.js +111 -70
  163. package/dist/resources/oauth-discovery.js +0 -5
  164. package/dist/resources/object-body-guard.js +12 -5
  165. package/dist/resources/org-event-reserved.js +92 -0
  166. package/dist/resources/org-event-write-fault.js +19 -0
  167. package/dist/resources/owner-field-guard.js +1 -1
  168. package/dist/resources/owner-scoped-write-policy.js +65 -21
  169. package/dist/resources/owner-scoped-writes.js +37 -6
  170. package/dist/resources/path-id-rewrite.js +76 -0
  171. package/dist/resources/presence-internal.js +5 -5
  172. package/dist/resources/promotion-binding.js +43 -0
  173. package/dist/resources/promotion-stamp.js +124 -33
  174. package/dist/resources/protocol-context.js +189 -0
  175. package/dist/resources/record-type-kit.js +2 -4
  176. package/dist/resources/request-target.js +28 -1
  177. package/dist/resources/request-transaction.js +11 -1
  178. package/dist/resources/seed-reservation.js +1 -2
  179. package/dist/resources/semantic-retrieval-core.js +7 -6
  180. package/dist/resources/server-row-validation.js +71 -0
  181. package/dist/resources/skill-manifest.js +4 -3
  182. package/dist/resources/skill-version-write.js +384 -27
  183. package/dist/resources/skill-write-fault.js +6 -3
  184. package/dist/resources/skill-write-policy.js +2 -1
  185. package/dist/resources/soul-write-policy.js +26 -7
  186. package/dist/resources/table-helpers.js +1 -1
  187. package/dist/resources/table-subscription-policy.js +518 -85
  188. package/dist/resources/table-subscriptions.js +20 -7
  189. package/dist/resources/txn-pause-point.js +122 -5
  190. package/dist/resources/usage-recording.js +54 -46
  191. package/dist/resources/write-back.js +176 -11
  192. package/dist/resources/write-journal-context.js +5 -0
  193. package/dist/resources/write-journal.js +289 -0
  194. package/dist/src/lib/agent-id-rule.js +44 -2
  195. package/dist/src/lib/atomic-write.js +1 -1
  196. package/dist/src/lib/federation-retry-row.js +101 -0
  197. package/dist/src/lib/instruction-event-contract.js +17 -0
  198. package/dist/src/lib/memory-attributes.js +2 -0
  199. package/dist/src/lib/open-live-row.js +20 -0
  200. package/dist/src/lib/soul-scope-fields.js +26 -0
  201. package/dist/src/rem/promote-policy.js +3 -3
  202. package/dist/src/rem/scheduler.js +21 -4
  203. package/docs/access-control.md +18 -9
  204. package/docs/api-reference.md +126 -28
  205. package/docs/auth.md +40 -18
  206. package/docs/claude-code.md +3 -3
  207. package/docs/deploying-on-fabric.md +108 -0
  208. package/docs/deployment.md +17 -0
  209. package/docs/embedding-in-a-harper-app.md +16 -14
  210. package/docs/entity-vocabulary.md +3 -6
  211. package/docs/federation.md +29 -2
  212. package/docs/integrations.md +11 -11
  213. package/docs/mcp-clients.md +7 -5
  214. package/docs/notes/mcp-agent-auth-consumer.md +4 -9
  215. package/docs/notes/mcp-oauth-model2.md +6 -6
  216. package/docs/openclaw.md +1 -1
  217. package/docs/quickstart-fabric.md +2 -2
  218. package/docs/quickstart.md +3 -3
  219. package/docs/releasing.md +13 -13
  220. package/docs/rem.md +24 -3
  221. package/docs/secrets-and-keys.md +2 -2
  222. package/docs/standalone-local.md +29 -2
  223. package/docs/supply-chain-policy.md +28 -20
  224. package/docs/the-team.md +2 -2
  225. package/docs/upgrade.md +14 -0
  226. package/package.json +18 -11
  227. package/schemas/agent.graphql +3 -3
  228. package/schemas/federation.graphql +10 -7
  229. package/schemas/memory.graphql +7 -2
  230. package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
  231. package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
  232. package/dist/resources/soul-adk-guard.js +0 -68
@@ -60,13 +60,116 @@ is_harper_command() {
60
60
  return 1
61
61
  }
62
62
 
63
+ # A newline, for matching whole lines of multi-line output with `case`.
64
+ NL='
65
+ '
66
+
67
+ # flair#2697: the pid Harper itself reads from hdb.pid, when the launcher can
68
+ # act on it. Harper's readPidFile is Number.parseInt(<file contents>, 10): it
69
+ # skips leading whitespace, takes one leading `+` or `-`, reads decimal digits
70
+ # up to the first non-digit, and yields NaN when there is none (`123junk` is
71
+ # 123, `junk123` is NaN, `-123` is -123). This prints the pid only when that
72
+ # parse is a positive integer of at most 10 digits (leading zeros dropped, so
73
+ # shell tests read it as decimal). It prints nothing, leaving the file to
74
+ # Harper's own check, for every other file: NaN, zero, a negative value (which
75
+ # Harper passes to process.kill, where it names a process group that can be
76
+ # alive), more digits than a pid has, a file containing a NUL byte (command
77
+ # substitution drops NUL bytes, so the shell would not see what Harper reads),
78
+ # and a file with any non-ASCII byte before the digits. The parse runs in a
79
+ # subshell with LC_ALL=C, so [:space:] is ASCII whitespace only whatever the
80
+ # caller's locale (bash under a UTF-8 locale would otherwise also strip a
81
+ # leading no-break space); a leading no-break space or byte-order mark, which
82
+ # parseInt skips, therefore yields no pid here.
83
+ harper_pid_file_pid() (
84
+ LC_ALL=C
85
+ export LC_ALL
86
+ HP_BYTES="$(wc -c < "$1" 2>/dev/null | tr -cd '0-9')"
87
+ HP_NONUL="$(tr -d '\000' < "$1" 2>/dev/null | wc -c | tr -cd '0-9')"
88
+ if [ -z "$HP_BYTES" ] || [ "$HP_BYTES" != "$HP_NONUL" ]; then exit 0; fi
89
+ HP_RAW="$(cat "$1" 2>/dev/null)" || exit 0
90
+ HP_RAW="${HP_RAW#"${HP_RAW%%[![:space:]]*}"}"
91
+ case "$HP_RAW" in +*) HP_RAW="${HP_RAW#+}" ;; esac
92
+ HP_PID="${HP_RAW%%[!0-9]*}"
93
+ HP_PID="${HP_PID#"${HP_PID%%[!0]*}"}"
94
+ case "$HP_PID" in ""|???????????*) exit 0 ;; esac
95
+ printf '%s\n' "$HP_PID"
96
+ )
97
+
98
+ # flair#2697: is pid "$1" alive the way Harper's isProcessRunning counts it?
99
+ # `kill -0` succeeds, or it fails only because the process belongs to another
100
+ # user (EPERM), which Harper also counts as running. The shell's `kill` exit
101
+ # status does not say which error it hit, so the second case is "ps lists the
102
+ # pid".
103
+ pid_exists() {
104
+ kill -0 "$1" 2>/dev/null && return 0
105
+ [ -n "$(ps -o pid= -p "$1" 2>/dev/null || true)" ]
106
+ }
107
+
108
+ # flair#2697: is there positive evidence that live pid "$1", whose command line
109
+ # as ps reports it is "$2", is NOT this instance? Every condition must hold:
110
+ # - the command line is not empty;
111
+ # - it does not contain `harper`, in any letter case;
112
+ # - it does not contain this instance's ROOTPATH, as given or as its physical
113
+ # path;
114
+ # - one of:
115
+ # - ps reports the process's uid and it is not this launcher's uid (the
116
+ # Harper this launcher starts runs as this launcher's user; lsof
117
+ # cannot list another user's files unless run as root);
118
+ # - same uid, and lsof is not installed;
119
+ # - same uid, and `lsof -p` exits 0 with a listing that includes the
120
+ # process's working directory, and lists no open file (the working
121
+ # directory included) at or under either form of ROOTPATH.
122
+ # Anything else, including a failed or empty lsof listing or any failed step
123
+ # here, returns 1: the process is treated as possibly this instance. The shape
124
+ # check alone is not enough: is_harper_command splits the ps line at spaces, so
125
+ # a Harper whose node executable path contains a space is not Harper-shaped,
126
+ # yet its command line names harper. On success, UP_WHY holds the clause the
127
+ # launcher's message uses for the last condition.
128
+ is_unrelated_process() {
129
+ UP_PID="$1"
130
+ UP_LINE="$2"
131
+ UP_WHY=""
132
+ case "$UP_LINE" in *[![:space:]]*) ;; *) return 1 ;; esac
133
+ UP_LOWER="$(printf '%s' "$UP_LINE" | tr '[:upper:]' '[:lower:]')" || return 1
134
+ [ -n "$UP_LOWER" ] || return 1
135
+ case "$UP_LOWER" in *harper*) return 1 ;; esac
136
+ UP_ROOT="${ROOTPATH%/}"
137
+ [ -n "$UP_ROOT" ] || return 1
138
+ UP_PHYS="$(cd -P -- "$ROOTPATH" >/dev/null 2>&1 && pwd -P)" || return 1
139
+ UP_PHYS="${UP_PHYS%/}"
140
+ [ -n "$UP_PHYS" ] || return 1
141
+ case "$UP_LINE" in *"$UP_ROOT"*|*"$UP_PHYS"*) return 1 ;; esac
142
+ UP_UID="$(ps -o uid= -p "$UP_PID" 2>/dev/null)" || return 1
143
+ UP_UID="$(printf '%s' "$UP_UID" | tr -d '[:space:]')" || return 1
144
+ UP_MY_UID="$(id -u 2>/dev/null)" || return 1
145
+ case "$UP_UID" in ""|*[!0-9]*) return 1 ;; esac
146
+ case "$UP_MY_UID" in ""|*[!0-9]*) return 1 ;; esac
147
+ if [ "$UP_UID" != "$UP_MY_UID" ]; then
148
+ UP_WHY="it runs as uid $UP_UID, not as this launcher's uid $UP_MY_UID"
149
+ return 0
150
+ fi
151
+ if ! command -v lsof >/dev/null 2>&1; then
152
+ UP_WHY="lsof is not available to list its open files"
153
+ return 0
154
+ fi
155
+ # -Ffn names both output fields: lsof on Linux prints only the fields named
156
+ # (-Fn alone gives no fd lines, so no `fcwd`), lsof on macOS adds fd lines
157
+ # either way.
158
+ UP_FILES="$(lsof -n -P -w -p "$UP_PID" -Ffn 2>/dev/null)" || return 1
159
+ UP_FILES="$NL$UP_FILES$NL"
160
+ case "$UP_FILES" in *"${NL}fcwd${NL}n/"*) ;; *) return 1 ;; esac
161
+ case "$UP_FILES" in *"${NL}n$UP_ROOT$NL"*|*"${NL}n$UP_ROOT/"*|*"${NL}n$UP_PHYS$NL"*|*"${NL}n$UP_PHYS/"*) return 1 ;; esac
162
+ UP_WHY="lsof lists none of its open files there"
163
+ return 0
164
+ }
165
+
63
166
  # flair#2040 / flair#2056: do not start a second Harper on a data directory
64
167
  # whose hdb.pid names a live process with a Harper-shaped command line (see
65
- # is_harper_command above for what that does and does not establish). `kill -0`
66
- # alone proves only that SOME process has the pid: if a direct process dies and
67
- # leaves hdb.pid behind, another process can later get that pid, and a refusal
68
- # on `kill -0` alone would make every KeepAlive retry exit 0 without ever
69
- # starting Flair.
168
+ # is_harper_command above for what that does and does not establish). That
169
+ # some process has the pid proves little: if a direct process dies and leaves
170
+ # hdb.pid behind, another process can later get that pid, and a refusal on
171
+ # liveness alone would make every KeepAlive retry exit 0 without ever starting
172
+ # Flair.
70
173
  #
71
174
  # So the launcher refuses only when the command line `ps -o command=` reports
72
175
  # for the live pid is Harper-shaped. A flair#1454 sidecar
@@ -81,9 +184,25 @@ is_harper_command() {
81
184
  # Exit 0 on the refusal: a deliberate no-op, not a crash. KeepAlive retries
82
185
  # after its throttle interval; once that process has exited, a retry does not
83
186
  # refuse.
187
+ #
188
+ # flair#2697: at startup Harper exits 4 ("Harper is already running") when the
189
+ # pid it reads from hdb.pid is non-zero, is not its own pid or a recognised
190
+ # PID-1 init process, and process.kill(pid, 0) succeeds or fails with EPERM.
191
+ # The launcher acts only on a positive pid read as Harper reads it
192
+ # (harper_pid_file_pid, which yields no pid for every other file) and counts it
193
+ # alive as Harper does (pid_exists). When the live pid is not Harper-shaped AND
194
+ # is_unrelated_process finds positive evidence that it is not this instance,
195
+ # the launcher marks hdb.pid to be moved aside; the move happens below, after
196
+ # the admin-pass checks pass and just before the exec. In every other case
197
+ # hdb.pid is left in place: a file that yields no pid, a pid that is not alive,
198
+ # a Harper-shaped pid (refused here when no sidecar disagrees; with a
199
+ # disagreeing sidecar Harper's own check decides, because Harper's own restart
200
+ # forks a new main process whose pid the sidecar does not name), and a live pid
201
+ # that is not Harper-shaped but may be this instance.
202
+ MOVE_PID_FILE=0
84
203
  if [ -n "${ROOTPATH:-}" ] && [ -f "$ROOTPATH/hdb.pid" ]; then
85
- LIVE_PID="$(tr -cd '0-9' < "$ROOTPATH/hdb.pid" 2>/dev/null || true)"
86
- if [ -n "$LIVE_PID" ] && [ "$LIVE_PID" != "$$" ] && [ "$LIVE_PID" -gt 1 ] 2>/dev/null && kill -0 "$LIVE_PID" 2>/dev/null; then
204
+ LIVE_PID="$(harper_pid_file_pid "$ROOTPATH/hdb.pid")"
205
+ if [ -n "$LIVE_PID" ] && [ "$LIVE_PID" != "$$" ] && [ "$LIVE_PID" -gt 1 ] 2>/dev/null && pid_exists "$LIVE_PID"; then
87
206
  DISAGREE=0
88
207
  SIDE="$ROOTPATH/flair-daemon.json"
89
208
  if [ -f "$SIDE" ]; then
@@ -113,9 +232,15 @@ if [ -n "${ROOTPATH:-}" ] && [ -f "$ROOTPATH/hdb.pid" ]; then
113
232
  fi
114
233
  fi
115
234
  fi
116
- if [ "$DISAGREE" = "0" ] && is_harper_command "$(ps -o command= -p "$LIVE_PID" 2>/dev/null || true)"; then
117
- echo "start-flair-with-admin-pass: $ROOTPATH/hdb.pid names pid $LIVE_PID, whose command line as ps reports it is node or bun followed by a Harper entry path; not starting a second instance. launchd retries after its throttle interval." >&2
118
- exit 0
235
+ LIVE_CMD="$(ps -o command= -p "$LIVE_PID" 2>/dev/null || true)"
236
+ if is_harper_command "$LIVE_CMD"; then
237
+ if [ "$DISAGREE" = "0" ]; then
238
+ echo "start-flair-with-admin-pass: $ROOTPATH/hdb.pid names pid $LIVE_PID, whose command line as ps reports it is node or bun followed by a Harper entry path; not starting a second instance. launchd retries after its throttle interval." >&2
239
+ exit 0
240
+ fi
241
+ elif is_unrelated_process "$LIVE_PID" "$LIVE_CMD"; then
242
+ MOVE_PID_FILE=1
243
+ MOVE_WHY="its command line as ps reports it contains neither \"harper\" (in any case) nor this data directory, and $UP_WHY"
119
244
  fi
120
245
  fi
121
246
  fi
@@ -163,6 +288,59 @@ fi
163
288
 
164
289
  export HDB_ADMIN_PASSWORD="$ADMIN_PASS"
165
290
 
291
+ # flair#2697: move hdb.pid aside (marked above) so Harper's own check does not
292
+ # refuse on a live process that is not this instance. The copy is named
293
+ # hdb.pid.stale-<epoch seconds>, or .<n> after it when that name is taken when
294
+ # checked. The copy is made with `ln -n -P`, which never replaces an existing
295
+ # name: -P links hdb.pid itself if it is a symlink, and -n treats a symlink at
296
+ # the chosen name as an existing file rather than following it (macOS ln(1):
297
+ # "-n Same as -h", "-h If the target_file or target_dir is a symbolic link, do
298
+ # not follow it"; GNU coreutils ln: "-n, --no-dereference treat LINK_NAME as a
299
+ # normal file if it is a symbolic link to a directory"). So if a file or
300
+ # symlink appears at that name meanwhile, `ln` fails. If a real directory
301
+ # appears there, `ln` puts the link inside it, and the launcher removes that
302
+ # link, checks the removal, and treats the move as failed. The original is
303
+ # removed only after a link at the chosen name succeeded; if that removal
304
+ # fails, the launcher removes the link again (checked) and treats the move as
305
+ # failed. On a failed move the launcher does not remove hdb.pid; it prints one
306
+ # line, which also names any link it could not remove, and execs Harper, whose
307
+ # own check then decides from whatever hdb.pid is present when it starts.
308
+ if [ "$MOVE_PID_FILE" = "1" ]; then
309
+ PID_FILE="$ROOTPATH/hdb.pid"
310
+ STALE_EPOCH="$(date +%s 2>/dev/null || true)"
311
+ STALE_BASE="$ROOTPATH/hdb.pid.stale-${STALE_EPOCH:-unknown}"
312
+ STALE="$STALE_BASE"
313
+ STALE_N=0
314
+ while [ -e "$STALE" ] || [ -L "$STALE" ]; do
315
+ STALE_N=$((STALE_N + 1))
316
+ if [ "$STALE_N" -gt 100 ]; then STALE=""; break; fi
317
+ STALE="$STALE_BASE.$STALE_N"
318
+ done
319
+ MOVED=0
320
+ LEFT_LINK=""
321
+ if [ -n "$STALE" ] && ln -n -P "$PID_FILE" "$STALE" 2>/dev/null; then
322
+ if [ -d "$STALE" ]; then
323
+ LINK="$STALE/hdb.pid"
324
+ elif rm -f "$PID_FILE" 2>/dev/null; then
325
+ MOVED=1
326
+ LINK=""
327
+ else
328
+ LINK="$STALE"
329
+ fi
330
+ if [ -n "$LINK" ]; then
331
+ rm -f "$LINK" 2>/dev/null || true
332
+ if [ -e "$LINK" ] || [ -L "$LINK" ]; then LEFT_LINK="$LINK"; fi
333
+ fi
334
+ fi
335
+ if [ "$MOVED" = "1" ]; then
336
+ echo "start-flair-with-admin-pass: $PID_FILE names pid $LIVE_PID, a live process that is not this instance: $MOVE_WHY; moved hdb.pid to $STALE and starting Harper." >&2
337
+ else
338
+ LEFT_NOTE=""
339
+ if [ -n "$LEFT_LINK" ]; then LEFT_NOTE=" (the link it made at $LEFT_LINK could not be removed)"; fi
340
+ echo "start-flair-with-admin-pass: $PID_FILE names pid $LIVE_PID, a live process that is not this instance: $MOVE_WHY; could not move hdb.pid aside$LEFT_NOTE. Starting Harper, whose own check decides from the hdb.pid present when it starts; if it reports Harper already running, move $PID_FILE out of the way." >&2
341
+ fi
342
+ fi
343
+
166
344
  # exec (not spawn) so launchd tracks Harper itself — the job's PID is Harper's
167
345
  # PID, and KeepAlive restarts the real service rather than a dead launcher.
168
346
  exec "$NODE" "$HARPER_BIN" run .
@@ -1,68 +0,0 @@
1
- import { ADK_SCOPE_TAG_PREFIX } from "../src/rem/promote-policy.js";
2
- /** Soul is agentId-scoped and cannot carry a per-user `adk:` tag. Writing an
3
- * ADK-sourced claim there leaks that user's distilled text to every other
4
- * user of the shared agentId. CLI refusal is not enough — a scripted
5
- * PUT /Soul must hit the same door.
6
- *
7
- * This vendor-string match is a dated rollout bridge only. Soul authorization
8
- * is the deny-by-default operator/internal allowlist in soul-write-policy.ts;
9
- * runtime denial does not depend on `adk:`. Remove this module after
10
- * ADK_SOUL_REFUSE_KILL_DATE once classified writers are deployed (#1540). */
11
- export const ADK_SOUL_REFUSAL = "adk_sourced_claim_cannot_be_written_to_soul";
12
- /** Calendar date (UTC) after which this `adk:` bridge must be removed. */
13
- export const ADK_SOUL_REFUSE_KILL_DATE = "2026-10-31";
14
- export function tagLooksAdk(tag) {
15
- return typeof tag === "string" && tag.toLowerCase().startsWith(ADK_SCOPE_TAG_PREFIX);
16
- }
17
- /** Request body carries an ADK scope tag (promotion leftover or forged). */
18
- export function bodyCarriesAdkScope(content) {
19
- if (tagLooksAdk(content?.scopeTag))
20
- return true;
21
- const tags = content?.tags;
22
- return Array.isArray(tags) && tags.some(tagLooksAdk);
23
- }
24
- export function rowLooksAdkSourced(row) {
25
- if (!row)
26
- return false;
27
- if (tagLooksAdk(row.scopeTag))
28
- return true;
29
- return Array.isArray(row.tags) && row.tags.some(tagLooksAdk);
30
- }
31
- function adkForbidden() {
32
- return new Response(JSON.stringify({ error: ADK_SOUL_REFUSAL }), {
33
- status: 403,
34
- headers: { "Content-Type": "application/json" },
35
- });
36
- }
37
- async function* searchByAgentId(tableName, agentId) {
38
- const { databases } = await import("harper");
39
- const table = databases.flair?.[tableName];
40
- if (!table?.search)
41
- return;
42
- const query = { conditions: [{ attribute: "agentId", comparator: "equals", value: agentId }] };
43
- yield* table.search(query);
44
- }
45
- /** Refuse a Soul write whose value is an ADK-sourced claim. Body tags are
46
- * sufficient; otherwise match stored MemoryCandidate.claim / Memory.content
47
- * that already carries an `adk:` scope tag. */
48
- export async function refuseAdkSourcedSoulWrite(content, lookup = {}) {
49
- if (bodyCarriesAdkScope(content))
50
- return adkForbidden();
51
- const value = typeof content?.value === "string" ? content.value : "";
52
- const agentId = typeof content?.agentId === "string" ? content.agentId : "";
53
- if (!value || !agentId)
54
- return null;
55
- const candidates = lookup.searchCandidates
56
- ?? ((id) => searchByAgentId("MemoryCandidate", id));
57
- for await (const row of candidates(agentId)) {
58
- if (row?.claim === value && rowLooksAdkSourced(row))
59
- return adkForbidden();
60
- }
61
- const memories = lookup.searchMemories
62
- ?? ((id) => searchByAgentId("Memory", id));
63
- for await (const row of memories(agentId)) {
64
- if (row?.content === value && rowLooksAdkSourced(row))
65
- return adkForbidden();
66
- }
67
- return null;
68
- }