feat(ai): propose_change tool — drafts to pending_changes, never applies

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
root
2026-06-01 18:16:53 +10:00
parent 2e121ce6d4
commit 7282729654
2 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import { describe, it, expect, beforeAll } from 'vitest';
import { pool } from '../../../../lib/db/pool.js';
import { resetDb } from '../../../helpers/db.js';
import { migrateUp } from '../../../../lib/db/migrate.js';
import { proposeChangeTool } from '../../../../lib/ai/agent/tools/propose_change.js';
let spaceId, agentId;
beforeAll(async () => {
await resetDb(); await migrateUp();
({ rows: [{ id: spaceId }] } = await pool.query(
`INSERT INTO spaces(slug,name) VALUES('s','S') RETURNING id`));
({ rows: [{ id: agentId }] } = await pool.query(`SELECT id FROM agents WHERE slug='companion'`));
});
const suggestAgent = (id) => ({ kind: 'agent', id, capabilities: { read: true, suggest: true, write: false }, scopes: {} });
describe('propose_change tool', () => {
it('writes a pending_changes row and never applies', async () => {
const ctx = { agent: suggestAgent(agentId), space_id: spaceId };
const out = await proposeChangeTool.handler(
{ entity_type: 'task', action: 'create', payload: { space_id: spaceId, title: 'Validate CSV' }, reason: 'tracking' },
ctx
);
expect(out.pending_change_id).toBeTruthy();
expect(out.applied).toBe(false);
const { rows } = await pool.query(`SELECT * FROM pending_changes WHERE id=$1`, [out.pending_change_id]);
expect(rows[0].status).toBe('pending');
expect(rows[0].agent_id).toBe(agentId);
const { rows: tasks } = await pool.query(`SELECT * FROM tasks WHERE title='Validate CSV'`);
expect(tasks).toHaveLength(0); // not applied
});
it('refuses when the agent cannot even suggest', async () => {
const denied = { kind: 'agent', id: agentId, capabilities: { read: true, suggest: false, write: false }, scopes: {} };
const out = await proposeChangeTool.handler(
{ entity_type: 'task', action: 'create', payload: { title: 'x' } },
{ agent: denied, space_id: spaceId }
);
expect(out.error).toMatch(/not permitted/i);
});
});