-
Notifications
You must be signed in to change notification settings - Fork 10
feat(agent): scaffold built-in Harper Agent component (#626) #839
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a336f0
Add built-in agent component scaffold (#626)
kriszyp 5bd2af9
Address Gemini review findings
kriszyp cafd17d
Address Gemini/Claude bot review findings on PR
kriszyp b8cb1e0
Fix latent bugs surfaced by cross-model review experiment
kriszyp e8b60be
fsTools: refuse to read/write through a symlink leaf
kriszyp edc22ff
loop: stay paused until all gated calls in a turn are resolved
kriszyp 22cfa83
Merge remote-tracking branch 'origin/main' into feat/builtin-agent-co…
kriszyp 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,203 @@ | ||
| /** | ||
| * Built-in Harper Agent component (#626). | ||
| * | ||
| * `startOnMainThread` is invoked once on the main thread by `componentLoader`. | ||
| * When `agent.enabled` is `false` (default) the component registers nothing | ||
| * and returns immediately — opt-in keeps surprise LLM costs at bay. When | ||
| * enabled, the six operations land on the operations API, the session table | ||
| * is realized lazily on first use, and the loop runs in-process. | ||
| * | ||
| * The component intentionally avoids `handleApplication`: it has nothing | ||
| * worker-thread-shaped to do. Operator-only tools (FS, schedule, fetch) are | ||
| * inline; registry-backed tools (#615/#617/#618) will fold in via toolset.ts | ||
| * once those land. | ||
| */ | ||
|
|
||
| import { dirname, isAbsolute, resolve as resolvePath } from 'node:path'; | ||
| import { CONFIG_PARAMS } from '../utility/hdbTerms.ts'; | ||
| import harperLogger from '../utility/logging/harper_logger.ts'; | ||
| import { Models } from '../resources/models/Models.ts'; | ||
| import { composeToolset } from './toolset.ts'; | ||
| import { buildOperations } from './operations.ts'; | ||
| import { runAgent, _resetInFlightForTests } from './loop.ts'; | ||
| import { appendMessage, getSession } from './session.ts'; | ||
| import type { AgentConfig, AgentScopes, AgentTool } from './types.ts'; | ||
|
|
||
| const log = harperLogger.loggerWithTag('agent'); | ||
|
|
||
| const DEFAULT_CONFIG: AgentConfig = { | ||
| enabled: false, | ||
| maxTurns: 50, | ||
| maxCostUsd: 5, | ||
| autoApprove: false, | ||
| allowDestructive: false, | ||
| user: 'hdb_agent', | ||
| }; | ||
|
|
||
| interface StartOpts { | ||
| server: { | ||
| registerOperation: (def: { name: string; execute: (op: any) => any | Promise<any> }) => void; | ||
| }; | ||
| // Component-level config plumbed by componentLoader (`...componentConfig`). | ||
| enabled?: boolean; | ||
| provider?: string; | ||
| model?: string; | ||
| maxTurns?: number; | ||
| maxCostUsd?: number; | ||
| autoApprove?: boolean; | ||
| allowDestructive?: boolean; | ||
| user?: string; | ||
| componentsScope?: string; | ||
| } | ||
|
|
||
| export async function startOnMainThread(opts: StartOpts): Promise<void> { | ||
| const config = mergeConfig(opts); | ||
| if (!config.enabled) { | ||
| log.info?.('Agent component disabled (agent.enabled=false); skipping registration'); | ||
| return; | ||
| } | ||
|
|
||
| // Lazily required to avoid pulling configUtils at module-eval time for tests. | ||
| const { getConfigPath, getConfigFilePath } = require('../config/configUtils.js'); | ||
| const scopes = resolveScopes(config, getConfigPath, getConfigFilePath); | ||
|
|
||
| const models = new Models(); | ||
| const abortControllers = new Map<string, AbortController>(); | ||
| let liveConfig: AgentConfig = config; | ||
| let composed = composeToolset({ | ||
| allowDestructive: liveConfig.allowDestructive, | ||
| onFollowup: handleFollowup, | ||
| }); | ||
|
|
||
| // Only warn when the operator explicitly configured `maxCostUsd`. Logging on the default | ||
| // every boot would flood the log without telling anyone anything actionable. | ||
| if (opts.maxCostUsd !== undefined) { | ||
| log.warn?.( | ||
| `agent.maxCostUsd=${liveConfig.maxCostUsd} is advertised but not yet enforced; cost-cap wiring depends on #612 telemetry.` | ||
| ); | ||
| } | ||
|
|
||
| async function handleFollowup(sessionId: string, prompt: string): Promise<void> { | ||
| const session = await getSession(sessionId); | ||
| if (!session) { | ||
| log.warn?.(`schedule_followup target session ${sessionId} not found`); | ||
| return; | ||
| } | ||
| await appendMessage(sessionId, { role: 'user', content: prompt, createdAt: Date.now() }); | ||
| startRun(sessionId); | ||
| } | ||
|
|
||
| function currentTools(): AgentTool[] { | ||
| return composed.tools; | ||
| } | ||
|
|
||
| function startRun(sessionId: string): void { | ||
| // A run is already active for this session. `runAgent` coalesces concurrent starts onto the | ||
| // existing in-flight promise (bound to the existing controller), so creating a second | ||
| // controller here would orphan it — `cancelRun` would then abort a controller nothing is | ||
| // listening to, leaving the live run uncancellable. Any message appended before this call | ||
| // (e.g. by a scheduled followup) is picked up by the in-flight loop on its next turn. | ||
| if (abortControllers.has(sessionId)) return; | ||
| const controller = new AbortController(); | ||
| abortControllers.set(sessionId, controller); | ||
| runAgent({ | ||
| sessionId, | ||
| models, | ||
| tools: currentTools(), | ||
| scopes, | ||
| maxTurns: liveConfig.maxTurns, | ||
| autoApprove: liveConfig.autoApprove, | ||
| signal: controller.signal, | ||
| generateOpts: { model: liveConfig.model }, | ||
| }) | ||
| .catch((err) => log.error?.(`Agent run failed for ${sessionId}: ${(err as Error)?.message ?? err}`)) | ||
| .finally(() => { | ||
| if (abortControllers.get(sessionId) === controller) abortControllers.delete(sessionId); | ||
| }); | ||
| } | ||
|
|
||
| function cancelRun(sessionId: string): boolean { | ||
| // Clear any scheduled followups first. Without this, a timer set via `schedule_followup` | ||
| // would fire after the operator cancelled, silently re-injecting a user prompt and kicking | ||
| // the loop off again — surprising behavior and avoidable LLM cost. | ||
| for (const [id, followup] of composed.scheduled.entries()) { | ||
| if (followup.sessionId === sessionId) { | ||
| clearTimeout(followup.timer); | ||
| composed.scheduled.delete(id); | ||
| } | ||
| } | ||
| const controller = abortControllers.get(sessionId); | ||
| if (!controller) return false; | ||
| controller.abort(new Error('cancelled by operator')); | ||
| abortControllers.delete(sessionId); | ||
| return true; | ||
| } | ||
|
|
||
| function setConfig(patch: Partial<AgentConfig>): AgentConfig { | ||
| const previousAllowDestructive = liveConfig.allowDestructive; | ||
| liveConfig = { ...liveConfig, ...patch }; | ||
| if (liveConfig.allowDestructive !== previousAllowDestructive) { | ||
| composed = composeToolset({ | ||
| allowDestructive: liveConfig.allowDestructive, | ||
| onFollowup: handleFollowup, | ||
| }); | ||
| } | ||
| // NOTE: an already in-flight run captured its toolset (and autoApprove) at start, so flipping | ||
| // allowDestructive here only affects subsequent runs — the live loop finishes on its existing | ||
| // toolset. Acceptable: the approval gate still applies on the next turn's run, and operators | ||
| // who need to halt a run immediately use cancel_agent_run. Tightening to per-turn re-evaluation | ||
| // would require threading a config getter into the loop; deferred until there's a need. | ||
| return liveConfig; | ||
| } | ||
|
|
||
| const operations = buildOperations({ | ||
| getConfig: () => liveConfig, | ||
| setConfig, | ||
| startRun, | ||
| cancelRun, | ||
| }); | ||
| for (const op of operations) opts.server.registerOperation(op); | ||
|
|
||
| log.info?.(`Agent component initialized with ${composed.tools.length} tools`); | ||
| } | ||
|
|
||
| function resolveScopes( | ||
| config: AgentConfig, | ||
| getConfigPath: (param: string) => string | undefined, | ||
| getConfigFilePath?: () => string | ||
| ): AgentScopes { | ||
| const componentsRoot = getConfigPath(CONFIG_PARAMS.COMPONENTSROOT) ?? process.cwd(); | ||
| const logDir = getConfigPath(CONFIG_PARAMS.LOGGING_ROOT) ?? process.cwd(); | ||
| const rootPath = getConfigPath(CONFIG_PARAMS.ROOTPATH) ?? componentsRoot; | ||
| const configFile = getConfigFilePath?.(); | ||
| const configDir = configFile ? dirname(configFile) : process.cwd(); | ||
| // A relative `componentsScope` is resolved against rootPath, as documented in the schema — | ||
| // NOT against componentsRoot, which would double-nest (`./components` → componentsRoot/components). | ||
| // With no scope set, the full componentsRoot is the FS write scope. | ||
| const scopedComponents = config.componentsScope | ||
| ? isAbsolute(config.componentsScope) | ||
| ? config.componentsScope | ||
| : resolvePath(rootPath, config.componentsScope) | ||
| : componentsRoot; | ||
| return { componentsRoot: scopedComponents, logDir, configDir }; | ||
| } | ||
|
|
||
| function mergeConfig(opts: StartOpts): AgentConfig { | ||
| return { | ||
| ...DEFAULT_CONFIG, | ||
| ...(opts.enabled !== undefined && { enabled: !!opts.enabled }), | ||
| ...(opts.provider !== undefined && { provider: String(opts.provider) }), | ||
| ...(opts.model !== undefined && { model: String(opts.model) }), | ||
| ...(opts.maxTurns !== undefined && { maxTurns: Number(opts.maxTurns) }), | ||
| ...(opts.maxCostUsd !== undefined && { maxCostUsd: Number(opts.maxCostUsd) }), | ||
| ...(opts.autoApprove !== undefined && { autoApprove: !!opts.autoApprove }), | ||
| ...(opts.allowDestructive !== undefined && { allowDestructive: !!opts.allowDestructive }), | ||
| ...(opts.user !== undefined && { user: String(opts.user) }), | ||
| ...(opts.componentsScope !== undefined && { componentsScope: String(opts.componentsScope) }), | ||
| }; | ||
| } | ||
|
|
||
| /** Test-only: reset module state between specs. */ | ||
| export function _resetForTests(): void { | ||
| _resetInFlightForTests(); | ||
| } | ||
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.