Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
/**
* Phase 1a's readers of what the person typed: a save request, the scope
* words in it, the answer to an offer (a closed list of whole-message forms:
* "ok now fix the lint errors" is not a yes), a correction, and the one form
* typed text is ever stored in, its hash.
*
* (`test/trace-typed.test.ts` is phase 0's, for whose words a transcript line holds.)
*/
import { test } from "node:test";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { ownText } from "../src/trace/claude.js";
import { NOT_SAVE, SAVE_REQUEST, classifyAnswer, isCorrection, isSaveRequest, scopeWords, typedPrompt, typedSha } from "../src/trace/typed.js";
test("typedPrompt is phase 0's ownText, null when that is empty", () => {
const cases = [
"publish 1.4.3",
'<ide_opened_file>The user opened /home/dev/shop/a.ts</ide_opened_file>\nwhy is the total negative',
"<task-notification>done</task-notification>",
"Caveat: the messages below were generated by the user while running local commands.",
"<system-reminder>x</system-reminder>",
" ",
'<pasted_content id="1">a long log</pasted_content id="1">',
'look at this <pasted_content id="1">log</pasted_content id="1">',
];
for (const raw of cases) assert.equal(typedPrompt(raw), ownText(raw) || null, raw);
assert.equal(typedPrompt("publish 1.4.3"), "publish 1.4.3");
assert.equal(typedPrompt("<task-notification>done</task-notification>"), null);
});
test("save requests: the spec's phrasings", () => {
for (const t of [
"save this as a skill", "can you keep what you just did as a skill?", "make a skill out of that", "save these steps for next time",
"Save this as a skill please", "turn that into a skill", "keep it as a skill", "make this into a skill", "save the steps as a skill",
"remember how you did this", "remember how to do that", "make a skill from this", "keep that for next time", "ok great, save what you did as a skill for the team",
]) {
assert.equal(isSaveRequest(t), true, t);
assert.match(t, SAVE_REQUEST);
}
});
test("near misses are not save requests", () => {
for (const t of [
"save this file", "don't save this as a skill", "save it to disk", "keep this as a note", "do not make that a skill", "never keep this as a skill",
"no need to save this as a skill", "save the skill file I wrote", "what is a skill", "remember to run the tests",
]) assert.equal(isSaveRequest(t), false, t);
assert.match("don't save this as a skill", NOT_SAVE);
// Over 25 words, it is a task that mentions skills.
assert.equal(isSaveRequest(`save this as a skill ${"and then also do this other thing ".repeat(4)}`), false);
// Inside pasted code or a quoted line, it is not the person asking.
assert.equal(isSaveRequest("what does this do\n```\nsave this as a skill\n```"), false);
assert.equal(isSaveRequest("> save this as a skill\nwhy does it say that"), false);
});
test("questions about saving, refusals, take-backs and maybes are not save requests", () => {
const wrong = [
// Status and history: what happened, not what to do.
"did you save it?", "how did we save the release steps?", "is it saved?", "did you save this as a skill?", "have you saved it as a skill?",
"did you make that into a skill?", "did you keep it as a skill", "was it saved as a skill?", "how did you save that as a skill",
"where did you save it as a skill", "when did we save this as a skill", "you saved this as a skill, right?", "did u save this as a skill",
"how do skills work? did you save this as a skill?", "did you remember how to do this?", "is it a skill now?",
// Why: a complaint or a question.
"why didn't you save it as a skill?", "why did you save this as a skill?",
// Not saving, said many ways.
"I don't want to save this as a skill", "please don't ever save this as a skill", "you shouldn't save this as a skill", "we won't keep this as a skill",
"never ever save that as a skill", "do NOT save this as a skill", "Don’t save this as a skill", "dont make this a skill", "I'd rather not keep this as a skill",
"don't bother saving this as a skill", "no, don't make it a skill",
// Said, then taken back.
"save this as a skill?? no wait, don't", "save these steps for next time? nah", "save this as a skill... actually no", "save this as a skill. never mind", "not yet, save this as a skill later",
// Wondering or asking how, not asking for it.
"what happens if I save this as a skill?", "should we save this as a skill?", "can you explain how to save this as a skill", "how do I save this as a skill?",
// Other things.
"save this file", "save the changes", "commit and save", "save it as skill.md", "save the file as skill.md", "turn this into a skill issue",
"remember how you did this last time? do it again",
].filter((t) => isSaveRequest(t));
assert.deepEqual(wrong, []);
// Still asks.
for (const t of ["can you save this as a skill?", "could you keep this as a skill", "please make a skill out of that", "ok, save this as a skill", "save this as a skill so I don't have to explain it again"]) {
assert.equal(isSaveRequest(t), true, t);
}
});
test("scopeWords", () => {
const cases: Array<[string, string]> = [
["save this as a skill", "this repo"],
["save this as a skill for every repo", "every repo"],
["always keep this as a skill", "every repo"],
["save it as a skill for all my repos", "every repo"],
["save this as a skill on this machine", "every repo"],
["save this as a skill for the team", "team"],
["make a skill out of that for our team", "team"],
["save this as a skill for the whole repo", "repo"],
["save this as a skill in this repo so everyone has it", "repo"],
["save this as a skill in this repo", "this repo"],
];
for (const [t, want] of cases) assert.equal(scopeWords(t), want, t);
});
test("classifyAnswer: yes, only in its whole-message forms", () => {
for (const t of [
"y", "yes", "Yes!", "yeah", "yep", "yup", "sure", "ok", "okay.", "yes please", "Yes, please", "yes thanks", "sure, go ahead", "go ahead", "do it", "please do",
"save it", "keep it", "keep them", "save them", "sounds good", "yes, keep it", "ok save it as a skill", "yes keep them please", "Yes 👍", "yeah do it",
"looks great, save it.", "Looks right, keep it", "perfect, save it as a skill", "great, keep them",
]) {
assert.equal(classifyAnswer(t, false), "yes", t);
assert.equal(classifyAnswer(t, true), "yes", t);
}
});
test("classifyAnswer: what must never count as a yes", () => {
for (const t of [
"ok now fix the lint errors", "sure, but run the tests first", "yes merge it", "yes please push", "yes please update prod", "Yes can you please ship that... right?",
"yes and also bump the version", "", "👍", "looks good", "looks good, now push it", "great, save it to disk", "nice, keep it running",
]) {
assert.equal(classifyAnswer(t, false), "other", t);
assert.equal(classifyAnswer(t, true), "other", t);
}
});
/**
* Replies a person might type right after an offer. Only a plain whole-message
* assent is a yes: anything that carries another instruction, a question, a
* quote, a pasted block or a long paste is not, whatever it starts with. The
* cost of a wrong "other" is one more offer later; the cost of a wrong "yes" is
* a skill the person never agreed to.
*/
const REPLIES: Array<[string, "yes" | "no" | "show" | "!yes"]> = [
// Plain assents.
["yes", "yes"], ["y", "yes"], ["Y", "yes"], ["yes.", "yes"], ["Yes!", "yes"], ["YES", "yes"], ["ok", "yes"], ["OK!", "yes"], ["okay", "yes"], ["OK.", "yes"],
["yes 🙂", "yes"], ["yes 👍", "yes"], ["keep it", "yes"], ["save it", "yes"], ["sure", "yes"], ["Sure!", "yes"], ["yep", "yes"], ["yeah", "yes"], ["yup", "yes"],
["yes please", "yes"], ["Yes, please.", "yes"], ["go ahead", "yes"], ["do it", "yes"], ["please do", "yes"], ["sounds good", "yes"], ["yes, keep it", "yes"],
["sure, save it as a skill", "yes"], ["yes keep them", "yes"], ["yes thanks", "yes"], [" yes ", "yes"], ["yes\n", "yes"], ["yes\nplease", "yes"], ["yes!!!", "yes"],
["yes...", "yes"], ["sure, keep that please", "yes"], ["ok save this", "yes"], ["yes, save them as a skill please", "yes"], ["looks great, save it", "yes"], ["perfect, keep it", "yes"],
// Another instruction rides along.
["yes but first run the tests", "!yes"], ["sure thing, and also rename the file", "!yes"], ["yes to the first one", "!yes"],
["yes, after the tests pass", "!yes"], ["yes 🙂 and push", "!yes"],
["yes; rm -rf build", "!yes"], ["yes && npm publish", "!yes"], ["yes. push it", "!yes"], ["yes - and deploy", "!yes"], ["yes!! ship it", "!yes"], ["yes ship it", "!yes"],
["do it again", "!yes"], ["go ahead and push", "!yes"], ["keep it simple", "!yes"], ["keep it running", "!yes"], ["save it to disk", "!yes"], ["save it somewhere", "!yes"],
["yes, keep it running", "!yes"], ["sure, keep it as a skill and push", "!yes"], ["sure go ahead and deploy", "!yes"], ["yes go ahead with the deploy", "!yes"],
["yes do it then publish", "!yes"], ["ok do it and push", "!yes"], ["yes please do it", "!yes"], ["yes (but rename it)", "!yes"], ["yes [push too]", "!yes"],
["yes 🚀🚀 deploy", "!yes"], ["yes\tplease push", "!yes"], ["yes keep it and push", "!yes"], ["keep it, and push to prod", "!yes"], ["save them all", "!yes"],
["keep them both", "!yes"], ["yes #push", "!yes"], ["yes // and push", "!yes"], ["yes I guess", "!yes"], ["yes, but not now", "!yes"], ["yes sir", "!yes"],
// More than one line, a quote, a pasted block, a long paste.
["yes\nalso push it", "!yes"], ["yes\n\nand bump the version", "!yes"], ["> Keep the 5 that worked as a skill? (yes / show me / no)\nyes", "!yes"],
["yes\n> also push to prod", "!yes"], ["yes\n```\nnpm publish\n```", "!yes"], ["```\nnpm ERR! code E401\n```\nyes", "!yes"], ["```\nlog line\nyes\n```", "!yes"],
["error: thing failed\nretrying\nyes", "!yes"], [`yes\n${"a".repeat(820)}`, "!yes"], [`yes ${"x".repeat(900)}`, "!yes"], ["yes [pasted text]", "!yes"],
// Questions are not assent.
["yes?", "!yes"], ["ok?", "!yes"], ["do it?", "!yes"], ["save it?", "!yes"], ["keep it?", "!yes"], ["sure?", "!yes"],
// An acknowledgement is not an assent.
["ok thanks", "!yes"], ["ok, thanks", "!yes"], ["okay thank you", "!yes"], ["thanks", "!yes"], ["thank you", "!yes"], ["great", "!yes"], ["nice", "!yes"],
["perfect", "!yes"], ["looks good", "!yes"], ["lgtm", "!yes"], ["cool", "!yes"],
// Not words on the list, other languages, look-alikes.
["yeah no", "!yes"], ["no worries, go ahead", "!yes"], ["haan", "!yes"], ["sí", "!yes"], ["si", "!yes"], ["oui", "!yes"], ["ja", "!yes"], ["k", "!yes"], ["kk", "!yes"],
["ok ok", "!yes"], ["yes yes", "!yes"], ["yes pls", "!yes"], ["ya", "!yes"], ["yass", "!yes"], ["aye", "!yes"], ["maybe", "!yes"], ["sure why not", "!yes"],
["I said yes", "!yes"], ["not yes", "!yes"], ["yes, no", "!yes"], ["no, go ahead", "!yes"], ["ship it", "!yes"], ["save", "!yes"], ["keep", "!yes"],
["\"yes\"", "!yes"], ["'yes'", "!yes"], ["`yes`", "!yes"], ["yes", "!yes"], ["yes", "!yes"], ["yes", "!yes"], ["yеs", "!yes"],
// No and show.
["no", "no"], ["n", "no"], ["nah", "no"], ["not now", "no"], ["no thanks", "no"], ["nope", "no"], ["skip it", "no"], ["never mind", "no"], ["pass", "no"],
["no thank you", "no"], ["don't", "no"], ["not now thanks", "no"], ["don't save it", "no"], ["no, don't keep it", "no"], ["Don’t save it.", "no"], ["no don't", "no"],
["show me", "show"], ["what are the steps", "show"], ["let me see it", "show"], ["show me the steps please", "show"], ["what are the steps?", "show"],
];
test("classifyAnswer: an adversarial table of replies to an offer", () => {
assert.ok(REPLIES.length >= 100, `${REPLIES.length} replies`);
const wrong: string[] = [];
for (const [t, want] of REPLIES) {
for (const afterShow of [false, true]) {
const got = classifyAnswer(t, afterShow);
const ok = want === "!yes" ? got !== "yes" : want === "show" && afterShow ? got === "other" : got === want;
if (!ok) wrong.push(`${JSON.stringify(t.slice(0, 50))} (afterShow ${afterShow}): ${got}, want ${want}`);
}
}
assert.deepEqual(wrong, []);
});
test("classifyAnswer: show me once, then it is other", () => {
for (const t of ["show me", "show", "show me the steps", "show it", "show me the draft", "show me first please", "let me see", "let me see it", "what are the steps", "see the steps", "what's in it", "whats in it"]) {
assert.equal(classifyAnswer(t, false), "show", t);
assert.equal(classifyAnswer(t, true), "other", t);
}
});
test("classifyAnswer: no", () => {
for (const t of ["n", "no", "No.", "nope", "nah", "not now", "no thanks", "no, thanks", "no thank you", "don't", "dont", "skip", "skip it", "nevermind", "never mind", "pass", "nope, thanks"]) {
assert.equal(classifyAnswer(t, false), "no", t);
}
});
test("classifyAnswer: a correction, otherwise other", () => {
for (const t of ["no, use the ssh remote", "that's wrong, push to beta first", "actually, run the tests first", "you keep pushing to prod first", "i told you to use ssh"]) {
assert.equal(classifyAnswer(t, false), "correction", t);
}
assert.equal(classifyAnswer("ff to prod now", false), "other");
});
test("isCorrection (ported)", () => {
assert.equal(isCorrection("no, I meant the other file"), true);
assert.equal(isCorrection("stop doing that"), true);
assert.equal(isCorrection("it should be beta not prod"), true);
assert.equal(isCorrection("you did it again?"), true);
assert.equal(isCorrection("push to beta"), false);
assert.equal(isCorrection(`no, ${"x".repeat(400)}`), false, "a task, not a correction");
});
test("typedSha: sha256 hex of the NFC text, trimmed, spaces collapsed", () => {
const a = typedSha("yes please");
assert.match(a, /^[0-9a-f]{64}$/);
assert.equal(typedSha(" yes please\n"), a);
assert.equal(typedSha("yes\tplease"), a);
assert.notEqual(typedSha("Yes please"), a);
assert.equal(typedSha("café"), typedSha("café"));
assert.equal(a, createHash("sha256").update("yes please").digest("hex"));
});