fix(accumulator): output contract travels with every run; real markdown in the lab

Custom fold instructions replaced the entire prompt, including the
structural rules validate_output() enforces — so any task-only prompt
('what is this project about?') was a guaranteed refusal. The schema's
contract is now rendered from ArtifactSchema::contract_prompt() and
composed into every model input after the instructions; instructions
are task focus only. Verified live: the exact instructions that refused
now fold with conforming sections and citations.

Lab: replace the hand-rolled artifact renderer (which left **bold** and
Signed-off-by: Riley Crane <rileycrane@squareup.com>
--- literal) with react-markdown + remark-gfm, citation chips preserved
via a #cite: link rewrite; styles for hr/blockquote/pre/table.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Riley Crane <rileycrane@squareup.com>
This commit is contained in:
Riley Crane
2026-08-31 23:10:07 -07:00
committed by Sol
parent e2065667da
commit 936fc522b3
7 changed files with 1016 additions and 76 deletions

View File

@@ -136,9 +136,14 @@ pub fn plan_run(
transcript.push_str("\n\n--- SOURCE EVENT IDS ---\n");
transcript.push_str(&ids.join("\n"));
}
// Instructions are the task; the schema contract is non-negotiable and
// travels with every run — otherwise custom instructions silently drop
// the structural rules that complete_run() will refuse on.
let model_input = format!(
"{}\n\n--- CONTEXT (time-ordered events) ---\n{}\n",
spec.instructions, transcript
"--- TASK ---\n{}\n\n{}\n\n--- CONTEXT (time-ordered events) ---\n{}\n",
spec.instructions,
schema::CHANNEL_DIGEST_V1.contract_prompt(),
transcript
);
let est = estimate::estimate(&spec.model, model_input.chars().count());
Ok(Plan::Ready(RunPlan {
@@ -395,6 +400,31 @@ mod tests {
assert_eq!(run.estimate.window_fit.fits, Some(true));
}
#[test]
fn custom_instructions_cannot_drop_the_output_contract() {
// A task-only prompt ("what is this project about?") must still carry
// the schema's structural rules, or validation is a guaranteed refusal.
let mut spec = spec();
spec.instructions = "what is this project trying to accomplish".to_string();
let plan = plan_run(
&spec,
None,
&BTreeSet::new(),
vec![sig(&hex_id('b'), 200, "hello")],
&BTreeMap::new(),
)
.expect("plan");
let Plan::Ready(run) = plan else {
panic!("expected Ready");
};
assert!(run
.model_input
.contains("what is this project trying to accomplish"));
assert!(run.model_input.contains("OUTPUT CONTRACT"));
assert!(run.model_input.contains("Working Context, Log"));
assert!(run.model_input.contains("Log is append-only"));
}
#[test]
fn oversized_backlog_chunks_honestly_and_never_seals_unread() {
let spec = spec();

View File

@@ -25,17 +25,39 @@ pub const CHANNEL_DIGEST_V1: ArtifactSchema = ArtifactSchema {
append_sections: &["Log"],
};
/// Default fold instructions for [`CHANNEL_DIGEST_V1`].
///
/// The contract lines are load-bearing: exact H1 headings, only-new log
/// entries (the engine splices history back), and full-id citations copied
/// from the run's SOURCE EVENT IDS list — ids embedded in message text are
/// never citations.
pub const CHANNEL_DIGEST_PROMPT: &str = "Maintain a bounded digest of the anchored selection.\n\
Return markdown with exactly these H1 sections, in this order: Working Context, Log.\n\
Working Context is a concise standing summary rewritten in light of all evidence.\n\
Log: output ONLY new dated entries for this run's evidence — the engine keeps the\n\
standing Log and appends your new entries to it; never repeat prior entries.\n\
For new evidence, cite the source events you actually use as [event:<id>], one id\n\
per bracket, copied in full from the SOURCE EVENT IDS list. Ids that appear inside\n\
message text are never citations. Do not invent facts. Output only the document markdown.";
impl ArtifactSchema {
/// The output contract, rendered as prompt text. [`crate::plan_run`]
/// composes this into every model input *alongside* the spec's
/// instructions, so custom instructions focus the task but can never drop
/// the structural rules that [`crate::validate::validate_output`] will
/// enforce — otherwise a task-only prompt is a guaranteed refusal.
pub fn contract_prompt(&self) -> String {
let mut s = format!(
"OUTPUT CONTRACT (mechanically enforced; nonconforming output is refused and \
nothing persists):\nWhatever the task above asks for, deliver it as markdown \
with exactly these H1 sections, in this order: {}.\n",
self.sections.join(", ")
);
for a in self.append_sections {
s.push_str(&format!(
"{a} is append-only: output ONLY new dated entries for this run's evidence \
— the engine keeps the standing {a} and appends your new entries to it; \
never repeat prior entries.\n"
));
}
s.push_str(
"For new evidence, cite the source events you actually use as [event:<id>], one \
id per bracket, copied in full from the SOURCE EVENT IDS list. Ids that appear \
inside message text are never citations. Do not invent facts. Output only the \
document markdown.",
);
s
}
}
/// Default fold *task* instructions for [`CHANNEL_DIGEST_V1`]. The structural
/// rules live in [`ArtifactSchema::contract_prompt`], which every run gets
/// regardless of what the instructions say.
pub const CHANNEL_DIGEST_PROMPT: &str = "Maintain a bounded digest of the anchored selection. \
Working Context is a concise standing summary rewritten in light of all evidence; the Log \
records dated evidence entries.";

View File

@@ -12,7 +12,9 @@
"dependencies": {
"@tanstack/react-query": "^5.90.5",
"react": "^19.1.0",
"react-dom": "^19.1.0"
"react-dom": "^19.1.0",
"react-markdown": "^10.1.0",
"remark-gfm": "^4.0.1"
},
"devDependencies": {
"@types/react": "^19.1.8",

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { useState, type ReactNode } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { api, fmtTime, shortId } from "../api";
type Props = {
@@ -119,64 +121,40 @@ function EvidenceCard({ id, onClose }: { id: string; onClose: () => void }) {
}
const CITATION = /\[event:([0-9a-f]{64})\]/g;
const CITE_HREF = "#cite:";
/** Minimal renderer for the artifact's markdown-shaped output: headings,
* list items, paragraphs — with `[event:<id>]` turned into citation chips. */
/** Real markdown rendering (GFM) for the artifact's output. `[event:<id>]`
* citations are pre-rewritten into `#cite:` links, which the `a` component
* turns into evidence chips; H1/H2 downshift to fit the pane's hierarchy. */
function renderOutput(output: string, pin: (id: string) => void): ReactNode {
const blocks: ReactNode[] = [];
let list: ReactNode[] = [];
let key = 0;
const flushList = () => {
if (list.length > 0) {
blocks.push(<ul key={key++}>{list}</ul>);
list = [];
}
};
for (const line of output.split("\n")) {
const trimmed = line.trim();
if (trimmed.startsWith("- ") || trimmed.startsWith("* ")) {
list.push(<li key={key++}>{inline(trimmed.slice(2), pin)}</li>);
continue;
}
flushList();
if (trimmed.startsWith("## ")) {
blocks.push(<h4 key={key++}>{inline(trimmed.slice(3), pin)}</h4>);
} else if (trimmed.startsWith("# ")) {
blocks.push(<h3 key={key++}>{inline(trimmed.slice(2), pin)}</h3>);
} else if (trimmed.length > 0) {
blocks.push(<p key={key++}>{inline(trimmed, pin)}</p>);
}
}
flushList();
return blocks;
}
function inline(text: string, pin: (id: string) => void): ReactNode[] {
const out: ReactNode[] = [];
let last = 0;
let key = 0;
CITATION.lastIndex = 0;
let m = CITATION.exec(text);
while (m !== null) {
if (m.index > last) out.push(text.slice(last, m.index));
const id = m[1];
if (id) {
out.push(
<button
key={key++}
className="chip"
title={id}
onClick={() => pin(id)}
>
{shortId(id)}
</button>,
);
}
last = m.index + m[0].length;
m = CITATION.exec(text);
}
if (last < text.length) out.push(text.slice(last));
return out;
const withChips = output.replace(
CITATION,
(_, id: string) => `[${shortId(id)}](${CITE_HREF}${id})`,
);
return (
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
h1: ({ children }) => <h3>{children}</h3>,
h2: ({ children }) => <h4>{children}</h4>,
a: ({ href, children }) => {
if (href?.startsWith(CITE_HREF)) {
const id = href.slice(CITE_HREF.length);
return (
<button className="chip" title={id} onClick={() => pin(id)}>
{children}
</button>
);
}
return (
<a href={href} target="_blank" rel="noreferrer">
{children}
</a>
);
},
}}
>
{withChips}
</ReactMarkdown>
);
}

View File

@@ -84,7 +84,7 @@ function CreateFold({ selection }: { selection: Selection }) {
<label>Instructions</label>
<textarea
rows={2}
placeholder="empty = built-in channel-digest prompt"
placeholder="task focus, e.g. 'track decisions and blockers' — the Working Context / Log output contract always applies"
value={instructions}
onChange={(e) => setInstructions(e.target.value)}
/>

View File

@@ -397,6 +397,40 @@ input[type="checkbox"],
margin: 6px 0;
}
.artifact-body hr {
border: none;
border-top: 1px solid var(--border);
margin: 14px 0;
}
.artifact-body blockquote {
margin: 8px 0;
padding: 2px 12px;
border-left: 3px solid var(--border);
color: var(--ink-3);
}
.artifact-body pre {
background: var(--bg);
border: 1px solid var(--border);
border-radius: 6px;
padding: 10px 12px;
overflow-x: auto;
white-space: pre-wrap;
}
.artifact-body table {
border-collapse: collapse;
margin: 8px 0;
}
.artifact-body th,
.artifact-body td {
border: 1px solid var(--border);
padding: 4px 8px;
text-align: left;
}
.version-strip {
display: flex;
gap: 4px;