[Bug]: multiple `@dir:` references each expand to the whole packed workspace, duplicating every directory N times in the model context

Author: sxh313Created Sep 23, 2026Updated Sep 23, 2026

Version

main @ c2ad42e3; multimodal/tarko/context-engineer/package.json is 0.3.0.

Issue Description

When a query contains more than one @dir: reference, the expanded context sent to the model contains the content of every referenced directory repeated once per reference.

multimodal/tarko/context-engineer/src/node/context-reference-processor.ts:162-173

typescript
const packResult = await this.workspacePack.packPaths(
  absoluteDirPaths.map((p) => p.absolutePath),
  workspacePath,
);

// Add packed content for each directory reference
for (const { relativePath } of absoluteDirPaths) {
  expandedContents.push(
    `<directory path="${relativePath}">\n${packResult.packedContent}\n</directory>`,
  );
}

WorkspacePack.packPaths() returns one combined document: formatForLLM() (workspace-pack.ts:280-327) walks all the paths it was given and emits one <directory path="…"> block for each of them. The loop above then wraps that whole document once per reference, so packedContent is copied N times, and each <directory path="X"> block holds the content of every other directory too.

Measured with the real code (Node 24, esbuild-bundled driver over a temp workspace containing src/app/a.ts and src/utils/u.ts, 20 lines each):

query                                chars   <directory tags>
"@dir:src/app"                        635       2
"@dir:src/utils"                     659        2
"@dir:src/app @dir:src/utils"        2510       6      <-- two single expansions total 1294
"@dir:src/app @dir:src/app"          1272       4      <-- same reference written twice, packed once, expanded twice

and the first block of the two-reference expansion mentions the other directory's code:

before: first <directory> block contains "app = 1": true, and also "util = 2": true
after:  first <directory> block contains "app = 1": true, and also "util = 2": false

Two single-reference queries behave correctly today, so this is only visible as soon as a user asks for two directories — and then it costs a full extra copy of everything per reference, on the request that carries the largest payload in the product.

How to reach it: ContextReferenceProcessor.processContextualReferences() is called from multimodal/tarko/agent-server/src/api/controllers/queries.ts:51 and :110 (the session query endpoints), and @dir: is parsed by the regex at context-reference-processor.ts:78 (/@(file|dir):([^\s]+)/g), so any prompt containing two @dir: tokens — e.g. Analyse @dir:src/app and @dir:src/utils — triggers it.

Expected behavior: each <directory path="X"> expansion contains only X's packed content, and the total expanded size is the sum of the individual references rather than N × the sum.

Additional context

The minimal fix is to give each reference its own packPaths([absolutePath], workspacePath) call, which keeps the existing single-reference output byte-for-byte identical, keeps the per-reference <directory path="X"> wrapper the current tests assert on, and lets the existing statistics log stay meaningful by summing the per-call stats. The repo already has a test file for exactly this unit (multimodal/tarko/context-engineer/tests/context-reference-processor.test.ts) with packPaths mocked, so a regression test for the multi-reference case is cheap to add.

Related finding I am deliberately not folding in: formatForLLM groups files with file.absolutePath.startsWith(processedPath) (workspace-pack.ts:287) without a separator check, so when one packPaths() call receives paths whose absolute strings share a prefix, /ws/ab/x.txt is also grouped under /ws/a. That is a WorkspacePack bug for direct callers of packPaths(); it deserves its own report, and the per-reference packing above happens to stop this call site from relying on it.

Source: bytedance/UI-TARS-desktop