mini-trellis 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 (186) hide show
  1. package/COPYRIGHT +20 -0
  2. package/LICENSE +235 -0
  3. package/README.md +66 -0
  4. package/bin/mini-trellis.js +3 -0
  5. package/dist/cli/index.d.ts +3 -0
  6. package/dist/cli/index.d.ts.map +1 -0
  7. package/dist/cli/index.js +153 -0
  8. package/dist/cli/index.js.map +1 -0
  9. package/dist/commands/init.d.ts +19 -0
  10. package/dist/commands/init.d.ts.map +1 -0
  11. package/dist/commands/init.js +401 -0
  12. package/dist/commands/init.js.map +1 -0
  13. package/dist/commands/mem.d.ts +30 -0
  14. package/dist/commands/mem.d.ts.map +1 -0
  15. package/dist/commands/mem.js +412 -0
  16. package/dist/commands/mem.js.map +1 -0
  17. package/dist/commands/migrate.d.ts +40 -0
  18. package/dist/commands/migrate.d.ts.map +1 -0
  19. package/dist/commands/migrate.js +272 -0
  20. package/dist/commands/migrate.js.map +1 -0
  21. package/dist/commands/uninstall.d.ts +11 -0
  22. package/dist/commands/uninstall.d.ts.map +1 -0
  23. package/dist/commands/uninstall.js +135 -0
  24. package/dist/commands/uninstall.js.map +1 -0
  25. package/dist/commands/upgrade.d.ts +28 -0
  26. package/dist/commands/upgrade.d.ts.map +1 -0
  27. package/dist/commands/upgrade.js +84 -0
  28. package/dist/commands/upgrade.js.map +1 -0
  29. package/dist/configurators/claude.d.ts +3 -0
  30. package/dist/configurators/claude.d.ts.map +1 -0
  31. package/dist/configurators/claude.js +62 -0
  32. package/dist/configurators/claude.js.map +1 -0
  33. package/dist/configurators/codex.d.ts +3 -0
  34. package/dist/configurators/codex.d.ts.map +1 -0
  35. package/dist/configurators/codex.js +21 -0
  36. package/dist/configurators/codex.js.map +1 -0
  37. package/dist/configurators/index.d.ts +24 -0
  38. package/dist/configurators/index.d.ts.map +1 -0
  39. package/dist/configurators/index.js +80 -0
  40. package/dist/configurators/index.js.map +1 -0
  41. package/dist/configurators/opencode.d.ts +2 -0
  42. package/dist/configurators/opencode.d.ts.map +1 -0
  43. package/dist/configurators/opencode.js +62 -0
  44. package/dist/configurators/opencode.js.map +1 -0
  45. package/dist/configurators/pi.d.ts +2 -0
  46. package/dist/configurators/pi.d.ts.map +1 -0
  47. package/dist/configurators/pi.js +18 -0
  48. package/dist/configurators/pi.js.map +1 -0
  49. package/dist/configurators/shared.d.ts +147 -0
  50. package/dist/configurators/shared.d.ts.map +1 -0
  51. package/dist/configurators/shared.js +377 -0
  52. package/dist/configurators/shared.js.map +1 -0
  53. package/dist/configurators/workflow.d.ts +15 -0
  54. package/dist/configurators/workflow.d.ts.map +1 -0
  55. package/dist/configurators/workflow.js +46 -0
  56. package/dist/configurators/workflow.js.map +1 -0
  57. package/dist/constants/paths.d.ts +74 -0
  58. package/dist/constants/paths.d.ts.map +1 -0
  59. package/dist/constants/paths.js +83 -0
  60. package/dist/constants/paths.js.map +1 -0
  61. package/dist/constants/version.d.ts +9 -0
  62. package/dist/constants/version.d.ts.map +1 -0
  63. package/dist/constants/version.js +15 -0
  64. package/dist/constants/version.js.map +1 -0
  65. package/dist/index.d.ts +9 -0
  66. package/dist/index.d.ts.map +1 -0
  67. package/dist/index.js +9 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/templates/claude/index.d.ts +11 -0
  70. package/dist/templates/claude/index.d.ts.map +1 -0
  71. package/dist/templates/claude/index.js +17 -0
  72. package/dist/templates/claude/index.js.map +1 -0
  73. package/dist/templates/claude/settings.json +40 -0
  74. package/dist/templates/codex/config.toml +39 -0
  75. package/dist/templates/codex/hooks.json +15 -0
  76. package/dist/templates/codex/index.d.ts +11 -0
  77. package/dist/templates/codex/index.d.ts.map +1 -0
  78. package/dist/templates/codex/index.js +22 -0
  79. package/dist/templates/codex/index.js.map +1 -0
  80. package/dist/templates/common/bundled-skills/mini-trellis-session-insight/SKILL.md +37 -0
  81. package/dist/templates/common/bundled-skills/mini-trellis-session-insight/references/cli-quick-reference.md +36 -0
  82. package/dist/templates/common/bundled-skills/mini-trellis-session-insight/references/triggering-patterns.md +93 -0
  83. package/dist/templates/common/commands/remember.md +22 -0
  84. package/dist/templates/common/index.d.ts +48 -0
  85. package/dist/templates/common/index.d.ts.map +1 -0
  86. package/dist/templates/common/index.js +104 -0
  87. package/dist/templates/common/index.js.map +1 -0
  88. package/dist/templates/common/skills/update-spec.md +16 -0
  89. package/dist/templates/extract.d.ts +40 -0
  90. package/dist/templates/extract.d.ts.map +1 -0
  91. package/dist/templates/extract.js +106 -0
  92. package/dist/templates/extract.js.map +1 -0
  93. package/dist/templates/markdown/agents.md +15 -0
  94. package/dist/templates/markdown/index.d.ts +10 -0
  95. package/dist/templates/markdown/index.d.ts.map +1 -0
  96. package/dist/templates/markdown/index.js +18 -0
  97. package/dist/templates/markdown/index.js.map +1 -0
  98. package/dist/templates/markdown/research-readme.md +9 -0
  99. package/dist/templates/markdown/spec/guides/index.md.txt +5 -0
  100. package/dist/templates/markdown/spec/guides/mini-memory.md.txt +30 -0
  101. package/dist/templates/markdown/workspace-index.md +23 -0
  102. package/dist/templates/opencode/lib/context-visibility.js +148 -0
  103. package/dist/templates/opencode/lib/session-utils.js +394 -0
  104. package/dist/templates/opencode/lib/trellis-context.js +627 -0
  105. package/dist/templates/opencode/package.json +5 -0
  106. package/dist/templates/opencode/plugins/session-start.js +65 -0
  107. package/dist/templates/pi/extensions/mini-trellis/index.ts.txt +170 -0
  108. package/dist/templates/pi/index.d.ts +4 -0
  109. package/dist/templates/pi/index.d.ts.map +1 -0
  110. package/dist/templates/pi/index.js +9 -0
  111. package/dist/templates/pi/index.js.map +1 -0
  112. package/dist/templates/pi/settings.json +9 -0
  113. package/dist/templates/shared-hooks/index.d.ts +13 -0
  114. package/dist/templates/shared-hooks/index.d.ts.map +1 -0
  115. package/dist/templates/shared-hooks/index.js +26 -0
  116. package/dist/templates/shared-hooks/index.js.map +1 -0
  117. package/dist/templates/shared-hooks/session-start.py +676 -0
  118. package/dist/templates/template-utils.d.ts +26 -0
  119. package/dist/templates/template-utils.d.ts.map +1 -0
  120. package/dist/templates/template-utils.js +64 -0
  121. package/dist/templates/template-utils.js.map +1 -0
  122. package/dist/templates/trellis/config.yaml +34 -0
  123. package/dist/templates/trellis/gitattributes.txt +9 -0
  124. package/dist/templates/trellis/gitignore.txt +32 -0
  125. package/dist/templates/trellis/index.d.ts +8 -0
  126. package/dist/templates/trellis/index.d.ts.map +1 -0
  127. package/dist/templates/trellis/index.js +16 -0
  128. package/dist/templates/trellis/index.js.map +1 -0
  129. package/dist/templates/trellis/scripts/__init__.py +5 -0
  130. package/dist/templates/trellis/scripts/add_session.py +1545 -0
  131. package/dist/templates/trellis/scripts/common/__init__.py +94 -0
  132. package/dist/templates/trellis/scripts/common/active_task.py +855 -0
  133. package/dist/templates/trellis/scripts/common/cli_adapter.py +950 -0
  134. package/dist/templates/trellis/scripts/common/config.py +482 -0
  135. package/dist/templates/trellis/scripts/common/developer.py +192 -0
  136. package/dist/templates/trellis/scripts/common/git.py +203 -0
  137. package/dist/templates/trellis/scripts/common/git_context.py +106 -0
  138. package/dist/templates/trellis/scripts/common/io.py +160 -0
  139. package/dist/templates/trellis/scripts/common/log.py +45 -0
  140. package/dist/templates/trellis/scripts/common/packages_context.py +238 -0
  141. package/dist/templates/trellis/scripts/common/paths.py +543 -0
  142. package/dist/templates/trellis/scripts/common/safe_commit.py +322 -0
  143. package/dist/templates/trellis/scripts/common/session_context.py +708 -0
  144. package/dist/templates/trellis/scripts/common/task_context.py +470 -0
  145. package/dist/templates/trellis/scripts/common/task_queue.py +188 -0
  146. package/dist/templates/trellis/scripts/common/task_store.py +1930 -0
  147. package/dist/templates/trellis/scripts/common/task_utils.py +498 -0
  148. package/dist/templates/trellis/scripts/common/tasks.py +122 -0
  149. package/dist/templates/trellis/scripts/common/trellis_config.py +239 -0
  150. package/dist/templates/trellis/scripts/common/types.py +110 -0
  151. package/dist/templates/trellis/scripts/common/workflow_phase.py +242 -0
  152. package/dist/templates/trellis/scripts/get_context.py +16 -0
  153. package/dist/templates/trellis/scripts/init_developer.py +51 -0
  154. package/dist/types/ai-tools.d.ts +30 -0
  155. package/dist/types/ai-tools.d.ts.map +1 -0
  156. package/dist/types/ai-tools.js +89 -0
  157. package/dist/types/ai-tools.js.map +1 -0
  158. package/dist/utils/atomic-write.d.ts +12 -0
  159. package/dist/utils/atomic-write.d.ts.map +1 -0
  160. package/dist/utils/atomic-write.js +30 -0
  161. package/dist/utils/atomic-write.js.map +1 -0
  162. package/dist/utils/cwd-guard.d.ts +38 -0
  163. package/dist/utils/cwd-guard.d.ts.map +1 -0
  164. package/dist/utils/cwd-guard.js +62 -0
  165. package/dist/utils/cwd-guard.js.map +1 -0
  166. package/dist/utils/file-writer.d.ts +36 -0
  167. package/dist/utils/file-writer.d.ts.map +1 -0
  168. package/dist/utils/file-writer.js +204 -0
  169. package/dist/utils/file-writer.js.map +1 -0
  170. package/dist/utils/managed-paths.d.ts +5 -0
  171. package/dist/utils/managed-paths.d.ts.map +1 -0
  172. package/dist/utils/managed-paths.js +51 -0
  173. package/dist/utils/managed-paths.js.map +1 -0
  174. package/dist/utils/managed-removal.d.ts +42 -0
  175. package/dist/utils/managed-removal.d.ts.map +1 -0
  176. package/dist/utils/managed-removal.js +223 -0
  177. package/dist/utils/managed-removal.js.map +1 -0
  178. package/dist/utils/posix.d.ts +13 -0
  179. package/dist/utils/posix.d.ts.map +1 -0
  180. package/dist/utils/posix.js +15 -0
  181. package/dist/utils/posix.js.map +1 -0
  182. package/dist/utils/uninstall-scrubbers.d.ts +66 -0
  183. package/dist/utils/uninstall-scrubbers.d.ts.map +1 -0
  184. package/dist/utils/uninstall-scrubbers.js +417 -0
  185. package/dist/utils/uninstall-scrubbers.js.map +1 -0
  186. package/package.json +80 -0
@@ -0,0 +1,394 @@
1
+ /* global process */
2
+ import { existsSync, readFileSync, readdirSync, statSync } from "fs"
3
+ import { join } from "path"
4
+ import { execFileSync } from "child_process"
5
+ import { platform } from "os"
6
+ import { debugLog } from "./trellis-context.js"
7
+
8
+ const PYTHON_CMD = platform() === "win32" ? "python" : "python3"
9
+
10
+ const FIRST_REPLY_NOTICE = `<first-reply-notice>
11
+ On the first visible assistant reply in this session, briefly acknowledge that mini-trellis SessionStart context loaded.
12
+ Choose the acknowledgment language in this order:
13
+ 1. Use the language of the user's current request (the user message that triggered this reply).
14
+ 2. If that request has no clear natural language, use an explicitly established project communication language.
15
+ 3. If neither provides a language, output the language-neutral fallback exactly: \`mini-trellis SessionStart ✓\`.
16
+ Continue directly with the user's request after the acknowledgment.
17
+ The acknowledgment must not alter the language used for the remainder of the response.
18
+ This notice is one-shot: do not repeat it after the first visible assistant reply in this session.
19
+ </first-reply-notice>`
20
+
21
+ function loadTrellisConfig(directory, contextKey = null) {
22
+ const scriptPath = join(directory, ".trellis", "scripts", "get_context.py")
23
+ if (!existsSync(scriptPath)) {
24
+ return { isMonorepo: false, packages: {}, specScope: null, activeTaskPackage: null, defaultPackage: null }
25
+ }
26
+ try {
27
+ const output = execFileSync(PYTHON_CMD, [scriptPath, "--mode", "packages", "--json"], {
28
+ cwd: directory,
29
+ timeout: 5000,
30
+ encoding: "utf-8",
31
+ stdio: ["pipe", "pipe", "pipe"],
32
+ env: {
33
+ ...process.env,
34
+ ...(contextKey ? { TRELLIS_CONTEXT_ID: contextKey } : {}),
35
+ },
36
+ })
37
+ const data = JSON.parse(output)
38
+ if (data.mode !== "monorepo") {
39
+ return { isMonorepo: false, packages: {}, specScope: null, activeTaskPackage: null, defaultPackage: null }
40
+ }
41
+ const pkgDict = {}
42
+ for (const pkg of (data.packages || [])) {
43
+ pkgDict[pkg.name] = pkg
44
+ }
45
+ return {
46
+ isMonorepo: true,
47
+ packages: pkgDict,
48
+ specScope: data.specScope || null,
49
+ activeTaskPackage: data.activeTaskPackage || null,
50
+ defaultPackage: data.defaultPackage || null,
51
+ }
52
+ } catch (e) {
53
+ debugLog("session", "loadTrellisConfig error:", e.message)
54
+ return { isMonorepo: false, packages: {}, specScope: null, activeTaskPackage: null, defaultPackage: null }
55
+ }
56
+ }
57
+
58
+ function checkLegacySpec(directory, config) {
59
+ if (!config.isMonorepo || Object.keys(config.packages).length === 0) {
60
+ return null
61
+ }
62
+
63
+ const specDir = join(directory, ".trellis", "spec")
64
+ if (!existsSync(specDir)) return null
65
+
66
+ let hasLegacy = false
67
+ for (const name of ["backend", "frontend"]) {
68
+ if (existsSync(join(specDir, name, "index.md"))) {
69
+ hasLegacy = true
70
+ break
71
+ }
72
+ }
73
+ if (!hasLegacy) return null
74
+
75
+ const pkgNames = Object.keys(config.packages).sort()
76
+ const missing = pkgNames.filter(name => !existsSync(join(specDir, name)))
77
+
78
+ if (missing.length === 0) return null
79
+
80
+ if (missing.length === pkgNames.length) {
81
+ return (
82
+ `[!] Legacy spec structure detected: found \`spec/backend/\` or \`spec/frontend/\` ` +
83
+ `but no package-scoped \`spec/<package>/\` directories.\n` +
84
+ `Monorepo packages: ${pkgNames.join(", ")}\n` +
85
+ `Please reorganize: \`spec/backend/\` -> \`spec/<package>/backend/\``
86
+ )
87
+ }
88
+ return (
89
+ `[!] Partial spec migration detected: packages ${missing.join(", ")} ` +
90
+ `still missing \`spec/<pkg>/\` directory.\n` +
91
+ `Please complete migration for all packages.`
92
+ )
93
+ }
94
+
95
+ function resolveSpecScope(config) {
96
+ if (!config.isMonorepo || Object.keys(config.packages).length === 0) {
97
+ return null
98
+ }
99
+
100
+ const { specScope, activeTaskPackage, defaultPackage, packages } = config
101
+ if (specScope == null) return null
102
+
103
+ if (specScope === "active_task") {
104
+ if (activeTaskPackage && activeTaskPackage in packages) return new Set([activeTaskPackage])
105
+ if (defaultPackage && defaultPackage in packages) return new Set([defaultPackage])
106
+ return null
107
+ }
108
+
109
+ if (Array.isArray(specScope)) {
110
+ const valid = new Set()
111
+ for (const entry of specScope) {
112
+ if (entry in packages) {
113
+ valid.add(entry)
114
+ }
115
+ }
116
+ if (valid.size > 0) return valid
117
+ if (activeTaskPackage && activeTaskPackage in packages) return new Set([activeTaskPackage])
118
+ if (defaultPackage && defaultPackage in packages) return new Set([defaultPackage])
119
+ return null
120
+ }
121
+
122
+ return null
123
+ }
124
+
125
+ function collectResearchTopics(directory) {
126
+ const researchDir = join(directory, ".trellis", "research")
127
+ if (!existsSync(researchDir)) return []
128
+ try {
129
+ return readdirSync(researchDir)
130
+ .filter(name => {
131
+ if (name.startsWith(".") || name.toLowerCase() === "readme.md") return false
132
+ try {
133
+ return statSync(join(researchDir, name)).isFile() && name.toLowerCase().endsWith(".md")
134
+ } catch {
135
+ return false
136
+ }
137
+ })
138
+ .sort()
139
+ .map(name => `.trellis/research/${name}`)
140
+ } catch {
141
+ return []
142
+ }
143
+ }
144
+
145
+ function collectSpecIndexPaths(directory, allowedPkgs) {
146
+ const specDir = join(directory, ".trellis", "spec")
147
+ const paths = []
148
+
149
+ const guidesIndex = join(specDir, "guides", "index.md")
150
+ if (existsSync(guidesIndex)) {
151
+ paths.push(".trellis/spec/guides/index.md")
152
+ }
153
+
154
+ if (!existsSync(specDir)) return paths
155
+
156
+ try {
157
+ const subs = readdirSync(specDir).filter(name => {
158
+ if (name.startsWith(".") || name === "guides") return false
159
+ try {
160
+ return statSync(join(specDir, name)).isDirectory()
161
+ } catch {
162
+ return false
163
+ }
164
+ }).sort()
165
+
166
+ for (const sub of subs) {
167
+ const indexFile = join(specDir, sub, "index.md")
168
+ if (existsSync(indexFile)) {
169
+ paths.push(`.trellis/spec/${sub}/index.md`)
170
+ } else {
171
+ if (allowedPkgs !== null && !allowedPkgs.has(sub)) continue
172
+ try {
173
+ const nested = readdirSync(join(specDir, sub)).filter(name => {
174
+ try {
175
+ return statSync(join(specDir, sub, name)).isDirectory()
176
+ } catch {
177
+ return false
178
+ }
179
+ }).sort()
180
+ for (const layer of nested) {
181
+ const nestedIndex = join(specDir, sub, layer, "index.md")
182
+ if (existsSync(nestedIndex)) {
183
+ paths.push(`.trellis/spec/${sub}/${layer}/index.md`)
184
+ }
185
+ }
186
+ } catch {
187
+ // Ignore directory read errors
188
+ }
189
+ }
190
+ }
191
+ } catch {
192
+ // Ignore spec directory read errors
193
+ }
194
+
195
+ return paths
196
+ }
197
+
198
+ function readDeveloper(directory) {
199
+ try {
200
+ const content = readFileSync(join(directory, ".trellis", ".developer"), "utf-8")
201
+ for (const line of content.split(/\r?\n/)) {
202
+ if (line.startsWith("name=")) return line.slice("name=".length).trim()
203
+ }
204
+ } catch {
205
+ // Ignore missing developer file
206
+ }
207
+ return "(not initialized)"
208
+ }
209
+
210
+ function runGit(directory, args) {
211
+ try {
212
+ return execFileSync("git", args, {
213
+ cwd: directory,
214
+ timeout: 3000,
215
+ encoding: "utf-8",
216
+ stdio: ["pipe", "pipe", "pipe"],
217
+ }).trim()
218
+ } catch {
219
+ return ""
220
+ }
221
+ }
222
+
223
+ function buildCompactCurrentState(ctx, specIndexPaths, researchTopics) {
224
+ const directory = ctx.directory
225
+ const lines = []
226
+ lines.push(`Developer: ${readDeveloper(directory)}`)
227
+
228
+ const branch = runGit(directory, ["branch", "--show-current"]) || "(detached)"
229
+ const dirtyCount = runGit(directory, ["status", "--porcelain"])
230
+ .split(/\r?\n/)
231
+ .filter(line => line.trim()).length
232
+ lines.push(`Git: branch ${branch}; ${dirtyCount === 0 ? "clean" : `dirty ${dirtyCount} paths`}.`)
233
+
234
+ const developer = readDeveloper(directory)
235
+ const workspaceDir = join(directory, ".trellis", "workspace", developer)
236
+ if (developer !== "(not initialized)" && existsSync(workspaceDir)) {
237
+ try {
238
+ const journals = readdirSync(workspaceDir)
239
+ .filter(name => /^journal-\d+\.md$/.test(name))
240
+ .sort((a, b) => Number(a.match(/\d+/)?.[0] || 0) - Number(b.match(/\d+/)?.[0] || 0))
241
+ const journal = journals[journals.length - 1]
242
+ if (journal) {
243
+ const journalPath = join(workspaceDir, journal)
244
+ const lineCount = readFileSync(journalPath, "utf-8").split(/\r?\n/).length
245
+ lines.push(`Journal: .trellis/workspace/${developer}/${journal}, ${lineCount} / 2000 lines.`)
246
+ }
247
+ } catch {
248
+ // Ignore journal errors
249
+ }
250
+ }
251
+
252
+ if (specIndexPaths.length > 0) {
253
+ lines.push(`Spec indexes: ${specIndexPaths.length} available.`)
254
+ }
255
+
256
+ if (researchTopics.length > 0) {
257
+ lines.push(`Research notes: ${researchTopics.length} in .trellis/research/.`)
258
+ } else {
259
+ lines.push("Research notes: none yet. Write .trellis/research/<topic>.md.")
260
+ }
261
+
262
+ return lines.join("\n")
263
+ }
264
+
265
+ export function buildSessionContext(ctx, platformInput = null) {
266
+ const directory = ctx.directory
267
+ const contextKey = typeof ctx.getContextKey === "function"
268
+ ? ctx.getContextKey(platformInput)
269
+ : null
270
+
271
+ const config = loadTrellisConfig(directory, contextKey)
272
+ const allowedPkgs = resolveSpecScope(config)
273
+ const paths = collectSpecIndexPaths(directory, allowedPkgs)
274
+ const researchTopics = collectResearchTopics(directory)
275
+
276
+ const parts = []
277
+
278
+ parts.push(`<session-context>
279
+ mini-trellis SessionStart context. Orient from journal, spec, and research.
280
+ </session-context>`)
281
+ parts.push(FIRST_REPLY_NOTICE)
282
+
283
+ const legacyWarning = checkLegacySpec(directory, config)
284
+ if (legacyWarning) {
285
+ parts.push(`<migration-warning>\n${legacyWarning}\n</migration-warning>`)
286
+ }
287
+
288
+ parts.push("<current-state>")
289
+ parts.push(buildCompactCurrentState(ctx, paths, researchTopics))
290
+ parts.push("</current-state>")
291
+
292
+ parts.push("<guidelines>")
293
+ parts.push(
294
+ "Memory: journal is git-durable session notes (`/mini-trellis:remember` or " +
295
+ "`python3 ./.trellis/scripts/add_session.py`). Cross-session dialogue is " +
296
+ "`mini-trellis mem list|search|context|extract`.\n" +
297
+ "Research lives in `.trellis/research/<topic>.md`; promote durable " +
298
+ "boundaries into `.trellis/spec/` as short markdown.\n"
299
+ )
300
+
301
+ if (paths.length > 0) {
302
+ parts.push("## Spec indexes (read on demand)")
303
+ for (const p of paths) {
304
+ parts.push(`- ${p}`)
305
+ }
306
+ parts.push("")
307
+ }
308
+
309
+ if (researchTopics.length > 0) {
310
+ parts.push("## Research notes (read on demand)")
311
+ for (const p of researchTopics) {
312
+ parts.push(`- ${p}`)
313
+ }
314
+ parts.push("")
315
+ }
316
+
317
+ parts.push("</guidelines>")
318
+
319
+ parts.push(`<ready>
320
+ Context loaded. Use journal, spec, research, and \`mini-trellis mem\` on demand. Remember at session end or after compact.
321
+ </ready>`)
322
+
323
+ return parts.join("\n\n")
324
+ }
325
+
326
+ function getTrellisMetadata(metadata) {
327
+ if (!metadata || typeof metadata !== "object") {
328
+ return {}
329
+ }
330
+
331
+ const trellis = metadata.trellis
332
+ if (!trellis || typeof trellis !== "object") {
333
+ return {}
334
+ }
335
+
336
+ return trellis
337
+ }
338
+
339
+ function markPartAsSessionStart(part) {
340
+ const metadata = part.metadata && typeof part.metadata === "object"
341
+ ? part.metadata
342
+ : {}
343
+ part.metadata = {
344
+ ...metadata,
345
+ trellis: {
346
+ ...getTrellisMetadata(metadata),
347
+ sessionStart: true,
348
+ },
349
+ }
350
+ }
351
+
352
+ function hasSessionStartMarker(part) {
353
+ if (!part || part.type !== "text" || typeof part.text !== "string") {
354
+ return false
355
+ }
356
+
357
+ return getTrellisMetadata(part.metadata).sessionStart === true
358
+ }
359
+
360
+ export function hasInjectedTrellisContext(messages) {
361
+ if (!Array.isArray(messages)) {
362
+ return false
363
+ }
364
+
365
+ return messages.some(message => {
366
+ if (!message?.info || message.info.role !== "user" || !Array.isArray(message.parts)) {
367
+ return false
368
+ }
369
+
370
+ return message.parts.some(hasSessionStartMarker)
371
+ })
372
+ }
373
+
374
+ export async function hasPersistedInjectedContext(client, directory, sessionID) {
375
+ try {
376
+ const response = await client.session.messages({
377
+ path: { id: sessionID },
378
+ query: { directory },
379
+ throwOnError: true,
380
+ })
381
+ return hasInjectedTrellisContext(response.data || [])
382
+ } catch (error) {
383
+ debugLog(
384
+ "session",
385
+ "Failed to read session history for dedupe:",
386
+ error instanceof Error ? error.message : String(error),
387
+ )
388
+ return false
389
+ }
390
+ }
391
+
392
+ export function markContextInjected(part) {
393
+ markPartAsSessionStart(part)
394
+ }