Skip to content
Beta. This surface may change before GA; breaking changes are dated in the changelog.

Run a Knowledge PR through agent CI

The deployment agent currently tells engineers to get one approval for a production rollout. You want it to say two. Treat that change like a PR. Test the proposed policy on a branch, review the diff, and verify the next answer on main after a human accepts the change.

1. Set up the space and the Slack review channel

Section titled “1. Set up the space and the Slack review channel”

Use Node.js 24 or newer and a scratch space with the default schema and standard admission. Mint a space key with read and write for the CI job. Have a person with Review access sign in to the console separately.

Terminal window
mkdir knowledge-pr-demo
cd knowledge-pr-demo
npm init -y
npm pkg set type=module
npm install @nicia-ai/client@^0.2.0
export NICIA_SPACE='your-scratch-space-handle'
read -rsp 'Nicia space key: ' NICIA_SPACE_KEY
export NICIA_SPACE_KEY
printf '\n'

Create an incoming webhook for your Slack review channel. Register it before opening the Proposal. Use a management key for this one-time setup. The commands use Bash and jq.

Terminal window
read -rsp 'Nicia management key: ' NICIA_MANAGEMENT_KEY
printf '\n'
read -rsp 'Slack webhook URL: ' SLACK_WEBHOOK_URL
printf '\n'
jq -n '{url: $ARGS.positional[0], types: ["proposal.opened"]}' \
--args "$SLACK_WEBHOOK_URL" | \
curl --fail-with-body --silent --show-error --request POST \
"https://api.nicia.ai/v1/brains/$NICIA_SPACE/webhooks" \
--header "Authorization: Bearer $NICIA_MANAGEMENT_KEY" --json @- | jq '.data.webhook.id'
unset NICIA_MANAGEMENT_KEY SLACK_WEBHOOK_URL

Nicia formats proposal.opened as a Slack message with a link to Review. Keep the webhook URL and management key out of CI.

2. Fork main, apply the candidate, and run the same task twice

Section titled “2. Fork main, apply the candidate, and run the same task twice”

Save the following as knowledge-pr.ts. The small agent task reads the deployment policy and extracts the required approval count. Your CI can run its own agent and scorer in expectApprovals using the same prompt and settings on both targets. Save each receipt with the corresponding result.

import { setTimeout } from "node:timers/promises";
import { NiciaSpaceClient } from "@nicia-ai/client";
const { NICIA_SPACE: handle, NICIA_SPACE_KEY: credential } = process.env;
if (!handle || !credential) throw new Error("Set NICIA_SPACE and NICIA_SPACE_KEY");
const space = new NiciaSpaceClient({
handle,
spaceKey: credential,
baseUrl: process.env.NICIA_API_URL ?? "https://api.nicia.ai",
});
const policyId = "deploy-policy";
const policy = (count: number) =>
`Production rollouts require ${count} ${count === 1 ? "approval" : "approvals"}.`;
async function expectApprovals(count: number, branch?: string) {
for (let attempt = 0; attempt < 30; attempt++) {
const context = await space.getContext({
prompt: "How many approvals are required for production rollouts?",
format: "items",
...(branch === undefined ? {} : { branch }),
});
const source = context.items?.find(
(item) => item.kind === "document" && item.id === policyId,
);
const approvals =
source?.kind === "document" ?
source.text.match(/Production rollouts require (\d+) approval/)?.[1]
: undefined;
if (approvals === String(count) && context.receipt) {
return { approvals: count, receipt: context.receipt };
}
await setTimeout(1000);
}
throw new Error(`Expected ${count} approval(s) on ${branch ?? "main"}`);
}
const [command, id] = process.argv.slice(2);
if (command === "seed") {
const { change } = await space.writeRecords({
records: [{ id: "deploy-fixture", label: "Deploy CI fixture", ifNoneMatch: "*" }],
});
if (change.status !== "applied") throw new Error(`Seed: ${change.status}`);
await space.writeDocuments({ id: policyId, text: policy(1) });
} else if (command === "open" && id) {
const { branch } = await space.createBranch({}, { idempotencyKey: `deploy-${id}` });
// The agent writes its candidate using the branch coordinate.
await space.writeDocuments({ id: policyId, text: policy(2), branch: branch.branchId });
const main = await expectApprovals(1);
const candidate = await expectApprovals(2, branch.branchId);
const { summary, diff } = await space.compareBranch(branch.branchId);
if (summary.conflicts || diff.records.length ||
diff.documents.length !== 1 || diff.documents[0]?.id !== policyId) {
throw new Error("Expected only the deployment policy to change");
}
const { proposal, revision } = await space.createProposal({
source: { kind: "branch", branchId: branch.branchId },
intent: "Require two approvals for production rollouts",
});
console.log({ main, candidate, diff, proposalId: proposal.id, changeIds: revision.changeIds });
} else if (command === "verify" && id) {
const { proposal } = await space.getProposal(id);
if (proposal.status !== "applied") throw new Error(`Proposal is ${proposal.status}`);
console.log(await expectApprovals(2));
} else {
throw new Error("Usage: node knowledge-pr.ts seed | open <run-id> | verify <proposal-id>");
}

Seed the fixture once. The seed creates an accepted record so there is a main head to fork. Use your CI run ID for the candidate branch. Keep the Proposal ID from the output for verification.

Terminal window
node knowledge-pr.ts seed
node knowledge-pr.ts open 'deploy-ci-001'

The report should show one approval on main, two on the branch, one document change in compareBranch, and a receipt for each answer. CI stops before opening a Proposal if the answers or diff differ from those expectations. If an external agent needs to write the candidate, give it a branch-scoped key and have it write with the branch ID. Keep the CI credential with the job that owns the main evaluation.

3. Approve in Review and verify the next main read

Section titled “3. Approve in Review and verify the next main read”

Follow the Slack notification to Review. Find the ChangeSet by the ID printed by CI, inspect the policy diff, and approve and apply it. Opening the Proposal pins a revision for Review. Applying the approved ChangeSet makes the update visible on main.

Terminal window
node knowledge-pr.ts verify '<proposal-id>'

The verification job expects two approvals and a fresh main receipt. Keep that receipt with the candidate and baseline receipts so you can show what each run was served.

Runnable CI recipe · Branch and Proposal API