ceulen 0.1.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/README.md +103 -14
  3. package/extensions/index.ts +113 -0
  4. package/extensions/lib/env.ts +75 -0
  5. package/extensions/lib/panel.ts +1720 -0
  6. package/extensions/lib/registry.ts +182 -0
  7. package/extensions/modules/composer/index.ts +128 -0
  8. package/extensions/modules/composer/lib/settings.ts +55 -0
  9. package/extensions/modules/composer/lib/shapes.ts +436 -0
  10. package/extensions/modules/composer/test/editor.test.ts +138 -0
  11. package/extensions/modules/composer/test/module.test.ts +218 -0
  12. package/extensions/modules/composer/test/screen.test.ts +195 -0
  13. package/extensions/modules/composer/test/shapes.test.ts +175 -0
  14. package/extensions/modules/config/index.ts +298 -0
  15. package/extensions/modules/config/piSettings.ts +467 -0
  16. package/extensions/modules/config/plugins.ts +290 -0
  17. package/extensions/modules/config/test/kernel.test.ts +523 -0
  18. package/extensions/modules/config/test/kernelV4.test.ts +196 -0
  19. package/extensions/modules/config/test/menu.test.ts +226 -0
  20. package/extensions/modules/config/test/panel.test.ts +395 -0
  21. package/extensions/modules/config/test/piSettings.test.ts +185 -0
  22. package/extensions/modules/config/test/plugins.test.ts +181 -0
  23. package/extensions/modules/config/test/preview-block.test.ts +88 -0
  24. package/extensions/modules/ponytail/UPSTREAM +26 -0
  25. package/extensions/modules/ponytail/index.ts +336 -0
  26. package/extensions/modules/ponytail/lib/config.ts +149 -0
  27. package/extensions/modules/ponytail/lib/instructions.ts +98 -0
  28. package/extensions/modules/ponytail/lib/subagent.ts +31 -0
  29. package/extensions/modules/ponytail/test/extension.test.ts +422 -0
  30. package/extensions/modules/ponytail/test/helpers.test.ts +163 -0
  31. package/extensions/modules/ponytail/test/subagent.test.ts +33 -0
  32. package/extensions/modules/router/commands/commands.ts +99 -0
  33. package/extensions/modules/router/configPanel.ts +89 -0
  34. package/extensions/modules/router/index.ts +75 -0
  35. package/extensions/modules/router/lib/client.ts +390 -0
  36. package/extensions/modules/router/lib/config.ts +142 -0
  37. package/extensions/modules/router/lib/migrate.ts +92 -0
  38. package/extensions/modules/router/lib/provider.ts +138 -0
  39. package/extensions/modules/router/lib/refresh.ts +19 -0
  40. package/extensions/modules/router/test/guard.test.ts +77 -0
  41. package/extensions/modules/router/test/unit.test.ts +1126 -0
  42. package/extensions/modules/usage/index.ts +1975 -0
  43. package/extensions/modules/usage/test/context.test.ts +387 -0
  44. package/extensions/modules/usage/test/env-gating.test.ts +113 -0
  45. package/extensions/modules/usage/test/opencode-go-usage.test.ts +139 -0
  46. package/extensions/modules/usage/test/parsers.test.ts +244 -0
  47. package/extensions/modules/usage/test/stale-ctx-regression.test.ts +294 -0
  48. package/extensions/modules/usage/test/zai-anthropic-adapter.test.ts +40 -0
  49. package/package.json +15 -3
  50. package/skills/ponytail/SKILL.md +124 -0
  51. package/skills/ponytail-audit/SKILL.md +41 -0
  52. package/skills/ponytail-debt/SKILL.md +44 -0
  53. package/skills/ponytail-gain/SKILL.md +51 -0
  54. package/skills/ponytail-help/SKILL.md +73 -0
  55. package/skills/ponytail-review/SKILL.md +57 -0
  56. package/extensions/index.js +0 -42
@@ -0,0 +1,298 @@
1
+ /**
2
+ * config module — owns the central `/config` panel for all ceulen modules.
3
+ *
4
+ * Renders one panel (extensions/lib/panel.ts) from per-module contributions:
5
+ * pi core settings (piSettings.ts), every ceulen module's descriptor groups,
6
+ * and the pi-package (plugins) groups — all categorized under OMP's settings
7
+ * taxonomy (Appearance, Model, Interaction, Context, Tasks, Providers, …).
8
+ *
9
+ * The bundle-level kill-switch is no longer a standalone "Modules" tab: each
10
+ * module's Enable row is prepended to its own functional section (Router →
11
+ * Providers, Ponytail → Tasks, …) so a feature is turned on/off where it is
12
+ * configured. Save routes edited row keys to their owner: `ceulen.disabled.*`
13
+ * → the kill-switch file, `pi.*` → SettingsManager.flush(), `ceulen.plugins.*`
14
+ * → the packages file, `<module>.<key>` → the module's `save()` — each in its
15
+ * own try/catch, so one failure never blocks the others.
16
+ *
17
+ * Naming note: `/settings` is a Pi builtin — an extension command with that
18
+ * name is skipped in autocomplete and renamed (`/settings:1`), never an
19
+ * override. `/config` is unused by Pi core.
20
+ */
21
+
22
+ import { SettingsManager, type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
23
+ import { openConfigPanel, row, type PanelGroup } from "../../lib/panel.js";
24
+ import { agentDirs, isCore, MODULES, disabledSource, readDisabled, writeDisabled, type ModuleConfig, type ModuleLoadDeps } from "../../lib/registry.js";
25
+ import { buildPiSettingsGroups, isPiKey, PI_TAB_ORDER, type PiMenuLookup } from "./piSettings.js";
26
+ import { buildPluginsGroups, isPluginsKey, openPluginsWorking, savePlugins } from "./plugins.js";
27
+
28
+ export const KILL_SWITCH_PREFIX = "ceulen.disabled.";
29
+
30
+ /** Read the current kill-switch state as a Set of ENABLED module names. */
31
+ function enabledModules(): Set<string> {
32
+ const disabled = new Set(readDisabled());
33
+ // Core modules are always enabled, whatever a stale settings file says.
34
+ return new Set(MODULES.map((m) => m.name).filter((n) => !disabled.has(n)));
35
+ }
36
+
37
+ /** The module's Enable kill-switch row over a live working Set (toggles
38
+ * mutate it; save diffs it against the persisted state). Lives at the top of
39
+ * the module's own section — the feature is turned on where it is
40
+ * configured. When the effective settings file is the project's, the
41
+ * description discloses where writes land. */
42
+ export function moduleEnableRow(name: string, describe: string, working: Set<string>): ReturnType<typeof row> {
43
+ const where = disabledSource().isProject ? "this project's .pi/settings.json" : "the global settings.json";
44
+ return row(`${KILL_SWITCH_PREFIX}${name}`, "Enabled", "toggle", working.has(name), (v) => {
45
+ if (v) working.add(name);
46
+ else working.delete(name);
47
+ }, {
48
+ description: `${describe} Writes ${where}.`,
49
+ // Sole warning rows: the kill-switch does NOT apply on save — the ⚠
50
+ // marks "needs /reload" against the instant-apply rest.
51
+ warning: "Takes effect after /reload (or restart).",
52
+ defaultValue: true,
53
+ });
54
+ }
55
+
56
+ /** Prepend a module's Enable row to the FIRST group of its contribution. */
57
+ export function withEnableRow(groups: PanelGroup[], name: string, describe: string, working: Set<string>): PanelGroup[] {
58
+ // Core modules are always on — no kill-switch row (nothing to toggle).
59
+ if (isCore(name)) return groups;
60
+ const [first, ...rest] = groups;
61
+ if (!first) return groups;
62
+ return [{ ...first, rows: [moduleEnableRow(name, describe, working), ...first.rows] }, ...rest];
63
+ }
64
+
65
+ /** The kill-switch list that results from toggling `working` (enabled set).
66
+ * Pure — exported for tests. Core modules are excluded (never disableable). */
67
+ export function nextDisabled(working: Set<string>): string[] {
68
+ return MODULES.filter((m) => !m.core).map((m) => m.name).filter((n) => !working.has(n));
69
+ }
70
+
71
+ /** Runtime lookups for the pi-settings menu rows, built from the /config
72
+ * command's live ExtensionContext. Theme browsing previews through the Theme
73
+ * INSTANCE form of `ctx.ui.setTheme` (live, no settings.json write) and
74
+ * remembers the pre-browse instance so Esc can undo; committing goes through
75
+ * the NAME form (live + persist). Exported for tests. */
76
+ export function piMenuLookup(ctx: ExtensionContext): PiMenuLookup {
77
+ let before: ReturnType<() => typeof ctx.ui.theme> | undefined;
78
+ return {
79
+ themes: () => ctx.ui.getAllThemes().map((t) => t.name),
80
+ previewTheme: (name) => {
81
+ const t = ctx.ui.getTheme(name);
82
+ if (!t) return;
83
+ // Capture the LIVE theme once per browse session — the instance form
84
+ // only swaps the running theme, never settings.json.
85
+ if (before === undefined) before = ctx.ui.theme;
86
+ ctx.ui.setTheme(t);
87
+ },
88
+ restoreTheme: () => {
89
+ const prev = before;
90
+ before = undefined;
91
+ if (prev) ctx.ui.setTheme(prev);
92
+ },
93
+ applyTheme: (name) => {
94
+ ctx.ui.setTheme(name);
95
+ before = undefined;
96
+ },
97
+ models: () =>
98
+ ctx.modelRegistry.getAvailable().map((mo) => ({
99
+ provider: mo.provider,
100
+ id: mo.id,
101
+ ...(mo.name && mo.name !== mo.id ? { description: mo.name } : {}),
102
+ })),
103
+ };
104
+ }
105
+
106
+ /** Run each contribution's save, isolated: one module's failure notifies an
107
+ * error but never blocks the others. Each save() no-ops unless one of its own
108
+ * keys was edited. Exported for tests. */
109
+ export async function saveContributions(
110
+ contributions: { name: string; cfg: ModuleConfig }[],
111
+ keys: Set<string>,
112
+ ctx: Pick<ExtensionContext, "ui">,
113
+ ): Promise<void> {
114
+ for (const c of contributions) {
115
+ try {
116
+ await c.cfg.save(keys, ctx as ExtensionContext);
117
+ } catch (e) {
118
+ ctx.ui.notify(`${c.name}: save failed: ${e instanceof Error ? e.message : e}`, "error");
119
+ }
120
+ }
121
+ }
122
+
123
+ function summaryLines(groups: PanelGroup[]): string[] {
124
+ const enabled = enabledModules();
125
+ const lines = [
126
+ "── Ceulen config ──",
127
+ `Modules: ${MODULES.map((m) => `${m.name}${enabled.has(m.name) ? "" : " (off)"}`).join(", ")}`,
128
+ "",
129
+ ];
130
+ // Generic row dump per tab/section (label: value) — stays in lockstep with
131
+ // the panel's assembly, pi settings and plugins included.
132
+ let lastTab = "";
133
+ let lastSection = "";
134
+ for (const g of groups) {
135
+ const tab = g.tab ?? g.label;
136
+ if (tab !== lastTab) {
137
+ lines.push(`[${tab}]`);
138
+ lastTab = tab;
139
+ lastSection = "";
140
+ }
141
+ if (g.label !== lastSection && g.tab !== undefined) {
142
+ lines.push(` ${g.label}`);
143
+ lastSection = g.label;
144
+ }
145
+ for (const r of g.rows) {
146
+ const value = r.mask && String(r.value ?? "") !== "" ? "••••" : String(r.value ?? "");
147
+ const warning = r.warning ? ` ⚠ ${r.warning}` : "";
148
+ const desc = r.description ? ` — ${r.description}` : "";
149
+ lines.push(` ${r.label}: ${value}${desc}${warning}`);
150
+ }
151
+ }
152
+ lines.push(
153
+ "",
154
+ " /config Open the central settings panel (TUI)",
155
+ " /ceulen Module status",
156
+ " /ponytail status Ponytail mode + default",
157
+ "",
158
+ "Kill-switch: `ceulen.disabled` in settings.json; /reload (or restart) applies.",
159
+ );
160
+ return lines;
161
+ }
162
+
163
+ export default function configModule(pi: ExtensionAPI, deps?: ModuleLoadDeps): void {
164
+ // One contribution factory per module that opted in, keyed by module name.
165
+ // Each factory closes over the module's OWN guarded pi (see ModuleLoadDeps),
166
+ // so a save that re-registers a provider stays the owner's claim.
167
+ const factories = deps?.configContribs ?? new Map<string, () => ModuleConfig>();
168
+
169
+ // Where each module's section lives in OMP's taxonomy — pretty section
170
+ // names + icons only; the TAB comes from the registry's `category` (one
171
+ // source: adding a module = one registry entry, no second map to touch).
172
+ // A module WITHOUT a contribution factory (usage, config) renders an
173
+ // Enable-only section here; unknown modules fall back to their own tab so
174
+ // a new module still renders.
175
+ const PRETTY_OF: Record<string, { section: string; icon: string }> = {
176
+ router: { section: "Router", icon: "🌐" },
177
+ usage: { section: "Usage footer", icon: "📊" },
178
+ composer: { section: "Composer", icon: "🎨" },
179
+ ponytail: { section: "Ponytail", icon: "🦥" },
180
+ config: { section: "Ceulen config", icon: "🧩" },
181
+ };
182
+ const SECTION_OF: Record<string, { tab: string; section: string; icon: string | undefined }> = Object.fromEntries(
183
+ MODULES.map((m) => [m.name, { tab: m.category, section: PRETTY_OF[m.name]?.section ?? m.name, icon: PRETTY_OF[m.name]?.icon }]),
184
+ );
185
+
186
+ pi.registerCommand("config", {
187
+ description: "Central settings panel for all ceulen modules.",
188
+ handler: async (args, ctx) => {
189
+ const sub = String(args ?? "").trim().toLowerCase();
190
+ const workingEnabled = new Set(enabledModules());
191
+ const pluginsWorking = openPluginsWorking();
192
+
193
+ // Pi core settings: an OWN SettingsManager instance over the same files
194
+ // (typed setters persist to global settings.json with pi's own locking;
195
+ // the running session picks changes up on /reload).
196
+ const piSettings = SettingsManager.create(ctx.cwd, agentDirs()[0]);
197
+
198
+ // A DISABLED module has no registered factory (the loader skips it), so
199
+ // its section collapses to the Enable row below — settings are moot
200
+ // while off, and the row must stay reachable to turn it back on.
201
+ const contributions = [...factories.entries()].map(([name, factory]) => ({ name, cfg: factory() }));
202
+
203
+ // Assemble in OMP taxonomy order: pi settings first (they own the
204
+ // Appearance/Model/Interaction/Context/Shell spine), then ceulen module
205
+ // sections with their Enable row, then plugins.
206
+ const assemble = (): PanelGroup[] => {
207
+ const seen = new Set(contributions.map((c) => c.name));
208
+ const modGroups = contributions.flatMap((c) => {
209
+ const section = SECTION_OF[c.name];
210
+ const groups = c.cfg.groups();
211
+ if (!section) return groups;
212
+ const module = MODULES.find((m) => m.name === c.name);
213
+ return withEnableRow(groups, c.name, module?.describe ?? "", workingEnabled);
214
+ });
215
+ // Modules with no contribution factory still need their Enable row
216
+ // reachable (usage, config itself) — and a disabled module needs the
217
+ // same synthesized section to be switchable back on. Core modules are
218
+ // always on: no synthesized Enable-only section (their contribution,
219
+ // if any, renders without one).
220
+ for (const m of MODULES) {
221
+ const section = SECTION_OF[m.name];
222
+ if (!section || seen.has(m.name) || m.core) continue;
223
+ modGroups.push({
224
+ key: `ceulen-${m.name}`,
225
+ label: section.section,
226
+ tab: section.tab,
227
+ icon: section.icon,
228
+ rows: [moduleEnableRow(m.name, m.describe ?? "", workingEnabled)],
229
+ });
230
+ }
231
+ // Plugins' package sections precede the config module's own switch.
232
+ const all = [...buildPiSettingsGroups(piSettings, piMenuLookup(ctx)), ...buildPluginsGroups(pluginsWorking), ...modGroups];
233
+ const rank = (g: PanelGroup) => {
234
+ const at = PI_TAB_ORDER.indexOf((g.tab ?? g.label) as (typeof PI_TAB_ORDER)[number]);
235
+ return at === -1 ? PI_TAB_ORDER.length : at;
236
+ };
237
+ return [...all].sort((a, b) => rank(a) - rank(b));
238
+ };
239
+
240
+ if (sub === "show" || ctx.mode !== "tui" || !ctx.hasUI) {
241
+ ctx.ui.notify(summaryLines(assemble()).join("\n"), "info");
242
+ return;
243
+ }
244
+
245
+ await openConfigPanel({
246
+ ctx,
247
+ cfg: {},
248
+ title: "Settings",
249
+ build: assemble,
250
+ onSave: async (_saved, edited) => {
251
+ const keys = edited ?? new Set<string>();
252
+
253
+ // Kill-switch first: compare the working Set against what's persisted.
254
+ if ([...keys].some((k) => k.startsWith(KILL_SWITCH_PREFIX))) {
255
+ try {
256
+ const file = writeDisabled(nextDisabled(workingEnabled));
257
+ ctx.ui.notify(`Module kill-switch saved to ${file} — /reload (or restart) to apply.`, "info");
258
+ } catch (e) {
259
+ ctx.ui.notify(`Kill-switch save failed: ${e instanceof Error ? e.message : e}`, "error");
260
+ }
261
+ }
262
+
263
+ // Pi packages (plugins): writes land in the file each entry came from.
264
+ if ([...keys].some(isPluginsKey)) {
265
+ try {
266
+ const files = savePlugins(pluginsWorking);
267
+ if (files.length > 0) {
268
+ ctx.ui.notify(`Plugins saved to ${files.join(", ")} — /reload (or restart) to apply.`, "info");
269
+ }
270
+ } catch (e) {
271
+ ctx.ui.notify(`Plugins save failed: ${e instanceof Error ? e.message : e}`, "error");
272
+ }
273
+ }
274
+
275
+ // Module contributions: each isolated — one module's failure must not
276
+ // block the others (nor the kill-switch above).
277
+ await saveContributions(contributions, keys, ctx);
278
+
279
+ // Pi core settings: setters already queued their writes; flush the
280
+ // manager's write queue and surface any persistence errors.
281
+ if ([...keys].some(isPiKey)) {
282
+ try {
283
+ await piSettings.flush();
284
+ const errors = piSettings.drainErrors();
285
+ if (errors.length > 0) {
286
+ ctx.ui.notify(`Pi settings: ${errors.map((e) => e.error.message).join("; ")}`, "error");
287
+ } else {
288
+ ctx.ui.notify("Pi settings saved — /reload (or restart) to apply.", "info");
289
+ }
290
+ } catch (e) {
291
+ ctx.ui.notify(`Pi settings save failed: ${e instanceof Error ? e.message : e}`, "error");
292
+ }
293
+ }
294
+ },
295
+ });
296
+ },
297
+ });
298
+ }