feat: let agents read supervisor story graphs
Expose saved decisions, timelines and source evidence through read-only Story Graph tools across Model, Harness and local/remote OpenCode and Continue. Supervisor enablement and runtime assignments govern discovery and calls; reads do not invoke models or modify stored evidence. Remote use requires matching Desktop and Agent changes. Make Continue discussion show the complete editable message with a separated destination and shared action layout. Sending uses the visible draft without reattaching removed context. Validation: existing focused MCP, runtime, controlled Linux Host, discussion component and Electron layout checks plus typechecks; staged diff check passed. Cross-scope fact reuse, historical reconstruction and full package acceptance remain deferred. Release note: Agents can consult saved supervisor decisions and their sources when continuing work. Continue discussion now lets you review and edit the complete message before sending.
This commit is contained in:
@@ -1188,8 +1188,8 @@ export class AcpRemoteRuntime implements AgentRuntime {
|
||||
raw = await this.awaitOperation(
|
||||
context,
|
||||
'启动远端请求',
|
||||
request.imageToolBinding
|
||||
? context.channel.preparePrompt(preparation, request.imageToolBinding, signal)
|
||||
request.imageToolBinding || request.storyGraphBinding
|
||||
? context.channel.preparePrompt(preparation, request.imageToolBinding, signal, request.storyGraphBinding)
|
||||
: context.channel.preparePrompt(preparation),
|
||||
signal
|
||||
)
|
||||
|
||||
@@ -1270,9 +1270,7 @@ export class ContinueHostAdapter {
|
||||
)
|
||||
}
|
||||
const capabilityServers = [
|
||||
...(runOptions.workMode === 'execute'
|
||||
? runOptions.sessionMcpServers ?? []
|
||||
: []),
|
||||
...(runOptions.sessionMcpServers ?? []),
|
||||
...(knowledgeCapability
|
||||
? [
|
||||
createLoopbackMcpServer(
|
||||
@@ -1340,7 +1338,7 @@ export class ContinueHostAdapter {
|
||||
)
|
||||
}
|
||||
const retainedServers =
|
||||
runOptions.workMode === 'ask' && Boolean(knowledgeCapability)
|
||||
runOptions.workMode === 'ask' && Boolean(knowledgeCapability || runOptions.sessionMcpServers?.length)
|
||||
? []
|
||||
: servers.filter(
|
||||
(server) =>
|
||||
@@ -1524,7 +1522,7 @@ export class ContinueHostAdapter {
|
||||
}
|
||||
if (
|
||||
runOptions.workMode === 'ask' &&
|
||||
runOptions.knowledgeCapability
|
||||
(runOptions.knowledgeCapability || runOptions.sessionMcpServers?.length)
|
||||
) {
|
||||
for (const toolName of scopedReadToolNames) {
|
||||
args.push('--allow', toolName)
|
||||
|
||||
@@ -43,6 +43,8 @@ import { defaultRuntimeSettings } from '../../shared/contracts'
|
||||
import type { ResolvedRuntimeSettings } from '../runtime-settings-store'
|
||||
import { AssistantDatabase } from '../assistant/assistant-database'
|
||||
import { ImageGenerationService } from './image-generation-service'
|
||||
import { KnowledgeMcpGateway } from './knowledge-mcp-gateway'
|
||||
import type { KnowledgeService } from '../knowledge/knowledge-service'
|
||||
|
||||
vi.mock('electron', () => ({ nativeImage: {} }))
|
||||
|
||||
@@ -101,6 +103,41 @@ it('generates and edits durable images through the controlled Harness Main proxy
|
||||
}, 60_000)
|
||||
|
||||
const CREDENTIAL_REF = 'GOODBUDDY_HARNESS_MODEL_API_KEY'
|
||||
|
||||
it('reads Story Graph through the controlled Harness ACP Main proxy in Ask and Execute, then removes it when disabled', async () => {
|
||||
const root = await realpath(await mkdtemp(join(tmpdir(), 'goodbuddy-dsh-graph-')))
|
||||
const db = new AssistantDatabase(':memory:'); db.initialize(root)
|
||||
let enabled = true
|
||||
const projectId = db.listProjects()[0]!.id
|
||||
const gateway = new KnowledgeMcpGateway({} as KnowledgeService, { storyGraphService: {
|
||||
available: async () => enabled, read: db.readStoryGraph.bind(db)
|
||||
} })
|
||||
const signal = new AbortController().signal
|
||||
const token = gateway.grant('harness-graph', [], signal, 'none', undefined, undefined, undefined, undefined, undefined,
|
||||
{ projectId, runtimeTarget: 'deepseek-harness' })!
|
||||
const inProcess = createInProcessLaunch(root, { stream(options) {
|
||||
const prompt = latestUserText(options)
|
||||
const tool = options.tools?.find(tool => tool.name === 'story_graph_search')
|
||||
if (!enabled) { expect(tool).toBeUndefined(); return textResponse('DISABLED') }
|
||||
expect(tool).toBeDefined()
|
||||
if (!toolResultText(options, prompt)) return toolCall(prompt, 'story_graph_search', { query: 'Decision' })
|
||||
expect(toolResultText(options, prompt)).toContain(projectId)
|
||||
return textResponse('GRAPH_READ')
|
||||
} })
|
||||
const provider = new ModelToolProvider(root, [], undefined, gateway)
|
||||
const runtime = new DeepSeekHarnessRuntime({ defaultWorkspace: root, baseUrl: 'https://chat.test/v1', model: 'fixture',
|
||||
launch: inProcess.launch, credentialRefs: { [CREDENTIAL_REF]: 'fixture' }, toolProvider: provider,
|
||||
initializationTimeoutMs: 20_000, promptTimeoutMs: 20_000, shutdownTimeoutMs: 5_000 })
|
||||
try {
|
||||
for (const mode of ['ask', 'execute', 'disabled'] as const) {
|
||||
enabled = mode !== 'disabled'
|
||||
const events = await collect(runtime.run({ requestId: crypto.randomUUID(), conversationId: 'graph', prompt: mode,
|
||||
workMode: mode === 'execute' ? 'execute' : 'ask', knowledgeCapabilityToken: token }, signal))
|
||||
expect(events.at(-1)?.type).toBe('done')
|
||||
expect(JSON.stringify(events)).toContain(enabled ? 'GRAPH_READ' : 'DISABLED')
|
||||
}
|
||||
} finally { await runtime.dispose(); await provider.dispose(); await gateway.dispose(); db.close(); await rm(root, { recursive: true, force: true }) }
|
||||
}, 60_000)
|
||||
const SKILL_CALL_ID = 'e2e-skill-call'
|
||||
const MCP_CALL_ID = 'e2e-mcp-call'
|
||||
const ASK_MCP_CALL_ID = 'e2e-ask-mcp-call'
|
||||
|
||||
@@ -350,6 +350,14 @@ function proxyToolInputSchema(
|
||||
ReturnType<ModelToolProviderLike['listTools']>
|
||||
>[number]
|
||||
): Record<string, unknown> {
|
||||
if (isStoryGraphTool(tool.name)) {
|
||||
const schema = structuredClone(tool.inputSchema)
|
||||
const scope = (schema.properties as Record<string, Record<string, unknown>>).scope!
|
||||
// Scope is a discriminated union: Global and projects are mutually exclusive.
|
||||
scope.oneOf = scope.anyOf
|
||||
delete scope.anyOf
|
||||
return dshCompatibleMainInputSchema(schema)
|
||||
}
|
||||
return isMainWebTool(tool) || tool.name === 'generate_image'
|
||||
? dshCompatibleMainInputSchema(tool.inputSchema)
|
||||
: tool.inputSchema
|
||||
@@ -367,7 +375,7 @@ function boundedProxyToolCatalog(
|
||||
}> {
|
||||
const catalog = tools.filter(
|
||||
(tool) =>
|
||||
isMainWebTool(tool) ||
|
||||
isMainWebTool(tool) || isStoryGraphTool(tool.name) ||
|
||||
(workMode === 'execute' && (tool.source === 'mcp' || tool.name === 'generate_image'))
|
||||
)
|
||||
if (catalog.length > MAX_MCP_PROXY_TOOLS) {
|
||||
@@ -856,7 +864,7 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
|
||||
)
|
||||
const catalog = boundedProxyToolCatalog(tools, context.workMode)
|
||||
this.proxyToolCatalogs.set(params.sessionId, tools.filter(
|
||||
(tool) => tool.source === 'mcp' || isMainWebTool(tool) || tool.name === 'generate_image'
|
||||
(tool) => tool.source === 'mcp' || isMainWebTool(tool) || isStoryGraphTool(tool.name) || tool.name === 'generate_image'
|
||||
))
|
||||
return { tools: catalog }
|
||||
}
|
||||
@@ -897,7 +905,7 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
|
||||
const tool = tools.find(
|
||||
(candidate) =>
|
||||
candidate.name === name &&
|
||||
(candidate.source === 'mcp' || candidate.name === 'generate_image' ||
|
||||
(candidate.source === 'mcp' || candidate.name === 'generate_image' || isStoryGraphTool(candidate.name) ||
|
||||
isMainWebTool(candidate))
|
||||
)
|
||||
if (!tool) {
|
||||
@@ -907,7 +915,7 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
|
||||
}
|
||||
const isWebTool = isMainWebTool(tool)
|
||||
if (
|
||||
!isWebTool &&
|
||||
!isWebTool && !isStoryGraphTool(name) &&
|
||||
(context.workMode !== 'execute' || !run.authorize)
|
||||
) {
|
||||
throw new Error(
|
||||
@@ -937,7 +945,7 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
|
||||
.slice(0, 1_000)}`
|
||||
)
|
||||
}
|
||||
if (!isWebTool) {
|
||||
if (!isWebTool && !isStoryGraphTool(name)) {
|
||||
const approval =
|
||||
run.toolProvider.getApproval(
|
||||
tool,
|
||||
@@ -1758,3 +1766,4 @@ export class DeepSeekHarnessRuntime implements AgentRuntime {
|
||||
}
|
||||
}
|
||||
import { imageToolDescriptionLimit } from '../../shared/image-generation-contracts'
|
||||
import { isStoryGraphTool } from '../../shared/story-graph-tools'
|
||||
|
||||
@@ -922,7 +922,7 @@ export class GoodBuddyHarnessControlPlane {
|
||||
definition,
|
||||
dispose
|
||||
})
|
||||
if (MAIN_WEB_TOOL_NAMES.has(tool.name)) {
|
||||
if (MAIN_WEB_TOOL_NAMES.has(tool.name) || isStoryGraphTool(tool.name)) {
|
||||
record.askToolDefinitions.set(tool.name, definition)
|
||||
}
|
||||
}
|
||||
@@ -1454,3 +1454,4 @@ export class GoodBuddyHarnessControlPlane {
|
||||
}
|
||||
}
|
||||
import { imageToolDescriptionLimit } from '../../shared/image-generation-contracts'
|
||||
import { isStoryGraphTool } from '../../shared/story-graph-tools'
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import { isStoryGraphTool, storyGraphToolNames, type StoryGraphToolName } from '../../shared/story-graph-tools'
|
||||
import type { RuntimeTarget } from '../../shared/capability-contracts'
|
||||
|
||||
export type StoryGraphBinding = { projectId?: string; runtimeTarget: RuntimeTarget }
|
||||
export type StoryGraphRemoteBinding = {
|
||||
available(): Promise<boolean>
|
||||
call(name: StoryGraphToolName, input: unknown, signal: AbortSignal): Promise<Record<string, unknown>>
|
||||
}
|
||||
export type StoryGraphService = {
|
||||
available(binding: StoryGraphBinding): Promise<boolean>
|
||||
read(name: StoryGraphToolName, input: unknown, projectId?: string, signal?: AbortSignal): Record<string, unknown>
|
||||
}
|
||||
import { knowledgeReferenceKey, toKnowledgeReference } from '../../shared/knowledge-reference'
|
||||
import {
|
||||
createServer,
|
||||
@@ -191,6 +203,7 @@ export type KnowledgeLibraryListItem = {
|
||||
}
|
||||
|
||||
type Capability = {
|
||||
storyGraph?: StoryGraphBinding
|
||||
imageToolBinding?: ImageToolBinding
|
||||
requestId: string
|
||||
libraryIds: readonly string[]
|
||||
@@ -242,6 +255,7 @@ type DownstreamMcpSession = {
|
||||
}
|
||||
|
||||
export type KnowledgeMcpGatewayOptions = {
|
||||
storyGraphService?: StoryGraphService
|
||||
capabilityTtlMs?: number
|
||||
maximumBodyBytes?: number
|
||||
now?: () => number
|
||||
@@ -364,6 +378,7 @@ async function readBoundedJson(
|
||||
}
|
||||
|
||||
export class KnowledgeMcpGateway {
|
||||
private readonly storyGraphService?: StoryGraphService
|
||||
private readonly capabilities = new Map<string, Capability>()
|
||||
private readonly downstreamMcpSessions = new Map<
|
||||
string,
|
||||
@@ -407,6 +422,7 @@ export class KnowledgeMcpGateway {
|
||||
this.maximumBodyBytes =
|
||||
options.maximumBodyBytes ?? MAX_REQUEST_BODY_BYTES
|
||||
this.now = options.now ?? Date.now
|
||||
this.storyGraphService = options.storyGraphService
|
||||
this.magicNotesDatabase = options.magicNotesDatabase
|
||||
this.configService = options.configService
|
||||
this.obsidianService = options.obsidianService
|
||||
@@ -466,7 +482,8 @@ export class KnowledgeMcpGateway {
|
||||
browserConversationId?: string,
|
||||
browserTabId?: BrowserTabId,
|
||||
browserUsageLease?: BrowserTabUsageLease,
|
||||
obsidian?: { settings: ObsidianSettings; access: 'read' | 'write' }
|
||||
obsidian?: { settings: ObsidianSettings; access: 'read' | 'write' },
|
||||
storyGraph?: StoryGraphBinding
|
||||
): string | undefined {
|
||||
const effectiveMagicNotesAccess = this.magicNotesDatabase
|
||||
? magicNotesAccess
|
||||
@@ -516,7 +533,8 @@ export class KnowledgeMcpGateway {
|
||||
effectiveMagicNotesAccess === 'none' &&
|
||||
effectiveConfigAccess === 'none' &&
|
||||
!effectiveBrowserConversationId &&
|
||||
!effectiveObsidian
|
||||
!effectiveObsidian &&
|
||||
!(storyGraph && this.storyGraphService)
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
@@ -529,6 +547,7 @@ export class KnowledgeMcpGateway {
|
||||
magicNotesAccess: effectiveMagicNotesAccess,
|
||||
configAccess: effectiveConfigAccess,
|
||||
obsidian: effectiveObsidian,
|
||||
storyGraph: this.storyGraphService ? storyGraph : undefined,
|
||||
browserConversationId: effectiveBrowserConversationId,
|
||||
browserTabId: effectiveBrowserTabId,
|
||||
browserUsageLease: effectiveBrowserUsageLease,
|
||||
@@ -799,6 +818,7 @@ export class KnowledgeMcpGateway {
|
||||
getAvailableToolNames(token: string): GoodBuddyBuiltinToolName[] {
|
||||
const capability = this.getCapability(token)
|
||||
return [
|
||||
...(capability.storyGraph ? storyGraphToolNames : []),
|
||||
...(capability.imageToolBinding ? ['generate_image' as const] : []),
|
||||
...(capability.libraryIds.length > 0
|
||||
? knowledgeToolNames
|
||||
@@ -1544,6 +1564,29 @@ export class KnowledgeMcpGateway {
|
||||
return { deleted: true, noteId: parsed.noteId }
|
||||
}
|
||||
|
||||
async isStoryGraphAvailable(token: string): Promise<boolean> {
|
||||
const binding = this.getCapability(token).storyGraph
|
||||
return Boolean(binding && this.storyGraphService && await this.storyGraphService.available(binding))
|
||||
}
|
||||
|
||||
bindRemoteStoryGraph(token: string): StoryGraphRemoteBinding | undefined {
|
||||
if (!this.getCapability(token).storyGraph) return undefined
|
||||
return { available: () => this.isStoryGraphAvailable(token), call: (name, input, signal) => this.callStoryGraphTool(token, name, input, signal) }
|
||||
}
|
||||
|
||||
async callStoryGraphTool(token: string, name: StoryGraphToolName, input: unknown, signal?: AbortSignal): Promise<Record<string, unknown>> {
|
||||
const capability = this.getCapability(token)
|
||||
const effectiveSignal = AbortSignal.any([capability.signal, capability.brokerController.signal, ...(signal ? [signal] : [])])
|
||||
effectiveSignal.throwIfAborted()
|
||||
if (!await this.isStoryGraphAvailable(token)) throw new Error('story_graph_unavailable: Supervisor or runtime capability is disabled')
|
||||
effectiveSignal.throwIfAborted()
|
||||
const result = this.storyGraphService!.read(name, input, capability.storyGraph!.projectId, effectiveSignal)
|
||||
effectiveSignal.throwIfAborted()
|
||||
if (!await this.isStoryGraphAvailable(token)) throw new Error('story_graph_unavailable: Supervisor or runtime capability is disabled')
|
||||
effectiveSignal.throwIfAborted()
|
||||
return result
|
||||
}
|
||||
|
||||
private async callScopedTool(
|
||||
token: string,
|
||||
name: ScopedDataToolName,
|
||||
@@ -1551,6 +1594,10 @@ export class KnowledgeMcpGateway {
|
||||
signal?: AbortSignal
|
||||
): Promise<Record<string, unknown>> {
|
||||
switch (name) {
|
||||
case 'story_graph_search':
|
||||
case 'story_graph_get_context':
|
||||
case 'story_graph_read_source':
|
||||
return this.callStoryGraphTool(token, name, input, signal)
|
||||
case 'knowledge_list':
|
||||
return { libraries: this.listLibraries(token, input) }
|
||||
case 'knowledge_search':
|
||||
@@ -1651,7 +1698,8 @@ export class KnowledgeMcpGateway {
|
||||
token,
|
||||
extra.signal
|
||||
)
|
||||
const scopedTools = [...availableTools].flatMap(
|
||||
const storyGraphAvailable = await this.isStoryGraphAvailable(token)
|
||||
const scopedTools = [...availableTools].filter(name => !name.startsWith('story_graph_') || storyGraphAvailable).flatMap(
|
||||
(name): Tool[] => {
|
||||
const definition = scopedDataToolByName.get(
|
||||
name as ScopedDataToolName
|
||||
@@ -1724,6 +1772,10 @@ export class KnowledgeMcpGateway {
|
||||
async (call, extra) => {
|
||||
const name = call.params.name
|
||||
const input = call.params.arguments ?? {}
|
||||
if (isStoryGraphTool(name)) {
|
||||
try { return { content: [{ type: 'text' as const, text: JSON.stringify(await this.callStoryGraphTool(token, name, input, extra.signal)) }] } }
|
||||
catch (error) { return { isError: true, content: [{ type: 'text' as const, text: error instanceof Error ? error.message : 'Story Graph read failed' }] } }
|
||||
}
|
||||
if (name === 'generate_image') {
|
||||
const capability = this.getCapability(token)
|
||||
if (!capability.imageToolBinding) throw new Error('Image capability is unavailable')
|
||||
|
||||
@@ -1015,6 +1015,7 @@ describe('ModelToolProvider', () => {
|
||||
listLibraries: vi.fn(() => []),
|
||||
search: vi.fn(async () => []),
|
||||
searchMagicNotes: vi.fn(() => []),
|
||||
isStoryGraphAvailable: vi.fn(async () => true),
|
||||
getAvailableToolNames: vi.fn(() => [...scopedDataToolByName.keys()])
|
||||
} as unknown as KnowledgeMcpGateway
|
||||
const context = {
|
||||
@@ -1072,6 +1073,7 @@ describe('ModelToolProvider', () => {
|
||||
|
||||
it('rechecks cached and refreshed MCP catalogs against each request scope without shrinking the cache', async () => {
|
||||
const gateway = {
|
||||
isStoryGraphAvailable: vi.fn(async () => true),
|
||||
getAvailableToolNames: vi.fn(() => [...scopedDataToolByName.keys()])
|
||||
} as unknown as KnowledgeMcpGateway
|
||||
const provider = new ModelToolProvider(await createWorkspace(), [createMcpServer(true)],
|
||||
@@ -1117,6 +1119,7 @@ describe('ModelToolProvider', () => {
|
||||
|
||||
it('enforces the aggregate MCP limit when a cached catalog gains scoped tools', async () => {
|
||||
const gateway = {
|
||||
isStoryGraphAvailable: vi.fn(async () => true),
|
||||
getAvailableToolNames: vi.fn(() => [...scopedDataToolByName.keys()])
|
||||
} as unknown as KnowledgeMcpGateway
|
||||
mocks.client.listTools.mockResolvedValue({ tools: Array.from({ length: 40 }, (_, index) => ({
|
||||
|
||||
@@ -1252,7 +1252,10 @@ export class ModelToolProvider implements ModelToolProviderLike {
|
||||
signal: AbortSignal
|
||||
): Promise<ModelToolDefinition[]> {
|
||||
signal.throwIfAborted()
|
||||
const scopedTools = this.getScopedTools(context)
|
||||
const grantedTools = this.getScopedTools(context)
|
||||
const storyGraphAvailable = grantedTools.some(tool => tool.name.startsWith('story_graph_')) && context.knowledgeCapabilityToken && this.knowledgeGateway
|
||||
? await this.knowledgeGateway.isStoryGraphAvailable(context.knowledgeCapabilityToken) : false
|
||||
const scopedTools = grantedTools.filter(tool => !tool.name.startsWith('story_graph_') || storyGraphAvailable)
|
||||
const imageTool = context.workMode === 'execute' ? await imageToolDefinition(context.imageToolBinding) : undefined
|
||||
const webTools = this.webSearchEnabled
|
||||
? this.getWebSearchDefinitions()
|
||||
@@ -1406,6 +1409,12 @@ export class ModelToolProvider implements ModelToolProviderLike {
|
||||
): Promise<ModelToolResult> {
|
||||
signal.throwIfAborted()
|
||||
assertToolAuthorizedForWorkMode(name, context)
|
||||
if (name.startsWith('story_graph_') && scopedDataToolByName.has(name as ScopedDataToolName)) {
|
||||
if (!this.knowledgeGateway || !context.knowledgeCapabilityToken) throw new Error('Story Graph capability is unavailable')
|
||||
return createTextToolResult(JSON.stringify(await this.knowledgeGateway.callStoryGraphTool(
|
||||
context.knowledgeCapabilityToken, name as import('../../shared/story-graph-tools').StoryGraphToolName, argumentsValue, signal
|
||||
)))
|
||||
}
|
||||
if (name.startsWith('obsidian_') && scopedDataToolByName.has(name as ScopedDataToolName)) {
|
||||
if (!this.knowledgeGateway || !context.knowledgeCapabilityToken) {
|
||||
throw new Error('Obsidian 工具授权不可用')
|
||||
|
||||
@@ -101,7 +101,7 @@ export class NativeClientCoordinator {
|
||||
target: selected.target, profile: selected.target === 'deepseek-harness' ? selected.settings.deepseekHarnessModelProfile
|
||||
: selected.target === 'continue' ? selected.settings.continueModelProfile : selected.settings.opencodeModelProfile,
|
||||
workMode, skills, mcpServers, builtin,
|
||||
libraries: conversation.knowledgeLibraryIds, magicNotes: application.magicNotesEnabled, obsidian,
|
||||
libraries: conversation.knowledgeLibraryIds, magicNotes: application.magicNotesEnabled, supervisor: application.heartbeatEnabled, obsidian,
|
||||
node: application.localToolEnvironment.node,
|
||||
binary: selected.target === 'continue' ? selected.settings.continueBinaryPath
|
||||
: selected.target === 'opencode' ? selected.settings.opencodeBinaryPath : undefined,
|
||||
@@ -162,9 +162,11 @@ export class NativeClientCoordinator {
|
||||
const token = gateway.grant(requestId, builtin.includes('knowledge-base') ? conversation.knowledgeLibraryIds ?? [] : [], signal,
|
||||
builtin.includes('magic-notes') && application.magicNotesEnabled ? access : 'none',
|
||||
builtin.includes('goodbuddy-config') ? { access, workspacePath: space.rootPath } : undefined,
|
||||
undefined, undefined, undefined, builtin.includes('obsidian') ? { settings: obsidian, access } : undefined)
|
||||
undefined, undefined, undefined, builtin.includes('obsidian') ? { settings: obsidian, access } : undefined,
|
||||
builtin.includes('story-graph') && application.heartbeatEnabled ? { projectId: project.id, runtimeTarget: selected.target } : undefined)
|
||||
const customToken = workMode === 'execute' ? gateway.grantCustomMcp(requestId, mcpServers, signal) : undefined
|
||||
const endpoints = [token, customToken].flatMap((value, index) => value ? [{ name: `goodbuddy-${index}`, url: gateway.getEndpoint()!, headers: { Authorization: `Bearer ${value}` } }] : [])
|
||||
const endpoints = [token, customToken].flatMap((value, index) => value ? [{ name: `goodbuddy-${index}`, url: gateway.getEndpoint()!, headers: { Authorization: `Bearer ${value}` },
|
||||
readOnlyTools: index === 0 && workMode === 'ask' ? gateway.getAvailableToolNames(value) : [] }] : [])
|
||||
if (selected.target === 'deepseek-harness') {
|
||||
const profile = selected.settings.deepseekHarnessModelProfile
|
||||
if (!profile) throw new Error('DS Web requires a text model connection')
|
||||
|
||||
@@ -91,4 +91,18 @@ describe('native terminal client', () => {
|
||||
expect(config.mcp.tools.url).toBe('http://127.0.0.1:12345/mcp')
|
||||
expect(await readFile(new URL(config.plugin[0]), 'utf8')).toContain('execute')
|
||||
})
|
||||
|
||||
it('allows only the bound read tools through native OpenCode Ask permissions and execution hook', async () => {
|
||||
const { client, input } = await fixture()
|
||||
await client.open(1, { ...input, mcpServers: [{ name: 'goodbuddy-0', url: 'http://127.0.0.1:12345/mcp', headers: {},
|
||||
readOnlyTools: ['story_graph_search', 'story_graph_get_context', 'story_graph_read_source'] }] })
|
||||
const config = JSON.parse(launches[0]!.spawnSpec.env.OPENCODE_CONFIG_CONTENT!)
|
||||
expect(config.mcp['goodbuddy-0']).toBeDefined()
|
||||
const hooks = await (await import(/* @vite-ignore */ new URL(config.plugin[0]).href)).default()
|
||||
for (const name of ['story_graph_search', 'story_graph_get_context', 'story_graph_read_source']) {
|
||||
expect(config.permission[`goodbuddy-0_${name}`]).toBe('allow')
|
||||
await expect(hooks['tool.execute.before']({ tool: `goodbuddy-0_${name}` })).resolves.toBeUndefined()
|
||||
}
|
||||
await expect(hooks['tool.execute.before']({ tool: 'goodbuddy-0_generate_image' })).rejects.toThrow('read-only')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -32,7 +32,7 @@ export type NativeTerminalClientInput = {
|
||||
size?: TerminalSize
|
||||
skillPackages?: RuntimeSkillPackage[]
|
||||
/** Session-lived, Main-owned MCP endpoints, such as the existing knowledge gateway. */
|
||||
mcpServers?: Array<{ name: string; url: string; headers: Record<string, string> }>
|
||||
mcpServers?: Array<{ name: string; url: string; headers: Record<string, string>; readOnlyTools?: readonly string[] }>
|
||||
}
|
||||
|
||||
export type NativeTerminalClientOptions = {
|
||||
@@ -141,7 +141,9 @@ export class NativeTerminalClient {
|
||||
await mkdir(history, { recursive: true })
|
||||
const origin = await proxy.listen()
|
||||
const skills = input.skillPackages ?? []
|
||||
const mcp = input.workMode === 'execute' ? input.mcpServers ?? [] : []
|
||||
const mcp = input.mcpServers ?? []
|
||||
const allowedContinueTools = [...continueReadTools, ...mcp.flatMap(server => server.readOnlyTools ?? [])]
|
||||
const allowedOpenCodeTools = [...openCodeReadTools, 'skill', ...mcp.flatMap(server => (server.readOnlyTools ?? []).map(name => `${server.name}_${name}`))]
|
||||
if (input.runtime === 'continue') {
|
||||
// Reuse the pinned adapter's protocol, permission-order and Windows fixes.
|
||||
const adapter = new ContinueHostAdapter({ binaryPath: detection.path,
|
||||
@@ -151,7 +153,7 @@ export class NativeTerminalClient {
|
||||
const bundle = await readFile(bundlePath, 'utf8')
|
||||
const executionMarker = 'async function hti(e,t={parallelToolCallCount:1}){'
|
||||
if (bundle.split(executionMarker).length !== 2) throw new Error('Continue native tool execution entry point is incompatible')
|
||||
await writeFile(bundlePath, bundle.replace(executionMarker, `${executionMarker}if(${JSON.stringify(input.workMode)}==="ask"&&!${JSON.stringify(continueReadTools)}.includes(e.name))throw new Error("GoodBuddy Ask mode is read-only");`))
|
||||
await writeFile(bundlePath, bundle.replace(executionMarker, `${executionMarker}if(${JSON.stringify(input.workMode)}==="ask"&&!${JSON.stringify(allowedContinueTools)}.includes(e.name))throw new Error("GoodBuddy Ask mode is read-only");`))
|
||||
args[0] = prepared.entryPath
|
||||
const configured = input.settings.continueConfigPath.trim()
|
||||
? await loadContinueConfig(input.settings.continueConfigPath.trim()) : {}
|
||||
@@ -172,7 +174,7 @@ export class NativeTerminalClient {
|
||||
Object.assign(env, { CONTINUE_GLOBAL_DIR: history, GOODBUDDY_DISABLE_CONTINUE_UPDATES: '1', CONTINUE_CLI_AUTO_UPDATED: '1', CONTINUE_CLI_ENABLE_TELEMETRY: '0', CONTINUE_METRICS_ENABLED: '0', CONTINUE_CLI_DISABLE_COMMIT_SIGNATURE: '1' })
|
||||
args.push('--config', configPath)
|
||||
if (input.workMode === 'execute') args.push('--auto')
|
||||
else args.push('--readonly', ...continueReadTools.flatMap(name => ['--allow', name]), '--exclude', '*')
|
||||
else args.push('--readonly', ...allowedContinueTools.flatMap(name => ['--allow', name]), '--exclude', '*')
|
||||
} else {
|
||||
const configDirectory = join(temporary, 'config')
|
||||
if (this.options.bundledRuntimePaths.opencodeConfig) {
|
||||
@@ -184,11 +186,11 @@ export class NativeTerminalClient {
|
||||
loopbackOrigin: origin, supportsImageInput: selected.supportsImageInput, workMode: input.workMode
|
||||
})
|
||||
const permission = input.workMode === 'execute' ? 'allow' : Object.fromEntries([
|
||||
['*', 'deny'], ...openCodeReadTools.map(name => [name, 'allow']), ['skill', 'allow']
|
||||
['*', 'deny'], ...allowedOpenCodeTools.map(name => [name, 'allow'])
|
||||
])
|
||||
// A tool hook keeps the launch mode fixed even when the TUI changes agents.
|
||||
const pluginPath = join(temporary, 'work-mode.mjs')
|
||||
await writeFile(pluginPath, `export default async () => ({'tool.execute.before': async ({tool}) => { if (${JSON.stringify(input.workMode)} === 'ask' && !${JSON.stringify([...openCodeReadTools, 'skill'])}.includes(tool)) throw new Error('GoodBuddy Ask mode is read-only'); }});`, { mode: 0o600 })
|
||||
await writeFile(pluginPath, `export default async () => ({'tool.execute.before': async ({tool}) => { if (${JSON.stringify(input.workMode)} === 'ask' && !${JSON.stringify(allowedOpenCodeTools)}.includes(tool)) throw new Error('GoodBuddy Ask mode is read-only'); }});`, { mode: 0o600 })
|
||||
Object.assign(env, {
|
||||
OPENCODE_CONFIG_DIR: configDirectory,
|
||||
OPENCODE_CONFIG_CONTENT: JSON.stringify({ ...config, permission,
|
||||
|
||||
@@ -114,7 +114,8 @@ export interface RemoteRuntimeChannel {
|
||||
preparePrompt(
|
||||
preparation: RemotePromptOperationPreparation,
|
||||
imageToolBinding?: ImageToolBinding,
|
||||
waitSignal?: AbortSignal
|
||||
waitSignal?: AbortSignal,
|
||||
storyGraphBinding?: import('./knowledge-mcp-gateway').StoryGraphRemoteBinding
|
||||
): Promise<RemotePromptOperationAcceptance>
|
||||
/** Starts an Agent-owned ACP prompt exactly once for its stable operation. */
|
||||
startOwnedPrompt?(
|
||||
|
||||
@@ -146,6 +146,7 @@ export type AgentExecutionRequest = Omit<AgentRequest, 'workMode'> & {
|
||||
trustedInstructions?: string
|
||||
/** Main-process-only request-scoped authorization for built-in data tools. */
|
||||
knowledgeCapabilityToken?: string
|
||||
storyGraphBinding?: import('./knowledge-mcp-gateway').StoryGraphRemoteBinding
|
||||
imageToolBinding?: ImageToolBinding
|
||||
/** Main-process-only browser tab fixed for this request. */
|
||||
browserTabId?: BrowserTabId
|
||||
|
||||
@@ -8742,6 +8742,10 @@ export class AssistantDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
readStoryGraph(name: import('../../shared/story-graph-tools').StoryGraphToolName, input: unknown, projectId?: string, signal?: AbortSignal): Record<string, unknown> {
|
||||
return readStoryGraph(this.requireDatabase(), name, input, projectId, signal)
|
||||
}
|
||||
|
||||
getSupervisionResult(id: string): { id: string; summary: string } | undefined {
|
||||
return this.requireDatabase().prepare('SELECT id, summary FROM supervision_results WHERE id = ?').get(id) as { id: string; summary: string } | undefined
|
||||
}
|
||||
@@ -11488,3 +11492,4 @@ export class AssistantDatabase {
|
||||
}
|
||||
import type { ImageOperation } from '../../shared/image-generation-contracts'
|
||||
import { SupervisionReviewStore, supervisionReviewMigration } from './supervision-review-store'
|
||||
import { readStoryGraph } from './story-graph-reader'
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
// @vitest-environment node
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
import { afterEach, expect, it } from 'vitest'
|
||||
import { AssistantDatabase } from './assistant-database'
|
||||
import type { StoredSupervisionResult } from './supervisor-service'
|
||||
import type { StoryGraphToolName } from '../../shared/story-graph-tools'
|
||||
import { readStoryGraph } from './story-graph-reader'
|
||||
|
||||
const cleanups: Array<() => void | Promise<void>> = []
|
||||
afterEach(async () => { for (const cleanup of cleanups.splice(0).reverse()) await cleanup() })
|
||||
type Page = { items: Array<{ object_ref: { type: string; id: string }; result_id: string | null; event_time: string | null;
|
||||
content: string; source_reference_ids: string[]; validity: string }>; page: { next_cursor: string | null; has_more: boolean; total_count: number } }
|
||||
|
||||
async function fixture() {
|
||||
const root = await mkdtemp(join(tmpdir(), 'story-graph-'))
|
||||
cleanups.push(() => rm(root, { recursive: true, force: true }))
|
||||
const path = join(root, 'assistant.sqlite')
|
||||
const db = new AssistantDatabase(path)
|
||||
db.initialize(root)
|
||||
cleanups.push(() => db.close())
|
||||
const sql = new DatabaseSync(path)
|
||||
cleanups.push(() => sql.close())
|
||||
const project = db.listProjects()[0]!
|
||||
const other = db.createProject({ name: 'Other', description: '', rootPath: root, defaultWorkMode: 'ask' })
|
||||
const messageId = randomUUID(), conversationId = randomUUID()
|
||||
const body = 'Choose A. Then explicitly revise A to B because of measured latency. Option C is only a proposal. '
|
||||
const update = (content: string, projectId = project.id) => db.saveLocalConversations([{ header: { id: conversationId, projectId, title: 'Decision', updatedAt: 1 },
|
||||
messages: [{ id: messageId, role: 'user', content, createdAt: Date.parse('2026-09-01T00:00:00Z'), state: 'complete' }] }])
|
||||
update(body)
|
||||
const request: StoredSupervisionResult['request'] = { trigger: 'manual', scope: { kind: 'projects', projectIds: [project.id] },
|
||||
timeRange: { from: '2026-09-01T00:00:00Z', to: '2026-09-30T00:00:00Z' } }
|
||||
const save = (description: string, occurredAt: string, persistedId?: string) => {
|
||||
const result: StoredSupervisionResult = { request, candidates: db.listSupervisionCandidates(request), evidence: [{ id: 'quote',
|
||||
sourceType: 'conversation', sourceId: conversationId, title: 'Decision evidence', content: body,
|
||||
occurredAt: '2026-09-01T00:00:00Z', locator: { source: `message:${messageId}`, projectId: project.id, conversationId, messageId, start: 0, end: Array.from(body).length, length: Array.from(body).length } }],
|
||||
output: { summary: description, changeDigest: '', openItems: ['Execution still pending'],
|
||||
entities: [{ id: 'choice', label: 'Decision', description, sourceReferenceIds: ['quote'], ...(persistedId ? { persistedId } : {}) }],
|
||||
events: [{ title: description, description, occurredAt, eventType: 'decision', entityIds: ['choice'], sourceReferenceIds: ['quote'] }],
|
||||
entityChanges: [{ entityId: 'choice', changeType: persistedId ? 'revised' : 'proposed', description, sourceReferenceIds: ['quote'] }], relations: [] } }
|
||||
db.saveSupervisionResult(result)
|
||||
return db.listSupervisionCandidates(request)[0]!.id
|
||||
}
|
||||
const read = (name: StoryGraphToolName, input: Record<string, unknown>) => readStoryGraph(sql, name, input, project.id)
|
||||
return { db, sql, read, save, project, other, request, update, body }
|
||||
}
|
||||
|
||||
it('retains A, explicit revision B and proposal C without latest-wins, with event-time order and complete nested pagination', async () => {
|
||||
const f = await fixture()
|
||||
const id = f.save('Decision A', '2026-09-01T00:00:00Z')
|
||||
f.save('Decision B explicitly revises A; execution pending', '2026-09-03T00:00:00Z', id)
|
||||
f.save('Decision C is an option, not a replacement', '2026-09-04T00:00:00Z', id)
|
||||
f.save('Decision late-recorded discussion', '2026-09-02T09:00:00+09:00', id)
|
||||
const input = { object_ref: { type: 'entity', id }, mode: 'timeline', page_size: 2, content_page_size: 71 }
|
||||
let cursor: string | null = null
|
||||
const fragments = new Map<string, string>()
|
||||
const eventTimes = new Map<string, string>()
|
||||
do {
|
||||
const result = f.read('story_graph_get_context', { ...input, ...(cursor ? { cursor } : {}) }) as Page
|
||||
for (const item of result.items) {
|
||||
const key = `${item.object_ref.type}:${item.object_ref.id}:${item.result_id}`
|
||||
fragments.set(key, (fragments.get(key) ?? '') + item.content)
|
||||
if (item.event_time) eventTimes.set(item.object_ref.id, item.event_time)
|
||||
expect(item.validity).toBe('unknown')
|
||||
}
|
||||
cursor = result.page.next_cursor
|
||||
} while (cursor)
|
||||
const data = [...fragments.values()].map(text => JSON.parse(text))
|
||||
expect(data.filter(item => item.canonical_label).map(item => item.description)).toEqual(expect.arrayContaining([
|
||||
'Decision A', 'Decision B explicitly revises A; execution pending', 'Decision C is an option, not a replacement'
|
||||
]))
|
||||
expect([...eventTimes.values()]).toEqual(['2026-09-01T00:00:00.000Z', '2026-09-02T00:00:00.000Z', '2026-09-03T00:00:00.000Z', '2026-09-04T00:00:00.000Z'])
|
||||
expect(data.some(item => item.content?.includes('measured latency'))).toBe(true)
|
||||
const search = f.read('story_graph_search', { query: 'Decision', object_types: ['event'], time_range: { from: '2026-09-02T09:00:00+09:00', to: '2026-09-02T00:00:00Z' } }) as Page
|
||||
expect(search.items).toHaveLength(1)
|
||||
expect(search.items[0]!.event_time).toBe('2026-09-02T00:00:00.000Z')
|
||||
})
|
||||
|
||||
it('enforces exact scope, rejects as_of and stale/changed cursors, and never writes during reads', async () => {
|
||||
const f = await fixture()
|
||||
const id = f.save('Decision A', '2026-09-01T00:00:00Z')
|
||||
const input = { query: 'Decision', page_size: 1 }
|
||||
const first = f.read('story_graph_search', input) as Page
|
||||
const before = f.sql.prepare('SELECT total_changes() AS n').get()!.n
|
||||
f.sql.exec('PRAGMA query_only = ON')
|
||||
expect(f.db.readStoryGraph('story_graph_search', input, f.other.id)).toMatchObject({ items: [], coverage: { status: 'unknown' } })
|
||||
expect(f.read('story_graph_search', { ...input, scope: { kind: 'global' } })).toMatchObject({ items: [] })
|
||||
expect(() => f.db.readStoryGraph('story_graph_get_context', { object_ref: { type: 'entity', id } }, f.other.id)).toThrow('scope_mismatch')
|
||||
expect(() => f.read('story_graph_get_context', { object_ref: { type: 'entity', id: 'missing' } })).toThrow('object_not_found')
|
||||
expect(() => f.db.readStoryGraph('story_graph_search', input)).toThrow('scope_required')
|
||||
expect(() => f.read('story_graph_search', { ...input, as_of: '2026-09-01' })).toThrow('unsupported_mode')
|
||||
expect(() => f.read('story_graph_get_context', { object_ref: { type: 'entity', id }, mode: 'as_of' })).toThrow('unsupported_mode')
|
||||
expect(() => f.read('story_graph_search', { ...input, query: 'Other', cursor: first.page.next_cursor })).toThrow('invalid_cursor')
|
||||
expect(f.sql.prepare('SELECT total_changes() AS n').get()!.n).toBe(before)
|
||||
f.sql.exec('PRAGMA query_only = OFF')
|
||||
f.db.applySupervisionEntityAction({ entityId: id, action: 'revise', description: 'Human confirmed preference' })
|
||||
expect(() => f.read('story_graph_search', { ...input, cursor: first.page.next_cursor })).toThrow('stale_cursor')
|
||||
const abort = new AbortController(); abort.abort()
|
||||
expect(() => f.db.readStoryGraph('story_graph_search', input, f.project.id, abort.signal)).toThrow()
|
||||
})
|
||||
|
||||
it('pages Unicode snapshots, reads explicit current versions, and reports deletion and moved scope without substituting history', async () => {
|
||||
const f = await fixture()
|
||||
f.save('Decision A', '2026-09-01T00:00:00Z')
|
||||
const source = f.sql.prepare('SELECT id FROM supervision_sources').get()!
|
||||
f.sql.prepare('UPDATE supervision_sources SET content = ? WHERE id = ?').run('A\u{1f600}B\u{1f601}C', source.id!)
|
||||
const input = { source_reference_id: source.id, page_size: 2 }
|
||||
let cursor: string | null = null, text = ''
|
||||
do {
|
||||
const page = f.read('story_graph_read_source', { ...input, ...(cursor ? { cursor } : {}) }) as { content: string; page: { next_cursor: string | null } }
|
||||
text += page.content; cursor = page.page.next_cursor
|
||||
} while (cursor)
|
||||
expect(text).toBe('A\u{1f600}B\u{1f601}C')
|
||||
const current = f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'current' })
|
||||
expect(current.content).toBe(f.body)
|
||||
// Released incremental references can have messageId without the newer source key.
|
||||
f.sql.prepare("UPDATE supervision_sources SET locator_json = json_remove(locator_json, '$.source') WHERE id = ?").run(source.id!)
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'current' })).toMatchObject({ content: f.body })
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'version', source_version: current.source_version })).toMatchObject({ content: f.body, content_kind: 'version' })
|
||||
f.update('Changed original')
|
||||
const currentPage = f.read('story_graph_read_source', { ...input, content_kind: 'current' }) as { page: { next_cursor: string } }
|
||||
f.update('Changed again')
|
||||
expect(() => f.read('story_graph_read_source', { ...input, content_kind: 'current', cursor: currentPage.page.next_cursor })).toThrow('stale_cursor')
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'version', source_version: current.source_version })).toMatchObject({ error: 'historical_version_unavailable' })
|
||||
f.update('Moved original', f.other.id)
|
||||
expect(() => f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'current' })).toThrow('scope_mismatch')
|
||||
expect(() => f.db.readStoryGraph('story_graph_read_source', { source_reference_id: source.id }, f.other.id)).toThrow('scope_mismatch')
|
||||
f.sql.exec('DELETE FROM messages')
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id, content_kind: 'current' })).toMatchObject({ error: 'current_source_unavailable' })
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id })).toMatchObject({ content: text, content_kind: 'snapshot' })
|
||||
f.sql.prepare('UPDATE supervision_sources SET source_type = ?, locator_json = ? WHERE id = ?').run('knowledge', JSON.stringify({ length: 999 }), source.id!)
|
||||
expect(f.read('story_graph_read_source', { source_reference_id: source.id })).toMatchObject({ original_source_length: 'unknown', reviewed_source_length: 999 })
|
||||
f.sql.prepare('UPDATE supervision_sources SET locator_json = ? WHERE id = ?').run(JSON.stringify({ detail: 'x'.repeat(100_001) }), source.id!)
|
||||
expect(() => f.read('story_graph_read_source', { source_reference_id: source.id })).toThrow('response_too_large')
|
||||
})
|
||||
@@ -0,0 +1,236 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import type { DatabaseSync } from 'node:sqlite'
|
||||
import { z } from 'zod'
|
||||
import type { HeartbeatScope } from '../../shared/assistant-contracts'
|
||||
import { storyGraphContextSchema, storyGraphSearchSchema, storyGraphSourceSchema, type StoryGraphToolName } from '../../shared/story-graph-tools'
|
||||
import type { SupervisionEvidence, SupervisionSummaryOutput } from '../../shared/supervision-contracts'
|
||||
|
||||
type Row = Record<string, unknown>
|
||||
type Fact = {
|
||||
object_ref: { type: string; id: string }
|
||||
object_revision: string
|
||||
result_id: string | null
|
||||
scope: HeartbeatScope
|
||||
event_time: string | null
|
||||
review_generated_at: string | null
|
||||
recorded_at: string | null
|
||||
confirmation_state: string
|
||||
validity: 'unknown'
|
||||
source_reference_ids: string[]
|
||||
data: Row
|
||||
}
|
||||
const digest = (value: unknown): string => createHash('sha256').update(JSON.stringify(value)).digest('hex')
|
||||
const cursorSchema = z.object({ key: z.string(), revision: z.string(), index: z.number().int().nonnegative(), offset: z.number().int().nonnegative() }).strict()
|
||||
const normalizeScope = (scope: HeartbeatScope): HeartbeatScope => scope.kind === 'global' ? scope : { kind: 'projects', projectIds: [...new Set(scope.projectIds)].sort() }
|
||||
const timestamp = (value: unknown): string | null => typeof value === 'string' && Number.isFinite(Date.parse(value)) ? new Date(value).toISOString() : null
|
||||
const bounded = (output: Row): Row => {
|
||||
if (Buffer.byteLength(JSON.stringify(output)) > 100_000) throw new Error('response_too_large: retry with a smaller page_size')
|
||||
return output
|
||||
}
|
||||
|
||||
// This is a projection of existing storage, not a second graph or a historical state store.
|
||||
export function readStoryGraph(db: DatabaseSync, name: StoryGraphToolName, input: unknown, projectId?: string, signal?: AbortSignal): Record<string, unknown> {
|
||||
signal?.throwIfAborted()
|
||||
if (input && typeof input === 'object' && ('as_of' in input || ('mode' in input && input.mode === 'as_of'))) throw new Error('unsupported_mode: as_of is not supported')
|
||||
const args = name === 'story_graph_search' ? storyGraphSearchSchema.parse(input)
|
||||
: name === 'story_graph_get_context' ? storyGraphContextSchema.parse(input) : storyGraphSourceSchema.parse(input)
|
||||
const requested = args.scope ?? (projectId ? { kind: 'projects' as const, projectIds: [projectId] } : undefined)
|
||||
if (!requested) throw new Error('scope_required: no current project; specify scope explicitly')
|
||||
const scope = normalizeScope(requested)
|
||||
if (scope.kind === 'projects') for (const id of scope.projectIds) {
|
||||
if (!db.prepare("SELECT id FROM projects WHERE id = ? AND status = 'active'").get(id)) throw new Error('scope_unavailable: project does not exist or is inactive')
|
||||
}
|
||||
const base = { requested_scope: args.scope ?? 'current_project', resolved_scope: scope, read_at: new Date().toISOString(),
|
||||
coverage: { status: 'unknown', semantic_completeness: 'unknown', unprocessed_sources: 'unknown', historical_reconstruction: false,
|
||||
note: 'Exact stored scope only. Saved results and fragments do not establish complete source or semantic coverage.' } }
|
||||
const key = digest({ name, ...args, scope, cursor: undefined })
|
||||
let cursor: z.infer<typeof cursorSchema> | undefined
|
||||
if (args.cursor) {
|
||||
try { cursor = cursorSchema.parse(JSON.parse(Buffer.from(args.cursor, 'base64url').toString('utf8'))) }
|
||||
catch { throw new Error('invalid_cursor') }
|
||||
if (cursor.key !== key) throw new Error('invalid_cursor: repeat the original filters and page sizes')
|
||||
}
|
||||
const page = (revision: string, index: number, offset: number, more: boolean, count: number, total: number) => ({
|
||||
returned_count: count, total_count: total, has_more: more, complete: !more,
|
||||
next_cursor: more ? Buffer.from(JSON.stringify({ key, revision, index, offset })).toString('base64url') : null,
|
||||
limit_reason: more ? 'page_size' : null
|
||||
})
|
||||
const checkRevision = (revision: string): void => { if (cursor && cursor.revision !== revision) throw new Error('stale_cursor: stored evidence changed; restart the query') }
|
||||
const sameScope = (value: unknown): boolean => JSON.stringify(normalizeScope(JSON.parse(String(value)))) === JSON.stringify(scope)
|
||||
if (name === 'story_graph_read_source') {
|
||||
const sourceArgs = storyGraphSourceSchema.parse(input)
|
||||
const source = db.prepare(`SELECT s.*, sr.scope_json, r.created_at AS generated_at FROM supervision_sources s
|
||||
JOIN supervision_results r ON r.id = s.result_id JOIN supervision_runs sr ON sr.id = r.run_id WHERE s.id = ?`).get(sourceArgs.source_reference_id)
|
||||
if (!source) throw new Error('source_not_found')
|
||||
if (!sameScope(source.scope_json)) throw new Error('scope_mismatch')
|
||||
const locator = source.locator_json ? JSON.parse(String(source.locator_json)) as Row : {}
|
||||
const version = typeof locator.revision === 'string' ? locator.revision : 'unknown'
|
||||
if (sourceArgs.content_kind === 'version' && !sourceArgs.source_version) throw new Error('source_version_required')
|
||||
let body = String(source.content)
|
||||
let returnedVersion = version
|
||||
let location = locator
|
||||
let originalLength: unknown = source.source_type === 'knowledge' ? 'unknown' : locator.length ?? 'unknown'
|
||||
if (sourceArgs.content_kind !== 'snapshot') {
|
||||
// Resolve only an exact saved locator. Never replace a missing historical original with a snapshot.
|
||||
const sourceKey = typeof locator.source === 'string' ? locator.source
|
||||
: source.source_type === 'conversation' && typeof locator.messageId === 'string' ? `message:${locator.messageId}`
|
||||
: source.source_type === 'task' ? `task:${source.source_id}` : ''
|
||||
const kind = sourceKey.slice(0, sourceKey.indexOf(':'))
|
||||
const key = sourceKey.slice(sourceKey.indexOf(':') + 1)
|
||||
const separator = key.lastIndexOf(':')
|
||||
const current = sourceKey ? db.prepare(`SELECT body, locator, project_id, context
|
||||
FROM supervision_review_current WHERE record_id = ? AND reference_index = ? AND type = ?`).get(
|
||||
kind === 'knowledge' ? key.slice(0, separator) : key,
|
||||
kind === 'knowledge' ? Number(key.slice(separator + 1)) : 0,
|
||||
kind === 'message' ? 'conversation' : kind) : undefined
|
||||
if (current && scope.kind === 'projects' && !scope.projectIds.includes(String(current.project_id))) throw new Error('scope_mismatch: source moved out of the requested scope')
|
||||
const currentRevision = current ? createHash('sha256').update(String(current.context)).digest('hex') : undefined
|
||||
if (!current || (sourceArgs.content_kind === 'version' && currentRevision !== sourceArgs.source_version)) {
|
||||
return { ...base, error: sourceArgs.content_kind === 'version' ? 'historical_version_unavailable' : 'current_source_unavailable',
|
||||
original_source_availability: current ? 'available' : 'unavailable', available_content_kinds: current ? ['snapshot', 'current'] : ['snapshot'] }
|
||||
}
|
||||
body = String(current.body)
|
||||
returnedVersion = currentRevision!
|
||||
location = JSON.parse(String(current.locator)) as Row
|
||||
originalLength = source.source_type === 'knowledge' ? 'unknown' : Array.from(body).length
|
||||
}
|
||||
const content = Array.from(body)
|
||||
const revision = digest({ source, body, returnedVersion, location })
|
||||
checkRevision(revision)
|
||||
const start = cursor?.offset ?? 0
|
||||
if (start > content.length) throw new Error('invalid_cursor')
|
||||
const end = Math.min(content.length, start + sourceArgs.page_size)
|
||||
signal?.throwIfAborted()
|
||||
return bounded({ ...base, source_reference_id: source.id, result_id: source.result_id, original_source_id: source.source_id,
|
||||
source_type: source.source_type, content_kind: sourceArgs.content_kind, requested_version: sourceArgs.source_version ?? null,
|
||||
content_extent: sourceArgs.content_kind === 'snapshot' ? 'saved_fragment' : source.source_type === 'knowledge' ? 'current_reference_fragment' : 'source_record',
|
||||
source_version: returnedVersion, reference_version: version, differs_from_reference: version === 'unknown' ? 'unknown' : returnedVersion !== version,
|
||||
object_revision: revision, source_location: location, availability: source.availability,
|
||||
original_source_availability: sourceArgs.content_kind === 'snapshot' ? 'unknown' : 'available', source_record_time: source.occurred_at, event_time: null,
|
||||
review_generated_at: source.generated_at, recorded_at: source.generated_at,
|
||||
content: content.slice(start, end).join(''), position: { start, end, unit: 'unicode_code_point' },
|
||||
saved_fragment: { start: locator.start ?? 'unknown', end: locator.end ?? 'unknown', length: Array.from(String(source.content)).length },
|
||||
reviewed_source_length: locator.length ?? 'unknown',
|
||||
original_source_length: originalLength,
|
||||
page: page(revision, 0, end, end < content.length, end - start, content.length) })
|
||||
}
|
||||
const facts: Fact[] = []
|
||||
const results = new Map<string, Row>()
|
||||
const stories = db.prepare('SELECT * FROM story_lines ORDER BY id').all().filter(row => sameScope(row.scope_json))
|
||||
const add = (type: string, data: Row, result?: Row, sources?: string[]): void => {
|
||||
facts.push({ object_ref: { type, id: String(data.id) }, object_revision: digest(data), result_id: result ? String(result.id) : null, scope,
|
||||
event_time: type === 'event' ? timestamp(data.occurred_at) : null,
|
||||
review_generated_at: result ? timestamp(result.created_at) : null,
|
||||
recorded_at: result ? timestamp(result.created_at) : type === 'story_line' ? timestamp(data.created_at) : null,
|
||||
confirmation_state: String(data.confirmation_state ?? 'automatic'), validity: 'unknown',
|
||||
source_reference_ids: sources ?? (data.source_reference_ids_json ? JSON.parse(String(data.source_reference_ids_json)) : []), data })
|
||||
}
|
||||
for (const story of stories) {
|
||||
signal?.throwIfAborted()
|
||||
add('story_line', story)
|
||||
for (const row of db.prepare(`SELECT r.*, sr.time_range_json FROM supervision_results r JOIN supervision_runs sr ON sr.id = r.run_id WHERE sr.scope_json = ? ORDER BY r.id`).all(String(story.scope_json))) {
|
||||
if (results.has(String(row.id))) continue
|
||||
results.set(String(row.id), row)
|
||||
const { graph_snapshot_json, ...summary } = row
|
||||
add('summary', summary, row)
|
||||
const snapshot = JSON.parse(String(graph_snapshot_json)) as { entities: Row[]; relations: Row[] }
|
||||
for (const entity of snapshot.entities) add('entity', entity, row)
|
||||
for (const relation of snapshot.relations) add('relation', relation, row)
|
||||
const sources = db.prepare('SELECT * FROM supervision_sources WHERE result_id = ? ORDER BY id').all(String(row.id))
|
||||
for (const source of sources) add('source_reference', source, row, [String(source.id)])
|
||||
for (const change of db.prepare('SELECT * FROM supervision_entity_changes WHERE result_id = ? ORDER BY id').all(String(row.id))) add('entity_change', change, row)
|
||||
// Full successful leaves retain descriptions that the navigation summary and identity projection can omit.
|
||||
for (const leaf of db.prepare('SELECT id, output_json, evidence_json FROM supervision_review_batches WHERE run_id = ? ORDER BY id').all(String(row.run_id))) {
|
||||
const output = JSON.parse(String(leaf.output_json)) as SupervisionSummaryOutput
|
||||
const evidence = JSON.parse(String(leaf.evidence_json)) as SupervisionEvidence[]
|
||||
const refs = new Map(evidence.map(item => [item.id, sources.filter(source => source.source_type === item.sourceType &&
|
||||
source.source_id === item.sourceId && source.content === item.content && source.locator_json === (item.locator ? JSON.stringify(item.locator) : null)).map(source => String(source.id))]))
|
||||
const references = (ids: string[]) => [...new Set(ids.flatMap(id => refs.get(id) ?? []))]
|
||||
add('summary', { id: `leaf:${leaf.id}`, summary: output.summary, change_digest: output.changeDigest,
|
||||
open_items: output.openItems, content_kind: 'successful_leaf' }, row, references(evidence.map(item => item.id)))
|
||||
// Persisted IDs are explicit identity evidence; local model IDs are namespaced to the saved leaf.
|
||||
for (const entity of output.entities) add('entity', { id: `leaf:${leaf.id}:${entity.id}`,
|
||||
canonical_label: entity.label, description: entity.description, persisted_entity_id: entity.persistedId ?? null,
|
||||
content_kind: 'successful_leaf', leaf_id: leaf.id }, row, references(entity.sourceReferenceIds))
|
||||
for (const relation of output.relations) add('relation', { id: `leaf:${leaf.id}:${digest(relation)}`,
|
||||
from_entity_id: `leaf:${leaf.id}:${relation.fromEntityId}`, to_entity_id: `leaf:${leaf.id}:${relation.toEntityId}`,
|
||||
relation_type: relation.relationType, reason: relation.reason, content_kind: 'successful_leaf' }, row, references(relation.sourceReferenceIds))
|
||||
}
|
||||
}
|
||||
for (const entity of db.prepare('SELECT * FROM supervision_entities WHERE story_line_id = ? ORDER BY id').all(String(story.id))) add('entity', entity)
|
||||
for (const relation of db.prepare('SELECT * FROM supervision_relations WHERE story_line_id = ? ORDER BY id').all(String(story.id))) add('relation', relation)
|
||||
for (const event of db.prepare('SELECT * FROM supervision_events WHERE story_line_id = ? ORDER BY id').all(String(story.id))) {
|
||||
const sources = db.prepare('SELECT source_id FROM supervision_event_sources WHERE event_id = ? ORDER BY source_id').all(String(event.id)).map(row => String(row.source_id))
|
||||
const entities = db.prepare('SELECT entity_id FROM supervision_event_entities WHERE event_id = ? ORDER BY entity_id').all(String(event.id)).map(row => String(row.entity_id))
|
||||
add('event', { ...event, entity_ids: entities }, results.get(String(event.result_id)), sources)
|
||||
}
|
||||
}
|
||||
const revision = digest(facts)
|
||||
checkRevision(revision)
|
||||
const timed = args as z.infer<typeof storyGraphSearchSchema>
|
||||
const matchesTime = (fact: Fact): boolean => !timed.time_range || Boolean(fact[timed.time_basis] &&
|
||||
Date.parse(fact[timed.time_basis]!) >= Date.parse(timed.time_range.from) && Date.parse(fact[timed.time_basis]!) <= Date.parse(timed.time_range.to))
|
||||
const metadata = { ...base, coverage: { ...base.coverage, published_result_count: results.size }, time_basis: timed.time_basis,
|
||||
time_range: timed.time_range ? { from: timestamp(timed.time_range.from), to: timestamp(timed.time_range.to) } : null,
|
||||
interpretation: 'Stored claims within the selected evidence scope; no inferred supersession, completion, or historical state.' }
|
||||
const query = 'query' in args ? args.query.toLowerCase() : ''
|
||||
let selected: Fact[]
|
||||
if (name === 'story_graph_search') {
|
||||
const search = storyGraphSearchSchema.parse(input)
|
||||
selected = facts.filter(fact => (!search.object_types || search.object_types.includes(fact.object_ref.type as typeof search.object_types[number])) && JSON.stringify(fact.data).toLowerCase().includes(query))
|
||||
} else {
|
||||
const context = storyGraphContextSchema.parse(input)
|
||||
const targets = facts.filter(fact => fact.object_ref.type === context.object_ref.type && fact.object_ref.id === context.object_ref.id)
|
||||
if (!targets.length) {
|
||||
const tables = { story_line: 'story_lines', event: 'supervision_events', entity: 'supervision_entities',
|
||||
entity_change: 'supervision_entity_changes', relation: 'supervision_relations', summary: 'supervision_results', source_reference: 'supervision_sources' }
|
||||
const leafId = context.object_ref.id.startsWith('leaf:') ? context.object_ref.id.split(':')[1] : undefined
|
||||
const exists = leafId ? db.prepare('SELECT id FROM supervision_review_batches WHERE id = ?').get(leafId)
|
||||
: db.prepare(`SELECT id FROM ${tables[context.object_ref.type]} WHERE id = ?`).get(context.object_ref.id)
|
||||
throw new Error(exists ? 'scope_mismatch: object is outside the requested scope' : 'object_not_found')
|
||||
}
|
||||
const related = facts.filter(fact => targets.includes(fact) || (context.object_ref.type === 'entity' &&
|
||||
(fact.data.entity_id === context.object_ref.id || fact.data.persisted_entity_id === context.object_ref.id || fact.data.from_entity_id === context.object_ref.id || fact.data.to_entity_id === context.object_ref.id || (Array.isArray(fact.data.entity_ids) && fact.data.entity_ids.includes(context.object_ref.id)))))
|
||||
const targetSources = new Set(related.flatMap(fact => fact.source_reference_ids))
|
||||
const entityIds = new Set(related.flatMap(fact => [fact.data.entity_id, fact.data.from_entity_id, fact.data.to_entity_id, ...(Array.isArray(fact.data.entity_ids) ? fact.data.entity_ids : [])]).filter((id): id is string => typeof id === 'string'))
|
||||
const targetResults = new Set(targets.flatMap(fact => fact.result_id ? [fact.result_id] : []))
|
||||
selected = facts.filter(fact => {
|
||||
if (context.object_ref.type === 'story_line') return true
|
||||
if (related.includes(fact) || (fact.object_ref.type === 'entity' && entityIds.has(fact.object_ref.id))) return true
|
||||
if (context.object_ref.type === 'summary') return fact.result_id !== null && targetResults.has(fact.result_id)
|
||||
return fact.source_reference_ids.some(id => targetSources.has(id))
|
||||
})
|
||||
}
|
||||
const unknownExcluded = timed.time_range ? selected.filter(fact => !fact[timed.time_basis]).length : 0
|
||||
selected = selected.filter(matchesTime)
|
||||
const score = (fact: Fact): number => {
|
||||
if (name !== 'story_graph_search') return 0
|
||||
return Object.entries(fact.data).reduce((sum, [field, value]) => sum + (JSON.stringify(value).toLowerCase().includes(query) ? (['title', 'canonical_label', 'summary'].includes(field) ? 3 : 1) : 0), 0)
|
||||
}
|
||||
selected.sort((a, b) => score(b) - score(a) || (name === 'story_graph_get_context' && 'mode' in args && args.mode === 'timeline'
|
||||
? (a.event_time ?? '\uffff').localeCompare(b.event_time ?? '\uffff') : 0) ||
|
||||
a.object_ref.type.localeCompare(b.object_ref.type) || a.object_ref.id.localeCompare(b.object_ref.id) || (a.result_id ?? '').localeCompare(b.result_id ?? ''))
|
||||
let index = cursor?.index ?? 0
|
||||
let offset = cursor?.offset ?? 0
|
||||
if (index > selected.length) throw new Error('invalid_cursor')
|
||||
const items: Row[] = []
|
||||
for (; index < selected.length && items.length < args.page_size; index++) {
|
||||
signal?.throwIfAborted()
|
||||
const { data, ...fact } = selected[index]!
|
||||
const text = Array.from(JSON.stringify(data))
|
||||
if (name === 'story_graph_search') {
|
||||
const fields = Object.keys(data).filter(field => JSON.stringify(data[field]).toLowerCase().includes(query))
|
||||
items.push({ ...fact, matched_fields: fields, preview: text.slice(0, 400).join(''), preview_is_excerpt: true })
|
||||
} else {
|
||||
const size = storyGraphContextSchema.parse(input).content_page_size
|
||||
const end = Math.min(text.length, offset + size)
|
||||
items.push({ ...fact, content: text.slice(offset, end).join(''), content_format: 'json_fragment', position: { start: offset, end, length: text.length, unit: 'unicode_code_point' } })
|
||||
if (end < text.length) { offset = end; break }
|
||||
offset = 0
|
||||
}
|
||||
// Fail explicitly rather than allowing a downstream runtime to truncate metadata.
|
||||
if (Buffer.byteLength(JSON.stringify(items)) > 90_000) throw new Error('response_too_large: retry with a smaller page_size')
|
||||
}
|
||||
const output = { ...metadata, unknown_time_excluded_count: unknownExcluded, ...('mode' in args ? { mode: args.mode } : {}), items, page: page(revision, index, offset, index < selected.length, items.length, selected.length) }
|
||||
return bounded(output)
|
||||
}
|
||||
@@ -86,6 +86,12 @@ it('persists publication failure and resumes with saved navigation without model
|
||||
expect(batch.output.summary).toBe(summary)
|
||||
expect(batch.output.events[0]!.description).toBe(description)
|
||||
}
|
||||
const calls = f.respond.mock.calls.length
|
||||
expect(f.db.readStoryGraph('story_graph_search', { query: 'Retained source qualification', scope: { kind: 'global' }, object_types: ['event'] }))
|
||||
.toMatchObject({ page: { total_count: 2 }, items: [expect.objectContaining({ source_reference_ids: [] }), expect.objectContaining({ source_reference_ids: [] })] })
|
||||
expect(f.db.readStoryGraph('story_graph_search', { query: 'Atlas', scope: { kind: 'global' }, object_types: ['entity'] }))
|
||||
.toMatchObject({ page: { has_more: false } })
|
||||
expect(f.respond).toHaveBeenCalledTimes(calls)
|
||||
})
|
||||
|
||||
it('stops oversized runtime responses with a readable error while preserving resumable progress', async () => {
|
||||
|
||||
@@ -165,6 +165,7 @@ describe('CapabilityService', () => {
|
||||
const stored = JSON.parse(await readFile(filePath, 'utf8'))
|
||||
delete stored.obsidian
|
||||
delete stored.builtinMcpServers.obsidian
|
||||
delete stored.builtinMcpServers['story-graph']
|
||||
stored.version = version
|
||||
if (version === 5) delete stored.builtinMcpServers['builtin-browser']
|
||||
if (version === 1) {
|
||||
@@ -322,6 +323,7 @@ describe('CapabilityService', () => {
|
||||
|
||||
await expect(service.getSnapshot()).resolves.toMatchObject({
|
||||
builtinMcpServers: [
|
||||
{ id: 'story-graph', enabled: true, assignments: ['model', 'opencode', 'continue', 'deepseek-harness'] },
|
||||
{
|
||||
id: 'knowledge-base',
|
||||
enabled: true,
|
||||
@@ -366,19 +368,23 @@ describe('CapabilityService', () => {
|
||||
|
||||
await expect(
|
||||
service.getEnabledBuiltinMcpServerIds('model')
|
||||
).resolves.toEqual(['knowledge-base', 'goodbuddy-config'])
|
||||
).resolves.toEqual(['story-graph', 'knowledge-base', 'goodbuddy-config'])
|
||||
await expect(
|
||||
service.getEnabledBuiltinMcpServerIds('opencode')
|
||||
).resolves.toEqual(['goodbuddy-config'])
|
||||
).resolves.toEqual(['story-graph', 'goodbuddy-config'])
|
||||
await expect(
|
||||
service.getEnabledBuiltinMcpServerIds('continue')
|
||||
).resolves.toEqual([
|
||||
'story-graph',
|
||||
'goodbuddy-config',
|
||||
'builtin-browser'
|
||||
])
|
||||
await expect(
|
||||
service.getEnabledBuiltinMcpServerIds('deepseek-harness')
|
||||
).resolves.toEqual([])
|
||||
).resolves.toEqual(['story-graph'])
|
||||
|
||||
await service.setBuiltinMcpServerAssignments('story-graph', ['deepseek-harness'])
|
||||
await expect(service.getEnabledBuiltinMcpServerIds('model')).resolves.not.toContain('story-graph')
|
||||
|
||||
const reloaded = new CapabilityService(
|
||||
filePath,
|
||||
@@ -388,6 +394,7 @@ describe('CapabilityService', () => {
|
||||
)
|
||||
await expect(reloaded.getSnapshot()).resolves.toMatchObject({
|
||||
builtinMcpServers: expect.arrayContaining([
|
||||
expect.objectContaining({ id: 'story-graph', assignments: ['deepseek-harness'] }),
|
||||
expect.objectContaining({
|
||||
id: 'knowledge-base',
|
||||
assignments: ['model']
|
||||
|
||||
@@ -134,6 +134,7 @@ const legacyBuiltinMcpServerStatesSchema = z
|
||||
|
||||
const builtinMcpServerStatesSchema =
|
||||
legacyBuiltinMcpServerStatesSchema.extend({
|
||||
'story-graph': builtinMcpServerStateSchema.extend({ assignments: capabilityAssignmentsSchema }).default({ enabled: true, assignments: ['model', 'opencode', 'continue', 'deepseek-harness'] }),
|
||||
'builtin-browser': builtinMcpServerStateSchema,
|
||||
obsidian: builtinMcpServerStateSchema.default({
|
||||
enabled: false,
|
||||
@@ -317,6 +318,7 @@ function defaultBuiltinMcpServerStates(
|
||||
})
|
||||
return {
|
||||
'knowledge-base': defaultState(),
|
||||
'story-graph': { enabled: true, assignments: ['model', 'opencode', 'continue', 'deepseek-harness'] },
|
||||
'magic-notes': defaultState(),
|
||||
'goodbuddy-config': defaultState(),
|
||||
obsidian: { ...defaultState(), enabled: false },
|
||||
@@ -880,6 +882,7 @@ export class CapabilityService {
|
||||
version: 6,
|
||||
obsidian: { vaultPath: '' },
|
||||
builtinMcpServers: {
|
||||
'story-graph': defaultBuiltinMcpServerStates()['story-graph'],
|
||||
obsidian: defaultBuiltinMcpServerStates().obsidian,
|
||||
...legacy.builtinMcpServers,
|
||||
'builtin-browser': {
|
||||
@@ -1750,7 +1753,7 @@ export class CapabilityService {
|
||||
assignments: CapabilityAssignments
|
||||
): Promise<CapabilitySnapshot> {
|
||||
return this.updateBuiltinMcpServerState(serverId, {
|
||||
assignments: builtinMcpAssignmentsSchema.parse(assignments)
|
||||
assignments: (serverId === 'story-graph' ? capabilityAssignmentsSchema : builtinMcpAssignmentsSchema).parse(assignments)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2053,11 +2056,9 @@ export class CapabilityService {
|
||||
target: RuntimeTarget
|
||||
): Promise<BuiltinMcpServerId[]> {
|
||||
const runtime = runtimeTargetSchema.parse(target)
|
||||
if (runtime === 'deepseek-harness') {
|
||||
return []
|
||||
}
|
||||
const state = await this.load()
|
||||
return builtinMcpServerIdSchema.options.filter((id) => {
|
||||
if (runtime === 'deepseek-harness' && id !== 'story-graph') return false
|
||||
const server = state.builtinMcpServers[id]
|
||||
if (!server.enabled || !server.assignments.includes(runtime)) {
|
||||
return false
|
||||
|
||||
@@ -976,6 +976,11 @@ if (hasSingleInstanceLock) {
|
||||
const startupKnowledgeGateway = new KnowledgeMcpGateway(
|
||||
startupKnowledgeService,
|
||||
{
|
||||
storyGraphService: {
|
||||
available: async ({ runtimeTarget }) => (await applicationSettingsStore.get()).heartbeatEnabled === true &&
|
||||
(await capabilityService.getEnabledBuiltinMcpServerIds(runtimeTarget)).includes('story-graph'),
|
||||
read: (name, input, projectId, signal) => startupAssistantDatabase.readStoryGraph(name, input, projectId, signal)
|
||||
},
|
||||
magicNotesDatabase: startupAssistantDatabase,
|
||||
configService: goodbuddyConfigService,
|
||||
obsidianService,
|
||||
@@ -1436,6 +1441,11 @@ if (hasSingleInstanceLock) {
|
||||
resourcesPath: app.isPackaged ? process.resourcesPath : undefined,
|
||||
openExternal: (url) => shell.openExternal(url),
|
||||
createGateway: () => new KnowledgeMcpGateway(startupKnowledgeService, {
|
||||
storyGraphService: {
|
||||
available: async ({ runtimeTarget }) => (await applicationSettingsStore.get()).heartbeatEnabled === true &&
|
||||
(await capabilityService.getEnabledBuiltinMcpServerIds(runtimeTarget)).includes('story-graph'),
|
||||
read: (name, input, projectId, signal) => startupAssistantDatabase.readStoryGraph(name, input, projectId, signal)
|
||||
},
|
||||
// This dedicated gateway lives until native-client disposal, not a chat request timeout.
|
||||
now: () => 0,
|
||||
magicNotesDatabase: startupAssistantDatabase, configService: goodbuddyConfigService,
|
||||
|
||||
@@ -9211,6 +9211,32 @@ describe('registerIpcHandlers agent terminal state', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it.each(['model', 'opencode', 'continue', 'deepseek-harness'])('binds Story Graph to the current project for %s and omits it when Supervisor is off', async runtimeId => {
|
||||
const received: AgentExecutionRequest[] = []
|
||||
const binding = { available: vi.fn(async () => true), call: vi.fn() }
|
||||
const gateway = { grant: vi.fn(() => 'graph-capability'), getAvailableToolNames: vi.fn(() => ['story_graph_search', 'story_graph_get_context', 'story_graph_read_source']),
|
||||
bindRemoteStoryGraph: vi.fn(() => binding), drainReferences: vi.fn(() => []), revoke: vi.fn() }
|
||||
const harness = createHarness({ runtimeId, capability: 'chat', supportsToolExecution: true,
|
||||
async *run(request: AgentExecutionRequest) { received.push(request); yield { requestId: request.requestId, type: 'done' } }
|
||||
}, undefined, 'always', undefined, false, undefined, undefined, gateway, false, undefined, {
|
||||
getEnabledBuiltinMcpServerIds: vi.fn(async () => ['story-graph'])
|
||||
})
|
||||
try {
|
||||
const projectId = '00000000-0000-4000-8000-000000000088'
|
||||
for (const enabled of [true, false]) {
|
||||
harness.getApplicationSettings.mockResolvedValue({ heartbeatEnabled: enabled })
|
||||
const requestId = crypto.randomUUID()
|
||||
await harness.handler!(trustedEvent(harness.webContents), { requestId, projectId, conversationId: 'graph-grant', prompt: 'Continue work', workMode: 'ask', knowledgeLibraryIds: [] })
|
||||
await vi.waitFor(() => expect(harness.assistantDatabase.updateTaskStatus).toHaveBeenCalledWith(requestId, 'completed'))
|
||||
}
|
||||
expect(gateway.grant).toHaveBeenCalledExactlyOnceWith(expect.any(String), [], expect.any(AbortSignal), 'none',
|
||||
undefined, undefined, undefined, undefined, undefined, { projectId, runtimeTarget: runtimeId })
|
||||
expect(received[0]).toMatchObject({ knowledgeCapabilityToken: 'graph-capability', storyGraphBinding: binding })
|
||||
expect(received[1]).not.toHaveProperty('storyGraphBinding')
|
||||
expect(gateway.bindRemoteStoryGraph).toHaveBeenCalledOnce()
|
||||
} finally { await harness.dispose() }
|
||||
})
|
||||
|
||||
it('grants read-only Magic Notes tools in Ask and write tools in Execute', async () => {
|
||||
const runtime = {
|
||||
runtimeId: 'model',
|
||||
|
||||
+15
-4
@@ -450,6 +450,7 @@ async function grantScopedDataCapability(input: {
|
||||
magicNotesAccess: MagicNotesCapabilityAccess
|
||||
configAccess?: MagicNotesCapabilityAccess
|
||||
obsidian?: { settings: ObsidianSettings; access: 'read' | 'write' }
|
||||
storyGraph?: import('./agent/knowledge-mcp-gateway').StoryGraphBinding
|
||||
workspacePath?: string
|
||||
browserConversationId?: string
|
||||
browserTabId?: BrowserTabId
|
||||
@@ -462,6 +463,7 @@ async function grantScopedDataCapability(input: {
|
||||
}): Promise<ScopedDataCapability> {
|
||||
const enabledServers = new Set(input.enabledServers)
|
||||
const obsidian = enabledServers.has('obsidian') ? input.obsidian : undefined
|
||||
const storyGraph = enabledServers.has('story-graph') ? input.storyGraph : undefined
|
||||
const libraryIds = enabledServers.has('knowledge-base')
|
||||
? input.libraryIds
|
||||
: []
|
||||
@@ -480,7 +482,7 @@ async function grantScopedDataCapability(input: {
|
||||
magicNotesAccess === 'none' &&
|
||||
configAccess === 'none' &&
|
||||
!browserConversationId &&
|
||||
!obsidian)
|
||||
!obsidian && !storyGraph)
|
||||
) {
|
||||
return { toolNames: [] }
|
||||
}
|
||||
@@ -506,7 +508,10 @@ async function grantScopedDataCapability(input: {
|
||||
authorizeApply: input.authorizeConfigApply
|
||||
}
|
||||
: undefined
|
||||
const token = obsidian
|
||||
const token = storyGraph
|
||||
? input.gateway.grant(input.requestId, libraryIds, input.signal, magicNotesAccess, config,
|
||||
browserConversationId, browserTabId, browserUsageLease, obsidian, storyGraph)
|
||||
: obsidian
|
||||
? input.gateway.grant(
|
||||
input.requestId,
|
||||
libraryIds,
|
||||
@@ -2280,6 +2285,8 @@ export function registerIpcHandlers(
|
||||
)
|
||||
: []
|
||||
const notesCapability = await grantScopedDataCapability({
|
||||
storyGraph: requestRuntimeTarget && (await applicationSettingsStore?.get())?.heartbeatEnabled
|
||||
? { runtimeTarget: requestRuntimeTarget, projectId: schedule.projectId ?? undefined } : undefined,
|
||||
obsidian: enabledBuiltinMcpServers.includes('obsidian')
|
||||
? {
|
||||
settings: await capabilityService.getObsidianSettings(),
|
||||
@@ -2397,7 +2404,8 @@ export function registerIpcHandlers(
|
||||
}),
|
||||
trustedInstructions,
|
||||
...(knowledgeCapabilityToken
|
||||
? { knowledgeCapabilityToken }
|
||||
? { knowledgeCapabilityToken, ...(notesCapability.toolNames.includes('story_graph_search')
|
||||
? { storyGraphBinding: knowledgeGateway?.bindRemoteStoryGraph(knowledgeCapabilityToken) } : {}) }
|
||||
: {}),
|
||||
...(notesCapability.browserTabId
|
||||
? { browserTabId: notesCapability.browserTabId }
|
||||
@@ -3807,6 +3815,8 @@ export function registerIpcHandlers(
|
||||
: undefined
|
||||
const imageToolAvailable = Boolean(await imageToolBinding?.describe())
|
||||
const scopedCapability = await grantScopedDataCapability({
|
||||
storyGraph: selectedRuntimeTarget && applicationSettings?.heartbeatEnabled
|
||||
? { runtimeTarget: selectedRuntimeTarget, projectId: enrichedRequest.projectId } : undefined,
|
||||
obsidian: enabledBuiltinMcpServers.includes('obsidian')
|
||||
? {
|
||||
settings: await capabilityService.getObsidianSettings(),
|
||||
@@ -3873,7 +3883,8 @@ export function registerIpcHandlers(
|
||||
}
|
||||
: enrichedRequest
|
||||
const request: AgentExecutionRequest = knowledgeCapabilityToken
|
||||
? { ...baseRequest, knowledgeCapabilityToken }
|
||||
? { ...baseRequest, knowledgeCapabilityToken, ...(scopedCapability.toolNames.includes('story_graph_search')
|
||||
? { storyGraphBinding: knowledgeGateway?.bindRemoteStoryGraph(knowledgeCapabilityToken) } : {}) }
|
||||
: baseRequest
|
||||
if (scopedCapability.browserTabId) {
|
||||
request.browserTabId = scopedCapability.browserTabId
|
||||
|
||||
@@ -12,8 +12,9 @@ export class MainImageToolSession {
|
||||
|
||||
constructor(
|
||||
private readonly channel: RuntimeProtocolBinaryChannel,
|
||||
private readonly binding: ImageToolBinding,
|
||||
signal?: AbortSignal
|
||||
private readonly binding: ImageToolBinding | undefined,
|
||||
signal?: AbortSignal,
|
||||
private readonly storyGraph?: import('../agent/knowledge-mcp-gateway').StoryGraphRemoteBinding
|
||||
) {
|
||||
this.unsubscribe = channel.onClose(() => this.close())
|
||||
const abort = (): void => this.close()
|
||||
@@ -45,9 +46,17 @@ export class MainImageToolSession {
|
||||
let reply: ReturnType<typeof remoteImageToolReplySchema.parse>
|
||||
try {
|
||||
this.wait.signal.throwIfAborted()
|
||||
if (this.binding.context.workMode !== 'execute') throw new Error('Image tools are unavailable in Ask mode')
|
||||
const result = await this.binding.call(call.input, call.callId, this.wait.signal)
|
||||
reply = remoteImageToolReplySchema.parse({ callId: call.callId, result })
|
||||
if ('name' in call) {
|
||||
if (!this.storyGraph) throw new Error('Story Graph capability unavailable')
|
||||
const storyGraphResult = call.name === 'story_graph_list'
|
||||
? { available: await this.storyGraph.available() }
|
||||
: await this.storyGraph.call(call.name, call.input, this.wait.signal)
|
||||
reply = remoteImageToolReplySchema.parse({ callId: call.callId, storyGraphResult })
|
||||
} else {
|
||||
if (!this.binding || this.binding.context.workMode !== 'execute') throw new Error('Image tools are unavailable in Ask mode')
|
||||
const result = await this.binding.call(call.input, call.callId, this.wait.signal)
|
||||
reply = remoteImageToolReplySchema.parse({ callId: call.callId, result })
|
||||
}
|
||||
} catch (error) {
|
||||
reply = { callId: call.callId, error: (error instanceof Error ? error.message : 'Image tool failed').slice(0, 4_000) }
|
||||
}
|
||||
|
||||
@@ -525,9 +525,17 @@ describe('ProtocolRemoteRuntimeChannel', () => {
|
||||
expect(describe).toHaveBeenCalledTimes(2)
|
||||
expect(fixture.client.blobBinaries[1]!.closed).toBe(true)
|
||||
expect(fixture.client.requests.at(-1)!.params).not.toHaveProperty('imageTool', expect.anything())
|
||||
const graph = { available: vi.fn(async () => true), call: vi.fn() }
|
||||
await fixture.channel.preparePrompt(prepare('graph-ask', 'ask'), undefined, wait.signal, graph)
|
||||
expect(fixture.client.requests.at(-1)!.params).toMatchObject({ imageTool: { storyGraph: true } })
|
||||
expect((fixture.client.requests.at(-1)!.params as { imageTool: object }).imageTool).not.toHaveProperty('description')
|
||||
graph.available.mockResolvedValue(false)
|
||||
await fixture.channel.preparePrompt(prepare('graph-disabled', 'ask'), undefined, wait.signal, graph)
|
||||
expect(fixture.client.blobBinaries[2]!.closed).toBe(true)
|
||||
expect(fixture.client.requests.at(-1)!.params).not.toHaveProperty('imageTool', expect.anything())
|
||||
fixture.client.responder = () => { throw new Error('preparation response lost') }
|
||||
await expect(fixture.channel.preparePrompt(prepare('operation-4'), binding, wait.signal)).rejects.toThrow()
|
||||
expect(fixture.client.blobBinaries[2]!.closed).toBe(true)
|
||||
expect(fixture.client.blobBinaries[3]!.closed).toBe(true)
|
||||
} finally { await fixture.channel.close() }
|
||||
})
|
||||
it('sends question replies on the authenticated control channel and rejects foreign bindings', async () => {
|
||||
|
||||
@@ -588,21 +588,23 @@ export class ProtocolRemoteRuntimeChannel
|
||||
async preparePrompt(
|
||||
preparation: z.input<typeof remotePromptOperationPreparationSchema>,
|
||||
imageToolBinding?: ImageToolBinding,
|
||||
waitSignal?: AbortSignal
|
||||
waitSignal?: AbortSignal,
|
||||
storyGraphBinding?: import('../agent/knowledge-mcp-gateway').StoryGraphRemoteBinding
|
||||
): Promise<
|
||||
z.infer<typeof remotePromptOperationAcceptanceSchema>
|
||||
> {
|
||||
this.#imageTool?.close()
|
||||
this.#imageTool = undefined
|
||||
const description = preparation.workMode === 'execute' ? await imageToolBinding?.describe() : undefined
|
||||
const storyGraph = await storyGraphBinding?.available()
|
||||
waitSignal?.throwIfAborted()
|
||||
let imageTool: z.infer<typeof remotePromptOperationPreparationSchema>['imageTool']
|
||||
if (description && imageToolBinding) {
|
||||
if ((description && imageToolBinding) || storyGraph) {
|
||||
this.#assertCurrent()
|
||||
const binary = this.#state.client.allocateBinaryChannel?.({ kind: 'blob' })
|
||||
if (!binary) throw new Error('Remote image tool transport is unavailable')
|
||||
imageTool = { channelId: binary.channelId, channelEpoch: binary.channelEpoch, description }
|
||||
this.#imageTool = new MainImageToolSession(binary, imageToolBinding, waitSignal)
|
||||
imageTool = { channelId: binary.channelId, channelEpoch: binary.channelEpoch, ...(description ? { description } : {}), ...(storyGraph ? { storyGraph: true } : {}) }
|
||||
this.#imageTool = new MainImageToolSession(binary, imageToolBinding, waitSignal, storyGraphBinding)
|
||||
}
|
||||
try {
|
||||
const parsed = remotePromptOperationPreparationSchema.parse({ ...preparation, imageTool })
|
||||
|
||||
Reference in New Issue
Block a user