@karmaniverous/stan-core 0.3.0 → 0.4.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 +188 -155
- package/dist/cjs/index.js +1 -1
- package/dist/mjs/index.js +1 -1
- package/dist/types/index.d.ts +70 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,159 +1,192 @@
|
|
|
1
|
-
> Engine for STAN — programmatic archiving/diffing, patch application, config loading, file selection, and imports staging. No CLI/TTY concerns.
|
|
2
|
-
|
|
3
|
-
# @karmaniverous/stan-core (engine)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
> Engine for STAN — programmatic archiving/diffing, patch application, config loading, file selection, and imports staging. No CLI/TTY concerns.
|
|
2
|
+
|
|
3
|
+
# @karmaniverous/stan-core (engine)
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@karmaniverous/stan-core)  [](./LICENSE) [Changelog](./CHANGELOG.md)
|
|
6
|
+
|
|
7
|
+
This package exposes the STAN engine as a library:
|
|
8
|
+
|
|
9
|
+
- File selection (gitignore + includes/excludes + reserved workspace rules)
|
|
10
|
+
- Archiving: full archive.tar and diff archive.diff.tar (binary screening)
|
|
11
|
+
- Patch engine: worktree‑first git apply cascade with jsdiff fallback
|
|
12
|
+
- File Ops: safe mv/rm/rmdir/mkdirp block as “pre‑ops”
|
|
13
|
+
- Config loading/validation (top‑level `stan-core` in stan.config.yml|json)
|
|
14
|
+
- Imports staging under <stanPath>/imports/<label>/…
|
|
15
|
+
- Response‑format validator (optional)
|
|
16
|
+
|
|
17
|
+
For the CLI and TTY runner, see @karmaniverous/stan-cli.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add @karmaniverous/stan-core
|
|
23
|
+
# or npm i @karmaniverous/stan-core
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Node: >= 20
|
|
27
|
+
|
|
28
|
+
## Quick examples
|
|
29
|
+
|
|
30
|
+
Create a full archive (binary‑safe) and a diff archive:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createArchive } from '@karmaniverous/stan-core/stan';
|
|
34
|
+
import { createArchiveDiff } from '@karmaniverous/stan-core/stan/diff';
|
|
35
|
+
|
|
36
|
+
const cwd = process.cwd();
|
|
37
|
+
const stanPath = '.stan';
|
|
38
|
+
|
|
39
|
+
// Full archive (excludes <stanPath>/diff and binaries; include outputs with { includeOutputDir: true })
|
|
40
|
+
const fullTar = await createArchive(cwd, stanPath, { includeOutputDir: false });
|
|
41
|
+
|
|
42
|
+
// Diff archive (changes vs snapshot under <stanPath>/diff)
|
|
43
|
+
const { diffPath } = await createArchiveDiff({
|
|
44
|
+
cwd,
|
|
45
|
+
stanPath,
|
|
46
|
+
baseName: 'archive',
|
|
47
|
+
includeOutputDirInDiff: false,
|
|
48
|
+
updateSnapshot: 'createIfMissing',
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Apply a unified diff (with safe fallback) and/or run File Ops:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import {
|
|
56
|
+
applyPatchPipeline,
|
|
57
|
+
detectAndCleanPatch,
|
|
58
|
+
executeFileOps,
|
|
59
|
+
parseFileOpsBlock,
|
|
60
|
+
} from '@karmaniverous/stan-core/stan/patch';
|
|
61
|
+
|
|
62
|
+
const cwd = process.cwd();
|
|
63
|
+
|
|
64
|
+
// File Ops (pre‑ops) example
|
|
65
|
+
const plan = parseFileOpsBlock(
|
|
66
|
+
[
|
|
67
|
+
'### File Ops',
|
|
68
|
+
'mkdirp src/new/dir',
|
|
69
|
+
'mv src/old.txt src/new/dir/new.txt',
|
|
70
|
+
].join('\n'),
|
|
71
|
+
);
|
|
72
|
+
if (plan.errors.length) throw new Error(plan.errors.join('\n'));
|
|
73
|
+
await executeFileOps(cwd, plan.ops, false);
|
|
74
|
+
|
|
75
|
+
// Unified diff example (from a string)
|
|
76
|
+
const cleaned = detectAndCleanPatch(`
|
|
77
|
+
diff --git a/README.md b/README.md
|
|
78
|
+
--- a/README.md
|
|
79
|
+
+++ b/README.md
|
|
80
|
+
@@ -1,1 +1,1 @@
|
|
81
|
+
-old
|
|
82
|
+
+new
|
|
83
|
+
`);
|
|
84
|
+
const out = await applyPatchPipeline({
|
|
85
|
+
cwd,
|
|
86
|
+
patchAbs: '/dev/null', // absolute path to a saved .patch file (not required for js fallback)
|
|
87
|
+
cleaned,
|
|
88
|
+
check: false, // true => sandbox write
|
|
89
|
+
});
|
|
90
|
+
if (!out.ok) {
|
|
91
|
+
// Inspect out.result.captures (git attempts) and out.js?.failed (jsdiff reasons)
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Load and validate repo config (namespaced `stan-core` in stan.config.yml|json):
|
|
96
|
+
|
|
97
|
+
YAML example:
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
stan-core:
|
|
101
|
+
stanPath: .stan
|
|
102
|
+
includes: []
|
|
103
|
+
excludes:
|
|
104
|
+
- CHANGELOG.md
|
|
105
|
+
imports:
|
|
106
|
+
cli-docs:
|
|
107
|
+
- ../stan-cli/.stan/system/stan.requirements.md
|
|
108
|
+
- ../stan-cli/.stan/system/stan.todo.md
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
TypeScript:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { loadConfig } from '@karmaniverous/stan-core/stan/config';
|
|
115
|
+
|
|
116
|
+
const cfg = await loadConfig(process.cwd());
|
|
117
|
+
// cfg has the minimal engine shape:
|
|
118
|
+
// {
|
|
119
|
+
// stanPath: string; includes?: string[]; excludes?: string[];
|
|
120
|
+
// imports?: Record<string, string[]>
|
|
121
|
+
// }
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Stage external imports under <stanPath>/imports/<label>/… before archiving:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { prepareImports } from '@karmaniverous/stan-core/stan';
|
|
128
|
+
await prepareImports({
|
|
129
|
+
cwd: process.cwd(),
|
|
130
|
+
stanPath: '.stan',
|
|
131
|
+
map: {
|
|
132
|
+
'@scope/docs': ['external/docs/**/*.md'],
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Validate assistant responses (optional utility):
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { validateResponseMessage } from '@karmaniverous/stan-core/stan';
|
|
141
|
+
const res = validateResponseMessage(replyBody);
|
|
142
|
+
if (!res.ok) console.error(res.errors.join('\n'));
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## API surface
|
|
146
|
+
|
|
147
|
+
Top‑level (via `import '@karmaniverous/stan-core/stan'`):
|
|
148
|
+
|
|
149
|
+
- Archiving/diff/snapshot: `createArchive`, `createArchiveDiff`, `writeArchiveSnapshot`
|
|
150
|
+
- Selection/FS: `listFiles`, `filterFiles`
|
|
151
|
+
- Patch engine: `applyPatchPipeline`, `detectAndCleanPatch`, `executeFileOps`, `parseFileOpsBlock`
|
|
152
|
+
- Imports: `prepareImports`
|
|
153
|
+
- Config: `loadConfig`, `loadConfigSync`, `resolveStanPath`, `resolveStanPathSync`
|
|
154
|
+
- Validation: `validateResponseMessage`
|
|
155
|
+
|
|
156
|
+
See CHANGELOG for behavior changes. Typedoc site is generated from source.
|
|
157
|
+
|
|
158
|
+
## Selection precedence and anchors
|
|
159
|
+
|
|
160
|
+
Core file selection applies in this order:
|
|
161
|
+
|
|
162
|
+
- Reserved denials always win and cannot be overridden:
|
|
163
|
+
- `.git/**`, `<stanPath>/diff/**`, `<stanPath>/patch/**`,
|
|
164
|
+
- archive outputs under `<stanPath>/output/…`,
|
|
165
|
+
- binary screening during archive classification.
|
|
166
|
+
- `includes` override `.gitignore` (but not `excludes`).
|
|
167
|
+
- `excludes` override `includes`.
|
|
168
|
+
- `anchors` (optional) re‑include paths after excludes and `.gitignore`, subject to reserved denials and binary screening.
|
|
169
|
+
|
|
170
|
+
APIs that accept anchors:
|
|
171
|
+
|
|
172
|
+
- `filterFiles(files, { …, anchors?: string[] })`
|
|
173
|
+
- `createArchive(cwd, stanPath, { …, anchors?: string[] })`
|
|
174
|
+
- `createArchiveDiff({ …, anchors?: string[] })`
|
|
175
|
+
- `writeArchiveSnapshot({ …, anchors?: string[] })`
|
|
176
|
+
|
|
177
|
+
## Environment variables
|
|
178
|
+
|
|
179
|
+
See ENVIRONMENT.md for a complete list of environment variable switches observed by the engine, tests, and release scripts.
|
|
180
|
+
|
|
181
|
+
## Migration (legacy configs)
|
|
182
|
+
|
|
183
|
+
This engine expects a top‑level, namespaced `stan-core` block in `stan.config.yml|json`.
|
|
184
|
+
If your repository still uses legacy, flat keys at the root, migrate with the CLI:
|
|
18
185
|
|
|
19
186
|
```bash
|
|
20
|
-
|
|
21
|
-
# or npm i @karmaniverous/stan-core
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
Node: >= 20
|
|
25
|
-
|
|
26
|
-
## Quick examples
|
|
27
|
-
|
|
28
|
-
Create a full archive (binary‑safe) and a diff archive:
|
|
29
|
-
|
|
30
|
-
```ts
|
|
31
|
-
import { createArchive } from '@karmaniverous/stan-core/stan';
|
|
32
|
-
import { createArchiveDiff } from '@karmaniverous/stan-core/stan/diff';
|
|
33
|
-
|
|
34
|
-
const cwd = process.cwd();
|
|
35
|
-
const stanPath = '.stan';
|
|
36
|
-
|
|
37
|
-
// Full archive (excludes <stanPath>/diff and binaries; include outputs with { includeOutputDir: true })
|
|
38
|
-
const fullTar = await createArchive(cwd, stanPath, { includeOutputDir: false });
|
|
39
|
-
|
|
40
|
-
// Diff archive (changes vs snapshot under <stanPath>/diff)
|
|
41
|
-
const { diffPath } = await createArchiveDiff({
|
|
42
|
-
cwd,
|
|
43
|
-
stanPath,
|
|
44
|
-
baseName: 'archive',
|
|
45
|
-
includeOutputDirInDiff: false,
|
|
46
|
-
updateSnapshot: 'createIfMissing',
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
Apply a unified diff (with safe fallback) and/or run File Ops:
|
|
51
|
-
|
|
52
|
-
```ts
|
|
53
|
-
import {
|
|
54
|
-
applyPatchPipeline,
|
|
55
|
-
detectAndCleanPatch,
|
|
56
|
-
executeFileOps,
|
|
57
|
-
parseFileOpsBlock,
|
|
58
|
-
} from '@karmaniverous/stan-core/stan/patch';
|
|
59
|
-
|
|
60
|
-
const cwd = process.cwd();
|
|
61
|
-
|
|
62
|
-
// File Ops (pre‑ops) example
|
|
63
|
-
const plan = parseFileOpsBlock([
|
|
64
|
-
'### File Ops',
|
|
65
|
-
'mkdirp src/new/dir',
|
|
66
|
-
'mv src/old.txt src/new/dir/new.txt',
|
|
67
|
-
].join('\n'));
|
|
68
|
-
if (plan.errors.length) throw new Error(plan.errors.join('\n'));
|
|
69
|
-
await executeFileOps(cwd, plan.ops, false);
|
|
70
|
-
|
|
71
|
-
// Unified diff example (from a string)
|
|
72
|
-
const cleaned = detectAndCleanPatch(`
|
|
73
|
-
diff --git a/README.md b/README.md
|
|
74
|
-
--- a/README.md
|
|
75
|
-
+++ b/README.md
|
|
76
|
-
@@ -1,1 +1,1 @@
|
|
77
|
-
-old
|
|
78
|
-
+new
|
|
79
|
-
`);
|
|
80
|
-
const out = await applyPatchPipeline({
|
|
81
|
-
cwd,
|
|
82
|
-
patchAbs: '/dev/null', // absolute path to a saved .patch file (not required for js fallback)
|
|
83
|
-
cleaned,
|
|
84
|
-
check: false, // true => sandbox write
|
|
85
|
-
});
|
|
86
|
-
if (!out.ok) {
|
|
87
|
-
// Inspect out.result.captures (git attempts) and out.js?.failed (jsdiff reasons)
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
Load and validate repo config (namespaced `stan-core` in stan.config.yml|json):
|
|
92
|
-
|
|
93
|
-
YAML example:
|
|
94
|
-
|
|
95
|
-
```yaml
|
|
96
|
-
stan-core:
|
|
97
|
-
stanPath: .stan
|
|
98
|
-
includes: []
|
|
99
|
-
excludes:
|
|
100
|
-
- CHANGELOG.md
|
|
101
|
-
imports:
|
|
102
|
-
cli-docs:
|
|
103
|
-
- ../stan-cli/.stan/system/stan.requirements.md
|
|
104
|
-
- ../stan-cli/.stan/system/stan.todo.md
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
TypeScript:
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { loadConfig } from '@karmaniverous/stan-core/stan/config';
|
|
111
|
-
|
|
112
|
-
const cfg = await loadConfig(process.cwd());
|
|
113
|
-
// cfg has the minimal engine shape:
|
|
114
|
-
// {
|
|
115
|
-
// stanPath: string; includes?: string[]; excludes?: string[];
|
|
116
|
-
// imports?: Record<string, string[]>
|
|
117
|
-
// }
|
|
187
|
+
stan init # offers to refactor legacy → namespaced; supports --dry-run and backups
|
|
118
188
|
```
|
|
119
189
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
await prepareImports({
|
|
124
|
-
cwd: process.cwd(),
|
|
125
|
-
stanPath: '.stan',
|
|
126
|
-
map: {
|
|
127
|
-
'@scope/docs': ['external/docs/**/*.md'],
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
Validate assistant responses (optional utility):
|
|
133
|
-
|
|
134
|
-
```ts
|
|
135
|
-
import { validateResponseMessage } from '@karmaniverous/stan-core/stan';
|
|
136
|
-
const res = validateResponseMessage(replyBody);
|
|
137
|
-
if (!res.ok) console.error(res.errors.join('\n'));
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## API surface
|
|
141
|
-
|
|
142
|
-
Top‑level (via `import '@karmaniverous/stan-core/stan'`):
|
|
143
|
-
|
|
144
|
-
- Archiving/diff/snapshot: `createArchive`, `createArchiveDiff`, `writeArchiveSnapshot`
|
|
145
|
-
- Selection/FS: `listFiles`, `filterFiles`
|
|
146
|
-
- Patch engine: `applyPatchPipeline`, `detectAndCleanPatch`, `executeFileOps`, `parseFileOpsBlock`
|
|
147
|
-
- Imports: `prepareImports`
|
|
148
|
-
- Config: `loadConfig`, `loadConfigSync`, `resolveStanPath`, `resolveStanPathSync`
|
|
149
|
-
- Validation: `validateResponseMessage`
|
|
150
|
-
|
|
151
|
-
See CHANGELOG for behavior changes. Typedoc site is generated from source.
|
|
152
|
-
|
|
153
|
-
## Environment variables
|
|
154
|
-
|
|
155
|
-
See ENVIRONMENT.md for a complete list of environment variable switches observed by the engine, tests, and release scripts.
|
|
156
|
-
|
|
157
|
-
## License
|
|
158
|
-
|
|
159
|
-
BSD‑3‑Clause
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
BSD‑3‑Clause
|