Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(cloud): gate lookup recording first and keep savings-only events free of session text

Shrish Dwivedibe1acf76a057
diff --git a/src/cloud/lookup-activity.ts b/src/cloud/lookup-activity.ts
index d0813acd..1db4a1af 100644
--- a/src/cloud/lookup-activity.ts
+++ b/src/cloud/lookup-activity.ts
@@ -9,7 +9,7 @@ import { noteAuthor } from "../notes/git-facts.js";
import { noteName } from "../notes/notes.js";
import { sessionKey } from "./nudge.js";
import { readCloud, type Cloud } from "./workspace.js";
-import { recordActivity } from "./workspace-sync.js";
+import { recordActivity, records } from "./workspace-sync.js";
/** The file part of a hit's `file:Lx-Ly` pointer. */
function pointerFile(pointer: string): string {
@@ -19,22 +19,29 @@ function pointerFile(pointer: string): string {
/**
* Queue one search event for `r`, when it showed a learning or saved tokens.
* Uses are counted from the learnings shown only; `saved_tokens` rides along.
- * Never throws.
+ * Query and files are sent only with a shown learning. Never throws.
*/
export function recordLookup(r: AskResult, repo: string, cloud: Cloud | null = readCloud()): void {
try {
+ // Cheapest checks first: most lookups are on a machine that isn't connected,
+ // or in a repo outside the workspace, and must cost nothing here.
+ const key = repoPlace(repo).key;
+ if (!records(cloud, key)) return;
const shown = r.notes ?? [];
const saved = askSavedTokens(r);
if (!shown.length && saved <= 0) return;
const me = noteAuthor(repo);
+ // A lookup that showed no learning leaves with its savings only: the query
+ // and the files it touched are session text, and stay on this machine.
+ const used = shown.length > 0;
recordActivity(
{
- repo: repoPlace(repo).key,
+ repo: key,
kind: "search",
asker: me,
session: sessionKey(repo) ?? `${me}-${new Date().toISOString().slice(0, 10)}`,
- query: r.query,
- files: [...new Set(r.hits.map((h) => pointerFile(h.pointer)))],
+ query: used ? r.query : "",
+ files: used ? [...new Set(r.hits.map((h) => pointerFile(h.pointer)))] : [],
shown_paths: shown.map((h) => noteName(h.note)),
shown_authors: shown.map((h) => h.note.author),
...(saved > 0 ? { saved_tokens: saved } : {}),
diff --git a/src/cloud/workspace-sync.ts b/src/cloud/workspace-sync.ts
index 23228f1b..a9bf5e36 100644
--- a/src/cloud/workspace-sync.ts
+++ b/src/cloud/workspace-sync.ts
@@ -148,13 +148,18 @@ function sendable(e: ActivityEvent, cloud: Cloud): boolean {
return cloud.repos.includes(e.repo) && Date.parse(e.at) >= (cloud.linkedAt || 0);
}
+/** Whether uses of `repo` are recorded at all: connected by `pair`/login (not `connect`) and the repo is the workspace's. */
+export function records(cloud: Cloud | null, repo: string): cloud is Cloud {
+ return !!cloud && cloud.via !== "connect" && cloud.repos.includes(repo);
+}
+
/**
* Remember one use of the learnings, to send with the next sync: only while
* this machine is connected, and only for a repo in the workspace. Not
* connected, or another repo, and nothing is written. Never throws.
*/
export function recordActivity(e: ActivityEvent, cloud: Cloud | null = readCloud()): void {
- if (!cloud || cloud.via === "connect" || !cloud.repos.includes(e.repo)) return;
+ if (!records(cloud, e.repo)) return;
try {
const p = outboxPath();
appendFileSync(p, `${JSON.stringify(e)}\n`);
diff --git a/test/activity-saved-tokens.test.ts b/test/activity-saved-tokens.test.ts
index fcb4345e..1ff5e9e1 100644
--- a/test/activity-saved-tokens.test.ts
+++ b/test/activity-saved-tokens.test.ts
@@ -66,7 +66,8 @@ test("a lookup that saved tokens is queued with them, though no learning was sho
assert.equal(events[0].kind, "search");
assert.equal(events[0].saved_tokens, 15392);
assert.deepEqual(events[0].shown_paths, []);
- assert.equal(events[0].query, "retry count");
+ assert.equal(events[0].query, "", "session text stays on this machine");
+ assert.deepEqual(events[0].files, []);
});
test("no estimate and no learning shown: nothing is queued", () => {
@@ -89,6 +90,8 @@ test("a learning shown without an estimate is still a use, with no saved_tokens"
const events = queued();
assert.equal(events.length, 1);
assert.deepEqual(events[0].shown_paths, ["2026-10-09-jitter-kumar.md"]);
+ assert.equal(events[0].query, "retry count", "unchanged: a shown learning keeps its query and files");
+ assert.deepEqual(events[0].files, ["client.go"]);
assert.equal("saved_tokens" in events[0], false);
});
@@ -103,3 +106,41 @@ test("an estimate with a learning shown carries both", () => {
assert.ok(e.saved_tokens > 0);
assert.deepEqual(e.shown_paths, ["2026-10-09-jitter-kumar.md"]);
});
+
+/** A result whose `saved` is counted when read, to prove a refused lookup does no work. */
+function countingReads(r: AskResult): { reads: () => number } {
+ let n = 0;
+ const saved = r.saved;
+ Object.defineProperty(r, "saved", { get: () => (n++, saved), configurable: true });
+ return { reads: () => n };
+}
+
+test("not connected: nothing is computed or queued", () => {
+ reset();
+ const { repo } = repoAndCloud();
+ const r = resultSaving(15392);
+ const c = countingReads(r);
+ recordLookup(r, repo, null);
+ assert.equal(c.reads(), 0);
+ assert.equal(queued().length, 0);
+});
+
+test("a connection made by `connect` records nothing", () => {
+ reset();
+ const { repo, cloud } = repoAndCloud();
+ const r = resultSaving(15392);
+ const c = countingReads(r);
+ recordLookup(r, repo, { ...cloud, via: "connect" });
+ assert.equal(c.reads(), 0);
+ assert.equal(queued().length, 0);
+});
+
+test("a repo outside the workspace records nothing", () => {
+ reset();
+ const { repo, cloud } = repoAndCloud();
+ const r = resultSaving(15392);
+ const c = countingReads(r);
+ recordLookup(r, repo, { ...cloud, repos: ["github.com/acme/other"] });
+ assert.equal(c.reads(), 0);
+ assert.equal(queued().length, 0);
+});