sneakoscope 7.3.0 → 7.6.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 (283) hide show
  1. package/README.md +129 -10
  2. package/crates/sks-core/Cargo.lock +1 -1
  3. package/crates/sks-core/Cargo.toml +1 -1
  4. package/dist/cli/codex-app-command.js +1 -1
  5. package/dist/cli/command-manifest-lite.js +4 -1
  6. package/dist/cli/command-registry.js +2 -0
  7. package/dist/cli/install-helpers-codex-lb-chain.js +42 -0
  8. package/dist/cli/install-helpers-codex-lb-config.js +114 -9
  9. package/dist/cli/install-helpers-install-support.js +6 -16
  10. package/dist/cli/install-helpers.js +788 -600
  11. package/dist/cli/router.js +30 -2
  12. package/dist/commands/codex-app.js +37 -1
  13. package/dist/commands/codex-lb.js +491 -300
  14. package/dist/config/skills-manifest.json +57 -71
  15. package/dist/core/align/align-route.js +127 -0
  16. package/dist/core/codex-app/codex-app-fast-ui-repair.js +8 -4
  17. package/dist/core/codex-app/codex-app-restart.js +83 -0
  18. package/dist/core/codex-app/codex-app-ui-state-snapshot.js +8 -4
  19. package/dist/core/codex-app/desktop-routing-snapshot.js +1 -1
  20. package/dist/core/codex-app/menubar/constants.js +5 -0
  21. package/dist/core/codex-app/menubar/generation-transaction-journal.js +1 -3
  22. package/dist/core/codex-app/menubar/installer.js +18 -0
  23. package/dist/core/codex-app/multi-provider-router.js +22 -10
  24. package/dist/core/codex-app/openrouter-activate.js +26 -1
  25. package/dist/core/codex-app.js +118 -26
  26. package/dist/core/codex-control/codex-app-server-runtime-env.js +86 -0
  27. package/dist/core/codex-control/codex-app-server-v2-client.js +78 -8
  28. package/dist/core/codex-control/codex-task-runner.js +2 -2
  29. package/dist/core/codex-lb/capability-runner.js +204 -0
  30. package/dist/core/codex-lb/capability-types.js +1 -0
  31. package/dist/core/codex-lb/codex-lb-env.js +9 -13
  32. package/dist/core/codex-lb/codex-lb-setup.js +45 -8
  33. package/dist/core/codex-lb/codex-lb-tool-catalog.js +52 -74
  34. package/dist/core/codex-lb/codex-lb-tool-output-recovery.js +1 -1
  35. package/dist/core/codex-lb/desktop-auth-invariant.js +185 -0
  36. package/dist/core/codex-lb/desktop-bridge/client.js +34 -0
  37. package/dist/core/codex-lb/desktop-bridge/header-policy.js +106 -0
  38. package/dist/core/codex-lb/desktop-bridge/http-forward.js +101 -0
  39. package/dist/core/codex-lb/desktop-bridge/index.js +10 -0
  40. package/dist/core/codex-lb/desktop-bridge/launchd.js +81 -0
  41. package/dist/core/codex-lb/desktop-bridge/location-rewrite.js +27 -0
  42. package/dist/core/codex-lb/desktop-bridge/security.js +274 -0
  43. package/dist/core/codex-lb/desktop-bridge/server.js +179 -0
  44. package/dist/core/codex-lb/desktop-bridge/state.js +119 -0
  45. package/dist/core/codex-lb/desktop-bridge/types.js +16 -0
  46. package/dist/core/codex-lb/desktop-bridge/websocket-forward.js +151 -0
  47. package/dist/core/codex-lb/desktop-center-credentials.js +189 -0
  48. package/dist/core/codex-lb/desktop-controller.js +1410 -0
  49. package/dist/core/codex-lb/desktop-mode.js +32 -0
  50. package/dist/core/codex-lb/desktop-real-evidence.js +168 -0
  51. package/dist/core/codex-lb/desktop-service.js +613 -0
  52. package/dist/core/codex-lb/legacy-migration.js +575 -0
  53. package/dist/core/codex-lb/migration-receipt.js +246 -0
  54. package/dist/core/codex-lb/probes/auxiliary-surfaces-probe.js +60 -0
  55. package/dist/core/codex-lb/probes/bridge-probe.js +75 -0
  56. package/dist/core/codex-lb/probes/catalog-probe.js +128 -0
  57. package/dist/core/codex-lb/probes/computer-use-probe.js +50 -0
  58. package/dist/core/codex-lb/probes/image-generation-probe.js +47 -0
  59. package/dist/core/codex-lb/probes/probe-evidence.js +39 -0
  60. package/dist/core/codex-lb/probes/voice-realtime-probe.js +44 -0
  61. package/dist/core/codex-lb/trusted-deep-evidence.js +403 -0
  62. package/dist/core/codex-native/core-skill-manifest.js +22 -6
  63. package/dist/core/codex-runtime/codex-desktop-config-policy.js +2 -1
  64. package/dist/core/commands/align-command.js +220 -0
  65. package/dist/core/commands/image-ux-review-command.js +280 -68
  66. package/dist/core/commands/naruto-command.js +257 -8
  67. package/dist/core/commands/ppt-command.js +2 -2
  68. package/dist/core/commands/telegram-command.js +402 -34
  69. package/dist/core/commands/triwiki-command.js +11 -1
  70. package/dist/core/commands/triwiki-graph-command.js +160 -0
  71. package/dist/core/commands/wiki-command.js +100 -20
  72. package/dist/core/doctor/skill-legacy-surface.js +1 -3
  73. package/dist/core/errors/message.js +3 -0
  74. package/dist/core/evidence/evidence-freshness.js +14 -0
  75. package/dist/core/feature-fixtures.js +9 -1
  76. package/dist/core/feature-registry.js +5 -3
  77. package/dist/core/fsx.js +7 -3
  78. package/dist/core/hooks-runtime/codex-commit-hooks-selftest.js +82 -0
  79. package/dist/core/hooks-runtime/context-graph-freshness-preflight.js +113 -0
  80. package/dist/core/hooks-runtime/hook-context.js +15 -2
  81. package/dist/core/hooks-runtime/managed-skill-generation-heal.js +46 -0
  82. package/dist/core/hooks-runtime/naruto-terminal-finalization.js +1 -0
  83. package/dist/core/hooks-runtime/official-subagent-lifecycle.js +1 -1
  84. package/dist/core/hooks-runtime/tool-output-quarantine.js +1 -1
  85. package/dist/core/hooks-runtime.js +33 -89
  86. package/dist/core/image-ux-review/callout-extraction.js +6 -0
  87. package/dist/core/image-ux-review/imagegen-adapter.js +64 -23
  88. package/dist/core/image-ux-review/real-callout-extractor.js +1 -0
  89. package/dist/core/image-ux-review.js +230 -12
  90. package/dist/core/imagegen/codex-lb-imagegen-target.js +21 -7
  91. package/dist/core/imagegen/imagegen-capability.js +10 -4
  92. package/dist/core/imagegen/require-imagegen.js +29 -5
  93. package/dist/core/init/skills.js +25 -13
  94. package/dist/core/init.js +1 -1
  95. package/dist/core/install/installed-package-smoke.js +157 -4
  96. package/dist/core/managed-assets/managed-assets-manifest.js +2 -1
  97. package/dist/core/managed-path-safety.js +8 -10
  98. package/dist/core/mcp-config/backup.js +2 -4
  99. package/dist/core/mcp-config/config-reader.js +2 -4
  100. package/dist/core/mcp-config/scope.js +3 -5
  101. package/dist/core/naruto/context-graph-advisor-scope.js +242 -0
  102. package/dist/core/naruto/context-graph-advisor.js +0 -0
  103. package/dist/core/perf/release-latency-slo.js +12 -6
  104. package/dist/core/pipeline-internals/runtime-core.js +78 -0
  105. package/dist/core/pipeline-internals/runtime-gates.js +5 -2
  106. package/dist/core/policy/latest-version-guidance.js +113 -0
  107. package/dist/core/ppt.js +8 -8
  108. package/dist/core/preflight/parallel-preflight-engine.js +1 -1
  109. package/dist/core/proof/auto-finalize.js +98 -6
  110. package/dist/core/proof/route-proof-gate.js +12 -1
  111. package/dist/core/proof/route-proof-policy.js +8 -8
  112. package/dist/core/release/gate-manifest.js +32 -1
  113. package/dist/core/release/npm-stage-contract.js +21 -0
  114. package/dist/core/release/package-size-budget.js +2 -2
  115. package/dist/core/release/release-gate-contract.js +5 -0
  116. package/dist/core/release/release-proof-io.js +1 -3
  117. package/dist/core/release/release-real-contract.js +5 -4
  118. package/dist/core/release/release-stamp-proof.js +1 -3
  119. package/dist/core/release/stage-publish.js +27 -0
  120. package/dist/core/remote/audit-idempotency.js +50 -18
  121. package/dist/core/remote/codex-public-activity.js +283 -0
  122. package/dist/core/remote/index.js +1 -0
  123. package/dist/core/remote/ssh-worker-client.js +4 -1
  124. package/dist/core/remote/worker.js +206 -27
  125. package/dist/core/research/research-adversarial-review.js +1 -3
  126. package/dist/core/research/research-claim-synthesizer.js +1 -3
  127. package/dist/core/research/research-falsification-runner.js +1 -3
  128. package/dist/core/research/research-source-evidence.js +1 -3
  129. package/dist/core/responses-stream.js +65 -19
  130. package/dist/core/routes/constants.js +1 -1
  131. package/dist/core/routes/design-policy.js +7 -2
  132. package/dist/core/routes/dollar-manifest-lite.js +2 -4
  133. package/dist/core/routes/evidence.js +1 -1
  134. package/dist/core/routes.js +30 -10
  135. package/dist/core/safety/command-contract/contracts.js +2 -0
  136. package/dist/core/safety/command-contract/types.js +1 -1
  137. package/dist/core/search/context-graph-seeds.js +205 -0
  138. package/dist/core/search/context.js +0 -0
  139. package/dist/core/search/provider.js +2 -2
  140. package/dist/core/secret-redaction.js +6 -0
  141. package/dist/core/subagents/naruto-help-contract.js +2 -1
  142. package/dist/core/subagents/naruto-host-credentials.js +161 -0
  143. package/dist/core/subagents/official-subagent-preparation.js +4 -1
  144. package/dist/core/subagents/official-subagent-prompt.js +44 -2
  145. package/dist/core/subagents/official-subagent-runner.js +26 -8
  146. package/dist/core/subagents/triwiki-attention.js +135 -123
  147. package/dist/core/telegram/bot-api.js +85 -10
  148. package/dist/core/telegram/center-snapshot.js +718 -0
  149. package/dist/core/telegram/config.js +158 -31
  150. package/dist/core/telegram/hub.js +288 -33
  151. package/dist/core/telegram/index.js +2 -0
  152. package/dist/core/telegram/ingress-spool.js +419 -0
  153. package/dist/core/telegram/ledgers.js +188 -29
  154. package/dist/core/telegram/messages.js +26 -6
  155. package/dist/core/telegram/owner-lock.js +10 -7
  156. package/dist/core/telegram/runtime-projection.js +38 -3
  157. package/dist/core/telegram/runtime.js +298 -63
  158. package/dist/core/telegram/service.js +137 -8
  159. package/dist/core/telegram/setup.js +291 -73
  160. package/dist/core/text/strings.js +3 -0
  161. package/dist/core/triwiki/code-pack.js +12 -120
  162. package/dist/core/triwiki/context-graph/compiler/cache-key.js +277 -0
  163. package/dist/core/triwiki/context-graph/compiler/extract.js +104 -0
  164. package/dist/core/triwiki/context-graph/compiler/freshness.js +108 -0
  165. package/dist/core/triwiki/context-graph/compiler/index.js +237 -0
  166. package/dist/core/triwiki/context-graph/compiler/merge.js +0 -0
  167. package/dist/core/triwiki/context-graph/compiler/serialize.js +115 -0
  168. package/dist/core/triwiki/context-graph/contracts.js +236 -0
  169. package/dist/core/triwiki/context-graph/extractors/code/builders.js +150 -0
  170. package/dist/core/triwiki/context-graph/extractors/code/declarations.js +161 -0
  171. package/dist/core/triwiki/context-graph/extractors/code/index.js +265 -0
  172. package/dist/core/triwiki/context-graph/extractors/code/inventory.js +237 -0
  173. package/dist/core/triwiki/context-graph/extractors/code/module-graph.js +252 -0
  174. package/dist/core/triwiki/context-graph/extractors/code/modules.js +79 -0
  175. package/dist/core/triwiki/context-graph/extractors/code/references.js +184 -0
  176. package/dist/core/triwiki/context-graph/extractors/code/selection.js +100 -0
  177. package/dist/core/triwiki/context-graph/extractors/code/ts-config.js +161 -0
  178. package/dist/core/triwiki/context-graph/extractors/code/types.js +18 -0
  179. package/dist/core/triwiki/context-graph/extractors/evidence/claims.js +289 -0
  180. package/dist/core/triwiki/context-graph/extractors/evidence/index.js +54 -0
  181. package/dist/core/triwiki/context-graph/extractors/evidence/proof-index.js +213 -0
  182. package/dist/core/triwiki/context-graph/extractors/evidence/proofs.js +266 -0
  183. package/dist/core/triwiki/context-graph/extractors/evidence/redaction.js +177 -0
  184. package/dist/core/triwiki/context-graph/extractors/evidence/shared.js +0 -0
  185. package/dist/core/triwiki/context-graph/extractors/evidence/sources.js +154 -0
  186. package/dist/core/triwiki/context-graph/extractors/index.js +7 -0
  187. package/dist/core/triwiki/context-graph/extractors/topology/commands.js +207 -0
  188. package/dist/core/triwiki/context-graph/extractors/topology/gate-edges.js +201 -0
  189. package/dist/core/triwiki/context-graph/extractors/topology/gates.js +182 -0
  190. package/dist/core/triwiki/context-graph/extractors/topology/globs.js +124 -0
  191. package/dist/core/triwiki/context-graph/extractors/topology/index.js +92 -0
  192. package/dist/core/triwiki/context-graph/extractors/topology/routes.js +135 -0
  193. package/dist/core/triwiki/context-graph/extractors/topology/shared.js +215 -0
  194. package/dist/core/triwiki/context-graph/graph-index.js +143 -0
  195. package/dist/core/triwiki/context-graph/ids.js +0 -0
  196. package/dist/core/triwiki/context-graph/lint/index.js +0 -0
  197. package/dist/core/triwiki/context-graph/lint/rules.js +232 -0
  198. package/dist/core/triwiki/context-graph/lint/warnings.js +93 -0
  199. package/dist/core/triwiki/context-graph/paths.js +116 -0
  200. package/dist/core/triwiki/context-graph/profiles.js +78 -0
  201. package/dist/core/triwiki/context-graph/projections/anchors.js +91 -0
  202. package/dist/core/triwiki/context-graph/projections/attention.js +58 -0
  203. package/dist/core/triwiki/context-graph/projections/code-pack-workspace.js +62 -0
  204. package/dist/core/triwiki/context-graph/projections/code-pack.js +144 -0
  205. package/dist/core/triwiki/context-graph/projections/index.js +7 -0
  206. package/dist/core/triwiki/context-graph/projections/module-view.js +140 -0
  207. package/dist/core/triwiki/context-graph/projections/node-summary.js +118 -0
  208. package/dist/core/triwiki/context-graph/projections/pack-contract.js +6 -0
  209. package/dist/core/triwiki/context-graph/query/explain.js +0 -0
  210. package/dist/core/triwiki/context-graph/query/index.js +178 -0
  211. package/dist/core/triwiki/context-graph/query/load.js +116 -0
  212. package/dist/core/triwiki/context-graph/query/pack.js +117 -0
  213. package/dist/core/triwiki/context-graph/query/rank.js +142 -0
  214. package/dist/core/triwiki/context-graph/query/ranking-config.js +64 -0
  215. package/dist/core/triwiki/context-graph/query/seeds.js +241 -0
  216. package/dist/core/triwiki/context-graph/query/snapshot-cache.js +72 -0
  217. package/dist/core/triwiki/context-graph/query/traverse.js +154 -0
  218. package/dist/core/triwiki/context-graph/query-types.js +29 -0
  219. package/dist/core/triwiki/context-graph/store/compile-lock.js +21 -0
  220. package/dist/core/triwiki/context-graph/store/event-log.js +48 -0
  221. package/dist/core/triwiki/context-graph/store/fragment-cache.js +85 -0
  222. package/dist/core/triwiki/context-graph/store/graph-status.js +85 -0
  223. package/dist/core/triwiki/context-graph/store/snapshot-store.js +155 -0
  224. package/dist/core/triwiki/triwiki-proof-bank-index-repair.js +115 -0
  225. package/dist/core/triwiki/triwiki-proof-bank-index-store.js +290 -0
  226. package/dist/core/triwiki/triwiki-proof-bank-index.js +126 -0
  227. package/dist/core/triwiki/triwiki-proof-bank.js +7 -1
  228. package/dist/core/verification/context-graph-affected.js +271 -0
  229. package/dist/core/version.js +1 -1
  230. package/dist/native/sks-menubar/Sources/AlertFactory.swift +11 -3
  231. package/dist/native/sks-menubar/Sources/AppDelegate.swift +9 -0
  232. package/dist/native/sks-menubar/Sources/ControlCenterWindowController.swift +25 -3
  233. package/dist/native/sks-menubar/Sources/ControlKit.swift +19 -8
  234. package/dist/native/sks-menubar/Sources/DiagnosticsViewController.swift +1 -2
  235. package/dist/native/sks-menubar/Sources/MCPServersViewController.swift +45 -17
  236. package/dist/native/sks-menubar/Sources/OverviewSummary.swift +162 -0
  237. package/dist/native/sks-menubar/Sources/OverviewViewController.swift +136 -190
  238. package/dist/native/sks-menubar/Sources/ProcessClient.swift +107 -11
  239. package/dist/native/sks-menubar/Sources/ProvidersMultiProvider.swift +5 -1
  240. package/dist/native/sks-menubar/Sources/ProvidersOpenRouter.swift +16 -4
  241. package/dist/native/sks-menubar/Sources/ProvidersRoleModels.swift +4 -1
  242. package/dist/native/sks-menubar/Sources/ProvidersRoutingTruth.swift +131 -0
  243. package/dist/native/sks-menubar/Sources/ProvidersViewController.swift +288 -341
  244. package/dist/native/sks-menubar/Sources/RemoteTelegramCenterState.swift +447 -0
  245. package/dist/native/sks-menubar/Sources/RemoteTelegramRendering.swift +484 -0
  246. package/dist/native/sks-menubar/Sources/RemoteTelegramViewController.swift +364 -115
  247. package/dist/native/sks-menubar/Sources/SettingsViewController.swift +73 -12
  248. package/dist/native/sks-menubar/Sources/SidebarItem.swift +3 -3
  249. package/dist/native/sks-menubar/Sources/StatusItemController.swift +13 -13
  250. package/dist/native/sks-menubar/Sources/TelegramHubSupervisor.swift +469 -0
  251. package/dist/native/sks-menubar/Sources/UpdatesViewController.swift +1 -6
  252. package/dist/native/sks-menubar/Tests/AppRuntimeTestSupport.swift +12 -0
  253. package/dist/native/sks-menubar/Tests/NativeTelegramRegressionRunner.swift +55 -0
  254. package/dist/native/sks-menubar/Tests/ProcessClientTests.swift +33 -0
  255. package/dist/native/sks-menubar/Tests/ProvidersViewControllerTests.swift +142 -0
  256. package/dist/native/sks-menubar/Tests/RemoteTelegramCenterStateTests.swift +56 -0
  257. package/dist/native/sks-menubar/Tests/TelegramHubSupervisorTests.swift +55 -0
  258. package/dist/scripts/codex-lb-catalog-passthrough-check.js +120 -0
  259. package/dist/scripts/codex-lb-desktop-auth-invariant-check.js +169 -0
  260. package/dist/scripts/codex-lb-desktop-bridge-check.js +394 -0
  261. package/dist/scripts/codex-lb-desktop-bridge-latency-check.js +196 -0
  262. package/dist/scripts/codex-lb-desktop-capabilities-check.js +287 -0
  263. package/dist/scripts/codex-lb-desktop-real-evidence-check.js +123 -0
  264. package/dist/scripts/codex-lb-fast-mode-truth-check.js +10 -3
  265. package/dist/scripts/codex-lb-fast-ui-preservation-check.js +81 -40
  266. package/dist/scripts/codex-lb-gpt56-fast-profile-check.js +99 -37
  267. package/dist/scripts/context-graph-check.js +191 -0
  268. package/dist/scripts/doctor-fixes-codex-app-fast-ui-check.js +1 -1
  269. package/dist/scripts/gate-policy-audit-check.js +1 -1
  270. package/dist/scripts/helper-dedup-check.js +0 -2
  271. package/dist/scripts/installed-package-smoke-check.js +4 -1
  272. package/dist/scripts/latest-version-guidance-check.js +74 -0
  273. package/dist/scripts/release-metadata-check.js +21 -4
  274. package/dist/scripts/release-real-check.js +7 -0
  275. package/dist/scripts/ux-review-imagegen-blackbox-check.js +25 -1
  276. package/dist/scripts/ux-review-run-wires-imagegen-check.js +9 -1
  277. package/package.json +8 -3
  278. package/release-gates.v2.json +176 -1
  279. package/runtime-required-scripts.json +30 -0
  280. package/schemas/triwiki/context-graph.schema.json +119 -0
  281. package/dist/cli/install-helpers-codex-lb-selftest-chain.js +0 -112
  282. package/dist/cli/install-helpers-codex-lb-selftest.js +0 -481
  283. package/dist/core/triwiki/code-index-scanner.js +0 -309
@@ -0,0 +1,16 @@
1
+ export const DESKTOP_BRIDGE_STATE_SCHEMA = 'sks.codex-lb-desktop-bridge.v1';
2
+ export const DESKTOP_BRIDGE_ALLOWED_PATH_PREFIXES = [
3
+ '/backend-api/codex/',
4
+ '/backend-api/files',
5
+ '/backend-api/transcribe',
6
+ '/backend-api/wham/',
7
+ '/v1/',
8
+ ];
9
+ export class DesktopBridgeError extends Error {
10
+ code;
11
+ constructor(code, options) {
12
+ super(code, options);
13
+ this.name = 'DesktopBridgeError';
14
+ this.code = code;
15
+ }
16
+ }
@@ -0,0 +1,151 @@
1
+ import net from 'node:net';
2
+ import tls from 'node:tls';
3
+ import { buildWebSocketHeaders } from './header-policy.js';
4
+ import { rewriteLocationHeader } from './location-rewrite.js';
5
+ import { resolveDesktopBridgeTarget } from './security.js';
6
+ import { desktopBridgeListenOrigin } from './state.js';
7
+ import { DesktopBridgeError } from './types.js';
8
+ const MAX_UPGRADE_RESPONSE_HEAD_BYTES = 64 * 1024;
9
+ const REDACTED_UPGRADE_RESPONSE_HEADERS = new Set([
10
+ 'authorization',
11
+ 'cookie',
12
+ 'proxy-authenticate',
13
+ 'proxy-authorization',
14
+ 'set-cookie',
15
+ 'x-codex-lb-api-key',
16
+ ]);
17
+ function serializeHeaders(headers) {
18
+ return Object.entries(headers).flatMap(([name, value]) => {
19
+ if (value === undefined)
20
+ return [];
21
+ if (Array.isArray(value))
22
+ return value.map((entry) => `${name}: ${String(entry)}`);
23
+ return [`${name}: ${String(value)}`];
24
+ });
25
+ }
26
+ function rewriteUpgradeResponseHead(head, config) {
27
+ const text = head.toString('latin1');
28
+ const lines = text.split('\r\n');
29
+ const statusLine = lines.shift();
30
+ if (!statusLine || !/^HTTP\/1\.[01] \d{3}(?: |$)/.test(statusLine)) {
31
+ throw new DesktopBridgeError('bridge_websocket_response_invalid');
32
+ }
33
+ const output = [statusLine];
34
+ for (const line of lines) {
35
+ if (!line)
36
+ continue;
37
+ if (/^[ \t]/.test(line))
38
+ throw new DesktopBridgeError('bridge_websocket_response_invalid');
39
+ const colon = line.indexOf(':');
40
+ if (colon <= 0)
41
+ throw new DesktopBridgeError('bridge_websocket_response_invalid');
42
+ const rawName = line.slice(0, colon);
43
+ const name = rawName.toLowerCase();
44
+ const value = line.slice(colon + 1).trim();
45
+ if (REDACTED_UPGRADE_RESPONSE_HEADERS.has(name) || name.startsWith('access-control-'))
46
+ continue;
47
+ if (name === 'location') {
48
+ output.push(`${rawName}: ${rewriteLocationHeader(value, config.remoteBaseUrl, desktopBridgeListenOrigin(config))}`);
49
+ continue;
50
+ }
51
+ output.push(line);
52
+ }
53
+ return Buffer.from(`${output.join('\r\n')}\r\n\r\n`, 'latin1');
54
+ }
55
+ function writeUpgradeFailure(clientSocket) {
56
+ if (clientSocket.destroyed)
57
+ return;
58
+ clientSocket.end('HTTP/1.1 502 Bad Gateway\r\n'
59
+ + 'Content-Type: application/json\r\n'
60
+ + 'Cache-Control: no-store\r\n'
61
+ + 'Connection: close\r\n'
62
+ + '\r\n'
63
+ + '{"error":{"type":"sks_bridge_error","code":"bridge_websocket_upstream_unavailable"}}');
64
+ }
65
+ export function forwardWebSocket(req, clientSocket, head, config) {
66
+ const target = resolveDesktopBridgeTarget(req.url, config.remote);
67
+ const upstream = config.remote.secure
68
+ ? tls.connect({
69
+ host: config.remote.address,
70
+ port: config.remote.port,
71
+ ...(config.remote.tlsServername ? { servername: config.remote.tlsServername } : {}),
72
+ })
73
+ : net.connect({
74
+ host: config.remote.address,
75
+ port: config.remote.port,
76
+ family: config.remote.family,
77
+ });
78
+ let connected = false;
79
+ let responseHead = Buffer.alloc(0);
80
+ const connectTimer = setTimeout(() => {
81
+ upstream.destroy(new DesktopBridgeError('bridge_upstream_connect_timeout'));
82
+ }, config.connectTimeoutMs);
83
+ connectTimer.unref();
84
+ const fail = () => {
85
+ clearTimeout(connectTimer);
86
+ if (!connected)
87
+ writeUpgradeFailure(clientSocket);
88
+ else
89
+ clientSocket.destroy();
90
+ };
91
+ const onConnected = () => {
92
+ connected = true;
93
+ clearTimeout(connectTimer);
94
+ upstream.setNoDelay(true);
95
+ upstream.setTimeout(config.idleTimeoutMs, () => {
96
+ upstream.destroy(new DesktopBridgeError('bridge_upstream_idle_timeout'));
97
+ });
98
+ if (clientSocket instanceof net.Socket) {
99
+ clientSocket.setNoDelay(true);
100
+ clientSocket.setTimeout(config.idleTimeoutMs, () => clientSocket.destroy());
101
+ }
102
+ const headers = buildWebSocketHeaders(req.headers, config, target.host);
103
+ const requestHead = [
104
+ `${req.method || 'GET'} ${target.pathname}${target.search} HTTP/1.1`,
105
+ ...serializeHeaders(headers),
106
+ '',
107
+ '',
108
+ ].join('\r\n');
109
+ upstream.write(requestHead);
110
+ if (head.length)
111
+ upstream.write(head);
112
+ clientSocket.pipe(upstream);
113
+ const onHandshakeData = (chunk) => {
114
+ responseHead = Buffer.concat([responseHead, chunk]);
115
+ if (responseHead.length > MAX_UPGRADE_RESPONSE_HEAD_BYTES) {
116
+ upstream.destroy(new DesktopBridgeError('bridge_websocket_response_headers_too_large'));
117
+ return;
118
+ }
119
+ const boundary = responseHead.indexOf('\r\n\r\n');
120
+ if (boundary < 0)
121
+ return;
122
+ upstream.off('data', onHandshakeData);
123
+ const rawHead = responseHead.subarray(0, boundary);
124
+ const remaining = responseHead.subarray(boundary + 4);
125
+ try {
126
+ clientSocket.write(rewriteUpgradeResponseHead(rawHead, config));
127
+ if (remaining.length)
128
+ clientSocket.write(remaining);
129
+ }
130
+ catch (error) {
131
+ upstream.destroy(error instanceof Error ? error : undefined);
132
+ return;
133
+ }
134
+ upstream.pipe(clientSocket);
135
+ responseHead = Buffer.alloc(0);
136
+ };
137
+ upstream.on('data', onHandshakeData);
138
+ };
139
+ if (config.remote.secure)
140
+ upstream.once('secureConnect', onConnected);
141
+ else
142
+ upstream.once('connect', onConnected);
143
+ upstream.once('error', fail);
144
+ upstream.once('close', () => {
145
+ clearTimeout(connectTimer);
146
+ if (!clientSocket.destroyed)
147
+ clientSocket.end();
148
+ });
149
+ clientSocket.once('error', () => upstream.destroy());
150
+ clientSocket.once('close', () => upstream.destroy());
151
+ }
@@ -0,0 +1,189 @@
1
+ import fsp from 'node:fs/promises';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { exists, runProcess, which } from '../fsx.js';
5
+ import { codexLbEnvPath, loadCodexLbEnv } from './codex-lb-env.js';
6
+ export const CODEX_LB_STALE_ENV_TWINS = ['codex-lb.env', 'sks.env'];
7
+ export const CODEX_LB_STALE_KEYCHAIN_SERVICES = ['CODEX_LB_API_KEY'];
8
+ export const CODEX_LB_OFFICIAL_KEYCHAIN_SERVICE = 'sks-codex-lb';
9
+ export async function loadOfficialCodexLbCredentials(opts = {}) {
10
+ const home = opts.home || process.env.HOME || os.homedir();
11
+ const load = opts.loadCodexLbEnvImpl || loadCodexLbEnv;
12
+ return load({
13
+ home,
14
+ processEnv: {},
15
+ envPath: opts.envPath || codexLbEnvPath(home),
16
+ ...(opts.metadataPath ? { metadataPath: opts.metadataPath } : {})
17
+ });
18
+ }
19
+ export async function purgeStaleCodexLbCredentialTwins(opts = {}) {
20
+ const home = opts.home || process.env.HOME || os.homedir();
21
+ const codexHome = path.join(home, '.codex');
22
+ const removed = [];
23
+ const blockers = [];
24
+ for (const name of CODEX_LB_STALE_ENV_TWINS) {
25
+ const target = path.join(codexHome, name);
26
+ try {
27
+ await fsp.lstat(target);
28
+ await fsp.rm(target, { force: true });
29
+ removed.push(target);
30
+ }
31
+ catch (error) {
32
+ if (error?.code !== 'ENOENT') {
33
+ blockers.push(`stale_twin_remove_failed:${name}`);
34
+ }
35
+ }
36
+ }
37
+ const keychainCleared = [];
38
+ if (process.platform === 'darwin') {
39
+ const security = opts.securityBin
40
+ || await which('security').catch(() => null)
41
+ || (await exists('/usr/bin/security') ? '/usr/bin/security' : null);
42
+ const account = opts.account || process.env.USER || 'sks';
43
+ const run = opts.runProcessImpl || runProcess;
44
+ if (security) {
45
+ for (const service of CODEX_LB_STALE_KEYCHAIN_SERVICES) {
46
+ const result = await run(security, [
47
+ 'delete-generic-password',
48
+ '-a',
49
+ account,
50
+ '-s',
51
+ service
52
+ ], { timeoutMs: 5000, maxOutputBytes: 8192 }).catch(() => ({ code: 1, stdout: '', stderr: '' }));
53
+ if (result.code === 0 || /could not be found|errSecItemNotFound|-25300/i.test(String(result.stderr || result.stdout || ''))) {
54
+ if (result.code === 0)
55
+ keychainCleared.push(service);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ return { removed, keychain_cleared: keychainCleared, blockers };
61
+ }
62
+ export async function syncDesktopCenterLaunchCredentials(opts = { mode: 'disabled' }) {
63
+ const home = opts.home || process.env.HOME || os.homedir();
64
+ const mode = String(opts.mode || 'disabled');
65
+ const platform = opts.platform || process.platform;
66
+ const purge = opts.skipPurge
67
+ ? { removed: [], keychain_cleared: [], blockers: [] }
68
+ : await purgeStaleCodexLbCredentialTwins({
69
+ home,
70
+ ...(opts.runProcessImpl ? { runProcessImpl: opts.runProcessImpl } : {})
71
+ });
72
+ const loaded = opts.loadedEnv || await loadOfficialCodexLbCredentials({
73
+ home,
74
+ ...(opts.loadCodexLbEnvImpl ? { loadCodexLbEnvImpl: opts.loadCodexLbEnvImpl } : {})
75
+ });
76
+ const fingerprint = loaded.api_key.fingerprint;
77
+ const basePresent = Boolean(loaded.base_url);
78
+ if (platform !== 'darwin' && !opts.force) {
79
+ return {
80
+ schema: 'sks.codex-lb-desktop-center-credentials.v1',
81
+ ok: true,
82
+ status: 'not_macos',
83
+ mode,
84
+ api_key_fingerprint: fingerprint,
85
+ base_url_present: basePresent,
86
+ launch_env: { api_key: 'skipped', base_url: 'skipped' },
87
+ stale_twins_removed: purge.removed,
88
+ stale_keychain_cleared: purge.keychain_cleared,
89
+ blockers: purge.blockers
90
+ };
91
+ }
92
+ const launchctl = opts.launchctlBin
93
+ || await which('launchctl').catch(() => null)
94
+ || (await exists('/bin/launchctl') ? '/bin/launchctl' : null);
95
+ if (!launchctl) {
96
+ return {
97
+ schema: 'sks.codex-lb-desktop-center-credentials.v1',
98
+ ok: false,
99
+ status: 'launchctl_missing',
100
+ mode,
101
+ api_key_fingerprint: fingerprint,
102
+ base_url_present: basePresent,
103
+ launch_env: { api_key: 'failed', base_url: 'failed' },
104
+ stale_twins_removed: purge.removed,
105
+ stale_keychain_cleared: purge.keychain_cleared,
106
+ blockers: ['launchctl_missing', ...purge.blockers]
107
+ };
108
+ }
109
+ const run = opts.runProcessImpl || runProcess;
110
+ const setEnv = async (key, value) => {
111
+ const result = await run(launchctl, ['setenv', key, value], { timeoutMs: 5000, maxOutputBytes: 8192 });
112
+ return result.code === 0;
113
+ };
114
+ const unsetEnv = async (key) => {
115
+ const result = await run(launchctl, ['unsetenv', key], { timeoutMs: 5000, maxOutputBytes: 8192 });
116
+ return result.code === 0;
117
+ };
118
+ if (mode === 'desktop-dual-auth-compat') {
119
+ if (!loaded.secret_api_key || !loaded.base_url) {
120
+ return {
121
+ schema: 'sks.codex-lb-desktop-center-credentials.v1',
122
+ ok: false,
123
+ status: 'center_credentials_unavailable',
124
+ mode,
125
+ api_key_fingerprint: fingerprint,
126
+ base_url_present: basePresent,
127
+ launch_env: { api_key: 'failed', base_url: 'failed' },
128
+ stale_twins_removed: purge.removed,
129
+ stale_keychain_cleared: purge.keychain_cleared,
130
+ blockers: [
131
+ ...(loaded.missing.length ? loaded.missing.map((item) => `codex_lb_missing:${item}`) : ['codex_lb_not_configured']),
132
+ ...purge.blockers
133
+ ]
134
+ };
135
+ }
136
+ const keyOk = await setEnv('CODEX_LB_API_KEY', loaded.secret_api_key);
137
+ const baseOk = await setEnv('CODEX_LB_BASE_URL', loaded.base_url);
138
+ await unsetEnv('OPENROUTER_API_KEY');
139
+ return {
140
+ schema: 'sks.codex-lb-desktop-center-credentials.v1',
141
+ ok: keyOk && baseOk,
142
+ status: keyOk && baseOk ? 'desktop_compat_launch_env_synced' : 'desktop_compat_launch_env_failed',
143
+ mode,
144
+ api_key_fingerprint: fingerprint,
145
+ base_url_present: true,
146
+ launch_env: {
147
+ api_key: keyOk ? 'set' : 'failed',
148
+ base_url: baseOk ? 'set' : 'failed'
149
+ },
150
+ stale_twins_removed: purge.removed,
151
+ stale_keychain_cleared: purge.keychain_cleared,
152
+ blockers: [
153
+ ...(keyOk ? [] : ['launchctl_setenv_CODEX_LB_API_KEY_failed']),
154
+ ...(baseOk ? [] : ['launchctl_setenv_CODEX_LB_BASE_URL_failed']),
155
+ ...purge.blockers
156
+ ]
157
+ };
158
+ }
159
+ const keyUnset = await unsetEnv('CODEX_LB_API_KEY');
160
+ const openRouterUnset = await unsetEnv('OPENROUTER_API_KEY');
161
+ let baseStatus = 'skipped';
162
+ if (loaded.base_url && (mode === 'desktop-native-bridge' || mode === 'cli-provider')) {
163
+ baseStatus = await setEnv('CODEX_LB_BASE_URL', loaded.base_url) ? 'set' : 'failed';
164
+ }
165
+ else if (mode === 'disabled') {
166
+ baseStatus = await unsetEnv('CODEX_LB_BASE_URL') ? 'unset' : 'failed';
167
+ }
168
+ const ok = keyUnset && openRouterUnset && baseStatus !== 'failed';
169
+ return {
170
+ schema: 'sks.codex-lb-desktop-center-credentials.v1',
171
+ ok,
172
+ status: ok ? 'desktop_secret_launch_env_cleared' : 'desktop_launch_env_cleanup_failed',
173
+ mode,
174
+ api_key_fingerprint: fingerprint,
175
+ base_url_present: basePresent,
176
+ launch_env: {
177
+ api_key: keyUnset ? 'unset' : 'failed',
178
+ base_url: baseStatus
179
+ },
180
+ stale_twins_removed: purge.removed,
181
+ stale_keychain_cleared: purge.keychain_cleared,
182
+ blockers: [
183
+ ...(keyUnset ? [] : ['launchctl_unsetenv_CODEX_LB_API_KEY_failed']),
184
+ ...(openRouterUnset ? [] : ['launchctl_unsetenv_OPENROUTER_API_KEY_failed']),
185
+ ...(baseStatus === 'failed' ? ['launchctl_base_url_sync_failed'] : []),
186
+ ...purge.blockers
187
+ ]
188
+ };
189
+ }