Knowledge Forge
English | 繁體中文
Knowledge Forge turns team-approved source material into a wiki that conforms to Google Open Knowledge Format 0.2. LangGraph orchestrates a fixed workflow with one LangChain reasoning-agent role for cross-document concept planning and synthesis. Each planning or Concept synthesis task runs in an isolated reasoning session.
Agents and people jointly maintain the resulting Bundle. A later update never silently overwrites human edits. A deterministic three-way merge combines non-overlapping changes. A conflict in the same structural block leaves the live Bundle unchanged and creates an auditable reconciliation workspace.
Knowledge Sources
The Input Corpus can contain PDFs, ordinary non-empty UTF-8 Markdown documents, and recognized OKF 0.2 import Bundles. Ordinary Markdown is opaque untrusted evidence: frontmatter, HTML, comments, and body text are indexed without executing includes, fetching URLs, following links, or reading linked images. Markdown evidence uses a durable heading path, same-level occurrence, and content hash; displayed line ranges are diagnostic only. A directory whose root index.md declares only okf_version: "0.2" is treated as an import boundary, not as ordinary evidence. Recognized Bundles are validated before import, and nested recognized Bundles are imported from their innermost boundary.
A recognized OKF 0.2 Bundle is imported without model configuration or reasoning. Its Concept Documents retain their exact UTF-8 Markdown bytes, identities, languages, types, extensions, provenance, citations, links, and image syntax; Knowledge Forge never translates or mutates imported content.
MVP scope
- Input: recursively discovered PDFs with complete text layers and ordinary Markdown sources under a specified directory; recognized OKF 0.2 Bundles are imported at their declared boundaries.
- Output: an OKF 0.2 Markdown Bundle. The MVP does not include a Web UI, chat, RAG API, or source PDFs.
- Model: for agent-backed operations, an OpenAI-compatible endpoint that supports tool calling through the Responses API. Chat Completions is not used.
- Retrieval: a temporary SQLite FTS5 evidence index created for each run and deleted afterward.
- Agent-generated Concept types:
Concept, Definition, Policy, Procedure, and FAQ; imported Concepts retain their source-declared types.
Image-only scanned PDFs, encrypted PDFs, damaged files, empty or non-UTF-8 Markdown, documents with no extractable text, symlinks, malformed imported Bundles, and normalized path or imported Concept ID collisions cause the complete operation to fail atomically. A PDF with extractable text can contain intentionally blank pages.
Source content is untrusted data. The agent has no shell, network, or arbitrary file-writing tools. Extracted source evidence is sent to the configured model endpoint when reasoning is required. Import-only operations do not call a model. Knowledge Forge does not write source files, extracted full text, or API keys into the Bundle. Model requests always use the Responses API with store: false. Knowledge Forge refuses to run when LangSmith or LangChain tracing is enabled, which prevents additional transmission of source content.
Source and output isolation
Every source-backed operation (generate, update, reconcile, verify, and validate when --source is supplied) requires the resolved --source and --out directory trees to be disjoint. Knowledge Forge rejects equal paths, an output nested anywhere under the source, or a source nested anywhere under the output. Resolving both paths first means relative spellings such as sources/../sources cannot bypass the check.
Rejection happens before source discovery, model configuration validation, model calls, staging, or report creation, so the live Bundle and private state remain absent or unchanged. Use sibling trees, for example:
workspace/
sources/
knowledge/
Source discovery is recursive and there is currently no manifest or ignore policy. Every supported file under --source belongs to the authoritative Input Corpus, so keep the source root clean or physically move material that the team has not authorized as evidence.
Installation
Python 3.12 and uv are required:
uv sync
The model name must be explicit for operations that require reasoning. API keys are not written into the Bundle or state:
cp .env.example .env
Edit .env in the current working directory:
OPENAI_API_KEY=...
OPENAI_MODEL=...
# Optional:
OPENAI_BASE_URL=https://models.example/v1
Knowledge Forge reads only the .env in the command's working directory. It does not search parent directories. CLI arguments and existing process environment variables take precedence over .env, so local files do not override values injected by CI, containers, or secret managers.
A custom OPENAI_BASE_URL must support the OpenAI Responses API (/v1/responses), function calling, and structured tool output. A service that implements only /v1/chat/completions cannot be used. Knowledge Forge allows parallel tool calls by default and preserves every model-issued call with its matching result during Responses replay.
You can also use the process environment directly:
export OPENAI_API_KEY=...
export OPENAI_MODEL=...
# Optional: export OPENAI_BASE_URL=https://models.example/v1
CLI
The model and endpoint for agent-backed generate and update can also be specified on the command line. A source tree containing only recognized OKF import Bundles is imported without model configuration or reasoning:
uv run knowledge-forge generate \
--source ./pdfs \
--out ./knowledge \
--model <model> \
--base-url https://models.example/v1
Although the CLI accepts --api-key, prefer .env, OPENAI_API_KEY, or a production secret manager so credentials do not remain in shell history. Git ignores .env; never put a real secret in .env.example.
Create a new Bundle. --out must not exist or must be an empty directory:
uv run knowledge-forge generate \
--source ./pdfs \
--out ./knowledge \
--language auto \
--max-agent-steps 50 \
--concept-concurrency 4
For generate and agent-backed update, --max-agent-steps is the maximum number of model calls for each reasoning task. Concept planning receives one independent budget, and every Concept synthesis receives a fresh budget. A complex Concept therefore cannot consume the model calls available to later Concepts. If a task exceeds its limit, the error identifies planning or the affected Concept and the candidate Bundle is not published.
After planning, generate synthesizes up to four independent Concept Documents concurrently by default. Set --concept-concurrency 1 to run synthesis sequentially, or select another positive limit to balance throughput against provider rate limits. Tasks may complete in any order, but the Bundle is assembled in ConceptPlan order and is published only when every Concept succeeds. The selected concurrency is part of Generation Identity.
Concept concurrency is separate from parallel tool calls. --concept-concurrency controls how many Concept synthesis tasks run at once. Within each task, parallel tool-call mode controls whether one model turn may request several search_pages or read_pages calls.
For a Responses-to-Bedrock gateway that cannot replay multiple tool results, explicitly select non-parallel compatibility mode on either generate or update:
uv run knowledge-forge generate \
--source ./pdfs \
--out ./knowledge \
--no-parallel-tool-calls
Use the same option when updating an existing Bundle:
uv run knowledge-forge update \
--source ./pdfs \
--out ./knowledge \
--no-parallel-tool-calls
Compatibility mode sends parallel_tool_calls: false. If the model or gateway ignores that request and still returns multiple calls, LangChain middleware deterministically keeps one call and its matching result for that turn. Provider API errors fail immediately; Knowledge Forge never switches modes automatically. The selected mode is shown on stderr and stored in the Generation Identity.
generate and update show the current phase on stderr. After the concept count is known, they also show synthesis progress:
[knowledge-forge] Tool-call mode: parallel.
[knowledge-forge] Reading Knowledge Sources...
[knowledge-forge] Loaded 3 Knowledge Sources with 84 evidence units.
[knowledge-forge] Indexing 84 evidence units from 3 Knowledge Sources...
[knowledge-forge] Planning concepts with the reasoning agent...
[knowledge-forge] Planned 6 concepts in Traditional Chinese.
[knowledge-forge] Synthesizing concept 1/6: refund-policy
Each completed phase includes its duration, and the command ends with a total-duration summary:
[knowledge-forge] Knowledge Source reading completed in 0.214s.
[knowledge-forge] Knowledge Source indexing completed in 0.038s.
[knowledge-forge] Concept planning completed in 12.481s.
[knowledge-forge] Concept synthesis 1/6 (refund-policy) completed in 8.327s.
[knowledge-forge] Concept rendering and validation completed in 0.006s.
[knowledge-forge] Candidate Bundle writing and validation completed in 0.014s.
[knowledge-forge] Atomic publication completed in 0.002s.
[knowledge-forge] Total processing time: 61.842s.
On failure, completed phases and total elapsed time are still reported while the existing error and exit code are preserved. These messages do not contain full source text, model responses, or API keys. Timing and progress are operational stderr output only; they do not enter the OKF Bundle, Agent Baseline, Generation Identity, reconciliation artifacts, or private state. The final command result remains on stdout for use in shell pipelines.
Update from the complete authoritative Knowledge Source set. --source is not the set of files changed in this run. It is the complete set of supported sources that the Team Knowledge Wiki must currently use:
uv run knowledge-forge update \
--source ./pdfs \
--out ../knowledge-base
When the Knowledge Sources, Bundle, state, and Generation Identity are all unchanged, update does not call the model or write files. Tool-call mode is part of Generation Identity, so changing it requires explicit regeneration instead of returning No changes. People can edit Concept body text and the type, title, description, tags, and status fields. Do not directly edit generated, sources, hashes, locators, verified, index.md, log.md, or .knowledge-forge/.
update reports durations only for phases that actually run. A deterministic No changes result reports current Bundle validation, Knowledge Source reading, no-change evaluation, and total time without model phases. An unchanged source set with human edits reports Agent Baseline reuse and merge phases without planning or synthesis. Regeneration reports temporary indexing, planning, and every Concept synthesis separately. Agent candidate merge and Reconciliation Conflict detection are measured as one combined phase because the structural three-way merge detects conflicts as it runs. Candidate validation, reconciliation artifact writing when required, and atomic publication when it occurs are also timed. Reconciliation keeps exit code 3, staleness keeps exit code 4, other operational failures keep exit code 2, and all still report completed phases and total elapsed time.
A valid document that a person adds under concepts/ is registered as a permanent Human-owned Concept during the next mutation. A normal update does not rewrite, delete, or adopt it. The MVP does not yet provide an ownership adoption command.
When referenced evidence, planning coverage, or the Generation Identity becomes stale, update leaves the live Bundle and managed state unchanged, writes a Regeneration Impact Report, and exits with code 4:
knowledge.staleness.md
knowledge.staleness/
manifest.json
The report distinguishes Concepts with changed or missing referenced evidence, stale planning coverage, and Agent-owned Concepts affected by a Generation Identity change. Imported and Human-owned Concepts are not marked stale merely because the generation policy changed. Review the report, then authorize full replanning and synthesis with the matching source set and generation options:
uv run knowledge-forge update \
--source ./sources \
--out ./knowledge \
--regenerate-all
The authorization is bound to the live Bundle, source set, and requested Generation Identity. After successful publication, the pending workspace is removed and the Markdown report is retained as a resolved audit record. Incremental regeneration of only affected Concepts is not currently supported.
Run deterministic validation without calling a model or changing files:
uv run knowledge-forge validate --out ./knowledge
The default command performs Portable OKF Validation and prints PASS (portable OKF 0.2) on success. It accepts any conformant v0.2 Bundle without requiring .knowledge-forge/ private state, a root index.md, an okf_version marker, a concepts/ namespace, or Knowledge Forge's controlled Concept types. Every non-reserved Markdown file must have parseable YAML frontmatter with a non-empty string type; index.md and log.md are reserved at every directory level. When optional v0.2 provenance, trust, freshness, lifecycle, citation, or attestation fields are present, their standard structure is validated. Producer extension fields remain allowed. Validation is read-only, and a failure reports actionable errors with exit code 2.
Portable validation ignores private Knowledge Forge state. If .knowledge-forge/ is present, it prints managed state detected — run with --managed for full validation on stderr while preserving the portable result. Run the explicit managed profile to validate the portable Bundle first, then state integrity, tool-managed files, provenance, hashes, baselines, ownership, and filesystem consistency:
uv run knowledge-forge validate --managed --out ./knowledge
To also verify the complete authoritative Knowledge Source set, provide --source. Managed validation remains read-only and prints PASS (managed Knowledge Forge Bundle)