Test a policy change with a Knowledge PR
Run the same small policy agent against main and an isolated candidate. It extracts the refund window from the document Nicia served with a receipt. If both answers and the diff match the expected change, the CI job opens a Proposal for a person to review.
Use Node.js 24 or newer and a new scratch space on the default schema and
standard admission mode. Mint a space key
with read and write. The reviewer uses their own console session.
mkdir knowledge-pr-cicd knowledge-pr-cinpm init -ynpm pkg set type=modulenpm install @nicia-ai/client@^0.1.1Save this as recipe.ts.
import { randomUUID } from "node:crypto";import { setTimeout } from "node:timers/promises";import { NiciaSpaceClient } from "@nicia-ai/client";
const policyId = "refund-policy";const question = "How many days does the refund policy allow?";const policy = (days: number) => `Refund requests are accepted within ${days} days.`;
export async function seed(space: NiciaSpaceClient) { // A first accepted record gives the space a main head to fork. const { change } = await space.writeRecords({ records: [ { id: "ci-fixture", label: "Knowledge PR fixture", ifNoneMatch: "*" }, ], }); if (change.status !== "applied") throw new Error(`Seed: ${change.status}`); await space.writeDocuments({ id: policyId, text: policy(14) });}
async function askAgent(space: NiciaSpaceClient, branch?: string) { const context = await space.getContext({ prompt: question, format: "items", ...(branch === undefined ? {} : { branch }), }); const document = context.items?.find( (item) => item.kind === "document" && item.id === policyId, ); const answer = document?.kind === "document" ? document.text.match( /Refund requests are accepted within (\d+) days\./, )?.[0] : undefined; return { answer, receipt: context.receipt };}
async function expectAnswer( space: NiciaSpaceClient, days: number, branch?: string,) { for (let attempt = 0; attempt < 30; attempt++) { const result = await askAgent(space, branch); if (result.answer === policy(days) && result.receipt) return result; await setTimeout(1_000); } throw new Error( `Expected a receipted ${days}-day answer on ${branch ?? "main"}`, );}
export async function openProposal( space: NiciaSpaceClient, runId: string = randomUUID(),) { const main = await expectAnswer(space, 14); const { branch } = await space.createBranch( {}, { idempotencyKey: `refund-${runId}` }, ); await space.writeDocuments({ id: policyId, text: policy(30), branch: branch.branchId, }); const candidate = await expectAnswer(space, 30, branch.branchId); await expectAnswer(space, 14); const { diff, summary } = await space.compareBranch(branch.branchId); if ( summary.conflicts || diff.records.length || diff.documents.length !== 1 || diff.documents[0]?.id !== policyId ) { throw new Error("Expected exactly one conflict-free refund policy change"); } const { proposal, revision } = await space.createProposal({ source: { kind: "branch", branchId: branch.branchId }, intent: "Extend the refund window from 14 to 30 days", }); return { main, candidate, proposalId: proposal.id, changeIds: revision.changeIds, };}
export async function verifyProposal( space: NiciaSpaceClient, proposalId: string,) { const { proposal } = await space.getProposal(proposalId); if (proposal.status !== "applied") throw new Error(`Proposal is ${proposal.status}`); return expectAnswer(space, 30);}
if (import.meta.main) { const { NICIA_SPACE: handle, NICIA_SPACE_KEY: credential } = process.env; if (!handle || !credential) throw new Error("Set NICIA_SPACE and NICIA_SPACE_KEY"); const baseUrl = process.env.NICIA_API_URL ?? "https://api.nicia.ai"; const space = new NiciaSpaceClient({ baseUrl, handle, credential }); const [command, proposalId] = process.argv.slice(2); if (command === "seed") await seed(space); else if (command === "open") console.log(await openProposal(space)); else if (command === "verify" && proposalId) console.log(await verifyProposal(space, proposalId)); else throw new Error("Usage: node recipe.ts seed | open | verify <proposal-id>");}Run and review
Section titled “Run and review”Seed main once, then have CI run the open command.
export NICIA_SPACE='your-scratch-space-handle'read -rsp 'Nicia space key: ' NICIA_SPACE_KEY; printf '\n'; export NICIA_SPACE_KEYnode recipe.ts seednode recipe.ts openThe job prints the 14-day main answer, the 30-day candidate answer, their receipt IDs, and the Proposal and ChangeSet IDs. Open the space’s Review tab with a reviewer account. Find the ChangeSet by its ID and approve and publish the reviewed change so it takes effect on main.
Run the verification job with the printed Proposal ID once Review shows it applied. The next main answer should say 30 days and carry a fresh receipt ID.
node recipe.ts verify '<proposal-id>'