@smthrs/kernel 0.0.0-stage → 1.0.0-rc.3

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 (267) hide show
  1. package/CHANGELOG.md +206 -0
  2. package/LICENSE +21 -0
  3. package/README.md +154 -2
  4. package/dist/cjs/CapabilitySet.d.ts +6 -0
  5. package/dist/cjs/CapabilitySet.d.ts.map +1 -0
  6. package/dist/cjs/CapabilitySet.js +19 -0
  7. package/dist/cjs/CapabilitySet.js.map +7 -0
  8. package/dist/cjs/ChildProcessEnvironment.d.ts +60 -0
  9. package/dist/cjs/ChildProcessEnvironment.d.ts.map +1 -0
  10. package/dist/cjs/ChildProcessEnvironment.js +61 -0
  11. package/dist/cjs/ChildProcessEnvironment.js.map +7 -0
  12. package/dist/cjs/ChildProcessSpawner.d.ts +100 -0
  13. package/dist/cjs/ChildProcessSpawner.d.ts.map +1 -0
  14. package/dist/cjs/ChildProcessSpawner.js +291 -0
  15. package/dist/cjs/ChildProcessSpawner.js.map +7 -0
  16. package/dist/cjs/CommandLine.d.ts +173 -0
  17. package/dist/cjs/CommandLine.d.ts.map +1 -0
  18. package/dist/cjs/CommandLine.js +54 -0
  19. package/dist/cjs/CommandLine.js.map +7 -0
  20. package/dist/cjs/ContainedSpawner.d.ts +135 -0
  21. package/dist/cjs/ContainedSpawner.d.ts.map +1 -0
  22. package/dist/cjs/ContainedSpawner.js +141 -0
  23. package/dist/cjs/ContainedSpawner.js.map +7 -0
  24. package/dist/cjs/FileSystem.d.ts +452 -0
  25. package/dist/cjs/FileSystem.d.ts.map +1 -0
  26. package/dist/cjs/FileSystem.js +890 -0
  27. package/dist/cjs/FileSystem.js.map +7 -0
  28. package/dist/cjs/FileSystemBatch.d.ts +94 -0
  29. package/dist/cjs/FileSystemBatch.d.ts.map +1 -0
  30. package/dist/cjs/FileSystemBatch.js +31 -0
  31. package/dist/cjs/FileSystemBatch.js.map +7 -0
  32. package/dist/cjs/GrantEvent.d.ts +240 -0
  33. package/dist/cjs/GrantEvent.d.ts.map +1 -0
  34. package/dist/cjs/GrantEvent.js +115 -0
  35. package/dist/cjs/GrantEvent.js.map +7 -0
  36. package/dist/cjs/GrantStore.d.ts +270 -0
  37. package/dist/cjs/GrantStore.d.ts.map +1 -0
  38. package/dist/cjs/GrantStore.js +857 -0
  39. package/dist/cjs/GrantStore.js.map +7 -0
  40. package/dist/cjs/HostError.d.ts +36 -0
  41. package/dist/cjs/HostError.d.ts.map +1 -0
  42. package/dist/cjs/HostError.js +17 -0
  43. package/dist/cjs/HostError.js.map +7 -0
  44. package/dist/cjs/HostServices.d.ts +101 -0
  45. package/dist/cjs/HostServices.d.ts.map +1 -0
  46. package/dist/cjs/HostServices.js +66 -0
  47. package/dist/cjs/HostServices.js.map +7 -0
  48. package/dist/cjs/HttpClient.d.ts +177 -0
  49. package/dist/cjs/HttpClient.d.ts.map +1 -0
  50. package/dist/cjs/HttpClient.js +217 -0
  51. package/dist/cjs/HttpClient.js.map +7 -0
  52. package/dist/cjs/Jj.d.ts +67 -0
  53. package/dist/cjs/Jj.d.ts.map +1 -0
  54. package/dist/cjs/Jj.js +118 -0
  55. package/dist/cjs/Jj.js.map +7 -0
  56. package/dist/cjs/JournalGrantStore.d.ts +65 -0
  57. package/dist/cjs/JournalGrantStore.d.ts.map +1 -0
  58. package/dist/cjs/JournalGrantStore.js +354 -0
  59. package/dist/cjs/JournalGrantStore.js.map +7 -0
  60. package/dist/cjs/Path.d.ts +37 -0
  61. package/dist/cjs/Path.d.ts.map +1 -0
  62. package/dist/cjs/Path.js +46 -0
  63. package/dist/cjs/Path.js.map +7 -0
  64. package/dist/cjs/ProcessConfinement.d.ts +55 -0
  65. package/dist/cjs/ProcessConfinement.d.ts.map +1 -0
  66. package/dist/cjs/ProcessConfinement.js +94 -0
  67. package/dist/cjs/ProcessConfinement.js.map +7 -0
  68. package/dist/cjs/ProcessLedger.d.ts +260 -0
  69. package/dist/cjs/ProcessLedger.d.ts.map +1 -0
  70. package/dist/cjs/ProcessLedger.js +190 -0
  71. package/dist/cjs/ProcessLedger.js.map +7 -0
  72. package/dist/cjs/Rooted.d.ts +53 -0
  73. package/dist/cjs/Rooted.d.ts.map +1 -0
  74. package/dist/cjs/Rooted.js +101 -0
  75. package/dist/cjs/Rooted.js.map +7 -0
  76. package/dist/cjs/Unreachable.d.ts +27 -0
  77. package/dist/cjs/Unreachable.d.ts.map +1 -0
  78. package/dist/cjs/Unreachable.js +43 -0
  79. package/dist/cjs/Unreachable.js.map +7 -0
  80. package/dist/cjs/Workspace.d.ts +62 -0
  81. package/dist/cjs/Workspace.d.ts.map +1 -0
  82. package/dist/cjs/Workspace.js +35 -0
  83. package/dist/cjs/Workspace.js.map +7 -0
  84. package/dist/cjs/index.d.ts +161 -0
  85. package/dist/cjs/index.d.ts.map +1 -0
  86. package/dist/cjs/index.js +73 -0
  87. package/dist/cjs/index.js.map +7 -0
  88. package/dist/cjs/internal/Containment.d.ts +24 -0
  89. package/dist/cjs/internal/Containment.d.ts.map +1 -0
  90. package/dist/cjs/internal/Containment.js +30 -0
  91. package/dist/cjs/internal/Containment.js.map +7 -0
  92. package/dist/cjs/internal/Pipeline.d.ts +18 -0
  93. package/dist/cjs/internal/Pipeline.d.ts.map +1 -0
  94. package/dist/cjs/internal/Pipeline.js +90 -0
  95. package/dist/cjs/internal/Pipeline.js.map +7 -0
  96. package/dist/cjs/internal/makeCapability.d.ts +16 -0
  97. package/dist/cjs/internal/makeCapability.d.ts.map +1 -0
  98. package/dist/cjs/internal/makeCapability.js +44 -0
  99. package/dist/cjs/internal/makeCapability.js.map +7 -0
  100. package/dist/cjs/package.json +1 -0
  101. package/dist/cjs/test/FileSystemBatchContract.d.ts +15 -0
  102. package/dist/cjs/test/FileSystemBatchContract.d.ts.map +1 -0
  103. package/dist/cjs/test/FileSystemBatchContract.js +76 -0
  104. package/dist/cjs/test/FileSystemBatchContract.js.map +7 -0
  105. package/dist/cjs/test/HostContract.d.ts +225 -0
  106. package/dist/cjs/test/HostContract.d.ts.map +1 -0
  107. package/dist/cjs/test/HostContract.js +489 -0
  108. package/dist/cjs/test/HostContract.js.map +7 -0
  109. package/dist/cjs/test/TestGrantStore.d.ts +31 -0
  110. package/dist/cjs/test/TestGrantStore.d.ts.map +1 -0
  111. package/dist/cjs/test/TestGrantStore.js +65 -0
  112. package/dist/cjs/test/TestGrantStore.js.map +7 -0
  113. package/dist/esm/CapabilitySet.d.ts +6 -0
  114. package/dist/esm/CapabilitySet.d.ts.map +1 -0
  115. package/dist/esm/CapabilitySet.js +6 -0
  116. package/dist/esm/CapabilitySet.js.map +1 -0
  117. package/dist/esm/ChildProcessEnvironment.d.ts +60 -0
  118. package/dist/esm/ChildProcessEnvironment.d.ts.map +1 -0
  119. package/dist/esm/ChildProcessEnvironment.js +91 -0
  120. package/dist/esm/ChildProcessEnvironment.js.map +1 -0
  121. package/dist/esm/ChildProcessSpawner.d.ts +100 -0
  122. package/dist/esm/ChildProcessSpawner.d.ts.map +1 -0
  123. package/dist/esm/ChildProcessSpawner.js +342 -0
  124. package/dist/esm/ChildProcessSpawner.js.map +1 -0
  125. package/dist/esm/CommandLine.d.ts +173 -0
  126. package/dist/esm/CommandLine.d.ts.map +1 -0
  127. package/dist/esm/CommandLine.js +195 -0
  128. package/dist/esm/CommandLine.js.map +1 -0
  129. package/dist/esm/ContainedSpawner.d.ts +135 -0
  130. package/dist/esm/ContainedSpawner.d.ts.map +1 -0
  131. package/dist/esm/ContainedSpawner.js +180 -0
  132. package/dist/esm/ContainedSpawner.js.map +1 -0
  133. package/dist/esm/FileSystem.d.ts +452 -0
  134. package/dist/esm/FileSystem.d.ts.map +1 -0
  135. package/dist/esm/FileSystem.js +989 -0
  136. package/dist/esm/FileSystem.js.map +1 -0
  137. package/dist/esm/FileSystemBatch.d.ts +94 -0
  138. package/dist/esm/FileSystemBatch.d.ts.map +1 -0
  139. package/dist/esm/FileSystemBatch.js +26 -0
  140. package/dist/esm/FileSystemBatch.js.map +1 -0
  141. package/dist/esm/GrantEvent.d.ts +240 -0
  142. package/dist/esm/GrantEvent.d.ts.map +1 -0
  143. package/dist/esm/GrantEvent.js +155 -0
  144. package/dist/esm/GrantEvent.js.map +1 -0
  145. package/dist/esm/GrantStore.d.ts +270 -0
  146. package/dist/esm/GrantStore.d.ts.map +1 -0
  147. package/dist/esm/GrantStore.js +951 -0
  148. package/dist/esm/GrantStore.js.map +1 -0
  149. package/dist/esm/HostError.d.ts +36 -0
  150. package/dist/esm/HostError.d.ts.map +1 -0
  151. package/dist/esm/HostError.js +20 -0
  152. package/dist/esm/HostError.js.map +1 -0
  153. package/dist/esm/HostServices.d.ts +101 -0
  154. package/dist/esm/HostServices.d.ts.map +1 -0
  155. package/dist/esm/HostServices.js +76 -0
  156. package/dist/esm/HostServices.js.map +1 -0
  157. package/dist/esm/HttpClient.d.ts +177 -0
  158. package/dist/esm/HttpClient.d.ts.map +1 -0
  159. package/dist/esm/HttpClient.js +299 -0
  160. package/dist/esm/HttpClient.js.map +1 -0
  161. package/dist/esm/Jj.d.ts +67 -0
  162. package/dist/esm/Jj.d.ts.map +1 -0
  163. package/dist/esm/Jj.js +111 -0
  164. package/dist/esm/Jj.js.map +1 -0
  165. package/dist/esm/JournalGrantStore.d.ts +65 -0
  166. package/dist/esm/JournalGrantStore.d.ts.map +1 -0
  167. package/dist/esm/JournalGrantStore.js +350 -0
  168. package/dist/esm/JournalGrantStore.js.map +1 -0
  169. package/dist/esm/Path.d.ts +37 -0
  170. package/dist/esm/Path.d.ts.map +1 -0
  171. package/dist/esm/Path.js +34 -0
  172. package/dist/esm/Path.js.map +1 -0
  173. package/dist/esm/ProcessConfinement.d.ts +55 -0
  174. package/dist/esm/ProcessConfinement.d.ts.map +1 -0
  175. package/dist/esm/ProcessConfinement.js +106 -0
  176. package/dist/esm/ProcessConfinement.js.map +1 -0
  177. package/dist/esm/ProcessLedger.d.ts +260 -0
  178. package/dist/esm/ProcessLedger.d.ts.map +1 -0
  179. package/dist/esm/ProcessLedger.js +283 -0
  180. package/dist/esm/ProcessLedger.js.map +1 -0
  181. package/dist/esm/Rooted.d.ts +53 -0
  182. package/dist/esm/Rooted.d.ts.map +1 -0
  183. package/dist/esm/Rooted.js +104 -0
  184. package/dist/esm/Rooted.js.map +1 -0
  185. package/dist/esm/Unreachable.d.ts +27 -0
  186. package/dist/esm/Unreachable.d.ts.map +1 -0
  187. package/dist/esm/Unreachable.js +26 -0
  188. package/dist/esm/Unreachable.js.map +1 -0
  189. package/dist/esm/Workspace.d.ts +62 -0
  190. package/dist/esm/Workspace.d.ts.map +1 -0
  191. package/dist/esm/Workspace.js +51 -0
  192. package/dist/esm/Workspace.js.map +1 -0
  193. package/dist/esm/index.d.ts +161 -0
  194. package/dist/esm/index.d.ts.map +1 -0
  195. package/dist/esm/index.js +151 -0
  196. package/dist/esm/index.js.map +1 -0
  197. package/dist/esm/internal/Containment.d.ts +24 -0
  198. package/dist/esm/internal/Containment.d.ts.map +1 -0
  199. package/dist/esm/internal/Containment.js +27 -0
  200. package/dist/esm/internal/Containment.js.map +1 -0
  201. package/dist/esm/internal/Pipeline.d.ts +18 -0
  202. package/dist/esm/internal/Pipeline.d.ts.map +1 -0
  203. package/dist/esm/internal/Pipeline.js +68 -0
  204. package/dist/esm/internal/Pipeline.js.map +1 -0
  205. package/dist/esm/internal/makeCapability.d.ts +16 -0
  206. package/dist/esm/internal/makeCapability.d.ts.map +1 -0
  207. package/dist/esm/internal/makeCapability.js +22 -0
  208. package/dist/esm/internal/makeCapability.js.map +1 -0
  209. package/dist/esm/test/FileSystemBatchContract.d.ts +15 -0
  210. package/dist/esm/test/FileSystemBatchContract.d.ts.map +1 -0
  211. package/dist/esm/test/FileSystemBatchContract.js +57 -0
  212. package/dist/esm/test/FileSystemBatchContract.js.map +1 -0
  213. package/dist/esm/test/HostContract.d.ts +225 -0
  214. package/dist/esm/test/HostContract.d.ts.map +1 -0
  215. package/dist/esm/test/HostContract.js +460 -0
  216. package/dist/esm/test/HostContract.js.map +1 -0
  217. package/dist/esm/test/TestGrantStore.d.ts +31 -0
  218. package/dist/esm/test/TestGrantStore.d.ts.map +1 -0
  219. package/dist/esm/test/TestGrantStore.js +57 -0
  220. package/dist/esm/test/TestGrantStore.js.map +1 -0
  221. package/docs/README.md +162 -0
  222. package/docs/api.md +1532 -0
  223. package/docs/concepts/decoration-in-place.md +117 -0
  224. package/docs/concepts/filesystem-batches.md +58 -0
  225. package/docs/concepts/filesystem-confinement.md +168 -0
  226. package/docs/concepts/grant-decisions.md +181 -0
  227. package/docs/concepts/process-containment.md +155 -0
  228. package/docs/contract.md +14 -0
  229. package/docs/guides/adapt-a-new-host-platform.md +148 -0
  230. package/docs/guides/answer-permission-requests.md +151 -0
  231. package/docs/guides/authorize-network-and-model-calls.md +118 -0
  232. package/docs/guides/contain-spawned-processes.md +118 -0
  233. package/docs/guides/guard-a-host-bundle.md +109 -0
  234. package/docs/guides/persist-grants-across-restarts.md +135 -0
  235. package/docs/guides/write-a-capability-policy.md +159 -0
  236. package/docs/installation.md +115 -0
  237. package/docs/quickstart.md +182 -0
  238. package/docs/testing.md +108 -0
  239. package/docs/troubleshooting.md +281 -0
  240. package/package.json +344 -3
  241. package/src/CapabilitySet.ts +6 -0
  242. package/src/ChildProcessEnvironment.ts +98 -0
  243. package/src/ChildProcessSpawner.ts +414 -0
  244. package/src/CommandLine.ts +250 -0
  245. package/src/ContainedSpawner.ts +284 -0
  246. package/src/FileSystem.ts +1655 -0
  247. package/src/FileSystemBatch.ts +90 -0
  248. package/src/GrantEvent.ts +174 -0
  249. package/src/GrantStore.ts +1345 -0
  250. package/src/HostError.ts +37 -0
  251. package/src/HostServices.ts +133 -0
  252. package/src/HttpClient.ts +406 -0
  253. package/src/Jj.ts +159 -0
  254. package/src/JournalGrantStore.ts +461 -0
  255. package/src/Path.ts +47 -0
  256. package/src/ProcessConfinement.ts +156 -0
  257. package/src/ProcessLedger.ts +423 -0
  258. package/src/Rooted.ts +128 -0
  259. package/src/Unreachable.ts +30 -0
  260. package/src/Workspace.ts +65 -0
  261. package/src/index.ts +181 -0
  262. package/src/internal/Containment.ts +37 -0
  263. package/src/internal/Pipeline.ts +85 -0
  264. package/src/internal/makeCapability.ts +26 -0
  265. package/src/test/FileSystemBatchContract.ts +59 -0
  266. package/src/test/HostContract.ts +785 -0
  267. package/src/test/TestGrantStore.ts +73 -0
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Permission-aware outgoing HTTP requests.
3
+ *
4
+ * There is no kernel `HttpClient` interface and no kernel `HttpClient` tag.
5
+ * Effect owns both, and its tag fixes the error channel to `HttpClientError`,
6
+ * so this module is only the middleware: a `Layer` over Effect's own tag that
7
+ * reads the raw client out of context and returns a guarded one in its place.
8
+ * Permission failures are projected into `HttpClientError` by
9
+ * {@link toHttpClientError}, which keeps the structured kernel failure on the
10
+ * transport reason's `cause` so {@link fromHttpClientError} can hand it back.
11
+ *
12
+ * **Redirects.** A redirect is a second network destination, so it needs a
13
+ * second grant check. Two halves guarantee that:
14
+ *
15
+ * 1. The client below this layer must **not** follow redirects on its own.
16
+ * That is a precondition on the host: Undici without a redirect
17
+ * interceptor, or Effect's fetch layer with `redirect: "manual"`. This
18
+ * layer also forces `RequestInit { redirect: "manual" }` over the ambient
19
+ * value for every request it executes, so a plain `FetchHttpClient.layer`,
20
+ * which follows redirects by default, cannot walk to another origin below
21
+ * the guard either. A custom client that ignores `RequestInit` must meet
22
+ * the precondition itself.
23
+ * 2. This layer composes Effect's own `HttpClient.followRedirects` *above*
24
+ * the guard. That combinator re-enters `postprocess` for every hop, and
25
+ * `postprocess` is the guarded one, so hop *n* is checked exactly like hop
26
+ * zero and an authorized origin cannot lend its grant to another.
27
+ * A caller that sets `FetchHttpClient.RequestInit.redirect` to `manual`
28
+ * receives one guarded hop and owns any further redirect requests itself.
29
+ *
30
+ * Governing design:
31
+ * `docs/specs/Concepts/Permission Kernel.md`,
32
+ * `docs/specs/Concepts/Effect Taxonomy.md`, and
33
+ * `docs/specs/Concepts/Host Adapters.md`.
34
+ *
35
+ * @since 1.0.0-rc.0
36
+ */
37
+ import { type PermissionError, type PermissionErrorPayload } from "@smthrs/capability/Permission";
38
+ import { Context, Effect, Layer, Option } from "effect";
39
+ import * as EffectHttpClient from "effect/unstable/http/HttpClient";
40
+ import * as HttpClientError from "effect/unstable/http/HttpClientError";
41
+ import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
42
+ import { GrantStore } from "./GrantStore.ts";
43
+ /**
44
+ * Effect's own outgoing HTTP client tag, unchanged. Re-exported so the kernel
45
+ * namespace stays one-stop; it is the *same* tag, never a second one.
46
+ *
47
+ * @category services
48
+ * @since 1.0.0-rc.0
49
+ */
50
+ export { HttpClient } from "effect/unstable/http/HttpClient";
51
+ /**
52
+ * Constructs a client from a low-level request runner, keeping Effect's own
53
+ * URL construction, tracing, header redaction, and interruption behavior.
54
+ *
55
+ * @category constructors
56
+ * @since 1.0.0-rc.0
57
+ */
58
+ export { make } from "effect/unstable/http/HttpClient";
59
+ /**
60
+ * The model identity an outgoing request is made on behalf of, if any.
61
+ *
62
+ * A model call is not a general `net:*` effect: the same host answers many
63
+ * models, and a grant for one must not be a grant for the rest. Effect's tag
64
+ * has no room for an extra method, so the intent rides on a `Context`
65
+ * reference instead — set it with {@link withModelCall} and the decorator asks
66
+ * for `model:call` rather than `net:get`/`net:post`.
67
+ *
68
+ * @category references
69
+ * @since 1.0.0-rc.0
70
+ */
71
+ export declare const ModelCall: Context.Reference<string | undefined>;
72
+ /**
73
+ * Runs `effect` with every outgoing request in it checked as a `model:call` on
74
+ * `modelId` instead of as plain network access.
75
+ *
76
+ * @category references
77
+ * @since 1.0.0-rc.0
78
+ */
79
+ export declare const withModelCall: (modelId: string) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
80
+ /**
81
+ * Projects a permission failure into the error channel Effect's `HttpClient`
82
+ * tag fixes.
83
+ *
84
+ * Nothing is lost: the reason is always a `TransportError` — the request did
85
+ * not leave the host because the capability kernel refused, suspended, or
86
+ * could not decide it — `description` carries the human rendering from
87
+ * `Permission.formatError`, and `cause` carries the structured failure itself,
88
+ * so {@link fromHttpClientError} hands the attended surface back the original
89
+ * `capability`, `tier`, `requestId`, and `reason`.
90
+ *
91
+ * @category constructors
92
+ * @since 1.0.0-rc.0
93
+ */
94
+ export declare const toHttpClientError: (options: {
95
+ readonly request: HttpClientRequest.HttpClientRequest;
96
+ readonly error: PermissionError;
97
+ }) => HttpClientError.HttpClientError;
98
+ /**
99
+ * Recovers the structured permission failure a {@link toHttpClientError}
100
+ * projection carries, so an attended surface can still reply to the request
101
+ * and an unattended report can still name the capability.
102
+ *
103
+ * Recovery validates data fields and the transport reason tag, not origin or
104
+ * class operations. Establish producer or request identity separately across
105
+ * a trust boundary.
106
+ *
107
+ * @category refinements
108
+ * @since 1.0.0-rc.0
109
+ */
110
+ export declare const fromHttpClientError: (error: HttpClientError.HttpClientError) => Option.Option<PermissionErrorPayload>;
111
+ /**
112
+ * Constructs an unavailable HTTP client stub.
113
+ *
114
+ * Every request reports the missing host as a `TransportError` naming the
115
+ * request, so an unconfigured capability answers rather than vanishing.
116
+ *
117
+ * @category constructors
118
+ * @since 1.0.0-rc.0
119
+ */
120
+ export declare const makeNoop: () => EffectHttpClient.HttpClient;
121
+ /**
122
+ * Provides an unavailable HTTP client stub.
123
+ *
124
+ * @category layers
125
+ * @since 1.0.0-rc.0
126
+ */
127
+ export declare const layerNoop: () => Layer.Layer<EffectHttpClient.HttpClient>;
128
+ /**
129
+ * Authorizes a request before a caller performs network preflight work such as
130
+ * DNS. The guarded transport consumes the same admission once, so a one-shot
131
+ * grant is not spent twice. Admissions match both store identity and the exact
132
+ * capability; redirects and retries require their own authorization.
133
+ *
134
+ * @category authorization
135
+ * @since 1.0.0
136
+ */
137
+ export declare const authorizePreflight: <A, E, R>(request: HttpClientRequest.HttpClientRequest, effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | HttpClientError.HttpClientError, R | GrantStore>;
138
+ /**
139
+ * Decorates the HTTP client in place with a `net:get`/`net:post`/`model:call`
140
+ * capability check, then follows redirects *through* that check.
141
+ *
142
+ * The layer provides the tag it also requires: compose it over a host client
143
+ * layer with `Layer.provide` and a consumer that never heard of the kernel is
144
+ * guarded too. Because every hop re-enters the guarded `postprocess`, an
145
+ * authorized first URL that redirects to an unauthorized one is denied at the
146
+ * second hop instead of being fetched.
147
+ *
148
+ * @category layers
149
+ * @since 1.0.0-rc.0
150
+ */
151
+ export declare const layer: Layer.Layer<EffectHttpClient.HttpClient, never, EffectHttpClient.HttpClient | GrantStore>;
152
+ /**
153
+ * Address snapshot authorized for one web request. Host transports must reject
154
+ * a different origin and use only these addresses for every connection attempt.
155
+ * Ordinary model traffic leaves this reference unset.
156
+ * @category references
157
+ * @since 1.0.0
158
+ */
159
+ export declare const Destination: Context.Reference<{
160
+ readonly origin: string;
161
+ readonly addresses: ReadonlyArray<string>;
162
+ } | undefined>;
163
+ /**
164
+ * Marks a trusted transport that enforces Destination on every connection.
165
+ * The assertion belongs to this client instance, not ambient context. A
166
+ * replacement client must establish its own transport guarantee.
167
+ * @category constructors
168
+ * @since 1.0.0
169
+ */
170
+ export declare const withDestinationPinning: <A extends EffectHttpClient.HttpClient>(client: A) => A;
171
+ /**
172
+ * Whether this exact client implements the Destination transport contract.
173
+ * @category predicates
174
+ * @since 1.0.0
175
+ */
176
+ export declare const supportsDestinationPinning: (client: EffectHttpClient.HttpClient) => boolean;
177
+ //# sourceMappingURL=HttpClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpClient.d.ts","sourceRoot":"","sources":["../../src/HttpClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAGH,OAAO,EAIL,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC5B,MAAM,+BAA+B,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,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;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EACxC,SAAS,iBAAiB,CAAC,iBAAiB,EAC5C,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,KAC7B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC,eAAe,EAAE,CAAC,GAAG,UAAU,CAQnE,CAAA;AAYJ;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,KAAK,CAC7B,gBAAgB,CAAC,UAAU,EAC3B,KAAK,EACL,gBAAgB,CAAC,UAAU,GAAG,UAAU,CAiDzC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,WAAW;qBAEH,MAAM;wBACH,aAAa,CAAC,MAAM,CAAC;cAEgC,CAAA;AAI7E;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,gBAAgB,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAG,CAGzF,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,GAAI,QAAQ,gBAAgB,CAAC,UAAU,KAAG,OAAoC,CAAA"}
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Permission-aware outgoing HTTP requests.
3
+ *
4
+ * There is no kernel `HttpClient` interface and no kernel `HttpClient` tag.
5
+ * Effect owns both, and its tag fixes the error channel to `HttpClientError`,
6
+ * so this module is only the middleware: a `Layer` over Effect's own tag that
7
+ * reads the raw client out of context and returns a guarded one in its place.
8
+ * Permission failures are projected into `HttpClientError` by
9
+ * {@link toHttpClientError}, which keeps the structured kernel failure on the
10
+ * transport reason's `cause` so {@link fromHttpClientError} can hand it back.
11
+ *
12
+ * **Redirects.** A redirect is a second network destination, so it needs a
13
+ * second grant check. Two halves guarantee that:
14
+ *
15
+ * 1. The client below this layer must **not** follow redirects on its own.
16
+ * That is a precondition on the host: Undici without a redirect
17
+ * interceptor, or Effect's fetch layer with `redirect: "manual"`. This
18
+ * layer also forces `RequestInit { redirect: "manual" }` over the ambient
19
+ * value for every request it executes, so a plain `FetchHttpClient.layer`,
20
+ * which follows redirects by default, cannot walk to another origin below
21
+ * the guard either. A custom client that ignores `RequestInit` must meet
22
+ * the precondition itself.
23
+ * 2. This layer composes Effect's own `HttpClient.followRedirects` *above*
24
+ * the guard. That combinator re-enters `postprocess` for every hop, and
25
+ * `postprocess` is the guarded one, so hop *n* is checked exactly like hop
26
+ * zero and an authorized origin cannot lend its grant to another.
27
+ * A caller that sets `FetchHttpClient.RequestInit.redirect` to `manual`
28
+ * receives one guarded hop and owns any further redirect requests itself.
29
+ *
30
+ * Governing design:
31
+ * `docs/specs/Concepts/Permission Kernel.md`,
32
+ * `docs/specs/Concepts/Effect Taxonomy.md`, and
33
+ * `docs/specs/Concepts/Host Adapters.md`.
34
+ *
35
+ * @since 1.0.0-rc.0
36
+ */
37
+ import { formatError, isPermissionError, PermissionDenied } from "@smthrs/capability/Permission";
38
+ import { Context, Effect, Layer, Option } from "effect";
39
+ import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient";
40
+ import * as Headers from "effect/unstable/http/Headers";
41
+ import * as HttpBody from "effect/unstable/http/HttpBody";
42
+ import * as EffectHttpClient from "effect/unstable/http/HttpClient";
43
+ import * as HttpClientError from "effect/unstable/http/HttpClientError";
44
+ import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
45
+ import * as UrlParams from "effect/unstable/http/UrlParams";
46
+ import { GrantStore } from "./GrantStore.js";
47
+ import { makeCapability } from "./internal/makeCapability.js";
48
+ const snapshotRawBody = (value) => {
49
+ if (value instanceof globalThis.Uint8Array)
50
+ return value.slice();
51
+ if (value instanceof ArrayBuffer)
52
+ return value.slice(0);
53
+ if (value instanceof URLSearchParams) {
54
+ return new URLSearchParams(value);
55
+ }
56
+ if (value instanceof Blob)
57
+ return value;
58
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
59
+ return value;
60
+ }
61
+ throw new TypeError("raw HTTP bodies must be immutable or copyable");
62
+ };
63
+ const snapshotBody = (body) => {
64
+ switch (body._tag) {
65
+ case "Empty":
66
+ return HttpBody.empty;
67
+ case "Raw":
68
+ return HttpBody.raw(snapshotRawBody(body.body), {
69
+ contentType: body.contentType,
70
+ contentLength: body.contentLength
71
+ });
72
+ case "Uint8Array":
73
+ return HttpBody.uint8Array(body.body.slice(), body.contentType);
74
+ case "FormData": {
75
+ const formData = new globalThis.FormData();
76
+ for (const [name, value] of body.formData.entries())
77
+ formData.append(name, value);
78
+ return HttpBody.formData(formData);
79
+ }
80
+ case "Stream":
81
+ return HttpBody.stream(body.stream, body.contentType, body.contentLength);
82
+ }
83
+ };
84
+ const snapshotRequest = (request) => HttpClientRequest.makeWith(request.method, request.url, UrlParams.make(Object.freeze(request.urlParams.params.map(([name, value]) => Object.freeze([name, value])))), Option.map(request.hash, (value) => value), Headers.fromInput(request.headers), snapshotBody(request.body));
85
+ const invalidRequest = (request) => new HttpClientError.HttpClientError({
86
+ reason: new HttpClientError.TransportError({
87
+ request,
88
+ description: "HTTP request must be an immutable supported request description"
89
+ })
90
+ });
91
+ /**
92
+ * Effect's own outgoing HTTP client tag, unchanged. Re-exported so the kernel
93
+ * namespace stays one-stop; it is the *same* tag, never a second one.
94
+ *
95
+ * @category services
96
+ * @since 1.0.0-rc.0
97
+ */
98
+ export { HttpClient } from "effect/unstable/http/HttpClient";
99
+ /**
100
+ * Constructs a client from a low-level request runner, keeping Effect's own
101
+ * URL construction, tracing, header redaction, and interruption behavior.
102
+ *
103
+ * @category constructors
104
+ * @since 1.0.0-rc.0
105
+ */
106
+ export { make } from "effect/unstable/http/HttpClient";
107
+ /**
108
+ * The model identity an outgoing request is made on behalf of, if any.
109
+ *
110
+ * A model call is not a general `net:*` effect: the same host answers many
111
+ * models, and a grant for one must not be a grant for the rest. Effect's tag
112
+ * has no room for an extra method, so the intent rides on a `Context`
113
+ * reference instead — set it with {@link withModelCall} and the decorator asks
114
+ * for `model:call` rather than `net:get`/`net:post`.
115
+ *
116
+ * @category references
117
+ * @since 1.0.0-rc.0
118
+ */
119
+ export const ModelCall = Context.Reference("@smthrs/kernel/HttpClient/ModelCall", { defaultValue: () => undefined });
120
+ /**
121
+ * Runs `effect` with every outgoing request in it checked as a `model:call` on
122
+ * `modelId` instead of as plain network access.
123
+ *
124
+ * @category references
125
+ * @since 1.0.0-rc.0
126
+ */
127
+ export const withModelCall = (modelId) => (effect) => Effect.provideService(effect, ModelCall, modelId);
128
+ /**
129
+ * Projects a permission failure into the error channel Effect's `HttpClient`
130
+ * tag fixes.
131
+ *
132
+ * Nothing is lost: the reason is always a `TransportError` — the request did
133
+ * not leave the host because the capability kernel refused, suspended, or
134
+ * could not decide it — `description` carries the human rendering from
135
+ * `Permission.formatError`, and `cause` carries the structured failure itself,
136
+ * so {@link fromHttpClientError} hands the attended surface back the original
137
+ * `capability`, `tier`, `requestId`, and `reason`.
138
+ *
139
+ * @category constructors
140
+ * @since 1.0.0-rc.0
141
+ */
142
+ export const toHttpClientError = (options) => new HttpClientError.HttpClientError({
143
+ reason: new HttpClientError.TransportError({
144
+ request: options.request,
145
+ description: formatError(options.error),
146
+ cause: options.error
147
+ })
148
+ });
149
+ /**
150
+ * Recovers the structured permission failure a {@link toHttpClientError}
151
+ * projection carries, so an attended surface can still reply to the request
152
+ * and an unattended report can still name the capability.
153
+ *
154
+ * Recovery validates data fields and the transport reason tag, not origin or
155
+ * class operations. Establish producer or request identity separately across
156
+ * a trust boundary.
157
+ *
158
+ * @category refinements
159
+ * @since 1.0.0-rc.0
160
+ */
161
+ export const fromHttpClientError = (error) => error.reason._tag === "TransportError" && isPermissionError(error.reason.cause)
162
+ ? Option.some(error.reason.cause)
163
+ : Option.none();
164
+ /**
165
+ * Constructs an unavailable HTTP client stub.
166
+ *
167
+ * Every request reports the missing host as a `TransportError` naming the
168
+ * request, so an unconfigured capability answers rather than vanishing.
169
+ *
170
+ * @category constructors
171
+ * @since 1.0.0-rc.0
172
+ */
173
+ export const makeNoop = () => EffectHttpClient.make((request) => Effect.fail(new HttpClientError.HttpClientError({
174
+ reason: new HttpClientError.TransportError({
175
+ request,
176
+ cause: new Error("HTTP is unavailable on this host")
177
+ })
178
+ })));
179
+ /**
180
+ * Provides an unavailable HTTP client stub.
181
+ *
182
+ * @category layers
183
+ * @since 1.0.0-rc.0
184
+ */
185
+ export const layerNoop = () => Layer.succeed(EffectHttpClient.HttpClient)(makeNoop());
186
+ /**
187
+ * Keeps HTTPS resources host-only so existing grants remain valid. Every
188
+ * other scheme is explicit, which prevents an HTTPS grant from authorizing a
189
+ * cleartext or otherwise different transport.
190
+ */
191
+ const capabilityFor = (request, modelId) => {
192
+ const action = modelId === undefined
193
+ ? request.method === "GET" || request.method === "HEAD" ? "net:get" : "net:post"
194
+ : "model:call";
195
+ let url;
196
+ try {
197
+ url = new URL(request.url);
198
+ }
199
+ catch {
200
+ return makeCapability(action, request.url).pipe(Effect.flatMap((capability) => Effect.fail(new PermissionDenied({
201
+ code: "permission_denied",
202
+ capability,
203
+ reason: "HTTP capability checks require an absolute, parseable URL"
204
+ }))));
205
+ }
206
+ const host = url.host.toLowerCase();
207
+ const resource = url.protocol === "https:" ? host : `${url.protocol}//${host}`;
208
+ return makeCapability(action, modelId === undefined ? resource : `${resource}/${modelId}`);
209
+ };
210
+ const Admission = Context.Reference("@smthrs/kernel/HttpClient/Admission", {
211
+ defaultValue: () => undefined
212
+ });
213
+ /**
214
+ * Authorizes a request before a caller performs network preflight work such as
215
+ * DNS. The guarded transport consumes the same admission once, so a one-shot
216
+ * grant is not spent twice. Admissions match both store identity and the exact
217
+ * capability; redirects and retries require their own authorization.
218
+ *
219
+ * @category authorization
220
+ * @since 1.0.0
221
+ */
222
+ export const authorizePreflight = (request, effect) => Effect.gen(function* () {
223
+ const store = yield* GrantStore;
224
+ const capability = yield* Effect.withFiber((fiber) => capabilityFor(request, fiber.getRef(ModelCall))).pipe(Effect.tap((capability) => store.check(capability)), Effect.mapError((error) => toHttpClientError({ request, error })));
225
+ return yield* Effect.provideService(effect, Admission, { store, capability, consumed: false });
226
+ });
227
+ /**
228
+ * Forces `redirect: "manual"` on the fetch options a fetch-backed client reads
229
+ * at execute time, keeping every other ambient field.
230
+ */
231
+ const manualRedirects = (context) => Context.add(context, FetchHttpClient.RequestInit, {
232
+ ...Context.getOrUndefined(context, FetchHttpClient.RequestInit),
233
+ redirect: "manual"
234
+ });
235
+ /**
236
+ * Decorates the HTTP client in place with a `net:get`/`net:post`/`model:call`
237
+ * capability check, then follows redirects *through* that check.
238
+ *
239
+ * The layer provides the tag it also requires: compose it over a host client
240
+ * layer with `Layer.provide` and a consumer that never heard of the kernel is
241
+ * guarded too. Because every hop re-enters the guarded `postprocess`, an
242
+ * authorized first URL that redirects to an unauthorized one is denied at the
243
+ * second hop instead of being fetched.
244
+ *
245
+ * @category layers
246
+ * @since 1.0.0-rc.0
247
+ */
248
+ export const layer = Layer.effect(EffectHttpClient.HttpClient, Effect.gen(function* () {
249
+ const client = yield* EffectHttpClient.HttpClient;
250
+ const grants = yield* GrantStore;
251
+ const snapshotted = EffectHttpClient.mapRequestEffect(client, (request) => Effect.try({
252
+ try: () => snapshotRequest(request),
253
+ catch: () => invalidRequest(request)
254
+ }));
255
+ const guarded = EffectHttpClient.transform(snapshotted, (execute, request) => Effect.withFiber((fiber) => capabilityFor(request, fiber.getRef(ModelCall)).pipe(Effect.flatMap((capability) => {
256
+ const admission = fiber.getRef(Admission);
257
+ if (admission !== undefined && !admission.consumed && admission.store === grants
258
+ && admission.capability.action === capability.action &&
259
+ admission.capability.resource === capability.resource) {
260
+ admission.consumed = true;
261
+ return Effect.void;
262
+ }
263
+ return grants.check(capability);
264
+ }), Effect.mapError((error) => toHttpClientError({ request, error })), Effect.andThen(Effect.updateContext(execute, manualRedirects)))));
265
+ const followed = EffectHttpClient.followRedirects(guarded);
266
+ const result = EffectHttpClient.makeWith((request) => Effect.withFiber((fiber) =>
267
+ // Read caller policy before the guard forces manual transport.
268
+ Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit)?.redirect === "manual"
269
+ ? guarded.postprocess(request)
270
+ : followed.postprocess(request)), guarded.preprocess);
271
+ return supportsDestinationPinning(client) ? withDestinationPinning(result) : result;
272
+ }));
273
+ /**
274
+ * Address snapshot authorized for one web request. Host transports must reject
275
+ * a different origin and use only these addresses for every connection attempt.
276
+ * Ordinary model traffic leaves this reference unset.
277
+ * @category references
278
+ * @since 1.0.0
279
+ */
280
+ export const Destination = Context.Reference("@smthrs/kernel/HttpClient/Destination", { defaultValue: () => undefined });
281
+ const pinnedClients = new WeakSet();
282
+ /**
283
+ * Marks a trusted transport that enforces Destination on every connection.
284
+ * The assertion belongs to this client instance, not ambient context. A
285
+ * replacement client must establish its own transport guarantee.
286
+ * @category constructors
287
+ * @since 1.0.0
288
+ */
289
+ export const withDestinationPinning = (client) => {
290
+ pinnedClients.add(client);
291
+ return client;
292
+ };
293
+ /**
294
+ * Whether this exact client implements the Destination transport contract.
295
+ * @category predicates
296
+ * @since 1.0.0
297
+ */
298
+ export const supportsDestinationPinning = (client) => pinnedClients.has(client);
299
+ //# sourceMappingURL=HttpClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpClient.js","sourceRoot":"","sources":["../../src/HttpClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAGH,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,gBAAgB,EAGjB,MAAM,+BAA+B,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACvD,OAAO,KAAK,eAAe,MAAM,sCAAsC,CAAA;AACvE,OAAO,KAAK,OAAO,MAAM,8BAA8B,CAAA;AACvD,OAAO,KAAK,QAAQ,MAAM,+BAA+B,CAAA;AACzD,OAAO,KAAK,gBAAgB,MAAM,iC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1C,uCAAuC,EAAE,EAAE,YAAY,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC,CAAA;AAE7E,MAAM,aAAa,GAAG,IAAI,OAAO,EAA+B,CAAA;AAEhE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAwC,MAAS,EAAK,EAAE;IAC5F,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IACzB,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,MAAmC,EAAW,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA","sourcesContent":["/**\n * Permission-aware outgoing HTTP requests.\n *\n * There is no kernel `HttpClient` interface and no kernel `HttpClient` tag.\n * Effect owns both, and its tag fixes the error channel to `HttpClientError`,\n * so this module is only the middleware: a `Layer` over Effect's own tag that\n * reads the raw client out of context and returns a guarded one in its place.\n * Permission failures are projected into `HttpClientError` by\n * {@link toHttpClientError}, which keeps the structured kernel failure on the\n * transport reason's `cause` so {@link fromHttpClientError} can hand it back.\n *\n * **Redirects.** A redirect is a second network destination, so it needs a\n * second grant check. Two halves guarantee that:\n *\n * 1. The client below this layer must **not** follow redirects on its own.\n * That is a precondition on the host: Undici without a redirect\n * interceptor, or Effect's fetch layer with `redirect: \"manual\"`. This\n * layer also forces `RequestInit { redirect: \"manual\" }` over the ambient\n * value for every request it executes, so a plain `FetchHttpClient.layer`,\n * which follows redirects by default, cannot walk to another origin below\n * the guard either. A custom client that ignores `RequestInit` must meet\n * the precondition itself.\n * 2. This layer composes Effect's own `HttpClient.followRedirects` *above*\n * the guard. That combinator re-enters `postprocess` for every hop, and\n * `postprocess` is the guarded one, so hop *n* is checked exactly like hop\n * zero and an authorized origin cannot lend its grant to another.\n * A caller that sets `FetchHttpClient.RequestInit.redirect` to `manual`\n * receives one guarded hop and owns any further redirect requests itself.\n *\n * Governing design:\n * `docs/specs/Concepts/Permission Kernel.md`,\n * `docs/specs/Concepts/Effect Taxonomy.md`, and\n * `docs/specs/Concepts/Host Adapters.md`.\n *\n * @since 1.0.0-rc.0\n */\n\nimport type { Capability } from \"@smthrs/capability/Capability\"\nimport {\n formatError,\n isPermissionError,\n PermissionDenied,\n type PermissionError,\n type PermissionErrorPayload\n} from \"@smthrs/capability/Permission\"\nimport { Context, Effect, Layer, Option } from \"effect\"\nimport * as FetchHttpClient from \"effect/unstable/http/FetchHttpClient\"\nimport * as Headers from \"effect/unstable/http/Headers\"\nimport * as HttpBody from \"effect/unstable/http/HttpBody\"\nimport * as EffectHttpClient from \"effect/unstable/http/HttpClient\"\nimport * as HttpClientError from \"effect/unstable/http/HttpClientError\"\nimport * as HttpClientRequest from \"effect/unstable/http/HttpClientRequest\"\nimport * as UrlParams from \"effect/unstable/http/UrlParams\"\nimport { GrantStore, type Service as GrantService } from \"./GrantStore.ts\"\nimport { makeCapability } from \"./internal/makeCapability.ts\"\n\nconst snapshotRawBody = (value: unknown): unknown => {\n if (value instanceof globalThis.Uint8Array) return value.slice()\n if (value instanceof ArrayBuffer) return value.slice(0)\n if (value instanceof URLSearchParams) {\n return new URLSearchParams(value)\n }\n if (value instanceof Blob) return value\n if (value === null || typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\") {\n return value\n }\n throw new TypeError(\"raw HTTP bodies must be immutable or copyable\")\n}\n\nconst snapshotBody = (body: HttpBody.HttpBody): HttpBody.HttpBody => {\n switch (body._tag) {\n case \"Empty\":\n return HttpBody.empty\n case \"Raw\":\n return HttpBody.raw(snapshotRawBody(body.body), {\n contentType: body.contentType,\n contentLength: body.contentLength\n })\n case \"Uint8Array\":\n return HttpBody.uint8Array(body.body.slice(), body.contentType)\n case \"FormData\": {\n const formData = new globalThis.FormData()\n for (const [name, value] of body.formData.entries()) formData.append(name, value)\n return HttpBody.formData(formData)\n }\n case \"Stream\":\n return HttpBody.stream(body.stream, body.contentType, body.contentLength)\n }\n}\n\nconst snapshotRequest = (request: HttpClientRequest.HttpClientRequest): HttpClientRequest.HttpClientRequest =>\n HttpClientRequest.makeWith(\n request.method,\n request.url,\n UrlParams.make(\n Object.freeze(request.urlParams.params.map(([name, value]) => Object.freeze([name, value] as const)))\n ),\n Option.map(request.hash, (value) => value),\n Headers.fromInput(request.headers),\n snapshotBody(request.body)\n )\n\nconst invalidRequest = (request: HttpClientRequest.HttpClientRequest): HttpClientError.HttpClientError =>\n new HttpClientError.HttpClientError({\n reason: new HttpClientError.TransportError({\n request,\n description: \"HTTP request must be an immutable supported request description\"\n })\n })\n\n/**\n * Effect's own outgoing HTTP client tag, unchanged. Re-exported so the kernel\n * namespace stays one-stop; it is the *same* tag, never a second one.\n *\n * @category services\n * @since 1.0.0-rc.0\n */\nexport { HttpClient } from \"effect/unstable/http/HttpClient\"\n\n/**\n * Constructs a client from a low-level request runner, keeping Effect's own\n * URL construction, tracing, header redaction, and interruption behavior.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n */\nexport { make } from \"effect/unstable/http/HttpClient\"\n\n/**\n * The model identity an outgoing request is made on behalf of, if any.\n *\n * A model call is not a general `net:*` effect: the same host answers many\n * models, and a grant for one must not be a grant for the rest. Effect's tag\n * has no room for an extra method, so the intent rides on a `Context`\n * reference instead — set it with {@link withModelCall} and the decorator asks\n * for `model:call` rather than `net:get`/`net:post`.\n *\n * @category references\n * @since 1.0.0-rc.0\n */\nexport const ModelCall: Context.Reference<string | undefined> = Context.Reference<string | undefined>(\n \"@smthrs/kernel/HttpClient/ModelCall\",\n { defaultValue: (): string | undefined => undefined }\n)\n\n/**\n * Runs `effect` with every outgoing request in it checked as a `model:call` on\n * `modelId` instead of as plain network access.\n *\n * @category references\n * @since 1.0.0-rc.0\n */\nexport const withModelCall = (modelId: string) => <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Effect.provideService(effect, ModelCall, modelId)\n\n/**\n * Projects a permission failure into the error channel Effect's `HttpClient`\n * tag fixes.\n *\n * Nothing is lost: the reason is always a `TransportError` — the request did\n * not leave the host because the capability kernel refused, suspended, or\n * could not decide it — `description` carries the human rendering from\n * `Permission.formatError`, and `cause` carries the structured failure itself,\n * so {@link fromHttpClientError} hands the attended surface back the original\n * `capability`, `tier`, `requestId`, and `reason`.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n */\nexport const toHttpClientError = (options: {\n readonly request: HttpClientRequest.HttpClientRequest\n readonly error: PermissionError\n}): HttpClientError.HttpClientError =>\n new HttpClientError.HttpClientError({\n reason: new HttpClientError.TransportError({\n request: options.request,\n description: formatError(options.error),\n cause: options.error\n })\n })\n\n/**\n * Recovers the structured permission failure a {@link toHttpClientError}\n * projection carries, so an attended surface can still reply to the request\n * and an unattended report can still name the capability.\n *\n * Recovery validates data fields and the transport reason tag, not origin or\n * class operations. Establish producer or request identity separately across\n * a trust boundary.\n *\n * @category refinements\n * @since 1.0.0-rc.0\n */\nexport const fromHttpClientError = (\n error: HttpClientError.HttpClientError\n): Option.Option<PermissionErrorPayload> =>\n error.reason._tag === \"TransportError\" && isPermissionError(error.reason.cause)\n ? Option.some(error.reason.cause)\n : Option.none()\n\n/**\n * Constructs an unavailable HTTP client stub.\n *\n * Every request reports the missing host as a `TransportError` naming the\n * request, so an unconfigured capability answers rather than vanishing.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n */\nexport const makeNoop = (): EffectHttpClient.HttpClient =>\n EffectHttpClient.make((request) =>\n Effect.fail(\n new HttpClientError.HttpClientError({\n reason: new HttpClientError.TransportError({\n request,\n cause: new Error(\"HTTP is unavailable on this host\")\n })\n })\n )\n )\n\n/**\n * Provides an unavailable HTTP client stub.\n *\n * @category layers\n * @since 1.0.0-rc.0\n */\nexport const layerNoop = (): Layer.Layer<EffectHttpClient.HttpClient> =>\n Layer.succeed(EffectHttpClient.HttpClient)(makeNoop())\n\n/**\n * Keeps HTTPS resources host-only so existing grants remain valid. Every\n * other scheme is explicit, which prevents an HTTPS grant from authorizing a\n * cleartext or otherwise different transport.\n */\nconst capabilityFor = (\n request: HttpClientRequest.HttpClientRequest,\n modelId: string | undefined\n): Effect.Effect<Capability, PermissionError> => {\n const action = modelId === undefined\n ? request.method === \"GET\" || request.method === \"HEAD\" ? \"net:get\" : \"net:post\"\n : \"model:call\"\n let url: URL\n try {\n url = new URL(request.url)\n } catch {\n return makeCapability(action, request.url).pipe(\n Effect.flatMap((capability) =>\n Effect.fail(\n new PermissionDenied({\n code: \"permission_denied\",\n capability,\n reason: \"HTTP capability checks require an absolute, parseable URL\"\n })\n )\n )\n )\n }\n const host = url.host.toLowerCase()\n const resource = url.protocol === \"https:\" ? host : `${url.protocol}//${host}`\n return makeCapability(action, modelId === undefined ? resource : `${resource}/${modelId}`)\n}\n\ninterface PreflightAdmission {\n readonly store: GrantService\n readonly capability: Capability\n consumed: boolean\n}\n\nconst Admission = Context.Reference<PreflightAdmission | undefined>(\"@smthrs/kernel/HttpClient/Admission\", {\n defaultValue: () => undefined\n})\n\n/**\n * Authorizes a request before a caller performs network preflight work such as\n * DNS. The guarded transport consumes the same admission once, so a one-shot\n * grant is not spent twice. Admissions match both store identity and the exact\n * capability; redirects and retries require their own authorization.\n *\n * @category authorization\n * @since 1.0.0\n */\nexport const authorizePreflight = <A, E, R>(\n request: HttpClientRequest.HttpClientRequest,\n effect: Effect.Effect<A, E, R>\n): Effect.Effect<A, E | HttpClientError.HttpClientError, R | GrantStore> =>\n Effect.gen(function*() {\n const store = yield* GrantStore\n const capability = yield* Effect.withFiber((fiber) => capabilityFor(request, fiber.getRef(ModelCall))).pipe(\n Effect.tap((capability) => store.check(capability)),\n Effect.mapError((error) => toHttpClientError({ request, error }))\n )\n return yield* Effect.provideService(effect, Admission, { store, capability, consumed: false })\n })\n\n/**\n * Forces `redirect: \"manual\"` on the fetch options a fetch-backed client reads\n * at execute time, keeping every other ambient field.\n */\nconst manualRedirects = (context: Context.Context<never>): Context.Context<never> =>\n Context.add(context, FetchHttpClient.RequestInit, {\n ...Context.getOrUndefined(context, FetchHttpClient.RequestInit),\n redirect: \"manual\"\n })\n\n/**\n * Decorates the HTTP client in place with a `net:get`/`net:post`/`model:call`\n * capability check, then follows redirects *through* that check.\n *\n * The layer provides the tag it also requires: compose it over a host client\n * layer with `Layer.provide` and a consumer that never heard of the kernel is\n * guarded too. Because every hop re-enters the guarded `postprocess`, an\n * authorized first URL that redirects to an unauthorized one is denied at the\n * second hop instead of being fetched.\n *\n * @category layers\n * @since 1.0.0-rc.0\n */\nexport const layer: Layer.Layer<\n EffectHttpClient.HttpClient,\n never,\n EffectHttpClient.HttpClient | GrantStore\n> = Layer.effect(\n EffectHttpClient.HttpClient,\n Effect.gen(function*() {\n const client = yield* EffectHttpClient.HttpClient\n const grants = yield* GrantStore\n const snapshotted = EffectHttpClient.mapRequestEffect(\n client,\n (request) =>\n Effect.try({\n try: () => snapshotRequest(request),\n catch: () => invalidRequest(request)\n })\n )\n const guarded = EffectHttpClient.transform(\n snapshotted,\n (execute, request) =>\n Effect.withFiber((fiber) =>\n capabilityFor(request, fiber.getRef(ModelCall)).pipe(\n Effect.flatMap((capability) => {\n const admission = fiber.getRef(Admission)\n if (\n admission !== undefined && !admission.consumed && admission.store === grants\n && admission.capability.action === capability.action &&\n admission.capability.resource === capability.resource\n ) {\n admission.consumed = true\n return Effect.void\n }\n return grants.check(capability)\n }),\n Effect.mapError((error: PermissionError) => toHttpClientError({ request, error })),\n Effect.andThen(Effect.updateContext(execute, manualRedirects))\n )\n )\n )\n const followed = EffectHttpClient.followRedirects(guarded)\n const result = EffectHttpClient.makeWith(\n (request) =>\n Effect.withFiber((fiber) =>\n // Read caller policy before the guard forces manual transport.\n Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit)?.redirect === \"manual\"\n ? guarded.postprocess(request)\n : followed.postprocess(request)\n ),\n guarded.preprocess\n )\n return supportsDestinationPinning(client) ? withDestinationPinning(result) : result\n })\n)\n\n/**\n * Address snapshot authorized for one web request. Host transports must reject\n * a different origin and use only these addresses for every connection attempt.\n * Ordinary model traffic leaves this reference unset.\n * @category references\n * @since 1.0.0\n */\nexport const Destination = Context.Reference<\n {\n readonly origin: string\n readonly addresses: ReadonlyArray<string>\n } | undefined\n>(\"@smthrs/kernel/HttpClient/Destination\", { defaultValue: () => undefined })\n\nconst pinnedClients = new WeakSet<EffectHttpClient.HttpClient>()\n\n/**\n * Marks a trusted transport that enforces Destination on every connection.\n * The assertion belongs to this client instance, not ambient context. A\n * replacement client must establish its own transport guarantee.\n * @category constructors\n * @since 1.0.0\n */\nexport const withDestinationPinning = <A extends EffectHttpClient.HttpClient>(client: A): A => {\n pinnedClients.add(client)\n return client\n}\n\n/**\n * Whether this exact client implements the Destination transport contract.\n * @category predicates\n * @since 1.0.0\n */\nexport const supportsDestinationPinning = (client: EffectHttpClient.HttpClient): boolean => pinnedClients.has(client)\n"]}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Permission-aware Jujutsu version-control operations.
3
+ *
4
+ * There is no kernel `Jj` interface and no kernel `Jj` tag. `@smthrs/jj` owns
5
+ * both, and its error channel already names the failures the kernel adds, so
6
+ * this module is only the middleware: a `Layer` over the same tag that reads
7
+ * the raw service out of context and returns a guarded one in its place.
8
+ *
9
+ * Governing design:
10
+ * `docs/specs/Concepts/Permission Kernel.md`,
11
+ * `docs/specs/Concepts/Effect Taxonomy.md`, and
12
+ * `docs/specs/Concepts/Host Adapters.md`.
13
+ *
14
+ * @since 1.0.0-rc.0
15
+ */
16
+ import { Jj } from "@smthrs/jj";
17
+ import { FileSystem as EffectFileSystem, Layer, Path as EffectPath } from "effect";
18
+ import { GrantStore } from "./GrantStore.ts";
19
+ import { Workspace } from "./Workspace.ts";
20
+ /**
21
+ * `@smthrs/jj`'s own Jujutsu tag, unchanged. Re-exported so the kernel
22
+ * namespace stays one-stop; it is the *same* tag, never a second one.
23
+ *
24
+ * @category services
25
+ * @since 1.0.0-rc.0
26
+ */
27
+ export { Jj } from "@smthrs/jj";
28
+ /**
29
+ * Provides an unavailable Jujutsu stub.
30
+ *
31
+ * @category layers
32
+ * @since 1.0.0-rc.0
33
+ */
34
+ export { layerNoop } from "@smthrs/jj";
35
+ /**
36
+ * Constructs a Jujutsu service from an implementation.
37
+ *
38
+ * @category constructors
39
+ * @since 1.0.0-rc.0
40
+ */
41
+ export { make } from "@smthrs/jj";
42
+ /**
43
+ * Constructs an unavailable Jujutsu stub.
44
+ *
45
+ * @category constructors
46
+ * @since 1.0.0-rc.0
47
+ */
48
+ export { makeNoop } from "@smthrs/jj";
49
+ /**
50
+ * Decorates the Jujutsu service in place with operation-specific capability
51
+ * checks.
52
+ *
53
+ * The layer provides the tag it also requires: compose it over a host jj layer
54
+ * with `Layer.provide` and every consumer of `Jj` — including one that never
55
+ * heard of the kernel — resolves the guarded implementation.
56
+ *
57
+ * `workspaceAdd` canonicalizes its destination and `root` canonicalizes its
58
+ * starting directory through the *raw* filesystem before asking for
59
+ * `jj:workspace-add` and `fs:write`, or for `jj:root`, so an existing symlink
60
+ * cannot turn an inside-workspace grant into outside authority or separate the
61
+ * authorized path from the directory jj is run in.
62
+ *
63
+ * @category layers
64
+ * @since 1.0.0-rc.0
65
+ */
66
+ export declare const layer: Layer.Layer<Jj, never, Jj | EffectFileSystem.FileSystem | EffectPath.Path | Workspace | GrantStore>;
67
+ //# sourceMappingURL=Jj.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Jj.d.ts","sourceRoot":"","sources":["../../src/Jj.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,EAAE,EAAmC,MAAM,YAAY,CAAA;AAChE,OAAO,EAAU,UAAU,IAAI,gBAAgB,EAAE,KAAK,EAAE,IAAI,IAAI,UAAU,EAAE,MAAM,QAAQ,CAAA;AAE1F,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C;;;;;;GAMG;AACH,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAA;AAE/B;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtC;;;;;GAKG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAEjC;;;;;GAKG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErC;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,KAAK,CAC7B,EAAE,EACF,KAAK,EACL,EAAE,GAAG,gBAAgB,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,GAAG,SAAS,GAAG,UAAU,CAgF5E,CAAA"}