{"record":{"id":"6d464bc733d6c635","repo":"github/copilot-sdk","slug":"missing-required-field-cwd-in-sessioncontext","errorCode":null,"errorMessage":"Missing required field 'cwd' in SessionContext","messagePattern":"Missing required field 'cwd' in SessionContext","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/copilot/client.py","lineNumber":1200,"sourceCode":"# Session Metadata Types\n# ============================================================================\n\n\n@dataclass\nclass SessionContext:\n    \"\"\"Working directory context for a session\"\"\"\n\n    working_directory: str  # Working directory where the session was created\n    git_root: str | None = None  # Git repository root (if in a git repo)\n    repository: str | None = None  # GitHub repository in \"owner/repo\" format\n    branch: str | None = None  # Current git branch\n\n    @staticmethod\n    def from_dict(obj: Any) -> SessionContext:\n        assert isinstance(obj, dict)\n        cwd = obj.get(\"cwd\")\n        if cwd is None:\n            raise ValueError(\"Missing required field 'cwd' in SessionContext\")\n        return SessionContext(\n            working_directory=str(cwd),\n            git_root=obj.get(\"gitRoot\"),\n            repository=obj.get(\"repository\"),\n            branch=obj.get(\"branch\"),\n        )\n\n    def to_dict(self) -> dict:\n        result: dict = {\"cwd\": self.working_directory}\n        if self.git_root is not None:\n            result[\"gitRoot\"] = self.git_root\n        if self.repository is not None:\n            result[\"repository\"] = self.repository\n        if self.branch is not None:\n            result[\"branch\"] = self.branch\n        return result\n\n","sourceCodeStart":1182,"sourceCodeEnd":1218,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/python/copilot/client.py#L1182-L1218","documentation":"SessionContext.from_dict requires the 'cwd' key in the session context payload and raises ValueError when absent or None. 'cwd' is mapped to working_directory, which the library treats as mandatory; gitRoot/repository/branch are optional.","triggerScenarios":"A session-update/context event arrives without 'cwd'; the producer sends 'workingDirectory' or 'working_directory' instead of 'cwd'; the session was created without a working directory.","commonSituations":"Sessions started without an initial working directory on the server; key-renaming across server versions or intermediaries; test fixtures omitting cwd.","solutions":["Ensure the event producer includes 'cwd' in the SessionContext payload","Remap alternate keys before decoding: obj.setdefault('cwd', obj.get('workingDirectory'))","Always provide session_fs.initial_working_directory when creating the session so the server can report cwd","Fix fixtures/mocks to include 'cwd'"],"exampleFix":"// before\nSessionContext.from_dict({\"gitRoot\": \"/repo\"})\n// after\nSessionContext.from_dict({\"cwd\": \"/repo\", \"gitRoot\": \"/repo\"})","handlingStrategy":"type-guard","validationCode":"def has_cwd(obj) -> bool:\n    return isinstance(obj, dict) and obj.get(\"cwd\") is not None","typeGuard":"def is_session_context_payload(obj: object) -> bool:\n    return isinstance(obj, dict) and isinstance(obj.get(\"cwd\"), str)","tryCatchPattern":"try:\n    ctx = SessionContext.from_dict(payload)\nexcept ValueError as e:\n    if \"Missing required field 'cwd'\" in str(e):\n        payload.setdefault(\"cwd\", os.getcwd())\n        ctx = SessionContext.from_dict(payload)\n    else:\n        raise","preventionTips":["Always configure session_fs.initial_working_directory so the server can report cwd","Normalize key names ('workingDirectory' -> 'cwd') at the ingestion boundary","Keep session-context fixtures aligned with the server payload schema"],"tags":["python","deserialization","session-context","schema"],"backgroundTag":"missing-required-argument","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}