Version Control
Relevant source files
The following files were used as context for generating this wiki page:
- src/tests/versioned_codebase_context.test.ts
- src/prompts/system_prompt.ts
- e2e-tests/snapshots/security_review.spec.ts_security-review---multi-select-and-fix-issues-1.aria.yml
- e2e-tests/snapshots/problems.spec.ts_problems-auto-fix---complex-delete-rename-write-1.txt
- src/prompts/summarize_chat_system_prompt.ts
The Dyad application incorporates a version control system deeply integrated with its AI chat functionality. This system leverages Git to track codebase changes over time, enabling the AI to access historical versions of files. This provides essential context during development, allows for accurate code analysis based on the state of the repository at a specific point in the conversation, and includes a mechanism to detect external changes made to the codebase outside of the AI's actions.
The core of this feature is the "Versioned Codebase Context", which processes chat history to reconstruct the file states referenced by the AI in previous messages. This ensures that the AI's understanding of the codebase remains consistent with the project's evolution.
Versioned Codebase Context
The primary function responsible for managing historical file versions is processChatMessagesWithVersionedFiles. This function analyzes the entire chat history to build a comprehensive context that includes file contents from various commits.
Sources: [src/tests/versioned_codebase_context.test.ts:210-226]
Context Building Data Flow
The process of building the versioned context involves several steps, triggered when preparing the context for the AI. It iterates through past assistant messages to find file references and fetches their content from the specific Git commit associated with that message.
The following diagram illustrates the data flow within the processChatMessagesWithVersionedFiles function.
Sources: [src/tests/versioned_codebase_context.test.ts:285-312]
File Path Extraction
File paths are extracted from assistant messages by the parseFilesFromMessage function. It searches for two specific XML-like tags:
<dyad-read path="..."></dyad-read>: Explicitly reads a single file.<dyad-code-search-result>...</dyad-code-search-result>: Contains a list of file paths, typically from a search tool, separated by newlines.
The function extracts all unique file paths from these tags within a given message string.
Sources: [src/tests/versioned_codebase_context.test.ts:18-199]
Key Data Structures
The processChatMessagesWithVersionedFiles function returns an object containing several key pieces of information that constitute the versioned context.
| Key | Type | Description |
|---|---|---|
fileIdToContent | Record<string, string> | A map where the key is the SHA-256 hash of a file's content and the value is the content itself. This deduplicates identical file versions. |
fileReferences | CodebaseFileReference[] | An array of files passed directly to the function (representing the current state), with their paths and content hashes. |
messageIndexToFilePathToFileId | Record<number, ...> | A map where the key is the index of a message in the chat history. The value is another map from a file path to its content hash (fileId). |
hasExternalChanges | boolean | A flag indicating if the codebase has been modified outside of the AI's actions since the last interaction. |
Sources: [src/tests/versioned_codebase_context.test.ts:210-239, 608-629]
Git Interaction Layer
All direct interactions with the Git repository are abstracted into a git_utils module. While the source code for this module is not provided, its interface is clearly defined by the mocks used in the testing suite.
This sequence diagram shows how the versioned_codebase_context module interacts with git_utils to retrieve file content.
Sources: [src/tests/versioned_codebase_context.test.ts:10-14, 299-304]
Core Git Utility Functions
| Function | Parameters | Description |
|---|---|---|
getFileAtCommit | path, filePath, commitHash | Retrieves the content of a specific filePath at a given commitHash from the Git repository located at path. Returns null if not found. |
getCurrentCommitHash | path | Fetches the hash of the current HEAD commit of the repository. |
isGitStatusClean | path | Checks if the working directory of the repository has any uncommitted changes. Returns true if clean, false otherwise. |
Sources: [src/tests/versioned_codebase_context.test.ts:11-13]
Detecting External Changes
A critical feature of the version control system is its ability to detect if the codebase has been modified outside of the AI's workflow. This is handled by the hasExternalChanges boolean flag.
The logic is as follows:
- Find the latest assistant message in the chat history that has a
commitHash. - If no such message exists,
hasExternalChangesdefaults totrue. - If a message is found, compare its
commitHashwith the repository's current HEAD commit hash (obtained viagetCurrentCommitHash). - Check if the repository's working directory is clean (via
isGitStatusClean). hasExternalChangesis set totrueif the commit hashes do not match OR if the working directory is not clean. Otherwise, it isfalse.
This ensures the AI can be notified that its understanding of the codebase might be outdated.
Sources: [src/tests/versioned_codebase_context.test.ts:608-706]
Version Creation
New versions (commits) are created when the AI performs actions that modify the filesystem. The system prompt defines several tags for file manipulation, which are interpreted by the backend to execute file operations and subsequently commit the changes. These actions create a new version point in the Git history.
<dyad-write path="..." description="...">...</dyad-write>: Creates or overwrites a file.<dyad-rename from="..." to="..."></dyad-rename>: Renames a file.<dyad-delete path="..."></dyad-delete>: Deletes a file.
An example from a test snapshot shows these tags in use, which would result in a new commit.
<dyad-delete path="src/main.tsx">
</dyad-delete>
<dyad-rename from="src/App.tsx" to="src/main.tsx">
</dyad-rename>
<dyad-write path="src/main.tsx" description="final main.tsx file.">
finalMainTsxFileWithError();
</dyad-write>
Sources: src/prompts/system_prompt.ts:110-114, [e2e-tests/snapshots/problems.spec.ts_problems-auto-fix---complex-delete-rename-write-1.txt:104-110]