ceulen 0.1.0 → 0.5.0

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,149 @@
1
+ // ponytail — shared configuration resolver
2
+ //
3
+ // Resolution order for default mode:
4
+ // 1. PONYTAIL_DEFAULT_MODE environment variable
5
+ // 2. Config file defaultMode field (XDG_CONFIG_HOME/ponytail/config.json)
6
+ // 3. 'full'
7
+ //
8
+ // Ported from pi-ponytail hooks/ponytail-config.js (CJS → ESM TS).
9
+
10
+ import * as fs from "node:fs";
11
+ import * as os from "node:os";
12
+ import * as path from "node:path";
13
+
14
+ export const DEFAULT_MODE = "full";
15
+ export const VALID_MODES = ["off", "lite", "full", "ultra", "review"] as const;
16
+ export const RUNTIME_MODES = ["off", "lite", "full", "ultra"] as const;
17
+
18
+ export type RuntimeMode = (typeof RUNTIME_MODES)[number];
19
+ export type PonytailMode = (typeof VALID_MODES)[number];
20
+
21
+ export function normalizeMode(mode: unknown): RuntimeMode | null {
22
+ if (typeof mode !== "string") return null;
23
+ const normalized = mode.trim().toLowerCase();
24
+ return (RUNTIME_MODES as readonly string[]).includes(normalized) ? (normalized as RuntimeMode) : null;
25
+ }
26
+
27
+ export function normalizePersistedMode(mode: unknown): PonytailMode | null {
28
+ if (typeof mode !== "string") return null;
29
+ const n = mode.trim().toLowerCase();
30
+ return normalizeMode(n) || ((VALID_MODES as readonly string[]).includes(n) ? (n as PonytailMode) : null);
31
+ }
32
+
33
+ export function isDeactivationCommand(text: unknown): boolean {
34
+ const t = String(text || "")
35
+ .trim()
36
+ .toLowerCase()
37
+ .replace(/[.!?\s]+$/, "");
38
+ return t === "stop ponytail" || t === "normal mode";
39
+ }
40
+
41
+ function getConfigDir(): string {
42
+ if (process.env.XDG_CONFIG_HOME) {
43
+ return path.join(process.env.XDG_CONFIG_HOME, "ponytail");
44
+ }
45
+ if (process.platform === "win32") {
46
+ return path.join(process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming"), "ponytail");
47
+ }
48
+ return path.join(os.homedir(), ".config", "ponytail");
49
+ }
50
+
51
+ function getConfigPath(): string {
52
+ return path.join(getConfigDir(), "config.json");
53
+ }
54
+
55
+ // Memoized read: one stat per call, re-parse only when path/mtime/size change.
56
+ // ponytail: stat-per-call keeps writeDefaultMode and test XDG swaps correct;
57
+ // drop the stat if profiling ever shows it matters.
58
+ let configCache: { path: string | null; mtimeMs: number; size: number; config: Record<string, unknown> } = {
59
+ path: null,
60
+ mtimeMs: -1,
61
+ size: -1,
62
+ config: {},
63
+ };
64
+
65
+ function readConfig(): Record<string, unknown> {
66
+ const configPath = getConfigPath();
67
+ let st: fs.Stats;
68
+ try {
69
+ st = fs.statSync(configPath);
70
+ if (configCache.path === configPath && configCache.mtimeMs === st.mtimeMs && configCache.size === st.size) {
71
+ return configCache.config;
72
+ }
73
+ } catch {
74
+ // Missing/unreadable file → empty config; cache it for this path.
75
+ if (configCache.path === configPath && configCache.mtimeMs === -1) {
76
+ return configCache.config;
77
+ }
78
+ configCache = { path: configPath, mtimeMs: -1, size: -1, config: {} };
79
+ return configCache.config;
80
+ }
81
+ let config: Record<string, unknown> = {};
82
+ try {
83
+ const raw = fs.readFileSync(configPath, "utf8").replace(/^\uFEFF/, "");
84
+ const parsed = JSON.parse(raw) as unknown;
85
+ if (parsed && typeof parsed === "object") config = parsed as Record<string, unknown>;
86
+ } catch {
87
+ // Malformed config → empty
88
+ }
89
+ configCache = { path: configPath, mtimeMs: st.mtimeMs, size: st.size, config };
90
+ return config;
91
+ }
92
+
93
+ function readConfigBool(envVar: string, configKey: string): boolean {
94
+ const env = process.env[envVar];
95
+ if (env !== undefined) {
96
+ const v = env.trim().toLowerCase();
97
+ return v !== "" && v !== "0" && v !== "false" && v !== "no";
98
+ }
99
+ return readConfig()[configKey] === true;
100
+ }
101
+
102
+ export function getDefaultMode(): RuntimeMode {
103
+ const envMode = process.env.PONYTAIL_DEFAULT_MODE;
104
+ // ponytail: a default must be a runtime level (off/lite/full/ultra)
105
+ if (envMode && (RUNTIME_MODES as readonly string[]).includes(envMode.toLowerCase())) {
106
+ return envMode.toLowerCase() as RuntimeMode;
107
+ }
108
+ const config = readConfig();
109
+ const configMode = config.defaultMode;
110
+ if (typeof configMode === "string" && (RUNTIME_MODES as readonly string[]).includes(configMode.toLowerCase())) {
111
+ return configMode.toLowerCase() as RuntimeMode;
112
+ }
113
+ return DEFAULT_MODE;
114
+ }
115
+
116
+ export function getQuietStartup(): boolean {
117
+ return readConfigBool("PONYTAIL_QUIET_STARTUP", "quietStartup");
118
+ }
119
+
120
+ export function getHideStatus(): boolean {
121
+ return readConfigBool("PONYTAIL_HIDE_STATUS", "hideStatus");
122
+ }
123
+
124
+ export function writeDefaultMode(mode: string): RuntimeMode | null {
125
+ const normalized = normalizeMode(mode);
126
+ if (!normalized) return null;
127
+
128
+ const config = readConfig();
129
+ config.defaultMode = normalized;
130
+
131
+ writeConfig(config);
132
+ return normalized;
133
+ }
134
+
135
+ /** Merge boolean flags into the config file (read-modify-write, atomic value
136
+ * write; the module's memoized read cache is invalidated). */
137
+ export function writeConfigBools(patch: { quietStartup?: boolean; hideStatus?: boolean }): void {
138
+ const config = readConfig();
139
+ if (patch.quietStartup !== undefined) config.quietStartup = patch.quietStartup;
140
+ if (patch.hideStatus !== undefined) config.hideStatus = patch.hideStatus;
141
+ writeConfig(config);
142
+ }
143
+
144
+ function writeConfig(config: Record<string, unknown>): void {
145
+ const configPath = getConfigPath();
146
+ fs.mkdirSync(path.dirname(configPath), { recursive: true });
147
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8");
148
+ configCache = { path: null, mtimeMs: -1, size: -1, config: {} }; // force re-read
149
+ }
@@ -0,0 +1,98 @@
1
+ // Shared Ponytail instruction builder for the Pi extension.
2
+ //
3
+ // Ported from pi-ponytail hooks/ponytail-instructions.js (CJS → ESM TS).
4
+ // SKILL_PATH resolves 4 levels up (lib → ponytail → modules → extensions →
5
+ // package root), which holds in-repo and in installed node_modules because the
6
+ // package ships `extensions/` + `skills/` together.
7
+
8
+ import * as fs from "node:fs";
9
+ import { DEFAULT_MODE, normalizeMode, normalizePersistedMode, type PonytailMode } from "./config.js";
10
+
11
+ const SKILL_PATH = new URL("../../../../skills/ponytail/SKILL.md", import.meta.url);
12
+
13
+ // Module-level cache: readFileSync(SKILL.md) once per process instead of on
14
+ // every before_agent_start (every turn). Re-read on failure so a SKILL.md
15
+ // that appears mid-session (install/update) is picked up on the next turn.
16
+ let skillBodyCache: string | null = null; // null = unread
17
+
18
+ function readSkillBody(): string | null {
19
+ if (skillBodyCache !== null) return skillBodyCache;
20
+ try {
21
+ skillBodyCache = fs.readFileSync(SKILL_PATH, "utf8");
22
+ } catch {
23
+ return null;
24
+ }
25
+ return skillBodyCache;
26
+ }
27
+
28
+ export function filterSkillBodyForMode(body: string, mode: string): string {
29
+ const effectiveMode = normalizeMode(mode) || DEFAULT_MODE;
30
+ const withoutFrontmatter = String(body || "").replace(/^---[\s\S]*?---\s*/, "");
31
+
32
+ return withoutFrontmatter
33
+ .split(/\r?\n/)
34
+ .filter((line) => {
35
+ const tableLabel = line.match(/^\|\s*\*\*(.+?)\*\*\s*\|/);
36
+ if (tableLabel) {
37
+ const labelMode = normalizeMode(tableLabel[1].trim());
38
+ if (labelMode) return labelMode === effectiveMode;
39
+ }
40
+
41
+ // Require a quoted value: every worked example is `- lite: "..."`. Without
42
+ // this, an ordinary rule bullet that happens to start with a mode word
43
+ // (e.g. "- Full: real rule text.") is silently dropped in every other mode.
44
+ const exampleLabel = line.match(/^-\s*([^:]+):\s*"/);
45
+ if (exampleLabel) {
46
+ const labelMode = normalizeMode(exampleLabel[1].trim());
47
+ if (labelMode) return labelMode === effectiveMode;
48
+ }
49
+
50
+ return true;
51
+ })
52
+ .join("\n");
53
+ }
54
+
55
+ export function getPonytailInstructions(mode: string): string {
56
+ const configuredMode: PonytailMode = normalizePersistedMode(mode) || DEFAULT_MODE;
57
+ if (configuredMode === "review") {
58
+ return "PONYTAIL MODE ACTIVE — level: review. Behavior defined by /ponytail-review skill.";
59
+ }
60
+ const effectiveMode = normalizeMode(configuredMode) || DEFAULT_MODE;
61
+
62
+ const body = readSkillBody();
63
+ if (body !== null) {
64
+ return (
65
+ "PONYTAIL MODE ACTIVE — level: " +
66
+ effectiveMode +
67
+ "\n\n" +
68
+ filterSkillBodyForMode(body, effectiveMode)
69
+ );
70
+ } else {
71
+ // ponytail: SKILL.md missing or unreadable — compact inline fallback keeps the ladder and rules.
72
+ return [
73
+ "PONYTAIL MODE ACTIVE — level: " + effectiveMode,
74
+ "",
75
+ "Persistence: ACTIVE EVERY RESPONSE. No drift back to over-building.",
76
+ 'Off only: "stop ponytail" / "normal mode". Default: **full**.',
77
+ "",
78
+ "The ladder (stop at the first rung that holds):",
79
+ "1. Does this need to exist at all? (YAGNI)",
80
+ "2. Already in this codebase? Reuse it.",
81
+ "3. Stdlib does it? Use it.",
82
+ "4. Native platform feature covers it?",
83
+ "5. Already-installed dependency solves it?",
84
+ "6. Can it be one line? One line.",
85
+ "7. Only then: minimum code that works.",
86
+ "",
87
+ "Rules: No speculative abstractions. Deletion over addition. Fewest files.",
88
+ "Mark shortcuts with ponytail: comments. One-line counterfactual: if the complex",
89
+ "version is not a one-liner or 5-line trivial, it's over-engineered.",
90
+ "",
91
+ "Output: Code first. Then at most 3 lines: what was skipped, when to add it.",
92
+ "Pattern: [code] → skipped: [X], add when [Y].",
93
+ "",
94
+ "Never simplify away: input validation, error handling, security, accessibility,",
95
+ "or anything explicitly requested. Non-trivial logic needs one runnable check.",
96
+ ].join("\n");
97
+ }
98
+ }
@@ -0,0 +1,31 @@
1
+ // ponytail — compact subagent-scope instructions injected into pi-subagent tool calls.
2
+ // Built-in-only children don't load extensions, so without this hook they never see the
3
+ // ruleset; extension-loaded children self-inject via before_agent_start, which skips
4
+ // when the marker is already present. Kept separate from instructions.ts to avoid
5
+ // stale exports on /reload.
6
+ //
7
+ // Ported from pi-ponytail hooks/ponytail-subagent.js (CJS → ESM TS).
8
+
9
+ export function getSubagentInstructions(mode: string): string {
10
+ const effectiveMode = mode || "full";
11
+ return [
12
+ "PONYTAIL MODE ACTIVE — level: " + effectiveMode + " (inherited from parent).",
13
+ "",
14
+ "The ladder (stop at the first rung that holds): 1) Does this need to exist at all? (YAGNI) 2) Already in this codebase? Reuse it — look before you write. 3) Stdlib does it? 4) Native platform feature covers it? 5) Already-installed dependency solves it? 6) Can it be one line? 7) Only then: the minimum code that works — after reading what you touch.",
15
+ "",
16
+ "Rules: no speculative abstractions, no config nobody sets, no scaffolding for later. Deletion over addition. Fewest files. Boring over clever. Mark deliberate corner-cuts with a `ponytail:` comment naming the ceiling and upgrade path.",
17
+ "",
18
+ "Never simplify away: input validation at trust boundaries, error handling that prevents data loss, security, accessibility, anything explicitly requested. Non-trivial logic (a branch, a loop, a parser, a money/security path) leaves ONE runnable check behind.",
19
+ "",
20
+ "Bug fix = root cause, not symptom: grep every caller, fix where all route through.",
21
+ "",
22
+ "Plans, reviews, and reports the task explicitly requests: deliver in full. This block governs what you build, not requested output.",
23
+ ].join("\n");
24
+ }
25
+
26
+ export function shouldInjectSubagentInstructions(): boolean {
27
+ const scope = String(process.env.PONYTAIL_SUBAGENT_SCOPE || "")
28
+ .trim()
29
+ .toLowerCase();
30
+ return scope !== "off";
31
+ }