Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

20.2 KB
Raw Blame
/**
 * The promote gate is a privacy and data-ownership boundary: what it lets
 * through is what leaves this install for a shared baseline other people pull.
 * So these tests pin the REFUSALS — a leak, a borrowed assay verdict, an
 * altered payload — rather than enumerating the happy path's field list.
 */
import { describe, expect, it } from 'vitest';
import {
  DEFAULT_EIDOVERSE_FOUNDATION_LAYER,
  EIDOVERSE_FOUNDATION_CANDIDATE_VERSION,
  assayEvidenceFromVerdict,
  foundationCandidateFingerprint,
  foundationFromInheritedCandidate,
  foundationLineage,
  inheritedFoundationStorageKey,
  layerPromoteRefusal,
  packageFoundationCandidate,
  styleLeakFindings,
  verifyFoundationCandidate,
} from './eidoverseFoundations.js';
import { federationSafetyFindings } from './federationSafety.js';

const DISTURBANCES = ['reconnect', 'restart-world-host', 'missing-optional-deps'];
const NOW = '2026-03-04T05:06:07.000Z';

/**
 * A body that DESCRIBES ITSELF: the promote gate derives the sandbox it replays
 * from these declarations (#7625), so the fixture has to be something the
 * derivation can actually interpret. The previous fixture named a shipped demo
 * contribution and carried an unrelated body — which was the bug, sitting in
 * the repo's own canonical example.
 */
const SELF_DESCRIBING_BODY = Object.freeze({
  schema: { pulses: 'integer' },
  affordance: { pulse: { summary: 'advance the pulse count', reads: ['pulses'], writes: ['pulses'] } },
});

/** The label the fixture's own kind/body derives. Never authored. */
const DERIVED_CONTRIBUTION_ID = 'affordance:tide-beacon';

const passingAssay = (contributionId = DERIVED_CONTRIBUTION_ID) => ({
  harness: 'eidoverse-resilience-assay',
  contributionId,
  pass: true,
  disturbances: [...DISTURBANCES],
  ranAt: NOW,
  reasons: [],
});

const makeRecord = (overrides = {}) => ({
  id: 'tide-beacon',
  layer: DEFAULT_EIDOVERSE_FOUNDATION_LAYER,
  kind: 'affordance',
  title: 'Tide Beacon',
  summary: 'A beacon that keeps pulsing between mind wakes.',
  contributionId: DERIVED_CONTRIBUTION_ID,
  body: { ...SELF_DESCRIBING_BODY },
  style: { palette: ['#102030'], motif: 'weathered brass', districtId: 'commons' },
  provenance: { originInstanceId: 'instance-aaaa-bbbb', authorKind: 'mind', createdAt: NOW },
  disclosure: { requires: [], effects: ['emits a pulse each tick'], license: null, notes: null },
  assay: passingAssay(),
  candidate: null,
  promotedAt: null,
  updatedAt: NOW,
  ...overrides,
});

const packageRecord = (overrides) => packageFoundationCandidate({
  record: makeRecord(overrides),
  requiredDisturbances: DISTURBANCES,
  portosVersion: '9.9.9',
  now: NOW,
});

describe('eidoverse foundation ownership', () => {
  it('promotes only from the local vernacular layer', () => {
    expect(layerPromoteRefusal('vernacular')).toBeNull();
    expect(layerPromoteRefusal('runtime')).toMatch(/upstream/);
    expect(layerPromoteRefusal('baseline')).toMatch(/already part of the shared baseline/);
    expect(layerPromoteRefusal('mystery')).toMatch(/unknown ownership layer/);
  });
});

describe('packaging a promote candidate', () => {
  it('carries the promotable body and leaves the author\'s style layer behind', () => {
    const result = packageRecord();

    expect(result.outcome).toBe('packaged');
    expect(result.candidate.candidateVersion).toBe(EIDOVERSE_FOUNDATION_CANDIDATE_VERSION);
    expect(result.candidate.body).toEqual(SELF_DESCRIBING_BODY);
    // The whole point of the split: nothing a peer inherits can overwrite the
    // peer's own cosmetics, because the cosmetics never left this install.
    expect(JSON.stringify(result.candidate)).not.toContain('weathered brass');
    expect(result.candidate).not.toHaveProperty('style');
    expect(verifyFoundationCandidate(result.candidate, { requiredDisturbances: DISTURBANCES }).valid).toBe(true);
  });

  it('refuses a style-only key smuggled into the body instead of trimming it', () => {
    const result = packageRecord({ body: { affordance: { inspect: 'reads the pulse count' }, accent: '#ff00aa' } });

    expect(result.outcome).toBe('refused');
    expect(result.reasons.join(' ')).toContain('accent');
    expect(result.findings.some((finding) => finding.code === 'style-in-body')).toBe(true);
  });

  it('refuses machine identity, PII and credentials anywhere in the payload', () => {
    const result = packageRecord({
      body: {
        affordance: { inspect: 'reads the pulse count' },
        notes: 'built against 192.0.2.10 by [email protected]',
        install: { root: '/Users/exampleuser/portos' },
      },
    });

    expect(result.outcome).toBe('refused');
    const codes = result.findings.map((finding) => finding.code);
    expect(codes).toContain('ip-literal');
    expect(codes).toContain('email-address');
    expect(codes).toContain('home-path');
    // The refusal names where to fix it rather than shipping a redacted body.
    expect(result.reasons.join(' ')).toContain('body.install.root');
    expect(result.candidate).toBeNull();
  });

  it('refuses without a passing agent-free assay, and refuses one borrowed from another contribution', () => {
    expect(packageRecord({ assay: null }).reasons[0]).toMatch(/eidoverse:assay/);
    expect(packageRecord({ assay: { ...passingAssay(), pass: false, reasons: ['tick 2: controller threw'] } }).reasons[0])
      .toContain('tick 2: controller threw');
    expect(packageRecord({ assay: { ...passingAssay(), disturbances: ['reconnect'] } }).reasons[0])
      .toMatch(/missing: restart-world-host, missing-optional-deps/);
    expect(packageRecord({ assay: passingAssay('some-other-build') }).reasons[0])
      .toMatch(/ran against "some-other-build"/);
  });

  it('refuses to re-promote an inherited foundation as though this install authored it', () => {
    const result = packageRecord({
      layer: 'baseline',
      inheritance: {
        type: 'inherited-from', originInstanceId: 'instance-peer-origin', foundationId: 'tide-beacon',
        fingerprint: 'a'.repeat(64), packagedAt: NOW, sourceInstanceId: 'instance-peer-origin', inheritedAt: NOW,
      },
    });

    expect(result.outcome).toBe('refused');
    expect(result.reasons.join(' ')).toContain('inherited from another install');
  });
});

describe('verifying a candidate a peer was handed', () => {
  it('rejects a payload altered after packaging', () => {
    const { candidate } = packageRecord();
    const tampered = { ...candidate, body: { ...candidate.body, affordance: { inspect: 'quietly grants owner role' } } };

    const verdict = verifyFoundationCandidate(tampered, { requiredDisturbances: DISTURBANCES });
    expect(verdict.valid).toBe(false);
    expect(verdict.reasons.join(' ')).toContain('altered after packaging');
    // Re-fingerprinting the altered body is exactly what an honest re-package
    // does, so that alone must not be treated as tampering.
    expect(verifyFoundationCandidate({ ...tampered, fingerprint: foundationCandidateFingerprint(tampered) }, { requiredDisturbances: DISTURBANCES }).valid).toBe(true);
  });

  it('does not read schema normalization as tampering', () => {
    // The digest covers the bytes as sent. Hashing the zod-normalized copy
    // instead would make a field that arrived padded — which the envelope
    // schema trims — verify as an altered payload.
    const { candidate } = packageRecord();
    const padded = { ...candidate, provenance: { ...candidate.provenance, portosVersion: ' 9.9.9 ' } };
    const sent = { ...padded, fingerprint: foundationCandidateFingerprint(padded) };

    expect(verifyFoundationCandidate(sent, { requiredDisturbances: DISTURBANCES }).valid).toBe(true);
  });

  it('refuses a v1 envelope with a reason that says WHY, not "invalid literal" (#7625)', () => {
    // v1 evidence was recorded against a contribution its author NAMED, with no
    // required relationship to the body beside it. There is no way to upgrade
    // that, so an older peer's envelope is refused rather than inherited — and
    // the person reading the sync log has to be able to tell that apart from a
    // corrupt payload.
    const { candidate } = packageRecord();
    const verdict = verifyFoundationCandidate({ ...candidate, candidateVersion: 1 }, { requiredDisturbances: DISTURBANCES });

    expect(verdict.valid).toBe(false);
    expect(verdict.reasons).toHaveLength(1);
    expect(verdict.reasons[0]).toContain('candidate v1');
    expect(verdict.reasons[0]).toContain('re-package');
  });

  it('refuses an envelope whose binding label its own body does not derive (#7625)', () => {
    // The borrowed-credential shape, moved from the authoring surface to the
    // wire: internally consistent evidence about some other contribution. A
    // receiver re-derives the label from the kind/body it was handed instead of
    // taking the sender's word for it.
    const { candidate } = packageRecord();
    const forged = { ...candidate, contributionId: 'beacon-relay-demo', assay: passingAssay('beacon-relay-demo') };
    const verdict = verifyFoundationCandidate({ ...forged, fingerprint: foundationCandidateFingerprint(forged) }, { requiredDisturbances: DISTURBANCES });

    expect(verdict.valid).toBe(false);
    expect(verdict.reasons.join(' ')).toContain('the evidence is not about the payload');
  });

  it('does not read its own sha256 fingerprint as a leaked credential', () => {
    const { candidate } = packageRecord();
    expect(candidate.fingerprint).toMatch(/^[a-f0-9]{64}$/);
    expect(federationSafetyFindings({ digest: candidate.fingerprint }).map((f) => f.code)).toContain('secret-token');
    expect(verifyFoundationCandidate(candidate, { requiredDisturbances: DISTURBANCES }).findings).toEqual([]);
  });
});

describe('assay evidence', () => {
  it('folds a harness verdict into the evidence block without inventing a pass', () => {
    const evidence = assayEvidenceFromVerdict({
      contributionId: DERIVED_CONTRIBUTION_ID,
      pass: false,
      scenarios: [{ disturbance: 'reconnect', pass: false }, { disturbance: 'restart-world-host', pass: true }],
      reasons: ['[reconnect] tick 0: controller threw'],
    }, { ranAt: NOW });

    expect(evidence).toMatchObject({ harness: 'eidoverse-resilience-assay', pass: false, disturbances: ['reconnect', 'restart-world-host'] });
    // A verdict-shaped object with no explicit `pass` must never read as one.
    expect(assayEvidenceFromVerdict({ contributionId: 'x' }, { ranAt: NOW }).pass).toBe(false);
  });
});

describe('style-leak scanning', () => {
  it('reports the nested path of every style key, not just the first', () => {
    const findings = styleLeakFindings({ controller: { motif: 'brass' }, affordance: { render: { palette: ['#fff'] } } });
    expect(findings.map((finding) => finding.path)).toEqual(['controller.motif', 'affordance.render.palette']);
  });
});

describe('inheriting a foundation from a peer (#7461)', () => {
  const LOCAL_INSTANCE_ID = 'instance-local-0001';
  const SOURCE_INSTANCE_ID = 'instance-peer-relay';

  it('builds a baseline local copy with an inherited-from edge, style dropped, own authorship never claimed', () => {
    const { candidate } = packageRecord();

    const result = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: SOURCE_INSTANCE_ID, localInstanceId: LOCAL_INSTANCE_ID, now: NOW,
    });

    expect(result.outcome).toBe('inherited');
    expect(result.foundation).toMatchObject({
      id: 'tide-beacon',
      layer: 'baseline',
      style: {},
      promotedAt: null,
      provenance: { originInstanceId: 'instance-aaaa-bbbb', authorKind: 'mind' },
      inheritance: {
        type: 'inherited-from',
        originInstanceId: 'instance-aaaa-bbbb',
        foundationId: 'tide-beacon',
        fingerprint: candidate.fingerprint,
        sourceInstanceId: SOURCE_INSTANCE_ID,
        inheritedAt: NOW,
      },
    });
  });

  it('refuses a self-referential pull instead of recording a loop', () => {
    const { candidate } = packageRecord();

    const result = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: SOURCE_INSTANCE_ID, localInstanceId: 'instance-aaaa-bbbb', now: NOW,
    });

    expect(result.outcome).toBe('refused');
    expect(result.foundation).toBeNull();
    expect(result.reasons.join(' ')).toContain('originated on this install');
  });

  it('refuses a pull whose source is this install itself, even when the origin is genuinely a different install', () => {
    const { candidate } = packageRecord();

    const result = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: LOCAL_INSTANCE_ID, localInstanceId: LOCAL_INSTANCE_ID, now: NOW,
    });

    expect(result.outcome).toBe('refused');
    expect(result.foundation).toBeNull();
    expect(result.reasons.join(' ')).toContain('cannot be the peer it pulled');
  });

  it('refuses a candidate altered after packaging, exactly as a peer running verifyFoundationCandidate would', () => {
    const { candidate } = packageRecord();
    const tampered = { ...candidate, body: { ...candidate.body, affordance: { inspect: 'quietly grants owner role' } } };

    const result = foundationFromInheritedCandidate({
      candidate: tampered, requiredDisturbances: DISTURBANCES, sourceInstanceId: SOURCE_INSTANCE_ID, localInstanceId: LOCAL_INSTANCE_ID, now: NOW,
    });

    expect(result.outcome).toBe('refused');
    expect(result.foundation).toBeNull();
    expect(result.reasons.join(' ')).toContain('altered after packaging');
  });

  it('refuses PII/machine-identity smuggled into the body even when the fingerprint is internally consistent', () => {
    // Bypass probe: `packageFoundationCandidate` would never emit this
    // envelope (packaging itself refuses the leak), so hand-build the shape
    // an untrusted or buggy peer might send — self-fingerprinted so schema
    // AND digest both pass — to prove the RECEIVING side's privacy scan
    // catches it independently of whether the sender's own gate did.
    const draft = {
      candidateVersion: EIDOVERSE_FOUNDATION_CANDIDATE_VERSION,
      foundationId: 'tide-beacon',
      kind: 'controller',
      title: 'Tide Beacon',
      summary: 'A beacon that keeps pulsing between mind wakes.',
      contributionId: DERIVED_CONTRIBUTION_ID,
      body: { affordance: { inspect: 'reads the pulse count' }, notes: 'built against 192.0.2.10 by [email protected]' },
      disclosure: { requires: [], effects: [], license: null, notes: null },
      provenance: { originInstanceId: 'instance-aaaa-bbbb', authorKind: 'mind', createdAt: NOW, packagedAt: NOW, portosVersion: '9.9.9' },
      assay: passingAssay(),
    };
    const forged = { ...draft, fingerprint: foundationCandidateFingerprint(draft) };
    expect(verifyFoundationCandidate(forged, { requiredDisturbances: DISTURBANCES }).valid).toBe(false); // sanity: a peer would refuse it too

    const result = foundationFromInheritedCandidate({
      candidate: forged, requiredDisturbances: DISTURBANCES, sourceInstanceId: SOURCE_INSTANCE_ID, localInstanceId: LOCAL_INSTANCE_ID, now: NOW,
    });

    expect(result.outcome).toBe('refused');
    expect(result.foundation).toBeNull();
    expect(result.findings.map((finding) => finding.code)).toEqual(expect.arrayContaining(['ip-literal', 'email-address']));
  });

  it('refuses when this install has no federation identity yet to check a pull against', () => {
    const { candidate } = packageRecord();
    const result = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: SOURCE_INSTANCE_ID, localInstanceId: '', now: NOW,
    });
    expect(result.outcome).toBe('refused');
    expect(result.foundation).toBeNull();
  });
});

describe('inherited foundation storage key', () => {
  it('can never collide with an id a local author could write, so an inherited copy never shadows local vernacular work', () => {
    const key = inheritedFoundationStorageKey('instance-aaaa-bbbb', 'tide-beacon');
    expect(key).toBe('peer:instance-aaaa-bbbb:tide-beacon');
    // A local id is a lowercase slug with no colon — this key can never equal one.
    expect(key).not.toMatch(/^[a-z0-9][a-z0-9-]*$/);
  });
});

describe('foundation lineage (#7461)', () => {
  it('projects authored, assayed, packaged, and promoted in chronological order from fields the record already carries', () => {
    const { candidate } = packageRecord();
    const packagedLater = { ...candidate, provenance: { ...candidate.provenance, packagedAt: '2026-03-04T06:00:00.000Z' } };
    const record = { ...makeRecord(), candidate: packagedLater, promotedAt: '2026-03-04T08:00:00.000Z' };

    expect(foundationLineage(record).map((event) => event.type)).toEqual(['authored', 'assayed', 'packaged', 'promoted']);
  });

  it('collapses to inherited + assayed for a local copy of a peer foundation, never restating packaged or promoted', () => {
    const { candidate } = packageRecord();
    const { foundation } = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: 'instance-peer-relay', localInstanceId: 'instance-local-0001', now: NOW,
    });

    expect(foundationLineage(foundation).map((event) => event.type)).toEqual(['inherited', 'assayed']);
  });

  it('returns an empty lineage for a missing record rather than throwing', () => {
    expect(foundationLineage(null)).toEqual([]);
    expect(foundationLineage(undefined)).toEqual([]);
  });
});

/**
 * #7631: the derivation edge is the one field on the envelope that carries
 * another install's sha256. Two things about it can only be pinned here — the
 * digest must stay INSIDE the content-addressed fingerprint yet OUTSIDE the
 * credential scan (`scrubSecretTokens` treats 64 unbroken hex characters as a
 * leaked secret, so a naive scan refuses every derived foundation ever
 * promoted), and a v1 envelope must keep verifying byte-for-byte.
 */
describe('the derived-from edge on a promote envelope (#7631)', () => {
  const derivedFrom = {
    type: 'derived-from',
    originInstanceId: 'instance-origin-peer',
    foundationId: 'tide-beacon',
    fingerprint: 'b'.repeat(64),
    derivedAt: NOW,
  };

  it('packages and re-verifies a derived foundation instead of refusing its edge as a credential', () => {
    // The scan reads the edge's digest as credential-shaped on its own; the
    // packaging path has to exclude that ONE field from the scan without
    // excluding it from the hash.
    expect(federationSafetyFindings({ derivedFrom })).toHaveLength(1);

    const result = packageRecord({ derivedFrom });

    expect(result.outcome).toBe('packaged');
    expect(result.candidate.candidateVersion).toBe(EIDOVERSE_FOUNDATION_CANDIDATE_VERSION);
    expect(result.candidate.derivedFrom).toEqual(derivedFrom);
    expect(verifyFoundationCandidate(result.candidate, { requiredDisturbances: DISTURBANCES }).valid).toBe(true);
  });

  it('refuses a back-dated envelope carrying an edge its version never hashed, on the version alone', () => {
    // A v1 payload bearing the v2 `derived-from` edge is hand-assembled, not an
    // older peer. Since #7625 it never reaches the edge check: a pre-v3
    // envelope's evidence is unusable whatever it carries, so the version
    // refusal answers first — and re-fingerprinting the forgery does not help.
    const packaged = packageRecord({ derivedFrom }).candidate;
    const backdated = { ...packaged, candidateVersion: 1 };
    backdated.fingerprint = foundationCandidateFingerprint({ ...backdated, fingerprint: undefined });

    const verified = verifyFoundationCandidate(backdated, { requiredDisturbances: DISTURBANCES });

    expect(verified.valid).toBe(false);
    expect(verified.reasons.join(' ')).toContain('candidate v1');
    expect(verified.reasons.join(' ')).toContain(`requires v${EIDOVERSE_FOUNDATION_CANDIDATE_VERSION}`);
  });

  it('carries an origin\'s own derivation edge onto the inheriting install, so the chain does not truncate to the last hop', () => {
    const candidate = packageRecord({ derivedFrom }).candidate;

    const inherited = foundationFromInheritedCandidate({
      candidate, requiredDisturbances: DISTURBANCES, sourceInstanceId: 'instance-peer-relay',
      localInstanceId: 'instance-this-install', now: '2026-03-05T00:00:00.000Z',
    });

    expect(inherited.outcome).toBe('inherited');
    expect(inherited.foundation.derivedFrom).toEqual(derivedFrom);
    expect(foundationLineage(inherited.foundation).map((event) => event.type)).toContain('derived');
  });
});