@spences10/pi-coding-preferences 0.0.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Scott Spence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @spences10/pi-coding-preferences
2
+
3
+ Pi extension that blocks configured coding workflow anti-patterns
4
+ before agents run them.
5
+
6
+ It ships with default preferences for Scott's workflow. Add user
7
+ preferences at `~/.pi/agent/coding-preferences.json` and project
8
+ preferences at `.pi/coding-preferences.json`; when either file exists,
9
+ configured rules are loaded instead of the built-in defaults.
10
+
11
+ ```json
12
+ {
13
+ "rules": [
14
+ {
15
+ "name": "no-npm",
16
+ "toolNames": ["bash"],
17
+ "target": "command",
18
+ "pattern": "^npm\\\\b",
19
+ "reason": "Use pnpm in this repo."
20
+ }
21
+ ]
22
+ }
23
+ ```
24
+
25
+ Rule targets are `command`, `path`, or `input`. Patterns are
26
+ JavaScript regular expressions.
27
+
28
+ ```bash
29
+ pi install npm:@spences10/pi-coding-preferences
30
+ ```
31
+
32
+ This is opt-in: installing the package globally applies it to your Pi
33
+ sessions, but projects and downstream users do not inherit it unless
34
+ they install the package.
@@ -0,0 +1,17 @@
1
+ import { type ExtensionAPI, type ToolCallEvent } from '@earendil-works/pi-coding-agent';
2
+ export type PreferenceRuleConfig = {
3
+ name: string;
4
+ toolNames?: string[];
5
+ pattern: string;
6
+ target?: 'command' | 'path' | 'input';
7
+ reason: string;
8
+ };
9
+ export type CodingPreferencesConfig = {
10
+ rules: PreferenceRuleConfig[];
11
+ };
12
+ export declare const default_config: CodingPreferencesConfig;
13
+ export declare function get_global_config_path(): string;
14
+ export declare function get_project_config_path(cwd?: string): string;
15
+ export declare function load_config(cwd?: string): CodingPreferencesConfig;
16
+ export declare function should_block_coding_preference(event: ToolCallEvent, config?: CodingPreferencesConfig): string | undefined;
17
+ export default function coding_preferences(pi: ExtensionAPI): void;
package/dist/index.js ADDED
@@ -0,0 +1,112 @@
1
+ // Config-driven coding workflow preferences for Pi agents.
2
+ import { getAgentDir, } from '@earendil-works/pi-coding-agent';
3
+ import { existsSync, readFileSync } from 'node:fs';
4
+ import { join, resolve } from 'node:path';
5
+ export const default_config = {
6
+ rules: [
7
+ {
8
+ name: 'no-secret-file-reads',
9
+ toolNames: ['read', 'bash'],
10
+ target: 'input',
11
+ pattern: String.raw `(^|/)\.env(?:\.[^/\s"']*)?$|\.tfvars(?:\.json)?$`,
12
+ reason: 'Blocked by coding preferences: do not read secret files into model context. Use nopeek to list or load only the required key names without exposing secret values.',
13
+ },
14
+ {
15
+ name: 'prefer-read-tool',
16
+ toolNames: ['bash'],
17
+ target: 'command',
18
+ pattern: String.raw `^\s*(?:cat|sed\s+(?:-n\s+)?["']?\d+(?:,\d+)?p["']?)\s+[^|;&>]+$`,
19
+ reason: 'Blocked by coding preferences: use the read tool for file inspection instead of cat or sed. Do not investigate this guardrail; retry with read.',
20
+ },
21
+ {
22
+ name: 'prefer-rg',
23
+ toolNames: ['bash'],
24
+ target: 'command',
25
+ pattern: '^\\s*grep\\b',
26
+ reason: 'Blocked by coding preferences: use rg for code/text search instead of grep. Do not investigate this guardrail; retry with rg.',
27
+ },
28
+ {
29
+ name: 'no-ad-hoc-todos',
30
+ toolNames: ['write', 'edit', 'bash'],
31
+ target: 'input',
32
+ pattern: '(^|/)(?:TODO|TODOS|todo|todos|tasks|TASKS)\\.md$',
33
+ reason: "Blocked by coding preferences: do not create ad-hoc TODO markdown files. Use Pi team/tasks or the project's existing issue tracker instead.",
34
+ },
35
+ ],
36
+ };
37
+ function input_record(event) {
38
+ return event.input;
39
+ }
40
+ function input_strings(value) {
41
+ if (typeof value === 'string')
42
+ return [value];
43
+ if (Array.isArray(value))
44
+ return value.flatMap(input_strings);
45
+ if (!value || typeof value !== 'object')
46
+ return [];
47
+ return Object.values(value).flatMap(input_strings);
48
+ }
49
+ function path_from(event) {
50
+ const input = input_record(event);
51
+ for (const key of ['path', 'file_path', 'filePath']) {
52
+ const value = input[key];
53
+ if (typeof value === 'string')
54
+ return value;
55
+ }
56
+ return undefined;
57
+ }
58
+ function command_from(event) {
59
+ const command = input_record(event).command;
60
+ return typeof command === 'string' ? command : undefined;
61
+ }
62
+ function target_values(event, target = 'input') {
63
+ if (target === 'command')
64
+ return command_from(event) ? [command_from(event)] : [];
65
+ if (target === 'path')
66
+ return path_from(event) ? [path_from(event)] : [];
67
+ return input_strings(event.input);
68
+ }
69
+ export function get_global_config_path() {
70
+ return join(getAgentDir(), 'coding-preferences.json');
71
+ }
72
+ export function get_project_config_path(cwd = process.cwd()) {
73
+ return join(resolve(cwd), '.pi', 'coding-preferences.json');
74
+ }
75
+ function read_config_file(path) {
76
+ if (!existsSync(path))
77
+ return undefined;
78
+ const parsed = JSON.parse(readFileSync(path, 'utf8'));
79
+ return { rules: parsed.rules ?? [] };
80
+ }
81
+ export function load_config(cwd = process.cwd()) {
82
+ const global_config = read_config_file(get_global_config_path());
83
+ const project_config = read_config_file(get_project_config_path(cwd));
84
+ if (!global_config && !project_config)
85
+ return default_config;
86
+ return {
87
+ rules: [
88
+ ...(global_config?.rules ?? []),
89
+ ...(project_config?.rules ?? []),
90
+ ],
91
+ };
92
+ }
93
+ export function should_block_coding_preference(event, config = load_config()) {
94
+ for (const rule of config.rules) {
95
+ if (rule.toolNames && !rule.toolNames.includes(event.toolName))
96
+ continue;
97
+ const pattern = new RegExp(rule.pattern);
98
+ if (target_values(event, rule.target).some((value) => pattern.test(value)))
99
+ return rule.reason;
100
+ }
101
+ return undefined;
102
+ }
103
+ export default function coding_preferences(pi) {
104
+ const config = load_config();
105
+ pi.on('tool_call', async (event) => {
106
+ const reason = should_block_coding_preference(event, config);
107
+ if (!reason)
108
+ return undefined;
109
+ return { block: true, reason };
110
+ });
111
+ }
112
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAE3D,OAAO,EACN,WAAW,GAIX,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAc1C,MAAM,CAAC,MAAM,cAAc,GAA4B;IACtD,KAAK,EAAE;QACN;YACC,IAAI,EAAE,sBAAsB;YAC5B,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;YAC3B,MAAM,EAAE,OAAO;YACf,OAAO,EAAE,MAAM,CAAC,GAAG,CAAA,kDAAkD;YACrE,MAAM,EACL,oKAAoK;SACrK;QACD;YACC,IAAI,EAAE,kBAAkB;YACxB,SAAS,EAAE,CAAC,MAAM,CAAC;YACnB,MAAM,EAAE,SAAS;YACjB,OAAO,EAAE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;IAChC,CAAC,CACD,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@spences10/pi-coding-preferences",
3
+ "version": "0.0.2",
4
+ "description": "Pi extension that nudges agents toward Scott's coding workflow preferences",
5
+ "keywords": [
6
+ "agent-guardrails",
7
+ "coding-preferences",
8
+ "pi",
9
+ "pi-package"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "Scott Spence <me@scottspence.com>",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/spences10/my-pi.git",
16
+ "directory": "packages/pi-coding-preferences"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "type": "module",
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "dependencies": {
29
+ "@earendil-works/pi-coding-agent": "^0.74.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^25.6.1",
33
+ "typescript": "^6.0.3",
34
+ "vitest": "^4.1.5"
35
+ },
36
+ "engines": {
37
+ "node": ">=24.15.0"
38
+ },
39
+ "pi": {
40
+ "extensions": [
41
+ "./dist/index.js"
42
+ ]
43
+ },
44
+ "scripts": {
45
+ "build": "pnpm --filter \"$npm_package_name^...\" run build:self && pnpm run build:self",
46
+ "build:self": "tsc -p tsconfig.build.json",
47
+ "check": "pnpm --filter \"$npm_package_name^...\" run build:self && pnpm run check:self",
48
+ "check:self": "tsc --noEmit -p tsconfig.json",
49
+ "test": "pnpm --filter \"$npm_package_name^...\" run build:self && pnpm run test:self",
50
+ "test:self": "vitest run",
51
+ "test:watch": "vitest"
52
+ }
53
+ }