autoforge-ai 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 (84) hide show
  1. package/.claude/commands/check-code.md +32 -0
  2. package/.claude/commands/checkpoint.md +40 -0
  3. package/.claude/commands/create-spec.md +613 -0
  4. package/.claude/commands/expand-project.md +234 -0
  5. package/.claude/commands/gsd-to-autoforge-spec.md +10 -0
  6. package/.claude/commands/review-pr.md +75 -0
  7. package/.claude/templates/app_spec.template.txt +331 -0
  8. package/.claude/templates/coding_prompt.template.md +265 -0
  9. package/.claude/templates/initializer_prompt.template.md +354 -0
  10. package/.claude/templates/testing_prompt.template.md +146 -0
  11. package/.env.example +64 -0
  12. package/LICENSE.md +676 -0
  13. package/README.md +423 -0
  14. package/agent.py +444 -0
  15. package/api/__init__.py +10 -0
  16. package/api/database.py +536 -0
  17. package/api/dependency_resolver.py +449 -0
  18. package/api/migration.py +156 -0
  19. package/auth.py +83 -0
  20. package/autoforge_paths.py +315 -0
  21. package/autonomous_agent_demo.py +293 -0
  22. package/bin/autoforge.js +3 -0
  23. package/client.py +607 -0
  24. package/env_constants.py +27 -0
  25. package/examples/OPTIMIZE_CONFIG.md +230 -0
  26. package/examples/README.md +531 -0
  27. package/examples/org_config.yaml +172 -0
  28. package/examples/project_allowed_commands.yaml +139 -0
  29. package/lib/cli.js +791 -0
  30. package/mcp_server/__init__.py +1 -0
  31. package/mcp_server/feature_mcp.py +988 -0
  32. package/package.json +53 -0
  33. package/parallel_orchestrator.py +1800 -0
  34. package/progress.py +247 -0
  35. package/prompts.py +427 -0
  36. package/pyproject.toml +17 -0
  37. package/rate_limit_utils.py +132 -0
  38. package/registry.py +614 -0
  39. package/requirements-prod.txt +14 -0
  40. package/security.py +959 -0
  41. package/server/__init__.py +17 -0
  42. package/server/main.py +261 -0
  43. package/server/routers/__init__.py +32 -0
  44. package/server/routers/agent.py +177 -0
  45. package/server/routers/assistant_chat.py +327 -0
  46. package/server/routers/devserver.py +309 -0
  47. package/server/routers/expand_project.py +239 -0
  48. package/server/routers/features.py +746 -0
  49. package/server/routers/filesystem.py +514 -0
  50. package/server/routers/projects.py +524 -0
  51. package/server/routers/schedules.py +356 -0
  52. package/server/routers/settings.py +127 -0
  53. package/server/routers/spec_creation.py +357 -0
  54. package/server/routers/terminal.py +453 -0
  55. package/server/schemas.py +593 -0
  56. package/server/services/__init__.py +36 -0
  57. package/server/services/assistant_chat_session.py +496 -0
  58. package/server/services/assistant_database.py +304 -0
  59. package/server/services/chat_constants.py +57 -0
  60. package/server/services/dev_server_manager.py +557 -0
  61. package/server/services/expand_chat_session.py +399 -0
  62. package/server/services/process_manager.py +657 -0
  63. package/server/services/project_config.py +475 -0
  64. package/server/services/scheduler_service.py +683 -0
  65. package/server/services/spec_chat_session.py +502 -0
  66. package/server/services/terminal_manager.py +756 -0
  67. package/server/utils/__init__.py +1 -0
  68. package/server/utils/process_utils.py +134 -0
  69. package/server/utils/project_helpers.py +32 -0
  70. package/server/utils/validation.py +54 -0
  71. package/server/websocket.py +903 -0
  72. package/start.py +456 -0
  73. package/ui/dist/assets/index-8W_wmZzz.js +168 -0
  74. package/ui/dist/assets/index-B47Ubhox.css +1 -0
  75. package/ui/dist/assets/vendor-flow-CVNK-_lx.js +7 -0
  76. package/ui/dist/assets/vendor-query-BUABzP5o.js +1 -0
  77. package/ui/dist/assets/vendor-radix-DTNNCg2d.js +45 -0
  78. package/ui/dist/assets/vendor-react-qkC6yhPU.js +1 -0
  79. package/ui/dist/assets/vendor-utils-COeKbHgx.js +2 -0
  80. package/ui/dist/assets/vendor-xterm-DP_gxef0.js +16 -0
  81. package/ui/dist/index.html +23 -0
  82. package/ui/dist/ollama.png +0 -0
  83. package/ui/dist/vite.svg +6 -0
  84. package/ui/package.json +57 -0
package/client.py ADDED
@@ -0,0 +1,607 @@
1
+ """
2
+ Claude SDK Client Configuration
3
+ ===============================
4
+
5
+ Functions for creating and configuring the Claude Agent SDK client.
6
+ """
7
+
8
+ import json
9
+ import os
10
+ import re
11
+ import shutil
12
+ import sys
13
+ from pathlib import Path
14
+
15
+ from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
16
+ from claude_agent_sdk.types import HookContext, HookInput, HookMatcher, SyncHookJSONOutput
17
+ from dotenv import load_dotenv
18
+
19
+ from env_constants import API_ENV_VARS
20
+ from security import SENSITIVE_DIRECTORIES, bash_security_hook
21
+
22
+ # Load environment variables from .env file if present
23
+ load_dotenv()
24
+
25
+ # Default Playwright headless mode - can be overridden via PLAYWRIGHT_HEADLESS env var
26
+ # When True, browser runs invisibly in background (default - saves CPU)
27
+ # When False, browser window is visible (useful for monitoring agent progress)
28
+ DEFAULT_PLAYWRIGHT_HEADLESS = True
29
+
30
+ # Default browser for Playwright - can be overridden via PLAYWRIGHT_BROWSER env var
31
+ # Options: chrome, firefox, webkit, msedge
32
+ # Firefox is recommended for lower CPU usage
33
+ DEFAULT_PLAYWRIGHT_BROWSER = "firefox"
34
+
35
+ # Extra read paths for cross-project file access (read-only)
36
+ # Set EXTRA_READ_PATHS environment variable with comma-separated absolute paths
37
+ # Example: EXTRA_READ_PATHS=/Volumes/Data/dev,/Users/shared/libs
38
+ EXTRA_READ_PATHS_VAR = "EXTRA_READ_PATHS"
39
+
40
+ # Sensitive directories that should never be allowed via EXTRA_READ_PATHS.
41
+ # Delegates to the canonical SENSITIVE_DIRECTORIES set in security.py so that
42
+ # this blocklist and the filesystem browser API share a single source of truth.
43
+ EXTRA_READ_PATHS_BLOCKLIST = SENSITIVE_DIRECTORIES
44
+
45
+ def convert_model_for_vertex(model: str) -> str:
46
+ """
47
+ Convert model name format for Vertex AI compatibility.
48
+
49
+ Vertex AI uses @ to separate model name from version (e.g., claude-opus-4-5@20251101)
50
+ while the Anthropic API uses - (e.g., claude-opus-4-5-20251101).
51
+
52
+ Args:
53
+ model: Model name in Anthropic format (with hyphens)
54
+
55
+ Returns:
56
+ Model name in Vertex AI format (with @ before date) if Vertex AI is enabled,
57
+ otherwise returns the model unchanged.
58
+ """
59
+ # Only convert if Vertex AI is enabled
60
+ if os.getenv("CLAUDE_CODE_USE_VERTEX") != "1":
61
+ return model
62
+
63
+ # Pattern: claude-{name}-{version}-{date} -> claude-{name}-{version}@{date}
64
+ # Example: claude-opus-4-5-20251101 -> claude-opus-4-5@20251101
65
+ # The date is always 8 digits at the end
66
+ match = re.match(r'^(claude-.+)-(\d{8})$', model)
67
+ if match:
68
+ base_name, date = match.groups()
69
+ return f"{base_name}@{date}"
70
+
71
+ # If already in @ format or doesn't match expected pattern, return as-is
72
+ return model
73
+
74
+
75
+ def get_playwright_headless() -> bool:
76
+ """
77
+ Get the Playwright headless mode setting.
78
+
79
+ Reads from PLAYWRIGHT_HEADLESS environment variable, defaults to True.
80
+ Returns True for headless mode (invisible browser), False for visible browser.
81
+ """
82
+ value = os.getenv("PLAYWRIGHT_HEADLESS", str(DEFAULT_PLAYWRIGHT_HEADLESS).lower()).strip().lower()
83
+ truthy = {"true", "1", "yes", "on"}
84
+ falsy = {"false", "0", "no", "off"}
85
+ if value not in truthy | falsy:
86
+ print(f" - Warning: Invalid PLAYWRIGHT_HEADLESS='{value}', defaulting to {DEFAULT_PLAYWRIGHT_HEADLESS}")
87
+ return DEFAULT_PLAYWRIGHT_HEADLESS
88
+ return value in truthy
89
+
90
+
91
+ # Valid browsers supported by Playwright MCP
92
+ VALID_PLAYWRIGHT_BROWSERS = {"chrome", "firefox", "webkit", "msedge"}
93
+
94
+
95
+ def get_playwright_browser() -> str:
96
+ """
97
+ Get the browser to use for Playwright.
98
+
99
+ Reads from PLAYWRIGHT_BROWSER environment variable, defaults to firefox.
100
+ Options: chrome, firefox, webkit, msedge
101
+ Firefox is recommended for lower CPU usage.
102
+ """
103
+ value = os.getenv("PLAYWRIGHT_BROWSER", DEFAULT_PLAYWRIGHT_BROWSER).strip().lower()
104
+ if value not in VALID_PLAYWRIGHT_BROWSERS:
105
+ print(f" - Warning: Invalid PLAYWRIGHT_BROWSER='{value}', "
106
+ f"valid options: {', '.join(sorted(VALID_PLAYWRIGHT_BROWSERS))}. "
107
+ f"Defaulting to {DEFAULT_PLAYWRIGHT_BROWSER}")
108
+ return DEFAULT_PLAYWRIGHT_BROWSER
109
+ return value
110
+
111
+
112
+ def get_extra_read_paths() -> list[Path]:
113
+ """
114
+ Get extra read-only paths from EXTRA_READ_PATHS environment variable.
115
+
116
+ Parses comma-separated absolute paths and validates each one:
117
+ - Must be an absolute path
118
+ - Must exist and be a directory
119
+ - Cannot be or contain sensitive directories (e.g., .ssh, .aws)
120
+
121
+ Returns:
122
+ List of validated, canonicalized Path objects.
123
+ """
124
+ raw_value = os.getenv(EXTRA_READ_PATHS_VAR, "").strip()
125
+ if not raw_value:
126
+ return []
127
+
128
+ validated_paths: list[Path] = []
129
+ home_dir = Path.home()
130
+
131
+ for path_str in raw_value.split(","):
132
+ path_str = path_str.strip()
133
+ if not path_str:
134
+ continue
135
+
136
+ # Parse and canonicalize the path
137
+ try:
138
+ path = Path(path_str).resolve()
139
+ except (OSError, ValueError) as e:
140
+ print(f" - Warning: Invalid EXTRA_READ_PATHS path '{path_str}': {e}")
141
+ continue
142
+
143
+ # Must be absolute (resolve() makes it absolute, but check original input)
144
+ if not Path(path_str).is_absolute():
145
+ print(f" - Warning: EXTRA_READ_PATHS requires absolute paths, skipping: {path_str}")
146
+ continue
147
+
148
+ # Must exist
149
+ if not path.exists():
150
+ print(f" - Warning: EXTRA_READ_PATHS path does not exist, skipping: {path_str}")
151
+ continue
152
+
153
+ # Must be a directory
154
+ if not path.is_dir():
155
+ print(f" - Warning: EXTRA_READ_PATHS path is not a directory, skipping: {path_str}")
156
+ continue
157
+
158
+ # Check against sensitive directory blocklist
159
+ is_blocked = False
160
+ for sensitive in EXTRA_READ_PATHS_BLOCKLIST:
161
+ sensitive_path = (home_dir / sensitive).resolve()
162
+ try:
163
+ # Block if path IS the sensitive dir or is INSIDE it
164
+ if path == sensitive_path or path.is_relative_to(sensitive_path):
165
+ print(f" - Warning: EXTRA_READ_PATHS blocked sensitive path: {path_str}")
166
+ is_blocked = True
167
+ break
168
+ # Also block if sensitive dir is INSIDE the requested path
169
+ if sensitive_path.is_relative_to(path):
170
+ print(f" - Warning: EXTRA_READ_PATHS path contains sensitive directory ({sensitive}): {path_str}")
171
+ is_blocked = True
172
+ break
173
+ except (OSError, ValueError):
174
+ # is_relative_to can raise on some edge cases
175
+ continue
176
+
177
+ if is_blocked:
178
+ continue
179
+
180
+ validated_paths.append(path)
181
+
182
+ return validated_paths
183
+
184
+
185
+ # Per-agent-type MCP tool lists.
186
+ # Only expose the tools each agent type actually needs, reducing tool schema
187
+ # overhead and preventing agents from calling tools meant for other roles.
188
+ #
189
+ # Tools intentionally omitted from ALL agent lists (UI/orchestrator only):
190
+ # feature_get_ready, feature_get_blocked, feature_get_graph,
191
+ # feature_remove_dependency
192
+ #
193
+ # The ghost tool "feature_release_testing" was removed entirely -- it was
194
+ # listed here but never implemented in mcp_server/feature_mcp.py.
195
+
196
+ CODING_AGENT_TOOLS = [
197
+ "mcp__features__feature_get_stats",
198
+ "mcp__features__feature_get_by_id",
199
+ "mcp__features__feature_get_summary",
200
+ "mcp__features__feature_claim_and_get",
201
+ "mcp__features__feature_mark_in_progress",
202
+ "mcp__features__feature_mark_passing",
203
+ "mcp__features__feature_mark_failing",
204
+ "mcp__features__feature_skip",
205
+ "mcp__features__feature_clear_in_progress",
206
+ ]
207
+
208
+ TESTING_AGENT_TOOLS = [
209
+ "mcp__features__feature_get_stats",
210
+ "mcp__features__feature_get_by_id",
211
+ "mcp__features__feature_get_summary",
212
+ "mcp__features__feature_mark_passing",
213
+ "mcp__features__feature_mark_failing",
214
+ ]
215
+
216
+ INITIALIZER_AGENT_TOOLS = [
217
+ "mcp__features__feature_get_stats",
218
+ "mcp__features__feature_create_bulk",
219
+ "mcp__features__feature_create",
220
+ "mcp__features__feature_add_dependency",
221
+ "mcp__features__feature_set_dependencies",
222
+ ]
223
+
224
+ # Union of all agent tool lists -- used for permissions (all tools remain
225
+ # *permitted* so the MCP server can respond, but only the agent-type-specific
226
+ # list is included in allowed_tools, which controls what the LLM sees).
227
+ ALL_FEATURE_MCP_TOOLS = sorted(
228
+ set(CODING_AGENT_TOOLS) | set(TESTING_AGENT_TOOLS) | set(INITIALIZER_AGENT_TOOLS)
229
+ )
230
+
231
+ # Playwright MCP tools for browser automation.
232
+ # Full set of tools for comprehensive UI testing including drag-and-drop,
233
+ # hover menus, file uploads, tab management, etc.
234
+ PLAYWRIGHT_TOOLS = [
235
+ # Core navigation & screenshots
236
+ "mcp__playwright__browser_navigate",
237
+ "mcp__playwright__browser_navigate_back",
238
+ "mcp__playwright__browser_take_screenshot",
239
+ "mcp__playwright__browser_snapshot",
240
+
241
+ # Element interaction
242
+ "mcp__playwright__browser_click",
243
+ "mcp__playwright__browser_type",
244
+ "mcp__playwright__browser_fill_form",
245
+ "mcp__playwright__browser_select_option",
246
+ "mcp__playwright__browser_press_key",
247
+ "mcp__playwright__browser_drag",
248
+ "mcp__playwright__browser_hover",
249
+ "mcp__playwright__browser_file_upload",
250
+
251
+ # JavaScript & debugging
252
+ "mcp__playwright__browser_evaluate",
253
+ # "mcp__playwright__browser_run_code", # REMOVED - causes Playwright MCP server crash
254
+ "mcp__playwright__browser_console_messages",
255
+ "mcp__playwright__browser_network_requests",
256
+
257
+ # Browser management
258
+ "mcp__playwright__browser_resize",
259
+ "mcp__playwright__browser_wait_for",
260
+ "mcp__playwright__browser_handle_dialog",
261
+ "mcp__playwright__browser_install",
262
+ "mcp__playwright__browser_close",
263
+ "mcp__playwright__browser_tabs",
264
+ ]
265
+
266
+ # Built-in tools available to agents.
267
+ # WebFetch and WebSearch are included so coding agents can look up current
268
+ # documentation for frameworks and libraries they are implementing.
269
+ BUILTIN_TOOLS = [
270
+ "Read",
271
+ "Write",
272
+ "Edit",
273
+ "Glob",
274
+ "Grep",
275
+ "Bash",
276
+ "WebFetch",
277
+ "WebSearch",
278
+ ]
279
+
280
+
281
+ def create_client(
282
+ project_dir: Path,
283
+ model: str,
284
+ yolo_mode: bool = False,
285
+ agent_id: str | None = None,
286
+ agent_type: str = "coding",
287
+ ):
288
+ """
289
+ Create a Claude Agent SDK client with multi-layered security.
290
+
291
+ Args:
292
+ project_dir: Directory for the project
293
+ model: Claude model to use
294
+ yolo_mode: If True, skip Playwright MCP server for rapid prototyping
295
+ agent_id: Optional unique identifier for browser isolation in parallel mode.
296
+ When provided, each agent gets its own browser profile.
297
+ agent_type: One of "coding", "testing", or "initializer". Controls which
298
+ MCP tools are exposed and the max_turns limit.
299
+
300
+ Returns:
301
+ Configured ClaudeSDKClient (from claude_agent_sdk)
302
+
303
+ Security layers (defense in depth):
304
+ 1. Sandbox - OS-level bash command isolation prevents filesystem escape
305
+ 2. Permissions - File operations restricted to project_dir only
306
+ 3. Security hooks - Bash commands validated against an allowlist
307
+ (see security.py for ALLOWED_COMMANDS)
308
+
309
+ Note: Authentication is handled by start.bat/start.sh before this runs.
310
+ The Claude SDK auto-detects credentials from the Claude CLI configuration
311
+ """
312
+ # Select the feature MCP tools appropriate for this agent type
313
+ feature_tools_map = {
314
+ "coding": CODING_AGENT_TOOLS,
315
+ "testing": TESTING_AGENT_TOOLS,
316
+ "initializer": INITIALIZER_AGENT_TOOLS,
317
+ }
318
+ feature_tools = feature_tools_map.get(agent_type, CODING_AGENT_TOOLS)
319
+
320
+ # Select max_turns based on agent type:
321
+ # - coding/initializer: 300 turns (complex multi-step implementation)
322
+ # - testing: 100 turns (focused verification of a single feature)
323
+ max_turns_map = {
324
+ "coding": 300,
325
+ "testing": 100,
326
+ "initializer": 300,
327
+ }
328
+ max_turns = max_turns_map.get(agent_type, 300)
329
+
330
+ # Build allowed tools list based on mode and agent type.
331
+ # In YOLO mode, exclude Playwright tools for faster prototyping.
332
+ allowed_tools = [*BUILTIN_TOOLS, *feature_tools]
333
+ if not yolo_mode:
334
+ allowed_tools.extend(PLAYWRIGHT_TOOLS)
335
+
336
+ # Build permissions list.
337
+ # We permit ALL feature MCP tools at the security layer (so the MCP server
338
+ # can respond if called), but the LLM only *sees* the agent-type-specific
339
+ # subset via allowed_tools above.
340
+ permissions_list = [
341
+ # Allow all file operations within the project directory
342
+ "Read(./**)",
343
+ "Write(./**)",
344
+ "Edit(./**)",
345
+ "Glob(./**)",
346
+ "Grep(./**)",
347
+ # Bash permission granted here, but actual commands are validated
348
+ # by the bash_security_hook (see security.py for allowed commands)
349
+ "Bash(*)",
350
+ # Allow web tools for looking up framework/library documentation
351
+ "WebFetch(*)",
352
+ "WebSearch(*)",
353
+ # Allow Feature MCP tools for feature management
354
+ *ALL_FEATURE_MCP_TOOLS,
355
+ ]
356
+
357
+ # Add extra read paths from environment variable (read-only access)
358
+ # Paths are validated, canonicalized, and checked against sensitive blocklist
359
+ extra_read_paths = get_extra_read_paths()
360
+ for path in extra_read_paths:
361
+ # Add read-only permissions for each validated path
362
+ permissions_list.append(f"Read({path}/**)")
363
+ permissions_list.append(f"Glob({path}/**)")
364
+ permissions_list.append(f"Grep({path}/**)")
365
+
366
+ if not yolo_mode:
367
+ # Allow Playwright MCP tools for browser automation (standard mode only)
368
+ permissions_list.extend(PLAYWRIGHT_TOOLS)
369
+
370
+ # Create comprehensive security settings
371
+ # Note: Using relative paths ("./**") restricts access to project directory
372
+ # since cwd is set to project_dir
373
+ security_settings = {
374
+ "sandbox": {"enabled": True, "autoAllowBashIfSandboxed": True},
375
+ "permissions": {
376
+ "defaultMode": "acceptEdits", # Auto-approve edits within allowed directories
377
+ "allow": permissions_list,
378
+ },
379
+ }
380
+
381
+ # Ensure project directory exists before creating settings file
382
+ project_dir.mkdir(parents=True, exist_ok=True)
383
+
384
+ # Write settings to a file in the project directory
385
+ from autoforge_paths import get_claude_settings_path
386
+ settings_file = get_claude_settings_path(project_dir)
387
+ settings_file.parent.mkdir(parents=True, exist_ok=True)
388
+ with open(settings_file, "w") as f:
389
+ json.dump(security_settings, f, indent=2)
390
+
391
+ print(f"Created security settings at {settings_file}")
392
+ print(" - Sandbox enabled (OS-level bash isolation)")
393
+ print(f" - Filesystem restricted to: {project_dir.resolve()}")
394
+ if extra_read_paths:
395
+ print(f" - Extra read paths (validated): {', '.join(str(p) for p in extra_read_paths)}")
396
+ print(" - Bash commands restricted to allowlist (see security.py)")
397
+ if yolo_mode:
398
+ print(" - MCP servers: features (database) - YOLO MODE (no Playwright)")
399
+ else:
400
+ print(" - MCP servers: playwright (browser), features (database)")
401
+ print(" - Project settings enabled (skills, commands, CLAUDE.md)")
402
+ print()
403
+
404
+ # Use system Claude CLI instead of bundled one (avoids Bun runtime crash on Windows)
405
+ system_cli = shutil.which("claude")
406
+ if system_cli:
407
+ print(f" - Using system CLI: {system_cli}")
408
+ else:
409
+ print(" - Warning: System 'claude' CLI not found, using bundled CLI")
410
+
411
+ # Build MCP servers config - features is always included, playwright only in standard mode
412
+ mcp_servers = {
413
+ "features": {
414
+ "command": sys.executable, # Use the same Python that's running this script
415
+ "args": ["-m", "mcp_server.feature_mcp"],
416
+ "env": {
417
+ # Only specify variables the MCP server needs
418
+ # (subprocess inherits parent environment automatically)
419
+ "PROJECT_DIR": str(project_dir.resolve()),
420
+ "PYTHONPATH": str(Path(__file__).parent.resolve()),
421
+ },
422
+ },
423
+ }
424
+ if not yolo_mode:
425
+ # Include Playwright MCP server for browser automation (standard mode only)
426
+ # Browser and headless mode configurable via environment variables
427
+ browser = get_playwright_browser()
428
+ playwright_args = [
429
+ "@playwright/mcp@latest",
430
+ "--viewport-size", "1280x720",
431
+ "--browser", browser,
432
+ ]
433
+ if get_playwright_headless():
434
+ playwright_args.append("--headless")
435
+ print(f" - Browser: {browser} (headless={get_playwright_headless()})")
436
+
437
+ # Browser isolation for parallel execution
438
+ # Each agent gets its own isolated browser context to prevent tab conflicts
439
+ if agent_id:
440
+ # Use --isolated for ephemeral browser context
441
+ # This creates a fresh, isolated context without persistent state
442
+ # Note: --isolated and --user-data-dir are mutually exclusive
443
+ playwright_args.append("--isolated")
444
+ print(f" - Browser isolation enabled for agent: {agent_id}")
445
+
446
+ mcp_servers["playwright"] = {
447
+ "command": "npx",
448
+ "args": playwright_args,
449
+ }
450
+
451
+ # Build environment overrides for API endpoint configuration
452
+ # These override system env vars for the Claude CLI subprocess,
453
+ # allowing AutoForge to use alternative APIs (e.g., GLM) without
454
+ # affecting the user's global Claude Code settings
455
+ sdk_env = {}
456
+ for var in API_ENV_VARS:
457
+ value = os.getenv(var)
458
+ if value:
459
+ sdk_env[var] = value
460
+
461
+ # Detect alternative API mode (Ollama, GLM, or Vertex AI)
462
+ base_url = sdk_env.get("ANTHROPIC_BASE_URL", "")
463
+ is_vertex = sdk_env.get("CLAUDE_CODE_USE_VERTEX") == "1"
464
+ is_alternative_api = bool(base_url) or is_vertex
465
+ is_ollama = "localhost:11434" in base_url or "127.0.0.1:11434" in base_url
466
+ model = convert_model_for_vertex(model)
467
+ if sdk_env:
468
+ print(f" - API overrides: {', '.join(sdk_env.keys())}")
469
+ if is_vertex:
470
+ project_id = sdk_env.get("ANTHROPIC_VERTEX_PROJECT_ID", "unknown")
471
+ region = sdk_env.get("CLOUD_ML_REGION", "unknown")
472
+ print(f" - Vertex AI Mode: Using GCP project '{project_id}' with model '{model}' in region '{region}'")
473
+ elif is_ollama:
474
+ print(" - Ollama Mode: Using local models")
475
+ elif "ANTHROPIC_BASE_URL" in sdk_env:
476
+ print(f" - GLM Mode: Using {sdk_env['ANTHROPIC_BASE_URL']}")
477
+
478
+ # Create a wrapper for bash_security_hook that passes project_dir via context
479
+ async def bash_hook_with_context(input_data, tool_use_id=None, context=None):
480
+ """Wrapper that injects project_dir into context for security hook."""
481
+ if context is None:
482
+ context = {}
483
+ context["project_dir"] = str(project_dir.resolve())
484
+ return await bash_security_hook(input_data, tool_use_id, context)
485
+
486
+ # PreCompact hook for logging and customizing context compaction.
487
+ # Compaction is handled automatically by Claude Code CLI when context approaches limits.
488
+ # This hook provides custom instructions that guide the summarizer to preserve
489
+ # critical workflow state while discarding verbose/redundant content.
490
+ async def pre_compact_hook(
491
+ input_data: HookInput,
492
+ tool_use_id: str | None,
493
+ context: HookContext,
494
+ ) -> SyncHookJSONOutput:
495
+ """
496
+ Hook called before context compaction occurs.
497
+
498
+ Compaction triggers:
499
+ - "auto": Automatic compaction when context approaches token limits
500
+ - "manual": User-initiated compaction via /compact command
501
+
502
+ Returns custom instructions that guide the compaction summarizer to:
503
+ 1. Preserve critical workflow state (feature ID, modified files, test results)
504
+ 2. Discard verbose content (screenshots, long grep outputs, repeated reads)
505
+ """
506
+ trigger = input_data.get("trigger", "auto")
507
+ custom_instructions = input_data.get("custom_instructions")
508
+
509
+ if trigger == "auto":
510
+ print("[Context] Auto-compaction triggered (context approaching limit)")
511
+ else:
512
+ print("[Context] Manual compaction requested")
513
+
514
+ if custom_instructions:
515
+ print(f"[Context] Custom instructions provided: {custom_instructions}")
516
+
517
+ # Build compaction instructions that preserve workflow-critical context
518
+ # while discarding verbose content that inflates token usage.
519
+ #
520
+ # The summarizer receives these instructions and uses them to decide
521
+ # what to keep vs. discard during context compaction.
522
+ compaction_guidance = "\n".join([
523
+ "## PRESERVE (critical workflow state)",
524
+ "- Current feature ID, feature name, and feature status (pending/in_progress/passing/failing)",
525
+ "- List of all files created or modified during this session, with their paths",
526
+ "- Last test/lint/type-check results: command run, pass/fail status, and key error messages",
527
+ "- Current step in the workflow (e.g., implementing, testing, fixing lint errors)",
528
+ "- Any dependency information (which features block this one)",
529
+ "- Git operations performed (commits, branches created)",
530
+ "- MCP tool call results (feature_claim_and_get, feature_mark_passing, etc.)",
531
+ "- Key architectural decisions made during this session",
532
+ "",
533
+ "## DISCARD (verbose content safe to drop)",
534
+ "- Full screenshot base64 data (just note that a screenshot was taken and what it showed)",
535
+ "- Long grep/find/glob output listings (summarize to: searched for X, found Y relevant files)",
536
+ "- Repeated file reads of the same file (keep only the latest read or a summary of changes)",
537
+ "- Full file contents from Read tool (summarize to: read file X, key sections were Y)",
538
+ "- Verbose npm/pip install output (just note: dependencies installed successfully/failed)",
539
+ "- Full lint/type-check output when passing (just note: lint passed with no errors)",
540
+ "- Browser console message dumps (summarize to: N errors found, key error was X)",
541
+ "- Redundant tool result confirmations ([Done] markers)",
542
+ ])
543
+
544
+ print("[Context] Applying custom compaction instructions (preserve workflow state, discard verbose content)")
545
+
546
+ # The SDK's HookSpecificOutput union type does not yet include a
547
+ # PreCompactHookSpecificOutput variant, but the CLI protocol accepts
548
+ # {"hookEventName": "PreCompact", "customInstructions": "..."}.
549
+ # The dict is serialized to JSON and sent to the CLI process directly,
550
+ # so the runtime behavior is correct despite the type mismatch.
551
+ return SyncHookJSONOutput(
552
+ hookSpecificOutput={ # type: ignore[typeddict-item]
553
+ "hookEventName": "PreCompact",
554
+ "customInstructions": compaction_guidance,
555
+ }
556
+ )
557
+
558
+ # PROMPT CACHING: The Claude Code CLI applies cache_control breakpoints internally.
559
+ # Our system_prompt benefits from automatic caching without explicit configuration.
560
+ # If explicit cache_control is needed, the SDK would need to accept content blocks
561
+ # with cache_control fields (not currently supported in v0.1.x).
562
+ return ClaudeSDKClient(
563
+ options=ClaudeAgentOptions(
564
+ model=model,
565
+ cli_path=system_cli, # Use system CLI to avoid bundled Bun crash (exit code 3)
566
+ system_prompt="You are an expert full-stack developer building a production-quality web application.",
567
+ setting_sources=["project"], # Enable skills, commands, and CLAUDE.md from project dir
568
+ max_buffer_size=10 * 1024 * 1024, # 10MB for large Playwright screenshots
569
+ allowed_tools=allowed_tools,
570
+ mcp_servers=mcp_servers, # type: ignore[arg-type] # SDK accepts dict config at runtime
571
+ hooks={
572
+ "PreToolUse": [
573
+ HookMatcher(matcher="Bash", hooks=[bash_hook_with_context]),
574
+ ],
575
+ # PreCompact hook for context management during long sessions.
576
+ # Compaction is automatic when context approaches token limits.
577
+ # This hook logs compaction events and can customize summarization.
578
+ "PreCompact": [
579
+ HookMatcher(hooks=[pre_compact_hook]),
580
+ ],
581
+ },
582
+ max_turns=max_turns,
583
+ cwd=str(project_dir.resolve()),
584
+ settings=str(settings_file.resolve()), # Use absolute path
585
+ env=sdk_env, # Pass API configuration overrides to CLI subprocess
586
+ # Enable extended context beta for better handling of long sessions.
587
+ # This provides up to 1M tokens of context with automatic compaction.
588
+ # See: https://docs.anthropic.com/en/api/beta-headers
589
+ # Disabled for alternative APIs (Ollama, GLM, Vertex AI) as they don't support this beta.
590
+ betas=[] if is_alternative_api else ["context-1m-2025-08-07"],
591
+ # Note on context management:
592
+ # The Claude Agent SDK handles context management automatically through the
593
+ # underlying Claude Code CLI. When context approaches limits, the CLI
594
+ # automatically compacts/summarizes previous messages.
595
+ #
596
+ # The SDK does NOT expose explicit compaction_control or context_management
597
+ # parameters. Instead, context is managed via:
598
+ # 1. betas=["context-1m-2025-08-07"] - Extended context window
599
+ # 2. PreCompact hook - Intercept and customize compaction behavior
600
+ # 3. max_turns - Limit conversation turns (per agent type: coding=300, testing=100)
601
+ #
602
+ # Future SDK versions may add explicit compaction controls. When available,
603
+ # consider adding:
604
+ # - compaction_control={"enabled": True, "context_token_threshold": 80000}
605
+ # - context_management={"edits": [...]} for tool use clearing
606
+ )
607
+ )
@@ -0,0 +1,27 @@
1
+ """
2
+ Shared Environment Variable Constants
3
+ ======================================
4
+
5
+ Single source of truth for environment variables forwarded to Claude CLI
6
+ subprocesses. Imported by both ``client.py`` (agent sessions) and
7
+ ``server/services/chat_constants.py`` (chat sessions) to avoid maintaining
8
+ duplicate lists.
9
+
10
+ These allow autoforge to use alternative API endpoints (Ollama, GLM,
11
+ Vertex AI) without affecting the user's global Claude Code settings.
12
+ """
13
+
14
+ API_ENV_VARS: list[str] = [
15
+ # Core API configuration
16
+ "ANTHROPIC_BASE_URL", # Custom API endpoint (e.g., https://api.z.ai/api/anthropic)
17
+ "ANTHROPIC_AUTH_TOKEN", # API authentication token
18
+ "API_TIMEOUT_MS", # Request timeout in milliseconds
19
+ # Model tier overrides
20
+ "ANTHROPIC_DEFAULT_SONNET_MODEL", # Model override for Sonnet
21
+ "ANTHROPIC_DEFAULT_OPUS_MODEL", # Model override for Opus
22
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL", # Model override for Haiku
23
+ # Vertex AI configuration
24
+ "CLAUDE_CODE_USE_VERTEX", # Enable Vertex AI mode (set to "1")
25
+ "CLOUD_ML_REGION", # GCP region (e.g., us-east5)
26
+ "ANTHROPIC_VERTEX_PROJECT_ID", # GCP project ID
27
+ ]