Metadata-Version: 2.4
Name: kosha-client
Version: 0.5.0
Summary: OpenSearch-compatible Python client for the Kosha search engine
Author-email: DecoverAI <engineering@decoverai.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/decover-tech/kosha
Project-URL: Source, https://github.com/decover-tech/kosha
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Provides-Extra: stubs
Requires-Dist: kosha-client-stubs>=0.1; extra == "stubs"
Provides-Extra: mcp
Requires-Dist: mcp<2,>=1.20; extra == "mcp"
Provides-Extra: hosted
Requires-Dist: mcp<2,>=1.20; extra == "hosted"
Requires-Dist: pyjwt[crypto]>=2.8; extra == "hosted"
Requires-Dist: boto3>=1.28; extra == "hosted"
Provides-Extra: ingest
Requires-Dist: pypdf>=4; extra == "ingest"
Requires-Dist: python-docx>=1.1; extra == "ingest"
Provides-Extra: ingest-worker
Requires-Dist: pypdf>=4; extra == "ingest-worker"
Requires-Dist: python-docx>=1.1; extra == "ingest-worker"
Requires-Dist: boto3>=1.28; extra == "ingest-worker"

# Python client for Kosha

## Structure

Following the **spec → codegen → thin client** pattern (see `DESIGN.md §12.1`):

```
kosha_client/
  __init__.py          # Public API — exports KoshaClient
  stubs/               # GENERATED — do not edit by hand
    models.py           # Request/response types from OpenAPI spec
    api.py              # HTTP client methods from OpenAPI spec
  transport.py         # THIN — auth, retry, timeout, logging
  compat.py            # THIN — opensearch compatibility shim (_Serializer, _Transport)
  client.py            # THIN — KoshaClient that delegates to stubs
```

## Layers

| Layer | Origin | Responsibility |
|-------|--------|----------------|
| `stubs/` | **Auto-generated** from `proto/kosha/v1/kosha.proto` via OpenAPI → openapi-generator | Request serialization, URL construction, response deserialization |
| `transport.py` | **Hand-written** (~30 lines) | Bearer/JWT auth injection, exponential backoff retry, log correlation |
| `compat.py` | **Hand-written** (~30 lines) | `_Serializer` / `_Transport` duck-types for `opensearchpy.helpers.bulk` |
| `client.py` | **Thin hand-written** (~100 lines) | `KoshaClient(search, index, bulk, count, ...)` that translates OpenSearch-style calls to stub methods |

## Regenerating stubs

```bash
# From repo root — generate OpenAPI spec from proto
make openapi
# The spec lands at api/openapi/v1/kosha.openapi.json (checked in)

# Generate Python stubs
npx @openapitools/openapi-generator-cli generate \
  -i api/openapi/v1/kosha.openapi.json \
  -g python \
  -o clients/python/kosha_client/stubs
```

## Native SDK: `Kosha` and ordinary documents

```python
from kosha_client import F, Kosha

kosha = Kosha()  # KOSHA_HOST and KOSHA_API_KEY
docs = kosha.namespace("docs", types={"status": "keyword"})  # sends nothing
docs.create()  # repeatable

docs.upsert([
    {"id": "1", "title": "breach of contract", "status": "open", "year": 2024},
    {"id": "2", "title": "merger antitrust review", "status": "closed", "year": 2023},
])
docs.update({"id": "1", "year": 2025})  # merge; other fields kept

result = docs.search("breach", filter=F.all_of(F.eq("status", "open"), F.range_("year", gte=2020)))
for hit in result["hits"]:
    print(hit["id"], hit["score"], hit["document"]["title"])
result["total_exact"], result["diagnostics"]  # lower-bound totals, degradation signals

docs.get("2")                        # the document, or None
docs.count(F.eq("status", "open"))   # {"count": ..., "exact": ...}
docs.delete(["2"])
```

`AsyncKosha` is the same API with `await` (`async with AsyncKosha() as kosha:`).
Also: `semantic_search`, `hybrid_search`, `aggregate`, `iterate`, `search_many`,
`bulk_upsert`, `create_branch` / `list_branches` / `delete_branch`, and
`docs.native` for wire-level calls. The contract, shared with the TypeScript and
Go SDKs, is [SDK.md](../SDK.md). `KoshaClient` below is the OpenSearch
compatibility adapter for existing OpenSearch code.

## Current state

Phase 1 ships with a lightweight hand-written client (`client.py`) that directly calls the
Kosha HTTP API. As the API stabilises, the hand-written code will shrink to just `transport.py`
+ `compat.py` + the delegation layer in `client.py`.

Since 0.3.0 the client calls only the `/v1` API (`/v1/namespaces/{ns}/documents`,
`.../replace`, `.../search`, ...), so it works against servers with
`KOSHA_LEGACY_ROUTES=off`; 0.2.x needs the legacy routes on.

**Server compatibility.** Upgrade the server before upgrading the SDK.
Partial updates (`update()`, bulk `update` actions, `update_by_query`) use
`POST /v1/namespaces/{ns}/replace` and require a server that supports it
(API contract 0.10.0 or later). The SDK surfaces any error from that
operation rather than retrying it on the deprecated route. `KoshaRequestError`
carries the server's error model: `code`, `retryable`, `retry_after_ms` and
`request_id` (see [docs/api/errors.md](../../docs/api/errors.md)), plus
`attempts`.

**Retries.** Every request follows the retry policy the TypeScript and Go
SDKs share ([clients/conformance/RETRY.md](../conformance/RETRY.md)): an
error the server marks `"retryable": true` (or a bare 429/503 from an older
server or a proxy) is retried after the server's `retry_after_ms` /
`Retry-After`, else with jittered backoff; `"retryable": false` (a quota) is
never retried. `max_retries` (default 3) counts attempts, so `max_retries=1`
fails fast. A connection that could not be opened is retried for every
operation; a timeout after the request was sent only for idempotent ones
(search, upsert, merge, delete-by-query, flush, every GET), unless you pass
`retry_non_idempotent=True`. `retry_on_timeout=False` turns off every
transport-error retry (timeouts, resets, refused connections, DNS
failures), as in 0.2.x; HTTP error responses still follow the policy.

**Native helpers.** `search_all(namespace, body, page_size=None)` yields
every hit of a native `/v1` search body, paging with `search_after` (the
sort gets an `_id` tie-break); `bulk_index(namespace, documents, ...)`
upserts native documents in batches by count and bytes, with bounded
concurrency and per-batch failures (`BulkError.result`). A batch closes
before its request body (the bytes actually sent, envelope included) would
pass `max_bytes_per_batch`, so the cap can be set to a tenant's
`max_body_bytes`. They take Kosha's
own bodies, not OpenSearch DSL, and behave like `searchAll` / `bulkIndex`
in the TypeScript and Go SDKs.

**Async client and namespace handles (`AsyncKoshaClient`).** `AsyncKoshaClient`
(`kosha_client/async_client.py`) provides native `/v1` client and immutable
`namespace(name)` APIs (`upsert`, `merge`, `search`, `search_all`, `bulk_index`,
`exists`, `delete_by_query`, `read`, `flush`, `stats`, `put_schema`, `get_schema`,
`branch`, `list_branches`) over nonblocking `asyncio` HTTP/1.1 with no extra
runtime dependencies. The same native `namespace(name)` handle is available on
synchronous `KoshaClient` so identical documents and query options work in both
variants. Serialization, `KoshaRequestError` / `KoshaConfigError` / `BulkError`,
idempotency rules, and `RETRY.md` retry defaults are shared with `Transport`.

```python
import asyncio
from kosha_client import AsyncKoshaClient

async def main() -> None:
    async with AsyncKoshaClient("http://localhost:8080", api_key="sk-...") as client:
        ns = client.namespace("docs")
        await ns.upsert([{"id": "d1", "fields": [{"name": "body", "field_type": "Text", "value": "hello"}]}])
        page = await ns.search({"query_text": "hello", "max_results": 5})
        async for hit in ns.search_all({"query_text": ""}, page_size=100):
            print(hit["doc_id"])

asyncio.run(main())
```

- **Lifecycle:** prefer `async with AsyncKoshaClient(...) as client:`, which
  always calls `await client.aclose()` on normal or exceptional exit to close
  pooled and active connections. Calling `aclose()` directly is idempotent;
  requests on a closed client raise `RuntimeError` without network I/O.
- **Cancellation:** cancelling an `asyncio.Task` awaiting a request, backoff
  sleep, `search_all()` iterator, or `bulk_index()` immediately aborts in-flight
  sockets, stops any retry or dispatch loop, cleans up child tasks, and
  propagates `asyncio.CancelledError`.
- **Bounded concurrency example:** see `examples/async_agent_retrieval.py` for
  parallel agent retrieval bounded with `asyncio.Semaphore`.

## Supported APIs

`KoshaClient` stands in for `opensearchpy.OpenSearch`. These methods are implemented:

| Method | Notes |
|--------|-------|
| `search` | Query DSL is translated to Kosha's query; a clause Kosha cannot express raises `KoshaRequestError(400)` instead of being dropped. |
| `count` | A zero-hit search with an exact total. |
| `get` | Fetch one document by id. See below. |
| `mget` | Fetch several documents by id. See below. |
| `msearch` | Several searches. See below. |
| `index`, `bulk`, `update` | `bulk` handles `index`/`create`/`update`; `delete` actions report 501. |
| `delete_by_query` | The whole query must translate as a filter. |
| `update_by_query` | Scripts support a small Painless subset (field copies, literals, `params.<map>[_did]` lookups). |
| `scroll` | Raises `NotImplementedError`: use `from`/`size` or `search_after`. |
| `indices.*` | `create`, `exists`, `delete`, `refresh`, `get`, `get_mapping`, `get_settings`, `put_mapping`, `put_settings`, `flush`, `close`. |
| `tasks.get` | Polls `update_by_query(wait_for_completion=False)` tasks. |

### `get` and `mget`

Kosha has no fetch-by-id route, and its filters cannot match `_id`. `get(index, id)` is a
one-hit search that seeks to the id: sort by `_id` descending with
`search_after: [id + "\0"]`. `id + "\0"` is the next string after `id`, so the first hit is
the largest id `<= id`, which is the document itself when it exists. The response has the ES
shape `{_index, _id, _version: 1, found, _source}`, and `_source` decodes exactly as in `search`.

- A missing document, or an index that does not exist yet, returns `{"found": false}`.
  **It does not raise.** opensearch-py raises `NotFoundError` here; with this client, check
  `found`.
- `_source` (bool, name, comma list or list), `_source_includes` and `_source_excludes`
  filter fields as in `search`. `refresh=True` flushes the index first.
- `get` sees what search sees. Under the server's default `strong` consistency (#255) that
  includes documents still in its write buffer; under `eventual` a buffered document (up to
  ~1 s, `KOSHA_FLUSH_MAX_AGE_MS`) is not found yet.

`mget(body, index=None)` accepts `{"docs": [{"_index"?, "_id", "_source"?}]}` or
`{"ids": [...]}` and returns `{"docs": [...]}` in request order, with `found: false` entries for
misses. It runs one `get`-style search per distinct document (ids cannot be combined into one
filter), so it is N round trips. A lookup the server rejects becomes an entry with an `error`
object instead of failing the whole call.

### `msearch`

`msearch(body, index=None)` takes the ES multi-search body as NDJSON (`str` or `bytes`) or as a
list alternating headers and search bodies (dicts or JSON strings). A header without `index`
searches `index`. **It is N sequential `POST /search` requests, not one batched request**:
Kosha has no multi-search endpoint. Each search goes through the same translation as `search`.
A search that fails becomes `{"error": {...}, "status": <code>}` in its slot and the rest still
run; successful items carry `"status": 200`. A malformed body (not JSON, or a header with no
search body) raises `KoshaRequestError(400)` before anything runs.

## Index introspection

`client.indices.get_mapping(index=...)` returns the namespace's real schema, read from
`GET /v1/namespaces/{ns}/schema` and translated to Elasticsearch types:

| Kosha `FieldType` | ES mapping type |
|-------------------|-----------------|
| `FIELD_TYPE_TEXT` | `text` |
| `FIELD_TYPE_KEYWORD` | `keyword` |
| `FIELD_TYPE_INTEGER` | `long` |
| `FIELD_TYPE_FLOAT` | `double` |
| `FIELD_TYPE_DATE` | `date` |
| `FIELD_TYPE_BOOLEAN` | `boolean` |
| `FIELD_TYPE_VECTOR` | `knn_vector` |
| `FIELD_TYPE_GEO_POINT` | `geo_point` |

Kosha is schemaless, so a namespace with no registered schema, or a server that predates the
schema route (404/405), returns `{"<index>": {"mappings": {"properties": {}}}}`. A field the
schema lists without a type appears as `{}`; the client never guesses a type.

`client.indices.get_settings(index=...)` reports only `{"index": {"provided_name": "<index>"}}`.
Kosha has no shards, replicas or refresh interval per namespace, so none are reported.

## Creating and deleting indices

`client.indices.create(index=..., body=...)` calls `POST /v1/namespaces`. Plain top-level
fields in `body["mappings"]["properties"]` are sent as the namespace schema (ES type to
`FieldType`, the reverse of the table above, plus `integer`/`short`/`byte` → `INTEGER`,
`float`/`half_float`/`scaled_float` → `FLOAT`, `date_nanos` → `DATE`, `dense_vector` →
`VECTOR`); object, nested and other types with no Kosha equivalent are left out, and
`settings` are ignored. Creating an index that exists raises `KoshaRequestError` with status
409; `ignore=400` (what ES callers pass for `resource_already_exists_exception`) or
`ignore=409` returns the error body instead. Creating first is optional: Kosha still creates a
namespace on its first write.

`client.indices.delete(index=...)` calls `DELETE /v1/namespaces/{ns}` for each named index and
permanently removes its documents and segments. A missing index raises `KoshaRequestError`
with status 404 (ES `NotFoundError`), unless `ignore_unavailable=True` or `ignore=404` is
passed. `client.indices.exists(index=...)` reads `GET /v1/namespaces/{ns}/stats`: 404 means
absent; other errors are raised.

## Rerank

A Cohere rerank over the top search hits, which Sage runs today as

```python
hits = client.search(index, body)["hits"]["hits"]
co.rerank(model="rerank-v3.5", query=q, documents=[h["_source"]["content"] for h in hits], top_n=50)
```

becomes one call; the server calls the provider and holds the API key:

```python
resp = client.search(index, body, rerank=KoshaClient.rerank_spec("content", model="rerank-v3.5", top_n=50))
```

`rerank` can also go in the body (Cohere's `rank_fields: ["content"]` is accepted for `field`).
Each hit's `_score` is the rerank score, with `_fused_score` (search stage) and `_rerank_score`
alongside. If the provider times out or fails, hits come back in search order and
`resp["rerank"] == {"degraded": True, "reason": ...}`.

## Agent tools and the MCP server

`AgentTools` (`kosha_client/agent.py`) is a thin SDK for AI agents over the native `/v1` API: one
method per tool, plain JSON-able arguments, and results sized for a model's context window.
`kosha-mcp` serves the same tools over the [Model Context Protocol](https://modelcontextprotocol.io)
(issue #584).

`AsyncAgentTools` is the same tools over `AsyncKoshaClient`: every method is a coroutine with the
same arguments and result (`await tools.search("indemnity")`), and `async with
AsyncAgentTools(...) as tools` closes the client it built. Both classes share one implementation,
so their results never drift apart. `kosha-mcp` runs on `AsyncAgentTools`: a tool call never holds
a worker thread while it waits on Kosha, so concurrent calls are not capped by a thread pool (only
the blocking S3/SQS calls of `upload_files` and `ingest_status` use one). In hosted mode, token
verification, the JWKS fetch and the grant exchange are async too.

```bash
uvx --from 'kosha-client[mcp]' kosha-mcp --help   # or: pip install 'kosha-client[mcp]'
export KOSHA_HOST=https://kosha.example.com KOSHA_API_KEY=sk-... KOSHA_NAMESPACE=docs
kosha-mcp                                  # stdio, for Claude Desktop / Claude Code / Cursor
kosha-mcp --transport http --port 8765     # streamable HTTP at http://127.0.0.1:8765/mcp
```

Every index tool takes an `index` argument; `--namespace` / `KOSHA_NAMESPACE` is only the
default when a call names none. Without `--auth oauth` the HTTP endpoint has no
authentication of its own: every caller acts with the server's `KOSHA_API_KEY`. It binds
loopback by default and refuses another `--host` unless you also pass `--allow-remote`, which
you should only do behind an authenticating proxy.

### Hosted mode (`--auth oauth`)

`kosha-mcp --transport http --auth oauth` is the multi-tenant server behind
`https://mcp.getkosha.dev/mcp` (issue #624), built as the `decoverai/kosha-mcp` image from
`clients/python/Dockerfile.mcp`:

```bash
claude mcp add --transport http kosha https://mcp.getkosha.dev/mcp   # Claude Code
codex mcp add kosha --url https://mcp.getkosha.dev/mcp                 # Codex
```

It is an OAuth 2.1 protected resource. `/.well-known/oauth-protected-resource` names the
authorization server (`--issuer`, default `https://app.getkosha.dev`), and a request without a
valid bearer token gets `401` with a `WWW-Authenticate` header pointing there, which is how
MCP clients start the login. A token is a JWT checked against the issuer's JWKS
(`--jwks-url`, default `<issuer>/.well-known/jwks.json`; RS256, ES256 or EdDSA) for `iss`,
`aud` (`--audience`, default the resource URL `--resource-url` and its host), `exp` and a
`grant` claim. The server swaps the grant for that grant's tenant API key at the account
service:

```http
POST <--grant-exchange-url, default <issuer>/internal/mcp/grant-key>
Authorization: Bearer $KOSHA_MCP_INTERNAL_TOKEN
{"grant": "<grant id>"}

200 {"api_key": "sk-...", "tenant": "acme"}   404/410: unknown or revoked grant -> 401
```

The answer is cached in memory for five minutes, so a revoked grant stops working within that
time; nothing is stored on disk and any replica can serve any request (stateless streamable
HTTP, JSON responses). Each tool call builds its own client with the caller's key, so the
server needs no `KOSHA_API_KEY`. All tools are registered, and the token's scopes decide what
a call may do: `kosha:read` (search, read and account tools), `kosha:write` (`index_documents`,
`branch`, `create_index`, `delete_index`) and `kosha:billing` (`usage`, `billing_link`, and
the Checkout link in a `payment_required` or `quota_exceeded` error). `create_api_key` needs
both `kosha:write` and `kosha:billing`, since the key outlives the grant. A call outside them
returns `permission_denied`. `GET /healthz` answers
`ok`. Install with the `hosted` extra (`pip install 'kosha-client[hosted]'`), which adds
`pyjwt[crypto]`.

| Tool | API call | Notes |
|------|----------|-------|
| `search` | `POST /search` with `query_text` | BM25; optional `filter`, `fields`, `include_fields`. |
| `semantic_search` | `POST /search` with `knn` | `query` (server-side embedder) or `vector`; `hybrid` fuses BM25 and kNN with RRF. |
| `filter` | `POST /search` with only `filter` | Unscored; with `sort`, a full page returns `next_search_after` for the next one (`_id` breaks ties). |
| `glob` | `POST /search` with a `glob` filter on `path`, sorted by `path` | `/contracts/**/*.pdf`; `case_insensitive`; hits are `id` and `path` unless `include_fields` names more; pages with `next_search_after`. |
| `ls` | `POST /search` with a `prefix` filter on `path`, sorted by `path` | Lists immediate subdirectories (with document counts) and direct files under `prefix` (default `/`); composes with an optional `filter`. |
| `read` | `POST /read` | By `id` or `path`: whole, `start_line`/`end_line`, `page` or `byte_start`/`byte_end`. `field` defaults to `body` (`AgentTools(text_field=...)`), where `kosha-ingest` stores text. Over `max_bytes` (64 KiB) it is `truncated`: read on from `byte_end`. |
| `count` | `POST /search`, `max_results: 0`, `exact_total_hits` | Returns `{"count", "relation"}`. |
| `aggregate` | `POST /search` with `aggs` | No hits returned. |
| `list_branches` | `GET /branches` | |
| `branch` | `POST /branch` | Only with `--allow-writes`; needs an `admin` key. |
| `index_documents` | `POST /documents` then `POST /flush` | Only with `--allow-writes`; needs a `write` key. Creates a missing index. |
| `account` | `GET /v1/billing` | Tenant, plan and `state`; `{"billing": "off"}` when the server bills nothing. |
| `list_indexes` | `GET /v1/namespaces` | Names and document counts. |
| `usage` | `GET /v1/usage` | This period's metered usage, total and per index. |
| `billing_link` | `POST /v1/billing/checkout` or `/portal` | `kind`: `checkout`, `portal`, or `auto` (portal once there is a billing customer, else checkout). |
| `create_index` | `POST /v1/namespaces` | Only with `--allow-writes`; optional `schema`. |
| `delete_index` | `DELETE /v1/namespaces/{index}` | Only with `--allow-writes`; annotated `destructiveHint`. |
| `create_api_key` | `POST /v1/api-keys` | Only with `--allow-writes`; needs an `admin` key. The secret is in this result only. |
| `upload_files` | Presigned S3 PUT + job enqueue | Enabled with `--upload-bucket` (`KOSHA_UPLOAD_BUCKET`); needs `kosha:write`. Returns 15-minute size-capped PUT URLs and `curl` lines. |
| `ingest_status` | Job status in upload bucket | Enabled with `--upload-bucket`; needs `kosha:read`. Reports per-file progress and errors. |

Every hit is `{"id", "score", "path"?, "fields", "truncated_fields"?, "highlight"?}`. `id` is the
stable document id. `path` is lifted out of the `path` field (`AgentTools(path_field=...)`) so an
agent can narrow with `{"prefix": {"path": "/contracts/2024/"}}` or `glob`, then `read` the
full text. Hits per call are capped at 100
(`max_results_cap`), each stored string is cut to 500 characters (`snippet_chars`) and named in
`truncated_fields`, and vectors are never returned. Without `--allow-writes` every tool is
annotated read-only, so a `read`-scoped key is enough (`usage` and `billing_link` need billing
access to the account).

A failed call is a tool result with `isError` whose text starts with a stable code (issue
#626), followed by the server's message; `structuredContent.error` has the same facts as JSON:

| Code | When | Extra |
|------|------|-------|
| `payment_required` | The account is suspended, read-only or past its free tier (HTTP 402) | `checkout_url`, a Checkout session to add a payment method; `reason`, `plan`, `state` |
| `quota_exceeded` | An index, document, storage or vector-dimension quota (`*_quota_exceeded`) | `limit` and `checkout_url` to upgrade |
| `not_found` | No such index, document or branch | |
| `invalid_query` | The server rejected the query or filter | |
| `invalid_argument` | A bad or missing argument | |
| `permission_denied`, `unauthenticated` | The key or token may not do this | |
| `rate_limited`, `unavailable` | Throttled or the server is down: retry | `retry_after_ms` |

The Checkout link comes from `POST /v1/billing/checkout`, which billing never gates; when
Kosha cannot start one (billing off), `--billing-url` / `KOSHA_BILLING_URL` is used instead.

Claude Desktop config (`claude_desktop_config.json`): see `examples/claude_desktop_config.json`.
From Python, without MCP: see `examples/agent_tools.py`.

## Ingesting PDF, DOCX and HTML files

`kosha-ingest` (`kosha_client/ingest.py`, issue #587) turns a folder of `.pdf`, `.docx`,
`.html`/`.htm`, `.txt` and `.md` files into Kosha documents. Extraction runs client-side
with [pypdf](https://pypi.org/project/pypdf/) and
[python-docx](https://pypi.org/project/python-docx/), both under the `ingest` extra.

```bash
pip install 'kosha-client[ingest]'
export KOSHA_HOST=https://kosha.example.com KOSHA_API_KEY=sk-...   # a write key
kosha-ingest ./shared-drive --namespace docs --prefix /drive      # one document per file
kosha-ingest ./shared-drive --namespace docs-pages --per-page     # one document per page
```

The command prints `{"indexed", "unchanged", "unsupported", "scanned", "failed"}` and exits 1
when a file failed to extract. Each document has these fields:

| Field | Type | Value |
|-------|------|-------|
| `path` | Keyword | `--prefix` + the path under the folder, e.g. `/drive/contracts/2024/msa.pdf`. |
| `body` | Text | The extracted text. Pages are separated by a form feed (`\f`). |
| `page` | Integer | With `--per-page` only: the 1-based page number. |
| `title` | Text | From the PDF metadata, DOCX core properties or HTML `<title>`, when one is set. |
| `mime_type`, `content_sha256`, `source` | Keyword | `source` defaults to `kosha-ingest` (`--source`). |
| `size_bytes`, `page_count` | Integer | |
| `modified_at` | Date | The file's mtime, in UTC. |
| `scanned` | Boolean | `true` for a PDF with no text layer. OCR is out of scope, so its body is empty. |

PDF pages are the PDF's own pages. A DOCX page ends at an explicit page break; Word's layout
pages are not known without rendering. HTML, `.md` and `.txt` files are one page, except that
form feeds in a `.txt` file (`pdftotext` output) split it into pages.

With `--per-page`, a search hit carries its page number. Without it, `read(path, page=n)` (#586)
returns page `n` of the document.

Re-running the command is idempotent. A document's id comes from its `path`, so a changed file
replaces its old document. A file with an unchanged `content_sha256` is skipped; `--force`
re-indexes it anyway. With `--per-page`, pages beyond a shortened file's new length are deleted.

## Changelog

### 0.5.0

- `Kosha` / `AsyncKosha` and immutable `Namespace` / `AsyncNamespace` handles: ordinary
  documents in and out, the contract shared with the TypeScript and Go SDKs
  (`clients/SDK.md`, #591-#593). `upsert`, `update` (merge), `bulk_upsert`, `get`, `delete`,
  `delete_where`, `search`, `semantic_search`, `hybrid_search`, `count` (exact or lower
  bound), `aggregate`, `iterate`, `search_many` (bounded, ordered, per-query errors), branch
  helpers, and `ns.native` for wire-level calls. Filter and aggregation builders in
  `kosha_client.F`.
- Search results keep every server diagnostic (`consistency_fallback`, kNN degradation,
  rerank degradation, rank details, timings) (#594).
- `KoshaClient` (OpenSearch compatibility) no longer fails silently: unstorable `_source`
  values, `put_mapping`, `put_settings` and `close` emit `KoshaCompatibilityWarning` (an error
  with `KOSHA_COMPAT_STRICT=1`); `indices.flush` reaches the server; search responses carry
  `kosha` diagnostics and `_rank_details` (#594).
- Breaking: `kosha_client.Namespace` is now the ordinary-document handle; the wire-level
  class is `NativeNamespace` (and `AsyncNativeNamespace`).

### 0.4.0

- Packaging: the distribution now carries this README as its PyPI description, and the wheel
  no longer installs a top-level `tests` package. 0.3.1 was tagged but never reached PyPI
  (the release check failed on the missing description), so this is the first 0.3+ release
  on PyPI; 0.2.x clients cannot index or search against a server with the legacy routes off,
  which is the default.
- Hosted `kosha-mcp`: `--transport http --auth oauth` serves the MCP endpoint as an OAuth
  protected resource, verifying JWT access tokens against the issuer's JWKS and swapping each
  token's grant for its tenant key, behind the new `hosted` extra (issue #624). Container
  image `decoverai/kosha-mcp` (`clients/python/Dockerfile.mcp`), published with each Python
  release (#627). See "Hosted mode".
- `kosha-mcp` tools take an `index` argument (`--namespace` is the default), and new
  `account`, `list_indexes`, `usage`, `billing_link`, `create_index`, `delete_index` and
  `create_api_key` tools, with matching `AgentTools` methods (#625).
- `kosha-mcp` tool errors start with a stable code (`payment_required` with a Checkout link,
  `quota_exceeded`, `not_found`, `invalid_query`, ...) and set `isError` (#626).
- `AgentTools` and the `kosha-mcp` MCP server (stdio and streamable HTTP), behind the new
  `mcp` extra (issue #584), with `glob` (#585) and `read` (#586) tools. See "Agent tools and
  the MCP server".
- `kosha-ingest`: PDF/DOCX/HTML/text ingestion with page boundaries, behind the new `ingest`
  extra (issue #587). See "Ingesting PDF, DOCX and HTML files".

### 0.3.1

- **Behavior change (type inference):** a `_source` value that is a list of equal-length numeric
  lists, each at least 8 long (per-token embeddings from a ColBERT-style model), is now sent as
  one `Vector` field holding the JSON bag, which the server indexes for late-interaction
  (MaxSim) search (issue #205). It used to be sent as a `Keyword` holding the same JSON. Ragged
  lists, lists of shorter tuples (polygons, bounding-box pairs) and non-numeric nested lists keep
  the `Keyword` mapping. Documents read back unchanged either way. If you index such values
  today and filter on them by exact match (a `Keyword` behavior), keep that field out of the
  upgrade or reindex: new writes send it as `Vector`, which is not filterable.
- `knn` passes `vectors` (a bag of query token vectors) and `scoring` (`"cosine"` or
  `"late_interaction"`) through to the server. `vector` with `vectors`, or either with
  `query_text`, is refused.

### 0.3.0

- Calls only the `/v1` API (see "Current state").
- **Behavior change:** HTTP errors are retried under the shared policy
  ([clients/conformance/RETRY.md](../conformance/RETRY.md)): 429 and 503,
  and any error with `"retryable": true`, are retried (up to `max_retries`
  attempts in total, 3 by default) after the server's delay. `max_retries=1`
  restores fail-fast.
- **Behavior change:** a timeout after the request was sent is no longer
  retried for non-idempotent operations (namespace create, schema put,
  branches, shards) unless `retry_non_idempotent=True`. Merges sent to the
  deprecated `POST /replace` (pre-0.9 servers) still count as idempotent.
- `retry_on_timeout=False` still stops every transport-error retry,
  refused connections and DNS failures included, as in 0.2.x.
- **Behavior change:** request bodies are compact UTF-8 JSON (no spaces,
  non-ASCII text unescaped), the same bytes the TypeScript and Go SDKs send.
  The server reads them the same; bodies with non-ASCII text are smaller.
- `KoshaRequestError` gains `code` (falling back to a tenant-limit
  `reason`), `retryable`, `retry_after_ms`, `request_id` and `attempts`.
- New `search_all()` and `bulk_index()` native helpers.
- The 0.2.17 retry rules are replaced by the shared policy above (503 is
  retried for writes too; 502/504 only for idempotent operations). The
  0.2.17 `retry_budget` (seconds) is still accepted and sets
  `total_budget_ms`. Connections stay kept alive and pooled as in 0.2.17.

### 0.2.17

- **Behavior change:** the transport retries what the server asks it to retry. A `429`
  (any request: the server sends it only for work it refused before starting) and a
  `502`/`503`/`504` on a read (search, count, get, `GET` routes) are retried after the
  server's `Retry-After` plus jitter, or a full-jitter exponential backoff without one.
  Attempts in total are `max_retries` (default 3), and no retry starts later than
  `retry_budget` seconds (default 20) after the first attempt; a `Retry-After` that does not
  fit the budget (a quota refusal says 60 s) raises at once. `5xx` on writes, `500` and
  every other `4xx` (including `402`) are raised immediately, as before.
  `retry_budget=0` restores the old behavior.
- The attempts and the budget apply **per HTTP call**. A method that makes several calls
  multiplies them: `msearch` (one search per body), `mget` (one search per id), `bulk`
  (one call per namespace and operation, per `helpers.bulk` chunk) and `refresh=True` writes
  (an extra flush) can each wait up to `retry_budget` once per call.
- An error body that is not a JSON object (a proxy's HTML page, a JSON list) now raises
  `KoshaRequestError` with the raw body as `error`, instead of an `AttributeError`.
- Connections are kept alive and reused, instead of one connection (one DNS lookup, one TCP
  handshake) per request. A process keeps at most 10 idle connections per host, for 15 s
  idle (the server closes at 30 s) and 60 s in total, so new server pods get traffic; all
  clients for the same host share them, and the pool is thread-safe and reset in a forked
  child. A read whose reused connection turns out closed is resent once on a new
  connection; a write takes the connection-error retry. Timeouts and errors are those of
  `urlopen` as before. A host that the `*_proxy` environment sends through a proxy still
  uses `urlopen` per request.

### 0.2.16

- Added the `consistency` option, `KoshaClient(..., consistency="strong" | "eventual")`
  (issue #255). It is sent with every search (`search`, `count`, `get`, `mget`, `msearch`);
  `None`, the default, leaves it to the server, which defaults to `strong`: writes are
  searchable as soon as they return, without a flush. With `consistency="strong"`,
  `refresh="wait_for"` no longer flushes (the write is already searchable); `refresh=True` and
  `indices.refresh()` still flush.

### 0.2.15

- Added `get`, `mget` and `msearch` (issue #198). They raised `AttributeError` before.
- **Behavior change:** `indices.delete()` deletes the namespace (`DELETE /v1/namespaces/{ns}`)
  instead of logging and returning `{"acknowledged": true}` without deleting anything. A
  missing index now raises a 404 `KoshaRequestError`.
- `indices.create()` creates the namespace (`POST /v1/namespaces`) with a schema translated
  from the ES mappings, and raises 409 when it already exists.
- `indices.exists()` uses the namespace stats route and raises errors other than 404 instead
  of reporting them as "absent".

### 0.2.14

- **Behavior change:** `indices.get_mapping()` no longer returns a fixed, made-up set of
  fields for every index. It returns the namespace's schema, or empty `properties` when there
  is none. Code that read fields from the old placeholder mapping now gets no fields and should
  handle an empty mapping. `indices.get()` reports the same mapping.
- **Behavior change:** `indices.get_settings()` no longer reports `refresh_interval`,
  `number_of_shards`, `number_of_replicas` or `knn`; it returns only `provided_name`.
- `get_mapping` / `get_settings` / `get` accept a list or comma-separated string of indices.
- Removed application-specific field names from examples, tests and docstrings.

### 0.2.11.2

Keep-alive on top of 0.2.11.1 (which it includes); nothing else from 0.2.12+ is included.

- Connections are kept alive and reused, instead of one connection (one DNS lookup, one TCP
  handshake) per request. A process keeps at most 10 idle connections per host, for 15 s
  idle (the server closes at 30 s) and 60 s in total, so new server pods get traffic; all
  clients for the same host share them, and the pool is thread-safe and reset in a forked
  child. A read whose reused connection turns out closed is resent once on a new
  connection; a write takes the connection-error retry. Timeouts and errors are those of
  `urlopen` as before. A host that the `*_proxy` environment sends through a proxy still
  uses `urlopen` per request.

### 0.2.11.1

Retry-only release on top of 0.2.11; nothing else from 0.2.12+ is included.

- **Behavior change:** the transport retries what the server asks it to retry. A `429`
  (any request) and a `502`/`503`/`504` on a read (`GET`, or a `POST` to the `search`
  route) are retried after the server's `Retry-After` plus jitter, or a full-jitter
  exponential backoff without one. Attempts in total are `max_retries` (default 3), and no
  retry starts later than `retry_budget` seconds (default 20) after the first attempt; a
  `Retry-After` that does not fit the budget (a quota refusal says 60 s) raises at once.
  `5xx` on writes, `500` and every other `4xx` (including `402`) are raised immediately, as
  before. `retry_budget=0` restores the old behavior.
- The attempts and the budget apply **per HTTP call**: `bulk` (one call per namespace and
  operation, plus a flush per namespace, per `helpers.bulk` chunk) can wait up to
  `retry_budget` once per call.
- An error body that is not a JSON object now raises `KoshaRequestError` with the raw body as
  `error`, instead of an `AttributeError`.
