FN-5663: add goal-citation audit trail to agent reasoning
Record goal citation evidence so agent reasoning can be traced end-to-end. - add core goal citation types, extraction helper, persistence schema, and store APIs for citation create/list workflows - add CLI support and tests for goal citation flows, including command wiring and regression coverage - update docs and add a published changeset for @runfusion/fusion describing the new audit-trail capability Files changed: .changeset/fn-5663-goal-citation-audit-trail.md | 10 + docs/agents.md | 15 ++ docs/cli-reference.md | 6 +- packages/cli/src/__tests__/bin.test.ts | 2 + .../cli/src/__tests__/goals-citations-cli.test.ts | 82 ++++++++ packages/cli/src/bin.ts | 19 +- packages/cli/src/commands/goals.ts | 43 +++++ packages/core/src/__tests__/db-migrate.test.ts | 10 +- packages/core/src/__tests__/db.test.ts | 34 ++-- .../src/__tests__/goal-citation-extractor.test.ts | 56 ++++++ .../src/__tests__/goal-citations-store.test.ts | 175 +++++++++++++++++ packages/core/src/__tests__/goals-schema.test.ts | 2 +- packages/core/src/__tests__/insight-store.test.ts | 10 +- packages/core/src/__tests__/mission-store.test.ts | 2 +- packages/core/src/__tests__/run-audit.test.ts | 2 +- packages/core/src/__tests__/secrets-schema.test.ts | 6 +- .../core/src/__tests__/store-merge-queue.test.ts | 2 +- packages/core/src/__tests__/task-documents.test.ts | 2 +- packages/core/src/db.ts | 51 ++++- packages/core/src/goal-citation-extractor.ts | 56 ++++++ packages/core/src/index.ts | 13 ++ packages/core/src/store.ts | 210 ++++++++++++++++++++- packages/core/src/types.ts | 51 ++++- .../src/store/__tests__/roadmap-store.test.ts | 4 +- 24 files changed, 817 insertions(+), 46 deletions(-) Fusion-Task-Id: FN-5663 Fusion-Task-Lineage: 720f2c4b-4363-464a-a76f-472ec6aca136
This commit is contained in:
10
.changeset/fn-5663-goal-citation-audit-trail.md
Normal file
10
.changeset/fn-5663-goal-citation-audit-trail.md
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
"@runfusion/fusion": minor
|
||||
---
|
||||
|
||||
Add a goal-citation audit trail to support Slice 2 anchoring success-signal measurement.
|
||||
|
||||
- Introduce a persisted `goal_citations` table (schema v93) with deduplication on `(goalId, surface, sourceRef)`.
|
||||
- Record citations from `agent_log` and `task_document` write seams.
|
||||
- Extract goal IDs using `GOAL_ID_PATTERN` (`/\bG-[0-9A-Z]+(?:-[0-9A-Z]+)*\b/g`) and store bounded snippets (max 200 chars).
|
||||
- Add `fn goals citations` with filters: `--goal`, `--agent`, `--surface`, `--since`, `--until`, `--limit`, and `--json`.
|
||||
@@ -1504,6 +1504,21 @@ Universal baseline: available by default across executor, step-session, reviewer
|
||||
- Read-only (no JS rendering, no auth flows, no POST/cookie workflows)
|
||||
- Use the `agent-browser` skill when JS rendering or interactive navigation is required
|
||||
|
||||
## Goal-citation audit trail (Slice 2 success signal)
|
||||
|
||||
Agents now emit a durable goal-citation signal whenever reasoning text includes a goal ID.
|
||||
|
||||
- Scanned surfaces: `agent_log` and `task_document`.
|
||||
- Regex contract: `GOAL_ID_PATTERN = /\bG-[0-9A-Z]+(?:-[0-9A-Z]+)*\b/g` (uppercase `G-...` only).
|
||||
- Snippets are bounded windows (`GOAL_CITATION_SNIPPET_MAX = 200`) around the first match per goal ID, with whitespace collapsed.
|
||||
- Query via CLI:
|
||||
- `fn goals citations --since <iso> --until <iso>`
|
||||
- `fn goals citations --goal G-XXXX --since <iso>`
|
||||
- Example row:
|
||||
- `2026-05-29T08:10:00.000Z G-1ABC-2-XYZ9 agent-ops agent_log agentLog:4821`
|
||||
- ` ...anchoring this plan to G-1ABC-2-XYZ9 before execution...`
|
||||
- Programmatic consumers can query the same signal through `TaskStore.listGoalCitations(...)`.
|
||||
|
||||
## Agent coordination tools summary
|
||||
|
||||
Seven coordination tools support spawning, provisioning, discovery, delegation, and direct-report config.
|
||||
|
||||
@@ -626,19 +626,21 @@ Subcommands: `create`, `list|ls`, `show|info`, `delete`, `activate-slice`.
|
||||
|
||||
## `fn goals`
|
||||
|
||||
Goal management operations (Slice 1 author-facing surface only; no agent anchoring yet).
|
||||
Goal management operations, including Slice 2 citation-audit queries.
|
||||
|
||||
```bash
|
||||
fn goals list [--status active|archived|all]
|
||||
fn goals create "Improve reliability" "Reduce flaky tests and retries"
|
||||
fn goals archive G-001
|
||||
fn goals citations [--goal <id>] [--agent <id>] [--surface <agent_log|task_document>] [--since <iso>] [--until <iso>] [--limit <n>] [--json]
|
||||
```
|
||||
|
||||
Subcommands: `list|ls`, `create`, `archive`.
|
||||
Subcommands: `list|ls`, `create`, `archive`, `citations`.
|
||||
|
||||
Notes:
|
||||
- `fn goals list` defaults to `active` goals.
|
||||
- Active goals have a hard cap of 5. Create operations fail cleanly once the cap is reached.
|
||||
- `fn goals citations` lists recorded goal-ID citations across `agent_log` and `task_document` and supports machine-readable output with `--json`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ const commandMocks = vi.hoisted(() => ({
|
||||
runGoalsList: vi.fn(),
|
||||
runGoalsCreate: vi.fn(),
|
||||
runGoalsArchive: vi.fn(),
|
||||
runGoalsCitations: vi.fn(),
|
||||
|
||||
runProjectList: vi.fn(),
|
||||
runProjectAdd: vi.fn(),
|
||||
@@ -177,6 +178,7 @@ vi.mock("../commands/goals.js", () => ({
|
||||
runGoalsList: commandMocks.runGoalsList,
|
||||
runGoalsCreate: commandMocks.runGoalsCreate,
|
||||
runGoalsArchive: commandMocks.runGoalsArchive,
|
||||
runGoalsCitations: commandMocks.runGoalsCitations,
|
||||
}));
|
||||
|
||||
vi.mock("../commands/project.js", () => ({
|
||||
|
||||
82
packages/cli/src/__tests__/goals-citations-cli.test.ts
Normal file
82
packages/cli/src/__tests__/goals-citations-cli.test.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("../project-resolver.js", () => ({
|
||||
getStore: vi.fn(),
|
||||
}));
|
||||
|
||||
const { getStore } = await import("../project-resolver.js");
|
||||
const { runGoalsCitations } = await import("../commands/goals.js");
|
||||
|
||||
describe("goals citations cli", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("filters by goal and since/until window", async () => {
|
||||
const listGoalCitations = vi.fn().mockReturnValue([
|
||||
{
|
||||
id: 2,
|
||||
goalId: "G-ONE",
|
||||
agentId: "executor",
|
||||
surface: "agent_log",
|
||||
sourceRef: "agentLog:2",
|
||||
snippet: "G-ONE cited",
|
||||
timestamp: "2026-05-01T00:00:00.000Z",
|
||||
},
|
||||
]);
|
||||
vi.mocked(getStore).mockResolvedValue({ listGoalCitations } as any);
|
||||
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined);
|
||||
|
||||
await runGoalsCitations(undefined, {
|
||||
goalId: "G-ONE",
|
||||
since: "2026-05-01T00:00:00.000Z",
|
||||
until: "2026-05-31T23:59:59.000Z",
|
||||
});
|
||||
|
||||
expect(listGoalCitations).toHaveBeenCalledWith({
|
||||
goalId: "G-ONE",
|
||||
agentId: undefined,
|
||||
surface: undefined,
|
||||
startTime: "2026-05-01T00:00:00.000Z",
|
||||
endTime: "2026-05-31T23:59:59.000Z",
|
||||
limit: 50,
|
||||
});
|
||||
expect(logSpy).toHaveBeenCalledWith(
|
||||
"2026-05-01T00:00:00.000Z G-ONE executor agent_log agentLog:2",
|
||||
);
|
||||
});
|
||||
|
||||
it("prints valid json with --json", async () => {
|
||||
const rows = [
|
||||
{
|
||||
id: 1,
|
||||
goalId: "G-JSON",
|
||||
agentId: "agent-1",
|
||||
surface: "task_document",
|
||||
sourceRef: "document:FN-1:plan:rev1",
|
||||
snippet: "G-JSON",
|
||||
timestamp: "2026-01-01T00:00:00.000Z",
|
||||
},
|
||||
];
|
||||
vi.mocked(getStore).mockResolvedValue({ listGoalCitations: vi.fn().mockReturnValue(rows) } as any);
|
||||
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined);
|
||||
|
||||
await runGoalsCitations(undefined, { json: true });
|
||||
|
||||
const output = logSpy.mock.calls[0]?.[0];
|
||||
expect(() => JSON.parse(String(output))).not.toThrow();
|
||||
expect(JSON.parse(String(output))).toEqual(rows);
|
||||
});
|
||||
|
||||
it("prints empty-state message when no matches", async () => {
|
||||
vi.mocked(getStore).mockResolvedValue({ listGoalCitations: vi.fn().mockReturnValue([]) } as any);
|
||||
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined);
|
||||
|
||||
await runGoalsCitations(undefined, {});
|
||||
expect(logSpy).toHaveBeenCalledWith("No goal citations match the filter.");
|
||||
});
|
||||
});
|
||||
@@ -127,7 +127,7 @@ async function loadCommandHandlers() {
|
||||
const { runBackupCreate, runBackupList, runBackupRestore, runBackupCleanup } = await import("./commands/backup.js");
|
||||
const { runMemoryBackupCreate, runMemoryBackupList, runMemoryBackupRestore } = await import("./commands/memory-backup.js");
|
||||
const { runMissionCreate, runMissionList, runMissionShow, runMissionDelete, runMissionActivateSlice } = await import("./commands/mission.js");
|
||||
const { runGoalsList, runGoalsCreate, runGoalsArchive } = await import("./commands/goals.js");
|
||||
const { runGoalsList, runGoalsCreate, runGoalsArchive, runGoalsCitations } = await import("./commands/goals.js");
|
||||
const { runProjectList, runProjectAdd, runProjectRemove, runProjectShow, runProjectInfo, runProjectSetDefault, runProjectDetect } = await import("./commands/project.js");
|
||||
const { runNodeList, runNodeConnect, runNodeDisconnect, runNodeShow, runNodeHealth, runMeshStatus } = await import("./commands/node.js");
|
||||
const { runInit } = await import("./commands/init.js");
|
||||
@@ -196,6 +196,7 @@ async function loadCommandHandlers() {
|
||||
runGoalsList,
|
||||
runGoalsCreate,
|
||||
runGoalsArchive,
|
||||
runGoalsCitations,
|
||||
runProjectList,
|
||||
runProjectAdd,
|
||||
runProjectRemove,
|
||||
@@ -316,6 +317,7 @@ PR:
|
||||
fn goals list [--status STATE] List goals (default: active)
|
||||
fn goals create [title] [desc] Create a new goal
|
||||
fn goals archive <id> Archive a goal
|
||||
fn goals citations [flags] List recorded goal-ID citations across agent logs and task documents (Slice 2 success signal)
|
||||
fn project list | ls [--json] List all registered projects
|
||||
fn project add [name] [path] [opts] Register a new project
|
||||
fn project remove | rm <name> [--force]
|
||||
@@ -603,6 +605,7 @@ async function main() {
|
||||
runGoalsList,
|
||||
runGoalsCreate,
|
||||
runGoalsArchive,
|
||||
runGoalsCitations,
|
||||
runProjectList,
|
||||
runProjectAdd,
|
||||
runProjectRemove,
|
||||
@@ -1367,9 +1370,21 @@ async function main() {
|
||||
await runGoalsArchive(id, projectName);
|
||||
break;
|
||||
}
|
||||
case "citations": {
|
||||
const goalId = getFlagValue(args, "--goal");
|
||||
const agentId = getFlagValue(args, "--agent");
|
||||
const surface = getFlagValue(args, "--surface") as "agent_log" | "task_document" | undefined;
|
||||
const since = getFlagValue(args, "--since");
|
||||
const until = getFlagValue(args, "--until");
|
||||
const limitValue = getFlagValue(args, "--limit");
|
||||
const limit = limitValue ? Number(limitValue) : undefined;
|
||||
const json = args.includes("--json");
|
||||
await runGoalsCitations(projectName, { goalId, agentId, surface, since, until, limit, json });
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.error(`Unknown subcommand: goals ${subcommand || ""}`);
|
||||
console.log("Try: fn goals list | create | archive");
|
||||
console.log("Try: fn goals list | create | archive | citations");
|
||||
process.exit(1);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createInterface } from "node:readline/promises";
|
||||
import type { GoalCitationSurface } from "@fusion/core";
|
||||
import { getStore } from "../project-resolver.js";
|
||||
|
||||
type GoalStatusFilter = "active" | "archived" | "all";
|
||||
@@ -7,6 +8,16 @@ interface RunGoalsListOptions {
|
||||
status?: GoalStatusFilter;
|
||||
}
|
||||
|
||||
interface RunGoalsCitationsOptions {
|
||||
goalId?: string;
|
||||
agentId?: string;
|
||||
surface?: GoalCitationSurface;
|
||||
since?: string;
|
||||
until?: string;
|
||||
limit?: number;
|
||||
json?: boolean;
|
||||
}
|
||||
|
||||
const ACTIVE_SOFT_WARNING_THRESHOLD = 3;
|
||||
const ACTIVE_HARD_LIMIT = 5;
|
||||
|
||||
@@ -115,6 +126,38 @@ export async function runGoalsCreate(
|
||||
}
|
||||
}
|
||||
|
||||
export async function runGoalsCitations(
|
||||
projectName: string | undefined,
|
||||
opts: RunGoalsCitationsOptions,
|
||||
): Promise<void> {
|
||||
const store = await getStore({ project: projectName });
|
||||
|
||||
const rows = store.listGoalCitations({
|
||||
goalId: opts.goalId,
|
||||
agentId: opts.agentId,
|
||||
surface: opts.surface,
|
||||
startTime: opts.since,
|
||||
endTime: opts.until,
|
||||
limit: opts.limit ?? 50,
|
||||
});
|
||||
|
||||
if (opts.json) {
|
||||
console.log(JSON.stringify(rows, null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
if (rows.length === 0) {
|
||||
console.log("No goal citations match the filter.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
console.log(`${row.timestamp} ${row.goalId} ${row.agentId} ${row.surface} ${row.sourceRef}`);
|
||||
console.log(` ${row.snippet}`);
|
||||
}
|
||||
console.log(`\n${rows.length} citation(s).`);
|
||||
}
|
||||
|
||||
export async function runGoalsArchive(idArg: string | undefined, projectName?: string): Promise<void> {
|
||||
if (!idArg) {
|
||||
console.error("Usage: fn goals archive <id>");
|
||||
|
||||
@@ -715,7 +715,7 @@ describe("schema migration", () => {
|
||||
|
||||
const row = db.prepare("SELECT deletedAt FROM tasks WHERE id = 'FN-legacy'").get() as { deletedAt: string | null };
|
||||
expect(row.deletedAt).toBeNull();
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -748,7 +748,7 @@ describe("schema migration", () => {
|
||||
{ id: "WS-001", mode: "prompt", gateMode: "advisory" },
|
||||
{ id: "WS-002", mode: "script", gateMode: "advisory" },
|
||||
]);
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -798,7 +798,7 @@ describe("schema migration", () => {
|
||||
reviewerContextRetryCount: 0,
|
||||
reviewerFallbackRetryCount: 0,
|
||||
});
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -827,7 +827,7 @@ describe("schema migration", () => {
|
||||
|
||||
const columns = db.prepare("PRAGMA table_info(milestones)").all() as Array<{ name: string }>;
|
||||
expect(columns.map((column) => column.name)).toContain("acceptanceCriteria");
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -862,7 +862,7 @@ describe("schema migration", () => {
|
||||
{ id: "WS-002", mode: "script", enabled: 1, gateMode: "advisory" },
|
||||
{ id: "WS-003", mode: "prompt", enabled: 0, gateMode: "advisory" },
|
||||
]);
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
|
||||
@@ -330,7 +330,7 @@ describe("Database", () => {
|
||||
});
|
||||
|
||||
it("seeds schema version", () => {
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
|
||||
it("includes tokenUsageCacheWriteTokens on freshly initialized tasks table", () => {
|
||||
@@ -373,7 +373,7 @@ describe("Database", () => {
|
||||
|
||||
it("is idempotent - calling init() twice does not fail", () => {
|
||||
expect(() => db.init()).not.toThrow();
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
it("does not overwrite existing config on re-init", () => {
|
||||
// Update the config
|
||||
@@ -1443,7 +1443,7 @@ describe("schema migrations", () => {
|
||||
db.init();
|
||||
|
||||
// Verify version bumped to 29 (includes v1→v2 through v26→v29)
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
// Verify new columns exist and existing data is intact
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
@@ -1468,11 +1468,11 @@ describe("schema migrations", () => {
|
||||
const db = new Database(fusionDir);
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
// Re-init should not fail
|
||||
db.init();
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -1507,7 +1507,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
expect(cols.map((col) => col.name)).toContain("priority");
|
||||
@@ -1548,7 +1548,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
const colNames = cols.map((col) => col.name);
|
||||
@@ -1620,7 +1620,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
const colNames = cols.map((col) => col.name);
|
||||
@@ -1860,7 +1860,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(chat_messages)").all() as Array<{ name: string }>;
|
||||
expect(cols.map((col) => col.name)).toContain("attachments");
|
||||
@@ -1934,7 +1934,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'agentRatings'").all() as Array<{ name: string }>;
|
||||
expect(tables).toEqual([{ name: "agentRatings" }]);
|
||||
@@ -1958,7 +1958,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'mission_events'").all() as Array<{ name: string }>;
|
||||
expect(tables).toEqual([{ name: "mission_events" }]);
|
||||
@@ -2062,7 +2062,7 @@ describe("schema migrations", () => {
|
||||
db.init();
|
||||
|
||||
// Verify version bumped to 29
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
// Verify new columns exist and existing data is intact
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
@@ -2281,7 +2281,7 @@ describe("schema migrations", () => {
|
||||
|
||||
localDb.init();
|
||||
|
||||
expect(localDb.getSchemaVersion()).toBe(92);
|
||||
expect(localDb.getSchemaVersion()).toBe(93);
|
||||
const columns = localDb.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
expect(columns.map((column) => column.name)).toContain("tokenUsageCacheWriteTokens");
|
||||
|
||||
@@ -2592,7 +2592,7 @@ describe("createDatabase factory", () => {
|
||||
const db = createDatabase(fusionDir);
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
expect(db.getLastModified()).toBeGreaterThan(0);
|
||||
|
||||
db.close();
|
||||
@@ -2746,7 +2746,7 @@ describe("migration v77 task token budget columns", () => {
|
||||
|
||||
migrated = new Database(fusion);
|
||||
migrated.init();
|
||||
expect(migrated.getSchemaVersion()).toBe(92);
|
||||
expect(migrated.getSchemaVersion()).toBe(93);
|
||||
const rows = migrated.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
const names = new Set(rows.map((row) => row.name));
|
||||
expect(names.has("tokenBudgetSoftAlertedAt")).toBe(true);
|
||||
@@ -2792,7 +2792,7 @@ describe("migration v67 drops orphan project auth tables", () => {
|
||||
|
||||
migrated = new Database(fusion);
|
||||
migrated.init();
|
||||
expect(migrated.getSchemaVersion()).toBe(92);
|
||||
expect(migrated.getSchemaVersion()).toBe(93);
|
||||
const tables = migrated
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'project_auth_%'")
|
||||
.all() as Array<{ name: string }>;
|
||||
@@ -2819,7 +2819,7 @@ describe("migration v67 drops orphan project auth tables", () => {
|
||||
|
||||
try {
|
||||
fresh.init();
|
||||
expect(fresh.getSchemaVersion()).toBe(92);
|
||||
expect(fresh.getSchemaVersion()).toBe(93);
|
||||
const tables = fresh
|
||||
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'project_auth_%'")
|
||||
.all() as Array<{ name: string }>;
|
||||
|
||||
56
packages/core/src/__tests__/goal-citation-extractor.test.ts
Normal file
56
packages/core/src/__tests__/goal-citation-extractor.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildSnippet,
|
||||
extractGoalCitations,
|
||||
GOAL_CITATION_SNIPPET_MAX,
|
||||
} from "../goal-citation-extractor.js";
|
||||
|
||||
describe("goal-citation-extractor", () => {
|
||||
it("extracts simple fixture goal IDs", () => {
|
||||
const matches = extractGoalCitations("prioritizing per G-FAKE001 today");
|
||||
expect(matches).toHaveLength(1);
|
||||
expect(matches[0]?.goalId).toBe("G-FAKE001");
|
||||
});
|
||||
|
||||
it("extracts realistic generated goal IDs", () => {
|
||||
expect(extractGoalCitations("working against G-MABC-0001-XYZW now")[0]?.goalId).toBe(
|
||||
"G-MABC-0001-XYZW",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects false positives", () => {
|
||||
expect(extractGoalCitations("FN-5663 g-lowercase GG-NOPE prefixG-X")).toEqual([]);
|
||||
});
|
||||
|
||||
it("deduplicates duplicate IDs and keeps first index", () => {
|
||||
const text = "G-FAKE001 then later G-FAKE001 again";
|
||||
const matches = extractGoalCitations(text);
|
||||
expect(matches).toHaveLength(1);
|
||||
expect(matches[0]).toEqual({ goalId: "G-FAKE001", index: text.indexOf("G-FAKE001") });
|
||||
});
|
||||
|
||||
it("returns empty for empty/undefined/non-string", () => {
|
||||
expect(extractGoalCitations("")).toEqual([]);
|
||||
expect(extractGoalCitations(undefined as unknown as string)).toEqual([]);
|
||||
expect(extractGoalCitations(42 as unknown as string)).toEqual([]);
|
||||
});
|
||||
|
||||
it("buildSnippet caps length and collapses whitespace", () => {
|
||||
const text = `before\n\nG-FAKE001\n\nafter`;
|
||||
const snippet = buildSnippet(text, text.indexOf("G-FAKE001"));
|
||||
expect(snippet.length).toBeLessThanOrEqual(GOAL_CITATION_SNIPPET_MAX);
|
||||
expect(snippet).toContain("G-FAKE001");
|
||||
expect(snippet).toBe("before G-FAKE001 after");
|
||||
});
|
||||
|
||||
it("buildSnippet handles left/right/short-text edges", () => {
|
||||
const leftText = "G-LEFT text";
|
||||
expect(buildSnippet(leftText, 0, 8)).toContain("G-LEFT");
|
||||
|
||||
const rightText = "prefix text ending G-RIGHT";
|
||||
expect(buildSnippet(rightText, rightText.indexOf("G-RIGHT"), 12)).toContain("G-RIGHT");
|
||||
|
||||
const shortText = "tiny G-SHORT";
|
||||
expect(buildSnippet(shortText, shortText.indexOf("G-SHORT"), 200)).toBe("tiny G-SHORT");
|
||||
});
|
||||
});
|
||||
175
packages/core/src/__tests__/goal-citations-store.test.ts
Normal file
175
packages/core/src/__tests__/goal-citations-store.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as extractor from "../goal-citation-extractor.js";
|
||||
import { createTaskStoreTestHarness } from "./store-test-helpers.js";
|
||||
|
||||
describe("goal citations store integration", () => {
|
||||
const harness = createTaskStoreTestHarness();
|
||||
|
||||
beforeEach(async () => {
|
||||
await harness.beforeEach();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
vi.restoreAllMocks();
|
||||
await harness.afterEach();
|
||||
});
|
||||
|
||||
it("records agent_log citations for goal IDs", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
|
||||
await store.appendAgentLog(task.id, "working on G-FAKE001 now", "text", undefined, "executor");
|
||||
(store as any).flushAgentLogBuffer();
|
||||
|
||||
const rows = store.listGoalCitations({ goalId: "G-FAKE001" });
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({
|
||||
goalId: "G-FAKE001",
|
||||
agentId: "executor",
|
||||
taskId: task.id,
|
||||
surface: "agent_log",
|
||||
});
|
||||
expect(rows[0]?.sourceRef).toMatch(/^agentLog:/);
|
||||
});
|
||||
|
||||
it("does not record citations for near-miss log text", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
|
||||
await store.appendAgentLog(task.id, "text with FN-9999 only", "text", undefined, "executor");
|
||||
(store as any).flushAgentLogBuffer();
|
||||
|
||||
expect(store.listGoalCitations()).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("records task_document citations and sourceRef shape", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
|
||||
await store.upsertTaskDocument(task.id, {
|
||||
key: "notes",
|
||||
content: "check G-ALPHA and G-BETA now",
|
||||
author: "agent",
|
||||
});
|
||||
|
||||
const rows = store.listGoalCitations({ surface: "task_document" });
|
||||
expect(rows).toHaveLength(2);
|
||||
expect(rows.every((row) => row.sourceRef.startsWith(`document:${task.id}:notes:rev1`))).toBe(true);
|
||||
});
|
||||
|
||||
it("records citations from appendAgentLogBatch seam", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
|
||||
await store.appendAgentLogBatch([
|
||||
{ taskId: task.id, text: "tracking G-BATCH001", type: "text", agent: "executor" },
|
||||
]);
|
||||
|
||||
const rows = store.listGoalCitations({ goalId: "G-BATCH001" });
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]).toMatchObject({ surface: "agent_log", agentId: "executor", taskId: task.id });
|
||||
});
|
||||
|
||||
it("deduplicates goal citations per goalId+surface+sourceRef", () => {
|
||||
const store = harness.store();
|
||||
const inserted = store.recordGoalCitations([
|
||||
{
|
||||
goalId: "G-DUP",
|
||||
agentId: "agent-1",
|
||||
taskId: "FN-1",
|
||||
surface: "task_document",
|
||||
sourceRef: "document:FN-1:plan:rev3",
|
||||
snippet: "mentions G-DUP",
|
||||
},
|
||||
{
|
||||
goalId: "G-DUP",
|
||||
agentId: "agent-1",
|
||||
taskId: "FN-1",
|
||||
surface: "task_document",
|
||||
sourceRef: "document:FN-1:plan:rev3",
|
||||
snippet: "mentions G-DUP",
|
||||
},
|
||||
]);
|
||||
|
||||
expect(inserted).toHaveLength(1);
|
||||
expect(store.listGoalCitations({ goalId: "G-DUP" })).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("re-upserting same citation source is deduped", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
|
||||
await store.upsertTaskDocument(task.id, {
|
||||
key: "plan",
|
||||
content: "first G-SAME",
|
||||
author: "agent",
|
||||
});
|
||||
const firstRows = store.listGoalCitations({ goalId: "G-SAME" });
|
||||
expect(firstRows).toHaveLength(1);
|
||||
|
||||
const insertedAgain = store.recordGoalCitations([
|
||||
{
|
||||
goalId: "G-SAME",
|
||||
agentId: "agent",
|
||||
taskId: task.id,
|
||||
surface: "task_document",
|
||||
sourceRef: `document:${task.id}:plan:rev1`,
|
||||
snippet: "G-SAME",
|
||||
},
|
||||
]);
|
||||
expect(insertedAgain).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("filters by goal and time window in descending timestamp order", () => {
|
||||
const store = harness.store();
|
||||
store.recordGoalCitations([
|
||||
{
|
||||
goalId: "G-WIN",
|
||||
agentId: "agent-1",
|
||||
surface: "agent_log",
|
||||
sourceRef: "agentLog:1",
|
||||
snippet: "G-WIN older",
|
||||
timestamp: "2026-01-01T00:00:00.000Z",
|
||||
},
|
||||
{
|
||||
goalId: "G-WIN",
|
||||
agentId: "agent-1",
|
||||
surface: "agent_log",
|
||||
sourceRef: "agentLog:2",
|
||||
snippet: "G-WIN newer",
|
||||
timestamp: "2026-01-02T00:00:00.000Z",
|
||||
},
|
||||
{
|
||||
goalId: "G-OTHER",
|
||||
agentId: "agent-1",
|
||||
surface: "agent_log",
|
||||
sourceRef: "agentLog:3",
|
||||
snippet: "other",
|
||||
timestamp: "2026-01-02T00:00:00.000Z",
|
||||
},
|
||||
]);
|
||||
|
||||
const rows = store.listGoalCitations({
|
||||
goalId: "G-WIN",
|
||||
startTime: "2026-01-01T12:00:00.000Z",
|
||||
endTime: "2026-01-03T00:00:00.000Z",
|
||||
});
|
||||
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]?.sourceRef).toBe("agentLog:2");
|
||||
});
|
||||
|
||||
it("does not throw when citation scan fails during appendAgentLog", async () => {
|
||||
const store = harness.store();
|
||||
const task = await store.createTask({ title: "Task", description: "desc" });
|
||||
vi.spyOn(extractor, "extractGoalCitations").mockImplementation(() => {
|
||||
throw new Error("boom");
|
||||
});
|
||||
|
||||
await expect(store.appendAgentLog(task.id, "G-FAKE001", "text", undefined, "executor")).resolves.toBeUndefined();
|
||||
expect(() => (store as any).flushAgentLogBuffer()).not.toThrow();
|
||||
|
||||
const logs = await store.getAgentLogs(task.id);
|
||||
expect(logs).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -91,6 +91,6 @@ describe("goals schema", () => {
|
||||
});
|
||||
|
||||
it("reports schema version 92", () => {
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1000,7 +1000,7 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
// Step 1: Create a fresh database at v33 (runs all migrations up to 33)
|
||||
const db1 = createDatabase(legacyDir);
|
||||
db1.init();
|
||||
expect(db1.getSchemaVersion()).toBe(92);
|
||||
expect(db1.getSchemaVersion()).toBe(93);
|
||||
db1.close();
|
||||
|
||||
// Step 2: Manually downgrade to version 32 and drop insight tables
|
||||
@@ -1035,7 +1035,7 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
expect(tableNamesBefore).not.toContain("project_insight_runs");
|
||||
// Now run init — this triggers the v32→v33 migration
|
||||
db3.init();
|
||||
expect(db3.getSchemaVersion()).toBe(92);
|
||||
expect(db3.getSchemaVersion()).toBe(93);
|
||||
|
||||
// Step 4: Verify insight tables exist after migration
|
||||
const tablesAfter = db3.prepare(
|
||||
@@ -1066,12 +1066,12 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
try {
|
||||
const db1 = createDatabase(testDir);
|
||||
db1.init();
|
||||
expect(db1.getSchemaVersion()).toBe(92);
|
||||
expect(db1.getSchemaVersion()).toBe(93);
|
||||
db1.close();
|
||||
|
||||
const db2 = createDatabase(testDir);
|
||||
expect(() => db2.init()).not.toThrow();
|
||||
expect(db2.getSchemaVersion()).toBe(92);
|
||||
expect(db2.getSchemaVersion()).toBe(93);
|
||||
db2.close();
|
||||
} finally {
|
||||
rmSync(testDir, { recursive: true, force: true });
|
||||
@@ -1085,7 +1085,7 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
// Step 1: Create a fresh DB and run migrations
|
||||
const db1 = createDatabase(compatDir);
|
||||
db1.init();
|
||||
expect(db1.getSchemaVersion()).toBe(92);
|
||||
expect(db1.getSchemaVersion()).toBe(93);
|
||||
|
||||
// Step 2: Strip lifecycle and cancelledAt columns by recreating the
|
||||
// table without them. This simulates a DB that was created before the
|
||||
|
||||
@@ -2886,7 +2886,7 @@ describe("MissionStore", () => {
|
||||
|
||||
describe("Loop State & Validator Run Schema (v31)", () => {
|
||||
it("schema version is 40 after migration", () => {
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
|
||||
it("mission_features table has loop state columns", () => {
|
||||
|
||||
@@ -584,7 +584,7 @@ describe("Run Audit", () => {
|
||||
});
|
||||
|
||||
it("schema version is bumped to 40", () => {
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -42,7 +42,7 @@ describe("secrets schema migrations", () => {
|
||||
const version = db
|
||||
.prepare("SELECT value FROM __meta WHERE key = 'schemaVersion'")
|
||||
.get() as { value: string };
|
||||
expect(version.value).toBe("92");
|
||||
expect(version.value).toBe("93");
|
||||
} finally {
|
||||
db.close();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
@@ -105,7 +105,7 @@ describe("secrets schema migrations", () => {
|
||||
const version = db
|
||||
.prepare("SELECT value FROM __meta WHERE key = 'schemaVersion'")
|
||||
.get() as { value: string };
|
||||
expect(version.value).toBe("92");
|
||||
expect(version.value).toBe("93");
|
||||
} finally {
|
||||
db.close();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
@@ -155,7 +155,7 @@ describe("secrets schema migrations", () => {
|
||||
.prepare("SELECT value FROM __meta WHERE key = 'schemaVersion'")
|
||||
.get() as { value: string };
|
||||
|
||||
expect(projectVersion.value).toBe("92");
|
||||
expect(projectVersion.value).toBe("93");
|
||||
expect(centralVersion.value).toBe("13");
|
||||
} finally {
|
||||
projectDb.close();
|
||||
|
||||
@@ -60,7 +60,7 @@ describe("TaskStore merge queue", () => {
|
||||
expect.arrayContaining(["idx_mergeQueue_lease_ready", "idx_mergeQueue_leaseExpiresAt"]),
|
||||
);
|
||||
|
||||
expect(store.getDatabase().getSchemaVersion()).toBe(92);
|
||||
expect(store.getDatabase().getSchemaVersion()).toBe(93);
|
||||
});
|
||||
|
||||
it("migrates a legacy v88 database and preserves task rows", async () => {
|
||||
|
||||
@@ -51,7 +51,7 @@ describe("TaskStore task documents", () => {
|
||||
|
||||
expect(tableNames.has("task_documents")).toBe(true);
|
||||
expect(tableNames.has("task_document_revisions")).toBe(true);
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
|
||||
const index = db
|
||||
.prepare(
|
||||
|
||||
@@ -149,7 +149,7 @@ export function probeFts5(db: DatabaseSync): boolean {
|
||||
|
||||
// ── Schema Definition ────────────────────────────────────────────────
|
||||
|
||||
const SCHEMA_VERSION = 92;
|
||||
const SCHEMA_VERSION = 93;
|
||||
|
||||
function normalizeTaskComments(
|
||||
steeringComments: SteeringComment[] | undefined,
|
||||
@@ -772,6 +772,22 @@ CREATE TABLE IF NOT EXISTS goals (
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idxGoalsStatus ON goals(status);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS goal_citations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
goalId TEXT NOT NULL,
|
||||
agentId TEXT NOT NULL,
|
||||
taskId TEXT,
|
||||
surface TEXT NOT NULL,
|
||||
sourceRef TEXT NOT NULL,
|
||||
snippet TEXT NOT NULL,
|
||||
timestamp TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsGoalId ON goal_citations(goalId);
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsAgentId ON goal_citations(agentId);
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsTimestamp ON goal_citations(timestamp);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uxGoalCitationsDedup
|
||||
ON goal_citations(goalId, surface, sourceRef);
|
||||
|
||||
-- Milestones table (phases within a mission)
|
||||
CREATE TABLE IF NOT EXISTS milestones (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -3602,6 +3618,39 @@ export class Database {
|
||||
});
|
||||
}
|
||||
|
||||
if (version < 93) {
|
||||
this.applyMigration(93, () => {
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS goal_citations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
goalId TEXT NOT NULL,
|
||||
agentId TEXT NOT NULL,
|
||||
taskId TEXT,
|
||||
surface TEXT NOT NULL,
|
||||
sourceRef TEXT NOT NULL,
|
||||
snippet TEXT NOT NULL,
|
||||
timestamp TEXT NOT NULL
|
||||
)
|
||||
`);
|
||||
this.db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsGoalId
|
||||
ON goal_citations(goalId)
|
||||
`);
|
||||
this.db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsAgentId
|
||||
ON goal_citations(agentId)
|
||||
`);
|
||||
this.db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idxGoalCitationsTimestamp
|
||||
ON goal_citations(timestamp)
|
||||
`);
|
||||
this.db.exec(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uxGoalCitationsDedup
|
||||
ON goal_citations(goalId, surface, sourceRef)
|
||||
`);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
56
packages/core/src/goal-citation-extractor.ts
Normal file
56
packages/core/src/goal-citation-extractor.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { GoalCitationMatch } from "./types.js";
|
||||
|
||||
export const GOAL_ID_PATTERN = /\bG-[0-9A-Z]+(?:-[0-9A-Z]+)*\b/g;
|
||||
|
||||
export const GOAL_CITATION_SNIPPET_MAX = 200;
|
||||
|
||||
export function extractGoalCitations(text: string): GoalCitationMatch[] {
|
||||
const normalized = String(text ?? "");
|
||||
if (normalized.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const matches: GoalCitationMatch[] = [];
|
||||
const seen = new Set<string>();
|
||||
const pattern = new RegExp(GOAL_ID_PATTERN.source, GOAL_ID_PATTERN.flags);
|
||||
|
||||
let match: RegExpExecArray | null;
|
||||
while ((match = pattern.exec(normalized)) !== null) {
|
||||
const goalId = match[0];
|
||||
if (seen.has(goalId)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(goalId);
|
||||
matches.push({ goalId, index: match.index });
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
export function buildSnippet(text: string, index: number, max = GOAL_CITATION_SNIPPET_MAX): string {
|
||||
const normalized = String(text ?? "");
|
||||
if (normalized.length === 0 || max <= 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const goalMatch = normalized
|
||||
.slice(Math.max(0, index))
|
||||
.match(/^G-[0-9A-Z]+(?:-[0-9A-Z]+)*/);
|
||||
const goalEnd = goalMatch ? index + goalMatch[0].length : index;
|
||||
|
||||
let start = Math.max(0, index - Math.floor(max / 2));
|
||||
let end = Math.min(normalized.length, start + max);
|
||||
if (end < goalEnd) {
|
||||
end = Math.min(normalized.length, goalEnd);
|
||||
start = Math.max(0, end - max);
|
||||
} else {
|
||||
start = Math.max(0, end - max);
|
||||
}
|
||||
|
||||
const snippet = normalized
|
||||
.slice(start, end)
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
|
||||
return snippet.length <= max ? snippet : snippet.slice(0, max).trim();
|
||||
}
|
||||
@@ -723,6 +723,19 @@ export { ACTIVE_GOAL_LIMIT, ActiveGoalLimitExceededError } from "./goal-types.js
|
||||
export type { Goal, GoalCreateInput, GoalListFilter, GoalStatus, GoalUpdateInput } from "./goal-types.js";
|
||||
export { GoalStore } from "./goal-store.js";
|
||||
export type { GoalStoreEvents } from "./goal-store.js";
|
||||
export type {
|
||||
GoalCitation,
|
||||
GoalCitationSurface,
|
||||
GoalCitationInput,
|
||||
GoalCitationFilter,
|
||||
GoalCitationMatch,
|
||||
} from "./types.js";
|
||||
export {
|
||||
extractGoalCitations,
|
||||
buildSnippet,
|
||||
GOAL_ID_PATTERN,
|
||||
GOAL_CITATION_SNIPPET_MAX,
|
||||
} from "./goal-citation-extractor.js";
|
||||
|
||||
// ── Central Infrastructure (Multi-Project Support) ───────────────────────────
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto";
|
||||
import { mkdir, readdir, readFile, writeFile, rename, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { existsSync, watch, type FSWatcher } from "node:fs";
|
||||
import type { Task, TaskDetail, TaskCreateInput, TaskAttachment, AgentLogEntry, BoardConfig, Column, CheckoutClaimPrecondition, MergeResult, Settings, GlobalSettings, ProjectSettings, ActivityLogEntry, ActivityEventType, TaskDocument, TaskDocumentRevision, TaskDocumentCreateInput, TaskDocumentWithTask, InboxTask, TaskLogEntry, RunMutationContext, RunAuditEvent, RunAuditEventInput, RunAuditEventFilter, ArchivedTaskEntry, ArchiveAgentLogMode, TaskPriority, SourceType, WorkflowStepTemplate, Agent, AutostashOrphanRecord, TaskCommitAssociation, TaskCommitAssociationMatchSource, TaskCommitAssociationConfidence, GithubIssueAction, MergeQueueEntry, MergeQueueEnqueueOptions, MergeQueueAcquireOptions, MergeQueueReleaseOutcome, HandoffToReviewOptions } from "./types.js";
|
||||
import type { Task, TaskDetail, TaskCreateInput, TaskAttachment, AgentLogEntry, BoardConfig, Column, CheckoutClaimPrecondition, MergeResult, Settings, GlobalSettings, ProjectSettings, ActivityLogEntry, ActivityEventType, TaskDocument, TaskDocumentRevision, TaskDocumentCreateInput, TaskDocumentWithTask, InboxTask, TaskLogEntry, RunMutationContext, RunAuditEvent, RunAuditEventInput, RunAuditEventFilter, ArchivedTaskEntry, ArchiveAgentLogMode, TaskPriority, SourceType, WorkflowStepTemplate, Agent, AutostashOrphanRecord, TaskCommitAssociation, TaskCommitAssociationMatchSource, TaskCommitAssociationConfidence, GithubIssueAction, MergeQueueEntry, MergeQueueEnqueueOptions, MergeQueueAcquireOptions, MergeQueueReleaseOutcome, HandoffToReviewOptions, GoalCitation, GoalCitationFilter, GoalCitationInput, GoalCitationSurface } from "./types.js";
|
||||
import { createActivityLogSnapshot, createRunAuditSnapshot, createTaskMetadataSnapshot, toTaskMetadataRecord, validateSnapshotEnvelope, type ActivityLogSnapshot, type RunAuditSnapshot, type TaskMetadataSnapshot } from "./shared-mesh-state.js";
|
||||
import { VALID_TRANSITIONS, DEFAULT_SETTINGS, isGlobalOnlySettingsKey, WORKFLOW_STEP_TEMPLATES, validateDocumentKey } from "./types.js";
|
||||
import { DEFAULT_PROJECT_SETTINGS } from "./settings-schema.js";
|
||||
@@ -14,6 +14,7 @@ import { GlobalSettingsStore } from "./global-settings.js";
|
||||
import { Database, toJson, toJsonNullable, fromJson } from "./db.js";
|
||||
import { ArchiveDatabase } from "./archive-db.js";
|
||||
import { detectLegacyData, migrateFromLegacy } from "./db-migrate.js";
|
||||
import { buildSnippet, extractGoalCitations } from "./goal-citation-extractor.js";
|
||||
import { MissionStore } from "./mission-store.js";
|
||||
import { PluginStore } from "./plugin-store.js";
|
||||
import { InsightStore } from "./insight-store.js";
|
||||
@@ -252,6 +253,17 @@ interface TaskDocumentRevisionRow {
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface GoalCitationRow {
|
||||
id: number;
|
||||
goalId: string;
|
||||
agentId: string;
|
||||
taskId: string | null;
|
||||
surface: GoalCitationSurface;
|
||||
sourceRef: string;
|
||||
snippet: string;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
/** Database row shape for the runAuditEvents table. */
|
||||
interface RunAuditEventRow {
|
||||
id: string;
|
||||
@@ -1738,6 +1750,120 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
|
||||
};
|
||||
}
|
||||
|
||||
private rowToGoalCitation(row: GoalCitationRow): GoalCitation {
|
||||
return {
|
||||
id: row.id,
|
||||
goalId: row.goalId,
|
||||
agentId: row.agentId,
|
||||
...(row.taskId ? { taskId: row.taskId } : {}),
|
||||
surface: row.surface,
|
||||
sourceRef: row.sourceRef,
|
||||
snippet: row.snippet,
|
||||
timestamp: row.timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
recordGoalCitations(inputs: GoalCitationInput[]): GoalCitation[] {
|
||||
if (inputs.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT OR IGNORE INTO goal_citations (goalId, agentId, taskId, surface, sourceRef, snippet, timestamp)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
RETURNING *
|
||||
`);
|
||||
|
||||
const inserted: GoalCitation[] = [];
|
||||
this.db.transaction(() => {
|
||||
for (const input of inputs) {
|
||||
const row = stmt.get(
|
||||
input.goalId,
|
||||
input.agentId,
|
||||
input.taskId ?? null,
|
||||
input.surface,
|
||||
input.sourceRef,
|
||||
input.snippet,
|
||||
input.timestamp ?? now,
|
||||
) as GoalCitationRow | undefined;
|
||||
if (row) {
|
||||
inserted.push(this.rowToGoalCitation(row));
|
||||
}
|
||||
}
|
||||
if (inserted.length > 0) {
|
||||
this.db.bumpLastModified();
|
||||
}
|
||||
});
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
listGoalCitations(filter: GoalCitationFilter = {}): GoalCitation[] {
|
||||
const clauses: string[] = [];
|
||||
const params: Array<string | number> = [];
|
||||
|
||||
if (filter.goalId) {
|
||||
clauses.push("goalId = ?");
|
||||
params.push(filter.goalId);
|
||||
}
|
||||
if (filter.agentId) {
|
||||
clauses.push("agentId = ?");
|
||||
params.push(filter.agentId);
|
||||
}
|
||||
if (filter.taskId) {
|
||||
clauses.push("taskId = ?");
|
||||
params.push(filter.taskId);
|
||||
}
|
||||
if (filter.surface) {
|
||||
clauses.push("surface = ?");
|
||||
params.push(filter.surface);
|
||||
}
|
||||
if (filter.startTime) {
|
||||
clauses.push("timestamp >= ?");
|
||||
params.push(filter.startTime);
|
||||
}
|
||||
if (filter.endTime) {
|
||||
clauses.push("timestamp <= ?");
|
||||
params.push(filter.endTime);
|
||||
}
|
||||
|
||||
const where = clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "";
|
||||
const limit = Math.max(1, Math.min(filter.limit ?? 200, 1000));
|
||||
|
||||
const rows = this.db
|
||||
.prepare(
|
||||
`SELECT * FROM goal_citations ${where} ORDER BY timestamp DESC, id DESC LIMIT ?`,
|
||||
)
|
||||
.all(...params, limit) as GoalCitationRow[];
|
||||
|
||||
return rows.map((row) => this.rowToGoalCitation(row));
|
||||
}
|
||||
|
||||
private scanAndRecordCitations(
|
||||
text: string,
|
||||
surface: GoalCitationSurface,
|
||||
sourceRef: string,
|
||||
agentId: string,
|
||||
taskId?: string,
|
||||
timestamp?: string,
|
||||
): GoalCitationInput[] {
|
||||
const matches = extractGoalCitations(text);
|
||||
if (matches.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return matches.map((match) => ({
|
||||
goalId: match.goalId,
|
||||
agentId,
|
||||
...(taskId ? { taskId } : {}),
|
||||
surface,
|
||||
sourceRef,
|
||||
snippet: buildSnippet(text, match.index),
|
||||
...(timestamp ? { timestamp } : {}),
|
||||
}));
|
||||
}
|
||||
|
||||
private getTaskSelectClause(slim: boolean, tableAlias?: string): string {
|
||||
if (!slim) {
|
||||
return tableAlias ? `${tableAlias}.*` : "*";
|
||||
@@ -8137,8 +8263,42 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
|
||||
INSERT INTO agentLogEntries (taskId, timestamp, text, type, detail, agent)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
const citationInputs: GoalCitationInput[] = [];
|
||||
for (const entry of validEntries) {
|
||||
stmt.run(entry.taskId, entry.timestamp, entry.text, entry.type, entry.detail, entry.agent);
|
||||
const insertResult = stmt.run(
|
||||
entry.taskId,
|
||||
entry.timestamp,
|
||||
entry.text,
|
||||
entry.type,
|
||||
entry.detail,
|
||||
entry.agent,
|
||||
) as { lastInsertRowid?: number | bigint };
|
||||
const insertedId = insertResult.lastInsertRowid;
|
||||
if (insertedId === undefined || insertedId === null) {
|
||||
continue;
|
||||
}
|
||||
const sourceRef = `agentLog:${String(insertedId)}`;
|
||||
try {
|
||||
citationInputs.push(
|
||||
...this.scanAndRecordCitations(
|
||||
entry.text,
|
||||
"agent_log",
|
||||
sourceRef,
|
||||
entry.agent ?? "unknown",
|
||||
entry.taskId,
|
||||
entry.timestamp,
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
console.warn("[fusion] Failed to scan goal citations from agent_log:", err);
|
||||
}
|
||||
}
|
||||
if (citationInputs.length > 0) {
|
||||
try {
|
||||
this.recordGoalCitations(citationInputs);
|
||||
} catch (err) {
|
||||
console.warn("[fusion] Failed to record goal citations from agent_log batch:", err);
|
||||
}
|
||||
}
|
||||
this.db.bumpLastModified();
|
||||
}
|
||||
@@ -8194,15 +8354,41 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
|
||||
`);
|
||||
|
||||
this.db.transaction(() => {
|
||||
const citationInputs: GoalCitationInput[] = [];
|
||||
for (const entry of normalizedEntries) {
|
||||
stmt.run(
|
||||
const insertResult = stmt.run(
|
||||
entry.taskId,
|
||||
timestamp,
|
||||
entry.text,
|
||||
entry.type,
|
||||
entry.detail ?? null,
|
||||
entry.agent ?? null,
|
||||
);
|
||||
) as { lastInsertRowid?: number | bigint };
|
||||
const insertedId = insertResult.lastInsertRowid;
|
||||
if (insertedId === undefined || insertedId === null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
citationInputs.push(
|
||||
...this.scanAndRecordCitations(
|
||||
entry.text,
|
||||
"agent_log",
|
||||
`agentLog:${String(insertedId)}`,
|
||||
entry.agent ?? "unknown",
|
||||
entry.taskId,
|
||||
timestamp,
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
console.warn("[fusion] Failed to scan goal citations from agent log batch:", err);
|
||||
}
|
||||
}
|
||||
if (citationInputs.length > 0) {
|
||||
try {
|
||||
this.recordGoalCitations(citationInputs);
|
||||
} catch (err) {
|
||||
console.warn("[fusion] Failed to record goal citations from appendAgentLogBatch:", err);
|
||||
}
|
||||
}
|
||||
this.db.bumpLastModified();
|
||||
});
|
||||
@@ -8712,6 +8898,22 @@ export class TaskStore extends EventEmitter<TaskStoreEvents> {
|
||||
const task = await this.getTask(taskId);
|
||||
this.emit("task:updated", task);
|
||||
|
||||
try {
|
||||
const citationInputs = this.scanAndRecordCitations(
|
||||
input.content,
|
||||
"task_document",
|
||||
`document:${taskId}:${input.key}:rev${document.revision}`,
|
||||
input.author ?? "user",
|
||||
taskId,
|
||||
document.updatedAt,
|
||||
);
|
||||
if (citationInputs.length > 0) {
|
||||
this.recordGoalCitations(citationInputs);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("[fusion] Failed to scan/record goal citations from task document:", err);
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
|
||||
@@ -1307,6 +1307,57 @@ export interface TaskDocumentWithTask extends TaskDocument {
|
||||
taskColumn?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Goal-citation Slice 2 success-signal surfaces where goal IDs are extracted.
|
||||
*/
|
||||
export type GoalCitationSurface = "agent_log" | "task_document";
|
||||
|
||||
/**
|
||||
* A unique extracted goal ID and the index of its first appearance in source text.
|
||||
*/
|
||||
export interface GoalCitationMatch {
|
||||
goalId: string;
|
||||
index: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Input payload for recording a single observed goal citation in the Slice 2 success-signal trail.
|
||||
* `snippet` must be a bounded source-text substring (≤200 chars), never the full source body.
|
||||
*/
|
||||
export interface GoalCitationInput {
|
||||
goalId: string;
|
||||
agentId: string;
|
||||
taskId?: string;
|
||||
surface: GoalCitationSurface;
|
||||
sourceRef: string;
|
||||
snippet: string;
|
||||
timestamp?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persisted goal-citation audit row used to measure Slice 2 anchoring success signal.
|
||||
* `snippet` is always a bounded substring (≤200 chars), not full source content.
|
||||
*/
|
||||
export interface GoalCitation extends Required<Pick<GoalCitationInput, "goalId" | "agentId" | "surface" | "sourceRef" | "snippet">> {
|
||||
id: number;
|
||||
taskId?: string;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter contract for querying goal-citation success-signal rows across scanned surfaces.
|
||||
* Snippet payloads remain bounded substrings (≤200 chars) of original text.
|
||||
*/
|
||||
export interface GoalCitationFilter {
|
||||
goalId?: string;
|
||||
agentId?: string;
|
||||
taskId?: string;
|
||||
surface?: GoalCitationSurface;
|
||||
startTime?: string;
|
||||
endTime?: string;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export const DOCUMENT_KEY_RE = /^[a-zA-Z0-9_-]{1,64}$/;
|
||||
|
||||
/** Shared GitHub owner/repo slug validation for repo override inputs. */
|
||||
|
||||
@@ -743,8 +743,8 @@ describe("RoadmapStore", () => {
|
||||
});
|
||||
|
||||
describe("schema version", () => {
|
||||
it("schema version is 92 after init", () => {
|
||||
expect(db.getSchemaVersion()).toBe(92);
|
||||
it("schema version is 93 after init", () => {
|
||||
expect(db.getSchemaVersion()).toBe(93);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user