@criogaid/pi-hashline-edit 0.13.0 → 0.15.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.
- package/README.md +18 -12
- package/README.zh-CN.md +18 -12
- package/package.json +8 -1
- package/src/core/AGENTS.md +3 -1
- package/src/core/apply.ts +110 -117
- package/src/core/character-set.ts +2 -3
- package/src/core/copy-span.ts +15 -0
- package/src/core/errors.ts +24 -9
- package/src/core/lines.ts +71 -17
- package/src/core/ranges.ts +5 -4
- package/src/core/replace-schema.ts +3 -1
- package/src/core/replace.ts +50 -29
- package/src/core/search-analysis.ts +21 -22
- package/src/core/search.ts +15 -32
- package/src/core/types.ts +3 -0
- package/src/index.ts +47 -39
- package/src/pi/AGENTS.md +30 -19
- package/src/pi/action-fusion.ts +284 -211
- package/src/pi/argument-diagnostics.ts +50 -37
- package/src/pi/argument-error.ts +2 -2
- package/src/pi/argument-validation.ts +163 -66
- package/src/pi/budgets.ts +12 -0
- package/src/pi/diagnostic-buffer.ts +12 -11
- package/src/pi/edit-tool.ts +138 -115
- package/src/pi/error-text.ts +10 -2
- package/src/pi/failure-context.ts +24 -37
- package/src/pi/file-commit.ts +131 -164
- package/src/pi/file-read.ts +122 -31
- package/src/pi/fusion-arguments.ts +214 -0
- package/src/pi/fusion-card.ts +102 -60
- package/src/pi/grep-candidates.ts +35 -22
- package/src/pi/grep-output.ts +197 -190
- package/src/pi/grep-render.ts +22 -13
- package/src/pi/grep-reread.ts +7 -6
- package/src/pi/grep-scope.ts +31 -16
- package/src/pi/grep-search.ts +56 -47
- package/src/pi/grep-tool.ts +35 -33
- package/src/pi/grep-worker.ts +3 -4
- package/src/pi/mutation-diff.ts +238 -0
- package/src/pi/mutation-result.ts +102 -79
- package/src/pi/mutation-runner.ts +107 -84
- package/src/pi/native-bash.ts +41 -0
- package/src/pi/path.ts +59 -6
- package/src/pi/read-tool.ts +173 -138
- package/src/pi/regex-worker.ts +14 -8
- package/src/pi/render.ts +101 -54
- package/src/pi/replace-regex.ts +1 -1
- package/src/pi/replace-tool.ts +14 -7
- package/src/pi/report-cause.ts +34 -17
- package/src/pi/report-schema.ts +184 -85
- package/src/pi/report.ts +133 -64
- package/src/pi/rg-process.ts +68 -48
- package/src/pi/schema.ts +4 -0
- package/src/pi/text-stream.ts +81 -87
- package/src/pi/workspace-cwd.js +2 -0
- package/src/pi/workspace.ts +46 -0
- package/src/pi/write-tool.ts +19 -13
- package/src/core/apply.test.ts +0 -644
- package/src/core/case-fold.test.ts +0 -35
- package/src/core/character-set.test.ts +0 -97
- package/src/core/errors.test.ts +0 -39
- package/src/core/hash.test.ts +0 -64
- package/src/core/lines.test.ts +0 -63
- package/src/core/ranges.test.ts +0 -123
- package/src/core/replace.test.ts +0 -187
- package/src/core/search-analysis.test.ts +0 -210
- package/src/core/search-matrix.test.ts +0 -64
- package/src/core/search-matrix.testing.ts +0 -171
- package/src/core/search.test.ts +0 -132
- package/src/core/text.test.ts +0 -52
- package/src/integration/config-registration.test.ts +0 -291
- package/src/integration/fusion-dispatch.test.ts +0 -320
- package/src/integration/grep-errors.test.ts +0 -81
- package/src/integration/grep-flags.test.ts +0 -76
- package/src/integration/grep-js-completeness.test.ts +0 -278
- package/src/integration/grep-line-candidates.test.ts +0 -201
- package/src/integration/grep-matrix.test.ts +0 -118
- package/src/integration/grep-order.test.ts +0 -78
- package/src/integration/grep-path-matrix.test.ts +0 -128
- package/src/integration/grep-prefilter-coverage.test.ts +0 -204
- package/src/integration/grep-process-limits.test.ts +0 -122
- package/src/integration/grep-regex.test.ts +0 -412
- package/src/integration/grep-rg.test.ts +0 -1659
- package/src/integration/grep-scan-budget.test.ts +0 -111
- package/src/integration/grep-translation.test.ts +0 -199
- package/src/integration/large-text.test.ts +0 -70
- package/src/integration/model-budget-lifecycle.test.ts +0 -186
- package/src/integration/mutation-file-types.test.ts +0 -30
- package/src/integration/mutation-file-types.testing.ts +0 -33
- package/src/integration/npm-package.test.ts +0 -19
- package/src/integration/npm-package.testing.ts +0 -243
- package/src/integration/publication-read-changes.test.ts +0 -454
- package/src/integration/read-changes.test.ts +0 -98
- package/src/integration/replace-package.test.ts +0 -22
- package/src/integration/replace-package.testing.ts +0 -36
- package/src/integration/replace-worker-boundary.test.ts +0 -127
- package/src/integration/report-replay.test.ts +0 -91
- package/src/integration/utf8-errors.test.ts +0 -46
- package/src/pi/action-fusion.test.ts +0 -659
- package/src/pi/action-fusion.testing.ts +0 -38
- package/src/pi/anchor-format.test.ts +0 -124
- package/src/pi/argument-error.testing.ts +0 -46
- package/src/pi/argument-format.test.ts +0 -196
- package/src/pi/argument-validation.test.ts +0 -1007
- package/src/pi/codemode.test.ts +0 -101
- package/src/pi/config.test.ts +0 -204
- package/src/pi/diagnostic-buffer.test.ts +0 -115
- package/src/pi/directory-sync.test.ts +0 -57
- package/src/pi/execute.test.ts +0 -1257
- package/src/pi/extension-loading.test.ts +0 -14
- package/src/pi/extension-loading.testing.ts +0 -3
- package/src/pi/failure-context.test.ts +0 -370
- package/src/pi/failure-path.test.ts +0 -289
- package/src/pi/file-commit.test.ts +0 -628
- package/src/pi/file-read.test.ts +0 -29
- package/src/pi/file-snapshot.test.ts +0 -22
- package/src/pi/fusion-card-interaction.test.ts +0 -143
- package/src/pi/fusion-card.test.ts +0 -353
- package/src/pi/grep-candidates.test.ts +0 -26
- package/src/pi/grep-matrix.test.ts +0 -68
- package/src/pi/grep-order.test.ts +0 -447
- package/src/pi/grep-output.testing.ts +0 -24
- package/src/pi/grep-rg.testing.ts +0 -73
- package/src/pi/grep.test.ts +0 -1670
- package/src/pi/model-budgets.test.ts +0 -96
- package/src/pi/mutation-outcome.testing.ts +0 -20
- package/src/pi/mutation-result.test.ts +0 -96
- package/src/pi/output-budgets.test.ts +0 -221
- package/src/pi/pi.test.ts +0 -158
- package/src/pi/read-delegation.test.ts +0 -119
- package/src/pi/read.test.ts +0 -735
- package/src/pi/regex-worker.test.ts +0 -42
- package/src/pi/registration.test.ts +0 -549
- package/src/pi/render.test.ts +0 -841
- package/src/pi/replace.test.ts +0 -634
- package/src/pi/report-failure.testing.ts +0 -27
- package/src/pi/report.test.ts +0 -533
- package/src/pi/result-safety.test.ts +0 -597
- package/src/pi/rg-process.test.ts +0 -133
- package/src/pi/rg-regex.test.ts +0 -40
- package/src/pi/text-stream.test.ts +0 -281
- package/src/pi/tool-call.testing.ts +0 -61
- package/src/pi/tool-factories.testing.ts +0 -59
- package/src/pi/write.test.ts +0 -256
- package/src/testing/extension-loader.testing.ts +0 -24
- package/src/testing/installation.testing.ts +0 -77
- package/src/testing/process.testing.ts +0 -33
- package/src/testing/regex-host.testing.ts +0 -20
- package/src/testing/regex-translation-corpus.ts +0 -267
- package/src/testing/rg-line-listing.fixture.mjs +0 -7
- package/src/testing/run-tests.ts +0 -47
- package/src/testing/session.testing.ts +0 -127
|
@@ -7,8 +7,7 @@
|
|
|
7
7
|
* semantic checks, and `run`, and adds its own renderers.
|
|
8
8
|
*
|
|
9
9
|
* Owns the sequencing every mutation tool must keep identical once Pi has
|
|
10
|
-
*
|
|
11
|
-
* the shared file mutation queue, the hand-off to Action Fusion, and
|
|
10
|
+
* checked the arguments: the shared file mutation queue, the hand-off to Action Fusion, and
|
|
12
11
|
* finalization. Each tool returns a typed `MutationOutcome` (commit facts,
|
|
13
12
|
* details, outcome facts); Action Fusion returns facts too, and this module's
|
|
14
13
|
* single `finalizeMutation` call turns them into the result, so nothing
|
|
@@ -26,42 +25,49 @@
|
|
|
26
25
|
import { withFileMutationQueue, type ExtensionToolContext } from "@earendil-works/pi-coding-agent";
|
|
27
26
|
import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
|
|
28
27
|
import type { Static, TObject, TProperties } from "typebox";
|
|
28
|
+
import { ACTION_FUSION_GUIDELINES } from "./action-fusion.ts";
|
|
29
|
+
import type { CheckArguments } from "./argument-validation.ts";
|
|
30
|
+
import { createMutationArgumentBoundary } from "./fusion-arguments.ts";
|
|
29
31
|
import {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
type
|
|
33
|
-
type
|
|
34
|
-
|
|
35
|
-
} from "./action-fusion.ts";
|
|
36
|
-
import { createArgumentPreparer, type CheckArguments } from "./argument-validation.ts";
|
|
37
|
-
import type { MutationToolName } from "./report-schema.ts";
|
|
32
|
+
SCRIPT_OUTPUT_SCHEMAS,
|
|
33
|
+
type MutationFacts,
|
|
34
|
+
type MutationToolName,
|
|
35
|
+
type PublicationStatus,
|
|
36
|
+
} from "./report-schema.ts";
|
|
38
37
|
import {
|
|
39
38
|
commitFile,
|
|
40
|
-
commitFreshness,
|
|
41
39
|
FileMutationError,
|
|
42
|
-
|
|
40
|
+
type CommitIntent,
|
|
41
|
+
type CommitResult,
|
|
43
42
|
} from "./file-commit.ts";
|
|
43
|
+
import { readEditableSnapshot } from "./file-read.ts";
|
|
44
44
|
import {
|
|
45
45
|
describeMutationFailure,
|
|
46
46
|
finalizeMutation,
|
|
47
47
|
generateMutationDetails,
|
|
48
|
+
type MutationCommandFacts,
|
|
48
49
|
type MutationOutcome,
|
|
50
|
+
type MutationScheduler,
|
|
49
51
|
type MutationFinalDetails,
|
|
50
52
|
type MutationResultDetails,
|
|
51
|
-
type MutationFacts,
|
|
52
53
|
} from "./mutation-result.ts";
|
|
53
|
-
import {
|
|
54
|
-
import { throwIfCancelled } from "./error-text.ts";
|
|
54
|
+
import { callSignal, throwIfCancelled } from "./error-text.ts";
|
|
55
55
|
import { runTool } from "./report.ts";
|
|
56
56
|
import type { OutputBudgets } from "./budgets.ts";
|
|
57
57
|
import type { ToolEnvironment } from "./config.ts";
|
|
58
58
|
import { STRICT_SAMPLING } from "./schema.ts";
|
|
59
|
+
import type { CopiedSpan } from "../core/copy-span.ts";
|
|
59
60
|
|
|
60
|
-
|
|
61
|
+
/** With Action Fusion off, each call runs alone under Pi's file mutation queue. */
|
|
62
|
+
export const runAlone: MutationScheduler = async ({ mutate }) => ({
|
|
63
|
+
kind: "alone",
|
|
64
|
+
outcome: await mutate(),
|
|
65
|
+
});
|
|
61
66
|
|
|
62
|
-
/** A mutation tool's registration environment
|
|
67
|
+
/** A mutation tool's registration environment. */
|
|
63
68
|
export interface MutationToolEnvironment extends ToolEnvironment {
|
|
64
|
-
|
|
69
|
+
/** `runAlone`, or Action Fusion's scheduler when the actionFusion setting is on. */
|
|
70
|
+
readonly schedule: MutationScheduler;
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
/** Result details shared by the read-modify-write tools. */
|
|
@@ -70,15 +76,13 @@ type TextMutationDetails = ReturnType<typeof generateMutationDetails>;
|
|
|
70
76
|
/** The file a mutation targets, resolved once per call. */
|
|
71
77
|
export interface MutationTarget {
|
|
72
78
|
readonly absolutePath: string;
|
|
73
|
-
|
|
79
|
+
publish(text: string, intent: CommitIntent): Promise<CommitResult>;
|
|
80
|
+
readonly signal: AbortSignal;
|
|
74
81
|
readonly budget: OutputBudgets;
|
|
75
82
|
}
|
|
76
83
|
|
|
77
|
-
/** A mutation tool's own parameters; `execute` separates then_run. */
|
|
78
|
-
type MutationParams<P extends TProperties> = Static<TObject<P>> & { readonly path: string };
|
|
79
|
-
|
|
80
84
|
/** What a mutation tool supplies to the shared definition; renderers stay with the tool. */
|
|
81
|
-
interface MutationToolSpec<P extends TProperties, TDetails, TTool extends MutationToolName>
|
|
85
|
+
interface MutationToolSpec<P extends TProperties, TDetails, TTool extends MutationToolName, Parsed>
|
|
82
86
|
extends MutationToolEnvironment {
|
|
83
87
|
readonly tool: TTool;
|
|
84
88
|
/** Parameters without then_run, which Action Fusion adds. */
|
|
@@ -86,29 +90,38 @@ interface MutationToolSpec<P extends TProperties, TDetails, TTool extends Mutati
|
|
|
86
90
|
readonly description: string;
|
|
87
91
|
readonly promptSnippet: string;
|
|
88
92
|
readonly promptGuidelines?: readonly string[];
|
|
89
|
-
/** Semantic argument checks
|
|
90
|
-
readonly check
|
|
93
|
+
/** Semantic argument checks and any execution data they derive. */
|
|
94
|
+
readonly check: CheckArguments<Parsed>;
|
|
91
95
|
/** Perform the mutation. Runs inside the file mutation queue. */
|
|
92
96
|
run(
|
|
93
|
-
params:
|
|
97
|
+
params: Static<TObject<P>>,
|
|
94
98
|
target: MutationTarget,
|
|
95
|
-
|
|
99
|
+
parsed: NoInfer<Parsed>,
|
|
100
|
+
): Promise<MutationOutcome<TDetails, NoInfer<TTool>>>;
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
/**
|
|
99
104
|
* The definition every mutation tool shares. Action Fusion adds then_run to the
|
|
100
|
-
* schema and guidelines only when it is on
|
|
101
|
-
*
|
|
102
|
-
*
|
|
105
|
+
* schema and guidelines only when it is on. The argument boundary separates
|
|
106
|
+
* the file and follow-up verdicts. Execution queues and publishes the file
|
|
107
|
+
* operation before settling the follow-up.
|
|
103
108
|
*/
|
|
104
109
|
export function mutationToolBase<
|
|
105
110
|
P extends TProperties,
|
|
106
111
|
TDetails extends object,
|
|
107
112
|
TTool extends MutationToolName,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
const
|
|
113
|
+
Parsed,
|
|
114
|
+
>(spec: MutationToolSpec<P, TDetails, TTool, Parsed>) {
|
|
115
|
+
const { tool, cwd, schema, schedule, budgets } = spec;
|
|
116
|
+
const { actionFusion } = spec.config;
|
|
117
|
+
const boundary = createMutationArgumentBoundary(
|
|
118
|
+
schema,
|
|
119
|
+
tool,
|
|
120
|
+
actionFusion,
|
|
121
|
+
cwd,
|
|
122
|
+
budgets,
|
|
123
|
+
spec.check,
|
|
124
|
+
);
|
|
112
125
|
return {
|
|
113
126
|
name: tool,
|
|
114
127
|
label: tool,
|
|
@@ -118,48 +131,70 @@ export function mutationToolBase<
|
|
|
118
131
|
...(spec.promptGuidelines ?? []),
|
|
119
132
|
...(actionFusion ? ACTION_FUSION_GUIDELINES : []),
|
|
120
133
|
],
|
|
121
|
-
parameters,
|
|
122
|
-
|
|
134
|
+
parameters: boundary.parameters,
|
|
135
|
+
outputSchema: SCRIPT_OUTPUT_SCHEMAS[tool],
|
|
136
|
+
prepareArguments: boundary.prepareArguments,
|
|
123
137
|
constrainedSampling: STRICT_SAMPLING,
|
|
124
138
|
renderShell: "default" as const,
|
|
125
|
-
execute(
|
|
139
|
+
async execute(
|
|
126
140
|
toolCallId: string,
|
|
127
|
-
params:
|
|
128
|
-
|
|
129
|
-
|
|
141
|
+
params: Static<typeof boundary.parameters>,
|
|
142
|
+
piSignal: AbortSignal | undefined,
|
|
143
|
+
piOnUpdate: AgentToolUpdateCallback<MutationResultDetails<TDetails>> | undefined,
|
|
130
144
|
ctx: ExtensionToolContext,
|
|
131
145
|
): Promise<AgentToolResult<MutationFinalDetails<TDetails>>> {
|
|
132
|
-
const
|
|
133
|
-
|
|
146
|
+
const signal = callSignal(piSignal);
|
|
147
|
+
// Pi's agent loop always supplies a callback; a direct call without one has no display.
|
|
148
|
+
const onUpdate = piOnUpdate ?? (() => {});
|
|
149
|
+
const {
|
|
150
|
+
args: mutationParams,
|
|
151
|
+
parsed,
|
|
152
|
+
paths: [{ absolutePath, spelling: displayPath }],
|
|
153
|
+
thenRun,
|
|
154
|
+
} = boundary.afterHooks(params);
|
|
134
155
|
const budget = budgets();
|
|
135
|
-
|
|
156
|
+
let commit: CommitResult | undefined;
|
|
157
|
+
let failedPublication: PublicationStatus | undefined;
|
|
158
|
+
let command: MutationCommandFacts = {};
|
|
159
|
+
const result = await runTool(
|
|
136
160
|
tool,
|
|
137
161
|
{ path: displayPath, signal, budget },
|
|
138
162
|
async () => {
|
|
139
|
-
const
|
|
140
|
-
|
|
163
|
+
const target: MutationTarget = {
|
|
164
|
+
absolutePath,
|
|
165
|
+
signal,
|
|
166
|
+
budget,
|
|
167
|
+
async publish(text, intent) {
|
|
168
|
+
try {
|
|
169
|
+
commit = await commitFile(absolutePath, text, intent, signal);
|
|
170
|
+
return commit;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error instanceof FileMutationError) failedPublication = error.publication;
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
};
|
|
141
177
|
const mutate = (): Promise<MutationOutcome<TDetails, TTool>> =>
|
|
142
|
-
withFileMutationQueue(absolutePath, () => spec.run(mutationParams, target));
|
|
143
|
-
const settled
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const { outcome, observation, command, terminate } = settled;
|
|
158
|
-
const result = finalizeMutation(tool, displayPath, outcome, observation, command, budget);
|
|
159
|
-
return terminate === undefined ? result : { ...result, terminate };
|
|
178
|
+
withFileMutationQueue(absolutePath, () => spec.run(mutationParams, target, parsed));
|
|
179
|
+
const settled = await schedule({
|
|
180
|
+
toolCallId,
|
|
181
|
+
absolutePath,
|
|
182
|
+
thenRun,
|
|
183
|
+
mutate,
|
|
184
|
+
signal,
|
|
185
|
+
ctx,
|
|
186
|
+
onUpdate,
|
|
187
|
+
recordCommand(facts) {
|
|
188
|
+
// Each fact is final when first observed, including the first display error.
|
|
189
|
+
command = { ...facts, ...command };
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
return finalizeMutation(displayPath, settled, command.progressFailure, budget);
|
|
160
193
|
},
|
|
161
|
-
|
|
194
|
+
(tool, error, context) =>
|
|
195
|
+
describeMutationFailure(tool, error, { ...context, commit, failedPublication, command }),
|
|
162
196
|
);
|
|
197
|
+
return command.terminate === undefined ? result : { ...result, terminate: command.terminate };
|
|
163
198
|
},
|
|
164
199
|
};
|
|
165
200
|
}
|
|
@@ -168,6 +203,8 @@ export function mutationToolBase<
|
|
|
168
203
|
interface TextChange<TTool extends MutationToolName> {
|
|
169
204
|
/** Complete replacement text; equal to the snapshot for a no-op. */
|
|
170
205
|
readonly text: string;
|
|
206
|
+
/** Identical ranges copied from the snapshot, used to delimit the diff. */
|
|
207
|
+
readonly copied: readonly CopiedSpan[];
|
|
171
208
|
/** The tool's own outcome facts, such as replace's match count. */
|
|
172
209
|
readonly facts: MutationFacts<TTool>;
|
|
173
210
|
}
|
|
@@ -191,24 +228,10 @@ export async function runTextMutation<TTool extends MutationToolName>(
|
|
|
191
228
|
// Cancelled before write: don't touch the disk.
|
|
192
229
|
throwIfCancelled(signal);
|
|
193
230
|
|
|
194
|
-
const commit = await
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return {
|
|
201
|
-
commit,
|
|
202
|
-
details: generateMutationDetails(currentText, next.text),
|
|
203
|
-
facts: next.facts,
|
|
204
|
-
};
|
|
205
|
-
} catch (error) {
|
|
206
|
-
// Keep result-building failures distinct from file publication failures.
|
|
207
|
-
throw new FileMutationError(
|
|
208
|
-
"POST_PROCESS_FAILED",
|
|
209
|
-
commit.publication,
|
|
210
|
-
"Result generation failed.",
|
|
211
|
-
{ cause: error },
|
|
212
|
-
);
|
|
213
|
-
}
|
|
231
|
+
const commit = await target.publish(next.text, { mode: "update", baseRevision });
|
|
232
|
+
return {
|
|
233
|
+
commit,
|
|
234
|
+
details: generateMutationDetails(currentText, next.text, next.copied),
|
|
235
|
+
facts: next.facts,
|
|
236
|
+
};
|
|
214
237
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Pi's Bash definition supplies the output schema and command-card renderers. */
|
|
2
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
3
|
+
import type { JsonValue } from "@earendil-works/pi-ai";
|
|
4
|
+
import { createBashToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { TObject } from "typebox";
|
|
6
|
+
import { Value } from "typebox/value";
|
|
7
|
+
|
|
8
|
+
export const nativeBash = createBashToolDefinition(process.cwd());
|
|
9
|
+
// ToolDefinition erases the type of outputSchema. Built-in Bash declares an object schema.
|
|
10
|
+
export const BASH_OUTPUT_SCHEMA = nativeBash.outputSchema as TObject;
|
|
11
|
+
const outputKeys = Object.keys(BASH_OUTPUT_SCHEMA.properties);
|
|
12
|
+
|
|
13
|
+
/** A partial or unreadable hook result can still establish a numeric exit. */
|
|
14
|
+
export function snapshotBashOutput(
|
|
15
|
+
result: Pick<AgentToolResult<unknown>, "structuredContent">,
|
|
16
|
+
recordObserverFailure: (error: unknown) => void,
|
|
17
|
+
) {
|
|
18
|
+
let exited = false;
|
|
19
|
+
try {
|
|
20
|
+
const value = result.structuredContent;
|
|
21
|
+
if (
|
|
22
|
+
value === null ||
|
|
23
|
+
typeof value !== "object" ||
|
|
24
|
+
!("exit_code" in value) ||
|
|
25
|
+
Array.isArray(value)
|
|
26
|
+
)
|
|
27
|
+
return { exited };
|
|
28
|
+
const exitCode = value.exit_code;
|
|
29
|
+
if (!Value.Check(BASH_OUTPUT_SCHEMA.properties.exit_code, exitCode)) return { exited };
|
|
30
|
+
exited = true;
|
|
31
|
+
const fields: Record<string, JsonValue> = {};
|
|
32
|
+
for (const key of outputKeys) {
|
|
33
|
+
const field = key === "exit_code" ? exitCode : value[key];
|
|
34
|
+
if (field !== undefined) fields[key] = field;
|
|
35
|
+
}
|
|
36
|
+
return { exited, structured: Value.Check(BASH_OUTPUT_SCHEMA, fields) ? fields : undefined };
|
|
37
|
+
} catch (error) {
|
|
38
|
+
recordObserverFailure(error);
|
|
39
|
+
return { exited };
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/pi/path.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Path canonicalization and lossless re-serialization for cross-platform tool
|
|
3
|
-
* input.
|
|
3
|
+
* input, and the form a path takes in a card's call line.
|
|
4
4
|
*
|
|
5
5
|
* @module pi-hashline-edit/pi
|
|
6
6
|
*/
|
|
@@ -14,10 +14,15 @@ const WINDOWS_SHELL_DRIVE = /^\/(?:mnt\/|cygdrive\/)?([a-z])(?:\/(.*))?$/is;
|
|
|
14
14
|
const LOCALHOST_UNC_PATH = /^\\\\(localhost)\\/i;
|
|
15
15
|
const LOCALHOST_UNC_URL = /^file:\/\/\/\/localhost\//i;
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/** Pi's file tools accept a path with a leading `@`, as the model mentions a file. */
|
|
18
|
+
const withoutMention = (path: string) => (path.startsWith("@") ? path.slice(1) : path);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The local path a model-supplied spelling names, with the same input conventions as Pi's built-in
|
|
22
|
+
* file tools; relative paths stay relative. Throws Node's error for a file URL with no local path.
|
|
23
|
+
*/
|
|
24
|
+
function localPath(path: string): string {
|
|
25
|
+
let normalized = withoutMention(path.replace(UNICODE_SPACES, " "));
|
|
21
26
|
if (
|
|
22
27
|
process.platform === "win32" &&
|
|
23
28
|
normalized.startsWith("/") &&
|
|
@@ -45,7 +50,37 @@ export function canonicalPath(cwd: string, path: string): string {
|
|
|
45
50
|
)
|
|
46
51
|
: fileURLToPath(normalized);
|
|
47
52
|
}
|
|
48
|
-
return
|
|
53
|
+
return normalized;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Resolve model-supplied paths with the same input conventions as Pi's built-in file tools. */
|
|
57
|
+
export function canonicalPath(cwd: string, path: string): string {
|
|
58
|
+
return resolve(cwd, localPath(path));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ResolvedArgumentPath {
|
|
62
|
+
readonly absolutePath: string;
|
|
63
|
+
readonly spelling: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Parse a local path argument, rejecting NUL before and after decoding or normalization. */
|
|
67
|
+
export function parseArgumentPath(path: string): string {
|
|
68
|
+
const nulMessage = "Path contains a NUL character.";
|
|
69
|
+
if (path.includes("\0")) throw new Error(nulMessage);
|
|
70
|
+
const local = localPath(path);
|
|
71
|
+
if (local.includes("\0")) throw new Error(nulMessage);
|
|
72
|
+
return local;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether a path that names no file reads as a caller glob: `*` or `?` in the path itself. A file
|
|
77
|
+
* URL and the Windows namespace prefix are path syntax, not glob characters.
|
|
78
|
+
*/
|
|
79
|
+
export function isGlobSpelling(path: string): boolean {
|
|
80
|
+
const spelling = withoutMention(path);
|
|
81
|
+
if (spelling.startsWith("file://")) return false;
|
|
82
|
+
const name = process.platform === "win32" ? spelling.replace(/^[\\/]{2}\?[\\/]/, "") : spelling;
|
|
83
|
+
return /[*?]/.test(name);
|
|
49
84
|
}
|
|
50
85
|
|
|
51
86
|
/** Return a readable path that resolves back to the same file through the tools' input syntax. */
|
|
@@ -65,3 +100,21 @@ export function serializePath(cwd: string, path: string): string {
|
|
|
65
100
|
const localhost = LOCALHOST_UNC_PATH.exec(absolute)?.[1];
|
|
66
101
|
return localhost === undefined ? url.href : `file:////${localhost}${url.pathname}`;
|
|
67
102
|
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A path inside the home directory with `~` in place of that directory, the form Pi's built-in
|
|
106
|
+
* cards display; a relative path or a path elsewhere is returned unchanged.
|
|
107
|
+
*/
|
|
108
|
+
export function shortenHomePath(path: string): string {
|
|
109
|
+
const home = homedir();
|
|
110
|
+
if (!home || !isAbsolute(path)) return path;
|
|
111
|
+
const rel = relative(home, path);
|
|
112
|
+
if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return path;
|
|
113
|
+
return rel ? `~${sep}${rel}` : "~";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** serializePath's absolute form, for a report that states where a path led. */
|
|
117
|
+
export function serializeAbsolutePath(absolute: string): string {
|
|
118
|
+
// No path lies inside itself, so serializePath keeps this one absolute.
|
|
119
|
+
return serializePath(absolute, absolute);
|
|
120
|
+
}
|