{"record":{"id":"e6b43e6b9f0a504b","repo":"agentscope-ai/agentscope","slug":"container-cp-failed-exit-process-returncode","errorCode":null,"errorMessage":"container cp failed (exit {process.returncode}): {stderr.decode(errors='replace')}","messagePattern":"container cp failed \\(exit (.+?)\\): (.+?)","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/agentscope/workspace/_applecontainer/_applecontainer_backend.py","lineNumber":207,"sourceCode":"            parent = posixpath.dirname(path)\n            if parent and parent != \"/\":\n                await self.exec_shell(\n                    [\"mkdir\", \"-p\", parent],\n                )\n\n            # Copy the temp file into the container.\n            container_path = f\"{self._container_id}:{path}\"\n            process = await asyncio.create_subprocess_exec(\n                \"container\",\n                \"cp\",\n                tmp_path,\n                container_path,\n                stdout=asyncio.subprocess.PIPE,\n                stderr=asyncio.subprocess.PIPE,\n            )\n            _stdout, stderr = await process.communicate()\n            if process.returncode != 0:\n                raise OSError(\n                    f\"container cp failed (exit {process.returncode}): \"\n                    f\"{stderr.decode(errors='replace')}\",\n                )\n        finally:\n            try:\n                os.unlink(tmp_path)\n            except OSError:\n                pass\n","sourceCodeStart":189,"sourceCodeEnd":216,"githubUrl":"https://github.com/agentscope-ai/agentscope/blob/e90f1c7592896cc95f6e5ee506194f533378247d/src/agentscope/workspace/_applecontainer/_applecontainer_backend.py#L189-L216","documentation":"AppleContainerWorkspace.write_file serializes bytes to a temp file and shells out to `container cp` to copy it into the container; a non-zero cp exit code raises OSError with the cp stderr. Indicates the container or destination path is broken.","triggerScenarios":"Calling await workspace.write_file(path, data) when the container is stopped/removed, the destination directory doesn't exist inside the container, permissions are wrong, or the Apple container CLI fails.","commonSituations":"Container was garbage-collected or stopped between operations, writing to a nested path whose parent directories don't exist in the container, disk pressure, or CLI version changes altering cp behavior.","solutions":["Ensure the container is still running: await workspace.exec_shell([\"true\"]) as a health check, recreate if needed.","mkdir -p the parent directory inside the container before writing to a nested path.","Write to the workspace root or pre-create directory structure at provisioning.","Check stderr in the OSError message for the exact cp failure."],"exampleFix":"# before\nawait ws.write_file(\"src/new_dir/main.py\", data)\n\n# after\nawait ws.exec_shell([\"mkdir\", \"-p\", \"src/new_dir\"])\nawait ws.write_file(\"src/new_dir/main.py\", data)","handlingStrategy":"try-catch","validationCode":"await ws.exec_shell([\"mkdir\", \"-p\", posixpath.dirname(path)])","typeGuard":null,"tryCatchPattern":"try:\n    await ws.write_file(path, data)\nexcept OSError as e:\n    if \"container cp failed\" in str(e):\n        await ws.exec_shell([\"mkdir\", \"-p\", dirname])\n        await ws.write_file(path, data)\n    else:\n        raise","preventionTips":["Pre-create destination directories inside the container.","Health-check the container before batch writes.","Parse the embedded cp stderr for root cause."],"tags":["applecontainer","workspace","container","cp-failed","write-file"],"backgroundTag":"container-copy-failed","analyzedSha":"e90f1c7592896cc95f6e5ee506194f533378247d","analyzedAt":"2026-08-28T18:24:12.087Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}