siyuan-note/siyuan · error

injectAgent

Error message

injectAgent: %v

What it means

injectAgent wraps siyuan.agent sandbox injection for a kernel plugin's goja runtime in a recover() that converts any panic during injection into the error 'injectAgent: %v'. It indicates an unexpected runtime failure (goja exception, nil object, value conversion panic) while installing the agent API into the plugin sandbox — plugin initialization for the agent subsystem failed.

Solutions

  1. Read the wrapped '%v' detail in kernel logs to find the panicking step, then fix the plugin code or report the kernel bug
  2. Disable or reinstall the offending plugin to rule out a corrupted bundle
  3. Update the kernel/plugin to matching versions and retry loading the plugin
Defensive patterns

Strategy: try-catch

Try / catch

if err := injectAgent(p, rt, siyuan); err != nil {
    logging.LogErrorf("plugin [%s] agent injection failed: %s", p.Name, err)
    // mark the plugin's agent API as unavailable instead of failing the whole load
}

Prevention

When it happens

Trigger: Plugin load/enable where building the siyuan.agent object or registering its methods panics — e.g. goja runtime in a bad state, a ToValue/Set failure, or a panic inside jsCapabilitySchemaToGoSchema helpers during injection.

Common situations: A corrupted or incompatible plugin bundle triggering goja panics during bootstrap; a kernel bug or version mismatch between plugin API expectations and the runtime; low-memory or runtime-shutdown races during plugin teardown/load.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/49a29a515ccd9d78. Report an issue: GitHub.

Appendix: source

Thrown at kernel/plugin/api_agent.go:49

// pluginCapabilityModelName 构造内核插件能力向模型暴露的函数名。
func pluginCapabilityModelName(pluginName, capabilityName string) string {
	name := fmt.Sprintf("plugin__%s__%s", util.SanitizeName(pluginName), util.SanitizeName(capabilityName))
	hash := sha256.Sum256([]byte(pluginName + "\x00" + capabilityName))
	suffix := fmt.Sprintf("__%x", hash[:6])
	if len(name) > maxCapabilityModelNameLen-len(suffix) {
		name = name[:maxCapabilityModelNameLen-len(suffix)]
	}
	return name + suffix
}

const maxCapabilityModelNameLen = 64

// injectAgent 将 siyuan.agent 注入插件 JS 沙箱。
func injectAgent(p *KernelPlugin, rt *goja.Runtime, siyuan *goja.Object) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("injectAgent: %v", r)
		}
	}()

	agentAPI := rt.NewObject()

	// siyuan.agent.registerCapability(name, config, handler) 返回 Promise<IRegisteredCapability>。
	lo.Must0(agentAPI.Set("registerCapability", rt.ToValue(func(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
		promise, resolve, reject := rt.NewPromise()

		var name string
		var title string
		var description string
		var effects *tools.ToolEffects
		var actionEffects map[string]tools.ToolEffects
		var inputSchema *tools.ToolSchema
		var outputSchema *tools.ToolSchema
		var handler goja.Callable

View on GitHub (pinned to 9f775e8a12)