Skip to content

feat(intelligence): withIntelligence hook — unify send/receive, fix dropped typed diffs#504

Merged
drewstone merged 4 commits into
mainfrom
feat/with-intelligence
Jul 9, 2026
Merged

feat(intelligence): withIntelligence hook — unify send/receive, fix dropped typed diffs#504
drewstone merged 4 commits into
mainfrom
feat/with-intelligence

Conversation

@drewstone

Copy link
Copy Markdown
Contributor

What

One agent-runtime SDK hook, withIntelligence, that unifies the three fragmented Intelligence send/receive mechanisms and fixes the dropped typed-diff receive.

The Intelligence plane's GET /v1/profiles/:target/composed already returns { agentProfile, agentProfileDiffs, capabilities }, but the old receive path (withCertifiedDelivery / CertifiedProfile / pullCertified) deserialized only promptSurface + artifacts and dropped the typed agentProfileDiffs. This PR deserializes what was already on the wire and surfaces it.

The hook

withIntelligence<I, O>(
  agent: (input: I, applied: AppliedIntelligence) => Promise<O>,
  config: IntelligenceConfig & { target?; refreshMs?; onProposals? },
): ((input: I) => Promise<O>) & { refresh(): Promise<void>; proposals(): ProposedProfileDiff[] }
  • SEND — a typed RunRecord { runId, traceId, project, target, input, output, outcome:{success?,score?,usage:{inferenceUsd,intelligenceUsd}}, model?, provider?, loopEvents? } serialized through the existing OTLP builders (flatOtelSpan + buildLoopOtelSpans, no second builder) to /v1/otlp. Best-effort / fail-open.
  • RECEIVECertifiedProfile + pullCertified extended with agentProfileDiffs: {diff: AgentProfileDiff, provenance:{version,lift,contentHash,promotedAt}}[], capabilities, and agentProfile. The previously-dropped typed diffs now round-trip.
  • AppliedIntelligence += proposals, applyProfile(base) (folds via applyAgentProfileDiff from @tangle-network/agent-interface@0.19.0), and record(report) to populate the RunRecord. The prompt-only composePrompt(base) convenience is preserved.
  • Safety — observe + deliver only. The hook never auto-applies a received diff at runtime; it surfaces them via proposals() / onProposals, and applyProfile is caller-invoked. This preserves the held-out invariant the plane enforces.

Clean replacement (no consumers — delete, not deprecate)

  • Deleted withTangleIntelligence and withCertifiedDelivery (and their prompt-only types). withIntelligence is the one hook. Repo-wide grep shows zero references to the deleted names.
  • Collapsed the endpoint env sprawl to one base URL: baseUrl config / TANGLE_INTELLIGENCE_URL env, from which both send (${base}/v1/otlp) and receive (${base}/v1/profiles/:target/composed) derive. Removed INTELLIGENCE_OTLP_ENDPOINT, the OTEL_EXPORTER_OTLP_ENDPOINT dual-read, and INTELLIGENCE_BASE. Send now gates on a tenant apiKey (the ingest requires it).
  • Migrated the in-repo examples + docs + the package-export verifier.

Verification

  • pnpm build (ESM + DTS), pnpm typecheck, pnpm verify:package, pnpm docs:freshness — all green.
  • pnpm test1280 passed, 2 skipped (132 files).
  • New round-trip test: a composed-endpoint fixture with agentProfileDiffs[] flows through the receive path; proposals() returns them and applied.applyProfile(base) equals applyAgentProfileDiff(base, diff) — the previously-dropped diffs are received.
  • New send test: a typed RunRecord ships to /v1/otlp carrying target + the usage split + model.

Deferred

The optional defineAgent.intelligence binding + improve(seedFrom: applied.proposals) is deferred — it ripples into the held-out improvement loop's proposer contract (a separate subsystem), and proposals are already accessible via applied.proposals / wrapped.proposals(), so nothing is blocked.

…diffs

Unify the three Intelligence send/receive fragments behind one hook. The
composed endpoint already returns agentProfileDiffs/capabilities/agentProfile;
the SDK now deserializes them instead of dropping them.

- withIntelligence(agent, config): observe + deliver only. SENDs a typed
  RunRecord via the shipped OTLP builders to /v1/otlp; RECEIVEs the certified
  profile and surfaces promoted profile diffs as proposals() / onProposals.
  Never auto-applies a diff at runtime (applyProfile is caller-invoked).
- CertifiedProfile + pullCertified extended with agentProfileDiffs (typed diff +
  held-out provenance), capabilities, and the composed agentProfile.
- Delete withTangleIntelligence and withCertifiedDelivery; collapse the endpoint
  env sprawl behind one base URL (baseUrl / TANGLE_INTELLIGENCE_URL) that both
  send and receive derive from.
…ound-trip

- with-intelligence.test.ts: the previously-dropped agentProfileDiffs now
  round-trip through proposals(); applyProfile == applyAgentProfileDiff; the
  hook never auto-applies (delivers only the certified prompt surface); a typed
  RunRecord ships to /v1/otlp carrying target + usage split.
- intelligence.test.ts: baseUrl replaces endpoint; export gates on the tenant
  key; add the exportRunRecord send path.
- delivery/capability tests: extend the CertifiedProfile fixtures with the typed
  diff fields; assert pullCertified deserializes them.
…baseUrl

Drop-in and webcode examples now wrap agents with withIntelligence; endpoint
config collapses to the single baseUrl. Send gates on the tenant apiKey, so the
offline demos pass a demo key to still exercise the export path.
…ase URL

Update the canonical API decision table, intelligence-sdk guide, build skill,
example READMEs, the go-live archive, and the regenerated typedoc pages. Fix the
package-export verifier's expected list. Collapse the last INTELLIGENCE_BASE /
INTELLIGENCE_OTLP_ENDPOINT env references to the single TANGLE_INTELLIGENCE_URL.
Grep across the repo now shows zero withTangleIntelligence/withCertifiedDelivery.
@drewstone drewstone merged commit 394a13c into main Jul 9, 2026
1 check passed
@drewstone drewstone deleted the feat/with-intelligence branch July 9, 2026 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants