Detect external changes with deep context (#1888)

<!-- CURSOR_SUMMARY -->
> [!NOTE]
> Adds commit-aware deep context by computing hasExternalChanges (via
latest assistant commit vs current repo + dirty check) and propagating
commitHash through messages/provider options.
> 
> - **Deep Smart Context**:
> - Add `hasExternalChanges` to `VersionedFiles`; compute by comparing
latest assistant `commitHash` with `getCurrentCommitHash` and checking
`isGitStatusClean`.
> - Make `sourceCommitHash` nullable; add `commitHash` in
`DyadEngineProviderOptions` and use it when scanning history.
> - **Chat Handling**:
> - Include `commitHash` in `messageHistory` and pass through
`providerOptions['dyad-engine']`.
> - **Git Utilities**:
> - New `isGitStatusClean(path)` supporting native git and
isomorphic-git.
> - **Tests/Snapshots**:
> - Mock `getCurrentCommitHash` and `isGitStatusClean`; update snapshot
to include `hasExternalChanges`.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
ad92d9dd5ead941de822e8da59c8819e4db8b775. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->







<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Detects external code changes in deep context by comparing the latest
assistant commit with the current repo state. Exposes a
hasExternalChanges flag so the engine can adapt responses when the
workspace diverges.

- **New Features**
  - Added hasExternalChanges to VersionedFiles.
- Computes by comparing the latest assistant commitHash with
getCurrentCommitHash and checking isGitStatusClean.
- Passes commitHash through chat messages and dyad-engine
providerOptions; sourceCommitHash is now nullable.
  - Defaults to true if detection fails (with a warning).

<sup>Written for commit 6ebb0b125c9a3421b4e5673870b204c9cb279265.
Summary will update automatically on new commits.</sup>

<!-- End of auto-generated description by cubic. -->
This commit is contained in:
Will Chen
2025-12-04 15:40:58 -08:00
committed by GitHub
parent 3701886505
commit 538745d546
5 changed files with 213 additions and 3 deletions

View File

@@ -39,6 +39,23 @@ export async function getCurrentCommitHash({
});
}
export async function isGitStatusClean({
path,
}: {
path: string;
}): Promise<boolean> {
const settings = readSettings();
if (settings.enableNativeGit) {
const { stdout } = await execAsync(`git -C "${path}" status --porcelain`);
return stdout.trim() === "";
} else {
const statusMatrix = await git.statusMatrix({ fs, dir: path });
return statusMatrix.every(
(row) => row[1] === 1 && row[2] === 1 && row[3] === 1,
);
}
}
export async function gitCommit({
path,
message,

View File

@@ -2,7 +2,11 @@ import { CodebaseFile, CodebaseFileReference } from "@/utils/codebase";
import { ModelMessage } from "@ai-sdk/provider-utils";
import crypto from "node:crypto";
import log from "electron-log";
import { getFileAtCommit } from "./git_utils";
import {
getCurrentCommitHash,
getFileAtCommit,
isGitStatusClean,
} from "./git_utils";
import { normalizePath } from "../../../shared/normalizePath";
const logger = log.scope("versioned_codebase_context");
@@ -11,10 +15,13 @@ export interface VersionedFiles {
fileIdToContent: Record<string, string>;
fileReferences: CodebaseFileReference[];
messageIndexToFilePathToFileId: Record<number, Record<string, string>>;
/** True if there are changes outside of files from the latest chat message (different commit or dirty git status) */
hasExternalChanges: boolean;
}
interface DyadEngineProviderOptions {
sourceCommitHash: string;
sourceCommitHash: string | null;
commitHash: string | null;
}
/**
@@ -211,9 +218,47 @@ export async function processChatMessagesWithVersionedFiles({
}
}
// Determine hasExternalChanges:
// Find the latest assistant message's commitHash
let latestCommitHash: string | undefined;
for (let i = chatMessages.length - 1; i >= 0; i--) {
const message = chatMessages[i];
if (message.role === "assistant") {
const engineOptions = message.providerOptions?.[
"dyad-engine"
] as unknown as DyadEngineProviderOptions;
if (engineOptions?.commitHash) {
latestCommitHash = engineOptions.commitHash;
break;
}
}
}
let hasExternalChanges = true; // Default to true if we can't determine
if (latestCommitHash) {
try {
// Get current commit hash
const currentCommitHash = await getCurrentCommitHash({ path: appPath });
// Check if git status is clean
const isClean = await isGitStatusClean({ path: appPath });
// hasExternalChanges is false only if commits match AND status is clean
hasExternalChanges = !(latestCommitHash === currentCommitHash && isClean);
logger.info(
`detected hasExternalChanges: ${hasExternalChanges} because latestCommitHash: ${latestCommitHash} and currentCommitHash: ${currentCommitHash} and isClean: ${isClean}`,
);
} catch (error) {
logger.warn("Failed to determine hasExternalChanges:", error);
// Keep default of true
}
}
return {
fileIdToContent,
fileReferences,
messageIndexToFilePathToFileId,
hasExternalChanges,
};
}