-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(server): fetch boot context from ContexGin HTTP API #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dimakis
wants to merge
1
commit into
main
Choose a base branch
from
feat/contexgin-http-boot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import type { BootContextMessage } from '../chat.js'; | ||
|
|
||
| // Mock fetch globally | ||
| const mockFetch = vi.fn(); | ||
| vi.stubGlobal('fetch', mockFetch); | ||
|
|
||
| // Mock logger to suppress output | ||
| vi.mock('../logger.js', () => ({ | ||
| createLogger: () => ({ | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| debug: vi.fn(), | ||
| }), | ||
| })); | ||
|
|
||
| // Mock the event store to avoid SQLite initialization | ||
| class FakeEventStore { | ||
| recordEvent = vi.fn(); | ||
| getEvents = vi.fn().mockReturnValue([]); | ||
| getSession = vi.fn().mockReturnValue(null); | ||
| upsertSession = vi.fn(); | ||
| } | ||
| vi.mock('../event-store.js', () => ({ | ||
| EventStore: FakeEventStore, | ||
| })); | ||
|
|
||
| // Mock repo-config | ||
| vi.mock('../repo-config.js', () => ({ | ||
| loadRepoConfig: vi.fn(() => ({ | ||
| contextBlocks: {}, | ||
| quickActions: [], | ||
| venvPaths: [], | ||
| resolvedVenvPaths: [], | ||
| allowedPaths: [], | ||
| roots: [], | ||
| toolTierOverrides: {}, | ||
| inboxPath: '', | ||
| resolvedInboxPath: '', | ||
| repos: {}, | ||
| isolation: true, | ||
| })), | ||
| })); | ||
|
|
||
| const { fetchBootContext } = await import('../chat.js'); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('fetchBootContext', () => { | ||
| const CONTEXGIN_URL = 'http://localhost:4195'; | ||
|
|
||
| it('returns contexgin boot context on successful response', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| agent: 'mitzo-conversational', | ||
| boot: { | ||
| content: '# Boot payload\nContext here.', | ||
| tokens: 11297, | ||
| sources: ['CONSTITUTION.md', 'memory/Profile/Principles.md'], | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const result = await fetchBootContext('mitzo-conversational', CONTEXGIN_URL); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledOnce(); | ||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${CONTEXGIN_URL}/api/agents/mitzo-conversational/context`, | ||
| { signal: expect.any(AbortSignal) }, | ||
| ); | ||
|
|
||
| expect(result).toEqual<BootContextMessage>({ | ||
| type: 'boot_context', | ||
| source: 'contexgin', | ||
| sourceCount: 2, | ||
| tokenCount: 11297, | ||
| tokenBudget: 11297, | ||
| sources: [ | ||
| { path: 'CONSTITUTION.md', kind: 'reference' }, | ||
| { path: 'memory/Profile/Principles.md', kind: 'reference' }, | ||
| ], | ||
| included: [], | ||
| trimmed: [], | ||
| fullMarkdown: '# Boot payload\nContext here.', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns local-fallback when ContexGin is unreachable', async () => { | ||
| mockFetch.mockRejectedValueOnce(new Error('ECONNREFUSED')); | ||
|
|
||
| const result = await fetchBootContext('mitzo-conversational', CONTEXGIN_URL); | ||
|
|
||
| expect(result.source).toBe('local-fallback'); | ||
| expect(result.sourceCount).toBe(0); | ||
| expect(result.tokenCount).toBe(0); | ||
| }); | ||
|
|
||
| it('returns local-fallback on non-200 response', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 404, | ||
| text: async () => '{"error":"Agent not found"}', | ||
| }); | ||
|
|
||
| const result = await fetchBootContext('nonexistent-agent', CONTEXGIN_URL); | ||
|
|
||
| expect(result.source).toBe('local-fallback'); | ||
| expect(result.sourceCount).toBe(0); | ||
| }); | ||
|
|
||
| it('returns local-fallback when response lacks boot field', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ agent: 'test', identity: {} }), | ||
| }); | ||
|
|
||
| const result = await fetchBootContext('mitzo-conversational', CONTEXGIN_URL); | ||
|
|
||
| expect(result.source).toBe('local-fallback'); | ||
| }); | ||
|
|
||
| it('handles empty sources array', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| boot: { content: 'minimal', tokens: 100, sources: [] }, | ||
| }), | ||
| }); | ||
|
|
||
| const result = await fetchBootContext('mitzo-conversational', CONTEXGIN_URL); | ||
|
|
||
| expect(result.source).toBe('contexgin'); | ||
| expect(result.sourceCount).toBe(0); | ||
| expect(result.sources).toEqual([]); | ||
| expect(result.tokenCount).toBe(100); | ||
| }); | ||
|
|
||
| it('filters non-string sources gracefully', async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| boot: { | ||
| content: 'payload', | ||
| tokens: 500, | ||
| sources: ['valid.md', null, 42, 'also-valid.md'], | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| const result = await fetchBootContext('mitzo-conversational', CONTEXGIN_URL); | ||
|
|
||
| expect(result.sources).toEqual([ | ||
| { path: 'valid.md', kind: 'reference' }, | ||
| { path: 'also-valid.md', kind: 'reference' }, | ||
| ]); | ||
| expect(result.sourceCount).toBe(2); | ||
| }); | ||
|
|
||
| it('uses default URL from env when not provided', async () => { | ||
| const origUrl = process.env.CONTEXGIN_URL; | ||
| process.env.CONTEXGIN_URL = 'http://test-host:9999'; | ||
|
|
||
| mockFetch.mockRejectedValueOnce(new Error('timeout')); | ||
|
|
||
| await fetchBootContext('mitzo-conversational'); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| 'http://test-host:9999/api/agents/mitzo-conversational/context', | ||
| expect.any(Object), | ||
| ); | ||
|
|
||
| if (origUrl !== undefined) { | ||
| process.env.CONTEXGIN_URL = origUrl; | ||
| } else { | ||
| delete process.env.CONTEXGIN_URL; | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 unsafe_assumptions: Env var cleanup in the last test is not wrapped in
try/finally. If an assertion fails before line 180,process.env.CONTEXGIN_URLleaks its test value into subsequent tests. Usetry/finallyor Vitest'svi.stubEnv()for safer cleanup.[fixable]