Files
loki/scripts/test/project-interview.ts
CatoandClaude Fable 5.1 49e668d418 feat(projects): interview the owner before building from one sentence (#799)
* feat(projects): interview the owner before building from one sentence

"Build it with Loki" created a project and immediately ran the kickoff —
profile extraction, milestones, repository, agent — on whatever the
OrangeCat page happened to say. For a service listing that is a title, a
price and one line of public description. Everything the kickoff produces
is a function of that text, so a thin brief produced a thin everything:
`isThinBrief` already named the failure and had nothing to do about it,
and the extractor wrote "Unknown" into the fields it could not infer.

The one person who knows the answers is right there, so ask them. A new
project from a handoff now lands on five short questions — who it is for,
what they do today, what this does for them, what the build must use,
what makes the first version worth shipping — and reaches the kickoff a
few sentences later with a profile worth building from.

- lib/project-interview.ts is the SSOT for which gaps are worth asking
  about. Pure: the same plan drives the route, the card and the tests, so
  a question can never be asked about a field already answered. Only the
  three essential gaps decide whether to interrupt; a missing stack
  preference rides along but never starts an interview.
- Answers are stored verbatim — as the attribute each one answers, and
  appended to the description, which is what the profile extractor, the
  roadmap step, the dispatch prompt and RAG actually read. No model sits
  between the owner's words and what gets stored; extraction happens
  after, from a brief that now says something.
- lib/project-interview-ai.ts only REWORDS the questions for this
  project. It cannot change which are asked, and every failure path
  returns the bank unchanged: generic wording beats no interview.
- `hasAnswer` guards both ends. "Unknown" is a gap worth asking about,
  and a typed "n/a" is a skip — a stored one is truthy, satisfies every
  "is this filled?" check downstream, and briefs an agent with nothing.
- Skipping is always available and never punished. Skip everything and
  you land exactly where this flow landed before.

A project that was merely LINKED to an existing Loki project still gets
neither flag — linking is not consent to start work on it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RU7EqR1TPicrJGv46cLDaH

* fix(interview): an error is never a wall, and a slow model costs wording, not waiting

Two places the interview could leave someone standing still:

- If the questions failed to load, the card showed the error and nothing
  else. The questions are an improvement to the build, not a condition of
  it, so the build is now one tap away from the error: "Skip to the build"
  lands exactly where the old one-click path landed.
- Tailoring the questions to the project could take 20s, all of it spent
  on "Reading your page…". The plain questions are already good. 8s now:
  a slow model degrades the wording, never the wait.

And the rule both follow, written into CLAUDE.md where the next agent reads
it: a gate records; it never blocks. Anything that pauses a person shows the
way forward on the same screen.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RU7EqR1TPicrJGv46cLDaH

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-09-20 07:57:48 -07:00

145 lines
6.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Inline tests for the interview planner (lib/project-interview.ts).
*
* Why this is worth testing: everything the interview produces is written
* straight into the profile that briefs an agent, with no model in between. So
* the two failures that matter are silent ones — asking about a field somebody
* already answered (which teaches the owner the questions are noise), and
* storing a non-answer like "n/a", which is truthy and therefore satisfies
* every "is this filled?" check downstream while saying nothing. Both are
* invisible in the UI and expensive in the dispatch prompt.
*
* Pure: no database, no model, no env. Run: npx tsx scripts/test/project-interview.ts
*/
import { PROJECT_ATTR } from "@/config/project-attrs";
import {
INTERVIEW_ANSWER_MAX,
INTERVIEW_FIELDS,
MAX_INTERVIEW_QUESTIONS,
interviewBrief,
mergeInterviewIntoDescription,
needsInterview,
planInterview,
usableAnswers,
} from "@/lib/project-interview";
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
// ── The plan ────────────────────────────────────────────────────────────────
const empty = planInterview({ attrs: {} });
assert(
empty.length === INTERVIEW_FIELDS.length && empty.length <= MAX_INTERVIEW_QUESTIONS,
"a project with no profile is asked every question, and never more than the cap",
);
assert(
empty[0].id === PROJECT_ATTR.CUSTOMERS,
"who it is for comes first — every later question is answered in terms of it",
);
const answered = planInterview({
attrs: { [PROJECT_ATTR.CUSTOMERS]: "Swiss cafés with 2–10 staff" },
});
assert(
!answered.some((field) => field.id === PROJECT_ATTR.CUSTOMERS),
"a field with a real answer is not asked about again",
);
// The exact live failure `hasAnswer` exists for: the extractor fills fields it
// could not infer with "Unknown" rather than omitting them. That is precisely
// the field worth asking a human about, so it must NOT count as answered.
const placeholder = planInterview({
attrs: {
[PROJECT_ATTR.CUSTOMERS]: "Unknown",
[PROJECT_ATTR.PROBLEM]: " ",
[PROJECT_ATTR.SOLUTION]: "n/a",
},
});
assert(
placeholder.length === INTERVIEW_FIELDS.length,
"Unknown / blank / n/a are gaps, not answers — ask about all of them",
);
// ── Whether to interrupt at all ─────────────────────────────────────────────
assert(needsInterview({ attrs: {} }), "a blank profile needs the interview");
assert(
!needsInterview({
attrs: {
[PROJECT_ATTR.CUSTOMERS]: "Swiss cafés",
[PROJECT_ATTR.PROBLEM]: "Bookings arrive by WhatsApp and get lost",
[PROJECT_ATTR.SOLUTION]: "One inbox that turns messages into bookings",
},
}),
"the three essential fields answered means no interruption — a missing stack preference is not a reason to stop someone on their way to work",
);
assert(
needsInterview({
attrs: { [PROJECT_ATTR.STACK]: "Next.js", [PROJECT_ATTR.DEFINITION_OF_DONE]: "It deploys" },
}),
"optional fields answered do not substitute for the essential ones",
);
// ── What gets stored ────────────────────────────────────────────────────────
const usable = usableAnswers({
[PROJECT_ATTR.CUSTOMERS]: " Swiss cafés with 2–10 staff ",
[PROJECT_ATTR.PROBLEM]: "n/a",
[PROJECT_ATTR.SOLUTION]: "",
[PROJECT_ATTR.STACK]: "x".repeat(INTERVIEW_ANSWER_MAX + 50),
// A key nobody was asked about. This route writes profile attributes, so a
// hand-rolled POST must not be able to set an arbitrary one through it.
mission: "smuggled in",
} as Record<string, string>);
assert(usable[PROJECT_ATTR.CUSTOMERS] === "Swiss cafés with 2–10 staff", "answers are trimmed");
assert(!(PROJECT_ATTR.PROBLEM in usable), "'n/a' is a skip wearing an answer's clothes");
assert(!(PROJECT_ATTR.SOLUTION in usable), "an empty answer is a skip");
assert(
usable[PROJECT_ATTR.STACK]?.length === INTERVIEW_ANSWER_MAX,
"a long answer is clamped, not rejected",
);
assert(!("mission" in usable), "only the fields the interview asks about can be written");
// ── The brief the answers become ────────────────────────────────────────────
const brief = interviewBrief({
[PROJECT_ATTR.SOLUTION]: "One inbox that turns messages into bookings",
[PROJECT_ATTR.CUSTOMERS]: "Swiss cafés",
});
assert(
brief.indexOf("Swiss cafés") < brief.indexOf("One inbox"),
"the brief reads in question order, not in the order the answers arrived",
);
assert(interviewBrief({}) === "", "a fully skipped interview writes nothing");
const base = "Booking help for cafés. CHF 100.";
const once = mergeInterviewIntoDescription(base, { [PROJECT_ATTR.CUSTOMERS]: "Swiss cafés" });
assert(once.startsWith(base), "the public description is kept, not replaced");
assert(once.includes("Swiss cafés"), "the owner's words are appended to the brief");
// Re-running the interview must not stack a second copy: the owner would read
// their own answers twice and every extraction downstream would weight them twice.
const twice = mergeInterviewIntoDescription(once, {
[PROJECT_ATTR.CUSTOMERS]: "Zurich cafés only",
});
assert(!twice.includes("Swiss cafés"), "a re-run replaces the previous block");
assert(twice.includes("Zurich cafés only"), "a re-run stores the new answer");
assert(
twice.split("From the owner:").length === 2,
"there is exactly one owner block however many times the interview runs",
);
assert(
mergeInterviewIntoDescription(once, {}) === base,
"skipping everything on a re-run leaves the original description alone",
);
assert(
mergeInterviewIntoDescription(null, { [PROJECT_ATTR.CUSTOMERS]: "Swiss cafés" }).startsWith(
"From the owner:",
),
"a project with no description at all still gets a brief",
);
console.log("✓ project interview");