pi-ext
bugabingas pi extensions
owned by admin
feat: add horst extension for per-host data and tool deltas
025546d594fdbd8abd1b0dc5ace5a6447c899c06
Oliver Jan Krylow <oliver@bugabinga.net> ·
SSH signature · not checked
Browse filesCompare with HEAD
Changed files (8)
extensions/horst/README.md
Addednew file mode 100644 3 +Per-host environment data and tool defaults for Pi.
7 +Loaded through the root pi-ext package.
8 +See [root README](../../README.md).
10 +##
Commands / tools / settings 17 + `horst` block in `~/.pi/agent/settings.json` (global) or `.pi/settings.json`
18 + (project); project wins per key
20 + `session_start`, `before_agent_start`
28 + "win32": {
"tools": {
"add": [
"powershell"],
"remove": [
"bash"] } }
31 + "winbox": {
"tools": {
"remove": [
"read"] } }
37 +- `os` keys: `win32`, `lnx`, `mac`
38 +- `hosts` keys: hostname, lowercased
39 +- both maps optional, unknown names ignored with a warning
40 +- fold order: `os` first, then `hosts`; `hosts` wins per field
41 +- no match: no change
45 +- Injects a stable host data section (`<horst>`) into the system prompt:
46 + hostname, os, kernel, arch, cpu, memory, shell, editor.
47 +- Loads the snapshot lazily exactly once per process:
48 + `node:os` getters plus one `/etc/os-release` read on Linux.
49 +- Only stable facts enter the prompt, so the section produces one prompt patch
50 + and then stays cache-stable.
51 +- Applies the configured tool delta once per session start on top of the
extensions/horst/__tests__/harness.test.ts
Addednew file mode 100644 1 +import { mkdirSync, mkdtempSync, rmSync, writeFileSync }
from "node:fs";
2 +import os, { tmpdir }
from "node:os";
3 +import path
from "node:path";
4 +import { afterEach, describe, expect, it }
from "vitest";
5 +import { createTestSession, type TestSession }
from "../../../test/harness";
12 + EMPTY_HORST_SETTINGS,
16 +}
from "../settings.ts";
18 +const horstPath = ()
=> path.
resolve(
import.
meta.dirname,
"../index.ts");
20 +describe(
"pickHorst", ()
=> {
21 + it(
"keeps well-formed tool deltas and lowercases keys", ()
=> {
22 + const settings
= pickHorst({
23 + os: { WIN32: { tools: { add: [
"powershell"], remove: [
"bash"] } } },
24 + hosts: { WinBox: { tools: { remove: [
"write"] } } },
26 + expect(settings).
toEqual({
27 + os: { win32: { add: [
"powershell"], remove: [
"bash"] } },
28 + hosts: { winbox: { remove: [
"write"] } },
32 + it(
"drops malformed entries and keeps empty results empty", ()
=> {
36 + lnx: { tools: { add: [
42] }, flat: { add: [
"ls"] } },
39 + default: { tools: { add: [
"bash"] } },
41 + ).
toEqual(EMPTY_HORST_SETTINGS);
42 + expect(
pickHorst(
undefined)).
toEqual(EMPTY_HORST_SETTINGS);
46 +describe(
"mergeHorst", ()
=> {
47 + it(
"lets later partials win per key", ()
=> {
48 + const merged
= mergeHorst(
50 + os: { win32: { add: [
"powershell"] } },
51 + hosts: { a: { remove: [
"read"] } },
53 + { os: { win32: { remove: [
"bash"] } }, hosts: { b: { add: [
"grep"] } } },
55 + expect(merged).
toEqual({
56 + os: { win32: { remove: [
"bash"] } },
57 + hosts: { a: { remove: [
"read"] }, b: { add: [
"grep"] } },
62 +describe(
"resolveHorstTools", ()
=> {
63 + it(
"maps platform names to os keys", ()
=> {
66 + { os: { lnx: { add: [
"grep"] } }, hosts: {} },
70 + ).
toEqual({ add: [
"grep"] });
73 + { os: { mac: { add: [
"grep"] } }, hosts: {} },
77 + ).
toEqual({ add: [
"grep"] });
80 + { os: { win32: { add: [
"powershell"] } }, hosts: {} },
84 + ).
toEqual({ add: [
"powershell"] });
87 + it(
"matches hostnames case-insensitively and wins per field over os", ()
=> {
89 + os: { win32: { add: [
"powershell"], remove: [
"bash"] } },
90 + hosts: { winbox: { remove: [
"write"] } },
92 + expect(
resolveHorstTools(settings,
"win32",
"WinBox")).
toEqual({
93 + add: [
"powershell"],
98 + it(
"returns undefined without a match", ()
=> {
100 + resolveHorstTools(EMPTY_HORST_SETTINGS,
"linux",
"x"),
105 +describe(
"renderHostSection", ()
=> {
106 + it(
"renders stable facts and omits absent shell/editor", ()
=> {
107 + const host
: HostSnapshot = {
108 + hostname:
"winbox",
109 + osLabel:
"Fedora Linux 42",
112 + cpuModel:
"AMD Ryzen 9",
116 + const section
= renderHostSection(host);
117 + expect(section).
toContain(
"hostname: winbox");
118 + expect(section).
toContain(
"os: Fedora Linux 42");
119 + expect(section).
toContain(
"cpu: AMD Ryzen 9 × 24");
120 + expect(section).
toContain(
"mem: 32 GiB");
121 + expect(section).not.
toContain(
"shell:");
122 + expect(section).not.
toContain(
"editor:");
126 +describe(
"horst pi-test-harness", ()
=> {
127 + let t
: TestSession | undefined;
134 + it(
"loads in real Pi extension runtime",
async ()
=> {
135 + t
= await createTestSession({ extensions: [
horstPath()] });
137 + t.session.extensionRunner.extensions.
map((extension)
=> extension.path),
138 + ).
toContain(
horstPath());
141 + it(
"applies the host tool delta from project settings",
async ()
=> {
142 + const root
= mkdtempSync(path.
join(
tmpdir(),
"horst-delta-"));
144 + mkdirSync(path.
join(
root,
".pi"), { recursive:
true });
146 + path.
join(
root,
".pi",
"settings.json"),
150 + [os.
hostname().
toLowerCase()]: {
151 + tools: { add: [
"grep",
"find"], remove: [
"write"] },
157 + t
= await createTestSession({ cwd:
root, extensions: [
horstPath()] });
158 + const active
= t.session.
getActiveToolNames();
159 + expect(active).
toContain(
"grep");
160 + expect(active).
toContain(
"find");
161 + expect(active).not.
toContain(
"write");
165 + rmSync(
root, { recursive:
true, force:
true });
169 + it(
"injects the stable host section each turn",
async ()
=> {
170 + t
= await createTestSession({ extensions: [
horstPath()] });
171 + const runner
= t.session.extensionRunner;
172 + const first
= await runner.
emitBeforeAgentStart(
"prompt",
undefined, {
175 + const second
= await runner.
emitBeforeAgentStart(
"prompt",
undefined, {
178 + const { horst }
= first.systemPromptOptions.sections;
179 + expect(horst).
toBe(second.systemPromptOptions.sections.horst);
180 + expect(horst).
toContain(
`hostname: ${os.hostname()}`);
181 + expect(
getHostSection()).resolves.
toBe(horst);
extensions/horst/host.ts
Addednew file mode 100644 6 +import { readFile }
from "node:fs/promises";
7 +import os
from "node:os";
9 +export interface HostSnapshot {
21 +function osTypeLabel()
: string {
22 + if (
process.
platform === "win32")
return "Windows";
23 + if (
process.
platform === "darwin")
return "macOS";
28 +async function readPrettyName()
: Promise<
string | undefined> {
29 + if (
process.
platform !== "linux")
return undefined;
31 + const content
= await readFile(
"/etc/os-release",
"utf8");
32 + const match
= /^PRETTY_NAME=(?:"([^"]*)"|(.*?))\s*$/m.
exec(content);
33 + return match?.[
1]
?? match?.[
2]
?? undefined;
39 +async function loadHostSnapshot()
: Promise<
HostSnapshot> {
40 + const cpus
= os.
cpus();
42 + hostname: os.
hostname(),
43 + osLabel: (
await readPrettyName())
?? osTypeLabel(),
44 + kernel: os.
release(),
46 + cpuModel: cpus[
0]?.model.
trim()
|| undefined,
47 + cpuCount: cpus.
length,
48 + memGiB:
Math.
round(os.
totalmem()
/ 2 ** 30),
49 + shell:
process.
env.SHELL,
50 + editor:
process.
env.VISUAL
?? process.
env.EDITOR,
54 +let snapshot
: Promise<
HostSnapshot>
| undefined;
57 +export function getHostSnapshot()
: Promise<
HostSnapshot> {
58 + if (
!snapshot) snapshot
= loadHostSnapshot();
62 +export function renderHostSection(host
: HostSnapshot)
: string {
64 + "Stable host environment facts:",
65 + `hostname: ${host.hostname}`,
66 + `os: ${host.osLabel}`,
67 + `kernel: ${host.kernel}`,
68 + `arch: ${host.arch}`,
70 + ? `cpu: ${host.cpuModel} × ${host.cpuCount}` 71 + : `cpu: ${host.cpuCount} cores`,
72 + `mem: ${host.memGiB} GiB`,
74 + if (host.shell) lines.
push(
`shell: ${host.shell}`);
75 + if (host.editor) lines.
push(
`editor: ${host.editor}`);
76 + return lines.
join(
"\n");
79 +let section
: Promise<
string>
| undefined;
82 +export function getHostSection()
: Promise<
string> {
83 + if (
!section) section
= getHostSnapshot().
then(renderHostSection);
extensions/horst/index.ts
Addednew file mode 100644 3 +import os
from "node:os";
7 +}
from "@earendil-works/pi-coding-agent";
8 +import { SettingsManager }
from "@earendil-works/pi-coding-agent";
9 +import { getHostSection }
from "./host.ts";
15 +}
from "./settings.ts";
17 +const SETTINGS_KEY
= "horst";
19 +function readHorstSettings(ctx
: ExtensionContext)
: HorstSettings {
20 + const manager
= SettingsManager.
create(ctx.cwd,
undefined, {
21 + projectTrusted: ctx.
isProjectTrusted(),
23 + const raw = (settings
: Record<
string,
unknown>)
=> 24 + pickHorst(settings[SETTINGS_KEY]);
26 + raw(manager.
getGlobalSettings()
as Record<
string,
unknown>),
27 + raw(manager.
getProjectSettings()
as Record<
string,
unknown>),
31 +export default function horstExtension(pi
: ExtensionAPI) {
32 + pi.
on(
"session_start",
async (_event, ctx)
=> {
33 + const delta
= resolveHorstTools(
34 + readHorstSettings(ctx),
39 + const known
= new Set(pi.
getAllTools().
map((tool)
=> tool.
name));
40 + const unknown
= [
...(delta.add
?? []),
...(delta.remove
?? [])].
filter(
41 + (name)
=> !known.
has(name),
43 + if (unknown.
length > 0)
44 + ctx.ui.
notify(
`horst: unknown tools: ${unknown.join(", ")}`,
"warning");
47 + .
filter((name)
=> !delta.remove?.
includes(name));
48 + for (
const name
of delta.add
?? []) {
49 + if (known.
has(name)
&& !next.
includes(name)) next.
push(name);
51 + pi.
setActiveTools(next);
54 + pi.
on(
"before_agent_start",
async (event)
=> {
55 + event.systemPromptOptions.sections.horst
= await getHostSection();
extensions/horst/mise.toml
Addednew file mode 100644 2 +description =
"Lint horst extension" 3 +depends = [
"//:install"]
5 + "biome ci --config-path ../.. .",
6 + "rumdl check --config ../../.rumdl.toml .",
7 + "taplo format --check --config ../../.taplo.toml mise.toml",
11 +description =
"Test horst extension" 12 +depends = [
"//:install"]
14 + {
task =
"//:test",
args = [
"extensions/horst"] },
18 +description =
"Check horst extension" 19 +depends = [
"//:install"]
21 + {
task =
"//:build",
args = [
"horst"] },
extensions/horst/package.json
Addednew file mode 100644 2 + "name":
"@bugabinga/pi-ext-horst",
5 + "peerDependencies": {
6 + "@earendil-works/pi-coding-agent":
"*" 9 + "description":
"Per-host environment data and tool defaults for Pi.",
14 + "devDependencies": {
15 + "@marcfargas/pi-test-harness":
"^0.6.1",
extensions/horst/settings.ts
Addednew file mode 100644 5 +export interface HorstToolDelta {
10 +export interface HorstSettings {
11 + os
: Record<
string,
HorstToolDelta>;
12 + hosts
: Record<
string,
HorstToolDelta>;
15 +export const EMPTY_HORST_SETTINGS
: HorstSettings = { os: {}, hosts: {} };
17 +export function horstOsKey(platform
: string)
: string | undefined {
18 + if (platform
=== "linux")
return "lnx";
19 + if (platform
=== "darwin")
return "mac";
20 + if (platform
=== "win32")
return "win32";
24 +function toolNames(value
: unknown)
: string[]
| undefined {
25 + const names
= Array.
isArray(value)
27 + (name)
: name
is string => 28 + typeof name
=== "string" && name.
trim().
length > 0,
31 + return names.
length > 0 ? names
: undefined;
34 +function pickMap(value
: unknown)
: Record<
string,
HorstToolDelta> {
35 + if (
typeof value
!== "object" || value
=== null)
return {};
36 + const map
: Record<
string,
HorstToolDelta>
= {};
37 + for (
const [key, entry]
of Object.
entries(value)) {
38 + const tools
= (entry
as { tools
?: unknown }
| null)?.tools;
39 + if (
typeof tools
!== "object" || tools
=== null)
continue;
40 + const { add, remove }
= tools
as { add
?: unknown; remove
?: unknown };
41 + const delta
: HorstToolDelta = {};
42 + const addNames
= toolNames(add);
43 + const removeNames
= toolNames(remove);
44 + if (addNames) delta.add
= addNames;
45 + if (removeNames) delta.remove
= removeNames;
46 + if (delta.add
|| delta.remove) map[key.
toLowerCase()]
= delta;
52 +export function pickHorst(raw
: unknown)
: HorstSettings {
53 + if (
typeof raw
!== "object" || raw
=== null)
return EMPTY_HORST_SETTINGS;
54 + const block
= raw
as { os
?: unknown; hosts
?: unknown };
55 + return { os:
pickMap(block.os), hosts:
pickMap(block.hosts) };
59 +export function mergeHorst(
...partials
: HorstSettings[])
: HorstSettings {
60 + const merged
: HorstSettings = { os: {}, hosts: {} };
61 + for (
const partial
of partials) {
62 + Object.
assign(merged.os, partial.os);
63 + Object.
assign(merged.hosts, partial.hosts);
72 +export function resolveHorstTools(
73 + settings
: HorstSettings,
76 +)
: HorstToolDelta | undefined {
77 + const osDelta
= settings.os[
horstOsKey(platform)
?? ""];
78 + const hostDelta
= settings.hosts[hostname.
toLowerCase()];
79 + if (
!osDelta
&& !hostDelta)
return undefined;
81 + add: hostDelta?.add
?? osDelta?.add,
82 + remove: hostDelta?.remove
?? osDelta?.remove,
package-lock.json
Modified 168 + "extensions/horst": {
169 + "name":
"@bugabinga/pi-ext-horst",
171 + "devDependencies": {
172 + "@marcfargas/pi-test-harness":
"^0.6.1",
175 + "peerDependencies": {
176 + "@earendil-works/pi-coding-agent":
"*" 168 179 "extensions/intellij": {
169 180 "name":
"@bugabinga/pi-ext-intellij",
170 181 "license":
"MIT",
1292 1303 "resolved":
"extensions/good-job",
1306 + "node_modules/@bugabinga/pi-ext-horst": {
1307 + "resolved":
"extensions/horst",
1295 1310 "node_modules/@bugabinga/pi-ext-intellij": {
1296 1311 "resolved":
"extensions/intellij",