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