diff --git a/src/core/environment/__tests__/getEnvironmentDetails.spec.ts b/src/core/environment/__tests__/getEnvironmentDetails.spec.ts index df47e83c21..bede9c912b 100644 --- a/src/core/environment/__tests__/getEnvironmentDetails.spec.ts +++ b/src/core/environment/__tests__/getEnvironmentDetails.spec.ts @@ -464,4 +464,31 @@ describe("getEnvironmentDetails", () => { const result = await getEnvironmentDetails(mockCline as Task, true) expect(result).toContain("File listing unavailable: unexpected string rejection") }) + + it("should surface task nesting depth for a root task", async () => { + mockCline = { ...mockCline, depth: 0 } + + const result = await getEnvironmentDetails(mockCline as Task) + + expect(result).toContain("# Task Context") + expect(result).toContain("Nesting depth: 0 (root = 0)") + expect(result).not.toContain("Parent task:") + }) + + it("should surface nesting depth and parent id for a child task", async () => { + mockCline = { ...mockCline, depth: 2, parentTaskId: "parent-task-id" } + + const result = await getEnvironmentDetails(mockCline as Task) + + expect(result).toContain("# Task Context") + expect(result).toContain("Nesting depth: 2 (root = 0)") + expect(result).toContain("Parent task: parent-task-id") + }) + + it("should omit the task context section when depth is unknown", async () => { + const result = await getEnvironmentDetails(mockCline as Task) + + expect(result).not.toContain("# Task Context") + expect(result).not.toContain("Nesting depth:") + }) }) diff --git a/src/core/environment/getEnvironmentDetails.ts b/src/core/environment/getEnvironmentDetails.ts index 0e7d18a57a..7d5ee59379 100644 --- a/src/core/environment/getEnvironmentDetails.ts +++ b/src/core/environment/getEnvironmentDetails.ts @@ -226,6 +226,18 @@ export async function getEnvironmentDetails(cline: Task, includeFileDetails: boo details += `${modeDetails.name}\n` details += `${modelId}\n` + // Surface this task's position in the delegation tree so a subtask can report + // its own nesting level — depth is never visible to the model anywhere else. + // The parent id is included only for children; resolving the parent's title + // would need a history-store lookup on every environment refresh, which we avoid. + if (typeof cline.depth === "number") { + details += `\n\n# Task Context` + details += `Nesting depth: ${cline.depth} (root = 0)` + if (cline.parentTaskId) { + details += `\nParent task: ${cline.parentTaskId}` + } + } + if (includeFileDetails) { details += `\n\n# Current Workspace Directory (${cline.cwd.toPosix()}) Files\n` const isDesktop = arePathsEqual(cline.cwd, path.join(os.homedir(), "Desktop"))