From 6d3fc2056cbab231d7b30bb2582ea3729145e6ca Mon Sep 17 00:00:00 2001 From: Adil Date: Sat, 7 Nov 2026 23:42:32 +0500 Subject: [PATCH 1/3] hive/openai-server: only system words that lead a request are the system prompt; later ones are a note in their turn --- hives/openai-server/src/conversation.ts | 17 +++++++++-- hives/openai-server/test/conversation.test.ts | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/hives/openai-server/src/conversation.ts b/hives/openai-server/src/conversation.ts index a460503a..64e3fde0 100644 --- a/hives/openai-server/src/conversation.ts +++ b/hives/openai-server/src/conversation.ts @@ -4,7 +4,10 @@ // sigil, one bloom a turn. A conversation reads as blooms: each user turn // opens one, its words, images, audio and files its content, and what the // assistant said, the tools it called and what came back from them are -// what grew in it. System and developer words are the system prompt. +// what grew in it. System and developer words that lead a request are the +// system prompt; said later, they are a note in the turn they came in, said +// once where they were said, so the history stays append-only and the +// system prompt does not change under it. // // A request's last bloom is what it asks for. Its words are the prompt's // content and the blooms before it the graft. A conversation that ends @@ -129,6 +132,7 @@ class Reader { system: string[] = []; blooms: Bloom[] = []; tail: Tail = "none"; + leading = true; #open(): Bloom { let last = this.blooms[this.blooms.length - 1]; @@ -139,17 +143,24 @@ class Reader { return last; } + told(text: string): void { + if (this.leading) this.system.push(text); + else if (text.length > 0) this.said([{ kind: PetalKind.text, text: `\n${text}\n` }]); + } + said(petals: Petal[]): void { const last = this.blooms[this.blooms.length - 1]; if (last && (last.grew ?? []).length === 0) last.content = [...(last.content ?? []), ...petals]; else this.blooms.push({ content: petals, grew: [] }); this.tail = "user"; + this.leading = false; } grew(petals: Petal[], tail: Tail): void { const bloom = this.#open(); bloom.grew = [...(bloom.grew ?? []), ...petals]; this.tail = tail; + this.leading = false; } done(): Said { @@ -184,7 +195,7 @@ export function fromChat(messages: ChatMessage[]): Said { switch (m.role) { case "system": case "developer": - r.system.push(resultOf(m.content)); + r.told(resultOf(m.content)); break; case "user": r.said(partsOf(m.content)); @@ -232,7 +243,7 @@ export function fromResponses(input: unknown, instructions?: string, before?: Sa const type = item?.type ?? (item?.role ? "message" : undefined); switch (type) { case "message": - if (item.role === "system" || item.role === "developer") r.system.push(resultOf(item.content)); + if (item.role === "system" || item.role === "developer") r.told(resultOf(item.content)); else if (item.role === "assistant") r.grew(partsOf(item.content), "assistant"); else r.said(partsOf(item.content)); break; diff --git a/hives/openai-server/test/conversation.test.ts b/hives/openai-server/test/conversation.test.ts index 6a172082..fce5c026 100644 --- a/hives/openai-server/test/conversation.test.ts +++ b/hives/openai-server/test/conversation.test.ts @@ -40,6 +40,36 @@ describe("fromChat", () => { }); }); +describe("system words said after the first turn", () => { + const note = (text: string) => ({ kind: "text", text: `\n${text}\n` }); + + test("are a note in the turn they came in, and the system prompt keeps only what led", () => { + const said = fromChat([ + { role: "system", content: "be terse" }, + { role: "user", content: "hi" }, + { role: "assistant", content: "hello" }, + { role: "user", content: "again" }, + { role: "system", content: "[bookkeeping] deem it" }, + ]); + expect(said.system).toBe("be terse"); + expect(said.blooms.length).toBe(2); + expect(said.blooms[1].content).toEqual([{ kind: "text", text: "again" }, note("[bookkeeping] deem it")]); + }); + + test("are said once each, where they were said, however many pile up", () => { + const said = fromResponses([ + { type: "message", role: "user", content: "one" }, + { type: "message", role: "system", content: "[bookkeeping] first" }, + { type: "message", role: "assistant", content: [{ type: "output_text", text: "ok" }] }, + { type: "message", role: "user", content: "two" }, + { type: "message", role: "system", content: "[bookkeeping] second" }, + ], "be terse"); + expect(said.system).toBe("be terse"); + expect(said.blooms[0].content).toEqual([{ kind: "text", text: "one" }, note("[bookkeeping] first")]); + expect(said.blooms[1].content).toEqual([{ kind: "text", text: "two" }, note("[bookkeeping] second")]); + }); +}); + describe("fromResponses", () => { test("items, instructions and a predecessor's blooms read alike", () => { const before = fromResponses("one"); From 6814b53c5554974ae87bff87fc1f4ba9a6306e7e Mon Sep 17 00:00:00 2001 From: Adil Date: Sun, 8 Nov 2026 00:08:16 +0500 Subject: [PATCH 2/3] hive/openai-server: a nurse says a system note back as a system message where it was said --- hives/openai-server/src/conversation.ts | 14 ++++++++++-- hives/openai-server/src/nurse.ts | 29 +++++++++++++++++++------ hives/openai-server/test/nurse.test.ts | 16 ++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/hives/openai-server/src/conversation.ts b/hives/openai-server/src/conversation.ts index 64e3fde0..b01d048e 100644 --- a/hives/openai-server/src/conversation.ts +++ b/hives/openai-server/src/conversation.ts @@ -7,7 +7,8 @@ // what grew in it. System and developer words that lead a request are the // system prompt; said later, they are a note in the turn they came in, said // once where they were said, so the history stays append-only and the -// system prompt does not change under it. +// system prompt does not change under it. A nurse says such a note back to +// its model as a system message, where the client said it. // // A request's last bloom is what it asks for. Its words are the prompt's // content and the blooms before it the graft. A conversation that ends @@ -128,6 +129,15 @@ function servedPetals(id: string, name: string, args: unknown, output: unknown, ]; } +export function noted(text: string): Petal { + return { kind: PetalKind.text, text: `\n${text}\n` }; +} + +export function noteOf(petal: Petal): string | undefined { + const text = petal.kind === PetalKind.text ? petal.text ?? "" : ""; + return text.startsWith("\n") && text.endsWith("\n") ? text.slice(9, -10) : undefined; +} + class Reader { system: string[] = []; blooms: Bloom[] = []; @@ -145,7 +155,7 @@ class Reader { told(text: string): void { if (this.leading) this.system.push(text); - else if (text.length > 0) this.said([{ kind: PetalKind.text, text: `\n${text}\n` }]); + else if (text.length > 0) this.said([noted(text)]); } said(petals: Petal[]): void { diff --git a/hives/openai-server/src/nurse.ts b/hives/openai-server/src/nurse.ts index a897de3b..45360069 100644 --- a/hives/openai-server/src/nurse.ts +++ b/hives/openai-server/src/nurse.ts @@ -27,6 +27,7 @@ import { randomUUID } from "node:crypto"; import { Bee, Chi, PetalKind, Role, type Bloom, type Petal, type PetalKindT, type Tone } from "../../../thrum-clients/ts/index.ts"; import { HIVE } from "./config.ts"; +import { noteOf } from "./conversation.ts"; import { SOURCE, VERSION } from "./requester.ts"; import { events, post } from "./upstream.ts"; @@ -62,9 +63,24 @@ function parts(petals: Petal[]): string | Array> { return out; } -function said(petals: Petal[]): Message | undefined { - const content = parts(petals); - return (typeof content === "string" ? content.length > 0 : content.length > 0) ? { role: "user", content } : undefined; +function said(petals: Petal[]): Message[] { + const out: Message[] = []; + let held: Petal[] = []; + const flush = () => { + const content = parts(held); + if (content.length > 0) out.push({ role: "user", content }); + held = []; + }; + for (const p of petals) { + const note = noteOf(p); + if (note === undefined) held.push(p); + else { + flush(); + out.push({ role: "system", content: note }); + } + } + flush(); + return out; } function grown(petals: Petal[], into: Message[]): void { @@ -93,8 +109,7 @@ function grown(petals: Petal[], into: Message[]): void { export function messagesOf(graft: Bloom[]): Message[] { const out: Message[] = []; for (const b of graft) { - const m = said(b.content ?? []); - if (m) out.push(m); + out.push(...said(b.content ?? [])); grown(b.grew ?? [], out); } return out; @@ -232,8 +247,8 @@ export class Nurse { s.waiting.clear(); const content = t.content && t.content.length > 0 ? t.content : t.text ? [{ kind: PetalKind.text, text: t.text }] : []; const asked = said(content); - if (asked) { - s.messages.push(asked); + if (asked.length > 0) { + s.messages.push(...asked); return this.#grow(sid, s); } const text = plainText(graft); diff --git a/hives/openai-server/test/nurse.test.ts b/hives/openai-server/test/nurse.test.ts index 274c8ebc..a413ffce 100644 --- a/hives/openai-server/test/nurse.test.ts +++ b/hives/openai-server/test/nurse.test.ts @@ -4,6 +4,7 @@ import { describe, expect, test } from "bun:test"; import { askOf, lateAnswers, messagesOf, plainText } from "../src/nurse.ts"; +import { fromChat } from "../src/conversation.ts"; describe("the nurse half", () => { test("a graft is the conversation, tool calls and answers in their places", () => { @@ -86,3 +87,18 @@ describe("the nurse half", () => { }); }); }); + +describe("system words said after the first turn", () => { + test("come back to the model as system messages where the client said them", () => { + const chat = [ + { role: "system", content: "be terse" }, + { role: "user", content: "hi" }, + { role: "assistant", content: "hello" }, + { role: "user", content: "again" }, + { role: "system", content: "[bookkeeping] deem it" }, + ]; + const said = fromChat(chat); + expect(said.system).toBe("be terse"); + expect(messagesOf(said.blooms)).toEqual(chat.slice(1)); + }); +}); From 043257526edb092f0c54c47865a2750b7d7c842e Mon Sep 17 00:00:00 2001 From: Adil Date: Sun, 8 Nov 2026 01:47:05 +0500 Subject: [PATCH 3/3] hive/openai-server: header says it in one line --- hives/openai-server/src/conversation.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/hives/openai-server/src/conversation.ts b/hives/openai-server/src/conversation.ts index b01d048e..95fb3d61 100644 --- a/hives/openai-server/src/conversation.ts +++ b/hives/openai-server/src/conversation.ts @@ -4,11 +4,7 @@ // sigil, one bloom a turn. A conversation reads as blooms: each user turn // opens one, its words, images, audio and files its content, and what the // assistant said, the tools it called and what came back from them are -// what grew in it. System and developer words that lead a request are the -// system prompt; said later, they are a note in the turn they came in, said -// once where they were said, so the history stays append-only and the -// system prompt does not change under it. A nurse says such a note back to -// its model as a system message, where the client said it. +// what grew in it. Leading system words are the system prompt; later, notes. // // A request's last bloom is what it asks for. Its words are the prompt's // content and the blooms before it the graft. A conversation that ends