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>
This commit is contained in:
gavrielc
2026-02-19 01:55:00 +02:00
committed by GitHub
parent a689f8b3fa
commit 51788de3b9
83 changed files with 13159 additions and 626 deletions

View File

@@ -0,0 +1,136 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import {
isCustomizeActive,
startCustomize,
commitCustomize,
abortCustomize,
} from '../customize.js';
import { CUSTOM_DIR } from '../constants.js';
import {
createTempDir,
setupNanoclawDir,
createMinimalState,
cleanup,
writeState,
} from './test-helpers.js';
import { readState, recordSkillApplication, computeFileHash } from '../state.js';
describe('customize', () => {
let tmpDir: string;
const originalCwd = process.cwd();
beforeEach(() => {
tmpDir = createTempDir();
setupNanoclawDir(tmpDir);
createMinimalState(tmpDir);
fs.mkdirSync(path.join(tmpDir, CUSTOM_DIR), { recursive: true });
process.chdir(tmpDir);
});
afterEach(() => {
process.chdir(originalCwd);
cleanup(tmpDir);
});
it('startCustomize creates pending.yaml and isCustomizeActive returns true', () => {
// Need at least one applied skill with file_hashes for snapshot
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
expect(isCustomizeActive()).toBe(false);
startCustomize('test customization');
expect(isCustomizeActive()).toBe(true);
const pendingPath = path.join(tmpDir, CUSTOM_DIR, 'pending.yaml');
expect(fs.existsSync(pendingPath)).toBe(true);
});
it('abortCustomize removes pending.yaml', () => {
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
startCustomize('test');
expect(isCustomizeActive()).toBe(true);
abortCustomize();
expect(isCustomizeActive()).toBe(false);
});
it('commitCustomize with no changes clears pending', () => {
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
startCustomize('no-op');
commitCustomize();
expect(isCustomizeActive()).toBe(false);
});
it('commitCustomize with changes creates patch and records in state', () => {
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
startCustomize('add feature');
// Modify the tracked file
fs.writeFileSync(trackedFile, 'export const x = 2;\nexport const y = 3;');
commitCustomize();
expect(isCustomizeActive()).toBe(false);
const state = readState();
expect(state.custom_modifications).toBeDefined();
expect(state.custom_modifications!.length).toBeGreaterThan(0);
expect(state.custom_modifications![0].description).toBe('add feature');
});
it('commitCustomize throws descriptive error on diff failure', () => {
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
startCustomize('diff-error test');
// Modify the tracked file
fs.writeFileSync(trackedFile, 'export const x = 2;');
// Make the base file a directory to cause diff to exit with code 2
const baseFilePath = path.join(tmpDir, '.nanoclaw', 'base', 'src', 'app.ts');
fs.mkdirSync(baseFilePath, { recursive: true });
expect(() => commitCustomize()).toThrow(/diff error/i);
});
it('startCustomize while active throws', () => {
const trackedFile = path.join(tmpDir, 'src', 'app.ts');
fs.mkdirSync(path.dirname(trackedFile), { recursive: true });
fs.writeFileSync(trackedFile, 'export const x = 1;');
recordSkillApplication('test-skill', '1.0.0', {
'src/app.ts': computeFileHash(trackedFile),
});
startCustomize('first');
expect(() => startCustomize('second')).toThrow();
});
});