Skip to content

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

7.2 KB
Raw Blame
/**
 * Whose words a Claude Code transcript line holds. Only what the person typed
 * can teach Trail anything: compaction summaries, task notices, slash-command
 * wrappers, subagent hand-backs and tool output are text the agent read. IDE
 * and reminder tags around a real prompt are stripped, not a reason to drop it.
 * One filter serves every learning path, cold start's included.
 */
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { isTypedByPerson, typedText } from "../src/trace/claude.js";
import { readSession } from "../src/notes/cold-start.js";
import {
  bashInput, commandWrapper, compactBoundary, compactSummary, hookContext, ideOpened, ideSelection, interrupted, jsonl, localStdout, meta, peer,
  queuedNotification, queuedPrompt, reminder, said, sidechain, taskNotification, text, thinking, title, toolResult, toolUse, typed, typedOld,
} from "./trace-fixtures.js";

test("a typed prompt counts, with or without the origin stamp", () => {
  assert.equal(isTypedByPerson(typed("run the integration tests")), true);
  assert.equal(typedText(typed("run the integration tests")), "run the integration tests");
  assert.equal(isTypedByPerson(typedOld("ff to prod")), true);
  assert.equal(typedText(typedOld([text("ff to prod")])), "ff to prod");
  assert.equal(typedText(typed("  first line\nsecond line  ")), "first line\nsecond line");
});

test("IDE and reminder tags are stripped and the prompt is kept", () => {
  const withFile = typed([ideOpened(), text("why does the total go negative")]);
  assert.equal(isTypedByPerson(withFile), true);
  assert.equal(typedText(withFile), "why does the total go negative");
  const withSelection = typed([ideSelection(), text("this returns 0, fix it")]);
  assert.equal(typedText(withSelection), "this returns 0, fix it");
  const withReminder = typed([reminder(), text("ship it")]);
  assert.equal(typedText(withReminder), "ship it");
  // One string with the tag inline, as older builds wrote it.
  const inline = typedOld(`${ideOpened().text}\nbump the version`);
  assert.equal(typedText(inline), "bump the version");
  // Nothing typed besides the tags: not the person's words.
  assert.equal(isTypedByPerson(typed([ideOpened()])), false);
  assert.equal(typedText(typed([ideOpened(), reminder()])), "");
});

test("pasted blocks become a marker; a paste alone has no typed words", () => {
  const p = typed('look at this <pasted_content id="1">npm ERR! code E401\nnpm ERR! 401 Unauthorized</pasted_content id="1"> and fix it');
  assert.equal(typedText(p), "look at this [pasted text] and fix it");
  const open = typed('look at this <pasted_content id="2">a log that never closes');
  assert.equal(typedText(open), "look at this [pasted text]");
  const only = typed('<pasted_content id="3">a whole page of output</pasted_content id="3">');
  assert.equal(isTypedByPerson(only), false);
  assert.equal(typedText(only), "");
});

test("text the agent read is never the person's", () => {
  const cases: Record<string, object> = {
    "compaction summary": compactSummary(),
    "compaction boundary": compactBoundary(),
    "meta line": meta(),
    "task notification": taskNotification(true),
    "task notification from an older build": taskNotification(false),
    "slash-command wrapper": commandWrapper(),
    "local command output": localStdout(),
    "shell escape": bashInput(),
    "peer hand-back": peer(),
    "subagent line": sidechain(),
    "tool result": toolResult("toolu_1", "ok", false),
    "interruption": interrupted(),
    "headless run": typed("summarise the repo", { entrypoint: "sdk-cli" }),
    "assistant text": said("Done."),
    "assistant thinking": thinking(),
    "tool call": toolUse("toolu_2", "Bash", { command: "ls" }),
    "queued task notice": queuedNotification(),
    "hook context": hookContext("[trail] 2 learnings for this repo"),
    "title row": title(),
    "a wrapper on a human-stamped line": typed("<command-message>plan is running…</command-message>\n<command-name>/plan</command-name>"),
    "a non-human origin": typed("anything", { origin: { kind: "channel" } }),
    "a resumed session preamble": typedOld("This session is being continued from a previous conversation that ran out of context."),
    "a message from another session": typedOld("Another Claude session sent a message: tests are green"),
    "a caveat line": typedOld("Caveat: The messages below were generated by the user while running local commands."),
  };
  for (const [name, entry] of Object.entries(cases)) {
    assert.equal(isTypedByPerson(entry), false, name);
    assert.equal(typedText(entry), "", name);
  }
  for (const junk of [null, undefined, 3, "run the tests", [], {}, { type: "user" }, { type: "user", message: null }, { type: "user", message: { content: 5 } }]) {
    assert.equal(isTypedByPerson(junk), false, JSON.stringify(junk));
    assert.equal(typedText(junk), "");
  }
});

test("only origin decides, never promptSource: the desktop and IDE apps mark typed prompts sdk", () => {
  // The terminal writes promptSource "typed"; the desktop app and the IDE extension write "sdk".
  for (const entrypoint of ["claude-desktop", "claude-vscode", "cli"]) {
    for (const promptSource of ["sdk", "typed", undefined]) {
      const row = typed("bump the version and tag it", { entrypoint, ...(promptSource ? { promptSource } : {}) });
      assert.equal(typedText(row), "bump the version and tag it", `${entrypoint} ${promptSource}`);
    }
  }
  // Older desktop rows: promptSource sdk and no origin at all, judged by their wrappers.
  assert.equal(isTypedByPerson(typedOld("check now?", { entrypoint: "claude-desktop", promptSource: "sdk" })), true);
  // Injected rows carry the same promptSource values; their origin says what they are.
  const notice = { ...taskNotification(true), entrypoint: "claude-desktop", promptSource: "sdk" };
  assert.equal(isTypedByPerson(notice), false);
  assert.equal(isTypedByPerson({ ...peer(), entrypoint: "claude-desktop", promptSource: "sdk" }), false);
  assert.equal(isTypedByPerson(typed("Finished: tests pass.", { entrypoint: "claude-desktop", promptSource: "sdk", origin: { kind: "peer" } })), false);
});

test("a prompt typed while the agent was busy (a queued command) is the person's", () => {
  const q = queuedPrompt("also bump the version");
  assert.equal(isTypedByPerson(q), true);
  assert.equal(typedText(q), "also bump the version");
  const parts = queuedPrompt([ideOpened(), text("and push the tag")]);
  assert.equal(typedText(parts), "and push the tag");
});

test("cold start reads the same filter: an IDE-tagged prompt is kept, a notice is not", () => {
  const dir = mkdtempSync(join(tmpdir(), "trace-typed-"));
  const f = join(dir, "s1.jsonl");
  writeFileSync(
    f,
    jsonl([
      typed([ideOpened(), text("never push to main, open a PR against dev")]),
      said("Opened the PR."),
      taskNotification(false),
      commandWrapper(),
      compactSummary(),
      queuedPrompt("and tag the release"),
      typed([reminder()]),
    ]),
  );
  const s = readSession(f)!;
  assert.deepEqual(
    s.prompts.map((p) => p.text),
    ["never push to main, open a PR against dev", "and tag the release"],
  );
});