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