@pi-in-go/pigpen-ahp 0.1.0

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 (304) hide show
  1. package/CREDITS.md +27 -0
  2. package/LICENSE +23 -0
  3. package/README.md +148 -0
  4. package/extensions/ahp/access_options_test.go +116 -0
  5. package/extensions/ahp/extension.go +54 -0
  6. package/extensions/ahp/go.mod +12 -0
  7. package/extensions/ahp/go.sum +2 -0
  8. package/extensions/ahp/go.work +8 -0
  9. package/extensions/ahp/internal/channels/chat.go +110 -0
  10. package/extensions/ahp/internal/channels/root.go +32 -0
  11. package/extensions/ahp/internal/channels/session.go +128 -0
  12. package/extensions/ahp/internal/channels/session_test.go +108 -0
  13. package/extensions/ahp/internal/compose/compose.go +153 -0
  14. package/extensions/ahp/internal/compose/compose_test.go +120 -0
  15. package/extensions/ahp/internal/gaps/gaps_test.go +88 -0
  16. package/extensions/ahp/internal/host/handshake_test.go +206 -0
  17. package/extensions/ahp/internal/host/host.go +1305 -0
  18. package/extensions/ahp/internal/host/reconnect_test.go +197 -0
  19. package/extensions/ahp/internal/host/replaywindow_test.go +31 -0
  20. package/extensions/ahp/internal/host/schema_test.go +79 -0
  21. package/extensions/ahp/internal/host/store.go +291 -0
  22. package/extensions/ahp/internal/host/store_update_test.go +85 -0
  23. package/extensions/ahp/internal/host/subscriptions_test.go +160 -0
  24. package/extensions/ahp/internal/host/surface_test.go +95 -0
  25. package/extensions/ahp/internal/host/workarounds.go +458 -0
  26. package/extensions/ahp/internal/host/workarounds_test.go +96 -0
  27. package/extensions/ahp/internal/live/live.go +328 -0
  28. package/extensions/ahp/internal/live/live_test.go +91 -0
  29. package/extensions/ahp/internal/live/store.go +113 -0
  30. package/extensions/ahp/internal/mapper/activity.go +199 -0
  31. package/extensions/ahp/internal/mapper/activity_test.go +188 -0
  32. package/extensions/ahp/internal/mapper/bench_test.go +38 -0
  33. package/extensions/ahp/internal/mapper/event_mapper_test.go +380 -0
  34. package/extensions/ahp/internal/mapper/fixtures_test.go +387 -0
  35. package/extensions/ahp/internal/mapper/helpers_test.go +251 -0
  36. package/extensions/ahp/internal/mapper/js.go +131 -0
  37. package/extensions/ahp/internal/mapper/mapper.go +600 -0
  38. package/extensions/ahp/internal/mapper/message_input_test.go +196 -0
  39. package/extensions/ahp/internal/mapper/messageinput.go +272 -0
  40. package/extensions/ahp/internal/mapper/schema_helpers_test.go +5 -0
  41. package/extensions/ahp/internal/mapper/testdata/LICENSE-pi-ahp +21 -0
  42. package/extensions/ahp/internal/mapper/testdata/fixtures/abort.json +711 -0
  43. package/extensions/ahp/internal/mapper/testdata/fixtures/bash-long-output.json +1312 -0
  44. package/extensions/ahp/internal/mapper/testdata/fixtures/compaction.json +6285 -0
  45. package/extensions/ahp/internal/mapper/testdata/fixtures/parallel-tools.json +2622 -0
  46. package/extensions/ahp/internal/mapper/testdata/fixtures/plain-text.json +1856 -0
  47. package/extensions/ahp/internal/mapper/testdata/fixtures/single-tool.json +1128 -0
  48. package/extensions/ahp/internal/mapper/testdata/fixtures/steering.json +3926 -0
  49. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-bash.json +2372 -0
  50. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-edit.json +3983 -0
  51. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-error.json +2850 -0
  52. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-find.json +1162 -0
  53. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-grep.json +2783 -0
  54. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-loop.json +1693 -0
  55. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-ls.json +2427 -0
  56. package/extensions/ahp/internal/mapper/testdata/fixtures/tool-write.json +1271 -0
  57. package/extensions/ahp/internal/mapper/title.go +34 -0
  58. package/extensions/ahp/internal/mapper/usermsg.go +87 -0
  59. package/extensions/ahp/internal/pi/activeturn_test.go +296 -0
  60. package/extensions/ahp/internal/pi/backend.go +64 -0
  61. package/extensions/ahp/internal/pi/catalogue.go +370 -0
  62. package/extensions/ahp/internal/pi/catalogue_test.go +271 -0
  63. package/extensions/ahp/internal/pi/chatdriver.go +533 -0
  64. package/extensions/ahp/internal/pi/chatdriver_test.go +416 -0
  65. package/extensions/ahp/internal/pi/clientactions.go +212 -0
  66. package/extensions/ahp/internal/pi/clientactions_test.go +273 -0
  67. package/extensions/ahp/internal/pi/completions.go +230 -0
  68. package/extensions/ahp/internal/pi/completions_test.go +272 -0
  69. package/extensions/ahp/internal/pi/config_test.go +269 -0
  70. package/extensions/ahp/internal/pi/deletesession.go +69 -0
  71. package/extensions/ahp/internal/pi/deletesession_test.go +153 -0
  72. package/extensions/ahp/internal/pi/disposal_test.go +362 -0
  73. package/extensions/ahp/internal/pi/fixture_test.go +308 -0
  74. package/extensions/ahp/internal/pi/foreign_test.go +122 -0
  75. package/extensions/ahp/internal/pi/harness_test.go +83 -0
  76. package/extensions/ahp/internal/pi/history.go +269 -0
  77. package/extensions/ahp/internal/pi/hydrated_test.go +171 -0
  78. package/extensions/ahp/internal/pi/hydratedlifecycle_test.go +196 -0
  79. package/extensions/ahp/internal/pi/hydration_test.go +209 -0
  80. package/extensions/ahp/internal/pi/hydrator.go +212 -0
  81. package/extensions/ahp/internal/pi/imageinput.go +224 -0
  82. package/extensions/ahp/internal/pi/imageinput_test.go +57 -0
  83. package/extensions/ahp/internal/pi/lifecycle_test.go +381 -0
  84. package/extensions/ahp/internal/pi/models.go +212 -0
  85. package/extensions/ahp/internal/pi/models_test.go +128 -0
  86. package/extensions/ahp/internal/pi/paging.go +95 -0
  87. package/extensions/ahp/internal/pi/pagingtruncate_test.go +323 -0
  88. package/extensions/ahp/internal/pi/projecttrust.go +222 -0
  89. package/extensions/ahp/internal/pi/registry.go +924 -0
  90. package/extensions/ahp/internal/pi/restart_test.go +185 -0
  91. package/extensions/ahp/internal/pi/services.go +70 -0
  92. package/extensions/ahp/internal/pi/sessionconfig.go +64 -0
  93. package/extensions/ahp/internal/pi/sessionfiles_test.go +74 -0
  94. package/extensions/ahp/internal/pi/sessionstore.go +83 -0
  95. package/extensions/ahp/internal/pi/summary_test.go +426 -0
  96. package/extensions/ahp/internal/pi/workarounds_wire_test.go +208 -0
  97. package/extensions/ahp/internal/pisession/fromentries_test.go +32 -0
  98. package/extensions/ahp/internal/pisession/json.go +31 -0
  99. package/extensions/ahp/internal/pisession/pisession.go +580 -0
  100. package/extensions/ahp/internal/settings/access.go +63 -0
  101. package/extensions/ahp/internal/settings/access_test.go +52 -0
  102. package/extensions/ahp/internal/settings/settings.go +117 -0
  103. package/extensions/ahp/internal/settings/settings_test.go +104 -0
  104. package/extensions/ahp/internal/svc/etag_other.go +15 -0
  105. package/extensions/ahp/internal/svc/etag_unix.go +34 -0
  106. package/extensions/ahp/internal/svc/glob.go +197 -0
  107. package/extensions/ahp/internal/svc/mime.go +37 -0
  108. package/extensions/ahp/internal/svc/paths.go +108 -0
  109. package/extensions/ahp/internal/svc/pty.go +64 -0
  110. package/extensions/ahp/internal/svc/pty_darwin.go +42 -0
  111. package/extensions/ahp/internal/svc/pty_linux.go +36 -0
  112. package/extensions/ahp/internal/svc/pty_other.go +9 -0
  113. package/extensions/ahp/internal/svc/pty_test.go +114 -0
  114. package/extensions/ahp/internal/svc/pty_unix.go +147 -0
  115. package/extensions/ahp/internal/svc/resource.go +495 -0
  116. package/extensions/ahp/internal/svc/resource_test.go +501 -0
  117. package/extensions/ahp/internal/svc/stat_bsd.go +9 -0
  118. package/extensions/ahp/internal/svc/stat_linux.go +9 -0
  119. package/extensions/ahp/internal/svc/terminal.go +454 -0
  120. package/extensions/ahp/internal/svc/terminal_test.go +512 -0
  121. package/extensions/ahp/internal/svc/watch.go +590 -0
  122. package/extensions/ahp/internal/svc/watch_test.go +627 -0
  123. package/extensions/ahp/internal/svc/watchevents_test.go +452 -0
  124. package/extensions/ahp/internal/svc/watchpolicy.go +81 -0
  125. package/extensions/ahp/internal/svc/watchpolicy_test.go +151 -0
  126. package/extensions/ahp/internal/svc/watchracy_test.go +61 -0
  127. package/extensions/ahp/internal/testkit/schema/LICENSE-agent-host-protocol +21 -0
  128. package/extensions/ahp/internal/testkit/schema/actions.schema.json +9208 -0
  129. package/extensions/ahp/internal/testkit/schema/commands.schema.json +10862 -0
  130. package/extensions/ahp/internal/testkit/schema/errors.schema.json +10928 -0
  131. package/extensions/ahp/internal/testkit/schema/notifications.schema.json +6667 -0
  132. package/extensions/ahp/internal/testkit/schema/state.schema.json +6392 -0
  133. package/extensions/ahp/internal/testkit/schema.go +360 -0
  134. package/extensions/ahp/internal/testkit/testkit.go +367 -0
  135. package/extensions/ahp/internal/twin/twin.go +27 -0
  136. package/extensions/ahp/internal/wire/dispatchable.go +106 -0
  137. package/extensions/ahp/internal/wire/dispatchable_list.go +15 -0
  138. package/extensions/ahp/internal/wire/helpers_test.go +5 -0
  139. package/extensions/ahp/internal/wire/wire.go +218 -0
  140. package/extensions/ahp/internal/wire/wire_test.go +123 -0
  141. package/extensions/ahp/internal/ws/helpers_test.go +7 -0
  142. package/extensions/ahp/internal/ws/vscode_test.go +198 -0
  143. package/extensions/ahp/internal/ws/ws.go +674 -0
  144. package/extensions/ahp/internal/ws/ws_test.go +324 -0
  145. package/extensions/ahp/realpig_test.go +589 -0
  146. package/extensions/ahp/runtime.go +311 -0
  147. package/extensions/ahp/runtime_test.go +110 -0
  148. package/extensions/ahp/testdata/upstream-tests.json +3833 -0
  149. package/extensions/ahp/third_party/agent-host-protocol-go/LICENSE +21 -0
  150. package/extensions/ahp/third_party/agent-host-protocol-go/NOTICE-PIGPEN.md +18 -0
  151. package/extensions/ahp/third_party/agent-host-protocol-go/ahp/client.go +1011 -0
  152. package/extensions/ahp/third_party/agent-host-protocol-go/ahp/error.go +111 -0
  153. package/extensions/ahp/third_party/agent-host-protocol-go/ahp/multi_host_state_mirror.go +239 -0
  154. package/extensions/ahp/third_party/agent-host-protocol-go/ahp/reducers.go +1939 -0
  155. package/extensions/ahp/third_party/agent-host-protocol-go/ahp/transport.go +176 -0
  156. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/actions.generated.go +2447 -0
  157. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/commands.generated.go +1546 -0
  158. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/common.go +205 -0
  159. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/errors.generated.go +65 -0
  160. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/messages.generated.go +138 -0
  161. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/notifications.generated.go +264 -0
  162. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/state.generated.go +6171 -0
  163. package/extensions/ahp/third_party/agent-host-protocol-go/ahptypes/version.generated.go +31 -0
  164. package/extensions/ahp/third_party/agent-host-protocol-go/go.mod +3 -0
  165. package/extensions/ahp/twins_test.go +91 -0
  166. package/package.json +40 -0
  167. package/proof/PORT.md +121 -0
  168. package/proof/mutations.json +152 -0
  169. package/proof/oracle/LICENSE +21 -0
  170. package/proof/oracle/README.md +120 -0
  171. package/proof/oracle/UPSTREAM.md +7 -0
  172. package/proof/oracle/package.json +68 -0
  173. package/proof/oracle/src/bin/cli.ts +66 -0
  174. package/proof/oracle/src/bin/tunnel.ts +128 -0
  175. package/proof/oracle/src/channels/chat.ts +134 -0
  176. package/proof/oracle/src/channels/root.ts +39 -0
  177. package/proof/oracle/src/channels/session.ts +118 -0
  178. package/proof/oracle/src/channels/terminal.ts +11 -0
  179. package/proof/oracle/src/core/channels.ts +108 -0
  180. package/proof/oracle/src/core/client-workarounds.ts +358 -0
  181. package/proof/oracle/src/core/connection.ts +41 -0
  182. package/proof/oracle/src/core/host.ts +882 -0
  183. package/proof/oracle/src/core/sequencer.ts +75 -0
  184. package/proof/oracle/src/core/state-store.ts +162 -0
  185. package/proof/oracle/src/core/uri.ts +25 -0
  186. package/proof/oracle/src/host/direct-settings.ts +110 -0
  187. package/proof/oracle/src/host/pi-host.ts +220 -0
  188. package/proof/oracle/src/host/serve.ts +71 -0
  189. package/proof/oracle/src/host/terminal-service.ts +346 -0
  190. package/proof/oracle/src/pi/activity.ts +171 -0
  191. package/proof/oracle/src/pi/changeset-service.ts +646 -0
  192. package/proof/oracle/src/pi/changeset-uri.ts +64 -0
  193. package/proof/oracle/src/pi/chat-driver.ts +529 -0
  194. package/proof/oracle/src/pi/completions.ts +160 -0
  195. package/proof/oracle/src/pi/delete-session.ts +53 -0
  196. package/proof/oracle/src/pi/event-mapper.ts +648 -0
  197. package/proof/oracle/src/pi/git-changes.ts +605 -0
  198. package/proof/oracle/src/pi/history.ts +305 -0
  199. package/proof/oracle/src/pi/image-input.ts +56 -0
  200. package/proof/oracle/src/pi/image-mime.ts +8 -0
  201. package/proof/oracle/src/pi/in-process-backend.ts +155 -0
  202. package/proof/oracle/src/pi/message-input.ts +212 -0
  203. package/proof/oracle/src/pi/models.ts +124 -0
  204. package/proof/oracle/src/pi/project-trust.ts +66 -0
  205. package/proof/oracle/src/pi/provider.ts +2 -0
  206. package/proof/oracle/src/pi/resource-paths.ts +80 -0
  207. package/proof/oracle/src/pi/resource-service.ts +356 -0
  208. package/proof/oracle/src/pi/resource-watch-policy.ts +35 -0
  209. package/proof/oracle/src/pi/resource-watch.ts +357 -0
  210. package/proof/oracle/src/pi/session-catalogue.ts +318 -0
  211. package/proof/oracle/src/pi/session-config.ts +84 -0
  212. package/proof/oracle/src/pi/session-history.ts +75 -0
  213. package/proof/oracle/src/pi/session-hydrator.ts +219 -0
  214. package/proof/oracle/src/pi/session-registry.ts +815 -0
  215. package/proof/oracle/src/pi/session-storage.ts +29 -0
  216. package/proof/oracle/src/pi/session-title.ts +18 -0
  217. package/proof/oracle/src/pi/turn-paging.ts +94 -0
  218. package/proof/oracle/src/pi/user-message.ts +66 -0
  219. package/proof/oracle/src/protocol/errors.ts +44 -0
  220. package/proof/oracle/src/protocol/jsonrpc.ts +89 -0
  221. package/proof/oracle/src/protocol/version.ts +39 -0
  222. package/proof/oracle/src/transport/websocket.ts +136 -0
  223. package/proof/oracle/src/tunnel/devtunnel.ts +293 -0
  224. package/proof/oracle/src/tunnel/discovery.ts +37 -0
  225. package/proof/oracle/test/active-turn-reconnect.test.ts +274 -0
  226. package/proof/oracle/test/activity.test.ts +171 -0
  227. package/proof/oracle/test/changeset-lifecycle.test.serial.ts +303 -0
  228. package/proof/oracle/test/changeset-uri.test.ts +37 -0
  229. package/proof/oracle/test/changeset.test.serial.ts +487 -0
  230. package/proof/oracle/test/chat-driver.test.ts +691 -0
  231. package/proof/oracle/test/client-actions.test.ts +409 -0
  232. package/proof/oracle/test/client-workarounds.test.ts +320 -0
  233. package/proof/oracle/test/completions.test.ts +323 -0
  234. package/proof/oracle/test/delete-session.test.ts +142 -0
  235. package/proof/oracle/test/direct-settings.test.ts +81 -0
  236. package/proof/oracle/test/event-mapper.test.ts +619 -0
  237. package/proof/oracle/test/fetch-turns.test.ts +207 -0
  238. package/proof/oracle/test/fixtures/abort.json +711 -0
  239. package/proof/oracle/test/fixtures/bash-long-output.json +1312 -0
  240. package/proof/oracle/test/fixtures/compaction.json +6285 -0
  241. package/proof/oracle/test/fixtures/parallel-tools.json +2622 -0
  242. package/proof/oracle/test/fixtures/plain-text.json +1856 -0
  243. package/proof/oracle/test/fixtures/single-tool.json +1128 -0
  244. package/proof/oracle/test/fixtures/steering.json +3926 -0
  245. package/proof/oracle/test/fixtures/tool-bash.json +2372 -0
  246. package/proof/oracle/test/fixtures/tool-edit.json +3983 -0
  247. package/proof/oracle/test/fixtures/tool-error.json +2850 -0
  248. package/proof/oracle/test/fixtures/tool-find.json +1162 -0
  249. package/proof/oracle/test/fixtures/tool-grep.json +2783 -0
  250. package/proof/oracle/test/fixtures/tool-loop.json +1693 -0
  251. package/proof/oracle/test/fixtures/tool-ls.json +2427 -0
  252. package/proof/oracle/test/fixtures/tool-write.json +1271 -0
  253. package/proof/oracle/test/handshake.test.ts +275 -0
  254. package/proof/oracle/test/harness.ts +153 -0
  255. package/proof/oracle/test/hydrated-session-lifecycle.test.ts +211 -0
  256. package/proof/oracle/test/image-input.test.ts +28 -0
  257. package/proof/oracle/test/image-session.test.ts +69 -0
  258. package/proof/oracle/test/live-turn.test.ts +304 -0
  259. package/proof/oracle/test/mapper-fixtures.test.ts +293 -0
  260. package/proof/oracle/test/message-input.test.ts +181 -0
  261. package/proof/oracle/test/model-discovery.test.ts +170 -0
  262. package/proof/oracle/test/models.test.ts +109 -0
  263. package/proof/oracle/test/pi-host.test.ts +91 -0
  264. package/proof/oracle/test/pi-replay.test.ts +141 -0
  265. package/proof/oracle/test/project-trust.test.ts +125 -0
  266. package/proof/oracle/test/protocol-surface.test.ts +121 -0
  267. package/proof/oracle/test/pty.test.ts +100 -0
  268. package/proof/oracle/test/reconnect.test.ts +389 -0
  269. package/proof/oracle/test/resource-watch-policy.test.ts +78 -0
  270. package/proof/oracle/test/resource-watch.test.serial.ts +536 -0
  271. package/proof/oracle/test/resource.test.ts +499 -0
  272. package/proof/oracle/test/schema.test.ts +88 -0
  273. package/proof/oracle/test/session-catalogue.test.ts +314 -0
  274. package/proof/oracle/test/session-config.test.ts +224 -0
  275. package/proof/oracle/test/session-disposal.test.ts +364 -0
  276. package/proof/oracle/test/session-hydration.test.ts +194 -0
  277. package/proof/oracle/test/session-lifecycle.test.ts +416 -0
  278. package/proof/oracle/test/session-storage.test.serial.ts +32 -0
  279. package/proof/oracle/test/session-summary.test.ts +418 -0
  280. package/proof/oracle/test/subscriptions.test.ts +198 -0
  281. package/proof/oracle/test/support/assertions.ts +32 -0
  282. package/proof/oracle/test/support/async.ts +26 -0
  283. package/proof/oracle/test/support/hydrated-session.ts +205 -0
  284. package/proof/oracle/test/support/images.ts +8 -0
  285. package/proof/oracle/test/support/recorded-fixtures.ts +60 -0
  286. package/proof/oracle/test/support/recorded-scenarios.ts +145 -0
  287. package/proof/oracle/test/support/replay.ts +194 -0
  288. package/proof/oracle/test/support/schema.ts +221 -0
  289. package/proof/oracle/test/support/session-files.ts +12 -0
  290. package/proof/oracle/test/support/session-storage.ts +22 -0
  291. package/proof/oracle/test/support/upstream.ts +25 -0
  292. package/proof/oracle/test/support/watch-events.ts +108 -0
  293. package/proof/oracle/test/terminal-service.test.ts +495 -0
  294. package/proof/oracle/test/truncate.test.ts +230 -0
  295. package/proof/oracle/test/tunnel.test.ts +258 -0
  296. package/proof/oracle/test/upstream-workarounds.test.ts +37 -0
  297. package/proof/oracle/test/uri.test.ts +24 -0
  298. package/proof/oracle/test/watch-events.test.ts +118 -0
  299. package/proof/oracle/tsconfig.json +32 -0
  300. package/proof/proof-piglet/piglet.yaml +18 -0
  301. package/proof/tools/fakellm/go.mod +3 -0
  302. package/proof/tools/fakellm/main.go +109 -0
  303. package/proof/tools/gen-dispatchable.py +30 -0
  304. package/provenance.json +28 -0
@@ -0,0 +1,487 @@
1
+ /** Static Git changesets over the complete AHP subscribe/resourceRead path. */
2
+
3
+ import assert from "node:assert/strict";
4
+ import { execFileSync } from "node:child_process";
5
+ import { randomUUID } from "node:crypto";
6
+ import {
7
+ appendFileSync,
8
+ mkdirSync,
9
+ mkdtempSync,
10
+ realpathSync,
11
+ renameSync,
12
+ rmSync,
13
+ unlinkSync,
14
+ writeFileSync,
15
+ } from "node:fs";
16
+ import { tmpdir } from "node:os";
17
+ import { basename, join } from "node:path";
18
+ import { after, before, describe, it } from "node:test";
19
+ import { pathToFileURL } from "node:url";
20
+ import {
21
+ AhpErrorCodes,
22
+ type ChangesetState,
23
+ ChangesetStatus,
24
+ ContentEncoding,
25
+ JsonRpcErrorCodes,
26
+ type SessionState,
27
+ SUPPORTED_PROTOCOL_VERSIONS,
28
+ } from "@microsoft/agent-host-protocol";
29
+ import { AhpClient } from "@microsoft/agent-host-protocol/client";
30
+ import { WebSocketTransport } from "@microsoft/agent-host-protocol/ws";
31
+ import { installRootChannel } from "../src/channels/root.ts";
32
+ import { sessionUri } from "../src/core/channels.ts";
33
+ import { AhpHost } from "../src/core/host.ts";
34
+ import { createPiHost, type PiHost } from "../src/host/pi-host.ts";
35
+ import { ChangesetService } from "../src/pi/changeset-service.ts";
36
+ import { piChangesetUri } from "../src/pi/changeset-uri.ts";
37
+ import { GitChanges } from "../src/pi/git-changes.ts";
38
+ import { PiSessionCatalogue } from "../src/pi/session-catalogue.ts";
39
+ import { SessionHydrator } from "../src/pi/session-hydrator.ts";
40
+ import { SessionRegistry } from "../src/pi/session-registry.ts";
41
+ import { type RunningServer, serveWebSocket } from "../src/transport/websocket.ts";
42
+ import { expectRpcError, must } from "./support/assertions.ts";
43
+ import { eventually } from "./support/async.ts";
44
+ import { startHydratedSessionFixture } from "./support/hydrated-session.ts";
45
+ import { assertValid } from "./support/schema.ts";
46
+ import { persistentSessionManagerFactory, persistentSessionStorage } from "./support/session-storage.ts";
47
+
48
+ function git(cwd: string, ...args: string[]): string {
49
+ return execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
50
+ }
51
+
52
+ function fileName(file: ChangesetState["files"][number]): string {
53
+ return basename(decodeURIComponent(new URL(file.edit.after?.uri ?? file.edit.before?.uri ?? "").pathname));
54
+ }
55
+
56
+ async function readyState(client: AhpClient, host: AhpHost, channel: string): Promise<ChangesetState> {
57
+ await eventually(`${channel} to become ready`, () => {
58
+ return (host.store.get(channel) as ChangesetState | undefined)?.status === ChangesetStatus.Ready;
59
+ });
60
+ const { result } = await client.subscribe(channel);
61
+ const state = must(result.snapshot, `${channel} snapshot`).state as ChangesetState;
62
+ assertValid("state", "ChangesetState", state);
63
+ return state;
64
+ }
65
+
66
+ describe("Git changesets", () => {
67
+ let workspace: string;
68
+ let sessionRoot: string;
69
+ let built: PiHost;
70
+ let server: RunningServer;
71
+ let client: AhpClient;
72
+
73
+ before(async () => {
74
+ workspace = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-"));
75
+ sessionRoot = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-sessions-"));
76
+ git(workspace, "init", "-q");
77
+ git(workspace, "config", "user.email", "test@example.com");
78
+ git(workspace, "config", "user.name", "pi-ahp test");
79
+ writeFileSync(join(workspace, "edited.txt"), "base\n");
80
+ writeFileSync(join(workspace, "deleted.txt"), "deleted\n");
81
+ writeFileSync(join(workspace, "old-name.txt"), "renamed\n");
82
+ writeFileSync(join(workspace, "tab\tname.txt"), "tab base\n");
83
+ git(workspace, "add", ".");
84
+ git(workspace, "commit", "-qm", "base");
85
+
86
+ appendFileSync(join(workspace, "edited.txt"), "committed\n");
87
+ unlinkSync(join(workspace, "deleted.txt"));
88
+ renameSync(join(workspace, "old-name.txt"), join(workspace, "new-name.txt"));
89
+ writeFileSync(join(workspace, "committed-only.txt"), "committed only\n");
90
+ appendFileSync(join(workspace, "tab\tname.txt"), "tab committed\n");
91
+ git(workspace, "add", ".");
92
+ git(workspace, "commit", "-qm", "latest");
93
+
94
+ appendFileSync(join(workspace, "edited.txt"), "dirty\n");
95
+ appendFileSync(join(workspace, "tab\tname.txt"), "tab dirty\n");
96
+ writeFileSync(join(workspace, "untracked.txt"), "untracked\n");
97
+
98
+ built = await createPiHost({
99
+ workingDirectory: workspace,
100
+ modelRuntime: { getAvailable: async () => [] },
101
+ sessionStorage: persistentSessionStorage(sessionRoot),
102
+ createBackend: () => ({
103
+ subscribe: () => () => {},
104
+ prompt: async () => {},
105
+ steer: async () => {},
106
+ abort: async () => {},
107
+ }),
108
+ deleteFile: () => ({ ok: true }),
109
+ });
110
+ server = await serveWebSocket(built.host, { host: "127.0.0.1", port: 0 });
111
+ client = new AhpClient(await WebSocketTransport.connect(`ws://127.0.0.1:${server.port}`));
112
+ client.connect();
113
+ await client.initialize({ clientId: "changeset-client", protocolVersions: SUPPORTED_PROTOCOL_VERSIONS });
114
+ });
115
+
116
+ after(async () => {
117
+ await client.shutdown();
118
+ await server.close();
119
+ built.terminals.shutdown();
120
+ await Promise.all([built.changesets.dispose(), built.watches.dispose()]);
121
+ rmSync(workspace, { recursive: true, force: true });
122
+ rmSync(sessionRoot, { recursive: true, force: true });
123
+ });
124
+
125
+ it("keeps the latest commit separate from uncommitted work and refreshes it", async () => {
126
+ const session = sessionUri(randomUUID());
127
+ await client.request("createSession", { channel: session });
128
+ await client.subscribe(session);
129
+ await eventually("the Git changeset catalogue", () => {
130
+ return ((built.host.store.get(session) as SessionState).changesets?.length ?? 0) === 2;
131
+ });
132
+ const sessionState = built.host.store.get(session) as SessionState;
133
+ assertValid("state", "SessionState", sessionState);
134
+ assert.deepEqual(
135
+ sessionState.changesets?.map(({ label, changeKind }) => ({ label, changeKind })),
136
+ [
137
+ { label: "Latest Commit", changeKind: "branch" },
138
+ { label: "Uncommitted Changes", changeKind: "uncommitted" },
139
+ ],
140
+ );
141
+ const [latestChannel, uncommittedChannel] = sessionState.changesets?.map((item) => item.uriTemplate) ?? [];
142
+ assert.ok(latestChannel);
143
+ assert.ok(uncommittedChannel);
144
+
145
+ await Promise.all([client.subscribe(latestChannel), client.subscribe(uncommittedChannel)]);
146
+ const latest = await readyState(client, built.host, latestChannel);
147
+ const uncommitted = await readyState(client, built.host, uncommittedChannel);
148
+ assert.deepEqual(latest.files.map(fileName).sort(), [
149
+ "committed-only.txt",
150
+ "deleted.txt",
151
+ "edited.txt",
152
+ "new-name.txt",
153
+ "tab\tname.txt",
154
+ ]);
155
+ assert.deepEqual(uncommitted.files.map(fileName).sort(), ["edited.txt", "tab\tname.txt", "untracked.txt"]);
156
+
157
+ const latestEdit = must(latest.files.find((file) => fileName(file) === "edited.txt"));
158
+ const uncommittedEdit = must(uncommitted.files.find((file) => fileName(file) === "edited.txt"));
159
+ const latestAfter = must(latestEdit.edit.after);
160
+ const uncommittedBeforeRef = must(uncommittedEdit.edit.before);
161
+ assert.ok(latestAfter.content.uri.startsWith("git-blob:"));
162
+ assert.match(new URL(latestAfter.content.uri).search, /^\?[A-Za-z0-9_-]+$/u);
163
+ assert.ok(uncommittedBeforeRef.content.uri.startsWith("git-blob:"));
164
+ const committedContent = await client.resourceRead({
165
+ uri: latestAfter.content.uri,
166
+ encoding: ContentEncoding.Utf8,
167
+ });
168
+ const uncommittedBefore = await client.resourceRead({
169
+ uri: uncommittedBeforeRef.content.uri,
170
+ encoding: ContentEncoding.Utf8,
171
+ });
172
+ assert.equal(committedContent.data, "base\ncommitted\n");
173
+ assert.equal(uncommittedBefore.data, committedContent.data);
174
+ assert.equal(built.changesets.activeWatcherCount, 1);
175
+
176
+ appendFileSync(join(workspace, "edited.txt"), "later\n");
177
+ await eventually("the uncommitted changeset to refresh", () => {
178
+ const state = built.host.store.get(uncommittedChannel) as ChangesetState | undefined;
179
+ return (
180
+ state?.status === ChangesetStatus.Ready &&
181
+ state.files.find((file) => fileName(file) === "edited.txt")?.edit.diff?.added === 2
182
+ );
183
+ });
184
+ const unchangedLatest = built.host.store.get(latestChannel) as ChangesetState;
185
+ const unchangedLatestFile = must(unchangedLatest.files.find((file) => fileName(file) === "edited.txt"));
186
+ const latestAfterRefresh = await client.resourceRead({
187
+ uri: must(unchangedLatestFile.edit.after).content.uri,
188
+ encoding: ContentEncoding.Utf8,
189
+ });
190
+ assert.equal(latestAfterRefresh.data, "base\ncommitted\n");
191
+
192
+ const liveFile = join(workspace, "live-list.txt");
193
+ writeFileSync(liveFile, "live\n");
194
+ await eventually("a newly added file to enter the changeset", () => {
195
+ const state = built.host.store.get(uncommittedChannel) as ChangesetState | undefined;
196
+ return state?.status === ChangesetStatus.Ready && state.files.some((file) => fileName(file) === "live-list.txt");
197
+ });
198
+ appendFileSync(liveFile, "updated\n");
199
+ await eventually("the added file to receive live updates", () => {
200
+ const state = built.host.store.get(uncommittedChannel) as ChangesetState | undefined;
201
+ return state?.files.find((file) => fileName(file) === "live-list.txt")?.edit.diff?.added === 2;
202
+ });
203
+ unlinkSync(liveFile);
204
+ await eventually("a deleted untracked file to leave the changeset", () => {
205
+ const state = built.host.store.get(uncommittedChannel) as ChangesetState | undefined;
206
+ return state?.status === ChangesetStatus.Ready && !state.files.some((file) => fileName(file) === "live-list.txt");
207
+ });
208
+
209
+ git(workspace, "add", ".");
210
+ git(workspace, "commit", "-qm", "move uncommitted work into latest");
211
+ await eventually("the committed work to move between changesets", () => {
212
+ const latestState = built.host.store.get(latestChannel) as ChangesetState | undefined;
213
+ const uncommittedState = built.host.store.get(uncommittedChannel) as ChangesetState | undefined;
214
+ return (
215
+ latestState?.status === ChangesetStatus.Ready &&
216
+ latestState.files.some((file) => fileName(file) === "untracked.txt") &&
217
+ uncommittedState?.status === ChangesetStatus.Ready &&
218
+ uncommittedState.files.length === 0
219
+ );
220
+ });
221
+ await Promise.all([client.unsubscribe(latestChannel), client.unsubscribe(uncommittedChannel)]);
222
+ await eventually("the unobserved Git watcher to close", () => built.changesets.activeWatcherCount === 0);
223
+ await client.subscribe(uncommittedChannel);
224
+ await eventually("the observed Git watcher to reopen", () => built.changesets.activeWatcherCount === 1);
225
+
226
+ await client.request("disposeSession", { channel: session });
227
+ assert.equal(built.host.store.has(latestChannel), false);
228
+ assert.equal(built.host.store.has(uncommittedChannel), false);
229
+ assert.equal(built.changesets.activeWatcherCount, 0);
230
+ });
231
+
232
+ it("limits changes to the session working-directory subtree", async () => {
233
+ const scopedDirectory = join(workspace, "scope");
234
+ const siblingDirectory = join(workspace, "sibling");
235
+ mkdirSync(scopedDirectory);
236
+ mkdirSync(siblingDirectory);
237
+ writeFileSync(join(scopedDirectory, "inside.txt"), "base\n");
238
+ writeFileSync(join(siblingDirectory, "outside.txt"), "base\n");
239
+ git(workspace, "add", ".");
240
+ git(workspace, "commit", "-qm", "add scoped files");
241
+ appendFileSync(join(scopedDirectory, "inside.txt"), "changed\n");
242
+ appendFileSync(join(siblingDirectory, "outside.txt"), "changed\n");
243
+
244
+ const session = sessionUri(randomUUID());
245
+ try {
246
+ await client.request("createSession", {
247
+ channel: session,
248
+ workingDirectories: [pathToFileURL(scopedDirectory).toString()],
249
+ });
250
+ await built.changesets.attach(must(built.sessions.get(session)));
251
+ const state = built.host.store.get(session) as SessionState;
252
+ const channel = must(state.changesets?.find(({ changeKind }) => changeKind === "uncommitted")).uriTemplate;
253
+ await client.subscribe(channel);
254
+ assert.deepEqual((await readyState(client, built.host, channel)).files.map(fileName), ["inside.txt"]);
255
+ await client.request("disposeSession", { channel: session });
256
+ } finally {
257
+ rmSync(scopedDirectory, { recursive: true, force: true });
258
+ rmSync(siblingDirectory, { recursive: true, force: true });
259
+ }
260
+ });
261
+
262
+ it("ignores ambient Git repository overrides", async () => {
263
+ const other = mkdtempSync(join(tmpdir(), "pi-ahp-other-git-"));
264
+ git(other, "init", "-q");
265
+ const previous = {
266
+ GIT_DIR: process.env.GIT_DIR,
267
+ GIT_INDEX_FILE: process.env.GIT_INDEX_FILE,
268
+ GIT_WORK_TREE: process.env.GIT_WORK_TREE,
269
+ };
270
+ try {
271
+ process.env.GIT_DIR = join(other, ".git");
272
+ process.env.GIT_INDEX_FILE = join(other, "foreign-index");
273
+ process.env.GIT_WORK_TREE = other;
274
+ const inspected = await new GitChanges().inspect(workspace);
275
+ assert.equal(inspected?.repositoryRoot, realpathSync(workspace));
276
+ } finally {
277
+ for (const [key, value] of Object.entries(previous)) {
278
+ if (value === undefined) delete process.env[key];
279
+ else process.env[key] = value;
280
+ }
281
+ rmSync(other, { recursive: true, force: true });
282
+ }
283
+ });
284
+
285
+ it("omits changesets when Git is unavailable", async () => {
286
+ const directory = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-no-git-"));
287
+ const emptyPath = mkdtempSync(join(tmpdir(), "pi-ahp-empty-path-"));
288
+ const session = sessionUri(randomUUID());
289
+ const previousPath = process.env.PATH;
290
+ try {
291
+ process.env.PATH = emptyPath;
292
+ await client.request("createSession", {
293
+ channel: session,
294
+ workingDirectories: [pathToFileURL(directory).toString()],
295
+ });
296
+ await built.changesets.attach(must(built.sessions.get(session)));
297
+ assert.equal((built.host.store.get(session) as SessionState).changesets, undefined);
298
+ } finally {
299
+ process.env.PATH = previousPath;
300
+ if (built.sessions.has(session)) await client.request("disposeSession", { channel: session });
301
+ rmSync(directory, { recursive: true, force: true });
302
+ rmSync(emptyPath, { recursive: true, force: true });
303
+ }
304
+ });
305
+
306
+ it("omits changesets outside Git workspaces", async () => {
307
+ const directory = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-plain-"));
308
+ const session = sessionUri(randomUUID());
309
+ try {
310
+ await client.request("createSession", {
311
+ channel: session,
312
+ workingDirectories: [pathToFileURL(directory).toString()],
313
+ });
314
+ await built.changesets.attach(must(built.sessions.get(session)));
315
+ assert.equal((built.host.store.get(session) as SessionState).changesets, undefined);
316
+ await client.request("disposeSession", { channel: session });
317
+ } finally {
318
+ rmSync(directory, { recursive: true, force: true });
319
+ }
320
+ });
321
+
322
+ it("omits Latest Commit before the repository has a commit", async () => {
323
+ const emptyRepository = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-empty-"));
324
+ git(emptyRepository, "init", "-q");
325
+ writeFileSync(join(emptyRepository, "first.txt"), "first\n");
326
+ const session = sessionUri(randomUUID());
327
+ try {
328
+ await client.request("createSession", {
329
+ channel: session,
330
+ workingDirectories: [pathToFileURL(emptyRepository).toString()],
331
+ });
332
+ await built.changesets.attach(must(built.sessions.get(session)));
333
+ const state = built.host.store.get(session) as SessionState;
334
+ assert.deepEqual(
335
+ state.changesets?.map(({ label }) => label),
336
+ ["Uncommitted Changes"],
337
+ );
338
+ const channel = must(state.changesets?.[0]).uriTemplate;
339
+ await client.subscribe(channel);
340
+ assert.deepEqual((await readyState(client, built.host, channel)).files.map(fileName), ["first.txt"]);
341
+ await client.request("disposeSession", { channel: session });
342
+ } finally {
343
+ rmSync(emptyRepository, { recursive: true, force: true });
344
+ }
345
+ });
346
+ });
347
+
348
+ it("resumes changeset updates when durable session deletion fails", async () => {
349
+ const workspace = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-delete-failure-"));
350
+ git(workspace, "init", "-q");
351
+ git(workspace, "config", "user.email", "test@example.com");
352
+ git(workspace, "config", "user.name", "pi-ahp test");
353
+ const file = join(workspace, "tracked.txt");
354
+ const sessionRoot = mkdtempSync(join(tmpdir(), "pi-ahp-changeset-delete-sessions-"));
355
+ writeFileSync(file, "base\n");
356
+ git(workspace, "add", ".");
357
+ git(workspace, "commit", "-qm", "base");
358
+ appendFileSync(file, "before failure\n");
359
+ let deletionAttempts = 0;
360
+ const built = await createPiHost({
361
+ workingDirectory: workspace,
362
+ modelRuntime: { getAvailable: async () => [] },
363
+ sessionStorage: persistentSessionStorage(sessionRoot),
364
+ createBackend: () => ({
365
+ subscribe: () => () => {},
366
+ prompt: async () => {},
367
+ steer: async () => {},
368
+ abort: async () => {},
369
+ }),
370
+ deleteFile: () => {
371
+ deletionAttempts += 1;
372
+ return { ok: false, error: "deletion blocked" };
373
+ },
374
+ });
375
+ const server = await serveWebSocket(built.host, { host: "127.0.0.1", port: 0 });
376
+ const client = new AhpClient(await WebSocketTransport.connect(`ws://127.0.0.1:${server.port}`));
377
+ client.connect();
378
+ const session = sessionUri(randomUUID());
379
+
380
+ try {
381
+ await client.initialize({ clientId: "changeset-delete-failure", protocolVersions: SUPPORTED_PROTOCOL_VERSIONS });
382
+ await client.request("createSession", { channel: session });
383
+ await built.changesets.attach(must(built.sessions.get(session)));
384
+ const state = built.host.store.get(session) as SessionState;
385
+ const channel = must(state.changesets?.find(({ changeKind }) => changeKind === "uncommitted")).uriTemplate;
386
+ await client.subscribe(channel);
387
+ await readyState(client, built.host, channel);
388
+
389
+ await expectRpcError(
390
+ client.request("disposeSession", { channel: session }),
391
+ JsonRpcErrorCodes.InternalError,
392
+ "failed durable deletion",
393
+ );
394
+ assert.equal(deletionAttempts, 1);
395
+ assert.equal(built.host.store.has(session), true);
396
+ assert.equal(built.host.store.has(channel), true);
397
+ await eventually("the changeset watcher to resume", () => built.changesets.activeWatcherCount === 1);
398
+
399
+ appendFileSync(file, "after failure\n");
400
+ await eventually("changes after failed deletion", () => {
401
+ const changeset = built.host.store.get(channel) as ChangesetState | undefined;
402
+ return changeset?.status === ChangesetStatus.Ready && changeset.files[0]?.edit.diff?.added === 2;
403
+ });
404
+ } finally {
405
+ await client.shutdown();
406
+ await server.close();
407
+ built.terminals.shutdown();
408
+ await Promise.all([built.changesets.dispose(), built.watches.dispose()]);
409
+ rmSync(workspace, { recursive: true, force: true });
410
+ rmSync(sessionRoot, { recursive: true, force: true });
411
+ }
412
+ });
413
+
414
+ it("hydrates the parent session when its changeset is subscribed directly", async () => {
415
+ const source = await startHydratedSessionFixture();
416
+ git(source.workspace, "init", "-q");
417
+ git(source.workspace, "config", "user.email", "test@example.com");
418
+ git(source.workspace, "config", "user.name", "pi-ahp test");
419
+ writeFileSync(join(source.workspace, "tracked.txt"), "tracked\n");
420
+ git(source.workspace, "add", ".");
421
+ git(source.workspace, "commit", "-qm", "tracked");
422
+
423
+ const host = new AhpHost();
424
+ installRootChannel(host, []);
425
+ const catalogue = new PiSessionCatalogue(source.root);
426
+ const sessions = new SessionRegistry({
427
+ host,
428
+ createSessionManager: persistentSessionManagerFactory(source.root),
429
+ findSessionFile: (sessionId) => catalogue.findSessionFile(sessionId),
430
+ deleteFile: () => ({ ok: true }),
431
+ });
432
+ const sessionHydrator = new SessionHydrator({
433
+ host,
434
+ catalogue,
435
+ isLive: (session) => sessions.has(session),
436
+ isDisposing: (session) => sessions.isDisposing(session),
437
+ adopt: (session) => void sessions.adopt(session),
438
+ });
439
+ const changesets = new ChangesetService(host, {
440
+ getSession: (sessionId) => sessions.get(sessionUri(sessionId)),
441
+ getSessionByChat: (chat) => sessions.getByChat(chat),
442
+ hydrateSession: async (sessionId) => {
443
+ await sessionHydrator.hydrate(sessionUri(sessionId));
444
+ return sessions.get(sessionUri(sessionId));
445
+ },
446
+ onSessionAvailable: (listener) => sessions.onSessionAvailable(listener),
447
+ onSessionDeletionCommitted: (listener) => sessions.onSessionDeletionCommitted(listener),
448
+ stateGraceMs: 0,
449
+ });
450
+ host.serve({
451
+ hydrator: {
452
+ async hydrate(channel) {
453
+ return (await changesets.hydrate(channel)) || sessionHydrator.hydrate(channel);
454
+ },
455
+ },
456
+ });
457
+ const server = await serveWebSocket(host, { host: "127.0.0.1", port: 0 });
458
+ const client = new AhpClient(await WebSocketTransport.connect(`ws://127.0.0.1:${server.port}`));
459
+ client.connect();
460
+
461
+ try {
462
+ await client.initialize({ clientId: "direct-changeset-client", protocolVersions: SUPPORTED_PROTOCOL_VERSIONS });
463
+ await expectRpcError(
464
+ client.subscribe(`ahp-changeset:/${source.sessionId}/unknown`),
465
+ AhpErrorCodes.NotFound,
466
+ "unknown changeset",
467
+ );
468
+ assert.equal(sessions.has(sessionUri(source.sessionId)), false);
469
+
470
+ const channel = piChangesetUri(source.sessionId, "latest-commit");
471
+ const { result } = await client.subscribe(channel);
472
+ assert.equal(result.snapshot?.resource, channel);
473
+ assert.equal(sessions.has(sessionUri(source.sessionId)), true);
474
+ assert.deepEqual((await readyState(client, host, channel)).files.map(fileName), ["tracked.txt"]);
475
+
476
+ await client.unsubscribe(channel);
477
+ await eventually("the unobserved changeset state to be evicted", () => !host.store.has(channel));
478
+ const restored = await client.subscribe(channel);
479
+ assert.equal(restored.result.snapshot?.resource, channel);
480
+ assert.deepEqual((await readyState(client, host, channel)).files.map(fileName), ["tracked.txt"]);
481
+ } finally {
482
+ await client.shutdown();
483
+ await server.close();
484
+ await changesets.dispose();
485
+ await source.close();
486
+ }
487
+ });