@posthog/agent 2.3.353 → 2.3.354

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.
@@ -8723,11 +8723,12 @@ async function getHeadSha(baseDir, options) {
8723
8723
 
8724
8724
  // src/server/agent-server.ts
8725
8725
  var import_hono = require("hono");
8726
+ var import_zod3 = require("zod");
8726
8727
 
8727
8728
  // package.json
8728
8729
  var package_default = {
8729
8730
  name: "@posthog/agent",
8730
- version: "2.3.353",
8731
+ version: "2.3.354",
8731
8732
  repository: "https://github.com/PostHog/code",
8732
8733
  description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
8733
8734
  exports: {
@@ -13883,6 +13884,17 @@ async function handleSystemMessage(message, context) {
13883
13884
  break;
13884
13885
  }
13885
13886
  }
13887
+ function classifyAgentError(result) {
13888
+ if (!result) return "agent_error";
13889
+ const text2 = result.trim();
13890
+ if (/API Error:\s*terminated\b/i.test(text2)) {
13891
+ return "upstream_stream_terminated";
13892
+ }
13893
+ if (/API Error:\s*Connection error\b/i.test(text2)) {
13894
+ return "upstream_connection_error";
13895
+ }
13896
+ return "agent_error";
13897
+ }
13886
13898
  function handleResultMessage(message) {
13887
13899
  const usage = extractUsageFromResult(message);
13888
13900
  switch (message.subtype) {
@@ -13898,9 +13910,13 @@ function handleResultMessage(message) {
13898
13910
  return { shouldStop: true, stopReason: "max_tokens", usage };
13899
13911
  }
13900
13912
  if (message.is_error) {
13913
+ const classification = classifyAgentError(message.result);
13901
13914
  return {
13902
13915
  shouldStop: true,
13903
- error: import_sdk.RequestError.internalError(void 0, message.result),
13916
+ error: import_sdk.RequestError.internalError(
13917
+ { classification, result: message.result },
13918
+ message.result
13919
+ ),
13904
13920
  usage
13905
13921
  };
13906
13922
  }
@@ -19114,6 +19130,14 @@ function validateCommandParams(method, params) {
19114
19130
  }
19115
19131
 
19116
19132
  // src/server/agent-server.ts
19133
+ var agentErrorClassificationSchema = import_zod3.z.enum([
19134
+ "upstream_stream_terminated",
19135
+ "upstream_connection_error",
19136
+ "agent_error"
19137
+ ]);
19138
+ var errorWithClassificationSchema = import_zod3.z.object({
19139
+ data: import_zod3.z.object({ classification: agentErrorClassificationSchema })
19140
+ });
19117
19141
  var NdJsonTap = class {
19118
19142
  constructor(onMessage) {
19119
19143
  this.onMessage = onMessage;
@@ -19816,6 +19840,23 @@ var AgentServer = class _AgentServer {
19816
19840
  );
19817
19841
  await this.sendInitialTaskMessage(payload, preTaskRun);
19818
19842
  }
19843
+ extractErrorClassification(error) {
19844
+ const message = error instanceof Error ? error.message : String(error ?? "");
19845
+ const parsed = errorWithClassificationSchema.safeParse(error);
19846
+ if (parsed.success) {
19847
+ return { classification: parsed.data.data.classification, message };
19848
+ }
19849
+ return { classification: classifyAgentError(message), message };
19850
+ }
19851
+ classifyAndSignalFailure(payload, phase, error) {
19852
+ const { classification, message } = this.extractErrorClassification(error);
19853
+ const errorMessage = classification === "upstream_stream_terminated" ? "Upstream LLM stream terminated" : classification === "upstream_connection_error" ? "Upstream LLM connection error" : message || "Agent error";
19854
+ this.logger.error(`send_${phase}_task_message_failed`, {
19855
+ classification,
19856
+ message
19857
+ });
19858
+ return this.signalTaskComplete(payload, "error", errorMessage);
19859
+ }
19819
19860
  async sendInitialTaskMessage(payload, prefetchedRun) {
19820
19861
  if (!this.session) return;
19821
19862
  let taskRun = prefetchedRun ?? null;
@@ -19908,7 +19949,7 @@ var AgentServer = class _AgentServer {
19908
19949
  if (this.session) {
19909
19950
  await this.session.logWriter.flushAll();
19910
19951
  }
19911
- await this.signalTaskComplete(payload, "error");
19952
+ await this.classifyAndSignalFailure(payload, "initial", error);
19912
19953
  }
19913
19954
  }
19914
19955
  async sendResumeMessage(payload, taskRun) {
@@ -19982,7 +20023,7 @@ Continue from where you left off. The user is waiting for your response.`
19982
20023
  if (this.session) {
19983
20024
  await this.session.logWriter.flushAll();
19984
20025
  }
19985
- await this.signalTaskComplete(payload, "error");
20026
+ await this.classifyAndSignalFailure(payload, "resume", error);
19986
20027
  }
19987
20028
  }
19988
20029
  static RESUME_HISTORY_TOKEN_BUDGET = 5e4;
@@ -20340,7 +20381,7 @@ ${attributionInstructions}
20340
20381
  });
20341
20382
  }
20342
20383
  }
20343
- async signalTaskComplete(payload, stopReason) {
20384
+ async signalTaskComplete(payload, stopReason, errorMessage) {
20344
20385
  if (this.session?.payload.run_id === payload.run_id) {
20345
20386
  try {
20346
20387
  await this.session.logWriter.flush(payload.run_id, {
@@ -20364,7 +20405,7 @@ ${attributionInstructions}
20364
20405
  try {
20365
20406
  await this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
20366
20407
  status,
20367
- error_message: stopReason === "error" ? "Agent error" : void 0
20408
+ error_message: errorMessage ?? "Agent error"
20368
20409
  });
20369
20410
  this.logger.info("Task completion signaled", { status, stopReason });
20370
20411
  } catch (error) {