conversation-session-formal-admission-contract.mjs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { resolve } from "node:path";
  2. import {
  3. validateProviderFixture,
  4. validateRequestArray,
  5. validateRequestLedger,
  6. validateSyntheticReasons,
  7. } from "./conversation-session-formal-admission-ledger.mjs";
  8. import {
  9. diagnostic,
  10. isRecord,
  11. readJson,
  12. toRepoPath,
  13. } from "./conversation-session-formal-admission-utils.mjs";
  14. import {
  15. resolveProviderFixturePaths,
  16. validateFileFixtures,
  17. validateStaticFilePaths,
  18. } from "./conversation-session-formal-admission-paths.mjs";
  19. export async function validateFormalAdmissionManifest({
  20. absoluteSpecPath,
  21. caseFixture,
  22. caseFixturePath,
  23. caseName,
  24. manifest,
  25. manifestPath,
  26. reasons,
  27. root,
  28. specPath,
  29. }) {
  30. const manifestSubjectPath = toRepoPath(root, manifestPath);
  31. if (!isRecord(manifest)) {
  32. reasons.push(
  33. diagnostic({
  34. code: "INVALID_MANIFEST_SHAPE",
  35. message: "case manifest must be a JSON object",
  36. specPath,
  37. subjectPath: manifestSubjectPath,
  38. }),
  39. );
  40. return;
  41. }
  42. validateManifestIdentity({
  43. absoluteSpecPath,
  44. caseName,
  45. manifest,
  46. manifestSubjectPath,
  47. reasons,
  48. root,
  49. specPath,
  50. });
  51. validateCaseFixtureIdentity({
  52. absoluteSpecPath,
  53. caseFixture,
  54. caseFixturePath,
  55. caseName,
  56. reasons,
  57. root,
  58. specPath,
  59. });
  60. const providerFixtures = validateStringArray(
  61. manifest.providerFixtures,
  62. "providerFixtures",
  63. manifestSubjectPath,
  64. specPath,
  65. reasons,
  66. );
  67. const fileFixtures = validateFileFixtures(
  68. manifest.fileFixtures ?? [],
  69. manifestSubjectPath,
  70. specPath,
  71. reasons,
  72. );
  73. const requests = validateRequestArray(manifest.requests, manifestSubjectPath, specPath, reasons);
  74. if (!providerFixtures || !fileFixtures || !requests) {
  75. return;
  76. }
  77. const desktopRoot = resolve(root, "packages/desktop");
  78. const providerFixtureRoot = resolve(desktopRoot, "test/e2e/fixtures/upstream");
  79. const staticFileFixtureRoot = resolve(desktopRoot, "test/e2e/fixtures");
  80. const resolvedProviderFixtures = await resolveProviderFixturePaths({
  81. desktopRoot,
  82. providerFixtureRoot,
  83. providerFixtures,
  84. reasons,
  85. root,
  86. specPath,
  87. });
  88. if (!resolvedProviderFixtures.some((fixture) => fixture.absolutePath === caseFixturePath)) {
  89. reasons.push(
  90. diagnostic({
  91. code: "MANIFEST_OMITS_CASE_FIXTURE",
  92. message: "providerFixtures does not include the case-local provider fixture",
  93. specPath,
  94. subjectPath: manifestSubjectPath,
  95. }),
  96. );
  97. }
  98. const providerFiles = await readProviderFiles({
  99. canonicalCommonPath: resolve(providerFixtureRoot, "common.json"),
  100. reasons,
  101. resolvedProviderFixtures,
  102. root,
  103. specPath,
  104. });
  105. await validateStaticFilePaths({
  106. desktopRoot,
  107. fixtureRoot: staticFileFixtureRoot,
  108. reasons,
  109. root,
  110. specPath,
  111. staticPaths: fileFixtures.staticPaths,
  112. });
  113. validateProviderPolicy({
  114. manifest,
  115. manifestSubjectPath,
  116. reasons,
  117. requests,
  118. specPath,
  119. });
  120. validateSyntheticReasons(requests, manifestSubjectPath, specPath, reasons);
  121. for (const providerFile of providerFiles) {
  122. validateSyntheticReasons(
  123. providerFile.fixtures.map((fixture) => ({
  124. id: fixture.id,
  125. ...(isRecord(fixture.metadata) ? fixture.metadata : {}),
  126. })),
  127. providerFile.subjectPath,
  128. specPath,
  129. reasons,
  130. );
  131. }
  132. const declaredCaseFixture = providerFiles.find(
  133. (fixture) => fixture.absolutePath === caseFixturePath,
  134. );
  135. const fallbackCaseFixtures = validateProviderFixture(
  136. caseFixture,
  137. toRepoPath(root, caseFixturePath),
  138. specPath,
  139. reasons,
  140. { reportShapeError: false },
  141. );
  142. const caseFixtures = declaredCaseFixture?.fixtures ?? fallbackCaseFixtures ?? null;
  143. if (
  144. caseFixtures?.length === 0 &&
  145. !["none", "external-live"].includes(manifest.providerRequestPolicy)
  146. ) {
  147. reasons.push(
  148. diagnostic({
  149. code: "EMPTY_CASE_FIXTURE_WITHOUT_NONE",
  150. message:
  151. 'an empty case-local provider fixture requires providerRequestPolicy "none" or "external-live"',
  152. specPath,
  153. subjectPath: manifestSubjectPath,
  154. }),
  155. );
  156. }
  157. validateRequestLedger({
  158. caseFixtures: caseFixtures ?? [],
  159. manifestSubjectPath,
  160. providerFiles,
  161. reasons,
  162. requests,
  163. specPath,
  164. });
  165. }
  166. function validateCaseFixtureIdentity({
  167. absoluteSpecPath,
  168. caseFixture,
  169. caseFixturePath,
  170. caseName,
  171. reasons,
  172. root,
  173. specPath,
  174. }) {
  175. if (!isRecord(caseFixture)) {
  176. return;
  177. }
  178. const subjectPath = toRepoPath(root, caseFixturePath);
  179. if (caseFixture.caseName !== caseName) {
  180. reasons.push(
  181. diagnostic({
  182. code: "CASE_FIXTURE_CASE_NAME_MISMATCH",
  183. message: `case fixture caseName must be ${caseName}; received ${JSON.stringify(caseFixture.caseName)}`,
  184. specPath,
  185. subjectPath,
  186. }),
  187. );
  188. }
  189. if (
  190. typeof caseFixture.spec !== "string" ||
  191. resolve(root, "packages/desktop", caseFixture.spec) !== absoluteSpecPath
  192. ) {
  193. reasons.push(
  194. diagnostic({
  195. code: "CASE_FIXTURE_SPEC_PATH_MISMATCH",
  196. message: `case fixture spec must resolve to ${specPath}; received ${JSON.stringify(caseFixture.spec)}`,
  197. specPath,
  198. subjectPath,
  199. }),
  200. );
  201. }
  202. }
  203. function validateManifestIdentity({
  204. absoluteSpecPath,
  205. caseName,
  206. manifest,
  207. manifestSubjectPath,
  208. reasons,
  209. root,
  210. specPath,
  211. }) {
  212. if (manifest.version !== 1) {
  213. reasons.push(
  214. diagnostic({
  215. code: "INVALID_MANIFEST_VERSION",
  216. message: `case manifest version must be 1; received ${JSON.stringify(manifest.version)}`,
  217. specPath,
  218. subjectPath: manifestSubjectPath,
  219. }),
  220. );
  221. }
  222. if (manifest.caseName !== caseName) {
  223. reasons.push(
  224. diagnostic({
  225. code: "MANIFEST_CASE_NAME_MISMATCH",
  226. message: `case manifest caseName must be ${caseName}; received ${JSON.stringify(manifest.caseName)}`,
  227. specPath,
  228. subjectPath: manifestSubjectPath,
  229. }),
  230. );
  231. }
  232. const desktopRoot = resolve(root, "packages/desktop");
  233. if (
  234. typeof manifest.spec !== "string" ||
  235. resolve(desktopRoot, manifest.spec) !== absoluteSpecPath
  236. ) {
  237. reasons.push(
  238. diagnostic({
  239. code: "MANIFEST_SPEC_PATH_MISMATCH",
  240. message: `case manifest spec must resolve to ${specPath}; received ${JSON.stringify(manifest.spec)}`,
  241. specPath,
  242. subjectPath: manifestSubjectPath,
  243. }),
  244. );
  245. }
  246. }
  247. async function readProviderFiles({
  248. canonicalCommonPath,
  249. reasons,
  250. resolvedProviderFixtures,
  251. root,
  252. specPath,
  253. }) {
  254. const providerFiles = [];
  255. for (const fixture of resolvedProviderFixtures) {
  256. const result = await readJson(fixture.absolutePath);
  257. const subjectPath = toRepoPath(root, fixture.absolutePath);
  258. if (result.missing) {
  259. reasons.push(
  260. diagnostic({
  261. code: "MISSING_PROVIDER_FIXTURE",
  262. message: `declared provider fixture is missing: ${fixture.manifestPath}`,
  263. specPath,
  264. subjectPath,
  265. }),
  266. );
  267. continue;
  268. }
  269. if (result.error) {
  270. reasons.push(
  271. diagnostic({
  272. code: "INVALID_PROVIDER_FIXTURE_JSON",
  273. message: `declared provider fixture is not valid JSON: ${result.error.message}`,
  274. specPath,
  275. subjectPath,
  276. }),
  277. );
  278. continue;
  279. }
  280. const fixtures = validateProviderFixture(result.value, subjectPath, specPath, reasons);
  281. if (fixtures) {
  282. providerFiles.push({
  283. absolutePath: fixture.absolutePath,
  284. fixtures,
  285. isCanonicalCommon: fixture.absolutePath === canonicalCommonPath,
  286. subjectPath,
  287. });
  288. }
  289. }
  290. return providerFiles;
  291. }
  292. function validateProviderPolicy({ manifest, manifestSubjectPath, reasons, requests, specPath }) {
  293. if (
  294. manifest.providerRequestPolicy !== undefined &&
  295. !["none", "external-live"].includes(manifest.providerRequestPolicy)
  296. ) {
  297. reasons.push(
  298. diagnostic({
  299. code: "INVALID_PROVIDER_REQUEST_POLICY",
  300. message: 'providerRequestPolicy must be "none" or "external-live" when present',
  301. specPath,
  302. subjectPath: manifestSubjectPath,
  303. }),
  304. );
  305. }
  306. // 真实账号专项绕过回放服务器;不能为了空 fixture 把真实模型请求声明为不存在。
  307. if (
  308. manifest.providerRequestPolicy === "external-live" &&
  309. (typeof manifest.externalProviderReason !== "string" ||
  310. !manifest.externalProviderReason.trim() ||
  311. manifest.noProviderRequests === true ||
  312. requests.length > 0)
  313. ) {
  314. reasons.push(
  315. diagnostic({
  316. code: "INVALID_EXTERNAL_LIVE_CONTRACT",
  317. message:
  318. "external-live requires a reason, an empty replay ledger and no noProviderRequests claim",
  319. specPath,
  320. subjectPath: manifestSubjectPath,
  321. }),
  322. );
  323. }
  324. if (manifest.providerRequestPolicy === "none" && requests.length > 0) {
  325. reasons.push(
  326. diagnostic({
  327. code: "PROVIDER_REQUEST_POLICY_NONE_WITH_REQUESTS",
  328. message: 'providerRequestPolicy "none" requires an empty requests ledger',
  329. specPath,
  330. subjectPath: manifestSubjectPath,
  331. }),
  332. );
  333. }
  334. }
  335. function validateStringArray(value, field, subjectPath, specPath, reasons) {
  336. if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
  337. reasons.push(
  338. diagnostic({
  339. code: "INVALID_MANIFEST_SHAPE",
  340. message: `case manifest ${field} must be an array of strings`,
  341. specPath,
  342. subjectPath,
  343. }),
  344. );
  345. return null;
  346. }
  347. return value;
  348. }