Files
Regolith/skills-engine/merge.ts
gavrielc 51788de3b9 Skills engine v0.1 + multi-channel infrastructure (#307)
* refactor: multi-channel infrastructure with explicit channel/is_group tracking

- Add channels[] array and findChannel() routing in index.ts, replacing
  hardcoded whatsapp.* calls with channel-agnostic callbacks
- Add channel TEXT and is_group INTEGER columns to chats table with
  COALESCE upsert to protect existing values from null overwrites
- is_group defaults to 0 (safe: unknown chats excluded from groups)
- WhatsApp passes explicit channel='whatsapp' and isGroup to onChatMetadata
- getAvailableGroups filters on is_group instead of JID pattern matching
- findChannel logs warnings instead of silently dropping unroutable JIDs
- Migration backfills channel/is_group from JID patterns for existing DBs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: skills engine v0.1 — deterministic skill packages with rerere resolution

Three-way merge engine for applying skill packages on top of a core
codebase. Skills declare which files they add/modify, and the engine
uses git merge-file for conflict detection with git rerere for
automatic resolution of previously-seen conflicts.

Key components:
- apply: three-way merge with backup/rollback safety net
- replay: clean-slate replay for uninstall and rebase
- update: core version updates with deletion detection
- rebase: bake applied skills into base (one-way)
- manifest: validation with path traversal protection
- resolution-cache: pre-computed rerere resolutions
- structured: npm deps, env vars, docker-compose merging
- CI: per-skill test matrix with conflict detection

151 unit tests covering merge, rerere, backup, replay, uninstall,
update, rebase, structured ops, and edge cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add Discord and Telegram skill packages

Skill packages for adding Discord and Telegram channels to NanoClaw.
Each package includes:
- Channel implementation (add/src/channels/)
- Three-way merge targets for index.ts, config.ts, routing.test.ts
- Intent docs explaining merge invariants
- Standalone integration tests
- manifest.yaml with dependency/conflict declarations

Applied via: npx tsx scripts/apply-skill.ts .claude/skills/add-discord
These are inert until applied — no runtime impact.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* remove unused docs (skills-system-status, implementation-guide)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 01:55:00 +02:00

151 lines
4.2 KiB
TypeScript

import { execFileSync, execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import { MergeResult } from './types.js';
export function isGitRepo(): boolean {
try {
execSync('git rev-parse --git-dir', { stdio: 'pipe' });
return true;
} catch {
return false;
}
}
/**
* Run git merge-file to three-way merge files.
* Modifies currentPath in-place.
* Returns { clean: true, exitCode: 0 } on clean merge,
* { clean: false, exitCode: N } on conflict (N = number of conflicts).
*/
export function mergeFile(
currentPath: string,
basePath: string,
skillPath: string,
): MergeResult {
try {
execFileSync('git', ['merge-file', currentPath, basePath, skillPath], {
stdio: 'pipe',
});
return { clean: true, exitCode: 0 };
} catch (err: any) {
const exitCode = err.status ?? 1;
if (exitCode > 0) {
// Positive exit code = number of conflicts
return { clean: false, exitCode };
}
// Negative exit code = error
throw new Error(`git merge-file failed: ${err.message}`);
}
}
/**
* Set up unmerged index entries for rerere adapter.
* Creates stages 1/2/3 so git rerere can record/resolve conflicts.
*/
export function setupRerereAdapter(
filePath: string,
baseContent: string,
oursContent: string,
theirsContent: string,
): void {
if (!isGitRepo()) return;
const gitDir = execSync('git rev-parse --git-dir', {
encoding: 'utf-8',
}).trim();
// Clean up stale MERGE_HEAD from a previous crash
if (fs.existsSync(path.join(gitDir, 'MERGE_HEAD'))) {
cleanupMergeState();
}
// Hash objects into git object store
const baseHash = execSync('git hash-object -w --stdin', {
input: baseContent,
encoding: 'utf-8',
}).trim();
const oursHash = execSync('git hash-object -w --stdin', {
input: oursContent,
encoding: 'utf-8',
}).trim();
const theirsHash = execSync('git hash-object -w --stdin', {
input: theirsContent,
encoding: 'utf-8',
}).trim();
// Create unmerged index entries (stages 1/2/3)
const indexInfo = [
`100644 ${baseHash} 1\t${filePath}`,
`100644 ${oursHash} 2\t${filePath}`,
`100644 ${theirsHash} 3\t${filePath}`,
].join('\n');
execSync('git update-index --index-info', {
input: indexInfo,
stdio: ['pipe', 'pipe', 'pipe'],
});
// Set MERGE_HEAD and MERGE_MSG (required for rerere)
const headHash = execSync('git rev-parse HEAD', {
encoding: 'utf-8',
}).trim();
fs.writeFileSync(path.join(gitDir, 'MERGE_HEAD'), headHash + '\n');
fs.writeFileSync(
path.join(gitDir, 'MERGE_MSG'),
`Skill merge: ${filePath}\n`,
);
}
/**
* Run git rerere to record or auto-resolve conflicts.
* When filePath is given, checks that specific file for remaining conflict markers.
* Returns true if rerere auto-resolved the conflict.
*/
export function runRerere(filePath: string): boolean {
if (!isGitRepo()) return false;
try {
execSync('git rerere', { stdio: 'pipe' });
// Check if the specific working tree file still has conflict markers.
// rerere resolves the working tree but does NOT update the index,
// so checking unmerged index entries would give a false negative.
const content = fs.readFileSync(filePath, 'utf-8');
return !content.includes('<<<<<<<');
} catch {
return false;
}
}
/**
* Clean up merge state after rerere operations.
* Pass filePath to only reset that file's index entries (preserving user's staged changes).
*/
export function cleanupMergeState(filePath?: string): void {
if (!isGitRepo()) return;
const gitDir = execSync('git rev-parse --git-dir', {
encoding: 'utf-8',
}).trim();
// Remove merge markers
const mergeHead = path.join(gitDir, 'MERGE_HEAD');
const mergeMsg = path.join(gitDir, 'MERGE_MSG');
if (fs.existsSync(mergeHead)) fs.unlinkSync(mergeHead);
if (fs.existsSync(mergeMsg)) fs.unlinkSync(mergeMsg);
// Reset only the specific file's unmerged index entries to avoid
// dropping the user's pre-existing staged changes
try {
if (filePath) {
execFileSync('git', ['reset', '--', filePath], { stdio: 'pipe' });
} else {
execSync('git reset', { stdio: 'pipe' });
}
} catch {
// May fail if nothing staged
}
}