Initial commit: omp-edit DSH plugin (oh-my-pi port) with docs and tests

This commit is contained in:
Agent committed 2026-10-10 19:15:23 +00:00
commit 71953ffb8f
19 files changed
+2412

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { makeHarness, registerBuiltins } from './harness.mjs'
import * as plugin from '../index.js'
async function setup(config = {}, model) {
const h = await makeHarness()
registerBuiltins(h)
plugin.apply(h.ctx, config)
const agent = h.newAgent(model)
return { h, agent }
}
const tagOf = (text, file) => new RegExp(`\\[${file.replace('.', '\\.')}#([0-9A-F]{4})\\]`).exec(text)?.[1]
test('read is tagged; hashline edit applies, returns fresh tag + preview + diff', async () => {
const { h, agent } = await setup()
h.write('app.py', 'def f():\n return 1\n\nprint(f())\n')
const r = await h.call(agent, 'read', { file_path: 'app.py' })
const tag = tagOf(r.text, 'app.py')
assert.ok(tag, r.text)
assert.match(r.text, /^2: return 1$/m)
const e = await h.call(agent, 'edit', { input: `[app.py#${tag}]\nPUT 2.=2:\n+ return 2` })
assert.equal(h.read('app.py'), 'def f():\n return 2\n\nprint(f())\n')
const newTag = tagOf(e.text, 'app.py')
assert.ok(newTag && newTag !== tag, e.text)
assert.match(e.text, /2: return 2/)
assert.ok(e.meta.diffs.length >= 1)
h.cleanup()
})
test('edit without read is refused (DSH observation policy)', async () => {
const { h, agent } = await setup()
h.write('x.txt', 'a\n')
await assert.rejects(h.call(agent, 'edit', { input: '[x.txt#0000]\nPUT 1.=1:\n+b' }), /never issued|read/)
h.cleanup()
})
test('stale tag after external change is recovered when anchors survive', async () => {
const { h, agent } = await setup()
h.write('s.txt', 'alpha\nbeta\ngamma\n')
const tag = tagOf((await h.call(agent, 'read', { file_path: 's.txt' })).text, 's.txt')
h.write('s.txt', 'HEADER\nalpha\nbeta\ngamma\n') // external edit
const e = await h.call(agent, 'edit', { input: `[s.txt#${tag}]\nPUT 3.=3:\n+GAMMA` })
assert.equal(h.read('s.txt'), 'HEADER\nalpha\nbeta\nGAMMA\n')
assert.match(e.text, /recovered stale tag/)
h.cleanup()
})
test('stale tag with changed anchor is rejected, nothing written', async () => {
const { h, agent } = await setup()
h.write('s.txt', 'alpha\nbeta\ngamma\n')
const tag = tagOf((await h.call(agent, 'read', { file_path: 's.txt' })).text, 's.txt')
h.write('s.txt', 'alpha\nBETA\ngamma\n')
await assert.rejects(h.call(agent, 'edit', { input: `[s.txt#${tag}]\nPUT 2.=2:\n+x` }), /anchored lines moved or changed/)
assert.equal(h.read('s.txt'), 'alpha\nBETA\ngamma\n')
h.cleanup()
})
test('multi-file move with named register, then chained edit using returned tag', async () => {
const { h, agent } = await setup()
h.write('a.js', 'function helper() {\n return 1\n}\n\nhelper()\n')
h.write('b.js', '// lib\n')
const ta = tagOf((await h.call(agent, 'read', { file_path: 'a.js' })).text, 'a.js')
const tb = tagOf((await h.call(agent, 'read', { file_path: 'b.js' })).text, 'b.js')
const e = await h.call(agent, 'edit', { input: `[a.js#${ta}]\nCUT 1* @fn\nCUT 4.=4\n[b.js#${tb}]\nPUT >$ @fn` })
assert.equal(h.read('a.js'), 'helper()\n')
assert.equal(h.read('b.js'), '// lib\nfunction helper() {\n return 1\n}\n')
const tb2 = tagOf(e.text, 'b.js')
await h.call(agent, 'edit', { input: `[b.js#${tb2}]\nPUT 3.=3:\n+ return 2` })
assert.match(h.read('b.js'), /return 2/)
h.cleanup()
})
test('grep output is tagged and counts as an observation', async () => {
const { h, agent } = await setup()
h.write('g.txt', 'one\nneedle here\nthree\n')
const g = await h.call(agent, 'grep', { pattern: 'needle' })
const tag = tagOf(g.text, 'g.txt')
assert.ok(tag, g.text)
assert.match(g.text, /^2:needle here$/m)
await h.call(agent, 'edit', { input: `[g.txt#${tag}]\nPUT 2.=2:\n+found` })
assert.equal(h.read('g.txt'), 'one\nfound\nthree\n')
h.cleanup()
})
test('REM and MV', async () => {
const { h, agent } = await setup()
h.write('old.txt', 'x\n')
h.write('gone.txt', 'y\n')
const t1 = tagOf((await h.call(agent, 'read', { file_path: 'old.txt' })).text, 'old.txt')
const t2 = tagOf((await h.call(agent, 'read', { file_path: 'gone.txt' })).text, 'gone.txt')
await h.call(agent, 'edit', { input: `[old.txt#${t1}]\nPUT 1.=1:\n+z\nMV new.txt\n[gone.txt#${t2}]\nREM` })
assert.equal(h.read('new.txt'), 'z\n')
assert.throws(() => h.read('old.txt'))
assert.throws(() => h.read('gone.txt'))
h.cleanup()
})
test('CRLF files keep CRLF', async () => {
const { h, agent } = await setup()
h.write('w.txt', 'a\r\nb\r\n')
const tag = tagOf((await h.call(agent, 'read', { file_path: 'w.txt' })).text, 'w.txt')
await h.call(agent, 'edit', { input: `[w.txt#${tag}]\nPUT 2.=2:\n+c` })
assert.equal(h.read('w.txt'), 'a\r\nc\r\n')
h.cleanup()
})
test('DeepSeek model falls back to replace mode; strict keeps hashline', async () => {
const { h, agent } = await setup({}, 'deepseek-v4')
const t = h.ctx.tools.get('edit', agent)
assert.ok(t.parameters.properties.old_string, 'replace schema')
h.write('r.txt', 'hello world\n')
await h.call(agent, 'read', { file_path: 'r.txt' })
await h.call(agent, 'edit', { file_path: 'r.txt', edits: [{ old_string: 'hello', new_string: 'goodbye' }, { old_string: 'world', new_string: 'moon' }] })
assert.equal(h.read('r.txt'), 'goodbye moon\n')
h.cleanup()
const s = await setup({ strict: true }, 'deepseek-v4')
assert.ok(s.h.ctx.tools.get('edit', s.agent).parameters.properties.input)
s.h.cleanup()
})
test('replace mode refuses when file changed since read', async () => {
const { h, agent } = await setup({ mode: 'replace' })
h.write('c.txt', 'v1\n')
await h.call(agent, 'read', { file_path: 'c.txt' })
h.write('c.txt', 'v2\n')
await assert.rejects(h.call(agent, 'edit', { file_path: 'c.txt', old_string: 'v2', new_string: 'v3' }), /changed since it was read/)
h.cleanup()
})
test('sloppy and apply_patch modes', async () => {
const s = await setup({ mode: 'sloppy' })
s.h.write('u.ts', 'const a = 1;\nrun(a);\n')
await s.h.call(s.agent, 'read', { file_path: 'u.ts' })
await s.h.call(s.agent, 'edit', { input: '*** Edit File: u.ts\n*** Find\nrun(a);\n*** Insert Before\nlog(a);' })
assert.equal(s.h.read('u.ts'), 'const a = 1;\nlog(a);\nrun(a);\n')
s.h.cleanup()
const p = await setup({ mode: 'apply_patch' })
p.h.write('m.py', 'def f():\n return 1\n')
await p.h.call(p.agent, 'read', { file_path: 'm.py' })
await p.h.call(p.agent, 'edit', { input: '*** Begin Patch\n*** Update File: m.py\n def f():\n- return 1\n+ return 2\n*** Add File: n.py\n+x = 1\n*** End Patch' })
assert.equal(p.h.read('m.py'), 'def f():\n return 2\n')
assert.equal(p.h.read('n.py'), 'x = 1\n')
p.h.cleanup()
})
test('model switch re-registers the edit tool in the agent scope', async () => {
const { h, agent } = await setup()
assert.ok(h.ctx.tools.get('edit', agent).parameters.properties.input)
await h.ctx.waterfall('agent/request', {}, async () => ({ provider: 'omni', model: 'deepseek-chat' }))
assert.ok(h.ctx.tools.get('edit', agent).parameters.properties.old_string)
h.cleanup()
})