|
| 1 | +# Invalid State Representations Report |
| 2 | + |
| 3 | +Based on the analysis of the codebase against AGENTS.md guidelines, the |
| 4 | +following interfaces allow invalid state representations that should be |
| 5 | +refactored: |
| 6 | + |
| 7 | +## 1. LLMRequest Interface |
| 8 | + |
| 9 | +**File**: `packages/llm/src/types.ts` **Lines**: 28-38 |
| 10 | + |
| 11 | +```typescript |
| 12 | +export interface LLMRequest { |
| 13 | + cache?: boolean; |
| 14 | + messages: LLMMessage[]; |
| 15 | + model: ModelName; |
| 16 | + system?: string; |
| 17 | + maxTokens?: number; |
| 18 | + stream?: boolean; |
| 19 | + stop?: string; |
| 20 | + mode?: "json"; |
| 21 | + metadata?: LLMRequestMetadata; |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +**Issue**: The `cache` property being optional creates ambiguity. Code |
| 26 | +throughout the codebase defaults it to `true` if undefined, but this implicit |
| 27 | +behavior isn't clear from the interface. |
| 28 | + |
| 29 | +**Recommendation**: Rename `cache` to `noCache` with `false` as default: |
| 30 | + |
| 31 | +```typescript |
| 32 | +export interface LLMRequest { |
| 33 | + noCache?: boolean; |
| 34 | + // ... other properties |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## 2. OAuth2Tokens Interface |
| 39 | + |
| 40 | +**File**: |
| 41 | +`packages/toolshed/routes/integrations/google-oauth/google-oauth.utils.ts` |
| 42 | +**Lines**: 9-16 |
| 43 | + |
| 44 | +```typescript |
| 45 | +export interface OAuth2Tokens { |
| 46 | + accessToken: string; |
| 47 | + refreshToken?: string; |
| 48 | + expiresIn?: number; |
| 49 | + tokenType: string; |
| 50 | + scope?: string[]; |
| 51 | + expiresAt?: number; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +**Issue**: `refreshToken` is optional but critical for token renewal. Code has |
| 56 | +to handle cases where it might be missing, leading to potential authentication |
| 57 | +failures. |
| 58 | + |
| 59 | +**Recommendation**: Create separate types for initial and renewable tokens: |
| 60 | + |
| 61 | +```typescript |
| 62 | +export interface InitialOAuth2Tokens { |
| 63 | + accessToken: string; |
| 64 | + tokenType: string; |
| 65 | + expiresIn: number; |
| 66 | + scope: string[]; |
| 67 | +} |
| 68 | + |
| 69 | +export interface RenewableOAuth2Tokens extends InitialOAuth2Tokens { |
| 70 | + refreshToken: string; |
| 71 | + expiresAt: number; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## 3. UserInfo Interface |
| 76 | + |
| 77 | +**File**: |
| 78 | +`packages/toolshed/routes/integrations/google-oauth/google-oauth.utils.ts` |
| 79 | +**Lines**: 18-28 |
| 80 | + |
| 81 | +```typescript |
| 82 | +export interface UserInfo { |
| 83 | + id?: string; |
| 84 | + email?: string; |
| 85 | + verified_email?: boolean; |
| 86 | + name?: string; |
| 87 | + given_name?: string; |
| 88 | + family_name?: string; |
| 89 | + picture?: string; |
| 90 | + locale?: string; |
| 91 | + error?: string; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +**Issue**: Mixes success and error states in one interface. Either you have user |
| 96 | +data OR an error, never both. |
| 97 | + |
| 98 | +**Recommendation**: Use discriminated union: |
| 99 | + |
| 100 | +```typescript |
| 101 | +type UserInfoResult = |
| 102 | + | { |
| 103 | + success: true; |
| 104 | + data: { |
| 105 | + id: string; |
| 106 | + email: string; |
| 107 | + verified_email: boolean; |
| 108 | + name: string; |
| 109 | + // ... other fields |
| 110 | + }; |
| 111 | + } |
| 112 | + | { success: false; error: string }; |
| 113 | +``` |
| 114 | + |
| 115 | +## 4. CallbackResult Interface |
| 116 | + |
| 117 | +**File**: |
| 118 | +`packages/toolshed/routes/integrations/google-oauth/google-oauth.utils.ts` |
| 119 | +**Lines**: 30-35 |
| 120 | + |
| 121 | +```typescript |
| 122 | +export interface CallbackResult extends Record<string, unknown> { |
| 123 | + success: boolean; |
| 124 | + error?: string; |
| 125 | + message?: string; |
| 126 | + details?: Record<string, unknown>; |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +**Issue**: When `success` is false, `error` should be required. When `success` |
| 131 | +is true, `error` shouldn't exist. |
| 132 | + |
| 133 | +**Recommendation**: Use discriminated union: |
| 134 | + |
| 135 | +```typescript |
| 136 | +type CallbackResult = |
| 137 | + | { success: true; message?: string; details?: Record<string, unknown> } |
| 138 | + | { success: false; error: string; details?: Record<string, unknown> }; |
| 139 | +``` |
| 140 | + |
| 141 | +## 5. BackgroundCharmServiceOptions Interface |
| 142 | + |
| 143 | +**File**: `packages/background-charm-service/src/service.ts` **Lines**: 12-19 |
| 144 | + |
| 145 | +```typescript |
| 146 | +export interface BackgroundCharmServiceOptions { |
| 147 | + identity: Identity; |
| 148 | + toolshedUrl: string; |
| 149 | + runtime: Runtime; |
| 150 | + bgSpace?: string; |
| 151 | + bgCause?: string; |
| 152 | + workerTimeoutMs?: number; |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +**Issue**: Optional properties have defaults (`BG_SYSTEM_SPACE_ID` and |
| 157 | +`BG_CELL_CAUSE`) applied in constructor, but this isn't clear from the |
| 158 | +interface. |
| 159 | + |
| 160 | +**Recommendation**: Make defaults explicit in the type: |
| 161 | + |
| 162 | +```typescript |
| 163 | +export interface BackgroundCharmServiceOptions { |
| 164 | + identity: Identity; |
| 165 | + toolshedUrl: string; |
| 166 | + runtime: Runtime; |
| 167 | + bgSpace: string; |
| 168 | + bgCause: string; |
| 169 | + workerTimeoutMs: number; |
| 170 | +} |
| 171 | + |
| 172 | +export const DEFAULT_BG_OPTIONS = { |
| 173 | + bgSpace: BG_SYSTEM_SPACE_ID, |
| 174 | + bgCause: BG_CELL_CAUSE, |
| 175 | + workerTimeoutMs: 30000, |
| 176 | +} as const; |
| 177 | +``` |
| 178 | + |
| 179 | +## 6. Module Interface |
| 180 | + |
| 181 | +**File**: `packages/builder/src/types.ts` **Lines**: 182-188 |
| 182 | + |
| 183 | +```typescript |
| 184 | +export interface Module { |
| 185 | + type: "ref" | "javascript" | "recipe" | "raw" | "isolated" | "passthrough"; |
| 186 | + implementation?: ((...args: any[]) => any) | Recipe | string; |
| 187 | + wrapper?: "handler"; |
| 188 | + argumentSchema?: JSONSchema; |
| 189 | + resultSchema?: JSONSchema; |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +**Issue**: `implementation` is optional but certain module types require it. The |
| 194 | +relationship between `type` and required properties isn't enforced. |
| 195 | + |
| 196 | +**Recommendation**: Use discriminated unions: |
| 197 | + |
| 198 | +```typescript |
| 199 | +type Module = |
| 200 | + | { |
| 201 | + type: "ref"; |
| 202 | + implementation: string; |
| 203 | + wrapper?: "handler"; |
| 204 | + argumentSchema?: JSONSchema; |
| 205 | + resultSchema?: JSONSchema; |
| 206 | + } |
| 207 | + | { |
| 208 | + type: "javascript"; |
| 209 | + implementation: (...args: any[]) => any; |
| 210 | + wrapper?: "handler"; |
| 211 | + argumentSchema?: JSONSchema; |
| 212 | + resultSchema?: JSONSchema; |
| 213 | + } |
| 214 | + | { |
| 215 | + type: "recipe"; |
| 216 | + implementation: Recipe; |
| 217 | + wrapper?: "handler"; |
| 218 | + argumentSchema?: JSONSchema; |
| 219 | + resultSchema?: JSONSchema; |
| 220 | + } |
| 221 | + | { type: "raw"; implementation: string } |
| 222 | + | { type: "isolated"; implementation: string } |
| 223 | + | { type: "passthrough" }; |
| 224 | +``` |
| 225 | + |
| 226 | +## Impact of These Issues |
| 227 | + |
| 228 | +1. **Runtime Errors**: Optional properties that are actually required lead to |
| 229 | + runtime failures |
| 230 | +2. **Defensive Programming**: Developers must add null checks everywhere, |
| 231 | + cluttering the code |
| 232 | +3. **Hidden Dependencies**: Implicit defaults make it hard to understand the |
| 233 | + true requirements |
| 234 | +4. **Type Safety Loss**: TypeScript can't catch invalid combinations of |
| 235 | + properties |
| 236 | + |
| 237 | +## General Principles from AGENTS.md |
| 238 | + |
| 239 | +"Making invalid states unrepresentable is good" - These interfaces violate this |
| 240 | +principle by allowing combinations of properties that shouldn't exist together |
| 241 | +or by making required properties optional. |
0 commit comments