Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/PEER-VERIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ For `upheld` and `refuted`, the verifier also signs a trust receipt about the su
`claim_refuted`, minted by `mintPeerVerificationReceipt`). The server stores it with the verdict. Trust routing weighs
`claim_verified` at +16 and `claim_refuted` at −26.

## Claims about one revision

A claim may declare `subject.revision`: the full commit ID it is about, and
optionally its tree ID, repository and target ref. For such a claim, evidence of
kind `git_commit`, `ci_pipeline` or `deploy_trace` counts only if its
`metadata.commitSha` is that commit, or its `metadata.treeSha` is the declared
tree. A commit on one SHA and a passing pipeline on another no longer verify the
claim together; the mismatch is reported with the missing evidence. The revision
is part of the verdict digest only when declared, so digests of claims without
one, and verdicts already given on them, are unchanged. Claims without a
revision are checked as before.

## What "independent" means here

The server checks that:
Expand Down
71 changes: 71 additions & 0 deletions src/protocol/continuity-claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,25 @@ export const DEFAULT_CONTINUITY_CLAIM_REQUIRED_EVIDENCE: Record<
blocker: ["operator_signoff"],
};

/**
* The exact tree a claim is about. When a claim declares one, evidence of a kind that names a revision (commit,
* pipeline, deploy trace) counts only if it names the same commit (or the same tree). Without it, evidence for
* different commits could together verify a claim that no single tree satisfies.
*/
export type ContinuityClaimRevision = {
repo?: string | null;
commitSha: string;
treeSha?: string | null;
targetRef?: string | null;
targetTipSha?: string | null;
};

export type ContinuityClaimSubject = {
type: "task" | "commitment" | "implementation" | "deployment" | "verification" | "blocker" | "memory" | "other";
ref: string;
title?: string | null;
metadata?: Record<string, unknown> | null;
revision?: ContinuityClaimRevision | null;
};

export type ContinuityClaimScope = {
Expand Down Expand Up @@ -250,6 +264,8 @@ export type ContinuityClaimVerificationResult = {
verified: boolean;
missingEvidence: ContinuityClaimEvidenceKind[];
nonVerifyingEvidence: ContinuityClaimEvidenceKind[];
/** Evidence not counted because it names a different revision than the claim declares. */
revisionMismatches?: Array<{ evidenceId: Uuid; kind: ContinuityClaimEvidenceKind; reason: string }>;
};

/**
Expand All @@ -260,3 +276,58 @@ export type ContinuityClaimVerificationResult = {
export const peerVerificationSatisfiesEvidence = (kind: ContinuityClaimEvidenceKind): boolean =>
kind === "peer_verification" ||
CONTINUITY_CLAIM_AUTHORITY_RANK[CONTINUITY_CLAIM_EVIDENCE_AUTHORITY[kind]] <= CONTINUITY_CLAIM_AUTHORITY_RANK.deploy;

/** Evidence kinds that are about one revision and must match a claim's declared revision. */
export const CONTINUITY_CLAIM_REVISION_EVIDENCE_KINDS: ContinuityClaimEvidenceKind[] = ["git_commit", "ci_pipeline", "deploy_trace"];

const GIT_OBJECT = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
const gitObject = (value: unknown): string | null =>
typeof value === "string" && GIT_OBJECT.test(value.trim().toLowerCase()) ? value.trim().toLowerCase() : null;
const shortText = (value: unknown, max: number): string | null =>
typeof value === "string" && value.trim() && value.trim().length <= max ? value.trim() : null;

/**
* A claim revision in canonical form, `null` when absent, or an error message when present but invalid. Commit and
* tree IDs are full SHA-1 or SHA-256 hex, so abbreviations cannot match the wrong object.
*/
export const normalizeContinuityClaimRevision = (value: unknown): ContinuityClaimRevision | null | { error: string } => {
if (value === undefined || value === null) return null;
if (typeof value !== "object" || Array.isArray(value)) return { error: "subject.revision must be an object" };
const item = value as Record<string, unknown>;
const commitSha = gitObject(item.commitSha);
if (!commitSha) return { error: "subject.revision.commitSha must be a full 40- or 64-character hex commit ID" };
for (const key of ["treeSha", "targetTipSha"] as const) {
if (item[key] !== undefined && item[key] !== null && !gitObject(item[key])) return { error: `subject.revision.${key} must be a full hex object ID` };
}
for (const [key, max] of [["repo", 500], ["targetRef", 255]] as const) {
if (item[key] !== undefined && item[key] !== null && !shortText(item[key], max)) return { error: `subject.revision.${key} must be a non-empty string of at most ${max} characters` };
}
const known = new Set(["repo", "commitSha", "treeSha", "targetRef", "targetTipSha"]);
const unknown = Object.keys(item).find(key => !known.has(key));
if (unknown) return { error: `subject.revision.${unknown} is not a known field` };
return {
commitSha,
...(shortText(item.repo, 500) ? { repo: shortText(item.repo, 500) } : {}),
...(gitObject(item.treeSha) ? { treeSha: gitObject(item.treeSha) } : {}),
...(shortText(item.targetRef, 255) ? { targetRef: shortText(item.targetRef, 255) } : {}),
...(gitObject(item.targetTipSha) ? { targetTipSha: gitObject(item.targetTipSha) } : {}),
};
};

/**
* Whether one piece of evidence counts toward a claim with this revision. Evidence of other kinds always counts.
* Revision-bearing evidence must carry `metadata.commitSha` equal to the claim's commit, or `metadata.treeSha` equal to
* the claim's tree when the claim names one (a rebuilt or re-merged commit with the same tree).
*/
export const continuityClaimEvidenceMatchesRevision = (
revision: ContinuityClaimRevision | null | undefined,
evidence: Pick<ContinuityClaimEvidence, "kind" | "metadata">,
): { matches: true } | { matches: false; reason: string } => {
if (!revision || !(CONTINUITY_CLAIM_REVISION_EVIDENCE_KINDS as readonly string[]).includes(evidence.kind)) return { matches: true };
const commit = gitObject(evidence.metadata?.commitSha);
const tree = gitObject(evidence.metadata?.treeSha);
if (commit === revision.commitSha) return { matches: true };
if (revision.treeSha && tree === revision.treeSha) return { matches: true };
if (!commit && !tree) return { matches: false, reason: `${evidence.kind} evidence names no commit; the claim is about ${revision.commitSha}` };
return { matches: false, reason: `${evidence.kind} evidence is for ${commit ?? `tree ${tree}`}, not the claim's ${revision.commitSha}` };
};
3 changes: 2 additions & 1 deletion src/protocol/peer-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export const peerVerificationClaimDigest = (
id: claim.id,
npub: claim.npub,
kind: claim.kind,
subject: { type: claim.subject.type, ref: claim.subject.ref },
// A revision is included only when declared, so digests of claims without one (and verdicts on them) are unchanged.
subject: { type: claim.subject.type, ref: claim.subject.ref, ...(claim.subject.revision ? { revision: claim.subject.revision } : {}) },
statement: claim.statement,
scope: {
workspaceId: claim.scope?.workspaceId ?? null,
Expand Down
58 changes: 58 additions & 0 deletions tests/domain/claim-revision.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createHash } from "node:crypto";
import { describe, expect, it } from "vitest";

import { canonicalJson } from "../../src/protocol/agent-deals";
import {
continuityClaimEvidenceMatchesRevision, normalizeContinuityClaimRevision, type ContinuityClaim,
} from "../../src/protocol/continuity-claims";
import { peerVerificationClaimDigest } from "../../src/protocol/peer-verification";

const A = "a".repeat(40), B = "b".repeat(40), T = "c".repeat(40);
const claim = (revision?: object): ContinuityClaim => ({
id: "4b8e7a52-5d0b-4f61-9d33-2d6b6f0f6a01", npub: "npub1owner" as never, kind: "implementation", status: "open",
subject: { type: "implementation", ref: "mr:84", ...(revision ? { revision: revision as never } : {}) },
statement: "The retry fix is merged and CI passed.",
scope: { workspaceId: "repo:onlybots", repo: null, taskId: null, environment: null, agentNpub: null, tags: ["b", "a"] },
authority: "runtime", verificationPlan: { requiredEvidence: ["git_commit", "ci_pipeline"], description: null, checklist: [] },
freshness: {}, supersedes: [], supersededBy: null, verifiedAt: null, rejectedAt: null, staleAt: null,
idempotencyKey: null, metadata: {}, createdAt: new Date(), updatedAt: new Date(),
});

describe("claim revisions", () => {
it("normalizes a revision and rejects abbreviations and unknown fields", () => {
expect(normalizeContinuityClaimRevision(undefined)).toBeNull();
expect(normalizeContinuityClaimRevision({ commitSha: A.toUpperCase(), repo: " group/app ", targetRef: "main" }))
.toEqual({ commitSha: A, repo: "group/app", targetRef: "main" });
expect(normalizeContinuityClaimRevision({ commitSha: "abc123" })).toMatchObject({ error: expect.stringContaining("full") });
expect(normalizeContinuityClaimRevision({ commitSha: A, treeSha: "xyz" })).toMatchObject({ error: expect.stringContaining("treeSha") });
expect(normalizeContinuityClaimRevision({ commitSha: A, extra: 1 })).toMatchObject({ error: expect.stringContaining("extra") });
expect(normalizeContinuityClaimRevision([A])).toMatchObject({ error: expect.any(String) });
});

it("counts revision-bearing evidence only for the declared commit or tree", () => {
const revision = { commitSha: A, treeSha: T };
expect(continuityClaimEvidenceMatchesRevision(revision, { kind: "git_commit", metadata: { commitSha: A } })).toEqual({ matches: true });
expect(continuityClaimEvidenceMatchesRevision(revision, { kind: "ci_pipeline", metadata: { commitSha: B } }))
.toMatchObject({ matches: false, reason: expect.stringContaining(B) });
expect(continuityClaimEvidenceMatchesRevision(revision, { kind: "ci_pipeline", metadata: { commitSha: B, treeSha: T } })).toEqual({ matches: true });
expect(continuityClaimEvidenceMatchesRevision(revision, { kind: "deploy_trace", metadata: {} }))
.toMatchObject({ matches: false, reason: expect.stringContaining("names no commit") });
expect(continuityClaimEvidenceMatchesRevision(revision, { kind: "operator_signoff", metadata: {} })).toEqual({ matches: true });
expect(continuityClaimEvidenceMatchesRevision(null, { kind: "ci_pipeline", metadata: { commitSha: B } })).toEqual({ matches: true });
});

it("leaves peer-verdict digests of claims without a revision unchanged, and binds those with one", () => {
const plain = claim();
// The digest formula before revisions existed.
const before = `sha256:${createHash("sha256").update(canonicalJson({
id: plain.id, npub: plain.npub, kind: plain.kind, subject: { type: plain.subject.type, ref: plain.subject.ref },
statement: plain.statement,
scope: { workspaceId: "repo:onlybots", repo: null, taskId: null, environment: null, agentNpub: null, tags: ["a", "b"] },
requiredEvidence: ["ci_pipeline", "git_commit"], staleAt: null,
}), "utf8").digest("hex")}`;
expect(peerVerificationClaimDigest(plain)).toBe(before);
const bound = peerVerificationClaimDigest(claim({ commitSha: A }));
expect(bound).not.toBe(before);
expect(peerVerificationClaimDigest(claim({ commitSha: B }))).not.toBe(bound);
});
});