legacy-capability-preload.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 仅供隔离CLI进程加载:隐藏新能力,严格核验三入口出站字段;不执行合成探针命令。
  2. import { appendFile } from "node:fs/promises";
  3. import { StringDecoder } from "node:string_decoder";
  4. const config = new URL(import.meta.url).searchParams;
  5. const mode = config.get("mode");
  6. const evidence = config.get("evidence");
  7. if (!evidence || !["legacy", "current"].includes(mode)) throw new Error("Invalid owner fixture");
  8. const capabilities = new Set();
  9. const write = process.stdout.write.bind(process.stdout);
  10. process.stdout.write = function (chunk, encoding, callback) {
  11. let output = chunk;
  12. try {
  13. const frame = JSON.parse(String(chunk));
  14. if (capabilities.has(frame.id) && frame.result) {
  15. capabilities.delete(frame.id);
  16. if (mode === "legacy") delete frame.result.highspeedCardOwner;
  17. output = `${JSON.stringify(frame)}\n`;
  18. }
  19. } catch {
  20. /* 非完整协议帧原样转发,不收集内容。 */
  21. }
  22. return write(output, encoding, callback);
  23. };
  24. const emit = process.stdin.emit;
  25. const decoder = new StringDecoder("utf8");
  26. let pending = "";
  27. process.stdin.emit = function (event, ...args) {
  28. if (event !== "data") return emit.call(this, event, ...args);
  29. pending += decoder.write(Buffer.isBuffer(args[0]) ? args[0] : Buffer.from(args[0]));
  30. let newline;
  31. while ((newline = pending.indexOf("\n")) !== -1) {
  32. const line = pending.slice(0, newline);
  33. pending = pending.slice(newline + 1);
  34. let frame;
  35. try {
  36. frame = JSON.parse(line);
  37. } catch {
  38. emit.call(this, event, Buffer.from(`${line}\n`));
  39. continue;
  40. }
  41. if (frame.method === "runtime/capabilities") capabilities.add(frame.id);
  42. if (
  43. frame.method === "v4/command" &&
  44. frame.params?.commandId?.startsWith("owner-compat-probe-")
  45. ) {
  46. const command = frame.params;
  47. const meta = command.payload?.highspeedMeta;
  48. const ownerPresent = typeof meta?.ownerKey === "string";
  49. const allowed = new Set([
  50. "schemaVersion",
  51. "cardId",
  52. "taskId",
  53. "provider",
  54. "model",
  55. "issuedAt",
  56. "expiresAt",
  57. "regularTps",
  58. ...(mode === "current" ? ["ownerKey"] : []),
  59. ]);
  60. const strictMetadata = !!meta && Object.keys(meta).every((key) => allowed.has(key));
  61. const metadataPreserved =
  62. meta?.schemaVersion === 1 &&
  63. meta?.cardId === "owner-probe-card" &&
  64. meta?.taskId === command.sessionId &&
  65. meta?.provider === "zai" &&
  66. meta?.model === "GLM-5.3-Flash" &&
  67. meta?.issuedAt === 1 &&
  68. meta?.expiresAt === 10000 &&
  69. meta?.regularTps === 50;
  70. const selection = command.payload?.modelSelection;
  71. const payloadPreserved =
  72. selection?.providerId === "account:bigmodel-individual-coding-plan" &&
  73. selection?.modelId === "GLM-5.3-Flash" &&
  74. selection?.options?.reasoningLevel === "high" &&
  75. Object.keys(selection.options).length === 1 &&
  76. command.payload?.modelExecution?.selectionScope === "execution" &&
  77. command.payload?.modelExecution?.requestAuth?.headers?.["X-Highspeed-Card-ID"] ===
  78. "owner-probe-card";
  79. const accepted =
  80. strictMetadata &&
  81. ownerPresent === (mode === "current") &&
  82. (mode !== "current" || meta.ownerKey === `v1:${"a".repeat(64)}`) &&
  83. metadataPreserved &&
  84. payloadPreserved;
  85. // 仅记录布尔证据;命令正文、headers和token都不得写入记录。
  86. void appendFile(
  87. evidence,
  88. `${JSON.stringify({ mode, type: command.type, strictMetadata, ownerPresent, metadataPreserved, payloadPreserved, accepted })}\n`,
  89. ).then(() =>
  90. write(
  91. `${JSON.stringify({ id: frame.id, result: { commandId: command.commandId, status: accepted ? "noop" : "rejected", reasonCode: accepted ? "owner_compat_validated" : "owner_metadata_rejected", revisionAtDecision: 0 } })}\n`,
  92. ),
  93. );
  94. continue;
  95. }
  96. emit.call(this, event, Buffer.from(`${line}\n`));
  97. }
  98. return true;
  99. };