@mindfoldhq/trellis 0.5.10 → 0.5.12

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.
@@ -26,37 +26,68 @@
26
26
  | `python3` command | ✅ Always available | ⚠️ May need `python` |
27
27
  | `python` command | ⚠️ May be Python 2 | ✅ Usually Python 3 |
28
28
 
29
- **Rule 1**: Always use explicit `python3` in documentation, help text, and error messages.
29
+ **Rule 1**: For user-facing docs, help text, and error messages, either:
30
+
31
+ - state the platform rule explicitly (`python` on Windows, `python3` elsewhere), or
32
+ - render the command through the same platform-aware helper / placeholder the code uses.
30
33
 
31
34
  ```python
32
35
  # BAD - Assumes shebang works
33
36
  print("Usage: ./script.py <args>")
34
37
  print("Run: script.py <args>")
35
38
 
36
- # GOOD - Explicit interpreter
37
- print("Usage: python3 script.py <args>")
38
- print("Run: python3 ./script.py <args>")
39
+ # GOOD - Platform-aware wording
40
+ print("Usage: python on Windows, python3 elsewhere")
41
+ print("Run: {{PYTHON_CMD}} ./.trellis/scripts/task.py <args>")
39
42
  ```
40
43
 
41
- **Rule 2**: When calling Python from TypeScript/Node.js, detect the available command:
44
+ **Rule 2**: When generating config files at init time, use placeholder + platform detection:
42
45
 
43
46
  ```typescript
47
+ // In template file (settings.json):
48
+ { "command": "{{PYTHON_CMD}} .claude/hooks/script.py" }
49
+
50
+ // In configurator:
44
51
  function getPythonCommand(): string {
52
+ return process.platform === "win32" ? "python" : "python3";
53
+ }
54
+
55
+ function replacePlaceholders(content: string): string {
56
+ return content.replace(/\{\{PYTHON_CMD\}\}/g, getPythonCommand());
57
+ }
58
+ ```
59
+
60
+ **Rule 3**: When calling Python at runtime from JavaScript, detect platform dynamically:
61
+
62
+ ```javascript
63
+ import { platform } from "os"
64
+
65
+ const PYTHON_CMD = platform() === "win32" ? "python" : "python3"
66
+ execSync(`${PYTHON_CMD} "${scriptPath}"`, { ... })
67
+ ```
68
+
69
+ **Rule 4**: If you need to verify Python is actually installed (not just choose
70
+ the command), probe the same platform-selected alias you will later render or
71
+ execute:
72
+
73
+ ```typescript
74
+ function getPythonCommand(platform = process.platform): string {
75
+ return platform === "win32" ? "python" : "python3";
76
+ }
77
+
78
+ function warnIfPythonTooOld(): void {
79
+ const cmd = getPythonCommand();
45
80
  try {
46
- execSync("python3 --version", { stdio: "pipe" });
47
- return "python3";
81
+ execSync(`${cmd} --version`, { stdio: "pipe" });
48
82
  } catch {
49
- try {
50
- execSync("python --version", { stdio: "pipe" });
51
- return "python";
52
- } catch {
53
- return "python3"; // Default, will fail with clear error
54
- }
83
+ // Missing Python is a separate error path; don't silently swap aliases.
55
84
  }
56
85
  }
57
86
  ```
58
87
 
59
- **Rule 3**: When calling Python from Python, use `sys.executable`:
88
+ **Rule 5**: Don't assume the Python version the AI CLI uses matches your shell's `python3`. The user's terminal may resolve `python3` → homebrew 3.11, but AI CLI hosts (including enterprise-forked Claude Code / Cursor distributions) spawn hook subprocesses with a minimal PATH that resolves `python3` → `/usr/bin/python3` → macOS system 3.9. Distributed templates must either target the lowest plausible version or use `from __future__ import annotations` for PEP 604 syntax. See `cli/backend/script-conventions.md` → **CRITICAL: PEP 604 Annotations Require `from __future__ import annotations`** for the hard rule and audit check.
89
+
90
+ **Rule 6**: When calling Python from Python, use `sys.executable`:
60
91
 
61
92
  ```python
62
93
  import sys
@@ -69,30 +100,6 @@ subprocess.run(["python3", "other_script.py"])
69
100
  subprocess.run([sys.executable, "other_script.py"])
70
101
  ```
71
102
 
72
- **Rule 4**: Don't assume the Python version your AI CLI uses matches your shell's `python3`. Your terminal may resolve `python3` → 3.11 (via homebrew/pyenv), but AI CLI hosts often spawn hook subprocesses with a minimal PATH that resolves `python3` → the system Python (3.9 on macOS). Any `.py` file run as an AI-CLI hook must be written for the lowest plausible Python version.
73
-
74
- Concrete failure: PEP 604 union syntax (`str | None`) requires Python 3.10+. If your hook file uses it, start with `from __future__ import annotations` so annotations become lazy strings and work on Python 3.7+:
75
-
76
- ```python
77
- #!/usr/bin/env python3
78
- """My hook."""
79
- from __future__ import annotations # REQUIRED for PEP 604 annotations
80
-
81
- def handler(x: str | None) -> dict | None: # OK — lazy annotation
82
- ...
83
- ```
84
-
85
- ```python
86
- # BAD — crashes on Python < 3.10:
87
- # TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
88
- def handler(x: str | None) -> dict | None:
89
- ...
90
- ```
91
-
92
- Note: `from __future__ import annotations` only covers **annotations**. Runtime expressions like `isinstance(x, int | str)` still require Python 3.10+. Avoid them in hook scripts.
93
-
94
- Applies to anything the AI CLI executes as a hook: `match/case` statements (3.10+), `tomllib` (3.11+), `ExceptionGroup` / `except*` (3.11+) — all crash on older Python regardless of `__future__`.
95
-
96
103
  ### 2. Path Handling
97
104
 
98
105
  | Assumption | macOS/Linux | Windows |
@@ -101,7 +108,7 @@ Applies to anything the AI CLI executes as a hook: `match/case` statements (3.10
101
108
  | `\` separator | ❌ Escape char | ✅ Native |
102
109
  | `pathlib.Path` | ✅ Works | ✅ Works |
103
110
 
104
- **Rule**: Use `pathlib.Path` for all path operations.
111
+ **Rule (Python)**: Use `pathlib.Path` for all path operations.
105
112
 
106
113
  ```python
107
114
  # BAD - String concatenation
@@ -112,6 +119,51 @@ from pathlib import Path
112
119
  path = Path(base) / filename
113
120
  ```
114
121
 
122
+ #### Logical key vs filesystem path (TypeScript)
123
+
124
+ A path string has two distinct roles. **Treat them differently.**
125
+
126
+ | Role | OS-native (`\` on Windows) | Always POSIX (`/`) |
127
+ |------|---------------------------|--------------------|
128
+ | `fs.readFileSync(p)` / `path.join(cwd, x)` for fs call | ✅ Required | ❌ May fail on Windows |
129
+ | `Map<relPath, content>` key, JSON field, hash dictionary key, anything persisted across OS | ❌ Cross-OS mismatch | ✅ Required |
130
+
131
+ **Rule**: Anywhere a path string crosses OS or persists (Map keys consumed by another OS, JSON fields, hash dictionary keys), normalize to POSIX. Anywhere it goes straight to `fs.*`, leave OS-native.
132
+
133
+ **Single source of truth**: `packages/cli/src/utils/posix.ts` exports `toPosix(p)`. Don't sprinkle `replaceAll('\\', '/')` at every `path.join` site — apply `toPosix` **once at the boundary**: collector exit (Map key entering hash dictionary) or write-time (`saveHashes` before `JSON.stringify`).
134
+
135
+ ```typescript
136
+ // BAD - logical key carries OS-native separator
137
+ function collectTemplates(): Map<string, string> {
138
+ const files = new Map<string, string>();
139
+ for (const entry of walk(dir)) {
140
+ files.set(path.join(".opencode", entry), readFile(entry)); // \ on Windows
141
+ }
142
+ return files;
143
+ }
144
+
145
+ // GOOD - normalize at the boundary
146
+ import { toPosix } from "../utils/posix.js";
147
+
148
+ function collectTemplates(): Map<string, string> {
149
+ const files = new Map<string, string>();
150
+ for (const entry of walk(dir)) {
151
+ files.set(toPosix(path.join(".opencode", entry)), readFile(entry));
152
+ }
153
+ return files;
154
+ }
155
+
156
+ // ALSO ACCEPTABLE - write-side defense (for storage helpers like saveHashes)
157
+ function saveHashes(cwd: string, hashes: Record<string, string>): void {
158
+ const normalized = Object.fromEntries(
159
+ Object.entries(hashes).map(([k, v]) => [toPosix(k), v])
160
+ );
161
+ fs.writeFileSync(getHashesPath(cwd), JSON.stringify(normalized, null, 2));
162
+ }
163
+ ```
164
+
165
+ **Common offender**: `path.relative(cwd, fullPath)` produces `\` on Windows. If you then use that string as a hash dictionary lookup key (`hashes[relPath]`), `toPosix` it first, or the lookup misses on Windows.
166
+
115
167
  ### 3. Line Endings
116
168
 
117
169
  | Format | macOS/Linux | Windows | Git |
@@ -119,7 +171,7 @@ path = Path(base) / filename
119
171
  | `\n` (LF) | ✅ Native | ⚠️ Some tools | ✅ Normalized |
120
172
  | `\r\n` (CRLF) | ⚠️ Extra char | ✅ Native | Converted |
121
173
 
122
- **Rule**: Use `.gitattributes` to enforce consistent line endings.
174
+ **Rule 1**: Use `.gitattributes` to enforce consistent line endings.
123
175
 
124
176
  ```gitattributes
125
177
  * text=auto eol=lf
@@ -127,6 +179,23 @@ path = Path(base) / filename
127
179
  *.py text eol=lf
128
180
  ```
129
181
 
182
+ **Rule 2**: When hashing or comparing **content** across platforms, normalize line endings before computing the hash. `.gitattributes` only governs git checkout — files written by users, scripts, or `core.autocrlf=true` may still arrive as CRLF, and `sha256(LF)` ≠ `sha256(CRLF)` for otherwise-identical content.
183
+
184
+ ```typescript
185
+ // BAD - Windows users with autocrlf=true get a different hash
186
+ export function computeHash(content: string): string {
187
+ return createHash("sha256").update(content, "utf-8").digest("hex");
188
+ }
189
+
190
+ // GOOD - normalize before hashing so logical content hashes identically
191
+ export function computeHash(content: string): string {
192
+ const normalized = content.replace(/\r\n/g, "\n");
193
+ return createHash("sha256").update(normalized, "utf-8").digest("hex");
194
+ }
195
+ ```
196
+
197
+ Apply this rule wherever the hash crosses OS boundaries (template hash dictionary, content fingerprints stored in JSON, integrity checks against a remote registry).
198
+
130
199
  ### 4. Environment Variables
131
200
 
132
201
  | Variable | macOS/Linux | Windows |
@@ -135,7 +204,7 @@ path = Path(base) / filename
135
204
  | `PATH` separator | `:` | `;` |
136
205
  | Case sensitivity | ✅ Case-sensitive | ❌ Case-insensitive |
137
206
 
138
- **Rule**: Use `pathlib.Path.home()` instead of environment variables.
207
+ **Rule 1**: Use `pathlib.Path.home()` instead of environment variables.
139
208
 
140
209
  ```python
141
210
  # BAD
@@ -145,6 +214,25 @@ home = os.environ.get("HOME")
145
214
  home = Path.home()
146
215
  ```
147
216
 
217
+ **Rule 2**: When injecting environment variables into shell commands, generate
218
+ the prefix for the actual host shell. Do not assume `export` works everywhere.
219
+ AI tool "Bash" surfaces on Windows may execute through PowerShell.
220
+
221
+ ```javascript
222
+ // BAD - breaks when the host shell is PowerShell
223
+ command = `export TRELLIS_CONTEXT_ID=${shellQuote(contextKey)}; ${command}`;
224
+
225
+ // GOOD - shell-aware command prefix
226
+ const prefix = process.platform === "win32"
227
+ ? `$env:TRELLIS_CONTEXT_ID = ${powershellQuote(contextKey)}; `
228
+ : `export TRELLIS_CONTEXT_ID=${shellQuote(contextKey)}; `;
229
+ command = `${prefix}${command}`;
230
+ ```
231
+
232
+ Also make duplicate-injection detection shell-aware. A guard that only matches
233
+ `export VAR=` will miss PowerShell's `$env:VAR = ...` form and can wrap an
234
+ already-correct command a second time.
235
+
148
236
  ### 5. Command Availability
149
237
 
150
238
  | Command | macOS/Linux | Windows |
@@ -173,6 +261,25 @@ def tail_follow(file_path: Path) -> None:
173
261
  time.sleep(0.1)
174
262
  ```
175
263
 
264
+ ### Optional Advisory Checks in Agent Sandboxes
265
+
266
+ AI CLI subprocesses may run with outbound network disabled even when the user's
267
+ normal terminal has network access. Prefer local CLI probes over optional
268
+ network probes when the local CLI already exposes the needed information.
269
+
270
+ **Rule 1**: Do not let a failed optional advisory check consume a once-per-session
271
+ marker. Write the marker only after the script resolves a usable value and can
272
+ make the intended decision. Otherwise a transient sandbox/network failure hides
273
+ the hint for the rest of the session.
274
+
275
+ **Rule 2**: If a local command can provide the needed value, try it with a short
276
+ timeout and captured output. For example, `trellis --version` already runs the
277
+ CLI's version comparison logic and can support an actionable update prompt
278
+ without duplicating npm registry parsing.
279
+
280
+ **Rule 3**: Keep advisory checks silent on failure. The user-facing context output
281
+ must not fail or become noisy because an advisory check could not complete.
282
+
176
283
  ### 6. File Encoding
177
284
 
178
285
  | Default Encoding | macOS/Linux | Windows |
@@ -183,6 +290,9 @@ def tail_follow(file_path: Path) -> None:
183
290
 
184
291
  **Rule**: Always explicitly specify `encoding="utf-8"` and use `errors="replace"`.
185
292
 
293
+ > **Checklist**: When writing scripts that print non-ASCII, did you configure stdout encoding?
294
+ > See `backend/script-conventions.md` for the specific pattern.
295
+
186
296
  ```python
187
297
  # BAD - Relies on system default
188
298
  with open(file, "r") as f:
@@ -223,6 +333,12 @@ result = subprocess.run(
223
333
 
224
334
  When making platform-related changes, check **all these locations**:
225
335
 
336
+ ### Commands / Skills Sync
337
+ - [ ] New command/skill added to ALL platforms (claude, cursor, iflow, codex, and any new platform)
338
+ - [ ] Each platform's test file updated with new entry in `EXPECTED_COMMAND_NAMES` / `EXPECTED_SKILL_NAMES`
339
+ - [ ] Platform-integration spec's required command table updated if adding a new required command
340
+ - [ ] Command format matches platform convention (see `platform-integration.md` → Command Format by Platform)
341
+
226
342
  ### Documentation & Help Text
227
343
  - [ ] Docstrings at top of Python files
228
344
  - [ ] `--help` output / argparse descriptions
@@ -239,7 +355,7 @@ When making platform-related changes, check **all these locations**:
239
355
  ```bash
240
356
  # Find all places that might need updating
241
357
  grep -r "python [a-z]" --include="*.py" --include="*.md"
242
- grep -r "\./" --include="*.py" --include="*.md" | grep -v python3
358
+ grep -r "{{PYTHON_CMD}}\\|python3\\|python " --include="*.py" --include="*.md"
243
359
  ```
244
360
 
245
361
  ---
@@ -248,10 +364,15 @@ grep -r "\./" --include="*.py" --include="*.md" | grep -v python3
248
364
 
249
365
  Before committing cross-platform code:
250
366
 
251
- - [ ] All Python invocations use `python3` explicitly (docs) or `sys.executable` (code)
367
+ - [ ] User-facing Python invocations are platform-aware (`python` on Windows, `python3` elsewhere) or use `{{PYTHON_CMD}}`
368
+ - [ ] Python subprocesses from Python use `sys.executable`
252
369
  - [ ] All paths use `pathlib.Path`
253
370
  - [ ] No hardcoded path separators (`/` or `\`)
371
+ - [ ] Path strings used as logical/persisted keys (Map keys, JSON fields, hash dictionary keys) are normalized via `toPosix()`; `fs.*` calls keep OS-native paths
372
+ - [ ] Content hashes computed across OSes normalize line endings (`\r\n` → `\n`) before hashing
373
+ - [ ] Cross-OS JSON with potential legacy pollution carries a `__version` sentinel and the loader discards unknown/legacy versions
254
374
  - [ ] No platform-specific commands without fallbacks (e.g., `tail -f`)
375
+ - [ ] Optional advisory checks do not burn once-per-session markers on failure
255
376
  - [ ] All file I/O specifies `encoding="utf-8"` and `errors="replace"`
256
377
  - [ ] All subprocess calls specify `encoding="utf-8"` and `errors="replace"`
257
378
  - [ ] Git commands use `-c i18n.logOutputEncoding=UTF-8`
@@ -283,6 +404,101 @@ output = {
283
404
 
284
405
  ---
285
406
 
407
+ ## Cross-Platform Persisted JSON: Schema Migration Sentinel
408
+
409
+ When a JSON file may be read/written across OSes (committed to git, synced via cloud, copied between machines) **and an older format may already exist on user disks with cross-platform pollution** (Windows-style keys, CRLF-derived hashes, locale-encoded strings), add a `__version` sentinel and let the loader discard old formats so the writer regenerates clean data.
410
+
411
+ **Why not migrate-in-place?** Path-key migration (`\\` → `/`) plus hash-input migration (CRLF → LF re-hash) plus encoding fixes are correlated — trying to translate the old payload risks producing wrong values. Discarding and regenerating is **safe**: the data is recomputable from disk, and `loadX` returning `{}` triggers the existing init/update path to rebuild canonical entries.
412
+
413
+ ```typescript
414
+ const SCHEMA_VERSION = 2;
415
+ type StoredV2 = { __version: number; hashes: Record<string, string> };
416
+
417
+ export function loadHashes(cwd: string): Record<string, string> {
418
+ const file = getHashesPath(cwd);
419
+ if (!fs.existsSync(file)) return {};
420
+
421
+ try {
422
+ const parsed = JSON.parse(fs.readFileSync(file, "utf-8")) as unknown;
423
+
424
+ // Reject legacy flat format (no __version) and unknown versions.
425
+ // The next saveHashes / initializeHashes will write a fresh v2 file.
426
+ if (
427
+ !parsed ||
428
+ typeof parsed !== "object" ||
429
+ (parsed as StoredV2).__version !== SCHEMA_VERSION ||
430
+ typeof (parsed as StoredV2).hashes !== "object"
431
+ ) {
432
+ return {};
433
+ }
434
+ return (parsed as StoredV2).hashes;
435
+ } catch {
436
+ return {};
437
+ }
438
+ }
439
+
440
+ export function saveHashes(cwd: string, hashes: Record<string, string>): void {
441
+ const payload: StoredV2 = { __version: SCHEMA_VERSION, hashes };
442
+ fs.writeFileSync(getHashesPath(cwd), JSON.stringify(payload, null, 2));
443
+ }
444
+ ```
445
+
446
+ **When to apply**:
447
+ - Hash dictionaries / content fingerprints (e.g., `.template-hashes.json`)
448
+ - Cache files where stale entries are recomputable from authoritative source
449
+ - Any cross-OS persisted file where format change correlates with cross-platform fixes
450
+
451
+ **When NOT to apply** — if losing the data hurts the user (task state, drafts, settings the user typed). Use real migration there. Sentinel + discard is only safe when data is recomputable.
452
+
453
+ **Reference**: `packages/cli/src/utils/template-hash.ts` v2 envelope.
454
+
455
+ ---
456
+
457
+ ## JSON/External Data Defensive Checks
458
+
459
+ When parsing JSON or external data, TypeScript types are **compile-time only**. Runtime data may not match.
460
+
461
+ **Rule**: Always add defensive checks for required fields before using them.
462
+
463
+ ```typescript
464
+ // BAD - Trusts TypeScript type definition
465
+ interface MigrationItem {
466
+ from: string; // TypeScript says required
467
+ to?: string;
468
+ }
469
+
470
+ function process(item: MigrationItem) {
471
+ const path = item.from; // Runtime: could be undefined!
472
+ }
473
+
474
+ // GOOD - Defensive check before use
475
+ function process(item: MigrationItem) {
476
+ if (!item.from) return; // Skip invalid data
477
+ const path = item.from; // Now guaranteed
478
+ }
479
+ ```
480
+
481
+ **When to apply**:
482
+ - Parsing JSON files (manifests, configs)
483
+ - API responses
484
+ - User input
485
+ - Any data from external sources
486
+
487
+ **Pattern**: Check existence → then use
488
+
489
+ ```typescript
490
+ // Filter pattern - skip invalid items
491
+ const validItems = items.filter(item => item.from && item.to);
492
+
493
+ // Early return pattern - bail on invalid
494
+ if (!data.requiredField) {
495
+ console.warn("Missing required field");
496
+ return defaultValue;
497
+ }
498
+ ```
499
+
500
+ ---
501
+
286
502
  ## Common Mistakes
287
503
 
288
504
  ### 1. "It works on my Mac"
@@ -318,6 +534,9 @@ python3 script.py # Works!
318
534
  # User's Windows (Python from python.org)
319
535
  python3 script.py # 'python3' is not recognized
320
536
  python script.py # Works!
537
+
538
+ # Trellis docs/config should say the rule, not guess one alias everywhere
539
+ {{PYTHON_CMD}} script.py
321
540
  ```
322
541
 
323
542
  ### 5. "UTF-8 is the default everywhere"
@@ -328,6 +547,9 @@ subprocess.run(cmd, capture_output=True, text=True) # Works!
328
547
 
329
548
  # User's Windows (GBK/CP1252 default)
330
549
  subprocess.run(cmd, capture_output=True, text=True) # Garbled Chinese/Unicode
550
+ ```
551
+
552
+ > **Note**: stdout encoding is also affected. See `backend/script-conventions.md` for the fix.
331
553
 
332
554
  ---
333
555
 
@@ -341,3 +563,27 @@ subprocess.run(cmd, capture_output=True, text=True) # Garbled Chinese/Unicode
341
563
  ---
342
564
 
343
565
  **Core Principle**: If it's not explicit, it's an assumption. And assumptions break.
566
+
567
+ ---
568
+
569
+ ## Release Checklist: Versioned Files
570
+
571
+ When releasing a new version, ensure **all versioned files** are created/updated:
572
+
573
+ - [ ] `src/migrations/manifests/{version}.json` - Migration manifest exists
574
+ - [ ] Manifest has correct version, description, changelog
575
+ - [ ] `pnpm build` copies manifests to `dist/`
576
+ - [ ] Test upgrade path from older versions (not just adjacent)
577
+
578
+ **Why this matters**: Missing manifests cause "path undefined" errors when users upgrade from older versions.
579
+
580
+ ```bash
581
+ # Verify all expected manifests exist
582
+ ls src/migrations/manifests/
583
+
584
+ # Test upgrade path
585
+ node -e "
586
+ const { getMigrationsForVersion } = require('./dist/migrations/index.js');
587
+ console.log('From 0.2.12:', getMigrationsForVersion('0.2.12', 'CURRENT').length);
588
+ "
589
+ ```
@@ -14,6 +14,24 @@ session_commit_message: "chore: record journal"
14
14
  # Maximum lines per journal file before rotating to a new one
15
15
  max_journal_lines: 2000
16
16
 
17
+ #-------------------------------------------------------------------------------
18
+ # Session Auto-Commit
19
+ #-------------------------------------------------------------------------------
20
+
21
+ # Auto-commit behavior for session journal + task archive operations.
22
+ # - true (default): scripts auto-stage and auto-commit journal / task changes
23
+ # after add_session.py / task.py archive runs.
24
+ # - false: scripts do not touch git. Files (journal-*.md, task archive moves)
25
+ # are still written to disk; you decide whether to git add / commit.
26
+ #
27
+ # Use `false` if your project's .gitignore intentionally excludes `.trellis/`
28
+ # and you want session data kept local-only, or if you prefer to review
29
+ # staged changes manually before each commit.
30
+ #
31
+ # Accepts: true / false / yes / no / 1 / 0 / on / off (case-insensitive).
32
+ #
33
+ # session_auto_commit: true
34
+
17
35
  #-------------------------------------------------------------------------------
18
36
  # Task Lifecycle Hooks
19
37
  #-------------------------------------------------------------------------------
@@ -44,6 +44,7 @@ from common.safe_commit import (
44
44
  from common.tasks import load_task
45
45
  from common.config import (
46
46
  get_packages,
47
+ get_session_auto_commit,
47
48
  get_session_commit_message,
48
49
  get_max_journal_lines,
49
50
  is_monorepo,
@@ -322,16 +323,27 @@ def _auto_commit_workspace(repo_root: Path) -> None:
322
323
 
323
324
  Path scope is restricted to specific products (journal files, index.md,
324
325
  active task dirs, the archive subtree). We never `git add` the whole
325
- `.trellis/` tree, and if `.gitignore` blocks the specific paths we retry
326
- with `git add -f <those-specific-paths>` — never `-f .trellis/`.
326
+ `.trellis/` tree, and if `.gitignore` blocks the specific paths we
327
+ warn + skip — never retry with ``-f``.
328
+
329
+ Honors ``session_auto_commit`` in ``.trellis/config.yaml``: when set to
330
+ ``false``, this function returns immediately without touching git
331
+ (journal/index files are still written to disk by the caller).
327
332
  """
333
+ if not get_session_auto_commit(repo_root):
334
+ print(
335
+ "[OK] session_auto_commit: false — skipping git stage/commit.",
336
+ file=sys.stderr,
337
+ )
338
+ return
339
+
328
340
  commit_msg = get_session_commit_message(repo_root)
329
341
  paths = safe_trellis_paths_to_add(repo_root)
330
342
  if not paths:
331
343
  print("[OK] No workspace changes to commit.", file=sys.stderr)
332
344
  return
333
345
 
334
- success, used_force, err = safe_git_add(paths, repo_root)
346
+ success, _, err = safe_git_add(paths, repo_root)
335
347
  if not success:
336
348
  if err and "ignored by" in err.lower():
337
349
  print_gitignore_warning(paths)
@@ -342,12 +354,6 @@ def _auto_commit_workspace(repo_root: Path) -> None:
342
354
  )
343
355
  return
344
356
 
345
- if used_force:
346
- print(
347
- "[OK] Staged Trellis-owned paths with -f (specific paths, not .trellis/).",
348
- file=sys.stderr,
349
- )
350
-
351
357
  # Check if there are staged changes for the paths we just staged.
352
358
  rc, _, _ = run_git(
353
359
  ["diff", "--cached", "--quiet", "--", *paths], cwd=repo_root
@@ -36,6 +36,29 @@ def _unquote(s: str) -> str:
36
36
  return s
37
37
 
38
38
 
39
+ def _strip_inline_comment(value: str) -> str:
40
+ """Strip ` # …` inline comments while preserving `#` inside quoted strings.
41
+
42
+ YAML treats ` #` (space-hash) as a comment opener; bare `#` inside a token
43
+ is part of the value. Quoted strings are immune.
44
+
45
+ Mirrors :func:`common.trellis_config._strip_inline_comment` so both
46
+ parsers handle ``key: value # comment`` identically.
47
+ """
48
+ in_quote: str | None = None
49
+ for idx, ch in enumerate(value):
50
+ if in_quote:
51
+ if ch == in_quote:
52
+ in_quote = None
53
+ continue
54
+ if ch in ('"', "'"):
55
+ in_quote = ch
56
+ continue
57
+ if ch == "#" and (idx == 0 or value[idx - 1].isspace()):
58
+ return value[:idx]
59
+ return value
60
+
61
+
39
62
  def parse_simple_yaml(content: str) -> dict:
40
63
  """Parse simple YAML with nested dict support (no dependencies).
41
64
 
@@ -93,7 +116,8 @@ def _parse_yaml_block(
93
116
  elif ":" in stripped:
94
117
  key, _, value = stripped.partition(":")
95
118
  key = key.strip()
96
- value = _unquote(value.strip())
119
+ value = _strip_inline_comment(value).strip()
120
+ value = _unquote(value)
97
121
  current_list = None
98
122
 
99
123
  if value:
@@ -142,6 +166,7 @@ def _next_content_line(lines: list[str], start: int) -> tuple[int, str]:
142
166
  # Defaults
143
167
  DEFAULT_SESSION_COMMIT_MESSAGE = "chore: record journal"
144
168
  DEFAULT_MAX_JOURNAL_LINES = 2000
169
+ DEFAULT_SESSION_AUTO_COMMIT = True
145
170
 
146
171
  CONFIG_FILE = "config.yaml"
147
172
 
@@ -187,6 +212,37 @@ def get_max_journal_lines(repo_root: Path | None = None) -> int:
187
212
  return DEFAULT_MAX_JOURNAL_LINES
188
213
 
189
214
 
215
+ def get_session_auto_commit(repo_root: Path | None = None) -> bool:
216
+ """Whether scripts should auto-stage + auto-commit session/task changes.
217
+
218
+ Governs both ``add_session.py:_auto_commit_workspace`` and
219
+ ``task_store.py:_auto_commit_archive``.
220
+
221
+ Default: ``True`` (existing behavior — auto-stage + auto-commit).
222
+ Set ``session_auto_commit: false`` in ``.trellis/config.yaml`` to skip
223
+ auto-staging entirely; the journal/archive files are still written to
224
+ disk, but the user manages ``git add`` / ``git commit`` themselves.
225
+
226
+ Accepts native YAML booleans (``true`` / ``false``) and the string
227
+ aliases ``true / false / yes / no / 1 / 0 / on / off`` (case-insensitive).
228
+ Invalid values fall back to ``True`` with a stderr warning.
229
+ """
230
+ config = _load_config(repo_root)
231
+ raw = config.get("session_auto_commit", DEFAULT_SESSION_AUTO_COMMIT)
232
+ if isinstance(raw, bool):
233
+ return raw
234
+ s = str(raw).strip().lower()
235
+ if s in ("true", "yes", "1", "on"):
236
+ return True
237
+ if s in ("false", "no", "0", "off"):
238
+ return False
239
+ print(
240
+ f"[WARN] invalid session_auto_commit value: {raw!r}; using true (default)",
241
+ file=sys.stderr,
242
+ )
243
+ return DEFAULT_SESSION_AUTO_COMMIT
244
+
245
+
190
246
  def get_hooks(event: str, repo_root: Path | None = None) -> list[str]:
191
247
  """Get hook commands for a lifecycle event.
192
248