repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

Registered lightweight The System command/governance proxies and deferred parser implementation; import fell 91% Win32 and 84% WSL with all checks passing

26984a5d6bef599b53ccc1ead244ea30b3a9e109

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Result: {"status":"keep","the_system_import_ms":91,"elapsed_s":175.2,"win32_start_reload_ms":9221.157,"win32_extension_tax_ms":5072.027,"win32_startup_ms":6842.446,"win32_reload_ms":2378.711,"wsl_start_reload_ms":1886.281,"wsl_extension_tax_ms":945.895,"wsl_the_system_import_ms":29}

Changed files (4)

extensions/the-system/index.ts

Modified
1 -import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
1 +import {
2 + type ExtensionAPI,
3 + type ExtensionContext,
4 + isToolCallEventType,
5 +} from "@earendil-works/pi-coding-agent";
2 6 import { createMockupTool } from "./mockup.js";
3 -import { registerSystemGovernance } from "./system.js";
4 -import { createSystemCommand } from "./system-command.js";
7 +import { systemArgumentCompletions } from "./system-command-meta.js";
9 +function isStaleContextError(error: unknown): boolean {
10 + return (
11 + error instanceof Error &&
12 + error.message.includes("ctx is stale after session replacement or reload")
13 + );
6 16 export default function registerTheSystem(pi: ExtensionAPI): void {
7 - registerSystemGovernance(pi);
17 + let generation = 0;
18 + let notified = false;
20 + const notifyOnce = async (ctx: ExtensionContext, ticket: number) => {
21 + if (notified || ticket !== generation) return;
22 + try {
23 + const { runtimeNotice } = await import("./system.js");
24 + if (notified || ticket !== generation) return;
25 + const message = runtimeNotice(ctx.cwd);
26 + if (!message) return;
27 + if (ctx.hasUI) ctx.ui.notify(message, "warning");
28 + notified = true;
29 + } catch (error) {
30 + if (!isStaleContextError(error)) throw error;
31 + }
32 + };
34 + pi.on("session_start", (_event, ctx) => {
35 + const ticket = ++generation;
36 + notified = false;
37 + const timer = setTimeout(() => {
38 + if (ticket === generation)
39 + void notifyOnce(ctx, ticket).catch((error) =>
40 + console.error("The System startup check failed:", error),
41 + );
42 + }, 0);
43 + timer.unref?.();
44 + });
45 + pi.on("session_shutdown", () => {
46 + generation++;
47 + notified = false;
48 + });
49 + pi.on("before_agent_start", async (event, ctx) => {
50 + await notifyOnce(ctx, generation);
51 + const { composeSystemPrompt } = await import("./system.js");
52 + return { systemPrompt: composeSystemPrompt(ctx.cwd, event.systemPrompt) };
53 + });
54 + pi.on("tool_call", async (event, ctx) => {
55 + if (
56 + !isToolCallEventType("write", event) &&
57 + !isToolCallEventType("edit", event)
58 + )
59 + return;
60 + const { preflightFileMutation } = await import("./system.js");
61 + const ui = {
62 + hasUI: ctx.hasUI,
63 + select: (title: string, items: string[]) => ctx.ui.select(title, items),
64 + };
65 + if (isToolCallEventType("write", event))
66 + return preflightFileMutation(ctx.cwd, "write", event.input, ui);
67 + if (isToolCallEventType("edit", event))
68 + return preflightFileMutation(ctx.cwd, "edit", event.input, ui);
69 + });
8 71 pi.registerTool(createMockupTool());
9 - pi.registerCommand("system", createSystemCommand(pi));
72 + pi.registerCommand("system", {
73 + description: "The System project governance",
74 + getArgumentCompletions: systemArgumentCompletions,
75 + handler: async (args, ctx) => {
76 + const { createSystemCommand } = await import("./system-command.js");
77 + await createSystemCommand(pi).handler(args, ctx);
78 + },
79 + });

extensions/the-system/system-command-meta.ts

Added
new file mode 100644
1 +import type { AutocompleteItem } from "@earendil-works/pi-tui";
3 +export const SYSTEM_SUBCOMMANDS = [
4 + "init",
5 + "doctor",
6 + "view",
7 + "book",
8 + "edit",
9 + "research",
10 + "spec",
11 + "spec-verify",
12 + "phase",
13 + "plan",
14 + "issue",
15 + "exec",
16 +] as const;
18 +export function systemArgumentCompletions(
19 + prefix: string,
20 +): AutocompleteItem[] | null {
21 + if (prefix.includes(" ")) return null;
22 + if (SYSTEM_SUBCOMMANDS.some((item) => item === prefix)) return null;
23 + const items = SYSTEM_SUBCOMMANDS.filter((item) => item.startsWith(prefix)).map(
24 + (item) => ({ value: item, label: item }),
25 + );
26 + return items.length ? items : null;

extensions/the-system/system-command.ts

Modified
6 6 ExtensionCommandContext,
7 7 RegisteredCommand,
8 8 } from "@earendil-works/pi-coding-agent";
9 -import type { AutocompleteItem } from "@earendil-works/pi-tui";
11 9 import { runDoctor } from "./doctor.ts";
12 10 import {
13 11 type DocumentType,
20 18 import { applyInitialization, inspectInitialization } from "./init.ts";
21 19 import { openHtmlFile } from "./open-browser.ts";
22 20 import { openEditableFile } from "./open-editor.ts";
21 +import { systemArgumentCompletions } from "./system-command-meta.ts";
23 22 import { systemPromptFor } from "./system-prompts.ts";
25 24 const LIFECYCLE = new Set([
32 31 "exec",
34 33 const NON_LIFECYCLE = new Set(["init", "doctor", "view", "book", "edit"]);
35 -const SUBCOMMANDS = [
36 - "init",
37 - "doctor",
38 - "view",
39 - "book",
40 - "edit",
41 - "research",
42 - "spec",
43 - "spec-verify",
44 - "phase",
45 - "plan",
46 - "issue",
47 - "exec",
48 -] as const;
50 34 type CommandOptions = Omit<RegisteredCommand, "name" | "sourceInfo">;
52 36 export function createSystemCommand(pi: ExtensionAPI): CommandOptions {
53 37 return {
54 38 description: "The System project governance",
55 - getArgumentCompletions(prefix: string): AutocompleteItem[] | null {
56 - if (prefix.includes(" ")) return null;
57 - if (SUBCOMMANDS.some((item) => item === prefix)) return null;
58 - const items = SUBCOMMANDS.filter((item) => item.startsWith(prefix)).map(
59 - (item) => ({ value: item, label: item }),
60 - );
61 - return items.length ? items : null;
62 - },
39 + getArgumentCompletions: systemArgumentCompletions,
63 40 handler: async (args: string, ctx: ExtensionCommandContext) => {
64 41 const parsed = parseSystemArgs(args);
65 42 if (!parsed.subcommand) {

extensions/the-system/system.ts

Modified
2 2 import { existsSync, lstatSync, readFileSync } from "node:fs";
3 3 import path from "node:path";
4 4 import type {
5 5 EditToolInput,
6 - ExtensionAPI,
7 - ExtensionContext,
8 6 WriteToolInput,
9 7 } from "@earendil-works/pi-coding-agent";
10 -import { isToolCallEventType } from "@earendil-works/pi-coding-agent";
11 8 import {
12 9 type Diagnostic,
13 10 type DocumentMetadata,
417 -export function registerSystemGovernance(pi: ExtensionAPI): void {
418 - let notified = false;
419 - const notifyOnce = (_event: unknown, ctx: ExtensionContext) => {
420 - if (notified || !ctx.hasUI) return;
421 - const message = runtimeNotice(ctx.cwd);
422 - if (!message) return;
423 - ctx.ui.notify(message, "warning");
424 - notified = true;
425 - };
426 - pi.on("session_start", async (event, ctx) => {
427 - notified = false;
428 - notifyOnce(event, ctx);
429 - });
430 - pi.on("before_agent_start", async (event, ctx) => {
431 - notifyOnce(event, ctx);
432 - return { systemPrompt: composeSystemPrompt(ctx.cwd, event.systemPrompt) };
433 - });
434 - pi.on("tool_call", async (event, ctx) => {
435 - const ui: GovernanceUI = {
436 - hasUI: ctx.hasUI,
437 - select: (title, items) => ctx.ui.select(title, items),
438 - };
439 - if (isToolCallEventType("write", event))
440 - return preflightFileMutation(ctx.cwd, "write", event.input, ui);
441 - if (isToolCallEventType("edit", event))
442 - return preflightFileMutation(ctx.cwd, "edit", event.input, ui);
443 - });