# OKF4net.Agents [Microsoft Agent Framework](https://github.com/microsoft/agent-framework) function tools for [Open Knowledge Format (OKF) v0.2](https://github.com/GoogleCloudPlatform/knowledge-catalog/tree/main/okf) knowledge bundles. This package exposes the operations of the [OKF4net](https://www.nuget.org/packages/OKF4net) library — reading, browsing, searching, writing, indexing and validating OKF bundles — as `AIFunction` tools that AI agents can call. It depends on `Microsoft.Agents.AI` (this is the integration layer; the underlying `OKF4net` core library remains dependency-free). ## Quick start ```csharp using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using OKF4net.Agents; var tools = new OkfBundleTools("./my_bundle"); AIAgent agent = chatClient.AsAIAgent( instructions: "You manage an OKF knowledge bundle.", tools: tools.GetTools()); var response = await agent.RunAsync("Summarize the concepts in this bundle."); ``` ## Sharing a `BundleHandle`, and what can write `new OkfBundleTools(string root)` builds its own `BundleHandle` over a `FileSystemBundleProvider`. To serve a bundle from another `IBundleProvider`, or to share one in-memory image between consumers, build the handle yourself and pass it to `new OkfBundleTools(handle)`; the tool set exposes it as `Handle`. Hand the **same** handle to the `OkfContextProvider` built over the tools and to anything else that reads the source: they then see one image, and a committed write by any of them (`BundleConceptWriter` over the handle) invalidates it for all. A consumer built from a root string makes its own handle, hence its own image. Whether the write tools are offered combines the provider and the mode: effective write = `provider.Capabilities.CanWrite && mode != OkfToolMode.ReadOnly`. `GetTools(OkfToolMode.ReadOnly)` omits the write tools, and so does any mode over a provider whose `CanWrite` is false (it is treated as `ReadOnly`); `RequireApprovalForWrites` wraps them in an approval gate. A direct call to a public write method over a read-only provider (`WriteConceptAsync`, `AppendLogAsync`, `RegenerateIndexesAsync`, `VerifyAsync`, and their obsolete `string` spellings) throws `BundleReadOnlyException` before any argument is checked and before anything is written. The obsolete `string` spellings also refuse a provider that does not complete synchronously, with `InvalidOperationException`; which exception a provider that is *both* read-only and asynchronous gets depends on the method, because each checks in its own order. `WriteConcept` and `AppendLog` check writability first and throw `BundleReadOnlyException`; `RegenerateIndexes` and `Verify` check synchronous completion first and throw `InvalidOperationException`. A host should not rely on that order: the spellings are obsolete, and the asynchronous methods throw `BundleReadOnlyException` whatever the provider's completion. ## The twelve tools `okf_read_concept`, `okf_browse`, `okf_graph`, `okf_search`, `okf_audit`, `okf_write_concept`, `okf_verify`, `okf_append_log`, `okf_regenerate_indexes`, `okf_validate_bundle`, `okf_changes_since`, `okf_get_computation` — plus a thirteenth, `okf_run_computation`, only when the tool set is constructed with an `OKF4net.Attestation` orchestrator wired in. All tools return agent-friendly markdown/plain text and never throw for expected errors (unknown ids, invalid paths, malformed input) — the agent receives an explanatory message instead. All four write tools (`okf_write_concept`, `okf_verify`, `okf_append_log`, `okf_regenerate_indexes`) serialize their writes and rely on the Agent Framework's tool-approval mechanism for gating. Their validation levels differ, though: only `okf_write_concept` checks a document against the stricter producer-grade OKF rules (non-empty `type`, `title`, `description`) before touching disk; `okf_verify` deliberately enforces only §11 conformance (a non-empty `type`) instead — recording a review is not producing content, and refusing a reviewer because a concept is missing a `description` would make precisely the concepts an audit surfaces unstampable. What `okf_verify` writes is a `{by, at}` review stamp (§5.2) — a dated declaration, not a proof; see the project README's `okf verify` section for what it does and doesn't guarantee. `okf_append_log` validates only its own `kind`/`text` arguments (non-empty, no embedded newline or null byte) and re-renders `log.md` through the §9 model — it does not touch concept documents at all. `okf_regenerate_indexes` performs no document validation whatsoever; it only rebuilds `index.md` listings from whatever is already on disk. Bundle content is treated as untrusted and is never injected as a system message. `OkfContextProvider` (an `AIContextProvider`, registered via `ChatClientAgentOptions.AIContextProviders`) layers on top of the same `OkfBundleTools` instance to automatically inject budget-bounded bundle context into each invocation (as a message, never as system instructions). Memory capture into deterministic, per-day memory concepts is **off by default** (`MemoryCaptureMode.Disabled`) — no LLM call, no extra tool round-trip — and only happens once a caller explicitly opts in via `OkfContextProviderOptions.MemoryCapture = MemoryCaptureMode.Enabled`, which writes captured exchanges into the same bundle the agent reads from. Note: the token budget is a soft chars/4 estimate (can be exceeded slightly), its `` elements keep their structure (`&` and `<` inside are escaped and the content is fenced, so bundle content can neither close one nor leave a fence open into the next) but are not a security boundary, and same-day memory capture is safe across concurrent sessions **within one process** — `OkfBundleTools` shares its write lock, via a process-wide registry, across every instance whose bundle root resolves to the same directory when the lock is taken (a junction or symlink to the bundle shares its lock; if resolution fails, the lexical path is the key; a topology change during an operation is not followed). Operations over an identical namespace-normalized lexical root remain serialized even when its resolved key changes, with one exception: a thread already holding a write lock never waits for that lexical gate. A nested acquisition that finds the gate busy (which ordinary contention on that root causes, with no topology change) holds no gate for its whole operation; if the root's resolution changes while it runs, another writer on the same lexical root can overlap it. Releasing the gate before the monitor, and retrying it once (without blocking) after resolving, narrow this but do not close it. Different aliases may still diverge during an active topology change; targets are not pinned. Nested calls over different resolved bundles need a consistent lock order over the registry's case-folded keys. A link on the path retargeted during an operation, even one every alias follows, splits the bundle in two for that purpose: aliases of it nested in opposite order can then deadlock between their resolved monitors. The guarantee does not extend across separate processes sharing a bundle path, and the reparse-point guard write tools rely on is a best-effort check-then-write, not a guarantee against a concurrent local actor substituting a path component mid-write (see the project README's concurrency caveats for the full scope). See the [project README](https://github.com/jchable/okf4net) for the full documentation, and NOTICE for the licensing of this package and of the material vendored in the repository it is built from. Licensed LGPL-3.0-or-later.