@gajae-code/agent-core 0.18.6 → 0.18.8

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/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.18.8] - 2026-10-08
6
+
7
+ ### Fixed
8
+
9
+ - Promote zero-token empty provider responses to explicit errors, including responses without typed transport metadata.
10
+ - Preserve clean-attempt fallback for typed capacity overloads after earlier committed tool work, while blocking replay of observable output.
11
+
12
+ - Preserve managed fallback retries when a provider stages an empty assistant text placeholder before reporting a statusless overload, while retaining non-empty terminal error content.
13
+
14
+ ## [0.18.7] - 2026-10-04
15
+
5
16
  ## [0.18.6] - 2026-10-03
6
17
 
7
18
  ### Fixed
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@gajae-code/agent-core",
4
- "version": "0.18.6",
4
+ "version": "0.18.8",
5
5
  "description": "General-purpose agent with transport abstraction, state management, and attachment support",
6
6
  "homepage": "https://gajae-code.com",
7
7
  "author": "Yeachan-Heo and Gajae Code Contributors",
@@ -32,9 +32,9 @@
32
32
  "fmt": "biome format --write ."
33
33
  },
34
34
  "dependencies": {
35
- "@gajae-code/ai": "0.18.6",
36
- "@gajae-code/natives": "0.18.6",
37
- "@gajae-code/utils": "0.18.6",
35
+ "@gajae-code/ai": "0.18.8",
36
+ "@gajae-code/natives": "0.18.8",
37
+ "@gajae-code/utils": "0.18.8",
38
38
  "@opentelemetry/api": "^1.9.0"
39
39
  },
40
40
  "devDependencies": {
package/src/agent-loop.ts CHANGED
@@ -10,7 +10,6 @@ import {
10
10
  type Context,
11
11
  classifyContextOverflow,
12
12
  classifyFallbackTrigger,
13
- EMPTY_RESPONSE_PROVIDER_CODE,
14
13
  EventStream,
15
14
  isProviderSafetyStopAuthenticated,
16
15
  isZodSchema,
@@ -770,12 +769,41 @@ function managedTransportFailure(failure: unknown) {
770
769
  return facts && typeof facts === "object" ? transportFailureFacts(facts) : undefined;
771
770
  }
772
771
 
772
+ function managedAssistantMessageHasContent(failure: unknown): boolean {
773
+ const content = managedProperty(failure, "content");
774
+ if (content === undefined) return false;
775
+ try {
776
+ if (!Array.isArray(content)) return true;
777
+ return content.some(block => {
778
+ if (!block || typeof block !== "object") return true;
779
+ const type = managedProperty(block, "type");
780
+ if (type === "text") {
781
+ const text = managedProperty(block, "text");
782
+ return typeof text !== "string" || text.length > 0;
783
+ }
784
+ if (type === "thinking") {
785
+ const thinking = managedProperty(block, "thinking");
786
+ const signature = managedProperty(block, "thinkingSignature");
787
+ return (
788
+ typeof thinking !== "string" ||
789
+ (thinking.length === 0 && signature !== undefined && typeof signature !== "string") ||
790
+ thinking.length > 0 ||
791
+ (typeof signature === "string" && signature.length > 0)
792
+ );
793
+ }
794
+ return true;
795
+ });
796
+ } catch {
797
+ return true;
798
+ }
799
+ }
773
800
  // AI owns provider-originated authority. The agent loop owns authority for
774
801
  // the rebuilt message objects it creates; this second WeakSet is deliberately
775
802
  // module-private so a public AI consumer cannot transfer authority to an
776
803
  // arbitrary destination. A destination is marked only while this managed
777
804
  // runtime is rebuilding a source that AI authenticated.
778
805
  const managedProviderSafetyStops = new WeakSet<object>();
806
+ const managedLocalEmptyResponses = new WeakSet<object>();
779
807
 
780
808
  function isManagedProviderSafetyStopAuthenticated(value: unknown): boolean {
781
809
  return (
@@ -784,21 +812,28 @@ function isManagedProviderSafetyStopAuthenticated(value: unknown): boolean {
784
812
  );
785
813
  }
786
814
 
787
- function managedRetryableFailure(failure: unknown): boolean {
815
+ function managedRetryableFailure(failure: unknown, transaction?: ManagedAttemptTransaction): boolean {
816
+ const hasObservableOutput =
817
+ transaction?.hasObservableAssistantOutput(failure) ?? managedAssistantMessageHasContent(failure);
818
+ if (
819
+ managedProperty(failure, "stopReason") === "error" &&
820
+ managedProperty(failure, "errorKind") === "local_empty_response"
821
+ ) {
822
+ return !hasObservableOutput;
823
+ }
788
824
  const facts = managedTransportFailure(failure);
789
825
  if (!facts) return false;
790
- // OpenAI's typed statusless capacity-overload code (issue #5018) never
791
- // becomes managed transaction authority. Before the code survived as
792
- // transport facts this failure produced none, so the staged attempt was
793
- // always committed; the shared Responses parser and Codex events now carry
794
- // it, and this check preserves that committed-failure behavior instead of
795
- // discarding the transaction. It reads only typed facts, never error text.
826
+ // Statusless typed capacity overloads are retryable only when the managed
827
+ // transaction and its terminal failure contain no observable assistant
828
+ // output. Empty provider-start text placeholders do not block retry, while
829
+ // terminal-only content remains committed. Admission reads typed facts, not
830
+ // provider error prose.
796
831
  if (
797
832
  facts.status === undefined &&
798
833
  facts.providerCode === SERVER_OVERLOADED_PROVIDER_CODE &&
799
834
  (facts.openaiErrorCode === undefined || facts.openaiErrorCode === SERVER_OVERLOADED_PROVIDER_CODE)
800
835
  ) {
801
- return false;
836
+ return !hasObservableOutput;
802
837
  }
803
838
  // A typed provider safety stop is terminal evidence ahead of any transport
804
839
  // class, but only with adapter-minted provenance: unauthenticated labels
@@ -825,21 +860,42 @@ function managedRetryableFailure(failure: unknown): boolean {
825
860
  );
826
861
  }
827
862
 
828
- function promoteTypedEmptyResponseStop(message: AssistantMessage): void {
863
+ function promoteEmptyResponseStop(
864
+ message: AssistantMessage,
865
+ providerMessage: unknown,
866
+ transaction?: ManagedAttemptTransaction,
867
+ ): void {
868
+ const providerContent = managedProperty(providerMessage, "content");
869
+ const providerErrorMessage = managedProperty(providerMessage, "errorMessage");
870
+ let providerContentIsEmpty = false;
871
+ try {
872
+ providerContentIsEmpty = Array.isArray(providerContent) && providerContent.length === 0;
873
+ } catch {
874
+ return;
875
+ }
829
876
  if (
877
+ managedProperty(providerMessage, "stopReason") !== "stop" ||
878
+ !providerContentIsEmpty ||
879
+ (typeof providerErrorMessage === "string" && providerErrorMessage !== "") ||
880
+ managedProperty(providerMessage, "errorKind") !== undefined ||
881
+ managedProperty(providerMessage, "errorStatus") !== undefined ||
882
+ transaction?.hasObservableAssistantOutput(providerMessage) ||
830
883
  message.stopReason !== "stop" ||
831
884
  message.content.length !== 0 ||
832
885
  message.usage.input !== 0 ||
833
886
  message.usage.output !== 0 ||
834
887
  message.usage.cacheRead !== 0 ||
835
888
  message.usage.cacheWrite !== 0 ||
836
- message.usage.totalTokens !== 0 ||
837
- managedTransportFailure(message)?.providerCode?.toLowerCase() !== EMPTY_RESPONSE_PROVIDER_CODE
889
+ message.usage.totalTokens !== 0
838
890
  ) {
839
891
  return;
840
892
  }
841
893
  message.stopReason = "error";
842
894
  message.errorMessage = "Provider returned an empty response with zero token usage";
895
+ // Preserve the runtime-owned retry classification for untyped provider
896
+ // payloads without inventing provider transport facts.
897
+ message.errorKind = "local_empty_response";
898
+ managedLocalEmptyResponses.add(message);
843
899
  }
844
900
  /**
845
901
  * Terminal safety-stop authority is provenance-bound, not data-bound: a
@@ -867,6 +923,7 @@ function sanitizeProviderSafetyStopProvenance(
867
923
  const errorKindRead = managedPropertyRead(message, "errorKind");
868
924
  if (
869
925
  errorKindRead.ok &&
926
+ errorKindRead.value !== "local_empty_response" &&
870
927
  (errorKindRead.value !== "provider_safety_stop" || isManagedProviderSafetyStopAuthenticated(message))
871
928
  ) {
872
929
  return message;
@@ -2138,7 +2195,11 @@ function managedAssistantShell(
2138
2195
  const errorKind =
2139
2196
  stopReason === "error" && managedProperty(source, "errorKind") === "provider_safety_stop"
2140
2197
  ? ("provider_safety_stop" as const)
2141
- : undefined;
2198
+ : stopReason === "error" &&
2199
+ ((typeof value === "object" && value !== null && managedLocalEmptyResponses.has(value)) ||
2200
+ (typeof source === "object" && source !== null && managedLocalEmptyResponses.has(source)))
2201
+ ? ("local_empty_response" as const)
2202
+ : undefined;
2142
2203
  const safeMetadata: Record<string, unknown> = {};
2143
2204
  if (isManagedPlainRecord(detailed.snapshot)) {
2144
2205
  for (const key of Object.keys(detailed.snapshot)) {
@@ -2185,6 +2246,7 @@ function managedAssistantShell(
2185
2246
  revokeProviderSafetyStop(value);
2186
2247
  if (typeof value === "object" && value !== null) managedProviderSafetyStops.delete(value);
2187
2248
  }
2249
+ if (errorKind === "local_empty_response") managedLocalEmptyResponses.add(rebuilt);
2188
2250
  return rebuilt;
2189
2251
  }
2190
2252
 
@@ -2374,7 +2436,9 @@ function managedAssistantContent(value: unknown): AssistantMessage["content"][nu
2374
2436
  }
2375
2437
  if (type === "thinking") {
2376
2438
  const thinking = managedProperty(value, "thinking");
2377
- return typeof thinking === "string" ? { type, thinking } : undefined;
2439
+ const thinkingSignature = managedProperty(value, "thinkingSignature");
2440
+ if (typeof thinking !== "string") return undefined;
2441
+ return { type, thinking, ...(typeof thinkingSignature === "string" ? { thinkingSignature } : {}) };
2378
2442
  }
2379
2443
  if (type === "redactedThinking") {
2380
2444
  const data = managedProperty(value, "data");
@@ -2673,6 +2737,7 @@ function isSupersededStreamingDelta(item: ManagedAttemptBatchItem): boolean {
2673
2737
 
2674
2738
  class ManagedAttemptTransaction {
2675
2739
  #batch: ManagedAttemptBatchItem[] = [];
2740
+ #hasObservableAssistantOutput = false;
2676
2741
  #stagedEventCount = 0;
2677
2742
  #stagedBytes = 0;
2678
2743
  /** Caps for this transaction, read once from the operator env knobs. */
@@ -2698,7 +2763,7 @@ class ManagedAttemptTransaction {
2698
2763
  push(event: AgentEvent): void {
2699
2764
  if (this.#committed) {
2700
2765
  if (event.type === "message_end" || event.type === "turn_end") {
2701
- this.#batch.push({ type: "event", event });
2766
+ this.#retain({ type: "event", event });
2702
2767
  return;
2703
2768
  }
2704
2769
  this.stream.push(event);
@@ -2801,7 +2866,7 @@ class ManagedAttemptTransaction {
2801
2866
  }
2802
2867
  // Each frame's exact accounted size is retained so compaction can debit
2803
2868
  // exactly what it reclaims instead of re-measuring the whole batch.
2804
- this.#batch.push({
2869
+ this.#retain({
2805
2870
  type: "assistant_event",
2806
2871
  message: partial,
2807
2872
  event: snapshotEvent,
@@ -2811,6 +2876,54 @@ class ManagedAttemptTransaction {
2811
2876
  this.#stagedBytes += retainedBytes;
2812
2877
  }
2813
2878
 
2879
+ hasObservableAssistantOutput(terminalMessage?: unknown): boolean {
2880
+ return this.#hasObservableAssistantOutput || managedAssistantMessageHasContent(terminalMessage);
2881
+ }
2882
+
2883
+ #retain(item: ManagedAttemptBatchItem): void {
2884
+ this.#hasObservableAssistantOutput ||= this.#itemHasObservableAssistantOutput(item);
2885
+ this.#batch.push(item);
2886
+ }
2887
+
2888
+ #itemHasObservableAssistantOutput(item: ManagedAttemptBatchItem): boolean {
2889
+ if (item.type === "assistant_event") {
2890
+ if (managedAssistantMessageHasContent(item.message)) return true;
2891
+ const event = item.event;
2892
+ if (
2893
+ event.type === "text_delta" ||
2894
+ event.type === "thinking_delta" ||
2895
+ event.type === "reasoning_summary_delta" ||
2896
+ event.type === "text_end" ||
2897
+ event.type === "thinking_end" ||
2898
+ event.type === "reasoning_summary_end"
2899
+ ) {
2900
+ if (event.type === "text_end" || event.type === "thinking_end" || event.type === "reasoning_summary_end")
2901
+ return event.content.length > 0;
2902
+ return event.delta.length > 0;
2903
+ }
2904
+ return event.type === "toolcall_start" || event.type === "toolcall_delta" || event.type === "toolcall_end";
2905
+ }
2906
+ const event = item.event;
2907
+ if ("message" in event && managedAssistantMessageHasContent(event.message)) return true;
2908
+ if ("error" in event && managedAssistantMessageHasContent(event.error)) return true;
2909
+ if (event.type === "message_update") {
2910
+ const update = event.assistantMessageEvent;
2911
+ if (
2912
+ update.type === "text_delta" ||
2913
+ update.type === "thinking_delta" ||
2914
+ update.type === "reasoning_summary_delta"
2915
+ )
2916
+ return update.delta.length > 0;
2917
+ if (update.type === "text_end" || update.type === "thinking_end" || update.type === "reasoning_summary_end")
2918
+ return update.content.length > 0;
2919
+ return update.type === "toolcall_start" || update.type === "toolcall_delta" || update.type === "toolcall_end";
2920
+ }
2921
+ return (
2922
+ event.type === "tool_execution_start" ||
2923
+ event.type === "tool_execution_update" ||
2924
+ event.type === "tool_execution_end"
2925
+ );
2926
+ }
2814
2927
  flush(): void {
2815
2928
  if (this.#discarded) return;
2816
2929
  for (const item of this.#batch) {
@@ -3097,7 +3210,7 @@ class ManagedAttemptTransaction {
3097
3210
  this.push(detached);
3098
3211
  return;
3099
3212
  }
3100
- this.#batch.push({ type: "event", event: detached, bytes: detachedBytes });
3213
+ this.#retain({ type: "event", event: detached, bytes: detachedBytes });
3101
3214
  this.#stagedEventCount++;
3102
3215
  this.#stagedBytes += detachedBytes;
3103
3216
  return;
@@ -3191,7 +3304,7 @@ class ManagedAttemptTransaction {
3191
3304
  }
3192
3305
  // Retain each frame's accounted size so compaction can debit exactly what
3193
3306
  // it reclaims instead of re-measuring the whole batch.
3194
- this.#batch.push({ type: "event", event: snapshot, bytes });
3307
+ this.#retain({ type: "event", event: snapshot, bytes });
3195
3308
  this.#stagedEventCount += 1;
3196
3309
 
3197
3310
  this.#stagedBytes += bytes;
@@ -3913,6 +4026,7 @@ async function runLoopBody(
3913
4026
  recoveryAttempt && !wasEscapedNonAsciiRecoveryAttempt
3914
4027
  ? undefined
3915
4028
  : { value: getLogicalTurnToolChoice() },
4029
+ attemptTransaction,
3916
4030
  );
3917
4031
  const detection = detectHarmonyLeakInAssistantMessage(message);
3918
4032
  if (detection && shouldMitigateHarmonyLeak(config.model, detection)) {
@@ -3936,7 +4050,7 @@ async function runLoopBody(
3936
4050
  stream.end(newMessages);
3937
4051
  return;
3938
4052
  }
3939
- if (config.fallbackManaged && transaction && managedRetryableFailure(err)) {
4053
+ if (config.fallbackManaged && transaction && managedRetryableFailure(err, transaction)) {
3940
4054
  transaction.discard();
3941
4055
  currentContext.messages.splice(contextMessageCount);
3942
4056
  newMessages.splice(newMessageCount);
@@ -4208,7 +4322,11 @@ async function runLoopBody(
4208
4322
  : "Provider returned an empty response with anomalously low token usage (possible context overflow via proxy)";
4209
4323
  }
4210
4324
 
4211
- if (config.fallbackManaged && message.stopReason === "error" && managedRetryableFailure(message)) {
4325
+ if (
4326
+ config.fallbackManaged &&
4327
+ message.stopReason === "error" &&
4328
+ managedRetryableFailure(message, transaction)
4329
+ ) {
4212
4330
  transaction?.discard();
4213
4331
  currentContext.messages.splice(contextMessageCount);
4214
4332
  newMessages.splice(newMessageCount);
@@ -4541,6 +4659,7 @@ async function streamAssistantResponse(
4541
4659
  },
4542
4660
  provisionalToolTransaction?: ManagedAttemptTransaction,
4543
4661
  toolChoiceOverride?: { value: ToolChoice | undefined },
4662
+ managedAttemptTransaction?: ManagedAttemptTransaction,
4544
4663
  ): Promise<AssistantMessage> {
4545
4664
  const managedDegradedFieldDiagnostics = new Set<string>();
4546
4665
  // Apply context transform if configured (AgentMessage[] → AgentMessage[])
@@ -4983,7 +5102,7 @@ async function streamAssistantResponse(
4983
5102
  const finalMessage = config.fallbackManaged
4984
5103
  ? managedAssistantShell(finished, config.model, managedDegradedFieldDiagnostics, true)
4985
5104
  : finished;
4986
- promoteTypedEmptyResponseStop(finalMessage);
5105
+ promoteEmptyResponseStop(finalMessage, finished, managedAttemptTransaction);
4987
5106
  if (promptPrefix) finalMessage.promptPrefix = promptPrefix;
4988
5107
  if (addedPartial) {
4989
5108
  context.messages[context.messages.length - 1] = finalMessage;
@@ -4997,6 +5116,13 @@ async function streamAssistantResponse(
4997
5116
  await finishChat(finalMessage);
4998
5117
  return finalMessage;
4999
5118
  }
5119
+ default: {
5120
+ const unexpectedEventType = (event as unknown as { type?: unknown }).type;
5121
+ if (config.fallbackManaged && typeof unexpectedEventType !== "string") {
5122
+ throw new ManagedAttemptSnapshotError("event.unknownType");
5123
+ }
5124
+ break;
5125
+ }
5000
5126
  }
5001
5127
  }
5002
5128
  } finally {
@@ -5008,6 +5134,7 @@ async function streamAssistantResponse(
5008
5134
  const trailing = config.fallbackManaged
5009
5135
  ? managedAssistantShell(finished, config.model, managedDegradedFieldDiagnostics, true)
5010
5136
  : finished;
5137
+ promoteEmptyResponseStop(trailing, finished, managedAttemptTransaction);
5011
5138
  if (promptPrefix) trailing.promptPrefix = promptPrefix;
5012
5139
  await finishChat(trailing);
5013
5140
  return trailing;