Skip to content

Commit b34eb16

Browse files
authored
fix(copilot): restore OpenClaw guidance to agent turns (#125997)
* fix(copilot): add OpenClaw prompt guidance Copilot append-mode system messages included credential safety, workspace bootstrap, and extra context but omitted OpenClaw delegation and reply-delivery policy. Build guidance from the final policy-filtered tool surface so visible delegated work, Skill Workshop, and source replies follow the same behavior as Codex. * fix(copilot): break prompt guidance import cycle The CI architecture gate detected a cycle through attempt-config and prompt-guidance. Isolate raw-run mode detection in a leaf module.
1 parent 56fb860 commit b34eb16

21 files changed

Lines changed: 971 additions & 357 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ extensions/copilot/src/provider-bridge.ts 1
293293
extensions/copilot/src/replay-shim.ts 3
294294
extensions/copilot/src/runtime.ts 1
295295
extensions/copilot/src/sdk-loader.ts 2
296-
extensions/copilot/src/tool-bridge.ts 7
296+
extensions/copilot/src/tool-bridge.ts 6
297297
extensions/copilot/src/workspace-bootstrap.ts 1
298298
extensions/crabbox/src/crabbox-worker-inspect.ts 3
299299
extensions/crabbox/src/crabbox-worker-provider.ts 1
@@ -1853,7 +1853,6 @@ src/agents/embedded-agent-runner/run/attempt-normalization.ts 1
18531853
src/agents/embedded-agent-runner/run/attempt-prompt-build.ts 2
18541854
src/agents/embedded-agent-runner/run/attempt-prompt-helpers.ts 4
18551855
src/agents/embedded-agent-runner/run/attempt-prompt-submit.ts 2
1856-
src/agents/embedded-agent-runner/run/attempt-prompt-support.ts 2
18571856
src/agents/embedded-agent-runner/run/attempt-queue-message.ts 7
18581857
src/agents/embedded-agent-runner/run/attempt-session-prepare.ts 4
18591858
src/agents/embedded-agent-runner/run/attempt-sessions-yield.ts 1

‎extensions/codex/src/app-server/thread-prompt.ts‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
buildDelegationGuidanceSection,
3+
buildHarnessVisibleReplyGuidance,
34
buildSkillWorkshopPromptSection,
45
resolveMainSessionDelegationMode,
56
SKILL_WORKSHOP_TOOL_NAME,
@@ -104,23 +105,13 @@ export function buildDeveloperInstructions(
104105
hasSessionsSend,
105106
}).join("\n")
106107
: undefined,
107-
buildVisibleReplyInstruction(params, messageToolAvailable),
108+
buildHarnessVisibleReplyGuidance({
109+
sourceReplyDeliveryMode: params.sourceReplyDeliveryMode,
110+
messageToolAvailable,
111+
}),
108112
TRANSCRIPT_CREDENTIAL_SAFETY_PROMPT,
109113
nativeCommandGuidance,
110114
params.extraSystemPrompt,
111115
];
112116
return sections.filter((section) => typeof section === "string" && section.trim()).join("\n\n");
113117
}
114-
115-
function buildVisibleReplyInstruction(
116-
params: EmbeddedRunAttemptParams,
117-
messageToolAvailable: boolean,
118-
): string {
119-
if (params.sourceReplyDeliveryMode === "message_tool_only" && messageToolAvailable) {
120-
return "Visible source replies are not automatically delivered for this run. Use `message(action=send)` for user-visible source-channel output. For progress, set `final=false`. Set `final=true`, or omit it, for the completed reply to the current source conversation; OpenClaw stops after confirming delivery. Do not repeat visible message content in your final answer.";
121-
}
122-
if (messageToolAvailable) {
123-
return "For the current source conversation, reply normally in your final assistant message; OpenClaw will deliver it through the active source conversation. Use `message` for supported non-text actions in the current conversation, such as reacting to its current message. Reserve other `message` actions for explicit out-of-band sends or media/file delivery. Reactions are not delivered automatically.";
124-
}
125-
return "For the current source conversation, reply normally in your final assistant message; OpenClaw will deliver it through the active source conversation.";
126-
}

‎extensions/copilot/src/attempt-config.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
detectAndLoadAgentHarnessPromptImages,
55
getModelProviderRequestTransport,
66
resolveUserPath,
7-
TRANSCRIPT_CREDENTIAL_SAFETY_PROMPT,
87
} from "openclaw/plugin-sdk/agent-harness-runtime";
98
import { toStringifiedError as toCopilotError } from "openclaw/plugin-sdk/error-runtime";
109
import { readNonEmptyStringPreservingWhitespace as readNonEmptyString } from "openclaw/plugin-sdk/string-coerce-runtime";
@@ -317,29 +316,6 @@ function resolveImageCapabilityModel(params: AttemptParamsLike): { input?: strin
317316
}
318317
return { input: ["image"] };
319318
}
320-
export function createSystemMessageContent(
321-
params: AttemptParamsLike,
322-
workspaceBootstrapInstructions: string | undefined,
323-
): string | undefined {
324-
if (isRawCopilotModelRun(params)) {
325-
return undefined;
326-
}
327-
const sections: string[] = [TRANSCRIPT_CREDENTIAL_SAFETY_PROMPT];
328-
const bootstrap = workspaceBootstrapInstructions?.trim();
329-
if (bootstrap) {
330-
sections.push(bootstrap);
331-
}
332-
const extraSystemPrompt = readNonEmptyString(params.extraSystemPrompt)?.trim();
333-
if (extraSystemPrompt) {
334-
const contextHeader =
335-
params.promptMode === "minimal" ? "## Subagent Context" : "## Conversation Context";
336-
sections.push(`${contextHeader}\n${extraSystemPrompt}`);
337-
}
338-
return sections.length > 0 ? sections.join("\n\n") : undefined;
339-
}
340-
export function isRawCopilotModelRun(params: AttemptParamsLike): boolean {
341-
return params.modelRun === true || params.promptMode === "none";
342-
}
343319
export { readNonEmptyString };
344320
export function readResolvedAttemptPath(value: unknown): string | undefined {
345321
const raw = readNonEmptyString(value)?.trim();

‎extensions/copilot/src/attempt-execution.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Tool as SdkTool } from "@github/copilot-sdk";
21
import type {
32
AgentMessage,
43
AnyAgentTool,
@@ -253,8 +252,10 @@ export async function runCopilotExecution(context: {
253252
frameImageIdentity?: string;
254253
} = { value: 0 };
255254
let codeModeEngaged: boolean | undefined;
255+
let promptToolPolicy:
256+
| Awaited<ReturnType<typeof createToolBridge>>["promptToolPolicy"]
257+
| undefined;
256258
try {
257-
let sdkTools: SdkTool[] = [];
258259
let resultContentSourceByToolName = new Map<
259260
string,
260261
NonNullable<AnyAgentTool["resultContentSource"]>
@@ -299,7 +300,7 @@ export async function runCopilotExecution(context: {
299300
});
300301
cleanupToolBridge = toolBridge.cleanup;
301302
codeModeEngaged = toolBridge.codeModeEngaged;
302-
sdkTools = toolBridge.sdkTools;
303+
promptToolPolicy = toolBridge.promptToolPolicy;
303304
resultContentSourceByToolName = new Map(
304305
toolBridge.sourceTools.flatMap((tool) =>
305306
tool.resultContentSource ? [[tool.name, tool.resultContentSource] as const] : [],
@@ -333,7 +334,7 @@ export async function runCopilotExecution(context: {
333334
operation: deps.operation,
334335
poolAcquire,
335336
ringZeroSystemAgentRun,
336-
sdkTools,
337+
promptToolPolicy,
337338
sessionProvider,
338339
settledToolFinalization,
339340
signal: params.abortSignal,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isRawCopilotModelRun(params: { modelRun?: boolean; promptMode?: string }): boolean {
2+
return params.modelRun === true || params.promptMode === "none";
3+
}

‎extensions/copilot/src/attempt-session-setup.ts‎

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import type { Tool as SdkTool } from "@github/copilot-sdk";
21
import type { AgentMessage } from "openclaw/plugin-sdk/agent-harness-runtime";
32
import {
43
resolveAgentHarnessBeforePromptBuildResult,
54
runAgentHarnessLlmInputHook,
65
} from "openclaw/plugin-sdk/agent-harness-runtime";
7-
import {
8-
createSessionConfig,
9-
createSystemMessageContent,
10-
isRawCopilotModelRun,
11-
type resolvePoolAcquire,
12-
} from "./attempt-config.js";
6+
import { createSessionConfig, type resolvePoolAcquire } from "./attempt-config.js";
7+
import { isRawCopilotModelRun } from "./attempt-mode.js";
138
import { assertCopilotAttemptHostCapabilities } from "./attempt-types.js";
149
import type {
1510
AttemptParamsLike,
1611
CopilotAgentEndHookParams,
1712
CopilotAttemptDeps,
1813
ModelRef,
1914
} from "./attempt-types.js";
15+
import { buildCopilotPromptGuidance } from "./prompt-guidance.js";
2016
import type { ResolvedCopilotProvider } from "./provider-bridge.js";
21-
import { filterCopilotToolsForAllowlist, shouldForceCopilotMessageTool } from "./tool-bridge.js";
17+
import { shouldForceCopilotMessageTool, type createCopilotToolBridge } from "./tool-bridge.js";
2218
import { createCopilotUserInputBridge } from "./user-input-bridge.js";
2319
import { resolveCopilotWorkspaceBootstrapContext } from "./workspace-bootstrap.js";
2420
export async function createCopilotSessionSetup(params: {
@@ -32,7 +28,7 @@ export async function createCopilotSessionSetup(params: {
3228
operation: CopilotAttemptDeps["operation"];
3329
poolAcquire: ReturnType<typeof resolvePoolAcquire>;
3430
ringZeroSystemAgentRun: boolean;
35-
sdkTools: SdkTool[];
31+
promptToolPolicy?: Awaited<ReturnType<typeof createCopilotToolBridge>>["promptToolPolicy"];
3632
sessionProvider: ResolvedCopilotProvider;
3733
settledToolFinalization: boolean;
3834
signal: AbortSignal | undefined;
@@ -48,7 +44,7 @@ export async function createCopilotSessionSetup(params: {
4844
operation,
4945
poolAcquire,
5046
ringZeroSystemAgentRun,
51-
sdkTools,
47+
promptToolPolicy,
5248
sessionProvider,
5349
settledToolFinalization,
5450
signal,
@@ -66,31 +62,42 @@ export async function createCopilotSessionSetup(params: {
6662
warn: (message) => console.warn(message),
6763
})
6864
: { instructions: undefined };
69-
const originalDeveloperInstructions = settledToolFinalization
70-
? ""
71-
: (createSystemMessageContent(input, workspaceBootstrap.instructions) ?? "");
72-
const promptBuild =
73-
settledToolFinalization || isRawCopilotModelRun(input)
74-
? {
75-
prompt: input.prompt,
76-
developerInstructions: originalDeveloperInstructions,
77-
}
78-
: await resolveAgentHarnessBeforePromptBuildResult({
79-
prompt: input.prompt,
80-
developerInstructions: originalDeveloperInstructions,
81-
messages,
82-
ctx: hookContext,
83-
bootstrapContextRunKind: input.bootstrapContextRunKind,
84-
});
65+
const forceToolNames =
66+
ordinaryAttemptInput && shouldForceCopilotMessageTool(ordinaryAttemptInput)
67+
? (["message"] as const)
68+
: undefined;
69+
let promptPolicyResult: ReturnType<NonNullable<typeof promptToolPolicy>["apply"]> | undefined;
70+
let promptBuild: Awaited<ReturnType<typeof resolveAgentHarnessBeforePromptBuildResult>>;
71+
if (settledToolFinalization) {
72+
promptBuild = { prompt: input.prompt, developerInstructions: "" };
73+
} else if (isRawCopilotModelRun(input)) {
74+
promptPolicyResult = promptToolPolicy?.apply();
75+
promptBuild = { prompt: input.prompt, developerInstructions: "" };
76+
} else {
77+
if (!promptToolPolicy) {
78+
throw new Error("Copilot ordinary attempts require a prompt tool policy.");
79+
}
80+
promptBuild = await resolveAgentHarnessBeforePromptBuildResult({
81+
prompt: input.prompt,
82+
developerInstructions: {
83+
build: ({ toolsAllow }) => {
84+
promptPolicyResult = promptToolPolicy.apply({ toolsAllow, forceToolNames });
85+
return buildCopilotPromptGuidance({
86+
attempt: input,
87+
callableToolNames: promptPolicyResult.callableToolNames,
88+
workspaceBootstrapInstructions: workspaceBootstrap.instructions,
89+
});
90+
},
91+
},
92+
messages,
93+
ctx: hookContext,
94+
bootstrapContextRunKind: input.bootstrapContextRunKind,
95+
});
96+
}
8597
const attemptInput =
8698
promptBuild.prompt === input.prompt ? input : { ...input, prompt: promptBuild.prompt };
87-
const promptTools = filterCopilotToolsForAllowlist(
88-
sdkTools,
89-
promptBuild.toolsAllow,
90-
ordinaryAttemptInput && shouldForceCopilotMessageTool(ordinaryAttemptInput)
91-
? { forceToolNames: ["message"] }
92-
: undefined,
93-
);
99+
const promptTools = promptPolicyResult?.tools ?? [];
100+
const finalDeveloperInstructions = promptBuild.developerInstructions;
94101
// Restricted turns may expose native ask_user only when its policy-filtered
95102
// OpenClaw equivalent survived the canonical tool catalog.
96103
const includeAskUser =
@@ -108,9 +115,7 @@ export async function createCopilotSessionSetup(params: {
108115
sessionId: input.sessionId,
109116
provider: modelRef.provider,
110117
model: modelRef.id,
111-
...(promptBuild.developerInstructions
112-
? { systemPrompt: promptBuild.developerInstructions }
113-
: {}),
118+
...(finalDeveloperInstructions ? { systemPrompt: finalDeveloperInstructions } : {}),
114119
prompt: additionalContext ? `${prompt}\n\n${additionalContext}` : prompt,
115120
historyMessages: [],
116121
imagesCount: promptImagesCount,
@@ -133,7 +138,7 @@ export async function createCopilotSessionSetup(params: {
133138
promptTools,
134139
poolAcquire.auth,
135140
sessionProvider,
136-
promptBuild.developerInstructions || undefined,
141+
finalDeveloperInstructions || undefined,
137142
effectiveWorkspaceDir,
138143
effectiveCwd,
139144
userInputBridge?.onUserInputRequest,
@@ -155,7 +160,7 @@ export async function createCopilotSessionSetup(params: {
155160
promptTools,
156161
poolAcquire.auth,
157162
poolAcquire.provider,
158-
promptBuild.developerInstructions || undefined,
163+
finalDeveloperInstructions || undefined,
159164
effectiveWorkspaceDir,
160165
effectiveCwd,
161166
userInputBridge?.onUserInputRequest,

0 commit comments

Comments
 (0)