Files
Regolith/skills-engine/file-ops.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

127 lines
3.8 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import type { FileOperation, FileOpsResult } from './types.js';
function safePath(projectRoot: string, relativePath: string): string | null {
const resolved = path.resolve(projectRoot, relativePath);
if (!resolved.startsWith(projectRoot + path.sep) && resolved !== projectRoot) {
return null;
}
return resolved;
}
export function executeFileOps(ops: FileOperation[], projectRoot: string): FileOpsResult {
const result: FileOpsResult = {
success: true,
executed: [],
warnings: [],
errors: [],
};
const root = path.resolve(projectRoot);
for (const op of ops) {
switch (op.type) {
case 'rename': {
if (!op.from || !op.to) {
result.errors.push(`rename: requires 'from' and 'to'`);
result.success = false;
return result;
}
const fromPath = safePath(root, op.from);
const toPath = safePath(root, op.to);
if (!fromPath) {
result.errors.push(`rename: path escapes project root: ${op.from}`);
result.success = false;
return result;
}
if (!toPath) {
result.errors.push(`rename: path escapes project root: ${op.to}`);
result.success = false;
return result;
}
if (!fs.existsSync(fromPath)) {
result.errors.push(`rename: source does not exist: ${op.from}`);
result.success = false;
return result;
}
if (fs.existsSync(toPath)) {
result.errors.push(`rename: target already exists: ${op.to}`);
result.success = false;
return result;
}
fs.renameSync(fromPath, toPath);
result.executed.push(op);
break;
}
case 'delete': {
if (!op.path) {
result.errors.push(`delete: requires 'path'`);
result.success = false;
return result;
}
const delPath = safePath(root, op.path);
if (!delPath) {
result.errors.push(`delete: path escapes project root: ${op.path}`);
result.success = false;
return result;
}
if (!fs.existsSync(delPath)) {
result.warnings.push(`delete: file does not exist (skipped): ${op.path}`);
result.executed.push(op);
break;
}
fs.unlinkSync(delPath);
result.executed.push(op);
break;
}
case 'move': {
if (!op.from || !op.to) {
result.errors.push(`move: requires 'from' and 'to'`);
result.success = false;
return result;
}
const srcPath = safePath(root, op.from);
const dstPath = safePath(root, op.to);
if (!srcPath) {
result.errors.push(`move: path escapes project root: ${op.from}`);
result.success = false;
return result;
}
if (!dstPath) {
result.errors.push(`move: path escapes project root: ${op.to}`);
result.success = false;
return result;
}
if (!fs.existsSync(srcPath)) {
result.errors.push(`move: source does not exist: ${op.from}`);
result.success = false;
return result;
}
if (fs.existsSync(dstPath)) {
result.errors.push(`move: target already exists: ${op.to}`);
result.success = false;
return result;
}
const dstDir = path.dirname(dstPath);
if (!fs.existsSync(dstDir)) {
fs.mkdirSync(dstDir, { recursive: true });
}
fs.renameSync(srcPath, dstPath);
result.executed.push(op);
break;
}
default: {
result.errors.push(`unknown operation type: ${(op as FileOperation).type}`);
result.success = false;
return result;
}
}
}
return result;
}