fix: session_status 'current' resolves to live run session instead of stale sandbox key (#76708) (#76995)

Summary:
- The PR threads a live `runSessionKey` through embedded tool construction, updates `session_status({sessionKey:"current"})` resolution, and adds unit, Telegram QA, workflow, and changelog coverage for #76708.
- Reproducibility: yes. Source inspection shows current main gives `session_status` only the sandbox/requester ... plus PR follow-up describe a focused Telegram Docker scenario that fails pre-fix and passes with this head.

Automerge notes:
- PR branch already contained follow-up commit before automerge: fix: preserve session visibility semantics for runSessionKey (#76708)
- PR branch already contained follow-up commit before automerge: fix: cover Telegram current session status

Validation:
- ClawSweeper review passed for head c3c964ecfd.
- Required merge gates passed before the squash merge.

Prepared head SHA: c3c964ecfd
Review: https://github.com/openclaw/openclaw/pull/76995#issuecomment-4367445187

Co-authored-by: Alex Knight <aknight@atlassian.com>
Co-authored-by: Alex Knight <15041791+amknight@users.noreply.github.com>
This commit is contained in:
Alex Knight
2026-05-04 14:04:43 +10:00
committed by GitHub
parent 02b9dbde39
commit 3f732aee83
12 changed files with 210 additions and 8 deletions

View File

@@ -331,6 +331,7 @@ describe("telegram live qa runtime", () => {
"telegram-tools-compact-command",
"telegram-whoami-command",
"telegram-context-command",
"telegram-current-session-status-tool",
"telegram-mentioned-message-reply",
"telegram-mention-gating",
]);
@@ -340,9 +341,15 @@ describe("telegram live qa runtime", () => {
"telegram-tools-compact-command",
"telegram-whoami-command",
"telegram-context-command",
"telegram-current-session-status-tool",
"telegram-mentioned-message-reply",
"telegram-mention-gating",
]);
expect(
scenarios
.find((scenario) => scenario.id === "telegram-current-session-status-tool")
?.buildRun("sut_bot").expectedTextIncludes,
).toEqual(["QA-TELEGRAM-CURRENT-SESSION-OK", ":telegram:group:"]);
expect(
scenarios
.find((scenario) => scenario.id === "telegram-mentioned-message-reply")

View File

@@ -47,6 +47,7 @@ type TelegramQaScenarioId =
| "telegram-tools-compact-command"
| "telegram-whoami-command"
| "telegram-context-command"
| "telegram-current-session-status-tool"
| "telegram-mentioned-message-reply"
| "telegram-mention-gating";
@@ -208,6 +209,7 @@ type TelegramMessage = {
type TelegramUpdate = {
update_id: number;
edited_message?: TelegramMessage;
message?: TelegramMessage;
};
@@ -270,6 +272,17 @@ const TELEGRAM_QA_SCENARIOS: TelegramQaScenarioDefinition[] = [
expectedTextIncludes: ["/context list", "Inline shortcut"],
}),
},
{
id: "telegram-current-session-status-tool",
title: "Telegram current session_status tool call",
defaultEnabled: false,
timeoutMs: 60_000,
buildRun: (sutUsername) => ({
expectReply: true,
input: `@${sutUsername} Telegram current session_status QA check. Call session_status with sessionKey set to current, then reply with the exact QA marker and resolved session key.`,
expectedTextIncludes: ["QA-TELEGRAM-CURRENT-SESSION-OK", ":telegram:group:"],
}),
},
{
id: "telegram-mentioned-message-reply",
title: "Telegram mentioned message gets a reply",
@@ -471,7 +484,7 @@ function detectMediaKinds(message: TelegramMessage) {
}
function normalizeTelegramObservedMessage(update: TelegramUpdate): TelegramObservedMessage | null {
const message = update.message;
const message = update.message ?? update.edited_message;
if (!message?.from?.id) {
return null;
}
@@ -608,7 +621,7 @@ async function flushTelegramUpdates(token: string) {
{
offset,
timeout: 0,
allowed_updates: ["message"],
allowed_updates: ["message", "edited_message"],
},
15_000,
);
@@ -653,10 +666,12 @@ async function waitForObservedMessage(params: {
observedMessages: TelegramObservedMessage[];
observationScenarioId: string;
observationScenarioTitle: string;
expectedTextIncludes?: string[];
}) {
const startedAt = Date.now();
let offset = params.initialOffset;
let lastPollingError: unknown;
let lastExpectedMismatch: Error | undefined;
while (Date.now() - startedAt < params.timeoutMs) {
const remainingMs = Math.max(
1_000,
@@ -671,7 +686,7 @@ async function waitForObservedMessage(params: {
{
offset,
timeout: timeoutSeconds,
allowed_updates: ["message"],
allowed_updates: ["message", "edited_message"],
},
timeoutSeconds * 1000 + 5_000,
);
@@ -703,10 +718,23 @@ async function waitForObservedMessage(params: {
};
params.observedMessages.push(observedMessage);
if (matchedScenario) {
try {
assertTelegramScenarioReply({
expectedTextIncludes: params.expectedTextIncludes,
message: observedMessage,
});
} catch (error) {
lastExpectedMismatch =
error instanceof Error ? error : new Error(formatErrorMessage(error));
continue;
}
return { message: observedMessage, nextOffset: offset, observedAtMs: batchObservedAtMs };
}
}
}
if (lastExpectedMismatch) {
throw lastExpectedMismatch;
}
const timeoutMessage = `timed out after ${params.timeoutMs}ms waiting for Telegram message`;
if (lastPollingError) {
throw new Error(
@@ -1332,6 +1360,9 @@ export async function runTelegramQaLive(params: {
observedMessages,
observationScenarioId: scenario.id,
observationScenarioTitle: scenario.title,
expectedTextIncludes: scenarioRun.expectReply
? scenarioRun.expectedTextIncludes
: undefined,
predicate: (message) =>
matchesTelegramScenarioReply({
allowAnySutReply: scenarioRun.allowAnySutReply,

View File

@@ -152,6 +152,7 @@ const QA_TOOL_PROGRESS_PROMPT_RE = /tool progress qa check/i;
const QA_GROUP_VISIBLE_REPLY_TOOL_PROMPT_RE = /qa group visible reply tool check/i;
const QA_GROUP_MESSAGE_UNAVAILABLE_FALLBACK_PROMPT_RE =
/qa group message unavailable fallback check/i;
const QA_TELEGRAM_CURRENT_SESSION_STATUS_PROMPT_RE = /telegram current session_status qa check/i;
const QA_SUBAGENT_DIRECT_FALLBACK_PROMPT_RE = /subagent direct fallback qa check/i;
const QA_SUBAGENT_DIRECT_FALLBACK_WORKER_RE = /subagent direct fallback worker/i;
const QA_SUBAGENT_DIRECT_FALLBACK_MARKER = "QA-SUBAGENT-DIRECT-FALLBACK-OK";
@@ -673,6 +674,28 @@ function hasToolErrorOutput(toolJson: Record<string, unknown> | null, toolOutput
return /\b(?:error|failed|failure|not found|no such file|enoent)\b/i.test(toolOutput);
}
function extractSessionStatusSessionKey(
toolJson: Record<string, unknown> | null,
toolOutput: string,
) {
const details = toolJson?.details;
if (details && typeof details === "object") {
const sessionKey = (details as { sessionKey?: unknown }).sessionKey;
if (typeof sessionKey === "string" && sessionKey.trim()) {
return sessionKey.trim();
}
}
const topLevelSessionKey = toolJson?.sessionKey;
if (typeof topLevelSessionKey === "string" && topLevelSessionKey.trim()) {
return topLevelSessionKey.trim();
}
const statusLineSessionKey = /(?:^|\n)[^\n]*Session:\s*([^\s\n]+)/u.exec(toolOutput)?.[1];
if (statusLineSessionKey?.trim()) {
return statusLineSessionKey.trim();
}
return /"sessionKey"\s*:\s*"([^"]+)"/.exec(toolOutput)?.[1]?.trim() ?? "";
}
function isHeartbeatPrompt(text: string) {
const trimmed = text.trim();
if (!trimmed || /remember this fact/i.test(trimmed)) {
@@ -1349,6 +1372,17 @@ async function buildResponsesPayload(
exactMarkerDirective ?? exactReplyDirective ?? "QA-GROUP-FALLBACK-OK",
);
}
if (QA_TELEGRAM_CURRENT_SESSION_STATUS_PROMPT_RE.test(allInputText)) {
if (!toolOutput && hasDeclaredTool(body, "session_status")) {
return buildToolCallEventsWithArgs("session_status", { sessionKey: "current" });
}
const sessionKey = extractSessionStatusSessionKey(toolJson, toolOutput);
return buildAssistantEvents(
sessionKey.includes(":telegram:group:")
? `QA-TELEGRAM-CURRENT-SESSION-OK ${sessionKey}`
: `QA-TELEGRAM-CURRENT-SESSION-BAD ${sessionKey || "missing-session-key"}`,
);
}
if (/\bmarker\b/i.test(allInputText) && exactReplyDirective) {
return buildAssistantEvents(exactReplyDirective);
}