File size: 3,538 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SPDX-License-Identifier: Apache-2.0
// © 2026 Lutar, Stephen P. — SZL Holdings
// ORCID: 0009-0001-0110-4173
//
// Layer 6 — a11oy policy gate for Composability (TH1)
//
// Policy rationale:
//   If systems A and B share a doctrine.json SHA, use compatible Λ-floors
//   (A exit ≤ B entry), and communicate via A2A receipt-envelope headers,
//   their composition A∘B is doctrine-locked. This gate validates the three
//   composability preconditions before allowing cross-system deployment.
//
//   Lean theorem cited: `composability` (TH1)
//   Lean file: Lutar/Composition/Composability.lean
//   Lean commit SHA: 1dca00032dfc9aa8559cc6c2e4b63192fcf52371
//   Lean status: theorem (derived)
//
// References:
//   Zenodo: https://doi.org/10.5281/zenodo.20162352
//   INNOVATIONS.md §4 TH1: composability theorem

export interface ComposabilityGateConfig {
  /** Require A2A envelope headers in payload. Default: true. */
  requireA2AHeaders?: boolean;
}

export interface ComposabilityGateOpts {
  doctrineShaA:     string;
  doctrineShaB:     string;
  aExitFloor:       number;
  bEntryFloor:      number;
  hasA2AHeaders:    boolean;
}

export interface ComposabilityDecision {
  allow:             boolean;
  rationale:         string;
  formula:           string;
  leanTheorem:       string;
  leanFile:          string;
  leanCommitSha:     string;
  doctrineMatch:     boolean;
  floorCompatible:   boolean;
  a2aHeadersPresent: boolean;
  lambdaScore:       number;
}

const LEAN_THEOREM = "composability";
const LEAN_FILE    = "Lutar/Composition/Composability.lean";
const LEAN_COMMIT  = "1dca00032dfc9aa8559cc6c2e4b63192fcf52371";

export function composabilityGate(
  config: ComposabilityGateConfig = {}
): (opts: ComposabilityGateOpts) => ComposabilityDecision {
  const requireA2AHeaders = config.requireA2AHeaders ?? true;

  return function gate(opts: ComposabilityGateOpts): ComposabilityDecision {
    const { doctrineShaA, doctrineShaB, aExitFloor, bEntryFloor, hasA2AHeaders } = opts;
    if (!doctrineShaA || !doctrineShaB) throw new Error(`ComposabilityGate: both doctrine SHAs required`);
    if (!Number.isFinite(aExitFloor) || !Number.isFinite(bEntryFloor)) {
      throw new Error(`ComposabilityGate: floors must be finite`);
    }

    const doctrineMatch      = doctrineShaA === doctrineShaB;
    const floorCompatible    = aExitFloor <= bEntryFloor;
    const a2aHeadersPresent  = hasA2AHeaders;
    const allow              = doctrineMatch && floorCompatible && (!requireA2AHeaders || a2aHeadersPresent);
    const lambdaScore        = [doctrineMatch, floorCompatible, a2aHeadersPresent].filter(Boolean).length / 3;

    const failures: string[] = [];
    if (!doctrineMatch) failures.push('doctrine SHA mismatch');
    if (!floorCompatible) failures.push(`A exit floor ${aExitFloor} > B entry floor ${bEntryFloor}`);
    if (requireA2AHeaders && !a2aHeadersPresent) failures.push('missing A2A headers');

    const rationale = allow
      ? `Composability (TH1): doctrine SHA match, A exit (${aExitFloor}) ≤ B entry (${bEntryFloor}), A2A headers present. A∘B doctrine-locked. Passes. Lean: ${LEAN_THEOREM} @${LEAN_COMMIT.slice(0, 12)}`
      : `Composability (TH1): preconditions failed — [${failures.join('; ')}]. Denied. Lean: ${LEAN_THEOREM} @${LEAN_COMMIT.slice(0, 12)}`;

    return { allow, rationale, formula: "Composability", leanTheorem: LEAN_THEOREM, leanFile: LEAN_FILE, leanCommitSha: LEAN_COMMIT, doctrineMatch, floorCompatible, a2aHeadersPresent, lambdaScore };
  };
}