Skip to content

Commit 5004758

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat: surface base + persona system prompts in observer feed (#1103)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 466bb99 commit 5004758

4 files changed

Lines changed: 162 additions & 6 deletions

File tree

‎crates/buzz-acp/src/pool.rs‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,7 @@ async fn create_session_and_apply_model(
435435
// for the session/new request. Only sent when the agent declares protocol
436436
// version >= 2 (supports systemPrompt); legacy agents ignore it.
437437
let combined_system_prompt: Option<String> = if agent.protocol_version >= 2 {
438-
match (ctx.base_prompt, ctx.system_prompt.as_deref()) {
439-
(Some(bp), Some(sp)) => Some(format!("{}\n\n{sp}", bp.trim_end())),
440-
(Some(bp), None) => Some(bp.trim_end().to_string()),
441-
(None, Some(sp)) => Some(sp.to_string()),
442-
(None, None) => None,
443-
}
438+
framed_system_prompt(ctx.base_prompt, ctx.system_prompt.as_deref())
444439
} else {
445440
None
446441
};
@@ -660,6 +655,26 @@ pub(crate) fn prepend_base_for_legacy(
660655
}
661656
}
662657

658+
/// Frame the `session/new` `systemPrompt` so each present prompt carries its own
659+
/// header, keeping the base/persona boundary recoverable downstream.
660+
///
661+
/// The header framing matches the legacy per-turn path (`queue::base_section`
662+
/// for `[Base]`, `[System]\n{...}` for the persona) so the desktop observer can
663+
/// split the combined value into labeled sub-sections. Each prompt is wrapped
664+
/// only when present, so a persona-only agent yields `[System]\n{persona}`
665+
/// rather than an unlabeled blob that would be mislabeled as `[Base]`.
666+
fn framed_system_prompt(base_prompt: Option<&str>, system_prompt: Option<&str>) -> Option<String> {
667+
match (base_prompt, system_prompt) {
668+
(Some(bp), Some(sp)) => Some(format!(
669+
"{}\n\n[System]\n{sp}",
670+
crate::queue::base_section(bp)
671+
)),
672+
(Some(bp), None) => Some(crate::queue::base_section(bp)),
673+
(None, Some(sp)) => Some(format!("[System]\n{sp}")),
674+
(None, None) => None,
675+
}
676+
}
677+
663678
/// Core async function spawned for each prompt.
664679
///
665680
/// Lifecycle:
@@ -2336,6 +2351,36 @@ mod tests {
23362351
assert_eq!(composed, "hello channel");
23372352
}
23382353

2354+
// ── framed_system_prompt tests ───────────────────────────────────────────
2355+
// Pin the session/new systemPrompt framing: each present prompt carries its
2356+
// own header so the desktop observer can split into labeled sub-sections.
2357+
2358+
#[test]
2359+
fn test_framed_system_prompt_both_present_carries_both_headers() {
2360+
let framed = framed_system_prompt(Some("base text"), Some("persona text"))
2361+
.expect("both present yields Some");
2362+
assert_eq!(framed, "[Base]\nbase text\n\n[System]\npersona text");
2363+
}
2364+
2365+
#[test]
2366+
fn test_framed_system_prompt_base_only_labels_base() {
2367+
let framed = framed_system_prompt(Some("base text"), None).expect("base yields Some");
2368+
assert_eq!(framed, "[Base]\nbase text");
2369+
}
2370+
2371+
#[test]
2372+
fn test_framed_system_prompt_persona_only_labels_system() {
2373+
// A bare persona would be mislabeled "Base" downstream — it must carry
2374+
// its own [System] header even when no base prompt exists.
2375+
let framed = framed_system_prompt(None, Some("persona text")).expect("persona yields Some");
2376+
assert_eq!(framed, "[System]\npersona text");
2377+
}
2378+
2379+
#[test]
2380+
fn test_framed_system_prompt_neither_is_none() {
2381+
assert!(framed_system_prompt(None, None).is_none());
2382+
}
2383+
23392384
// ── parse_thread_response tests ──────────────────────────────────────────
23402385

23412386
#[test]

‎desktop/src/features/agents/ui/agentSessionTranscript.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
extractToolIdentity,
2121
extractToolResult,
2222
parsePromptText,
23+
parseSystemPromptSections,
2324
} from "./agentSessionTranscriptHelpers";
2425

2526
export { describeRawEvent } from "./agentSessionTranscriptHelpers";
@@ -335,6 +336,27 @@ export function processTranscriptEvent(
335336
);
336337
}
337338
}
339+
} else if (event.kind === "acp_write" && method === "session/new") {
340+
// The base + persona prompts ride session/new's systemPrompt, framed by
341+
// the harness as [Base]/[System]. Surface them as one "System prompt" item
342+
// keyed per channel-session — the frame carries no session id (it predates
343+
// session creation), and session/new fires once per channel-session, so a
344+
// re-created session correctly replaces the prior item.
345+
const params = asRecord(payload.params);
346+
const systemPrompt = asString(params.systemPrompt);
347+
if (systemPrompt) {
348+
const sections = parseSystemPromptSections(systemPrompt);
349+
if (sections.length > 0) {
350+
upsertMetadata(
351+
d,
352+
`system-prompt:${ch}`,
353+
"System prompt",
354+
sections,
355+
event.timestamp,
356+
channelId,
357+
);
358+
}
359+
}
338360
} else if (event.kind === "acp_read" && method === "session/update") {
339361
const params = asRecord(payload.params);
340362
const update = asRecord(params.update);

‎desktop/src/features/agents/ui/agentSessionTranscriptHelpers.test.mjs‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import test from "node:test";
44
import {
55
extractPromptText,
66
parsePromptText,
7+
parseSystemPromptSections,
78
} from "./agentSessionTranscriptHelpers.ts";
89

910
const HEX = "a".repeat(64);
@@ -122,3 +123,51 @@ test("extractPromptText returns empty string when prompt is missing or not an ar
122123
assert.equal(extractPromptText({}), "");
123124
assert.equal(extractPromptText({ params: { prompt: "nope" } }), "");
124125
});
126+
127+
// --- parseSystemPromptSections: deterministic Base/System split ---
128+
129+
test("parseSystemPromptSections splits both prompts into Base and System", () => {
130+
const framed = "[Base]\nbase text\n\n[System]\npersona text";
131+
const sections = parseSystemPromptSections(framed);
132+
assert.deepEqual(sections, [
133+
{ title: "Base", body: "base text" },
134+
{ title: "System", body: "persona text" },
135+
]);
136+
});
137+
138+
test("parseSystemPromptSections yields one Base section for a base-only frame", () => {
139+
const sections = parseSystemPromptSections("[Base]\nbase text");
140+
assert.deepEqual(sections, [{ title: "Base", body: "base text" }]);
141+
});
142+
143+
test("parseSystemPromptSections yields one System section for a persona-only frame", () => {
144+
const sections = parseSystemPromptSections("[System]\npersona text");
145+
assert.deepEqual(sections, [{ title: "System", body: "persona text" }]);
146+
});
147+
148+
test("parseSystemPromptSections keeps embedded bracket lines literal in bodies", () => {
149+
// A persona that itself contains a [Context]-like line must NOT split into a
150+
// spurious sub-section — the body is read literally after the first boundary.
151+
const framed = "[Base]\nbase\n\n[System]\nrule one\n[Context]\nrule two";
152+
const sections = parseSystemPromptSections(framed);
153+
assert.deepEqual(sections, [
154+
{ title: "Base", body: "base" },
155+
{ title: "System", body: "rule one\n[Context]\nrule two" },
156+
]);
157+
});
158+
159+
test("parseSystemPromptSections degrades to a labeled Base when [System] header is elided", () => {
160+
// Oversize trim can drop the [System] header mid-string. Without a boundary
161+
// the whole value stays under a correctly-labeled Base — no missing label,
162+
// no inflated count, just a truncated body.
163+
const elided = "[Base]\nbase text …[elided 900000 bytes]… persona tail";
164+
const sections = parseSystemPromptSections(elided);
165+
assert.deepEqual(sections, [
166+
{ title: "Base", body: "base text …[elided 900000 bytes]… persona tail" },
167+
]);
168+
});
169+
170+
test("parseSystemPromptSections returns no sections for empty input", () => {
171+
assert.deepEqual(parseSystemPromptSections(""), []);
172+
assert.deepEqual(parseSystemPromptSections(" "), []);
173+
});

‎desktop/src/features/agents/ui/agentSessionTranscriptHelpers.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ export function parsePromptText(text: string): {
4949
};
5050
}
5151

52+
/**
53+
* Split the framed `session/new` `systemPrompt` into its `Base`/`System`
54+
* sub-sections deterministically.
55+
*
56+
* The harness frames the value as `[Base]\n{base}\n\n[System]\n{persona}`, with
57+
* either prompt omitted when absent: base-only is `[Base]\n{base}`, persona-only
58+
* is `[System]\n{persona}`. We partition on the FIRST `\n[System]\n` boundary and
59+
* read each labeled body literally. Unlike the generic `parsePromptSections`,
60+
* embedded `[...]` lines inside a body never start a new section — so a persona
61+
* containing a bracketed line, or a mid-string-elided header on an oversize
62+
* prompt, can never drop a label or inflate the section count.
63+
*/
64+
export function parseSystemPromptSections(
65+
systemPrompt: string,
66+
): PromptSection[] {
67+
const sections: PromptSection[] = [];
68+
69+
// Persona-only frame: no [Base], starts directly with [System].
70+
if (systemPrompt.startsWith("[System]\n")) {
71+
const body = systemPrompt.slice("[System]\n".length).trim();
72+
if (body) sections.push({ title: "System", body });
73+
return sections;
74+
}
75+
76+
// Otherwise the head (up to the first [System] boundary, or the whole string)
77+
// is the [Base] body.
78+
const marker = "\n[System]\n";
79+
const at = systemPrompt.indexOf(marker);
80+
const head = at === -1 ? systemPrompt : systemPrompt.slice(0, at);
81+
const baseBody = head.replace(/^\[Base]\n/, "").trim();
82+
if (baseBody) sections.push({ title: "Base", body: baseBody });
83+
84+
if (at !== -1) {
85+
const systemBody = systemPrompt.slice(at + marker.length).trim();
86+
sections.push({ title: "System", body: systemBody });
87+
}
88+
89+
return sections;
90+
}
91+
5292
function parsePromptSections(text: string): PromptSection[] {
5393
const sections: PromptSection[] = [];
5494
let current: PromptSection | null = null;

0 commit comments

Comments
 (0)