* feat: add commit attribution with per-file AI contribution tracking via git notes
Track character-level AI vs human contributions per file and store
detailed attribution metadata as git notes (refs/notes/ai-attribution)
after each successful git commit. This enables open-source AI disclosure
and enterprise compliance audits without polluting commit messages.
* feat: enhance commit attribution with real AI/human ratios and generated file exclusion
- Replace line-based diff with a prefix/suffix character-level algorithm
for precise contribution calculation (e.g. "Esc"→"esc" = 1 char, not whole line)
- Compute real AI vs human contribution percentages at commit time by analyzing
git diff --stat output: humanChars = max(0, diffSize - trackedAiChars)
- Add generated file exclusion (lock files, dist/, .min.js, .d.ts, etc.)
ported from an existing generatedFiles.ts
- Add file deletion tracking via recordDeletion()
- Update git notes payload format: {aiChars, humanChars, percent} per file
with real percentages instead of hardcoded 100%
* feat: add surface tracking, prompt counting, session persistence, and PR attribution
Align with the full attribution feature set:
- Surface tracking: read QWEN_CODE_ENTRYPOINT env var (cli/ide/api/sdk),
include surfaceBreakdown in git notes payload
- Prompt counting: incrementPromptCount() hooked into client.ts message
loop, tracks promptCount/permissionPromptCount/escapeCount
- Session persistence: toSnapshot()/restoreFromSnapshot() for serializing
attribution state; ChatRecordingService.recordAttributionSnapshot()
writes to session JSONL; client.ts restores on session resume
- PR attribution: addAttributionToPR() in shell.ts detects `gh pr create`
and appends "🤖 Generated with Qwen Code (N-shotted by Qwen-Coder)"
- Session baseline: saves content hash on first AI edit of each file
for precise human/AI contribution detection
- generatePRAttribution() method for programmatic access
* fix: audit fixes — initial commit handling, cron prompt exclusion, failed commit counter preservation
- Handle initial commit (no HEAD~1) by detecting parent with rev-parse
and falling back to --root for first commit in repo
- Exclude Cron-triggered messages from promptCount (not user-initiated)
- Add commitSucceeded parameter to clearAttributions() so failed/disabled
commits don't reset the prompts-since-last-commit counter
- Add test for clearAttributions(false) behavior
* fix: cross-platform and correctness fixes from multi-round audit
- Normalize path.relative() to forward slashes for Windows compatibility
- Use diff-tree --root for initial commits (git diff --root is invalid)
- Replace String.replace() with indexOf+slice to avoid $& special patterns
- Fix clearAttributions(false→true) when co-author disabled but commit succeeded
- Use real newlines instead of literal \n in PR attribution text
- Add surface fallback in restoreFromSnapshot for version compatibility
- Fix single-quote regex to not assume bash supports \' escaping
- Case-insensitive directory matching in generated file detection
- Handle renamed file brace notation in parseDiffStat
* fix(attribution): also snapshot on ToolResult turns so resume keeps tool edits
Previously, recordAttributionSnapshot() only ran at the start of UserQuery
and Cron turns — before the tools for that turn had executed. A session
that wrote a file in turn 1 and committed in turn 2 (across process
boundaries via --resume) lost the tracked edit: the last persisted
snapshot was the turn-1-start snapshot (empty fileStates), so on resume
the attribution service restored empty state and no git notes were
attached to the commit.
Move the snapshot call out of the UserQuery/Cron conditional and run it
on every non-Retry turn. ToolResult turns are scheduled right after
tools execute, so their start-of-turn snapshot now captures any edits
those tools made. Retry turns are skipped since the state is unchanged
from the prior turn.
Added unit tests asserting the snapshot fires for ToolResult/UserQuery
turns and skips Retry turns.
Verified end-to-end in a scratch repo: write-file in turn 1 (no commit)
→ exit → --resume → commit in turn 2 → git notes now contain the
recorded file with correct aiChars and promptCount: 2.
* refactor(attribution): merge duplicate retry guard and update stale doc
Collapse the two back-to-back messageType !== Retry blocks in
sendMessageStream into one, and refresh chatRecordingService's
recordAttributionSnapshot doc comment to reflect that snapshots fire
on every non-retry turn (not just after user prompts).
* feat(attribution): split gitCoAuthor into independent commit and pr toggles
Matches the shape used upstream in Claude Code's `attribution.{commit,pr}`
so users can disable the PR body line without losing the commit-message
Co-authored-by trailer (or vice versa). The previous boolean forced both
to move together, which conflated two different surfaces.
- settingsSchema: gitCoAuthor becomes an object with nested commit/pr
booleans, each `showInDialog: true` so both appear in /settings.
- Config constructor accepts legacy boolean (coerced to { commit: v, pr: v })
so stored preferences from the pre-split schema carry over.
- shell.ts: attachCommitAttribution and addCoAuthorToGitCommit read .commit;
addAttributionToPR reads .pr.
* feat(settings): add v3→v4 migration for gitCoAuthor shape change
Legacy gitCoAuthor was a single boolean and shipped ~4 months ago; the
previous commit split it into { commit, pr } sub-toggles. Without a
migration, users who had set gitCoAuthor: false would see the settings
dialog show the default (true) for both sub-toggles — misleading and
likely to flip their preference on the next save because getNestedValue
returns undefined when asked for .commit on a boolean.
- New v3-to-v4 migration expands boolean → { commit: v, pr: v },
preserves already-object values, resets invalid values to {} with a
warning.
- SETTINGS_VERSION bumped 3 → 4; existing integration assertions use the
constant so the next bump is a single-line change.
- Regenerate vscode-ide-companion settings.schema.json to reflect the
new nested shape.
- Docs: split the single gitCoAuthor row into .commit and .pr.
* test(migration): cover null/array/number and partial object for v3-to-v4
The migration already treats any non-boolean, non-object value as invalid
(reset to {} with warning), but the existing test only exercised the
string "yes" branch. Add parameterized cases for null, array, and number
so a future regression that accepts these in the valid bucket gets caught.
Also cover partial objects — the migration must not paternalistically
fill defaults; that responsibility lives in normalizeGitCoAuthor at the
Config boundary.
* fix(shell): address PR review for compound commits and PR body escaping
Two critical issues called out in review:
1. attachCommitAttribution treated the final shell exit code as proof
that `git commit` itself failed. For compound commands like
`git commit -m "x" && npm test`, the commit can succeed and a later
step can fail; the previous code then cleared attribution without
writing the git note. Now we snapshot HEAD before the command (via
`git rev-parse HEAD` through child_process.execFile, kept independent
of the mockable ShellExecutionService) and detect commit creation by
HEAD movement, so attribution lands whenever a new commit was created
regardless of later steps.
2. addAttributionToPR spliced the configured generator name into the
user-approved `gh pr create --body "..."` argument verbatim. A name
containing `"`, `$`, a backtick, or `'` could break the command or be
evaluated as command substitution. Now we shell-escape the appended
text per the surrounding quote style before splicing.
Tests cover the new escape paths for both double- and single-quoted
bodies, including a generator name designed to break interpolation
(`$(rm -rf /) "danger" \`eval\``) and one with an apostrophe.
* fix(attribution): address Copilot review on shell, schema, and totals
Six items called out on PR #3115 by Copilot:
- shell.ts: addAttributionToPR's bash quote escaping doesn't apply to
cmd.exe / PowerShell, where `\$` and `'\''` aren't honored. Skip the
PR body rewrite entirely on Windows — losing PR attribution there is
preferable to corrupting the user-approved `gh pr create` command.
- attributionTrailer.ts + shell.ts call site: buildGitNotesCommand used
bash-style single-quote escaping on the JSON note, which is broken on
Windows. Switched to argv form (`{ command, args }`) and routed the
invocation through child_process.execFile so shell quoting is bypassed
entirely. Tests updated to assert the argv shape.
- commitAttribution.ts: when a tracked file's aiChars exceeded the diff
--stat-derived diffSize (long-line edits where diffSize ≈ lines * 40),
humanChars clamped to 0 but aiChars stayed inflated, leaving aiChars +
humanChars > the committed change magnitude. Clamp aiChars to diffSize
so the totals stay consistent.
- shell.ts parseDiffStat: only normalized rename brace notation
(`{old => new}`). Cross-directory renames emit `old/path => new/path`
without braces, leaving diffSizes keyed by the full string. Added a
second normalization step.
- shell.ts: addAttributionToPR docstring claimed `(X% N-shotted)` but
the implementation only emits `(N-shotted by Generator)`. Updated the
docstring to match the actual behavior.
- settingsSchema.ts + generator: gitCoAuthor went from boolean to object
in the V4 migration. The exported JSON Schema now wraps the field in
`anyOf: [boolean, object]` (via a new `legacyTypes` hint on
SettingDefinition) so users with a stored boolean don't see a spurious
IDE warning before their next launch runs the migration.
* fix(attribution): parse binary diffs, source generator from model, sync schema $version
Three follow-up review items from Copilot:
- parseDiffStat now handles git's binary-diff format (`path | Bin A ->
B bytes`) using the byte delta with a floor of 1. Without this,
binary edits arrived at the attribution payload as diffSize=0 and
were silently dropped. Also extracted the parser to a top-level
exported function so the binary path is unit-testable; added five
targeted cases (text/binary/rename normalisation/summary skip).
- attachCommitAttribution now passes `this.config.getModel()` into
generateNotePayload instead of the user-configurable
`gitCoAuthor.name`. The note's `generator` field reflects which
model produced the changes — and CommitAttributionService's
sanitizeModelName() actually has the codename to scrub now.
- generate-settings-schema.ts imports SETTINGS_VERSION instead of
hardcoding `default: 3`, so a future bump propagates to the emitted
JSON schema in one place. Regenerated settings.schema.json bumps
$version's default from 3 to 4 to match the V4 migration.
* fix(attribution): repo-root baseDir, escape co-author trailer, switch to numstat
Three Critical items called out by wenshao:
- attachCommitAttribution was passing config.getTargetDir() as `baseDir`
to generateNotePayload, but getCommittedFileInfo returns paths
relative to `git rev-parse --show-toplevel`. When the working
directory was a subdirectory of the repo, path.relative produced
`../...` keys that never matched in the AI-attribution lookup,
silently zeroing out attribution for every file outside getTargetDir.
StagedFileInfo now carries an optional `repoRoot` (filled in by
getCommittedFileInfo via `git rev-parse --show-toplevel`) and the
caller prefers it over the target dir.
- addCoAuthorToGitCommit interpolated `gitCoAuthorSettings.name` and
`.email` into the rewritten command without escaping. A name
containing `$()`, backticks, or `"` could be evaluated as command
substitution under double quotes, or break the user-approved
`git commit -m "..."` quoting. Now escapes per the surrounding quote
style with the same helpers addAttributionToPR uses, gates on
non-Windows for the same shell-quoting reason, and fixes the regex
to accept `-m"msg"` shorthand (no space) so users who type the
bash-shorthand form aren't silently denied a trailer.
- parseDiffStat used `git diff --stat` output and approximated each
line as ~40 chars by parsing a graphical text bar. Replaced with
`git diff --numstat` which gives unambiguous integer
additions+deletions per file; the heuristic remains but the parser
is no longer fooled by the visual `++--` markers. Binary entries
fall back to a fixed estimate so they still land in the map (rather
than dropping out as diffSize=0).
Suggestions also addressed: stale duplicate JSDoc on
addCoAuthorToGitCommit removed, misleading `clearAttributions`
comments rewritten to describe what the boolean argument actually
does. Tests cover the new shorthand path, escape behavior, and
numstat parsing (text/binary/rename/malformed).
* fix(shell): shell-aware git-commit detection and apostrophe-escape handling
Two more Critical items called out by wenshao plus the matching Copilot
quote-handling notes:
- attachCommitAttribution and addCoAuthorToGitCommit now go through a
shell-aware `looksLikeGitCommit` helper instead of a raw
`\bgit\s+commit\b` regex. The helper splits the command on shell
separators (`splitCommands`) and checks each segment, so `echo "git
commit"` no longer triggers attribution clearing or trailer
injection. The same helper bails on any segment that contains `cd`
or `git -C <path>`, since either could redirect the commit into a
different repo than our cwd — writing notes or capturing HEAD there
would corrupt unrelated state.
- The post-command attribution call now runs regardless of whether the
shell wrapper aborted. `git commit -m "x" && sleep 999` could move
HEAD and then time out, leaving the new commit without its
attribution note while the stale per-file attribution stayed around
for a later unrelated commit. attachCommitAttribution still gates on
HEAD movement, so it's a no-op when no commit was actually created.
- The `-m '...'` and `--body '...'` regexes used to match only the
first quote segment, so a command like `git commit -m 'don'\''t'`
(bash's standard apostrophe-escape form) would have the trailer
spliced mid-message and break the command's quoting. The single-
quote patterns now use a negative lookahead / inner alternation to
either skip those messages entirely (commit path) or match the
whole escape-aware body (PR path).
Tests cover the new behavior: quoted "git commit" is left alone, the
`cd && git commit` and `git -C` patterns get no trailer, and the
apostrophe-escape form passes through unchanged for both `-m` and
`--body`.
* fix(attribution): drop magic 100 fallback for empty deletions
Deleted files with no AI tracking now use diffSize directly. With
numstat as the input source, diffSize is an exact count, and an
empty-file deletion legitimately reports zero — a magic fallback would
only inflate totals.
* fix(shell): broaden git-commit detection, gate background, drop dead helpers
Five Copilot follow-ups:
- looksLikeGitCommit now strips leading env-var assignments
(`GIT_COMMITTER_DATE=now git commit ...`) and a small allowlist of
safe wrappers (`sudo`, `command`) before matching. The previous
exact-prefix match silently skipped trailer injection on common
real-world commit forms.
- A new looksLikeGhPrCreate (same shell-aware shape) replaces the raw
`\bgh\s+pr\s+create\b` regex in addAttributionToPR, so quoted text
like `echo "gh pr create --body \"x\""` no longer triggers a
command-string rewrite.
- executeBackground refuses to run `git commit` and tells the user to
re-run foreground. The BackgroundShellRegistry lifecycle has no
hook for the post-command pre/post-HEAD comparison or git-notes
write, so allowing the commit through would create the new commit
without notes and leak stale per-file attribution into the next
foreground commit.
- recordDeletion was unused outside its own test — removed (and the
test). When AI-driven deletions need tracking we'll add it with an
actual integration point rather than carrying dead API surface.
- generatePRAttribution was likewise unused; addAttributionToPR
builds the trailer string inline. The two formats had already
diverged. Removed the helper and its tests; reviving from git
history is straightforward if a future caller needs it.
Tests: env-var and sudo prefixes now produce trailers; quoted
"gh pr create" leaves the command unchanged; existing 81 shell tests
still pass alongside the trimmed 25 commitAttribution tests.
* fix(shell): unified git-commit detection split by intent
Six items called out across CodeQL, Copilot, and wenshao:
- The earlier `looksLikeGitCommit`/`stripCommandPrefix` returned a
single yes/no and rejected ANY `cd` in the chain. That fixed the
wrong-repo case but also disabled attribution for `git commit -m
"x" && cd ..` (commit already landed safely in our cwd; the cd
came after). It also conflated three distinct decisions onto one
predicate.
New `gitCommitContext` returns both `hasCommit` and
`attributableInCwd`, walking segments in order so that a `cd`
AFTER the commit doesn't invalidate it. Callers now pick the right
arm:
- background-mode refusal uses `hasCommit` (refuses even
`cd /elsewhere && git commit` since we can't attribute it
afterward either way)
- HEAD snapshot, addCoAuthorToGitCommit, and the
attachCommitAttribution gate use `attributableInCwd`
- Tokenisation switches from a regex while-loop to `shell-quote`'s
`parse`. Quoted env values like `FOO="a b" git commit` now skip
correctly (the old `\S*\s+` form would cut after the opening
quote). Eliminates the CodeQL polynomial-regex alert at the same
time since the `\S*\s+` pattern is gone.
- attachCommitAttribution now snapshots prompt counters via
`clearAttributions(true)` whenever a commit lands, even if no
per-file attributions were tracked. Previously the early-return
on `hasAttributions() === false` meant `promptCountAtLastCommit`
never advanced, so a later `gh pr create` reported an inflated
N-shotted count spanning multiple commits.
Tests: env-var and sudo prefixes still produce trailers; quoted
"git commit" / "gh pr create" leave commands unchanged; cd BEFORE
commit suppresses the rewrite while cd AFTER commit does not; `git
-C <path> commit` is treated as a commit (refused in background)
but not as attributable.
* fix(shell): position-independent git subcommand detection + bash-shell guard
Six review items, two of them critical:
- gitCommitContext was checking fixed-position tokens (`arg1`, `arg3`)
and missed every git invocation that puts a global flag between
`git` and the subcommand: `git -c user.email=x@y commit`,
`git --no-pager commit`, `git -C /p -c k=v commit`, etc. In
background mode these would slip past the refusal guard; in
foreground they got no co-author trailer, no git note, and no
prompt-counter snapshot. New `parseGitInvocation` walks past
git's global flags (with their values) before reading the
subcommand, and reports `changesCwd` for `-C` / `--git-dir` /
`--work-tree`.
- The Windows guard on addCoAuthorToGitCommit and addAttributionToPR
used `os.platform() === 'win32'`, which incorrectly skipped Windows
+ Git Bash (`getShellConfiguration().shell === 'bash'`). Switched
both to gate on `getShellConfiguration().shell !== 'bash'` so Git
Bash users keep the feature.
- attachCommitAttribution was re-parsing `gitCommitContext(command)`
even though `execute()` already gates on `commitCtx.attributableInCwd`.
Removed the redundant re-parse — drift between the two checks would
silently diverge trailer injection from git-notes writes.
- tokeniseSegment (formerly tokeniseProgram) now logs via debugLogger
on parse failure instead of swallowing silently. Easier to debug
if shell-quote ever throws on something unusual.
- Added a comment on `cwdShifted` documenting that it's a one-way
latch — `cd src && cd ..` will still skip attribution. The
trade-off matches the wrong-repo guard's "better miss than corrupt
unrelated repos" intent.
- Stale `--stat` reference in the aiChars-clamp comment updated to
`--numstat` to match the actual git command in
ShellToolInvocation.getCommittedFileInfo.
Tests: `git -c key=val commit` and `git --no-pager commit` now
produce a trailer; existing 82 shell tests still pass.
* fix(shell): refuse multi-commit attribution; misc review follow-ups
Five follow-ups from the latest review pass:
- attachCommitAttribution now refuses to write a single git note for
shell commands that produce more than one commit (e.g.
`git commit -m a && git commit -m b`). The singleton's per-file
attribution map can't be partitioned across the individual commits,
so attaching the combined note to HEAD would mis-attribute earlier
commits' changes to the last one. Walks `preHead..HEAD` via
`git rev-list --count`; on multi-commit detection it snapshots the
prompt counters and bails with a debug warning instead of writing
a misleading note.
- parseGitInvocation now recognises the attached `-C/path` form
(e.g. `git -C/path commit -m x`). shell-quote tokenises that as a
single `-C/path` token which previously fell to the generic flag
branch with `changesCwd = false`, leaving an out-of-cwd commit
classified as attributable.
- attachCommitAttribution dropped its unused `command` parameter
(the caller already gates on `commitCtx.attributableInCwd`, so
re-parsing was removed earlier; the parameter became dead).
- Added wiring guards in edit.test.ts and write-file.test.ts:
AI-originated edits/writes hit `CommitAttributionService.recordEdit`,
`modified_by_user: true` skips, and write-file's distinction
between a true new file and an overwritten empty file (`null` vs
`''` old content) is now pinned by `aiCreated` assertions.
* fix(attribution): partial-commit clear, symlink baseDir, gh/git flag handling
Two Critical items, two Copilot, and five wenshao Suggestions:
- attachCommitAttribution's `finally` block used to call
`clearAttributions()` unconditionally, wiping per-file tracking
for files the AI had edited but the user excluded from this
commit. Added `clearAttributedFiles(committedAbsolutePaths)` to
the service and the call site now passes only the paths that
actually landed in this commit; entries for un-`add`ed files stay
pending for a later commit.
- generateNotePayload now runs both `baseDir` and each tracked
absolute path through `fs.realpathSync` before `path.relative`.
On macOS in particular `/var` symlinks to `/private/var`, so the
toplevel from `git rev-parse --show-toplevel` and the absolute
path captured by edit/write-file tools could diverge — producing
`../../actual/path` keys in the lookup that never matched and
silently zeroed all per-file AI attribution.
- tokeniseSegment now consumes value-taking sudo flags (`-u`,
`-g`, `-h`, `-D`, `-r`, `-t`, `-C`, plus the long forms). Without
this, `sudo -u other git commit` left `other` standing in for
the program name and skipped the trailer entirely.
- A duplicate JSDoc block above `countCommitsAfter` (a leftover
from the earlier extraction of `getGitHead`) was removed; both
helpers now have one accurate comment each.
- attachCommitAttribution's multi-commit guard now also runs when
`preHead === null` (brand-new repo), via `git rev-list --count
HEAD`. A compound `git init && git commit -m a && git commit -m b`
no longer slips through and mis-attributes combined data to the
last commit.
- addCoAuthorToGitCommit's `-m` matching switched to `matchAll` and
takes the LAST match. `git commit -m "title" -m "body"` puts the
trailer at the end of the body so `git interpret-trailers`
recognises it; the previous first-match behaviour stuffed the
trailer in the title where git treats it as plain message text.
- addAttributionToPR's `--body` regex accepts both space and
`=` separators (`--body "..."` and `--body="..."`); the `=` form
is common with gh.
- New `parseGhInvocation` walks past gh's global flags
(`--repo`, `-R`, `--hostname`) so `gh --repo owner/repo pr
create ...` is detected. The earlier fixed-position check at
tokens[1]/tokens[2] missed any command with a global flag.
- getCommittedFileInfo now fans out the two `rev-parse` calls and
the three diff calls with `Promise.all`. They're independent and
serialising them was paying spawn latency 5× per commit.
Tests: sudo with `-u user`, multi `-m`, `gh --repo owner/repo`,
`--body="..."`, plus the existing 84 shell tests still pass.
* fix(attribution): canonicalize file paths centrally in CommitAttributionService
Two related Copilot follow-ups:
- recordEdit/getFileAttribution/clearAttributedFiles now run input
paths through fs.realpathSync before storing/looking up, so a
symlinked path (e.g. macOS /var ↔ /private/var) resolves to the
same key regardless of which form the caller passes. Previously
edit.ts/write-file.ts handed in non-realpath'd absolute paths
while generateNotePayload tried to realpath only inside its
lookup loop, leaving partial-clear and clear-on-finally paths
unable to find entries when the forms diverged.
- restoreFromSnapshot also canonicalises on the way in so a
session resumed from a pre-fix snapshot (where keys may not
have been canonical) ends up with the same shape as newly
recorded entries — otherwise a single file could end up with
two parallel records.
- generateNotePayload's lookup loop dropped its per-entry realpath
call (now redundant since keys are canonical at write time),
keeping only the realpath of `baseDir` (which still comes from
`git rev-parse --show-toplevel` and may be a symlink).
- Updated `clearAttributedFiles` doc to describe the new semantics:
callers can pass either the resolved repo-relative path or an
already-canonical absolute path, and either will match.
* fix(attribution): canonicalize-from-root cleanup; fix mixed-quote -m / gh -R=
Five review items, one Critical:
- attachCommitAttribution now canonicalises via the repo *root* (one
realpath call) and resolves committed paths against that canonical
root, rather than per-leaf realpath inside clearAttributedFiles.
At cleanup time the leaf for a just-deleted file no longer exists,
so per-leaf fs.realpathSync would fail and silently fall back to a
non-canonical path that misses the stored canonical key — leaving
stale attributions for deleted files.
clearAttributedFiles drops its internal realpath and now documents
the canonical-paths-required precondition explicitly.
- addCoAuthorToGitCommit picks the LAST `-m` regardless of quote
style. Previously `doubleMatch ?? singleMatch` always preferred
the last double-quoted match, so `git commit -m "Title" -m
'Body'` injected the trailer into the title where git
interpret-trailers would silently ignore it. Now compares match
indices, and the escape helper follows the actually-selected
match's quote style.
- parseGhInvocation handles `-R=value` (the equals form of the
short `--repo` alias). `--repo=...` and `--hostname=...` were
already covered; `-R=...` previously fell through to the generic
flag branch and skipped the value.
- New tests for the symlink-aware canonicalisation: macOS-style
`/var` ↔ `/private/var` mapping is mocked via vi.mock on
node:fs, with cases for record-then-look-up under either form,
generateNotePayload with a symlinked baseDir, partial clear via
the canonical-root-derived path (deleted leaf), and snapshot
restore canonicalisation.
- Doc-only: integration-test header comments updated from
"V1 -> V2 -> V3" / "migration to V3" to reflect the actual V4
end state (assertions already used the literal `4`).
* fix(shell): scope -m rewrite to commit segment, reject nested matches
Two Critical findings on addCoAuthorToGitCommit, plus a Copilot
maintainability nit:
- The `-m` regex used to scan the whole compound command, so
`git commit -m "fix" && git tag -a v1 -m "release"` would target
the LATER tag annotation (last -m wins) and splice the trailer
there instead of the commit message. The rewrite now scopes to
the actual `git commit` segment via a new
findAttributableCommitSegment(): same shell-aware walk
gitCommitContext does, but returning the segment's character
range so the regex can be run on a slice and spliced back into
the original command.
- Within the segment, a literal `-m '...'` *inside* a quoted body
was treated as a real later -m. For
`git commit -m "docs mention -m 'flag' for completeness"`, the
inner single-quoted -m sits at a higher index than the real
outer -m, and the previous index comparison would have it win —
splicing the trailer mid-message and corrupting the quoting.
The new code checks whether the candidate is nested inside the
other quote-style's range (start/end containment) and prefers
the outer match when so.
- Hoisted three constant Sets (sudo flag list, git global flags
taking values, git global flags shifting cwd, gh global flags)
out of the per-call scope to module constants. Functional
no-op, but keeps the parsing helpers easier to read and avoids
re-allocating the Sets on every command.
Two regression tests added for the cases above:
- inner `-m '...'` inside the outer message body is preserved
literally and the trailer lands after the body
- `git tag -a v1 -m "release notes"` after a real
`git commit -m "fix"` is left untouched, with the trailer
appended to "fix" only
* fix(attribution): cd-leak, numstat partial failure, $() bailout, gh pr new alias
Five Critical/Suggestion items:
- `cd subdir && git commit` (or any non-attributable commit chain
whose HEAD movement still happens in our cwd, e.g. cd into a
subdirectory of the same repo) used to skip attribution AND fail
to clear pending per-file entries. Those entries then leaked into
the next foreground commit, inflating its AI percentage. New
`else if (commitCtx.hasCommit)` branch in execute() compares pre-
and post-HEAD; if HEAD moved we drop the per-file state. preHead
is now snapshotted whenever ANY commit was attempted, not only
attributable ones.
- getCommittedFileInfo's three diff calls run in `Promise.all`. If
`--numstat` failed while `--name-only` succeeded, every file's
diffSize would be 0 and generateNotePayload would clamp aiChars
to 0 — emitting a structurally valid note with all-zero AI
percentages. Detect the partial-failure shape (files non-empty,
diffSizes empty) and return empty so no note is written.
- addCoAuthorToGitCommit and addAttributionToPR now bail when the
captured `-m`/`--body` value contains `$(`. The tool description
recommends `git commit -m "$(cat <<'EOF' ... EOF)"` for
multi-line messages, but the regex's `(?:[^"\\]|\\.)*` body group
stops at the first interior `"` from a nested shell token —
splicing the trailer there breaks the command before it reaches
the executor.
- looksLikeGhPrCreate now accepts `gh pr new` as well — it's a
documented alias for `gh pr create` and was silently skipped.
- Removed `incrementPermissionPromptCount` / `incrementEscapeCount`
and their getters: they had no production callers, so the backing
fields just round-tripped through snapshots as 0. The four
snapshot fields are now optional so pre-fix snapshots that carry
non-zero values still load cleanly and just get ignored.
Three regression tests added: heredoc-style `-m "$(cat <<EOF...)"`
preserved literally, heredoc-style `--body` likewise, `gh pr new
--body "..."` rewritten with attribution.
* fix(attribution): --amend, --message/-b aliases, .d.ts over-exclusion
Four Copilot follow-ups, three of them user-visible coverage gaps:
- `git commit --amend` was diffing `HEAD~1..HEAD` for attribution,
which spans the entire amended commit (parent → amended) rather
than the actual amend delta. A message-only amend would emit a
note attributing every file in the original commit to this
amend. New `isAmendCommit` helper detects the flag and
getCommittedFileInfo switches to `HEAD@{1}..HEAD` (the pre-amend
HEAD vs the amended HEAD); if the reflog is GC'd we bail with a
warning rather than over-attribute.
- `git commit --message "..."` and `--message="..."` were silently
skipped because the regex only recognised the short `-m` form.
The flag prefix now matches both alternatives via
`(?:-[a-zA-Z]*m|--message)\s*=?\s*` (non-capturing inner group
so the existing `[full, prefix, body]` destructure still works).
- `gh pr create -b "..."` (the short alias for `--body`) was the
same gap on the PR side; `(?:--body|-b)[\s=]+` now covers both
forms.
- `.d.ts` was an over-broad blanket exclusion in
EXCLUDED_EXTENSIONS — declaration files are commonly authored
(ambient declarations, asset shims like `*.d.ts` for
`import './x.svg'`); the repo even contains
`packages/vscode-ide-companion/src/assets.d.ts`. Removed `.d.ts`
from the extensions Set and adjusted the test to assert the new
behavior. Auto-generated `.d.ts` (e.g. `tsc --declaration`
output) still gets caught by the build-directory rules.
Tests added: `--amend` plumbing covered by the new branch in
getCommittedFileInfo (no targeted unit test — the diff invocation
goes through ShellExecutionService and is exercised by the existing
post-command path); `--message`/`--message="..."`/-b/-b="..."` all
have positive trailer-injection assertions; `.d.ts` test split into
"hand-authored" (negative) and "in dist" (positive).
* fix(attribution): cd-subdir, scope --body, multi-commit count guard, /clear reset
Four bugs flagged this round:
- gitCommitContext / findAttributableCommitSegment used a blanket
"any cd shifts cwd" gate, breaking the very common
`cd subdir && git commit -m "..."` flow even though the commit
lands in the same repo. New `cdTargetMayChangeRepo` heuristic:
treat relative paths that don't escape upward (no leading `..`,
no absolute path, no `~`/`$VAR` expansion, no bare `cd`/`cd -`)
as in-repo and let attribution proceed. Conservative on anything
it can't statically verify.
- addAttributionToPR was running the `--body`/`-b` regex against
the FULL compound command string. In
`curl -b "session=abc" && gh pr create --body "summary"` the
regex would match curl's `-b` cookie flag and inject attribution
into the cookie value, corrupting the curl call. Added
`findGhPrCreateSegment` (analog of `findAttributableCommitSegment`)
and scoped the body regex to that segment, splicing back into
the original command via offsetting the in-segment match index.
- The multi-commit guard treated `runGitCount === 0` as "single
commit" and bypassed itself. After `commitCreated === true`, a
count of 0 is impossible in normal operation — it means
rev-list errored or timed out. Now we bail on `commitCount !== 1`
with a tailored message: anything other than exactly 1 commit
is suspicious and refuses the note.
- The CommitAttributionService singleton survives across
`Config.startNewSession()` (the `/clear` and resume paths). New
`CommitAttributionService.resetInstance()` call alongside the
existing chat-recording / file-cache resets in startNewSession
prevents pending attributions from a prior session attaching to
a commit in the new one.
Three regression tests added: `cd src && git commit` produces a
trailer (in-repo cd), `cd .. && git commit` does not (could escape
repo root), and `curl -b "..." && gh pr create --body "..."` leaves
curl's cookie value untouched while attribution lands in gh's body.
* fix(attribution): cd embedded .., env wrapper, Windows ARG_MAX, segment-locator warn
Four review items, all small but real:
- cdTargetMayChangeRepo missed embedded `..` traversal — `cd
foo/../../escape` and similar would slip past the leading-`..`
check and be treated as in-repo. Added an `includes('/..')` /
`includes('\\..')` check (catches POSIX and Windows separators
without false-positiving on `..` chars inside ordinary names,
which only escape when followed by a separator).
- tokeniseSegment now recognises `env` as a safe wrapper alongside
`sudo`/`command`, so `env GIT_COMMITTER_DATE=now git commit ...`
resolves to `git`. After the wrapper detection we also skip any
`KEY=VALUE` argv entries (env's own argument syntax for setting
vars before the program).
- buildGitNotesCommand's MAX_NOTE_BYTES dropped from 128 KB to
30 KB. Windows' CreateProcess lpCommandLine is capped around
32,768 UTF-16 chars including the executable path and other argv
entries; a 128 KB note would still fail to spawn even though
the function returned a command instead of null. 30 KB leaves
~2 KB of headroom for the rest of the argv on Windows and is
larger than any real commit's metadata in practice.
- findAttributableCommitSegment / findGhPrCreateSegment now log a
debugLogger.warn when `command.indexOf(sub, cursor)` returns -1
— splitCommands strips line continuations (`\<newline>`), so a
multi-line command can have the trimmed segment text fail to
match its source. Previously the segment was silently skipped
with no signal; the warn makes the failure observable when
QWEN_DEBUG_LOG_FILE is set.
Two regression tests added: `cd foo/../../escape && git commit`
gets no trailer (embedded-`..` heuristic catches it), and
`env GIT_COMMITTER_DATE=now git commit` does (env wrapper skipped).
* fix(attribution): scope isAmendCommit to attributable segment only
`git -C ../other commit --amend && git commit -m x` would previously
flag the second (fresh) commit as an amend, causing
attachCommitAttribution to diff `HEAD@{1}..HEAD` against an unrelated
reflog entry. Mirror findAttributableCommitSegment's cd/cwd tracking
so only the first commit segment that runs in the original cwd
determines amend status.
* fix(attribution): last-match --body, symlink leaf canonicalisation, scoped prompt count
- addAttributionToPR: use matchAll/last-match for `--body`/`-b` so the
trailer lands in the gh-honoured (final) body when multiple flags are
present. Mirrors addCoAuthorToGitCommit. Adds regression test.
- attachCommitAttribution: also fs.realpathSync the per-file resolved
path (not just the repo root) so files behind intermediate symlinks
are matched against canonical keys recordEdit stored, instead of
silently zeroing attribution and leaking entries past commit.
- incrementPromptCount: scope to SendMessageType.UserQuery — ToolResult,
Retry, Hook, Cron, Notification are model/background re-entries of
the same logical turn. Tracking them all inflated the "N-shotted"
trailer (one user message could become 10-shotted with 10 tool calls).
- AttributionSnapshot: add `version: 1` field; restoreFromSnapshot now
refuses incompatible versions and validates per-field types so a
partially-written snapshot can't seed `Math.min(undefined, n) === NaN`
into git-notes payloads.
- Drop unused permission/escape counters (declared, persisted, never
read or incremented) — fields, snapshot tolerance, and clear-method
bookkeeping all removed; AttributionSnapshot interface simplifies.
- isGeneratedFile: switch directory rule from substring `.includes('/dist/')`
to segment-boundary check (split on `/`) so project dirs like
`my-dist/` or `xbuild/` don't match. `.lock` removed from the blanket
extension exclusion — well-known lockfiles already covered by
EXCLUDED_FILENAMES; hand-authored `.lock` files (e.g. `.terraform.lock.hcl`)
now stay attributable.
- getClientSurface: document `QWEN_CODE_ENTRYPOINT` as the embedder
override hook so the always-`'cli'` default is intentional.
* fix(attribution): skip values for env -u NAME and -S string
`env`'s value-taking flags (`-u`/`--unset`, `-S`/`--split-string`) were
not in the wrapper's flag-skip allowlist, so `env -u FOO git commit ...`
left FOO as the next token and the parser treated it as the program —
masking the real `git commit` from attribution detection. Add an
ENV_FLAGS_WITH_VALUE table mirroring the sudo allowlist. Regression
test added.
* fix(attribution): submodule leak, PR body nesting, shallow-clone bail, schema default
- attachCommitAttribution: when HEAD didn't move in our cwd, leave
pending attributions alone instead of dropping them. The case can be
a failed commit, `git reset HEAD~1`, OR `cd submodule && git commit`
(inner repo's HEAD moves, ours doesn't). Dropping was overly
aggressive and silently lost outer-repo edits in the submodule case.
- addAttributionToPR: mirror addCoAuthorToGitCommit's nested-match
rejection so `gh pr create --body "docs mention -b 'flag'"` picks the
outer `--body`, not the inner literal `-b`. Splicing into the inner
match would corrupt the body. Regression test added.
- getCommittedFileInfo: when `rev-parse --verify HEAD~1` fails, also
check `rev-list --count HEAD === 1` to confirm HEAD is the true
root commit. In a shallow clone, HEAD~1 is unreadable but the commit
has a parent recorded — falling back to `diff-tree --root` would
diff against the empty tree and over-attribute the entire commit.
Bail with a debug warning instead.
- generate-settings-schema: lift `default` (and `description`) out of
the inner `anyOf[N]` schema to the outer level when wrapping with
`legacyTypes`. Most JSON-schema-driven editors only surface
top-level defaults; burying the default under `anyOf` lost the
"enabled by default" hint. Also extend the default filter to
publish non-empty plain objects (so `gitCoAuthor`'s default can
appear). gitCoAuthor's source default updated to the runtime shape
`{commit: true, pr: true}` to match `normalizeGitCoAuthor`.
* fix(attribution): drop unsafe full-clear, tag analysis-failure with null
ju1p (Copilot): the `else if (commitCtx.hasCommit)` branch fully
cleared the singleton on `cd /abs/same-repo/subdir && git commit`
(or `git -C . commit`), losing pending AI edits the user hadn't
staged. We can't tell which files were in the commit from this
branch, and the next attributable commit's partial-clear handles
cleanup correctly anyway. Drop the branch entirely.
ju2D (Copilot): `getCommittedFileInfo` returned the same empty
StagedFileInfo for both "could not analyze" (shallow clone, --amend
without reflog, --numstat partial failure, exception) and
"intentionally empty" (--allow-empty). The caller couldn't tell them
apart, so the partial clear became a no-op on analysis failure and
the just-committed AI edits leaked to the next commit. Switch the
return type to `StagedFileInfo | null` and have the caller treat
null as "fall back to full clear" while empty StagedFileInfo
(--allow-empty) leaves attributions intact for the next real commit.
* fix(attribution): dedup snapshot writes, cap excludedGenerated, doc commit toggle scope
rsf- (Copilot): recordAttributionSnapshot wrote a full snapshot to
the JSONL on every non-retry turn, even when the tracked state was
unchanged. Long-running sessions accumulated thousands of identical
snapshot copies, inflating session size and slowing /resume hydrate.
Dedup by JSON-equality with the prior write — first write always
goes through, identical successors are no-ops.
rsgo (Copilot): excludedGenerated path list was unbounded. A commit
churning thousands of generated artifacts (large dist/ rebuild)
could push the JSON note past MAX_NOTE_BYTES (30KB) and lose
attribution for the real source files in the same commit. Cap the
serialized sample at MAX_EXCLUDED_GENERATED_SAMPLE (50) and add
excludedGeneratedCount for the true total.
rsg9 + rshM (Copilot): the gitCoAuthor.commit description claimed
the toggle only controlled the Co-authored-by trailer, but
attachCommitAttribution also gates the per-file git-notes payload
on the same flag. Update both the schema description and the
settings.md table to mention both effects so disabling the option
isn't a silent surprise.
* fix(attribution): depth-1 shallow detection, snapshot dedup post-rewind/post-failure
sfGz (Copilot): rev-list --count HEAD === 1 cannot distinguish a
true root commit from a depth-1 shallow clone — both report 1
because rev-list only walks locally available objects. Switch to
git log -1 --pretty=%P HEAD which reads the parent SHA directly
from commit metadata: empty means a real root, non-empty means a
parent is recorded (whether or not its object is local). The
shallow-clone bail is now reliable.
sfIm (Copilot): the dedup key persisted across rewindRecording, so
the previous snapshot living on the now-abandoned branch would
match the next post-rewind snapshot and silently skip the write,
leaving /resume on the rewound session with no attribution state.
Reset lastAttributionSnapshotJson when rewindRecording fires.
sfJE (Copilot): dedup key was committed before the async write
settled. A transient write failure would update the key, then
permanently suppress all future identical snapshots even though
nothing was ever persisted. Switch to optimistic-set then rollback
on appendRecord rejection — synchronous identical calls dedup
cleanly, but a failed write clears the key so the next identical
snapshot retries. appendRecord now returns the per-record write
promise (writeChain still has its swallow-catch for chain liveness)
so callers needing per-write success can react to it. Tests added
in chatRecordingService.test.ts for both rewind-reset and
rollback-on-failure paths.
* fix(attribution): preHead race, regex apostrophe-escape, surface failures, dead code
t2G0 (deepseek-v4-pro): addCoAuthorToGitCommit single-quote regex now
matches the bash close-escape-reopen apostrophe form using
((?:[^']|'\\'')*) — the same pattern bodySinglePattern uses for
gh pr create. Input like git commit -m 'don'\''t' was previously
silently un-rewritten because the negative lookahead bailed; the
trailer now lands at the FINAL closing quote. Test updated.
tMBP (gpt-5.5): preHead capture switched from concurrent async
getGitHead to a synchronous getGitHeadSync (execFileSync) BEFORE
ShellExecutionService.execute spawns the user's command. A fast
hot-cached git commit could move HEAD before the async rev-parse
resolved, leaving preHead === postHead and silently skipping the
attribution note. Trade ~10–50 ms event-loop block per
commit-shaped command for correctness of the post-command HEAD
comparison.
t2Gv (deepseek-v4-pro): attribution write failures (note exec
non-zero, payload too large, diff-analysis exception, shallow
clone / amend-without-reflog) are now surfaced on the shell tool's
returnDisplay AND llmContent so the user and agent both see when
their commit succeeded but the per-file git note didn't land.
attachCommitAttribution now returns string | null (warning text or
null for intentional skips like no-tracked-edits). Co-authored-by
trailer is unaffected — only the note is gated by these failures.
t2Gy (deepseek-v4-pro): committedAbsolutePaths now matches against
the canonical keys already stored in fileAttributions
(matchCommittedFiles iterates by relative path against the
canonical repo root) instead of re-resolving each diff path
on the fly. realpathSync(resolved) failed for deleted files and
didn't follow intermediate symlinks, leaving stale per-file
attribution alive past commit and inflating AI percentages on
subsequent commits.
t2HI (deepseek-v4-pro): removed dead sessionBaselines /
FileBaseline / contentHash / computeContentHash infrastructure
(~40 lines). The fields were written, persisted, and restored but
never read for any computation or decision. AttributionSnapshot
schema stays at version 1 — restore tolerates pre-fix snapshots
that carried the now-ignored baselines field.
t2HM (deepseek-v4-pro): extracted the duplicated lastMatch helper
in addCoAuthorToGitCommit and addAttributionToPR into a single
module-level lastMatchOf so future fixes can't be applied to only
one copy.
* chore(schema): regenerate settings.schema.json to match gitCoAuthor.commit description
The settingsSchema.ts source for `gitCoAuthor.commit.description` was
updated in 3c0e3293b but the JSON schema only picked up the OUTER
description rewrite and missed this inner property's. The Lint check
("Check settings schema is up-to-date") fails on that drift; this
commit re-runs `npm run generate:settings-schema` to sync them.
* fix(attribution): preserve unstaged AI edits across cleanup branches
uxU5 + uxVQ + uxUO (Copilot): every cleanup branch in
attachCommitAttribution that called clearAttributions(true) was
wholesale-erasing pending AI edits for files the user never staged
in this commit. Reviewer scenarios:
- multi-commit chain (`commit a && commit b`) bails out without
writing a note, but unstaged edits to file Z (touched by neither
commit) get cleared along with the chain's committed files.
- attribution toggle off: same — toggling the flag wipes pending
unstaged work.
- analysis failure (shallow clone, --amend without reflog, partial
diff failure): the finally-block fallback wholesale-cleared
every pending file, consuming unrelated AI edits.
- 0%-AI commit: when no file in the commit was AI-touched,
generateNotePayload was emitting an "0% AI" note attached to a
commit that legitimately had no AI involvement — actively
misleading metadata.
Add `noteCommitWithoutClearing()` to the service: snapshots the
prompt counter as the new "at last commit" but leaves the per-file
map alone. Use it in the multi-commit, no-tracked-edits,
toggle-off, and analysis-failure paths. The committed-files
partial-clear (clearAttributedFiles) still runs in the success
path. The 0%-AI no-match case now skips the note write entirely.
* fix(attribution): runGit null-on-failure, versionless v3→v4 migration
z54M (Copilot): runGit returned '' on both successful-empty-output
and silent failure, so a `--name-only` that errored mid-way through
the diff fan-out aliased to a real `--allow-empty` commit. The
empty-commit branch then preserved pending attributions, leaving
the just-committed file's tracked AI edit alive to re-attribute on
the next commit. Switch runGit to `Promise<string | null>`,
distinguishing exit code 0 (any output, including '') from non-zero
(null). The diff-stage fan-out and ancillary probes now treat null
as analysis failure and bail with `return null` instead of falling
into the empty-commit path.
z539 (Copilot): the v3→v4 `shouldMigrate` only fired on
`$version === 3`. A versionless settings file carrying the legacy
`general.gitCoAuthor: false` boolean would skip every migration
(gitCoAuthor isn't in V1_INDICATOR_KEYS — it post-dates V2), get
its `$version` normalized to 4 by the loader, and leave the
boolean in place. The settings dialog then reads the V4
`{commit, pr}` shape, sees missing keys, defaults both to true, and
silently overwrites the user's opt-out on the next save. Also fire
when `$version` is absent AND the value at `general.gitCoAuthor`
is a boolean. Tests cover the new path and confirm the existing
versioned/object-shape paths are untouched.
* fix(attribution): toggle-off partial clear, normalizeGitCoAuthor type-check, terraform lockfile
0oAK (Copilot): the gitCoAuthor.commit toggle-off branch returned
before computing the committed file set, leaving the just-committed
files' tracked AI work in the singleton. Re-enabling the toggle and
committing the same file again would re-attribute earlier (already-
committed) AI edits to the new commit. Move the toggle gate AFTER
matchCommittedFiles so the finally block does a proper partial clear
of the just-committed files even when the note write is skipped.
0oAg (Copilot): normalizeGitCoAuthor copied value?.commit / value?.pr
without type-checking. settings.json is hand-editable; a stored
`{ commit: "false" }` reached runtime as a truthy string and behaved
as if attribution were enabled. Add a per-field bool coercion that
falls back to the schema default (true) for any non-boolean,
matching what the dialog and IDE schema already imply. Tests cover
the string / number / null cases.
0oAo (Copilot): v3→v4 shouldMigrate only special-cased versionless
legacy booleans — versionless files with invalid gitCoAuthor values
(`"off"`, `[]`, etc.) skipped the migration and the loader stamped
`$version: 4` over the bad value. Runtime normalization then
silently re-enabled attribution. Extend shouldMigrate to fire on ANY
versionless non-object value at general.gitCoAuthor; the existing
migrate() body's drop-and-warn path resets it. Already-object
shapes (hand-edited to v4) still skip cleanly. Tests added.
0oAt (Copilot): `.terraform.lock.hcl` got dropped from generated-file
exclusion when `.lock` was removed from the blanket extension list
in 3c0e3293b. It's a generated provider lockfile in the same class
as `package-lock.json` and dominates Terraform-repo commits. Re-add
to EXCLUDED_FILENAMES and add a regression test covering both
repo-root and module-nested locations.
* fix(attribution): harden restoreFromSnapshot against corrupt payloads
1KMY (Copilot): snapshot.surface was copied without type validation.
A corrupted/partially-written snapshot with a non-string surface
(e.g. {}, 42, null) would later be serialized into the git note as
"[object Object]" and used as a Map key downstream, breaking the
expected payload shape. Type-check and fall back to the current
client surface for any non-string (or empty-string) value.
1KLq (Copilot): per-field sanitiseCount enforced
`promptCount >= 0` and `promptCountAtLastCommit >= 0` independently,
but never the cross-field invariant. A snapshot with
promptCountAtLastCommit > promptCount would surface a negative
getPromptsSinceLastCommit() and propagate as a "(-N)-shotted"
trailer into PR text. Clamp atLastCommit to total on restore.
1KL_ (Copilot): when a snapshot carried both the symlinked and
canonical paths for the same file (a session straddling the
canonicalisation fix), `set(realpathOrSelf(k), ...)` overwrote the
first entry with the second, silently dropping the AI contribution
the first form had accumulated. Merge instead: sum aiContribution
and OR aiCreated when collapsing duplicate keys.
Tests cover all three branches: non-string surface fallback,
promptCount clamp, and duplicate-key merge.
* fix(attribution): roll back snapshot dedup key on sync appendRecord failure
1UMh (Copilot): appendRecord can throw synchronously before returning
a promise — e.g. when ensureConversationFile() rethrows a non-EEXIST
writeFileSync error. The async .catch() handler attached to the
promise never runs in that case, so the optimistic dedup-key set
sticks on a write that never landed and permanently suppresses
identical retries. Roll back lastAttributionSnapshotJson in the outer
catch too. Regression test forces writeFileSync to throw EACCES on
the first invocation, then asserts the second identical snapshot
attempt fires a fresh write rather than getting deduped.
* docs(attribution): align cleanup-branch comments with noteCommitWithoutClearing
Three doc/test-fixture stale-after-refactor cleanups (Copilot
4MDx / 4MEI / 4MEa):
- shell.ts:1944 (around the stagedInfo === null branch): the comment
still claimed the finally block "falls back to a full clear", but
1ece87438 switched analysis-failure cleanup to
noteCommitWithoutClearing(). Update the comment so the reasoning
matches what the code actually does (and so a future reader doesn't
reintroduce the wholesale clear thinking it's already there).
- shell.ts: getCommittedFileInfo docstring carried the same stale
"full clear" claim for the `null` return value. Update to describe
the noteCommitWithoutClearing() fallback and the smaller-evil
trade-off for the just-committed file.
- chatRecordingService.test.ts: baseSnapshot fixture for the
recordAttributionSnapshot tests still carried `baselines: {}`,
even though that field was removed from AttributionSnapshot in
296fb55ae's dead-code purge. Structural typing let it compile,
but the fixture didn't reflect the production shape — drop it.
* fix(attribution): restore fire-and-forget appendRecord, route rollback via callback
6OcJ (Copilot): refactor in 715c258fb returned a Promise from
appendRecord so the snapshot dedup-key path could chain rollback —
but recordUserMessage / recordAssistantTurn / recordAtCommand /
recordSlashCommand / rewindRecording all call appendRecord without
await or .catch(). A transient jsonl.writeLine rejection on any of
those would surface as an unhandled-promise-rejection (warning, or
crash on --unhandled-rejections=throw).
Restore the original fire-and-forget semantics: appendRecord again
returns void and internally swallows async failures (logging via
debugLogger). Per-record failure reactions are routed through an
optional onError callback — recordAttributionSnapshot uses this to
roll back lastAttributionSnapshotJson when the write that set it
ends up rejecting.
Tests: add a fire-and-forget regression that mocks writeLine to
reject and asserts no unhandledRejection events fire while the
existing snapshot rollback tests (sync + async) still pass via the
new callback path.
* fix(attribution): GIT_DIR repo-shift bail, snapshot envelope validation, narrow legacyTypes
80ME (gpt-5.5 /review, [Critical]): tokeniseSegment unconditionally
stripped every leading KEY=value token. `GIT_DIR=elsewhere/.git git
commit ...` was therefore treated as an in-cwd commit, picked up the
Co-authored-by trailer, and produced a per-file note that landed
against our cwd's HEAD even though the actual commit went to a
different repo. Define a GIT_ENV_SHIFTS_REPO set (GIT_DIR,
GIT_WORK_TREE, GIT_COMMON_DIR, GIT_INDEX_FILE, GIT_NAMESPACE) and
have tokeniseSegment refuse to parse any segment whose leading env
block (including the env-wrapper's KEY=VALUE block) carries one of
these. Identity / date variables (GIT_AUTHOR_*, GIT_COMMITTER_*) are
deliberately NOT in the set — they tweak metadata but don't relocate
the repo. Tests cover plain prefix, env-wrapped prefix, and a
GIT_COMMITTER_DATE positive control that should still get the trailer.
8EeQ (Copilot): restoreFromSnapshot received `snapshot as
AttributionSnapshot` from a structural cast off `unknown` (the
resume path), so its TS-typed shape was only a hint. A corrupted
JSONL line (non-object / array / wrong type discriminator / missing
type) would skip past the version check straight into
Object.entries(snapshot.fileStates) — and a non-object fileStates
(an array, say) seeded fileAttributions with numeric-string keys.
Add envelope-level shape gates (isPlainObject + type discriminator)
and a fileStates plain-object check before iterating; both bail to a
clean reset rather than poisoning the singleton. Tests added.
8Eej (Copilot): SettingDefinition.legacyTypes was typed as
SettingsType[] which includes 'enum' and 'object' — JSON Schema's
`type` keyword doesn't accept those values. Adding
`legacyTypes: ['enum']` would silently produce an invalid
settings.schema.json. Narrow the field's type to
ReadonlyArray<'boolean' | 'string' | 'number' | 'array'> (the
JSON-Schema-primitive subset). Future complex-shape legacy support
should land its own branch in convertSettingToJsonSchema.
* docs(attribution): correct legacyTypes / EXCLUDED_DIRECTORY_SEGMENTS comments
9Ta_ (Copilot): the JSDoc on legacyTypes claimed JSON Schema's
`type` keyword does not accept `'object'` — that's wrong; `'object'`
IS a valid JSON Schema type. Reword to reflect the actual rationale:
`'enum'` is not a valid JSON Schema `type` value at all (enum
constraints use the `enum` keyword), and a bare `{type: 'object'}`
would accept any object regardless of what the field's pre-expansion
shape actually allowed. The narrowed `boolean | string | number |
array` set is exactly what the one-liner generator can faithfully
emit; richer legacy shapes belong in their own branch of
convertSettingToJsonSchema.
9Tbs (Copilot): the comment in generatedFiles.ts referenced
`EXCLUDED_DIRECTORIES`, but the constant is `EXCLUDED_DIRECTORY_SEGMENTS`
(renamed during the segment-boundary refactor). Update the
reference so a future maintainer scanning for the rule doesn't
chase a non-existent identifier.
* fix(attribution): SHA-pin git notes, on-disk hash divergence detection, env -C cwd-shift
tanzhenxin review #1 — Note targets symbolic HEAD, not captured SHA:
buildGitNotesCommand hard-coded 'HEAD' as the target; postHead was
captured at commit-detection time but only used for the !== preHead
diff. Between that capture and the execFile, three more awaited git
calls run — anything that moves HEAD in the same cwd (post-commit
hook, chained `commit && tag -m`, parallel process) silently lands
the note on the wrong commit because of `-f`. Thread postHead
through buildGitNotesCommand as a required `targetCommit` arg.
Test asserts the targeted SHA, not the symbolic ref.
tanzhenxin review #2 — Accumulator has no baseline:
recordEdit was monotonic per-path with no reset for out-of-band
mutations. Re-instate FileAttribution.contentHash and:
- recordEdit hashes the input `oldContent` and resets the per-file
accumulator if it doesn't match what AI's last write recorded
(catches paste-replace via external editor, manual save, etc.
WHEN AI subsequently edits the same file again).
- New validateOnDiskHashes() rehashes every tracked file's CURRENT
on-disk content and drops entries whose hash diverged. Called
from attachCommitAttribution before matchCommittedFiles so a
commit can never credit AI for a human-only diff. Deleted files
(readFileSync throws) are left alone — the commit's deletion
record is what the note should reflect.
tanzhenxin review #4 — Failed-commit / staleness leak:
The recordEdit divergence check above + commit-time
validateOnDiskHashes together catch tanzhenxin's exact scenario
(AI edits a.ts → hook rejects → user manually edits a.ts → user
commits → no AI credit because validateOnDiskHashes drops the
stale entry). The !commitCreated branch still preserves
attributions to keep the submodule case working — the staleness
problem is now solved at the next commit's validation step.
Self-review item — env -C / --chdir treated as repo-shifting:
Added ENV_FLAGS_SHIFT_CWD set covering -C / --chdir. tokeniseSegment
returns null for `env -C DIR git commit ...` segments — same
contract as a leading GIT_DIR=... assignment. Without this we'd
either misidentify /elsewhere as the program (silently dropping
attribution) or, worse if -C went into the value-skip set,
trailer-inject onto a commit that lands in /elsewhere's repo. Tests
added alongside the existing GIT_DIR repo-shift cases.
339 tests pass; typecheck clean.
* fix(attribution): pickBool intent-aware, shouldClear gate, ETIMEDOUT surface, drop dead exports
-wgA + -wg0 (deepseek): pickBool defaulted non-boolean to true,
turning a hand-edited `{ commit: "false" }` into enabled
attribution. Replace with intent-aware parsing: "true"/"yes"/"on"/
"1" → true, "false"/"no"/"off"/"0"/"" → false, anything else
(unknown strings, non-1 numbers, objects, arrays, null) → false.
Genuinely-absent sub-fields still default to true (schema default).
Migration test scenarios covered. Tests now cover ~17 input cases
across both string/number/null/object/unknown forms.
-wgq (deepseek): when buildGitNotesCommand returned null (oversized
payload) or git notes itself failed, the finally block called
clearAttributedFiles(committedAbsolutePaths) — irreversibly
deleting per-file attribution data the user might need to amend &
retry. Introduce a separate `shouldClear` set that's only assigned
on successful note write OR explicit toggle-off. Failure paths
(oversized, exitCode != 0, exception, analysis failure) leave
shouldClear null so the finally block calls noteCommitWithoutClearing
instead — preserving per-file state for the user's recovery.
9p7W (Copilot): execFile callback coerced ETIMEDOUT / SIGTERM
(timeout) into a generic exitCode=1 warning. Detect both
`error.code === 'ETIMEDOUT'` and `error.killed === true &&
error.signal === 'SIGTERM'` so the user-visible warning correctly
names "timed out after 5s" instead of "exited 1".
-wg7 (deepseek): formatAttributionSummary and getAttributionNotesRef
were exported but had zero production callers (only tests). Remove
the dead exports + their tests (~40 LOC). If/when a logging surface
needs them, they can be re-introduced.
-wgb (deepseek): tokeniseSegment doesn't recursively unwrap
`bash -c '...'` / `sh -c` / `zsh -c`, so addCoAuthorToGitCommit
won't splice the trailer into a wrapped command. The background
refusal AND the post-commit note path DO catch the wrapped commit
because stripShellWrapper at the top of execute peels the wrapper
before gitCommitContext / getGitHead run — so the worst-case
("background bash -c 'git commit' bypasses the guard") doesn't
materialize. The remaining gap (no Co-authored-by trailer for
bash -c-wrapped commits) requires recursively splicing into the
inner script with proper bash single-quote re-quoting; significant
enough that it's worth its own PR. Documented as a partial-coverage
limitation.
339 → 325 tests pass after the dead-export removal; typecheck clean.
* fix(attribution): committed-blob validation, deleted-leaf canonicalisation, sudo/env shifts, dir-stack
gpt-5.5 review (issue 4389405179):
1. realpathOrSelf falls back to the non-canonical input when the
leaf doesn't exist (deleted file). recordEdit stored the entry
under the canonical path; lookup post-deletion misses on macOS
where /var ↔ /private/var. Canonicalise the parent and rejoin
the basename for missing leaves so deleted-file getFileAttribution
still resolves the canonical key. Test updated to assert the
lookup-after-unlink path explicitly.
2. validateOnDiskHashes read the LIVE working-tree, so a user who
`git add`'d AI's content and then made additional unstaged edits
would have the entry dropped on a commit whose blob still matched
AI's hash. Replace with `validateAgainst(getContent)` that takes
a caller-supplied reader; attachCommitAttribution now passes a
reader that fetches the COMMITTED blob via `git show HEAD:<rel>`.
Working-tree validation kept as `validateAgainstWorkingTree` for
code paths without a committed ref. Returns null = no comparison
signal (entry preserved). Tests cover all three readers
(committed-blob via stub, working-tree, null-passthrough).
deepseek-v4-pro review #1: sanitiseAttribution defaults missing
contentHash to '' on legacy-snapshot restore. recordEdit's
divergence check would then trip on every subsequent edit and
silently reset all the AI work. Skip the divergence check when
existing.contentHash is empty — we have no baseline to compare
against, so don't drop. Test added covering legacy-snapshot
preservation through validateAgainst.
deepseek #4: validateAgainst now logs every entry drop via
debugLogger.debug so a 3am operator can see WHICH entry got
dropped and tied to which canonical key.
deepseek #8: GIT_NAMESPACE removed from GIT_ENV_SHIFTS_REPO. It
prefixes ref names within the same repo but doesn't redirect git
to a different on-disk repository, so a commit underneath it still
lands in our cwd's repo. Doc comment explains the distinction.
deepseek #9: pushd/popd treated as cwd-shifting alongside cd in
gitCommitContext / isAmendCommit / findAttributableCommitSegment.
pushd reuses cdTargetMayChangeRepo (relative-no-escape stays
in-repo); popd unconditionally flips cwdShifted because we don't
track the bash dir-stack.
deepseek #10: sudo's value-taking flag table now has a parallel
SUDO_FLAGS_SHIFT_CWD set covering -D / --chdir (Linux sudo 1.9.2+).
Any segment whose sudo wrapper sees one of those flags returns null
from tokeniseSegment — same contract as env -C / --chdir and
GIT_DIR=...
328 tests pass; typecheck clean both packages.
* fix(attribution): scope validateAgainst to committed set, SHA-pin reader, intent-aware migration
Round 1 of multi-pass audit on b3a06a7c4. Three correctness fixes:
1. validateAgainst was iterating ALL fileAttributions but the
committed-blob reader (git show HEAD:<rel>) returns HEAD's
pre-AI content for files NOT in the just-made commit. Result:
pending unstaged AI work was silently wiped on every commit
because the divergence check ran against the wrong baseline
for unrelated files. Fix: build the committed scope first via
matchCommittedFiles, scope the reader to that set (return null
for everything else), validate, then RE-run matchCommittedFiles
to pick up dropped entries. The validateAgainstWorkingTree
wrapper had no production caller — removed it and its test.
2. The committed-blob reader used symbolic `HEAD` instead of the
captured postHead SHA — same TOCTOU concern buildGitNotesCommand
already addressed. A post-commit hook moving HEAD between
capture and the reader's `git show` would silently compare
against the wrong commit's content and trip the divergence
check spuriously. Pin the reader to `git show <postHead>:<rel>`.
3. v3→v4 migration's invalid-string fallback used to reset to {}.
Combined with the runtime pickBool's "absent → schema default
true" rule, that silently re-enabled attribution for users who
hand-edited `"gitCoAuthor": "off"` to disable. Migration now
recognises enable-intent strings (true/yes/on/1/enabled) and
disable-intent strings (false/no/off/0/disabled/'') and maps
them to {commit, pr} explicitly. Unrecognised strings fall to
{commit: false, pr: false} with a warning — same safer-by-default
contract as runtime pickBool. Test grid covers all 11 cases.
Also tidied the FileAttribution.contentHash JSDoc to reference
the renamed `validateAgainst` (was still pointing at the dropped
`validateOnDiskHashes` name).
1085 tests pass; typecheck clean both packages.
* chore(attribution): extract pickOuterLastMatch, log unrecognised pickBool inputs
Round 2 of multi-pass audit. Two cleanups, no behaviour changes:
1. addCoAuthorToGitCommit and addAttributionToPR each carried their
own copy of the matchRange / isInside / "pick LAST non-nested
match" logic (~25 LOC duplicated). Extracted to module-level
helpers `matchSpan`, `isMatchInside`, and `pickOuterLastMatch<T>`
so a future bug fix can't apply to only one of the two
rewriters. Behaviour identical — same algorithm, same edge cases.
2. normalizeGitCoAuthor's pickBool silently maps unrecognised
strings to false (safer-by-default vs the old "default-to-true
on mismatch" policy, but a user who hand-edited
`{ commit: "maybe" }` had no signal that their setting was being
ignored). Add a `gitCoAuthorLogger.warn` listing the accepted
forms so a debug-mode user can see the actual coercion. Known
disable-intent strings (false/no/off/0/empty) stay silent —
they're explicit user intent. Also pass the field name so the
warning identifies which sub-toggle (commit vs pr) was bad.
1101 tests pass; typecheck clean.
* fix(attribution): canonicalise BOM and CRLF before hashing
Round 3 of multi-pass audit. One real correctness fix.
Edit and WriteFile preserve the file's BOM and CRLF line-ending
choice when writing back, so the on-disk bytes can include a leading
U+FEFF and CRLFs even when AI's recordEdit input was given with LF
and no BOM. The committed-blob reader's `git show <sha>:<rel>`
returns those raw bytes verbatim, and computeContentHash hashed them
as-is — so a UTF-8 BOM file or a CRLF-line-ending file would always
have a mismatch between AI's recorded hash and the on-disk hash, and
validateAgainst would drop the entry on every commit.
Add `canonicaliseForHash`: strips a leading U+FEFF and normalises
CRLF→LF before computing the SHA-256. Both sides (recordEdit when
storing the post-write hash, and validateAgainst when comparing to
the on-disk read) flow through computeContentHash, so the
canonicalisation is symmetric. The hash is metadata used only for
divergence detection — collapsing these visual differences is the
right comparison semantics.
Three regression tests added: BOM-only, CRLF-only, and BOM+CRLF
combined. All exercise the typical case where AI's recordEdit input
is LF + no BOM but the on-disk content (post-writeTextFile) has the
file's preserved BOM/lineEnding choice.
* fix(attribution): reset accumulator when re-creating a deleted tracked file
Round 4 of multi-pass audit + Copilot finding from review 4236842362
(I missed it in the previous refresh).
recordEdit's existing prior-state check was symmetric on diverged
oldContent but ASYMMETRIC on a fresh file lifetime: when AI creates
`foo.ts` (oldContent=null), then user `rm foo.ts`, then AI
re-creates `foo.ts` (oldContent=null again), the second recordEdit
saw `existing` (from the first lifetime) and SKIPPED the divergence
check (because oldContent === null bails out of that branch). The
accumulator carried 100 chars from the deleted file plus 5 chars
from the new content = 155, vs the actual 5 on disk. Subsequent
generateNotePayload's clamp against `(adds+dels) * 40` couldn't
catch this — the diff size for a 1-line addition is 40, far above
the actual content size.
Add a fresh-file-lifetime branch: when `existing` is set AND the
caller reports `oldContent === null`, reset aiContribution and
aiCreated before counting the new contribution. The new edit is
treated as a brand-new file at the same path (which is what the
caller's null oldContent means semantically).
Test added covering the exact `AI create → delete → AI re-create`
flow. Also verified `should treat new files as ai-created` and
`should accumulate contributions across multiple edits` still pass.
* fix(attribution): treat git -C . as in-cwd, gate preHead on attributable
Round 5 of multi-pass audit. Two related correctness/efficiency
fixes around the cwd-shift parser and the preHead capture.
1. `git -C .` (and `-C ./`, `-C.`) is a no-op cwd shift but the
"any -C → cwd-shifted" rule was treating it the same as
`-C /tmp/other`, suppressing attribution for what's effectively
`git commit` with an explicit current-dir marker. Add an
`isNoopCwdTarget` helper used in both the spaced (`-C .`) and
attached (`-C.`) branches of `parseGitInvocation`. `--git-dir`
/ `--work-tree` are left unconditional — those aren't cwd in the
same sense.
2. preHead was being captured for ANY hasCommit, including the
non-attributable cases (`cd /elsewhere && git commit`,
`git -C /other commit`). The only consumer of preHead is the
`attachCommitAttribution` call inside the `attributableInCwd`
branch — there is intentionally NO cleanup branch for the
non-attributable case (see the existing comment around the
`else if (commitCtx.hasCommit)` non-branch). The execFileSync
for `getGitHeadSync` is dead work in that path: ~10–50 ms
blocking the event loop before the user's real command spawns.
Gate the capture on `attributableInCwd` to match the consumer.
Tests added for the three -C dot-form variants. Full suite green:
146 in shell.test.ts, 56 in commitAttribution.test.ts.
* fix(core): preserve attribution across renamed files
* fix(attribution): preserve env-vars in tokens, exclude empty -C targets
Round 7 of multi-pass audit. Two related fixes around how
`shell-quote` handles env-var references and how the cwd-shift
detector reads them.
1. `shell-quote.parse` collapses `$NAME` references it cannot
resolve to the empty string. The downstream cwd-shift checks
(`cdTargetMayChangeRepo`'s `target.includes('$')` repo-shift
detector, and the new `isNoopCwdTarget` no-op detector) were
designed to catch env-var targets but received `''` instead of
`$NAME` from `tokeniseSegment` and silently failed. Concretely,
`cd $HOME && git commit` and `git -C $HOME commit` would both
pass through as in-cwd attributable, stamping our trailer onto
commits that land in whatever repo `$HOME`/`$REPO_ROOT`
resolves to at runtime.
Pass an env getter `(key) => '$' + key` to `shell-quote.parse`
inside `tokeniseSegment` so unresolved references stay literal
in tokens (`['cd', '$HOME']` instead of `['cd', '']`).
`target.includes('$')` now fires correctly, and the no-op
detector sees `$HOME` (non-`.`) and rejects it. KEY=value
leading-env detection is unaffected (shell-quote doesn't
interpolate inside KEY=value tokens).
2. Even with env preservation, an `''` target can still slip
through (literal `-C ""`, escaped quotes, edge cases in
shell-quote). Round 5's `isNoopCwdTarget` accepted `''` as a
no-op alongside `'.'` / `'./'`, which would re-introduce the
attribution-on-wrong-repo problem if any path produced an
empty token. Tighten to `'.'` and `'./'` only — the only
missed cases are literal `-C ""` (malformed, won't actually
commit) and the rare `-C $PWD` (now also caught conservatively,
since `$PWD` becomes literal `$PWD` and isn't `.` or `./`).
Tests added for `cd $HOME` / `cd $REPO_ROOT && git commit` and
`git -C $HOME commit` / `git -C "" commit`. Full suite green
(150 in shell.test.ts, 58 in commitAttribution.test.ts).
* fix(attribution): SHA-pin diff/rev-list phase, document aiChars heuristic
Addresses tanzhenxin's review (4240760004) — two residuals after
the prior pinning round.
1. Diff phase still races against HEAD.
The note write itself was already pinned to the captured `postHead`
(`git notes add -f <postHead>`), but the *content* of the note —
`getCommittedFileInfo`'s probe + diff calls and the multi-commit
guard's `rev-list --count` — were still going through symbolic
`HEAD` / `HEAD~1` / `HEAD@{1}`. Several awaited subprocesses run
between the postHead capture and these reads, so a husky / lefthook
auto-amender, signed-commits hook, chained `git tag -m`, or
parallel git process moving HEAD in that window would leave the
note attached to commit A but describing commit B's contents.
Same TOCTOU class as the prior critical, half-closed.
Thread `postHead` (and `preHead` for amend) through
`getCommittedFileInfo`. Probes become `rev-parse --verify
${postHead}~1` and `log -1 --pretty=%P ${postHead}`; diffs become
`${postHead}~1..${postHead}` (parent case),
`${preHead}..${postHead}` (amend — preHead is the pre-amend SHA
captured before the user's command and is exactly what HEAD@{1}
resolved to at parse time, with the added benefit that it can't be
GC'd between capture and use), and `diff-tree --root <postHead>`
(root commit). The amend branch keeps the existing reflog-vs-
no-reflog warning, just driven off `preHead` instead of HEAD@{1}.
Same pin applied to `countCommitsAfter` (now `${preHead}..
${postHead}`) and `countCommitsFromRoot` (now `${postHead}`).
Why parent case uses `${postHead}~1` and NOT `${preHead}`: in
`git reset HEAD~3 && git commit` chains the captured preHead
points well above postHead's parent, and `${preHead}..${postHead}`
would describe the reset-away commits as deletions, drastically
over-attributing. The actual parent of the just-landed commit is
what we want, and `${postHead}~1` is the SHA-pinned form of that.
2. `aiChars` reads as a literal char count but isn't.
The field is emitted as a plain integer named `aiChars`; the PR
description's example shows values like 3200 / 1500 / 4700 that
anyone parsing the note will read as literal character counts.
Internally it's `(addedLines + deletedLines) × 40` for text and a
flat 1024 for binary, with the per-file AI accumulator clamped
against that ceiling. So 1000 one-character lines and 1000
thousand-character lines both report aiChars=40000, and a 5 MB
image change and a 1-byte binary tweak both report 1024. Anyone
aggregating raw aiChars for compliance reporting gets
systematically wrong numbers.
Add a comprehensive doc block on `FileAttributionDetail` (and
`CommitAttributionNote`) calling out the heuristic explicitly,
noting that `percent` / `summary.aiPercent` are the correct
fields for aggregation since both numerator and denominator use
the same proxy. Also expand the `APPROX_CHARS_PER_LINE` /
`BINARY_DIFF_SIZE_FALLBACK` const docs to point at the same
caveat. (Not renaming the fields — that'd break any downstream
consumer already parsing the existing schema; the doc is the
minimum-disruption call here.)
208 attribution tests pass; type-check clean.
* fix(attribution): use posix join in applyCommittedRenames for Windows compat
Windows CI failure on the two new rename tests (visible at PR #3115's
`Test (windows-latest, *)` jobs):
AssertionError: expected undefined to be defined
❯ src/services/commitAttribution.test.ts:572:66 (basic move)
AssertionError: expected 11 to be 22 (merge into existing)
Root cause: `path.join(canonicalRepoRoot, ...renamedRel.split('/'))`
calls `path.win32.join` on Windows, which forces backslash separators
regardless of input form. The test's `fs.realpathSync` mock returns
forward-slash paths (matching the macOS `/var` ↔ `/private/var`
fixture style), so `recordEdit` stores keys like
`/private/var/repo/src/old.ts`. The rename's joined target then came
out as `\\private\\var\\repo\\src\\new.ts`, the mock left it
unchanged (no `/var/` prefix to translate), and the subsequent
`fileAttributions.get(renamedAbs)` / `getFileAttribution(...)` lookups
missed the just-set entry — the rename silently dropped attribution.
The fix: build the joined path with `path.posix.join` against a
forward-slash-normalised `posixRepoRoot`, then let `realpathOrSelf`
canonicalise to the platform's storage form. This way:
- On real Windows production: posix-joined `D:/repo/src/new.ts` is
accepted by `fs.realpathSync` (Win32 API takes mixed slashes) and
returned in backslash form, matching what `recordEdit` stored.
- On real Linux/macOS production: forward-slash throughout, no-op.
- In the symlink-aware test (any platform): forward-slash matches
the mock-fixture storage form.
`matchCommittedFiles` already does the inverse normalisation
(`.split(path.sep).join('/')` for the relative-form check), so the
in/out paths line up either way.
Skipped adding a path.sep-mocked Linux-side regression because the
ESM module namespace doesn't allow `vi.spyOn` on path's exports.
The Windows CI job is the regression catcher; a focused-rerun
should now go green.
* docs(attribution): refresh stale HEAD~1/HEAD@{1} references in comments
The SHA-pinning round (8c3312027) replaced symbolic `HEAD~1..HEAD` /
`HEAD@{1}..HEAD` with `${postHead}~1..${postHead}` and
`${preHead}..${postHead}` in `getCommittedFileInfo` and the rev-list
helpers, but three docstrings / inline comments still described the
old shapes:
- `isAmendCommit` JSDoc said the amend switch goes from `HEAD~1..HEAD`
to `HEAD@{1}..HEAD`. Updated to reference `${postHead}~1..${postHead}`
and `${preHead}..${postHead}`, with the why (amended commit's parent
is the original's parent so the standard parent diff lumps both
commits' changes).
- `attachCommitAttribution`'s amend branch comment had the same drift;
updated to mention `${preHead}..${postHead}` directly.
- `getCommittedFileInfo` JSDoc said it diffs "HEAD against its parent
(HEAD~1)" and listed "--amend with no reflog" as an analysis-failure
case. Updated to mention postHead-pinning and the preHead-driven
amend bail (the reflog-GC dependency was dropped in the SHA-pin
round).
The remaining `HEAD~1..HEAD` references at countCommitsAfter:1959 and
getCommittedFileInfo:2523 are intentional — they describe the old
buggy shape as contrast for why we pin now.
No code change; tests + tsc still clean.
* fix(attribution): catch attached-value forms of env/sudo cwd-shift flags
Round 13 audit found a real bug: `sudo --chdir=/tmp git commit`,
`env -C/tmp git commit`, `env --chdir=/tmp git commit`, and
`sudo -D/tmp git commit` were all silently slipping through the
cwd-shift detector and getting our `Co-authored-by` trailer stamped
onto commits that landed in a different repo.
Root cause: `shell-quote` tokenises both the long attached form
(`--chdir=/tmp`) and the short attached form (`-C/tmp`) as a single
argv entry. The previous SHIFT_CWD detector did set-membership only
against the bare flag (`{'-C', '--chdir'}` for env;
`{'-D', '--chdir'}` for sudo), so the attached-form tokens never
matched and `tokeniseSegment` returned a normally-attributable
`['git', 'commit', ...]` segment.
Fix: introduce `isShiftCwdFlag(flag, set)` that catches:
- bare set-membership (existing behavior),
- long attached: `--name=...` when `--name` is in the set,
- short attached: `-Xanything` when `-X` is in the set and the
token is longer than the flag itself.
The flag does NOT need to consume an extra value token in the
attached-form case (the value is already embedded), so the existing
TAKES_VALUE bookkeeping is unaffected — we just bail with `null`
from `tokeniseSegment` before reaching the value-skip step.
Tests added: `env --chdir=`, `env -C/...` (attached), `sudo --chdir=`,
`sudo -D/...` (attached) — each is asserted NOT to add a co-author
trailer. 154 shell tests pass; type-check + lint clean.
* test(attribution): cover attached-form git -C/--git-dir/--work-tree
Adds three regression cases to the existing "git -C <path>" suppression
test: the short attached form `-C/path` (single shell-quote token)
and the long attached forms `--git-dir=/path` / `--work-tree=/path`.
parseGitInvocation already had the prefix checks at lines 416/425, but
no test exercised them — paired with the b89b65533 sudo/env attached-
form fix this round closes the family of "shell-quote single-token
flag with embedded value" cases that the bare set-membership checks
would otherwise miss.
157 shell tests pass; type-check clean.
* docs(attribution): document why backtick body doesn't bail like $(
The addCoAuthorToGitCommit body capture has a known truncation case
when an inner unescaped `"` appears inside the captured body — handled
for `$(...)` command substitution with an explicit bailout, but not
for backtick command substitution. The trade-off was unspoken; spell
it out so a future reviewer doesn't read the asymmetry as an
oversight.
Bare-backtick bodies (`\`func()\`` markdown-style) are common in
commit messages, have no inner `"`, and the regex captures them
correctly. Pathological backtick-with-inner-quote bodies (`\`cmd
"with" quotes\``) are a near-zero-traffic case where bash itself
already interprets the backticks as command substitution, so the
user has likely already broken their own command before our rewrite
runs. Bailing on any backtick would lose attribution for the common
case to defend against the rare one.
Also drops a stray blank line in commitAttribution.test.ts left over
from an earlier regression-test attempt.
* fix(attribution): scope trailer rewrite to before unquoted shell comment
Round 13 follow-on. Both `addCoAuthorToGitCommit` and
`addAttributionToPR` ran their `-m` / `--body` regex against the full
segment string, including any trailing shell comment. For a command
like `git commit -m "real" # -m "fake"` (a human-authored script
might leave a comment-out flag in place), `lastMatchOf` would pick
the comment's `-m "fake"`, splice the `Co-authored-by:` trailer in
there, and bash would silently discard the entire segment as a
comment — leaving the actual commit unattributed. Same shape for
`gh pr create --body "real" # --body "fake"`.
Fix: introduce `findUnquotedCommentStart(s)` — a bash-aware position
scanner that tracks single/double-quote state and treats `#` as a
comment marker only when it begins a word (start of input or
preceded by whitespace), not when it appears inside a quoted region
or mid-token like `foo#bar`. Both rewriters slice the segment to
`[0, commentStart)` before running their regex, so the trailer can
only land in the live (pre-comment) part.
Tests added:
- `git commit -m "real" # -m "fake"` — trailer lands in `"real"`
body BEFORE the `#`, comment's `-m "fake"` is left untouched.
- `git commit -m "fix #123 add feature"` — `#` inside the quoted
body is correctly NOT treated as a comment; the `#123` stays
inside the body and the trailer is appended.
159 shell tests pass; type-check clean.
* fix(attribution): warn on gh pr create flows that can't be rewritten + cover legacy gitCoAuthor migration end-to-end
Two residuals from this morning's review pass.
1. ANm7O — `addAttributionToPR` silently skipped for `--body-file`,
`--fill`, and bare `gh pr create` (editor) flows.
The rewriter only knows how to splice into an inline `--body`/`-b`
argv entry. For a `gh pr create` that uses `--body-file path`,
`--fill` (uses commit messages), or no body flag at all (editor
prompt), there's no inline body to splice into and the function
returned the unmodified command. Users with `gitCoAuthor.pr`
enabled would see PRs created without the attribution line and
have no signal as to why.
Add a debugLogger.warn at the no-match path naming the unsupported
flows and pointing the user at the inline form. Don't try to
handle `--body-file` automatically — that would mean mutating the
user's file on disk, which is well outside what an unprompted
command rewriter should do; `--fill` and editor flows have no body
in argv at all and can't be rewritten without re-architecting.
Tests added for `--body-file <path>`, `--fill`, and bare
`gh pr create` — each is asserted to leave the command unchanged
(no `Generated with Qwen Code` line spliced in).
2. ANm7L — settings-migration integration suite didn't cover the
exact V3 legacy shape this PR introduces.
`v3-to-v4.test.ts` already pins the migration body, but the end-
to-end CLI load → migrate → write path could regress without the
integration suite noticing. The existing v3LegacyDisableSettings
fixture has no `general.gitCoAuthor` field, so the V3→V4 step
technically fires but doesn't exercise the new boolean-expansion
logic.
Add a `v3GitCoAuthorBooleanSettings` fixture and a paired test
case that writes `general: { gitCoAuthor: false }` at $version 3,
runs the same `mcp list` CLI invocation, and asserts the saved
file has $version 4 plus `general.gitCoAuthor` exactly
`{ commit: false, pr: false }` — with sibling general.* keys and
unrelated top-level sections preserved.
162 shell tests pass; type-check + lint clean.
148 KiB
Qwen Code Configuration
Tip
Authentication / API keys: Authentication (API Key, Alibaba Cloud Coding Plan) and auth-related environment variables (like
OPENAI_API_KEY) are documented in Authentication.
Note
Note on New Configuration Format: The format of the
settings.jsonfile has been updated to a new, more organized structure. The old format will be migrated automatically. Qwen Code offers several ways to configure its behavior, including environment variables, command-line arguments, and settings files. This document outlines the different configuration methods and available settings.
Configuration layers
Configuration is applied in the following order of precedence (lower numbers are overridden by higher numbers):
| Level | Configuration Source | Description |
|---|---|---|
| 1 | Default values | Hardcoded defaults within the application |
| 2 | System defaults file | System-wide default settings that can be overridden by other settings files |
| 3 | User settings file | Global settings for the current user |
| 4 | Project settings file | Project-specific settings |
| 5 | System settings file | System-wide settings that override all other settings files |
| 6 | Environment variables | System-wide or session-specific variables, potentially loaded from .env files |
| 7 | Command-line arguments | Values passed when launching the CLI |
Settings files
Qwen Code uses JSON settings files for persistent configuration. There are four locations for these files:
| File Type | Location | Scope |
|---|---|---|
| System defaults file | Linux: /etc/qwen-code/system-defaults.jsonWindows: C:\ProgramData\qwen-code\system-defaults.jsonmacOS: /Library/Application Support/QwenCode/system-defaults.json The path can be overridden using the QWEN_CODE_SYSTEM_DEFAULTS_PATH environment variable. |
Provides a base layer of system-wide default settings. These settings have the lowest precedence and are intended to be overridden by user, project, or system override settings. |
| User settings file | ~/.qwen/settings.json (where ~ is your home directory). |
Applies to all Qwen Code sessions for the current user. |
| Project settings file | .qwen/settings.json within your project's root directory. |
Applies only when running Qwen Code from that specific project. Project settings override user settings. |
| System settings file | Linux: /etc/qwen-code/settings.json Windows: C:\ProgramData\qwen-code\settings.json macOS: /Library/Application Support/QwenCode/settings.jsonThe path can be overridden using the QWEN_CODE_SYSTEM_SETTINGS_PATH environment variable. |
Applies to all Qwen Code sessions on the system, for all users. System settings override user and project settings. May be useful for system administrators at enterprises to have controls over users' Qwen Code setups. |
Note
Note on environment variables in settings: String values within your
settings.jsonfiles can reference environment variables using either$VAR_NAMEor${VAR_NAME}syntax. These variables will be automatically resolved when the settings are loaded. For example, if you have an environment variableMY_API_TOKEN, you could use it insettings.jsonlike this:"apiKey": "$MY_API_TOKEN".
The .qwen directory in your project
In addition to a project settings file, a project's .qwen directory can contain other project-specific files related to Qwen Code's operation, such as:
- Custom sandbox profiles (e.g.
.qwen/sandbox-macos-custom.sb,.qwen/sandbox.Dockerfile). - Agent Skills under
.qwen/skills/(each Skill is a directory containing aSKILL.md).
Configuration migration
Qwen Code automatically migrates legacy configuration settings to the new format. Old settings files are backed up before migration. The following settings have been renamed from negative (disable*) to positive (enable*) naming:
| Old Setting | New Setting | Notes |
|---|---|---|
disableAutoUpdate + disableUpdateNag |
general.enableAutoUpdate |
Consolidated into a single setting |
disableLoadingPhrases |
ui.accessibility.enableLoadingPhrases |
|
disableFuzzySearch |
context.fileFiltering.enableFuzzySearch |
|
disableCacheControl |
model.generationConfig.enableCacheControl |
Note
Boolean value inversion: When migrating, boolean values are inverted (e.g.,
disableAutoUpdate: truebecomesenableAutoUpdate: false).
Consolidation policy for disableAutoUpdate and disableUpdateNag
When both legacy settings are present with different values, the migration follows this policy: if either disableAutoUpdate or disableUpdateNag is true, then enableAutoUpdate becomes false:
disableAutoUpdate |
disableUpdateNag |
Migrated enableAutoUpdate |
|---|---|---|
false |
false |
true |
false |
true |
false |
true |
false |
false |
true |
true |
false |
Available settings in settings.json
Settings are organized into categories. Most settings should be placed within their corresponding top-level category object in your settings.json file. A few compatibility settings, such as proxy, are top-level keys.
top-level
| Setting | Type | Description | Default |
|---|---|---|---|
proxy |
string | Proxy URL for CLI HTTP requests. Precedence is --proxy > proxy in settings.json > HTTPS_PROXY / https_proxy / HTTP_PROXY / http_proxy environment variables. |
undefined |
general
| Setting | Type | Description | Default |
|---|---|---|---|
general.preferredEditor |
string | The preferred editor to open files in. | undefined |
general.vimMode |
boolean | Enable Vim keybindings. | false |
general.enableAutoUpdate |
boolean | Enable automatic update checks and installations on startup. | true |
general.showSessionRecap |
boolean | Auto-show a one-line "where you left off" recap when returning to the terminal after being away. Off by default. Use /recap to trigger manually regardless of this setting. |
false |
general.sessionRecapAwayThresholdMinutes |
number | Minutes the terminal must be blurred before an auto-recap fires on focus-in. Only used when showSessionRecap is enabled. |
5 |
general.gitCoAuthor.commit |
boolean | Add a Co-authored-by trailer to git commit messages AND attach a per-file AI-attribution git note (refs/notes/ai-attribution) for commits made through Qwen Code. Disabling skips both. |
true |
general.gitCoAuthor.pr |
boolean | Append a Qwen Code attribution line to pull request descriptions when running gh pr create. |
true |
general.checkpointing.enabled |
boolean | Enable session checkpointing for recovery. | false |
general.defaultFileEncoding |
string | Default encoding for new files. Use "utf-8" (default) for UTF-8 without BOM, or "utf-8-bom" for UTF-8 with BOM. Only change this if your project specifically requires BOM. |
"utf-8" |
output
| Setting | Type | Description | Default | Possible Values |
|---|---|---|---|---|
output.format |
string | The format of the CLI output. | "text" |
"text", "json" |
ui
| Setting | Type | Description | Default |
|---|---|---|---|
ui.theme |
string | The color theme for the UI. See Themes for available options. | undefined |
ui.customThemes |
object | Custom theme definitions. | {} |
ui.statusLine |
object | Custom status line configuration. A shell command whose output is shown in the footer's left section. See Status Line. | undefined |
ui.hideWindowTitle |
boolean | Hide the window title bar. | false |
ui.hideTips |
boolean | Hide all tips (startup and post-response) in the UI. See Contextual Tips. | false |
ui.hideBanner |
boolean | Hide the startup ASCII logo and info panel. Tips and chat input still render unless ui.hideTips is also set. |
false |
ui.customBannerTitle |
string | Replace the default >_ Qwen Code title in the banner info panel. The (vX.Y.Z) version suffix is always appended; auth, model, and path lines are not affected. Sanitized; capped at 80 characters. |
"" |
ui.customBannerSubtitle |
string | Optional subtitle line rendered between the banner title and the auth/model line, in place of the blank spacer row. Sanitized; capped at 160 characters. Empty (default) keeps the original blank spacer. | "" |
ui.customAsciiArt |
string | object | Replace the QWEN ASCII logo in the banner. Accepts an inline string (used for both width tiers), { "path": "./brand.txt" } (relative paths resolve against the owning settings file's directory; read once at startup with O_NOFOLLOW on POSIX, capped at 64 KB), or { "small": ..., "large": ... } for width-aware selection. Sanitized; capped at 200 lines × 200 columns per tier. |
undefined |
ui.hideFooter |
boolean | Hide the footer from the UI. | false |
ui.showMemoryUsage |
boolean | Display memory usage information in the UI. | false |
ui.showLineNumbers |
boolean | Show line numbers in code blocks in the CLI output. | true |
ui.renderMode |
string | Default Markdown display mode. Use "render" for rich visual previews or "raw" to show source-oriented Markdown by default. Toggle during a session with Alt/Option+M; on macOS the terminal must send Option as Meta. See Markdown Rendering. |
"render" |
ui.showCitations |
boolean | Show citations for generated text in the chat. | true |
ui.compactMode |
boolean | Hide tool output and thinking for a cleaner view. Toggle with Ctrl+O during a session or via the Settings dialog. Tool approval prompts are never hidden, even in compact mode. The setting persists across sessions. |
false |
ui.shellOutputMaxLines |
number | Max number of shell output lines shown inline. Set to 0 to disable the cap and show full output. Hidden lines are surfaced via the +N lines indicator. Errors, !-prefix user-initiated commands, confirming tools, and focused embedded shells always show full output. |
5 |
enableWelcomeBack |
boolean | Show welcome back dialog when returning to a project with conversation history. When enabled, Qwen Code will automatically detect if you're returning to a project with a previously generated project summary (.qwen/PROJECT_SUMMARY.md) and show a dialog allowing you to continue your previous conversation or start fresh. If you choose Start new chat session, that choice is remembered for the current project until the project summary changes. This feature integrates with the /summary command and quit confirmation dialog. |
true |
ui.accessibility.enableLoadingPhrases |
boolean | Enable loading phrases (disable for accessibility). | true |
ui.accessibility.screenReader |
boolean | Enables screen reader mode, which adjusts the TUI for better compatibility with screen readers. | false |
ui.customWittyPhrases |
array of strings | A list of custom phrases to display during loading states. When provided, the CLI will cycle through these phrases instead of the default ones. | [] |
ui.enableFollowupSuggestions |
boolean | Enable followup suggestions that predict what you want to type next after the model responds. Suggestions appear as ghost text and can be accepted with Tab, Enter, or Right Arrow. | true |
ui.enableCacheSharing |
boolean | Use cache-aware forked queries for suggestion generation. Reduces cost on providers that support prefix caching (experimental). | true |
ui.enableSpeculation |
boolean | Speculatively execute accepted suggestions before submission. Results appear instantly when you accept (experimental). | false |
experimental.emitToolUseSummaries |
boolean | Generate short LLM-based labels summarizing each tool-call batch. See Tool-Use Summaries. Requires fastModel to be configured; silently skipped otherwise. Can be overridden per-session with QWEN_CODE_EMIT_TOOL_USE_SUMMARIES=0 or =1. |
true |
ide
| Setting | Type | Description | Default |
|---|---|---|---|
ide.enabled |
boolean | Enable IDE integration mode. | false |
ide.hasSeenNudge |
boolean | Whether the user has seen the IDE integration nudge. | false |
privacy
| Setting | Type | Description | Default |
|---|---|---|---|
privacy.usageStatisticsEnabled |
boolean | Enable collection of usage statistics. | true |
model
| Setting | Type | Description | Default |
|---|---|---|---|
model.name |
string | The Qwen model to use for conversations. | undefined |
model.maxSessionTurns |
number | Maximum number of user/model/tool turns to keep in a session. -1 means unlimited. | -1 |
model.generationConfig |
object | Advanced overrides passed to the underlying content generator. Supports request controls such as timeout, maxRetries, enableCacheControl, splitToolMedia (set true for strict OpenAI-compatible servers like LM Studio that reject non-text content on role: "tool" messages — splits media into a follow-up user message), contextWindowSize (override model's context window size), modalities (override auto-detected input modalities), customHeaders (custom HTTP headers for API requests), extra_body (additional body parameters for OpenAI-compatible API requests only), and reasoning ({ effort: 'low' | 'medium' | 'high' | 'max', budget_tokens?: number } to control thinking intensity, or false to disable; 'max' is a DeepSeek extension — see Reasoning / thinking configuration for per-provider behavior. Note: when samplingParams is set on an OpenAI-compatible provider, the pipeline ships those keys verbatim and the separate top-level reasoning field is dropped — put reasoning_effort inside samplingParams (or extra_body) instead in that case), along with fine-tuning knobs under samplingParams (for example temperature, top_p, max_tokens). Leave unset to rely on provider defaults. |
undefined |
model.chatCompression.contextPercentageThreshold |
number | Sets the threshold for chat history compression as a percentage of the model's total token limit. This is a value between 0 and 1 that applies to both automatic compression and the manual /compress command. For example, a value of 0.6 will trigger compression when the chat history exceeds 60% of the token limit. Use 0 to disable compression entirely. |
0.7 |
model.skipNextSpeakerCheck |
boolean | Skip the next speaker check. | false |
model.skipLoopDetection |
boolean | Disables loop detection checks. Loop detection prevents infinite loops in AI responses but can generate false positives that interrupt legitimate workflows. Enable this option if you experience frequent false positive loop detection interruptions. | false |
model.skipStartupContext |
boolean | Skips sending the startup workspace context (environment summary and acknowledgement) at the beginning of each session. Enable this if you prefer to provide context manually or want to save tokens on startup. | false |
model.enableOpenAILogging |
boolean | Enables logging of OpenAI API calls for debugging and analysis. When enabled, API requests and responses are logged to JSON files. | false |
model.openAILoggingDir |
string | Custom directory path for OpenAI API logs. If not specified, defaults to logs/openai in the current working directory. Supports absolute paths, relative paths (resolved from current working directory), and ~ expansion (home directory). |
undefined |
Example model.generationConfig:
{
"model": {
"generationConfig": {
"timeout": 60000,
"contextWindowSize": 128000,
"modalities": {
"image": true
},
"enableCacheControl": true,
"customHeaders": {
"X-Client-Request-ID": "req-123"
},
"extra_body": {
"enable_thinking": true
},
"samplingParams": {
"temperature": 0.2,
"top_p": 0.8,
"max_tokens": 1024
}
}
}
}
max_tokens (adaptive output tokens):
When samplingParams.max_tokens is not set, Qwen Code uses an adaptive output token strategy to optimize GPU resource usage:
- Requests start with a default limit of 8K output tokens
- If the response is truncated (the model hits the limit), Qwen Code automatically retries with 64K tokens
- The partial output is discarded and replaced with the full response from the retry
This is transparent to users — you may briefly see a retry indicator if escalation occurs. Since 99% of responses are under 5K tokens, the retry happens rarely (<1% of requests).
To override this behavior, either set samplingParams.max_tokens in your settings or use the QWEN_CODE_MAX_OUTPUT_TOKENS environment variable.
contextWindowSize:
Overrides the default context window size for the selected model. Qwen Code determines the context window using built-in defaults based on model name matching, with a constant fallback value. Use this setting when a provider's effective context limit differs from Qwen Code's default. This value defines the model's assumed maximum context capacity, not a per-request token limit.
When the selected model is defined in modelProviders, set
contextWindowSize in that provider entry's generationConfig instead of the
top-level model.generationConfig. Provider model entries are sealed, so
top-level generation settings do not fill missing provider fields.
modalities:
Overrides the auto-detected input modalities for the selected model. Qwen Code automatically detects supported modalities (image, PDF, audio, video) based on model name pattern matching. Use this setting when the auto-detection is incorrect — for example, to enable pdf for a model that supports it but isn't recognized. Format: { "image": true, "pdf": true, "audio": true, "video": true }. Omit a key or set it to false for unsupported types.
customHeaders:
Allows you to add custom HTTP headers to all API requests. This is useful for request tracing, monitoring, API gateway routing, or when different models require different headers. For provider models, define customHeaders in modelProviders[].generationConfig.customHeaders. For runtime models without a matching provider entry, define it in model.generationConfig.customHeaders. No merging occurs between the two levels.
The extra_body field allows you to add custom parameters to the request body sent to the API. This is useful for provider-specific options that are not covered by the standard configuration fields. Note: This field is only supported for OpenAI-compatible providers (openai, qwen-oauth). It is ignored for Anthropic and Gemini providers. For provider models, define extra_body in modelProviders[].generationConfig.extra_body. For runtime models without a matching provider entry, define it in model.generationConfig.extra_body.
model.openAILoggingDir examples:
"~/qwen-logs"- Logs to~/qwen-logsdirectory"./custom-logs"- Logs to./custom-logsrelative to current directory"/tmp/openai-logs"- Logs to absolute path/tmp/openai-logs
fastModel
| Setting | Type | Description | Default |
|---|---|---|---|
fastModel |
string | Model used for generating prompt suggestions and speculative execution. Leave empty to use the main model. A smaller/faster model (e.g., qwen3-coder-flash) reduces latency and cost. Can also be set via /model --fast. |
"" |
context
| Setting | Type | Description | Default |
|---|---|---|---|
context.fileName |
string or array of strings | The name of the context file(s). | undefined |
context.importFormat |
string | The format to use when importing memory. | undefined |
context.includeDirectories |
array | Additional directories to include in the workspace context. Specifies an array of additional absolute or relative paths to include in the workspace context. Missing directories will be skipped with a warning by default. Paths can use ~ to refer to the user's home directory. This setting can be combined with the --include-directories command-line flag. |
[] |
context.loadFromIncludeDirectories |
boolean | Controls the behavior of the /memory refresh command. If set to true, QWEN.md files should be loaded from all directories that are added. If set to false, QWEN.md should only be loaded from the current directory. |
false |
context.fileFiltering.respectGitIgnore |
boolean | Respect .gitignore files when searching. | true |
context.fileFiltering.respectQwenIgnore |
boolean | Respect .qwenignore files when searching. | true |
context.fileFiltering.enableRecursiveFileSearch |
boolean | Whether to enable searching recursively for filenames under the current tree when completing @ prefixes in the prompt. |
true |
context.fileFiltering.enableFuzzySearch |
boolean | When true, enables fuzzy search capabilities when searching for files. Set to false to improve performance on projects with a large number of files. |
true |
context.clearContextOnIdle.toolResultsThresholdMinutes |
number | Minutes of inactivity before clearing old tool result content. Use -1 to disable. |
60 |
context.clearContextOnIdle.toolResultsNumToKeep |
number | Number of most-recent compactable tool results to preserve when clearing. Floor at 1. | 5 |
Troubleshooting File Search Performance
If you are experiencing performance issues with file searching (e.g., with @ completions), especially in projects with a very large number of files, here are a few things you can try in order of recommendation:
- Use
.qwenignore: Create a.qwenignorefile in your project root to exclude directories that contain a large number of files that you don't need to reference (e.g., build artifacts, logs,node_modules). Reducing the total number of files crawled is the most effective way to improve performance. - Disable Fuzzy Search: If ignoring files is not enough, you can disable fuzzy search by setting
enableFuzzySearchtofalsein yoursettings.jsonfile. This will use a simpler, non-fuzzy matching algorithm, which can be faster. - Disable Recursive File Search: As a last resort, you can disable recursive file search entirely by setting
enableRecursiveFileSearchtofalse. This will be the fastest option as it avoids a recursive crawl of your project. However, it means you will need to type the full path to files when using@completions.
tools
| Setting | Type | Description | Default | Notes |
|---|---|---|---|---|
tools.sandbox |
boolean or string | Sandbox execution environment (can be a boolean or a path string). | undefined |
|
tools.sandboxImage |
string | Sandbox image URI used by Docker/Podman when --sandbox-image and QWEN_SANDBOX_IMAGE are not set. |
undefined |
|
tools.shell.enableInteractiveShell |
boolean | Use node-pty for an interactive shell experience. Fallback to child_process still applies. |
false |
|
tools.core |
array of strings | Deprecated. Will be removed in next version. Use permissions.allow + permissions.deny instead. Restricts built-in tools to an allowlist. All tools not in the list are disabled. |
undefined |
|
tools.exclude |
array of strings | Deprecated. Use permissions.deny instead. Tool names to exclude from discovery. Automatically migrated to the permissions format on first load. |
undefined |
|
tools.allowed |
array of strings | Deprecated. Use permissions.allow instead. Tool names that bypass the confirmation dialog. Automatically migrated to the permissions format on first load. |
undefined |
|
tools.approvalMode |
string | Sets the default approval mode for tool usage. | default |
Possible values: plan (analyze only, do not modify files or execute commands), default (require approval before file edits or shell commands run), auto-edit (automatically approve file edits), yolo (automatically approve all tool calls) |
tools.discoveryCommand |
string | Command to run for tool discovery. | undefined |
|
tools.callCommand |
string | Defines a custom shell command for calling a specific tool that was discovered using tools.discoveryCommand. The shell command must meet the following criteria: It must take function name (exactly as in function declaration) as first command line argument. It must read function arguments as JSON on stdin, analogous to functionCall.args. It must return function output as JSON on stdout, analogous to functionResponse.response.content. |
undefined |
|
tools.useRipgrep |
boolean | Use ripgrep for file content search instead of the fallback implementation. Provides faster search performance. | true |
|
tools.useBuiltinRipgrep |
boolean | Use the bundled ripgrep binary. When set to false, the system-level rg command will be used instead. This setting is only effective when tools.useRipgrep is true. |
true |
|
tools.truncateToolOutputThreshold |
number | Truncate tool output if it is larger than this many characters. Applies to Shell, Grep, Glob, ReadFile and ReadManyFiles tools. | 25000 |
Requires restart: Yes |
tools.truncateToolOutputLines |
number | Maximum lines or entries kept when truncating tool output. Applies to Shell, Grep, Glob, ReadFile and ReadManyFiles tools. | 1000 |
Requires restart: Yes |
Note
Migrating from
tools.core/tools.exclude/tools.allowed: These legacy settings are deprecated and automatically migrated to the newpermissionsformat on first load. Prefer configuringpermissions.allow/permissions.denydirectly. Use/permissionsto manage rules interactively.
memory
| Setting | Type | Description | Default |
|---|---|---|---|
memory.enableManagedAutoMemory |
boolean | Enable background extraction of memories from conversations. | true |
memory.enableManagedAutoDream |
boolean | Enable automatic consolidation (deduplication and cleanup) of collected memories. | false |
See Memory for details on how auto-memory works and how to use the /memory, /remember, and /dream commands.
permissions
The permissions system provides fine-grained control over which tools can run, which require confirmation, and which are blocked.
Decision priority (highest first): deny > ask > allow > (default/interactive mode)
The first matching rule wins. Rules use the format "ToolName" or "ToolName(specifier)".
| Setting | Type | Description | Default |
|---|---|---|---|
permissions.allow |
array of strings | Rules for auto-approved tool calls (no confirmation needed). Merged across all scopes (user + project + system). | undefined |
permissions.ask |
array of strings | Rules for tool calls that always require user confirmation. Takes priority over allow. |
undefined |
permissions.deny |
array of strings | Rules for blocked tool calls. Highest priority — overrides both allow and ask. |
undefined |
Tool name aliases (any of these work in rules):
| Alias | Canonical tool | Notes |
|---|---|---|
Bash, Shell |
run_shell_command |
|
Read, ReadFile |
read_file |
Meta-category — see below |
Edit, EditFile |
edit |
Meta-category — see below |
Write, WriteFile |
write_file |
|
Grep, SearchFiles |
grep_search |
|
Glob, FindFiles |
glob |
|
ListFiles |
list_directory |
|
WebFetch |
web_fetch |
|
Agent |
task |
|
Skill |
skill |
Meta-categories:
Some rule names automatically cover multiple tools:
| Rule name | Tools covered |
|---|---|
Read |
read_file, grep_search, glob, list_directory |
Edit |
edit, write_file |
Important
Read(/path/**)matches all four read tools (file read, grep, glob, and directory listing). To restrict only file reading, useReadFile(/path/**)orread_file(/path/**).
Rule syntax examples:
| Rule | Meaning |
|---|---|
"Bash" |
All shell commands |
"Bash(git *)" |
Shell commands starting with git (word boundary: NOT gitk) |
"Bash(git push *)" |
Shell commands like git push origin main |
"Bash(npm run *)" |
Any npm run script |
"Read" |
All file read operations (read, grep, glob, list) |
"Read(./secrets/**)" |
Read any file under ./secrets/ recursively |
"Edit(/src/**/*.ts)" |
Edit TypeScript files under project root /src/ |
"WebFetch(api.example.com)" |
Fetch from api.example.com and all its subdomains |
"mcp__puppeteer" |
All tools from the puppeteer MCP server |
Path pattern prefixes:
| Prefix | Meaning | Example |
|---|---|---|
// |
Absolute path from filesystem root | //etc/passwd |
~/ |
Relative to home directory | ~/Documents/*.pdf |
/ |
Relative to project root | /src/**/*.ts |
./ |
Relative to current working directory | ./secrets/** |
| (none) | Same as ./ |
secrets/** |
Shell command bypass prevention:
Permission rules for Read, Edit, and WebFetch are also enforced when the agent runs equivalent shell commands. For example, if Read(./.env) is in deny, the agent cannot bypass it via cat .env in a shell command. Supported shell commands include cat, grep, curl, wget, cp, mv, rm, chmod, and many more. Unknown/safe commands (e.g. git) are unaffected by file/network rules.
Migrating from legacy settings:
| Legacy setting | Equivalent permissions rule |
Notes |
|---|---|---|
tools.allowed |
permissions.allow |
Auto-migrated on first load |
tools.exclude |
permissions.deny |
Auto-migrated on first load |
tools.core |
permissions.allow (allowlist) |
Auto-migrated; unlisted tools are disabled at registry level |
Example configuration:
{
"permissions": {
"allow": ["Bash(git *)", "Bash(npm run *)", "Read(//Users/alice/code/**)"],
"ask": ["Bash(git push *)", "Edit"],
"deny": ["Bash(rm -rf *)", "Read(.env)", "WebFetch(malicious.com)"]
}
}
Tip
Use
/permissionsin the interactive CLI to view, add, and remove rules without editingsettings.jsondirectly.
slashCommands
Controls which slash commands are available in the CLI. Useful for locking down the command surface in multi-tenant or enterprise deployments.
| Setting | Type | Description | Default |
|---|---|---|---|
slashCommands.disabled |
array of strings | Slash command names to hide and refuse to execute. Matched case-insensitively against the final command name (for extension commands this is the disambiguated form, e.g. myext.deploy). Merged as a union across scopes, so workspace settings can add to but not remove entries defined in user or system settings. |
undefined |
The same denylist can also be provided via the --disabled-slash-commands CLI
flag (comma-separated or repeated) and the QWEN_DISABLED_SLASH_COMMANDS
environment variable; values from all three sources are unioned together.
Example — lock down built-ins for a sandboxed deployment:
{
"slashCommands": {
"disabled": ["auth", "mcp", "extensions", "ide", "quit"]
}
}
With these values in a system-level settings.json (/etc/qwen-code/settings.json
or QWEN_CODE_SYSTEM_SETTINGS_PATH), users cannot shrink the denylist from
their own scope, and the disabled commands will not appear in autocomplete or
execute when typed.
Note
This setting only gates slash commands (e.g.
/auth,/mcp). It does not affect tool permissions — seepermissions.denyfor that. It also does not intercept keyboard shortcuts such asCtrl+CorEsc.
mcp
| Setting | Type | Description | Default |
|---|---|---|---|
mcp.serverCommand |
string | Command to start an MCP server. | undefined |
mcp.allowed |
array of strings | An allowlist of MCP servers to allow. Allows you to specify a list of MCP server names that should be made available to the model. This can be used to restrict the set of MCP servers to connect to. Note that this will be ignored if --allowed-mcp-server-names is set. |
undefined |
mcp.excluded |
array of strings | A denylist of MCP servers to exclude. A server listed in both mcp.excluded and mcp.allowed is excluded. Note that this will be ignored if --allowed-mcp-server-names is set. |
undefined |
Note
Security Note for MCP servers: These settings use simple string matching on MCP server names, which can be modified. If you're a system administrator looking to prevent users from bypassing this, consider configuring the
mcpServersat the system settings level such that the user will not be able to configure any MCP servers of their own. This should not be used as an airtight security mechanism.
lsp
Warning
Experimental Feature: LSP support is currently experimental and disabled by default. Enable it using the
--experimental-lspcommand line flag.
Language Server Protocol (LSP) provides code intelligence features like go-to-definition, find references, and diagnostics.
LSP server configuration is done through .lsp.json files in your project root directory, not through settings.json. See the LSP documentation for configuration details and examples.
security
| Setting | Type | Description | Default |
|---|---|---|---|
security.folderTrust.enabled |
boolean | Setting to track whether Folder trust is enabled. | false |
security.auth.selectedType |
string | The currently selected authentication type. | undefined |
security.auth.enforcedType |
string | The required auth type (useful for enterprises). | undefined |
security.auth.useExternal |
boolean | Whether to use an external authentication flow. | undefined |
advanced
| Setting | Type | Description | Default |
|---|---|---|---|
advanced.autoConfigureMemory |
boolean | Automatically configure Node.js memory limits. | false |
advanced.dnsResolutionOrder |
string | The DNS resolution order. | undefined |
advanced.excludedEnvVars |
array of strings | Environment variables to exclude from project context. Specifies environment variables that should be excluded from being loaded from project .env files. This prevents project-specific environment variables (like DEBUG=true) from interfering with the CLI behavior. Variables from .qwen/.env files are never excluded. |
["DEBUG","DEBUG_MODE"] |
advanced.bugCommand |
object | Configuration for the bug report command. Overrides the default URL for the /bug command. Properties: urlTemplate (string): A URL that can contain {title} and {info} placeholders. Example: "bugCommand": { "urlTemplate": "https://bug.example.com/new?title={title}&info={info}" } |
undefined |
mcpServers
Configures connections to one or more Model-Context Protocol (MCP) servers for discovering and using custom tools. Qwen Code attempts to connect to each configured MCP server to discover available tools. If multiple MCP servers expose a tool with the same name, the tool names will be prefixed with the server alias you defined in the configuration (e.g., serverAlias__actualToolName) to avoid conflicts. Note that the system might strip certain schema properties from MCP tool definitions for compatibility. At least one of command, url, or httpUrl must be provided. If multiple are specified, the order of precedence is httpUrl, then url, then command.
| Property | Type | Description | Optional |
|---|---|---|---|
mcpServers.<SERVER_NAME>.command |
string | The command to execute to start the MCP server via standard I/O. | Yes |
mcpServers.<SERVER_NAME>.args |
array of strings | Arguments to pass to the command. | Yes |
mcpServers.<SERVER_NAME>.env |
object | Environment variables to set for the server process. | Yes |
mcpServers.<SERVER_NAME>.cwd |
string | The working directory in which to start the server. | Yes |
mcpServers.<SERVER_NAME>.url |
string | The URL of an MCP server that uses Server-Sent Events (SSE) for communication. | Yes |
mcpServers.<SERVER_NAME>.httpUrl |
string | The URL of an MCP server that uses streamable HTTP for communication. | Yes |
mcpServers.<SERVER_NAME>.headers |
object | A map of HTTP headers to send with requests to url or httpUrl. |
Yes |
mcpServers.<SERVER_NAME>.timeout |
number | Timeout in milliseconds for requests to this MCP server. | Yes |
mcpServers.<SERVER_NAME>.trust |
boolean | Trust this server and bypass all tool call confirmations. | Yes |
mcpServers.<SERVER_NAME>.description |
string | A brief description of the server, which may be used for display purposes. | Yes |
mcpServers.<SERVER_NAME>.includeTools |
array of strings | List of tool names to include from this MCP server. When specified, only the tools listed here will be available from this server (allowlist behavior). If not specified, all tools from the server are enabled by default. | Yes |
mcpServers.<SERVER_NAME>.excludeTools |
array of strings | List of tool names to exclude from this MCP server. Tools listed here will not be available to the model, even if they are exposed by the server. Note: excludeTools takes precedence over includeTools - if a tool is in both lists, it will be excluded. |
Yes |
telemetry
Configures logging and metrics collection for Qwen Code. For more information, see telemetry.
| Setting | Type | Description | Default |
|---|---|---|---|
telemetry.enabled |
boolean | Whether or not telemetry is enabled. | |
telemetry.target |
string | The destination for collected telemetry. Supported values are local and gcp. |
|
telemetry.otlpEndpoint |
string | The endpoint for the OTLP Exporter. | |
telemetry.otlpProtocol |
string | The protocol for the OTLP Exporter (grpc or http). |
|
telemetry.logPrompts |
boolean | Whether or not to include the content of user prompts in the logs. | |
telemetry.includeSensitiveSpanAttributes |
boolean | Whether to include prompt, function_args, and response_text in spans created by the log-to-span bridge. Only controls bridge spans; OTel logs and other telemetry sinks may still receive response_text. |
false |
telemetry.outfile |
string | The file to write telemetry to when target is local. |
|
telemetry.useCollector |
boolean | Whether to use an external OTLP collector. |
Example settings.json
Here is an example of a settings.json file with the nested structure, new as of v0.3.0:
{
"proxy": "http://localhost:7890",
"general": {
"vimMode": true,
"preferredEditor": "code"
},
"ui": {
"theme": "GitHub",
"hideTips": false,
"customWittyPhrases": [
"You forget a thousand things every day. Make sure this is one of 'em",
"Connecting to AGI"
]
},
"tools": {
"approvalMode": "yolo",
"sandbox": "docker",
"sandboxImage": "ghcr.io/qwenlm/qwen-code:0.14.1",
"discoveryCommand": "bin/get_tools",
"callCommand": "bin/call_tool",
"exclude": ["write_file"]
},
"mcpServers": {
"mainServer": {
"command": "bin/mcp_server.py"
},
"anotherServer": {
"command": "node",
"args": ["mcp_server.js", "--verbose"]
}
},
"telemetry": {
"enabled": true,
"target": "local",
"otlpEndpoint": "http://localhost:4317",
"logPrompts": true,
"includeSensitiveSpanAttributes": false
},
"privacy": {
"usageStatisticsEnabled": true
},
"model": {
"name": "qwen3-coder-plus",
"maxSessionTurns": 10,
"enableOpenAILogging": false,
"openAILoggingDir": "~/qwen-logs",
},
"context": {
"fileName": ["CONTEXT.md", "QWEN.md"],
"includeDirectories": ["path/to/dir1", "~/path/to/dir2", "../path/to/dir3"],
"loadFromIncludeDirectories": true,
"fileFiltering": {
"respectGitIgnore": false
}
},
"advanced": {
"excludedEnvVars": ["DEBUG", "DEBUG_MODE", "NODE_ENV"]
}
}
Shell History
The CLI keeps a history of shell commands you run. To avoid conflicts between different projects, this history is stored in a project-specific directory within your user's home folder.
- Location:
~/.qwen/tmp/<project_hash>/shell_history<project_hash>is a unique identifier generated from your project's root path.- The history is stored in a file named
shell_history.
Environment Variables & .env Files
Environment variables are a common way to configure applications, especially for sensitive information (like tokens) or for settings that might change between environments.
Qwen Code can automatically load environment variables from .env files.
For authentication-related variables (like OPENAI_*) and the recommended .qwen/.env approach, see Authentication.
Tip
Environment Variable Exclusion: Some environment variables (like
DEBUGandDEBUG_MODE) are automatically excluded from project.envfiles by default to prevent interference with the CLI behavior. Variables from.qwen/.envfiles are never excluded. You can customize this behavior using theadvanced.excludedEnvVarssetting in yoursettings.jsonfile.
Environment Variables Table
| Variable | Description | Notes |
|---|---|---|
QWEN_TELEMETRY_ENABLED |
Set to true or 1 to enable telemetry. Any other value is treated as disabling it. |
Overrides the telemetry.enabled setting. |
QWEN_TELEMETRY_TARGET |
Sets the telemetry target (local or gcp). |
Overrides the telemetry.target setting. |
QWEN_TELEMETRY_OTLP_ENDPOINT |
Sets the OTLP endpoint for telemetry. | Overrides the telemetry.otlpEndpoint setting. |
QWEN_TELEMETRY_OTLP_PROTOCOL |
Sets the OTLP protocol (grpc or http). |
Overrides the telemetry.otlpProtocol setting. |
QWEN_TELEMETRY_LOG_PROMPTS |
Set to true or 1 to enable or disable logging of user prompts. Any other value is treated as disabling it. |
Overrides the telemetry.logPrompts setting. |
QWEN_TELEMETRY_INCLUDE_SENSITIVE_SPAN_ATTRIBUTES |
Set to true or 1 to include prompt, function_args, and response_text in spans created by the log-to-span bridge. Any other value disables it. |
Overrides the telemetry.includeSensitiveSpanAttributes setting. Only controls bridge spans; OTel logs and other telemetry sinks may still receive response_text. |
QWEN_TELEMETRY_OUTFILE |
Sets the file path to write telemetry to when the target is local. |
Overrides the telemetry.outfile setting. |
QWEN_TELEMETRY_USE_COLLECTOR |
Set to true or 1 to enable or disable using an external OTLP collector. Any other value is treated as disabling it. |
Overrides the telemetry.useCollector setting. |
QWEN_SANDBOX |
Alternative to the sandbox setting in settings.json. |
Accepts true, false, docker, podman, or a custom command string. |
QWEN_SANDBOX_IMAGE |
Overrides sandbox image selection for Docker/Podman. | Takes precedence over tools.sandboxImage. |
SEATBELT_PROFILE |
(macOS specific) Switches the Seatbelt (sandbox-exec) profile on macOS. |
permissive-open: (Default) Restricts writes to the project folder (and a few other folders, see packages/cli/src/utils/sandbox-macos-permissive-open.sb) but allows other operations. strict: Uses a strict profile that declines operations by default. <profile_name>: Uses a custom profile. To define a custom profile, create a file named sandbox-macos-<profile_name>.sb in your project's .qwen/ directory (e.g., my-project/.qwen/sandbox-macos-custom.sb). |
DEBUG or DEBUG_MODE |
(often used by underlying libraries or the CLI itself) Set to true or 1 to enable verbose debug logging, which can be helpful for troubleshooting. |
Note: These variables are automatically excluded from project .env files by default to prevent interference with the CLI behavior. Use .qwen/.env files if you need to set these for Qwen Code specifically. |
NO_COLOR |
Set to any value to disable all color output in the CLI. | |
CLI_TITLE |
Set to a string to customize the title of the CLI. | |
CODE_ASSIST_ENDPOINT |
Specifies the endpoint for the code assist server. | This is useful for development and testing. |
QWEN_CODE_MAX_OUTPUT_TOKENS |
Overrides the default maximum output tokens per response. When not set, Qwen Code uses an adaptive strategy: starts with 8K tokens and automatically retries with 64K if the response is truncated. Set this to a specific value (e.g., 16000) to use a fixed limit instead. |
Takes precedence over the capped default (8K) but is overridden by samplingParams.max_tokens in settings. Disables automatic escalation when set. Example: export QWEN_CODE_MAX_OUTPUT_TOKENS=16000 |
QWEN_CODE_UNATTENDED_RETRY |
Set to true or 1 to enable persistent retry mode. When enabled, transient API capacity errors (HTTP 429 Rate Limit and 529 Overloaded) are retried indefinitely with exponential backoff (capped at 5 minutes per retry) and heartbeat keepalives every 30 seconds on stderr. |
Designed for CI/CD pipelines and background automation where long-running tasks should survive temporary API outages. Must be set explicitly — CI=true alone does not activate this mode. See Headless Mode for details. Example: export QWEN_CODE_UNATTENDED_RETRY=1 |
QWEN_CODE_PROFILE_STARTUP |
Set to 1 to enable startup performance profiling. Writes a JSON timing report to ~/.qwen/startup-perf/ with per-phase durations. |
Only active inside the sandbox child process. Zero overhead when not set. Example: export QWEN_CODE_PROFILE_STARTUP=1 |
Command-Line Arguments
Arguments passed directly when running the CLI can override other configurations for that specific session.
For sandbox image selection, precedence is:
--sandbox-image > QWEN_SANDBOX_IMAGE > tools.sandboxImage > built-in default image.
Command-Line Arguments Table
| Argument | Alias | Description | Possible Values | Notes |
|---|---|---|---|---|
--model |
-m |
Specifies the Qwen model to use for this session. | Model name | Example: npm start -- --model qwen3-coder-plus |
--prompt |
-p |
Used to pass a prompt directly to the command. This invokes Qwen Code in a non-interactive mode. | Your prompt text | For scripting examples, use the --output-format json flag to get structured output. |
--prompt-interactive |
-i |
Starts an interactive session with the provided prompt as the initial input. | Your prompt text | The prompt is processed within the interactive session, not before it. Cannot be used when piping input from stdin. Example: qwen -i "explain this code" |
--system-prompt |
Overrides the built-in main session system prompt for this run. | Your prompt text | Loaded context files such as QWEN.md are still appended after this override. Can be combined with --append-system-prompt. |
|
--append-system-prompt |
Appends extra instructions to the main session system prompt for this run. | Your prompt text | Applied after the built-in prompt and loaded context files. Can be combined with --system-prompt. See Headless Mode for examples. |
|
--output-format |
-o |
Specifies the format of the CLI output for non-interactive mode. | text, json, stream-json |
text: (Default) The standard human-readable output. json: A machine-readable JSON output emitted at the end of execution. stream-json: Streaming JSON messages emitted as they occur during execution. For structured output and scripting, use the --output-format json or --output-format stream-json flag. See Headless Mode for detailed information. |
--input-format |
Specifies the format consumed from standard input. | text, stream-json |
text: (Default) Standard text input from stdin or command-line arguments. stream-json: JSON message protocol via stdin for bidirectional communication. Requirement: --input-format stream-json requires --output-format stream-json to be set. When using stream-json, stdin is reserved for protocol messages. See Headless Mode for detailed information. |
|
--include-partial-messages |
Include partial assistant messages when using stream-json output format. When enabled, emits stream events (message_start, content_block_delta, etc.) as they occur during streaming. |
Default: false. Requirement: Requires --output-format stream-json to be set. See Headless Mode for detailed information about stream events. |
||
--sandbox |
-s |
Enables sandbox mode for this session. | ||
--sandbox-image |
Sets the sandbox image URI. | |||
--debug |
-d |
Enables debug mode for this session, providing more verbose output. | ||
--all-files |
-a |
If set, recursively includes all files within the current directory as context for the prompt. | ||
--help |
-h |
Displays help information about command-line arguments. | ||
--show-memory-usage |
Displays the current memory usage. | |||
--yolo |
Enables YOLO mode, which automatically approves all tool calls. | |||
--approval-mode |
Sets the approval mode for tool calls. | plan, default, auto-edit, yolo |
Supported modes: plan: Analyze only—do not modify files or execute commands. default: Require approval for file edits or shell commands (default behavior). auto-edit: Automatically approve edit tools (edit, write_file) while prompting for others. yolo: Automatically approve all tool calls (equivalent to --yolo). Cannot be used together with --yolo. Use --approval-mode=yolo instead of --yolo for the new unified approach. Example: qwen --approval-mode auto-editSee more about Approval Mode. |
|
--allowed-tools |
A comma-separated list of tool names that will bypass the confirmation dialog. | Tool names | Example: qwen --allowed-tools "Shell(git status)" |
|
--disabled-slash-commands |
Slash command names to hide/disable (comma-separated or repeated). Unioned with the slashCommands.disabled setting and the QWEN_DISABLED_SLASH_COMMANDS environment variable. Matched case-insensitively against the final command name. |
Command names | Example: qwen --disabled-slash-commands "auth,mcp,extensions" |
|
--telemetry |
Enables telemetry. | |||
--telemetry-target |
Sets the telemetry target. | See telemetry for more information. | ||
--telemetry-otlp-endpoint |
Sets the OTLP endpoint for telemetry. | See telemetry for more information. | ||
--telemetry-otlp-protocol |
Sets the OTLP protocol for telemetry (grpc or http). |
Defaults to grpc. See telemetry for more information. |
||
--telemetry-log-prompts |
Enables logging of prompts for telemetry. | See telemetry for more information. | ||
--checkpointing |
Enables checkpointing. | |||
--acp |
Enables ACP mode (Agent Client Protocol). Useful for IDE/editor integrations like Zed. | Stable. Replaces the deprecated --experimental-acp flag. |
||
--experimental-lsp |
Enables experimental LSP (Language Server Protocol) feature for code intelligence (go-to-definition, find references, diagnostics, etc.). | Experimental. Requires language servers to be installed. | ||
--extensions |
-e |
Specifies a list of extensions to use for the session. | Extension names | If not provided, all available extensions are used. Use the special term qwen -e none to disable all extensions. Example: qwen -e my-extension -e my-other-extension |
--list-extensions |
-l |
Lists all available extensions and exits. | ||
--proxy |
Sets the proxy for the CLI. | Proxy URL | Example: --proxy http://localhost:7890. |
|
--include-directories |
Includes additional directories in the workspace for multi-directory support. | Directory paths | Can be specified multiple times or as comma-separated values. 5 directories can be added at maximum. Example: --include-directories /path/to/project1,/path/to/project2 or --include-directories /path/to/project1 --include-directories /path/to/project2 |
|
--screen-reader |
Enables screen reader mode, which adjusts the TUI for better compatibility with screen readers. | |||
--version |
Displays the version of the CLI. | |||
--openai-logging |
Enables logging of OpenAI API calls for debugging and analysis. | This flag overrides the enableOpenAILogging setting in settings.json. |
||
--openai-logging-dir |
Sets a custom directory path for OpenAI API logs. | Directory path | This flag overrides the openAILoggingDir setting in settings.json. Supports absolute paths, relative paths, and ~ expansion. Example: qwen --openai-logging-dir "~/qwen-logs" --openai-logging |
Context Files (Hierarchical Instructional Context)
While not strictly configuration for the CLI's behavior, context files (defaulting to QWEN.md but configurable via the context.fileName setting) are crucial for configuring the instructional context (also referred to as "memory"). This powerful feature allows you to give project-specific instructions, coding style guides, or any relevant background information to the AI, making its responses more tailored and accurate to your needs. The CLI includes UI elements, such as an indicator in the footer showing the number of loaded context files, to keep you informed about the active context.
- Purpose: These Markdown files contain instructions, guidelines, or context that you want the Qwen model to be aware of during your interactions. The system is designed to manage this instructional context hierarchically.
Example Context File Content (e.g. QWEN.md)
Here's a conceptual example of what a context file at the root of a TypeScript project might contain:
# Project: My Awesome TypeScript Library
## General Instructions:
- When generating new TypeScript code, please follow the existing coding style.
- Ensure all new functions and classes have JSDoc comments.
- Prefer functional programming paradigms where appropriate.
- All code should be compatible with TypeScript 5.0 and Node.js 20+.
## Coding Style:
- Use 2 spaces for indentation.
- Interface names should be prefixed with `I` (e.g., `IUserService`).
- Private class members should be prefixed with an underscore (`_`).
- Always use strict equality (`===` and `!==`).
## Specific Component: `src/api/client.ts`
- This file handles all outbound API requests.
- When adding new API call functions, ensure they include robust error handling and logging.
- Use the existing `fetchWithRetry` utility for all GET requests.
## Regarding Dependencies:
- Avoid introducing new external dependencies unless absolutely necessary.
- If a new dependency is required, please state the reason.
This example demonstrates how you can provide general project context, specific coding conventions, and even notes about particular files or components. The more relevant and precise your context files are, the better the AI can assist you. Project-specific context files are highly encouraged to establish conventions and context.
- Hierarchical Loading and Precedence: The CLI implements a hierarchical memory system by loading context files (e.g.,
QWEN.md) from several locations. Content from files lower in this list (more specific) typically overrides or supplements content from files higher up (more general). The exact concatenation order and final context can be inspected using the/memory showcommand. The typical loading order is:- Global Context File:
- Location:
~/.qwen/<configured-context-filename>(e.g.,~/.qwen/QWEN.mdin your user home directory). - Scope: Provides default instructions for all your projects.
- Location:
- Project Root & Ancestors Context Files:
- Location: The CLI searches for the configured context file in the current working directory and then in each parent directory up to either the project root (identified by a
.gitfolder) or your home directory. - Scope: Provides context relevant to the entire project or a significant portion of it.
- Location: The CLI searches for the configured context file in the current working directory and then in each parent directory up to either the project root (identified by a
- Global Context File:
- Concatenation & UI Indication: The contents of all found context files are concatenated (with separators indicating their origin and path) and provided as part of the system prompt. The CLI footer displays the count of loaded context files, giving you a quick visual cue about the active instructional context.
- Importing Content: You can modularize your context files by importing other Markdown files using the
@path/to/file.mdsyntax. For more details, see the Memory Import Processor documentation. - Commands for Memory Management:
- Use
/memory refreshto force a re-scan and reload of all context files from all configured locations. This updates the AI's instructional context. - Use
/memory showto display the combined instructional context currently loaded, allowing you to verify the hierarchy and content being used by the AI. - See the Commands documentation for full details on the
/memorycommand and its sub-commands (showandrefresh).
- Use
By understanding and utilizing these configuration layers and the hierarchical nature of context files, you can effectively manage the AI's memory and tailor Qwen Code's responses to your specific needs and projects.
Sandbox
Qwen Code can execute potentially unsafe operations (like shell commands and file modifications) within a sandboxed environment to protect your system.
Sandbox is disabled by default, but you can enable it in a few ways:
- Using
--sandboxor-sflag. - Setting
QWEN_SANDBOXenvironment variable. - Sandbox is enabled when using
--yoloor--approval-mode=yoloby default.
By default, it uses a pre-built qwen-code-sandbox Docker image.
For project-specific sandboxing needs, you can create a custom Dockerfile at .qwen/sandbox.Dockerfile in your project's root directory. This Dockerfile can be based on the base sandbox image:
FROM qwen-code-sandbox
# Add your custom dependencies or configurations here
# For example:
# RUN apt-get update && apt-get install -y some-package
# COPY ./my-config /app/my-config
When .qwen/sandbox.Dockerfile exists, you can use BUILD_SANDBOX environment variable when running Qwen Code to automatically build the custom sandbox image:
BUILD_SANDBOX=1 qwen -s
Usage Statistics
To help us improve Qwen Code, we collect anonymized usage statistics. This data helps us understand how the CLI is used, identify common issues, and prioritize new features.
What we collect:
- Tool Calls: We log the names of the tools that are called, whether they succeed or fail, and how long they take to execute. We do not collect the arguments passed to the tools or any data returned by them.
- API Requests: We log the model used for each request, the duration of the request, and whether it was successful. We do not collect the content of the prompts or responses.
- Session Information: We collect information about the configuration of the CLI, such as the enabled tools and the approval mode.
What we DON'T collect:
- Personally Identifiable Information (PII): We do not collect any personal information, such as your name, email address, or API keys.
- Prompt and Response Content: We do not log the content of your prompts or the responses from the model.
- File Content: We do not log the content of any files that are read or written by the CLI.
How to opt out:
You can opt out of usage statistics collection at any time by setting the usageStatisticsEnabled property to false under the privacy category in your settings.json file:
{
"privacy": {
"usageStatisticsEnabled": false
}
}
Note
When usage statistics are enabled, events are sent to an Alibaba Cloud RUM collection endpoint.