Skip to content

Commit 0b1a27a

Browse files
fix(agents): include session identity in runtime prompt
1 parent 0fc5a57 commit 0b1a27a

6 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/agents/embedded-agent-runner/run/attempt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,8 @@ export async function runEmbeddedAttempt(
18001800
workspaceDir: effectiveWorkspace,
18011801
cwd: effectiveCwd,
18021802
runtime: {
1803+
sessionKey: params.sessionKey,
1804+
sessionId: params.sessionId,
18031805
host: machineName,
18041806
os: `${os.type()} ${os.release()}`,
18051807
arch: os.arch(),

src/agents/embedded-agent-runner/system-prompt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export function buildEmbeddedSystemPrompt(params: {
5757
nativeCommandGuidanceLines?: string[];
5858
runtimeInfo: {
5959
agentId?: string;
60+
sessionKey?: string;
61+
sessionId?: string;
6062
host: string;
6163
os: string;
6264
arch: string;

src/agents/system-prompt-params.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function buildParams(params: { config?: OpenClawConfig; workspaceDir?: string; c
3030
});
3131
}
3232

33-
describe("buildSystemPromptParams repo root", () => {
33+
describe("buildSystemPromptParams", () => {
3434
it("detects repo root from workspaceDir", async () => {
3535
const temp = await makeTempDir("workspace");
3636
const repoRoot = path.join(temp, "repo");
@@ -105,4 +105,22 @@ describe("buildSystemPromptParams repo root", () => {
105105

106106
expect(runtimeInfo.repoRoot).toBeUndefined();
107107
});
108+
109+
it("carries session identity into runtime info", () => {
110+
const { runtimeInfo } = buildSystemPromptParams({
111+
agentId: "main",
112+
runtime: {
113+
sessionKey: "agent:main:main",
114+
sessionId: "23ae7fce-3c27-4a51-b58e-d800d8ca091f",
115+
host: "host",
116+
os: "os",
117+
arch: "arch",
118+
node: "node",
119+
model: "model",
120+
},
121+
});
122+
123+
expect(runtimeInfo.sessionKey).toBe("agent:main:main");
124+
expect(runtimeInfo.sessionId).toBe("23ae7fce-3c27-4a51-b58e-d800d8ca091f");
125+
});
108126
});

src/agents/system-prompt-params.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919

2020
type RuntimeInfoInput = {
2121
agentId?: string;
22+
sessionKey?: string;
23+
sessionId?: string;
2224
host: string;
2325
os: string;
2426
arch: string;

src/agents/system-prompt.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,11 +1129,13 @@ describe("buildAgentSystemPrompt", () => {
11291129
expect(prompt).not.toContain("capabilities= InlineButtons ,voice,inlinebuttons,Voice");
11301130
});
11311131

1132-
it("includes agent id in runtime when provided", () => {
1132+
it("includes agent and session identity in runtime when provided", () => {
11331133
const prompt = buildAgentSystemPrompt({
11341134
workspaceDir: "/tmp/openclaw",
11351135
runtimeInfo: {
11361136
agentId: "work",
1137+
sessionKey: "agent:main:main",
1138+
sessionId: "23ae7fce-3c27-4a51-b58e-d800d8ca091f",
11371139
host: "host",
11381140
os: "macOS",
11391141
arch: "arm64",
@@ -1143,6 +1145,8 @@ describe("buildAgentSystemPrompt", () => {
11431145
});
11441146

11451147
expect(prompt).toContain("agent=work");
1148+
expect(prompt).toContain("session=agent:main:main");
1149+
expect(prompt).toContain("sessionId=23ae7fce-3c27-4a51-b58e-d800d8ca091f");
11461150
});
11471151

11481152
it("includes reasoning visibility hint", () => {
@@ -1160,6 +1164,8 @@ describe("buildAgentSystemPrompt", () => {
11601164
const line = buildRuntimeLine(
11611165
{
11621166
agentId: "work",
1167+
sessionKey: "agent:main:subagent:runtime-check",
1168+
sessionId: "23ae7fce-3c27-4a51-b58e-d800d8ca091f",
11631169
host: "host",
11641170
repoRoot: "/repo",
11651171
os: "macOS",
@@ -1174,6 +1180,8 @@ describe("buildAgentSystemPrompt", () => {
11741180
);
11751181

11761182
expect(line).toContain("agent=work");
1183+
expect(line).toContain("session=agent:main:subagent:runtime-check");
1184+
expect(line).toContain("sessionId=23ae7fce-3c27-4a51-b58e-d800d8ca091f");
11771185
expect(line).toContain("host=host");
11781186
expect(line).toContain("repo=/repo");
11791187
expect(line).toContain("os=macOS (arm64)");

src/agents/system-prompt.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ export function buildAgentSystemPrompt(params: {
709709
nativeCommandGuidanceLines?: string[];
710710
runtimeInfo?: {
711711
agentId?: string;
712+
sessionKey?: string;
713+
sessionId?: string;
712714
host?: string;
713715
os?: string;
714716
arch?: string;
@@ -1349,6 +1351,8 @@ function buildActiveProcessSessionReferenceLines(
13491351
export function buildRuntimeLine(
13501352
runtimeInfo?: {
13511353
agentId?: string;
1354+
sessionKey?: string;
1355+
sessionId?: string;
13521356
host?: string;
13531357
os?: string;
13541358
arch?: string;
@@ -1366,6 +1370,8 @@ export function buildRuntimeLine(
13661370
const normalizedRuntimeCapabilities = normalizePromptCapabilityIds(runtimeCapabilities);
13671371
return `Runtime: ${[
13681372
runtimeInfo?.agentId ? `agent=${runtimeInfo.agentId}` : "",
1373+
runtimeInfo?.sessionKey ? `session=${sanitizeForPromptLiteral(runtimeInfo.sessionKey)}` : "",
1374+
runtimeInfo?.sessionId ? `sessionId=${sanitizeForPromptLiteral(runtimeInfo.sessionId)}` : "",
13691375
runtimeInfo?.host ? `host=${runtimeInfo.host}` : "",
13701376
runtimeInfo?.repoRoot ? `repo=${runtimeInfo.repoRoot}` : "",
13711377
runtimeInfo?.os

0 commit comments

Comments
 (0)