@sous-io/sous 1.0.19 → 1.1.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/dist/sous.js +451 -349
- package/docs/markdown/README.md +8 -7
- package/docs/markdown/_sidebar.md +30 -0
- package/docs/markdown/codex.md +129 -0
- package/docs/markdown/commands.md +306 -52
- package/docs/markdown/config-inspection.md +36 -6
- package/docs/markdown/config-layers.md +8 -8
- package/docs/markdown/config-variables.md +42 -3
- package/docs/markdown/configuration.md +276 -22
- package/docs/markdown/design-principles.md +1 -1
- package/docs/markdown/editing-files.md +600 -0
- package/docs/markdown/handlers.md +239 -0
- package/docs/markdown/repositories-authoring.md +91 -15
- package/docs/markdown/repositories-consuming.md +66 -22
- package/docs/markdown/repositories-contributing.md +1 -1
- package/docs/markdown/repositories-file-formats.md +156 -4
- package/docs/markdown/repositories-providers.md +92 -19
- package/docs/markdown/repositories-quickstart.md +14 -4
- package/docs/markdown/repositories-troubleshooting.md +12 -7
- package/docs/markdown/repositories-variables.md +6 -2
- package/docs/markdown/repositories.md +7 -2
- package/package.json +38 -33
- package/recipes/core/sous-skills/skills/about-sous/SKILL.tpl.md +13 -10
- package/recipes/core/sous-skills/skills/about-sous-configuration/SKILL.tpl.md +4 -4
- package/recipes/core/sous-skills/skills/about-sous-recipes/SKILL.tpl.md +8 -4
- package/recipes/core/sous-skills/skills/about-sous-repos/SKILL.tpl.md +31 -7
- package/recipes/core/sous-skills/sous.recipe.yaml +1 -1
- package/sous.config.schema.json +273 -20
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
# Editing files Sous does not own
|
|
2
|
+
|
|
3
|
+
Sous writes the files it owns (compiled skills, agents, the memories file) and overwrites them on every
|
|
4
|
+
build. Some files belong to the project or to another tool: Claude Code's `.claude/settings.json`,
|
|
5
|
+
`.mcp.json`, `.pre-commit-config.yaml`, `.gitignore`, the frontmatter of an agent file, a file in
|
|
6
|
+
your home directory. For those, Sous has **the editor**: you declare an edit, and Sous makes exactly
|
|
7
|
+
that change, leaves every other byte of the file alone, remembers what it did, and undoes it when the
|
|
8
|
+
edit is no longer declared.
|
|
9
|
+
|
|
10
|
+
Declared edits live in one config key, `editor.edits`. A recipe declares them in its manifest's
|
|
11
|
+
`edits` list, a plugin adds them from its code, and a project can write them in its own config.
|
|
12
|
+
|
|
13
|
+
## A first edit
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
# .sous/sous.config.yaml
|
|
17
|
+
editor:
|
|
18
|
+
edits:
|
|
19
|
+
# The list contains these rules; Sous adds the missing ones.
|
|
20
|
+
- file: .claude/settings.json
|
|
21
|
+
ensureIncludes:
|
|
22
|
+
path: permissions.allow
|
|
23
|
+
items:
|
|
24
|
+
- "Bash(git-commit.sh:*)"
|
|
25
|
+
- "Bash(issues.sh:*)"
|
|
26
|
+
|
|
27
|
+
# One value at a path.
|
|
28
|
+
- file: .claude/settings.json
|
|
29
|
+
set:
|
|
30
|
+
path: env.SESSION_HARNESS_MODE
|
|
31
|
+
value: strict
|
|
32
|
+
|
|
33
|
+
# A line in a plain text file.
|
|
34
|
+
- file: .gitignore
|
|
35
|
+
ensureLine:
|
|
36
|
+
line: "docs/context/.kw-session-*"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
In a recipe's `sous.recipe.yaml` the same entries go in a top-level `edits:` list.
|
|
40
|
+
|
|
41
|
+
Each entry names one `file` (relative to the project root; `~` and `${variables}` work, and a file
|
|
42
|
+
outside the project is allowed) and then one or more **operations**. An operation's key always holds
|
|
43
|
+
an object with everything it needs, including `path`, the place in the file it acts on. Leave `path`
|
|
44
|
+
out, or give `""`, to act on the whole document:
|
|
45
|
+
|
|
46
|
+
```yaml
|
|
47
|
+
- file: words.json
|
|
48
|
+
sort: {}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
?> There is no short form. `set: "x"` is an error; write `set: { path: ..., value: "x" }`.
|
|
52
|
+
|
|
53
|
+
### Several operations in one entry
|
|
54
|
+
|
|
55
|
+
An entry may hold several operation keys. They run in a fixed order, not the order you write them
|
|
56
|
+
(a list is sorted after anything is added to it); the order is in the table below. For an exact
|
|
57
|
+
order, or one operation more than once, use an `operations` list:
|
|
58
|
+
|
|
59
|
+
```yaml
|
|
60
|
+
- file: .claude/settings.json
|
|
61
|
+
operations:
|
|
62
|
+
- ensureIncludes:
|
|
63
|
+
path: permissions.allow
|
|
64
|
+
items: ["Bash(git-commit.sh:*)"]
|
|
65
|
+
- ensureIncludes:
|
|
66
|
+
path: permissions.deny
|
|
67
|
+
items: ["Bash(rm:*)"]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## What Sous does on each build
|
|
71
|
+
|
|
72
|
+
- **It applies every declared edit**, after compiling and pruning (so a recipe can edit a file the
|
|
73
|
+
compiler writes), and before the recipes' `afterBuild` handlers. If someone changed a value Sous
|
|
74
|
+
sets, Sous puts it back; to keep a different value, declare it in the project's own config, which comes after every
|
|
75
|
+
recipe's and so wins.
|
|
76
|
+
- **It undoes an edit that is no longer declared:** the recipe was unsubscribed, the plugin removed, a
|
|
77
|
+
condition stopped matching. A value Sous set goes back to what it was before; an item Sous added to
|
|
78
|
+
a list is removed, and items others added stay. A key Sous deleted comes back in its old place,
|
|
79
|
+
after the key that was before it, with its old text and its comments. Undoing happens only after
|
|
80
|
+
a config that loaded without errors.
|
|
81
|
+
- **A missing file is created** with just the declared content, and removed again (after a backup)
|
|
82
|
+
when nothing declares it any more.
|
|
83
|
+
- **A file Sous cannot parse is skipped** with a warning and left exactly as it is.
|
|
84
|
+
- **It says what it did only in debug output** (`--log-level debug`). Ordinary builds stay quiet.
|
|
85
|
+
- **`sous build --dry-run`** works out every edit and checks every assertion, and writes nothing.
|
|
86
|
+
`sous doctor` checks the assertions the same way.
|
|
87
|
+
|
|
88
|
+
The record of what Sous applied is `.sous/sous.edits.json`, and backups go to
|
|
89
|
+
`.sous/backups/<date>/<time>/<path>`. Both stay out of git.
|
|
90
|
+
|
|
91
|
+
### Protecting against an unwanted rollback
|
|
92
|
+
|
|
93
|
+
Every entry takes these settings:
|
|
94
|
+
|
|
95
|
+
| Setting | Default | What it does |
|
|
96
|
+
| --- | --- | --- |
|
|
97
|
+
| `once` | off | Applies the entry on the first run only; later changes to the value are kept. |
|
|
98
|
+
| `rollback` | on | Undoes the entry when it is no longer declared. Off leaves the result in place. |
|
|
99
|
+
| `backupBeforeChange` | off | Copies the file to `.sous/backups/` before Sous changes it. |
|
|
100
|
+
| `backupOnRemoval` | on | Copies a file to `.sous/backups/` before a rollback deletes it. |
|
|
101
|
+
|
|
102
|
+
### Starter files and folders
|
|
103
|
+
|
|
104
|
+
A recipe can set up files and folders the project then keeps:
|
|
105
|
+
|
|
106
|
+
```yaml
|
|
107
|
+
edits:
|
|
108
|
+
- ensureDirectoryExists:
|
|
109
|
+
paths:
|
|
110
|
+
- docs/context
|
|
111
|
+
- docs/work
|
|
112
|
+
|
|
113
|
+
- file: .kw/descriptor.json
|
|
114
|
+
ensureFileExists:
|
|
115
|
+
content:
|
|
116
|
+
$file: starter/descriptor.tpl.json
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
`ensureFileExists` writes the file only when it is missing, so the project's later edits stay. If
|
|
120
|
+
the recipe goes away, the file is backed up and removed, and each folder Sous created is removed if it
|
|
121
|
+
is empty, including the folders Sous created to hold a new file (`.claude/` for a new
|
|
122
|
+
`.claude/settings.json`); a folder that was already there, or that holds anything, stays.
|
|
123
|
+
|
|
124
|
+
## Operations
|
|
125
|
+
|
|
126
|
+
Operations that state what must be true change the file only when it is not already true, and are
|
|
127
|
+
the ones to reach for. Actions (`append` and the others marked below) always act; Sous runs an
|
|
128
|
+
action once, when the entry is first applied.
|
|
129
|
+
|
|
130
|
+
| Order | Operation | Parameters | Other names |
|
|
131
|
+
| --- | --- | --- | --- |
|
|
132
|
+
| 0 | `ensureFileExists` | `content` | `createIfMissing` |
|
|
133
|
+
| 0 | `ensureDirectoryExists` | `paths` (no `file`) | |
|
|
134
|
+
| 10 | `ensureObject`, `ensureList`, `wrapInList`, `unwrapList` | | |
|
|
135
|
+
| 20 | `set` | `value`, `ifMissing` | `ensureValue`, `replace`; with `ifMissing: true`: `setIfMissing`, `ensureDefault` |
|
|
136
|
+
| 20 | `setIfEquals` | `value`, `equals` | |
|
|
137
|
+
| 20 | `renameKey` | `name` | |
|
|
138
|
+
| 20 | `copy` | `to`, `ifMissing` | |
|
|
139
|
+
| 20 | `move` | `to`, `ifMissing` | |
|
|
140
|
+
| 25 | `merge` | `value`, `ifMissing` | `ensureKeys`, `deepMerge`; with `ifMissing: true`: `mergeIfMissing` |
|
|
141
|
+
| 25 | `replaceWhere`, `updateWhere`, `mergeWhere` | `where`, `item` or `value` | |
|
|
142
|
+
| 30 | `upsertWhere` | `match`, `item` | `ensureItemWhere` |
|
|
143
|
+
| 40 | `ensureIncludes` | `items` (or `item`), `position` | `appendIfMissing`, `union`; `ensureFirst`, `ensureLast`, `ensureAt` (with `index`) |
|
|
144
|
+
| 40 | `prependIfMissing` | `items` (or `item`) | |
|
|
145
|
+
| 40 | `append` (action) | `item` or `items`, `position` | `prepend`, `insertAt` (with `index`), `insertBefore`, `insertAfter` |
|
|
146
|
+
| 40 | `insertBeforeWhere`, `insertAfterWhere` (actions) | `where`, `item` or `items` | |
|
|
147
|
+
| 50 | `remove` | `values` or `where` | `removeValues`, `ensureExcludes`, `removeValue`; `removeWhere`, `ensureNoItemWhere` |
|
|
148
|
+
| 50 | `removeAt` (action) | `index` | |
|
|
149
|
+
| 50 | `delete` | | `ensureAbsent` |
|
|
150
|
+
| 50 | `deleteIfEquals`, `deleteIfEmpty`, `deleteKeys`, `keepOnlyKeys` | `value`, `keys` | |
|
|
151
|
+
| 50 | `removeLine` | `line` | |
|
|
152
|
+
| 60 | `ensureLine` | `line`, `position` | |
|
|
153
|
+
| 60 | `ensureBlock` | `name`, `content`, `position`, `comment` | |
|
|
154
|
+
| 60 | `appendText`, `prependText` (actions) | `value` | |
|
|
155
|
+
| 60 | `replaceText`, `replacePattern` | `match`, `value` | |
|
|
156
|
+
| 70 | `increment`, `decrement`, `toggle` (actions) | `value` | |
|
|
157
|
+
| 80 | `dedupe` | `by` | `ensureUnique`, `dedupeBy` |
|
|
158
|
+
| 90 | `sort` | `by`, `descending` | `ensureSorted`, `sortBy` |
|
|
159
|
+
| 95 | `sortKeys` | | |
|
|
160
|
+
| 95 | `moveKeyBefore`, `moveKeyAfter` | `key`, `anchor` | |
|
|
161
|
+
| 95 | `moveKeyFirst`, `moveKeyLast` | `key` | |
|
|
162
|
+
| 95 | `moveItem` | `item` or `where`, `position` | with `where`: `moveItemWhere` |
|
|
163
|
+
| 100 | `setCommentBefore` | `text` | |
|
|
164
|
+
| 100 | `setCommentAfter` | `text` | |
|
|
165
|
+
| 100 | `removeComment` | `before`, `after` | |
|
|
166
|
+
|
|
167
|
+
Besides `path` (and `assertion`), every operation that acts inside a document also takes
|
|
168
|
+
`document`: see [A YAML file holding several documents](#a-yaml-file-holding-several-documents).
|
|
169
|
+
|
|
170
|
+
Every name in the last column is the same operation with an option set, so these two entries mean
|
|
171
|
+
the same thing:
|
|
172
|
+
|
|
173
|
+
```yaml
|
|
174
|
+
- file: .claude/settings.json
|
|
175
|
+
ensureFirst:
|
|
176
|
+
path: permissions.allow
|
|
177
|
+
item: "Bash(git-commit.sh:*)"
|
|
178
|
+
|
|
179
|
+
- file: .claude/settings.json
|
|
180
|
+
ensureIncludes:
|
|
181
|
+
path: permissions.allow
|
|
182
|
+
items: ["Bash(git-commit.sh:*)"]
|
|
183
|
+
position: first
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
`position` takes `first`, `last`, an index from 0, `{ after: ... }`, `{ before: ... }` or
|
|
187
|
+
`{ line: n }` (from 1). For `ensureIncludes`, `first`, `last` and an index mean "the item is there",
|
|
188
|
+
moving it if needed. For `ensureLine` a position only says where a missing line goes; a line already
|
|
189
|
+
anywhere in the file is left alone, and an `after` or `before` line that is not found means the end.
|
|
190
|
+
|
|
191
|
+
`prependIfMissing` puts each missing item at the start, in the order given; an item already in the
|
|
192
|
+
list stays where it is (`ensureFirst` would move it). The key moves (`moveKeyFirst` and the others)
|
|
193
|
+
change nothing when the key or the anchor is missing.
|
|
194
|
+
|
|
195
|
+
An order is undone like a value: `sort`, `sortKeys` and the key moves record the order the list or
|
|
196
|
+
object had before Sous first changed it, and put it back when the entry goes away; an item or key
|
|
197
|
+
added since keeps its place. `dedupe` records each item it removed and where, and puts them back.
|
|
198
|
+
`replaceText` is undone by turning `value` back into `match`. A `replacePattern` cannot be worked
|
|
199
|
+
backwards, so Sous keeps the text from before its last replacement and puts it back only while the
|
|
200
|
+
text is still exactly what Sous left; if anyone has changed it since, it stays as it is.
|
|
201
|
+
|
|
202
|
+
`ensureBlock` keeps a block between two marker lines, written in the file's comment style (`#`,
|
|
203
|
+
`//`, `<!-- -->`, `/* */`, `--` or `;`, from the file's name, or `comment` to choose):
|
|
204
|
+
|
|
205
|
+
```text
|
|
206
|
+
# >>> sous managed: kw (do not edit between these markers)
|
|
207
|
+
docs/context/.kw-session-*
|
|
208
|
+
# <<< sous managed: kw
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
A block added at the end of a file gets a blank line before it. If a marker line is missing or
|
|
212
|
+
appears twice, Sous does not guess where the block ends: it skips the edit with a warning, and the
|
|
213
|
+
block's assertion fails.
|
|
214
|
+
|
|
215
|
+
A key or item that is deleted or moves (a sort, a key move, `moveItem`, `move`) takes its own
|
|
216
|
+
lines, the comment lines directly above it and the comment at the end of its line. Blank lines
|
|
217
|
+
never move with a key: they stay between the same places. Sorting keys whose groups are separated
|
|
218
|
+
by a blank line keeps the blank line where it was, so a key can end up on the other side of it;
|
|
219
|
+
undoing the sort puts every key back, the blank line still in its place. A deleted key leaves the
|
|
220
|
+
blank lines around it, and undoing puts it back between the same ones.
|
|
221
|
+
|
|
222
|
+
`sortKeys` on this object:
|
|
223
|
+
|
|
224
|
+
```yaml
|
|
225
|
+
# about b
|
|
226
|
+
b: 2 # two
|
|
227
|
+
|
|
228
|
+
# about a
|
|
229
|
+
a: 1
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
gives this one: each key took its comments, and the blank line stayed between the first and
|
|
233
|
+
second places.
|
|
234
|
+
|
|
235
|
+
```yaml
|
|
236
|
+
# about a
|
|
237
|
+
a: 1
|
|
238
|
+
|
|
239
|
+
# about b
|
|
240
|
+
b: 2 # two
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Undoing an edit gives back the exact text it changed: `unwrapList` undone writes `["only"]` back on
|
|
244
|
+
one line, as it was.
|
|
245
|
+
|
|
246
|
+
### Comments
|
|
247
|
+
|
|
248
|
+
A comment belongs to a key or a list item: the comment lines directly above it (at its
|
|
249
|
+
indentation, with no blank line between) and the comment at the end of its line. `setCommentBefore`
|
|
250
|
+
and `setCommentAfter` replace the comment there, or add one; `removeComment` takes it away
|
|
251
|
+
(`before: true` or `after: true` for only one; neither for both). `text` is the comment's words,
|
|
252
|
+
without `#` or `//`. A comment before may have several lines; a comment after is one line.
|
|
253
|
+
|
|
254
|
+
```yaml
|
|
255
|
+
- file: .claude/agents/reviewer.md
|
|
256
|
+
setCommentBefore:
|
|
257
|
+
path: model
|
|
258
|
+
text: |-
|
|
259
|
+
Chosen by the platform team.
|
|
260
|
+
Ask in #platform before changing it.
|
|
261
|
+
setCommentAfter:
|
|
262
|
+
path: tools[1]
|
|
263
|
+
text: needed by the release script
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
```markdown
|
|
267
|
+
---
|
|
268
|
+
name: reviewer
|
|
269
|
+
# Chosen by the platform team.
|
|
270
|
+
# Ask in #platform before changing it.
|
|
271
|
+
model: opus
|
|
272
|
+
tools:
|
|
273
|
+
- Read
|
|
274
|
+
- Bash # needed by the release script
|
|
275
|
+
---
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Only the comment's own bytes change, in the file's line endings, and undoing puts back exactly what
|
|
279
|
+
was there. This works in JSONC, YAML and Markdown frontmatter. Plain JSON has no comments, so
|
|
280
|
+
on a `.json` file (such as `.claude/settings.json`) the edit is skipped with a warning. A value
|
|
281
|
+
that shares its line with others (inside `[...]` or `{...}`) has no comment of its own and is
|
|
282
|
+
skipped too. No assertion is checked for a comment.
|
|
283
|
+
|
|
284
|
+
### A YAML file holding several documents
|
|
285
|
+
|
|
286
|
+
A YAML file can hold several documents separated by `---` lines, as a Kubernetes manifest does.
|
|
287
|
+
A path cannot say which document it means, so an operation on such a file names one with
|
|
288
|
+
`document`, its index from 0. Every other document, and every `---` line, stays byte for byte:
|
|
289
|
+
|
|
290
|
+
```yaml
|
|
291
|
+
- file: deploy/web.yaml
|
|
292
|
+
set:
|
|
293
|
+
document: 1
|
|
294
|
+
path: spec.replicas
|
|
295
|
+
value: 3
|
|
296
|
+
setCommentAfter:
|
|
297
|
+
document: 1
|
|
298
|
+
path: spec.replicas
|
|
299
|
+
text: three in production
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
`document` goes inside each operation, beside `path`, so one entry can edit several documents. On a
|
|
303
|
+
file with one document, `document: 0` is that document. An index past the last document skips the
|
|
304
|
+
edit with a warning; an operation without `document` on a file holding several skips the file, as
|
|
305
|
+
any file Sous cannot parse. An operation with `document` gets no automatic assertion, and
|
|
306
|
+
`ensureLine` and the other text operations take `document` only with the `path` of a string in it.
|
|
307
|
+
|
|
308
|
+
**Formats:** JSON, JSONC (comments, key order and layout are kept, and an object or list written
|
|
309
|
+
beside one-line neighbours goes on one line; a `tsconfig*.json` or a `.json` file in `.vscode/` is
|
|
310
|
+
read as JSONC), YAML (comments are kept; a string keeps the quotes or block style its author chose,
|
|
311
|
+
and a text of several lines is written as a block), the YAML frontmatter of a Markdown
|
|
312
|
+
file (the body is kept byte for byte), a YAML file holding several documents (one at a time, with
|
|
313
|
+
`document`), and any other file as plain text lines, each keeping its own
|
|
314
|
+
line ending. TOML files are skipped with a warning for now.
|
|
315
|
+
|
|
316
|
+
### Copying a value
|
|
317
|
+
|
|
318
|
+
`copy` makes the value at `to` a copy of the value at `path`, as it is in the file on each build. A
|
|
319
|
+
value already at `to` is replaced, and put back when the entry goes away. Nothing happens while
|
|
320
|
+
`path` is missing. With `ifMissing: true`, only a missing `to` is filled.
|
|
321
|
+
|
|
322
|
+
```yaml
|
|
323
|
+
- file: .claude/settings.json
|
|
324
|
+
copy:
|
|
325
|
+
path: env.ACME_HOME
|
|
326
|
+
to: env.ACME_HOME_FALLBACK
|
|
327
|
+
ifMissing: true
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
settings.at("env.ACME_HOME").copy("env.ACME_HOME_FALLBACK", { ifMissing: true });
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
When you know the value when you write the entry, `set` says the same thing directly. `copy` is for
|
|
335
|
+
a value that is only known from the file. A `to` that names a list item (`steps[1]`) replaces that
|
|
336
|
+
item; to add to a list, use `ensureIncludes`. `copy` has no automatic assertion.
|
|
337
|
+
|
|
338
|
+
### Moving a value
|
|
339
|
+
|
|
340
|
+
`move` takes the value at `path` to `to`. Within one object it is a rename: the key keeps its
|
|
341
|
+
place, its value's text and the comment on its line. To another object, the key goes last in that
|
|
342
|
+
object with its own text under its new name, at that object's indentation: its comment lines above
|
|
343
|
+
and the comment on its line go with it, and undoing puts it back where it was. A value already at
|
|
344
|
+
`to` is replaced (the new value written in the file's style); with
|
|
345
|
+
`ifMissing: true` it is kept, and nothing moves. Nothing happens while `path` is missing.
|
|
346
|
+
|
|
347
|
+
```yaml
|
|
348
|
+
- file: .claude/settings.json
|
|
349
|
+
move:
|
|
350
|
+
path: env.ACME_DIR # the old name
|
|
351
|
+
to: acme.dir
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
settings.at("env.ACME_DIR").move("acme.dir");
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
When the entry goes away, the value goes back to `path`, in its old place among the keys, and what
|
|
359
|
+
`to` held is put back. Its automatic assertion is that `path` no longer exists, so another edit
|
|
360
|
+
that brings the old key back is reported.
|
|
361
|
+
|
|
362
|
+
Other ways to say it: within one object, `renameKey` does the same (`renameKey: { path:
|
|
363
|
+
env.ACME_DIR, name: ACME_HOME }`), but leaves both keys alone when the new name is taken. When you
|
|
364
|
+
know the value, `set` at `to` plus `delete` at `path` says the same thing. `path` must end in a key:
|
|
365
|
+
an item of a list is moved within its list with `moveItem`.
|
|
366
|
+
|
|
367
|
+
### Inserting beside an item that matches
|
|
368
|
+
|
|
369
|
+
`insertBeforeWhere` and `insertAfterWhere` add items just before or just after the first item whose
|
|
370
|
+
fields match `where`. They are actions, like `append`: the items are added even if they are already
|
|
371
|
+
there, and Sous does it once, when the entry is first applied. When no item matches, the items go
|
|
372
|
+
at the end, as with `insertBefore` and an `after` or `before` line that is not found. A missing list
|
|
373
|
+
is created; when the entry goes away, the items it added are removed (and a list it created).
|
|
374
|
+
|
|
375
|
+
```yaml
|
|
376
|
+
- file: .claude/settings.json
|
|
377
|
+
insertAfterWhere:
|
|
378
|
+
path: hooks.PreToolUse
|
|
379
|
+
where: { matcher: Bash }
|
|
380
|
+
item: { matcher: Bash, hooks: [{ type: command, command: acme-guard.sh }] }
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
```ts
|
|
384
|
+
settings.at("hooks.PreToolUse").insertAfterWhere({ matcher: "Bash" }, { matcher: "Bash", hooks: [] });
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
Other ways to say it: when you can write out the whole item to put it beside, `insertBefore` and
|
|
388
|
+
`insertAfter` (`append` with `position: { before: ... }`) do the same. When the item should be in
|
|
389
|
+
the list once, whatever builds come and go, use a statement instead: `upsertWhere` (the list has an
|
|
390
|
+
item with these fields, equal to this one) or `ensureIncludes`.
|
|
391
|
+
|
|
392
|
+
### Moving a list item
|
|
393
|
+
|
|
394
|
+
`moveItem` puts a list item, found by value (`item`) or by fields (`where`), at `position`: `first`,
|
|
395
|
+
`last`, an index from 0 (counted in the list without the item), `{ before: item }`,
|
|
396
|
+
`{ after: item }`, `{ beforeWhere: {...} }` or `{ afterWhere: {...} }`. The item moves with its own
|
|
397
|
+
text and the comment on its line. Nothing happens when the item is not in the list, when the item
|
|
398
|
+
to put it beside is not there, or when it is already in place. When the entry goes away, the item
|
|
399
|
+
goes back where it was before Sous first moved it.
|
|
400
|
+
|
|
401
|
+
```yaml
|
|
402
|
+
- file: .claude/settings.json
|
|
403
|
+
moveItem:
|
|
404
|
+
path: permissions.allow
|
|
405
|
+
item: "Bash(git-commit.sh:*)"
|
|
406
|
+
position: { before: "Bash(*)" }
|
|
407
|
+
|
|
408
|
+
- file: .claude/settings.json
|
|
409
|
+
moveItemWhere:
|
|
410
|
+
path: hooks.PreToolUse
|
|
411
|
+
where: { matcher: Bash }
|
|
412
|
+
position: first
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
settings.at("permissions.allow").moveItem("Bash(git-commit.sh:*)", { before: "Bash(*)" });
|
|
417
|
+
settings.at("hooks.PreToolUse").moveItemWhere({ matcher: "Bash" }, "first");
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
`moveItemWhere` is `moveItem` with `where`. Other ways to say it: `ensureFirst`, `ensureLast` and
|
|
421
|
+
`ensureAt` (`ensureIncludes` with `position`) also put an item first, last or at an index, and add it
|
|
422
|
+
when it is missing; `moveItem` only moves an item that is there. `moveItem` has no automatic
|
|
423
|
+
assertion; add `assert: { first: ... }` (or `last`, `at`) when the place matters.
|
|
424
|
+
|
|
425
|
+
## Assertions
|
|
426
|
+
|
|
427
|
+
An assertion states something that must hold in a file after every edit. It fails the build by
|
|
428
|
+
default, so use one where your tool breaks without the value:
|
|
429
|
+
|
|
430
|
+
```yaml
|
|
431
|
+
edits:
|
|
432
|
+
- file: .claude/settings.json
|
|
433
|
+
assert:
|
|
434
|
+
path: permissions.allow
|
|
435
|
+
includes: "Bash(git-commit.sh:*)"
|
|
436
|
+
message: The session harness cannot commit without this rule.
|
|
437
|
+
|
|
438
|
+
- file: AGENTS.override.md
|
|
439
|
+
assert:
|
|
440
|
+
maxBytes: 32768
|
|
441
|
+
level: warn
|
|
442
|
+
message: Codex cuts project instructions off at 32 KiB.
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
- `level: warn` prints a warning instead of failing. `sous build --ignore-failed-assertions` (or
|
|
446
|
+
`--keep-going`, which also covers failed checks) lets a build finish in spite of failed
|
|
447
|
+
assertions, still printing each as a warning, and the build ends as successful. There is no
|
|
448
|
+
setting that makes this permanent.
|
|
449
|
+
- When an assertion at level error fails, that file is not written; every other file is, and the
|
|
450
|
+
build then ends with exit code 1. Sous shows what changed the value:
|
|
451
|
+
|
|
452
|
+
```text
|
|
453
|
+
Assertion failed in .claude/settings.json at permissions.allow
|
|
454
|
+
voltagrid/session-harness expects it to include "Bash(git-commit.sh:*)".
|
|
455
|
+
The session harness cannot commit without this rule.
|
|
456
|
+
|
|
457
|
+
What changed this value, in order:
|
|
458
|
+
1. voltagrid/session-harness ensureIncludes ["Bash(git-commit.sh:*)"]
|
|
459
|
+
2. acme/strict-permissions set []
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
**Checks:** `equals`, `includes`, `excludes`, `exists`, `first`, `last`, `at`, `itemWhere`,
|
|
463
|
+
`noItemWhere`, `unique`, `sorted`, `sortedKeys`, `type`, `matches`, `includesLine`, `includesBlock`,
|
|
464
|
+
`lessThan`, `lessThanOrEqualTo`, `greaterThan`, `greaterThanOrEqualTo`, `fileExists`,
|
|
465
|
+
`fileDoesNotExist`, `directoryExists`, `directoryDoesNotExist`, `maxBytes`, `minBytes`,
|
|
466
|
+
`maxCharacters`, `minCharacters`, `maxTokens`, `minTokens`. `not: true` turns the checks of an
|
|
467
|
+
`assert` around. `fileExists: false` and `fileDoesNotExist: true` mean the same.
|
|
468
|
+
|
|
469
|
+
?> Token counts use the `o200k_base` tokenizer, the one Sous uses for the files it writes, so they are
|
|
470
|
+
approximate for any particular model.
|
|
471
|
+
|
|
472
|
+
### Automatic assertions and conflicts
|
|
473
|
+
|
|
474
|
+
Every edit also checks, at level warn, that its own result still holds once every edit is made. When
|
|
475
|
+
two recipes set different values at one path, the later one wins and the earlier one's check warns,
|
|
476
|
+
with the history above. If you wrote your own assertion at a path, Sous adds no automatic one for you
|
|
477
|
+
there. To quiet an automatic check that is wrong for your case:
|
|
478
|
+
|
|
479
|
+
```yaml
|
|
480
|
+
edits:
|
|
481
|
+
# On one edit.
|
|
482
|
+
- file: .claude/settings.json
|
|
483
|
+
sort:
|
|
484
|
+
path: permissions.allow
|
|
485
|
+
assertion: none
|
|
486
|
+
|
|
487
|
+
# At a path: this key and below (the default), chosen kinds.
|
|
488
|
+
- file: .claude/settings.json
|
|
489
|
+
silence:
|
|
490
|
+
path: permissions
|
|
491
|
+
kinds: [sorted, unique]
|
|
492
|
+
|
|
493
|
+
# Every kind except these; scope key-only or below narrows it.
|
|
494
|
+
- file: .claude/settings.json
|
|
495
|
+
silence:
|
|
496
|
+
path: permissions.allow
|
|
497
|
+
except: [includes]
|
|
498
|
+
scope: key-only
|
|
499
|
+
|
|
500
|
+
# The whole file.
|
|
501
|
+
- file: .claude/settings.json
|
|
502
|
+
silence: {}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
You can only silence the automatic checks of your own edits.
|
|
506
|
+
|
|
507
|
+
## In a plugin
|
|
508
|
+
|
|
509
|
+
A plugin adds entries from a contributor, on every config build. When the plugin is removed it stops
|
|
510
|
+
adding them, and Sous undoes its edits; the plugin needs no uninstall code. Two styles record the same
|
|
511
|
+
entries:
|
|
512
|
+
|
|
513
|
+
```ts
|
|
514
|
+
import { definePlugin, type ContributorDefinition } from "@sous-io/sous-plugin-api";
|
|
515
|
+
import { editor, expect } from "@sous-io/sous-plugin-editor";
|
|
516
|
+
|
|
517
|
+
const editsContributor: ContributorDefinition = {
|
|
518
|
+
id: "acme-tools/document-edits",
|
|
519
|
+
|
|
520
|
+
contribute(config) {
|
|
521
|
+
// Objects in the manifest's shape.
|
|
522
|
+
config.atPath("editor.edits").appendValue({
|
|
523
|
+
file: "${projectRoot}/.claude/settings.json",
|
|
524
|
+
set: { path: "env.ACME_COMMS_DIR", value: "${projectRoot}/comms" },
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
// The chain. A handle on one file: nothing is read or written here.
|
|
528
|
+
const settings = editor(config).file("${projectRoot}/.claude/settings.json");
|
|
529
|
+
|
|
530
|
+
settings
|
|
531
|
+
.at("permissions.allow")
|
|
532
|
+
.appendIfMissing("Bash(acme-commit.sh:*)")
|
|
533
|
+
.dedupe()
|
|
534
|
+
.sort();
|
|
535
|
+
|
|
536
|
+
expect( settings.at("permissions.allow") ).to.include("Bash(acme-commit.sh:*)");
|
|
537
|
+
expect( settings.byteCount ).to.be.lessThan(100000).orWarn();
|
|
538
|
+
},
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
export default definePlugin({
|
|
542
|
+
name: "sous-plugin-acme-tools",
|
|
543
|
+
version: "1.0.0",
|
|
544
|
+
|
|
545
|
+
register(sous) {
|
|
546
|
+
sous.config.contribute(editsContributor);
|
|
547
|
+
},
|
|
548
|
+
});
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
!> The chain records edits; it does not make them. The file is read later, after the config is
|
|
552
|
+
built, so the chain has no `get` or `exists`, and `settings.byteCount` is a reference to "the byte
|
|
553
|
+
count of that file", not a number: `if (settings.byteCount > 100)` does not work. For "only if", use
|
|
554
|
+
an operation that carries the condition (`setIfMissing`, `setIfEquals`) or an assertion.
|
|
555
|
+
|
|
556
|
+
- `editor(config).file(path, { once, rollback, backupBeforeChange, backupOnRemoval })` gives a file;
|
|
557
|
+
`.at(path)` a value in it. Every operation and every other name is a method: its main parameters in
|
|
558
|
+
order, then an options object (`.ensureLine("x", { position: "start" })`). Comments too:
|
|
559
|
+
`.at("model").setCommentBefore("Chosen by the platform team")`.
|
|
560
|
+
- `.selectDocument(index)` on a file gives one document of a YAML file holding several; every
|
|
561
|
+
operation recorded through it carries `document: index`
|
|
562
|
+
(`editor(config).file("deploy/web.yaml").selectDocument(1).at("spec.replicas").set(3)`).
|
|
563
|
+
- `.withoutAssertion()` after an operation drops its automatic check;
|
|
564
|
+
`settings.silence("permissions", ["sorted"])`, with `.thisKeyOnly()`, `.belowOnly()` or
|
|
565
|
+
`.except([...])`, and `settings.withoutAutomaticAssertions()` silence more.
|
|
566
|
+
- `expect(...)` takes a value, a file (`expect(oldManifest).to.not.exist()`), a size
|
|
567
|
+
(`byteCount`, `characterCount`, `tokenCount`) or a folder from `editor(config).directory(path)`.
|
|
568
|
+
It reads like Chai, with comparisons spelled out (`lessThan`, `lessThanOrEqualTo`, `greaterThan`,
|
|
569
|
+
`greaterThanOrEqualTo`), and every assertion ends in a method call: `.to.have.unique.members()`,
|
|
570
|
+
`.to.be.an("array").that.is.sorted()`, `.to.have.first.member(x)`. `.orWarn()` and
|
|
571
|
+
`.withMessage("...")` go last.
|
|
572
|
+
- A text file's lines and managed blocks: `expect( ignore ).to.have.line("dist/")`
|
|
573
|
+
(`includesLine`) and `expect( ignore ).to.includeBlock("kw")` (`includesBlock`; a second
|
|
574
|
+
argument also checks the block's content), each with `.not` for the opposite.
|
|
575
|
+
- A folder is checked only for being there: `expect( editor(config).directory("docs/context") ).to.exist()`
|
|
576
|
+
(`directoryExists`) and `.to.not.exist()` (`directoryDoesNotExist`).
|
|
577
|
+
|
|
578
|
+
```ts
|
|
579
|
+
const ignore = editor(config).file(".gitignore");
|
|
580
|
+
|
|
581
|
+
expect( ignore ).to.includeBlock("session-files").withMessage("The session files must stay ignored.");
|
|
582
|
+
expect( editor(config).directory("docs/context") ).to.exist().orWarn();
|
|
583
|
+
expect( editor(config).directory(".old-cache") ).to.not.exist();
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
### Editing directly
|
|
587
|
+
|
|
588
|
+
For work that is an action rather than a standing state, a plugin can use the `editor` service. It
|
|
589
|
+
only edits: it keeps no record, so the plugin undoes its own work.
|
|
590
|
+
|
|
591
|
+
```ts
|
|
592
|
+
const file = ctx.services.get("editor").open("${projectRoot}/.claude/settings.json");
|
|
593
|
+
const record = file.apply("ensureIncludes", { path: "permissions.allow", items: ["Bash(x:*)"] });
|
|
594
|
+
|
|
595
|
+
file.document.atPath("permissions.allow").get(); // read anything back
|
|
596
|
+
file.write(); // writes only when it changed
|
|
597
|
+
|
|
598
|
+
// Later, to undo it:
|
|
599
|
+
file.undo("ensureIncludes", { path: "permissions.allow", items: ["Bash(x:*)"] }, record);
|
|
600
|
+
```
|