Repository navigation
feat(needle): comprehensive integration of local pre-query planner bridge, cross-platform python runners, robust provider fallbacks, and REPL/agent routing #2292
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
FluxLuFFy
wants to merge
10
commits into
Twigpine:main
Choose a base branch
from
FluxLuFFy:feature/needle-integration
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f510e7a
feat: integrate Needle local planner bridge & fallback
9b48fad
fix: harden Needle worker lifecycle
01b4bad
fix: stabilize Needle REPL sessions
2a30e98
fix: scope Needle planning to current turn
6b2d827
fix: bypass Needle for image turns
8ce756c
fix: preserve Needle turn safety
de60692
fix: complete Needle fallback coverage
3d0810f
fix: stop Needle turns cleanly on shutdown
ada0804
fix: clear Needle state on early abort
ce76b43
fix: pair cancelled Needle tool prelude
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
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
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,12 @@ | ||
| import type { Command } from '../../commands.js' | ||
|
|
||
| const needle: Command = { | ||
| type: 'local', | ||
| name: 'needle', | ||
| description: 'Enable or disable the local Needle tool-calling planner', | ||
| supportsNonInteractive: true, | ||
| argumentHint: 'enable | disable | status | mode <worker|main>', | ||
| load: () => import('./needle.js'), | ||
| } | ||
|
|
||
| export default needle |
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,64 @@ | ||
| import { describe, expect, it, beforeEach, afterEach, spyOn } from 'bun:test' | ||
| import { mkdtempSync, rmSync } from 'fs' | ||
| import { tmpdir } from 'os' | ||
| import { join } from 'path' | ||
| import { call as needleCall } from './needle.js' | ||
| import * as needleClient from '../../services/needle/needleClient.js' | ||
| import { getGlobalConfig, saveGlobalConfig } from '../../utils/config.js' | ||
| import { setClaudeConfigHomeDirForTesting } from '../../utils/envUtils.js' | ||
| import type { LocalJSXCommandContext } from '../../types/command.js' | ||
| import { | ||
| acquireSharedMutationLock, | ||
| releaseSharedMutationLock, | ||
| } from '../../test/sharedMutationLock.js' | ||
|
|
||
| describe('needle command', () => { | ||
| let configDir: string | ||
| beforeEach(async () => { | ||
| await acquireSharedMutationLock('commands/needle.test.ts') | ||
| configDir = mkdtempSync(join(tmpdir(), 'openclaude-needle-command-')) | ||
| process.env.CLAUDE_CONFIG_DIR = configDir | ||
| setClaudeConfigHomeDirForTesting(configDir) | ||
| }) | ||
| afterEach(() => { | ||
| saveGlobalConfig(current => ({ ...current, needleEnabled: false, needleMode: 'worker' })) | ||
| setClaudeConfigHomeDirForTesting(undefined) | ||
| delete process.env.CLAUDE_CONFIG_DIR | ||
| rmSync(configDir, { recursive: true, force: true }) | ||
| releaseSharedMutationLock() | ||
| }) | ||
|
|
||
| it('refuses enablement when Needle is unavailable', async () => { | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| process.env.OPENCLAUDE_NEEDLE_PYTHON = process.platform === 'win32' ? 'missing-needle-python.exe' : '/bin/false' | ||
| const context = {} as unknown as LocalJSXCommandContext | ||
| try { | ||
| const result = await needleCall('enable', context) | ||
| expect(getGlobalConfig().needleEnabled).toBe(false) | ||
| expect(result.type).toBe('text') | ||
| if (result.type === 'text') expect(result.value).toContain('was not enabled') | ||
| } finally { | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| } | ||
| }) | ||
|
|
||
| it('toggles the worker and persists its mode', async () => { | ||
| const availability = spyOn(needleClient, 'checkNeedleAvailability').mockResolvedValue({ available: true }) | ||
| const context = {} as unknown as LocalJSXCommandContext | ||
| try { | ||
| await needleCall('enable', context) | ||
| expect(getGlobalConfig().needleEnabled).toBe(true) | ||
| expect(getGlobalConfig().needleMode).toBe('worker') | ||
|
|
||
| await needleCall('mode main', context) | ||
| expect(getGlobalConfig().needleMode).toBe('main') | ||
|
|
||
| const status = await needleCall('status', context) | ||
| expect(status.type).toBe('text') | ||
| if (status.type === 'text') expect(status.value).toContain('main') | ||
| } finally { | ||
| availability.mockRestore() | ||
| } | ||
| }) | ||
| }) | ||
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,62 @@ | ||
| import type { LocalCommandCall } from '../../types/command.js' | ||
| import { getGlobalConfig, saveGlobalConfig } from '../../utils/config.js' | ||
| import chalk from 'chalk' | ||
| import { checkNeedleAvailability } from '../../services/needle/needleClient.js' | ||
|
|
||
| export const call: LocalCommandCall = async args => { | ||
| const parts = String(args ?? '').trim().toLowerCase().split(/\s+/).filter(Boolean) | ||
| const command = parts[0] ?? 'status' | ||
| const config = getGlobalConfig() | ||
| const enabled = config.needleEnabled === true | ||
| const mode = config.needleMode ?? 'worker' | ||
|
|
||
| if (command === 'status') { | ||
| return { | ||
| type: 'text', | ||
| value: `Needle: ${enabled ? chalk.green('enabled') : chalk.red('disabled')} (${mode} mode)`, | ||
| } | ||
| } | ||
|
|
||
| if (command === 'enable' || command === 'disable') { | ||
| const nextEnabled = command === 'enable' | ||
| if (nextEnabled) { | ||
| const availability = await checkNeedleAvailability() | ||
| if (!availability.available) { | ||
| return { | ||
| type: 'text', | ||
| value: `Needle was not enabled because its Python package is unavailable. Install it from source, then retry /needle enable. (${availability.error ?? 'import failed'})`, | ||
| } | ||
| } | ||
| } | ||
| saveGlobalConfig(current => ({ | ||
| ...current, | ||
| needleEnabled: nextEnabled, | ||
| needleMode: current.needleMode ?? 'worker', | ||
| })) | ||
| return { | ||
| type: 'text', | ||
| value: `Needle ${nextEnabled ? chalk.green('enabled') : chalk.red('disabled')} (${mode} mode).`, | ||
| } | ||
| } | ||
|
|
||
| if (command === 'mode') { | ||
| const nextMode = parts[1] | ||
| if (nextMode !== 'worker' && nextMode !== 'main') { | ||
| return { type: 'text', value: 'Usage: /needle mode <worker|main>' } | ||
| } | ||
| saveGlobalConfig(current => ({ ...current, needleMode: nextMode })) | ||
| return { | ||
| type: 'text', | ||
| value: `Needle mode set to ${chalk.cyan(nextMode)}. ${ | ||
| nextMode === 'main' | ||
| ? 'The main conversation will use Needle when enabled.' | ||
| : 'AgentTool workers will use Needle when enabled.' | ||
| }`, | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| type: 'text', | ||
| value: 'Usage: /needle <enable|disable|status|mode worker|main>', | ||
| } | ||
| } |
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
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,108 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { | ||
| buildNeedleToolPayload, | ||
| DEFAULT_NEEDLE_TIMEOUT_MS, | ||
| getNeedlePythonCommand, | ||
| getNeedleTimeoutMs, | ||
| NEEDLE_WORKER_MODEL, | ||
| } from './needleClient.js' | ||
|
|
||
| describe('Needle client configuration', () => { | ||
| test('uses the dedicated worker model identifier', () => { | ||
| expect(NEEDLE_WORKER_MODEL).toBe('needle-worker') | ||
| }) | ||
|
|
||
| test('uses platform-appropriate Python launchers', () => { | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| try { | ||
| expect(getNeedlePythonCommand('linux')).toEqual({ command: 'python3', args: [] }) | ||
| expect(getNeedlePythonCommand('darwin')).toEqual({ command: 'python3', args: [] }) | ||
| expect(getNeedlePythonCommand('win32')).toEqual({ command: 'py', args: ['-3'] }) | ||
| } finally { | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| } | ||
| }) | ||
|
|
||
| test('converts Anthropic-style tool schemas to Needle payloads', () => { | ||
| expect(buildNeedleToolPayload([ | ||
| { name: 'read', description: 'Read a file', input_schema: { type: 'object', properties: {} } }, | ||
| ])).toEqual([ | ||
| { name: 'read', description: 'Read a file', parameters: { type: 'object', properties: {} } }, | ||
| ]) | ||
| }) | ||
|
|
||
| test.skipIf(process.platform === 'win32')('restarts the worker after a child exit before serving later requests', async () => { | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| process.env.OPENCLAUDE_NEEDLE_PYTHON = '/bin/false' | ||
| const client = new (await import('./needleClient.js')).NeedleClient(100) | ||
| try { | ||
| await expect(client.complete({ prompt: 'first', tools: [] })).rejects.toThrow('exited with code 1') | ||
| await expect(client.complete({ prompt: 'second', tools: [] })).rejects.toThrow('exited with code 1') | ||
| } finally { | ||
| client.close() | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| } | ||
| }) | ||
|
|
||
| test.skipIf(process.platform === 'win32')('restarts the worker after a zero child exit before serving later requests', async () => { | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| process.env.OPENCLAUDE_NEEDLE_PYTHON = '/bin/true' | ||
| const client = new (await import('./needleClient.js')).NeedleClient(100) | ||
| try { | ||
| await expect(client.complete({ prompt: 'first', tools: [] })).rejects.toThrow('exited with code 0') | ||
| await expect(client.complete({ prompt: 'second', tools: [] })).rejects.toThrow('exited with code 0') | ||
| } finally { | ||
| client.close() | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| } | ||
| }) | ||
|
|
||
| test.skipIf(process.platform === 'win32')('rejects malformed matching responses instead of leaving requests pending', async () => { | ||
| const directory = mkdtempSync(join(tmpdir(), 'needle-runtime-')) | ||
| const runtime = join(directory, 'runtime.py') | ||
| const command = join(directory, 'python') | ||
| writeFileSync(runtime, `import json, sys\nfor line in sys.stdin:\n request = json.loads(line)\n print(json.dumps({"id": request["id"]}), flush=True)\n`) | ||
| writeFileSync(command, `#!/bin/sh\nexec python3 /${runtime.replace(/^\//, '')}\n`) | ||
| chmodSync(command, 0o755) | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| process.env.OPENCLAUDE_NEEDLE_PYTHON = command | ||
| const client = new (await import('./needleClient.js')).NeedleClient(1_000) | ||
| try { | ||
| await expect(client.complete({ prompt: 'malformed', tools: [] })).rejects.toThrow('Needle worker returned no result') | ||
| } finally { | ||
| client.close() | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| rmSync(directory, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| test.skipIf(process.platform === 'win32')('restarts the worker after a timeout before serving later requests', async () => { | ||
| const directory = mkdtempSync(join(tmpdir(), 'needle-runtime-')) | ||
| const runtime = join(directory, 'runtime.py') | ||
| const command = join(directory, 'python') | ||
| writeFileSync(runtime, `import json, sys, time\nfor line in sys.stdin:\n request = json.loads(line)\n if request["prompt"] == "delay": time.sleep(0.2)\n print(json.dumps({"id": request["id"], "result": {"function_calls": [{"name": "read", "arguments": {"prompt": request["prompt"]}}]}}), flush=True)\n`) | ||
| writeFileSync(command, `#!/bin/sh\nexec python3 /${runtime.replace(/^\//, '')}\n`) | ||
| chmodSync(command, 0o755) | ||
| const previous = process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| process.env.OPENCLAUDE_NEEDLE_PYTHON = command | ||
| const client = new (await import('./needleClient.js')).NeedleClient(30) | ||
| try { | ||
| await expect(client.complete({ prompt: 'delay', tools: [] })).rejects.toThrow('timed out') | ||
| const response = await client.complete({ prompt: 'fresh', tools: [] }) | ||
| expect(response.functionCalls[0]?.arguments.prompt).toBe('fresh') | ||
| } finally { | ||
| client.close() | ||
| if (previous === undefined) delete process.env.OPENCLAUDE_NEEDLE_PYTHON | ||
| else process.env.OPENCLAUDE_NEEDLE_PYTHON = previous | ||
| rmSync(directory, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.