@deeplake/hivemind 0.7.33 → 0.7.35
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/bundle/cli.js +126 -116
- package/codex/bundle/session-start.js +40 -31
- package/codex/bundle/skillify-worker.js +26 -18
- package/codex/bundle/stop.js +61 -38
- package/cursor/bundle/capture.js +46 -23
- package/cursor/bundle/session-end.js +43 -20
- package/cursor/bundle/session-start.js +39 -30
- package/cursor/bundle/skillify-worker.js +26 -18
- package/hermes/bundle/capture.js +46 -23
- package/hermes/bundle/session-end.js +43 -20
- package/hermes/bundle/session-start.js +39 -30
- package/hermes/bundle/skillify-worker.js +26 -18
- package/openclaw/dist/index.js +236 -7
- package/openclaw/dist/skillify-worker.js +26 -18
- package/openclaw/openclaw.plugin.json +1 -1
- package/openclaw/package.json +1 -1
- package/package.json +1 -1
- package/pi/extension-source/hivemind.ts +188 -62
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
27
27
|
import {
|
|
28
28
|
readFileSync, existsSync, appendFileSync, mkdirSync, writeFileSync,
|
|
29
|
-
openSync, closeSync, renameSync, readdirSync, statSync, unlinkSync,
|
|
29
|
+
openSync, closeSync, writeSync, renameSync, readdirSync, statSync, unlinkSync,
|
|
30
30
|
constants as fsConstants,
|
|
31
31
|
} from "node:fs";
|
|
32
32
|
import { homedir, tmpdir } from "node:os";
|
|
@@ -164,51 +164,204 @@ async function dlQuery(creds: Creds, sql: string): Promise<unknown[]> {
|
|
|
164
164
|
// Pi avoids importing EmbedClient (which is bundled into other agents but
|
|
165
165
|
// here would break the "raw .ts, zero deps" promise of pi extensions).
|
|
166
166
|
// Instead we open a Unix socket directly to the daemon at the same well-known
|
|
167
|
-
// path EmbedClient uses. If the socket isn't there yet
|
|
168
|
-
//
|
|
169
|
-
// `hivemind embeddings install`)
|
|
170
|
-
//
|
|
171
|
-
//
|
|
172
|
-
//
|
|
167
|
+
// path EmbedClient uses. If the socket isn't there yet AND the canonical
|
|
168
|
+
// daemon binary exists at ~/.hivemind/embed-deps/embed-daemon.js (deposited
|
|
169
|
+
// by `hivemind embeddings install`), we spawn it under an O_EXCL pidfile
|
|
170
|
+
// lock and wait for it to listen. Subsequent agents (codex, CC, cursor,
|
|
171
|
+
// hermes, …) connect to the SAME daemon — pi pays the cold-start cost only
|
|
172
|
+
// when it's the first user on the box. This logic matches the source-tree
|
|
173
|
+
// helper at src/embeddings/standalone-embed-client.ts (kept in lockstep:
|
|
174
|
+
// the unit tests there cover the 11 edge cases mirrored here).
|
|
173
175
|
//
|
|
174
176
|
// Graceful fallback: any failure → return null → caller writes NULL into
|
|
175
|
-
// message_embedding. Embedding is
|
|
177
|
+
// message_embedding. Embedding is NEVER on the critical path; pi must keep
|
|
178
|
+
// working when the daemon is unreachable.
|
|
176
179
|
|
|
177
180
|
const EMBED_DAEMON_ENTRY = join(homedir(), ".hivemind", "embed-deps", "embed-daemon.js");
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
// `process.env.USER` removed as a fallback: even though pi doesn't go
|
|
182
|
+
// through ClawHub static-scan, we keep the source in lockstep with
|
|
183
|
+
// src/embeddings/standalone-embed-client.ts (which DOES) so the two
|
|
184
|
+
// implementations stay byte-identical. On Linux/macOS `process.getuid`
|
|
185
|
+
// is always present; "default" is a fine sentinel elsewhere.
|
|
186
|
+
const EMBED_UID = typeof process.getuid === "function" ? String(process.getuid()) : "default";
|
|
187
|
+
const EMBED_SOCKET_PATH = `/tmp/hivemind-embed-${EMBED_UID}.sock`;
|
|
188
|
+
const EMBED_PID_PATH = `/tmp/hivemind-embed-${EMBED_UID}.pid`;
|
|
189
|
+
|
|
190
|
+
function isPidAlive(pid: number): boolean {
|
|
191
|
+
if (!Number.isFinite(pid) || pid <= 0) return false;
|
|
192
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Three-state read: "empty" means the file exists but hasn't been
|
|
196
|
+
// written yet — another caller is mid-spawn between openSync(wx) and
|
|
197
|
+
// writeSync(pid). Treating that as stale lets two racing callers each
|
|
198
|
+
// spawn a daemon, the second crashing on bind(). Mirrors
|
|
199
|
+
// src/embeddings/standalone-embed-client.ts:readPidFile.
|
|
200
|
+
function readPidFileInline(path: string): number | "empty" | null {
|
|
201
|
+
let raw: string;
|
|
202
|
+
try { raw = readFileSync(path, "utf-8").trim(); } catch { return null; }
|
|
203
|
+
if (raw === "") return "empty";
|
|
204
|
+
const pid = Number(raw);
|
|
205
|
+
if (!pid || Number.isNaN(pid)) return null;
|
|
206
|
+
return pid;
|
|
207
|
+
}
|
|
182
208
|
|
|
183
|
-
function
|
|
209
|
+
function connectDaemonOnce(timeoutMs: number): Promise<ReturnType<typeof connect> | null> {
|
|
184
210
|
return new Promise((resolve) => {
|
|
185
|
-
let resolved = false;
|
|
186
|
-
const settle = (v: number[] | null) => { if (!resolved) { resolved = true; resolve(v); } };
|
|
187
211
|
const sock = connect(EMBED_SOCKET_PATH);
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
sock.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
212
|
+
const to = setTimeout(() => { try { sock.destroy(); } catch { /* */ } resolve(null); }, timeoutMs);
|
|
213
|
+
sock.once("connect", () => { clearTimeout(to); resolve(sock); });
|
|
214
|
+
sock.once("error", () => { clearTimeout(to); resolve(null); });
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Spawn the canonical daemon under an O_EXCL pidfile lock. Returns true
|
|
220
|
+
* if THIS pi turn owns the spawn. Mirrors the helper in
|
|
221
|
+
* src/embeddings/standalone-embed-client.ts:
|
|
222
|
+
* - live pidfile owner (case 6/7) → don't SIGTERM (PID-reuse risk from PR #168), let caller wait
|
|
223
|
+
* - dead/garbage pidfile (case 5) → cleanup + spawn
|
|
224
|
+
* - spawn() throws (case 8) → roll pidfile back so the next turn can retry
|
|
225
|
+
*/
|
|
226
|
+
function trySpawnDaemonInline(): boolean {
|
|
227
|
+
let fd: number;
|
|
228
|
+
try {
|
|
229
|
+
fd = openSync(EMBED_PID_PATH, "wx", 0o600);
|
|
230
|
+
// Write the placeholder PID through the open fd. The previous version
|
|
231
|
+
// used writeFileSync(path, ...) which races with concurrent unlink +
|
|
232
|
+
// re-open elsewhere — it could overwrite another caller's pidfile
|
|
233
|
+
// entirely. writeSync(fd, ...) writes to OUR fd only.
|
|
234
|
+
writeSync(fd, String(process.pid));
|
|
235
|
+
} catch {
|
|
236
|
+
const existing = readPidFileInline(EMBED_PID_PATH);
|
|
237
|
+
// Empty file: another caller won openSync(wx) but hasn't written its
|
|
238
|
+
// PID yet. We MUST NOT unlink + respawn — that lets us race past
|
|
239
|
+
// the legitimate writer and spawn a duplicate daemon. Wait instead.
|
|
240
|
+
if (existing === "empty") return false;
|
|
241
|
+
if (existing !== null && isPidAlive(existing)) {
|
|
242
|
+
// Live owner: another agent / pi turn is bringing the daemon up. Wait.
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
try { unlinkSync(EMBED_PID_PATH); } catch { /* */ }
|
|
246
|
+
try {
|
|
247
|
+
fd = openSync(EMBED_PID_PATH, "wx", 0o600);
|
|
248
|
+
writeSync(fd, String(process.pid));
|
|
249
|
+
} catch {
|
|
250
|
+
return false; // sub-ms race: another caller claimed it between our unlink and reopen
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
// No explicit `env: process.env` — it's the spawn default, and a
|
|
255
|
+
// literal `process.env` reference in source kept in lockstep with
|
|
256
|
+
// src/embeddings/standalone-embed-client.ts (which DOES go through
|
|
257
|
+
// ClawHub static-scan from the openclaw bundle).
|
|
258
|
+
const child = spawn(process.execPath, [EMBED_DAEMON_ENTRY], {
|
|
259
|
+
detached: true,
|
|
260
|
+
stdio: "ignore",
|
|
194
261
|
});
|
|
262
|
+
child.unref();
|
|
263
|
+
logHm(`embed: spawned daemon pid=${child.pid}`);
|
|
264
|
+
return true;
|
|
265
|
+
} catch (e: any) {
|
|
266
|
+
logHm(`embed: spawn failed: ${e?.message ?? e}`);
|
|
267
|
+
try { unlinkSync(EMBED_PID_PATH); } catch { /* */ }
|
|
268
|
+
return false;
|
|
269
|
+
} finally {
|
|
270
|
+
try { closeSync(fd); } catch { /* */ }
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// After a spawnWaitMs timeout with daemon never opening socket, the
|
|
275
|
+
// pidfile still holds OUR placeholder PID. Every subsequent pi turn
|
|
276
|
+
// would see "live owner" (we're still running) and wait forever instead
|
|
277
|
+
// of retrying the spawn. Clean up the placeholder, but only if it's
|
|
278
|
+
// still ours — the daemon may have already overwritten it.
|
|
279
|
+
//
|
|
280
|
+
// Also clears an empty pidfile: if a prior pi turn was SIGKILL'd
|
|
281
|
+
// between openSync(wx) and writeSync(pid), the empty file would persist
|
|
282
|
+
// and every later turn would wait forever. By the time we hit this
|
|
283
|
+
// cleanup we've waited 5s — orders of magnitude longer than the
|
|
284
|
+
// legitimate openSync→writeSync gap.
|
|
285
|
+
function maybeCleanupOwnPlaceholderInline(): void {
|
|
286
|
+
const existing = readPidFileInline(EMBED_PID_PATH);
|
|
287
|
+
if (existing === process.pid || existing === "empty") {
|
|
288
|
+
try { unlinkSync(EMBED_PID_PATH); } catch { /* already gone */ }
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
async function sendEmbedRequest(sock: ReturnType<typeof connect>, text: string, kind: "document" | "query", timeoutMs: number): Promise<number[] | null> {
|
|
293
|
+
return new Promise((resolve) => {
|
|
294
|
+
let resolved = false;
|
|
295
|
+
const settle = (v: number[] | null) => { if (!resolved) { resolved = true; resolve(v); try { sock.destroy(); } catch { /* */ } } };
|
|
296
|
+
let buf = "";
|
|
297
|
+
const timer = setTimeout(() => settle(null), timeoutMs);
|
|
195
298
|
sock.on("data", (chunk: Buffer) => {
|
|
196
299
|
buf += chunk.toString("utf-8");
|
|
197
300
|
const nl = buf.indexOf("\n");
|
|
198
|
-
if (nl
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
301
|
+
if (nl === -1) return;
|
|
302
|
+
clearTimeout(timer);
|
|
303
|
+
try {
|
|
304
|
+
const resp = JSON.parse(buf.slice(0, nl));
|
|
305
|
+
// Daemon may return `{ error: "unknown op" }` from an older protocol — graceful NULL.
|
|
306
|
+
if (!Array.isArray(resp.embedding)) return settle(null);
|
|
307
|
+
// JSON-over-socket is untrusted at runtime. Reject any non-finite
|
|
308
|
+
// element (string, null, NaN, Infinity, object). Without this, a
|
|
309
|
+
// misbehaving daemon could ship bad values that flow into the
|
|
310
|
+
// ARRAY[...]::FLOAT4[] SQL literal.
|
|
311
|
+
for (const v of resp.embedding) {
|
|
312
|
+
if (typeof v !== "number" || !Number.isFinite(v)) return settle(null);
|
|
313
|
+
}
|
|
314
|
+
settle(resp.embedding);
|
|
315
|
+
} catch { settle(null); }
|
|
206
316
|
});
|
|
207
317
|
sock.on("error", () => { clearTimeout(timer); settle(null); });
|
|
208
318
|
sock.on("close", () => { clearTimeout(timer); settle(null); });
|
|
319
|
+
// Protocol shape comes from src/embeddings/protocol.ts: { op, id, kind, text }.
|
|
320
|
+
// id is a string ("1"), not a number, and the verb field is "op" not "type".
|
|
321
|
+
sock.write(JSON.stringify({ op: "embed", id: "1", kind, text }) + "\n");
|
|
209
322
|
});
|
|
210
323
|
}
|
|
211
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Full spawn-on-miss embedding flow. Returns null on any failure; never
|
|
327
|
+
* throws. 11 edge cases mirror the unit tests in
|
|
328
|
+
* tests/shared/standalone-embed-client.test.ts.
|
|
329
|
+
*/
|
|
330
|
+
async function tryEmbedOverSocket(text: string, kind: "document" | "query"): Promise<number[] | null> {
|
|
331
|
+
// Case 3 — happy path: socket alive, daemon ready.
|
|
332
|
+
let sock = await connectDaemonOnce(1000);
|
|
333
|
+
if (!sock) {
|
|
334
|
+
// Case 1 — binary missing: never spawn.
|
|
335
|
+
if (!existsSync(EMBED_DAEMON_ENTRY)) {
|
|
336
|
+
logHm(`embed: no daemon at ${EMBED_DAEMON_ENTRY} — run 'hivemind embeddings install'`);
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
// Cases 2 / 4 / 5 / 7 / 8 — trySpawn handles them; loser waits.
|
|
340
|
+
trySpawnDaemonInline();
|
|
341
|
+
// Case 9 — poll for socket up to 5s.
|
|
342
|
+
const deadline = Date.now() + 5000;
|
|
343
|
+
let delay = 30;
|
|
344
|
+
while (Date.now() < deadline) {
|
|
345
|
+
await new Promise(r => setTimeout(r, delay));
|
|
346
|
+
delay = Math.min(delay * 1.5, 300);
|
|
347
|
+
if (!existsSync(EMBED_SOCKET_PATH)) continue;
|
|
348
|
+
sock = await connectDaemonOnce(1000);
|
|
349
|
+
if (sock) break;
|
|
350
|
+
}
|
|
351
|
+
if (!sock) {
|
|
352
|
+
// Clean up our placeholder PID so the next pi turn can retry the
|
|
353
|
+
// spawn instead of waiting on us forever.
|
|
354
|
+
maybeCleanupOwnPlaceholderInline();
|
|
355
|
+
logHm(`embed: daemon never opened socket within 5s`);
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
// Cases 10 / 11 — request timeout / daemon error → null.
|
|
360
|
+
const v = await sendEmbedRequest(sock, text, kind, 5000);
|
|
361
|
+
if (v === null) logHm(`embed: daemon returned null (timeout or error)`);
|
|
362
|
+
return v;
|
|
363
|
+
}
|
|
364
|
+
|
|
212
365
|
// ---------- summary state + wiki-worker spawn ---------------------------------
|
|
213
366
|
//
|
|
214
367
|
// Mirror of src/hooks/summary-state.ts (same dir, same JSON shape, shared
|
|
@@ -569,39 +722,12 @@ async function embed(text: string): Promise<number[] | null> {
|
|
|
569
722
|
logHm(`embed: skipped (empty text)`);
|
|
570
723
|
return null;
|
|
571
724
|
}
|
|
572
|
-
//
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
// 2) no daemon binary deposited → fallback NULL
|
|
579
|
-
if (!existsSync(EMBED_DAEMON_ENTRY)) {
|
|
580
|
-
logHm(`embed: no daemon at ${EMBED_DAEMON_ENTRY} — run 'hivemind embeddings install'`);
|
|
581
|
-
return null;
|
|
582
|
-
}
|
|
583
|
-
// 3) spawn the canonical daemon detached; daemon's own pidfile lock guards
|
|
584
|
-
// against double-spawn if multiple pi turns race.
|
|
585
|
-
logHm(`embed: spawning daemon at ${EMBED_DAEMON_ENTRY}`);
|
|
586
|
-
try {
|
|
587
|
-
spawn(process.execPath, [EMBED_DAEMON_ENTRY], { detached: true, stdio: "ignore" }).unref();
|
|
588
|
-
} catch (e: any) {
|
|
589
|
-
logHm(`embed: spawn failed: ${e?.message ?? e}`);
|
|
590
|
-
return null;
|
|
591
|
-
}
|
|
592
|
-
// 4) poll for the socket up to ~5s, then retry the embed once
|
|
593
|
-
for (let i = 0; i < 25; i++) {
|
|
594
|
-
await new Promise(r => setTimeout(r, 200));
|
|
595
|
-
if (existsSync(EMBED_SOCKET_PATH)) {
|
|
596
|
-
v = await tryEmbedOverSocket(text, "document");
|
|
597
|
-
if (v !== null) {
|
|
598
|
-
logHm(`embed: ok after spawn (dims=${v.length}, polls=${i + 1})`);
|
|
599
|
-
return v;
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
logHm(`embed: timed out after spawn (5s)`);
|
|
604
|
-
return null;
|
|
725
|
+
// Single round-trip: tryEmbedOverSocket spawns the daemon on miss
|
|
726
|
+
// (O_EXCL race-safe, mirrors src/embeddings/standalone-embed-client.ts)
|
|
727
|
+
// and embeds in one call. Returns null on any failure.
|
|
728
|
+
const v = await tryEmbedOverSocket(text, "document");
|
|
729
|
+
if (v !== null) logHm(`embed: ok (dims=${v.length})`);
|
|
730
|
+
return v;
|
|
605
731
|
}
|
|
606
732
|
|
|
607
733
|
function embedSqlLiteral(emb: number[] | null): string {
|