effect-agent 0.1.0-beta.103 → 0.1.0-beta.106

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 (166) hide show
  1. package/dist/{Agent-CM6JubNX.d.mts → Agent-Bk-mLtov.d.mts} +2 -2
  2. package/dist/{AgentError-CggcAQ7V.d.mts → AgentError-Bn1ytXRD.d.mts} +20 -4
  3. package/dist/{AgentRuntime-B1SXMVN7.d.mts → AgentRuntime-BEXToYl1.d.mts} +8 -8
  4. package/dist/{AgentRuntime-BWOnNc0d.mjs → AgentRuntime-e2M5UoXn.mjs} +4 -3
  5. package/dist/AgentRuntime-e2M5UoXn.mjs.map +1 -0
  6. package/dist/{DurableAgentRuntime-C9eRZS9N.mjs → DurableAgentRuntime-wd1x0TcB.mjs} +67 -36
  7. package/dist/DurableAgentRuntime-wd1x0TcB.mjs.map +1 -0
  8. package/dist/{EventSource-K7XUzurY.d.mts → EventSource-pH-v0k42.d.mts} +3 -3
  9. package/dist/FailureDiagnostic-BElLOoED.mjs +274 -0
  10. package/dist/FailureDiagnostic-BElLOoED.mjs.map +1 -0
  11. package/dist/FailureDiagnostic-CaAkzGHL.d.mts +223 -0
  12. package/dist/{Messaging-QMYhp297.d.mts → Messaging-Bl4SBKpR.d.mts} +3 -3
  13. package/dist/{MessagingHost-gAxs5gme.d.mts → MessagingHost-nwBMdRoh.d.mts} +3 -3
  14. package/dist/{Output-CjocJzsl.d.mts → Output-B6RQ5jRU.d.mts} +2 -2
  15. package/dist/{PreparedInputAdmission-KLk5rGFI.d.mts → PreparedInputAdmission-D3-BgmA9.d.mts} +8 -4
  16. package/dist/{Records-Bidb7ByL.d.mts → Records-B4S02MX1.d.mts} +201 -14
  17. package/dist/{RunJournal-srpbEBEy.mjs → RunJournal-BdIlgAEj.mjs} +2 -2
  18. package/dist/{RunJournal-srpbEBEy.mjs.map → RunJournal-BdIlgAEj.mjs.map} +1 -1
  19. package/dist/{RunOptions-Bruk54SE.d.mts → RunOptions-qOcPv08p.d.mts} +10 -5
  20. package/dist/{Schedule-Cy9up8Uf.d.mts → Schedule-D2TVEu3W.d.mts} +7 -2
  21. package/dist/{Scheduling-CHCsSCGo.mjs → Scheduling-DPb2QyAK.mjs} +2 -2
  22. package/dist/{Scheduling-CHCsSCGo.mjs.map → Scheduling-DPb2QyAK.mjs.map} +1 -1
  23. package/dist/{Subagent-DP0CzO16.d.mts → Subagent-BxuDuCHt.d.mts} +172 -8
  24. package/dist/{Subagent-C2s8PFrd.mjs → Subagent-uXWHXiKb.mjs} +64 -29
  25. package/dist/Subagent-uXWHXiKb.mjs.map +1 -0
  26. package/dist/{SubagentHost-CYcxkpRc.d.mts → SubagentHost-DEPw4YME.d.mts} +6 -3
  27. package/dist/{Subscription-CHyoQelH.d.mts → Subscription-nX0rz4GQ.d.mts} +3 -3
  28. package/dist/{SubscriptionInput-DqrHW-Ja.d.mts → SubscriptionInput-CAG3_pyJ.d.mts} +3 -3
  29. package/dist/{Worker--j87JI__.d.mts → Worker-Bbj9uq5H.d.mts} +5 -2
  30. package/dist/{agent-registration-ByeHejH3.mjs → agent-registration-B5CI-Ygk.mjs} +7 -5
  31. package/dist/agent-registration-B5CI-Ygk.mjs.map +1 -0
  32. package/dist/capabilities/Commands.d.mts +1 -1
  33. package/dist/capabilities/Messaging.d.mts +2 -2
  34. package/dist/capabilities/RunHooks.d.mts +1 -1
  35. package/dist/capabilities/Subagent.d.mts +2 -2
  36. package/dist/capabilities/Subagent.mjs +1 -1
  37. package/dist/core/Agent.d.mts +1 -1
  38. package/dist/core/AgentError.d.mts +2 -2
  39. package/dist/core/AgentError.mjs +19 -2
  40. package/dist/core/AgentError.mjs.map +1 -1
  41. package/dist/core/FailureDiagnostic.d.mts +2 -0
  42. package/dist/core/FailureDiagnostic.mjs +2 -0
  43. package/dist/core/Worker.d.mts +1 -1
  44. package/dist/core/Worker.mjs +4 -1
  45. package/dist/core/Worker.mjs.map +1 -1
  46. package/dist/durable/ActivityStore.d.mts +1 -1
  47. package/dist/durable/ActivityStore.mjs +1 -1
  48. package/dist/durable/Admin.d.mts +1 -1
  49. package/dist/durable/Admin.mjs +1 -1
  50. package/dist/durable/AgentRegistration.d.mts +1 -1
  51. package/dist/durable/AgentRegistration.mjs +1 -1
  52. package/dist/durable/Certification.d.mts +1 -1
  53. package/dist/durable/CommittedActivity.d.mts +1 -1
  54. package/dist/durable/CommittedActivity.mjs +1 -1
  55. package/dist/durable/Digest.d.mts +1 -1
  56. package/dist/durable/Digest.mjs +7 -7
  57. package/dist/durable/Digest.mjs.map +1 -1
  58. package/dist/durable/DurableAgentRuntime.d.mts +1 -1
  59. package/dist/durable/DurableAgentRuntime.mjs +1 -1
  60. package/dist/durable/EventSource.d.mts +1 -1
  61. package/dist/durable/EventSource.mjs +1 -1
  62. package/dist/durable/GitHubWorkflowSource.d.mts +3 -3
  63. package/dist/durable/GitHubWorkflowSource.mjs +1 -1
  64. package/dist/durable/MessageDelivery.d.mts +15 -3
  65. package/dist/durable/MessageDelivery.mjs +44 -24
  66. package/dist/durable/MessageDelivery.mjs.map +1 -1
  67. package/dist/durable/MessageDeliveryStoreConformance.d.mts +4 -1
  68. package/dist/durable/MessageDeliveryStoreConformance.mjs +1 -1
  69. package/dist/durable/PersistentHistory.d.mts +1 -1
  70. package/dist/durable/PersistentHistory.mjs +2 -2
  71. package/dist/durable/PreparedInputAdmission.d.mts +1 -1
  72. package/dist/durable/PreparedInputAdmission.mjs +8 -4
  73. package/dist/durable/PreparedInputAdmission.mjs.map +1 -1
  74. package/dist/durable/Records.d.mts +1 -1
  75. package/dist/durable/Records.mjs +831 -2
  76. package/dist/durable/Records.mjs.map +1 -0
  77. package/dist/durable/Recovery.d.mts +1 -1
  78. package/dist/durable/Recovery.mjs +1 -1
  79. package/dist/durable/RunJournal.d.mts +1 -1
  80. package/dist/durable/RunJournal.mjs +1 -1
  81. package/dist/durable/Schedule.d.mts +1 -1
  82. package/dist/durable/Schedule.mjs +12 -3
  83. package/dist/durable/Schedule.mjs.map +1 -1
  84. package/dist/durable/ScheduleStoreConformance.d.mts +1 -1
  85. package/dist/durable/ScheduleStoreConformance.mjs +1 -1
  86. package/dist/durable/ScheduleTransition.d.mts +1 -1
  87. package/dist/durable/ScheduleTransition.mjs +1 -1
  88. package/dist/durable/Scheduling.d.mts +2 -2
  89. package/dist/durable/Scheduling.mjs +1 -1
  90. package/dist/durable/SqlMessageDeliveryStore.mjs +13 -11
  91. package/dist/durable/SqlMessageDeliveryStore.mjs.map +1 -1
  92. package/dist/durable/SqlStorageV2Upgrade.d.mts +2 -2
  93. package/dist/durable/SqlStorageV2Upgrade.mjs +1 -1
  94. package/dist/durable/SqlSubscriptionStore.d.mts +2 -2
  95. package/dist/durable/SqlSubscriptionStore.mjs +1 -1
  96. package/dist/durable/SqlThreadNativeReads.d.mts +1 -1
  97. package/dist/durable/SqlThreadNativeReads.mjs +2 -2
  98. package/dist/durable/SubmissionLedger.d.mts +1 -1
  99. package/dist/durable/SubmissionLedger.mjs +6 -3
  100. package/dist/durable/SubmissionLedger.mjs.map +1 -1
  101. package/dist/durable/SubmissionLedgerConformance.d.mts +1 -1
  102. package/dist/durable/SubmissionLedgerConformance.mjs +1 -1
  103. package/dist/durable/SubmissionStatus.d.mts +1 -1
  104. package/dist/durable/Subscription.d.mts +1 -1
  105. package/dist/durable/Subscription.mjs +1 -1
  106. package/dist/durable/SubscriptionInput.d.mts +1 -1
  107. package/dist/durable/SubscriptionInput.mjs +2 -2
  108. package/dist/durable/SubscriptionStoreConformance.d.mts +1 -1
  109. package/dist/durable/SubscriptionStoreConformance.mjs +1 -1
  110. package/dist/durable/SubscriptionTools.d.mts +3 -3
  111. package/dist/durable/SubscriptionTransition.d.mts +1 -1
  112. package/dist/durable/SubscriptionTransition.mjs +1 -1
  113. package/dist/durable/Subscriptions.d.mts +5 -5
  114. package/dist/durable/Subscriptions.mjs +1 -1
  115. package/dist/durable/ThreadContextHistory.d.mts +1 -1
  116. package/dist/durable/ThreadContextHistory.mjs +1 -1
  117. package/dist/durable/ThreadContextHistoryProjection.d.mts +1 -1
  118. package/dist/durable/ThreadContextHistoryProjection.mjs +1 -1
  119. package/dist/durable/ThreadInvariants.d.mts +1 -1
  120. package/dist/durable/ThreadInvariants.mjs +2 -2
  121. package/dist/durable/ThreadProjection.d.mts +1 -1
  122. package/dist/durable/ThreadProjection.mjs +1 -1
  123. package/dist/durable/ThreadProjectionMaintenance.d.mts +1 -1
  124. package/dist/durable/ThreadStore.d.mts +1 -1
  125. package/dist/durable/ThreadStore.mjs +2 -2
  126. package/dist/durable/ThreadStoreConformance.d.mts +1 -1
  127. package/dist/durable/ThreadStoreConformance.mjs +1 -1
  128. package/dist/durable/ToolReconciler.mjs +1 -1
  129. package/dist/durable/WorkerHost.d.mts +3 -3
  130. package/dist/engine/AgentRuntime.d.mts +1 -1
  131. package/dist/engine/AgentRuntime.mjs +1 -1
  132. package/dist/engine/MessagingHost.d.mts +1 -1
  133. package/dist/engine/Output.d.mts +1 -1
  134. package/dist/engine/RunOptions.d.mts +1 -1
  135. package/dist/engine/RunOptions.mjs +2 -0
  136. package/dist/engine/RunOptions.mjs.map +1 -1
  137. package/dist/engine/SubagentHost.d.mts +1 -1
  138. package/dist/engine/SubagentHost.mjs.map +1 -1
  139. package/dist/index.d.mts +18 -17
  140. package/dist/index.mjs +8 -7
  141. package/package.json +1 -1
  142. package/src/capabilities/internal/subagent-background.ts +66 -23
  143. package/src/capabilities/internal/subagent-contract.ts +4 -0
  144. package/src/core/AgentError.ts +20 -0
  145. package/src/core/FailureDiagnostic.ts +343 -0
  146. package/src/core/Worker.ts +4 -1
  147. package/src/durable/Digest.ts +22 -28
  148. package/src/durable/DurableAgentRuntime.ts +37 -13
  149. package/src/durable/MessageDelivery.ts +82 -24
  150. package/src/durable/PreparedInputAdmission.ts +17 -4
  151. package/src/durable/Records.ts +5 -15
  152. package/src/durable/Schedule.ts +11 -2
  153. package/src/durable/SqlMessageDeliveryStore.ts +21 -9
  154. package/src/durable/SubmissionLedger.ts +4 -1
  155. package/src/durable/internal/agent-registration.ts +46 -14
  156. package/src/durable/internal/worker-host.ts +32 -19
  157. package/src/engine/RunOptions.ts +11 -3
  158. package/src/engine/SubagentHost.ts +3 -0
  159. package/src/engine/internal/agent-runtime.ts +23 -16
  160. package/src/index.ts +1 -0
  161. package/dist/AgentRuntime-BWOnNc0d.mjs.map +0 -1
  162. package/dist/DurableAgentRuntime-C9eRZS9N.mjs.map +0 -1
  163. package/dist/Records-D8167fHn.mjs +0 -844
  164. package/dist/Records-D8167fHn.mjs.map +0 -1
  165. package/dist/Subagent-C2s8PFrd.mjs.map +0 -1
  166. package/dist/agent-registration-ByeHejH3.mjs.map +0 -1
@@ -122,10 +122,14 @@ import {
122
122
  } from "./agent-registration.ts";
123
123
  import { lastWorkerReportMessageId } from "./agent-updates.ts";
124
124
 
125
- const failure = (operation: WorkerError["operation"], reason: WorkerError["reason"]) =>
126
- WorkerError.make({ operation, reason });
125
+ const failure = (
126
+ operation: WorkerError["operation"],
127
+ reason: WorkerError["reason"],
128
+ cause?: unknown,
129
+ ) => WorkerError.make({ operation, reason, ...(cause === undefined ? {} : { cause }) });
127
130
 
128
- const storageFailure = (operation: WorkerError["operation"]) => () => failure(operation, "storage");
131
+ const storageFailure = (operation: WorkerError["operation"]) => (cause: unknown) =>
132
+ failure(operation, "storage", cause);
129
133
 
130
134
  const decode = <S extends Schema.Top>(
131
135
  schema: S,
@@ -133,7 +137,7 @@ const decode = <S extends Schema.Top>(
133
137
  operation: WorkerError["operation"],
134
138
  ) =>
135
139
  Schema.decodeUnknownEffect(Schema.toType(schema))(value).pipe(
136
- Effect.mapError(() => failure(operation, "corrupt")),
140
+ Effect.mapError((cause) => failure(operation, "corrupt", cause)),
137
141
  );
138
142
 
139
143
  const sameOrigin = Schema.toEquivalence(WorkerOrigin);
@@ -837,7 +841,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
837
841
  // framework message carries the new information independently of the current input codec.
838
842
  if (!retainedFrameworkInput)
839
843
  yield* Schema.decodeEffect(Schema.toEncoded(targetBinding.definition.input))(input).pipe(
840
- Effect.mapError(() => failure("start", "corrupt")),
844
+ Effect.mapError((cause) => failure("start", "corrupt", cause)),
841
845
  );
842
846
 
843
847
  const targetRequest = {
@@ -1239,7 +1243,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1239
1243
  },
1240
1244
  Effect.mapError((error) =>
1241
1245
  error.reason === "storage" || error.reason === "corrupt"
1242
- ? LedgerError.make({ operation: "worker-update", message: error.reason })
1246
+ ? LedgerError.make({ operation: "worker-update", message: error.reason, cause: error })
1243
1247
  : UpdateError.make({ reason: error.reason === "denied" ? "denied" : "unavailable" }),
1244
1248
  ),
1245
1249
  );
@@ -1400,6 +1404,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1400
1404
  outcome: host.outcome,
1401
1405
  encodedParameters: hostAdmission.parameters,
1402
1406
  encodedResult: host.result ?? null,
1407
+ ...(host.outcome === "failed" ? { diagnostic: host.result } : {}),
1403
1408
  budgetExhausted: host.finishReason === "budget-exhausted",
1404
1409
  },
1405
1410
  };
@@ -1589,7 +1594,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1589
1594
  if (Option.isNone(deps.deliveries)) return yield* failure("inspect", "unavailable");
1590
1595
 
1591
1596
  const envelope = yield* Schema.decodeUnknownEffect(PreparedInput)(decision.envelope).pipe(
1592
- Effect.mapError(() => failure("inspect", "corrupt")),
1597
+ Effect.mapError((cause) => failure("inspect", "corrupt", cause)),
1593
1598
  );
1594
1599
 
1595
1600
  const prepared = yield* withCrypto(
@@ -1716,7 +1721,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1716
1721
 
1717
1722
  const binding = (target: Agent.AnyDefinition, operation: WorkerError["operation"]) =>
1718
1723
  resolveDefinitionBinding(deps.bindings, target).pipe(
1719
- Effect.mapError(() => failure(operation, "declaration-unavailable")),
1724
+ Effect.mapError((cause) => failure(operation, "declaration-unavailable", cause)),
1720
1725
  );
1721
1726
 
1722
1727
  const preparedTarget = Effect.fn("WorkerHost.preparedTarget")(function* (
@@ -1726,7 +1731,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1726
1731
  const resolved = yield* binding(target, "start");
1727
1732
 
1728
1733
  yield* Schema.decodeUnknownEffect(Schema.toEncoded(target.input))(encodedInput).pipe(
1729
- Effect.mapError(() => failure("start", "corrupt")),
1734
+ Effect.mapError((cause) => failure("start", "corrupt", cause)),
1730
1735
  );
1731
1736
  const input = yield* decode(PersistedJson, encodedInput, "start");
1732
1737
 
@@ -1858,6 +1863,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1858
1863
  outcome: settlement.outcome,
1859
1864
  encodedParameters: admission.parameters,
1860
1865
  encodedResult: result.result ?? null,
1866
+ ...(result.outcome === "failed" ? { diagnostic: result.result } : {}),
1861
1867
  budgetExhausted: result.finishReason === "budget-exhausted",
1862
1868
  };
1863
1869
  });
@@ -1937,6 +1943,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1937
1943
  error._tag === "MessageDeliveryError" && error.reason === "capacity"
1938
1944
  ? "capacity"
1939
1945
  : "storage",
1946
+ error,
1940
1947
  ),
1941
1948
  ),
1942
1949
  );
@@ -1955,16 +1962,20 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1955
1962
  .pipe(
1956
1963
  Effect.mapError((error) =>
1957
1964
  error._tag === "AdmissionConflict" || error._tag === "AgentInputError"
1958
- ? ScheduledInputRefused.make({ code: error._tag })
1965
+ ? ScheduledInputRefused.make({ code: error._tag, cause: error })
1959
1966
  : error._tag === "AdmissionPolicyError" && error.reason === "refused"
1960
- ? ScheduledInputRefused.make({ code: error.code })
1961
- : ScheduledInputRetryable.make({ reason: "storage" }),
1967
+ ? ScheduledInputRefused.make({ code: error.code, cause: error })
1968
+ : ScheduledInputRetryable.make({ reason: "storage", cause: error }),
1962
1969
  ),
1963
1970
  ),
1964
1971
  submissionStatus: (receipt) =>
1965
1972
  deps
1966
1973
  .status(receipt)
1967
- .pipe(Effect.mapError(() => ScheduledInputRetryable.make({ reason: "storage" }))),
1974
+ .pipe(
1975
+ Effect.mapError((error) =>
1976
+ ScheduledInputRetryable.make({ reason: "storage", cause: error }),
1977
+ ),
1978
+ ),
1968
1979
  }),
1969
1980
  Effect.provideService(Crypto.Crypto, deps.crypto),
1970
1981
  Effect.mapError(storageFailure(operation)),
@@ -1985,6 +1996,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1985
1996
  return yield* failure(
1986
1997
  operation,
1987
1998
  Option.getOrElse(reason, () => "denied"),
1999
+ processed.lastFailureDiagnostic,
1988
2000
  );
1989
2001
  }
1990
2002
 
@@ -1995,6 +2007,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
1995
2007
  processed.status === "pending" && processed.retry.lastFailure === null
1996
2008
  ? "delivery-pending"
1997
2009
  : "storage",
2010
+ processed.lastFailureDiagnostic,
1998
2011
  );
1999
2012
  });
2000
2013
 
@@ -2093,7 +2106,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2093
2106
  const sourcePolicy = Option.getOrElse(prepared.source.policyOverride, () => context.policy);
2094
2107
 
2095
2108
  const grant = yield* Schema.decodeUnknownEffect(SubagentGrant)(request.encodedGrant).pipe(
2096
- Effect.mapError(() => failure("start", "corrupt")),
2109
+ Effect.mapError((cause) => failure("start", "corrupt", cause)),
2097
2110
  );
2098
2111
 
2099
2112
  const messageId = yield* messageIdFor([
@@ -2196,7 +2209,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2196
2209
 
2197
2210
  yield* Schema.decodeUnknownEffect(Schema.toEncoded(request.target.input))(
2198
2211
  request.encodedInput,
2199
- ).pipe(Effect.mapError(() => failure("followUp", "corrupt")));
2212
+ ).pipe(Effect.mapError((cause) => failure("followUp", "corrupt", cause)));
2200
2213
 
2201
2214
  const messageId = yield* messageIdFor([
2202
2215
  context.source.threadId,
@@ -2282,7 +2295,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2282
2295
  record,
2283
2296
  }),
2284
2297
  ),
2285
- Effect.mapError(() => failure("observe", "corrupt")),
2298
+ Effect.mapError((cause) => failure("observe", "corrupt", cause)),
2286
2299
  ),
2287
2300
  );
2288
2301
 
@@ -2358,7 +2371,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2358
2371
  )
2359
2372
  .pipe(
2360
2373
  Effect.mapError((error) =>
2361
- error._tag === "JoinedToHost" ? error : failure("cancel", "storage"),
2374
+ error._tag === "JoinedToHost" ? error : failure("cancel", "storage", error),
2362
2375
  ),
2363
2376
  );
2364
2377
  }),
@@ -2372,7 +2385,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2372
2385
  inputDigest: Digest,
2373
2386
  ) {
2374
2387
  const message = yield* Schema.decodeEffect(FrameworkMessage)(unvalidated).pipe(
2375
- Effect.mapError(() => failure("followUp", "corrupt")),
2388
+ Effect.mapError((cause) => failure("followUp", "corrupt", cause)),
2376
2389
  );
2377
2390
 
2378
2391
  const childThreadId =
@@ -2409,7 +2422,7 @@ export const makeWorkerRuntime = Effect.fn("WorkerHost.make")(function* (
2409
2422
  if (frozen === undefined) return yield* failure("followUp", "denied");
2410
2423
 
2411
2424
  const envelope = yield* Schema.decodeUnknownEffect(PreparedInput)(frozen).pipe(
2412
- Effect.mapError(() => failure("followUp", "corrupt")),
2425
+ Effect.mapError((cause) => failure("followUp", "corrupt", cause)),
2413
2426
  );
2414
2427
 
2415
2428
  if (
@@ -2,7 +2,7 @@ import { type Cause, Effect, Context, type DateTime, Layer, Schema } from "effec
2
2
  import type { LanguageModel, Model, Prompt, Response } from "effect/unstable/ai";
3
3
 
4
4
  import { type AnyDefinition } from "../core/Agent.ts";
5
- import { type AgentInputError } from "../core/AgentError.ts";
5
+ import { type AgentInputError, type AgentToolAuthorizationCheckError } from "../core/AgentError.ts";
6
6
  import { type AgentPolicy } from "../core/AgentPolicy.ts";
7
7
  import type { Update, UpdateError } from "../core/AgentUpdates.ts";
8
8
  import {
@@ -396,7 +396,13 @@ export interface RunToolCallDescriptor {
396
396
  /** Decision returned by a host's action-time Tool authorization policy. */
397
397
  export type RunToolAuthorizationDecision =
398
398
  | { readonly _tag: "allowed" }
399
- | { readonly _tag: "denied"; readonly reason: string };
399
+ | {
400
+ readonly _tag: "denied";
401
+ /** Safe model-facing explanation. */
402
+ readonly reason: string;
403
+ /** Original local policy evidence; retained privately at native Run settlement. */
404
+ readonly cause?: Cause.Cause<unknown>;
405
+ };
400
406
 
401
407
  /**
402
408
  * Exact authority presented before one application Tool Handler may start.
@@ -459,13 +465,15 @@ export const RunContextPreparationPassthrough: Layer.Layer<RunContextPreparation
459
465
  /**
460
466
  * Host action-time authority for native and programmatic application Tools. Implementations close over
461
467
  * their dependencies at Layer construction and return a denial when execution is not authorized.
468
+ * A dependency or validation failure instead fails with AgentToolAuthorizationCheckError and
469
+ * retains its original Cause. Defects and interruption remain in the Effect Cause channel.
462
470
  * Durable coordinators capture this service once and retain it across replacement Attempts.
463
471
  * Ephemeral Runs also resolve this service at their Run boundary. A typed per-run
464
472
  * `RunOptions.toolAuthorization` overrides it while retaining its own error and requirement channel.
465
473
  */
466
474
  export class RunToolAuthorization extends Context.Service<
467
475
  RunToolAuthorization,
468
- RunToolAuthorizationHook
476
+ RunToolAuthorizationHook<AgentToolAuthorizationCheckError>
469
477
  >()("@effect-agent/engine/RunToolAuthorization") {
470
478
  /** Explicit compatibility policy: Tool execution requires no additional host authorization. */
471
479
  static readonly allowAll: Layer.Layer<RunToolAuthorization> = Layer.succeed(
@@ -3,6 +3,7 @@ import { Context, Schema, Stream } from "effect";
3
3
 
4
4
  import type * as Agent from "../core/Agent.ts";
5
5
  import type { AgentPolicy } from "../core/AgentPolicy.ts";
6
+ import type * as FailureDiagnostic from "../core/FailureDiagnostic.ts";
6
7
  import type { DelegationId, RunId, SettlementId, ThreadId } from "../core/Identifiers.ts";
7
8
  import { type IdempotencyKey, type JoinedToHost, type Receipt } from "../core/Receipt.ts";
8
9
  import type { SubagentBudgetReservation } from "../core/SubagentContract.ts";
@@ -63,6 +64,8 @@ export type WorkerObservation =
63
64
  readonly outcome: "completed" | "failed" | "aborted";
64
65
  readonly encodedParameters: unknown;
65
66
  readonly encodedResult: unknown;
67
+ /** Operator-private failed-settlement evidence; never part of a model Tool result. */
68
+ readonly diagnostic?: FailureDiagnostic.Failure;
66
69
  readonly budgetExhausted: boolean;
67
70
  };
68
71
 
@@ -41,6 +41,7 @@ import {
41
41
  AgentOutputError,
42
42
  AgentRunDispositionError,
43
43
  AgentToolAuthorizationDenied,
44
+ type AgentToolAuthorizationCheckError,
44
45
  AgentPolicyError,
45
46
  ContextBudgetError,
46
47
  ContextOverflowError,
@@ -433,6 +434,7 @@ export type AgentRuntimeFailure<
433
434
  | ContextOverflowError
434
435
  | CompactionError
435
436
  | RunContextPreparationError
437
+ | AgentToolAuthorizationCheckError
436
438
  | ModelProtocolError
437
439
  | AgentApprovalDenied
438
440
  | AgentToolAuthorizationDenied
@@ -941,7 +943,7 @@ const modelResponseCodecFor = (toolkit: Toolkit.Any) => {
941
943
  return codec;
942
944
  };
943
945
 
944
- const ownModelResponsePartGeneral = Effect.fn("AgentRuntime.ownModelResponsePart")(function* <
946
+ const ownModelResponsePartGeneral = Effect.fnUntraced(function* <
945
947
  Tools extends Record<string, Tool.Any>,
946
948
  >(
947
949
  part: unknown,
@@ -1782,20 +1784,21 @@ const preflightToolAuthorization = <HookError, HookRequirements>(
1782
1784
  HookError | ModelProtocolError | AgentToolAuthorizationDenied,
1783
1785
  HookRequirements
1784
1786
  > => {
1785
- const authorization = isSubagentToolAllowed(
1786
- options.subagentGrant,
1787
- options.delegationDepth ?? options.parentLink?.depth ?? 0,
1788
- call.toolName,
1789
- annotations,
1790
- )
1791
- ? options.toolAuthorization
1792
- : {
1793
- authorize: () =>
1794
- Effect.succeed({
1795
- _tag: "denied" as const,
1796
- reason: "Tool exceeds the inherited subagent grant",
1797
- }),
1798
- };
1787
+ const authorization: RunToolAuthorizationHook<HookError, HookRequirements> | undefined =
1788
+ isSubagentToolAllowed(
1789
+ options.subagentGrant,
1790
+ options.delegationDepth ?? options.parentLink?.depth ?? 0,
1791
+ call.toolName,
1792
+ annotations,
1793
+ )
1794
+ ? options.toolAuthorization
1795
+ : {
1796
+ authorize: () =>
1797
+ Effect.succeed({
1798
+ _tag: "denied" as const,
1799
+ reason: "Tool exceeds the inherited subagent grant",
1800
+ }),
1801
+ };
1799
1802
 
1800
1803
  if (authorization === undefined) return Stream.empty;
1801
1804
 
@@ -1817,6 +1820,7 @@ const preflightToolAuthorization = <HookError, HookRequirements>(
1817
1820
  toolCallId: call.toolCallId,
1818
1821
  toolName: call.toolName,
1819
1822
  message: decision.reason,
1823
+ ...(decision.cause === undefined ? {} : { cause: decision.cause }),
1820
1824
  });
1821
1825
 
1822
1826
  return Stream.fromEffect(
@@ -7848,7 +7852,10 @@ function streamWithCompletion<
7848
7852
  }
7849
7853
 
7850
7854
  const options: RunOptions<
7851
- HookError | ThreadHistoryError | RunContextPreparationError,
7855
+ | HookError
7856
+ | ThreadHistoryError
7857
+ | RunContextPreparationError
7858
+ | AgentToolAuthorizationCheckError,
7852
7859
  HookRequirements
7853
7860
  > = {
7854
7861
  ...runOptions,
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export * as ContextHistory from "./engine/ContextHistory.ts";
14
14
  export * as ContextTools from "./capabilities/ContextTools.ts";
15
15
  export * as ContextWindow from "./engine/ContextWindow.ts";
16
16
  export * as DurableStep from "./engine/DurableStep.ts";
17
+ export * as FailureDiagnostic from "./core/FailureDiagnostic.ts";
17
18
  export * as InMemory from "./InMemory.ts";
18
19
  export * as Thread from "./core/Thread.ts";
19
20
  export * as IdGenerator from "./core/IdGenerator.ts";