@sema-agent/core
Version:
Stateless, task-oriented AI agent core
58 lines (57 loc) • 3.36 kB
JavaScript
import { strict as assert } from "node:assert";
import { beginContract } from "../store-contracts/contract-harness.js";
const AUDIENCE_RANK = { none: 0, operator: 1, user: 2 };
const KNOWN_TRACE = { kind: "breaker.transition", version: 1, taskId: "contract-probe", key: "probe:model", phase: "open", failures: 1, ts: 0 };
const TRACE_PROBE = {
subject: "TraceEvent",
narrowest: "operator",
read: (consumer, record, origin) => consumer.readTrace(record, origin),
known: () => ({ ...KNOWN_TRACE }),
unknown: [
{ at: "kind", record: (word) => ({ kind: word, version: 1, taskId: "contract-probe", ts: 0 }) },
{ at: "phase of a known kind", record: (word) => ({ ...KNOWN_TRACE, phase: word }) },
],
};
const PROBES = { TraceEvent: TRACE_PROBE, TRACE_EVENT_KINDS: TRACE_PROBE };
export const CONTRACT_PROBE_PREFIX = "__contract_probe__.";
const PROTOTYPE_WORDS = ["constructor", "__proto__", "toString"];
export function openFaceContract(make, hooks = {}) {
const { run, settle } = beginContract(hooks.runAssertion);
const origin = { engineVersion: "0.0.0-contract-probe" };
for (const probe of new Set(Object.values(PROBES))) {
const keptAndNarrow = (reading) => {
assert.equal(reading.kept, true, "(1) the record is kept");
assert.ok(AUDIENCE_RANK[reading.audience] <= AUDIENCE_RANK[probe.narrowest], `(2) shown to ${reading.audience}, wider than ${probe.narrowest}`);
};
run(`${probe.subject}: a word this build mints is kept and recorded as nothing`, async () => {
const reading = await probe.read(make(), probe.known(), origin);
assert.equal(reading.kept, true);
assert.equal(reading.recorded, undefined);
});
for (const place of probe.unknown) {
const words = [`${CONTRACT_PROBE_PREFIX}${place.at.replace(/\W+/g, "_")}`, ...PROTOTYPE_WORDS];
for (const word of words) {
run(`${probe.subject}: an unknown word at ${place.at} (${word}) is kept, shown no wider, recorded once`, async () => {
const consumer = make();
const first = await probe.read(consumer, place.record(word), origin);
keptAndNarrow(first);
assert.deepEqual(first.recorded, { subject: probe.subject, word, engineVersion: origin.engineVersion }, "(3) the word is recorded");
const again = await probe.read(consumer, place.record(word), origin);
keptAndNarrow(again);
assert.equal(again.recorded, undefined, "(3) recorded once");
});
}
run(`${probe.subject}: the decision at ${place.at} is not taken from the unknown word`, async () => {
const consumer = make();
const decisions = new Set();
for (const word of [`${CONTRACT_PROBE_PREFIX}one`, `${CONTRACT_PROBE_PREFIX}other`, ...PROTOTYPE_WORDS]) {
const reading = await probe.read(consumer, place.record(word), origin);
keptAndNarrow(reading);
decisions.add(reading.decision);
}
assert.equal(decisions.size, 1, `(4) unknown words, one decision: ${[...decisions].join(" / ")}`);
});
}
}
return settle();
}