feat(walkthrough): guided AI walkthrough for diffs, branches, and PRs (#2572)
A diff is ordered by file path, which is almost never the order in which a change makes sense. This adds a Walkthrough surface that reorders it: the model groups related hunks into stops, explains what each group changes about behavior, and orders the stops so each builds on the last. It explains and orders; judging code stays with the existing Review action. Reviews uncommitted work (all, staged, unstaged), a branch against its base, or a pull request. Generation is always user-initiated — nothing runs on a timer, on a file change, or as a side effect of opening a panel. Invariants worth preserving: - Hunk identity is derived on the server and only there. Ids are content hashes, so an anchor that no longer resolves is proof the code it described changed, and staleness needs no heuristics. The client matches ids to ids and never recomputes them; two implementations would have to agree forever. - The digest is never truncated. A diff that does not fit the model's context is refused with an actionable reason, because a walkthrough written against half a diff reads as confident and is wrong. - Nothing disappears. Lockfiles and other generated output are excluded from the model's input by name — never by size — and everything no stop covers is listed at the end, so "have I seen all of it" stays answerable. - Cost is explicit. Results are content-addressed, so returning the working tree to an earlier state costs nothing; generation outlives its request, so a refresh detaches the client rather than discarding paid-for work, and only an explicit cancel stops it. Supporting changes to shared modules: - git: expose the existing getRangeDiff as GET /api/git listUntrackedPaths and getUntrackedDiffs. The latter resolve the repository once for a batch instead of per file, taking a panel ~340ms on an 80-file working tree. - small-model: structured output across four wire forma and abort signal, and an onOverflow policy so an oversized prompt fails loudly instead of being silently clipped. A provider remembered so the prompt-side fallback goes first next time. - models.dev metadata: surface structured_output as tri false blocks a model, a missing field does not, because the catalog omits it for roughly half of all models. Desktop and tablet only: VS Code serves Git through its these routes, and the mobile shell does not consume the surface registry. Docs: packages/docs walkthrough page in English and all eight locales.
This commit is contained in:
committed by
GitHub
parent
b1ec34162e
commit
34d0ff7383
@@ -26,9 +26,11 @@ The following functions are exported and used by the web server:
|
||||
### Status and Diff Operations
|
||||
- `getStatus(directory)`: Get comprehensive Git status including current branch, tracking, ahead/behind, file changes, diff stats, merge/rebase state.
|
||||
- `getDiff(directory, { path, staged, contextLines })`: Get diff output for files or entire working tree. Untracked symbolic links are represented as link entries without following their targets.
|
||||
- `getRangeDiff(directory, { base, head, path, contextLines })`: Get diff between two refs.
|
||||
- `getRangeDiff(directory, { base, head, path, contextLines })`: Get diff between two refs. Uses three-dot `base...head` semantics, so work merged into `head` from `base` is excluded and only the branch's own changes are returned. Prefers `origin/<base>` when that remote-tracking ref exists, so a stale local base branch does not resurface already-merged commits. Exposed as `GET /api/git/range-diff` (`path` optional; omit it for the whole range).
|
||||
- `getRangeFiles(directory, { base, head })`: Get list of changed files between two refs.
|
||||
- `getFileDiff(directory, { path, staged })`: Get original and modified file contents for a single file (handles images as data URLs and symbolic links as their link-target text).
|
||||
- `listUntrackedPaths(directory)`: List individual untracked file paths honoring ignore rules. Much cheaper than `getStatus` when that is all a caller needs. Deliberately not `--directory`: collapsed directory entries end in a slash and are rejected by the per-file diff helpers, so a caller would silently lose every file inside a new directory.
|
||||
- `getUntrackedDiffs(directory, filePaths, { concurrency, contextLines })`: Diffs for untracked files against an empty tree. Resolves the repository context once instead of per file (`getDiff` re-resolves every call, costing an extra `rev-parse` each time) and bounds how many diff processes run at once. Returns one entry per input path in order; unreadable paths yield `''` rather than failing the batch.
|
||||
- `collectDiffs(directory, files)`: Collect diff output for multiple files.
|
||||
- `revertFile(directory, filePath, options)`: Revert a file. Default scope `all` discards staged and working-tree changes; scope `working` discards only unstaged/working-tree changes.
|
||||
- `stageFile(directory, filePath)`: Add one file path to the index.
|
||||
@@ -109,6 +111,9 @@ The following functions are internal helpers used by exported functions:
|
||||
- `mergeInProgress`: Object with `{ head, message }` if merge in progress.
|
||||
- `rebaseInProgress`: Object with `{ headName, onto }` if rebase in progress.
|
||||
|
||||
### Runtime availability of range diffs
|
||||
- `GET /api/git/range-diff` is served by the OpenChamber web server, so it is available to web, desktop, and mobile clients. The shared `GitAPI.getGitRangeDiff` is therefore optional: web supplies the HTTP implementation, and VS Code does not implement it because the extension host serves Git through its own bridge rather than these routes. Features built on range diffs (currently the AI diff walkthrough) are not offered in VS Code.
|
||||
|
||||
### Staged and unstaged change handling
|
||||
- `status.files` exposes both `index` and `working_dir` codes. Shared UI uses these as separate scopes: staged rows are derived from non-empty `index` statuses, while unstaged rows are derived from `working_dir` statuses and untracked files.
|
||||
- A file with both staged and unstaged changes can appear in both UI sections. Staged rows request diffs with `staged: true`; unstaged rows request normal working-tree diffs.
|
||||
|
||||
@@ -397,6 +397,37 @@ export function registerGitRoutes(app) {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/git/range-diff', async (req, res) => {
|
||||
const { getRangeDiff } = await getGitLibraries();
|
||||
try {
|
||||
const directory = req.query.directory;
|
||||
if (!directory || typeof directory !== 'string') {
|
||||
return res.status(400).json({ error: 'directory parameter is required' });
|
||||
}
|
||||
|
||||
const base = req.query.base;
|
||||
const head = req.query.head;
|
||||
if (!base || typeof base !== 'string' || !head || typeof head !== 'string') {
|
||||
return res.status(400).json({ error: 'base and head parameters are required' });
|
||||
}
|
||||
|
||||
const pathParam = typeof req.query.path === 'string' && req.query.path ? req.query.path : undefined;
|
||||
const context = req.query.context ? parseInt(String(req.query.context), 10) : undefined;
|
||||
|
||||
const diff = await getRangeDiff(directory, {
|
||||
base,
|
||||
head,
|
||||
path: pathParam,
|
||||
contextLines: Number.isFinite(context) ? context : 3,
|
||||
});
|
||||
|
||||
res.json({ diff });
|
||||
} catch (error) {
|
||||
console.error('Failed to get git range diff:', error);
|
||||
res.status(500).json({ error: error.message || 'Failed to get git range diff' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/git/revert', async (req, res) => {
|
||||
const { revertFile } = await getGitLibraries();
|
||||
try {
|
||||
|
||||
@@ -488,6 +488,16 @@ const createRepositoryGitContext = async (directory) => {
|
||||
return { directoryPath, directoryGit, repoRoot, git };
|
||||
};
|
||||
|
||||
/**
|
||||
* Absolute repository root for a directory anywhere inside it. Callers that key
|
||||
* persisted data by repository need this so two directories in the same
|
||||
* repository do not address different records.
|
||||
*/
|
||||
export async function getRepositoryRoot(directory) {
|
||||
const { repoRoot } = await createRepositoryGitContext(directory);
|
||||
return repoRoot;
|
||||
}
|
||||
|
||||
const resolveGitInternalPath = async (repoRoot, git, gitPath) => {
|
||||
const resolved = await git.raw(['rev-parse', '--git-path', gitPath]);
|
||||
return path.resolve(repoRoot, resolved.trim());
|
||||
@@ -2427,6 +2437,78 @@ export async function getDiff(directory, { path: filePath, staged = false, conte
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Individual untracked file paths, honoring ignore rules.
|
||||
*
|
||||
* Deliberately not `--directory`: collapsed directory entries end in a slash
|
||||
* and are not valid inputs to the per-file diff helpers, so a caller would
|
||||
* silently lose every file inside a new directory. Listing files costs more
|
||||
* entries but each one is usable.
|
||||
*
|
||||
* Callers that only need this list should not pay for `getStatus`, which also
|
||||
* computes ahead/behind, diff stats, and merge state — an order of magnitude
|
||||
* more work for an answer they throw away.
|
||||
*/
|
||||
export async function listUntrackedPaths(directory) {
|
||||
const { repoRoot } = await createRepositoryGitContext(directory);
|
||||
const result = await runGitCommand(repoRoot, [
|
||||
'ls-files',
|
||||
'--others',
|
||||
'--exclude-standard',
|
||||
]);
|
||||
if (!result.success) return [];
|
||||
return String(result.stdout || '')
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Diffs for untracked files, produced against an empty tree.
|
||||
*
|
||||
* `getDiff` re-resolves the repository context on every call, which costs an
|
||||
* extra `rev-parse` per file; a walkthrough of a branch with thirty new files
|
||||
* pays that thirty times. This resolves once and reuses it, with a bounded pool
|
||||
* so a repository full of new files cannot flood the process table.
|
||||
*
|
||||
* Returns one entry per input path, in order; unreadable paths yield `''`
|
||||
* rather than failing the batch.
|
||||
*/
|
||||
export async function getUntrackedDiffs(directory, filePaths = [], { concurrency = 8, contextLines = 3 } = {}) {
|
||||
const paths = (Array.isArray(filePaths) ? filePaths : []).filter((value) => typeof value === 'string' && value);
|
||||
if (paths.length === 0) return [];
|
||||
|
||||
const { directoryPath, directoryGit, repoRoot, git } = await createRepositoryGitContext(directory);
|
||||
const results = new Array(paths.length).fill('');
|
||||
let cursor = 0;
|
||||
|
||||
const worker = async () => {
|
||||
while (cursor < paths.length) {
|
||||
const index = cursor++;
|
||||
try {
|
||||
const fileContext = await resolveGitFileContext(directoryPath, directoryGit, paths[index], repoRoot);
|
||||
const args = ['diff', '--no-color'];
|
||||
if (typeof contextLines === 'number' && !Number.isNaN(contextLines)) {
|
||||
args.push(`-U${Math.max(0, contextLines)}`);
|
||||
}
|
||||
args.push('--no-index', '--', '/dev/null', fileContext.repoPath);
|
||||
try {
|
||||
results[index] = await git.raw(args);
|
||||
} catch (error) {
|
||||
// `git diff --no-index` exits 1 whenever there are differences, which
|
||||
// for a new file is always.
|
||||
results[index] = error?.exitCode === 1 && error?.message ? error.message : '';
|
||||
}
|
||||
} catch {
|
||||
results[index] = '';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await Promise.all(Array.from({ length: Math.min(concurrency, paths.length) }, worker));
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function getRangeDiff(directory, { base, head, path: filePath, contextLines = 3 } = {}) {
|
||||
const { directoryPath, directoryGit, repoRoot, git } = await createRepositoryGitContext(directory);
|
||||
const baseRef = typeof base === 'string' ? base.trim() : '';
|
||||
|
||||
Reference in New Issue
Block a user