Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion docs/architecture/http-api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
last_edited: 2026-09-16
last_edited: 2026-10-04
title: HTTP API
description: The agent-first HTTP API — filesystem-shaped endpoints, revision preconditions, and the daemon's error contract.
---
Expand Down Expand Up @@ -109,6 +109,7 @@ Endpoints are filesystem-shaped, under `/api/v1`:
| `POST /processing/jobs` · `GET /processing/jobs/{id}` | run a reviewed plan with streamed job identity / read aggregate status | Implemented |
| `POST /processing/consent/grants` · `POST /processing/consent/revocations` | grant reviewed profile consent / revoke this operator's processing consent | Implemented |
| `GET /renditions/{attachment_id}` · `POST /renditions/select` | stream retained sanitized Markdown by attachment or exact source selector | Implemented |
| `POST /evidence/windows` | read bounded text from an exact current/live file and active rendition | Implemented |
| `GET /coverage?profile=&vault_uid=&content_version_id=` · `POST /search` | inspect separate rendition/embedding coverage / search an authorized source-version set | Implemented |
| `POST /search/similar` | stored-vector neighbors | Implemented |
| `POST /derivatives/purge-plans` · `POST /derivatives/purge-jobs` | preview / run a live derivative purge without changing immutable backups | Implemented |
Expand Down Expand Up @@ -449,6 +450,48 @@ unsatisfiable ranges return `416 invalid_rendition_range`.
See the [Markdown contract](document-derivatives.md#sanitized-markdown-contract)
for the envelope and body-relative navigation.

#### Exact evidence windows

`POST /api/v1/evidence/windows` (`readEvidenceWindow`) reads an exact cited
sanitized-Markdown rendition. It requires the ordinary API key and a JSON
request of at most 16 KiB. The character and identity limits keep its JSON
response below 128 KiB.

Supply `vault_uid`, positive `node_id`, `content_version_id`, `content_sha256`,
`rendition_attachment_id`, `build_id`, and `rendition_sha256`. All identities
are required. Hashes and build/attachment IDs are lowercase 64-character hex.
Obtain these identities from file-version and rendition metadata, then retain
them together. An existing stored build alone does not authorize a read.

`offset` is a nonnegative Unicode scalar offset. Omitted or zero `max_chars`
selects 8,000; explicit limits are 1 through 16,000. The response repeats every
identity and supplies `text`, `actual_start`, exclusive `actual_end`,
`next_offset`, `eof`, UTF-8 `response_bytes`, and `media_type: "text/markdown"`.
An offset at EOF returns empty text and `eof: true`.

The daemon checks request syntax, vault identity, current/live node and version,
content hash, active attachment, build, Markdown artifact hash, then range.
A well-formed reference that no longer matches visible authority returns
`404 evidence_unavailable` without text or details about hidden components.
Invalid JSON or references rejected by the shared reader return 400. Schema
violations, including unknown fields, return 422. Offsets beyond EOF return 416.
Unavailable physical storage returns 503 with the standard `content_missing`,
`store_fenced`, or `store_unavailable` code. Cancellation and actual integrity
or cleanup failures are preserved. Reads do not run processing or choose a
newer version or rendition.

A rename preserves the citation; content or rendition replacement, trash,
pruning, and retirement invalidate it. Digests identify catalog authority;
a partial window does not claim fresh whole-artifact hash verification.
Search segment offsets are not Markdown offsets. Begin an overview at zero,
or continue using offsets returned by an earlier evidence window.

`POST /api/v1/renditions/windows` retains its existing request fields, defaults,
and current/live visibility checks. Both routes also require a nonempty source
blob hash and check active rendition authority in the same catalog snapshot.
The stricter evidence route adds mandatory identity preconditions to the same
bounded reader.

#### Similar documents

`POST /api/v1/search/similar` accepts `selector`, optional `binding_id`,
Expand Down
44 changes: 43 additions & 1 deletion docs/embedding.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
last_edited: 2026-09-16
last_edited: 2026-10-04
title: Embed in Go
description: Own one or more independently rooted Docbank vaults inside a Go application, with CGO or pure-Go SQLite.
---
Expand Down Expand Up @@ -256,6 +256,48 @@ runner.
Set `DocumentSearchRequest.ContentFirst` to prefer content matches, following the
[HTTP search ordering contract](architecture/http-api.md#coverage-and-source-fenced-search).

## Read exact evidence windows

Use `ReadEvidenceWindow` to read a bounded excerpt from a rendition you already
identified. Retain the file's vault, node, content-version and content SHA-256
alongside the attachment, build and sanitized-Markdown SHA-256 returned by
`Rendition` or document detail metadata. Pass all seven identities in an
`EvidenceWindowRequest`:

```go
window, err := vault.ReadEvidenceWindow(ctx, docbank.EvidenceWindowRequest{
VaultUID: vault.ID(), NodeID: receipt.Node.ID,
ContentVersionID: receipt.Version.ID, ContentSHA256: receipt.Computed.SHA256,
RenditionAttachmentID: rendition.AttachmentID,
BuildID: rendition.BuildID, RenditionSHA256: rendition.SHA256,
Offset: 0, MaxChars: 8_000,
})
if err != nil {
return err
}
// Keep window.NextOffset to continue from the exclusive end of this window.
```

Offsets count Unicode scalars. Zero `MaxChars` selects 8,000; explicit limits
are 1 through 16,000. `ActualStart`, `ActualEnd`, and `NextOffset` describe the
returned range; `ResponseBytes` counts its UTF-8 bytes. A read starting at EOF returns
empty text with `EOF: true`. An offset beyond EOF matches
`ErrInvalidRenditionWindow`; malformed references match `ErrInvalidEvidenceRequest`.

Only the current, live content version and its active rendition are readable.
A rename preserves the reference. A content or rendition replacement, trash,
pruning, or retirement makes the old reference match `ErrEvidenceUnavailable`.
The read never runs processing, selects a newer rendition, or pins retention.
The vault lifecycle lease covers the internal blob read and cleanup and is
released before the function returns. Callers receive text, not an open stream.

The returned digest identifies catalog authority. A bounded read can end before
whole-artifact checksum verification reaches EOF. Search chunk and embedding
segment offsets use their own coordinates; do not use them as Markdown offsets.
Start a document overview at zero, or retain offsets from an earlier window.
See the [HTTP evidence contract](architecture/http-api.md#exact-evidence-windows)
for the equivalent authenticated route.

## Retain and process a remote recording

Embedded callers can keep the recording identity separate from the protected
Expand Down
81 changes: 81 additions & 0 deletions evidence_window_external_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package docbank_test

import (
"bytes"
"io"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/docbank"
"go.kenn.io/docbank/document/plaintext"
)

func TestEmbeddedEvidenceWindow(t *testing.T) {
t.Parallel()
provider, err := plaintext.New(plaintext.Profile{MaxDocumentBytes: 1 << 20})
require.NoError(t, err)
vault, err := docbank.New(t.Context(), docbank.Config{Root: t.TempDir(), Processing: docbank.ProcessingOptions{Profiles: map[string]docbank.ProcessingProfileConfig{
"private": {Profile: embeddedProcessingProfile(t, provider.Descriptor()), RenditionProvider: provider},
}}})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, vault.Close()) })
receipt, err := vault.Put(t.Context(), "/evidence.txt", strings.NewReader(strings.Repeat("aé界🙂z", 4000)), docbank.PutOptions{MediaType: "text/plain"})
require.NoError(t, err)
selector := docbank.ProcessingSelector{NodeID: receipt.Node.ID, ContentVersionID: receipt.Version.ID, Profile: "private"}
plan, err := vault.PlanProcessing(t.Context(), docbank.ProcessingPlanRequest{Selector: selector})
require.NoError(t, err)
_, err = vault.StartProcessing(t.Context(), docbank.StartProcessingRequest{PlanRequest: docbank.ProcessingPlanRequest{Selector: selector}, PlanFingerprint: plan.Fingerprint, Consent: true})
require.NoError(t, err)
rendition, err := vault.Rendition(t.Context(), docbank.RenditionRequest{Selector: selector})
require.NoError(t, err)
body, err := io.ReadAll(rendition.Reader)
require.NoError(t, err)
require.NoError(t, rendition.Reader.Close())
request := docbank.EvidenceWindowRequest{VaultUID: vault.ID(), NodeID: receipt.Node.ID, ContentVersionID: receipt.Version.ID, ContentSHA256: receipt.Computed.SHA256, RenditionAttachmentID: rendition.AttachmentID, BuildID: rendition.BuildID, RenditionSHA256: rendition.SHA256}
got, err := vault.ReadEvidenceWindow(t.Context(), request)
require.NoError(t, err)
runes := []rune(string(body))
assert.Equal(t, string(runes[:8000]), got.Text)
assert.False(t, got.EOF)
assert.Equal(t, receipt.Version.ID, got.ContentVersionID)
assert.Equal(t, receipt.Computed.SHA256, got.ContentSHA256)
assert.Equal(t, rendition.AttachmentID, got.RenditionAttachmentID)
assert.Equal(t, rendition.BuildID, got.BuildID)
assert.Equal(t, rendition.SHA256, got.RenditionSHA256)
assert.Equal(t, len(string(runes[:8000])), got.ResponseBytes)
assert.Equal(t, "text/markdown", got.MediaType)
request.MaxChars = 16000
got, err = vault.ReadEvidenceWindow(t.Context(), request)
require.NoError(t, err)
assert.Equal(t, string(runes[:16000]), got.Text)
assert.Equal(t, 16000, got.NextOffset)
require.Contains(t, string(body), "aé界🙂z")
prefix, _, found := bytes.Cut(body, []byte("aé界🙂z"))
require.True(t, found)
offset := len([]rune(string(prefix)))
request.Offset = offset + 1
request.MaxChars = 3
got, err = vault.ReadEvidenceWindow(t.Context(), request)
require.NoError(t, err)
assert.Equal(t, "é界🙂", got.Text)
assert.Equal(t, offset+1, got.ActualStart)
assert.Equal(t, offset+4, got.ActualEnd)
assert.Equal(t, offset+4, got.NextOffset)
stale := request
stale.BuildID = strings.Repeat("a", 64)
got, err = vault.ReadEvidenceWindow(t.Context(), stale)
require.ErrorIs(t, err, docbank.ErrEvidenceUnavailable)
assert.Empty(t, got.Text)
malformed := request
malformed.ContentSHA256 = ""
_, err = vault.ReadEvidenceWindow(t.Context(), malformed)
require.ErrorIs(t, err, docbank.ErrInvalidEvidenceRequest)
request.Offset = len([]rune(string(body))) + 1
_, err = vault.ReadEvidenceWindow(t.Context(), request)
require.ErrorIs(t, err, docbank.ErrInvalidRenditionWindow)
require.NoError(t, vault.Close())
_, err = vault.ReadEvidenceWindow(t.Context(), request)
require.ErrorIs(t, err, docbank.ErrClosed)
}
139 changes: 139 additions & 0 deletions frontend/src/generated/docbank.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions internal/api/processing_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,34 @@ type RenditionTextWindow struct {
EOF bool `json:"eof"`
ResponseBytes int `json:"response_bytes" minimum:"0" maximum:"1048576"`
}

// EvidenceWindowRequest requires exact current/live file and rendition identity.
type EvidenceWindowRequest struct {
VaultUID string `json:"vault_uid" format:"uuid"`
NodeID int64 `json:"node_id" minimum:"1"`
ContentVersionID string `json:"content_version_id" format:"uuid"`
ContentSHA256 string `json:"content_sha256" pattern:"^[0-9a-f]{64}$"`
RenditionAttachmentID string `json:"rendition_attachment_id" pattern:"^[0-9a-f]{64}$"`
BuildID string `json:"build_id" pattern:"^[0-9a-f]{64}$"`
RenditionSHA256 string `json:"rendition_sha256" pattern:"^[0-9a-f]{64}$"`
Offset int `json:"offset,omitzero" minimum:"0"`
MaxChars int `json:"max_chars,omitzero" minimum:"0" maximum:"16000" default:"8000"`
}

// EvidenceWindow reports catalog identities and a bounded Unicode excerpt.
type EvidenceWindow struct {
VaultUID string `json:"vault_uid" format:"uuid"`
NodeID int64 `json:"node_id" minimum:"1"`
ContentVersionID string `json:"content_version_id" format:"uuid"`
ContentSHA256 string `json:"content_sha256" pattern:"^[0-9a-f]{64}$"`
RenditionAttachmentID string `json:"rendition_attachment_id" pattern:"^[0-9a-f]{64}$"`
BuildID string `json:"build_id" pattern:"^[0-9a-f]{64}$"`
RenditionSHA256 string `json:"rendition_sha256" pattern:"^[0-9a-f]{64}$"`
Text string `json:"text" maxLength:"16000"`
ActualStart int `json:"actual_start" minimum:"0"`
ActualEnd int `json:"actual_end" minimum:"0"`
NextOffset int `json:"next_offset" minimum:"0"`
EOF bool `json:"eof"`
ResponseBytes int `json:"response_bytes" minimum:"0" maximum:"64000"`
MediaType string `json:"media_type" enum:"text/markdown"`
}
Loading
Loading