-
Notifications
You must be signed in to change notification settings - Fork 0
feat(adapters): model evidence-backed capability states #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agent-bundle": minor | ||
| --- | ||
|
|
||
| Replace target adapter Boolean capability records with evidence-backed supported, degraded, unavailable, and prohibited states while retaining `TargetRegistry.supports()` as a derived compatibility view. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { sha256Hex, stableJson } from '../core/digest.ts'; | ||
| import type { CapabilityEvidence, CapabilityState } from '../core/capabilities.ts'; | ||
| import type { TargetAdapterMetadata } from './types.ts'; | ||
|
|
||
| /** Builds immutable evidence from a target's pinned capability-table metadata. */ | ||
| export const capabilityEvidence = ( | ||
| target: string, | ||
| metadata: TargetAdapterMetadata, | ||
| ): CapabilityEvidence => Object.freeze({ | ||
| capabilityRevision: metadata.capabilityRevision, | ||
| capabilitySha256: metadata.capabilitySha256, | ||
| observedVersion: metadata.observedVersion, | ||
| target, | ||
| }); | ||
|
|
||
| export const supportedCapability = (evidence: CapabilityEvidence): CapabilityState => Object.freeze({ | ||
| evidence, | ||
| state: 'supported', | ||
| }); | ||
|
|
||
| export const unavailableCapability = (reason: string): CapabilityState => Object.freeze({ | ||
| reason, | ||
| state: 'unavailable', | ||
| }); | ||
|
|
||
| export const capabilityStateFromSupport = ( | ||
| supported: boolean, | ||
| evidence: CapabilityEvidence, | ||
| unavailableReason: string, | ||
| ): CapabilityState => supported ? supportedCapability(evidence) : unavailableCapability(unavailableReason); | ||
|
|
||
| /** The temporary Boolean compatibility rule: only supported maps to true. */ | ||
| export const capabilityIsSupported = (capability: CapabilityState | undefined): boolean => { | ||
| if (capability === undefined) return false; | ||
| switch (capability.state) { | ||
| case 'supported': | ||
| return true; | ||
| case 'degraded': | ||
| case 'unavailable': | ||
| case 'prohibited': | ||
| return false; | ||
| default: { | ||
| const exhaustive: never = capability; | ||
| return exhaustive; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** Immutable Boolean view retained for callers that have not migrated yet. */ | ||
| export const capabilityBooleanView = ( | ||
| capabilities: Readonly<Record<string, CapabilityState>>, | ||
| ): Readonly<Record<string, boolean>> => Object.freeze(Object.fromEntries( | ||
| Object.entries(capabilities).map(([name, capability]) => [name, capabilityIsSupported(capability)]), | ||
| )); | ||
|
|
||
| const evidenceFor = (capability: CapabilityState): CapabilityEvidence | undefined => { | ||
| switch (capability.state) { | ||
| case 'supported': | ||
| return capability.evidence; | ||
| case 'degraded': | ||
| return capability.evidence; | ||
| case 'unavailable': | ||
| case 'prohibited': | ||
| return undefined; | ||
| default: { | ||
| const exhaustive: never = capability; | ||
| return exhaustive; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const precedenceFor = (capability: CapabilityState): 0 | 1 | 2 | 3 => { | ||
| switch (capability.state) { | ||
| case 'supported': | ||
| return 0; | ||
| case 'degraded': | ||
| return 1; | ||
| case 'unavailable': | ||
| return 2; | ||
| case 'prohibited': | ||
| return 3; | ||
| default: { | ||
| const exhaustive: never = capability; | ||
| return exhaustive; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const reasonFor = (capability: CapabilityState, precedence: 1 | 2 | 3): string | undefined => { | ||
| switch (capability.state) { | ||
| case 'supported': | ||
| return undefined; | ||
| case 'degraded': | ||
| return precedence === 1 ? capability.reason : undefined; | ||
| case 'unavailable': | ||
| return precedence === 2 ? capability.reason : undefined; | ||
| case 'prohibited': | ||
| return precedence === 3 ? capability.reason : undefined; | ||
| default: { | ||
| const exhaustive: never = capability; | ||
| return exhaustive; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const mergedReason = ( | ||
| left: CapabilityState, | ||
| right: CapabilityState, | ||
| precedence: 1 | 2 | 3, | ||
| ): string => [...new Set([reasonFor(left, precedence), reasonFor(right, precedence)] | ||
| .filter((reason): reason is string => reason !== undefined))] | ||
| .sort((first, second) => first.localeCompare(second)) | ||
| .join('; '); | ||
|
|
||
| /** Merges evidence without discarding either pinned table identity. */ | ||
| export const mergeCapabilityEvidence = ( | ||
| left: CapabilityEvidence, | ||
| right: CapabilityEvidence, | ||
| ): CapabilityEvidence => { | ||
| const evidence = [left, right].sort((first, second) => { | ||
| const targetOrder = first.target.localeCompare(second.target); | ||
| return targetOrder === 0 ? stableJson(first).localeCompare(stableJson(second)) : targetOrder; | ||
| }); | ||
| return Object.freeze({ | ||
| capabilityRevision: evidence.map((entry) => `${entry.target}@${entry.capabilityRevision}`).join('+'), | ||
| capabilitySha256: sha256Hex(stableJson(evidence)), | ||
| observedVersion: evidence.map((entry) => `${entry.target}@${entry.observedVersion}`).join('+'), | ||
| target: evidence.map((entry) => entry.target).join('+'), | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Intersects two host judgments for a composite adapter. Prohibition dominates, | ||
| * then unavailability, then degradation; two supported states merge evidence. | ||
| */ | ||
| export const intersectCapabilityStates = ( | ||
| left: CapabilityState, | ||
| right: CapabilityState, | ||
| ): CapabilityState => { | ||
| const leftPrecedence = precedenceFor(left); | ||
| const rightPrecedence = precedenceFor(right); | ||
| const precedence = leftPrecedence > rightPrecedence ? leftPrecedence : rightPrecedence; | ||
| switch (precedence) { | ||
| case 0: | ||
| if (left.state !== 'supported' || right.state !== 'supported') { | ||
| throw new Error('Supported capability intersection lost its evidence invariant.'); | ||
| } | ||
| return supportedCapability(mergeCapabilityEvidence(left.evidence, right.evidence)); | ||
| case 1: { | ||
| const leftEvidence = evidenceFor(left); | ||
| const rightEvidence = evidenceFor(right); | ||
| const evidence = leftEvidence === undefined || rightEvidence === undefined | ||
| ? undefined | ||
| : mergeCapabilityEvidence(leftEvidence, rightEvidence); | ||
| return Object.freeze({ | ||
| ...(evidence === undefined ? {} : { evidence }), | ||
| reason: mergedReason(left, right, precedence), | ||
| state: 'degraded', | ||
| }); | ||
| } | ||
| case 2: | ||
| return Object.freeze({ reason: mergedReason(left, right, precedence), state: 'unavailable' }); | ||
| case 3: | ||
| return Object.freeze({ reason: mergedReason(left, right, precedence), state: 'prohibited' }); | ||
| default: { | ||
| const exhaustive: never = precedence; | ||
| return exhaustive; | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.