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
126 changes: 105 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,105 @@
MIT License

Copyright (c) 2026 BYK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Functional Source License, Version 1.1, Apache 2.0 Future License

## Abbreviation

FSL-1.1-Apache-2.0

## Notice

Copyright 2026 Burak Yigit Kaya

## Terms and Conditions

### Licensor ("We")

The party offering the Software under these Terms and Conditions.

### The Software

The "Software" is each version of the software that we make available under
these Terms and Conditions, as indicated by our inclusion of these Terms and
Conditions with the Software.

### License Grant

Subject to your compliance with this License Grant and the Patents,
Redistribution and Trademark clauses below, we hereby grant you the right to
use, copy, modify, create derivative works, publicly perform, publicly display
and redistribute the Software for any Permitted Purpose identified below.

### Permitted Purpose

A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
means making the Software available to others in a commercial product or
service that:

1. substitutes for the Software;

2. substitutes for any other product or service we offer using the Software
that exists as of the date we make the Software available; or

3. offers the same or substantially similar functionality as the Software.

Permitted Purposes specifically include using the Software:

1. for your internal use and access;

2. for non-commercial education;

3. for non-commercial research; and

4. in connection with professional services that you provide to a licensee
using the Software in accordance with these Terms and Conditions.

### Patents

To the extent your use for a Permitted Purpose would necessarily infringe our
patents, the license grant above includes a license under our patents. If you
make a claim against any party that the Software infringes or contributes to
the infringement of any patent, then your patent license to the Software ends
immediately.

### Redistribution

The Terms and Conditions apply to all copies, modifications and derivatives of
the Software.

If you redistribute any copies, modifications or derivatives of the Software,
you must include a copy of or a link to these Terms and Conditions and not
remove any copyright notices provided in or with the Software.

### Disclaimer

THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.

IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.

### Trademarks

Except for displaying the License Details and identifying us as the origin of
the Software, you have no right under these Terms and Conditions to use our
trademarks, trade names, service marks or product names.

## Grant of Future License

We hereby irrevocably grant you an additional license to use the Software under
the Apache License, Version 2.0 that is effective on the second anniversary of
the date we make the Software available. On or after that date, you may use the
Software under the Apache License, Version 2.0, in which case the following
will apply:

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lore-monorepo",
"private": true,
"type": "module",
"license": "MIT",
"license": "FSL-1.1-Apache-2.0",
"description": "Monorepo root for Lore — three-tier memory architecture",
"main": "./packages/opencode/src/index.ts",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@loreai/core",
"version": "0.12.0",
"type": "module",
"license": "MIT",
"license": "FSL-1.1-Apache-2.0",
"description": "Shared memory engine for Lore — three-tier storage, distillation, gradient context management",
"main": "./dist/node/index.js",
"types": "./dist/node/index.d.ts",
Expand Down
139 changes: 111 additions & 28 deletions packages/core/src/agents-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { dirname } from "path";
import { dirname, join } from "path";
import * as ltm from "./ltm";
import { serialize, inline, h, ul, liph, strong, t, root, unescapeMarkdown } from "./markdown";

Expand All @@ -34,6 +34,16 @@ const ALL_START_MARKERS = [
"<!-- This section is auto-maintained by lore (https://github.com/BYK/opencode-lore) -->",
] as const;

/**
* Filename for the dedicated lore knowledge file. Always at the project root.
* Unlike the agents file (AGENTS.md / CLAUDE.md), this file is entirely owned
* by lore — no section markers needed, no non-lore content to preserve.
*/
export const LORE_FILE = ".lore.md";

const LORE_FILE_HEADER =
"<!-- Managed by lore (https://github.com/BYK/loreai) — manual edits are imported on next session. -->";

/** Regex matching a valid UUID (v4 or v7) — 8-4-4-4-12 hex groups. */
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;

Expand Down Expand Up @@ -270,16 +280,25 @@ function buildSection(projectPath: string): string {
// ---------------------------------------------------------------------------

/**
* Write current knowledge entries into the AGENTS.md file, preserving all
* non-lore content. Creates the file if it doesn't exist.
* Write a pointer to `.lore.md` inside the agents file (AGENTS.md / CLAUDE.md),
* preserving all non-lore content. Also writes `.lore.md` with the actual
* knowledge entries as a side effect.
*/
export function exportToFile(input: {
projectPath: string;
filePath: string;
}): void {
const sectionBody = buildSection(input.projectPath);
// Write the actual entries to .lore.md first.
exportLoreFile(input.projectPath);

// Build a pointer section for the agents file instead of full entries.
const pointerBody =
"\n## Long-term Knowledge\n\n" +
"For long-term knowledge entries managed by [lore](https://github.com/BYK/loreai) " +
"(gotchas, patterns, decisions, architecture), see [`.lore.md`](.lore.md) " +
"in the project root.\n";
const newSection =
LORE_SECTION_START + sectionBody + LORE_SECTION_END + "\n";
LORE_SECTION_START + pointerBody + LORE_SECTION_END + "\n";

let fileContent = "";
if (existsSync(input.filePath)) {
Expand Down Expand Up @@ -329,38 +348,25 @@ export function shouldImport(input: {
}

// ---------------------------------------------------------------------------
// Import
// Import helpers
// ---------------------------------------------------------------------------

/**
* Import knowledge entries from the agents file into the local DB.
* Upsert parsed entries into the local DB.
*
* Behaviour per entry:
* - Known UUID (already in DB) → update content if it changed (manual edit)
* - Unknown UUID (other machine)→ create with that exact ID
* - No UUID (hand-written) → create with a new UUIDv7
* - Duplicate UUID in same file → first occurrence wins, rest ignored
*/
export function importFromFile(input: {
projectPath: string;
filePath: string;
}): void {
if (!existsSync(input.filePath)) return;

const fileContent = readFileSync(input.filePath, "utf8");
const { section, before } = splitFile(fileContent);

// Determine what to parse:
// - If lore markers exist: parse ONLY the lore section body (avoid re-importing our own output)
// - If no markers: parse the full file (first-time hand-written AGENTS.md import)
const textToParse = section ?? fileContent;

const fileEntries = parseEntriesFromSection(textToParse);
if (!fileEntries.length) return;

function _importEntries(
entries: ParsedFileEntry[],
projectPath: string,
): void {
const seenIds = new Set<string>();

for (const entry of fileEntries) {
for (const entry of entries) {
if (entry.id !== null) {
// Deduplicate: if same UUID appears twice in file, first wins
if (seenIds.has(entry.id)) continue;
Expand All @@ -375,7 +381,7 @@ export function importFromFile(input: {
} else {
// Unknown UUID — entry came from another machine, preserve its ID
ltm.create({
projectPath: input.projectPath,
projectPath,
category: entry.category,
title: entry.title,
content: entry.content,
Expand All @@ -387,13 +393,13 @@ export function importFromFile(input: {
} else {
// Hand-written entry — create with a new UUIDv7
// Check for a near-duplicate by title to avoid double-import on re-runs
const existing = ltm.forProject(input.projectPath, true);
const existing = ltm.forProject(projectPath, true);
const titleMatch = existing.find(
(e) => e.title.toLowerCase() === entry.title.toLowerCase(),
);
if (!titleMatch) {
ltm.create({
projectPath: input.projectPath,
projectPath,
category: entry.category,
title: entry.title,
content: entry.content,
Expand All @@ -404,3 +410,80 @@ export function importFromFile(input: {
}
}
}

// ---------------------------------------------------------------------------
// Import from agents file (AGENTS.md / CLAUDE.md)
// ---------------------------------------------------------------------------

/**
* Import knowledge entries from the agents file into the local DB.
* Used for backward compatibility when `.lore.md` doesn't exist yet.
*/
export function importFromFile(input: {
projectPath: string;
filePath: string;
}): void {
if (!existsSync(input.filePath)) return;

const fileContent = readFileSync(input.filePath, "utf8");
const { section } = splitFile(fileContent);

// Determine what to parse:
// - If lore markers exist: parse ONLY the lore section body (avoid re-importing our own output)
// - If no markers: parse the full file (first-time hand-written AGENTS.md import)
const textToParse = section ?? fileContent;

const fileEntries = parseEntriesFromSection(textToParse);
if (!fileEntries.length) return;

_importEntries(fileEntries, input.projectPath);
}

// ---------------------------------------------------------------------------
// .lore.md — dedicated knowledge file
// ---------------------------------------------------------------------------

/**
* Returns true if a `.lore.md` file exists in the project root.
*/
export function loreFileExists(projectPath: string): boolean {
return existsSync(join(projectPath, LORE_FILE));
}

/**
* Export current knowledge entries to `.lore.md` in the project root.
* The entire file is lore-owned — no section markers, no content to preserve.
*/
export function exportLoreFile(projectPath: string): void {
const sectionBody = buildSection(projectPath);
const content = LORE_FILE_HEADER + "\n" + sectionBody;
writeFileSync(join(projectPath, LORE_FILE), content, "utf8");
}

/**
* Returns true if `.lore.md` needs to be imported:
* - File exists and its content differs from what lore would currently produce
*/
export function shouldImportLoreFile(projectPath: string): boolean {
const fp = join(projectPath, LORE_FILE);
if (!existsSync(fp)) return false;

const fileContent = readFileSync(fp, "utf8");
const expected = LORE_FILE_HEADER + "\n" + buildSection(projectPath);
return hashSection(fileContent) !== hashSection(expected);
}

/**
* Import knowledge entries from `.lore.md` into the local DB.
* Parses the full file content (no section markers to split on).
*/
export function importLoreFile(projectPath: string): void {
const fp = join(projectPath, LORE_FILE);
if (!existsSync(fp)) return;

const fileContent = readFileSync(fp, "utf8");
const fileEntries = parseEntriesFromSection(fileContent);
if (!fileEntries.length) return;

_importEntries(fileEntries, projectPath);
}
Loading
Loading