{"record":{"id":"f31023cbf7ff3e31","repo":"mastra-ai/mastra","slug":"unable-to-read-workspace-diff","errorCode":null,"errorMessage":"Unable to read workspace diff","messagePattern":"Unable to read workspace diff","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/routes/fs.ts","lineNumber":722,"sourceCode":"  const safePreviousPath = previousPath ? assertRelativePath(previousPath, 'previousPath') : undefined;\n  const handle = await sessionSandbox(session);\n  if (!handle) throw new Error('Session workspace is not available');\n\n  const pathspecs = safePreviousPath ? [safePreviousPath, safePath] : [safePath];\n  let result = await executeBoundedGitDiff(handle.sandbox, [\n    '--literal-pathspecs',\n    '-C',\n    handle.workdir,\n    'diff',\n    '--find-renames',\n    '--no-ext-diff',\n    '--no-color',\n    '--unified=3',\n    'HEAD',\n    '--',\n    ...pathspecs,\n  ]);\n  if (result.exitCode !== 0) throw new Error(result.stderr || 'Unable to read workspace diff');\n\n  if (!result.stdout) {\n    const untracked = await handle.sandbox.executeCommand(\n      'git',\n      ['--literal-pathspecs', '-C', handle.workdir, 'ls-files', '--others', '--exclude-standard', '--', safePath],\n      { timeout: 30_000 },\n    );\n    if (untracked.exitCode === 0 && untracked.stdout.trim()) {\n      result = await executeBoundedGitDiff(\n        handle.sandbox,\n        [\n          '-C',\n          handle.workdir,\n          'diff',\n          '--no-index',\n          '--no-ext-diff',\n          '--no-color',\n          '--unified=3',","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/routes/fs.ts#L704-L740","documentation":"The bounded `git diff HEAD -- <pathspec>` returned a non-zero exit code. The function surfaces git's stderr (falling back to a generic message) because no diff could be produced — typically a bad pathspec, a path outside the repo, or a corrupt/unavailable git state in the sandbox workdir.","triggerScenarios":"Diffing a path that doesn't match any tracked/untracked file pattern accepted by git; pathspec syntax errors; git failing in the sandbox (missing HEAD, not a repository, permission issues) with a non-zero exit.","commonSituations":"Typo'd or URL-encoded paths with characters git treats specially; diffing files in a workspace whose git metadata was stripped; newly created workdir without an initial commit so HEAD doesn't exist.","solutions":["Read the surfaced stderr (the thrown message) to identify the exact git failure and fix the pathspec accordingly.","Ensure the workspace has a valid HEAD (initial commit exists) before diffing; commit or initialize the repo if needed.","Check the session workdir is a real git repository with readable .git metadata.","Retry with an exact, relative path (assertRelativePath-normalized) without special characters."],"exampleFix":"// before\nawait getSessionWorkspaceDiff(session, '../outside/file.ts');\n// after\nconst ok = await stat(session, 'src/index.ts'); // confirm path exists & is inside workspace\nif (ok) await getSessionWorkspaceDiff(session, 'src/index.ts');","handlingStrategy":"try-catch","validationCode":"const handle = await sessionSandbox(session);\nconst stat = await handle.filesystem.stat(path);\nconst headOk = (await handle.sandbox.executeCommand('git', ['--literal-pathspecs', '-C', handle.workdir, 'rev-parse', 'HEAD'], { timeout: 30_000 })).exitCode === 0;\nconst ready = stat.type === 'file' && headOk;","typeGuard":"function diffablePath(p: string): boolean {\n  return !p.includes('..') && !p.startsWith('/') && p.trim() !== '';\n}","tryCatchPattern":"try {\n  const diff = await getSessionWorkspaceDiff(session, path);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('fatal:')) {\n    // stderr surfaced by the throw — log it and fix the pathspec/repo state\n    logger.error('git diff failed', { stderr: e.message, path });\n  } else throw e;\n}","preventionTips":["Ensure the workspace repo has an initial commit so HEAD exists.","Validate paths are relative, in-repo, and free of git-special characters.","Log git stderr (the thrown message) to diagnose pathspec/repo problems quickly."],"tags":["git","diff","exit-code"],"backgroundTag":"git-command-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}