Skip to content

Commit 50114a7

Browse files
author
easonLiangWorldedtech
committed
Merge branch 'main' into fix/shared-sdk-error-handle-gap
Resolves the NativeToolCallParser conflicts between #695's raw-chunk streaming fixes (index-keyed trackers, nameSeen empty-name recording, start-when-both-known, typed startedId) and this branch's compound-key streaming state (makeStreamingKey, name-locked tracked entries, name-carrying deltas for compound-key routing): - processRawChunk name guard merged to name !== undefined && !tracked.hasStarted: keeps #695's pre-start empty-name recording and this branch's post-start lock that prevents a later chunk from rekeying the tracked entry and orphaning its state. - buffered-delta flush emits id: startedId plus name: tracked.name so the flushed pre-start deltas route through the same compound key Task.ts uses. - spec: union of both test blocks; #695's drives adapted to the compound-key API (makeStreamingKey routing) and to the name-carrying delta/end event shapes; #799's collision and finalizeRawChunks suites retained. Local proofs: tsc --noEmit, eslint --prune-suppressions --max-warnings=0, parser/task/provider suites (197 tests), knip, parser-scope model check, lifecycle:model-check.
2 parents e6ae20e + 2da6ea2 commit 50114a7

53 files changed

Lines changed: 3358 additions & 655 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/__tests__/extension.spec.ts‎

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ vi.mock("vscode", () => ({
1212
tabGroups: {
1313
onDidChangeTabs: vi.fn(),
1414
},
15-
onDidChangeActiveTextEditor: vi.fn(),
15+
onDidChangeActiveTextEditor: vi.fn().mockReturnValue({ dispose: vi.fn() }),
1616
},
1717
workspace: {
1818
registerTextDocumentContentProvider: vi.fn(),
@@ -205,6 +205,7 @@ vi.mock("../core/webview/ClineProvider", async () => {
205205
{
206206
// Static method used by extension.ts
207207
getVisibleInstance: vi.fn().mockReturnValue(mockInstance),
208+
getAllInstances: vi.fn().mockReturnValue([]),
208209
sideBarId: "zoo-code.SidebarProvider",
209210
},
210211
),
@@ -238,6 +239,141 @@ describe("extension.ts", () => {
238239
settingsUpdatedHandler = undefined
239240
})
240241

242+
test("initializes the code index scope and registers it for extension cleanup", async () => {
243+
vi.resetModules()
244+
const { CodeIndexScope } = await import("../services/code-index/code-index-scope")
245+
const init = vi.spyOn(CodeIndexScope.prototype, "init")
246+
const dispose = vi.spyOn(CodeIndexScope.prototype, "dispose")
247+
try {
248+
const { activate } = await import("../extension")
249+
await activate(mockContext)
250+
251+
const scopes = mockContext.subscriptions.filter((entry) => entry instanceof CodeIndexScope)
252+
expect(scopes).toHaveLength(1)
253+
expect(init).toHaveBeenCalledExactlyOnceWith()
254+
expect(init.mock.contexts[0]).toBe(scopes[0])
255+
expect(dispose).not.toHaveBeenCalled()
256+
scopes[0].dispose()
257+
expect(dispose).toHaveBeenCalledExactlyOnceWith()
258+
} finally {
259+
init.mockRestore()
260+
dispose.mockRestore()
261+
}
262+
})
263+
264+
test("publishes indexing status to matching providers and logs delivery failures", async () => {
265+
vi.resetModules()
266+
const { CodeIndexScope } = await import("../services/code-index/code-index-scope")
267+
const { ClineProvider } = await import("../core/webview/ClineProvider")
268+
const log = vi.spyOn(console, "error").mockImplementation(() => {})
269+
const provider = ClineProvider.getVisibleInstance()!
270+
Object.defineProperty(provider, "workspacePath", { configurable: true, value: "/workspace" })
271+
const getAll = vi.mocked(ClineProvider.getAllInstances)
272+
const post = vi.mocked(provider.postMessageToWebview)
273+
getAll.mockReturnValue([provider, provider])
274+
post.mockRejectedValueOnce(new Error("delivery failed")).mockResolvedValue(undefined)
275+
try {
276+
const { activate } = await import("../extension")
277+
await activate(mockContext)
278+
const scope = mockContext.subscriptions.find((entry) => entry instanceof CodeIndexScope)!
279+
const status = {
280+
systemStatus: "Standby" as const,
281+
message: "Ready",
282+
processedItems: 0,
283+
totalItems: 0,
284+
currentItemUnit: "blocks",
285+
workspacePath: "/workspace",
286+
workspaceEnabled: true,
287+
autoEnableDefault: true,
288+
}
289+
scope["statusManager"]!["publishStatus"](status)
290+
await Promise.resolve()
291+
expect(post).toHaveBeenCalledTimes(2)
292+
expect(post).toHaveBeenCalledWith({ type: "indexingStatusUpdate", values: status })
293+
expect(log).toHaveBeenCalledWith(
294+
"[CodeIndexStatusManager] Failed to publish indexing status:",
295+
expect.objectContaining({ message: "delivery failed" }),
296+
)
297+
scope.dispose()
298+
} finally {
299+
log.mockRestore()
300+
getAll.mockReturnValue([])
301+
post.mockReset()
302+
Reflect.deleteProperty(provider, "workspacePath")
303+
}
304+
})
305+
306+
test.each([
307+
{ workspacePath: "/other-workspace", receivesStatus: false },
308+
{ workspacePath: "/workspace", receivesStatus: true },
309+
{ workspacePath: undefined, receivesStatus: true },
310+
{ workspacePath: "", receivesStatus: true },
311+
])(
312+
"routes status for provider workspace=$workspacePath with receivesStatus=$receivesStatus",
313+
async ({ workspacePath, receivesStatus }) => {
314+
vi.resetModules()
315+
const { CodeIndexScope } = await import("../services/code-index/code-index-scope")
316+
const { ClineProvider } = await import("../core/webview/ClineProvider")
317+
const provider = ClineProvider.getVisibleInstance()!
318+
Object.defineProperty(provider, "workspacePath", { configurable: true, value: workspacePath })
319+
const getAll = vi.mocked(ClineProvider.getAllInstances)
320+
getAll.mockReturnValue([provider])
321+
try {
322+
const { activate } = await import("../extension")
323+
await activate(mockContext)
324+
const scope = mockContext.subscriptions.find((entry) => entry instanceof CodeIndexScope)!
325+
const status = {
326+
systemStatus: "Indexing" as const,
327+
message: "Processing confidential.ts",
328+
processedItems: 1,
329+
totalItems: 2,
330+
currentItemUnit: "files",
331+
workspacePath: "/workspace",
332+
workspaceEnabled: true,
333+
autoEnableDefault: true,
334+
}
335+
scope["statusManager"]!["publishStatus"](status)
336+
await Promise.resolve()
337+
if (receivesStatus) {
338+
expect(provider.postMessageToWebview).toHaveBeenCalledExactlyOnceWith({
339+
type: "indexingStatusUpdate",
340+
values: status,
341+
})
342+
} else {
343+
expect(provider.postMessageToWebview).not.toHaveBeenCalled()
344+
}
345+
scope.dispose()
346+
} finally {
347+
getAll.mockReturnValue([])
348+
Reflect.deleteProperty(provider, "workspacePath")
349+
}
350+
},
351+
)
352+
353+
test.each([new Error("scope initialization failed"), "scope initialization failed"])(
354+
"continues activation and logs code index scope initialization failure: %s",
355+
async (error) => {
356+
vi.resetModules()
357+
const { CodeIndexScope } = await import("../services/code-index/code-index-scope")
358+
const init = vi.spyOn(CodeIndexScope.prototype, "init").mockImplementationOnce(() => {
359+
throw error
360+
})
361+
try {
362+
const { activate } = await import("../extension")
363+
await expect(activate(mockContext)).resolves.toBeDefined()
364+
365+
const vscode = await import("vscode")
366+
const channel = vi.mocked(vscode.window.createOutputChannel).mock.results.at(-1)?.value
367+
expect(channel?.appendLine).toHaveBeenCalledWith(
368+
"[CodeIndexScope] Failed to initialize: scope initialization failed",
369+
)
370+
expect(mockContext.subscriptions.filter((entry) => entry instanceof CodeIndexScope)).toHaveLength(1)
371+
} finally {
372+
init.mockRestore()
373+
}
374+
},
375+
)
376+
241377
test("does not call dotenv.config when optional .env does not exist", async () => {
242378
vi.resetModules()
243379
vi.clearAllMocks()

‎src/core/assistant-message/NativeToolCallParser.ts‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ export class NativeToolCallParser {
7676
// Raw chunk tracking state (keyed by index from one API stream)
7777
private static rawChunkTrackersByScope = new WeakMap<
7878
object,
79-
Map<number, { id: string; name: string; hasStarted: boolean; deltaBuffer: string[] }>
79+
Map<
80+
number,
81+
{
82+
id?: string
83+
name: string
84+
// Track whether the provider sent a name, including an empty name.
85+
nameSeen: boolean
86+
hasStarted: boolean
87+
deltaBuffer: string[]
88+
}
89+
>
8090
>()
8191

8292
public static createScope(): object {
@@ -139,52 +149,59 @@ export class NativeToolCallParser {
139149

140150
let tracked = rawChunkTracker.get(index)
141151

142-
// Initialize new tool call tracking when we receive an id
143-
if (id && !tracked) {
152+
// Create the tracker on first sight of this index, independent of whether
153+
// an id has arrived yet. Keying the lifecycle by index (not id) ensures any
154+
// `arguments` that stream before the id is known are buffered rather than dropped.
155+
if (!tracked) {
144156
tracked = {
145157
id,
146158
name: name || "",
159+
nameSeen: name !== undefined,
147160
hasStarted: false,
148161
deltaBuffer: [],
149162
}
150163
rawChunkTracker.set(index, tracked)
151164
}
152165

153-
if (!tracked) {
154-
return events
166+
// Record id and name as they arrive (they may come in separate chunks).
167+
if (id) {
168+
tracked.id = id
155169
}
156170

157171
// Lock the name once the tool call has started: the end events and the
158172
// consumer's compound keys use the start name, so a later chunk carrying a
159173
// different name must not rekey the tracked entry and orphan its state.
160-
if (name && !tracked.hasStarted) {
174+
if (name !== undefined && !tracked.hasStarted) {
161175
tracked.name = name
176+
tracked.nameSeen = true
162177
}
163178

164-
// Emit start event when we have the name
165-
if (!tracked.hasStarted && tracked.name) {
179+
// Emit start event only once both id and name are known. Using a local
180+
// non-null id keeps emitted events typed as id: string.
181+
if (!tracked.hasStarted && tracked.id && tracked.nameSeen) {
182+
const startedId = tracked.id
166183
events.push({
167184
type: "tool_call_start",
168-
id: tracked.id,
185+
id: startedId,
169186
name: tracked.name,
170187
})
171188
tracked.hasStarted = true
172189

173-
// Flush buffered deltas
190+
// Flush buffered deltas accumulated during the pre-start window.
174191
for (const bufferedDelta of tracked.deltaBuffer) {
175192
events.push({
176193
type: "tool_call_delta",
177-
id: tracked.id,
194+
id: startedId,
178195
name: tracked.name,
179196
delta: bufferedDelta,
180197
})
181198
}
182199
tracked.deltaBuffer = []
183200
}
184201

185-
// Emit delta event for argument chunks
202+
// Emit delta event for argument chunks, buffering until start is emitted.
186203
if (args) {
187-
if (tracked.hasStarted) {
204+
if (tracked.hasStarted && tracked.id) {
188205
events.push({
189206
type: "tool_call_delta",
190207
id: tracked.id,
@@ -209,7 +226,7 @@ export class NativeToolCallParser {
209226

210227
if (rawChunkTracker) {
211228
for (const [, tracked] of rawChunkTracker.entries()) {
212-
if (tracked.hasStarted) {
229+
if (tracked.hasStarted && tracked.id) {
213230
events.push({
214231
type: "tool_call_end",
215232
id: tracked.id,

0 commit comments

Comments
 (0)