Skip to content

Commit 73ae583

Browse files
authored
refactor(tasks): simplify subagent activity tracking (#122198)
* refactor(tasks): simplify subagent activity tracking * fix(android): single-source subagent activity expiry clock * chore(i18n): refresh native source baseline
1 parent 8c56730 commit 73ae583

6 files changed

Lines changed: 169 additions & 182 deletions

File tree

‎apps/.i18n/native-source.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,15 +1939,15 @@
19391939
},
19401940
{
19411941
"kind": "ui-call",
1942-
"line": 6051,
1942+
"line": 6052,
19431943
"path": "apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt",
19441944
"source": "Timed out waiting for a reply; try again or refresh.",
19451945
"surface": "android",
19461946
"id": "native.android.2274c6848b1c098b"
19471947
},
19481948
{
19491949
"kind": "ui-call",
1950-
"line": 6268,
1950+
"line": 6269,
19511951
"path": "apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt",
19521952
"source": "Timed out confirming the sent message; refresh to check delivery.",
19531953
"surface": "android",

‎apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5906,10 +5906,11 @@ class ChatController internal constructor(
59065906
_subagentActivities.value = _subagentActivities.value + (taskId to activity)
59075907
subagentActivityExpiryJobs.remove(taskId)?.cancel()
59085908
if (!activity.isWorking) {
5909+
val expiresAt = (activity.endedAtMs ?: now) + SUBAGENT_ACTIVITY_RETENTION_MS
5910+
val expiryDelayMs = (expiresAt - now).coerceAtLeast(0L)
59095911
subagentActivityExpiryJobs[taskId] =
59105912
scope.launch {
5911-
val expiresAt = (activity.endedAtMs ?: now) + SUBAGENT_ACTIVITY_RETENTION_MS
5912-
delay((expiresAt - System.currentTimeMillis()).coerceAtLeast(0L))
5913+
delay(expiryDelayMs)
59135914
synchronized(subagentActivityLock) {
59145915
if (_subagentActivities.value[taskId] == activity) {
59155916
_subagentActivities.value = _subagentActivities.value - taskId

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ struct ChatSubagentActivity: Identifiable, Equatable, Sendable {
2525
let status: ChatSubagentActivityStatus
2626
let snippet: String?
2727
let diffStat: ChatToolDiffStat?
28-
let startedAt: Double
2928
let updatedAt: Double
30-
let endedAt: Double?
3129
let terminalObservedAt: Double?
32-
let childSessionKey: String?
3330
let terminalSummary: String?
3431
}
3532

@@ -55,7 +52,7 @@ struct ChatSubagentActivityState: Equatable, Sendable {
5552
} else {
5653
fallbackSnippet ?? previous?.snippet
5754
}
58-
let endedAt = Self.timestampMilliseconds(task.endedat) ?? previous?.endedAt
55+
let endedAt = Self.timestampMilliseconds(task.endedat)
5956
let updatedAt = Self.timestampMilliseconds(task.updatedat)
6057
?? previous?.updatedAt
6158
?? endedAt
@@ -72,14 +69,8 @@ struct ChatSubagentActivityState: Equatable, Sendable {
7269
status: status,
7370
snippet: snippet,
7471
diffStat: Self.diffStat(task.diffstat) ?? previous?.diffStat,
75-
startedAt: Self.timestampMilliseconds(task.startedat)
76-
?? previous?.startedAt
77-
?? Self.timestampMilliseconds(task.createdat)
78-
?? nowMilliseconds,
7972
updatedAt: updatedAt,
80-
endedAt: endedAt,
8173
terminalObservedAt: terminalObservedAt,
82-
childSessionKey: Self.nonBlank(task.childsessionkey) ?? previous?.childSessionKey,
8374
terminalSummary: Self.nonBlank(task.terminalsummary) ?? previous?.terminalSummary)
8475
}
8576

‎src/agents/embedded-agent-live-edit-diff.ts‎

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseStreamingJson } from "@openclaw/ai/internal/runtime";
22
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
3-
import { resolveFileMutationToolName, type FileMutationToolName } from "./tool-mutation-names.js";
3+
import { countStreamingFileMutationLines } from "./file-mutation-args.js";
4+
import { resolveFileMutationToolName } from "./tool-mutation-names.js";
45

56
const LIVE_EDIT_DIFF_MIN_INTERVAL_MS = 250;
67
const LIVE_EDIT_DIFF_MAX_PARTIAL_JSON_CHARS = 1024 * 1024;
@@ -20,63 +21,6 @@ type LiveEditDiffProgress = {
2021
diff: { added: number; removed: number };
2122
};
2223

23-
function countNewlines(value: unknown): number {
24-
if (typeof value !== "string") {
25-
return 0;
26-
}
27-
let count = 0;
28-
for (let index = value.indexOf("\n"); index >= 0; index = value.indexOf("\n", index + 1)) {
29-
count += 1;
30-
}
31-
return count;
32-
}
33-
34-
function countEditLines(args: Record<string, unknown>): { added: number; removed: number } {
35-
const replacements = Array.isArray(args.edits) ? args.edits : [args];
36-
let added = 0;
37-
let removed = 0;
38-
for (const replacement of replacements) {
39-
if (!replacement || typeof replacement !== "object" || Array.isArray(replacement)) {
40-
continue;
41-
}
42-
const record = replacement as Record<string, unknown>;
43-
added += countNewlines(record.newText ?? record.new_string);
44-
removed += countNewlines(record.oldText ?? record.old_string);
45-
}
46-
return { added, removed };
47-
}
48-
49-
function countPatchLines(patch: unknown): { added: number; removed: number } {
50-
if (typeof patch !== "string") {
51-
return { added: 0, removed: 0 };
52-
}
53-
let added = 0;
54-
let removed = 0;
55-
let lineStart = 0;
56-
for (let lineEnd = patch.indexOf("\n"); lineEnd >= 0; lineEnd = patch.indexOf("\n", lineStart)) {
57-
if (patch[lineStart] === "+") {
58-
added += 1;
59-
} else if (patch[lineStart] === "-") {
60-
removed += 1;
61-
}
62-
lineStart = lineEnd + 1;
63-
}
64-
return { added, removed };
65-
}
66-
67-
function countLiveEditDiff(
68-
kind: FileMutationToolName,
69-
args: Record<string, unknown>,
70-
): { added: number; removed: number } {
71-
if (kind === "write") {
72-
return { added: countNewlines(args.content), removed: 0 };
73-
}
74-
if (kind === "edit") {
75-
return countEditLines(args);
76-
}
77-
return countPatchLines(args.input ?? args.patch);
78-
}
79-
8024
function readToolCallBlock(event: Record<string, unknown>): Record<string, unknown> | undefined {
8125
const contentIndex = event.contentIndex;
8226
const partial = event.partial;
@@ -160,7 +104,7 @@ export function updateLiveEditDiffProgress(
160104
// Parsing is the expensive part. Rate-limit it before touching cumulative JSON
161105
// so fragmented large arguments cannot create quadratic work on the event path.
162106
progress.lastCheckedAtMs = now;
163-
const counted = countLiveEditDiff(kind, parseStreamingJson(partialJson));
107+
const counted = countStreamingFileMutationLines(kind, parseStreamingJson(partialJson));
164108
// Streaming parses are best effort. Never move a visible counter backwards if
165109
// an incomplete JSON boundary temporarily exposes less of the same arguments.
166110
progress.added = Math.max(progress.added, counted.added);

‎src/agents/file-mutation-args.ts‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import path from "node:path";
2+
import {
3+
asOptionalObjectRecord,
4+
readStringField,
5+
} from "@openclaw/normalization-core/record-coerce";
6+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
7+
import { extractApplyPatchTargetPaths } from "./apply-patch-paths.js";
8+
import type { FileMutationToolName } from "./tool-mutation-names.js";
9+
10+
type FileMutationLineCount = { added: number; removed: number };
11+
type FileMutationDelta = FileMutationLineCount & { files: string[] };
12+
13+
function readTarget(record: Record<string, unknown>): string | undefined {
14+
const target = normalizeOptionalString(record.path ?? record.file_path ?? record.filePath);
15+
return target ? path.resolve(target) : undefined;
16+
}
17+
18+
function readEdits(args: Record<string, unknown>): Record<string, unknown>[] {
19+
const candidates = Array.isArray(args.edits) ? args.edits : [args];
20+
return candidates.flatMap((candidate) => {
21+
const edit = asOptionalObjectRecord(candidate);
22+
return edit ? [edit] : [];
23+
});
24+
}
25+
26+
function countNewlines(value: unknown): number {
27+
if (typeof value !== "string") {
28+
return 0;
29+
}
30+
let count = 0;
31+
for (let index = value.indexOf("\n"); index >= 0; index = value.indexOf("\n", index + 1)) {
32+
count += 1;
33+
}
34+
return count;
35+
}
36+
37+
/** Counts only newline-terminated content so partial streamed JSON never guesses a line. */
38+
export function countStreamingFileMutationLines(
39+
kind: FileMutationToolName,
40+
args: Record<string, unknown>,
41+
): FileMutationLineCount {
42+
if (kind === "write") {
43+
return { added: countNewlines(readStringField(args, "content")), removed: 0 };
44+
}
45+
if (kind === "edit") {
46+
return readEdits(args).reduce<FileMutationLineCount>(
47+
(total, edit) => ({
48+
added: total.added + countNewlines(edit.newText ?? edit.new_string),
49+
removed: total.removed + countNewlines(edit.oldText ?? edit.old_string),
50+
}),
51+
{ added: 0, removed: 0 },
52+
);
53+
}
54+
const patch = args.input ?? args.patch;
55+
if (typeof patch !== "string") {
56+
return { added: 0, removed: 0 };
57+
}
58+
let added = 0;
59+
let removed = 0;
60+
let lineStart = 0;
61+
for (let lineEnd = patch.indexOf("\n"); lineEnd >= 0; lineEnd = patch.indexOf("\n", lineStart)) {
62+
added += Number(patch[lineStart] === "+");
63+
removed += Number(patch[lineStart] === "-");
64+
lineStart = lineEnd + 1;
65+
}
66+
return { added, removed };
67+
}
68+
69+
function readCodexChangeDelta(args: Record<string, unknown>): FileMutationDelta | undefined {
70+
const files: string[] = [];
71+
let added = 0;
72+
let removed = 0;
73+
for (const candidate of Array.isArray(args.changes) ? args.changes : []) {
74+
const change = asOptionalObjectRecord(candidate);
75+
const target = change ? readTarget(change) : undefined;
76+
if (!change || !target) {
77+
continue;
78+
}
79+
files.push(target);
80+
const stat = asOptionalObjectRecord(change.stat);
81+
added +=
82+
typeof stat?.added === "number" && Number.isFinite(stat.added) ? Math.max(0, stat.added) : 0;
83+
removed +=
84+
typeof stat?.removed === "number" && Number.isFinite(stat.removed)
85+
? Math.max(0, stat.removed)
86+
: 0;
87+
}
88+
return files.length > 0 ? { files, added, removed } : undefined;
89+
}
90+
91+
/** Reads complete tool arguments using task-fold line semantics. */
92+
export function readCompletedFileMutationDelta(
93+
kind: FileMutationToolName,
94+
args: Record<string, unknown>,
95+
): FileMutationDelta | undefined {
96+
if (kind === "apply_patch") {
97+
const patch = readStringField(args, "input");
98+
if (patch === undefined) {
99+
return readCodexChangeDelta(args);
100+
}
101+
const files = extractApplyPatchTargetPaths(args);
102+
if (files.length === 0) {
103+
return undefined;
104+
}
105+
let added = 0;
106+
let removed = 0;
107+
let inBody = false;
108+
for (const line of patch.split(/\r\n|\r|\n/)) {
109+
if (/^\s*\*\*\* (?:Add|Update|Delete) File: /.test(line)) {
110+
inBody = true;
111+
} else if (!/^\s*\*\* /.test(line) && inBody) {
112+
added += Number(line.startsWith("+"));
113+
removed += Number(line.startsWith("-"));
114+
}
115+
}
116+
return { files, added, removed };
117+
}
118+
const target = readTarget(args);
119+
if (!target) {
120+
return undefined;
121+
}
122+
if (kind === "write") {
123+
const content = readStringField(args, "content");
124+
return content === undefined
125+
? undefined
126+
: {
127+
files: [target],
128+
added: content.length === 0 ? 0 : content.split(/\r\n|\r|\n/).length,
129+
removed: 0,
130+
};
131+
}
132+
let added = 0;
133+
let removed = 0;
134+
let hasCompleteEdit = false;
135+
for (const edit of readEdits(args)) {
136+
const oldText =
137+
typeof edit.oldText === "string"
138+
? edit.oldText
139+
: typeof edit.old_string === "string"
140+
? edit.old_string
141+
: undefined;
142+
const newText =
143+
typeof edit.newText === "string"
144+
? edit.newText
145+
: typeof edit.new_string === "string"
146+
? edit.new_string
147+
: undefined;
148+
if (oldText === undefined || newText === undefined) {
149+
continue;
150+
}
151+
hasCompleteEdit = true;
152+
added += newText.length === 0 ? 0 : newText.split(/\r\n|\r|\n/).length;
153+
removed += oldText.length === 0 ? 0 : oldText.split(/\r\n|\r|\n/).length;
154+
}
155+
return hasCompleteEdit ? { files: [target], added, removed } : undefined;
156+
}

0 commit comments

Comments
 (0)