# AGENTS.md - ICA Client SDK Contributor Guide

## Scope
Guide for contributors working on `ica-client-sdk-ts`.
This SDK currently focuses on DIDComm onboarding flows (`_verify`, `_create`, `_remove`) and helper extraction methods.

## Current Surface (implemented)
- Main client: `src/IcaClient.ts`
- Public exports: `src/index.ts`
- Types: `src/types.ts`
- Tests: `src/__tests__/IcaClient.test.ts`

Implemented request flows:
- `POST /ica/cds-{jurisdiction}/v1/{sector}/terms/pdf/contract/_verify`
- `POST /ica/cds-{jurisdiction}/v1/{sector}/terms/pdf/contract/_verify-response`
- `POST /ica/cds-{jurisdiction}/v1/{sector}/entity/did/document/_create`
- `POST /ica/cds-{jurisdiction}/v1/{sector}/entity/did/document/_create-response`
- `POST /ica/cds-{jurisdiction}/v1/{sector}/terms/pdf/contract/_remove`
- `POST /ica/cds-{jurisdiction}/v1/{sector}/terms/pdf/contract/_remove-response`

Transport pattern:
- POST request returns `202` + `Location`.
- Polling methods call matching `*_response` endpoint.

## Backend Auth Surface (implemented)
The SDK now exposes high-level methods for the ICA backend auth lifecycle:
- organization bootstrap: `/organization/dataspace/auth/_exchange`
- api key lifecycle: `/api-key/org.schema/action/_create|_disable|_remove|_search`
- identity auth: `/identity/auth/_dcr|_code|_token|_exchange`

Critical clarification:
- `_dcr` here is backend technical identity binding (`client_id` + backend public JWK in `meta.jws.protected.jwk`).
- It is not the human wallet VP/Clearing House flow.
- Human/controller authentication and organization trust checks happen before backend API key issuance, not inside `_dcr`.

Bootstrap variants:
1. Controller-led DCR: controller creates API key and executes `_dcr` with backend public key; backend skips `_dcr` when already `bound`.
2. Backend-led DCR: controller creates API key, backend executes `_dcr` itself.

Runtime requirement:
- For `identity-exchange.v1`, `client_id` is still required by `_code/_token/_exchange`.

API key policy model (must stay explicit):
- API key provisioning is atomic: each `data[].resource` is one authorization rule.
- Required per rule: `scope`.
- Recommended per rule: `target` (endpoint/action restriction) and `instrument` (ODRL JSON object).
- Current profile boundary:
  - ICA/GW do not support `api-key-exception.v1` runtime mode.
  - DataConv may support it only as explicit gated profile.

## Transport Protection Semantics

Agents must keep these semantics explicit in docs/code reviews:

1. `didcomm-plain`:
- no JWS/JWE envelope generated by transport layer,
- technical JWK can appear in `meta.jws.protected.jwk` for DCR.

2. `didcomm-signed`:
- protected header is generated by signing/envelope layer,
- business auth methods should not require raw `meta.jws.protected` payload assembly.

3. `didcomm-encrypted` (nested signed payload):
- protected headers are generated by encryption/signing envelope tooling,
- business layer should pass key/material parameters, not raw envelope JSON.

Current SDK status:
- backend-auth paths are currently implemented for `application/didcomm-plain+json`.
- `identityDcrWithBinding(...)` is implemented for parameter-first DCR input.
- signed/encrypted envelope generation for backend-auth remains outside current method set.

## Parameter-First Contract (for agents)

When adding/refactoring methods, prioritize explicit variables:

- `clientId`
- `bearerToken`
- `codeVerifier`
- `codeChallengeMethod` (`S256`)
- `controllerSigAlg`
- `controllerSigKid`
- `controllerSigPublicJwk`
- `transportProtection` (`plain|signed|encrypted`)

Do not make agents pass opaque JSON strings when typed parameters can express the same intent.

Reference for exact sequence and contracts:
- `dataspace-ica-ts/docs/backend-auth-migration.md`
- `dataspace-ica-ts/src/api/openapi.ts`
- `dataspace-ica-ts/src/api/managers/backend-auth-request-manager.ts`

## Required Design Rules
- Keep existing API backward-compatible.
- Follow existing async style: submit -> poll response.
- Preserve injectable transports:
  - `httpClient` (axios)
  - `fetch`
- Reuse request helper internals (`request`, `sleep`, retry handling).
- Return typed SDK responses rather than raw `any`.

## Backend Auth Client Methods
- `controllerExchange(...)`
- `pollControllerExchangeResponse(thid)`
- `createApiKey(...)`, `createApiKeyRules(...)`, `disableApiKey(...)`, `removeApiKey(...)`, `searchApiKeys(...)`
- `pollApiKeyActionResponse(thid)`
- `identityDcr(...)`, `pollIdentityDcrResponse(thid)`
- `identityDcrWithBinding(...)` (parameter-first DCR)
- `identityCode(...)`, `pollIdentityCodeResponse(thid)`
- `identityToken(...)`, `pollIdentityTokenResponse(thid)`
- `identityExchange(...)`, `pollIdentityExchangeResponse(thid)`

Orchestration helper:
- `runBackendAuthFlow(...)` for `_create -> _dcr -> _code -> _token -> _exchange`
  - supports `dcrMode`: `force|skip|auto`

## Testing Requirements
When changing auth/transport behavior:
- `npm run build`
- `npm test`
- Add test coverage for:
  - `202 + Location` submit handling
  - polling retries / retry-after behavior
  - terminal success/failure payload mapping
  - content-type compatibility (`application/json` and DIDComm where applicable)

## Docs Requirements
If new methods are added:
- Update `README.md` usage section with end-to-end backend auth example.
- Include endpoint-to-method mapping table.
- Mention dependency on ICA `SECURITY_MODE` behavior for bearer validation.
