Product case study · November 20, 2026

Ask Claude Code why a copied config changes the original

A dict.copy() can leave nested settings shared. Reproduce the leak, ask Claude Code for a bounded repair, and test that two jobs stay independent.

Reading time
6 min
Checked
Nov 20, 2026
Two paper folders hold separate blue card stacks, with a severed charcoal thread between them on warm paper.
Separate jobs need separate mutable settings, all the way down.
Bottom line

Test what happens after you edit the copy. A new outer dictionary proves very little about the lists inside it.

You copy a job’s settings, add one recipient, and discover that the next job has inherited that recipient too. The copy is real. The independence isn’t.

This is a useful bug to give Claude Code because the reassuring test passes: the new dictionary is a different object. The failure lives one level deeper. I would rather test one edit to a nested list than collect a dozen assertions that the outer dictionary looks right.

You’ll need Python 3, Bash on macOS, Linux or WSL, and an installed, signed-in Claude Code. The fixture and reference repair below were tested locally; the agent prompt is an authored exercise, not a recorded Claude run. Official documentation was checked November 20, 2026.

Make two jobs share a list by accident

Run this in a terminal to create a disposable practice folder:

config_lab=$(mktemp -d /tmp/claude-config.XXXXXX)
cd "$config_lab"
cat > job_config.py <<'PY'
def clone_config(template):
    return template.copy()


if __name__ == "__main__":
    template = {"delivery": {"recipients": ["editor"]}}
    job = clone_config(template)
    job["delivery"]["recipients"].append("reviewer")
    print("different outer dict:", job is not template)
    print("template recipients:", template["delivery"]["recipients"])
PY
python3 job_config.py

The broken version prints:

different outer dict: True
template recipients: ['editor', 'reviewer']

Python documents dict.copy() as a shallow copy. The outer dictionary is new, but its values still refer to the same objects. Here, both dictionaries point to the same delivery dictionary and ultimately the same recipients list.

This is a different problem from a mutable default argument. There is no default parameter in this function. The caller explicitly supplies the template, and the helper copies too little of it.

Define what independence means

Save this as SPEC.md in the practice folder:

clone_config(template) receives a finite, acyclic tree of plain dictionaries
with string keys, lists, strings, numbers, booleans and None.
Return an equal configuration with no mutable dictionary or list shared
with the input. Preserve all values and keys, including unknown fields.
Mutating either the input or output after the call must not affect the other.
Two separate calls must produce mutually independent configurations.
An empty dictionary returns an independent empty dictionary.
Custom objects, cycles, files and network clients are outside this contract.
Only job_config.py may change. Preserve its demo.
Keep SPEC.md and test_job_config.py fixed. Use only the standard library.

The unknown-fields rule matters. An agent could rebuild only delivery.recipients and appear to fix the demo while silently dropping the rest of your settings. We want a general copy of this limited data shape, not a special case for one visible list.

Save these four tests as test_job_config.py:

import unittest
from job_config import clone_config


def sample():
    return {
        "delivery": {"recipients": ["editor"]},
        "steps": [{"options": ["draft"]}],
        "extra": {"enabled": False, "limit": 0, "note": None},
    }


class ConfigTests(unittest.TestCase):
    def test_equal_values_and_new_outer_dict(self):
        source = sample()
        result = clone_config(source)
        self.assertEqual(result, sample())
        self.assertIsNot(result, source)

    def test_edits_to_result_leave_source_unchanged(self):
        source = sample()
        result = clone_config(source)
        result["delivery"]["recipients"].append("reviewer")
        result["steps"][0]["options"].append("final")
        result["extra"]["enabled"] = True
        self.assertEqual(source, sample())

    def test_source_and_sibling_edits_stay_separate(self):
        source = sample()
        first = clone_config(source)
        second = clone_config(source)
        source["steps"][0]["options"].append("source-only")
        first["delivery"]["recipients"].append("first-only")
        self.assertEqual(second, sample())
        self.assertEqual(first["steps"], sample()["steps"])
        self.assertEqual(source["delivery"], sample()["delivery"])

    def test_empty_dictionary(self):
        source = {}
        result = clone_config(source)
        self.assertEqual(result, {})
        self.assertIsNot(result, source)

Run the baseline:

python3 -m unittest -v test_job_config

Locally, four tests ran and two failed. Equality and outer identity passed; the mutation tests failed. That’s the evidence worth handing to the agent. Python’s unittest reference documents this invocation and the assertions.

Ask Claude to repair the ownership boundary

Start claude inside the practice folder and paste:

Read SPEC.md, job_config.py and test_job_config.py.
Run python3 -m unittest -v test_job_config and report the actual baseline.
Explain which objects the source and returned config share.
Change only job_config.py to satisfy the contract; preserve the demo.
Do not change the tests or spec, install packages, commit or delegate.
Run the same four tests and python3 job_config.py after the repair.
Explain why checking only outer dictionary identity missed this bug.
State the limits of your repair for objects outside the specified data shape.

Anthropic’s common workflows recommends giving Claude reproduction steps and error details when debugging. Here, the reproduction is small enough to read before accepting any patch. Review the active permissions and proposed commands; the prompt’s file boundary is an instruction, not a filesystem sandbox.

If Claude proposes {**template} or dict(template), ask it to rerun the mutation tests. A different spelling for constructing the outer dictionary doesn’t establish independence for its nested values.

Check the repair by changing both sides

For the stated plain-data contract, this reference repair passed all four tests locally:

from copy import deepcopy


def clone_config(template):
    return deepcopy(template)

Replace the function and add the import; keep the demo below it. Python’s copy documentation explains that deep copying recursively copies contained objects. This fits the small settings tree in our exercise. It also warns that deep copying may copy data intended to be shared, so I wouldn’t make it a blanket rule for every object in an application.

Run the checks yourself after leaving Claude:

python3 -m unittest -v test_job_config
python3 job_config.py

The repaired demo prints:

different outer dict: True
template recipients: ['editor']

If only the outer-identity test passes, inspect the nested assignments again. If the test command reports zero tests, check the filename and the four test_ methods. If importing job_config fails, run the command from the folder containing both Python files.

For a large application, decide which data should be owned by each job before copying it. A shared connection pool is different from a per-job recipients list; this lab deliberately excludes service objects. Copying an entire application state can be expensive and semantically wrong even when the code runs.

The useful review question is: what can the caller mutate after this function returns? Put that mutation in a test. A copy that survives the edit is much better evidence than a copy that merely has a new identity.

Sources

Checked November 20, 2026:

Put this to work

Why a new dictionary can still share mutable values with its source.

Try

Repair a small Python configuration helper in a disposable folder.

Prove it worked

Run four unchanged tests before and after the repair.

Keep in view

  • Reproduce a nested list changing through a supposedly independent copy.
  • Give Claude Code a fixed contract and four acceptance tests.
  • Check isolation in both directions, including between two jobs.
Learn the workflow: Set up a Claude Code project session