fix(copilot): mark internal hook injections as agent-initiated
Apply the internal initiator marker to automated continuation, recovery, babysitter, stop-hook, and hook-message injections so Copilot attribution consistently sets x-initiator=agent for system-generated prompts.
This commit is contained in:
@@ -5,7 +5,7 @@ import { MESSAGE_STORAGE, PART_STORAGE } from "./constants"
|
||||
import type { MessageMeta, OriginalMessageContext, TextPart, ToolPermission } from "./types"
|
||||
import { log } from "../../shared/logger"
|
||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||
import { normalizeSDKResponse } from "../../shared"
|
||||
import { createInternalAgentTextPart, normalizeSDKResponse } from "../../shared"
|
||||
|
||||
export interface StoredMessage {
|
||||
agent?: string
|
||||
@@ -331,7 +331,7 @@ export function injectHookMessage(
|
||||
const textPart: TextPart = {
|
||||
id: partID,
|
||||
type: "text",
|
||||
text: hookContent,
|
||||
text: createInternalAgentTextPart(hookContent).text,
|
||||
synthetic: true,
|
||||
time: {
|
||||
start: now,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import type { BackgroundManager } from "../../features/background-agent"
|
||||
import { log } from "../../shared/logger"
|
||||
import { resolveInheritedPromptTools } from "../../shared"
|
||||
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||
import { HOOK_NAME } from "./hook-name"
|
||||
import { BOULDER_CONTINUATION_PROMPT } from "./system-reminder-templates"
|
||||
import { resolveRecentPromptContextForSession } from "./recent-model-resolver"
|
||||
@@ -53,7 +53,7 @@ export async function injectBoulderContinuation(input: {
|
||||
agent: agent ?? "atlas",
|
||||
...(promptContext.model !== undefined ? { model: promptContext.model } : {}),
|
||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||
parts: [{ type: "text", text: prompt }],
|
||||
parts: [createInternalAgentTextPart(prompt)],
|
||||
},
|
||||
query: { directory: ctx.directory },
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import { loadClaudeHooksConfig } from "../config"
|
||||
import { loadPluginExtendedConfig } from "../config-loader"
|
||||
import { executeStopHooks, type StopContext } from "../stop"
|
||||
import type { PluginConfig } from "../types"
|
||||
import { isHookDisabled, log } from "../../../shared"
|
||||
import { createInternalAgentTextPart, isHookDisabled, log } from "../../../shared"
|
||||
import {
|
||||
clearSessionHookState,
|
||||
sessionErrorState,
|
||||
@@ -94,7 +94,7 @@ export function createSessionEventHandler(ctx: PluginInput, config: PluginConfig
|
||||
.prompt({
|
||||
path: { id: sessionID },
|
||||
body: {
|
||||
parts: [{ type: "text", text: stopResult.injectPrompt }],
|
||||
parts: [createInternalAgentTextPart(stopResult.injectPrompt)],
|
||||
},
|
||||
query: { directory: ctx.directory },
|
||||
})
|
||||
|
||||
@@ -3,7 +3,11 @@ import { log } from "../../shared/logger"
|
||||
import { findNearestMessageWithFields } from "../../features/hook-message-injector"
|
||||
import { getMessageDir } from "./message-storage-directory"
|
||||
import { withTimeout } from "./with-timeout"
|
||||
import { normalizeSDKResponse, resolveInheritedPromptTools } from "../../shared"
|
||||
import {
|
||||
createInternalAgentTextPart,
|
||||
normalizeSDKResponse,
|
||||
resolveInheritedPromptTools,
|
||||
} from "../../shared"
|
||||
|
||||
type MessageInfo = {
|
||||
agent?: string
|
||||
@@ -64,7 +68,7 @@ export async function injectContinuationPrompt(
|
||||
...(agent !== undefined ? { agent } : {}),
|
||||
...(model !== undefined ? { model } : {}),
|
||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||
parts: [{ type: "text", text: options.prompt }],
|
||||
parts: [createInternalAgentTextPart(options.prompt)],
|
||||
},
|
||||
query: { directory: options.directory },
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
declare const require: (name: string) => any
|
||||
const { describe, expect, test } = require("bun:test")
|
||||
import { extractResumeConfig, resumeSession } from "./resume"
|
||||
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||
import type { MessageData } from "./types"
|
||||
|
||||
describe("session-recovery resume", () => {
|
||||
@@ -44,5 +45,8 @@ describe("session-recovery resume", () => {
|
||||
// then
|
||||
expect(ok).toBe(true)
|
||||
expect(promptBody?.tools).toEqual({ question: false, bash: true })
|
||||
expect(Array.isArray(promptBody?.parts)).toBe(true)
|
||||
const firstPart = (promptBody?.parts as Array<{ text?: string }>)?.[0]
|
||||
expect(firstPart?.text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { createOpencodeClient } from "@opencode-ai/sdk"
|
||||
import type { MessageData, ResumeConfig } from "./types"
|
||||
import { resolveInheritedPromptTools } from "../../shared"
|
||||
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||
|
||||
const RECOVERY_RESUME_TEXT = "[session recovered - continuing previous task]"
|
||||
|
||||
@@ -30,7 +30,7 @@ export async function resumeSession(client: Client, config: ResumeConfig): Promi
|
||||
await client.session.promptAsync({
|
||||
path: { id: config.sessionID },
|
||||
body: {
|
||||
parts: [{ type: "text", text: RECOVERY_RESUME_TEXT }],
|
||||
parts: [createInternalAgentTextPart(RECOVERY_RESUME_TEXT)],
|
||||
agent: config.agent,
|
||||
model: config.model,
|
||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||
|
||||
@@ -2,18 +2,26 @@ declare const require: (name: string) => any
|
||||
const { describe, expect, test } = require("bun:test")
|
||||
|
||||
import { injectContinuation } from "./continuation-injection"
|
||||
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||
|
||||
describe("injectContinuation", () => {
|
||||
test("inherits tools from resolved message info when reinjecting", async () => {
|
||||
// given
|
||||
let capturedTools: Record<string, boolean> | undefined
|
||||
let capturedText: string | undefined
|
||||
const ctx = {
|
||||
directory: "/tmp/test",
|
||||
client: {
|
||||
session: {
|
||||
todo: async () => ({ data: [{ id: "1", content: "todo", status: "pending", priority: "high" }] }),
|
||||
promptAsync: async (input: { body: { tools?: Record<string, boolean> } }) => {
|
||||
promptAsync: async (input: {
|
||||
body: {
|
||||
tools?: Record<string, boolean>
|
||||
parts?: Array<{ type: string; text: string }>
|
||||
}
|
||||
}) => {
|
||||
capturedTools = input.body.tools
|
||||
capturedText = input.body.parts?.[0]?.text
|
||||
return {}
|
||||
},
|
||||
},
|
||||
@@ -37,5 +45,6 @@ describe("injectContinuation", () => {
|
||||
|
||||
// then
|
||||
expect(capturedTools).toEqual({ question: false, bash: true })
|
||||
expect(capturedText).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
|
||||
import type { BackgroundManager } from "../../features/background-agent"
|
||||
import { normalizeSDKResponse, resolveInheritedPromptTools } from "../../shared"
|
||||
import {
|
||||
createInternalAgentTextPart,
|
||||
normalizeSDKResponse,
|
||||
resolveInheritedPromptTools,
|
||||
} from "../../shared"
|
||||
import {
|
||||
findNearestMessageWithFields,
|
||||
findNearestMessageWithFieldsFromSDK,
|
||||
@@ -151,7 +155,7 @@ ${todoList}`
|
||||
agent: agentName,
|
||||
...(model !== undefined ? { model } : {}),
|
||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||
parts: [{ type: "text", text: prompt }],
|
||||
parts: [createInternalAgentTextPart(prompt)],
|
||||
},
|
||||
query: { directory: ctx.directory },
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { _resetForTesting, setMainSession } from "../../features/claude-code-session-state"
|
||||
import type { BackgroundTask } from "../../features/background-agent"
|
||||
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||
import { createUnstableAgentBabysitterHook } from "./index"
|
||||
|
||||
const projectDir = process.cwd()
|
||||
@@ -93,6 +94,7 @@ describe("unstable-agent-babysitter hook", () => {
|
||||
expect(text).toContain("background_output")
|
||||
expect(text).toContain("background_cancel")
|
||||
expect(text).toContain("deep thought")
|
||||
expect(text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||
})
|
||||
|
||||
test("fires reminder for hung minimax task", async () => {
|
||||
@@ -128,6 +130,7 @@ describe("unstable-agent-babysitter hook", () => {
|
||||
expect(text).toContain("background_output")
|
||||
expect(text).toContain("background_cancel")
|
||||
expect(text).toContain("minimax thought")
|
||||
expect(text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||
})
|
||||
|
||||
test("does not remind stable model tasks", async () => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { BackgroundManager } from "../../features/background-agent"
|
||||
import { getMainSessionID, getSessionAgent } from "../../features/claude-code-session-state"
|
||||
import { log } from "../../shared/logger"
|
||||
import { resolveInheritedPromptTools } from "../../shared"
|
||||
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||
import {
|
||||
buildReminder,
|
||||
extractMessages,
|
||||
@@ -158,7 +158,7 @@ export function createUnstableAgentBabysitterHook(ctx: BabysitterContext, option
|
||||
...(agent ? { agent } : {}),
|
||||
...(model ? { model } : {}),
|
||||
...(tools ? { tools } : {}),
|
||||
parts: [{ type: "text", text: reminder }],
|
||||
parts: [createInternalAgentTextPart(reminder)],
|
||||
},
|
||||
query: { directory: ctx.directory },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user