@posthog/agent 1.29.0 → 2.0.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.
@@ -1,4 +1,4 @@
1
- import { exec, execFile } from "node:child_process";
1
+ import { execFile } from "node:child_process";
2
2
  import * as crypto from "node:crypto";
3
3
  import * as fs from "node:fs/promises";
4
4
  import * as path from "node:path";
@@ -6,7 +6,6 @@ import { promisify } from "node:util";
6
6
  import type { WorktreeInfo } from "./types.js";
7
7
  import { Logger } from "./utils/logger.js";
8
8
 
9
- const execAsync = promisify(exec);
10
9
  const execFileAsync = promisify(execFile);
11
10
 
12
11
  export interface WorktreeConfig {
@@ -512,14 +511,14 @@ export class WorktreeManager {
512
511
  return this.worktreeBasePath !== null;
513
512
  }
514
513
 
515
- private async runGitCommand(command: string): Promise<string> {
514
+ private async runGitCommand(args: string[]): Promise<string> {
516
515
  try {
517
- const { stdout } = await execAsync(`git ${command}`, {
516
+ const { stdout } = await execFileAsync("git", args, {
518
517
  cwd: this.mainRepoPath,
519
518
  });
520
519
  return stdout.trim();
521
520
  } catch (error) {
522
- throw new Error(`Git command failed: ${command}\n${error}`);
521
+ throw new Error(`Git command failed: git ${args.join(" ")}\n${error}`);
523
522
  }
524
523
  }
525
524
 
@@ -605,48 +604,68 @@ export class WorktreeManager {
605
604
  }
606
605
 
607
606
  private async getDefaultBranch(): Promise<string> {
608
- try {
609
- const remoteBranch = await this.runGitCommand(
610
- "symbolic-ref refs/remotes/origin/HEAD",
611
- );
612
- return remoteBranch.replace("refs/remotes/origin/", "");
613
- } catch {
614
- // Fallback: check if main exists, otherwise use master
615
- try {
616
- await this.runGitCommand("rev-parse --verify main");
617
- return "main";
618
- } catch {
619
- try {
620
- await this.runGitCommand("rev-parse --verify master");
621
- return "master";
622
- } catch {
623
- throw new Error(
624
- "Cannot determine default branch. No main or master branch found.",
625
- );
626
- }
627
- }
607
+ // Try all methods in parallel for speed
608
+ const [symbolicRef, mainExists, masterExists] = await Promise.allSettled([
609
+ this.runGitCommand(["symbolic-ref", "refs/remotes/origin/HEAD"]),
610
+ this.runGitCommand(["rev-parse", "--verify", "main"]),
611
+ this.runGitCommand(["rev-parse", "--verify", "master"]),
612
+ ]);
613
+
614
+ // Prefer symbolic ref (most accurate)
615
+ if (symbolicRef.status === "fulfilled") {
616
+ return symbolicRef.value.replace("refs/remotes/origin/", "");
628
617
  }
618
+
619
+ // Fallback to main if it exists
620
+ if (mainExists.status === "fulfilled") {
621
+ return "main";
622
+ }
623
+
624
+ // Fallback to master if it exists
625
+ if (masterExists.status === "fulfilled") {
626
+ return "master";
627
+ }
628
+
629
+ throw new Error(
630
+ "Cannot determine default branch. No main or master branch found.",
631
+ );
629
632
  }
630
633
 
631
634
  async createWorktree(options?: {
632
635
  baseBranch?: string;
633
636
  }): Promise<WorktreeInfo> {
637
+ const totalStart = Date.now();
638
+
639
+ // Run setup tasks in parallel for speed
640
+ const setupPromises: Promise<unknown>[] = [];
641
+
634
642
  // Only modify .git/info/exclude when using in-repo storage
635
643
  if (!this.usesExternalPath()) {
636
- await this.ensureArrayDirIgnored();
637
- }
638
-
639
- // Ensure the worktree folder exists when using external path
640
- if (this.usesExternalPath()) {
644
+ setupPromises.push(this.ensureArrayDirIgnored());
645
+ } else {
646
+ // Ensure the worktree folder exists when using external path
641
647
  const folderPath = this.getWorktreeFolderPath();
642
- await fs.mkdir(folderPath, { recursive: true });
648
+ setupPromises.push(fs.mkdir(folderPath, { recursive: true }));
643
649
  }
644
650
 
645
- // Generate unique worktree name
646
- const worktreeName = await this.generateUniqueWorktreeName();
651
+ // Generate unique worktree name (in parallel with above)
652
+ const worktreeNamePromise = this.generateUniqueWorktreeName();
653
+ setupPromises.push(worktreeNamePromise);
654
+
655
+ // Get default branch in parallel if not provided
656
+ const baseBranchPromise = options?.baseBranch
657
+ ? Promise.resolve(options.baseBranch)
658
+ : this.getDefaultBranch();
659
+ setupPromises.push(baseBranchPromise);
660
+
661
+ // Wait for all setup to complete
662
+ await Promise.all(setupPromises);
663
+ const setupTime = Date.now() - totalStart;
664
+
665
+ const worktreeName = await worktreeNamePromise;
666
+ const baseBranch = await baseBranchPromise;
647
667
  const worktreePath = this.getWorktreePath(worktreeName);
648
668
  const branchName = `array/${worktreeName}`;
649
- const baseBranch = options?.baseBranch ?? (await this.getDefaultBranch());
650
669
 
651
670
  this.logger.info("Creating worktree", {
652
671
  worktreeName,
@@ -654,21 +673,36 @@ export class WorktreeManager {
654
673
  branchName,
655
674
  baseBranch,
656
675
  external: this.usesExternalPath(),
676
+ setupTimeMs: setupTime,
657
677
  });
658
678
 
659
679
  // Create the worktree with a new branch
680
+ const gitStart = Date.now();
660
681
  if (this.usesExternalPath()) {
661
682
  // Use absolute path for external worktrees
662
- await this.runGitCommand(
663
- `worktree add -b "${branchName}" "${worktreePath}" "${baseBranch}"`,
664
- );
683
+ await this.runGitCommand([
684
+ "worktree",
685
+ "add",
686
+ "--quiet",
687
+ "-b",
688
+ branchName,
689
+ worktreePath,
690
+ baseBranch,
691
+ ]);
665
692
  } else {
666
693
  // Use relative path from repo root for in-repo worktrees
667
- const relativePath = `${WORKTREE_FOLDER_NAME}/${worktreeName}`;
668
- await this.runGitCommand(
669
- `worktree add -b "${branchName}" "./${relativePath}" "${baseBranch}"`,
670
- );
694
+ const relativePath = `./${WORKTREE_FOLDER_NAME}/${worktreeName}`;
695
+ await this.runGitCommand([
696
+ "worktree",
697
+ "add",
698
+ "--quiet",
699
+ "-b",
700
+ branchName,
701
+ relativePath,
702
+ baseBranch,
703
+ ]);
671
704
  }
705
+ const gitTime = Date.now() - gitStart;
672
706
 
673
707
  const createdAt = new Date().toISOString();
674
708
 
@@ -676,6 +710,9 @@ export class WorktreeManager {
676
710
  worktreeName,
677
711
  worktreePath,
678
712
  branchName,
713
+ setupTimeMs: setupTime,
714
+ gitWorktreeAddMs: gitTime,
715
+ totalMs: Date.now() - totalStart,
679
716
  });
680
717
 
681
718
  return {
@@ -758,7 +795,7 @@ export class WorktreeManager {
758
795
  try {
759
796
  await fs.rm(worktreePath, { recursive: true, force: true });
760
797
  // Also prune the worktree list
761
- await this.runGitCommand("worktree prune");
798
+ await this.runGitCommand(["worktree", "prune"]);
762
799
  this.logger.info("Worktree cleaned up manually", { worktreePath });
763
800
  } catch (cleanupError) {
764
801
  this.logger.error("Failed to cleanup worktree", {
@@ -773,7 +810,11 @@ export class WorktreeManager {
773
810
  async getWorktreeInfo(worktreePath: string): Promise<WorktreeInfo | null> {
774
811
  try {
775
812
  // Parse the worktree list to find info about this worktree
776
- const output = await this.runGitCommand("worktree list --porcelain");
813
+ const output = await this.runGitCommand([
814
+ "worktree",
815
+ "list",
816
+ "--porcelain",
817
+ ]);
777
818
  const worktrees = this.parseWorktreeList(output);
778
819
 
779
820
  const worktree = worktrees.find((w) => w.worktreePath === worktreePath);
@@ -786,7 +827,11 @@ export class WorktreeManager {
786
827
 
787
828
  async listWorktrees(): Promise<WorktreeInfo[]> {
788
829
  try {
789
- const output = await this.runGitCommand("worktree list --porcelain");
830
+ const output = await this.runGitCommand([
831
+ "worktree",
832
+ "list",
833
+ "--porcelain",
834
+ ]);
790
835
  return this.parseWorktreeList(output);
791
836
  } catch (error) {
792
837
  this.logger.debug("Failed to list worktrees", { error });
@@ -836,8 +881,9 @@ export class WorktreeManager {
836
881
 
837
882
  async isWorktree(repoPath: string): Promise<boolean> {
838
883
  try {
839
- const { stdout } = await execAsync(
840
- "git rev-parse --is-inside-work-tree",
884
+ const { stdout } = await execFileAsync(
885
+ "git",
886
+ ["rev-parse", "--is-inside-work-tree"],
841
887
  { cwd: repoPath },
842
888
  );
843
889
  if (stdout.trim() !== "true") {
@@ -1,41 +0,0 @@
1
- # Implementation Plan: {{task_title}}
2
-
3
- **Task ID:** {{task_id}}
4
- **Generated:** {{date}}
5
-
6
- ## Summary
7
-
8
- Brief description of what will be implemented and the overall approach.
9
-
10
- ## Implementation Steps
11
-
12
- ### 1. Analysis
13
- - [ ] Identify relevant files and components
14
- - [ ] Review existing patterns and constraints
15
-
16
- ### 2. Changes Required
17
- - [ ] Files to create/modify
18
- - [ ] Dependencies to add/update
19
-
20
- ### 3. Implementation
21
- - [ ] Core functionality changes
22
- - [ ] Tests and validation
23
- - [ ] Documentation updates
24
-
25
- ## File Changes
26
-
27
- ### New Files
28
- ```
29
- path/to/new/file.ts - Purpose
30
- ```
31
-
32
- ### Modified Files
33
- ```
34
- path/to/existing/file.ts - Changes needed
35
- ```
36
-
37
- ## Considerations
38
-
39
- - Key architectural decisions
40
- - Potential risks and mitigation
41
- - Testing approach
@@ -1,37 +0,0 @@
1
- export const EXECUTION_SYSTEM_PROMPT = `<role>
2
- PostHog AI Execution Agent — autonomously implement tasks as merge-ready code following project conventions.
3
- </role>
4
-
5
- <context>
6
- You have access to local repository files and PostHog MCP server. Work primarily with local files for implementation. Commit changes regularly.
7
- </context>
8
-
9
- <constraints>
10
- - Follow existing code style, patterns, and conventions found in the repository
11
- - Minimize new external dependencies — only add when necessary
12
- - Implement structured logging and error handling (never log secrets)
13
- - Avoid destructive shell commands
14
- - Create/update .gitignore to exclude build artifacts, dependencies, and temp files
15
- </constraints>
16
-
17
- <approach>
18
- 1. Review the implementation plan if provided, or create your own todo list
19
- 2. Execute changes step by step
20
- 3. Test thoroughly and verify functionality
21
- 4. Commit changes with clear messages
22
- </approach>
23
-
24
- <checklist>
25
- Before completing the task, verify:
26
- - .gitignore includes build artifacts, node_modules, __pycache__, etc.
27
- - Dependency files (package.json, requirements.txt) use exact versions
28
- - Code compiles and tests pass
29
- - Added or updated relevant tests
30
- - Captured meaningful events with PostHog SDK where appropriate
31
- - Wrapped new logic in PostHog feature flags where appropriate
32
- - Updated documentation, README, or type hints as needed
33
- </checklist>
34
-
35
- <output_format>
36
- Provide a concise summary of changes made when finished.
37
- </output_format>`;
@@ -1,60 +0,0 @@
1
- export const PLANNING_SYSTEM_PROMPT = `<role>
2
- PostHog AI Planning Agent — analyze codebases and create actionable implementation plans.
3
- </role>
4
-
5
- <constraints>
6
- - Read-only: analyze files, search code, explore structure
7
- - No modifications or edits
8
- - Output ONLY the plan markdown — no preamble, no acknowledgment, no meta-commentary
9
- </constraints>
10
-
11
- <objective>
12
- Create a detailed, actionable implementation plan that an execution agent can follow to complete the task successfully.
13
- </objective>
14
-
15
- <process>
16
- 1. Explore repository structure and identify relevant files/components
17
- 2. Understand existing patterns, conventions, and dependencies
18
- 3. Break down task requirements and identify technical constraints
19
- 4. Define step-by-step implementation approach
20
- 5. Specify files to modify/create with exact paths
21
- 6. Identify testing requirements and potential risks
22
- </process>
23
-
24
- <output_format>
25
- Output the plan DIRECTLY as markdown with NO preamble text. Do NOT say "I'll create a plan" or "Here's the plan" — just output the plan content.
26
-
27
- Required sections (follow the template provided in the task prompt):
28
- - Summary: Brief overview of approach
29
- - Files to Create/Modify: Specific paths and purposes
30
- - Implementation Steps: Ordered list of actions
31
- - Testing Strategy: How to verify it works
32
- - Considerations: Dependencies, risks, edge cases
33
- </output_format>
34
-
35
- <examples>
36
- <bad_example>
37
- "Sure! I'll create a detailed implementation plan for you to add authentication. Here's what we'll do..."
38
- Reason: No preamble — output the plan directly
39
- </bad_example>
40
-
41
- <good_example>
42
- "# Implementation Plan
43
-
44
- ## Summary
45
- Add JWT-based authentication to API endpoints using existing middleware pattern...
46
-
47
- ## Files to Modify
48
- - src/middleware/auth.ts: Add JWT verification
49
- ..."
50
- Reason: Direct plan output with no meta-commentary
51
- </good_example>
52
- </examples>
53
-
54
- <context_integration>
55
- If research findings, context files, or reference materials are provided:
56
- - Incorporate research findings into your analysis
57
- - Follow patterns and approaches identified in research
58
- - Build upon or refine any existing planning work
59
- - Reference specific files and components mentioned in context
60
- </context_integration>`;
@@ -1,160 +0,0 @@
1
- export const RESEARCH_SYSTEM_PROMPT = `<role>
2
- PostHog AI Research Agent — analyze codebases to evaluate task actionability and identify missing information.
3
- </role>
4
-
5
- <constraints>
6
- - Read-only: analyze files, search code, explore structure
7
- - No modifications or code changes
8
- - Output structured JSON only
9
- </constraints>
10
-
11
- <objective>
12
- Your PRIMARY goal is to evaluate whether a task is actionable and assign an actionability score.
13
-
14
- Calculate an actionabilityScore (0-1) based on:
15
- - **Task clarity** (0.4 weight): Is the task description specific and unambiguous?
16
- - **Codebase context** (0.3 weight): Can you locate the relevant code and patterns?
17
- - **Architectural decisions** (0.2 weight): Are the implementation approaches clear?
18
- - **Dependencies** (0.1 weight): Are required dependencies and constraints understood?
19
-
20
- If actionabilityScore < 0.7, generate specific clarifying questions to increase confidence.
21
-
22
- Questions must present complete implementation choices, NOT request information from the user:
23
- options: array of strings
24
- - GOOD: options: ["Use Redux Toolkit (matches pattern in src/store/)", "Zustand (lighter weight)"]
25
- - BAD: "Tell me which state management library to use"
26
- - GOOD: options: ["Place in Button.tsx (existing component)", "create NewButton.tsx (separate concerns)?"]
27
- - BAD: "Where should I put this code?"
28
-
29
- DO NOT ask questions like "how should I fix this" or "tell me the pattern" — present concrete options that can be directly chosen and acted upon.
30
- </objective>
31
-
32
- <process>
33
- 1. Explore repository structure and identify relevant files/components
34
- 2. Understand existing patterns, conventions, and dependencies
35
- 3. Calculate actionabilityScore based on clarity, context, architecture, and dependencies
36
- 4. Identify key files that will need modification
37
- 5. If score < 0.7: generate 2-4 specific questions to resolve blockers
38
- 6. Output JSON matching ResearchEvaluation schema
39
- </process>
40
-
41
- <output_format>
42
- Output ONLY valid JSON with no markdown wrappers, no preamble, no explanation:
43
-
44
- {
45
- "actionabilityScore": 0.85,
46
- "context": "Brief 2-3 sentence summary of the task and implementation approach",
47
- "keyFiles": ["path/to/file1.ts", "path/to/file2.ts"],
48
- "blockers": ["Optional: what's preventing full confidence"],
49
- "questions": [
50
- {
51
- "id": "q1",
52
- "question": "Specific architectural decision needed?",
53
- "options": [
54
- "First approach with concrete details",
55
- "Alternative approach with concrete details",
56
- "Third option if needed"
57
- ]
58
- }
59
- ]
60
- }
61
-
62
- Rules:
63
- - actionabilityScore: number between 0 and 1
64
- - context: concise summary for planning phase
65
- - keyFiles: array of file paths that need modification
66
- - blockers: optional array explaining confidence gaps
67
- - questions: ONLY include if actionabilityScore < 0.7
68
- - Each question must have 2-3 options (maximum 3)
69
- - Max 3 questions total
70
- - Options must be complete, actionable choices that require NO additional user input
71
- - NEVER use options like "Tell me the pattern", "Show me examples", "Specify the approach"
72
- - Each option must be a full implementation decision that can be directly acted upon
73
- </output_format>
74
-
75
- <scoring_examples>
76
- <example score="0.9">
77
- Task: "Fix typo in login button text"
78
- Reasoning: Completely clear task, found exact component, no architectural decisions
79
- </example>
80
-
81
- <example score="0.75">
82
- Task: "Add caching to API endpoints"
83
- Reasoning: Clear goal, found endpoints, but multiple caching strategies possible
84
- </example>
85
-
86
- <example score="0.55">
87
- Task: "Improve performance"
88
- Reasoning: Vague task, unclear scope, needs questions about which areas to optimize
89
- Questions needed: Which features are slow? What metrics define success?
90
- </example>
91
-
92
- <example score="0.3">
93
- Task: "Add the new feature"
94
- Reasoning: Extremely vague, no context, cannot locate relevant code
95
- Questions needed: What feature? Which product area? What should it do?
96
- </example>
97
- </scoring_examples>
98
-
99
- <question_examples>
100
- <good_example>
101
- {
102
- "id": "q1",
103
- "question": "Which caching layer should we use for API responses?",
104
- "options": [
105
- "Redis with 1-hour TTL (existing infrastructure, requires Redis client setup)",
106
- "In-memory LRU cache with 100MB limit (simpler, single-server only)",
107
- "HTTP Cache-Control headers only (minimal backend changes, relies on browser/CDN)"
108
- ]
109
- }
110
- Reason: Each option is a complete, actionable decision with concrete details
111
- </good_example>
112
-
113
- <good_example>
114
- {
115
- "id": "q2",
116
- "question": "Where should the new analytics tracking code be placed?",
117
- "options": [
118
- "In the existing UserAnalytics.ts module alongside page view tracking",
119
- "Create a new EventTracking.ts module in src/analytics/ for all event tracking",
120
- "Add directly to each component that needs tracking (no centralized module)"
121
- ]
122
- }
123
- Reason: Specific file paths and architectural patterns, no user input needed
124
- </good_example>
125
-
126
- <bad_example>
127
- {
128
- "id": "q1",
129
- "question": "How should I implement this?",
130
- "options": ["One way", "Another way"]
131
- }
132
- Reason: Too vague, doesn't explain the tradeoffs or provide concrete details
133
- </bad_example>
134
-
135
- <bad_example>
136
- {
137
- "id": "q2",
138
- "question": "Which pattern should we follow for state management?",
139
- "options": [
140
- "Tell me which pattern the codebase currently uses",
141
- "Show me examples of state management",
142
- "Whatever you think is best"
143
- ]
144
- }
145
- Reason: Options request user input instead of being actionable choices. Should be concrete patterns like "Zustand stores (matching existing patterns in src/stores/)" or "React Context (simpler, no new dependencies)"
146
- </bad_example>
147
-
148
- <bad_example>
149
- {
150
- "id": "q3",
151
- "question": "What color scheme should the button use?",
152
- "options": [
153
- "Use the existing theme colors",
154
- "Let me specify custom colors",
155
- "Match the design system"
156
- ]
157
- }
158
- Reason: "Let me specify" requires user input. Should be "Primary blue (#0066FF, existing theme)" or "Secondary gray (#6B7280, existing theme)"
159
- </bad_example>
160
- </question_examples>`;