๐ OMP EXTENSION SPEEDRUN MASTERCLASS
Status: 100% Perfected & Synthesized Objective: Practical Highest-Level Idiot Overstanding of OMP Extensions.
An OMP Extension is a single TypeScript file loaded by the oh-my-pi framework. It exports a default function that binds to the OMP event bus via pi.on().
๐ก๏ธ The 3 Golden Invariants (Do Not Violate)
- โก Zero-Blocking (Async Egress): Never await heavy network or disk I/O in your event handlers. OMP halts while it waits for you. Enqueue payloads and use an out-of-band
setIntervalflusher. - ๐ก๏ธ Zero-Crashing (Safe Wrappers): If your extension throws an unhandled exception, OMP dies. Wrap every hook in a
try/catch(see the Idiot-Proof Skeleton). - ๐งน Zero-Leakage (Redaction):
tool_callandbefore_provider_requestpayloads contain raw LLM prompts, which will include the userโs API keys, JWTs, and passwords. You MUST apply regex scrubbing before writing to disk or network.
๐ช The Definitive Lifecycle Hooks Dictionary
Extracted directly from production extensions (langfuse.ts, ompkeep.ts, orca-agent-status.ts).
๐ง Session & Agent Lifecycle
session_startโ Fired once per user execution. Good place to initialize trace IDs.session_shutdownโ Fired on exit. CRITICAL: Use this toclearIntervaland await your final network flush.goal_updatedโ Fired when the sessionโs overarching objective changes.before_agent_start/agent_startโ Fired when a subagent is spawned.agent_endโ Fired when a subagent terminates.
๐ Turn & Generation (The Core Loop)
turn_start/turn_endโ A โturnโ is one cycle of thinking/acting. End payload containsdurationMsandcontextTokens.before_provider_requestโ Right before the LLM is called. Contains the full prompt.after_provider_responseโ LLM responds. Contains output tokens, latency, cost, and stop reasons.message_endโ Fired when an agent message concludes.
๐ ๏ธ Tool Execution
tool_execution_start/tool_callโ Agent requests a tool. Contains raw input arguments (scrub these!).tool_execution_end/tool_resultโ Tool finishes. Contains output/error. Deep-truncate outputs (they can be huge base64 strings).
๐จ Diagnostics & Recovery
session_compactโ Context window exceeded; OMP summarized the history.auto_retry_startโ OMP caught an error and is automatically retrying.ttsr_triggeredโ Test-Time System Prompt Modification triggered (rule injection).
๐๏ธ The โIdiot-Proofโ Skeleton (Copy-Paste)
This skeleton guarantees you will never crash OMP and never block the main thread.
import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent";
export default function myGodTierExtension(pi: ExtensionAPI): void {
// 1. STATE & QUEUES
const queue: any[] = [];
let flushTimer: ReturnType<typeof setInterval>;
let pendingFlush = Promise.resolve();
// 2. THE SAFE WRAPPER (Prevents Crashing OMP)
function safeOn(event: string, handler: (...args: any[]) => Promise<void>) {
pi.on(event as any, async (...args: any[]) => {
try { await handler(...args); }
catch (err) { pi.logger.warn(`[MyExt] Error in ${event}: ${String(err)}`); }
});
}
// 3. NON-BLOCKING FLUSHER
function flush() {
if (queue.length === 0) return;
const batch = queue.splice(0);
pendingFlush = pendingFlush.then(async () => {
try {
// e.g., await fetch('...', { body: JSON.stringify(batch), signal: AbortSignal.timeout(5000) });
} catch (e) { /* ignore */ }
});
}
// 4. LIFECYCLE BINDINGS
safeOn("session_start", async (_ev, ctx) => {
flushTimer = setInterval(flush, 2000); // Flush every 2s out-of-band
pi.logger.info("[MyExt] Online.");
});
safeOn("before_provider_request", async (ev) => {
// Redact and push to queue, DO NOT await fetch here!
queue.push({ type: "llm_req", model: ev.model });
});
safeOn("session_shutdown", async () => {
clearInterval(flushTimer);
flush();
await pendingFlush; // Block ONLY on shutdown to guarantee delivery
});
}