offload.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * Reasoning offload — config resolution, persistence, and strict degradation.
  3. *
  4. * The offload sends explore's assembled source to a BYO OpenAI-compatible
  5. * reasoning endpoint and returns the synthesized answer. Two invariants are
  6. * load-bearing and covered here:
  7. * 1. The API key is NEVER written to disk — the config stores only the NAME of
  8. * an env var (`keyEnv`); the key is resolved at call time.
  9. * 2. The path is STRICTLY DEGRADABLE — any failure (no endpoint, network error,
  10. * non-2xx, empty body) returns null so the caller serves local source; it
  11. * never throws and never surfaces an error to the agent.
  12. */
  13. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  14. import * as fs from 'fs';
  15. import * as path from 'path';
  16. import * as os from 'os';
  17. import {
  18. readOffloadConfig,
  19. writeOffloadConfig,
  20. resolveOffload,
  21. MANAGED_DEFAULT_URL,
  22. MANAGED_DEFAULT_MODEL,
  23. } from '../src/reasoning/config';
  24. import { readOffloadToken, writeOffloadToken } from '../src/reasoning/credentials';
  25. import { isOffloadEnabled, synthesizeOffload, stripAgentDirectives } from '../src/reasoning/reasoner';
  26. describe('reasoning offload', () => {
  27. let home: string;
  28. // Point ~/.codegraph at a throwaway dir (os.homedir() honors $HOME on POSIX,
  29. // $USERPROFILE on Windows) + start from a clean env each test.
  30. const HOME_ENV = ['HOME', 'USERPROFILE'];
  31. const OFFLOAD_ENV = [
  32. 'CODEGRAPH_OFFLOAD_URL', 'CODEGRAPH_OFFLOAD_MODEL', 'CODEGRAPH_OFFLOAD_KEY',
  33. 'CODEGRAPH_OFFLOAD_EFFORT', 'CODEGRAPH_OFFLOAD_STYLE', 'CODEGRAPH_OFFLOAD_TIMEOUT_MS',
  34. 'CODEGRAPH_OFFLOAD_MAXTOKENS', 'CODEGRAPH_OFFLOAD_STRIP', 'CODEGRAPH_OFFLOAD_DEBUG',
  35. 'CEREBRAS_API_KEY',
  36. ];
  37. let saved: Record<string, string | undefined>;
  38. beforeEach(() => {
  39. home = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-offload-'));
  40. saved = {};
  41. for (const k of [...HOME_ENV, ...OFFLOAD_ENV]) { saved[k] = process.env[k]; delete process.env[k]; }
  42. process.env.HOME = home;
  43. process.env.USERPROFILE = home;
  44. });
  45. afterEach(() => {
  46. for (const k of [...HOME_ENV, ...OFFLOAD_ENV]) {
  47. if (saved[k] === undefined) delete process.env[k];
  48. else process.env[k] = saved[k];
  49. }
  50. vi.restoreAllMocks();
  51. if (fs.existsSync(home)) fs.rmSync(home, { recursive: true, force: true });
  52. });
  53. describe('config persistence', () => {
  54. it('is off, with sensible defaults, when nothing is configured', () => {
  55. const c = resolveOffload();
  56. expect(c.enabled).toBe(false);
  57. expect(c.origin).toBe('none');
  58. expect(c.model).toBe('gpt-oss-120b');
  59. expect(c.effort).toBe('low');
  60. expect(c.style).toBe('plain');
  61. expect(isOffloadEnabled()).toBe(false);
  62. });
  63. it('round-trips the config block and never writes the API key to disk', () => {
  64. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1', model: 'gpt-oss-120b', keyEnv: 'CEREBRAS_API_KEY' });
  65. expect(readOffloadConfig().url).toBe('https://api.cerebras.ai/v1');
  66. const raw = fs.readFileSync(path.join(home, '.codegraph', 'config.json'), 'utf8');
  67. expect(raw).toContain('CEREBRAS_API_KEY'); // the env var NAME is stored
  68. // ...but no actual secret material. Set a key and confirm it isn't on disk.
  69. process.env.CEREBRAS_API_KEY = 'sk-super-secret-value';
  70. expect(fs.readFileSync(path.join(home, '.codegraph', 'config.json'), 'utf8'))
  71. .not.toContain('sk-super-secret-value');
  72. });
  73. it('resolves the API key from the configured env var at call time', () => {
  74. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1', keyEnv: 'CEREBRAS_API_KEY' });
  75. expect(resolveOffload().apiKey).toBeUndefined(); // env var not set yet
  76. process.env.CEREBRAS_API_KEY = 'sk-live';
  77. const c = resolveOffload();
  78. expect(c.enabled).toBe(true);
  79. expect(c.apiKey).toBe('sk-live');
  80. expect(c.keySource).toBe('CEREBRAS_API_KEY');
  81. expect(c.origin).toBe('config');
  82. });
  83. it('clears the offload block on disable, leaving other config keys intact', () => {
  84. const cfgPath = path.join(home, '.codegraph', 'config.json');
  85. fs.mkdirSync(path.dirname(cfgPath), { recursive: true });
  86. fs.writeFileSync(cfgPath, JSON.stringify({ somethingElse: 1, offload: { url: 'x' } }));
  87. writeOffloadConfig(null);
  88. const after = JSON.parse(fs.readFileSync(cfgPath, 'utf8'));
  89. expect(after.offload).toBeUndefined();
  90. expect(after.somethingElse).toBe(1);
  91. });
  92. });
  93. describe('env overrides config', () => {
  94. it('lets CODEGRAPH_OFFLOAD_URL override the file and report origin=env', () => {
  95. writeOffloadConfig({ url: 'https://file.example/v1' });
  96. process.env.CODEGRAPH_OFFLOAD_URL = 'https://env.example/v1';
  97. const c = resolveOffload();
  98. expect(c.url).toBe('https://env.example/v1');
  99. expect(c.origin).toBe('env');
  100. });
  101. it('reads the key directly from CODEGRAPH_OFFLOAD_KEY when set', () => {
  102. process.env.CODEGRAPH_OFFLOAD_URL = 'https://env.example/v1';
  103. process.env.CODEGRAPH_OFFLOAD_KEY = 'sk-direct';
  104. const c = resolveOffload();
  105. expect(c.apiKey).toBe('sk-direct');
  106. expect(c.keySource).toBe('CODEGRAPH_OFFLOAD_KEY');
  107. });
  108. });
  109. describe('strict degradation (never throws, returns null to fall back)', () => {
  110. it('returns null when no endpoint is configured', async () => {
  111. expect(await synthesizeOffload({ query: 'q', context: 'ctx' })).toBeNull();
  112. });
  113. it('returns null when the upstream request rejects', async () => {
  114. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1' });
  115. vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('ECONNREFUSED')));
  116. expect(await synthesizeOffload({ query: 'q', context: 'ctx' })).toBeNull();
  117. });
  118. it('returns null on a non-2xx response', async () => {
  119. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1' });
  120. vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
  121. ok: false, status: 500, text: async () => 'boom',
  122. }));
  123. expect(await synthesizeOffload({ query: 'q', context: 'ctx' })).toBeNull();
  124. });
  125. it('returns null when the model returns an empty answer', async () => {
  126. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1' });
  127. vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
  128. ok: true, status: 200, json: async () => ({ choices: [{ message: { content: ' ' } }] }),
  129. }));
  130. expect(await synthesizeOffload({ query: 'q', context: 'ctx' })).toBeNull();
  131. });
  132. });
  133. describe('success path', () => {
  134. it('returns the synthesized answer (with the plain footer) and posts an OpenAI-compatible body with the key', async () => {
  135. writeOffloadConfig({ url: 'https://api.cerebras.ai/v1', model: 'gpt-oss-120b', keyEnv: 'CEREBRAS_API_KEY' });
  136. process.env.CEREBRAS_API_KEY = 'sk-live';
  137. const fetchMock = vi.fn().mockResolvedValue({
  138. ok: true, status: 200,
  139. json: async () => ({ choices: [{ message: { content: 'Coverage: full.\nThe answer.' }, finish_reason: 'stop' }] }),
  140. });
  141. vi.stubGlobal('fetch', fetchMock);
  142. const out = await synthesizeOffload({ query: 'how does X work', context: 'source here' });
  143. expect(out).toContain('Coverage: full.');
  144. expect(out).toContain('Synthesized by CodeGraph'); // plain footer present
  145. const [calledUrl, init] = fetchMock.mock.calls[0];
  146. expect(calledUrl).toBe('https://api.cerebras.ai/v1/chat/completions');
  147. expect((init.headers as Record<string, string>).authorization).toBe('Bearer sk-live');
  148. const body = JSON.parse(init.body as string);
  149. expect(body.model).toBe('gpt-oss-120b');
  150. expect(body.messages[1].content).toContain('source here');
  151. expect(body.messages[1].content).toContain('how does X work');
  152. });
  153. });
  154. describe('stripAgentDirectives', () => {
  155. it('drops the agent-directed header but keeps source sections', () => {
  156. const ctx = [
  157. '## Exploration: how does X work',
  158. 'Found 12 symbols across 3 files.',
  159. '',
  160. '#### src/a.ts — foo(function)',
  161. 'code body',
  162. ].join('\n');
  163. const stripped = stripAgentDirectives(ctx);
  164. expect(stripped).not.toContain('## Exploration:');
  165. expect(stripped).not.toContain('Found 12 symbols');
  166. expect(stripped).toContain('#### src/a.ts');
  167. expect(stripped).toContain('code body');
  168. });
  169. });
  170. describe('managed tier (CodeGraph AI)', () => {
  171. it('stores the org token at 0600 in credentials.json, not in config.json', () => {
  172. writeOffloadConfig({ managed: true });
  173. writeOffloadToken('cgai_secrettoken');
  174. expect(readOffloadToken()).toBe('cgai_secrettoken');
  175. // config.json carries the managed flag but NOT the token.
  176. const cfg = fs.readFileSync(path.join(home, '.codegraph', 'config.json'), 'utf8');
  177. expect(cfg).toContain('managed');
  178. expect(cfg).not.toContain('cgai_secrettoken');
  179. const credPath = path.join(home, '.codegraph', 'credentials.json');
  180. expect(fs.readFileSync(credPath, 'utf8')).toContain('cgai_secrettoken');
  181. // POSIX perms must be owner-only (0600). (Windows has no POSIX mode bits.)
  182. if (process.platform !== 'win32') {
  183. expect(fs.statSync(credPath).mode & 0o777).toBe(0o600);
  184. }
  185. });
  186. it('resolves managed mode to the gateway URL + public model id + login token', () => {
  187. writeOffloadConfig({ managed: true });
  188. writeOffloadToken('cgai_live');
  189. const c = resolveOffload();
  190. expect(c.enabled).toBe(true);
  191. expect(c.managed).toBe(true);
  192. expect(c.url).toBe(MANAGED_DEFAULT_URL);
  193. expect(c.model).toBe(MANAGED_DEFAULT_MODEL);
  194. expect(c.apiKey).toBe('cgai_live');
  195. expect(c.keySource).toBe('codegraph login');
  196. });
  197. it('is NOT enabled when managed but signed out (no token)', () => {
  198. writeOffloadConfig({ managed: true });
  199. const c = resolveOffload();
  200. expect(c.managed).toBe(true);
  201. expect(c.enabled).toBe(false); // url defaults, but no token → effectively logged out
  202. expect(isOffloadEnabled()).toBe(false);
  203. });
  204. it('clears the token on logout', () => {
  205. writeOffloadToken('cgai_live');
  206. writeOffloadToken(null);
  207. expect(readOffloadToken()).toBeUndefined();
  208. });
  209. it('lets env override the managed endpoint and token (for testing)', () => {
  210. writeOffloadConfig({ managed: true });
  211. writeOffloadToken('cgai_stored');
  212. process.env.CODEGRAPH_OFFLOAD_URL = 'http://localhost:8787/v1';
  213. process.env.CODEGRAPH_OFFLOAD_KEY = 'cgai_env';
  214. const c = resolveOffload();
  215. expect(c.url).toBe('http://localhost:8787/v1');
  216. expect(c.apiKey).toBe('cgai_env');
  217. expect(c.keySource).toBe('CODEGRAPH_OFFLOAD_KEY');
  218. });
  219. });
  220. });