Skip to content

Commit a9d7ea9

Browse files
steipetexialonglee
andcommitted
fix(ui): correlate question summaries with agent runs
Co-authored-by: Peter Lee <[email protected]>
1 parent 22be93a commit a9d7ea9

24 files changed

Lines changed: 143 additions & 89 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewayProtocol.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ data class QuestionRecord(
101101
val questions: List<Question>,
102102
val agentId: String? = null,
103103
val sessionKey: String? = null,
104+
val runId: String? = null,
104105
val createdAtMs: Long,
105106
val expiresAtMs: Long,
106107
val status: String,

‎apps/shared/OpenClawKit/Sources/OpenClawChatUI/ChatQuestionCard.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public final class OpenClawQuestionCardModel: Identifiable {
5757
questions: record.questions,
5858
agentid: record.agentid,
5959
sessionkey: record.sessionkey,
60+
runid: record.runid,
6061
createdatms: record.createdatms,
6162
expiresatms: record.expiresatms,
6263
status: record.status,
@@ -161,6 +162,7 @@ public final class OpenClawQuestionCardModel: Identifiable {
161162
questions: self.record.questions,
162163
agentid: self.record.agentid,
163164
sessionkey: self.record.sessionkey,
165+
runid: self.record.runid,
164166
createdatms: self.record.createdatms,
165167
expiresatms: self.record.expiresatms,
166168
status: .answered,
@@ -178,6 +180,7 @@ public final class OpenClawQuestionCardModel: Identifiable {
178180
questions: self.record.questions,
179181
agentid: self.record.agentid,
180182
sessionkey: self.record.sessionkey,
183+
runid: self.record.runid,
181184
createdatms: self.record.createdatms,
182185
expiresatms: self.record.expiresatms,
183186
status: .cancelled,
@@ -195,6 +198,7 @@ public final class OpenClawQuestionCardModel: Identifiable {
195198
questions: self.record.questions,
196199
agentid: self.record.agentid,
197200
sessionkey: self.record.sessionkey,
201+
runid: self.record.runid,
198202
createdatms: self.record.createdatms,
199203
expiresatms: self.record.expiresatms,
200204
status: .answered,
@@ -222,6 +226,7 @@ public final class OpenClawQuestionCardModel: Identifiable {
222226
questions: self.record.questions,
223227
agentid: self.record.agentid,
224228
sessionkey: self.record.sessionkey,
229+
runid: self.record.runid,
225230
createdatms: self.record.createdatms,
226231
expiresatms: self.record.expiresatms,
227232
status: resolved.status,

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15439,6 +15439,7 @@ public struct QuestionRecord: Codable, Sendable {
1543915439
public let questions: [Question]
1544015440
public let agentid: String?
1544115441
public let sessionkey: String?
15442+
public let runid: String?
1544215443
public let createdatms: Int
1544315444
public let expiresatms: Int
1544415445
public let status: QuestionStatus
@@ -15450,6 +15451,7 @@ public struct QuestionRecord: Codable, Sendable {
1545015451
questions: [Question],
1545115452
agentid: String? = nil,
1545215453
sessionkey: String? = nil,
15454+
runid: String? = nil,
1545315455
createdatms: Int,
1545415456
expiresatms: Int,
1545515457
status: QuestionStatus,
@@ -15460,6 +15462,7 @@ public struct QuestionRecord: Codable, Sendable {
1546015462
self.questions = questions
1546115463
self.agentid = agentid
1546215464
self.sessionkey = sessionkey
15465+
self.runid = runid
1546315466
self.createdatms = createdatms
1546415467
self.expiresatms = expiresatms
1546515468
self.status = status
@@ -15472,6 +15475,7 @@ public struct QuestionRecord: Codable, Sendable {
1547215475
case questions
1547315476
case agentid = "agentId"
1547415477
case sessionkey = "sessionKey"
15478+
case runid = "runId"
1547515479
case createdatms = "createdAtMs"
1547615480
case expiresatms = "expiresAtMs"
1547715481
case status
@@ -15485,19 +15489,22 @@ public struct QuestionRequestParams: Codable, Sendable {
1548515489
public let questions: [QuestionRequestQuestion]
1548615490
public let agentid: String?
1548715491
public let sessionkey: String?
15492+
public let runid: String?
1548815493
public let timeoutms: Int?
1548915494

1549015495
public init(
1549115496
id: String? = nil,
1549215497
questions: [QuestionRequestQuestion],
1549315498
agentid: String? = nil,
1549415499
sessionkey: String? = nil,
15500+
runid: String? = nil,
1549515501
timeoutms: Int? = nil)
1549615502
{
1549715503
self.id = id
1549815504
self.questions = questions
1549915505
self.agentid = agentid
1550015506
self.sessionkey = sessionkey
15507+
self.runid = runid
1550115508
self.timeoutms = timeoutms
1550215509
}
1550315510

@@ -15506,6 +15513,7 @@ public struct QuestionRequestParams: Codable, Sendable {
1550615513
case questions
1550715514
case agentid = "agentId"
1550815515
case sessionkey = "sessionKey"
15516+
case runid = "runId"
1550915517
case timeoutms = "timeoutMs"
1551015518
}
1551115519
}

‎apps/shared/OpenClawKit/Tests/OpenClawKitTests/ChatQuestionCardTests.swift‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ private func questionRecord(
1010
createdAtMs: Int = 1_000_000,
1111
expiresAtMs: Int = 4_000_000_000_000,
1212
status: QuestionStatus = .pending,
13-
answers: QuestionAnswers? = nil) -> QuestionRecord
13+
answers: QuestionAnswers? = nil,
14+
runId: String? = "run-question") -> QuestionRecord
1415
{
1516
QuestionRecord(
1617
id: "ask_123",
@@ -28,6 +29,7 @@ private func questionRecord(
2829
],
2930
agentid: "main",
3031
sessionkey: "agent:main:main",
32+
runid: runId,
3133
createdatms: createdAtMs,
3234
expiresatms: expiresAtMs,
3335
status: status,
@@ -126,6 +128,29 @@ private func questionRecord(
126128
#expect(model.terminalSummaryText(for: model.record.questions[0]) == "Pizza")
127129
}
128130

131+
@MainActor
132+
@Test func `question card preserves run identity across local terminal records`() {
133+
let answered = OpenClawQuestionCardModel(record: questionRecord())
134+
answered.markAnsweredLocally(answers: ["meal": ["Pizza"]])
135+
#expect(answered.record.runid == "run-question")
136+
137+
let skipped = OpenClawQuestionCardModel(record: questionRecord())
138+
skipped.markSkippedLocally()
139+
#expect(skipped.record.runid == "run-question")
140+
141+
let elsewhere = OpenClawQuestionCardModel(record: questionRecord())
142+
elsewhere.markAnsweredElsewhere()
143+
#expect(elsewhere.record.runid == "run-question")
144+
145+
let resolved = OpenClawQuestionCardModel(record: questionRecord())
146+
resolved.apply(resolved: .init(id: resolved.id, status: .answered))
147+
#expect(resolved.record.runid == "run-question")
148+
149+
let refreshed = OpenClawQuestionCardModel(record: questionRecord(answers: .init(answers: [:])))
150+
#expect(refreshed.apply(record: questionRecord(status: .answered, runId: "run-refresh")))
151+
#expect(refreshed.record.runid == "run-refresh")
152+
}
153+
129154
@MainActor
130155
@Test func `question card locally expired state remains terminal`() {
131156
let expiresAt = Date(timeIntervalSince1970: 1500)

‎docs/tools/ask-user.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ If the question expires or is cancelled before an answer arrives, the tool
5353
returns `status: "no_answer"`. The agent then continues with its best judgment.
5454
An aborted agent run cancels its pending Gateway question.
5555

56+
Gateway question records include the optional originating `runId`. Clients can
57+
use it to keep the prompt and its terminal answer summary with the correct agent
58+
turn, including after reconnecting and recovering the question with
59+
`question.list` or `question.get`.
60+
5661
## Tool schema
5762

5863
```ts

‎extensions/codex/src/app-server/user-input-bridge.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function createCodexUserInputBridge(params: {
140140
questions: requestParams.questions,
141141
sessionKey: params.paramsForRun.sessionKey ?? params.paramsForRun.sessionId,
142142
agentId: params.paramsForRun.agentId,
143+
runId: params.paramsForRun.runId,
143144
timeoutMs:
144145
requestParams.autoResolutionMs ??
145146
params.paramsForRun.timeoutMs ??

‎extensions/copilot/src/user-input-bridge.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function createCopilotUserInputBridge(params: {
4444
questions: [question],
4545
sessionKey: params.paramsForRun.sessionKey ?? params.paramsForRun.sessionId,
4646
agentId: params.paramsForRun.agentId,
47+
runId: params.paramsForRun.runId,
4748
timeoutMs: params.paramsForRun.timeoutMs ?? DEFAULT_USER_INPUT_TIMEOUT_MS,
4849
gatewayCall,
4950
delivery: params.paramsForRun,

‎packages/gateway-protocol/src/questions-validators.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Value } from "typebox/value";
12
import { describe, expect, it } from "vitest";
23
import {
4+
QuestionRecordSchema,
35
validateQuestionRequestParams,
46
validateQuestionResolveParams,
57
validateQuestionWaitAnswerParams,
@@ -21,13 +23,24 @@ describe("question protocol validators", () => {
2123
expect(
2224
validateQuestionRequestParams({
2325
id: "client-question-id",
26+
runId: "agent-run-id",
2427
questions: [question],
2528
timeoutMs: 100,
2629
}),
2730
).toBe(true);
2831
expect(validateQuestionWaitAnswerParams({ id: "question-uuid", timeoutMs: 50 })).toBe(true);
2932
expect(validateQuestionResolveParams({ id: "question-uuid", answers })).toBe(true);
3033
expect(validateQuestionResolveParams({ id: "question-uuid", cancel: true })).toBe(true);
34+
expect(
35+
Value.Check(QuestionRecordSchema, {
36+
id: "client-question-id",
37+
runId: "agent-run-id",
38+
questions: [question],
39+
createdAtMs: 1,
40+
expiresAtMs: 2,
41+
status: "pending",
42+
}),
43+
).toBe(true);
3144
});
3245

3346
it("enforces the shared question header cap", () => {

‎packages/gateway-protocol/src/schema/questions.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const QuestionRecordSchema = closedObject({
5656
questions: Type.Array(QuestionSchema, { minItems: 1, maxItems: 3 }),
5757
agentId: Type.Optional(NonEmptyString),
5858
sessionKey: Type.Optional(NonEmptyString),
59+
runId: Type.Optional(NonEmptyString),
5960
createdAtMs: Type.Integer({ minimum: 0 }),
6061
expiresAtMs: Type.Integer({ minimum: 0 }),
6162
status: QuestionStatusSchema,
@@ -68,6 +69,7 @@ export const QuestionRequestParamsSchema = closedObject({
6869
questions: Type.Array(QuestionRequestQuestionSchema, { minItems: 1, maxItems: 3 }),
6970
agentId: Type.Optional(NonEmptyString),
7071
sessionKey: Type.Optional(NonEmptyString),
72+
runId: Type.Optional(NonEmptyString),
7173
timeoutMs: Type.Optional(Type.Integer({ minimum: 1 })),
7274
});
7375

‎src/agents/harness/gateway-question.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe("gateway harness questions", () => {
116116
const waitError = new Error("gateway disconnected");
117117
const gatewayCall: AgentHarnessQuestionGatewayCall = async (method, _opts, params) => {
118118
if (method === "question.request") {
119+
expect(params).toMatchObject({ runId: "run-delivery" });
119120
return { id: (params as { id: string }).id };
120121
}
121122
if (method === "question.waitAnswer") {
@@ -130,6 +131,7 @@ describe("gateway harness questions", () => {
130131
const run = runAgentHarnessGatewayQuestion({
131132
questions,
132133
sessionKey: "agent:main:delivery",
134+
runId: "run-delivery",
133135
timeoutMs: 60_000,
134136
gatewayCall,
135137
delivery: { onBlockReply },

0 commit comments

Comments
 (0)