Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/ai/egress.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/ai/egress.go |
| Original SHA-256 | b403b538187e661753dad626541745cb542f38d254928cd6923d2231485c0c0b |
| Git blob SHA | ac52c39eac729c3bc5096e2c9fc0bd9375154de2 |
| Reading-copy SHA-256 | b403b538187e661753dad626541745cb542f38d254928cd6923d2231485c0c0b |
Recovered source transcript
Download complete source text ↓114 lines · 5.4 KB
package ai
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/missionworkforce/missionmatch/internal/platform/audit"
"github.com/missionworkforce/missionmatch/internal/platform/database/dbgen"
"github.com/missionworkforce/missionmatch/internal/platform/scrub"
)
// ActionExternalInference is the audit action recorded for every call that leaves the local plane for an
// external inference backend. It sits in the same aedt namespace as aedt.ai.answered.
const ActionExternalInference = "aedt.inference.external"
// AllowExternal is the single gate the orchestrator calls before any payload leaves for an external
// inference backend such as OpenAI. It refuses a payload that may not egress (an unknown class or a
// private data marker) and, when the payload is permitted, returns the audit metadata that must be
// recorded for the call. Binding the gate and the audit in one call means a caller cannot send without
// both. The metadata records the backend, the payload class, the byte count, and the content hash, and
// never the content, matching how the orchestrator already audits with a map of abstract fields.
func AllowExternal(backend string, provs []scrub.Provenance, class scrub.EgressClass, payload []byte) (map[string]any, error) {
if err := scrub.AllowEgress(provs, class, payload); err != nil {
return nil, err
}
return map[string]any{
"backend": backend,
"provenance": provenanceStrings(provs),
"payload_class": string(class),
"byte_count": len(payload),
"content_sha256": scrub.ContentHash(payload),
}, nil
}
// AllowExternalAudited is AllowExternal plus the PERSISTED audit row, and it is what every caller must use.
//
// AllowExternal returns audit metadata and trusts its caller to record it. That trust was misplaced. The
// comment above claims that binding the gate and the audit in one call means a caller cannot send without
// both, and the binding was never real: the only caller wrote the metadata to a LOG LINE and persisted
// nothing. ActionExternalInference was declared, documented, and covered by a unit test that asserted the
// shape of a map. No row was ever written for it. So the single class of call that physically leaves this
// machine was the only class with no audit trail, discovered 29 July 2026 while closing finding three.
//
// IT FAILS CLOSED. An external call whose audit row cannot be written does not happen. That is the posture
// the auth module already takes for security events, for the same reason: an unrecorded egress is worse
// than a refused one, because a refusal is visible and an unrecorded call is not.
//
// The row is written BEFORE the payload leaves, which is the only ordering that can be trusted. A row
// written afterwards is missing exactly when the process dies mid call, which is the case an auditor cares
// about most.
func AllowExternalAudited(ctx context.Context, db dbgen.DBTX, org *uuid.UUID, backend string, provs []scrub.Provenance, class scrub.EgressClass, payload []byte) (map[string]any, error) {
meta, err := AllowExternal(backend, provs, class, payload)
if err != nil {
return nil, err
}
blob, merr := json.Marshal(meta)
if merr != nil {
return nil, fmt.Errorf("ai: external egress refused, its audit metadata would not marshal: %w", merr)
}
hash, _ := meta["content_sha256"].(string)
if aerr := audit.OnPool(ctx, db, org, audit.Event{
Action: ActionExternalInference,
EntityType: "external_inference",
EntityID: hash,
NewValues: blob,
}); aerr != nil {
return nil, fmt.Errorf("ai: external egress REFUSED because its audit row could not be written: %w", aerr)
}
return meta, nil
}
// RecordExternalOutcome records WHICH backend actually served a permitted call, keyed to the same content
// hash as the row AllowExternalAudited wrote.
//
// It exists because the gateway FALLS BACK to the local runtime when the selected backend fails, which is
// correct for availability and invisible in the audit trail without this row. Without it the ledger cannot
// answer the only question that matters about an external lane: did this payload actually leave, or did a
// failure quietly turn it into a local call that looked identical from here. The gateway tags its response
// model with the backend that produced it, so the answer is recorded rather than assumed.
//
// This row is written AFTER the call, so unlike the egress row it is genuinely best effort: the call has
// already happened and refusing anything at this point would be theatre. The error is returned so a caller
// can report it, never to gate the work.
func RecordExternalOutcome(ctx context.Context, db dbgen.DBTX, org *uuid.UUID, contentHash, servedModel string, tokens int) error {
blob, err := json.Marshal(map[string]any{
"content_sha256": contentHash,
"served_model": servedModel,
"tokens": tokens,
})
if err != nil {
return err
}
return audit.OnPool(ctx, db, org, audit.Event{
Action: ActionExternalInference,
EntityType: "external_inference_result",
EntityID: contentHash,
NewValues: blob,
})
}
// provenanceStrings renders the element provenance set for the audit row. The SET is recorded, not a single
// value, because a payload composed from several rows has several origins and an audit that flattened them
// could not answer which one authorised the call.
func provenanceStrings(provs []scrub.Provenance) []string {
out := make([]string, 0, len(provs))
for _, p := range provs {
out = append(out, string(p))
}
return out
}