@yan-geroge/omg 0.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.
Files changed (59) hide show
  1. package/.claude/agents/trellis-check.md +109 -0
  2. package/.claude/agents/trellis-implement.md +109 -0
  3. package/.claude/agents/trellis-research.md +137 -0
  4. package/.claude/commands/trellis/continue.md +55 -0
  5. package/.claude/commands/trellis/finish-work.md +66 -0
  6. package/.claude/hooks/inject-subagent-context.py +749 -0
  7. package/.claude/hooks/inject-workflow-state.py +387 -0
  8. package/.claude/hooks/session-start.py +797 -0
  9. package/.claude/settings.json +73 -0
  10. package/.claude/skills/trellis-before-dev/SKILL.md +34 -0
  11. package/.claude/skills/trellis-brainstorm/SKILL.md +548 -0
  12. package/.claude/skills/trellis-break-loop/SKILL.md +130 -0
  13. package/.claude/skills/trellis-check/SKILL.md +92 -0
  14. package/.claude/skills/trellis-meta/SKILL.md +73 -0
  15. package/.claude/skills/trellis-meta/references/customize-local/add-project-local-conventions.md +83 -0
  16. package/.claude/skills/trellis-meta/references/customize-local/change-agents.md +54 -0
  17. package/.claude/skills/trellis-meta/references/customize-local/change-context-loading.md +81 -0
  18. package/.claude/skills/trellis-meta/references/customize-local/change-hooks.md +57 -0
  19. package/.claude/skills/trellis-meta/references/customize-local/change-skills-or-commands.md +78 -0
  20. package/.claude/skills/trellis-meta/references/customize-local/change-spec-structure.md +83 -0
  21. package/.claude/skills/trellis-meta/references/customize-local/change-task-lifecycle.md +90 -0
  22. package/.claude/skills/trellis-meta/references/customize-local/change-workflow.md +64 -0
  23. package/.claude/skills/trellis-meta/references/customize-local/overview.md +55 -0
  24. package/.claude/skills/trellis-meta/references/local-architecture/context-injection.md +68 -0
  25. package/.claude/skills/trellis-meta/references/local-architecture/generated-files.md +80 -0
  26. package/.claude/skills/trellis-meta/references/local-architecture/overview.md +51 -0
  27. package/.claude/skills/trellis-meta/references/local-architecture/spec-system.md +102 -0
  28. package/.claude/skills/trellis-meta/references/local-architecture/task-system.md +101 -0
  29. package/.claude/skills/trellis-meta/references/local-architecture/workflow.md +75 -0
  30. package/.claude/skills/trellis-meta/references/local-architecture/workspace-memory.md +71 -0
  31. package/.claude/skills/trellis-meta/references/platform-files/agents.md +79 -0
  32. package/.claude/skills/trellis-meta/references/platform-files/hooks-and-settings.md +69 -0
  33. package/.claude/skills/trellis-meta/references/platform-files/overview.md +59 -0
  34. package/.claude/skills/trellis-meta/references/platform-files/platform-map.md +74 -0
  35. package/.claude/skills/trellis-meta/references/platform-files/skills-and-commands.md +83 -0
  36. package/.claude/skills/trellis-spec-bootstarp/SKILL.md +41 -0
  37. package/.claude/skills/trellis-spec-bootstarp/references/mcp-setup.md +90 -0
  38. package/.claude/skills/trellis-spec-bootstarp/references/repository-analysis.md +59 -0
  39. package/.claude/skills/trellis-spec-bootstarp/references/spec-task-planning.md +61 -0
  40. package/.claude/skills/trellis-spec-bootstarp/references/spec-writing.md +70 -0
  41. package/.claude/skills/trellis-update-spec/SKILL.md +356 -0
  42. package/CLAUDE.md +88 -0
  43. package/agents/architect.md +80 -0
  44. package/agents/executor.md +79 -0
  45. package/agents/planner.md +76 -0
  46. package/agents/reviewer.md +81 -0
  47. package/agents/tdd-guide.md +95 -0
  48. package/agents/verifier.md +81 -0
  49. package/hooks/keyword-detector.mjs +115 -0
  50. package/hooks/lib/keyword-detector.js +89 -0
  51. package/hooks/session-start.mjs +104 -0
  52. package/marketplace.json +12 -0
  53. package/package.json +34 -0
  54. package/plugin.json +14 -0
  55. package/scripts/diagnose-marketplace.js +84 -0
  56. package/scripts/e2e-diagnose.mjs +118 -0
  57. package/scripts/validate-with-csc-schema.mjs +87 -0
  58. package/skills/costrict-autopilot/SKILL.md +53 -0
  59. package/skills/costrict-team/SKILL.md +65 -0
@@ -0,0 +1,797 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Session Start Hook - Inject structured context
5
+ """
6
+ from __future__ import annotations
7
+
8
+ # IMPORTANT: Suppress all warnings FIRST
9
+ import warnings
10
+ warnings.filterwarnings("ignore")
11
+
12
+ import json
13
+ import os
14
+ import re
15
+ import shlex
16
+ import subprocess
17
+ import sys
18
+ from io import StringIO
19
+ from pathlib import Path
20
+
21
+
22
+ def _normalize_windows_shell_path(path_str: str) -> str:
23
+ """Normalize Unix-style shell paths to real Windows paths.
24
+
25
+ On Windows, shells like Git Bash / MSYS2 / Cygwin may report paths like
26
+ `/d/Users/...` or `/cygdrive/d/Users/...`. `Path.resolve()` will misinterpret
27
+ these as `D:/d/Users...` on drive D: (or similar), breaking repo root
28
+ detection.
29
+
30
+ This function is intentionally conservative: it only rewrites patterns that
31
+ unambiguously represent a drive letter mount.
32
+ """
33
+ if not isinstance(path_str, str) or not path_str:
34
+ return path_str
35
+
36
+ # Only relevant on Windows; keep other platforms untouched.
37
+ if not sys.platform.startswith("win"):
38
+ return path_str
39
+
40
+ p = path_str.strip()
41
+
42
+ # Already a Windows drive path (C:\... or C:/...)
43
+ if re.match(r"^[A-Za-z]:[\/]", p):
44
+ return p
45
+
46
+ # MSYS/Git-Bash style: /c/Users/... or /d/Work/...
47
+ m = re.match(r"^/([A-Za-z])/(.*)", p)
48
+ if m:
49
+ drive, rest = m.group(1).upper(), m.group(2)
50
+ rest = rest.replace('/', '\\')
51
+ return f"{drive}:\\{rest}"
52
+
53
+ # Cygwin style: /cygdrive/c/Users/...
54
+ m = re.match(r"^/cygdrive/([A-Za-z])/(.*)", p)
55
+ if m:
56
+ drive, rest = m.group(1).upper(), m.group(2)
57
+ rest = rest.replace('/', '\\')
58
+ return f"{drive}:\\{rest}"
59
+
60
+ # WSL mounted drive (sometimes leaked into env): /mnt/c/Users/...
61
+ m = re.match(r"^/mnt/([A-Za-z])/(.*)", p)
62
+ if m:
63
+ drive, rest = m.group(1).upper(), m.group(2)
64
+ rest = rest.replace('/', '\\')
65
+ return f"{drive}:\\{rest}"
66
+
67
+ return path_str
68
+
69
+
70
+ FIRST_REPLY_NOTICE = """<first-reply-notice>
71
+ On the first visible assistant reply in this session, begin with exactly one short Chinese sentence:
72
+ Trellis SessionStart 已注入:workflow、当前任务状态、开发者身份、git 状态、active tasks、spec 索引已加载。
73
+ Then continue directly with the user's request. This notice is one-shot: do not repeat it after the first assistant reply in the same session.
74
+ </first-reply-notice>"""
75
+
76
+ # Force UTF-8 on stdin/stdout/stderr on Windows. Default codepage there is
77
+ # cp936 / cp1252 / etc. — non-ASCII content (Chinese task names, prd snippets)
78
+ # both in stdin (hook payload from host CLI) and stdout (our emitted blocks)
79
+ # raises UnicodeDecodeError / UnicodeEncodeError. Equivalent to `python -X utf8`
80
+ # but applied per-stream so we don't depend on host CLI's command wiring.
81
+ if sys.platform.startswith("win"):
82
+ import io as _io
83
+ for _stream_name in ("stdin", "stdout", "stderr"):
84
+ _stream = getattr(sys, _stream_name, None)
85
+ if _stream is None:
86
+ continue
87
+ if hasattr(_stream, "reconfigure"):
88
+ try:
89
+ _stream.reconfigure(encoding="utf-8", errors="replace") # type: ignore[union-attr]
90
+ except Exception:
91
+ pass
92
+ elif hasattr(_stream, "detach"):
93
+ try:
94
+ setattr(sys, _stream_name, _io.TextIOWrapper(_stream.detach(), encoding="utf-8", errors="replace"))
95
+ except Exception:
96
+ pass
97
+
98
+
99
+
100
+ def _has_curated_jsonl_entry(jsonl_path: Path) -> bool:
101
+ """Return True iff jsonl has at least one row with a ``file`` field.
102
+
103
+ A freshly seeded jsonl only contains a ``{"_example": ...}`` row (no
104
+ ``file`` key) — that is NOT "ready". Readiness requires at least one
105
+ curated entry. Matches the contract used by hook-inject and pull-based
106
+ sub-agent context loaders.
107
+ """
108
+ try:
109
+ for line in jsonl_path.read_text(encoding="utf-8").splitlines():
110
+ line = line.strip()
111
+ if not line:
112
+ continue
113
+ try:
114
+ row = json.loads(line)
115
+ except json.JSONDecodeError:
116
+ continue
117
+ if isinstance(row, dict) and row.get("file"):
118
+ return True
119
+ except (OSError, UnicodeDecodeError):
120
+ return False
121
+ return False
122
+
123
+
124
+ def should_skip_injection() -> bool:
125
+ """Check if any platform's non-interactive flag is set, or if Trellis
126
+ hooks are explicitly disabled via TRELLIS_HOOKS=0 / TRELLIS_DISABLE_HOOKS=1.
127
+ """
128
+ if os.environ.get("TRELLIS_HOOKS") == "0":
129
+ return True
130
+ if os.environ.get("TRELLIS_DISABLE_HOOKS") == "1":
131
+ return True
132
+ non_interactive_vars = [
133
+ "CLAUDE_NON_INTERACTIVE",
134
+ "QODER_NON_INTERACTIVE",
135
+ "CODEBUDDY_NON_INTERACTIVE",
136
+ "FACTORY_NON_INTERACTIVE",
137
+ "CURSOR_NON_INTERACTIVE",
138
+ "GEMINI_NON_INTERACTIVE",
139
+ "KIRO_NON_INTERACTIVE",
140
+ "COPILOT_NON_INTERACTIVE",
141
+ ]
142
+ return any(os.environ.get(var) == "1" for var in non_interactive_vars)
143
+
144
+
145
+ def read_file(path: Path, fallback: str = "") -> str:
146
+ try:
147
+ return path.read_text(encoding="utf-8")
148
+ except (FileNotFoundError, PermissionError):
149
+ return fallback
150
+
151
+
152
+ def _detect_platform(input_data: dict) -> str | None:
153
+ if isinstance(input_data.get("cursor_version"), str):
154
+ return "cursor"
155
+ env_map = {
156
+ "CLAUDE_PROJECT_DIR": "claude",
157
+ "CURSOR_PROJECT_DIR": "cursor",
158
+ "CODEBUDDY_PROJECT_DIR": "codebuddy",
159
+ "FACTORY_PROJECT_DIR": "droid",
160
+ "GEMINI_PROJECT_DIR": "gemini",
161
+ "QODER_PROJECT_DIR": "qoder",
162
+ "KIRO_PROJECT_DIR": "kiro",
163
+ "COPILOT_PROJECT_DIR": "copilot",
164
+ }
165
+ for env_name, platform in env_map.items():
166
+ if os.environ.get(env_name):
167
+ return platform
168
+ script_parts = set(Path(sys.argv[0]).parts)
169
+ if ".claude" in script_parts:
170
+ return "claude"
171
+ if ".cursor" in script_parts:
172
+ return "cursor"
173
+ if ".codex" in script_parts:
174
+ return "codex"
175
+ if ".gemini" in script_parts:
176
+ return "gemini"
177
+ if ".qoder" in script_parts:
178
+ return "qoder"
179
+ if ".codebuddy" in script_parts:
180
+ return "codebuddy"
181
+ if ".factory" in script_parts:
182
+ return "droid"
183
+ if ".kiro" in script_parts:
184
+ return "kiro"
185
+ return None
186
+
187
+
188
+ def _resolve_context_key(trellis_dir: Path, input_data: dict) -> str | None:
189
+ scripts_dir = trellis_dir / "scripts"
190
+ if str(scripts_dir) not in sys.path:
191
+ sys.path.insert(0, str(scripts_dir))
192
+ from common.active_task import resolve_context_key # type: ignore[import-not-found]
193
+
194
+ return resolve_context_key(input_data, platform=_detect_platform(input_data))
195
+
196
+
197
+ def _persist_context_key_for_bash(context_key: str | None) -> None:
198
+ """Expose Trellis session identity to later Claude Code Bash commands.
199
+
200
+ Claude Code SessionStart hooks can append exports to CLAUDE_ENV_FILE; those
201
+ variables are then available to Bash tools in the same conversation. Without
202
+ this bridge, `task.py start` has hook stdin during SessionStart but no
203
+ session identity when the AI later runs it as a normal shell command.
204
+ """
205
+ if not context_key:
206
+ return
207
+ env_file = os.environ.get("CLAUDE_ENV_FILE")
208
+ if not env_file:
209
+ return
210
+ try:
211
+ with open(env_file, "a", encoding="utf-8") as handle:
212
+ handle.write(f"export TRELLIS_CONTEXT_ID={shlex.quote(context_key)}\n")
213
+ except OSError:
214
+ pass
215
+
216
+
217
+ def _resolve_active_task(trellis_dir: Path, input_data: dict):
218
+ scripts_dir = trellis_dir / "scripts"
219
+ if str(scripts_dir) not in sys.path:
220
+ sys.path.insert(0, str(scripts_dir))
221
+ from common.active_task import resolve_active_task # type: ignore[import-not-found]
222
+
223
+ return resolve_active_task(
224
+ trellis_dir.parent,
225
+ input_data,
226
+ platform=_detect_platform(input_data),
227
+ )
228
+
229
+
230
+ def run_script(script_path: Path, context_key: str | None = None) -> str:
231
+ try:
232
+ if script_path.suffix == ".py":
233
+ # Add PYTHONIOENCODING to force UTF-8 in subprocess
234
+ env = os.environ.copy()
235
+ env["PYTHONIOENCODING"] = "utf-8"
236
+ if context_key:
237
+ env["TRELLIS_CONTEXT_ID"] = context_key
238
+ cmd = [sys.executable, "-W", "ignore", str(script_path)]
239
+ else:
240
+ env = os.environ.copy()
241
+ if context_key:
242
+ env["TRELLIS_CONTEXT_ID"] = context_key
243
+ cmd = [str(script_path)]
244
+
245
+ result = subprocess.run(
246
+ cmd,
247
+ capture_output=True,
248
+ text=True,
249
+ encoding="utf-8",
250
+ errors="replace",
251
+ timeout=5,
252
+ cwd=script_path.parent.parent.parent,
253
+ env=env,
254
+ )
255
+ return result.stdout if result.returncode == 0 else "No context available"
256
+ except (subprocess.TimeoutExpired, FileNotFoundError, PermissionError):
257
+ return "No context available"
258
+
259
+
260
+ def _normalize_task_ref(task_ref: str) -> str:
261
+ normalized = task_ref.strip()
262
+ if not normalized:
263
+ return ""
264
+
265
+ path_obj = Path(normalized)
266
+ if path_obj.is_absolute():
267
+ return str(path_obj)
268
+
269
+ normalized = normalized.replace("\\", "/")
270
+ while normalized.startswith("./"):
271
+ normalized = normalized[2:]
272
+
273
+ if normalized.startswith("tasks/"):
274
+ return f".trellis/{normalized}"
275
+
276
+ return normalized
277
+
278
+
279
+ def _resolve_task_dir(trellis_dir: Path, task_ref: str) -> Path:
280
+ normalized = _normalize_task_ref(task_ref)
281
+ path_obj = Path(normalized)
282
+ if path_obj.is_absolute():
283
+ return path_obj
284
+ if normalized.startswith(".trellis/"):
285
+ return trellis_dir.parent / path_obj
286
+ return trellis_dir / "tasks" / path_obj
287
+
288
+
289
+ def _get_task_status(trellis_dir: Path, input_data: dict) -> str:
290
+ """Check current task status and return structured status string with explicit next action.
291
+
292
+ Returns a block with three fields:
293
+ - Status: current state
294
+ - Task: task identifier (when applicable)
295
+ - Next-Action: explicit skill/command/tool call the AI should invoke
296
+ """
297
+ active = _resolve_active_task(trellis_dir, input_data)
298
+
299
+ # Case 1: No active task — waiting for user to describe intent
300
+ if not active.task_path:
301
+ return (
302
+ "Status: NO ACTIVE TASK\n"
303
+ f"Source: {active.source}\n"
304
+ "Next-Action: After the user describes their intent, load skill `trellis-brainstorm` "
305
+ "to clarify requirements and create a task via `python ./.trellis/scripts/task.py create`.\n"
306
+ "Research reminder: for research-heavy tasks (comparing tools, reading external docs, "
307
+ "cross-platform surveys), spawn `trellis-research` sub-agents via the Task tool — "
308
+ "they persist findings to `{TASK_DIR}/research/*.md` and keep main context clean. "
309
+ "Do NOT do 10+ inline WebFetch/WebSearch in the main conversation.\n"
310
+ "User override (per-turn escape hatch): if the user's first message explicitly opts "
311
+ "out of the workflow (\"跳过 trellis\" / \"别走流程\" / \"小修一下\" / \"直接改\" / "
312
+ "\"skip trellis\" / \"no task\" / \"just do it\"), honor it for this turn — "
313
+ "acknowledge briefly and proceed without creating a task. Per-turn only."
314
+ )
315
+
316
+ # Case 2: Stale pointer — task dir was deleted
317
+ task_ref = active.task_path
318
+ task_dir = _resolve_task_dir(trellis_dir, task_ref)
319
+ if active.stale or not task_dir.is_dir():
320
+ return (
321
+ f"Status: STALE POINTER\nTask: {task_ref}\n"
322
+ f"Source: {active.source}\n"
323
+ f"Next-Action: Run `python ./.trellis/scripts/task.py finish` to clear the stale pointer, "
324
+ "then ask the user what to work on next."
325
+ )
326
+
327
+ # Read task.json
328
+ task_json_path = task_dir / "task.json"
329
+ task_data = {}
330
+ if task_json_path.is_file():
331
+ try:
332
+ task_data = json.loads(task_json_path.read_text(encoding="utf-8"))
333
+ except (json.JSONDecodeError, PermissionError):
334
+ pass
335
+
336
+ task_title = task_data.get("title", task_ref)
337
+ task_status = task_data.get("status", "unknown")
338
+
339
+ # Case 3: Task completed — time to archive
340
+ if task_status == "completed":
341
+ return (
342
+ f"Status: COMPLETED\nTask: {task_title}\n"
343
+ f"Source: {active.source}\n"
344
+ f"Next-Action: Load skill `trellis-update-spec` to capture learnings, "
345
+ f"then archive with `python ./.trellis/scripts/task.py archive {task_dir.name}`."
346
+ )
347
+
348
+ has_prd = (task_dir / "prd.md").is_file()
349
+
350
+ # Case 4: No PRD — still in Plan phase
351
+ if not has_prd:
352
+ return (
353
+ f"Status: PLANNING\nTask: {task_title}\n"
354
+ f"Source: {active.source}\n"
355
+ "Next-Action: Load skill `trellis-brainstorm` to clarify requirements with the user "
356
+ "and produce prd.md in the task directory.\n"
357
+ "Research reminder: when the task needs external research (tool comparison, docs, "
358
+ "conventions survey), spawn `trellis-research` sub-agents — don't WebFetch/WebSearch "
359
+ "inline in the main session. Findings go to `{task_dir}/research/*.md`; PRD only links to them."
360
+ )
361
+
362
+ # Case 4b: PRD exists but implement.jsonl has only seed (no curated entries) — Phase 1.3 gate
363
+ implement_jsonl = task_dir / "implement.jsonl"
364
+ if implement_jsonl.is_file() and not _has_curated_jsonl_entry(implement_jsonl):
365
+ return (
366
+ f"Status: PLANNING (Phase 1.3)\nTask: {task_title}\n"
367
+ f"Source: {active.source}\n"
368
+ "Next-Action: Curate `implement.jsonl` and `check.jsonl` with the spec + research files "
369
+ "the Phase 2 sub-agents will need. Only spec paths (`.trellis/spec/**/*.md`) and research "
370
+ "files (`{TASK_DIR}/research/*.md`) — no code paths. Run "
371
+ "`python ./.trellis/scripts/get_context.py --mode packages` to list available specs, "
372
+ "then edit the jsonl files or use `python ./.trellis/scripts/task.py add-context`. "
373
+ "See `.trellis/workflow.md` Phase 1.3 for details."
374
+ )
375
+
376
+ # Case 5: PRD + curated jsonl (or agent-less platform with no jsonl) — enter Execute phase
377
+ return (
378
+ f"Status: READY\nTask: {task_title}\n"
379
+ f"Source: {active.source}\n"
380
+ "Next required action: dispatch `trellis-implement` per Phase 2.1. "
381
+ "For agent-capable platforms, the default is to NOT edit code in the main session. "
382
+ "After implementation, dispatch `trellis-check` per Phase 2.2 before reporting completion.\n"
383
+ "Sub-agent roster: `trellis-implement` (writes code), `trellis-check` (verifies + self-fixes), "
384
+ "`trellis-research` (persists findings to `research/*.md` — use when you'd otherwise do "
385
+ "multiple WebFetch/WebSearch inline).\n"
386
+ "Sub-agent self-exemption: if you are reading this as a `trellis-implement` or "
387
+ "`trellis-check` sub-agent (your own role / agent name reflects that), this dispatch "
388
+ "instruction does NOT apply to you — you are already the dispatched sub-agent. "
389
+ "Implement / check directly without spawning another sub-agent of the same kind.\n"
390
+ "User override (per-turn escape hatch): if the user's CURRENT message explicitly tells the "
391
+ "main session to handle it directly (\"你直接改\" / \"别派 sub-agent\" / \"main session 写就行\" / "
392
+ "\"do it inline\" / \"不用 sub-agent\"), honor it for this turn and edit code directly. "
393
+ "Per-turn only; do NOT invent an override the user did not say."
394
+ )
395
+
396
+
397
+ def _load_trellis_config(trellis_dir: Path, input_data: dict) -> tuple:
398
+ """Load Trellis config for session-start decisions.
399
+
400
+ Returns:
401
+ (is_mono, packages_dict, spec_scope, task_pkg, default_pkg)
402
+ """
403
+ scripts_dir = trellis_dir / "scripts"
404
+ if str(scripts_dir) not in sys.path:
405
+ sys.path.insert(0, str(scripts_dir))
406
+
407
+ try:
408
+ from common.config import get_default_package, get_packages, get_spec_scope, is_monorepo # type: ignore[import-not-found]
409
+ from common.paths import get_current_task # type: ignore[import-not-found]
410
+
411
+ repo_root = trellis_dir.parent
412
+ is_mono = is_monorepo(repo_root)
413
+ packages = get_packages(repo_root) or {}
414
+ scope = get_spec_scope(repo_root)
415
+
416
+ # Get active task's package
417
+ task_pkg = None
418
+ current = get_current_task(
419
+ repo_root,
420
+ input_data,
421
+ platform=_detect_platform(input_data),
422
+ )
423
+ if current:
424
+ task_json = repo_root / current / "task.json"
425
+ if task_json.is_file():
426
+ try:
427
+ data = json.loads(task_json.read_text(encoding="utf-8"))
428
+ if isinstance(data, dict):
429
+ tp = data.get("package")
430
+ if isinstance(tp, str) and tp:
431
+ task_pkg = tp
432
+ except (json.JSONDecodeError, OSError):
433
+ pass
434
+
435
+ default_pkg = get_default_package(repo_root)
436
+ return is_mono, packages, scope, task_pkg, default_pkg
437
+ except Exception:
438
+ return False, {}, None, None, None
439
+
440
+
441
+ def _check_legacy_spec(trellis_dir: Path, is_mono: bool, packages: dict) -> str | None:
442
+ """Check for legacy spec directory structure in monorepo.
443
+
444
+ Returns warning message if legacy structure detected, None otherwise.
445
+ """
446
+ if not is_mono or not packages:
447
+ return None
448
+
449
+ spec_dir = trellis_dir / "spec"
450
+ if not spec_dir.is_dir():
451
+ return None
452
+
453
+ # Check for legacy flat spec dirs (spec/backend/, spec/frontend/ with index.md)
454
+ has_legacy = False
455
+ for legacy_name in ("backend", "frontend"):
456
+ legacy_dir = spec_dir / legacy_name
457
+ if legacy_dir.is_dir() and (legacy_dir / "index.md").is_file():
458
+ has_legacy = True
459
+ break
460
+
461
+ if not has_legacy:
462
+ return None
463
+
464
+ # Check which packages are missing spec/<pkg>/ directory
465
+ missing = [
466
+ name for name in sorted(packages.keys())
467
+ if not (spec_dir / name).is_dir()
468
+ ]
469
+
470
+ if not missing:
471
+ return None # All packages have spec dirs
472
+
473
+ if len(missing) == len(packages):
474
+ return (
475
+ f"[!] Legacy spec structure detected: found `spec/backend/` or `spec/frontend/` "
476
+ f"but no package-scoped `spec/<package>/` directories.\n"
477
+ f"Monorepo packages: {', '.join(sorted(packages.keys()))}\n"
478
+ f"Please reorganize: `spec/backend/` -> `spec/<package>/backend/`"
479
+ )
480
+ return (
481
+ f"[!] Partial spec migration detected: packages {', '.join(missing)} "
482
+ f"still missing `spec/<pkg>/` directory.\n"
483
+ f"Please complete migration for all packages."
484
+ )
485
+
486
+
487
+ def _resolve_spec_scope(
488
+ is_mono: bool,
489
+ packages: dict,
490
+ scope,
491
+ task_pkg: str | None,
492
+ default_pkg: str | None,
493
+ ) -> set | None:
494
+ """Resolve which packages should have their specs injected.
495
+
496
+ Returns:
497
+ Set of package names to include, or None for full scan.
498
+ """
499
+ if not is_mono or not packages:
500
+ return None # Single-repo: full scan
501
+
502
+ if scope is None:
503
+ return None # No scope configured: full scan
504
+
505
+ if isinstance(scope, str) and scope == "active_task":
506
+ if task_pkg and task_pkg in packages:
507
+ return {task_pkg}
508
+ if default_pkg and default_pkg in packages:
509
+ return {default_pkg}
510
+ return None # Fallback to full scan
511
+
512
+ if isinstance(scope, list):
513
+ valid = set()
514
+ for entry in scope:
515
+ if entry in packages:
516
+ valid.add(entry)
517
+ else:
518
+ print(
519
+ f"Warning: spec_scope contains unknown package: {entry}, ignoring",
520
+ file=sys.stderr,
521
+ )
522
+
523
+ if valid:
524
+ # Warn if active task is out of scope
525
+ if task_pkg and task_pkg not in valid:
526
+ print(
527
+ f"Warning: active task package '{task_pkg}' is out of configured spec_scope",
528
+ file=sys.stderr,
529
+ )
530
+ return valid
531
+
532
+ # All entries invalid: fallback chain
533
+ print(
534
+ "Warning: all spec_scope entries invalid, falling back to task/default/full",
535
+ file=sys.stderr,
536
+ )
537
+ if task_pkg and task_pkg in packages:
538
+ return {task_pkg}
539
+ if default_pkg and default_pkg in packages:
540
+ return {default_pkg}
541
+ return None # Full scan
542
+
543
+ return None # Unknown scope type: full scan
544
+
545
+
546
+ def _extract_range(content: str, start_header: str, end_header: str) -> str:
547
+ """Extract lines starting at `## start_header` up to (but excluding) `## end_header`.
548
+
549
+ Both parameters are full header lines WITHOUT the `## ` prefix (e.g. "Phase Index").
550
+ Returns empty string if start header is not found.
551
+ End header missing → extracts to end of file.
552
+ """
553
+ lines = content.splitlines()
554
+ start: int | None = None
555
+ end: int = len(lines)
556
+ start_match = f"## {start_header}"
557
+ end_match = f"## {end_header}"
558
+ for i, line in enumerate(lines):
559
+ stripped = line.strip()
560
+ if start is None and stripped == start_match:
561
+ start = i
562
+ continue
563
+ if start is not None and stripped == end_match:
564
+ end = i
565
+ break
566
+ if start is None:
567
+ return ""
568
+ return "\n".join(lines[start:end]).rstrip()
569
+
570
+
571
+ _BREADCRUMB_TAG_RE = re.compile(
572
+ r"\[workflow-state:([A-Za-z0-9_-]+)\]\s*\n.*?\n\s*\[/workflow-state:\1\]",
573
+ re.DOTALL,
574
+ )
575
+
576
+
577
+ def _strip_breadcrumb_tag_blocks(content: str) -> str:
578
+ """Remove `[workflow-state:STATUS]...[/workflow-state:STATUS]` blocks.
579
+
580
+ The tag blocks live inside `## Phase Index` (since v0.5.0-rc.0, when
581
+ they were colocated with their phase summaries) and are consumed by the
582
+ UserPromptSubmit hook (`inject-workflow-state.py`). The session-start
583
+ payload already covers the full step bodies, so re-inlining the
584
+ breadcrumbs here would just duplicate context.
585
+ """
586
+ return _BREADCRUMB_TAG_RE.sub("", content)
587
+
588
+
589
+ def _build_workflow_overview(workflow_path: Path) -> str:
590
+ """Inject the workflow guide for the session.
591
+
592
+ Contents:
593
+ 1. Section index (all `## ` headings — navigation)
594
+ 2. Phase Index section (rules, skill routing table, anti-rationalization table)
595
+ 3. Phase 1/2/3 step-level details (the actual how-to for each step)
596
+
597
+ The meta sections (Core Principles / Trellis System / Customizing
598
+ Trellis) are NOT injected — Core Principles is short prose the AI can
599
+ Read on demand; Trellis System lists reference commands duplicated in
600
+ step bodies; Customizing Trellis is for forks. Workflow-state breadcrumb
601
+ tag blocks (which now live inside Phase Index since v0.5.0-rc.0) are
602
+ stripped from the extracted range — they're consumed by the
603
+ UserPromptSubmit hook, not the session-start preamble.
604
+
605
+ Total budget: Phase Index ~2 KB + Phase 1/2/3 ~7 KB = ~9 KB.
606
+ """
607
+ content = read_file(workflow_path)
608
+ if not content:
609
+ return "No workflow.md found"
610
+
611
+ out_lines = [
612
+ "# Development Workflow — Section Index",
613
+ "Full guide: .trellis/workflow.md (read on demand)",
614
+ "",
615
+ "## Table of Contents",
616
+ ]
617
+ for line in content.splitlines():
618
+ if line.startswith("## "):
619
+ out_lines.append(line)
620
+ out_lines += ["", "---", ""]
621
+
622
+ # Extract Phase Index through the end of Phase 3 (before "Customizing
623
+ # Trellis" — the docs-for-forks footer added in v0.5.0-rc.0). Since
624
+ # sections appear in order Phase Index → Phase 1 → Phase 2 → Phase 3 →
625
+ # Customizing Trellis, a single range grab captures all four. The
626
+ # breadcrumb tag blocks now embedded inside Phase Index are stripped so
627
+ # they don't duplicate the per-turn UserPromptSubmit injection.
628
+ phases = _extract_range(
629
+ content, "Phase Index", "Customizing Trellis (for forks)"
630
+ )
631
+ if phases:
632
+ out_lines.append(_strip_breadcrumb_tag_blocks(phases).rstrip())
633
+
634
+ return "\n".join(out_lines).rstrip()
635
+
636
+
637
+ def main():
638
+ if should_skip_injection():
639
+ sys.exit(0)
640
+
641
+ try:
642
+ hook_input = json.loads(sys.stdin.read())
643
+ if not isinstance(hook_input, dict):
644
+ hook_input = {}
645
+ except (json.JSONDecodeError, ValueError):
646
+ hook_input = {}
647
+
648
+ # Try platform-specific env vars, hook cwd, fallback to cwd
649
+ project_dir_env_vars = [
650
+ "CLAUDE_PROJECT_DIR",
651
+ "QODER_PROJECT_DIR",
652
+ "CODEBUDDY_PROJECT_DIR",
653
+ "FACTORY_PROJECT_DIR",
654
+ "CURSOR_PROJECT_DIR",
655
+ "GEMINI_PROJECT_DIR",
656
+ "KIRO_PROJECT_DIR",
657
+ "COPILOT_PROJECT_DIR",
658
+ ]
659
+ project_dir = None
660
+ for var in project_dir_env_vars:
661
+ val = os.environ.get(var)
662
+ if val:
663
+ project_dir = Path(_normalize_windows_shell_path(val)).resolve()
664
+ break
665
+ if project_dir is None:
666
+ project_dir = Path(_normalize_windows_shell_path(hook_input.get("cwd", "."))).resolve()
667
+
668
+ trellis_dir = project_dir / ".trellis"
669
+ context_key = _resolve_context_key(trellis_dir, hook_input)
670
+ _persist_context_key_for_bash(context_key)
671
+
672
+ # Load config for scope filtering and legacy detection
673
+ is_mono, packages, scope_config, task_pkg, default_pkg = _load_trellis_config(
674
+ trellis_dir,
675
+ hook_input,
676
+ )
677
+ allowed_pkgs = _resolve_spec_scope(is_mono, packages, scope_config, task_pkg, default_pkg)
678
+
679
+ output = StringIO()
680
+
681
+ output.write("""<session-context>
682
+ You are starting a new session in a Trellis-managed project.
683
+ Read and follow all instructions below carefully.
684
+ </session-context>
685
+
686
+ """)
687
+ output.write(FIRST_REPLY_NOTICE)
688
+ output.write("\n\n")
689
+
690
+ # Legacy migration warning
691
+ legacy_warning = _check_legacy_spec(trellis_dir, is_mono, packages)
692
+ if legacy_warning:
693
+ output.write(f"<migration-warning>\n{legacy_warning}\n</migration-warning>\n\n")
694
+
695
+ output.write("<current-state>\n")
696
+ context_script = trellis_dir / "scripts" / "get_context.py"
697
+ output.write(run_script(context_script, context_key))
698
+ output.write("\n</current-state>\n\n")
699
+
700
+ output.write("<workflow>\n")
701
+ output.write(_build_workflow_overview(trellis_dir / "workflow.md"))
702
+ output.write("\n</workflow>\n\n")
703
+
704
+ output.write("<guidelines>\n")
705
+ output.write(
706
+ "Project spec indexes are listed by path below. Each index contains a "
707
+ "**Pre-Development Checklist** listing the specific guideline files to "
708
+ "read before coding.\n\n"
709
+ "- If you're spawning an implement/check sub-agent, context is injected "
710
+ "or loaded by the sub-agent via `{task}/implement.jsonl` / `check.jsonl`. "
711
+ "You do NOT need to read these indexes yourself.\n"
712
+ "- For agent-capable platforms, the default is to dispatch "
713
+ "`trellis-implement` and `trellis-check` (so JSONL context is loaded by "
714
+ "the sub-agents) rather than editing code in the main session. "
715
+ "Honor a per-turn user override only if the user's current message "
716
+ "explicitly opts out (see <task-status> below for override phrases).\n"
717
+ "- Sub-agent self-exemption: if you are reading this as a `trellis-implement` "
718
+ "or `trellis-check` sub-agent, the \"dispatch trellis-implement / trellis-check\" "
719
+ "rule above does NOT apply to you — you are already the dispatched sub-agent. "
720
+ "Do NOT spawn another sub-agent of the same kind; implement / check directly.\n\n"
721
+ )
722
+
723
+ # guides/ is cross-package thinking — always include inline (small, broadly useful)
724
+ guides_index = trellis_dir / "spec" / "guides" / "index.md"
725
+ if guides_index.is_file():
726
+ output.write("## guides (inlined — cross-package thinking guides)\n")
727
+ output.write(read_file(guides_index))
728
+ output.write("\n\n")
729
+
730
+ # Other spec indexes — paths only (main agent reads on demand;
731
+ # sub-agents get their specific specs via jsonl injection)
732
+ paths: list[str] = []
733
+ spec_dir = trellis_dir / "spec"
734
+ if spec_dir.is_dir():
735
+ for sub in sorted(spec_dir.iterdir()):
736
+ if not sub.is_dir() or sub.name.startswith("."):
737
+ continue
738
+ if sub.name == "guides":
739
+ continue # already inlined above
740
+
741
+ index_file = sub / "index.md"
742
+ if index_file.is_file():
743
+ # Flat spec dir (single-repo layer like spec/backend/)
744
+ paths.append(f".trellis/spec/{sub.name}/index.md")
745
+ else:
746
+ # Nested package dirs (monorepo: spec/<pkg>/<layer>/index.md)
747
+ # Apply scope filter
748
+ if allowed_pkgs is not None and sub.name not in allowed_pkgs:
749
+ continue
750
+ for nested in sorted(sub.iterdir()):
751
+ if not nested.is_dir():
752
+ continue
753
+ nested_index = nested / "index.md"
754
+ if nested_index.is_file():
755
+ paths.append(
756
+ f".trellis/spec/{sub.name}/{nested.name}/index.md"
757
+ )
758
+
759
+ if paths:
760
+ output.write("## Available spec indexes (read on demand)\n")
761
+ for p in paths:
762
+ output.write(f"- {p}\n")
763
+ output.write("\n")
764
+
765
+ output.write(
766
+ "Discover more via: "
767
+ "`python ./.trellis/scripts/get_context.py --mode packages`\n"
768
+ )
769
+ output.write("</guidelines>\n\n")
770
+
771
+ # Check task status and inject structured tag
772
+ task_status = _get_task_status(trellis_dir, hook_input)
773
+ output.write(f"<task-status>\n{task_status}\n</task-status>\n\n")
774
+
775
+ output.write("""<ready>
776
+ Context loaded. Workflow index, project state, and guidelines are already injected above — do NOT re-read them.
777
+ When the user sends the first message, follow <task-status> and the workflow guide.
778
+ If a task is READY, execute its Next required action without asking whether to continue.
779
+ </ready>""")
780
+
781
+ context_text = output.getvalue()
782
+ result = {
783
+ # Claude Code / Qoder / CodeBuddy / Droid / Gemini / Copilot format
784
+ "hookSpecificOutput": {
785
+ "hookEventName": "SessionStart",
786
+ "additionalContext": context_text,
787
+ },
788
+ # Cursor sessionStart format (top-level snake_case per Cursor docs)
789
+ "additional_context": context_text,
790
+ }
791
+
792
+ # Output JSON - stdout is already configured for UTF-8
793
+ print(json.dumps(result, ensure_ascii=False), flush=True)
794
+
795
+
796
+ if __name__ == "__main__":
797
+ main()