@ftrdotdev/pi-tui 0.2.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/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +80 -0
- package/src/autocomplete-contract.test.ts +126 -0
- package/src/autocomplete-contract.ts +91 -0
- package/src/commands/git-execution.test.ts +69 -0
- package/src/commands/git-execution.ts +198 -0
- package/src/commands/git-status.test.ts +379 -0
- package/src/commands/git-status.ts +320 -0
- package/src/components/bordered-box.test.ts +136 -0
- package/src/components/bordered-box.ts +106 -0
- package/src/components/define-component.ts +33 -0
- package/src/components/index.ts +35 -0
- package/src/config.test.ts +469 -0
- package/src/config.ts +307 -0
- package/src/frame/box-frame.test.ts +44 -0
- package/src/frame/box-frame.ts +160 -0
- package/src/icons.ts +203 -0
- package/src/index.ts +359 -0
- package/src/runtime.ts +256 -0
- package/src/session-lifecycle.ts +41 -0
- package/src/state.test.ts +7 -0
- package/src/state.ts +90 -0
- package/src/telemetry.ts +301 -0
- package/src/ui/editor-tint.test.ts +70 -0
- package/src/ui/editor-tint.ts +78 -0
- package/src/ui/editor.ts +403 -0
- package/src/ui/footer.test.ts +350 -0
- package/src/ui/footer.ts +225 -0
- package/src/ui/header.test.ts +260 -0
- package/src/ui/header.ts +165 -0
- package/src/ui/preview.test.ts +639 -0
- package/src/ui/preview.ts +374 -0
- package/src/ui/render-helpers.ts +275 -0
- package/src/ui/settings-command.test.ts +218 -0
- package/src/ui/settings-command.ts +390 -0
- package/src/utils.ts +448 -0
- package/src/vim/editor-close-autocomplete.ts +26 -0
- package/src/vim/editor-host.test.ts +340 -0
- package/src/vim/editor-state.test.ts +385 -0
- package/src/vim/editor-state.ts +314 -0
- package/src/vim/mode-indicator.test.ts +27 -0
- package/src/vim/mode-indicator.ts +44 -0
- package/src/vim/selection-render.test.ts +77 -0
- package/src/vim/selection-render.ts +85 -0
- package/src/vim/text-model.test.ts +85 -0
- package/src/vim/text-model.ts +75 -0
- package/src/vim/vim-router.test.ts +50 -0
- package/src/vim/vim-router.ts +55 -0
- package/src/vim/word-motion.ts +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Felix Nordén
|
|
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,63 @@
|
|
|
1
|
+
# pi-tui
|
|
2
|
+
|
|
3
|
+
House TUI components and the pi interface extension, built on the
|
|
4
|
+
[pi coding agent](https://pi.dev) and Effect.
|
|
5
|
+
|
|
6
|
+
Package: `@ftrdotdev/pi-tui` · [MIT](LICENSE)
|
|
7
|
+
|
|
8
|
+
## What it is
|
|
9
|
+
|
|
10
|
+
`pi-tui` has two sides:
|
|
11
|
+
|
|
12
|
+
1. **Components** — composable, theme-aware UI pieces that other `@ftrdotdev`
|
|
13
|
+
packages and pi extensions import as code: `makeBorderedBox` for themed
|
|
14
|
+
boxes, `defineComponent` for closure-based components, and a pure
|
|
15
|
+
box-frame model that does string/geometry arithmetic without pi or effect
|
|
16
|
+
imports (available at `@ftrdotdev/pi-tui/box-frame`).
|
|
17
|
+
|
|
18
|
+
2. **The interface extension** — a pi extension that installs the session
|
|
19
|
+
chrome: header, footer (git status, runtime info, thinking level, usage,
|
|
20
|
+
cost), editor autocomplete, the `/settings` command, preview, and turn
|
|
21
|
+
telemetry. It registers through the `pi` field in `package.json` and loads
|
|
22
|
+
`src/index.ts`; it is deliberately not re-exported from the component
|
|
23
|
+
barrel, so importing a component never drags in the extension's side
|
|
24
|
+
effects.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Install the extension from npm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pi install npm:@ftrdotdev/pi-tui
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
To use only the components without the extension, depend on it from your own
|
|
35
|
+
package:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bun add @ftrdotdev/pi-tui
|
|
39
|
+
npm i @ftrdotdev/pi-tui
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { makeBorderedBox, type BorderedBoxOptions } from "@ftrdotdev/pi-tui";
|
|
44
|
+
import { buildBoxFrame, type BoxFrameOptions } from "@ftrdotdev/pi-tui/box-frame";
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Component model
|
|
48
|
+
|
|
49
|
+
Components handed to pi (`setWidget`, `ui.custom`, `setFooter`,
|
|
50
|
+
`setHeader`, `setEditorComponent`, entry renderers) must be plain objects
|
|
51
|
+
built from closures: pi-tui invokes `render`/`invalidate` as methods of the
|
|
52
|
+
wrapper object it was given, so an unbound class method would lose `this`.
|
|
53
|
+
`makeBorderedBox` and `defineComponent` build closure components that carry
|
|
54
|
+
no `this` and survive any hand-off.
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
| Command | Purpose |
|
|
59
|
+
| ------------------- | -------------------------------------------- |
|
|
60
|
+
| `bun test` | Run the test suite (vitest + @effect/vitest) |
|
|
61
|
+
| `bun test:watch` | Run the test suite in watch mode |
|
|
62
|
+
| `bunx tsc --noEmit` | Type check |
|
|
63
|
+
| `bun lint` | Lint with oxlint |
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ftrdotdev/pi-tui",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "House TUI components and the pi interface extension: themed boxes, a pure box-frame model, and the header/footer/editor/settings/preview chrome",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"components",
|
|
7
|
+
"effect",
|
|
8
|
+
"extension",
|
|
9
|
+
"pi",
|
|
10
|
+
"pi-coding-agent",
|
|
11
|
+
"pi-package",
|
|
12
|
+
"terminal",
|
|
13
|
+
"tui"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/felixnorden/pi-mono#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/felixnorden/pi-mono/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Felix Nordén <felixnorden@gmail.com>",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/felixnorden/pi-mono.git",
|
|
24
|
+
"directory": "packages/tui"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./src/components/index.ts",
|
|
35
|
+
"default": "./src/components/index.ts"
|
|
36
|
+
},
|
|
37
|
+
"./box-frame": {
|
|
38
|
+
"types": "./src/frame/box-frame.ts",
|
|
39
|
+
"default": "./src/frame/box-frame.ts"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:watch": "vitest",
|
|
49
|
+
"lint": "oxlint && oxlint --type-aware -A all -D typescript/unbound-method",
|
|
50
|
+
"lint:fix": "oxlint --fix",
|
|
51
|
+
"fmt": "oxfmt",
|
|
52
|
+
"prepare": "bun effect-tsgo patch --typescript --oxlint",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@effect/platform-node": "4.0.0-rc.108",
|
|
57
|
+
"effect": "4.0.0-rc.108"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@effect/tsgo": "0.36.4",
|
|
61
|
+
"@effect/vitest": "4.0.0-rc.108",
|
|
62
|
+
"@types/bun": "latest",
|
|
63
|
+
"oxfmt": "^0.63.0",
|
|
64
|
+
"oxlint": "1.77.0",
|
|
65
|
+
"oxlint-tsgolint": "7.0.2001",
|
|
66
|
+
"typescript": "7.0.2",
|
|
67
|
+
"vitest": "^4.1.10"
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@earendil-works/pi-ai": "*",
|
|
71
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
72
|
+
"@earendil-works/pi-tui": "*",
|
|
73
|
+
"typebox": "*"
|
|
74
|
+
},
|
|
75
|
+
"pi": {
|
|
76
|
+
"extensions": [
|
|
77
|
+
"./src/index.ts"
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { SelectList } from "@earendil-works/pi-tui";
|
|
3
|
+
import {
|
|
4
|
+
decodeAutocompleteInternals,
|
|
5
|
+
inspectAutocompleteInternals,
|
|
6
|
+
} from "./autocomplete-contract.ts";
|
|
7
|
+
|
|
8
|
+
const editorWith = (internals: Record<string, unknown>) => internals as unknown;
|
|
9
|
+
|
|
10
|
+
it("decodes a valid editor with a renderable list and prefix", () => {
|
|
11
|
+
const view = decodeAutocompleteInternals(
|
|
12
|
+
editorWith({
|
|
13
|
+
autocompleteList: { render: (_width: number) => ["item"] },
|
|
14
|
+
autocompletePrefix: "@src/",
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
assert.strictEqual(view?.autocompleteList?.render(10)[0], "item");
|
|
18
|
+
assert.strictEqual(view?.autocompletePrefix, "@src/");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("decodes an editor with no autocomplete active", () => {
|
|
22
|
+
const view = decodeAutocompleteInternals(editorWith({ autocompleteList: null }));
|
|
23
|
+
assert.strictEqual(view?.autocompleteList, undefined);
|
|
24
|
+
assert.strictEqual(view?.autocompletePrefix, undefined);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("rejects a list whose render is not a function", () => {
|
|
28
|
+
const view = decodeAutocompleteInternals(
|
|
29
|
+
editorWith({
|
|
30
|
+
autocompleteList: { render: "not a function" },
|
|
31
|
+
autocompletePrefix: "@src/",
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
assert.strictEqual(view, undefined);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("rejects a non-object list", () => {
|
|
38
|
+
const view = decodeAutocompleteInternals(
|
|
39
|
+
editorWith({ autocompleteList: 42, autocompletePrefix: "@src/" }),
|
|
40
|
+
);
|
|
41
|
+
assert.strictEqual(view, undefined);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("rejects a non-string prefix", () => {
|
|
45
|
+
const view = decodeAutocompleteInternals(
|
|
46
|
+
editorWith({
|
|
47
|
+
autocompleteList: { render: () => ["item"] },
|
|
48
|
+
autocompletePrefix: 42,
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
assert.strictEqual(view, undefined);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("degrades to an empty view when internals were renamed", () => {
|
|
55
|
+
const view = decodeAutocompleteInternals(
|
|
56
|
+
editorWith({
|
|
57
|
+
suggestionList: { render: () => ["item"] },
|
|
58
|
+
suggestionPrefix: "@src/",
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
// Unknown keys are ignored and optional fields are absent: no list to frame,
|
|
62
|
+
// so the editor falls back to the unframed passthrough.
|
|
63
|
+
assert.strictEqual(view?.autocompleteList, undefined);
|
|
64
|
+
assert.strictEqual(view?.autocompletePrefix, undefined);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("inspect reports ok with the decoded view on a matching editor", () => {
|
|
68
|
+
const inspection = inspectAutocompleteInternals(
|
|
69
|
+
editorWith({ autocompleteList: { render: () => ["item"] } }),
|
|
70
|
+
);
|
|
71
|
+
assert.strictEqual(inspection.ok, true);
|
|
72
|
+
if (inspection.ok) assert.strictEqual(inspection.view.autocompleteList?.render(10)[0], "item");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("inspect reports a non-empty issue with the field path on a mismatch", () => {
|
|
76
|
+
const inspection = inspectAutocompleteInternals(
|
|
77
|
+
editorWith({
|
|
78
|
+
autocompleteList: { render: "not a function" },
|
|
79
|
+
autocompletePrefix: "@src/",
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
assert.strictEqual(inspection.ok, false);
|
|
83
|
+
if (!inspection.ok) {
|
|
84
|
+
assert.strictEqual(inspection.issue.length > 0, true);
|
|
85
|
+
assert.strictEqual(inspection.issue.includes("autocompleteList"), true);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const selectListTheme = {
|
|
90
|
+
selectedPrefix: (t: string) => t,
|
|
91
|
+
selectedText: (t: string) => t,
|
|
92
|
+
description: (t: string) => t,
|
|
93
|
+
scrollInfo: (t: string) => t,
|
|
94
|
+
noMatch: (t: string) => t,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
it("accepts a real SelectList instance (prototype render) and preserves identity", () => {
|
|
98
|
+
// Regression: pi's editor stores a SelectList class instance whose `render`
|
|
99
|
+
// is a prototype method. Field decode must not wrap it, or `this` binding
|
|
100
|
+
// breaks when the editor calls render on the decoded value.
|
|
101
|
+
const list = new SelectList([{ value: "a", label: "A" }], 5, selectListTheme);
|
|
102
|
+
const inspection = inspectAutocompleteInternals({
|
|
103
|
+
autocompleteList: list,
|
|
104
|
+
autocompletePrefix: "@",
|
|
105
|
+
});
|
|
106
|
+
assert.strictEqual(inspection.ok, true);
|
|
107
|
+
if (inspection.ok) {
|
|
108
|
+
assert.strictEqual(inspection.view.autocompleteList, list);
|
|
109
|
+
const lines = inspection.view.autocompleteList?.render(20) ?? [];
|
|
110
|
+
assert.strictEqual(lines.length > 0, true);
|
|
111
|
+
assert.strictEqual(
|
|
112
|
+
lines.some((line) => line.includes("A")),
|
|
113
|
+
true,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("decode accepts an editor holding a real SelectList", () => {
|
|
119
|
+
const list = new SelectList([{ value: "a", label: "A" }], 5, selectListTheme);
|
|
120
|
+
const view = decodeAutocompleteInternals({
|
|
121
|
+
autocompleteList: list,
|
|
122
|
+
autocompletePrefix: "@",
|
|
123
|
+
});
|
|
124
|
+
assert.strictEqual(view?.autocompleteList?.render(20).length, 1);
|
|
125
|
+
assert.strictEqual(view?.autocompletePrefix, "@");
|
|
126
|
+
});
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Result, Schema, SchemaIssue } from "effect";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runtime contract for the parts of pi's built-in editor we reach into.
|
|
5
|
+
*
|
|
6
|
+
* The suggestion dropdown is rendered inside the pi-tui `Editor` component,
|
|
7
|
+
* which keeps its autocomplete state in private fields (`autocompleteList`,
|
|
8
|
+
* `autocompletePrefix`). The extension API exposes no renderer hook for that
|
|
9
|
+
* dropdown, so this extension frames the suggestions itself (see editor.ts).
|
|
10
|
+
*
|
|
11
|
+
* Instead of an unchecked `(this as any).autocompleteList` cast, we validate
|
|
12
|
+
* the exact surface we depend on with an Effect Schema. If pi renames or
|
|
13
|
+
* reshapes these internals, decoding fails softly and the editor falls back
|
|
14
|
+
* to the unframed passthrough instead of crashing with a TypeError.
|
|
15
|
+
*
|
|
16
|
+
* The schema checks a duck-type contract, not pi's real (private) types:
|
|
17
|
+
* - `autocompleteList`, when present, must be render-able to lines.
|
|
18
|
+
* - `autocompletePrefix`, when present, must be a string.
|
|
19
|
+
* Both fields are optional: `autocompleteList` is absent while no
|
|
20
|
+
* suggestions are showing.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** The only surface we read off the built-in list: render lines at a width. */
|
|
24
|
+
export interface AutocompleteListLike {
|
|
25
|
+
render(width: number): string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Structural refinement: has a `render` property that is a function.
|
|
30
|
+
*
|
|
31
|
+
* Deliberately NOT a `Schema.Struct({ render: ... })`: field decode only sees
|
|
32
|
+
* own enumerable properties, and pi's list is a class instance whose `render`
|
|
33
|
+
* lives on the prototype. A Struct would both reject the instance and wrap it
|
|
34
|
+
* in a plain object, which would break `this` binding when calling `render`.
|
|
35
|
+
* `ObjectKeyword` accepts the raw instance and the refine keeps its identity.
|
|
36
|
+
*/
|
|
37
|
+
const Renderable = Schema.ObjectKeyword.pipe(
|
|
38
|
+
Schema.refine(
|
|
39
|
+
(input): input is AutocompleteListLike =>
|
|
40
|
+
typeof (input as { render?: unknown }).render === "function",
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
export const AutocompleteInternals = Schema.Struct({
|
|
45
|
+
autocompleteList: Schema.optional(Renderable),
|
|
46
|
+
autocompletePrefix: Schema.optional(Schema.String),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export type AutocompleteInternals = (typeof AutocompleteInternals)["Type"];
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Decode an editor instance against the contract.
|
|
53
|
+
* Returns `undefined` when the internals do not match (pi internals changed).
|
|
54
|
+
*/
|
|
55
|
+
export function decodeAutocompleteInternals(target: unknown): AutocompleteInternals | undefined {
|
|
56
|
+
const inspection = inspectAutocompleteInternals(target);
|
|
57
|
+
return inspection.ok ? inspection.view : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type AutocompleteInternalsInspection =
|
|
61
|
+
| { readonly ok: true; readonly view: AutocompleteInternals }
|
|
62
|
+
| { readonly ok: false; readonly issue: string };
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Decode an editor instance against the contract, keeping the failure
|
|
66
|
+
* detail for diagnostics. The rc.108 default formatter crashes on some
|
|
67
|
+
* filter issues, so we render a compact path summary ourselves.
|
|
68
|
+
*/
|
|
69
|
+
export function inspectAutocompleteInternals(target: unknown): AutocompleteInternalsInspection {
|
|
70
|
+
const result = Schema.decodeUnknownResult(AutocompleteInternals)(target);
|
|
71
|
+
if (Result.isSuccess(result)) return { ok: true, view: result.success };
|
|
72
|
+
return { ok: false, issue: describeIssue(result.failure.issue) };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function describeIssue(issue: SchemaIssue.Issue, depth = 0): string {
|
|
76
|
+
if (depth > 4) return "...";
|
|
77
|
+
switch (issue._tag) {
|
|
78
|
+
case "Pointer":
|
|
79
|
+
return `${issue.path.join(".")}: ${describeIssue(issue.issue, depth + 1)}`;
|
|
80
|
+
case "Composite":
|
|
81
|
+
return issue.issues.map((sub) => describeIssue(sub, depth + 1)).join("; ");
|
|
82
|
+
case "AnyOf":
|
|
83
|
+
return issue.issues.map((sub) => describeIssue(sub, depth + 1)).join(" | ");
|
|
84
|
+
case "Filter":
|
|
85
|
+
return `filter failed (${describeIssue(issue.issue, depth + 1)})`;
|
|
86
|
+
case "Encoding":
|
|
87
|
+
return `encoding failed (${describeIssue(issue.issue, depth + 1)})`;
|
|
88
|
+
default:
|
|
89
|
+
return issue._tag;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import { Duration, Effect } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
GIT_EXECUTION_CAP,
|
|
5
|
+
GitExecutionService,
|
|
6
|
+
GitSpawnError,
|
|
7
|
+
GitTimeoutError,
|
|
8
|
+
} from "./git-execution.ts";
|
|
9
|
+
|
|
10
|
+
it.effect("run returns stdout and exit code from a stub response", () =>
|
|
11
|
+
Effect.gen(function* () {
|
|
12
|
+
const svc = yield* GitExecutionService;
|
|
13
|
+
const r = yield* svc.run("git", ["status"], "/cwd");
|
|
14
|
+
assert.deepStrictEqual(r, { stdout: "## main\n", exitCode: 0 });
|
|
15
|
+
}).pipe(
|
|
16
|
+
Effect.provide(
|
|
17
|
+
GitExecutionService.layerStub([
|
|
18
|
+
{ command: "git", args: ["status"], stdout: "## main\n", exitCode: 0 },
|
|
19
|
+
]),
|
|
20
|
+
),
|
|
21
|
+
),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
it.effect("run fails with GitSpawnError for a command with no stub response", () =>
|
|
25
|
+
Effect.gen(function* () {
|
|
26
|
+
const svc = yield* GitExecutionService;
|
|
27
|
+
const tag = yield* svc.run("git", ["status"], "/cwd").pipe(
|
|
28
|
+
Effect.match({
|
|
29
|
+
onFailure: (err) => err._tag,
|
|
30
|
+
onSuccess: () => "unexpected-success",
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
assert.strictEqual(tag, "GitSpawnError");
|
|
34
|
+
}).pipe(Effect.provide(GitExecutionService.layerStub([]))),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
it.live("run fails with GitTimeoutError when the cap fires on a never-completing invocation", () =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const svc = yield* GitExecutionService;
|
|
40
|
+
const tag = yield* svc.run("git", ["hang"], "/cwd").pipe(
|
|
41
|
+
Effect.match({
|
|
42
|
+
onFailure: (err) => err._tag,
|
|
43
|
+
onSuccess: () => "unexpected-success",
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
assert.strictEqual(tag, "GitTimeoutError");
|
|
47
|
+
}).pipe(
|
|
48
|
+
Effect.provide(
|
|
49
|
+
GitExecutionService.layerStub(
|
|
50
|
+
[{ command: "git", args: ["hang"], neverCompletes: true }],
|
|
51
|
+
"10 millis",
|
|
52
|
+
),
|
|
53
|
+
),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
it("the default execution cap is the fixed 2-second constant", () => {
|
|
58
|
+
assert.strictEqual(Duration.toMillis(GIT_EXECUTION_CAP), 2000);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("GitSpawnError and GitTimeoutError carry the expected tags", () => {
|
|
62
|
+
const spawnErr = new GitSpawnError({ command: "git", message: "boom" });
|
|
63
|
+
assert.strictEqual(spawnErr._tag, "GitSpawnError");
|
|
64
|
+
assert.instanceOf(spawnErr, GitSpawnError);
|
|
65
|
+
|
|
66
|
+
const timeoutErr = new GitTimeoutError({ command: "git", durationMillis: 2000 });
|
|
67
|
+
assert.strictEqual(timeoutErr._tag, "GitTimeoutError");
|
|
68
|
+
assert.instanceOf(timeoutErr, GitTimeoutError);
|
|
69
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Context, Duration, Effect, Layer, Schema, Stream, PlatformError } from "effect";
|
|
2
|
+
import * as CP from "effect/unstable/process";
|
|
3
|
+
|
|
4
|
+
/** Fixed per-invocation time cap applied to every git command (2 seconds). */
|
|
5
|
+
export const GIT_EXECUTION_CAP: Duration.Duration = Duration.seconds(2);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The stdout and exit code of a single command invocation.
|
|
9
|
+
*
|
|
10
|
+
* A non-zero exit code is data, not an effect failure: only platform-level
|
|
11
|
+
* failures (missing executable, unusable working directory, cap firing) fail
|
|
12
|
+
* the effect, as {@link GitSpawnError} or {@link GitTimeoutError}. Interpreting
|
|
13
|
+
* the exit code is the caller's concern.
|
|
14
|
+
*/
|
|
15
|
+
export interface CommandResult {
|
|
16
|
+
readonly stdout: string;
|
|
17
|
+
readonly exitCode: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A git invocation failed at the platform level (spawn, working directory, ...). */
|
|
21
|
+
export class GitSpawnError extends Schema.TaggedError<GitSpawnError>()("GitSpawnError", {
|
|
22
|
+
command: Schema.String,
|
|
23
|
+
message: Schema.String,
|
|
24
|
+
}) {}
|
|
25
|
+
|
|
26
|
+
/** A git invocation exceeded the execution cap; the hung child is interrupted. */
|
|
27
|
+
export class GitTimeoutError extends Schema.TaggedError<GitTimeoutError>()("GitTimeoutError", {
|
|
28
|
+
command: Schema.String,
|
|
29
|
+
durationMillis: Schema.Finite,
|
|
30
|
+
}) {}
|
|
31
|
+
|
|
32
|
+
/** Error union of the execution service: platform failures and cap firing. */
|
|
33
|
+
export type GitExecutionError = GitSpawnError | GitTimeoutError;
|
|
34
|
+
|
|
35
|
+
/** Apply the cap to one invocation and map the timeout to a tagged error. */
|
|
36
|
+
const withCap = <E extends GitExecutionError>(
|
|
37
|
+
cap: Duration.Input,
|
|
38
|
+
command: string,
|
|
39
|
+
effect: Effect.Effect<CommandResult, E, never>,
|
|
40
|
+
): Effect.Effect<CommandResult, GitExecutionError, never> =>
|
|
41
|
+
effect.pipe(
|
|
42
|
+
Effect.timeout(cap),
|
|
43
|
+
Effect.catchTag("TimeoutError", () =>
|
|
44
|
+
Effect.fail(new GitTimeoutError({ command, durationMillis: Duration.toMillis(cap) })),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Executes one command in a working directory and returns its stdout and exit
|
|
50
|
+
* code. Every invocation is capped at {@link GIT_EXECUTION_CAP}; a timeout
|
|
51
|
+
* interrupts the child and fails with {@link GitTimeoutError}, and platform
|
|
52
|
+
* failures map to {@link GitSpawnError}.
|
|
53
|
+
*/
|
|
54
|
+
export class GitExecutionService extends Context.Service<
|
|
55
|
+
GitExecutionService,
|
|
56
|
+
{
|
|
57
|
+
/**
|
|
58
|
+
* Runs one command with the given args in `cwd` and returns the captured
|
|
59
|
+
* stdout plus the exit code of that single invocation.
|
|
60
|
+
*/
|
|
61
|
+
readonly run: (
|
|
62
|
+
command: string,
|
|
63
|
+
args: ReadonlyArray<string>,
|
|
64
|
+
cwd: string,
|
|
65
|
+
) => Effect.Effect<CommandResult, GitExecutionError, never>;
|
|
66
|
+
}
|
|
67
|
+
>()("tui/git/GitExecutionService") {
|
|
68
|
+
/**
|
|
69
|
+
* The real execution service over the platform
|
|
70
|
+
* `ChildProcessSpawner.ChildProcessSpawner`.
|
|
71
|
+
*
|
|
72
|
+
* @param cap - override the default execution cap (tests inject shorter caps)
|
|
73
|
+
*/
|
|
74
|
+
static make(
|
|
75
|
+
cap: Duration.Input = GIT_EXECUTION_CAP,
|
|
76
|
+
): Layer.Layer<GitExecutionService, never, CP.ChildProcessSpawner.ChildProcessSpawner> {
|
|
77
|
+
return Layer.effect(
|
|
78
|
+
GitExecutionService,
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const spawner = yield* CP.ChildProcessSpawner.ChildProcessSpawner;
|
|
81
|
+
const run = Effect.fn("GitExecutionService.run")(function* (
|
|
82
|
+
command: string,
|
|
83
|
+
args: ReadonlyArray<string>,
|
|
84
|
+
cwd: string,
|
|
85
|
+
) {
|
|
86
|
+
const commandEffect = Effect.scoped(
|
|
87
|
+
Effect.gen(function* () {
|
|
88
|
+
const handle = yield* spawner.spawn(CP.ChildProcess.make(command, args, { cwd }));
|
|
89
|
+
const stdout = yield* Stream.mkString(Stream.decodeText(handle.stdout));
|
|
90
|
+
const exitCode = yield* handle.exitCode;
|
|
91
|
+
return { stdout, exitCode };
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
return yield* withCap(
|
|
95
|
+
cap,
|
|
96
|
+
command,
|
|
97
|
+
commandEffect.pipe(
|
|
98
|
+
Effect.mapError(
|
|
99
|
+
(err: PlatformError.PlatformError) =>
|
|
100
|
+
new GitSpawnError({ command, message: err.message }),
|
|
101
|
+
),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
return GitExecutionService.of({ run });
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The real execution service over the platform spawner, with the default 2-second cap. */
|
|
111
|
+
static readonly layer: Layer.Layer<
|
|
112
|
+
GitExecutionService,
|
|
113
|
+
never,
|
|
114
|
+
CP.ChildProcessSpawner.ChildProcessSpawner
|
|
115
|
+
> = GitExecutionService.make();
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A stub execution service for tests, keyed by exact command + args.
|
|
119
|
+
* See {@link makeStubLayer} for the behavior contract.
|
|
120
|
+
*/
|
|
121
|
+
static readonly layerStub = (
|
|
122
|
+
entries: ReadonlyArray<StubEntry>,
|
|
123
|
+
cap: Duration.Input = GIT_EXECUTION_CAP,
|
|
124
|
+
): Layer.Layer<GitExecutionService> => makeStubLayer(entries, cap);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A scripted response for one exact command + args pair in the stub layer.
|
|
129
|
+
* The `cwd` argument of the invocation is ignored: responses are keyed by
|
|
130
|
+
* command + args only.
|
|
131
|
+
*/
|
|
132
|
+
export interface StubCommand {
|
|
133
|
+
readonly command: string;
|
|
134
|
+
readonly args: ReadonlyArray<string>;
|
|
135
|
+
readonly stdout: string;
|
|
136
|
+
readonly exitCode: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A stub-layer entry: either a scripted response, or a marker that the
|
|
141
|
+
* invocation never completes (so the execution cap can be exercised).
|
|
142
|
+
*/
|
|
143
|
+
export type StubEntry =
|
|
144
|
+
| StubCommand
|
|
145
|
+
| {
|
|
146
|
+
readonly command: string;
|
|
147
|
+
readonly args: ReadonlyArray<string>;
|
|
148
|
+
readonly neverCompletes: true;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Builds a stub {@link GitExecutionService} for tests, replacing the platform
|
|
153
|
+
* spawner entirely (no real processes are started).
|
|
154
|
+
*
|
|
155
|
+
* - Responses are looked up by exact command + args; an unlisted command fails
|
|
156
|
+
* loudly with {@link GitSpawnError} ("fail fast": a test that forgot to
|
|
157
|
+
* script an invocation never passes silently).
|
|
158
|
+
* - `neverCompletes` entries hang until the cap fires, exercising the timeout
|
|
159
|
+
* path without a real spawner.
|
|
160
|
+
* - The same cap wrapping as the real layer applies, so the default 2-second
|
|
161
|
+
* cap (or an injected shorter one) behaves identically.
|
|
162
|
+
*/
|
|
163
|
+
export const makeStubLayer = (
|
|
164
|
+
entries: ReadonlyArray<StubEntry>,
|
|
165
|
+
cap: Duration.Input = GIT_EXECUTION_CAP,
|
|
166
|
+
): Layer.Layer<GitExecutionService> =>
|
|
167
|
+
Layer.effect(
|
|
168
|
+
GitExecutionService,
|
|
169
|
+
Effect.sync(() => {
|
|
170
|
+
const lookup = (command: string, args: ReadonlyArray<string>): StubEntry | undefined =>
|
|
171
|
+
entries.find(
|
|
172
|
+
(entry) =>
|
|
173
|
+
entry.command === command &&
|
|
174
|
+
entry.args.length === args.length &&
|
|
175
|
+
entry.args.every((arg, i) => arg === args[i]),
|
|
176
|
+
);
|
|
177
|
+
const run = Effect.fn("GitExecutionService.stubRun")(function* (
|
|
178
|
+
command: string,
|
|
179
|
+
args: ReadonlyArray<string>,
|
|
180
|
+
_cwd: string,
|
|
181
|
+
) {
|
|
182
|
+
const entry = lookup(command, args);
|
|
183
|
+
if (entry === undefined) {
|
|
184
|
+
return yield* new GitSpawnError({
|
|
185
|
+
command,
|
|
186
|
+
message: `no stub response for ${command} ${args.join(" ")}`,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if ("neverCompletes" in entry) {
|
|
190
|
+
return yield* Effect.never;
|
|
191
|
+
}
|
|
192
|
+
return { stdout: entry.stdout, exitCode: entry.exitCode };
|
|
193
|
+
});
|
|
194
|
+
return GitExecutionService.of({
|
|
195
|
+
run: (command, args, cwd) => withCap(cap, command, run(command, args, cwd)),
|
|
196
|
+
});
|
|
197
|
+
}),
|
|
198
|
+
);
|