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
8 changes: 7 additions & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
name: Docs

# Builds the static documentation site (site/) plus generated Javadoc and deploys
# it to GitHub Pages. Served at the default project Pages URL: https://chaturai.github.io/cortex/.
# it to GitHub Pages. Served at https://cortex.chatur.ai/ (the default project Pages URL,
# https://chaturai.github.io/cortex/, redirects there).
#
# The custom domain is configured in the repository's Pages settings, not in this repo: publishing
# from a custom Actions workflow means GitHub creates no CNAME file and ignores one if present, so
# there is deliberately no site/CNAME to keep in sync. Every link in site/ is relative, which is why
# the domain change needed no edits there.
on:
push:
branches: [ "main" ]
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0

# `build` enforces coverage on its own: `check` depends on jacocoTestCoverageVerification, which
# fails below LINE 85%. The aggregated report is still generated even though nothing uploads it
# now that Codecov is gone -- it is the only view of cortex-api's (transitive) coverage, and the
# aggregation has been silently broken before, so running it here keeps that a build failure
# rather than a surprise the next time someone asks for the report locally.
- name: Build with Gradle Wrapper
run: ./gradlew build :cortex-spring-boot-autoconfigure:testCodeCoverageReport

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: cortex-spring-boot-autoconfigure/build/reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml

# NOTE: The Gradle Wrapper is the default and recommended way to run Gradle (https://docs.gradle.org/current/userguide/gradle_wrapper.html).
# If your project does not have the Gradle Wrapper configured, you can use the following configuration to run Gradle with a specified version.
#
Expand Down
6 changes: 6 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ has nothing to restore; every other failure propagates and fails the boot.
- **AssertJ only — zero bare `assert`.** The suite had 136 of them; bare `assert` silently no-ops
without `-ea`, so any runner without it passed everything. Don't reintroduce one.
- `*IntegrationTests` boots Spring; `*Tests` is isolated. The names are honest now — keep them so.
- **The docs site's SKOS tutorial is pinned by a test.** `site/skos-tutorial.html` prints shapes,
rules, and sample Turtle; `SkosTutorialTests` (starter-example) runs exactly those through
`CortexBuilder`, loading the shapes and rules from `src/test/resources/skos-tutorial.{shapes,rules}`
— the same text the page shows. Same reasoning as `ExampleShapesTests`: instructions nothing
executes are how `cortex-schema-plugin` drifted. If you edit the tutorial's Turtle, edit those two
resources too, and vice versa.

## Known open items

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Cortex

[![CI](https://github.com/chaturai/cortex/actions/workflows/gradle.yml/badge.svg?branch=main)](https://github.com/chaturai/cortex/actions/workflows/gradle.yml)
[![Coverage](https://codecov.io/gh/chaturai/cortex/branch/main/graph/badge.svg)](https://codecov.io/gh/chaturai/cortex)
[![Release](https://img.shields.io/github/v/tag/chaturai/cortex?label=release&sort=semver)](https://github.com/chaturai/cortex/tags)
[![Maven Central](https://img.shields.io/maven-central/v/ai.chatur/cortex-spring-boot-starter?label=Maven%20Central)](https://central.sonatype.com/artifact/ai.chatur/cortex-spring-boot-starter)

An ontology-backed knowledge graph memory store with branch-based ingestion, provenance, inference, and full-text search — exposed to AI agents over MCP.

📖 **[Documentation](https://cortex.chatur.ai/)** — [getting started](https://cortex.chatur.ai/getting-started.html), a [SKOS tutorial](https://cortex.chatur.ai/skos-tutorial.html), the [architecture](https://cortex.chatur.ai/architecture.html), the [MCP server guide](https://cortex.chatur.ai/mcp.html), the [configuration reference](https://cortex.chatur.ai/configuration.html), and [Javadoc](https://cortex.chatur.ai/api/).

## What's in the box?

Adding the starter auto-configures a complete knowledge graph memory for your Spring Boot application:
Expand All @@ -16,7 +17,7 @@ Adding the starter auto-configures a complete knowledge graph memory for your Sp
- **Branch-based ingestion** — incoming assertions are linted, SHACL-validated **together with the already approved assertions** (so new statements may rely on approved ones to conform), trimmed of triples the graph already contains, and staged on a branch as an RDF patch. Nothing enters the graph until a human approves it; if every incoming triple is already approved, nothing is staged.
- **PROV-O provenance** — every ingestion is recorded as a `prov:Activity` (started when the branch is staged, ended when it is approved), and every approved statement is reified and linked to its activity with `prov:wasGeneratedBy`.
- **Rule-based inference** — derived statements are recomputed in full on startup, and extended incrementally with each approval's newly-added statements (not recomputed from scratch), and included in all query results.
- **Full-text search** — a Lucene index over `rdfs:label` and `rdfs:comment` (`rdf:type` is mapped as well, but Jena's text index only indexes literal values, so type IRIs are skipped).
- **Full-text search** — a Lucene index over `rdfs:label` and `rdfs:comment`, held as separate fields so a name outranks a description. `rdf:type` is deliberately not indexed: class URIs would mix their tokens into the same relevance space as human-readable prose, giving every typed resource the same filler terms.
- **An MCP server** — tools and resources that let AI agents read and write the graph (see below).
- **A web UI** — pages to explore the graph, search it from the navbar, and review, edit, and approve pending branches (see below).
- **Export and import** — the approved assertions can be downloaded as a Turtle file from `/export`, and a Turtle document can be uploaded at `/import`, which stages it on a branch for review like any other ingest rather than replacing the graph.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
package ai.chatur.cortex.spring.example;

import static org.assertj.core.api.Assertions.assertThat;

import ai.chatur.cortex.Cortex;
import ai.chatur.cortex.CortexBuilder;
import ai.chatur.cortex.IngestResult;
import ai.chatur.cortex.LintResult;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

/**
* Pins the SKOS tutorial published at {@code site/skos-tutorial.html}, which builds a controlled
* vocabulary on the SKOS Core standard rather than a hand-written ontology.
*
* <p>The ontology is this module's real {@code skos.ttl}; the shapes and rules are {@code
* skos-tutorial.shapes} and {@code skos-tutorial.rules} in this module's test resources, and are
* the same files the tutorial prints verbatim. That is the point of the class: {@code
* cortex-schema-plugin} has no CI and its instructions drifted out of step with the code once
* already, and the site is the same kind of hazard — instructions nothing executes. Change the
* tutorial's shapes, rules, or sample Turtle and this fails.
*
* <p>Each test builds its own fully in-memory graph, so the ingest-and-approve cases cannot see
* each other's approvals.
*/
class SkosTutorialTests {

/** The sample vocabulary the tutorial asks an agent to ingest. */
private static final String TTL =
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix : <https://example.org/vocab/> .

:topics a skos:ConceptScheme ;
skos:prefLabel "Documentation topics" .

:knowledge-graph a skos:Concept ;
skos:prefLabel "Knowledge graph" ;
skos:definition "A graph-structured store of facts about entities and their relationships." ;
skos:topConceptOf :topics .

:ontology a skos:Concept ;
skos:prefLabel "Ontology" ;
skos:definition "A formal vocabulary of classes and properties for a domain." ;
skos:broader :knowledge-graph .

:shacl a skos:Concept ;
skos:prefLabel "SHACL" ;
skos:altLabel "Shapes Constraint Language" ;
skos:definition "A language for validating the structure of RDF data." ;
skos:broader :ontology ;
skos:related :ontology .
""";

private Cortex cortex;

@BeforeEach
void setUp() throws IOException {
cortex =
CortexBuilder.create()
.ontologies(List.of(resource("skos.ttl")))
.shapes(List.of(resource("skos-tutorial.shapes")))
.rules(List.of(resource("skos-tutorial.rules")))
.build();
}

private static String resource(String name) throws IOException {
return new ClassPathResource(name).getContentAsString(StandardCharsets.UTF_8);
}

@Test
void shouldStageTheTutorialVocabulary() {
IngestResult result = cortex.ingest(TTL);

assertThat(result.errors()).isNull();
assertThat(result.valid())
.as("the sample vocabulary the tutorial tells the reader to ingest must actually ingest")
.isTrue();
assertThat(result.branch()).isNotNull();
}

@Test
void shouldInferTransitiveAncestryAndTheInverseOfBroader() {
// The tutorial's headline claim: skos:broader is a sub-property of skos:broaderTransitive in
// SKOS itself, so the broaderTransitive rule only has to chain -- :shacl reaches
// :knowledge-graph through :ontology without either edge being asserted.
cortex.approve(cortex.ingest(TTL).branch());

String ancestors =
cortex.query(
"""
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?ancestor WHERE {
<https://example.org/vocab/shacl> skos:broaderTransitive ?ancestor .
}
""");
assertThat(ancestors).contains("vocab/ontology").contains("vocab/knowledge-graph");

String narrower =
cortex.query(
"""
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?narrower WHERE {
<https://example.org/vocab/knowledge-graph> skos:narrower ?narrower .
}
""");
assertThat(narrower).as("the narrower rule inverts skos:broader").contains("vocab/ontology");
}

@Test
void shouldInferSchemeMembershipFromASingleTopConceptStatement() {
// skos:topConceptOf is a sub-property of skos:inScheme, so one asserted statement yields both
// the inverse (hasTopConcept, by rule) and membership (inScheme, by the generic sub-property
// rule).
cortex.approve(cortex.ingest(TTL).branch());

String result =
cortex.query(
"""
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?c ?s WHERE {
<https://example.org/vocab/topics> skos:hasTopConcept ?c .
?c skos:inScheme ?s .
}
""");

assertThat(result).contains("vocab/knowledge-graph").contains("vocab/topics");
}

@Test
void shouldSearchConceptsByTheirInferredPrefLabel() {
// Cortex indexes rdfs:label, and skos:prefLabel is a sub-property of it -- which is the whole
// reason a SKOS graph is searchable at all without mapping labels by hand.
cortex.approve(cortex.ingest(TTL).branch());

assertThat(cortex.search("knowledge")).contains("knowledge-graph");
}

@Test
void shouldSearchConceptsByTheirInferredAltLabel() {
cortex.approve(cortex.ingest(TTL).branch());

assertThat(cortex.search("Constraint"))
.as("skos:altLabel is a sub-property of rdfs:label too, so synonyms are searchable")
.contains("shacl");
}

@Test
void shouldSearchConceptsByADefinitionTurnedIntoAComment() {
// skos:definition is a sub-property of skos:note, NOT of rdfs:comment, so without the
// tutorial's definitionComment rule none of these definitions would be indexed or rendered.
cortex.approve(cortex.ingest(TTL).branch());

assertThat(cortex.search("validating")).contains("shacl");
}

@Test
void shouldRejectATermOutsideTheVocabularyNamespace() {
IngestResult result =
cortex.ingest(
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<https://elsewhere.example/thing> a skos:Concept ;
skos:prefLabel "Elsewhere" ;
skos:definition "Not in the vocabulary namespace." .
""");

assertThat(result.valid()).isFalse();
assertThat(result.branch()).isNull();
assertThat(result.errors()).contains("https://example.org/vocab/{name}");
}

@Test
void shouldRejectAConceptWithNoDefinition() {
IngestResult result =
cortex.ingest(
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<https://example.org/vocab/undefined> a skos:Concept ;
skos:prefLabel "Undefined" .
""");

assertThat(result.valid()).isFalse();
assertThat(result.errors()).contains("every concept needs a skos:definition");
}

@Test
void shouldRejectASecondPrefLabel() {
// SKOS's own integrity condition, and the constraint that stops an agent quietly introducing a
// second name for a concept that already has one.
IngestResult result =
cortex.ingest(
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<https://example.org/vocab/twice> a skos:Concept ;
skos:prefLabel "Once" , "Twice" ;
skos:definition "A concept named two ways." .
""");

assertThat(result.valid()).isFalse();
assertThat(result.errors()).contains("every term needs exactly one skos:prefLabel");
}

@Test
void shouldRejectAClassTheShapesDoNotAdmit() {
// skos:Collection is declared by SKOS, so linting passes it -- only the shapes' sh:in list
// keeps it out. The tutorial's rejection table claims exactly this split.
IngestResult result =
cortex.ingest(
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<https://example.org/vocab/bundle> a skos:Collection ;
skos:prefLabel "A bundle" .
""");

assertThat(result.valid()).isFalse();
assertThat(result.errors())
.contains("only skos:Concept and skos:ConceptScheme may be asserted");
}

@Test
void shouldRejectAPredicateSkosDoesNotDeclare() {
// Linting, not SHACL: the closed-world vocabulary check fires before anything is staged.
LintResult result =
cortex.lint(
"""
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix ex: <https://example.org/made-up#> .

<https://example.org/vocab/thing> a skos:Concept ;
ex:invented "nope" .
""");

assertThat(result.valid()).isFalse();
assertThat(result.errors()).contains("invented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

[subClass: (?x rdf:type ?c1) (?c1 rdfs:subClassOf ?c2) -> (?x rdf:type ?c2)]
[subProperty: (?x ?p1 ?y) (?p1 rdfs:subPropertyOf ?p2) -> (?x ?p2 ?y)]

[narrower: (?a skos:broader ?b) -> (?b skos:narrower ?a)]

[broaderTransitive:
(?a skos:broader ?b) (?b skos:broaderTransitive ?c) -> (?a skos:broaderTransitive ?c)]

[hasTopConcept: (?c skos:topConceptOf ?s) -> (?s skos:hasTopConcept ?c)]

[definitionComment: (?c skos:definition ?d) -> (?c rdfs:comment ?d)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

@prefix s: <https://example.org/shapes#> .

s:VocabularyShape a sh:NodeShape ;
sh:targetSubjectsOf rdf:type ;
sh:nodeKind sh:IRI ;
sh:pattern "^https://example.org/vocab/[^/?#]+$" ;
sh:message "vocabulary terms must be https://example.org/vocab/{name} IRIs" ;
sh:property [
sh:path rdf:type ;
sh:minCount 1 ;
sh:in ( skos:Concept skos:ConceptScheme ) ;
sh:message "only skos:Concept and skos:ConceptScheme may be asserted" ;
] ;
sh:property [
sh:path skos:prefLabel ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:nodeKind sh:Literal ;
sh:message "every term needs exactly one skos:prefLabel" ;
] .

s:ConceptShape a sh:NodeShape ;
sh:targetClass skos:Concept ;
sh:property [
sh:path skos:definition ;
sh:minCount 1 ;
sh:nodeKind sh:Literal ;
sh:message "every concept needs a skos:definition" ;
] ;
sh:property [
sh:path skos:broader ;
sh:nodeKind sh:IRI ;
sh:message "skos:broader must point at another concept" ;
] .
Loading
Loading