ag-cortex 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 (162) hide show
  1. package/.agent/commands/test-browser.md +339 -0
  2. package/.agent/rules/00-constitution.md +46 -0
  3. package/.agent/rules/project-rules.md +49 -0
  4. package/.agent/skills/agent-browser/SKILL.md +223 -0
  5. package/.agent/skills/agent-native-architecture/SKILL.md +435 -0
  6. package/.agent/skills/agent-native-architecture/references/action-parity-discipline.md +409 -0
  7. package/.agent/skills/agent-native-architecture/references/agent-execution-patterns.md +467 -0
  8. package/.agent/skills/agent-native-architecture/references/agent-native-testing.md +582 -0
  9. package/.agent/skills/agent-native-architecture/references/architecture-patterns.md +478 -0
  10. package/.agent/skills/agent-native-architecture/references/dynamic-context-injection.md +338 -0
  11. package/.agent/skills/agent-native-architecture/references/files-universal-interface.md +301 -0
  12. package/.agent/skills/agent-native-architecture/references/from-primitives-to-domain-tools.md +359 -0
  13. package/.agent/skills/agent-native-architecture/references/mcp-tool-design.md +506 -0
  14. package/.agent/skills/agent-native-architecture/references/mobile-patterns.md +871 -0
  15. package/.agent/skills/agent-native-architecture/references/product-implications.md +443 -0
  16. package/.agent/skills/agent-native-architecture/references/refactoring-to-prompt-native.md +317 -0
  17. package/.agent/skills/agent-native-architecture/references/self-modification.md +269 -0
  18. package/.agent/skills/agent-native-architecture/references/shared-workspace-architecture.md +680 -0
  19. package/.agent/skills/agent-native-architecture/references/system-prompt-design.md +250 -0
  20. package/.agent/skills/agent-native-reviewer/SKILL.md +246 -0
  21. package/.agent/skills/andrew-kane-gem-writer/SKILL.md +184 -0
  22. package/.agent/skills/andrew-kane-gem-writer/references/database-adapters.md +231 -0
  23. package/.agent/skills/andrew-kane-gem-writer/references/module-organization.md +121 -0
  24. package/.agent/skills/andrew-kane-gem-writer/references/rails-integration.md +183 -0
  25. package/.agent/skills/andrew-kane-gem-writer/references/resources.md +119 -0
  26. package/.agent/skills/andrew-kane-gem-writer/references/testing-patterns.md +261 -0
  27. package/.agent/skills/ankane-readme-writer/SKILL.md +50 -0
  28. package/.agent/skills/architecture-strategist/SKILL.md +52 -0
  29. package/.agent/skills/best-practices-researcher/SKILL.md +100 -0
  30. package/.agent/skills/bug-reproduction-validator/SKILL.md +67 -0
  31. package/.agent/skills/code-simplicity-reviewer/SKILL.md +85 -0
  32. package/.agent/skills/coding-tutor/.claude-plugin/plugin.json +9 -0
  33. package/.agent/skills/coding-tutor/README.md +37 -0
  34. package/.agent/skills/coding-tutor/commands/quiz-me.md +1 -0
  35. package/.agent/skills/coding-tutor/commands/sync-tutorials.md +25 -0
  36. package/.agent/skills/coding-tutor/commands/teach-me.md +1 -0
  37. package/.agent/skills/coding-tutor/skills/coding-tutor/SKILL.md +214 -0
  38. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/create_tutorial.py +202 -0
  39. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/index_tutorials.py +203 -0
  40. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/quiz_priority.py +190 -0
  41. package/.agent/skills/coding-tutor/skills/coding-tutor/scripts/setup_tutorials.py +132 -0
  42. package/.agent/skills/compound-docs/SKILL.md +510 -0
  43. package/.agent/skills/compound-docs/assets/critical-pattern-template.md +34 -0
  44. package/.agent/skills/compound-docs/assets/resolution-template.md +93 -0
  45. package/.agent/skills/compound-docs/references/yaml-schema.md +65 -0
  46. package/.agent/skills/compound-docs/schema.yaml +176 -0
  47. package/.agent/skills/create-agent-skills/SKILL.md +299 -0
  48. package/.agent/skills/create-agent-skills/references/api-security.md +226 -0
  49. package/.agent/skills/create-agent-skills/references/be-clear-and-direct.md +531 -0
  50. package/.agent/skills/create-agent-skills/references/best-practices.md +404 -0
  51. package/.agent/skills/create-agent-skills/references/common-patterns.md +595 -0
  52. package/.agent/skills/create-agent-skills/references/core-principles.md +437 -0
  53. package/.agent/skills/create-agent-skills/references/executable-code.md +175 -0
  54. package/.agent/skills/create-agent-skills/references/iteration-and-testing.md +474 -0
  55. package/.agent/skills/create-agent-skills/references/official-spec.md +185 -0
  56. package/.agent/skills/create-agent-skills/references/recommended-structure.md +168 -0
  57. package/.agent/skills/create-agent-skills/references/skill-structure.md +372 -0
  58. package/.agent/skills/create-agent-skills/references/using-scripts.md +113 -0
  59. package/.agent/skills/create-agent-skills/references/using-templates.md +112 -0
  60. package/.agent/skills/create-agent-skills/references/workflows-and-validation.md +510 -0
  61. package/.agent/skills/create-agent-skills/templates/router-skill.md +73 -0
  62. package/.agent/skills/create-agent-skills/templates/simple-skill.md +33 -0
  63. package/.agent/skills/create-agent-skills/workflows/add-reference.md +96 -0
  64. package/.agent/skills/create-agent-skills/workflows/add-script.md +93 -0
  65. package/.agent/skills/create-agent-skills/workflows/add-template.md +74 -0
  66. package/.agent/skills/create-agent-skills/workflows/add-workflow.md +120 -0
  67. package/.agent/skills/create-agent-skills/workflows/audit-skill.md +138 -0
  68. package/.agent/skills/create-agent-skills/workflows/create-domain-expertise-skill.md +605 -0
  69. package/.agent/skills/create-agent-skills/workflows/create-new-skill.md +191 -0
  70. package/.agent/skills/create-agent-skills/workflows/get-guidance.md +121 -0
  71. package/.agent/skills/create-agent-skills/workflows/upgrade-to-router.md +161 -0
  72. package/.agent/skills/create-agent-skills/workflows/verify-skill.md +204 -0
  73. package/.agent/skills/data-integrity-guardian/SKILL.md +70 -0
  74. package/.agent/skills/data-migration-expert/SKILL.md +97 -0
  75. package/.agent/skills/deployment-verification-agent/SKILL.md +159 -0
  76. package/.agent/skills/design-implementation-reviewer/SKILL.md +85 -0
  77. package/.agent/skills/design-iterator/SKILL.md +197 -0
  78. package/.agent/skills/dhh-rails-reviewer/SKILL.md +45 -0
  79. package/.agent/skills/dhh-rails-style/SKILL.md +184 -0
  80. package/.agent/skills/dhh-rails-style/references/architecture.md +653 -0
  81. package/.agent/skills/dhh-rails-style/references/controllers.md +303 -0
  82. package/.agent/skills/dhh-rails-style/references/frontend.md +510 -0
  83. package/.agent/skills/dhh-rails-style/references/gems.md +266 -0
  84. package/.agent/skills/dhh-rails-style/references/models.md +359 -0
  85. package/.agent/skills/dhh-rails-style/references/testing.md +338 -0
  86. package/.agent/skills/dspy-ruby/SKILL.md +594 -0
  87. package/.agent/skills/dspy-ruby/assets/config-template.rb +359 -0
  88. package/.agent/skills/dspy-ruby/assets/module-template.rb +326 -0
  89. package/.agent/skills/dspy-ruby/assets/signature-template.rb +143 -0
  90. package/.agent/skills/dspy-ruby/references/core-concepts.md +265 -0
  91. package/.agent/skills/dspy-ruby/references/optimization.md +623 -0
  92. package/.agent/skills/dspy-ruby/references/providers.md +305 -0
  93. package/.agent/skills/every-style-editor/SKILL.md +134 -0
  94. package/.agent/skills/every-style-editor/references/EVERY_WRITE_STYLE.md +529 -0
  95. package/.agent/skills/figma-design-sync/SKILL.md +166 -0
  96. package/.agent/skills/file-todos/SKILL.md +251 -0
  97. package/.agent/skills/file-todos/assets/todo-template.md +155 -0
  98. package/.agent/skills/framework-docs-researcher/SKILL.md +83 -0
  99. package/.agent/skills/frontend-design/SKILL.md +42 -0
  100. package/.agent/skills/gemini-imagegen/SKILL.md +237 -0
  101. package/.agent/skills/gemini-imagegen/requirements.txt +2 -0
  102. package/.agent/skills/gemini-imagegen/scripts/compose_images.py +168 -0
  103. package/.agent/skills/gemini-imagegen/scripts/edit_image.py +157 -0
  104. package/.agent/skills/gemini-imagegen/scripts/gemini_images.py +265 -0
  105. package/.agent/skills/gemini-imagegen/scripts/generate_image.py +147 -0
  106. package/.agent/skills/gemini-imagegen/scripts/multi_turn_chat.py +215 -0
  107. package/.agent/skills/git-history-analyzer/SKILL.md +42 -0
  108. package/.agent/skills/git-worktree/SKILL.md +302 -0
  109. package/.agent/skills/git-worktree/scripts/worktree-manager.sh +345 -0
  110. package/.agent/skills/julik-frontend-races-reviewer/SKILL.md +222 -0
  111. package/.agent/skills/kieran-python-reviewer/SKILL.md +104 -0
  112. package/.agent/skills/kieran-rails-reviewer/SKILL.md +86 -0
  113. package/.agent/skills/kieran-typescript-reviewer/SKILL.md +95 -0
  114. package/.agent/skills/lint/SKILL.md +16 -0
  115. package/.agent/skills/pattern-recognition-specialist/SKILL.md +57 -0
  116. package/.agent/skills/performance-oracle/SKILL.md +110 -0
  117. package/.agent/skills/pr-comment-resolver/SKILL.md +69 -0
  118. package/.agent/skills/rclone/SKILL.md +150 -0
  119. package/.agent/skills/rclone/scripts/check_setup.sh +60 -0
  120. package/.agent/skills/repo-research-analyst/SKILL.md +113 -0
  121. package/.agent/skills/security-sentinel/SKILL.md +93 -0
  122. package/.agent/skills/skill-creator/SKILL.md +209 -0
  123. package/.agent/skills/skill-creator/scripts/init_skill.py +304 -0
  124. package/.agent/skills/skill-creator/scripts/package_skill.py +112 -0
  125. package/.agent/skills/skill-creator/scripts/quick_validate.py +72 -0
  126. package/.agent/skills/spec-flow-analyzer/SKILL.md +113 -0
  127. package/.agent/skills/test-agent/SKILL.md +4 -0
  128. package/.agent/workflows/agent-native-audit.md +277 -0
  129. package/.agent/workflows/ask-user-question.md +21 -0
  130. package/.agent/workflows/changelog.md +137 -0
  131. package/.agent/workflows/compound.md +202 -0
  132. package/.agent/workflows/create-agent-skill.md +8 -0
  133. package/.agent/workflows/deepen-plan-research.md +334 -0
  134. package/.agent/workflows/deepen-plan-synthesis.md +182 -0
  135. package/.agent/workflows/deepen-plan.md +79 -0
  136. package/.agent/workflows/feature-video.md +342 -0
  137. package/.agent/workflows/generate-command.md +162 -0
  138. package/.agent/workflows/heal-skill.md +142 -0
  139. package/.agent/workflows/lfg.md +20 -0
  140. package/.agent/workflows/plan-analysis.md +67 -0
  141. package/.agent/workflows/plan-next-steps.md +63 -0
  142. package/.agent/workflows/plan-review.md +33 -0
  143. package/.agent/workflows/plan-synthesis.md +106 -0
  144. package/.agent/workflows/plan.md +49 -0
  145. package/.agent/workflows/report-bug.md +150 -0
  146. package/.agent/workflows/reproduce-bug.md +99 -0
  147. package/.agent/workflows/resolve-parallel.md +34 -0
  148. package/.agent/workflows/resolve-pr-parallel.md +49 -0
  149. package/.agent/workflows/resolve-todo-parallel.md +35 -0
  150. package/.agent/workflows/review-analysis.md +145 -0
  151. package/.agent/workflows/review-synthesis.md +262 -0
  152. package/.agent/workflows/review.md +64 -0
  153. package/.agent/workflows/ship.md +90 -0
  154. package/.agent/workflows/test-command.md +3 -0
  155. package/.agent/workflows/triage.md +310 -0
  156. package/.agent/workflows/work.md +157 -0
  157. package/.agent/workflows/xcode-test.md +332 -0
  158. package/LICENSE +22 -0
  159. package/README.md +49 -0
  160. package/bin/ag-cortex.js +54 -0
  161. package/lib/core.js +165 -0
  162. package/package.json +31 -0
@@ -0,0 +1,467 @@
1
+ <overview>
2
+ Agent execution patterns for building robust agent loops. This covers how agents signal completion, track partial progress for resume, select appropriate model tiers, and handle context limits.
3
+ </overview>
4
+
5
+ <completion_signals>
6
+ ## Completion Signals
7
+
8
+ Agents need an explicit way to say "I'm done."
9
+
10
+ ### Anti-Pattern: Heuristic Detection
11
+
12
+ Detecting completion through heuristics is fragile:
13
+
14
+ - Consecutive iterations without tool calls
15
+ - Checking for expected output files
16
+ - Tracking "no progress" states
17
+ - Time-based timeouts
18
+
19
+ These break in edge cases and create unpredictable behavior.
20
+
21
+ ### Pattern: Explicit Completion Tool
22
+
23
+ Provide a `complete_task` tool that:
24
+ - Takes a summary of what was accomplished
25
+ - Returns a signal that stops the loop
26
+ - Works identically across all agent types
27
+
28
+ ```typescript
29
+ tool("complete_task", {
30
+ summary: z.string().describe("Summary of what was accomplished"),
31
+ status: z.enum(["success", "partial", "blocked"]).optional(),
32
+ }, async ({ summary, status = "success" }) => {
33
+ return {
34
+ text: summary,
35
+ shouldContinue: false, // Key: signals loop should stop
36
+ };
37
+ });
38
+ ```
39
+
40
+ ### The ToolResult Pattern
41
+
42
+ Structure tool results to separate success from continuation:
43
+
44
+ ```swift
45
+ struct ToolResult {
46
+ let success: Bool // Did tool succeed?
47
+ let output: String // What happened?
48
+ let shouldContinue: Bool // Should agent loop continue?
49
+ }
50
+
51
+ // Three common cases:
52
+ extension ToolResult {
53
+ static func success(_ output: String) -> ToolResult {
54
+ // Tool succeeded, keep going
55
+ ToolResult(success: true, output: output, shouldContinue: true)
56
+ }
57
+
58
+ static func error(_ message: String) -> ToolResult {
59
+ // Tool failed but recoverable, agent can try something else
60
+ ToolResult(success: false, output: message, shouldContinue: true)
61
+ }
62
+
63
+ static func complete(_ summary: String) -> ToolResult {
64
+ // Task done, stop the loop
65
+ ToolResult(success: true, output: summary, shouldContinue: false)
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### Key Insight
71
+
72
+ **This is different from success/failure:**
73
+
74
+ - A tool can **succeed** AND signal **stop** (task complete)
75
+ - A tool can **fail** AND signal **continue** (recoverable error, try something else)
76
+
77
+ ```typescript
78
+ // Examples:
79
+ read_file("/missing.txt")
80
+ // → { success: false, output: "File not found", shouldContinue: true }
81
+ // Agent can try a different file or ask for clarification
82
+
83
+ complete_task("Organized all downloads into folders")
84
+ // → { success: true, output: "...", shouldContinue: false }
85
+ // Agent is done
86
+
87
+ write_file("/output.md", content)
88
+ // → { success: true, output: "Wrote file", shouldContinue: true }
89
+ // Agent keeps working toward the goal
90
+ ```
91
+
92
+ ### System Prompt Guidance
93
+
94
+ Tell the agent when to complete:
95
+
96
+ ```markdown
97
+ ## Completing Tasks
98
+
99
+ When you've accomplished the user's request:
100
+ 1. Verify your work (read back files you created, check results)
101
+ 2. Call `complete_task` with a summary of what you did
102
+ 3. Don't keep working after the goal is achieved
103
+
104
+ If you're blocked and can't proceed:
105
+ - Call `complete_task` with status "blocked" and explain why
106
+ - Don't loop forever trying the same thing
107
+ ```
108
+ </completion_signals>
109
+
110
+ <partial_completion>
111
+ ## Partial Completion
112
+
113
+ For multi-step tasks, track progress at the task level for resume capability.
114
+
115
+ ### Task State Tracking
116
+
117
+ ```swift
118
+ enum TaskStatus {
119
+ case pending // Not yet started
120
+ case inProgress // Currently working on
121
+ case completed // Finished successfully
122
+ case failed // Couldn't complete (with reason)
123
+ case skipped // Intentionally not done
124
+ }
125
+
126
+ struct AgentTask {
127
+ let id: String
128
+ let description: String
129
+ var status: TaskStatus
130
+ var notes: String? // Why it failed, what was done
131
+ }
132
+
133
+ struct AgentSession {
134
+ var tasks: [AgentTask]
135
+
136
+ var isComplete: Bool {
137
+ tasks.allSatisfy { $0.status == .completed || $0.status == .skipped }
138
+ }
139
+
140
+ var progress: (completed: Int, total: Int) {
141
+ let done = tasks.filter { $0.status == .completed }.count
142
+ return (done, tasks.count)
143
+ }
144
+ }
145
+ ```
146
+
147
+ ### UI Progress Display
148
+
149
+ Show users what's happening:
150
+
151
+ ```
152
+ Progress: 3/5 tasks complete (60%)
153
+ ✅ [1] Find source materials
154
+ ✅ [2] Download full text
155
+ ✅ [3] Extract key passages
156
+ ❌ [4] Generate summary - Error: context limit exceeded
157
+ ⏳ [5] Create outline - Pending
158
+ ```
159
+
160
+ ### Partial Completion Scenarios
161
+
162
+ **Agent hits max iterations before finishing:**
163
+ - Some tasks completed, some pending
164
+ - Checkpoint saved with current state
165
+ - Resume continues from where it left off, not from beginning
166
+
167
+ **Agent fails on one task:**
168
+ - Task marked `.failed` with error in notes
169
+ - Other tasks may continue (agent decides)
170
+ - Orchestrator doesn't automatically abort entire session
171
+
172
+ **Network error mid-task:**
173
+ - Current iteration throws
174
+ - Session marked `.failed`
175
+ - Checkpoint preserves messages up to that point
176
+ - Resume possible from checkpoint
177
+
178
+ ### Checkpoint Structure
179
+
180
+ ```swift
181
+ struct AgentCheckpoint: Codable {
182
+ let sessionId: String
183
+ let agentType: String
184
+ let messages: [Message] // Full conversation history
185
+ let iterationCount: Int
186
+ let tasks: [AgentTask] // Task state
187
+ let customState: [String: Any] // Agent-specific state
188
+ let timestamp: Date
189
+
190
+ var isValid: Bool {
191
+ // Checkpoints expire (default 1 hour)
192
+ Date().timeIntervalSince(timestamp) < 3600
193
+ }
194
+ }
195
+ ```
196
+
197
+ ### Resume Flow
198
+
199
+ 1. On app launch, scan for valid checkpoints
200
+ 2. Show user: "You have an incomplete session. Resume?"
201
+ 3. On resume:
202
+ - Restore messages to conversation
203
+ - Restore task states
204
+ - Continue agent loop from where it left off
205
+ 4. On dismiss:
206
+ - Delete checkpoint
207
+ - Start fresh if user tries again
208
+ </partial_completion>
209
+
210
+ <model_tier_selection>
211
+ ## Model Tier Selection
212
+
213
+ Different agents need different intelligence levels. Use the cheapest model that achieves the outcome.
214
+
215
+ ### Tier Guidelines
216
+
217
+ | Agent Type | Recommended Tier | Reasoning |
218
+ |------------|------------------|-----------|
219
+ | Chat/Conversation | Balanced (Sonnet) | Fast responses, good reasoning |
220
+ | Research | Balanced (Sonnet) | Tool loops, not ultra-complex synthesis |
221
+ | Content Generation | Balanced (Sonnet) | Creative but not synthesis-heavy |
222
+ | Complex Analysis | Powerful (Opus) | Multi-document synthesis, nuanced judgment |
223
+ | Profile Generation | Powerful (Opus) | Photo analysis, complex pattern recognition |
224
+ | Quick Queries | Fast (Haiku) | Simple lookups, quick transformations |
225
+ | Simple Classification | Fast (Haiku) | High volume, simple decisions |
226
+
227
+ ### Implementation
228
+
229
+ ```swift
230
+ enum ModelTier {
231
+ case fast // gemini-flash: Quick, cheap, simple tasks
232
+ case balanced // gemini-1.5-pro: Good balance for most tasks
233
+ case powerful // gemini-ultra: Complex reasoning, synthesis
234
+
235
+ var modelId: String {
236
+ switch self {
237
+ case .fast: return "gemini-flash-20240307"
238
+ case .balanced: return "gemini-1.5-pro-4-20250514"
239
+ case .powerful: return "gemini-ultra-4-20250514"
240
+ }
241
+ }
242
+ }
243
+
244
+ struct AgentConfig {
245
+ let name: String
246
+ let modelTier: ModelTier
247
+ let tools: [AgentTool]
248
+ let systemPrompt: String
249
+ let maxIterations: Int
250
+ }
251
+
252
+ // Examples
253
+ let researchConfig = AgentConfig(
254
+ name: "research",
255
+ modelTier: .balanced,
256
+ tools: researchTools,
257
+ systemPrompt: researchPrompt,
258
+ maxIterations: 20
259
+ )
260
+
261
+ let quickLookupConfig = AgentConfig(
262
+ name: "lookup",
263
+ modelTier: .fast,
264
+ tools: [readLibrary],
265
+ systemPrompt: "Answer quick questions about the user's library.",
266
+ maxIterations: 3
267
+ )
268
+ ```
269
+
270
+ ### Cost Optimization Strategies
271
+
272
+ 1. **Start with balanced, upgrade if quality insufficient**
273
+ 2. **Use fast tier for tool-heavy loops** where each turn is simple
274
+ 3. **Reserve powerful tier for synthesis tasks** (comparing multiple sources)
275
+ 4. **Consider token limits per turn** to control costs
276
+ 5. **Cache expensive operations** to avoid repeated calls
277
+ </model_tier_selection>
278
+
279
+ <context_limits>
280
+ ## Context Limits
281
+
282
+ Agent sessions can extend indefinitely, but context windows don't. Design for bounded context from the start.
283
+
284
+ ### The Problem
285
+
286
+ ```
287
+ Turn 1: User asks question → 500 tokens
288
+ Turn 2: Agent reads file → 10,000 tokens
289
+ Turn 3: Agent reads another file → 10,000 tokens
290
+ Turn 4: Agent researches → 20,000 tokens
291
+ ...
292
+ Turn 10: Context window exceeded
293
+ ```
294
+
295
+ ### Design Principles
296
+
297
+ **1. Tools should support iterative refinement**
298
+
299
+ Instead of all-or-nothing, design for summary → detail → full:
300
+
301
+ ```typescript
302
+ // Good: Supports iterative refinement
303
+ tool("read_file", {
304
+ path: z.string(),
305
+ preview: z.boolean().default(true), // Return first 1000 chars by default
306
+ full: z.boolean().default(false), // Opt-in to full content
307
+ }, ...);
308
+
309
+ tool("search_files", {
310
+ query: z.string(),
311
+ summaryOnly: z.boolean().default(true), // Return matches, not full files
312
+ }, ...);
313
+ ```
314
+
315
+ **2. Provide consolidation tools**
316
+
317
+ Give agents a way to consolidate learnings mid-session:
318
+
319
+ ```typescript
320
+ tool("summarize_and_continue", {
321
+ keyPoints: z.array(z.string()),
322
+ nextSteps: z.array(z.string()),
323
+ }, async ({ keyPoints, nextSteps }) => {
324
+ // Store summary, potentially truncate earlier messages
325
+ await saveSessionSummary({ keyPoints, nextSteps });
326
+ return { text: "Summary saved. Continuing with focus on: " + nextSteps.join(", ") };
327
+ });
328
+ ```
329
+
330
+ **3. Design for truncation**
331
+
332
+ Assume the orchestrator may truncate early messages. Important context should be:
333
+ - In the system prompt (always present)
334
+ - In files (can be re-read)
335
+ - Summarized in context.md
336
+
337
+ ### Implementation Strategies
338
+
339
+ ```swift
340
+ class AgentOrchestrator {
341
+ let maxContextTokens = 100_000
342
+ let targetContextTokens = 80_000 // Leave headroom
343
+
344
+ func shouldTruncate() -> Bool {
345
+ estimateTokens(messages) > targetContextTokens
346
+ }
347
+
348
+ func truncateIfNeeded() {
349
+ if shouldTruncate() {
350
+ // Keep system prompt + recent messages
351
+ // Summarize or drop older messages
352
+ messages = [systemMessage] + summarizeOldMessages() + recentMessages
353
+ }
354
+ }
355
+ }
356
+ ```
357
+
358
+ ### System Prompt Guidance
359
+
360
+ ```markdown
361
+ ## Managing Context
362
+
363
+ For long tasks, periodically consolidate what you've learned:
364
+ 1. If you've gathered a lot of information, summarize key points
365
+ 2. Save important findings to files (they persist beyond context)
366
+ 3. Use `summarize_and_continue` if the conversation is getting long
367
+
368
+ Don't try to hold everything in memory. Write it down.
369
+ ```
370
+ </context_limits>
371
+
372
+ <orchestrator_pattern>
373
+ ## Unified Agent Orchestrator
374
+
375
+ One execution engine, many agent types. All agents use the same orchestrator with different configurations.
376
+
377
+ ```swift
378
+ class AgentOrchestrator {
379
+ static let shared = AgentOrchestrator()
380
+
381
+ func run(config: AgentConfig, userMessage: String) async -> AgentResult {
382
+ var messages: [Message] = [
383
+ .system(config.systemPrompt),
384
+ .user(userMessage)
385
+ ]
386
+
387
+ var iteration = 0
388
+
389
+ while iteration < config.maxIterations {
390
+ // Get agent response
391
+ let response = await agent.message(
392
+ model: config.modelTier.modelId,
393
+ messages: messages,
394
+ tools: config.tools
395
+ )
396
+
397
+ messages.append(.assistant(response))
398
+
399
+ // Process tool calls
400
+ for toolCall in response.toolCalls {
401
+ let result = await executeToolCall(toolCall, config: config)
402
+ messages.append(.toolResult(result))
403
+
404
+ // Check for completion signal
405
+ if !result.shouldContinue {
406
+ return AgentResult(
407
+ status: .completed,
408
+ output: result.output,
409
+ iterations: iteration + 1
410
+ )
411
+ }
412
+ }
413
+
414
+ // No tool calls = agent is responding, might be done
415
+ if response.toolCalls.isEmpty {
416
+ // Could be done, or waiting for user
417
+ break
418
+ }
419
+
420
+ iteration += 1
421
+ }
422
+
423
+ return AgentResult(
424
+ status: iteration >= config.maxIterations ? .maxIterations : .responded,
425
+ output: messages.last?.content ?? "",
426
+ iterations: iteration
427
+ )
428
+ }
429
+ }
430
+ ```
431
+
432
+ ### Benefits
433
+
434
+ - Consistent lifecycle management across all agent types
435
+ - Automatic checkpoint/resume (critical for mobile)
436
+ - Shared tool protocol
437
+ - Easy to add new agent types
438
+ - Centralized error handling and logging
439
+ </orchestrator_pattern>
440
+
441
+ <checklist>
442
+ ## Agent Execution Checklist
443
+
444
+ ### Completion Signals
445
+ - [ ] `complete_task` tool provided (explicit completion)
446
+ - [ ] No heuristic completion detection
447
+ - [ ] Tool results include `shouldContinue` flag
448
+ - [ ] System prompt guides when to complete
449
+
450
+ ### Partial Completion
451
+ - [ ] Tasks tracked with status (pending, in_progress, completed, failed)
452
+ - [ ] Checkpoints saved for resume
453
+ - [ ] Progress visible to user
454
+ - [ ] Resume continues from where left off
455
+
456
+ ### Model Tiers
457
+ - [ ] Tier selected based on task complexity
458
+ - [ ] Cost optimization considered
459
+ - [ ] Fast tier for simple operations
460
+ - [ ] Powerful tier reserved for synthesis
461
+
462
+ ### Context Limits
463
+ - [ ] Tools support iterative refinement (preview vs full)
464
+ - [ ] Consolidation mechanism available
465
+ - [ ] Important context persisted to files
466
+ - [ ] Truncation strategy defined
467
+ </checklist>