The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(omp): capture print and ACP user prompts
Kyle Elliott914aa27fb5de
diff --git a/cli/beacon-hooks/cmd/omp_event_test.go b/cli/beacon-hooks/cmd/omp_event_test.goindex 6c928af1..19f62128 100644--- a/cli/beacon-hooks/cmd/omp_event_test.go+++ b/cli/beacon-hooks/cmd/omp_event_test.go@@ -218,6 +218,49 @@ func TestOmpEventPromptRecordsTextAndSource(t *testing.T) {}}+// Print and ACP never emit `input`; the extension forwards the delivered user message instead,+// with its text under `prompt`. The row names that origin, because the text is what Oh My Pi sent+// the model after expanding templates and commands, not what an `input` event would carry.+func TestOmpEventUserMessagePromptRecordsDeliveredText(t *testing.T) {+ logPath := ompTestLog(t)++ runHookWithInput(t, runOmpEvent, map[string]interface{}{+ "type": "message_end", "message": map[string]interface{}{"role": "user"},+ "prompt": "summarize the changes", "sessionId": "sess-print", "ompMode": "print",+ })++ event := ompEventWithAction(t, logPath, "prompt.submitted")+ if prompt := nested(t, event, "prompt"); prompt["text"] != "summarize the changes" {+ t.Fatalf("prompt.text = %v, want the delivered text", prompt["text"])+ }+ if content := nested(t, event, "content"); content["included"] != true || content["hash"] == "" {+ t.Fatalf("content = %v, want retained content with a hash", content)+ }+ raw := nested(t, event, "raw")+ if raw["omp_prompt_origin"] != "message_end" {+ t.Fatalf("raw.omp_prompt_origin = %v, want message_end", raw["omp_prompt_origin"])+ }+ if _, ok := raw["omp_input_source"]; ok {+ t.Fatalf("raw.omp_input_source = %v, want none: no input event reported a source", raw["omp_input_source"])+ }+}++// A user message the extension did not mark as a prompt -- every one an older extension forwarded+// -- records nothing, so an interactive or RPC prompt already recorded from `input` is never+// counted twice.+func TestOmpEventUnmarkedUserMessageProducesNothing(t *testing.T) {+ events := ompRuntime.endpointEvents(map[string]interface{}{+ "type": "message_end",+ "message": map[string]interface{}{+ "role": "user",+ "content": []interface{}{map[string]interface{}{"type": "text", "text": "typed"}},+ },+ }, "sess-1")+ if len(events) != 0 {+ t.Fatalf("unmarked user message produced %v, want nothing", events)+ }+}+// A tool_call is not an approval. Oh My Pi's tool_call handler can block a call, but that is an// extension deciding rather than an operator being asked; the runtime's real operator decisions// arrive as their own approval events. Recording a block as an approval would be indistinguishablediff --git a/cli/beacon-hooks/cmd/pi_family.go b/cli/beacon-hooks/cmd/pi_family.goindex 1056fef6..1b745ea1 100644--- a/cli/beacon-hooks/cmd/pi_family.go+++ b/cli/beacon-hooks/cmd/pi_family.go@@ -1178,18 +1178,48 @@ func piToolMessageSuffix(action string) string {}}+// userMessagePromptEvents records a user message as a submitted prompt when the extension has+// marked it as one by attaching its text under `prompt`.+//+// The extension makes that call because only it has the state to: interactive and RPC sessions+// report every submission as `input` first, and recording the user message there as well would+// count each prompt twice. On print and ACP, which never emit `input`, the user message is the+// only record of what was asked. A user message without `prompt` -- every one an older extension+// forwarded -- produces nothing.+//+// The text is the message Oh My Pi delivered to the model, after it expanded slash commands,+// prompt templates and model mentions, not the keystrokes an `input` event carries. The origin is+// recorded so a reader can tell the two shapes apart.+func (f piFamily) userMessagePromptEvents(input, fields map[string]interface{}) []normalizedEvent {+ prompt := getFirstStr(input, "prompt")+ if prompt == "" {+ return nil+ }+ fields["raw"] = mergeNested(fields["raw"], map[string]interface{}{f.rawKey("prompt_origin"): "message_end"})+ events := []normalizedEvent{f.promptEvent(fields, prompt, "")}+ if link, ok := handoffLinkEvent(fields, prompt); ok {+ events = append(events, link)+ }+ return events+}+// messageEndEvents records what a finished assistant message tells us: its token usage, and the-// model's reasoning when the provider returned any.+// model's reasoning when the provider returned any. A user message is a prompt; see+// userMessagePromptEvents.//// A finalized message is the only place these runtimes report usage, and message_end fires for-// user and toolResult messages too, so a message with neither usage nor reasoning produces nothing-// rather than an empty row per turn.+// toolResult messages too, so a message with neither usage nor reasoning produces nothing rather+// than an empty row per turn.func (f piFamily) messageEndEvents(input map[string]interface{}, fields map[string]interface{}) []normalizedEvent {message := firstMap(input, "message")if message == nil {return nil}- if role := getFirstStr(message, "role"); role != "assistant" {+ role := getFirstStr(message, "role")+ if role == "user" {+ return f.userMessagePromptEvents(input, fields)+ }+ if role != "assistant" {return nil}if model := getFirstStr(message, "model", "responseModel"); model != "" {diff --git a/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.tsindex b7ad5c8d..cf8b56d8 100644--- a/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts+++ b/cli/beacon/internal/endpoint/hooks/assets/omp/beacon.ts@@ -38,6 +38,7 @@ interface OmpContext {sessionManager?: OmpSessionManagermodel?: { id?: string; name?: string; provider?: string } | undefinedmode?: string+ agent?: { kind?: string }}// The events Beacon subscribes to, and nothing else.@@ -62,6 +63,9 @@ interface OmpContext {// server sends, most of them routine tools/resources list refreshes. It describes MCP transport// plumbing rather than an action the agent took. The MCP activity worth recording is the agent// calling an MCP tool, which already arrives as a tool_call/tool_result pair.+//+// `message_end` carries each finalized assistant message, and on print and ACP -- which never emit+// `input` -- the operator's own prompt message as well (see submittedPromptText).const subscribedEvents = ["session_start","session_shutdown",@@ -126,6 +130,25 @@ function decidedToolInput(event: OmpEvent): unknown {return pendingToolCalls.get(id)}+// The text of a user message Oh My Pi delivered to the model, or "" when the message is not an+// operator's prompt.+//+// Runtime-generated turns (auto-continue, reminders) are marked `synthetic`, and messages one agent+// hands to another carry `attribution: "agent"`; neither is something an operator asked. Image+// parts are left out: the prompt row records what was asked, not the attachment bytes.+function userMessageText(message: unknown): string {+ if (!message || typeof message !== "object") return ""+ const { role, synthetic, attribution, content } = message as Record<string, unknown>+ if (role !== "user" || synthetic === true || attribution === "agent") return ""+ if (typeof content === "string") return content+ if (!Array.isArray(content)) return ""+ const texts: string[] = []+ for (const part of content as Array<{ type?: unknown; text?: unknown } | null>) {+ if (part?.type === "text" && typeof part.text === "string") texts.push(part.text)+ }+ return texts.join("\n")+}+function debugLog(message: string, extra?: unknown) {if (!debugEnabled) returntry {@@ -302,12 +325,42 @@ type OmpExtensionAPI = {// createBeaconExtension is exported for tests; Oh My Pi loads the default export below.export function createBeaconExtension() {+ // The id of the current session once it has emitted `input` ("" for a session without one), or+ // null until it has. Interactive and RPC sessions emit `input` for every submission; ACP and+ // print never do. RPC and ACP both report `ctx.mode` "rpc", so this is what tells them apart.+ let inputSessionId: string | null = null++ // submittedPromptText decides whether a user message is a prompt no `input` event recorded.+ //+ // Interactive sessions are excluded outright: every prompt typed there arrives as `input`, and a+ // user message without one was sent by an extension, not typed. A session that has emitted+ // `input` is an RPC client, which reports every submission the same way. A subagent's messages+ // come from its parent's task, not an operator.+ const submittedPromptText = (message: unknown, ctx: OmpContext | undefined, sessionId: string) => {+ if (ctx?.mode === "tui" || ctx?.agent?.kind === "sub" || inputSessionId === sessionId) return ""+ return userMessageText(message)+ }+const forward = async (event: OmpEvent, ctx: OmpContext) => {try {// The event is spread last so a field it carries itself wins over the context's. `user_bash`// and `user_python` both carry their own cwd, and the approval events carry their own// sessionId -- in each case the event's is the one that describes this action.- const envelope: Record<string, unknown> = { ...identity(ctx), ...event }+ const fields = identity(ctx)+ const envelope: Record<string, unknown> = { ...fields, ...event }+ const sessionId = typeof envelope.sessionId === "string" ? envelope.sessionId : ""++ if (event.type === "input") {+ inputSessionId = sessionId+ } else if (event.type === "message_end" && (event.message as { role?: unknown })?.role === "user") {+ // Only the role and text leave the extension: a user message can also carry image bytes+ // and runtime bookkeeping the prompt row has no use for. One that is not a prompt is not+ // sent at all; the mapper records nothing for it.+ const prompt = submittedPromptText(event.message, ctx, sessionId)+ if (!prompt) return+ await sendToBeacon({ ...fields, type: "message_end", message: { role: "user" }, prompt })+ return+ }if (event.type === "tool_call") {rememberToolCall(event)@@ -319,6 +372,7 @@ export function createBeaconExtension() {// Done here rather than in a second `pi.on` registration so the whole cache lifecycle is// visible in one place.pendingToolCalls.clear()+ inputSessionId = null} else if (event.type === "tool_approval_requested" || event.type === "tool_approval_resolved") {// Attached under `input`, the same key `tool_call` uses, so the mapper reads one shape for// both and an approval resolves to the same command or file path its tool call did.diff --git a/docs/runtimes/oh-my-pi.mdx b/docs/runtimes/oh-my-pi.mdxindex 5925b811..603d5587 100644--- a/docs/runtimes/oh-my-pi.mdx+++ b/docs/runtimes/oh-my-pi.mdx@@ -99,7 +99,7 @@ Unlike Pi, a project-level install needs no trust prompt: Oh My Pi loads `.omp`| Harness attribution | Supported. `omp`, `omp_cli`, `omp-cli`, `oh-my-pi`, `oh_my_pi`, `ohmypi`, and `omp.sh` all normalize to `omp` || Extension install, status, uninstall, and repair | Supported || Session lifecycle | Supported. `session.started` and `session.ended` |-| Prompts | Supported. `prompt.submitted`, with whether the input was typed, delivered over RPC, or injected by another extension |+| Prompts | Supported. `prompt.submitted`. In the terminal and over RPC it comes from Oh My Pi's `input` event and records whether the input was typed, delivered over RPC, or injected by another extension. Print (`omp -p`) and ACP sessions never emit `input`, so there it comes from the user message Oh My Pi delivers to the model, marked `raw.omp_prompt_origin: message_end`; see Known gaps for how that text differs || Tool use | Supported. `tool.invoked` before execution, then `tool.completed` or `tool.failed`, joined by `gen_ai.tool.call.id` || **Approval decisions** | **Supported.** `approval.requested`, `approval.allowed`, and `approval.denied` from real operator prompts, carrying the command or file being decided on, the approval mode the session was running under, and the tool call id of the execution they decided. This is the capability Oh My Pi has that upstream Pi does not || Commands | Supported. `command.executed` for the agent's `bash` tool, for commands you run with the `!` prefix, and for code you run with the `$` Python prefix — the last two marked as operator-initiated |@@ -162,6 +162,7 @@ The extension itself sends nothing over the network. It spawns the local `beacon- **Streaming events are not collected.** The extension subscribes to ten of Oh My Pi's event types and ignores the rest, which are provider-request internals, streaming message updates, compaction and retry signals, and TUI plumbing. Assistant output is recorded once when a message finalizes rather than token by token, so Beacon stays out of the streaming path.- **MCP notifications are not collected.** `mcp_notification` fires for every JSON-RPC notification a connected server sends, most of them routine tools and resources list refreshes. It describes MCP transport plumbing rather than an action the agent took. MCP tool calls the agent makes *are* collected.- **One profile at a time.** Beacon installs into the active profile, named by `OMP_PROFILE` or `PI_PROFILE`, or into the default profile. It does not enumerate `~/.omp/profiles/`, so instrumenting another profile means running the install with that profile's variable set.+- **Print and ACP prompts are recorded as delivered, not as typed.** Oh My Pi expands slash commands, prompt templates, and model mentions before it delivers a user message, so on print and ACP `prompt.text` holds the expanded text, which can include a template's body, and carries no input source. Runtime-generated turns, messages one agent hands another, and subagent tasks are not recorded as prompts. A message an extension sends as the user is, because Oh My Pi attributes it to the user. `/skill:` invocations are delivered as a different message type and are not recorded on these surfaces.- **Session switches are not distinguished.** Oh My Pi's `session_start` carries no reason, and its `session_switch`, `session_branch`, and `session_tree` events are not subscribed to, so a resumed or forked session is not marked as such.## Deployment notesdiff --git a/plugins/omp-beacon/src/beacon.test.ts b/plugins/omp-beacon/src/beacon.test.tsindex a0b6e3bd..30b4652f 100644--- a/plugins/omp-beacon/src/beacon.test.ts+++ b/plugins/omp-beacon/src/beacon.test.ts@@ -122,6 +122,143 @@ describe("beacon oh my pi extension", () => {expect(sent[0].text).toBe("do the thing")})+ // Print and ACP never emit `input`, so the user message Oh My Pi delivers is the only record of+ // the prompt. Only its role and text leave the extension.+ test("forwards a print prompt from its user message", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)++ await omp.fire(+ {+ type: "message_end",+ message: {+ role: "user",+ attribution: "user",+ content: [+ { type: "text", text: "summarize the changes" },+ { type: "image", data: "aW1hZ2UtYnl0ZXM=", mimeType: "image/png" },+ ],+ timestamp: 1,+ },+ },+ context({ mode: "print" }),+ )++ expect(sent).toEqual([+ {+ cwd: "/repo",+ ompMode: "print",+ sessionId: "sess-1",+ type: "message_end",+ message: { role: "user" },+ prompt: "summarize the changes",+ },+ ])+ })++ // ACP reports `ctx.mode` "rpc" like an RPC client, but never emits `input`.+ test("forwards an ACP prompt, which arrives with no input event", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)++ await omp.fire({ type: "message_end", message: { role: "user", content: "fix the test" } }, context({ mode: "rpc" }))++ expect(sent.map((event) => event.prompt)).toEqual(["fix the test"])+ })++ test("does not record an RPC client's prompt twice", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)+ const ctx = context({ mode: "rpc" })++ await omp.fire({ type: "input", text: "fix the test", source: "rpc" }, ctx)+ await omp.fire({ type: "message_end", message: { role: "user", content: "fix the test" } }, ctx)++ expect(sent.map((event) => event.type)).toEqual(["input"])+ })++ // An image-only submission still shows the session reports its prompts as `input`.+ test("an input without text still marks the session as reporting input", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)+ const ctx = context({ mode: "rpc" })++ await omp.fire({ type: "input", text: "first", source: "rpc" }, ctx)+ await omp.fire({ type: "input", text: "", source: "rpc" }, ctx)+ await omp.fire({ type: "message_end", message: { role: "user", content: "first" } }, ctx)++ expect(sent.map((event) => event.type)).toEqual(["input", "input"])+ })++ // Every prompt typed in the terminal arrives as `input`. A user message without one was sent by+ // an extension, so it is not recorded as a prompt even before the first `input`.+ test("never records an interactive user message as a prompt", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)++ await omp.fire({ type: "message_end", message: { role: "user", content: "sent by an extension" } }, context())+ await omp.fire({ type: "input", text: "typed", source: "interactive" }, context())+ await omp.fire({ type: "message_end", message: { role: "user", content: "typed" } }, context())++ expect(sent.map((event) => event.type)).toEqual(["input"])+ })++ test("does not record runtime-generated or agent-handed messages as prompts", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)+ const ctx = context({ mode: "print" })++ await omp.fire({ type: "message_end", message: { role: "user", synthetic: true, content: "continue" } }, ctx)+ await omp.fire({ type: "message_end", message: { role: "user", attribution: "agent", content: "handoff" } }, ctx)+ await omp.fire({ type: "message_end", message: { role: "developer", content: "reminder" } }, ctx)++ expect(sent.filter((event) => "prompt" in event)).toEqual([])+ })++ test("does not record a subagent's task as a prompt", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)++ await omp.fire(+ { type: "message_end", message: { role: "user", content: "explore the repo" } },+ context({ mode: "print", agent: { kind: "sub" } }),+ )++ expect(sent).toEqual([])+ })++ test("judges a new session on its own input", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)++ await omp.fire({ type: "input", text: "first", source: "rpc" }, context({ mode: "rpc" }))+ await omp.fire({ type: "session_start" }, context({ mode: "rpc" }))+ const next = context({ mode: "rpc", sessionManager: { getSessionId: () => "sess-2" } })+ await omp.fire({ type: "message_end", message: { role: "user", content: "second" } }, next)++ expect(sent.map((event) => event.type)).toEqual(["input", "session_start", "message_end"])+ })++ test("records repeated identical print prompts each time", async () => {+ const sent = captureSends()+ const omp = fakeOmp()+ createBeaconExtension().register(omp.api)+ const ctx = context({ mode: "print", sessionManager: undefined })++ await omp.fire({ type: "message_end", message: { role: "user", content: "repeat" } }, ctx)+ await omp.fire({ type: "message_end", message: { role: "user", content: "repeat" } }, ctx)++ expect(sent.map((event) => event.prompt)).toEqual(["repeat", "repeat"])+ })+test("forwards an approval decision with its outcome intact", async () => {const sent = captureSends()const omp = fakeOmp()diff --git a/plugins/omp-beacon/src/beacon.ts b/plugins/omp-beacon/src/beacon.tsindex b7ad5c8d..cf8b56d8 100644--- a/plugins/omp-beacon/src/beacon.ts+++ b/plugins/omp-beacon/src/beacon.ts@@ -38,6 +38,7 @@ interface OmpContext {sessionManager?: OmpSessionManagermodel?: { id?: string; name?: string; provider?: string } | undefinedmode?: string+ agent?: { kind?: string }}// The events Beacon subscribes to, and nothing else.@@ -62,6 +63,9 @@ interface OmpContext {// server sends, most of them routine tools/resources list refreshes. It describes MCP transport// plumbing rather than an action the agent took. The MCP activity worth recording is the agent// calling an MCP tool, which already arrives as a tool_call/tool_result pair.+//+// `message_end` carries each finalized assistant message, and on print and ACP -- which never emit+// `input` -- the operator's own prompt message as well (see submittedPromptText).const subscribedEvents = ["session_start","session_shutdown",@@ -126,6 +130,25 @@ function decidedToolInput(event: OmpEvent): unknown {return pendingToolCalls.get(id)}+// The text of a user message Oh My Pi delivered to the model, or "" when the message is not an+// operator's prompt.+//+// Runtime-generated turns (auto-continue, reminders) are marked `synthetic`, and messages one agent+// hands to another carry `attribution: "agent"`; neither is something an operator asked. Image+// parts are left out: the prompt row records what was asked, not the attachment bytes.+function userMessageText(message: unknown): string {+ if (!message || typeof message !== "object") return ""+ const { role, synthetic, attribution, content } = message as Record<string, unknown>+ if (role !== "user" || synthetic === true || attribution === "agent") return ""+ if (typeof content === "string") return content+ if (!Array.isArray(content)) return ""+ const texts: string[] = []+ for (const part of content as Array<{ type?: unknown; text?: unknown } | null>) {+ if (part?.type === "text" && typeof part.text === "string") texts.push(part.text)+ }+ return texts.join("\n")+}+function debugLog(message: string, extra?: unknown) {if (!debugEnabled) returntry {@@ -302,12 +325,42 @@ type OmpExtensionAPI = {// createBeaconExtension is exported for tests; Oh My Pi loads the default export below.export function createBeaconExtension() {+ // The id of the current session once it has emitted `input` ("" for a session without one), or+ // null until it has. Interactive and RPC sessions emit `input` for every submission; ACP and+ // print never do. RPC and ACP both report `ctx.mode` "rpc", so this is what tells them apart.+ let inputSessionId: string | null = null++ // submittedPromptText decides whether a user message is a prompt no `input` event recorded.+ //+ // Interactive sessions are excluded outright: every prompt typed there arrives as `input`, and a+ // user message without one was sent by an extension, not typed. A session that has emitted+ // `input` is an RPC client, which reports every submission the same way. A subagent's messages+ // come from its parent's task, not an operator.+ const submittedPromptText = (message: unknown, ctx: OmpContext | undefined, sessionId: string) => {+ if (ctx?.mode === "tui" || ctx?.agent?.kind === "sub" || inputSessionId === sessionId) return ""+ return userMessageText(message)+ }+const forward = async (event: OmpEvent, ctx: OmpContext) => {try {// The event is spread last so a field it carries itself wins over the context's. `user_bash`// and `user_python` both carry their own cwd, and the approval events carry their own// sessionId -- in each case the event's is the one that describes this action.- const envelope: Record<string, unknown> = { ...identity(ctx), ...event }+ const fields = identity(ctx)+ const envelope: Record<string, unknown> = { ...fields, ...event }+ const sessionId = typeof envelope.sessionId === "string" ? envelope.sessionId : ""++ if (event.type === "input") {+ inputSessionId = sessionId+ } else if (event.type === "message_end" && (event.message as { role?: unknown })?.role === "user") {+ // Only the role and text leave the extension: a user message can also carry image bytes+ // and runtime bookkeeping the prompt row has no use for. One that is not a prompt is not+ // sent at all; the mapper records nothing for it.+ const prompt = submittedPromptText(event.message, ctx, sessionId)+ if (!prompt) return+ await sendToBeacon({ ...fields, type: "message_end", message: { role: "user" }, prompt })+ return+ }if (event.type === "tool_call") {rememberToolCall(event)@@ -319,6 +372,7 @@ export function createBeaconExtension() {// Done here rather than in a second `pi.on` registration so the whole cache lifecycle is// visible in one place.pendingToolCalls.clear()+ inputSessionId = null} else if (event.type === "tool_approval_requested" || event.type === "tool_approval_resolved") {// Attached under `input`, the same key `tool_call` uses, so the mapper reads one shape for// both and an approval resolves to the same command or file path its tool call did.