thoth-agents 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 (128) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +339 -0
  3. package/dist/agents/deep.d.ts +2 -0
  4. package/dist/agents/designer.d.ts +2 -0
  5. package/dist/agents/explorer.d.ts +2 -0
  6. package/dist/agents/index.d.ts +8 -0
  7. package/dist/agents/librarian.d.ts +2 -0
  8. package/dist/agents/oracle.d.ts +2 -0
  9. package/dist/agents/orchestrator.d.ts +15 -0
  10. package/dist/agents/prompt-dialects.d.ts +28 -0
  11. package/dist/agents/prompt-sections.d.ts +47 -0
  12. package/dist/agents/prompt-utils.d.ts +25 -0
  13. package/dist/agents/quick.d.ts +2 -0
  14. package/dist/cli/codex-config-io.d.ts +31 -0
  15. package/dist/cli/codex-install.d.ts +41 -0
  16. package/dist/cli/codex-paths.d.ts +29 -0
  17. package/dist/cli/config-io.d.ts +22 -0
  18. package/dist/cli/config-manager.d.ts +4 -0
  19. package/dist/cli/custom-skills.d.ts +48 -0
  20. package/dist/cli/index.d.ts +3 -0
  21. package/dist/cli/index.js +4579 -0
  22. package/dist/cli/install.d.ts +3 -0
  23. package/dist/cli/model-key-normalization.d.ts +1 -0
  24. package/dist/cli/paths.d.ts +21 -0
  25. package/dist/cli/providers.d.ts +120 -0
  26. package/dist/cli/skill-manifest.d.ts +32 -0
  27. package/dist/cli/skills.d.ts +26 -0
  28. package/dist/cli/system.d.ts +6 -0
  29. package/dist/cli/types.d.ts +58 -0
  30. package/dist/config/constants.d.ts +17 -0
  31. package/dist/config/index.d.ts +5 -0
  32. package/dist/config/loader.d.ts +33 -0
  33. package/dist/config/schema.d.ts +279 -0
  34. package/dist/config/utils.d.ts +10 -0
  35. package/dist/delegation/project-id.d.ts +10 -0
  36. package/dist/delegation/types.d.ts +39 -0
  37. package/dist/harness/adapters/codex-surfaces.d.ts +219 -0
  38. package/dist/harness/adapters/codex.d.ts +8 -0
  39. package/dist/harness/adapters/opencode.d.ts +10 -0
  40. package/dist/harness/codex-plugin-paths.d.ts +1 -0
  41. package/dist/harness/core/agent-pack.d.ts +88 -0
  42. package/dist/harness/core/memory-governance.d.ts +33 -0
  43. package/dist/harness/core/sdd.d.ts +100 -0
  44. package/dist/harness/core/skills.d.ts +183 -0
  45. package/dist/harness/generate-codex-plugin.d.ts +9 -0
  46. package/dist/harness/registry.d.ts +6 -0
  47. package/dist/harness/types.d.ts +85 -0
  48. package/dist/harness/writers/codex-plugin-package.d.ts +26 -0
  49. package/dist/harness/writers/codex-toml.d.ts +10 -0
  50. package/dist/harness/writers/skill-layout.d.ts +15 -0
  51. package/dist/hooks/auto-update-checker/cache.d.ts +7 -0
  52. package/dist/hooks/auto-update-checker/checker.d.ts +28 -0
  53. package/dist/hooks/auto-update-checker/constants.d.ts +11 -0
  54. package/dist/hooks/auto-update-checker/index.d.ts +18 -0
  55. package/dist/hooks/auto-update-checker/types.d.ts +23 -0
  56. package/dist/hooks/chat-headers.d.ts +16 -0
  57. package/dist/hooks/delegate-task-retry/guidance.d.ts +2 -0
  58. package/dist/hooks/delegate-task-retry/hook.d.ts +8 -0
  59. package/dist/hooks/delegate-task-retry/index.d.ts +4 -0
  60. package/dist/hooks/delegate-task-retry/patterns.d.ts +11 -0
  61. package/dist/hooks/foreground-fallback/index.d.ts +73 -0
  62. package/dist/hooks/index.d.ts +10 -0
  63. package/dist/hooks/json-error-recovery/hook.d.ts +18 -0
  64. package/dist/hooks/json-error-recovery/index.d.ts +1 -0
  65. package/dist/hooks/phase-reminder/index.d.ts +26 -0
  66. package/dist/hooks/post-read-nudge/index.d.ts +18 -0
  67. package/dist/hooks/skill-sync.d.ts +10 -0
  68. package/dist/hooks/thoth-mem/index.d.ts +46 -0
  69. package/dist/hooks/thoth-mem/protocol.d.ts +7 -0
  70. package/dist/index.d.ts +5 -0
  71. package/dist/index.js +6509 -0
  72. package/dist/mcp/context7.d.ts +7 -0
  73. package/dist/mcp/exa.d.ts +6 -0
  74. package/dist/mcp/grep-app.d.ts +7 -0
  75. package/dist/mcp/index.d.ts +7 -0
  76. package/dist/mcp/thoth.d.ts +3 -0
  77. package/dist/mcp/types.d.ts +12 -0
  78. package/dist/sdd/artifact-governance/artifact-loader.d.ts +55 -0
  79. package/dist/sdd/artifact-governance/index.d.ts +6 -0
  80. package/dist/sdd/artifact-governance/tasks-validator.d.ts +17 -0
  81. package/dist/sdd/artifact-governance/types.d.ts +52 -0
  82. package/dist/thoth/client.d.ts +14 -0
  83. package/dist/thoth/index.d.ts +2 -0
  84. package/dist/tools/ast-grep/cli.d.ts +15 -0
  85. package/dist/tools/ast-grep/constants.d.ts +25 -0
  86. package/dist/tools/ast-grep/downloader.d.ts +5 -0
  87. package/dist/tools/ast-grep/index.d.ts +10 -0
  88. package/dist/tools/ast-grep/tools.d.ts +3 -0
  89. package/dist/tools/ast-grep/types.d.ts +30 -0
  90. package/dist/tools/ast-grep/utils.d.ts +4 -0
  91. package/dist/tools/index.d.ts +2 -0
  92. package/dist/tools/lsp/client.d.ts +42 -0
  93. package/dist/tools/lsp/config-store.d.ts +29 -0
  94. package/dist/tools/lsp/config.d.ts +4 -0
  95. package/dist/tools/lsp/constants.d.ts +25 -0
  96. package/dist/tools/lsp/index.d.ts +4 -0
  97. package/dist/tools/lsp/tools.d.ts +5 -0
  98. package/dist/tools/lsp/types.d.ts +35 -0
  99. package/dist/tools/lsp/utils.d.ts +34 -0
  100. package/dist/utils/agent-variant.d.ts +47 -0
  101. package/dist/utils/env.d.ts +1 -0
  102. package/dist/utils/file-io.d.ts +3 -0
  103. package/dist/utils/frontmatter-yaml.d.ts +4 -0
  104. package/dist/utils/index.d.ts +7 -0
  105. package/dist/utils/internal-initiator.d.ts +6 -0
  106. package/dist/utils/logger.d.ts +1 -0
  107. package/dist/utils/polling.d.ts +21 -0
  108. package/dist/utils/subprocess.d.ts +24 -0
  109. package/dist/utils/tmux-session-manager.d.ts +63 -0
  110. package/dist/utils/tmux.d.ts +32 -0
  111. package/dist/utils/zip-extractor.d.ts +1 -0
  112. package/package.json +81 -0
  113. package/src/skills/_shared/openspec-convention.md +131 -0
  114. package/src/skills/_shared/persistence-contract.md +162 -0
  115. package/src/skills/_shared/thoth-mem-convention.md +124 -0
  116. package/src/skills/executing-plans/SKILL.md +245 -0
  117. package/src/skills/plan-reviewer/SKILL.md +115 -0
  118. package/src/skills/requirements-interview/SKILL.md +212 -0
  119. package/src/skills/sdd-apply/SKILL.md +89 -0
  120. package/src/skills/sdd-archive/SKILL.md +87 -0
  121. package/src/skills/sdd-design/SKILL.md +92 -0
  122. package/src/skills/sdd-init/SKILL.md +148 -0
  123. package/src/skills/sdd-propose/SKILL.md +89 -0
  124. package/src/skills/sdd-spec/SKILL.md +93 -0
  125. package/src/skills/sdd-tasks/SKILL.md +142 -0
  126. package/src/skills/sdd-verify/SKILL.md +99 -0
  127. package/src/skills/thoth-mem-agents/SKILL.md +355 -0
  128. package/thoth-agents.schema.json +470 -0
@@ -0,0 +1,4579 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli/index.ts
4
+ import { pathToFileURL } from "url";
5
+
6
+ // src/harness/adapters/codex.ts
7
+ import { existsSync as existsSync5, readFileSync as readFileSync4 } from "fs";
8
+ import { dirname as dirname2, resolve } from "path";
9
+
10
+ // src/agents/prompt-dialects.ts
11
+ var OPENCODE_CAPABILITIES = {
12
+ agentDefinitions: "supported",
13
+ delegatedExecution: "supported",
14
+ parallelDelegation: "supported",
15
+ runtimeHooks: "supported",
16
+ mcpConfiguration: "supported",
17
+ skillPackaging: "supported",
18
+ rolePermissions: "supported",
19
+ parentContextInjection: "supported",
20
+ memoryGovernanceEnforcement: "supported"
21
+ };
22
+ var CODEX_PROMPT_CAPABILITIES = {
23
+ agentDefinitions: "supported",
24
+ delegatedExecution: "instruction-only",
25
+ parallelDelegation: "instruction-only",
26
+ runtimeHooks: "unknown",
27
+ mcpConfiguration: "supported",
28
+ skillPackaging: "supported",
29
+ rolePermissions: "instruction-only",
30
+ parentContextInjection: "instruction-only",
31
+ memoryGovernanceEnforcement: "instruction-only"
32
+ };
33
+ function supportedCapabilityProfile(capabilities) {
34
+ return {
35
+ capabilities,
36
+ renderCapabilityDisclosure: () => void 0
37
+ };
38
+ }
39
+ function codexCapabilityDisclosure(capability) {
40
+ const status = CODEX_PROMPT_CAPABILITIES[capability];
41
+ if (status === "supported") {
42
+ return void 0;
43
+ }
44
+ if (status === "unknown") {
45
+ return `${capability}: unknown in Codex; treat related behavior as diagnostic-only unless the active Codex host documents support.`;
46
+ }
47
+ return `${capability}: ${status} in Codex; preserve the role responsibility as prompt guidance because equivalent runtime enforcement is not guaranteed.`;
48
+ }
49
+ var OPENCODE_PROMPT_DIALECT = {
50
+ harness: "opencode",
51
+ tools: {
52
+ delegationTool: "task",
53
+ backgroundDelegationTool: "task(background=true)",
54
+ backgroundStatusTool: "task_status",
55
+ userQuestionTool: "question",
56
+ progressTool: "todowrite",
57
+ hostStatusSurface: "task_status",
58
+ roleReference: (role) => `@${role}`
59
+ },
60
+ capabilities: supportedCapabilityProfile(OPENCODE_CAPABILITIES),
61
+ dispatchLabel(method) {
62
+ switch (method) {
63
+ case "root-coordinator":
64
+ return "root coordinator";
65
+ case "task":
66
+ return "task";
67
+ case "synchronous-task-only":
68
+ return "synchronous task only";
69
+ }
70
+ },
71
+ renderRoleInvocation(role) {
72
+ return `@${role}`;
73
+ }
74
+ };
75
+ var CODEX_PROMPT_DIALECT = {
76
+ harness: "codex",
77
+ tools: {
78
+ delegationTool: "Codex custom-agent task",
79
+ backgroundDelegationTool: "Codex background role-agent run",
80
+ backgroundStatusTool: "Codex host status surface",
81
+ userQuestionTool: "request_user_input",
82
+ progressTool: "Codex progress tracking surface",
83
+ hostStatusSurface: "Codex host status surface",
84
+ roleReference: (role) => `${role} role agent`
85
+ },
86
+ capabilities: {
87
+ capabilities: CODEX_PROMPT_CAPABILITIES,
88
+ renderCapabilityDisclosure: codexCapabilityDisclosure
89
+ },
90
+ dispatchLabel(method) {
91
+ switch (method) {
92
+ case "root-coordinator":
93
+ return "ambient Codex root session coordinator";
94
+ case "task":
95
+ return "Codex custom-agent task";
96
+ case "synchronous-task-only":
97
+ return "synchronous Codex custom-agent task only";
98
+ }
99
+ },
100
+ renderRoleInvocation(role) {
101
+ return role === "orchestrator" ? "orchestrator role agent" : `${role} subagent`;
102
+ }
103
+ };
104
+
105
+ // src/agents/prompt-sections.ts
106
+ function createQuestionProtocolSection() {
107
+ return {
108
+ kind: "question-protocol",
109
+ toolConcept: "userQuestion"
110
+ };
111
+ }
112
+ function createSubagentRulesSection(memoryAccess = "base") {
113
+ return {
114
+ kind: "subagent-rules",
115
+ memoryAccess,
116
+ progressConcept: "progress",
117
+ userQuestionConcept: "userQuestion"
118
+ };
119
+ }
120
+ function createResponseBudgetSection() {
121
+ return { kind: "response-budget" };
122
+ }
123
+ function createStepBudgetSection(steps) {
124
+ if (steps === void 0 || !Number.isInteger(steps) || steps <= 0) {
125
+ return void 0;
126
+ }
127
+ return { kind: "step-budget", steps };
128
+ }
129
+ function detectModelFamilyFromModel(model) {
130
+ const id = getPrimaryModelId(model)?.toLowerCase();
131
+ if (!id) {
132
+ return void 0;
133
+ }
134
+ if (id.includes("claude") || id.startsWith("anthropic/")) {
135
+ return "claude";
136
+ }
137
+ if (id.includes("gpt") || id.startsWith("openai/")) {
138
+ return "openai";
139
+ }
140
+ if (id.includes("gemini") || id.startsWith("google/")) {
141
+ return "gemini";
142
+ }
143
+ if (id.includes("kimi") || id.includes("k2")) {
144
+ return "kimi";
145
+ }
146
+ if (id.includes("glm") || id.startsWith("zai-")) {
147
+ return "glm";
148
+ }
149
+ return void 0;
150
+ }
151
+ function createModelFamilySection(role, model) {
152
+ const family = detectModelFamilyFromModel(model);
153
+ if (!family) {
154
+ return void 0;
155
+ }
156
+ return { kind: "model-family", role, family };
157
+ }
158
+ function roleText(template) {
159
+ return { kind: "role-text", template };
160
+ }
161
+ function specialistSections({
162
+ role,
163
+ mode,
164
+ dispatch,
165
+ scope,
166
+ responsibility,
167
+ rules,
168
+ memoryAccess,
169
+ output
170
+ }) {
171
+ const dispatchLabel = dispatch === "task" ? "{{dispatch.task}}" : "{{dispatch.synchronous-task-only}}";
172
+ return [
173
+ roleText(`<role>
174
+ You are ${role}.
175
+ </role>
176
+
177
+ <mode>
178
+ - Mode: ${mode}
179
+ - Dispatch method: ${dispatchLabel}
180
+ - Scope: ${scope}
181
+ </mode>
182
+
183
+ <responsibility>
184
+ ${responsibility}
185
+ </responsibility>
186
+
187
+ <rules>`),
188
+ createSubagentRulesSection(memoryAccess),
189
+ roleText(`${rules.join("\n")}
190
+ </rules>`),
191
+ createQuestionProtocolSection(),
192
+ roleText(`<output>`),
193
+ createResponseBudgetSection(),
194
+ roleText(`${output}
195
+ </output>`)
196
+ ];
197
+ }
198
+ function createOrchestratorPromptSections() {
199
+ return [
200
+ roleText(`<role>
201
+ You are the delegate-first root coordinator and decision engine for thoth-agents.
202
+ The root agent is the orchestrator/root coordinator for the session.
203
+ Orchestrator-only, root-only, or orchestrator-owned rules still apply even if
204
+ the harness does not name this agent "orchestrator".
205
+ </role>
206
+
207
+ <style>
208
+ Respond in the user's language. Be warm, direct, evidence-led, and concise.
209
+ Push back when context, risk, or assumptions are weak. Avoid verbosity.
210
+ </style>
211
+
212
+ <core-rules>
213
+ - Mode: primary coordinator. Mutation: none.
214
+ - Load \`thoth-mem-agents\` and \`requirements-interview\`.
215
+ - You MUST NOT read or write any file in the workspace except \`openspec/\` coordination artifacts for the SDD pipeline.
216
+ - Delegate all inspection, writing, searching, debugging, and verification.
217
+ - Own the thinking: analyze the request, choose the approach, synthesize facts, make decisions, ask \`{{userQuestionTool}}\`, manage progress, and own root-session memory.
218
+ - Use sub-agents for evidence and action, not to outsource architecture or planning.
219
+ - Never request raw file dumps from sub-agents; ask for findings, paths, line anchors, diffs, verification, and blockers.
220
+ - Use openspec/ for coordination artifacts, especially
221
+ openspec/changes/{change-name}/tasks.md.
222
+ - Visual or UX work and screenshots always go to {{role.designer}}.
223
+ - Verify through delegation, not inline.
224
+ - Verification should follow the user's project instructions and use the smallest sufficient delegated checks: typecheck, lint, focused tests, or build when appropriate.
225
+ </core-rules>
226
+
227
+ <session-bootstrap>
228
+ - At the start of a new root session, when thoth-mem tools are available, load \`thoth-mem-agents\` and \`requirements-interview\`, call \`mem_session_start\` with the current project and session identity, then save the real user prompt with \`mem_save_prompt\`.
229
+ - Save only the real user request with \`mem_save_prompt\`; never save generated sub-agent prompts, handoffs, summaries, or tool scaffolding as user intent.
230
+ - If thoth-mem tools or required session/project identity are unavailable, disclose that memory bootstrap could not run and continue without pretending memory was saved.
231
+ </session-bootstrap>
232
+
233
+ <routing>
234
+ {{role.explorer}}: read-only codebase discovery. Use for broad search, symbols, references, unknown paths, or multiple candidates.
235
+ {{role.librarian}}: read-only external docs/public examples. Use for version-sensitive APIs, official docs, or unfamiliar libraries.
236
+ {{role.oracle}}: read-only review/diagnosis. Use for architecture, security/correctness risk, plan review, persistent bugs, or high-stakes ambiguity.
237
+ {{role.designer}}: write-capable UI/UX owner. Use for user-facing UI, styles, layout, interactions, and all visual QA.
238
+ {{role.quick}}: write-capable narrow implementer. Use for clear, mechanical, low-risk, uniform edits.
239
+ {{role.deep}}: write-capable thorough implementer. Use for backend logic, data flow, APIs, state, refactors, edge cases, or correctness-critical work.
240
+
241
+ Tiebreakers:
242
+ - User-facing UI -> {{role.designer}}. Backend/system logic -> {{role.deep}}. Mechanical pattern -> {{role.quick}}.
243
+ - Discovery first when paths or facts are unknown; implementation agent may read known local context for its own task, but should not redo broad discovery already assigned to {{role.explorer}}/{{role.librarian}}.
244
+ - Do not use {{role.oracle}} for routine synthesis. After {{role.explorer}}/{{role.librarian}} results, you combine facts, inferences, unknowns, confidence, and next step.
245
+ </routing>
246
+
247
+ <subagent-prompts>
248
+ - Every sub-agent prompt you write must be in English, regardless of the user's language.
249
+ - Keep user-facing replies in the user's language, but translate delegated task prompts, internal handoffs, SDD envelopes, and verification requests into English.
250
+ - Prefer 2-3 surgical discovery probes over one broad exploration when independent facts can be gathered in parallel.
251
+ - A surgical probe asks one narrow question and returns only the anchors needed for your decision.
252
+ </subagent-prompts>
253
+
254
+ <internal-handoff>
255
+ Before dispatching {{role.designer}}, {{role.quick}}, or {{role.deep}} after discovery, synthesize a compact internal handoff. This is an implementation detail between you and sub-agents, not a user-facing step or artifact.
256
+
257
+ Internal handoff fields:
258
+ - Goal: the specific outcome for this task.
259
+ - Decision: the chosen approach and why it is the right next move.
260
+ - Evidence: relevant files, symbols, line anchors, docs, constraints, and known invariants from {{role.explorer}}/{{role.librarian}}.
261
+ - Scope: exact files/areas to change and non-goals to avoid.
262
+ - Steps: ordered implementation instructions, including what to preserve.
263
+ - Verification: smallest sufficient checks or visual QA required.
264
+ - Uncertainty: remaining unknowns the implementer may resolve locally, plus what should be escalated instead of guessed.
265
+
266
+ Never mention the internal handoff to the user, ask the user to prepare it, or present handoff preparation as the recommended next step. To the user, describe the actual work: discovery, design, implementation, verification, or the concrete decision needed.
267
+
268
+ For {{role.explorer}}/{{role.librarian}}, ask narrow fact-finding questions that will fill missing internal handoff fields: likely files, symbols, call sites, constraints, examples, versioned API facts, and verification targets. Require decision-ready findings, not raw context.
269
+ </internal-handoff>
270
+
271
+ <dispatch>
272
+ - If independent delegations are ready, launch them in the same response.
273
+ - Default to normal synchronous \`{{delegationTool}}\` execution.
274
+ - Experimental background \`{{backgroundDelegationTool}}\` is allowed only for {{role.explorer}} and {{role.librarian}} for asynchronous delegation.
275
+ - {{role.oracle}}, {{role.designer}}, {{role.quick}}, and {{role.deep}} always use normal synchronous \`{{delegationTool}}\` execution.
276
+ - When using background \`{{delegationTool}}\`, treat it as conditional and non-portable: if the host does not expose the experimental path, fall back to normal synchronous \`{{delegationTool}}\`.
277
+ - Use \`{{backgroundStatusTool}}\` to wait, poll, and collect background task results before synthesizing or reporting completion.
278
+ - If a result is empty, contradictory, or low-confidence, retry once with a materially sharper prompt; then escalate with evidence via \`{{userQuestionTool}}\`.
279
+ - If a named subagent hits capacity, retry that same role up to 3 attempts.
280
+ - Never switch to \`default\`, \`worker\`, or any other role.
281
+ - After 3 failures, stay on the same role; if a same-role model override exists, use it. Otherwise report a capacity blocker.
282
+ - Write-capable dispatches must include the internal handoff when one exists, so implementers can edit instead of rediscovering the plan.
283
+ - Never tell sub-agents to discard working-tree changes.
284
+ </dispatch>
285
+
286
+ <sdd>
287
+ All work always starts with requirements-interview skill.
288
+
289
+ Routes:
290
+ - Direct implementation for low-complexity work.
291
+ - Accelerated SDD: propose -> tasks.
292
+ - Full SDD: propose -> spec -> design -> tasks.
293
+
294
+ Hard gates:
295
+ - Artifact-producing SDD phases are dispatched to {{role.deep}} or {{role.quick}} with the matching skill loaded.
296
+ - {{role.oracle}} is read-only and only handles plan-reviewer.
297
+ - Never skip artifacts or jump from requirements-interview to implementation when SDD is selected.
298
+ - Before SDD execution, load \`executing-plans\`; then track progress in {{progressTool}} plus the persistent artifact.
299
+ - If openspec persistence is selected and openspec/ is missing, dispatch sdd-init first.
300
+ - During SDD execution, batch compatible implementation work.
301
+ - Group consecutive ready SDD tasks for the same execution agent into one dispatch when dependencies, scope, and verification can be handled together. Keep per-task tracking and evidence; do not split a compatible {{role.designer}}/{{role.quick}}/{{role.deep}} run into one delegation per checkbox.
302
+
303
+ SDD dispatch envelope must include: skill name, persistence mode, pipeline type, change name, project name, needed prior artifact context, verification expectation, and return envelope.
304
+ After each phase, verify the sub-agent reported the openspec path and/or thoth-mem topic_key. Retry once if missing.
305
+
306
+ Artifact governance handoff:
307
+ - After \`sdd-tasks\`, you may surface report-only artifact governance findings before execution preparation starts.
308
+ - Delegate governance inspection; do not inspect repository artifacts inline.
309
+ - Do not treat governance findings as an execution gate.
310
+ - Do not let governance validation replace \`plan-reviewer\` or \`executing-plans\`.
311
+ - Root thoth-mem ownership stays with you; sub-agents may surface findings but must not own session memory, prompts, or progress checkpoints.
312
+
313
+ Plan gate: after tasks, ask with \`{{userQuestionTool}}\`: "Review plan with {{role.oracle}} before executing (Recommended)" or "Proceed to execution".
314
+ If reviewed, the review loop is complete only after [OKAY].
315
+ If {{role.oracle}} returns [OKAY], ask the user with \`{{userQuestionTool}}\` whether to proceed to implementation or stop with the approved plan.
316
+ Do not dispatch \`sdd-apply\` after oracle approval until the user confirms implementation.
317
+ Post-execution: delegate sdd-verify, then sdd-archive when verification passes.
318
+ </sdd>
319
+
320
+ <progress-memory>
321
+ - Keep {{progressTool}} top-level and lean for multi-step work.
322
+ - When SDD is active, update both {{progressTool}} and openspec/changes/{change-name}/tasks.md before dispatch and after results.
323
+ - Root-session memory is yours: search before repeated work; save durable decisions, discoveries, bugs, patterns, constraints, and session summaries.
324
+ - Durable \`mem_save\` guidance: save architecture decisions, accepted or rejected recommendations, bug fixes with root cause, non-obvious discoveries, conventions, configuration changes, and durable user preferences. Use stable topic keys for evolving topics, and keep general observations outside the protected \`sdd/*\` namespace.
325
+ - Targeted 3-layer recall protocol: \`mem_search\` with compact results -> \`mem_timeline\` around promising observations -> \`mem_get_observation\` only for records needed in full. Use preview search only when compact results do not disambiguate.
326
+ - SDD memory artifacts use deterministic topic keys only in thoth-mem or hybrid persistence modes: \`sdd/{change}/{artifact}\`.
327
+ - Before ending the root session, call \`mem_session_summary\` with a concise Goal, Instructions, Discoveries, Accomplished, Next Steps, and Relevant Files summary. Do not claim memory was saved unless the tool call succeeded.
328
+ - After compaction, first preserve the compacted summary with \`mem_session_summary\`, then recover recent context and use the 3-layer recall protocol before continuing work.
329
+ </progress-memory>
330
+
331
+ <communication>
332
+ State the plan briefly, delegate, then summarize outcomes without replaying raw work. Before any tool call or delegation, emit a short user-visible status/preamble that names the next action and target; for parallel dispatches, one compact sentence covering the batch is enough. Keep preambles about next action, evidence, and verification, not private reasoning. Separate evidence, inference, and uncertainty when it matters. Never ask blocking questions in prose.
333
+ </communication>`),
334
+ createQuestionProtocolSection()
335
+ ];
336
+ }
337
+ function createReadOnlySpecialistPromptSections(role) {
338
+ if (role === "explorer") {
339
+ return specialistSections({
340
+ role,
341
+ mode: "read-only",
342
+ dispatch: "task",
343
+ scope: "local repository discovery",
344
+ responsibility: "Find workspace facts fast. Return decision-ready evidence for internal handoffs: paths, lines, symbols, constraints, edit targets, and conclusions.",
345
+ rules: [
346
+ "- Questions should be rare; exhaust local evidence first.",
347
+ "- Prefer paths, lines, symbols, and concise summaries over dumps.",
348
+ "- When full content is explicitly requested, reproduce it faithfully."
349
+ ],
350
+ memoryAccess: "readonly",
351
+ output: `
352
+ Return exactly these sections, in this order:
353
+
354
+ STATUS: one of CONFIRMED | PARTIAL | INCONCLUSIVE
355
+ - CONFIRMED = direct evidence answers the question with high confidence.
356
+ - PARTIAL = some direct evidence, but gaps remain or multiple candidates exist.
357
+ - INCONCLUSIVE = no sufficient evidence found. Never fabricate a confident answer from naming similarity alone.
358
+
359
+ FINDINGS: bullets with claim, evidence type [direct|inferred|assumed], confidence [high|medium|low], and file:line anchors for concrete claims.
360
+
361
+ ALTERNATIVES CONSIDERED: ranked candidates when more than one plausible match exists. Omit if only one candidate.
362
+
363
+ UNRESOLVED QUESTIONS: what remains ambiguous. State what additional context would unblock the search.
364
+
365
+ UNCHECKED AREAS: what you did not inspect that could change the answer. Omit if nothing notable.
366
+
367
+ SHORT EVIDENCE: at most one short excerpt per key finding, max 2 lines each. Skip if citations are self-explanatory.
368
+
369
+ Lead with STATUS. Stay under 40 lines total when possible. If the schema forces more lines, exceed the budget rather than drop required fields.`
370
+ });
371
+ }
372
+ if (role === "librarian") {
373
+ return specialistSections({
374
+ role,
375
+ mode: "read-only",
376
+ dispatch: "task",
377
+ scope: "external research plus local confirmation when needed",
378
+ responsibility: "Gather authoritative external evidence that helps the orchestrator make implementation decisions. Prefer official docs first, then high-signal public examples. Every substantive claim must carry a source URL.",
379
+ rules: [
380
+ "- Questions should be rare; exhaust available sources first.",
381
+ "- Prefer official documentation over commentary when both answer the same point.",
382
+ "- Distinguish clearly between official guidance and community examples."
383
+ ],
384
+ memoryAccess: "readonly",
385
+ output: `- Organize by finding. Include a source URL for every claim.
386
+ - Distinguish official docs from community examples.
387
+ - Return synthesized findings, not full documentation excerpts.
388
+ - Target: under 40 lines total.`
389
+ });
390
+ }
391
+ return specialistSections({
392
+ role,
393
+ mode: "read-only",
394
+ dispatch: "synchronous task only",
395
+ scope: "advice, diagnosis, architecture, code review, and plan review",
396
+ responsibility: "Provide strategic technical guidance anchored to evidence. Use systematic-debugging for bugs, plan-reviewer for SDD plans, and web-assisted research when deeper diagnosis needs it.",
397
+ rules: [
398
+ "- Cite exact files and lines for local claims.",
399
+ "- Separate observations, risks, and recommendations.",
400
+ "- Ask only when tradeoffs, risk tolerance, or approval materially change the recommendation."
401
+ ],
402
+ memoryAccess: "readonly",
403
+ output: `- Cite exact files and lines \u2014 do not quote large code blocks.
404
+ - Separate observations, risks, and recommendations.
405
+ - For diagnosis: root cause + fix recommendation, not step-by-step trace.
406
+ - Target: under 50 lines total.`
407
+ });
408
+ }
409
+ function createWriteCapableSpecialistPromptSections(role) {
410
+ if (role === "designer") {
411
+ return specialistSections({
412
+ role,
413
+ mode: "write-capable",
414
+ dispatch: "synchronous task only",
415
+ scope: "UI/UX decisions, implementation, and visual verification",
416
+ responsibility: "Own the user-facing solution end to end: choose the UX approach, implement it, and verify it visually. Use playwright-cli only in non-interactive, single-run mode (for example `playwright test`), never with persistent UI or watcher flags.\nWhen dispatched for QA-only tasks (no implementation), take screenshots, inspect the UI, and return a structured visual QA report: what looks correct, what has issues, and recommended fixes.",
417
+ rules: [
418
+ "- Treat the orchestrator's internal handoff as the handoff; do not rediscover settled scope or constraints.",
419
+ "- Own UX decisions instead of bouncing them back unless a real user preference is required.",
420
+ "- Verify visually when feasible; do not stop at code that merely compiles.",
421
+ "- Keep changes focused on the user-facing outcome.",
422
+ "- NEVER run blocking or long-running commands: no `playwright test --ui`, `playwright show-report`, `--headed --debug`, dev servers, or watchers. Use single-run variants and capture screenshots/traces as artifacts."
423
+ ],
424
+ memoryAccess: "writable",
425
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
426
+ For non-SDD work: state what was implemented, verification status, and remaining caveats.
427
+ - Include visual verification status when applicable.
428
+ - Target: under 30 lines total.`
429
+ });
430
+ }
431
+ if (role === "quick") {
432
+ return specialistSections({
433
+ role,
434
+ mode: "write-capable",
435
+ dispatch: "synchronous task only",
436
+ scope: "fast bounded implementation",
437
+ responsibility: "Implement well-defined changes quickly. Favor speed over exhaustive analysis when the task is narrow and the path is clear.",
438
+ rules: [
439
+ "- Optimize for fast execution on narrow, clear tasks.",
440
+ "- Treat the orchestrator's internal handoff as the starting point; follow its file anchors, scope, non-goals, and verification target.",
441
+ "- Read only the context you need.",
442
+ "- Do not redo broad discovery. If the handoff lacks essential anchors, surface the missing context instead of turning the task into open-ended exploration.",
443
+ "- Avoid multi-step planning; if the task stops being bounded, surface it.",
444
+ "- Ask only for implementation-local ambiguity, not orchestrator-level routing.",
445
+ "- NEVER run git commands that discard changes (`git restore`, `git checkout --`, `git reset`, `git clean`). Files modified by prior tasks are intentional SDD progress, not unintended changes."
446
+ ],
447
+ memoryAccess: "writable",
448
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
449
+ For non-SDD work: status + summary + files changed + issues. Nothing more.
450
+ - Target: under 20 lines total.`
451
+ });
452
+ }
453
+ return specialistSections({
454
+ role,
455
+ mode: "write-capable",
456
+ dispatch: "synchronous task only",
457
+ scope: "thorough implementation and verification",
458
+ responsibility: "Handle correctness-critical, multi-file, or edge-case-heavy changes with full local context analysis. Use test-driven-development and systematic-debugging when relevant before implementing fixes.",
459
+ rules: [
460
+ "- Treat the orchestrator's internal handoff as the architecture handoff; validate it against nearby code, but do not restart upstream discovery unless evidence contradicts it.",
461
+ "- Do not skip verification \u2014 thoroughness is your value proposition.",
462
+ "- Investigate related files, types, and call sites before changing shared behavior, prioritizing the anchors and constraints in the handoff.",
463
+ "- Ask when a real architecture or implementation tradeoff blocks correct execution."
464
+ ],
465
+ memoryAccess: "writable",
466
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
467
+ For non-SDD work: summary + files changed + verification results + edge cases considered.
468
+ - Save detailed analysis for follow-up requests; return only actionable conclusions.
469
+ - Target: under 40 lines total.`
470
+ });
471
+ }
472
+ function getPrimaryModelId(model) {
473
+ if (Array.isArray(model)) {
474
+ const first = model[0];
475
+ return typeof first === "string" ? first : first?.id;
476
+ }
477
+ return model;
478
+ }
479
+ function renderQuestionProtocol(_section, dialect) {
480
+ return `<questions>
481
+ Use \`${dialect.tools.userQuestionTool}\` only for blocking choices: unresolved ambiguity that changes the result, destructive/security-sensitive actions, or missing secrets. Do all non-blocked work first, ask one targeted question with a recommended default first, then stop.
482
+ </questions>`;
483
+ }
484
+ function renderSubagentRules(section, dialect) {
485
+ const rules = [
486
+ `- Single-task leaf agent: do not delegate, manage SDD phases, act as orchestrator, or call \`${dialect.tools.progressTool}\`.`,
487
+ `- Use \`${dialect.tools.userQuestionTool}\` only for local blocking decisions.`,
488
+ "- Never discard working-tree changes: no `git restore`, `git checkout -- <path>`, `git reset --hard`, `git clean`, or `git stash`.",
489
+ "- Avoid blocking/watch commands; use terminating checks only."
490
+ ];
491
+ if (section.memoryAccess === "readonly") {
492
+ rules.push(
493
+ "- Use read-only thoth-mem only when dispatch gives session_id/project: `mem_search` -> `mem_timeline` -> `mem_get_observation`.",
494
+ "- Never call `mem_session_start`, `mem_session_summary`, or `mem_save_prompt`; those tools are orchestrator-owned.",
495
+ "- Never write memory; memory writes are orchestrator-owned."
496
+ );
497
+ }
498
+ if (section.memoryAccess === "writable") {
499
+ rules.push(
500
+ "- Use delegated thoth-mem tools only (mem_save, mem_search, mem_get_observation, mem_timeline, mem_suggest_topic_key).",
501
+ "- Never call `mem_session_start`, `mem_session_summary`, or `mem_save_prompt`; those tools are orchestrator-owned.",
502
+ "- Always use the parent session_id/project from dispatch for every thoth-mem call.",
503
+ "- If either is missing, do NOT call thoth-mem.",
504
+ "- For reads, use only `mem_search` -> `mem_timeline` -> `mem_get_observation`.",
505
+ "- You do not own durable memory of your own; `mem_save` writes under the orchestrator's session/project only."
506
+ );
507
+ }
508
+ return rules.join("\n");
509
+ }
510
+ function renderResponseBudget() {
511
+ return "Return concise structured results: status, summary, files, verification/issues. Never return raw file dumps.";
512
+ }
513
+ function renderStepBudget(section) {
514
+ return `<step-budget>
515
+ - You have a hard execution budget of ${section.steps} steps.
516
+ - Plan your tool use before acting, prioritize the highest-signal checks first, and stop once you have enough evidence to answer.
517
+ - Avoid repeated searches or reads. If the remaining work will exceed the budget, return partial findings with the next best target instead of looping.
518
+ </step-budget>`;
519
+ }
520
+ function getRoleModelProfile(role) {
521
+ switch (role) {
522
+ case "orchestrator":
523
+ return "- Exploit your role by selecting the right specialist category, launching independent tasks together, and synthesizing facts/inferences/unknowns before the next dispatch.";
524
+ case "explorer":
525
+ return "- Exploit your role by scanning broadly first, then narrowing to symbol/path evidence with ranked candidates and confidence.";
526
+ case "librarian":
527
+ return "- Exploit your role by prioritizing official docs, dates, versions, and source quality before summarizing public examples.";
528
+ case "oracle":
529
+ return "- Exploit your role by challenging assumptions, identifying risk, and giving a decision-ready recommendation backed by evidence.";
530
+ case "designer":
531
+ return "- Exploit your role by making concrete UX choices, implementing them, and verifying the visible result instead of stopping at code review.";
532
+ case "quick":
533
+ return "- Exploit your role by applying the smallest clear edit, avoiding broad exploration, and returning immediately after focused verification.";
534
+ case "deep":
535
+ return "- Exploit your role by building a complete mental model of shared behavior, writing tests first when behavior changes, and verifying edge cases.";
536
+ }
537
+ }
538
+ function renderModelFamily(section) {
539
+ const roleGuidance = getRoleModelProfile(section.role);
540
+ if (section.family === "claude") {
541
+ return `<model-profile family="claude">
542
+ - Use XML-like sections, label uncertainty, and delegate aggressively when agentic.
543
+ ${roleGuidance}
544
+ </model-profile>`;
545
+ }
546
+ if (section.family === "openai") {
547
+ return `<model-profile family="openai">
548
+ - Plan briefly, then act. Keep tool dispatch explicit: action, target, return shape.
549
+ ${roleGuidance}
550
+ </model-profile>`;
551
+ }
552
+ if (section.family === "gemini") {
553
+ return `<model-profile family="gemini">
554
+ - Use long-context breadth deliberately, then ground conclusions in exact anchors.
555
+ ${roleGuidance}
556
+ </model-profile>`;
557
+ }
558
+ if (section.family === "kimi") {
559
+ return `<model-profile family="kimi">
560
+ - Favor repository-scale navigation before edits; keep patches grounded in current file state.
561
+ ${roleGuidance}
562
+ </model-profile>`;
563
+ }
564
+ return `<model-profile family="glm">
565
+ - Use compact checklists, conservative steps, clear verification, and concrete blockers.
566
+ ${roleGuidance}
567
+ </model-profile>`;
568
+ }
569
+ function renderRoleText(section, dialect) {
570
+ return section.template.replaceAll("{{delegationTool}}", dialect.tools.delegationTool).replaceAll(
571
+ "{{backgroundDelegationTool}}",
572
+ dialect.tools.backgroundDelegationTool ?? dialect.tools.delegationTool
573
+ ).replaceAll(
574
+ "{{backgroundStatusTool}}",
575
+ dialect.tools.backgroundStatusTool ?? dialect.tools.hostStatusSurface ?? ""
576
+ ).replaceAll("{{userQuestionTool}}", dialect.tools.userQuestionTool).replaceAll("{{progressTool}}", dialect.tools.progressTool).replaceAll("{{dispatch.task}}", dialect.dispatchLabel("task")).replaceAll(
577
+ "{{dispatch.synchronous-task-only}}",
578
+ dialect.dispatchLabel("synchronous-task-only")
579
+ ).replace(
580
+ /\{\{role\.(\w+)\}\}/g,
581
+ (_match, role) => dialect.renderRoleInvocation(role)
582
+ );
583
+ }
584
+ function renderPromptSection(section, dialect) {
585
+ switch (section.kind) {
586
+ case "question-protocol":
587
+ return renderQuestionProtocol(section, dialect);
588
+ case "subagent-rules":
589
+ return renderSubagentRules(section, dialect);
590
+ case "response-budget":
591
+ return renderResponseBudget();
592
+ case "step-budget":
593
+ return renderStepBudget(section);
594
+ case "model-family":
595
+ return renderModelFamily(section);
596
+ case "role-text":
597
+ return renderRoleText(section, dialect);
598
+ }
599
+ }
600
+ function renderRolePrompt(sections, dialect) {
601
+ return sections.map((section) => renderPromptSection(section, dialect).trim()).filter(Boolean).join("\n\n");
602
+ }
603
+
604
+ // src/agents/prompt-utils.ts
605
+ var QUESTION_PROTOCOL = renderPromptSection(
606
+ createQuestionProtocolSection(),
607
+ OPENCODE_PROMPT_DIALECT
608
+ );
609
+ var SUBAGENT_RULES = renderPromptSection(
610
+ createSubagentRulesSection("base"),
611
+ OPENCODE_PROMPT_DIALECT
612
+ );
613
+ var SUBAGENT_RULES_READONLY = renderPromptSection(
614
+ createSubagentRulesSection("readonly"),
615
+ OPENCODE_PROMPT_DIALECT
616
+ );
617
+ var SUBAGENT_RULES_WRITABLE = renderPromptSection(
618
+ createSubagentRulesSection("writable"),
619
+ OPENCODE_PROMPT_DIALECT
620
+ );
621
+ var RESPONSE_BUDGET = renderPromptSection(
622
+ createResponseBudgetSection(),
623
+ OPENCODE_PROMPT_DIALECT
624
+ );
625
+ function trimPromptSection(section) {
626
+ const trimmed = section?.trim();
627
+ return trimmed ? trimmed : void 0;
628
+ }
629
+ function appendPromptSections(...sections) {
630
+ return sections.map(trimPromptSection).filter(Boolean).join("\n\n");
631
+ }
632
+ function replacePromptPlaceholders(template, placeholders = {}) {
633
+ return Object.entries(placeholders).reduce((prompt, [key, value]) => {
634
+ if (value === void 0) {
635
+ return prompt;
636
+ }
637
+ return prompt.replaceAll(`{{${key}}}`, String(value));
638
+ }, template);
639
+ }
640
+ function composeAgentPrompt({
641
+ basePrompt,
642
+ customPrompt,
643
+ customAppendPrompt,
644
+ placeholders
645
+ }) {
646
+ const resolvedBase = replacePromptPlaceholders(basePrompt, placeholders);
647
+ if (customPrompt) {
648
+ return replacePromptPlaceholders(customPrompt, placeholders);
649
+ }
650
+ return appendPromptSections(
651
+ resolvedBase,
652
+ replacePromptPlaceholders(customAppendPrompt ?? "", placeholders)
653
+ );
654
+ }
655
+
656
+ // src/config/constants.ts
657
+ var AGENT_ALIASES = {
658
+ explore: "explorer",
659
+ "frontend-ui-ux-engineer": "designer"
660
+ };
661
+ var DEFAULT_MODELS = {
662
+ orchestrator: void 0,
663
+ oracle: "openai/gpt-5.4",
664
+ librarian: "openai/gpt-5.4-mini",
665
+ explorer: "openai/gpt-5.4-mini",
666
+ designer: "openai/gpt-5.4-mini",
667
+ quick: "openai/gpt-5.4-mini",
668
+ deep: "openai/gpt-5.4"
669
+ };
670
+ var DEFAULT_TIMEOUT_MS = 2 * 60 * 1e3;
671
+ var MAX_POLL_TIME_MS = 5 * 60 * 1e3;
672
+ var DEFAULT_THOTH_COMMAND = ["pnpm", "dlx", "thoth-mem@latest"];
673
+
674
+ // src/config/loader.ts
675
+ import * as fs from "fs";
676
+ import * as path from "path";
677
+
678
+ // src/cli/config-io.ts
679
+ import {
680
+ copyFileSync,
681
+ existsSync as existsSync2,
682
+ readFileSync,
683
+ renameSync,
684
+ statSync,
685
+ writeFileSync
686
+ } from "fs";
687
+
688
+ // src/cli/paths.ts
689
+ import { existsSync, mkdirSync } from "fs";
690
+ import { homedir } from "os";
691
+ import { dirname, join } from "path";
692
+ function getDefaultOpenCodeConfigDir() {
693
+ const userConfigDir = process.env.XDG_CONFIG_HOME ? process.env.XDG_CONFIG_HOME : join(homedir(), ".config");
694
+ return join(userConfigDir, "opencode");
695
+ }
696
+ function getCustomOpenCodeConfigDir() {
697
+ const configDir = process.env.OPENCODE_CONFIG_DIR?.trim();
698
+ return configDir || void 0;
699
+ }
700
+ function getConfigDir() {
701
+ const customConfigDir = getCustomOpenCodeConfigDir();
702
+ if (customConfigDir) {
703
+ return customConfigDir;
704
+ }
705
+ return getDefaultOpenCodeConfigDir();
706
+ }
707
+ function getOpenCodeConfigPaths() {
708
+ const configDir = getDefaultOpenCodeConfigDir();
709
+ return [join(configDir, "opencode.json"), join(configDir, "opencode.jsonc")];
710
+ }
711
+ function getConfigJson() {
712
+ return getOpenCodeConfigPaths()[0];
713
+ }
714
+ function getConfigJsonc() {
715
+ return getOpenCodeConfigPaths()[1];
716
+ }
717
+ function getLiteConfig() {
718
+ return join(getConfigDir(), "thoth-agents.json");
719
+ }
720
+ function getLiteConfigJsonc() {
721
+ return join(getConfigDir(), "thoth-agents.jsonc");
722
+ }
723
+ function getExistingLiteConfigPath() {
724
+ const jsonPath = getLiteConfig();
725
+ if (existsSync(jsonPath)) return jsonPath;
726
+ const jsoncPath = getLiteConfigJsonc();
727
+ if (existsSync(jsoncPath)) return jsoncPath;
728
+ return jsonPath;
729
+ }
730
+ function getExistingConfigPath() {
731
+ const jsonPath = getConfigJson();
732
+ if (existsSync(jsonPath)) return jsonPath;
733
+ const jsoncPath = getConfigJsonc();
734
+ if (existsSync(jsoncPath)) return jsoncPath;
735
+ return jsonPath;
736
+ }
737
+ function ensureConfigDir() {
738
+ const configDir = getConfigDir();
739
+ if (!existsSync(configDir)) {
740
+ mkdirSync(configDir, { recursive: true });
741
+ }
742
+ }
743
+ function ensureOpenCodeConfigDir() {
744
+ const configDir = dirname(getConfigJson());
745
+ if (!existsSync(configDir)) {
746
+ mkdirSync(configDir, { recursive: true });
747
+ }
748
+ }
749
+
750
+ // src/cli/providers.ts
751
+ var MODEL_MAPPINGS = {
752
+ openai: {
753
+ orchestrator: { model: "openai/gpt-5.4" },
754
+ oracle: { model: "openai/gpt-5.4", variant: "high" },
755
+ librarian: { model: "openai/gpt-5.4-mini", variant: "low" },
756
+ explorer: { model: "openai/gpt-5.4-mini", variant: "low" },
757
+ designer: { model: "openai/gpt-5.4-mini", variant: "medium" },
758
+ quick: { model: "openai/gpt-5.4-mini", variant: "low" },
759
+ deep: { model: "openai/gpt-5.4", variant: "high" }
760
+ },
761
+ kimi: {
762
+ orchestrator: { model: "kimi-for-coding/k2p5" },
763
+ oracle: { model: "kimi-for-coding/k2p5", variant: "high" },
764
+ librarian: { model: "kimi-for-coding/k2p5", variant: "low" },
765
+ explorer: { model: "kimi-for-coding/k2p5", variant: "low" },
766
+ designer: { model: "kimi-for-coding/k2p5", variant: "medium" },
767
+ quick: { model: "kimi-for-coding/k2p5", variant: "low" },
768
+ deep: { model: "kimi-for-coding/k2p5", variant: "high" }
769
+ },
770
+ copilot: {
771
+ orchestrator: { model: "github-copilot/claude-opus-4.6" },
772
+ oracle: { model: "github-copilot/claude-opus-4.6", variant: "high" },
773
+ librarian: { model: "github-copilot/grok-code-fast-1", variant: "low" },
774
+ explorer: { model: "github-copilot/grok-code-fast-1", variant: "low" },
775
+ designer: {
776
+ model: "github-copilot/gemini-3.1-pro-preview",
777
+ variant: "medium"
778
+ },
779
+ quick: { model: "github-copilot/claude-sonnet-4.6", variant: "low" },
780
+ deep: { model: "github-copilot/claude-opus-4.6", variant: "high" }
781
+ },
782
+ "zai-plan": {
783
+ orchestrator: { model: "zai-coding-plan/glm-5" },
784
+ oracle: { model: "zai-coding-plan/glm-5", variant: "high" },
785
+ librarian: { model: "zai-coding-plan/glm-5", variant: "low" },
786
+ explorer: { model: "zai-coding-plan/glm-5", variant: "low" },
787
+ designer: { model: "zai-coding-plan/glm-5", variant: "medium" },
788
+ quick: { model: "zai-coding-plan/glm-5", variant: "low" },
789
+ deep: { model: "zai-coding-plan/glm-5", variant: "high" }
790
+ }
791
+ };
792
+ function generateLiteConfig(installConfig) {
793
+ const config = {
794
+ preset: "openai",
795
+ presets: {}
796
+ };
797
+ const createAgentConfig = (modelInfo) => {
798
+ return {
799
+ model: modelInfo.model,
800
+ variant: modelInfo.variant
801
+ };
802
+ };
803
+ const buildPreset = (mappingName) => {
804
+ const mapping = MODEL_MAPPINGS[mappingName];
805
+ return Object.fromEntries(
806
+ Object.entries(mapping).map(([agentName, modelInfo]) => [
807
+ agentName,
808
+ createAgentConfig(modelInfo)
809
+ ])
810
+ );
811
+ };
812
+ config.presets.openai = buildPreset("openai");
813
+ if (installConfig.hasTmux) {
814
+ config.tmux = {
815
+ enabled: true,
816
+ layout: "main-vertical",
817
+ main_pane_size: 60
818
+ };
819
+ }
820
+ return config;
821
+ }
822
+
823
+ // src/cli/config-io.ts
824
+ var PACKAGE_NAME = "thoth-agents";
825
+ function stripJsonComments(json) {
826
+ const commentPattern = /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
827
+ const trailingCommaPattern = /\\"|"(?:\\"|[^"])*"|(,)(\s*[}\]])/g;
828
+ return json.replace(
829
+ commentPattern,
830
+ (match, commentGroup) => commentGroup ? "" : match
831
+ ).replace(
832
+ trailingCommaPattern,
833
+ (match, comma, closing) => comma ? closing : match
834
+ );
835
+ }
836
+ function parseConfigFile(path3) {
837
+ try {
838
+ if (!existsSync2(path3)) return { config: null };
839
+ const stat = statSync(path3);
840
+ if (stat.size === 0) return { config: null };
841
+ const content = readFileSync(path3, "utf-8");
842
+ if (content.trim().length === 0) return { config: null };
843
+ return { config: JSON.parse(stripJsonComments(content)) };
844
+ } catch (err) {
845
+ return { config: null, error: String(err) };
846
+ }
847
+ }
848
+ function parseConfig(path3) {
849
+ const result = parseConfigFile(path3);
850
+ if (result.config || result.error) return result;
851
+ if (path3.endsWith(".json")) {
852
+ const jsoncPath = path3.replace(/\.json$/, ".jsonc");
853
+ return parseConfigFile(jsoncPath);
854
+ }
855
+ return { config: null };
856
+ }
857
+ function writeConfig(configPath, config) {
858
+ if (configPath.endsWith(".jsonc")) {
859
+ console.warn(
860
+ "[config-manager] Writing to .jsonc file - comments will not be preserved"
861
+ );
862
+ }
863
+ const tmpPath = `${configPath}.tmp`;
864
+ const bakPath = `${configPath}.bak`;
865
+ const content = `${JSON.stringify(config, null, 2)}
866
+ `;
867
+ if (existsSync2(configPath)) {
868
+ copyFileSync(configPath, bakPath);
869
+ }
870
+ writeFileSync(tmpPath, content);
871
+ renameSync(tmpPath, configPath);
872
+ }
873
+ async function addPluginToOpenCodeConfig() {
874
+ const configPath = getExistingConfigPath();
875
+ try {
876
+ ensureOpenCodeConfigDir();
877
+ } catch (err) {
878
+ return {
879
+ success: false,
880
+ configPath,
881
+ error: `Failed to create config directory: ${err}`
882
+ };
883
+ }
884
+ try {
885
+ const { config: parsedConfig, error } = parseConfig(configPath);
886
+ if (error) {
887
+ return {
888
+ success: false,
889
+ configPath,
890
+ error: `Failed to parse config: ${error}`
891
+ };
892
+ }
893
+ const config = parsedConfig ?? {};
894
+ const plugins = config.plugin ?? [];
895
+ const filteredPlugins = plugins.filter(
896
+ (p) => p !== PACKAGE_NAME && !p.startsWith(`${PACKAGE_NAME}@`)
897
+ );
898
+ filteredPlugins.push(`${PACKAGE_NAME}@latest`);
899
+ config.plugin = filteredPlugins;
900
+ writeConfig(configPath, config);
901
+ return { success: true, configPath };
902
+ } catch (err) {
903
+ return {
904
+ success: false,
905
+ configPath,
906
+ error: `Failed to update opencode config: ${err}`
907
+ };
908
+ }
909
+ }
910
+ function writeLiteConfig(installConfig, targetPath) {
911
+ const configPath = targetPath ?? getLiteConfig();
912
+ try {
913
+ ensureConfigDir();
914
+ const config = generateLiteConfig(installConfig);
915
+ const tmpPath = `${configPath}.tmp`;
916
+ const bakPath = `${configPath}.bak`;
917
+ const content = `${JSON.stringify(config, null, 2)}
918
+ `;
919
+ if (existsSync2(configPath)) {
920
+ copyFileSync(configPath, bakPath);
921
+ }
922
+ writeFileSync(tmpPath, content);
923
+ renameSync(tmpPath, configPath);
924
+ return { success: true, configPath };
925
+ } catch (err) {
926
+ return {
927
+ success: false,
928
+ configPath,
929
+ error: `Failed to write lite config: ${err}`
930
+ };
931
+ }
932
+ }
933
+ function disableDefaultAgents() {
934
+ const configPath = getExistingConfigPath();
935
+ try {
936
+ ensureOpenCodeConfigDir();
937
+ const { config: parsedConfig, error } = parseConfig(configPath);
938
+ if (error) {
939
+ return {
940
+ success: false,
941
+ configPath,
942
+ error: `Failed to parse config: ${error}`
943
+ };
944
+ }
945
+ const config = parsedConfig ?? {};
946
+ const agent = config.agent ?? {};
947
+ agent.explore = { disable: true };
948
+ agent.general = { disable: true };
949
+ config.agent = agent;
950
+ writeConfig(configPath, config);
951
+ return { success: true, configPath };
952
+ } catch (err) {
953
+ return {
954
+ success: false,
955
+ configPath,
956
+ error: `Failed to disable default agents: ${err}`
957
+ };
958
+ }
959
+ }
960
+ function detectCurrentConfig() {
961
+ const result = {
962
+ isInstalled: false,
963
+ hasKimi: false,
964
+ hasOpenAI: false,
965
+ hasAnthropic: false,
966
+ hasCopilot: false,
967
+ hasZaiPlan: false,
968
+ hasAntigravity: false,
969
+ hasChutes: false,
970
+ hasOpencodeZen: false,
971
+ hasTmux: false
972
+ };
973
+ const { config } = parseConfig(getExistingConfigPath());
974
+ if (!config) return result;
975
+ const plugins = config.plugin ?? [];
976
+ result.isInstalled = plugins.some((p) => p.startsWith(PACKAGE_NAME));
977
+ result.hasAntigravity = plugins.some(
978
+ (p) => p.startsWith("opencode-antigravity-auth")
979
+ );
980
+ const providers = config.provider;
981
+ result.hasKimi = !!providers?.kimi;
982
+ result.hasAnthropic = !!providers?.anthropic;
983
+ result.hasCopilot = !!providers?.["github-copilot"];
984
+ result.hasZaiPlan = !!providers?.["zai-coding-plan"];
985
+ result.hasChutes = !!providers?.chutes;
986
+ if (providers?.google) result.hasAntigravity = true;
987
+ const { config: liteConfig } = parseConfig(getLiteConfig());
988
+ if (liteConfig && typeof liteConfig === "object") {
989
+ const configObj = liteConfig;
990
+ const presetName = configObj.preset;
991
+ const presets = configObj.presets;
992
+ const agents = presets?.[presetName];
993
+ if (agents) {
994
+ const models = Object.values(agents).map((a) => a?.model).filter(Boolean);
995
+ result.hasOpenAI = models.some((m) => m?.startsWith("openai/"));
996
+ result.hasAnthropic = models.some((m) => m?.startsWith("anthropic/"));
997
+ result.hasCopilot = models.some((m) => m?.startsWith("github-copilot/"));
998
+ result.hasZaiPlan = models.some((m) => m?.startsWith("zai-coding-plan/"));
999
+ result.hasOpencodeZen = models.some((m) => m?.startsWith("opencode/"));
1000
+ if (models.some((m) => m?.startsWith("google/"))) {
1001
+ result.hasAntigravity = true;
1002
+ }
1003
+ if (models.some((m) => m?.startsWith("chutes/"))) {
1004
+ result.hasChutes = true;
1005
+ }
1006
+ }
1007
+ if (configObj.tmux && typeof configObj.tmux === "object") {
1008
+ const tmuxConfig = configObj.tmux;
1009
+ result.hasTmux = tmuxConfig.enabled === true;
1010
+ }
1011
+ }
1012
+ return result;
1013
+ }
1014
+
1015
+ // src/config/schema.ts
1016
+ import { z } from "zod";
1017
+ var AGENT_NAMES = [
1018
+ "orchestrator",
1019
+ "oracle",
1020
+ "designer",
1021
+ "explorer",
1022
+ "librarian",
1023
+ "quick",
1024
+ "deep"
1025
+ ];
1026
+ var FALLBACK_AGENT_NAMES = [...AGENT_NAMES];
1027
+ var MANUAL_AGENT_NAMES = [...AGENT_NAMES];
1028
+ var ProviderModelIdSchema = z.string().regex(
1029
+ /^[^/\s]+\/[^\s]+$/,
1030
+ "Expected provider/model format (provider/.../model)"
1031
+ );
1032
+ var ManualAgentPlanSchema = z.object({
1033
+ primary: ProviderModelIdSchema,
1034
+ fallback1: ProviderModelIdSchema,
1035
+ fallback2: ProviderModelIdSchema,
1036
+ fallback3: ProviderModelIdSchema
1037
+ }).superRefine((value, ctx) => {
1038
+ const unique = /* @__PURE__ */ new Set([
1039
+ value.primary,
1040
+ value.fallback1,
1041
+ value.fallback2,
1042
+ value.fallback3
1043
+ ]);
1044
+ if (unique.size !== 4) {
1045
+ ctx.addIssue({
1046
+ code: z.ZodIssueCode.custom,
1047
+ message: "primary and fallbacks must be unique per agent"
1048
+ });
1049
+ }
1050
+ });
1051
+ var ManualPlanSchema = z.object({
1052
+ orchestrator: ManualAgentPlanSchema,
1053
+ oracle: ManualAgentPlanSchema,
1054
+ designer: ManualAgentPlanSchema,
1055
+ explorer: ManualAgentPlanSchema,
1056
+ librarian: ManualAgentPlanSchema,
1057
+ quick: ManualAgentPlanSchema,
1058
+ deep: ManualAgentPlanSchema
1059
+ }).strict();
1060
+ var AgentModelChainSchema = z.array(z.string()).min(1);
1061
+ var FallbackChainsSchema = z.object({
1062
+ orchestrator: AgentModelChainSchema.optional(),
1063
+ oracle: AgentModelChainSchema.optional(),
1064
+ designer: AgentModelChainSchema.optional(),
1065
+ explorer: AgentModelChainSchema.optional(),
1066
+ librarian: AgentModelChainSchema.optional(),
1067
+ quick: AgentModelChainSchema.optional(),
1068
+ deep: AgentModelChainSchema.optional()
1069
+ }).catchall(AgentModelChainSchema);
1070
+ var AgentOverrideConfigSchema = z.object({
1071
+ model: z.union([
1072
+ z.string(),
1073
+ z.array(
1074
+ z.union([
1075
+ z.string(),
1076
+ z.object({
1077
+ id: z.string(),
1078
+ variant: z.string().optional()
1079
+ })
1080
+ ])
1081
+ )
1082
+ ]).optional(),
1083
+ temperature: z.number().min(0).max(2).optional(),
1084
+ steps: z.number().int().min(1).optional(),
1085
+ variant: z.string().optional().catch(void 0)
1086
+ });
1087
+ var TmuxLayoutSchema = z.enum([
1088
+ "main-horizontal",
1089
+ // Main pane on top, agents stacked below
1090
+ "main-vertical",
1091
+ // Main pane on left, agents stacked on right
1092
+ "tiled",
1093
+ // All panes equal size grid
1094
+ "even-horizontal",
1095
+ // All panes side by side
1096
+ "even-vertical"
1097
+ // All panes stacked vertically
1098
+ ]);
1099
+ var TmuxConfigSchema = z.object({
1100
+ enabled: z.boolean().default(false),
1101
+ layout: TmuxLayoutSchema.default("main-vertical"),
1102
+ main_pane_size: z.number().min(20).max(80).default(60)
1103
+ // percentage for main pane
1104
+ });
1105
+ var PresetSchema = z.record(z.string(), AgentOverrideConfigSchema);
1106
+ var AgentNameSchema = z.enum(AGENT_NAMES);
1107
+ var McpNameSchema = z.enum([
1108
+ "exa",
1109
+ "context7",
1110
+ "grep_app",
1111
+ "thoth_mem"
1112
+ ]);
1113
+ var ThothConfigSchema = z.object({
1114
+ command: z.array(z.string()).optional(),
1115
+ data_dir: z.string().optional(),
1116
+ environment: z.record(z.string(), z.string()).optional(),
1117
+ timeout: z.number().optional(),
1118
+ http_port: z.number().optional()
1119
+ });
1120
+ var ArtifactStoreModeSchema = z.enum([
1121
+ "thoth-mem",
1122
+ "openspec",
1123
+ "hybrid"
1124
+ ]);
1125
+ var ArtifactStoreConfigSchema = z.object({
1126
+ mode: ArtifactStoreModeSchema.default("hybrid")
1127
+ });
1128
+ var CodexGenerationConfigSchema = z.object({
1129
+ enabled: z.boolean().default(false),
1130
+ outputRoot: z.string().optional(),
1131
+ dryRun: z.boolean().default(true)
1132
+ });
1133
+ var FailoverConfigSchema = z.object({
1134
+ enabled: z.boolean().default(true),
1135
+ timeoutMs: z.number().min(0).default(15e3),
1136
+ retryDelayMs: z.number().min(0).default(500),
1137
+ chains: FallbackChainsSchema.default({})
1138
+ });
1139
+ var PluginConfigSchema = z.object({
1140
+ preset: z.string().optional(),
1141
+ setDefaultAgent: z.boolean().optional(),
1142
+ scoringEngineVersion: z.enum(["v1", "v2-shadow", "v2"]).optional(),
1143
+ balanceProviderUsage: z.boolean().optional(),
1144
+ manualPlan: ManualPlanSchema.optional(),
1145
+ presets: z.record(z.string(), PresetSchema).optional(),
1146
+ agents: z.record(z.string(), AgentOverrideConfigSchema).optional(),
1147
+ disabled_mcps: z.array(z.string()).optional(),
1148
+ tmux: TmuxConfigSchema.optional(),
1149
+ fallback: FailoverConfigSchema.optional(),
1150
+ thoth: ThothConfigSchema.optional(),
1151
+ artifactStore: ArtifactStoreConfigSchema.optional(),
1152
+ codex: CodexGenerationConfigSchema.optional()
1153
+ });
1154
+
1155
+ // src/config/loader.ts
1156
+ var PROMPTS_DIR_NAME = "thoth-agents";
1157
+ function loadAgentPrompt(agentName, preset) {
1158
+ const presetDirName = preset && /^[a-zA-Z0-9_-]+$/.test(preset) ? preset : void 0;
1159
+ const promptsDir = path.join(getConfigDir(), PROMPTS_DIR_NAME);
1160
+ const promptSearchDirs = presetDirName ? [path.join(promptsDir, presetDirName), promptsDir] : [promptsDir];
1161
+ const result = {};
1162
+ const readFirstPrompt = (fileName, errorPrefix) => {
1163
+ for (const dir of promptSearchDirs) {
1164
+ const promptPath = path.join(dir, fileName);
1165
+ if (!fs.existsSync(promptPath)) {
1166
+ continue;
1167
+ }
1168
+ try {
1169
+ return fs.readFileSync(promptPath, "utf-8");
1170
+ } catch (error) {
1171
+ console.warn(
1172
+ `[thoth-agents] ${errorPrefix} ${promptPath}:`,
1173
+ error instanceof Error ? error.message : String(error)
1174
+ );
1175
+ }
1176
+ }
1177
+ return void 0;
1178
+ };
1179
+ result.prompt = readFirstPrompt(
1180
+ `${agentName}.md`,
1181
+ "Error reading prompt file"
1182
+ );
1183
+ result.appendPrompt = readFirstPrompt(
1184
+ `${agentName}_append.md`,
1185
+ "Error reading append prompt file"
1186
+ );
1187
+ return result;
1188
+ }
1189
+
1190
+ // src/config/utils.ts
1191
+ function getAgentOverride(config, name) {
1192
+ const overrides = config?.agents ?? {};
1193
+ return overrides[name] ?? overrides[Object.keys(AGENT_ALIASES).find((k) => AGENT_ALIASES[k] === name) ?? ""];
1194
+ }
1195
+
1196
+ // src/mcp/context7.ts
1197
+ var CONTEXT7_MCP_URL = "https://mcp.context7.com/mcp";
1198
+ var context7 = {
1199
+ type: "remote",
1200
+ url: CONTEXT7_MCP_URL,
1201
+ headers: process.env.CONTEXT7_API_KEY ? { CONTEXT7_API_KEY: process.env.CONTEXT7_API_KEY } : void 0,
1202
+ oauth: false
1203
+ };
1204
+
1205
+ // src/mcp/exa.ts
1206
+ var exa = {
1207
+ type: "local",
1208
+ command: ["npx", "-y", "exa-mcp-server"]
1209
+ };
1210
+
1211
+ // src/mcp/grep-app.ts
1212
+ var GREP_APP_MCP_URL = "https://mcp.grep.app";
1213
+
1214
+ // src/harness/core/agent-pack.ts
1215
+ var AGENT_ROLES = [
1216
+ {
1217
+ name: "orchestrator",
1218
+ mode: "primary-non-mutating",
1219
+ dispatch: "root-coordinator",
1220
+ canMutateWorkspace: false,
1221
+ scope: "coordination, routing, decisions, progress, and root memory",
1222
+ responsibility: "Delegate-first coordinator for SDD workflow, specialist dispatch, and root-session memory ownership.",
1223
+ toolGovernance: [
1224
+ "delegates inspection, writing, debugging, and verification",
1225
+ "owns question prompts and root-session memory",
1226
+ "does not perform inline workspace implementation"
1227
+ ],
1228
+ verification: [
1229
+ "routes verification to specialists",
1230
+ "summarizes evidence from changed files, diagnostics, and tests"
1231
+ ]
1232
+ },
1233
+ {
1234
+ name: "explorer",
1235
+ mode: "read-only",
1236
+ dispatch: "task",
1237
+ canMutateWorkspace: false,
1238
+ scope: "local repository discovery",
1239
+ responsibility: "Find workspace facts fast and return paths, lines, symbols, constraints, edit targets, and conclusions.",
1240
+ toolGovernance: [
1241
+ "read/search/code-navigation tools only",
1242
+ "no durable memory writes",
1243
+ "no task delegation or progress ownership"
1244
+ ],
1245
+ verification: ["reports confidence, anchors, unchecked areas, and gaps"]
1246
+ },
1247
+ {
1248
+ name: "librarian",
1249
+ mode: "read-only",
1250
+ dispatch: "task",
1251
+ canMutateWorkspace: false,
1252
+ scope: "external research plus local confirmation when needed",
1253
+ responsibility: "Gather authoritative external evidence and distinguish official docs from examples.",
1254
+ toolGovernance: [
1255
+ "research and read-only local confirmation tools",
1256
+ "no workspace mutation",
1257
+ "no durable memory writes"
1258
+ ],
1259
+ verification: ["sources every substantive external claim with a URL"]
1260
+ },
1261
+ {
1262
+ name: "oracle",
1263
+ mode: "read-only",
1264
+ dispatch: "synchronous-task-only",
1265
+ canMutateWorkspace: false,
1266
+ scope: "advice, diagnosis, architecture, code review, and plan review",
1267
+ responsibility: "Provide strategic technical guidance anchored to evidence and review SDD plans.",
1268
+ toolGovernance: [
1269
+ "read-only analysis and review",
1270
+ "no implementation or artifact-producing SDD phases",
1271
+ "no task delegation"
1272
+ ],
1273
+ verification: ["separates observations, risks, and recommendations"]
1274
+ },
1275
+ {
1276
+ name: "designer",
1277
+ mode: "write-capable",
1278
+ dispatch: "synchronous-task-only",
1279
+ canMutateWorkspace: true,
1280
+ scope: "UI/UX decisions, implementation, and visual verification",
1281
+ responsibility: "Own user-facing implementation choices and visual QA for UI work.",
1282
+ toolGovernance: [
1283
+ "may edit focused UI/UX files",
1284
+ "owns screenshots and visual QA",
1285
+ "does not delegate or own SDD progress"
1286
+ ],
1287
+ verification: ["includes visual verification status when applicable"]
1288
+ },
1289
+ {
1290
+ name: "quick",
1291
+ mode: "write-capable",
1292
+ dispatch: "synchronous-task-only",
1293
+ canMutateWorkspace: true,
1294
+ scope: "fast bounded implementation",
1295
+ responsibility: "Implement well-defined narrow or mechanical changes with focused verification.",
1296
+ toolGovernance: [
1297
+ "may edit bounded targets",
1298
+ "does not perform broad rediscovery",
1299
+ "does not delegate or own SDD progress"
1300
+ ],
1301
+ verification: ["runs the smallest sufficient focused check"]
1302
+ },
1303
+ {
1304
+ name: "deep",
1305
+ mode: "write-capable",
1306
+ dispatch: "synchronous-task-only",
1307
+ canMutateWorkspace: true,
1308
+ scope: "thorough implementation and verification",
1309
+ responsibility: "Handle correctness-critical, multi-file, or edge-case-heavy changes with full local context analysis.",
1310
+ toolGovernance: [
1311
+ "may edit implementation and tests",
1312
+ "validates shared behavior against related code and call sites",
1313
+ "does not delegate or own SDD progress"
1314
+ ],
1315
+ verification: ["does not skip verification and reports edge-case evidence"]
1316
+ }
1317
+ ];
1318
+ var DELEGATE_FIRST_RULES = [
1319
+ "The orchestrator coordinates, decides, asks blocking questions, and delegates evidence or action.",
1320
+ "Explorer, librarian, and oracle remain read-only specialists.",
1321
+ "Designer, quick, and deep are write-capable leaf agents with synchronous task dispatch.",
1322
+ "Subagents return findings, diffs, verification, and blockers rather than raw file dumps.",
1323
+ "No leaf agent owns SDD progress checkboxes or orchestrator-only memory."
1324
+ ];
1325
+ var VERIFICATION_PROTOCOL = [
1326
+ "Completion reports include changed files and verification evidence.",
1327
+ "Behavior changes require the smallest sufficient automated check or an explicitly documented check.",
1328
+ "Visual changes require designer-owned visual QA when feasible."
1329
+ ];
1330
+ var AGENT_PACK_CONTRACT = {
1331
+ roles: [...AGENT_ROLES],
1332
+ delegateFirstRules: [...DELEGATE_FIRST_RULES],
1333
+ verificationProtocol: [...VERIFICATION_PROTOCOL]
1334
+ };
1335
+ function getAgentPackContract() {
1336
+ return {
1337
+ roles: AGENT_PACK_CONTRACT.roles.map((role) => ({
1338
+ ...role,
1339
+ toolGovernance: [...role.toolGovernance],
1340
+ verification: [...role.verification]
1341
+ })),
1342
+ delegateFirstRules: [...AGENT_PACK_CONTRACT.delegateFirstRules],
1343
+ verificationProtocol: [...AGENT_PACK_CONTRACT.verificationProtocol]
1344
+ };
1345
+ }
1346
+
1347
+ // src/harness/core/memory-governance.ts
1348
+ var ROOT_OWNED_TOOLS = [
1349
+ "mem_session_start",
1350
+ "mem_session_summary",
1351
+ "mem_save_prompt"
1352
+ ];
1353
+ var READ_RECALL_CHAIN = [
1354
+ "mem_search",
1355
+ "mem_timeline",
1356
+ "mem_get_observation"
1357
+ ];
1358
+ var WRITE_CAPABLE_DELEGATED_TOOLS = [
1359
+ "mem_save",
1360
+ "mem_search",
1361
+ "mem_get_observation",
1362
+ "mem_timeline",
1363
+ "mem_suggest_topic_key"
1364
+ ];
1365
+ var ALL_MEMORY_TOOLS = [
1366
+ ...ROOT_OWNED_TOOLS,
1367
+ ...READ_RECALL_CHAIN,
1368
+ "mem_suggest_topic_key",
1369
+ "mem_save"
1370
+ ];
1371
+ function uniqueTools(tools) {
1372
+ return [...new Set(tools)];
1373
+ }
1374
+ function roleAllowedTools(role) {
1375
+ if (role.name === "orchestrator") {
1376
+ return uniqueTools([...ROOT_OWNED_TOOLS, ...WRITE_CAPABLE_DELEGATED_TOOLS]);
1377
+ }
1378
+ if (role.mode === "read-only") {
1379
+ return [...READ_RECALL_CHAIN];
1380
+ }
1381
+ return [...WRITE_CAPABLE_DELEGATED_TOOLS];
1382
+ }
1383
+ function roleRules(role) {
1384
+ const sharedSubagentRules = [
1385
+ "Every subagent memory call requires the parent session_id and project from dispatch; if either is missing, do not call thoth-mem.",
1386
+ "Never call mem_session_start, mem_session_summary, or mem_save_prompt; those tools are root/orchestrator-owned.",
1387
+ "Protect the sdd/* topic namespace; SDD artifacts may use deterministic sdd/{change}/{artifact} topic keys only in persistence modes that include thoth-mem."
1388
+ ];
1389
+ if (role.name === "orchestrator") {
1390
+ return [
1391
+ "mem_session_start, mem_session_summary, and mem_save_prompt are root/main orchestrator-owned tools and responsibilities.",
1392
+ "In harnesses without an orchestrator-named agent, root/main orchestrator-owned means the initial/root agent when the harness does not expose an orchestrator-named agent.",
1393
+ "Dispatch parent session_id and project when authorizing subagent memory use.",
1394
+ "Protect the sdd/* topic namespace and write SDD memory artifacts only in thoth-mem or hybrid persistence modes."
1395
+ ];
1396
+ }
1397
+ if (role.mode === "read-only") {
1398
+ return [
1399
+ ...sharedSubagentRules,
1400
+ "Read-only agents may only perform bounded, project-scoped recall with mem_search -> mem_timeline -> mem_get_observation when authorized.",
1401
+ "Read-only agents must never write durable memory."
1402
+ ];
1403
+ }
1404
+ return [
1405
+ ...sharedSubagentRules,
1406
+ "Write-capable agents may call mem_save only for delegated durable observations under the parent session/project.",
1407
+ "For reads, use only mem_search -> mem_timeline -> mem_get_observation."
1408
+ ];
1409
+ }
1410
+ function getRoleMemoryGovernance(role) {
1411
+ const allowedTools = roleAllowedTools(role);
1412
+ return {
1413
+ role: role.name,
1414
+ rootOwnedTools: [...ROOT_OWNED_TOOLS],
1415
+ allowedTools,
1416
+ forbiddenTools: ALL_MEMORY_TOOLS.filter(
1417
+ (tool) => !allowedTools.includes(tool)
1418
+ ),
1419
+ requiresParentContext: role.name !== "orchestrator",
1420
+ mayReadProjectMemory: role.name !== "orchestrator",
1421
+ mayWriteDurableObservations: role.mode === "write-capable",
1422
+ protectsSddNamespace: true,
1423
+ rules: roleRules(role)
1424
+ };
1425
+ }
1426
+ function renderMemoryGovernanceInstructions(role, dialect) {
1427
+ const governance = getRoleMemoryGovernance(role);
1428
+ const harnessRules = dialect ? [
1429
+ `- Harness wording: use ${dialect.renderRoleInvocation("orchestrator")} as the memory owner and \`${dialect.tools.userQuestionTool}\` for blocking memory-context questions.`,
1430
+ `- Progress ownership remains with the coordinator; report memory-governance verification for tracking in ${dialect.tools.progressTool}.`
1431
+ ] : [];
1432
+ return [
1433
+ "thoth-mem governance:",
1434
+ ...governance.rules.map((rule) => `- ${rule}`),
1435
+ ...harnessRules,
1436
+ `- Runtime enforcement: ${role.name === "orchestrator" ? "root-owned" : "instruction-level unless the target harness validates per-agent memory controls"}.`
1437
+ ].join("\n");
1438
+ }
1439
+ function memoryGovernanceDiagnostics(input) {
1440
+ const diagnostics = [];
1441
+ if (input.permissionControls !== "supported") {
1442
+ diagnostics.push({
1443
+ severity: "warning",
1444
+ code: `${input.harness}.permission.memory.enforcement_gap`,
1445
+ message: "Runtime controls for root-only memory tools are unavailable; governance is rendered as instruction-level guidance.",
1446
+ harness: input.harness,
1447
+ capability: "rolePermissions",
1448
+ fallback: "instruction-only"
1449
+ });
1450
+ }
1451
+ if (input.parentContextInjection !== "supported") {
1452
+ diagnostics.push({
1453
+ severity: input.parentContextInjection === "unknown" ? "error" : "warning",
1454
+ code: `${input.harness}.context.parent_injection.unvalidated`,
1455
+ message: "Parent session_id/project injection is not runtime-enforced; subagents must be instructed not to use memory without explicit dispatch context.",
1456
+ harness: input.harness,
1457
+ capability: "parentContextInjection",
1458
+ fallback: "instruction-only"
1459
+ });
1460
+ }
1461
+ if (input.memoryWriteControls !== "supported") {
1462
+ diagnostics.push({
1463
+ severity: "warning",
1464
+ code: `${input.harness}.permission.memory_write.enforcement_gap`,
1465
+ message: "Runtime controls for delegated memory writes are unavailable; write-capable agents receive instruction-level mem_save limits only.",
1466
+ harness: input.harness,
1467
+ capability: "memoryGovernanceEnforcement",
1468
+ fallback: "instruction-only"
1469
+ });
1470
+ }
1471
+ return diagnostics;
1472
+ }
1473
+
1474
+ // src/harness/core/skills.ts
1475
+ var ORCHESTRATOR_ONLY = ["orchestrator"];
1476
+ var SHARED_SKILL_SUPPORT = {
1477
+ name: "_shared",
1478
+ description: "Shared OpenSpec, persistence, and thoth-mem SDD conventions",
1479
+ allowedRoles: [
1480
+ "orchestrator",
1481
+ "explorer",
1482
+ "librarian",
1483
+ "oracle",
1484
+ "designer",
1485
+ "quick",
1486
+ "deep"
1487
+ ],
1488
+ sourcePath: "src/skills/_shared",
1489
+ kind: "shared-support",
1490
+ purpose: "support"
1491
+ };
1492
+ var BUNDLED_SKILL_REGISTRY = [
1493
+ {
1494
+ name: "requirements-interview",
1495
+ description: "Mandatory step-0 discovery interview to understand user intent, clarify scope, and choose the right path before implementation",
1496
+ allowedRoles: ORCHESTRATOR_ONLY,
1497
+ sourcePath: "src/skills/requirements-interview",
1498
+ kind: "skill",
1499
+ purpose: "requirements"
1500
+ },
1501
+ {
1502
+ name: "thoth-mem-agents",
1503
+ description: "Orchestrator/subagent thoth-mem workflow contract for parent session_id/project ownership, prompt-save prohibitions, and safe durable memory usage",
1504
+ allowedRoles: [
1505
+ "orchestrator",
1506
+ "explorer",
1507
+ "librarian",
1508
+ "oracle",
1509
+ "designer",
1510
+ "quick",
1511
+ "deep"
1512
+ ],
1513
+ sourcePath: "src/skills/thoth-mem-agents",
1514
+ kind: "skill",
1515
+ purpose: "memory"
1516
+ },
1517
+ {
1518
+ name: "plan-reviewer",
1519
+ description: "Review SDD task plans for execution blockers and valid references",
1520
+ allowedRoles: ["orchestrator", "oracle"],
1521
+ sourcePath: "src/skills/plan-reviewer",
1522
+ kind: "skill",
1523
+ purpose: "review"
1524
+ },
1525
+ {
1526
+ name: "sdd-init",
1527
+ description: "Initialize OpenSpec structure and SDD project context",
1528
+ allowedRoles: ORCHESTRATOR_ONLY,
1529
+ sourcePath: "src/skills/sdd-init",
1530
+ kind: "skill",
1531
+ purpose: "sdd"
1532
+ },
1533
+ {
1534
+ name: "sdd-propose",
1535
+ description: "Create change proposals for OpenSpec workflows",
1536
+ allowedRoles: ORCHESTRATOR_ONLY,
1537
+ sourcePath: "src/skills/sdd-propose",
1538
+ kind: "skill",
1539
+ purpose: "sdd"
1540
+ },
1541
+ {
1542
+ name: "sdd-spec",
1543
+ description: "Write OpenSpec delta specifications",
1544
+ allowedRoles: ORCHESTRATOR_ONLY,
1545
+ sourcePath: "src/skills/sdd-spec",
1546
+ kind: "skill",
1547
+ purpose: "sdd"
1548
+ },
1549
+ {
1550
+ name: "sdd-design",
1551
+ description: "Create technical design artifacts for changes",
1552
+ allowedRoles: ORCHESTRATOR_ONLY,
1553
+ sourcePath: "src/skills/sdd-design",
1554
+ kind: "skill",
1555
+ purpose: "sdd"
1556
+ },
1557
+ {
1558
+ name: "sdd-tasks",
1559
+ description: "Generate phased implementation task checklists",
1560
+ allowedRoles: ORCHESTRATOR_ONLY,
1561
+ sourcePath: "src/skills/sdd-tasks",
1562
+ kind: "skill",
1563
+ purpose: "sdd"
1564
+ },
1565
+ {
1566
+ name: "sdd-apply",
1567
+ description: "Execute tasks and persist implementation progress",
1568
+ allowedRoles: ORCHESTRATOR_ONLY,
1569
+ sourcePath: "src/skills/sdd-apply",
1570
+ kind: "skill",
1571
+ purpose: "sdd"
1572
+ },
1573
+ {
1574
+ name: "executing-plans",
1575
+ description: "Execute SDD task lists with real-time progress tracking, sub-agent dispatch, and verification checkpoints",
1576
+ allowedRoles: ORCHESTRATOR_ONLY,
1577
+ sourcePath: "src/skills/executing-plans",
1578
+ kind: "skill",
1579
+ purpose: "sdd"
1580
+ },
1581
+ {
1582
+ name: "sdd-verify",
1583
+ description: "Build verification reports and compliance matrices",
1584
+ allowedRoles: ORCHESTRATOR_ONLY,
1585
+ sourcePath: "src/skills/sdd-verify",
1586
+ kind: "skill",
1587
+ purpose: "sdd"
1588
+ },
1589
+ {
1590
+ name: "sdd-archive",
1591
+ description: "Archive completed OpenSpec changes with audit trails",
1592
+ allowedRoles: ORCHESTRATOR_ONLY,
1593
+ sourcePath: "src/skills/sdd-archive",
1594
+ kind: "skill",
1595
+ purpose: "sdd"
1596
+ }
1597
+ ];
1598
+ var SKILL_REGISTRY = [
1599
+ ...BUNDLED_SKILL_REGISTRY,
1600
+ SHARED_SKILL_SUPPORT
1601
+ ];
1602
+ function getSkillRegistry() {
1603
+ return SKILL_REGISTRY.map((entry) => ({
1604
+ ...entry,
1605
+ allowedRoles: [...entry.allowedRoles]
1606
+ }));
1607
+ }
1608
+
1609
+ // src/harness/writers/codex-plugin-package.ts
1610
+ import { createHash } from "crypto";
1611
+
1612
+ // src/harness/adapters/codex-surfaces.ts
1613
+ var CODEX_HOOK_EVENTS = [
1614
+ "SessionStart",
1615
+ "UserPromptSubmit",
1616
+ "PreToolUse",
1617
+ "PermissionRequest",
1618
+ "PostToolUse",
1619
+ "Stop"
1620
+ ];
1621
+ var SUPPORTED_CODEX_HOOK_OUTPUT_FIELDS = ["message"];
1622
+ var CODEX_PLUGIN_MANIFEST_FIELDS = [
1623
+ "name",
1624
+ "version",
1625
+ "description",
1626
+ "skills",
1627
+ "mcpServers",
1628
+ "apps",
1629
+ "hooks",
1630
+ "interface"
1631
+ ];
1632
+ var CODEX_SURFACES = [
1633
+ {
1634
+ id: "project-agent-toml",
1635
+ target: "agent-definition",
1636
+ status: "validated",
1637
+ artifactKind: "agent-config",
1638
+ path: ".codex/agents/{name}.toml",
1639
+ fields: [
1640
+ "name",
1641
+ "description",
1642
+ "developer_instructions",
1643
+ "model",
1644
+ "model_reasoning_effort",
1645
+ "sandbox_mode"
1646
+ ],
1647
+ diagnosticCode: "codex.surface.agent_definition.validated",
1648
+ summary: "Project-scoped custom agents are standalone TOML files.",
1649
+ evidence: "OpenAI Codex Subagents docs: ~/.codex/agents/ and .codex/agents/ with required name, description, developer_instructions."
1650
+ },
1651
+ {
1652
+ id: "project-config-toml",
1653
+ target: "config",
1654
+ status: "validated",
1655
+ artifactKind: "harness-config",
1656
+ path: ".codex/config.toml",
1657
+ fields: [
1658
+ "model",
1659
+ "model_reasoning_effort",
1660
+ "approval_policy",
1661
+ "sandbox_mode",
1662
+ "features",
1663
+ "mcp_servers",
1664
+ "skills.config",
1665
+ "agents"
1666
+ ],
1667
+ diagnosticCode: "codex.surface.config.validated",
1668
+ summary: "Codex supports project-scoped config TOML after trust.",
1669
+ evidence: "OpenAI Codex Configuration Reference: ~/.codex/config.toml and project .codex/config.toml overrides."
1670
+ },
1671
+ {
1672
+ id: "mcp-server-config",
1673
+ target: "mcp-config",
1674
+ status: "validated",
1675
+ artifactKind: "mcp-config",
1676
+ path: ".codex/config.toml",
1677
+ fields: ["mcp_servers.<id>", "url", "command", "args", "env", "tools"],
1678
+ diagnosticCode: "codex.surface.mcp.validated",
1679
+ summary: "MCP servers are configured through Codex config TOML.",
1680
+ evidence: "OpenAI Codex config docs describe mcp_servers and per-tool approval overrides."
1681
+ },
1682
+ {
1683
+ id: "repo-skills-directory",
1684
+ target: "skill-directory",
1685
+ status: "validated",
1686
+ artifactKind: "skill",
1687
+ path: ".agents/skills/{skill}/SKILL.md",
1688
+ fields: ["SKILL.md", "scripts/", "references/", "assets/"],
1689
+ diagnosticCode: "codex.surface.skills.validated",
1690
+ summary: "Repository skills are discovered from .agents/skills.",
1691
+ evidence: "OpenAI Codex Skills docs: repo skills are scanned from .agents/skills up to the repository root."
1692
+ },
1693
+ {
1694
+ id: "project-hooks-json",
1695
+ target: "hook-config",
1696
+ status: "validated",
1697
+ artifactKind: "hook-config",
1698
+ path: ".codex/hooks.json",
1699
+ fields: [
1700
+ "SessionStart.command",
1701
+ "UserPromptSubmit.command",
1702
+ "PreToolUse.command",
1703
+ "PermissionRequest.command",
1704
+ "PostToolUse.command",
1705
+ "Stop.command"
1706
+ ],
1707
+ diagnosticCode: "codex.surface.hooks_json.validated",
1708
+ summary: "Codex supports project-local hooks.json command handlers.",
1709
+ evidence: "OpenAI Codex hooks docs describe hooks.json with SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, PostToolUse, and Stop command handlers."
1710
+ },
1711
+ {
1712
+ id: "inline-hooks-table",
1713
+ target: "hook-config",
1714
+ status: "validated",
1715
+ artifactKind: "hook-config",
1716
+ path: ".codex/config.toml",
1717
+ fields: [
1718
+ "hooks.SessionStart.command",
1719
+ "hooks.UserPromptSubmit.command",
1720
+ "hooks.PreToolUse.command",
1721
+ "hooks.PermissionRequest.command",
1722
+ "hooks.PostToolUse.command",
1723
+ "hooks.Stop.command"
1724
+ ],
1725
+ diagnosticCode: "codex.surface.inline_hooks.validated",
1726
+ summary: "Codex supports inline [hooks] configuration in TOML.",
1727
+ evidence: "OpenAI Codex configuration docs describe inline [hooks] settings gated by trusted project configuration."
1728
+ },
1729
+ {
1730
+ id: "features-hooks-toggle",
1731
+ target: "hook-config",
1732
+ status: "validated",
1733
+ artifactKind: "hook-config",
1734
+ path: ".codex/config.toml",
1735
+ fields: ["features.hooks"],
1736
+ diagnosticCode: "codex.surface.features_hooks.validated",
1737
+ summary: "Codex hook loading is controlled by the features.hooks toggle.",
1738
+ evidence: "OpenAI Codex configuration docs require enabling [features].hooks before project hook configuration is active."
1739
+ },
1740
+ {
1741
+ id: "plugin-hooks-bundle",
1742
+ target: "hook-config",
1743
+ status: "validated",
1744
+ artifactKind: "hook-config",
1745
+ path: ".codex/plugins/{plugin}/hooks.json",
1746
+ fields: ["features.plugin_hooks", "plugin.hooks", "plugin.trust_review"],
1747
+ diagnosticCode: "codex.surface.plugin_hooks.validated",
1748
+ summary: "Codex plugin hook bundles are documented behind plugin hook feature and trust review gates.",
1749
+ evidence: "OpenAI Codex plugin docs describe bundled hook configuration requiring plugin_hooks enablement and trust review."
1750
+ },
1751
+ {
1752
+ id: "plugin-manifest-json",
1753
+ target: "plugin-manifest",
1754
+ status: "validated",
1755
+ artifactKind: "manifest",
1756
+ path: ".codex-plugin/plugin.json",
1757
+ fields: [...CODEX_PLUGIN_MANIFEST_FIELDS],
1758
+ diagnosticCode: "codex.surface.plugin_manifest.validated",
1759
+ summary: "Codex plugin packages are described by a plugin-root plugin.json manifest.",
1760
+ evidence: "OpenAI Codex plugin docs describe plugin.json with official fields including name, version, description, skills, mcpServers, apps, hooks, and interface."
1761
+ },
1762
+ {
1763
+ id: "plugin-skills-directory",
1764
+ target: "skill-directory",
1765
+ status: "validated",
1766
+ artifactKind: "skill",
1767
+ path: ".codex-plugin/skills/{skill}/SKILL.md",
1768
+ fields: ["skills", "./skills/"],
1769
+ diagnosticCode: "codex.surface.plugin_skills.validated",
1770
+ summary: "Codex plugin packages can bundle skills under plugin-root skills/.",
1771
+ evidence: "OpenAI Codex plugin docs describe plugin-bundled skills referenced from plugin.json using plugin-root relative paths."
1772
+ },
1773
+ {
1774
+ id: "plugin-hooks-json",
1775
+ target: "hook-config",
1776
+ status: "validated",
1777
+ artifactKind: "hook-config",
1778
+ path: ".codex-plugin/hooks/hooks.json",
1779
+ fields: ["hooks", "./hooks/hooks.json"],
1780
+ diagnosticCode: "codex.surface.plugin_hooks_json.validated",
1781
+ summary: "Codex plugin packages can bundle hook configuration under plugin-root hooks/.",
1782
+ evidence: "OpenAI Codex plugin docs describe hook bundle assets referenced from plugin.json and gated by plugin_hooks plus trust review."
1783
+ },
1784
+ {
1785
+ id: "plugin-mcp-json",
1786
+ target: "mcp-config",
1787
+ status: "validated",
1788
+ artifactKind: "mcp-config",
1789
+ path: ".codex-plugin/.mcp.json",
1790
+ fields: ["mcpServers", "./.mcp.json"],
1791
+ diagnosticCode: "codex.surface.plugin_mcp_json.validated",
1792
+ summary: "Codex plugin packages can bundle MCP server definitions in plugin-root .mcp.json.",
1793
+ evidence: "OpenAI Codex plugin docs describe bundled .mcp.json server definitions referenced from plugin.json using plugin-root relative paths."
1794
+ },
1795
+ {
1796
+ id: "inline-hooks",
1797
+ target: "hook-config",
1798
+ status: "unknown",
1799
+ artifactKind: "hook-config",
1800
+ fields: ["features.hooks", "hooks"],
1801
+ diagnosticCode: "codex.surface.hooks.unvalidated",
1802
+ summary: "Lifecycle hook availability is documented, but hook event shape and parity with OpenCode runtime hooks is not validated for this adapter.",
1803
+ evidence: "Codex config reference mentions hooks; this implementation has not validated exact event schemas for plugin parity.",
1804
+ fallback: "diagnostic-only"
1805
+ },
1806
+ {
1807
+ id: "per-agent-runtime-permissions",
1808
+ target: "permission-control",
1809
+ status: "unsupported",
1810
+ fields: ["per-agent tool allow/deny equivalent to OpenCode permissions"],
1811
+ diagnosticCode: "codex.permission.memory.enforcement_gap",
1812
+ summary: "Codex sandbox and approval policy can constrain sessions, but OpenCode-style per-agent MCP/tool deny maps are not validated.",
1813
+ evidence: "Codex approvals/security docs cover sandbox and approval policies, not exact OpenCode per-agent permission maps.",
1814
+ fallback: "instruction-only"
1815
+ },
1816
+ {
1817
+ id: "programmatic-delegation-runtime",
1818
+ target: "delegation-runtime",
1819
+ status: "unsupported",
1820
+ fields: [
1821
+ "task API",
1822
+ "background task sessions",
1823
+ "tmux lifecycle hooks",
1824
+ "automatic subagent session close"
1825
+ ],
1826
+ diagnosticCode: "codex.delegation.runtime.unsupported",
1827
+ summary: "Codex subagents are user/instruction-triggered; no OpenCode plugin task API or automatic subagent session close parity is validated.",
1828
+ evidence: "Codex subagents docs describe manual spawning and agent threads, not this plugin runtime task API or a validated runtime close hook.",
1829
+ fallback: "instruction-only"
1830
+ },
1831
+ {
1832
+ id: "parent-context-injection",
1833
+ target: "parent-context-injection",
1834
+ status: "unknown",
1835
+ fields: ["parent session_id", "project"],
1836
+ diagnosticCode: "codex.context.parent_injection.unvalidated",
1837
+ summary: "No machine-enforced parent context injection mechanism is validated; prompts must instruct users to include parent session_id/project.",
1838
+ evidence: "No validated Codex surface in Phase 1 proves automatic parent context injection into spawned agents.",
1839
+ fallback: "instruction-only"
1840
+ }
1841
+ ];
1842
+ function getCodexSurfaceRecords() {
1843
+ return CODEX_SURFACES.map((surface) => ({
1844
+ ...surface,
1845
+ fields: [...surface.fields]
1846
+ }));
1847
+ }
1848
+ function getCodexSurface(id) {
1849
+ return getCodexSurfaceRecords().find((surface) => surface.id === id);
1850
+ }
1851
+ function assertCodexSurfaceCanGenerate(id) {
1852
+ const surface = getCodexSurface(id);
1853
+ if (!surface) {
1854
+ return {
1855
+ ok: false,
1856
+ diagnostic: {
1857
+ severity: "error",
1858
+ code: "harness.surface_unvalidated",
1859
+ message: `Codex surface "${id}" is not registered and cannot generate artifacts.`,
1860
+ harness: "codex",
1861
+ surface: id,
1862
+ fallback: "diagnostic-only"
1863
+ }
1864
+ };
1865
+ }
1866
+ if (surface.status !== "validated" || !surface.artifactKind) {
1867
+ return {
1868
+ ok: false,
1869
+ diagnostic: codexSurfaceDiagnostic(surface)
1870
+ };
1871
+ }
1872
+ return { ok: true, surface };
1873
+ }
1874
+ function codexSurfaceDiagnostic(surface) {
1875
+ const severity = surface.status === "unsupported" ? "warning" : "error";
1876
+ return {
1877
+ severity,
1878
+ code: surface.diagnosticCode,
1879
+ message: `${surface.summary} Artifact generation is disabled for this surface.`,
1880
+ harness: "codex",
1881
+ surface: surface.id,
1882
+ fallback: surface.fallback ?? "diagnostic-only"
1883
+ };
1884
+ }
1885
+ function normalizeCodexPath(value) {
1886
+ return value.replace(/\\/g, "/");
1887
+ }
1888
+ function codexPluginPackageDiagnostic(code, message, surface) {
1889
+ return {
1890
+ severity: "error",
1891
+ code,
1892
+ message,
1893
+ harness: "codex",
1894
+ surface,
1895
+ fallback: "diagnostic-only"
1896
+ };
1897
+ }
1898
+ function isPathUnderCodexPlugin(pathValue) {
1899
+ const normalized = normalizeCodexPath(pathValue);
1900
+ const segments = normalized.split("/");
1901
+ return !normalized.startsWith("/") && !/^[A-Za-z]:\//.test(normalized) && !segments.includes("..") && (normalized === ".codex-plugin" || normalized === ".codex-plugin/" || normalized.startsWith(".codex-plugin/"));
1902
+ }
1903
+ function fieldAllowedForPluginSurface(surface, field) {
1904
+ if (surface.id === "plugin-manifest-json") {
1905
+ return CODEX_PLUGIN_MANIFEST_FIELDS.includes(
1906
+ field
1907
+ );
1908
+ }
1909
+ return surface.fields.includes(field);
1910
+ }
1911
+ function validateCodexPluginPackageSurface(input) {
1912
+ const canGenerate = assertCodexSurfaceCanGenerate(input.surfaceId);
1913
+ if (!canGenerate.ok) {
1914
+ return {
1915
+ ok: false,
1916
+ diagnostics: [
1917
+ {
1918
+ ...canGenerate.diagnostic,
1919
+ code: "codex.plugin.surface.unvalidated",
1920
+ message: `Codex plugin package surface "${input.surfaceId}" is not validated for .codex-plugin artifacts.`
1921
+ }
1922
+ ]
1923
+ };
1924
+ }
1925
+ const diagnostics = [];
1926
+ const surface = canGenerate.surface;
1927
+ if (!surface.path?.startsWith(".codex-plugin")) {
1928
+ diagnostics.push(
1929
+ codexPluginPackageDiagnostic(
1930
+ "codex.plugin.surface.unvalidated",
1931
+ `Codex surface "${input.surfaceId}" is validated, but not as a .codex-plugin package surface.`,
1932
+ input.surfaceId
1933
+ )
1934
+ );
1935
+ }
1936
+ if (input.path && !isPathUnderCodexPlugin(input.path)) {
1937
+ diagnostics.push(
1938
+ codexPluginPackageDiagnostic(
1939
+ "codex.plugin.path.unvalidated",
1940
+ `Codex plugin package path "${input.path}" must stay under .codex-plugin/.`,
1941
+ input.surfaceId
1942
+ )
1943
+ );
1944
+ }
1945
+ for (const field of input.fields ?? []) {
1946
+ if (!fieldAllowedForPluginSurface(surface, field)) {
1947
+ diagnostics.push(
1948
+ codexPluginPackageDiagnostic(
1949
+ "codex.plugin.field.unvalidated",
1950
+ `Codex plugin field "${field}" is not validated for surface "${input.surfaceId}".`,
1951
+ input.surfaceId
1952
+ )
1953
+ );
1954
+ }
1955
+ }
1956
+ if (diagnostics.length > 0) return { ok: false, diagnostics };
1957
+ return { ok: true, surface };
1958
+ }
1959
+ function isCodexHookEvent(event) {
1960
+ return CODEX_HOOK_EVENTS.includes(event);
1961
+ }
1962
+ function codexHookDiagnostic(code, message) {
1963
+ return {
1964
+ severity: "warning",
1965
+ code,
1966
+ message,
1967
+ harness: "codex",
1968
+ surface: "hook-config",
1969
+ fallback: "diagnostic-only"
1970
+ };
1971
+ }
1972
+ function inferHookHandlerType(handler) {
1973
+ if (!handler) return void 0;
1974
+ if (typeof handler.type === "string") return handler.type;
1975
+ if ("command" in handler) return "command";
1976
+ if ("prompt" in handler) return "prompt";
1977
+ if ("agent" in handler) return "agent";
1978
+ return void 0;
1979
+ }
1980
+ function validateCodexHookSurface(input) {
1981
+ const diagnostics = [];
1982
+ const handlerType = inferHookHandlerType(input.handler);
1983
+ if (!isCodexHookEvent(input.event)) {
1984
+ diagnostics.push(
1985
+ codexHookDiagnostic(
1986
+ "codex.hooks.event.unsupported",
1987
+ `Codex hook event "${input.event}" is not documented for this adapter.`
1988
+ )
1989
+ );
1990
+ }
1991
+ if (handlerType !== "command") {
1992
+ diagnostics.push(
1993
+ codexHookDiagnostic(
1994
+ handlerType === "prompt" ? "codex.hooks.handler.prompt_unsupported" : handlerType === "agent" ? "codex.hooks.handler.agent_unsupported" : "codex.hooks.handler.unsupported",
1995
+ `Codex hook handler "${handlerType ?? "unknown"}" is not supported; only command handlers are validated.`
1996
+ )
1997
+ );
1998
+ }
1999
+ if (input.handler?.async === true) {
2000
+ diagnostics.push(
2001
+ codexHookDiagnostic(
2002
+ "codex.hooks.async.unsupported",
2003
+ "Async Codex hook execution is not validated for this adapter."
2004
+ )
2005
+ );
2006
+ }
2007
+ const unsupportedOutputField = input.outputFields?.find(
2008
+ (field) => !SUPPORTED_CODEX_HOOK_OUTPUT_FIELDS.includes(field)
2009
+ );
2010
+ if (unsupportedOutputField) {
2011
+ diagnostics.push(
2012
+ codexHookDiagnostic(
2013
+ "codex.hooks.output_field.unsupported",
2014
+ `Codex hook output field "${unsupportedOutputField}" is not supported by the validated hook surface.`
2015
+ )
2016
+ );
2017
+ }
2018
+ if (input.interceptsToolExecution) {
2019
+ diagnostics.push(
2020
+ codexHookDiagnostic(
2021
+ "codex.hooks.tool_interception.unsupported",
2022
+ "Full tool interception is not supported; Codex hooks are diagnostic/config surfaces, not OpenCode runtime enforcement hooks."
2023
+ )
2024
+ );
2025
+ }
2026
+ if (diagnostics.length > 0 || !isCodexHookEvent(input.event)) {
2027
+ return { ok: false, diagnostics };
2028
+ }
2029
+ return { ok: true, event: input.event, handlerType: "command" };
2030
+ }
2031
+
2032
+ // src/harness/writers/codex-plugin-package.ts
2033
+ var PLUGIN_MANIFEST_SURFACE_ID = "plugin-manifest-json";
2034
+ function normalizePath(value) {
2035
+ return value.replace(/\\/g, "/");
2036
+ }
2037
+ function pluginRootReference(pathValue) {
2038
+ const normalized = normalizePath(pathValue);
2039
+ const relative4 = normalized.slice(".codex-plugin/".length);
2040
+ return `./${relative4}`;
2041
+ }
2042
+ function sha256(content) {
2043
+ return `sha256:${createHash("sha256").update(content).digest("hex")}`;
2044
+ }
2045
+ function isRecord(value) {
2046
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2047
+ }
2048
+ function stableValue(value) {
2049
+ if (Array.isArray(value)) return value.map(stableValue);
2050
+ if (!isRecord(value)) return value;
2051
+ return Object.fromEntries(
2052
+ Object.keys(value).sort((left, right) => left.localeCompare(right)).map((key) => [key, stableValue(value[key])])
2053
+ );
2054
+ }
2055
+ function stableJson(value) {
2056
+ return `${JSON.stringify(value, null, 2)}
2057
+ `;
2058
+ }
2059
+ function orderManifestFields(manifest) {
2060
+ const ordered = {};
2061
+ for (const field of CODEX_PLUGIN_MANIFEST_FIELDS) {
2062
+ if (field in manifest) ordered[field] = manifest[field];
2063
+ }
2064
+ return ordered;
2065
+ }
2066
+ function fieldDiagnostic(field) {
2067
+ return {
2068
+ severity: "error",
2069
+ code: "codex.plugin.field.unvalidated",
2070
+ message: `Skipping unvalidated Codex plugin manifest field "${field}".`,
2071
+ harness: "codex",
2072
+ surface: PLUGIN_MANIFEST_SURFACE_ID,
2073
+ fallback: "diagnostic-only"
2074
+ };
2075
+ }
2076
+ function artifactKindForSurface(surfaceId) {
2077
+ if (surfaceId === "plugin-mcp-json") return "mcp-config";
2078
+ return surfaceId === "plugin-hooks-json" ? "hook-config" : "skill";
2079
+ }
2080
+ function codexHookPackageDiagnostic(code, message) {
2081
+ return {
2082
+ severity: "warning",
2083
+ code,
2084
+ message,
2085
+ harness: "codex",
2086
+ surface: "plugin-hooks-json",
2087
+ fallback: "diagnostic-only"
2088
+ };
2089
+ }
2090
+ function normalizeHookDefinition(hook) {
2091
+ return {
2092
+ command: hook.handler.command,
2093
+ ...hook.outputFields?.length ? { output: [...hook.outputFields].sort() } : {}
2094
+ };
2095
+ }
2096
+ function renderHookDefinitions(hookDefinitions) {
2097
+ const diagnostics = [];
2098
+ const hooksByEvent = /* @__PURE__ */ new Map();
2099
+ for (const hook of hookDefinitions) {
2100
+ const validation = validateCodexHookSurface(hook);
2101
+ if (!validation.ok) {
2102
+ diagnostics.push(...validation.diagnostics);
2103
+ continue;
2104
+ }
2105
+ const eventHooks = hooksByEvent.get(validation.event) ?? [];
2106
+ eventHooks.push(normalizeHookDefinition(hook));
2107
+ hooksByEvent.set(validation.event, eventHooks);
2108
+ }
2109
+ if (hooksByEvent.size === 0) {
2110
+ diagnostics.push(
2111
+ codexHookPackageDiagnostic(
2112
+ "codex.plugin.hooks.none_packaged",
2113
+ "No Codex plugin hooks were packaged because no hook definitions passed existing Codex hook validation."
2114
+ )
2115
+ );
2116
+ return { diagnostics };
2117
+ }
2118
+ const content = stableJson(
2119
+ Object.fromEntries(
2120
+ [...hooksByEvent.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([event, hooks]) => [event, hooks.map(stableValue)])
2121
+ )
2122
+ );
2123
+ return { content, diagnostics };
2124
+ }
2125
+ function parseRawHookContent(content) {
2126
+ const diagnostics = [];
2127
+ const hookDefinitions = [];
2128
+ let parsed;
2129
+ try {
2130
+ parsed = JSON.parse(content);
2131
+ } catch {
2132
+ return {
2133
+ hookDefinitions,
2134
+ diagnostics: [
2135
+ codexHookPackageDiagnostic(
2136
+ "codex.plugin.hooks.invalid_json",
2137
+ "Skipping Codex plugin hooks asset because hooks.json content is not valid JSON."
2138
+ )
2139
+ ]
2140
+ };
2141
+ }
2142
+ if (!isRecord(parsed)) {
2143
+ return {
2144
+ hookDefinitions,
2145
+ diagnostics: [
2146
+ codexHookPackageDiagnostic(
2147
+ "codex.plugin.hooks.invalid_shape",
2148
+ "Skipping Codex plugin hooks asset because hooks.json must be an object keyed by Codex hook event."
2149
+ )
2150
+ ]
2151
+ };
2152
+ }
2153
+ for (const [event, definitions] of Object.entries(parsed)) {
2154
+ if (!Array.isArray(definitions)) {
2155
+ diagnostics.push(
2156
+ codexHookPackageDiagnostic(
2157
+ "codex.plugin.hooks.invalid_shape",
2158
+ `Skipping Codex plugin hook event "${event}" because its value is not an array of command handlers.`
2159
+ )
2160
+ );
2161
+ continue;
2162
+ }
2163
+ for (const definition of definitions) {
2164
+ if (!isRecord(definition)) {
2165
+ diagnostics.push(
2166
+ codexHookPackageDiagnostic(
2167
+ "codex.plugin.hooks.invalid_shape",
2168
+ `Skipping Codex plugin hook event "${event}" entry because it is not an object.`
2169
+ )
2170
+ );
2171
+ continue;
2172
+ }
2173
+ const output = definition.output;
2174
+ hookDefinitions.push({
2175
+ event,
2176
+ handler: {
2177
+ type: typeof definition.type === "string" ? definition.type : "command",
2178
+ command: definition.command,
2179
+ async: definition.async
2180
+ },
2181
+ outputFields: Array.isArray(output) ? output.filter((field) => typeof field === "string") : void 0,
2182
+ interceptsToolExecution: definition.interceptsToolExecution === true
2183
+ });
2184
+ }
2185
+ }
2186
+ return { hookDefinitions, diagnostics };
2187
+ }
2188
+ function resolveHookAssetContent(asset) {
2189
+ if (asset.surfaceId !== "plugin-hooks-json") {
2190
+ return { content: asset.content, diagnostics: [] };
2191
+ }
2192
+ const fromInput = asset.hookDefinitions ? { hookDefinitions: [...asset.hookDefinitions], diagnostics: [] } : asset.content !== void 0 ? parseRawHookContent(asset.content) : { hookDefinitions: [], diagnostics: [] };
2193
+ const rendered = renderHookDefinitions(fromInput.hookDefinitions);
2194
+ return {
2195
+ content: rendered.content,
2196
+ diagnostics: [...fromInput.diagnostics, ...rendered.diagnostics]
2197
+ };
2198
+ }
2199
+ function renderCodexPluginPackage(input) {
2200
+ const artifacts = [];
2201
+ const diagnostics = [];
2202
+ const manifest = {};
2203
+ const provenance = [];
2204
+ for (const field of Object.keys(input.manifest)) {
2205
+ if (!CODEX_PLUGIN_MANIFEST_FIELDS.includes(
2206
+ field
2207
+ )) {
2208
+ diagnostics.push(fieldDiagnostic(field));
2209
+ }
2210
+ }
2211
+ const manifestValidation = validateCodexPluginPackageSurface({
2212
+ surfaceId: PLUGIN_MANIFEST_SURFACE_ID,
2213
+ path: ".codex-plugin/plugin.json",
2214
+ fields: Object.keys(input.manifest).filter(
2215
+ (field) => CODEX_PLUGIN_MANIFEST_FIELDS.includes(
2216
+ field
2217
+ )
2218
+ )
2219
+ });
2220
+ if (!manifestValidation.ok)
2221
+ diagnostics.push(...manifestValidation.diagnostics);
2222
+ for (const field of CODEX_PLUGIN_MANIFEST_FIELDS) {
2223
+ if (field in input.manifest)
2224
+ manifest[field] = stableValue(input.manifest[field]);
2225
+ }
2226
+ for (const asset of [...input.assets ?? []].sort(
2227
+ (left, right) => normalizePath(left.path).localeCompare(normalizePath(right.path))
2228
+ )) {
2229
+ const assetPath = normalizePath(asset.path);
2230
+ const validation = validateCodexPluginPackageSurface({
2231
+ surfaceId: asset.surfaceId,
2232
+ path: assetPath,
2233
+ fields: [asset.manifestField]
2234
+ });
2235
+ if (!validation.ok) {
2236
+ diagnostics.push(...validation.diagnostics);
2237
+ continue;
2238
+ }
2239
+ const hookContent = resolveHookAssetContent(asset);
2240
+ diagnostics.push(...hookContent.diagnostics);
2241
+ if (asset.surfaceId === "plugin-hooks-json" && !hookContent.content) {
2242
+ continue;
2243
+ }
2244
+ const assetContent = hookContent.content ?? asset.content;
2245
+ const reference = pluginRootReference(assetPath);
2246
+ manifest[asset.manifestField] = reference;
2247
+ if (assetContent !== void 0 && !assetPath.endsWith("/")) {
2248
+ artifacts.push({
2249
+ harness: "codex",
2250
+ kind: artifactKindForSurface(asset.surfaceId),
2251
+ path: assetPath,
2252
+ description: asset.description ?? `Codex plugin asset for ${asset.manifestField}`,
2253
+ content: assetContent
2254
+ });
2255
+ }
2256
+ provenance.push({
2257
+ field: asset.manifestField,
2258
+ path: assetPath,
2259
+ reference,
2260
+ ...asset.provenanceName ? { name: asset.provenanceName } : {},
2261
+ ...asset.sourcePath ? { sourcePath: normalizePath(asset.sourcePath) } : {},
2262
+ ...assetContent !== void 0 ? { sha256: sha256(assetContent) } : {}
2263
+ });
2264
+ }
2265
+ artifacts.push({
2266
+ harness: "codex",
2267
+ kind: "manifest",
2268
+ path: ".codex-plugin/plugin.json",
2269
+ description: "Deterministic Codex plugin package manifest.",
2270
+ content: stableJson(orderManifestFields(manifest))
2271
+ });
2272
+ artifacts.push({
2273
+ harness: "codex",
2274
+ kind: "manifest",
2275
+ path: ".codex-plugin/.thoth-agents-plugin-assets.json",
2276
+ description: "Deterministic Codex plugin package asset provenance.",
2277
+ content: stableJson({
2278
+ generatedBy: "thoth-agents",
2279
+ assets: provenance.sort(
2280
+ (left, right) => left.path.localeCompare(right.path)
2281
+ )
2282
+ })
2283
+ });
2284
+ return { artifacts, diagnostics };
2285
+ }
2286
+
2287
+ // src/harness/writers/codex-toml.ts
2288
+ function escapeTomlString(value) {
2289
+ return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r");
2290
+ }
2291
+ function escapeTomlMultilineString(value) {
2292
+ return value.replace(/\\/g, "\\\\").replace(/"""/g, '\\"\\"\\"').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
2293
+ }
2294
+ function renderScalar(value) {
2295
+ if (typeof value === "string") return `"${escapeTomlString(value)}"`;
2296
+ if (typeof value === "number" || typeof value === "boolean") {
2297
+ return String(value);
2298
+ }
2299
+ if (Array.isArray(value)) {
2300
+ return `[${value.map(renderScalar).join(", ")}]`;
2301
+ }
2302
+ throw new Error(`Unsupported TOML scalar: ${String(value)}`);
2303
+ }
2304
+ function renderField(key, value) {
2305
+ if (key === "developer_instructions" && typeof value === "string") {
2306
+ return [`${key} = """`, escapeTomlMultilineString(value), '"""', ""];
2307
+ }
2308
+ return [`${key} = ${renderScalar(value)}`];
2309
+ }
2310
+ function isRecord2(value) {
2311
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2312
+ }
2313
+ function fieldOrder(fields, keys) {
2314
+ const orderedRoots = fields.map((field) => field.split(".")[0]);
2315
+ return [...keys].sort((left, right) => {
2316
+ const leftIndex = orderedRoots.indexOf(left);
2317
+ const rightIndex = orderedRoots.indexOf(right);
2318
+ if (leftIndex !== -1 || rightIndex !== -1) {
2319
+ return (leftIndex === -1 ? Number.MAX_SAFE_INTEGER : leftIndex) - (rightIndex === -1 ? Number.MAX_SAFE_INTEGER : rightIndex);
2320
+ }
2321
+ return left.localeCompare(right);
2322
+ });
2323
+ }
2324
+ function isAllowedRoot(fields, key) {
2325
+ return fields.some(
2326
+ (field) => field === key || field.startsWith(`${key}.`) || field.includes("<id>") && key === field.split(".")[0]
2327
+ );
2328
+ }
2329
+ function renderTable(lines, tablePath, value) {
2330
+ lines.push(`[${tablePath.join(".")}]`);
2331
+ const nested = [];
2332
+ for (const key of Object.keys(value).sort()) {
2333
+ const entry = value[key];
2334
+ if (isRecord2(entry)) {
2335
+ nested.push([key, entry]);
2336
+ } else {
2337
+ lines.push(...renderField(key, entry));
2338
+ }
2339
+ }
2340
+ lines.push("");
2341
+ for (const [key, entry] of nested) {
2342
+ renderTable(lines, [...tablePath, key], entry);
2343
+ }
2344
+ }
2345
+ function renderCodexToml(input) {
2346
+ const canGenerate = assertCodexSurfaceCanGenerate(input.surfaceId);
2347
+ if (!canGenerate.ok) {
2348
+ return { content: "", diagnostics: [canGenerate.diagnostic] };
2349
+ }
2350
+ const surface = getCodexSurface(input.surfaceId);
2351
+ const fields = surface?.fields ?? [];
2352
+ const diagnostics = [];
2353
+ const lines = [];
2354
+ const tables = [];
2355
+ for (const key of fieldOrder(fields, Object.keys(input.values))) {
2356
+ const value = input.values[key];
2357
+ if (!isAllowedRoot(fields, key)) {
2358
+ diagnostics.push({
2359
+ severity: "warning",
2360
+ code: "codex.toml.field.unvalidated",
2361
+ message: `Skipping unvalidated Codex TOML field "${key}" for surface "${input.surfaceId}".`,
2362
+ harness: "codex",
2363
+ surface: input.surfaceId,
2364
+ fallback: "diagnostic-only"
2365
+ });
2366
+ continue;
2367
+ }
2368
+ if (isRecord2(value)) {
2369
+ tables.push([key, value]);
2370
+ } else {
2371
+ lines.push(...renderField(key, value));
2372
+ }
2373
+ }
2374
+ if (tables.length > 0 && lines.length > 0) lines.push("");
2375
+ for (const [key, value] of tables) {
2376
+ renderTable(lines, [key], value);
2377
+ }
2378
+ while (lines.at(-1) === "") lines.pop();
2379
+ return {
2380
+ content: lines.join("\n") + (lines.length > 0 ? "\n" : ""),
2381
+ diagnostics
2382
+ };
2383
+ }
2384
+
2385
+ // src/harness/writers/skill-layout.ts
2386
+ import { createHash as createHash2 } from "crypto";
2387
+ import * as fs2 from "fs";
2388
+ import * as path2 from "path";
2389
+ var OUTPUT_MODE_CONFIG = {
2390
+ "plugin-package": {
2391
+ basePath: ".codex-plugin/skills",
2392
+ manifestPath: ".codex-plugin/skills/.thoth-agents-manifest.json",
2393
+ surfaceId: "plugin-skills-directory",
2394
+ label: "plugin-bundled"
2395
+ },
2396
+ "repo-local-fallback": {
2397
+ basePath: ".agents/skills",
2398
+ manifestPath: ".agents/skills/.thoth-agents-manifest.json",
2399
+ surfaceId: "repo-skills-directory",
2400
+ label: "fallback .agents/skills"
2401
+ }
2402
+ };
2403
+ function normalizePath2(value) {
2404
+ return value.split(path2.sep).join("/");
2405
+ }
2406
+ function collectFiles(directory) {
2407
+ if (!fs2.existsSync(directory)) return [];
2408
+ const entries = fs2.readdirSync(directory, { withFileTypes: true });
2409
+ const files = [];
2410
+ for (const entry of entries) {
2411
+ const entryPath = path2.join(directory, entry.name);
2412
+ if (entry.isDirectory()) {
2413
+ files.push(...collectFiles(entryPath));
2414
+ } else if (entry.isFile()) {
2415
+ files.push(entryPath);
2416
+ }
2417
+ }
2418
+ return files.sort((left, right) => left.localeCompare(right));
2419
+ }
2420
+ function sha2562(content) {
2421
+ return `sha256:${createHash2("sha256").update(content).digest("hex")}`;
2422
+ }
2423
+ function resolveOutputModes(input) {
2424
+ const requested = input.outputModes ?? (input.outputMode ? [input.outputMode] : []);
2425
+ const modes = requested.length > 0 ? requested : ["plugin-package"];
2426
+ return [...new Set(modes)];
2427
+ }
2428
+ function duplicateScopeDiagnostic(skillNames, surfaceId) {
2429
+ if (skillNames.length === 0) return void 0;
2430
+ return {
2431
+ severity: "warning",
2432
+ code: "codex.skill.duplicate_scope_precedence_unverified",
2433
+ message: `Codex skills are selected for both plugin-bundled and fallback .agents/skills scopes: ${skillNames.join(", ")}. Plugin-bundled skills are the intended primary package content, but runtime precedence with fallback scopes is unresolved and must be validated before relying on ordering.`,
2434
+ harness: "codex",
2435
+ surface: surfaceId,
2436
+ fallback: "diagnostic-only"
2437
+ };
2438
+ }
2439
+ function renderCodexSkillLayout(input) {
2440
+ const surface = assertCodexSurfaceCanGenerate(input.surfaceId);
2441
+ if (!surface.ok) {
2442
+ return { artifacts: [], diagnostics: [surface.diagnostic] };
2443
+ }
2444
+ const artifacts = [];
2445
+ const diagnostics = [];
2446
+ const outputModes = resolveOutputModes(input);
2447
+ for (const mode of outputModes) {
2448
+ const modeSurface = assertCodexSurfaceCanGenerate(
2449
+ OUTPUT_MODE_CONFIG[mode].surfaceId
2450
+ );
2451
+ if (!modeSurface.ok) diagnostics.push(modeSurface.diagnostic);
2452
+ }
2453
+ const duplicateDiagnostic = duplicateScopeDiagnostic(
2454
+ outputModes.length > 1 ? input.skills.map((skill) => skill.name).sort() : [],
2455
+ input.surfaceId
2456
+ );
2457
+ if (duplicateDiagnostic) diagnostics.push(duplicateDiagnostic);
2458
+ const manifests = new Map(
2459
+ outputModes.map((mode) => [mode, []])
2460
+ );
2461
+ for (const skill of [...input.skills].sort(
2462
+ (left, right) => left.name.localeCompare(right.name)
2463
+ )) {
2464
+ const sourceRoot = path2.join(input.projectRoot, skill.sourcePath);
2465
+ const files = collectFiles(sourceRoot);
2466
+ if (files.length === 0) {
2467
+ diagnostics.push({
2468
+ severity: "warning",
2469
+ code: "codex.skill.source_missing",
2470
+ message: `Skipping Codex skill "${skill.name}" because source path "${skill.sourcePath}" was not found.`,
2471
+ harness: "codex",
2472
+ surface: input.surfaceId,
2473
+ fallback: "diagnostic-only"
2474
+ });
2475
+ continue;
2476
+ }
2477
+ for (const file of files) {
2478
+ const relative4 = normalizePath2(path2.relative(sourceRoot, file));
2479
+ const content = fs2.readFileSync(file, "utf8");
2480
+ const sourcePath = normalizePath2(path2.relative(input.projectRoot, file));
2481
+ for (const mode of outputModes) {
2482
+ const config = OUTPUT_MODE_CONFIG[mode];
2483
+ const outputPath = `${config.basePath}/${skill.name}/${relative4}`;
2484
+ artifacts.push({
2485
+ harness: "codex",
2486
+ kind: "skill",
2487
+ path: outputPath,
2488
+ description: `Codex ${config.label} skill artifact for ${skill.name}`,
2489
+ content
2490
+ });
2491
+ manifests.get(mode)?.push({
2492
+ name: skill.name,
2493
+ sourcePath,
2494
+ outputPath,
2495
+ sha256: sha2562(content)
2496
+ });
2497
+ }
2498
+ }
2499
+ }
2500
+ for (const mode of outputModes) {
2501
+ const config = OUTPUT_MODE_CONFIG[mode];
2502
+ artifacts.push({
2503
+ harness: "codex",
2504
+ kind: "manifest",
2505
+ path: config.manifestPath,
2506
+ description: `Generated Codex ${config.label} skill source manifest with source hashes.`,
2507
+ content: `${JSON.stringify({ generatedBy: "thoth-agents", skills: manifests.get(mode) ?? [] }, null, 2)}
2508
+ `
2509
+ });
2510
+ }
2511
+ return { artifacts, diagnostics };
2512
+ }
2513
+
2514
+ // src/harness/adapters/codex.ts
2515
+ function readRootPackageVersion(startDir) {
2516
+ const packageJsonPath = findRootPackageJsonPath([startDir, process.cwd()]);
2517
+ return readPackageJsonVersion(packageJsonPath);
2518
+ }
2519
+ function createCodexPluginPackageManifest(projectRoot) {
2520
+ return {
2521
+ name: "thoth-agents",
2522
+ version: readRootPackageVersion(projectRoot),
2523
+ description: "Delegate-first OpenCode plugin with seven agents, thoth-mem persistence, and bundled SDD skills."
2524
+ };
2525
+ }
2526
+ function findRootPackageJsonPath(startDirs) {
2527
+ for (const startDir of startDirs) {
2528
+ let currentDir = resolve(startDir);
2529
+ while (true) {
2530
+ const packageJsonPath = resolve(currentDir, "package.json");
2531
+ if (existsSync5(packageJsonPath)) {
2532
+ const packageJsonText = readFileSync4(packageJsonPath, "utf8");
2533
+ const packageJson = JSON.parse(packageJsonText);
2534
+ if (packageJson.name === "thoth-agents") {
2535
+ return packageJsonPath;
2536
+ }
2537
+ }
2538
+ const parentDir = dirname2(currentDir);
2539
+ if (parentDir === currentDir) {
2540
+ break;
2541
+ }
2542
+ currentDir = parentDir;
2543
+ }
2544
+ }
2545
+ throw new Error(
2546
+ "Unable to locate the thoth-agents root package.json from the render context or current working directory."
2547
+ );
2548
+ }
2549
+ function readPackageJsonVersion(packageJsonPath) {
2550
+ const packageJsonText = readFileSync4(packageJsonPath, "utf8");
2551
+ const packageJson = JSON.parse(packageJsonText);
2552
+ if (typeof packageJson.version !== "string" || packageJson.version.length === 0) {
2553
+ throw new Error("Root package.json version must be a non-empty string.");
2554
+ }
2555
+ return packageJson.version;
2556
+ }
2557
+ function stableJson2(value) {
2558
+ return `${JSON.stringify(value, null, 2)}
2559
+ `;
2560
+ }
2561
+ function codexCommandConfig(commandParts) {
2562
+ const [command = "", ...args] = commandParts;
2563
+ return {
2564
+ command,
2565
+ ...args.length > 0 ? { args } : {}
2566
+ };
2567
+ }
2568
+ function codexMcpConfig(config) {
2569
+ if ("url" in config) {
2570
+ return { url: config.url };
2571
+ }
2572
+ const [command = "", ...args] = config.command;
2573
+ return {
2574
+ command,
2575
+ ...args.length > 0 ? { args } : {},
2576
+ ...config.environment && Object.keys(config.environment).length > 0 ? { env: config.environment } : {}
2577
+ };
2578
+ }
2579
+ function createCodexBuiltinMcpServers() {
2580
+ return {
2581
+ exa: codexMcpConfig(exa),
2582
+ context7: { url: CONTEXT7_MCP_URL },
2583
+ grep_app: { url: GREP_APP_MCP_URL },
2584
+ thoth_mem: codexCommandConfig(DEFAULT_THOTH_COMMAND)
2585
+ };
2586
+ }
2587
+ function createCodexBuiltinMcpJsonConfig() {
2588
+ return {
2589
+ mcpServers: createCodexBuiltinMcpServers()
2590
+ };
2591
+ }
2592
+ function createCodexBuiltinMcpTomlConfig() {
2593
+ return {
2594
+ mcp_servers: createCodexBuiltinMcpServers()
2595
+ };
2596
+ }
2597
+ var CODEX_SUBAGENT_DEFAULT_MODELS = {
2598
+ oracle: "gpt-5.5",
2599
+ librarian: "gpt-5.4-mini",
2600
+ explorer: "gpt-5.4-mini",
2601
+ designer: "gpt-5.4-mini",
2602
+ quick: "gpt-5.4-mini",
2603
+ deep: "gpt-5.5"
2604
+ };
2605
+ var CODEX_SUBAGENT_REASONING_EFFORTS = {
2606
+ oracle: "high",
2607
+ explorer: "low",
2608
+ librarian: "medium",
2609
+ designer: "medium",
2610
+ quick: "low",
2611
+ deep: "medium"
2612
+ };
2613
+ var CODEX_ROOT_START = "<!-- thoth-agents:codex-root:start -->";
2614
+ var CODEX_ROOT_END = "<!-- thoth-agents:codex-root:end -->";
2615
+ var CODEX_CAPABILITIES = CODEX_PROMPT_DIALECT.capabilities.capabilities;
2616
+ function codexPromptSections(roleName) {
2617
+ switch (roleName) {
2618
+ case "orchestrator":
2619
+ return createOrchestratorPromptSections();
2620
+ case "explorer":
2621
+ case "librarian":
2622
+ case "oracle":
2623
+ return createReadOnlySpecialistPromptSections(roleName);
2624
+ case "designer":
2625
+ case "quick":
2626
+ case "deep":
2627
+ return createWriteCapableSpecialistPromptSections(roleName);
2628
+ }
2629
+ }
2630
+ function codexModelFamilyPromptSection(roleName, model) {
2631
+ const section = createModelFamilySection(roleName, model);
2632
+ return section ? renderPromptSection(section, CODEX_PROMPT_DIALECT) : void 0;
2633
+ }
2634
+ function codexStepBudgetPromptSection(steps) {
2635
+ const section = createStepBudgetSection(steps);
2636
+ return section ? renderPromptSection(section, CODEX_PROMPT_DIALECT) : void 0;
2637
+ }
2638
+ function renderCodexRolePrompt(roleName, config, model) {
2639
+ const promptOverrides = loadAgentPrompt(roleName, config?.preset);
2640
+ const override = getAgentOverride(config, roleName);
2641
+ const basePrompt = renderRolePrompt(
2642
+ codexPromptSections(roleName),
2643
+ CODEX_PROMPT_DIALECT
2644
+ );
2645
+ const prompt = composeAgentPrompt({
2646
+ basePrompt,
2647
+ customPrompt: promptOverrides.prompt,
2648
+ customAppendPrompt: appendPromptSections(
2649
+ codexModelFamilyPromptSection(roleName, model),
2650
+ promptOverrides.appendPrompt
2651
+ )
2652
+ });
2653
+ return appendPromptSections(
2654
+ prompt,
2655
+ codexStepBudgetPromptSection(override?.steps)
2656
+ );
2657
+ }
2658
+ function codexRoleInstructions(role) {
2659
+ return [
2660
+ "<role-operational-contract>",
2661
+ `- Role: ${role.name}`,
2662
+ `- Mode: ${role.mode}`,
2663
+ `- Scope: ${role.scope}`,
2664
+ `- Responsibility: ${role.responsibility}`,
2665
+ "- Use request_user_input for local blocking decisions.",
2666
+ "- Permissions, memory governance, runtime hooks, and provider-per-agent controls are instruction-level unless the active Codex runtime documents stronger enforcement.",
2667
+ `- ${role.name} runs as a Codex custom agent TOML; there is no selectable orchestrator TOML.`,
2668
+ ...role.toolGovernance.map((rule) => `- ${rule}`),
2669
+ ...role.verification.map((rule) => `- ${rule}`),
2670
+ "</role-operational-contract>"
2671
+ ].join("\n");
2672
+ }
2673
+ function roleInstructions(role, config) {
2674
+ const model = getCodexAgentModel(role, config) ?? DEFAULT_MODELS[role.name];
2675
+ return [
2676
+ renderCodexRolePrompt(role.name, config, model),
2677
+ codexRoleInstructions(role),
2678
+ renderMemoryGovernanceInstructions(role, CODEX_PROMPT_DIALECT)
2679
+ ].join("\n\n");
2680
+ }
2681
+ function renderCodexRootInstructions(config) {
2682
+ const rootOverride = getAgentOverride(config, "orchestrator");
2683
+ const rootPrompt = renderCodexRolePrompt(
2684
+ "orchestrator",
2685
+ config,
2686
+ rootOverride?.model ?? DEFAULT_MODELS.orchestrator
2687
+ );
2688
+ return [
2689
+ CODEX_ROOT_START,
2690
+ rootPrompt,
2691
+ "<codex-runtime>",
2692
+ "- The ambient Codex root session is the root/main orchestrator; orchestrator-only and root-owned instructions apply to it because Codex does not generate a selectable orchestrator agent TOML.",
2693
+ "- On each new root session, when thoth-mem tools are installed and session/project identity is known, call mem_session_start with the active project and session identity, then save the real user prompt with mem_save_prompt before later delegation.",
2694
+ "- If thoth-mem tools or identity values are unavailable, disclose that memory bootstrap could not run and continue without claiming memory was saved.",
2695
+ "- Use the ambient Codex root session as the delegate-first root coordinator; do not generate or select an orchestrator TOML.",
2696
+ "- Delegate by invoking the installed Codex role agents: explorer, librarian, oracle, designer, quick, and deep.",
2697
+ "- After receiving a delegated subagent response, close that subagent session unless you will retry or intentionally keep using that exact same session; explorer and librarian sessions must always be closed immediately after their response, and retry sessions must be closed after the retry result unless explicit same-session reuse is still required.",
2698
+ "- Use packaged thoth-agents plugin capabilities through Codex plugin, skill, MCP, and hook review surfaces after enabling them with /plugins and /hooks.",
2699
+ "- For blocking user decisions in Codex Default mode, use request_user_input after features.default_mode_request_user_input is enabled; do not ask those questions in plain prose.",
2700
+ "- Permissions, memory policy, provider-per-agent controls, and hooks are instruction-only unless the active Codex runtime documents stronger enforcement.",
2701
+ "</codex-runtime>",
2702
+ CODEX_ROOT_END,
2703
+ ""
2704
+ ].join("\n");
2705
+ }
2706
+ function agentModelReasoningEffort(role) {
2707
+ return isCodexSubagentName(role.name) ? CODEX_SUBAGENT_REASONING_EFFORTS[role.name] : "medium";
2708
+ }
2709
+ function isCodexSubagentName(name) {
2710
+ return name in CODEX_SUBAGENT_DEFAULT_MODELS;
2711
+ }
2712
+ function getPrimaryModelId2(model) {
2713
+ if (Array.isArray(model)) {
2714
+ const first = model[0];
2715
+ return typeof first === "string" ? first : first?.id;
2716
+ }
2717
+ return model;
2718
+ }
2719
+ function getCodexAgentModel(role, config) {
2720
+ if (!isCodexSubagentName(role.name)) return void 0;
2721
+ return getPrimaryModelId2(config?.agents?.[role.name]?.model) ?? CODEX_SUBAGENT_DEFAULT_MODELS[role.name];
2722
+ }
2723
+ function codexSurfaceHasField(surfaceId, field) {
2724
+ return getCodexSurface(surfaceId)?.fields.includes(field) ?? false;
2725
+ }
2726
+ function hasCodexConfig(context) {
2727
+ return "config" in context;
2728
+ }
2729
+ function renderAgentArtifacts({ config }) {
2730
+ const artifacts = [];
2731
+ const diagnostics = [];
2732
+ const supportsReasoningEffort = codexSurfaceHasField(
2733
+ "project-agent-toml",
2734
+ "model_reasoning_effort"
2735
+ );
2736
+ for (const role of getAgentPackContract().roles.filter(
2737
+ (candidate) => candidate.name !== "orchestrator"
2738
+ )) {
2739
+ const model = getCodexAgentModel(role, config);
2740
+ const toml = renderCodexToml({
2741
+ surfaceId: "project-agent-toml",
2742
+ values: {
2743
+ name: role.name,
2744
+ description: role.responsibility,
2745
+ developer_instructions: roleInstructions(role, config),
2746
+ ...model ? { model } : {},
2747
+ ...supportsReasoningEffort ? { model_reasoning_effort: agentModelReasoningEffort(role) } : {},
2748
+ sandbox_mode: role.canMutateWorkspace ? "workspace-write" : "read-only"
2749
+ }
2750
+ });
2751
+ diagnostics.push(...toml.diagnostics);
2752
+ artifacts.push({
2753
+ harness: "codex",
2754
+ kind: "agent-config",
2755
+ path: `.codex/agents/thoth-agents-${role.name}.toml`,
2756
+ description: `Codex agent definition for ${role.name}.`,
2757
+ content: toml.content
2758
+ });
2759
+ }
2760
+ return { artifacts, diagnostics };
2761
+ }
2762
+ function renderConfigArtifacts() {
2763
+ const config = renderCodexToml({
2764
+ surfaceId: "project-config-toml",
2765
+ values: {
2766
+ approval_policy: "on-request",
2767
+ sandbox_mode: "workspace-write",
2768
+ "skills.config": { enabled: true, sources: ["repo"] },
2769
+ agents: getAgentPackContract().roles.filter((role) => role.name !== "orchestrator").map((role) => role.name)
2770
+ }
2771
+ });
2772
+ const mcp = renderCodexToml({
2773
+ surfaceId: "mcp-server-config",
2774
+ values: createCodexBuiltinMcpTomlConfig()
2775
+ });
2776
+ return {
2777
+ artifacts: [
2778
+ {
2779
+ harness: "codex",
2780
+ kind: "harness-config",
2781
+ path: ".codex/config.toml",
2782
+ description: "Codex project configuration snippet for the agent pack.",
2783
+ content: config.content
2784
+ },
2785
+ {
2786
+ harness: "codex",
2787
+ kind: "mcp-config",
2788
+ path: ".codex/config.toml",
2789
+ description: "Codex MCP configuration snippet for thoth-mem.",
2790
+ content: mcp.content
2791
+ }
2792
+ ],
2793
+ diagnostics: [...config.diagnostics, ...mcp.diagnostics]
2794
+ };
2795
+ }
2796
+ function capabilityDiagnostics() {
2797
+ const surfaceDiagnostics = getCodexSurfaceRecords().filter((surface) => surface.status !== "validated").map(codexSurfaceDiagnostic);
2798
+ const governanceDiagnostics = memoryGovernanceDiagnostics({
2799
+ harness: "codex",
2800
+ permissionControls: CODEX_CAPABILITIES.rolePermissions,
2801
+ parentContextInjection: CODEX_CAPABILITIES.parentContextInjection,
2802
+ memoryWriteControls: CODEX_CAPABILITIES.memoryGovernanceEnforcement
2803
+ });
2804
+ return [...surfaceDiagnostics, ...governanceDiagnostics];
2805
+ }
2806
+ function hookReadinessDiagnostics() {
2807
+ return [
2808
+ {
2809
+ severity: "warning",
2810
+ code: "codex.hooks.project_trust.required",
2811
+ message: "Codex project-local hooks require trusted project configuration before .codex/hooks.json or inline [hooks] command handlers are active; generated artifacts remain diagnostic-only until trust and features.hooks are reviewed.",
2812
+ harness: "codex",
2813
+ surface: "project-hooks-json",
2814
+ fallback: "diagnostic-only"
2815
+ },
2816
+ {
2817
+ severity: "warning",
2818
+ code: "codex.hooks.features_hooks.required",
2819
+ message: "Codex hook loading is gated by features.hooks; this adapter reports the docs-backed config surface but does not generate hook scripts or claim runtime enforcement.",
2820
+ harness: "codex",
2821
+ surface: "features-hooks-toggle",
2822
+ fallback: "diagnostic-only"
2823
+ },
2824
+ {
2825
+ severity: "warning",
2826
+ code: "codex.hooks.plugin_trust.required",
2827
+ message: "Bundled Codex plugin hook configuration is package content only; activation requires features.plugin_hooks and plugin hook trust review, and this adapter does not enable hooks automatically or claim hard permission enforcement.",
2828
+ harness: "codex",
2829
+ surface: "plugin-hooks-bundle",
2830
+ fallback: "diagnostic-only"
2831
+ }
2832
+ ];
2833
+ }
2834
+ function resolveSkillOutputModes(context) {
2835
+ return context.options?.codexSkillOutputModes ?? ["plugin-package"];
2836
+ }
2837
+ var codexAdapter = {
2838
+ id: "codex",
2839
+ displayName: "Codex",
2840
+ capabilities: CODEX_CAPABILITIES,
2841
+ render(context) {
2842
+ const config = hasCodexConfig(context) ? context.config : void 0;
2843
+ const agentArtifacts = renderAgentArtifacts({ config });
2844
+ const configArtifacts = renderConfigArtifacts();
2845
+ const skillOutputModes = resolveSkillOutputModes(context);
2846
+ const skillLayout = renderCodexSkillLayout({
2847
+ projectRoot: context.projectRoot,
2848
+ skills: getSkillRegistry(),
2849
+ surfaceId: "plugin-skills-directory",
2850
+ outputModes: skillOutputModes
2851
+ });
2852
+ const pluginPackage = renderCodexPluginPackage({
2853
+ manifest: createCodexPluginPackageManifest(context.projectRoot),
2854
+ assets: [
2855
+ {
2856
+ surfaceId: "plugin-skills-directory",
2857
+ manifestField: "skills",
2858
+ path: ".codex-plugin/skills/",
2859
+ description: "Codex plugin-bundled skill directory."
2860
+ },
2861
+ {
2862
+ surfaceId: "plugin-mcp-json",
2863
+ manifestField: "mcpServers",
2864
+ path: ".codex-plugin/.mcp.json",
2865
+ description: "Codex plugin-bundled MCP server definitions.",
2866
+ content: stableJson2(createCodexBuiltinMcpJsonConfig())
2867
+ },
2868
+ {
2869
+ surfaceId: "plugin-hooks-json",
2870
+ manifestField: "hooks",
2871
+ path: ".codex-plugin/hooks/hooks.json",
2872
+ description: "Codex plugin-bundled hook configuration.",
2873
+ hookDefinitions: []
2874
+ }
2875
+ ]
2876
+ });
2877
+ return {
2878
+ harness: "codex",
2879
+ artifacts: [
2880
+ ...agentArtifacts.artifacts,
2881
+ ...configArtifacts.artifacts,
2882
+ ...pluginPackage.artifacts,
2883
+ ...skillLayout.artifacts
2884
+ ],
2885
+ diagnostics: [
2886
+ ...agentArtifacts.diagnostics,
2887
+ ...configArtifacts.diagnostics,
2888
+ ...pluginPackage.diagnostics,
2889
+ ...skillLayout.diagnostics,
2890
+ ...hookReadinessDiagnostics(),
2891
+ ...capabilityDiagnostics()
2892
+ ]
2893
+ };
2894
+ }
2895
+ };
2896
+
2897
+ // src/cli/install.ts
2898
+ import { existsSync as existsSync10 } from "fs";
2899
+ import { homedir as homedir3 } from "os";
2900
+ import { cwd } from "process";
2901
+
2902
+ // src/cli/codex-install.ts
2903
+ import {
2904
+ copyFileSync as copyFileSync3,
2905
+ existsSync as existsSync7,
2906
+ mkdirSync as mkdirSync3,
2907
+ readFileSync as readFileSync6,
2908
+ rmSync,
2909
+ writeFileSync as writeFileSync3
2910
+ } from "fs";
2911
+ import { basename, dirname as dirname4, isAbsolute, join as join6, relative as relative2 } from "path";
2912
+
2913
+ // src/harness/codex-plugin-paths.ts
2914
+ import { join as join4 } from "path";
2915
+ var CODEX_PLUGIN_ARTIFACT_PREFIX = ".codex-plugin/";
2916
+ function codexPluginRootArtifactPath(artifactPath) {
2917
+ if (!artifactPath.startsWith(CODEX_PLUGIN_ARTIFACT_PREFIX)) {
2918
+ throw new Error(`Expected Codex plugin artifact path: ${artifactPath}`);
2919
+ }
2920
+ const relativePath = artifactPath.slice(CODEX_PLUGIN_ARTIFACT_PREFIX.length);
2921
+ if (relativePath === ".mcp.json") return relativePath;
2922
+ if (relativePath === "plugin.json" || relativePath.startsWith(".")) {
2923
+ return join4(".codex-plugin", relativePath);
2924
+ }
2925
+ return relativePath;
2926
+ }
2927
+
2928
+ // src/cli/codex-config-io.ts
2929
+ import {
2930
+ copyFileSync as copyFileSync2,
2931
+ existsSync as existsSync6,
2932
+ mkdirSync as mkdirSync2,
2933
+ readFileSync as readFileSync5,
2934
+ renameSync as renameSync2,
2935
+ writeFileSync as writeFileSync2
2936
+ } from "fs";
2937
+ import { dirname as dirname3 } from "path";
2938
+ function isRecord3(value) {
2939
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2940
+ }
2941
+ function splitArrayItems(raw) {
2942
+ const items = [];
2943
+ let current = "";
2944
+ let quote;
2945
+ let escaped = false;
2946
+ for (const char of raw) {
2947
+ if (quote === "double" && escaped) {
2948
+ current += char;
2949
+ escaped = false;
2950
+ continue;
2951
+ }
2952
+ if (quote === "double" && char === "\\") {
2953
+ current += char;
2954
+ escaped = true;
2955
+ continue;
2956
+ }
2957
+ if (!quote && char === ",") {
2958
+ items.push(current.trim());
2959
+ current = "";
2960
+ continue;
2961
+ }
2962
+ if (!quote && char === "'") quote = "single";
2963
+ else if (quote === "single" && char === "'") quote = void 0;
2964
+ else if (!quote && char === '"') quote = "double";
2965
+ else if (quote === "double" && char === '"') quote = void 0;
2966
+ current += char;
2967
+ }
2968
+ if (current.trim()) items.push(current.trim());
2969
+ return items;
2970
+ }
2971
+ function parseBasicString(value) {
2972
+ return value.slice(1, -1).replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\"/g, '"').replace(/\\\\/g, "\\");
2973
+ }
2974
+ function parseScalar(raw) {
2975
+ const value = raw.trim();
2976
+ if (value === "true") return true;
2977
+ if (value === "false") return false;
2978
+ if (/^-?\d+(\.\d+)?$/.test(value)) return Number(value);
2979
+ if (value.startsWith("[") && value.endsWith("]")) {
2980
+ const body = value.slice(1, -1).trim();
2981
+ if (!body) return [];
2982
+ return splitArrayItems(body).map(parseScalar);
2983
+ }
2984
+ if (value.startsWith('"') && value.endsWith('"')) {
2985
+ return parseBasicString(value);
2986
+ }
2987
+ if (value.startsWith("'") && value.endsWith("'")) {
2988
+ return value.slice(1, -1);
2989
+ }
2990
+ return value;
2991
+ }
2992
+ function parseTomlKeySegment(raw) {
2993
+ const segment = raw.trim();
2994
+ if (segment.startsWith('"') && segment.endsWith('"')) {
2995
+ return parseBasicString(segment);
2996
+ }
2997
+ if (segment.startsWith("'") && segment.endsWith("'")) {
2998
+ return segment.slice(1, -1);
2999
+ }
3000
+ return segment;
3001
+ }
3002
+ function parseTomlKeyPath(raw) {
3003
+ const segments = [];
3004
+ let current = "";
3005
+ let quote;
3006
+ let escaped = false;
3007
+ for (const char of raw) {
3008
+ if (quote === "double" && escaped) {
3009
+ current += char;
3010
+ escaped = false;
3011
+ continue;
3012
+ }
3013
+ if (quote === "double" && char === "\\") {
3014
+ current += char;
3015
+ escaped = true;
3016
+ continue;
3017
+ }
3018
+ if (!quote && char === ".") {
3019
+ segments.push(parseTomlKeySegment(current));
3020
+ current = "";
3021
+ continue;
3022
+ }
3023
+ if (!quote && char === "'") quote = "single";
3024
+ else if (quote === "single" && char === "'") quote = void 0;
3025
+ else if (!quote && char === '"') quote = "double";
3026
+ else if (quote === "double" && char === '"') quote = void 0;
3027
+ current += char;
3028
+ }
3029
+ segments.push(parseTomlKeySegment(current));
3030
+ return segments;
3031
+ }
3032
+ function ensureTable(root, path3) {
3033
+ let current = root;
3034
+ for (const segment of path3) {
3035
+ const existing = current[segment];
3036
+ if (!isRecord3(existing)) current[segment] = {};
3037
+ current = current[segment];
3038
+ }
3039
+ return current;
3040
+ }
3041
+ function parseCodexToml(content) {
3042
+ const root = {};
3043
+ let table = [];
3044
+ for (const rawLine of content.split(/\r?\n/)) {
3045
+ const trimmed = rawLine.trim();
3046
+ if (!trimmed || trimmed.startsWith("#")) continue;
3047
+ const arrayTableMatch = /^\[\[([^\]]+)\]\]$/.exec(trimmed);
3048
+ const tableMatch = /^\[([^\]]+)\]$/.exec(trimmed);
3049
+ const tablePath = arrayTableMatch?.[1] ?? tableMatch?.[1];
3050
+ if (tablePath) {
3051
+ table = parseTomlKeyPath(tablePath);
3052
+ ensureTable(root, table);
3053
+ continue;
3054
+ }
3055
+ const assignment = /^([^=]+)=(.*)$/.exec(trimmed);
3056
+ if (!assignment) throw new Error(`Unsupported TOML line: ${rawLine}`);
3057
+ ensureTable(root, table)[assignment[1].trim()] = parseScalar(assignment[2]);
3058
+ }
3059
+ return root;
3060
+ }
3061
+ function renderScalar2(value) {
3062
+ if (typeof value === "string") {
3063
+ const canUseLiteralString = value.includes("\\") && !value.includes("'") && [...value].every((char) => char.charCodeAt(0) >= 32);
3064
+ if (canUseLiteralString) {
3065
+ return `'${value}'`;
3066
+ }
3067
+ return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
3068
+ }
3069
+ return String(value);
3070
+ }
3071
+ function renderValue(value) {
3072
+ if (Array.isArray(value)) return `[${value.map(renderScalar2).join(", ")}]`;
3073
+ return renderScalar2(value);
3074
+ }
3075
+ function renderTomlKeySegment(segment) {
3076
+ if (/^[A-Za-z0-9_]+$/.test(segment)) return segment;
3077
+ const canUseLiteralKey = (segment.includes("\\") || segment.includes(".")) && !segment.includes("'") && [...segment].every((char) => char.charCodeAt(0) >= 32);
3078
+ if (canUseLiteralKey) return `'${segment}'`;
3079
+ return `"${segment.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
3080
+ }
3081
+ function renderTomlSection(lines, value, path3 = []) {
3082
+ if (path3.length > 0)
3083
+ lines.push(`[${path3.map(renderTomlKeySegment).join(".")}]`);
3084
+ const nested = [];
3085
+ for (const key of Object.keys(value).sort()) {
3086
+ const entry = value[key];
3087
+ if (isRecord3(entry)) nested.push([key, entry]);
3088
+ else lines.push(`${key} = ${renderValue(entry)}`);
3089
+ }
3090
+ if (path3.length > 0) lines.push("");
3091
+ for (const [key, entry] of nested)
3092
+ renderTomlSection(lines, entry, [...path3, key]);
3093
+ }
3094
+ function renderCodexTomlDocument(document) {
3095
+ const lines = [];
3096
+ renderTomlSection(lines, document);
3097
+ while (lines.at(-1) === "") lines.pop();
3098
+ return `${lines.join("\n")}
3099
+ `;
3100
+ }
3101
+ function mergeCodexManagedConfig(document, options) {
3102
+ const features = ensureTable(document, ["features"]);
3103
+ features.default_mode_request_user_input = true;
3104
+ const diffSummary = ["ensure features.default_mode_request_user_input = true"];
3105
+ if (options.pluginId) {
3106
+ const plugin = ensureTable(document, ["plugins", options.pluginId]);
3107
+ plugin.enabled = true;
3108
+ diffSummary.push(`ensure plugins."${options.pluginId}".enabled = true`);
3109
+ } else {
3110
+ diffSummary.push(
3111
+ "plugin enablement left to /plugins; no guessed plugin id written"
3112
+ );
3113
+ }
3114
+ return {
3115
+ content: renderCodexTomlDocument(document),
3116
+ diffSummary,
3117
+ warnings: [
3118
+ "Codex TOML comments and formatting may be rewritten; a backup is created before apply."
3119
+ ]
3120
+ };
3121
+ }
3122
+ function writeCodexConfigMerge(options) {
3123
+ try {
3124
+ const before = existsSync6(options.configPath) ? readFileSync5(options.configPath, "utf8") : "";
3125
+ const parsed = parseCodexToml(before);
3126
+ const merged = mergeCodexManagedConfig(parsed, {
3127
+ pluginId: options.pluginId
3128
+ });
3129
+ const changed = before !== merged.content;
3130
+ if (options.dryRun || !changed) {
3131
+ return {
3132
+ success: true,
3133
+ configPath: options.configPath,
3134
+ changed,
3135
+ diffSummary: merged.diffSummary,
3136
+ warnings: merged.warnings
3137
+ };
3138
+ }
3139
+ mkdirSync2(dirname3(options.configPath), { recursive: true });
3140
+ const backupPath = `${options.configPath}.bak`;
3141
+ if (existsSync6(options.configPath))
3142
+ copyFileSync2(options.configPath, backupPath);
3143
+ const tmpPath = `${options.configPath}.tmp`;
3144
+ writeFileSync2(tmpPath, merged.content);
3145
+ renameSync2(tmpPath, options.configPath);
3146
+ return {
3147
+ success: true,
3148
+ configPath: options.configPath,
3149
+ backupPath: existsSync6(backupPath) ? backupPath : void 0,
3150
+ changed,
3151
+ diffSummary: merged.diffSummary,
3152
+ warnings: merged.warnings
3153
+ };
3154
+ } catch (error) {
3155
+ return {
3156
+ success: false,
3157
+ configPath: options.configPath,
3158
+ changed: false,
3159
+ diffSummary: [],
3160
+ warnings: [],
3161
+ error: error instanceof Error ? error.message : String(error)
3162
+ };
3163
+ }
3164
+ }
3165
+
3166
+ // src/cli/codex-paths.ts
3167
+ import { homedir as homedir2 } from "os";
3168
+ import { join as join5 } from "path";
3169
+ var CODEX_ROLE_NAMES = [
3170
+ "explorer",
3171
+ "librarian",
3172
+ "oracle",
3173
+ "designer",
3174
+ "quick",
3175
+ "deep"
3176
+ ];
3177
+ function getCodexHome(options = {}) {
3178
+ const explicit = options.codexHome ?? process.env.CODEX_HOME?.trim();
3179
+ return explicit || join5(options.homeDir ?? homedir2(), ".codex");
3180
+ }
3181
+ function resolveCodexTargets(options) {
3182
+ const codexHome = getCodexHome(options);
3183
+ const agentsDir = options.scope === "project" ? join5(options.projectRoot, ".codex", "agents") : join5(codexHome, "agents");
3184
+ return {
3185
+ scope: options.scope,
3186
+ codexHome,
3187
+ configPath: join5(codexHome, "config.toml"),
3188
+ rootInstructionsPath: join5(codexHome, "AGENTS.md"),
3189
+ roleAgentPaths: CODEX_ROLE_NAMES.map((role) => ({
3190
+ role,
3191
+ path: join5(agentsDir, `thoth-agents-${role}.toml`)
3192
+ })),
3193
+ managedModelsPath: join5(
3194
+ codexHome,
3195
+ "agents",
3196
+ ".thoth-agents-managed-models.json"
3197
+ ),
3198
+ skillsDir: options.scope === "project" ? join5(options.projectRoot, ".agents", "skills") : join5(options.homeDir ?? homedir2(), ".agents", "skills"),
3199
+ packageRoot: join5(options.projectRoot, ".codex-plugin"),
3200
+ personalPluginRoot: join5(codexHome, "plugins", "thoth-agents"),
3201
+ personalMarketplacePath: join5(
3202
+ options.homeDir ?? homedir2(),
3203
+ ".agents",
3204
+ "plugins",
3205
+ "marketplace.json"
3206
+ )
3207
+ };
3208
+ }
3209
+
3210
+ // src/cli/codex-install.ts
3211
+ var ROOT_START = "<!-- thoth-agents:codex-root:start -->";
3212
+ var ROOT_END = "<!-- thoth-agents:codex-root:end -->";
3213
+ var MANAGED_MODEL_STATE_VERSION = 1;
3214
+ function mergeManagedBlock(existing, managedBlock) {
3215
+ const start = existing.indexOf(ROOT_START);
3216
+ const end = existing.indexOf(ROOT_END);
3217
+ if (start !== -1 && end !== -1 && end > start) {
3218
+ return `${existing.slice(0, start)}${managedBlock}${existing.slice(end + ROOT_END.length).replace(/^\s*\n/, "")}`;
3219
+ }
3220
+ return `${existing}${existing.endsWith("\n") || existing.length === 0 ? "" : "\n"}
3221
+ ${managedBlock}`;
3222
+ }
3223
+ function writeTextWithBackup(path3, content) {
3224
+ mkdirSync3(dirname4(path3), { recursive: true });
3225
+ if (existsSync7(path3) && readFileSync6(path3, "utf8") === content) return false;
3226
+ if (existsSync7(path3)) copyFileSync3(path3, `${path3}.bak`);
3227
+ writeFileSync3(path3, content);
3228
+ return true;
3229
+ }
3230
+ function packageArtifactTarget(packageRoot, artifact) {
3231
+ return join6(packageRoot, codexPluginRootArtifactPath(artifact.path));
3232
+ }
3233
+ function normalizeRelativeMarketplacePath(path3) {
3234
+ const normalized = path3.replaceAll("\\", "/");
3235
+ if (isAbsolute(path3) || /^[A-Za-z]:\//.test(normalized)) return normalized;
3236
+ if (normalized.startsWith("./")) return normalized;
3237
+ return `./${normalized}`;
3238
+ }
3239
+ function marketplaceSourcePath(homeDir, personalPluginRoot) {
3240
+ return normalizeRelativeMarketplacePath(
3241
+ relative2(homeDir, personalPluginRoot)
3242
+ );
3243
+ }
3244
+ function managedMarketplaceEntry(homeDir, personalPluginRoot) {
3245
+ return {
3246
+ name: "thoth-agents",
3247
+ source: {
3248
+ source: "local",
3249
+ path: marketplaceSourcePath(homeDir, personalPluginRoot)
3250
+ },
3251
+ policy: {
3252
+ installation: "AVAILABLE",
3253
+ authentication: "ON_INSTALL"
3254
+ },
3255
+ category: "Productivity"
3256
+ };
3257
+ }
3258
+ function stableJson3(value) {
3259
+ return `${JSON.stringify(value, null, 2)}
3260
+ `;
3261
+ }
3262
+ function emptyManagedModelState() {
3263
+ return {
3264
+ version: MANAGED_MODEL_STATE_VERSION,
3265
+ models: {}
3266
+ };
3267
+ }
3268
+ function readManagedModelState(path3) {
3269
+ if (!existsSync7(path3)) return emptyManagedModelState();
3270
+ try {
3271
+ const parsed = JSON.parse(readFileSync6(path3, "utf8"));
3272
+ if (parsed.version !== MANAGED_MODEL_STATE_VERSION || !parsed.models || typeof parsed.models !== "object" || Array.isArray(parsed.models)) {
3273
+ return emptyManagedModelState();
3274
+ }
3275
+ return {
3276
+ version: MANAGED_MODEL_STATE_VERSION,
3277
+ models: Object.fromEntries(
3278
+ Object.entries(parsed.models).filter(
3279
+ (entry) => typeof entry[0] === "string" && typeof entry[1] === "string"
3280
+ )
3281
+ )
3282
+ };
3283
+ } catch {
3284
+ return emptyManagedModelState();
3285
+ }
3286
+ }
3287
+ function parseRoleTomlModel(content) {
3288
+ const match = /^model\s*=\s*"((?:\\.|[^"\\])*)"\s*$/m.exec(content);
3289
+ if (!match) return void 0;
3290
+ return match[1].replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\"/g, '"').replace(/\\\\/g, "\\");
3291
+ }
3292
+ function escapeTomlString2(value) {
3293
+ return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r");
3294
+ }
3295
+ function replaceRoleTomlModel(content, model) {
3296
+ const rendered = `model = "${escapeTomlString2(model)}"`;
3297
+ if (/^model\s*=\s*"(?:\\.|[^"\\])*"\s*$/m.test(content)) {
3298
+ return content.replace(/^model\s*=\s*"(?:\\.|[^"\\])*"\s*$/m, rendered);
3299
+ }
3300
+ return `${rendered}
3301
+ ${content}`;
3302
+ }
3303
+ function roleManagedModelStateKey(path3) {
3304
+ return basename(path3);
3305
+ }
3306
+ function resolveRoleTomlContent(options) {
3307
+ const renderedModel = parseRoleTomlModel(options.renderedContent);
3308
+ const key = roleManagedModelStateKey(options.targetPath);
3309
+ if (!renderedModel) return options.renderedContent;
3310
+ if (options.reset || !existsSync7(options.targetPath)) {
3311
+ options.nextState.models[key] = renderedModel;
3312
+ return options.renderedContent;
3313
+ }
3314
+ const currentModel = parseRoleTomlModel(
3315
+ readFileSync6(options.targetPath, "utf8")
3316
+ );
3317
+ const trackedModel = options.state.models[key];
3318
+ const isUserOwned = currentModel !== void 0 && (trackedModel === void 0 ? currentModel !== renderedModel : currentModel !== trackedModel);
3319
+ if (isUserOwned) {
3320
+ if (trackedModel !== void 0)
3321
+ options.nextState.models[key] = trackedModel;
3322
+ return replaceRoleTomlModel(options.renderedContent, currentModel);
3323
+ }
3324
+ options.nextState.models[key] = renderedModel;
3325
+ return options.renderedContent;
3326
+ }
3327
+ function mergePersonalMarketplace(existing, homeDir, personalPluginRoot) {
3328
+ const parsed = existing.trim() ? JSON.parse(existing) : {};
3329
+ const plugins = Array.isArray(parsed.plugins) ? parsed.plugins : [];
3330
+ const managedEntry = managedMarketplaceEntry(homeDir, personalPluginRoot);
3331
+ const nextPlugins = plugins.filter(
3332
+ (entry) => !(entry && typeof entry === "object" && "name" in entry && entry.name === "thoth-agents")
3333
+ ).concat(managedEntry);
3334
+ return stableJson3({
3335
+ ...parsed,
3336
+ name: typeof parsed.name === "string" ? parsed.name : "personal-marketplace",
3337
+ interface: parsed.interface && typeof parsed.interface === "object" ? parsed.interface : { displayName: "Personal Plugin Marketplace" },
3338
+ plugins: nextPlugins
3339
+ });
3340
+ }
3341
+ function roleArtifactContent(role, artifacts) {
3342
+ const path3 = `.codex/agents/thoth-agents-${role}.toml`;
3343
+ const artifact = artifacts.find((candidate) => candidate.path === path3);
3344
+ if (!artifact?.content)
3345
+ throw new Error(`Missing Codex role artifact: ${path3}`);
3346
+ return String(artifact.content);
3347
+ }
3348
+ function buildCodexSetupPlan(config) {
3349
+ const targets = resolveCodexTargets({
3350
+ scope: config.scope,
3351
+ projectRoot: config.projectRoot,
3352
+ homeDir: config.homeDir,
3353
+ codexHome: config.codexHome
3354
+ });
3355
+ const render = codexAdapter.render({ projectRoot: config.projectRoot });
3356
+ const packageArtifacts = render.artifacts.filter(
3357
+ (artifact) => artifact.path.startsWith(".codex-plugin/")
3358
+ );
3359
+ const rootBlock = renderCodexRootInstructions();
3360
+ const managedModelState = readManagedModelState(targets.managedModelsPath);
3361
+ const nextManagedModelState = emptyManagedModelState();
3362
+ const items = [
3363
+ {
3364
+ kind: "root-instructions",
3365
+ action: "merge-managed-block",
3366
+ targetPath: targets.rootInstructionsPath,
3367
+ description: `Merge managed Codex root instructions into ${targets.rootInstructionsPath}.`,
3368
+ requiresBackup: true,
3369
+ content: rootBlock
3370
+ },
3371
+ ...targets.roleAgentPaths.map(
3372
+ (target) => ({
3373
+ kind: "role-subagent-toml",
3374
+ action: "write-role-toml",
3375
+ targetPath: target.path,
3376
+ description: `Materialize Codex role subagent ${target.role}.`,
3377
+ requiresBackup: existsSync7(target.path),
3378
+ role: target.role,
3379
+ content: resolveRoleTomlContent({
3380
+ renderedContent: roleArtifactContent(target.role, render.artifacts),
3381
+ targetPath: target.path,
3382
+ state: managedModelState,
3383
+ nextState: nextManagedModelState,
3384
+ reset: config.reset
3385
+ })
3386
+ })
3387
+ ),
3388
+ {
3389
+ kind: "managed-model-state",
3390
+ action: "write-managed-model-state",
3391
+ targetPath: targets.managedModelsPath,
3392
+ description: "Record thoth-agents-managed Codex role model ownership state.",
3393
+ requiresBackup: existsSync7(targets.managedModelsPath),
3394
+ content: stableJson3(nextManagedModelState)
3395
+ },
3396
+ ...packageArtifacts.map(
3397
+ (artifact) => ({
3398
+ kind: "personal-plugin-source",
3399
+ action: "refresh-package",
3400
+ targetPath: packageArtifactTarget(targets.personalPluginRoot, artifact),
3401
+ description: `Refresh Personal Codex plugin source asset ${artifact.path}.`,
3402
+ requiresBackup: false,
3403
+ content: String(artifact.content ?? "")
3404
+ })
3405
+ ),
3406
+ {
3407
+ kind: "personal-marketplace",
3408
+ action: "merge-marketplace",
3409
+ targetPath: targets.personalMarketplacePath,
3410
+ description: "Register Personal Codex marketplace entry for the local thoth-agents plugin source.",
3411
+ requiresBackup: existsSync7(targets.personalMarketplacePath),
3412
+ content: targets.personalPluginRoot
3413
+ },
3414
+ {
3415
+ kind: "user-config",
3416
+ action: "merge-toml",
3417
+ targetPath: targets.configPath,
3418
+ description: "Merge managed Codex feature gates into user config.toml.",
3419
+ requiresBackup: true
3420
+ },
3421
+ {
3422
+ kind: "diagnostic",
3423
+ action: "diagnose-only",
3424
+ targetPath: targets.codexHome,
3425
+ description: "Report /plugins, /hooks, precedence, and capability review steps.",
3426
+ requiresBackup: false
3427
+ }
3428
+ ];
3429
+ return {
3430
+ dryRun: config.dryRun === true,
3431
+ reset: config.reset,
3432
+ items,
3433
+ configPath: targets.configPath,
3434
+ pluginId: config.pluginId,
3435
+ diagnostics: [
3436
+ "Restart Codex, then run /plugins to review and enable the Personal thoth-agents plugin registered through ~/.agents/plugins/marketplace.json.",
3437
+ "Run /hooks to review and trust plugin hooks; features.plugin_hooks does not bypass hook trust review.",
3438
+ "Codex Default mode user-input requests require features.default_mode_request_user_input = true and use the request_user_input tool; other modes may not expose it.",
3439
+ "Higher-precedence Codex config (project, profile, CLI, system, or admin) may override user config feature flags."
3440
+ ],
3441
+ disclaimers: [
3442
+ "Role permissions, provider-per-agent settings, memory governance, and hook enforcement are instruction-level or user-managed unless documented Codex runtime controls are available.",
3443
+ "Codex v1 reset is managed-only; no broad destructive --force behavior is implemented."
3444
+ ]
3445
+ };
3446
+ }
3447
+ function formatCodexSetupPlan(plan) {
3448
+ const refreshPackageGroups = /* @__PURE__ */ new Map();
3449
+ for (const item of plan.items) {
3450
+ if (item.action !== "refresh-package") continue;
3451
+ const group = refreshPackageGroups.get(item.kind) ?? [];
3452
+ group.push(item);
3453
+ refreshPackageGroups.set(item.kind, group);
3454
+ }
3455
+ const renderedRefreshKinds = /* @__PURE__ */ new Set();
3456
+ const lines = [];
3457
+ for (const item of plan.items) {
3458
+ if (item.action !== "refresh-package") {
3459
+ lines.push(`- ${item.action}: ${item.targetPath} (${item.description})`);
3460
+ continue;
3461
+ }
3462
+ if (renderedRefreshKinds.has(item.kind)) continue;
3463
+ renderedRefreshKinds.add(item.kind);
3464
+ lines.push(formatRefreshPackageGroup(item.kind, refreshPackageGroups));
3465
+ }
3466
+ return ["Codex setup plan:", ...lines].join("\n");
3467
+ }
3468
+ function formatRefreshPackageGroup(kind, groups) {
3469
+ const items = groups.get(kind) ?? [];
3470
+ const description = kind === "personal-plugin-source" ? "Refresh Personal Codex plugin source" : "Refresh documented .codex-plugin package";
3471
+ return `- refresh-package: ${commonTargetDirectory(items)} (${description}, ${items.length} files.)`;
3472
+ }
3473
+ function commonTargetDirectory(items) {
3474
+ if (items.length === 0) return "";
3475
+ let common = dirname4(items[0]?.targetPath ?? "");
3476
+ for (const item of items.slice(1)) {
3477
+ while (!isSameOrChildPath(item.targetPath, common)) {
3478
+ const parent = dirname4(common);
3479
+ if (parent === common) return common;
3480
+ common = parent;
3481
+ }
3482
+ }
3483
+ return common;
3484
+ }
3485
+ function isSameOrChildPath(path3, parent) {
3486
+ return path3 === parent || path3.startsWith(`${parent}\\`) || path3.startsWith(`${parent}/`);
3487
+ }
3488
+ function uniqueMessages(messages) {
3489
+ return [...new Set(messages)];
3490
+ }
3491
+ function applyCodexSetup(plan) {
3492
+ const changed = [];
3493
+ const diagnostics = uniqueMessages([
3494
+ ...plan.diagnostics,
3495
+ ...plan.disclaimers
3496
+ ]);
3497
+ if (plan.dryRun) return { success: true, changed, diagnostics };
3498
+ try {
3499
+ for (const targetPath of managedRefreshRoots(plan)) {
3500
+ rmSync(targetPath, { recursive: true, force: true });
3501
+ }
3502
+ for (const item of plan.items) {
3503
+ if (item.action === "diagnose-only") continue;
3504
+ if (item.action === "merge-toml") {
3505
+ const result = writeCodexConfigMerge({
3506
+ configPath: item.targetPath,
3507
+ dryRun: false,
3508
+ pluginId: plan.pluginId
3509
+ });
3510
+ diagnostics.push(...result.diffSummary, ...result.warnings);
3511
+ if (!result.success) throw new Error(result.error);
3512
+ if (result.changed) changed.push(item.targetPath);
3513
+ continue;
3514
+ }
3515
+ if (item.action === "merge-marketplace") {
3516
+ if (item.content === void 0) continue;
3517
+ const content2 = mergePersonalMarketplace(
3518
+ existsSync7(item.targetPath) ? readFileSync6(item.targetPath, "utf8") : "",
3519
+ dirname4(dirname4(dirname4(item.targetPath))),
3520
+ item.content
3521
+ );
3522
+ if (writeTextWithBackup(item.targetPath, content2))
3523
+ changed.push(item.targetPath);
3524
+ continue;
3525
+ }
3526
+ if (item.content === void 0) continue;
3527
+ const content = item.action === "merge-managed-block" ? mergeManagedBlock(
3528
+ existsSync7(item.targetPath) ? readFileSync6(item.targetPath, "utf8") : "",
3529
+ item.content
3530
+ ) : item.content;
3531
+ if (writeTextWithBackup(item.targetPath, content))
3532
+ changed.push(item.targetPath);
3533
+ }
3534
+ return { success: true, changed, diagnostics: uniqueMessages(diagnostics) };
3535
+ } catch (error) {
3536
+ return {
3537
+ success: false,
3538
+ changed,
3539
+ diagnostics: uniqueMessages(diagnostics),
3540
+ error: error instanceof Error ? error.message : String(error)
3541
+ };
3542
+ }
3543
+ }
3544
+ function managedRefreshRoots(plan) {
3545
+ const refreshGroups = /* @__PURE__ */ new Map();
3546
+ for (const item of plan.items) {
3547
+ if (item.action !== "refresh-package") continue;
3548
+ const group = refreshGroups.get(item.kind) ?? [];
3549
+ group.push(item);
3550
+ refreshGroups.set(item.kind, group);
3551
+ }
3552
+ return [...refreshGroups].filter(([kind]) => kind === "personal-plugin-source").map(([, items]) => commonTargetDirectory(items)).filter((path3) => path3.length > 0);
3553
+ }
3554
+
3555
+ // src/cli/system.ts
3556
+ import { statSync as statSync2 } from "fs";
3557
+
3558
+ // src/utils/subprocess.ts
3559
+ import {
3560
+ spawn as nodeSpawn,
3561
+ spawnSync as nodeSpawnSync
3562
+ } from "child_process";
3563
+ import { Readable } from "stream";
3564
+ function emptyReadableStream() {
3565
+ return new ReadableStream({
3566
+ start(controller) {
3567
+ controller.close();
3568
+ }
3569
+ });
3570
+ }
3571
+ function toWebReadable(stream) {
3572
+ if (!stream) {
3573
+ return emptyReadableStream();
3574
+ }
3575
+ return Readable.toWeb(
3576
+ stream
3577
+ );
3578
+ }
3579
+ function fallbackStdin() {
3580
+ return {
3581
+ write: () => void 0,
3582
+ end: () => void 0
3583
+ };
3584
+ }
3585
+ function spawn(command, options = {}) {
3586
+ const child = nodeSpawn(command[0], command.slice(1), {
3587
+ cwd: options.cwd,
3588
+ env: options.env,
3589
+ stdio: [
3590
+ options.stdin ?? "pipe",
3591
+ options.stdout ?? "pipe",
3592
+ options.stderr ?? "pipe"
3593
+ ]
3594
+ });
3595
+ const managed = {
3596
+ stdin: child.stdin ?? fallbackStdin(),
3597
+ stdout: toWebReadable(child.stdout),
3598
+ stderr: toWebReadable(child.stderr),
3599
+ exited: new Promise((resolve2) => {
3600
+ child.on("exit", (code) => {
3601
+ managed.exitCode = code;
3602
+ resolve2(code ?? 1);
3603
+ });
3604
+ child.on("error", () => {
3605
+ managed.exitCode = 1;
3606
+ resolve2(1);
3607
+ });
3608
+ }),
3609
+ exitCode: child.exitCode,
3610
+ kill: () => {
3611
+ child.kill();
3612
+ }
3613
+ };
3614
+ return managed;
3615
+ }
3616
+
3617
+ // src/cli/system.ts
3618
+ var cachedOpenCodePath = null;
3619
+ function getOpenCodePaths() {
3620
+ const home = process.env.HOME || process.env.USERPROFILE || "";
3621
+ return [
3622
+ // PATH (try this first)
3623
+ "opencode",
3624
+ // User local installations (Linux & macOS)
3625
+ `${home}/.local/bin/opencode`,
3626
+ `${home}/.opencode/bin/opencode`,
3627
+ `${home}/bin/opencode`,
3628
+ // System-wide installations
3629
+ "/usr/local/bin/opencode",
3630
+ "/opt/opencode/bin/opencode",
3631
+ "/usr/bin/opencode",
3632
+ "/bin/opencode",
3633
+ // macOS specific
3634
+ "/Applications/OpenCode.app/Contents/MacOS/opencode",
3635
+ `${home}/Applications/OpenCode.app/Contents/MacOS/opencode`,
3636
+ // Homebrew (macOS & Linux)
3637
+ "/opt/homebrew/bin/opencode",
3638
+ "/home/linuxbrew/.linuxbrew/bin/opencode",
3639
+ `${home}/homebrew/bin/opencode`,
3640
+ // macOS user Library
3641
+ `${home}/Library/Application Support/opencode/bin/opencode`,
3642
+ // Snap (Linux)
3643
+ "/snap/bin/opencode",
3644
+ "/var/snap/opencode/current/bin/opencode",
3645
+ // Flatpak (Linux)
3646
+ "/var/lib/flatpak/exports/bin/ai.opencode.OpenCode",
3647
+ `${home}/.local/share/flatpak/exports/bin/ai.opencode.OpenCode`,
3648
+ // Nix (Linux/macOS)
3649
+ "/nix/store/opencode/bin/opencode",
3650
+ `${home}/.nix-profile/bin/opencode`,
3651
+ "/run/current-system/sw/bin/opencode",
3652
+ // Cargo (Rust toolchain)
3653
+ `${home}/.cargo/bin/opencode`,
3654
+ // npm/npx global
3655
+ `${home}/.npm-global/bin/opencode`,
3656
+ "/usr/local/lib/node_modules/opencode/bin/opencode",
3657
+ // Yarn global
3658
+ `${home}/.yarn/bin/opencode`,
3659
+ // PNPM
3660
+ `${home}/.pnpm-global/bin/opencode`
3661
+ ];
3662
+ }
3663
+ function resolveOpenCodePath() {
3664
+ if (cachedOpenCodePath) {
3665
+ return cachedOpenCodePath;
3666
+ }
3667
+ const paths = getOpenCodePaths();
3668
+ for (const opencodePath of paths) {
3669
+ if (opencodePath === "opencode") continue;
3670
+ try {
3671
+ const stat = statSync2(opencodePath);
3672
+ if (stat.isFile()) {
3673
+ cachedOpenCodePath = opencodePath;
3674
+ return opencodePath;
3675
+ }
3676
+ } catch {
3677
+ }
3678
+ }
3679
+ return "opencode";
3680
+ }
3681
+ async function isOpenCodeInstalled() {
3682
+ const paths = getOpenCodePaths();
3683
+ for (const opencodePath of paths) {
3684
+ try {
3685
+ const proc = spawn([opencodePath, "--version"], {
3686
+ stdout: "pipe",
3687
+ stderr: "pipe"
3688
+ });
3689
+ await proc.exited;
3690
+ if (proc.exitCode === 0) {
3691
+ cachedOpenCodePath = opencodePath;
3692
+ return true;
3693
+ }
3694
+ } catch {
3695
+ }
3696
+ }
3697
+ return false;
3698
+ }
3699
+ async function getOpenCodeVersion() {
3700
+ const opencodePath = resolveOpenCodePath();
3701
+ try {
3702
+ const proc = spawn([opencodePath, "--version"], {
3703
+ stdout: "pipe",
3704
+ stderr: "pipe"
3705
+ });
3706
+ const output = await new Response(proc.stdout).text();
3707
+ await proc.exited;
3708
+ if (proc.exitCode === 0) {
3709
+ return output.trim();
3710
+ }
3711
+ } catch {
3712
+ }
3713
+ return null;
3714
+ }
3715
+ function getOpenCodePath() {
3716
+ const path3 = resolveOpenCodePath();
3717
+ return path3 === "opencode" ? null : path3;
3718
+ }
3719
+
3720
+ // src/cli/custom-skills.ts
3721
+ import {
3722
+ copyFileSync as copyFileSync4,
3723
+ existsSync as existsSync9,
3724
+ mkdirSync as mkdirSync5,
3725
+ readdirSync as readdirSync3,
3726
+ rmSync as rmSync2,
3727
+ statSync as statSync4
3728
+ } from "fs";
3729
+ import { dirname as dirname5, join as join8, parse } from "path";
3730
+ import { fileURLToPath } from "url";
3731
+
3732
+ // src/cli/skill-manifest.ts
3733
+ import { createHash as createHash3 } from "crypto";
3734
+ import {
3735
+ existsSync as existsSync8,
3736
+ mkdirSync as mkdirSync4,
3737
+ readdirSync as readdirSync2,
3738
+ readFileSync as readFileSync7,
3739
+ statSync as statSync3,
3740
+ writeFileSync as writeFileSync4
3741
+ } from "fs";
3742
+ import { join as join7, relative as relative3 } from "path";
3743
+ var SHARED_SKILL_DIRECTORY = "_shared";
3744
+ var SKILLS_SOURCE_ROOT = join7("src", "skills");
3745
+ var MANIFEST_FILE_NAME = ".skill-manifest.json";
3746
+ function getManifestPath() {
3747
+ return join7(getCustomSkillsDir(), MANIFEST_FILE_NAME);
3748
+ }
3749
+ function listFilesRecursive(dirPath) {
3750
+ if (!existsSync8(dirPath)) {
3751
+ return [];
3752
+ }
3753
+ const files = [];
3754
+ const entries = readdirSync2(dirPath).sort(
3755
+ (left, right) => left.localeCompare(right)
3756
+ );
3757
+ for (const entry of entries) {
3758
+ const entryPath = join7(dirPath, entry);
3759
+ const stat = statSync3(entryPath);
3760
+ if (stat.isDirectory()) {
3761
+ files.push(...listFilesRecursive(entryPath));
3762
+ continue;
3763
+ }
3764
+ files.push(entryPath);
3765
+ }
3766
+ return files;
3767
+ }
3768
+ function readPackageVersion(packageRoot) {
3769
+ const packageJsonPath = join7(packageRoot, "package.json");
3770
+ const packageJson = JSON.parse(readFileSync7(packageJsonPath, "utf-8"));
3771
+ if (typeof packageJson.version !== "string" || packageJson.version.length === 0) {
3772
+ throw new Error(`Invalid package version in ${packageJsonPath}`);
3773
+ }
3774
+ return packageJson.version;
3775
+ }
3776
+ function readManifest() {
3777
+ const manifestPath = getManifestPath();
3778
+ if (!existsSync8(manifestPath)) {
3779
+ return null;
3780
+ }
3781
+ try {
3782
+ return JSON.parse(readFileSync7(manifestPath, "utf-8"));
3783
+ } catch (error) {
3784
+ console.warn(`Failed to read skill manifest: ${manifestPath}`, error);
3785
+ return null;
3786
+ }
3787
+ }
3788
+ function writeManifest(manifest) {
3789
+ const manifestPath = getManifestPath();
3790
+ mkdirSync4(getCustomSkillsDir(), { recursive: true });
3791
+ writeFileSync4(manifestPath, `${JSON.stringify(manifest, null, 2)}
3792
+ `);
3793
+ }
3794
+ function computeSkillHash(skillDirPath) {
3795
+ const hash = createHash3("sha256");
3796
+ for (const filePath of listFilesRecursive(skillDirPath)) {
3797
+ hash.update(`${relative3(skillDirPath, filePath)}
3798
+ `);
3799
+ hash.update(readFileSync7(filePath));
3800
+ hash.update("\n");
3801
+ }
3802
+ return hash.digest("hex");
3803
+ }
3804
+ function findRemovedSkills(manifest) {
3805
+ const currentSkillNames = new Set(CUSTOM_SKILLS.map((skill) => skill.name));
3806
+ return Object.keys(manifest.skills).filter(
3807
+ (skillName) => !currentSkillNames.has(skillName)
3808
+ );
3809
+ }
3810
+ function checkSkillsNeedUpdate(packageRoot) {
3811
+ const manifest = readManifest();
3812
+ const pluginVersion = readPackageVersion(packageRoot);
3813
+ const sharedHash = computeSkillHash(
3814
+ join7(packageRoot, SKILLS_SOURCE_ROOT, SHARED_SKILL_DIRECTORY)
3815
+ );
3816
+ const versionChanged = manifest !== null && manifest.pluginVersion !== pluginVersion;
3817
+ const sharedChanged = manifest !== null && manifest.sharedHash !== sharedHash;
3818
+ const removedSkills = manifest ? findRemovedSkills(manifest) : [];
3819
+ const skillsNeedingUpdate = CUSTOM_SKILLS.flatMap((skill) => {
3820
+ const sourcePath = join7(packageRoot, skill.sourcePath);
3821
+ const targetPath = join7(getCustomSkillsDir(), skill.name);
3822
+ const sourceHash = computeSkillHash(sourcePath);
3823
+ const manifestEntry = manifest?.skills[skill.name];
3824
+ const reasons = [];
3825
+ if (!manifest) {
3826
+ reasons.push("manifest-missing");
3827
+ }
3828
+ if (versionChanged) {
3829
+ reasons.push("version-change");
3830
+ }
3831
+ if (sharedChanged) {
3832
+ reasons.push("shared-hash-mismatch");
3833
+ }
3834
+ if (!manifestEntry) {
3835
+ reasons.push("new-skill");
3836
+ } else if (manifestEntry.hash !== sourceHash) {
3837
+ reasons.push("hash-mismatch");
3838
+ }
3839
+ if (!existsSync8(targetPath)) {
3840
+ reasons.push("missing-install");
3841
+ }
3842
+ if (reasons.length === 0) {
3843
+ return [];
3844
+ }
3845
+ return [
3846
+ {
3847
+ skill,
3848
+ sourceHash,
3849
+ targetPath,
3850
+ reasons
3851
+ }
3852
+ ];
3853
+ });
3854
+ return {
3855
+ pluginVersion,
3856
+ sharedHash,
3857
+ manifest,
3858
+ versionChanged,
3859
+ sharedChanged,
3860
+ needsUpdate: skillsNeedingUpdate.length > 0 || removedSkills.length > 0,
3861
+ skillsNeedingUpdate,
3862
+ removedSkills
3863
+ };
3864
+ }
3865
+
3866
+ // src/cli/custom-skills.ts
3867
+ var SHARED_SKILL_DIRECTORY2 = "_shared";
3868
+ var SHARED_SKILL_SOURCE_PATH = `src/skills/${SHARED_SKILL_DIRECTORY2}`;
3869
+ var CUSTOM_SKILLS = BUNDLED_SKILL_REGISTRY.map(
3870
+ (skill) => ({
3871
+ name: skill.name,
3872
+ description: skill.description,
3873
+ allowedAgents: [...skill.allowedRoles],
3874
+ sourcePath: skill.sourcePath
3875
+ })
3876
+ );
3877
+ function getCustomSkillsDir() {
3878
+ return join8(getConfigDir(), "skills");
3879
+ }
3880
+ function copyDirRecursive(src, dest) {
3881
+ if (!existsSync9(dest)) {
3882
+ mkdirSync5(dest, { recursive: true });
3883
+ }
3884
+ const entries = readdirSync3(src);
3885
+ for (const entry of entries) {
3886
+ const srcPath = join8(src, entry);
3887
+ const destPath = join8(dest, entry);
3888
+ const stat = statSync4(srcPath);
3889
+ if (stat.isDirectory()) {
3890
+ copyDirRecursive(srcPath, destPath);
3891
+ } else {
3892
+ const destDir = dirname5(destPath);
3893
+ if (!existsSync9(destDir)) {
3894
+ mkdirSync5(destDir, { recursive: true });
3895
+ }
3896
+ copyFileSync4(srcPath, destPath);
3897
+ }
3898
+ }
3899
+ }
3900
+ function installSharedSkillAssets(packageRoot) {
3901
+ const sharedSourcePath = join8(packageRoot, SHARED_SKILL_SOURCE_PATH);
3902
+ const sharedTargetPath = join8(getCustomSkillsDir(), SHARED_SKILL_DIRECTORY2);
3903
+ if (!existsSync9(sharedSourcePath)) {
3904
+ console.error(`Custom skill shared assets not found: ${sharedSourcePath}`);
3905
+ return false;
3906
+ }
3907
+ rmSync2(sharedTargetPath, { recursive: true, force: true });
3908
+ copyDirRecursive(sharedSourcePath, sharedTargetPath);
3909
+ return true;
3910
+ }
3911
+ function findPackageRoot(startDir) {
3912
+ let currentDir = startDir;
3913
+ const filesystemRoot = parse(startDir).root;
3914
+ while (true) {
3915
+ if (existsSync9(join8(currentDir, "package.json")) && existsSync9(join8(currentDir, "src", "skills"))) {
3916
+ return currentDir;
3917
+ }
3918
+ if (currentDir === filesystemRoot) {
3919
+ return null;
3920
+ }
3921
+ const parentDir = dirname5(currentDir);
3922
+ if (parentDir === currentDir) {
3923
+ return null;
3924
+ }
3925
+ currentDir = parentDir;
3926
+ }
3927
+ }
3928
+ function resolvePackageRoot(packageRoot) {
3929
+ if (packageRoot) {
3930
+ return packageRoot;
3931
+ }
3932
+ const moduleDir = fileURLToPath(new URL(".", import.meta.url));
3933
+ return findPackageRoot(moduleDir) ?? fileURLToPath(new URL("../..", import.meta.url));
3934
+ }
3935
+ function installCustomSkillFiles(skill, packageRoot) {
3936
+ const sourcePath = join8(packageRoot, skill.sourcePath);
3937
+ const targetPath = join8(getCustomSkillsDir(), skill.name);
3938
+ if (!existsSync9(sourcePath)) {
3939
+ console.error(`Custom skill source not found: ${sourcePath}`);
3940
+ return false;
3941
+ }
3942
+ rmSync2(targetPath, { recursive: true, force: true });
3943
+ copyDirRecursive(sourcePath, targetPath);
3944
+ return true;
3945
+ }
3946
+ function removeObsoleteSkills(removedSkillNames) {
3947
+ const removedSkills = [];
3948
+ for (const skillName of removedSkillNames) {
3949
+ const targetPath = join8(getCustomSkillsDir(), skillName);
3950
+ try {
3951
+ console.log(`Removing obsolete bundled skill: ${skillName}`);
3952
+ rmSync2(targetPath, { recursive: true, force: true });
3953
+ removedSkills.push(skillName);
3954
+ } catch (error) {
3955
+ console.warn(`Failed to remove obsolete bundled skill: ${skillName}`);
3956
+ console.warn(error);
3957
+ }
3958
+ }
3959
+ return removedSkills;
3960
+ }
3961
+ function buildManifest(packageRoot, updatedSkills, previousManifest, pluginVersion, sharedHash) {
3962
+ const installedAt = (/* @__PURE__ */ new Date()).toISOString();
3963
+ const updatedSkillNames = new Set(
3964
+ updatedSkills.map(({ skill }) => skill.name)
3965
+ );
3966
+ const skills = Object.fromEntries(
3967
+ CUSTOM_SKILLS.map((skill) => {
3968
+ const previousEntry = previousManifest?.skills[skill.name];
3969
+ const nextInstalledAt = updatedSkillNames.has(skill.name) ? installedAt : previousEntry?.installedAt ?? installedAt;
3970
+ return [
3971
+ skill.name,
3972
+ {
3973
+ hash: computeSkillHash(join8(packageRoot, skill.sourcePath)),
3974
+ installedAt: nextInstalledAt
3975
+ }
3976
+ ];
3977
+ })
3978
+ );
3979
+ return {
3980
+ pluginVersion,
3981
+ sharedHash,
3982
+ skills
3983
+ };
3984
+ }
3985
+ function pruneRemovedSkillsFromManifest(manifest, removedSkills) {
3986
+ const removedSkillNames = new Set(removedSkills);
3987
+ return {
3988
+ ...manifest,
3989
+ skills: Object.fromEntries(
3990
+ Object.entries(manifest.skills).filter(
3991
+ ([skillName]) => !removedSkillNames.has(skillName)
3992
+ )
3993
+ )
3994
+ };
3995
+ }
3996
+ function installCustomSkills(packageRoot = resolvePackageRoot()) {
3997
+ const updateCheck = checkSkillsNeedUpdate(packageRoot);
3998
+ if (!updateCheck.needsUpdate) {
3999
+ return {
4000
+ success: true,
4001
+ updatedSkills: [],
4002
+ skippedSkills: [...CUSTOM_SKILLS],
4003
+ failedSkills: [],
4004
+ removedSkills: []
4005
+ };
4006
+ }
4007
+ const removedSkills = removeObsoleteSkills(updateCheck.removedSkills);
4008
+ if (removedSkills.length > 0 && updateCheck.manifest) {
4009
+ writeManifest(
4010
+ pruneRemovedSkillsFromManifest(updateCheck.manifest, removedSkills)
4011
+ );
4012
+ }
4013
+ if (updateCheck.skillsNeedingUpdate.length === 0) {
4014
+ writeManifest(
4015
+ buildManifest(
4016
+ packageRoot,
4017
+ [],
4018
+ updateCheck.manifest,
4019
+ updateCheck.pluginVersion,
4020
+ updateCheck.sharedHash
4021
+ )
4022
+ );
4023
+ return {
4024
+ success: true,
4025
+ updatedSkills: [],
4026
+ skippedSkills: [...CUSTOM_SKILLS],
4027
+ failedSkills: [],
4028
+ removedSkills
4029
+ };
4030
+ }
4031
+ if (!installSharedSkillAssets(packageRoot)) {
4032
+ return {
4033
+ success: false,
4034
+ updatedSkills: [],
4035
+ skippedSkills: [],
4036
+ failedSkills: updateCheck.skillsNeedingUpdate.map(
4037
+ ({ skill, reasons }) => ({
4038
+ skill,
4039
+ reasons
4040
+ })
4041
+ ),
4042
+ removedSkills
4043
+ };
4044
+ }
4045
+ const updatesBySkillName = new Map(
4046
+ updateCheck.skillsNeedingUpdate.map((entry) => [entry.skill.name, entry])
4047
+ );
4048
+ const updatedSkills = [];
4049
+ const skippedSkills = [];
4050
+ const failedSkills = [];
4051
+ for (const skill of CUSTOM_SKILLS) {
4052
+ const pendingUpdate = updatesBySkillName.get(skill.name);
4053
+ if (!pendingUpdate) {
4054
+ skippedSkills.push(skill);
4055
+ continue;
4056
+ }
4057
+ if (installCustomSkillFiles(skill, packageRoot)) {
4058
+ updatedSkills.push({
4059
+ skill,
4060
+ reasons: pendingUpdate.reasons
4061
+ });
4062
+ continue;
4063
+ }
4064
+ failedSkills.push({
4065
+ skill,
4066
+ reasons: pendingUpdate.reasons
4067
+ });
4068
+ }
4069
+ if (failedSkills.length > 0) {
4070
+ return {
4071
+ success: false,
4072
+ updatedSkills,
4073
+ skippedSkills,
4074
+ failedSkills,
4075
+ removedSkills
4076
+ };
4077
+ }
4078
+ writeManifest(
4079
+ buildManifest(
4080
+ packageRoot,
4081
+ updatedSkills,
4082
+ updateCheck.manifest,
4083
+ updateCheck.pluginVersion,
4084
+ updateCheck.sharedHash
4085
+ )
4086
+ );
4087
+ return {
4088
+ success: true,
4089
+ updatedSkills,
4090
+ skippedSkills,
4091
+ failedSkills,
4092
+ removedSkills
4093
+ };
4094
+ }
4095
+
4096
+ // src/cli/skills.ts
4097
+ import { spawnSync } from "child_process";
4098
+ var RECOMMENDED_SKILLS = [
4099
+ {
4100
+ name: "simplify",
4101
+ repo: "https://github.com/brianlovin/claude-config",
4102
+ skillName: "simplify",
4103
+ description: "YAGNI code simplification expert"
4104
+ },
4105
+ {
4106
+ name: "playwright-cli",
4107
+ repo: "https://github.com/microsoft/playwright-cli",
4108
+ skillName: "playwright-cli",
4109
+ description: "Browser automation for visual checks and testing"
4110
+ }
4111
+ ];
4112
+ function installSkill(skill) {
4113
+ const args = [
4114
+ "skills",
4115
+ "add",
4116
+ skill.repo,
4117
+ "--skill",
4118
+ skill.skillName,
4119
+ "-a",
4120
+ "opencode",
4121
+ "-y",
4122
+ "--global"
4123
+ ];
4124
+ try {
4125
+ const result = spawnSync("npx", args, { stdio: "inherit" });
4126
+ if (result.status !== 0) {
4127
+ return false;
4128
+ }
4129
+ if (skill.postInstallCommands && skill.postInstallCommands.length > 0) {
4130
+ console.log(`Running post-install commands for ${skill.name}...`);
4131
+ for (const cmd of skill.postInstallCommands) {
4132
+ console.log(`> ${cmd}`);
4133
+ const [command, ...cmdArgs] = cmd.split(" ");
4134
+ const cmdResult = spawnSync(command, cmdArgs, { stdio: "inherit" });
4135
+ if (cmdResult.status !== 0) {
4136
+ console.warn(`Post-install command failed: ${cmd}`);
4137
+ }
4138
+ }
4139
+ }
4140
+ return true;
4141
+ } catch (error) {
4142
+ console.error(`Failed to install skill: ${skill.name}`, error);
4143
+ return false;
4144
+ }
4145
+ }
4146
+
4147
+ // src/cli/install.ts
4148
+ var GREEN = "\x1B[32m";
4149
+ var BLUE = "\x1B[34m";
4150
+ var YELLOW = "\x1B[33m";
4151
+ var RED = "\x1B[31m";
4152
+ var BOLD = "\x1B[1m";
4153
+ var DIM = "\x1B[2m";
4154
+ var RESET = "\x1B[0m";
4155
+ var SYMBOLS = {
4156
+ check: `${GREEN}\u2713${RESET}`,
4157
+ cross: `${RED}\u2717${RESET}`,
4158
+ arrow: `${BLUE}\u2192${RESET}`,
4159
+ bullet: `${DIM}\u2022${RESET}`,
4160
+ info: `${BLUE}\u2139${RESET}`,
4161
+ warn: `${YELLOW}\u26A0${RESET}`,
4162
+ star: `${YELLOW}\u2605${RESET}`
4163
+ };
4164
+ function printHeader(isUpdate) {
4165
+ console.log();
4166
+ console.log(`${BOLD}thoth-agents ${isUpdate ? "Update" : "Install"}${RESET}`);
4167
+ console.log("=".repeat(30));
4168
+ console.log();
4169
+ }
4170
+ function printStep(step, total, message) {
4171
+ console.log(`${DIM}[${step}/${total}]${RESET} ${message}`);
4172
+ }
4173
+ function printSuccess(message) {
4174
+ console.log(`${SYMBOLS.check} ${message}`);
4175
+ }
4176
+ function printError(message) {
4177
+ console.log(`${SYMBOLS.cross} ${RED}${message}${RESET}`);
4178
+ }
4179
+ function printInfo(message) {
4180
+ console.log(`${SYMBOLS.info} ${message}`);
4181
+ }
4182
+ function formatCustomSkillReasons(report) {
4183
+ if (report.updatedSkills.length === 0 && report.removedSkills.length === 0) {
4184
+ return "all bundled skills already up to date";
4185
+ }
4186
+ return `${report.updatedSkills.length} updated, ${report.removedSkills.length} removed, ${report.skippedSkills.length} unchanged`;
4187
+ }
4188
+ function formatSkillReasons(reasons) {
4189
+ return reasons.join(", ");
4190
+ }
4191
+ async function checkOpenCodeInstalled() {
4192
+ const installed = await isOpenCodeInstalled();
4193
+ if (!installed) {
4194
+ printError("OpenCode is not installed on this system.");
4195
+ printInfo("Install it with:");
4196
+ console.log(
4197
+ ` ${BLUE}curl -fsSL https://opencode.ai/install | bash${RESET}`
4198
+ );
4199
+ console.log();
4200
+ printInfo("Or if already installed, add it to your PATH:");
4201
+ console.log(` ${BLUE}export PATH="$HOME/.local/bin:$PATH"${RESET}`);
4202
+ console.log(` ${BLUE}export PATH="$HOME/.opencode/bin:$PATH"${RESET}`);
4203
+ return { ok: false };
4204
+ }
4205
+ const version = await getOpenCodeVersion();
4206
+ const path3 = getOpenCodePath();
4207
+ const detectedVersion = version ?? "";
4208
+ const pathInfo = path3 ? ` (${DIM}${path3}${RESET})` : "";
4209
+ printSuccess(`OpenCode ${detectedVersion} detected${pathInfo}`);
4210
+ return { ok: true, version: version ?? void 0, path: path3 ?? void 0 };
4211
+ }
4212
+ function handleStepResult(result, successMsg) {
4213
+ if (!result.success) {
4214
+ printError(`Failed: ${result.error}`);
4215
+ return false;
4216
+ }
4217
+ printSuccess(
4218
+ `${successMsg} ${SYMBOLS.arrow} ${DIM}${result.configPath}${RESET}`
4219
+ );
4220
+ return true;
4221
+ }
4222
+ function formatConfigSummary() {
4223
+ const lines = [];
4224
+ lines.push(`${BOLD}Configuration Summary${RESET}`);
4225
+ lines.push("");
4226
+ lines.push(` ${BOLD}Preset:${RESET} ${BLUE}openai${RESET}`);
4227
+ lines.push(` ${SYMBOLS.check} Seven-agent thoth-agents roster`);
4228
+ lines.push(` ${SYMBOLS.check} OpenAI models by default`);
4229
+ lines.push(` ${SYMBOLS.check} thoth-mem enabled for orchestrator memory`);
4230
+ lines.push(` ${SYMBOLS.check} Delegation results persisted to disk`);
4231
+ lines.push(` ${SYMBOLS.check} Bundled SDD skills ready for install`);
4232
+ const seeDocs = "see docs/provider-configurations.md";
4233
+ lines.push(` ${DIM}\u25CB Kimi \u2014 ${seeDocs}${RESET}`);
4234
+ lines.push(` ${DIM}\u25CB GitHub Copilot \u2014 ${seeDocs}${RESET}`);
4235
+ lines.push(` ${DIM}\u25CB ZAI Coding Plan \u2014 ${seeDocs}${RESET}`);
4236
+ return lines.join("\n");
4237
+ }
4238
+ async function runInstall(config) {
4239
+ const detected = detectCurrentConfig();
4240
+ const isUpdate = detected.isInstalled;
4241
+ printHeader(isUpdate);
4242
+ let totalSteps = 4;
4243
+ if (config.installSkills) totalSteps += 1;
4244
+ if (config.installCustomSkills) totalSteps += 1;
4245
+ let step = 1;
4246
+ printStep(step++, totalSteps, "Checking OpenCode installation...");
4247
+ if (config.dryRun) {
4248
+ printInfo("Dry run mode - skipping OpenCode check");
4249
+ } else {
4250
+ const { ok } = await checkOpenCodeInstalled();
4251
+ if (!ok) return 1;
4252
+ }
4253
+ printStep(step++, totalSteps, "Adding thoth-agents plugin...");
4254
+ if (config.dryRun) {
4255
+ printInfo("Dry run mode - skipping plugin installation");
4256
+ } else {
4257
+ const pluginResult = await addPluginToOpenCodeConfig();
4258
+ if (!handleStepResult(pluginResult, "Plugin added")) return 1;
4259
+ }
4260
+ printStep(step++, totalSteps, "Disabling OpenCode default agents...");
4261
+ if (config.dryRun) {
4262
+ printInfo("Dry run mode - skipping agent disabling");
4263
+ } else {
4264
+ const agentResult = disableDefaultAgents();
4265
+ if (!handleStepResult(agentResult, "Default agents disabled")) return 1;
4266
+ }
4267
+ printStep(step++, totalSteps, "Writing thoth-agents configuration...");
4268
+ if (config.dryRun) {
4269
+ const liteConfig = generateLiteConfig(config);
4270
+ printInfo("Dry run mode - configuration that would be written:");
4271
+ console.log(`
4272
+ ${JSON.stringify(liteConfig, null, 2)}
4273
+ `);
4274
+ } else {
4275
+ const configPath = getExistingLiteConfigPath();
4276
+ const configExists = existsSync10(configPath);
4277
+ if (configExists && !config.reset) {
4278
+ printInfo(
4279
+ `Configuration already exists at ${configPath}. Use --reset to overwrite.`
4280
+ );
4281
+ } else {
4282
+ const liteResult = writeLiteConfig(
4283
+ config,
4284
+ configExists ? configPath : void 0
4285
+ );
4286
+ if (!handleStepResult(
4287
+ liteResult,
4288
+ configExists ? "Config reset" : "Config written"
4289
+ ))
4290
+ return 1;
4291
+ }
4292
+ }
4293
+ if (config.installSkills) {
4294
+ printStep(step++, totalSteps, "Installing recommended external skills...");
4295
+ if (config.dryRun) {
4296
+ printInfo("Dry run mode - would install skills:");
4297
+ for (const skill of RECOMMENDED_SKILLS) {
4298
+ printInfo(` - ${skill.name}`);
4299
+ }
4300
+ } else {
4301
+ let skillsInstalled = 0;
4302
+ for (const skill of RECOMMENDED_SKILLS) {
4303
+ printInfo(`Installing ${skill.name}...`);
4304
+ if (installSkill(skill)) {
4305
+ printSuccess(`Installed: ${skill.name}`);
4306
+ skillsInstalled++;
4307
+ } else {
4308
+ printInfo(`Skipped: ${skill.name} (already installed)`);
4309
+ }
4310
+ }
4311
+ printSuccess(
4312
+ `${skillsInstalled}/${RECOMMENDED_SKILLS.length} skills processed`
4313
+ );
4314
+ }
4315
+ }
4316
+ if (config.installCustomSkills) {
4317
+ printStep(step++, totalSteps, "Installing bundled thoth-agents skills...");
4318
+ if (config.dryRun) {
4319
+ printInfo("Dry run mode - would install bundled skills:");
4320
+ for (const skill of CUSTOM_SKILLS) {
4321
+ printInfo(` - ${skill.name}`);
4322
+ }
4323
+ } else {
4324
+ const report = installCustomSkills();
4325
+ for (const updatedSkill of report.updatedSkills) {
4326
+ printSuccess(
4327
+ `Installed: ${updatedSkill.skill.name} (${formatSkillReasons(updatedSkill.reasons)})`
4328
+ );
4329
+ }
4330
+ for (const skippedSkill of report.skippedSkills) {
4331
+ printInfo(`Up to date: ${skippedSkill.name}`);
4332
+ }
4333
+ for (const removedSkill of report.removedSkills) {
4334
+ printInfo(`Removed obsolete skill: ${removedSkill}`);
4335
+ }
4336
+ for (const failedSkill of report.failedSkills) {
4337
+ printError(
4338
+ `Failed: ${failedSkill.skill.name} (${formatSkillReasons(failedSkill.reasons)})`
4339
+ );
4340
+ }
4341
+ if (!report.success) {
4342
+ return 1;
4343
+ }
4344
+ printSuccess(
4345
+ `Bundled skills processed: ${formatCustomSkillReasons(report)}`
4346
+ );
4347
+ }
4348
+ }
4349
+ console.log();
4350
+ console.log(formatConfigSummary());
4351
+ console.log();
4352
+ const statusMsg = isUpdate ? "thoth-agents updated!" : "thoth-agents installation complete!";
4353
+ console.log(`${SYMBOLS.star} ${BOLD}${GREEN}${statusMsg}${RESET}`);
4354
+ console.log();
4355
+ console.log(`${BOLD}Next steps:${RESET}`);
4356
+ console.log();
4357
+ console.log(` 1. Start OpenCode:`);
4358
+ console.log(` ${BLUE}$ opencode${RESET}`);
4359
+ console.log();
4360
+ const modelsInfo = "Default configuration uses OpenAI models (gpt-5.4 / gpt-5.4-mini).";
4361
+ console.log(`${BOLD}${modelsInfo}${RESET}`);
4362
+ console.log(
4363
+ ` ${DIM}Includes the seven-agent roster, thoth-mem memory defaults,${RESET}`
4364
+ );
4365
+ console.log(
4366
+ ` ${DIM}native task delegation, and bundled SDD skills.${RESET}`
4367
+ );
4368
+ const altProviders = "For alternative providers (Kimi, GitHub Copilot, ZAI Coding Plan)";
4369
+ console.log(`${BOLD}${altProviders}, see:${RESET}`);
4370
+ const docsUrl = "https://github.com/EremesNG/thoth-agents/blob/master/docs/provider-configurations.md";
4371
+ console.log(` ${BLUE}${docsUrl}${RESET}`);
4372
+ console.log();
4373
+ return 0;
4374
+ }
4375
+ function createInstallConfig(args) {
4376
+ return {
4377
+ agent: args.agent ?? "opencode",
4378
+ hasTmux: args.tmux === "yes",
4379
+ installSkills: args.skills === "yes",
4380
+ installCustomSkills: true,
4381
+ dryRun: args.dryRun,
4382
+ reset: args.reset ?? false
4383
+ };
4384
+ }
4385
+ async function install(args) {
4386
+ const config = createInstallConfig(args);
4387
+ if (config.agent === "codex") {
4388
+ const plan = buildCodexSetupPlan({
4389
+ dryRun: config.dryRun,
4390
+ reset: config.reset,
4391
+ scope: "user",
4392
+ projectRoot: cwd(),
4393
+ homeDir: homedir3()
4394
+ });
4395
+ console.log(formatCodexSetupPlan(plan));
4396
+ const result = applyCodexSetup(plan);
4397
+ for (const diagnostic of result.diagnostics) printInfo(diagnostic);
4398
+ if (!result.success) {
4399
+ printError(`Codex install failed: ${result.error}`);
4400
+ return 1;
4401
+ }
4402
+ printSuccess(
4403
+ config.dryRun ? "Codex dry-run complete; no files written" : "Codex agent-pack setup complete"
4404
+ );
4405
+ return 0;
4406
+ }
4407
+ return runInstall(config);
4408
+ }
4409
+
4410
+ // src/cli/index.ts
4411
+ function parseInstallArgs(args) {
4412
+ const result = {
4413
+ tui: true,
4414
+ agent: "opencode"
4415
+ };
4416
+ for (const arg of args) {
4417
+ if (arg === "--no-tui") {
4418
+ result.tui = false;
4419
+ } else if (arg.startsWith("--tmux=")) {
4420
+ result.tmux = arg.split("=")[1];
4421
+ } else if (arg.startsWith("--skills=")) {
4422
+ result.skills = arg.split("=")[1];
4423
+ } else if (arg === "--dry-run") {
4424
+ result.dryRun = true;
4425
+ } else if (arg === "--reset") {
4426
+ result.reset = true;
4427
+ } else if (arg.startsWith("--agent=")) {
4428
+ const agent = arg.split("=")[1];
4429
+ if (agent !== "opencode" && agent !== "codex") {
4430
+ throw new Error(
4431
+ `Unsupported install agent: ${agent}. Supported agents: opencode, codex.`
4432
+ );
4433
+ }
4434
+ result.agent = agent;
4435
+ } else if (arg === "-h" || arg === "--help") {
4436
+ printHelp();
4437
+ process.exit(0);
4438
+ }
4439
+ }
4440
+ return result;
4441
+ }
4442
+ function parseGenerateArgs(args) {
4443
+ const result = {};
4444
+ for (const arg of args) {
4445
+ if (arg.startsWith("--harness=")) {
4446
+ const harness = arg.split("=")[1];
4447
+ if (harness !== "codex") {
4448
+ return {
4449
+ command: "error",
4450
+ message: `Unsupported generate harness: ${harness}.`
4451
+ };
4452
+ }
4453
+ result.harness = harness;
4454
+ } else if (arg === "--dry-run") {
4455
+ result.dryRun = true;
4456
+ } else if (arg.startsWith("--output-root=")) {
4457
+ result.outputRoot = arg.split("=")[1];
4458
+ }
4459
+ }
4460
+ if (result.harness !== "codex") {
4461
+ return {
4462
+ command: "error",
4463
+ message: "Codex generation requires --harness=codex."
4464
+ };
4465
+ }
4466
+ return {
4467
+ command: "generate",
4468
+ generateArgs: {
4469
+ harness: result.harness,
4470
+ dryRun: result.dryRun,
4471
+ outputRoot: result.outputRoot
4472
+ }
4473
+ };
4474
+ }
4475
+ function parseCliArgs(args) {
4476
+ if (args.length === 0 || args[0] === "install") {
4477
+ const hasSubcommand = args[0] === "install";
4478
+ try {
4479
+ const installArgs = parseInstallArgs(args.slice(hasSubcommand ? 1 : 0));
4480
+ return { command: "install", installArgs };
4481
+ } catch (error) {
4482
+ return {
4483
+ command: "error",
4484
+ message: error instanceof Error ? error.message : String(error)
4485
+ };
4486
+ }
4487
+ }
4488
+ if (args[0] === "generate") {
4489
+ return parseGenerateArgs(args.slice(1));
4490
+ }
4491
+ if (args[0] === "-h" || args[0] === "--help") {
4492
+ return { command: "help" };
4493
+ }
4494
+ return { command: "error", message: `Unknown command: ${args[0]}` };
4495
+ }
4496
+ function printHelp() {
4497
+ console.log(`
4498
+ thoth-agents installer (thoth-agents)
4499
+
4500
+ Usage: pnpm dlx thoth-agents install [OPTIONS]
4501
+ pnpm dlx thoth-agents generate --harness=codex --dry-run
4502
+
4503
+ Options:
4504
+ --tmux=yes|no Enable tmux integration (yes/no)
4505
+ --skills=yes|no Install recommended external skills
4506
+ --no-tui Non-interactive mode
4507
+ --dry-run Simulate install without writing files
4508
+ --reset Repair managed installer-owned targets
4509
+ --agent=opencode|codex Select OpenCode plugin install (default) or Codex agent-pack setup
4510
+ -h, --help Show this help message
4511
+
4512
+ Generate options:
4513
+ --harness=codex Explicitly select Codex artifact generation
4514
+ --output-root=PATH Override generation root metadata
4515
+
4516
+ thoth-agents installs the seven-agent roster, thoth-mem defaults,
4517
+ native task delegation, and bundled SDD skills for OpenCode.
4518
+
4519
+ Bundled thoth-agents skills are always installed.
4520
+ Use --skills=no to skip only recommended external skills.
4521
+
4522
+ The generated config uses OpenAI by default.
4523
+ For alternative providers, see docs/provider-configurations.md.
4524
+
4525
+ Examples:
4526
+ pnpm dlx thoth-agents@latest install
4527
+ pnpm dlx thoth-agents@latest install --agent=opencode
4528
+ pnpm dlx thoth-agents@latest install --agent=codex
4529
+ pnpm dlx thoth-agents@latest install --agent=codex --dry-run
4530
+ pnpm dlx thoth-agents install --no-tui --tmux=no --skills=yes
4531
+ pnpm dlx thoth-agents install --dry-run
4532
+ pnpm dlx thoth-agents install --reset
4533
+ pnpm dlx thoth-agents generate --harness=codex --dry-run
4534
+ `);
4535
+ }
4536
+ function printCodexGeneration(args) {
4537
+ if (!args.dryRun) {
4538
+ console.error(
4539
+ "Codex generation is dry-run only in this MVP. Pass --dry-run."
4540
+ );
4541
+ return 1;
4542
+ }
4543
+ const result = codexAdapter.render({
4544
+ projectRoot: process.cwd(),
4545
+ options: {
4546
+ dryRun: true,
4547
+ outputRoot: args.outputRoot,
4548
+ targetHarness: "codex"
4549
+ }
4550
+ });
4551
+ console.log(JSON.stringify(result, null, 2));
4552
+ return 0;
4553
+ }
4554
+ async function main() {
4555
+ const parsed = parseCliArgs(process.argv.slice(2));
4556
+ if (parsed.command === "install") {
4557
+ const exitCode = await install(parsed.installArgs);
4558
+ process.exit(exitCode);
4559
+ } else if (parsed.command === "generate") {
4560
+ process.exit(printCodexGeneration(parsed.generateArgs));
4561
+ } else if (parsed.command === "help") {
4562
+ printHelp();
4563
+ process.exit(0);
4564
+ } else {
4565
+ console.error(parsed.message);
4566
+ console.error("Run with --help for usage information");
4567
+ process.exit(1);
4568
+ }
4569
+ }
4570
+ var entrypointUrl = process.argv[1] ? pathToFileURL(process.argv[1]).href : void 0;
4571
+ if (import.meta.url === entrypointUrl) {
4572
+ main().catch((err) => {
4573
+ console.error("Fatal error:", err);
4574
+ process.exit(1);
4575
+ });
4576
+ }
4577
+ export {
4578
+ parseCliArgs
4579
+ };