Skip to main content

loong_contracts/
errors.rs

1use thiserror::Error;
2
3use crate::contracts::{Capability, HarnessKind};
4
5#[non_exhaustive]
6#[derive(Debug, Error, PartialEq, Eq)]
7pub enum PackError {
8    #[error("pack_id must not be empty")]
9    EmptyPackId,
10    #[error("domain must not be empty")]
11    EmptyDomain,
12    #[error("version is not valid semver: {0}")]
13    InvalidVersion(String),
14    #[error("pack must grant at least one capability")]
15    EmptyCapabilities,
16}
17
18#[non_exhaustive]
19#[derive(Debug, Error, PartialEq, Eq)]
20pub enum PolicyError {
21    #[error("token {token_id} expired at {expires_at_epoch_s}")]
22    ExpiredToken {
23        token_id: String,
24        expires_at_epoch_s: u64,
25    },
26    #[error("token {token_id} missing required capability {capability:?}")]
27    MissingCapability {
28        token_id: String,
29        capability: Capability,
30    },
31    #[error("token pack mismatch: token pack {token_pack_id} vs runtime pack {runtime_pack_id}")]
32    PackMismatch {
33        token_pack_id: String,
34        runtime_pack_id: String,
35    },
36    #[error("token {token_id} has been revoked")]
37    RevokedToken { token_id: String },
38    #[error("policy extension {extension} denied request: {reason}")]
39    ExtensionDenied { extension: String, reason: String },
40    #[error("tool call denied by policy for `{tool_name}`: {reason}")]
41    ToolCallDenied { tool_name: String, reason: String },
42}
43
44#[non_exhaustive]
45#[derive(Debug, Error, PartialEq, Eq)]
46pub enum HarnessError {
47    #[error("harness adapter not found: {0}")]
48    AdapterNotFound(String),
49    #[error(
50        "harness adapter kind mismatch for adapter {adapter}: expected {expected:?}, actual {actual:?}"
51    )]
52    AdapterKindMismatch {
53        adapter: String,
54        expected: HarnessKind,
55        actual: HarnessKind,
56    },
57    #[error("harness adapter execution failed: {0}")]
58    Execution(String),
59}
60
61#[non_exhaustive]
62#[derive(Debug, Error, PartialEq, Eq)]
63pub enum ConnectorError {
64    #[error("connector not found: {0}")]
65    NotFound(String),
66    #[error("core connector adapter not found: {0}")]
67    CoreAdapterNotFound(String),
68    #[error("connector extension not found: {0}")]
69    ExtensionNotFound(String),
70    #[error("no default core connector adapter is configured")]
71    NoDefaultCoreAdapter,
72    #[error("connector execution failed: {0}")]
73    Execution(String),
74}
75
76#[non_exhaustive]
77#[derive(Debug, Error, PartialEq, Eq)]
78pub enum RuntimePlaneError {
79    #[error("core runtime adapter not found: {0}")]
80    CoreAdapterNotFound(String),
81    #[error("runtime extension not found: {0}")]
82    ExtensionNotFound(String),
83    #[error("no default core runtime adapter is configured")]
84    NoDefaultCoreAdapter,
85    #[error("runtime execution failed: {0}")]
86    Execution(String),
87}
88
89#[non_exhaustive]
90#[derive(Debug, Error, PartialEq, Eq)]
91pub enum ToolPlaneError {
92    #[error("core tool adapter not found: {0}")]
93    CoreAdapterNotFound(String),
94    #[error("tool extension not found: {0}")]
95    ExtensionNotFound(String),
96    #[error("no default core tool adapter is configured")]
97    NoDefaultCoreAdapter,
98    #[error("tool execution failed: {0}")]
99    Execution(String),
100}
101
102#[non_exhaustive]
103#[derive(Debug, Error, PartialEq, Eq)]
104pub enum MemoryPlaneError {
105    #[error("core memory adapter not found: {0}")]
106    CoreAdapterNotFound(String),
107    #[error("memory extension not found: {0}")]
108    ExtensionNotFound(String),
109    #[error("no default core memory adapter is configured")]
110    NoDefaultCoreAdapter,
111    #[error("memory execution failed: {0}")]
112    Execution(String),
113}
114
115#[non_exhaustive]
116#[derive(Debug, Error, PartialEq, Eq)]
117pub enum IntegrationError {
118    #[error("provider not found: {0}")]
119    ProviderNotFound(String),
120    #[error("channel not found: {0}")]
121    ChannelNotFound(String),
122    #[error("channel is disabled: {0}")]
123    ChannelDisabled(String),
124    #[error("plugin scan root does not exist: {0}")]
125    PluginScanRootNotFound(String),
126    #[error("failed to read plugin source file {path}: {reason}")]
127    PluginFileRead { path: String, reason: String },
128    #[error("invalid plugin manifest in {path}: {reason}")]
129    PluginManifestParse { path: String, reason: String },
130    #[error(
131        "plugin manifest conflict between package {package_manifest_path} and source {source_path} on {field}: package {package_value} vs source {source_value}"
132    )]
133    PluginManifestConflict {
134        package_manifest_path: String,
135        source_path: String,
136        field: String,
137        package_value: String,
138        source_value: String,
139    },
140    #[error("awareness root does not exist: {0}")]
141    AwarenessRootNotFound(String),
142    #[error("failed to inspect awareness file {path}: {reason}")]
143    AwarenessFileRead { path: String, reason: String },
144    #[error("plugin absorb failed for {plugin_id}: {reason}")]
145    PluginAbsorbFailed { plugin_id: String, reason: String },
146}
147
148#[non_exhaustive]
149#[derive(Debug, Error, PartialEq, Eq)]
150pub enum AuditError {
151    #[error("audit sink failure: {0}")]
152    Sink(String),
153}
154
155#[non_exhaustive]
156#[derive(Debug, Error, PartialEq, Eq)]
157pub enum KernelError {
158    #[error("pack not found: {0}")]
159    PackNotFound(String),
160    #[error("duplicate pack id: {0}")]
161    DuplicatePack(String),
162    #[error("connector {connector} is not allowed by pack {pack_id}")]
163    ConnectorNotAllowed { connector: String, pack_id: String },
164    #[error("pack {pack_id} does not grant capability {capability:?}")]
165    PackCapabilityBoundary {
166        pack_id: String,
167        capability: Capability,
168    },
169    #[error(transparent)]
170    Pack(#[from] PackError),
171    #[error(transparent)]
172    Policy(#[from] PolicyError),
173    #[error(transparent)]
174    Harness(#[from] HarnessError),
175    #[error(transparent)]
176    Connector(#[from] ConnectorError),
177    #[error(transparent)]
178    RuntimePlane(#[from] RuntimePlaneError),
179    #[error(transparent)]
180    ToolPlane(#[from] ToolPlaneError),
181    #[error(transparent)]
182    MemoryPlane(#[from] MemoryPlaneError),
183    #[error(transparent)]
184    Integration(#[from] IntegrationError),
185    #[error(transparent)]
186    Audit(#[from] AuditError),
187}