Lines 531-571javascript
532 /* skip unreadable */
535 // Final guard: JSON escaping inflates the body slightly, so trim the oldest kept sessions until the
536 // GZIP the body — transcripts are JSON text and compress ~3-4x, so 100MB+ of real sessions fits
537 // under the server cap without dropping ANY of them (big sessions are the richest data). The trim
538 // loop only fires if even the COMPRESSED payload exceeds the cap — for a normal user, never.
539 const { gzipSync } = await import("node:zlib");
540 let body = gzipSync(JSON.stringify({ user_id: id.uid, raw_transcripts: raw }));
541 const HARD_CAP = 90 * 1024 * 1024; // matches the server body cap; compressed size
542 while (body.length > HARD_CAP && Object.keys(raw).length > 1) {
543 const keys = Object.keys(raw); // insertion order = newest first → drop the oldest kept
544 delete raw[keys[keys.length - 1]];
546 body = gzipSync(JSON.stringify({ user_id: id.uid, raw_transcripts: raw }));
548 const kept = Object.keys(raw).length;
549 const plain = JSON.stringify({ user_id: id.uid, raw_transcripts: raw });
550 sp.text(`Building your graph from ${kept} session${kept === 1 ? "" : "s"}…`);
551 const post = (token, gzip) =>
552 fetch(`${API}/ingest`, {
555 "content-type": "application/json",
556 ...(gzip ? { "content-encoding": "gzip" } : {}),
557 ...(token ? { authorization: `Bearer ${token}` } : {}),
559 body: gzip ? body : plain,
562 let r = await post(id.access_token, true);
563 // SELF-HEAL on 401: token was stale AND refresh didn't recover it (refresh_token also expired).
564 // Re-run the Google login ONCE for a fresh session, then retry — instead of dead-ending the user.
565 if (r.status === 401 && !process.env.SKALPEL_USER) {
566 sp.text("Session expired — re-opening sign-in…");
567 spawnSync("node", [join(__dir, "login.mjs")], { stdio: "inherit" });
568 const fresh = await identity();
HighSame File Env Network Execution
A single source file combines environment access, network access, and code or shell execution; review context before blocking.
setup.mjsView on unpkg · L551 569 if (fresh?.access_token) r = await post(fresh.access_token, true);
571 // COMPAT: if a server that predates gzip decompression 5xx's on the gzipped body, retry it plain