@posthog/agent 2.3.298 → 2.3.304

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "2.3.298",
3
+ "version": "2.3.304",
4
4
  "repository": "https://github.com/PostHog/code",
5
5
  "description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
6
6
  "exports": {
@@ -60,20 +60,32 @@ describe("CodexAcpAgent", () => {
60
60
  vi.clearAllMocks();
61
61
  });
62
62
 
63
- function createAgent(): CodexAcpAgent {
63
+ function createAgent(overrides: Partial<AgentSideConnection> = {}): {
64
+ agent: CodexAcpAgent;
65
+ client: AgentSideConnection & {
66
+ extNotification: ReturnType<typeof vi.fn>;
67
+ sessionUpdate: ReturnType<typeof vi.fn>;
68
+ };
69
+ } {
64
70
  const client = {
65
71
  extNotification: vi.fn(),
66
- } as unknown as AgentSideConnection;
67
-
68
- return new CodexAcpAgent(client, {
72
+ sessionUpdate: vi.fn(),
73
+ ...overrides,
74
+ } as unknown as AgentSideConnection & {
75
+ extNotification: ReturnType<typeof vi.fn>;
76
+ sessionUpdate: ReturnType<typeof vi.fn>;
77
+ };
78
+
79
+ const agent = new CodexAcpAgent(client, {
69
80
  codexProcessOptions: {
70
81
  cwd: process.cwd(),
71
82
  },
72
83
  });
84
+ return { agent, client };
73
85
  }
74
86
 
75
87
  it("applies the requested initial mode for a new session", async () => {
76
- const agent = createAgent();
88
+ const { agent } = createAgent();
77
89
  mockCodexConnection.newSession.mockResolvedValue({
78
90
  sessionId: "session-1",
79
91
  modes: { currentModeId: "auto", availableModes: [] },
@@ -96,7 +108,7 @@ describe("CodexAcpAgent", () => {
96
108
  });
97
109
 
98
110
  it("preserves the live session mode when loading an existing session", async () => {
99
- const agent = createAgent();
111
+ const { agent } = createAgent();
100
112
  mockCodexConnection.loadSession.mockResolvedValue({
101
113
  modes: { currentModeId: "read-only", availableModes: [] },
102
114
  configOptions: [],
@@ -114,4 +126,93 @@ describe("CodexAcpAgent", () => {
114
126
  .sessionState.permissionMode,
115
127
  ).toBe("read-only");
116
128
  });
129
+
130
+ it("prepends _meta.prContext to the forwarded prompt but not to the broadcast", async () => {
131
+ const { agent, client } = createAgent();
132
+ mockCodexConnection.newSession.mockResolvedValue({
133
+ sessionId: "session-1",
134
+ modes: { currentModeId: "auto", availableModes: [] },
135
+ configOptions: [],
136
+ } satisfies Partial<NewSessionResponse>);
137
+ await agent.newSession({
138
+ cwd: process.cwd(),
139
+ } as never);
140
+
141
+ mockCodexConnection.prompt.mockResolvedValue({ stopReason: "end_turn" });
142
+
143
+ await agent.prompt({
144
+ sessionId: "session-1",
145
+ prompt: [{ type: "text", text: "ship the fix" }],
146
+ _meta: { prContext: "PR #123 is open; review before editing." },
147
+ } as never);
148
+
149
+ // codex-acp receives the PR context prepended as a text block.
150
+ expect(mockCodexConnection.prompt).toHaveBeenCalledWith(
151
+ expect.objectContaining({
152
+ prompt: [
153
+ { type: "text", text: "PR #123 is open; review before editing." },
154
+ { type: "text", text: "ship the fix" },
155
+ ],
156
+ }),
157
+ );
158
+ // The broadcast shows only the real user turn — the prContext prefix
159
+ // is internal routing and should not render as a user message.
160
+ expect(client.sessionUpdate).toHaveBeenCalledTimes(1);
161
+ expect(client.sessionUpdate).toHaveBeenCalledWith({
162
+ sessionId: "session-1",
163
+ update: {
164
+ sessionUpdate: "user_message_chunk",
165
+ content: { type: "text", text: "ship the fix" },
166
+ },
167
+ });
168
+ });
169
+
170
+ it("broadcasts user prompt as user_message_chunk before delegating to codex-acp", async () => {
171
+ const { agent, client } = createAgent();
172
+ // Seed an active session so prompt() has the state it expects.
173
+ mockCodexConnection.newSession.mockResolvedValue({
174
+ sessionId: "session-1",
175
+ modes: { currentModeId: "auto", availableModes: [] },
176
+ configOptions: [],
177
+ } satisfies Partial<NewSessionResponse>);
178
+ await agent.newSession({
179
+ cwd: process.cwd(),
180
+ } as never);
181
+
182
+ const callOrder: string[] = [];
183
+ client.sessionUpdate.mockImplementation(async () => {
184
+ callOrder.push("sessionUpdate");
185
+ });
186
+ mockCodexConnection.prompt.mockImplementation(async () => {
187
+ callOrder.push("prompt");
188
+ return { stopReason: "end_turn" };
189
+ });
190
+
191
+ await agent.prompt({
192
+ sessionId: "session-1",
193
+ prompt: [
194
+ { type: "text", text: "first chunk" },
195
+ { type: "text", text: "second chunk" },
196
+ ],
197
+ } as never);
198
+
199
+ expect(client.sessionUpdate).toHaveBeenCalledTimes(2);
200
+ expect(client.sessionUpdate).toHaveBeenNthCalledWith(1, {
201
+ sessionId: "session-1",
202
+ update: {
203
+ sessionUpdate: "user_message_chunk",
204
+ content: { type: "text", text: "first chunk" },
205
+ },
206
+ });
207
+ expect(client.sessionUpdate).toHaveBeenNthCalledWith(2, {
208
+ sessionId: "session-1",
209
+ update: {
210
+ sessionUpdate: "user_message_chunk",
211
+ content: { type: "text", text: "second chunk" },
212
+ },
213
+ });
214
+ // Broadcast must land before the prompt reaches codex-acp so the user
215
+ // turn is persisted even if the underlying prompt fails.
216
+ expect(callOrder).toEqual(["sessionUpdate", "sessionUpdate", "prompt"]);
217
+ });
117
218
  });
@@ -93,6 +93,24 @@ function toCodexPermissionMode(mode?: string): PermissionMode {
93
93
  return "auto";
94
94
  }
95
95
 
96
+ /**
97
+ * Prepend `_meta.prContext` (set by the agent-server on Slack-originated
98
+ * follow-up runs) to the prompt as a text block, mirroring Claude's
99
+ * `promptToClaude` behavior. Without this, codex cloud runs lose the
100
+ * PR-review context that follow-up flows rely on.
101
+ */
102
+ function prependPrContext(params: PromptRequest): PromptRequest {
103
+ const prContext = (params._meta as Record<string, unknown> | undefined)
104
+ ?.prContext;
105
+ if (typeof prContext !== "string" || prContext.length === 0) {
106
+ return params;
107
+ }
108
+ return {
109
+ ...params,
110
+ prompt: [{ type: "text", text: prContext }, ...params.prompt],
111
+ };
112
+ }
113
+
96
114
  const CODEX_NATIVE_MODE: Record<CodeExecutionMode, CodexNativeMode> = {
97
115
  default: "auto",
98
116
  acceptEdits: "auto",
@@ -369,7 +387,17 @@ export class CodexAcpAgent extends BaseAcpAgent {
369
387
  this.session.interruptReason = undefined;
370
388
  resetUsage(this.sessionState);
371
389
 
372
- const response = await this.codexConnection.prompt(params);
390
+ // codex-acp does not echo the user prompt back on the agent→client
391
+ // channel, so without this broadcast the tapped stream (persisted to S3
392
+ // and rendered by the PostHog web UI) never sees a user turn and only
393
+ // the assistant reply shows up. Mirrors ClaudeAcpAgent.broadcastUserMessage.
394
+ // The original params (no _meta.prContext prefix) is broadcast so the
395
+ // injected PR context is not rendered as a user message.
396
+ await this.broadcastUserMessage(params);
397
+
398
+ const response = await this.codexConnection.prompt(
399
+ prependPrContext(params),
400
+ );
373
401
 
374
402
  // Usage is already accumulated via sessionUpdate notifications in
375
403
  // codex-client.ts. Do NOT also add response.usage here or tokens
@@ -417,6 +445,20 @@ export class CodexAcpAgent extends BaseAcpAgent {
417
445
  });
418
446
  }
419
447
 
448
+ private async broadcastUserMessage(params: PromptRequest): Promise<void> {
449
+ for (const chunk of params.prompt) {
450
+ const notification = {
451
+ sessionId: params.sessionId,
452
+ update: {
453
+ sessionUpdate: "user_message_chunk" as const,
454
+ content: chunk,
455
+ },
456
+ };
457
+ await this.client.sessionUpdate(notification);
458
+ this.appendNotification(params.sessionId, notification);
459
+ }
460
+ }
461
+
420
462
  async setSessionMode(
421
463
  params: SetSessionModeRequest,
422
464
  ): Promise<SetSessionModeResponse> {