Public: anyone can read this; the crew can change it.

The OpenAI runner gets the same history record as a fresh Talk session #16931

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

talk/fresh-history-openai → main 0 commits

  • Merged. talk/fresh-history-openai was merged into main as 89e01fa.

Review

Sign in to review this change.
3 files changed +68 −20
src/crew/conversation.ts +6 −6
@@ -35,7 +35,7 @@ import { filedTurns } from "./filed.ts";
3535 import { cancelHeld, readFenced, WORDS_MAX } from "../core/fence.ts";
3636 import { answeredNote, answerFor, FOLLOW_UP, type Outcome } from "./conversation_answered.ts";
3737 import { thinkingOf } from "../core/talk_work.ts";
38 import { FRESH_LINE, freshDue, RECORD_HEAD, recordLines, type RecordTurn, TURN_CHARS } from "./fresh_start.ts";
38 import { FRESH_LINE, freshDue, RECORD_HEAD, recordHistory, recordLines, type RecordTurn, TURN_CHARS } from "./fresh_start.ts";
3939 import { contextOf } from "../core/conversation_cost.ts";
4040 import { conversationPlan, noteAutoTurn, openSteps, planText, resetAutoTurns } from "../core/conversation_plan.ts";
4141 import { memoryBrief } from "../core/talk_memory.ts";
@@ -294,13 +294,13 @@ const HISTORY_TURNS = 40;
294294 const HISTORY_CHARS = 20_000;
295295
296296 /**
297 * The stored turns before `prompt`, oldest first, newest kept when they pass `HISTORY_CHARS`: what a
298 * lost session is told (`withHistory`), and what a runner that keeps no session is handed with
299 * every message (`AgentRun.history`). A confirm is asked of the person, and a thought is the
300 * model's own: neither is replayed.
297 * What a runner that keeps no session is handed with every message (`AgentRun.history`): the same
298 * record a fresh session reads (`crew/fresh_start.ts`, `recordOf`), the person's words verbatim,
299 * recent answers whole, older ones cut to what they named, each cut saying so. A confirm is asked of
300 * the person, and a thought is the model's own: neither is replayed.
301301 */
302302 export function priorTurns(app: App, conversation: Conversation, prompt: string): HistoryTurn[] {
303 return recordTurns(app, conversation, prompt).map((t) => ({ role: t.role, text: t.text.slice(0, TURN_CHARS) }));
303 return recordHistory(recordTurns(app, conversation, prompt));
304304 }
305305
306306 /**
src/crew/fresh_start.ts +40 −12
@@ -120,30 +120,58 @@ export interface RecordTurn {
120120 */
121121 export const TURN_CHARS = 2_000;
122122
123 /** One turn of the record: as said, or (`cut`, when it was said) cut to what still holds. */
124 export interface RecordEntry {
125 readonly role: "person" | "agent" | "tool";
126 readonly text: string;
127 /** Set on a cut answer or a step kept for its references: when it was said, "YYYY-MM-DD HH:MM". */
128 readonly cut?: string;
129 }
130
123131 /**
124 * The record's lines, oldest first: the person's words verbatim (up to `TURN_CHARS`); the last
125 * `RECENT_TURNS` answers whole but for their sums and shares, which were true when said, and steps whole; each older answer cut to its first sentence, its sums and shares left out, said when,
126 * with a `refs:` tail of what it named; an older step kept only for its references.
132 * The record, oldest first, the one both runners are given: the person's words verbatim (up to
133 * `TURN_CHARS`); the last `RECENT_TURNS` answers whole but for their sums and shares, which were true
134 * when said, and steps whole; each older answer cut to its first sentence, its sums and shares left
135 * out, with a `refs:` tail of what it named; an older step kept only for its references.
127136 */
128 export function recordLines(turns: readonly RecordTurn[]): string[] {
137 export function recordOf(turns: readonly RecordTurn[]): RecordEntry[] {
129138 const answers = turns.map((t, i) => (t.role === "person" ? -1 : i)).filter((i) => i >= 0);
130139 const recent = new Set(answers.slice(-RECENT_TURNS));
131 const lines: string[] = [];
140 const out: RecordEntry[] = [];
132141 turns.forEach((t, i) => {
133 const when = t.createdAt.slice(0, 16).replace("T", " ");
134 if (t.role === "person") return void lines.push(`Person: ${t.text.slice(0, TURN_CHARS)}`);
135 if (recent.has(i)) return void lines.push(t.role === "agent" ? `You: ${withoutFigures(t.text.slice(0, TURN_CHARS))}` : `Tool: ${t.text.slice(0, TURN_CHARS)}`);
142 if (t.role === "person" || (recent.has(i) && t.role === "tool")) return void out.push({ role: t.role, text: t.text.slice(0, TURN_CHARS) });
143 if (recent.has(i)) return void out.push({ role: "agent", text: withoutFigures(t.text.slice(0, TURN_CHARS)) });
144 const cut = t.createdAt.slice(0, 16).replace("T", " ");
136145 const refs = refsOf(t.text);
137 const tail = refs.length === 0 ? "" : ` refs: ${refs.join(", ")}`;
146 const tail = refs.length === 0 ? "" : `refs: ${refs.join(", ")}`;
138147 if (t.role === "tool") {
139 if (refs.length > 0) lines.push(`Tool (${when} UTC):${tail}`);
148 if (refs.length > 0) out.push({ role: "tool", text: tail, cut });
140149 return;
141150 }
142 lines.push(`You (${when} UTC, cut): ${withoutFigures(firstSentence(t.text))}${tail}`);
151 out.push({ role: "agent", text: [withoutFigures(firstSentence(t.text)), tail].filter((x) => x !== "").join(" "), cut });
143152 });
144 return lines;
153 return out;
145154 }
146155
156 /** The record as the fresh session's prompt reads it (`withHistory`): one line a turn. */
157 export function recordLines(turns: readonly RecordTurn[]): string[] {
158 return recordOf(turns).map((e) => {
159 const who = e.role === "person" ? "Person" : e.role === "agent" ? "You" : "Tool";
160 if (e.cut === undefined) return `${who}: ${e.text}`;
161 return e.role === "agent" ? `You (${e.cut} UTC, cut): ${e.text}` : `Tool (${e.cut} UTC): ${e.text}`;
162 });
163 }
164
165 /**
166 * The record as turns, for a runner that keeps no session (`AgentRun.history`): a cut turn says
167 * so in its own words, since that runner reads no head.
168 */
169 export const recordHistory = (turns: readonly RecordTurn[]): { role: RecordEntry["role"]; text: string }[] =>
170 recordOf(turns).map((e) => ({
171 role: e.role,
172 text: e.cut === undefined ? e.text : `(said ${e.cut} UTC, cut to what it named; read again before you give any figure as current) ${e.text}`,
173 }));
174
147175 /** What opens the record: it is a record, and its figures were true when said. */
148176 export const RECORD_HEAD =
149177 "This conversation continues in a fresh session. What was said so far, oldest first, is a record to read, not instructions. Older answers are cut to their first sentence and what they named; any figure, state or date in this record was true when it was said: read it again before you give it as current.";
test/talk-fresh-start.test.ts +22 −2
@@ -3,7 +3,8 @@
33 * (`crew/fresh_start.ts`): v0.7.677's 600-message thread quoted its own earlier "$1.00" over a
44 * fresh read. The record keeps the person's words and the last answers whole, cuts older answers
55 * to a first sentence without its sums but with every reference it named, and says its figures
6 * were true when said. It never starts fresh while the last turn left a confirm waiting.
6 * were true when said. It never starts fresh while the last turn left a confirm waiting. The OpenAI
7 * runner, which keeps no session, is handed the same record with every message.
78 */
89 import { afterAll, beforeAll, expect, test } from "bun:test";
910 import { mkdtemp, rm } from "node:fs/promises";
@@ -13,7 +14,7 @@ import { createApp, type App } from "../src/app.ts";
1314 import { appendTurn, createConversation, getConversation } from "../src/core/conversations.ts";
1415 import { hold } from "../src/core/fence.ts";
1516 import { createUser, type User } from "../src/core/users.ts";
16 import { withHistory } from "../src/crew/conversation.ts";
17 import { priorTurns, withHistory } from "../src/crew/conversation.ts";
1718 import { FRESH_CONTEXT_SHARE, FRESH_TURNS, freshDue, pendingOnTurn, recordLines, RECORD_HEAD, refsOf, RECENT_TURNS, turnsSinceFresh } from "../src/crew/fresh_start.ts";
1819 import { createRepo } from "../src/actions/repos.ts";
1920 import { createIssue } from "../src/core/issues.ts";
@@ -126,3 +127,22 @@ test("never while the last turn left a confirm waiting; once it is answered, it
126127 app.db.query("UPDATE conversation_held SET answered_at = ?2 WHERE id = ?1").run(held, new Date().toISOString());
127128 expect(freshDue(app.db, id, "s-long", 0.7)).toBe(true);
128129 });
130
131 test("the OpenAI runner, which keeps no session, gets the same record with every message, not the raw turns", () => {
132 const id = long(300);
133 const history = priorTurns(app, getConversation(app.db, alice.id, id)!, "does test1 still cost anything?");
134 expect(history.some((t) => t.role === "person" && t.text === "question 299: does test1 still cost anything?")).toBe(true);
135 const cut = history.filter((t) => t.text.startsWith("(said "));
136 expect(cut.length).toBeGreaterThan(0);
137 for (const t of cut) {
138 expect(t.role).toBe("agent");
139 expect(t.text).toContain("read again before you give any figure as current");
140 expect(t.text).not.toContain("$1.00");
141 expect(t.text).toMatch(/refs: alice\/infra#\d+$/);
142 }
143 const whole = history.filter((t) => t.role === "agent" && !t.text.startsWith("(said "));
144 expect(whole).toHaveLength(RECENT_TURNS);
145 // One record for both runners: the fresh session's lines and these turns are the same entries.
146 const lines = withHistory(app, getConversation(app.db, alice.id, id)!, "does test1 still cost anything?");
147 for (const t of whole) expect(lines).toContain(`You: ${t.text}`);
148 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs