@spences10/pi-harness 0.1.2 → 0.2.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.
- package/CHANGELOG.md +123 -0
- package/README.md +120 -175
- package/dist/assessment.d.ts +4 -12
- package/dist/assessment.js +19 -192
- package/dist/assessment.js.map +1 -1
- package/dist/enforcement/policy.d.ts +2 -0
- package/dist/enforcement/policy.js +47 -73
- package/dist/enforcement/policy.js.map +1 -1
- package/dist/extension.d.ts +1 -1
- package/dist/extension.js +184 -403
- package/dist/extension.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/prompt.d.ts +1 -1
- package/dist/prompt.js +1 -7
- package/dist/prompt.js.map +1 -1
- package/dist/runtime/baseline.d.ts +6 -0
- package/dist/runtime/baseline.js +145 -0
- package/dist/runtime/baseline.js.map +1 -0
- package/dist/runtime/index.d.ts +2 -1
- package/dist/runtime/index.js +72 -29
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/outcome.d.ts +1 -2
- package/dist/runtime/outcome.js +21 -35
- package/dist/runtime/outcome.js.map +1 -1
- package/dist/runtime/renderers.js +34 -70
- package/dist/runtime/renderers.js.map +1 -1
- package/dist/runtime/verification.d.ts +15 -0
- package/dist/runtime/verification.js +97 -0
- package/dist/runtime/verification.js.map +1 -0
- package/dist/schema.d.ts +12 -2
- package/dist/schema.js +6 -8
- package/dist/schema.js.map +1 -1
- package/package.json +6 -5
- package/skills/create-harness/SKILL.md +19 -62
- package/skills/execute-harness/SKILL.md +27 -66
- package/skills/review-harness/SKILL.md +21 -54
package/dist/assessment.js
CHANGED
|
@@ -3,25 +3,16 @@ import { harness_create_params_schema } from './schema.js';
|
|
|
3
3
|
export const HARNESS_ASSESSMENT_CUSTOM_TYPE = 'pi-harness-assessment-state';
|
|
4
4
|
export const harness_assess_params_schema = Type.Object({
|
|
5
5
|
task: Type.String({
|
|
6
|
-
|
|
6
|
+
minLength: 1,
|
|
7
|
+
description: 'Optional investigation before choosing an execution path',
|
|
7
8
|
}),
|
|
8
9
|
});
|
|
9
10
|
export const harness_assessment_submit_params_schema = Type.Object({
|
|
10
|
-
task: Type.String(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})),
|
|
16
|
-
existing_primitives: Type.Array(Type.String({
|
|
17
|
-
description: 'Existing mechanisms that already satisfy part or all of the need',
|
|
18
|
-
})),
|
|
19
|
-
rejected_options: Type.Array(Type.String({
|
|
20
|
-
description: 'Rejected option and the evidence-based reason for rejection',
|
|
21
|
-
})),
|
|
22
|
-
smallest_vertical_slice: Type.String({
|
|
23
|
-
description: 'Smallest useful outcome or experiment that can test the need',
|
|
24
|
-
}),
|
|
11
|
+
task: Type.String(),
|
|
12
|
+
evidence: Type.Array(Type.String()),
|
|
13
|
+
existing_primitives: Type.Array(Type.String()),
|
|
14
|
+
rejected_options: Type.Array(Type.String()),
|
|
15
|
+
smallest_vertical_slice: Type.String(),
|
|
25
16
|
recommendation: Type.Union([
|
|
26
17
|
Type.Literal('harness'),
|
|
27
18
|
Type.Literal('direct'),
|
|
@@ -29,204 +20,40 @@ export const harness_assessment_submit_params_schema = Type.Object({
|
|
|
29
20
|
]),
|
|
30
21
|
proposed_contract: Type.Optional(harness_create_params_schema),
|
|
31
22
|
});
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
'context_get',
|
|
35
|
-
'context_list',
|
|
36
|
-
'context_search',
|
|
37
|
-
'context_stats',
|
|
38
|
-
'find',
|
|
39
|
-
'grep',
|
|
40
|
-
'harness_assess',
|
|
41
|
-
'harness_assessment_submit',
|
|
42
|
-
'harness_read',
|
|
43
|
-
'ls',
|
|
44
|
-
'lsp_definition',
|
|
45
|
-
'lsp_diagnostics',
|
|
46
|
-
'lsp_diagnostics_many',
|
|
47
|
-
'lsp_document_symbols',
|
|
48
|
-
'lsp_find_symbol',
|
|
49
|
-
'lsp_hover',
|
|
50
|
-
'lsp_references',
|
|
51
|
-
'mcp__mcp-omnisearch__ai_search',
|
|
52
|
-
'mcp__mcp-omnisearch__web_extract',
|
|
53
|
-
'mcp__mcp-omnisearch__web_search',
|
|
54
|
-
'mcp__mcp-sqlite-tools__close_database',
|
|
55
|
-
'mcp__mcp-sqlite-tools__database_info',
|
|
56
|
-
'mcp__mcp-sqlite-tools__describe_table',
|
|
57
|
-
'mcp__mcp-sqlite-tools__execute_read_query',
|
|
58
|
-
'mcp__mcp-sqlite-tools__export_schema',
|
|
59
|
-
'mcp__mcp-sqlite-tools__list_databases',
|
|
60
|
-
'mcp__mcp-sqlite-tools__list_tables',
|
|
61
|
-
'mcp__mcp-sqlite-tools__open_database',
|
|
62
|
-
'read',
|
|
63
|
-
]);
|
|
64
|
-
const SAFE_COMMAND_PATTERNS = [
|
|
65
|
-
/^(?:pwd|whoami|uname|date|uptime)(?:\s+.*)?$/,
|
|
66
|
-
/^(?:cat|head|tail|less|more|grep|rg|ls|wc|sort|uniq|diff|file|stat|du|df|tree|which|whereis|type|bat|eza)(?:\s+.*)?$/,
|
|
67
|
-
/^find(?:\s+.*)?$/,
|
|
68
|
-
/^git\s+(?:status|log|diff|show|rev-parse|ls-files|ls-tree|grep)(?:\s+.*)?$/,
|
|
69
|
-
/^git\s+branch(?:\s+(?:--list|-a|--all|-r|--remotes|--show-current|--contains|--merged|--no-merged)(?:\s+.*)?)?$/,
|
|
70
|
-
/^git\s+remote(?:\s+(?:-v|show|get-url)(?:\s+.*)?)?$/,
|
|
71
|
-
/^git\s+config\s+--get(?:\s+.*)?$/,
|
|
72
|
-
/^gh\s+(?:issue\s+(?:list|view)|pr\s+(?:list|view|diff|checks)|repo\s+view)(?:\s+.*)?$/,
|
|
73
|
-
/^gh\s+api(?:\s+.*)?$/,
|
|
74
|
-
/^(?:npm|pnpm|yarn)\s+(?:list|ls|view|info|why|outdated)(?:\s+.*)?$/,
|
|
75
|
-
/^(?:node|python|python3)\s+--version$/,
|
|
76
|
-
/^jq(?:\s+.*)?$/,
|
|
77
|
-
/^sed\s+-n(?:\s+.*)?$/,
|
|
78
|
-
];
|
|
79
|
-
const UNSAFE_READ_COMMAND_PATTERNS = [
|
|
80
|
-
/\bfind\b.*\s-(?:delete|exec|execdir|ok|okdir|fprint|fprintf|fls)\b/i,
|
|
81
|
-
/\brg\b.*\s--pre(?:-glob)?\b/i,
|
|
82
|
-
/\b(?:git\s+(?:log|diff|show)|sort)\b.*(?:\s--output(?:=|\s+)|\s-o\s+)/i,
|
|
83
|
-
/\bsed\b.*\s-i(?:\s|$|[^a-z])/i,
|
|
84
|
-
/\bgh\s+api\b.*(?:\s-X\s*|\s--method(?:=|\s+))(?!GET\b)/i,
|
|
85
|
-
/\bgh\s+api\b.*(?:\s-f\s|\s-F\s|\s--field(?:=|\s+)|\s--raw-field(?:=|\s+))/i,
|
|
86
|
-
/\bgit\s+branch\b.*\s-[dDmM]\b/i,
|
|
87
|
-
];
|
|
88
|
-
export function create_assessment_state(task, source, tools_before_assessment, now = new Date()) {
|
|
89
|
-
const normalized_task = task.trim();
|
|
90
|
-
if (!normalized_task) {
|
|
23
|
+
export function create_assessment_state(task, source, tools_before_assessment = [], now = new Date()) {
|
|
24
|
+
if (!task.trim())
|
|
91
25
|
throw new Error('Assessment task is required');
|
|
92
|
-
}
|
|
93
26
|
return {
|
|
94
27
|
id: `assessment-${now.getTime().toString(36)}`,
|
|
95
28
|
version: 1,
|
|
96
29
|
status: 'assessing',
|
|
97
|
-
task:
|
|
30
|
+
task: task.trim(),
|
|
98
31
|
source,
|
|
99
32
|
started_at: now.toISOString(),
|
|
100
|
-
tools_before_assessment
|
|
33
|
+
tools_before_assessment,
|
|
101
34
|
};
|
|
102
35
|
}
|
|
103
36
|
export function submit_assessment_record(state, record) {
|
|
104
|
-
if (record.recommendation === 'harness') {
|
|
105
|
-
if (!record.proposed_contract) {
|
|
106
|
-
throw new Error('A harness recommendation requires a proposed contract');
|
|
107
|
-
}
|
|
108
|
-
if (!record.proposed_contract.allowed_paths?.length) {
|
|
109
|
-
throw new Error('An approved harness contract requires explicit allowed paths');
|
|
110
|
-
}
|
|
111
|
-
if (!record.proposed_contract.validation_commands?.length) {
|
|
112
|
-
throw new Error('An approved harness contract requires validation commands');
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
37
|
return {
|
|
116
38
|
...state,
|
|
117
39
|
version: state.version + 1,
|
|
118
|
-
status: '
|
|
40
|
+
status: 'assessed',
|
|
119
41
|
task: record.task.trim(),
|
|
120
42
|
record,
|
|
121
|
-
decision: undefined,
|
|
122
43
|
};
|
|
123
44
|
}
|
|
124
45
|
export function assessment_active(state) {
|
|
125
|
-
return
|
|
126
|
-
state?.status === 'awaiting_approval');
|
|
127
|
-
}
|
|
128
|
-
export function assessment_tool_names(active_tool_names) {
|
|
129
|
-
const read_tools = [...new Set(active_tool_names)].filter((name) => ASSESSMENT_TOOL_NAMES.has(name));
|
|
130
|
-
return [
|
|
131
|
-
...new Set([
|
|
132
|
-
...read_tools,
|
|
133
|
-
'harness_assess',
|
|
134
|
-
'harness_assessment_submit',
|
|
135
|
-
'harness_read',
|
|
136
|
-
]),
|
|
137
|
-
];
|
|
138
|
-
}
|
|
139
|
-
export function is_assessment_tool_allowed(tool_name) {
|
|
140
|
-
return ASSESSMENT_TOOL_NAMES.has(tool_name);
|
|
141
|
-
}
|
|
142
|
-
function has_unquoted_shell_control(command) {
|
|
143
|
-
let quote;
|
|
144
|
-
for (let index = 0; index < command.length; index++) {
|
|
145
|
-
const character = command[index];
|
|
146
|
-
const next = command[index + 1];
|
|
147
|
-
if (quote === 'single') {
|
|
148
|
-
if (character === "'")
|
|
149
|
-
quote = undefined;
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
if (quote === 'double') {
|
|
153
|
-
if (character === '\\') {
|
|
154
|
-
index++;
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (character === '"') {
|
|
158
|
-
quote = undefined;
|
|
159
|
-
continue;
|
|
160
|
-
}
|
|
161
|
-
if (character === '`' || (character === '$' && next === '(')) {
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
if (character === '\\') {
|
|
167
|
-
index++;
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
if (character === "'") {
|
|
171
|
-
quote = 'single';
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
if (character === '"') {
|
|
175
|
-
quote = 'double';
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
if (character === '\n' ||
|
|
179
|
-
character === '\r' ||
|
|
180
|
-
character === ';' ||
|
|
181
|
-
character === '&' ||
|
|
182
|
-
character === '|' ||
|
|
183
|
-
character === '<' ||
|
|
184
|
-
character === '>' ||
|
|
185
|
-
character === '`' ||
|
|
186
|
-
(character === '$' && next === '(')) {
|
|
187
|
-
return true;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return quote !== undefined;
|
|
191
|
-
}
|
|
192
|
-
export function check_assessment_command(command) {
|
|
193
|
-
const normalized = command.trim();
|
|
194
|
-
if (!normalized) {
|
|
195
|
-
return { ok: false, reason: 'Assessment mode: empty command' };
|
|
196
|
-
}
|
|
197
|
-
if (has_unquoted_shell_control(normalized) ||
|
|
198
|
-
UNSAFE_READ_COMMAND_PATTERNS.some((pattern) => pattern.test(normalized))) {
|
|
199
|
-
return {
|
|
200
|
-
ok: false,
|
|
201
|
-
reason: 'Assessment mode: shell composition or a mutating command option is not allowed',
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
if (!SAFE_COMMAND_PATTERNS.some((pattern) => pattern.test(normalized))) {
|
|
205
|
-
return {
|
|
206
|
-
ok: false,
|
|
207
|
-
reason: 'Assessment mode: command is not in the read-only allowlist; use one read command per call',
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
return { ok: true };
|
|
46
|
+
return state?.status === 'assessing';
|
|
211
47
|
}
|
|
212
48
|
export function assessment_context(state) {
|
|
213
|
-
|
|
214
|
-
? 'An assessment record is waiting for direct user approval. Do not start implementation or call harness_create.'
|
|
215
|
-
: 'Investigate with read-only tools, then call harness_assessment_submit with one evidence-based recommendation.';
|
|
216
|
-
return `## Active harness assessment\n\nAssessment: \`${state.id}\` v${state.version}\nTask: ${state.task}\n\nRepository mutation is disabled. Treat proposed capabilities as candidates, not tasks. Establish the current source of truth, find existing primitives, reject unsupported options, and define the smallest useful vertical slice. A harness contract is optional and is valid only when the evidence shows that an enforceable execution contract adds value. Bash accepts one allowlisted read command per call; use the dedicated read, find, grep, LSP, and research tools when available.\n\n${approval}`;
|
|
49
|
+
return `## Optional harness assessment\n\nTask: ${state.task}\n\nInvestigate the need, existing primitives, and smallest useful change before recommending an execution path. This is advisory planning, not a read-only security mode: tools remain available and normal user authorization still applies. Submit findings with harness_assessment_submit. Assessment is not required to create a harness.`;
|
|
217
50
|
}
|
|
218
51
|
export function format_assessment_record(state) {
|
|
219
52
|
const record = state.record;
|
|
220
|
-
if (!record)
|
|
221
|
-
return `Assessment ${state.id}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
: `- ${empty}`;
|
|
226
|
-
const contract = record.proposed_contract;
|
|
227
|
-
const contract_section = contract
|
|
228
|
-
? `\n\n## Proposed harness contract\n\n- Task: ${contract.task}\n- Cwd: ${contract.cwd ?? 'current project'}\n- Allowed paths: ${contract.allowed_paths?.join(', ') ?? 'none'}\n- Validation: ${contract.validation_commands?.join(' && ') ?? 'none'}\n- Test changes: ${contract.allow_test_changes ? 'allowed' : 'not allowed'}\n- Forbidden paths: ${contract.forbidden_paths?.join(', ') ?? 'defaults'}\n- Forbidden commands: ${contract.forbidden_commands?.join(', ') ?? 'defaults'}\n- Escalation rules: ${contract.escalation_rules?.join('; ') ?? 'defaults'}\n- Planner: ${contract.planner_model ?? 'default'} (${contract.planner_thinking ?? 'default'})\n- Executor: ${contract.executor_model ?? 'default'} (${contract.executor_thinking ?? 'default'})\n- Reviewer: ${contract.reviewer_model ?? 'default'} (${contract.reviewer_thinking ?? 'default'})`
|
|
229
|
-
: '';
|
|
230
|
-
return `# Harness assessment: ${state.id} v${state.version}\n\n- Status: ${state.status}\n- Recommendation: ${record.recommendation}\n\n## Outcome\n\n${record.task}\n\n## Evidence\n\n${list(record.evidence, 'No evidence recorded')}\n\n## Existing primitives\n\n${list(record.existing_primitives, 'None found')}\n\n## Rejected options\n\n${list(record.rejected_options, 'None recorded')}\n\n## Smallest vertical slice\n\n${record.smallest_vertical_slice}${contract_section}`;
|
|
53
|
+
if (!record)
|
|
54
|
+
return `Assessment ${state.id}: ${state.task}`;
|
|
55
|
+
const list = (values) => values.map((value) => `- ${value}`).join('\n') ||
|
|
56
|
+
'- None recorded';
|
|
57
|
+
return `# Harness assessment\n\n${record.task}\n\nRecommendation: ${record.recommendation}\n\n## Evidence\n\n${list(record.evidence)}\n\n## Existing primitives\n\n${list(record.existing_primitives)}\n\n## Rejected options\n\n${list(record.rejected_options)}\n\n## Smallest vertical slice\n\n${record.smallest_vertical_slice}`;
|
|
231
58
|
}
|
|
232
59
|
//# sourceMappingURL=assessment.js.map
|
package/dist/assessment.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assessment.js","sourceRoot":"","sources":["../src/assessment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAe,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAE3D,MAAM,CAAC,MAAM,8BAA8B,GAC1C,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"assessment.js","sourceRoot":"","sources":["../src/assessment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAe,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAE3D,MAAM,CAAC,MAAM,8BAA8B,GAC1C,6BAA6B,CAAC;AAC/B,MAAM,CAAC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string, pattern: string, path: string): boolean;
|
|
2
3
|
export declare function check_path_allowed(contract: HarnessContract, input_path: string): {
|
|
3
4
|
ok: true;
|
|
4
5
|
} | {
|
|
@@ -11,3 +12,4 @@ export declare function check_command_allowed(contract: HarnessContract, command
|
|
|
11
12
|
ok: false;
|
|
12
13
|
reason: string;
|
|
13
14
|
};
|
|
15
|
+
export declare function build_path_helpers(): string;
|
|
@@ -1,86 +1,60 @@
|
|
|
1
|
-
import { isAbsolute, relative, resolve } from 'node:path';
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const normalized_pattern = pattern.replaceAll('\\', '/');
|
|
7
|
-
if (!isAbsolute(pattern))
|
|
8
|
-
return normalized_pattern;
|
|
9
|
-
return relative(cwd, resolve(pattern)).replaceAll('\\', '/');
|
|
10
|
-
}
|
|
11
|
-
function pattern_matches(cwd, pattern, absolute_path) {
|
|
12
|
-
const normalized_pattern = pattern_to_relative(cwd, pattern);
|
|
13
|
-
const relative_path = relative(cwd, absolute_path).replaceAll('\\', '/');
|
|
14
|
-
if (normalized_pattern === '.')
|
|
1
|
+
import { isAbsolute, matchesGlob, relative, resolve, } from 'node:path';
|
|
2
|
+
export function pattern_matches(cwd, pattern, path) {
|
|
3
|
+
const normalized = (isAbsolute(pattern) ? relative(cwd, pattern) : pattern).replaceAll('\\', '/');
|
|
4
|
+
const candidate = relative(cwd, resolve(cwd, path)).replaceAll('\\', '/');
|
|
5
|
+
if (normalized === '.')
|
|
15
6
|
return true;
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
relative_path.startsWith(`${prefix}/`));
|
|
7
|
+
if (!/[*?[{]/.test(normalized)) {
|
|
8
|
+
return (candidate === normalized ||
|
|
9
|
+
candidate.startsWith(`${normalized}/`));
|
|
20
10
|
}
|
|
21
|
-
|
|
22
|
-
const regex = new RegExp(`^${normalized_pattern
|
|
23
|
-
.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
24
|
-
.replaceAll('**', '.*')
|
|
25
|
-
.replaceAll('*', '[^/]*')}$`);
|
|
26
|
-
return regex.test(relative_path);
|
|
27
|
-
}
|
|
28
|
-
const target = isAbsolute(pattern)
|
|
29
|
-
? resolve(pattern)
|
|
30
|
-
: resolve_project_path(cwd, pattern);
|
|
31
|
-
return (absolute_path === target || absolute_path.startsWith(`${target}/`));
|
|
32
|
-
}
|
|
33
|
-
function is_test_path(absolute_path) {
|
|
34
|
-
const path = absolute_path.replaceAll('\\', '/');
|
|
35
|
-
return (/(^|\/)(__tests__|tests?)\//.test(path) ||
|
|
36
|
-
/\.(test|spec)\.[cm]?[jt]sx?$/.test(path));
|
|
11
|
+
return matchesGlob(candidate, normalized);
|
|
37
12
|
}
|
|
38
13
|
export function check_path_allowed(contract, input_path) {
|
|
39
14
|
const cwd = resolve(contract.policy.cwd);
|
|
40
|
-
const absolute_path =
|
|
41
|
-
|
|
42
|
-
if (!
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
if (contract.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
if (!contract.scaffold.
|
|
55
|
-
|
|
56
|
-
|
|
15
|
+
const absolute_path = resolve(cwd, input_path);
|
|
16
|
+
let reason;
|
|
17
|
+
if (absolute_path !== cwd && !absolute_path.startsWith(`${cwd}/`)) {
|
|
18
|
+
reason = 'path is outside cwd';
|
|
19
|
+
}
|
|
20
|
+
else if (contract.policy.forbidden_paths.some((pattern) => pattern_matches(cwd, pattern, input_path))) {
|
|
21
|
+
reason = 'path is forbidden';
|
|
22
|
+
}
|
|
23
|
+
else if ((!contract.scaffold.allow_test_changes ||
|
|
24
|
+
contract.policy.allow_test_changes === false) &&
|
|
25
|
+
(/(^|\/)(__tests__|tests?)\//.test(input_path) ||
|
|
26
|
+
/\.(test|spec)\.[cm]?[jt]sx?$/.test(input_path))) {
|
|
27
|
+
reason = 'test changes are not allowed by harness.json';
|
|
28
|
+
}
|
|
29
|
+
else if (!contract.scaffold.allowed_paths.some((pattern) => pattern_matches(cwd, pattern, input_path)) ||
|
|
30
|
+
!(contract.policy.allowed_paths ?? contract.scaffold.allowed_paths).some((pattern) => pattern_matches(cwd, pattern, input_path))) {
|
|
31
|
+
reason = 'path is outside allowed_paths';
|
|
32
|
+
}
|
|
33
|
+
return reason
|
|
34
|
+
? {
|
|
57
35
|
ok: false,
|
|
58
|
-
reason: `Harness ${contract.id}:
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!contract.scaffold.allowed_paths.some((pattern) => pattern_matches(cwd, pattern, absolute_path))) {
|
|
62
|
-
return {
|
|
63
|
-
ok: false,
|
|
64
|
-
reason: `Harness ${contract.id}: path is outside allowed_paths: ${input_path}`,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
return { ok: true };
|
|
36
|
+
reason: `Harness ${contract.id}: ${reason}: ${input_path}`,
|
|
37
|
+
}
|
|
38
|
+
: { ok: true };
|
|
68
39
|
}
|
|
69
40
|
export function check_command_allowed(contract, command) {
|
|
70
41
|
const blocked = contract.policy.forbidden_commands.find((needle) => command.includes(needle));
|
|
71
|
-
|
|
72
|
-
|
|
42
|
+
return blocked
|
|
43
|
+
? {
|
|
73
44
|
ok: false,
|
|
74
45
|
reason: `Harness ${contract.id}: command contains forbidden pattern: ${blocked}`,
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
46
|
+
}
|
|
47
|
+
: { ok: true };
|
|
48
|
+
}
|
|
49
|
+
export function build_path_helpers() {
|
|
50
|
+
return String.raw `
|
|
51
|
+
function pattern_matches(pattern, path) {
|
|
52
|
+
const normalized = (isAbsolute(pattern) ? relative(cwd, pattern) : pattern).replaceAll('\\', '/');
|
|
53
|
+
const candidate = relative(cwd, resolve(cwd, path)).replaceAll('\\', '/');
|
|
54
|
+
if (normalized === '.') return true;
|
|
55
|
+
if (!/[*?[{]/.test(normalized)) return candidate === normalized || candidate.startsWith(normalized + '/');
|
|
56
|
+
return matchesGlob(candidate, normalized);
|
|
57
|
+
}
|
|
58
|
+
`;
|
|
85
59
|
}
|
|
86
60
|
//# sourceMappingURL=policy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../src/enforcement/policy.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../src/enforcement/policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,UAAU,EACV,WAAW,EACX,QAAQ,EACR,OAAO,GACP,MAAM,WAAW,CAAC;AAGnB,MAAM,UAAU,eAAe,CAC9B,GAAW,EACX,OAAe,EACf,IAAY;IAEZ,MAAM,UAAU,GAAG,CAClB,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CACtD,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACxB,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,UAAU,CAC7D,IAAI,EACJ,GAAG,CACH,CAAC;IACF,IAAI,UAAU,KAAK,GAAG;QAAE,OAAO,IAAI,CAAC;IACpC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAChC,OAAO,CACN,SAAS,KAAK,UAAU;YACxB,SAAS,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,CACtC,CAAC;IACH,CAAC;IACD,OAAO,WAAW,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,QAAyB,EACzB,UAA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AC;YACA,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,WAAW,QAAQ,CAAC,EAAE,KAAK,MAAM,KAAK,UAAU,EAAE;SAC1D;QACF,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,qBAAqB,CACpC,QAAyB,EACzB,OAAe;IAEf,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAClE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CACxB,CAAC;IACF,OAAO,OAAO;QACb,CAAC,CAAC;YACA,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,WAAW,QAAQ,CAAC,EAAE,yCAAyC,OAAO,EAAE;SAChF;QACF,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,kBAAkB;IACjC,OAAO,MAAM,CAAC,GAAG,CAAA;;;;;;;;CAQjB,CAAC;AACF,CAAC"}
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { BeforeAgentStartEvent, ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
2
|
export declare function should_inject_harness_prompt(event: Pick<BeforeAgentStartEvent, 'systemPromptOptions'>): boolean;
|
|
3
|
-
export default function harness(pi: ExtensionAPI):
|
|
3
|
+
export default function harness(pi: ExtensionAPI): void;
|