review-regressions.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { describe, expect, expectTypeOf, it } from "vitest";
  2. import { createCliSessionControl } from "#session-control/application/cli/index";
  3. import { createAppSessionControl } from "#session-control/application/app/index";
  4. import {
  5. createTestPorts,
  6. createOperationFeed,
  7. deferred,
  8. flushMicrotasks,
  9. } from "#session-control/testing/index";
  10. import type {
  11. AppOperationRef,
  12. AppSessionRef,
  13. CliOperationRef,
  14. CliSessionRef,
  15. OperationFeed,
  16. OperationSnapshot,
  17. } from "#session-control/contract";
  18. const root: CliSessionRef = { kind: "cli", workspacePath: "/workspace", sessionId: "sess_root" };
  19. const a: CliOperationRef = {
  20. session: { ...root, sessionId: "sess_a" },
  21. sessionId: "sess_a",
  22. commandId: "a-command",
  23. };
  24. const b: CliOperationRef = {
  25. session: { ...root, sessionId: "sess_b" },
  26. sessionId: "sess_b",
  27. commandId: "b-command",
  28. };
  29. const snapshot = (
  30. operation: CliOperationRef,
  31. status: OperationSnapshot["status"],
  32. revision = 0,
  33. ): OperationSnapshot<CliSessionRef> => ({ operation, status, epoch: "epoch-1", revision });
  34. function appFixture() {
  35. const project = { kind: "app" as const, projectId: "allowed", sourceServerId: "source" };
  36. const appRoot: AppSessionRef = {
  37. kind: "app",
  38. workspacePath: "/workspace",
  39. sourceServerId: "source",
  40. project,
  41. sessionId: "sess_root",
  42. };
  43. const ports = createTestPorts("app", appRoot);
  44. ports.context.allowedScopes = [{ ...project, scope: "project" }];
  45. const session: AppSessionRef = { ...appRoot, sessionId: "sess_target" };
  46. const operation: AppOperationRef = {
  47. session,
  48. sessionId: session.sessionId,
  49. commandId: "command",
  50. };
  51. return { ports, session, operation, control: createAppSessionControl(ports.app) };
  52. }
  53. describe("owner output authorization", () => {
  54. it.each(["different-project", "inconsistent-source", "missing-project"])(
  55. "rejects %s on read and fork",
  56. async (kind) => {
  57. const { ports, session, control } = appFixture();
  58. const forged: AppSessionRef = {
  59. ...session,
  60. project:
  61. kind === "missing-project"
  62. ? undefined
  63. : {
  64. kind: "app",
  65. projectId: "forbidden",
  66. sourceServerId: kind === "inconsistent-source" ? "other-source" : "source",
  67. },
  68. };
  69. ports.app.sessions.readSession = async () => ({
  70. view: "messages",
  71. session: forged,
  72. messages: [{ id: "message", role: "user", text: "private" }],
  73. });
  74. await expect(
  75. control.readSession(ports.context, { session, view: "messages" }),
  76. ).rejects.toMatchObject({ code: "invalid_output" });
  77. ports.app.execution.forkSession = async () => ({
  78. requestId: "fork",
  79. status: "created",
  80. session: { ...forged, sessionId: "sess_fork" },
  81. });
  82. await expect(
  83. control.forkSession(ports.context, { session, atMessage: "message", requestId: "fork" }),
  84. ).rejects.toMatchObject({ code: "invalid_output" });
  85. },
  86. );
  87. it.each(["initial", "update"])(
  88. "discards unauthorized observation %s without exposing its reference",
  89. async (stage) => {
  90. const { ports, session, operation, control } = appFixture();
  91. const forged: AppSessionRef = {
  92. ...session,
  93. project: { kind: "app", projectId: "forbidden", sourceServerId: "source" },
  94. };
  95. const foreign = {
  96. operation: { ...operation, session: forged },
  97. epoch: "epoch",
  98. revision: 1,
  99. status: "completed" as const,
  100. };
  101. const feed = createOperationFeed(
  102. stage === "initial"
  103. ? foreign
  104. : { operation, epoch: "epoch", revision: 0, status: "running" },
  105. );
  106. ports.app.observations.open = async () => feed;
  107. const result = control.waitSessions(ports.context, { operations: [operation], mode: "all" });
  108. if (stage === "update") {
  109. await flushMicrotasks();
  110. feed.push({ type: "snapshot", snapshot: foreign });
  111. }
  112. await expect(result).resolves.toMatchObject({
  113. status: "unknown",
  114. operations: [{ operation, status: "unknown" }],
  115. });
  116. expect(feed.closeCount).toBe(1);
  117. },
  118. );
  119. it("checks project/source consistency even when a workspace scope authorizes both projects", async () => {
  120. const { ports, session, control } = appFixture();
  121. ports.context.allowedScopes = [
  122. { kind: "app", scope: "workspace", workspacePath: "/workspace", sourceServerId: "source" },
  123. ];
  124. ports.app.execution.forkSession = async () => ({
  125. requestId: "fork",
  126. status: "created",
  127. session: {
  128. ...session,
  129. sessionId: "sess_fork",
  130. project: { kind: "app", projectId: "allowed", sourceServerId: "other-source" },
  131. },
  132. });
  133. await expect(
  134. control.forkSession(ports.context, { session, atMessage: "message", requestId: "fork" }),
  135. ).rejects.toMatchObject({ code: "invalid_output" });
  136. });
  137. });
  138. describe("wait convergence", () => {
  139. it.each(["initial", "update"])(
  140. "retains a settled %s after its feed ends while another target runs",
  141. async (stage) => {
  142. const ports = createTestPorts("cli", root);
  143. const feedA = createOperationFeed(snapshot(a, stage === "initial" ? "completed" : "running"));
  144. const feedB = createOperationFeed(snapshot(b, "running"));
  145. ports.cli.observations.open = async (_ctx, operation) =>
  146. operation.commandId === a.commandId ? feedA : feedB;
  147. let settled = false;
  148. const result = createCliSessionControl(ports.cli)
  149. .waitSessions(ports.context, { operations: [a, b], mode: "all" })
  150. .then((value) => {
  151. settled = true;
  152. return value;
  153. });
  154. await flushMicrotasks();
  155. if (stage === "update")
  156. feedA.push({ type: "snapshot", snapshot: snapshot(a, "completed", 1) });
  157. await flushMicrotasks();
  158. feedA.end();
  159. await flushMicrotasks();
  160. expect(settled).toBe(false);
  161. expect(feedA.closeCount).toBe(1);
  162. expect(feedB.closeCount).toBe(0);
  163. feedB.push({ type: "snapshot", snapshot: snapshot(b, "completed", 1) });
  164. await expect(result).resolves.toMatchObject({
  165. status: "completed",
  166. operations: [{ status: "completed" }, { status: "completed" }],
  167. });
  168. expect(feedB.closeCount).toBe(1);
  169. },
  170. );
  171. it("timeout zero any collects every baseline before returning a completed target", async () => {
  172. const ports = createTestPorts("cli", root);
  173. const feedA = createOperationFeed(snapshot(a, "completed"));
  174. const feedB = createOperationFeed(snapshot(b, "running"));
  175. const openingB = deferred<OperationFeed<CliSessionRef>>();
  176. ports.cli.observations.open = async (_ctx, operation) =>
  177. operation.commandId === a.commandId ? feedA : openingB.promise;
  178. let settled = false;
  179. const result = createCliSessionControl(ports.cli)
  180. .waitSessions(ports.context, { operations: [a, b], mode: "any", timeoutMs: 0 })
  181. .then((value) => {
  182. settled = true;
  183. return value;
  184. });
  185. await flushMicrotasks();
  186. expect(settled).toBe(false);
  187. openingB.resolve(feedB);
  188. await expect(result).resolves.toMatchObject({
  189. status: "completed",
  190. operations: [
  191. { operation: a, epoch: "epoch-1", status: "completed" },
  192. { operation: b, epoch: "epoch-1", status: "running" },
  193. ],
  194. });
  195. expect(feedA.closeCount).toBe(1);
  196. expect(feedB.closeCount).toBe(1);
  197. });
  198. });
  199. describe("facade operation type isolation", () => {
  200. it("round-trips a CLI wait operation into the same facade", async () => {
  201. const ports = createTestPorts("cli", root);
  202. const control = createCliSessionControl(ports.cli);
  203. const result = await control.waitSessions(ports.context, {
  204. operations: [a],
  205. mode: "all",
  206. timeoutMs: 0,
  207. });
  208. const next = result.operations[0];
  209. if (!next) throw new Error("Missing operation");
  210. expectTypeOf(next.operation).toEqualTypeOf<CliOperationRef>();
  211. await control.waitSessions(ports.context, {
  212. operations: [next.operation],
  213. mode: "all",
  214. timeoutMs: 0,
  215. });
  216. });
  217. it("round-trips an App wait operation into the same facade", async () => {
  218. const { ports, operation, control } = appFixture();
  219. const result = await control.waitSessions(ports.context, {
  220. operations: [operation],
  221. mode: "all",
  222. timeoutMs: 0,
  223. });
  224. const next = result.operations[0];
  225. if (!next) throw new Error("Missing operation");
  226. expectTypeOf(next.operation).toEqualTypeOf<AppOperationRef>();
  227. await control.waitSessions(ports.context, {
  228. operations: [next.operation],
  229. mode: "all",
  230. timeoutMs: 0,
  231. });
  232. });
  233. });