Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e7db0ef6b | |||
| 31f84a7ab0 | |||
| 793a5c619d | |||
| b89e31f468 | |||
| b9131c728e | |||
| cd338822f2 | |||
| 7242588dd9 | |||
| c34a8b3c58 | |||
| 08b143ea0b | |||
| 1269de5b96 | |||
| 263fe40146 | |||
| 1e8ccb8962 |
@@ -0,0 +1,220 @@
|
||||
name: "retrospect-workflow"
|
||||
description: "Post-execution retrospective: analyze a completed thread, find inefficiencies, and improve the workflow definition."
|
||||
roles:
|
||||
analyst:
|
||||
description: "Scans thread execution for anomalies and produces a findings report"
|
||||
goal: "You are a workflow execution analyst. You review completed thread data to find inefficiencies, wasted effort, and procedure gaps."
|
||||
capabilities:
|
||||
- data-analysis
|
||||
procedure: |
|
||||
You receive a completed thread ID in your task prompt.
|
||||
|
||||
Phase 0 — Validation (must pass before any analysis):
|
||||
1. Run `uwf step list <thread-id>` to get thread metadata including the workflow hash
|
||||
2. Run `uwf workflow show <workflow-hash>` to get the workflow name
|
||||
3. Verify the workflow exists locally: check `.workflows/<name>.yaml` in the current repo
|
||||
- If NOT found: output $status=wrong_project with the workflow name. Do NOT proceed.
|
||||
4. Compare the thread's workflow hash against the current registered version:
|
||||
- Run `uwf workflow show <name>` to get the current hash
|
||||
- If hashes differ: the thread ran on an older version. Note this — you will need to diff versions after analysis.
|
||||
|
||||
Phase 1 — Overview scan:
|
||||
5. From the step list, compute a health signal for each step:
|
||||
- Duration: flag if >2x the median of other steps
|
||||
- Output tokens: flag if >2x the median
|
||||
- Status flow: flag non-happy-path transitions (rejected, fix_code, fix_spec, hook_failed)
|
||||
- Step count: flag if the same role appears more than expected (indicates loops)
|
||||
6. If no anomalies found AND versions match: output $status=clean
|
||||
7. If no anomalies found BUT versions differ:
|
||||
- Diff the two workflow versions to check if any procedure changes are relevant
|
||||
- If the current version already addresses potential concerns: output $status=clean with a note
|
||||
- Otherwise: proceed to Phase 2
|
||||
|
||||
Phase 2 — Targeted deep-dive (only for flagged steps):
|
||||
8. For each flagged step, run `uwf step show <hash>` to get the detail with turns
|
||||
9. Analyze the turn sequence for:
|
||||
- Repeated tool calls with the same or similar input (blind retries)
|
||||
- Tool errors followed by no strategy change (same approach retried)
|
||||
- Unnecessary exploration (reading files or running commands unrelated to the task)
|
||||
- Hallucinated commands or flags (commands that don't exist or wrong syntax)
|
||||
- Excessive turns before reaching the goal
|
||||
10. For each finding, record:
|
||||
- Which role and step hash
|
||||
- What happened (specific turn indices and commands)
|
||||
- Root cause hypothesis (procedure gap, missing pitfall, unclear instruction)
|
||||
- Suggested fix (what to add/change in the procedure)
|
||||
11. If versions differ: compare findings against the version diff.
|
||||
Mark any finding that is already fixed in the current version as "resolved_in_current".
|
||||
Only report findings that are NOT yet addressed.
|
||||
|
||||
Output a structured findings report. Set $status=clean if nothing actionable, $status=findings if unresolved issues exist, or $status=wrong_project if the workflow doesn't belong here.
|
||||
output: "A findings report with per-issue root cause and suggested procedure fixes. Set $status to clean or findings (with report hash)."
|
||||
frontmatter:
|
||||
oneOf:
|
||||
- properties:
|
||||
$status: { const: "clean" }
|
||||
summary: { type: string }
|
||||
required: [$status, summary]
|
||||
- properties:
|
||||
$status: { const: "findings" }
|
||||
report: { type: string }
|
||||
targetWorkflow: { type: string }
|
||||
required: [$status, report, targetWorkflow]
|
||||
- properties:
|
||||
$status: { const: "wrong_project" }
|
||||
workflowName: { type: string }
|
||||
required: [$status, workflowName]
|
||||
proposer:
|
||||
description: "Translates findings into concrete workflow edits"
|
||||
goal: "You are a workflow improvement proposer. You read the analyst's findings and produce specific, minimal edits to the workflow YAML."
|
||||
capabilities:
|
||||
- planning
|
||||
procedure: |
|
||||
1. Read the analyst's findings report from your task prompt
|
||||
2. Locate the target workflow YAML:
|
||||
- Workflow definitions live in the WORKFLOW ENGINE repo (where `uwf` is developed), NOT in the repo that was analyzed.
|
||||
- Find it via: `uwf workflow show <targetWorkflow> --format yaml` to read the current definition
|
||||
- The physical file is `.workflows/<targetWorkflow>.yaml` in the workflow engine repo
|
||||
- Use `git rev-parse --show-toplevel` in the current directory to find the workflow engine repo root
|
||||
3. Read the current workflow YAML to understand existing procedures
|
||||
4. For each finding, draft a minimal edit:
|
||||
- Prefer adding a pitfall note or clarifying instruction over restructuring
|
||||
- If a procedure step is ambiguous, make it explicit
|
||||
- If a tool usage pattern is wrong, add a "Do NOT" or "IMPORTANT" note
|
||||
- Keep edits surgical — don't rewrite procedures that work fine
|
||||
5. Check if existing tests need updating (search for test files referencing the workflow)
|
||||
6. Produce a change plan as CAS text node via `uwf cas put-text "<plan>"`
|
||||
|
||||
The plan should list each edit with:
|
||||
- File path
|
||||
- What to change (old text → new text, or addition)
|
||||
- Why (linked to which finding)
|
||||
- Any test updates needed
|
||||
output: "A change plan stored in CAS. Set $status to ready (with plan hash and repoPath) or no_action (if findings don't warrant changes)."
|
||||
frontmatter:
|
||||
oneOf:
|
||||
- properties:
|
||||
$status: { const: "ready" }
|
||||
plan: { type: string }
|
||||
repoPath: { type: string }
|
||||
required: [$status, plan, repoPath]
|
||||
- properties:
|
||||
$status: { const: "no_action" }
|
||||
reason: { type: string }
|
||||
required: [$status, reason]
|
||||
developer:
|
||||
description: "Applies the proposed workflow edits"
|
||||
goal: "You are a developer agent. You apply workflow YAML edits and update related tests."
|
||||
capabilities:
|
||||
- coding
|
||||
procedure: |
|
||||
IMPORTANT: Always work in a git worktree, NEVER modify the main working directory directly.
|
||||
The workflow definitions live in THIS repo (the workflow engine), not the repo that was analyzed.
|
||||
|
||||
Before starting any work, set up an isolated worktree:
|
||||
1. Use `git rev-parse --show-toplevel` to find the repo root (do NOT use repoPath from proposer — that's the analyzed repo)
|
||||
2. `git fetch origin` to get latest refs
|
||||
3. `git worktree add .worktrees/retrospect/<short-slug> -b retrospect/<short-slug> origin/main`
|
||||
4. `cd .worktrees/retrospect/<short-slug> && bun install`
|
||||
5. ALL subsequent work must happen inside the worktree directory.
|
||||
|
||||
Then apply changes:
|
||||
6. Read the change plan from CAS: `uwf cas get <plan hash>`
|
||||
7. Apply each edit from the plan to the workflow YAML
|
||||
8. Update or add tests as specified in the plan
|
||||
9. Run `bun run build` and `bun test` to verify
|
||||
10. Run `bun run check` for lint
|
||||
11. Commit with message: `improve: <workflow-name> — <brief summary>`
|
||||
output: "List all files changed and provide a summary. Set $status to done (with branch/worktree), or failed (with reason)."
|
||||
frontmatter:
|
||||
oneOf:
|
||||
- properties:
|
||||
$status: { const: "done" }
|
||||
branch: { type: string }
|
||||
worktree: { type: string }
|
||||
required: [$status, branch, worktree]
|
||||
- properties:
|
||||
$status: { const: "failed" }
|
||||
reason: { type: string }
|
||||
required: [$status, reason]
|
||||
reviewer:
|
||||
description: "Reviews the workflow edits for correctness"
|
||||
goal: "You are a reviewer. You verify that workflow edits are minimal, correct, and actually address the findings."
|
||||
capabilities:
|
||||
- code-review
|
||||
procedure: |
|
||||
The worktree path is provided in your task prompt. cd into it first.
|
||||
|
||||
Review criteria:
|
||||
1. Each edit must trace back to a specific finding — no drive-by changes
|
||||
2. Edits should be minimal — don't rewrite working procedures
|
||||
3. New pitfall notes or instructions must be clear and actionable
|
||||
4. Tests must be updated if assertions changed
|
||||
5. `bun run build` and `bun test` must pass
|
||||
6. `bunx biome check` must pass
|
||||
|
||||
IMPORTANT: `tea pr create` must run from the MAIN repo directory (not a worktree), because tea cannot detect the repo from worktree `.git` files.
|
||||
output: "Explain your decision. Set $status to approved (with branch/worktree) or rejected (with comments)."
|
||||
frontmatter:
|
||||
oneOf:
|
||||
- properties:
|
||||
$status: { const: "approved" }
|
||||
branch: { type: string }
|
||||
worktree: { type: string }
|
||||
required: [$status, branch, worktree]
|
||||
- properties:
|
||||
$status: { const: "rejected" }
|
||||
comments: { type: string }
|
||||
worktree: { type: string }
|
||||
required: [$status, comments, worktree]
|
||||
committer:
|
||||
description: "Commits and creates PR"
|
||||
goal: "You are a committer agent. You create a clean commit and push a PR."
|
||||
capabilities: []
|
||||
procedure: |
|
||||
The worktree path, branch name, and repo info are provided in your task prompt.
|
||||
cd into the worktree first.
|
||||
|
||||
Note: You inherit the developer's worktree and branch. Do NOT create a new branch.
|
||||
1. Stage all changes: `git add -A`
|
||||
2. Commit with a descriptive message: `git commit -m "improve: <workflow> — <summary>"`
|
||||
3. Push the branch: `git push -u origin <branch-name>`
|
||||
- If push hook fails: capture the error log in your output, mark hook_failed
|
||||
4. On push success: create a PR via `tea pr create --title "..." --description "..."`
|
||||
- IMPORTANT: `tea pr create` must run from the MAIN repo directory (not a worktree), because tea cannot detect the repo from worktree `.git` files. cd to the repo root first.
|
||||
- Do NOT pass `--repo` — let tea auto-detect from the main repo's git remote.
|
||||
- PR description must include: What / Why / Findings / Changes sections
|
||||
- On tea failure: capture stderr/stdout, include PR details for manual creation, mark hook_failed
|
||||
5. After PR creation, clean up the worktree:
|
||||
- cd to the repo root (parent of .worktrees)
|
||||
- `git worktree remove <worktree-path>`
|
||||
output: "Include PR URL on success or error log on failure. Set $status to committed (with prUrl) or hook_failed (with error)."
|
||||
frontmatter:
|
||||
oneOf:
|
||||
- properties:
|
||||
$status: { const: "committed" }
|
||||
prUrl: { type: string }
|
||||
required: [$status, prUrl]
|
||||
- properties:
|
||||
$status: { const: "hook_failed" }
|
||||
error: { type: string }
|
||||
required: [$status, error]
|
||||
graph:
|
||||
$START:
|
||||
_: { role: "analyst", prompt: "Analyze completed thread {{{threadId}}} for execution anomalies." }
|
||||
analyst:
|
||||
clean: { role: "$END", prompt: "No issues found. Thread executed cleanly." }
|
||||
findings: { role: "proposer", prompt: "Findings report: {{{report}}}. Target workflow: {{{targetWorkflow}}}. Propose minimal edits." }
|
||||
wrong_project: { role: "$END", prompt: "Thread uses workflow '{{{workflowName}}}' which does not exist in this project. Run retrospect from the correct repo." }
|
||||
proposer:
|
||||
no_action: { role: "$END", prompt: "No actionable changes needed: {{{reason}}}." }
|
||||
ready: { role: "developer", prompt: "Apply the change plan (CAS hash: {{{plan}}}) to the workflow definitions in this repo." }
|
||||
developer:
|
||||
done: { role: "reviewer", prompt: "Review workflow edits on branch {{{branch}}} at {{{worktree}}}." }
|
||||
failed: { role: "$END", prompt: "Developer failed: {{{reason}}}. Ending workflow." }
|
||||
reviewer:
|
||||
rejected: { role: "developer", prompt: "Reviewer rejected: {{{comments}}}. Fix the issues in {{{worktree}}}." }
|
||||
approved: { role: "committer", prompt: "Approved. Commit and push branch {{{branch}}} from {{{worktree}}}." }
|
||||
committer:
|
||||
hook_failed: { role: "developer", prompt: "Push hook failed: {{{error}}}. Fix and re-submit." }
|
||||
committed: { role: "$END", prompt: "PR created: {{{prUrl}}}. Workflow improved." }
|
||||
@@ -3,7 +3,8 @@
|
||||
"version": "0.5.3",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
"json-cas": "./src/index.ts"
|
||||
"json-cas": "./src/index.ts",
|
||||
"ucas": "./src/index.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "bun test",
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const pkgPath = resolve(import.meta.dir, "../package.json");
|
||||
|
||||
describe("ucas command alias", () => {
|
||||
test("T1: ucas bin entry exists in package.json", async () => {
|
||||
const pkg = await Bun.file(pkgPath).json();
|
||||
expect(pkg.bin.ucas).toBe("./src/index.ts");
|
||||
});
|
||||
|
||||
test("T2: json-cas bin entry is preserved in package.json", async () => {
|
||||
const pkg = await Bun.file(pkgPath).json();
|
||||
expect(pkg.bin["json-cas"]).toBe("./src/index.ts");
|
||||
});
|
||||
|
||||
test("T3: ucas command is executable and shows help", async () => {
|
||||
const entrypoint = resolve(import.meta.dir, "index.ts");
|
||||
const proc = Bun.spawn(["bun", entrypoint, "--help"], {
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
const exitCode = await proc.exited;
|
||||
const stdout = await new Response(proc.stdout).text();
|
||||
expect(exitCode).toBe(0);
|
||||
expect(stdout.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("T4: both commands point to the same entrypoint", async () => {
|
||||
const pkg = await Bun.file(pkgPath).json();
|
||||
expect(pkg.bin.ucas).toBe(pkg.bin["json-cas"]);
|
||||
});
|
||||
});
|
||||
@@ -9,11 +9,14 @@ import {
|
||||
CasNodeNotFoundError,
|
||||
computeHash,
|
||||
createVariableStore,
|
||||
gc,
|
||||
getSchema,
|
||||
InvalidScopeError,
|
||||
InvalidTagFormatError,
|
||||
putSchema,
|
||||
refs,
|
||||
SchemaMismatchError,
|
||||
TagLabelConflictError,
|
||||
VariableNotFoundError,
|
||||
validate,
|
||||
verify,
|
||||
@@ -23,10 +26,17 @@ import { createFsStore } from "@uncaged/json-cas-fs";
|
||||
|
||||
// ---- Argument parsing ----
|
||||
|
||||
type Flags = Record<string, string | boolean>;
|
||||
type Flags = Record<string, string | boolean | string[]>;
|
||||
|
||||
/** Flags that consume the next token as their value. All others are boolean. */
|
||||
const VALUE_FLAGS = new Set(["store", "format", "scope", "value", "var-db"]);
|
||||
const VALUE_FLAGS = new Set([
|
||||
"store",
|
||||
"format",
|
||||
"scope",
|
||||
"value",
|
||||
"var-db",
|
||||
"tag",
|
||||
]);
|
||||
|
||||
function parseArgs(argv: string[]): { flags: Flags; positional: string[] } {
|
||||
const flags: Flags = {};
|
||||
@@ -39,7 +49,19 @@ function parseArgs(argv: string[]): { flags: Flags; positional: string[] } {
|
||||
if (VALUE_FLAGS.has(key)) {
|
||||
const next = argv[i + 1];
|
||||
if (next !== undefined && !next.startsWith("--")) {
|
||||
flags[key] = next;
|
||||
// Handle repeatable flags (like --tag)
|
||||
if (key === "tag") {
|
||||
const existing = flags[key];
|
||||
if (Array.isArray(existing)) {
|
||||
existing.push(next);
|
||||
} else if (typeof existing === "string") {
|
||||
flags[key] = [existing, next];
|
||||
} else {
|
||||
flags[key] = [next];
|
||||
}
|
||||
} else {
|
||||
flags[key] = next;
|
||||
}
|
||||
i++;
|
||||
} else {
|
||||
flags[key] = true;
|
||||
@@ -113,8 +135,19 @@ async function getVariableSchemaHash(): Promise<Hash> {
|
||||
schema: { type: "string" },
|
||||
created: { type: "number" },
|
||||
updated: { type: "number" },
|
||||
tags: { type: "object" },
|
||||
labels: { type: "array", items: { type: "string" } },
|
||||
},
|
||||
required: ["id", "scope", "value", "schema", "created", "updated"],
|
||||
required: [
|
||||
"id",
|
||||
"scope",
|
||||
"value",
|
||||
"schema",
|
||||
"created",
|
||||
"updated",
|
||||
"tags",
|
||||
"labels",
|
||||
],
|
||||
};
|
||||
|
||||
// Compute hash or retrieve from store
|
||||
@@ -135,6 +168,38 @@ async function wrapVariableEnvelope(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse tag/label arguments
|
||||
* Returns: { tags: Record<string, string>, labels: string[], deleteNames: string[] }
|
||||
*/
|
||||
function parseTagsLabels(args: string[]): {
|
||||
tags: Record<string, string>;
|
||||
labels: string[];
|
||||
deleteNames: string[];
|
||||
} {
|
||||
const tags: Record<string, string> = {};
|
||||
const labels: string[] = [];
|
||||
const deleteNames: string[] = [];
|
||||
|
||||
for (const arg of args) {
|
||||
if (arg.startsWith(":")) {
|
||||
// Deletion syntax: :name
|
||||
deleteNames.push(arg.slice(1));
|
||||
} else if (arg.includes(":")) {
|
||||
// Tag: key:value (split on first colon)
|
||||
const colonIdx = arg.indexOf(":");
|
||||
const key = arg.slice(0, colonIdx);
|
||||
const value = arg.slice(colonIdx + 1);
|
||||
tags[key] = value;
|
||||
} else {
|
||||
// Label: bare identifier
|
||||
labels.push(arg);
|
||||
}
|
||||
}
|
||||
|
||||
return { tags, labels, deleteNames };
|
||||
}
|
||||
|
||||
// ---- Commands ----
|
||||
|
||||
async function cmdInit(): Promise<void> {
|
||||
@@ -308,6 +373,7 @@ async function cmdCat(args: string[]): Promise<void> {
|
||||
async function cmdVarCreate(_args: string[]): Promise<void> {
|
||||
const scope = flags.scope as string | undefined;
|
||||
const value = flags.value as string | undefined;
|
||||
const tagFlags = flags.tag;
|
||||
|
||||
if (!scope) die("Usage: json-cas var create --scope <scope> --value <hash>");
|
||||
if (!value) die("Usage: json-cas var create --scope <scope> --value <hash>");
|
||||
@@ -315,11 +381,31 @@ async function cmdVarCreate(_args: string[]): Promise<void> {
|
||||
const varStore = openVarStore();
|
||||
|
||||
try {
|
||||
const variable = varStore.create(scope, value);
|
||||
// Parse tags/labels from --tag flags
|
||||
const tagArgs = Array.isArray(tagFlags)
|
||||
? tagFlags
|
||||
: typeof tagFlags === "string"
|
||||
? [tagFlags]
|
||||
: [];
|
||||
const { tags, labels, deleteNames } = parseTagsLabels(tagArgs);
|
||||
|
||||
// Check for conflicts in initial tags/labels
|
||||
if (deleteNames.length > 0) {
|
||||
die("Error: Cannot use deletion syntax (:name) in var create");
|
||||
}
|
||||
|
||||
const variable = varStore.create(scope, value, {
|
||||
tags: Object.keys(tags).length > 0 ? tags : undefined,
|
||||
labels: labels.length > 0 ? labels : undefined,
|
||||
});
|
||||
const envelope = await wrapVariableEnvelope(variable);
|
||||
out(envelope);
|
||||
} catch (e) {
|
||||
if (e instanceof InvalidScopeError || e instanceof CasNodeNotFoundError) {
|
||||
if (
|
||||
e instanceof InvalidScopeError ||
|
||||
e instanceof CasNodeNotFoundError ||
|
||||
e instanceof TagLabelConflictError
|
||||
) {
|
||||
die(`Error: ${e.message}`);
|
||||
}
|
||||
throw e;
|
||||
@@ -394,13 +480,67 @@ async function cmdVarDelete(args: string[]): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function cmdVarList(_args: string[]): Promise<void> {
|
||||
const scope = (flags.scope as string | undefined) ?? "";
|
||||
async function cmdVarTag(args: string[]): Promise<void> {
|
||||
const id = args[0];
|
||||
if (!id) die("Usage: json-cas var tag <id> <tag>...");
|
||||
|
||||
const tagArgs = args.slice(1);
|
||||
if (tagArgs.length === 0) {
|
||||
die("Usage: json-cas var tag <id> <tag>...");
|
||||
}
|
||||
|
||||
const varStore = openVarStore();
|
||||
|
||||
try {
|
||||
const variables = varStore.list({ scope });
|
||||
const { tags, labels, deleteNames } = parseTagsLabels(tagArgs);
|
||||
|
||||
const variable = varStore.tag(id, {
|
||||
add: Object.keys(tags).length > 0 ? tags : undefined,
|
||||
addLabels: labels.length > 0 ? labels : undefined,
|
||||
delete: deleteNames.length > 0 ? deleteNames : undefined,
|
||||
});
|
||||
|
||||
const envelope = await wrapVariableEnvelope(variable);
|
||||
out(envelope);
|
||||
} catch (e) {
|
||||
if (
|
||||
e instanceof VariableNotFoundError ||
|
||||
e instanceof TagLabelConflictError ||
|
||||
e instanceof InvalidTagFormatError
|
||||
) {
|
||||
die(`Error: ${e.message}`);
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
varStore.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function cmdVarList(_args: string[]): Promise<void> {
|
||||
const scope = (flags.scope as string | undefined) ?? "";
|
||||
const tagFlags = flags.tag;
|
||||
|
||||
const varStore = openVarStore();
|
||||
|
||||
try {
|
||||
// Parse tags/labels from --tag flags
|
||||
const tagArgs = Array.isArray(tagFlags)
|
||||
? tagFlags
|
||||
: typeof tagFlags === "string"
|
||||
? [tagFlags]
|
||||
: [];
|
||||
const { tags, labels, deleteNames } = parseTagsLabels(tagArgs);
|
||||
|
||||
// Check for invalid deletion syntax in filters
|
||||
if (deleteNames.length > 0) {
|
||||
die("Error: Cannot use deletion syntax (:name) in var list filters");
|
||||
}
|
||||
|
||||
const variables = varStore.list({
|
||||
scope,
|
||||
tags: Object.keys(tags).length > 0 ? tags : undefined,
|
||||
labels: labels.length > 0 ? labels : undefined,
|
||||
});
|
||||
const envelope = await wrapVariableEnvelope(variables);
|
||||
out(envelope);
|
||||
} catch (e) {
|
||||
@@ -413,6 +553,18 @@ async function cmdVarList(_args: string[]): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function cmdGc(_args: string[]): Promise<void> {
|
||||
const store = createFsStore(storePath);
|
||||
const varStore = createVariableStore(varDbPath, store);
|
||||
|
||||
try {
|
||||
const stats = gc(store, varStore);
|
||||
out(stats);
|
||||
} finally {
|
||||
varStore.close();
|
||||
}
|
||||
}
|
||||
|
||||
function printUsage(): void {
|
||||
console.log(`\
|
||||
Usage: json-cas [--store <path>] [--json] <command> [args]
|
||||
@@ -432,16 +584,19 @@ Commands:
|
||||
walk <hash> [--format tree] Recursive traversal
|
||||
hash <type-hash> <file.json> Compute hash without storing (dry run)
|
||||
cat <hash> [--payload] Output node (--payload for payload only)
|
||||
var create --scope <s> --value <h> Create a variable
|
||||
var create --scope <s> --value <h> [--tag <tag>...] Create a variable
|
||||
var get <id> Get a variable by ID
|
||||
var update <id> <hash> Update variable value
|
||||
var delete <id> Delete a variable
|
||||
var list [--scope <prefix>] List variables (optionally filter by scope prefix)
|
||||
var tag <id> <tag>... Add/update/delete tags and labels
|
||||
var list [--scope <prefix>] [--tag <tag>...] List variables (filter by scope/tags/labels)
|
||||
gc Run garbage collection
|
||||
|
||||
Flags:
|
||||
--store <path> Store directory (default: ~/.uncaged/json-cas)
|
||||
--var-db <path> Variable database path (default: <store>/variables.db)
|
||||
--json Compact JSON output`);
|
||||
--json Compact JSON output
|
||||
--tag <tag> Tag/label (can be repeated): key:value (tag), name (label), :name (delete)`);
|
||||
}
|
||||
|
||||
// ---- Dispatch ----
|
||||
@@ -530,6 +685,9 @@ switch (cmd) {
|
||||
case "delete":
|
||||
await cmdVarDelete(subRest);
|
||||
break;
|
||||
case "tag":
|
||||
await cmdVarTag(subRest);
|
||||
break;
|
||||
case "list":
|
||||
await cmdVarList(subRest);
|
||||
break;
|
||||
@@ -539,6 +697,10 @@ switch (cmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
case "gc":
|
||||
await cmdGc(rest);
|
||||
break;
|
||||
|
||||
default:
|
||||
die(`Unknown command: ${cmd}`);
|
||||
}
|
||||
|
||||
@@ -1,822 +0,0 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { unlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
describe("CLI var commands", () => {
|
||||
let storePath: string;
|
||||
let varDbPath: string;
|
||||
let cliPath: string;
|
||||
let schemaHash: string;
|
||||
let hashA: string;
|
||||
let hashB: string;
|
||||
let testCounter = 0;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary paths with counter to ensure uniqueness
|
||||
testCounter++;
|
||||
storePath = join(tmpdir(), `test-cli-store-${Date.now()}-${testCounter}`);
|
||||
varDbPath = join(storePath, "variables.db");
|
||||
cliPath = join(import.meta.dir, "index.ts");
|
||||
|
||||
// Initialize store and create test data
|
||||
const initResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "init"],
|
||||
{
|
||||
encoding: "utf-8",
|
||||
},
|
||||
);
|
||||
expect(initResult.status).toBe(0);
|
||||
|
||||
// Create a schema
|
||||
const schemaFile = join(tmpdir(), `schema-${Date.now()}.json`);
|
||||
await Bun.write(
|
||||
schemaFile,
|
||||
JSON.stringify({
|
||||
type: "object",
|
||||
properties: { name: { type: "string" } },
|
||||
}),
|
||||
);
|
||||
|
||||
const schemaPutResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "schema", "put", schemaFile],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(schemaPutResult.status).toBe(0);
|
||||
schemaHash = schemaPutResult.stdout.trim();
|
||||
|
||||
// Create test CAS nodes
|
||||
const dataFileA = join(tmpdir(), `data-a-${Date.now()}.json`);
|
||||
await Bun.write(dataFileA, JSON.stringify({ name: "hello" }));
|
||||
|
||||
const putResultA = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "put", schemaHash, dataFileA],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(putResultA.status).toBe(0);
|
||||
hashA = putResultA.stdout.trim();
|
||||
|
||||
const dataFileB = join(tmpdir(), `data-b-${Date.now()}.json`);
|
||||
await Bun.write(dataFileB, JSON.stringify({ name: "world" }));
|
||||
|
||||
const putResultB = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "put", schemaHash, dataFileB],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(putResultB.status).toBe(0);
|
||||
hashB = putResultB.stdout.trim();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Cleanup
|
||||
try {
|
||||
unlinkSync(varDbPath);
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
});
|
||||
|
||||
describe("Test Group 1: Variable Creation", () => {
|
||||
test("1.1: Create variable with valid scope", () => {
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(result.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
|
||||
// Check the actual variable in the value field
|
||||
const variable = output.value;
|
||||
expect(variable.id).toMatch(/^[0-9A-HJKMNP-TV-Z]{26}$/);
|
||||
expect(variable.scope).toBe("uwf/thread/");
|
||||
expect(variable.value).toBe(hashA);
|
||||
expect(variable.schema).toBe(schemaHash);
|
||||
expect(variable.created).toBeGreaterThan(Date.now() - 5000);
|
||||
expect(variable.updated).toBe(variable.created);
|
||||
});
|
||||
|
||||
test("1.2: Create variable fails with scope not ending in /", () => {
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stderr).toContain("scope must end with /");
|
||||
});
|
||||
|
||||
test("1.3: Create variable fails with non-existent CAS node", () => {
|
||||
const fakeHash = "FAKEHASH00000";
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/",
|
||||
"--value",
|
||||
fakeHash,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stderr).toContain("CAS node not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 2: Variable Retrieval", () => {
|
||||
test("2.1: Get existing variable", () => {
|
||||
// Create a variable first
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const created = JSON.parse(createResult.stdout).value;
|
||||
|
||||
// Get the variable
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", created.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(result.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
|
||||
// Check the actual variable in the value field
|
||||
const variable = output.value;
|
||||
expect(variable.id).toBe(created.id);
|
||||
expect(variable.scope).toBe("uwf/thread/");
|
||||
expect(variable.value).toBe(hashA);
|
||||
expect(variable.schema).toBe(schemaHash);
|
||||
});
|
||||
|
||||
test("2.2: Get non-existent variable", () => {
|
||||
const fakeId = "01ARZ3NDEKTSV4RRFFQ69G5FAV";
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", fakeId],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stderr).toContain("Variable not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 3: Variable Update (Schema Consistent)", () => {
|
||||
test("3.1: Update variable with matching schema", async () => {
|
||||
// Create a variable
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const created = JSON.parse(createResult.stdout).value;
|
||||
|
||||
// Wait a bit to ensure different timestamp
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
// Update the variable
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "update", created.id, hashB],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(result.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
|
||||
// Check the actual variable in the value field
|
||||
const variable = output.value;
|
||||
expect(variable.id).toBe(created.id);
|
||||
expect(variable.value).toBe(hashB);
|
||||
expect(variable.schema).toBe(schemaHash);
|
||||
expect(variable.updated).toBeGreaterThan(created.created);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 4: Variable Update (Schema Mismatch)", () => {
|
||||
test("4.1: Update variable fails with schema mismatch", async () => {
|
||||
// Create another schema
|
||||
const schema2File = join(tmpdir(), `schema2-${Date.now()}.json`);
|
||||
await Bun.write(
|
||||
schema2File,
|
||||
JSON.stringify({
|
||||
type: "object",
|
||||
properties: { count: { type: "number" } },
|
||||
}),
|
||||
);
|
||||
|
||||
const schema2PutResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "schema", "put", schema2File],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const schemaHash2 = schema2PutResult.stdout.trim();
|
||||
|
||||
// Create a node with the second schema
|
||||
const dataFileC = join(tmpdir(), `data-c-${Date.now()}.json`);
|
||||
await Bun.write(dataFileC, JSON.stringify({ count: 42 }));
|
||||
|
||||
const putResultC = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "put", schemaHash2, dataFileC],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const hashC = putResultC.stdout.trim();
|
||||
|
||||
// Create a variable with first schema
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const created = JSON.parse(createResult.stdout).value;
|
||||
|
||||
// Try to update with different schema
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "update", created.id, hashC],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stderr.toLowerCase()).toContain("schema mismatch");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 5: Variable Deletion", () => {
|
||||
test("5.1: Delete existing variable", () => {
|
||||
// Create a variable
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const created = JSON.parse(createResult.stdout).value;
|
||||
|
||||
// Delete the variable
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "delete", created.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(result.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
|
||||
// Check the actual variable in the value field
|
||||
const variable = output.value;
|
||||
expect(variable.id).toBe(created.id);
|
||||
|
||||
// Verify it's deleted
|
||||
const getResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", created.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(getResult.status).not.toBe(0);
|
||||
});
|
||||
|
||||
test("5.3: Delete non-existent variable", () => {
|
||||
const fakeId = "01ARZ3NDEKTSV4RRFFQ69G5FAV";
|
||||
const result = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "delete", fakeId],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(result.status).not.toBe(0);
|
||||
expect(result.stderr).toContain("Variable not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 6: Variable Listing", () => {
|
||||
test("6.1: List variables with scope prefix", () => {
|
||||
// Create variables with different scopes
|
||||
const createResult1 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult1.status).toBe(0);
|
||||
const var1 = JSON.parse(createResult1.stdout).value;
|
||||
|
||||
const createResult2 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashB,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult2.status).toBe(0);
|
||||
const var2 = JSON.parse(createResult2.stdout).value;
|
||||
|
||||
const createResult3 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/agent/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult3.status).toBe(0);
|
||||
const var3 = JSON.parse(createResult3.stdout).value;
|
||||
|
||||
const createResult4 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"app/config/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult4.status).toBe(0);
|
||||
|
||||
// List all variables with uwf/ prefix
|
||||
const listResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "list", "--scope", "uwf/"],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(listResult.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(listResult.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
expect(Array.isArray(output.value)).toBe(true);
|
||||
|
||||
// Check the actual variables in the value field
|
||||
const variables = output.value;
|
||||
expect(variables).toHaveLength(3);
|
||||
expect(
|
||||
variables.every((v: { scope: string }) => v.scope.startsWith("uwf/")),
|
||||
).toBe(true);
|
||||
|
||||
// Verify ordering by created timestamp
|
||||
expect(variables[0].id).toBe(var1.id);
|
||||
expect(variables[1].id).toBe(var2.id);
|
||||
expect(variables[2].id).toBe(var3.id);
|
||||
});
|
||||
|
||||
test("6.2: List all variables when no scope specified", () => {
|
||||
// Create variables with different scopes
|
||||
const createResult1 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult1.status).toBe(0);
|
||||
|
||||
const createResult2 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"app/config/",
|
||||
"--value",
|
||||
hashB,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult2.status).toBe(0);
|
||||
|
||||
// List all variables without scope filter
|
||||
const listResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "list"],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(listResult.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(listResult.stdout);
|
||||
// Expect envelope format
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
expect(Array.isArray(output.value)).toBe(true);
|
||||
|
||||
const variables = output.value;
|
||||
expect(variables).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("6.3: List returns empty array when no matches", () => {
|
||||
// Create a variable
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult.status).toBe(0);
|
||||
|
||||
// List with non-matching scope
|
||||
const listResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"list",
|
||||
"--scope",
|
||||
"nonexistent/",
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(listResult.status).toBe(0);
|
||||
|
||||
const output = JSON.parse(listResult.stdout);
|
||||
expect(output.type).toMatch(/^[0-9A-HJKMNP-TV-Z]{13}$/);
|
||||
expect(output.value).toBeDefined();
|
||||
expect(Array.isArray(output.value)).toBe(true);
|
||||
expect(output.value).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("6.4: List fails with invalid scope format", () => {
|
||||
const listResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "list", "--scope", "uwf"],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
|
||||
expect(listResult.status).not.toBe(0);
|
||||
expect(listResult.stderr).toContain("scope must end with /");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test Group 7: Integration Tests", () => {
|
||||
test("7.1: Full lifecycle workflow", async () => {
|
||||
// Create variable
|
||||
const createResult = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(createResult.status).toBe(0);
|
||||
const var1 = JSON.parse(createResult.stdout).value;
|
||||
expect(var1.value).toBe(hashA);
|
||||
|
||||
// Get variable
|
||||
const getResult1 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(getResult1.status).toBe(0);
|
||||
const retrieved1 = JSON.parse(getResult1.stdout).value;
|
||||
expect(retrieved1.value).toBe(hashA);
|
||||
|
||||
// Wait to ensure different timestamp
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
// Update variable
|
||||
const updateResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "update", var1.id, hashB],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(updateResult.status).toBe(0);
|
||||
const updated = JSON.parse(updateResult.stdout).value;
|
||||
expect(updated.value).toBe(hashB);
|
||||
|
||||
// Get updated variable
|
||||
const getResult2 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(getResult2.status).toBe(0);
|
||||
const retrieved2 = JSON.parse(getResult2.stdout).value;
|
||||
expect(retrieved2.value).toBe(hashB);
|
||||
|
||||
// Delete variable
|
||||
const deleteResult = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "delete", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(deleteResult.status).toBe(0);
|
||||
|
||||
// Verify deletion
|
||||
const getResult3 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(getResult3.status).not.toBe(0);
|
||||
});
|
||||
|
||||
test("7.2: Multiple variables with same scope", () => {
|
||||
// Create two variables
|
||||
const createResult1 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const var1 = JSON.parse(createResult1.stdout).value;
|
||||
|
||||
const createResult2 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashB,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const var2 = JSON.parse(createResult2.stdout).value;
|
||||
|
||||
// Verify independence
|
||||
expect(var1.id).not.toBe(var2.id);
|
||||
|
||||
const getResult1 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const retrieved1 = JSON.parse(getResult1.stdout).value;
|
||||
expect(retrieved1.value).toBe(hashA);
|
||||
|
||||
const getResult2 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var2.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const retrieved2 = JSON.parse(getResult2.stdout).value;
|
||||
expect(retrieved2.value).toBe(hashB);
|
||||
|
||||
// Delete var1, verify var2 still exists
|
||||
spawnSync("bun", [
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"delete",
|
||||
var1.id,
|
||||
]);
|
||||
|
||||
const getResult2Final = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var2.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(getResult2Final.status).toBe(0);
|
||||
const retrieved2Final = JSON.parse(getResult2Final.stdout).value;
|
||||
expect(retrieved2Final.value).toBe(hashB);
|
||||
});
|
||||
|
||||
test("7.3: Variables with hierarchical scopes", () => {
|
||||
const createResult1 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const var1 = JSON.parse(createResult1.stdout).value;
|
||||
|
||||
const createResult2 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/thread/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const var2 = JSON.parse(createResult2.stdout).value;
|
||||
|
||||
const createResult3 = spawnSync(
|
||||
"bun",
|
||||
[
|
||||
cliPath,
|
||||
"--store",
|
||||
storePath,
|
||||
"var",
|
||||
"create",
|
||||
"--scope",
|
||||
"uwf/workflow/",
|
||||
"--value",
|
||||
hashA,
|
||||
],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
const var3 = JSON.parse(createResult3.stdout).value;
|
||||
|
||||
expect(var1.scope).toBe("uwf/");
|
||||
expect(var2.scope).toBe("uwf/thread/");
|
||||
expect(var3.scope).toBe("uwf/workflow/");
|
||||
|
||||
// Verify all exist
|
||||
const get1 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var1.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(get1.status).toBe(0);
|
||||
|
||||
const get2 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var2.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(get2.status).toBe(0);
|
||||
|
||||
const get3 = spawnSync(
|
||||
"bun",
|
||||
[cliPath, "--store", storePath, "var", "get", var3.id],
|
||||
{ encoding: "utf-8" },
|
||||
);
|
||||
expect(get3.status).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
unlinkSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { join } from "node:path";
|
||||
@@ -175,6 +176,44 @@ export function createFsStore(dir: string): BootstrapCapableStore {
|
||||
return typeIndex.get(typeHash) ?? [];
|
||||
},
|
||||
|
||||
listAll(): Hash[] {
|
||||
return Array.from(data.keys());
|
||||
},
|
||||
|
||||
delete(hash: Hash): void {
|
||||
const node = data.get(hash);
|
||||
if (node) {
|
||||
data.delete(hash);
|
||||
// Delete file
|
||||
try {
|
||||
unlinkSync(join(dir, `${hash}.bin`));
|
||||
} catch {
|
||||
// ignore if file doesn't exist
|
||||
}
|
||||
// Remove from type index
|
||||
const list = typeIndex.get(node.type);
|
||||
if (list) {
|
||||
const idx = list.indexOf(hash);
|
||||
if (idx !== -1) {
|
||||
list.splice(idx, 1);
|
||||
}
|
||||
if (list.length === 0) {
|
||||
typeIndex.delete(node.type);
|
||||
// Delete empty index file
|
||||
try {
|
||||
unlinkSync(join(indexDir, node.type));
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
} else {
|
||||
// Rewrite index file
|
||||
const body = `${list.join("\n")}\n`;
|
||||
writeFileSync(join(indexDir, node.type), body, "utf8");
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
[BOOTSTRAP_STORE]: putSelfReferencing,
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { unlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { bootstrap } from "./bootstrap.js";
|
||||
import { gc } from "./gc.js";
|
||||
import { putSchema } from "./schema.js";
|
||||
import { createMemoryStore } from "./store.js";
|
||||
import type { Store } from "./types.js";
|
||||
import { VariableStore } from "./variable-store.js";
|
||||
|
||||
const tmpDbPath = () =>
|
||||
join(
|
||||
tmpdir(),
|
||||
`test-gc-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
|
||||
);
|
||||
|
||||
describe("GC - Variable Model Refactoring", () => {
|
||||
let store: Store;
|
||||
let dbPath: string;
|
||||
|
||||
afterEach(() => {
|
||||
try {
|
||||
unlinkSync(dbPath);
|
||||
} catch {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
});
|
||||
|
||||
test("GC preserves variable-referenced nodes", async () => {
|
||||
store = createMemoryStore();
|
||||
await bootstrap(store);
|
||||
const schema = { type: "object", properties: { name: { type: "string" } } };
|
||||
const schemaHash = await putSchema(store, schema);
|
||||
|
||||
const hashRef = await store.put(schemaHash, { name: "referenced" });
|
||||
const hashOrphan = await store.put(schemaHash, { name: "orphan" });
|
||||
|
||||
dbPath = tmpDbPath();
|
||||
const varStore = new VariableStore(dbPath, store);
|
||||
|
||||
varStore.set("config", hashRef);
|
||||
|
||||
const stats = gc(store, varStore);
|
||||
|
||||
expect(store.has(hashRef)).toBe(true);
|
||||
expect(store.has(hashOrphan)).toBe(false);
|
||||
expect(stats.scanned).toBe(1);
|
||||
expect(stats.collected).toBeGreaterThanOrEqual(1);
|
||||
|
||||
varStore.close();
|
||||
});
|
||||
|
||||
test("GC preserves nodes from variables with same name, different schemas", async () => {
|
||||
store = createMemoryStore();
|
||||
await bootstrap(store);
|
||||
const schemaA = { type: "object", properties: { x: { type: "number" } } };
|
||||
const schemaB = { type: "object", properties: { y: { type: "string" } } };
|
||||
const schemaAHash = await putSchema(store, schemaA);
|
||||
const schemaBHash = await putSchema(store, schemaB);
|
||||
|
||||
const hashA = await store.put(schemaAHash, { x: 42 });
|
||||
const hashB = await store.put(schemaBHash, { y: "hello" });
|
||||
const hashOrphan = await store.put(schemaAHash, { x: 99 });
|
||||
|
||||
dbPath = tmpDbPath();
|
||||
const varStore = new VariableStore(dbPath, store);
|
||||
|
||||
varStore.set("config", hashA);
|
||||
varStore.set("config", hashB);
|
||||
|
||||
const stats = gc(store, varStore);
|
||||
|
||||
expect(store.has(hashA)).toBe(true);
|
||||
expect(store.has(hashB)).toBe(true);
|
||||
expect(store.has(hashOrphan)).toBe(false);
|
||||
expect(stats.scanned).toBe(2);
|
||||
|
||||
varStore.close();
|
||||
});
|
||||
|
||||
test("GC removes nodes after variable deletion", async () => {
|
||||
store = createMemoryStore();
|
||||
await bootstrap(store);
|
||||
const schema = { type: "object", properties: { name: { type: "string" } } };
|
||||
const schemaHash = await putSchema(store, schema);
|
||||
|
||||
const hashRef = await store.put(schemaHash, { name: "referenced" });
|
||||
|
||||
dbPath = tmpDbPath();
|
||||
const varStore = new VariableStore(dbPath, store);
|
||||
|
||||
varStore.set("config", hashRef);
|
||||
varStore.remove("config", schemaHash);
|
||||
|
||||
const stats = gc(store, varStore);
|
||||
|
||||
expect(store.has(hashRef)).toBe(false);
|
||||
expect(stats.scanned).toBe(0);
|
||||
|
||||
varStore.close();
|
||||
});
|
||||
|
||||
test("GC is global across all variables", async () => {
|
||||
store = createMemoryStore();
|
||||
await bootstrap(store);
|
||||
const schemaA = { type: "object", properties: { x: { type: "number" } } };
|
||||
const schemaB = { type: "object", properties: { y: { type: "string" } } };
|
||||
const schemaAHash = await putSchema(store, schemaA);
|
||||
const schemaBHash = await putSchema(store, schemaB);
|
||||
|
||||
const hash1 = await store.put(schemaAHash, { x: 1 });
|
||||
const hash2 = await store.put(schemaAHash, { x: 2 });
|
||||
const hash3 = await store.put(schemaBHash, { y: "a" });
|
||||
const hashOrphan = await store.put(schemaAHash, { x: 999 });
|
||||
|
||||
dbPath = tmpDbPath();
|
||||
const varStore = new VariableStore(dbPath, store);
|
||||
|
||||
varStore.set("uwf.thread", hash1);
|
||||
varStore.set("uwf.workflow", hash2);
|
||||
varStore.set("app.config", hash3);
|
||||
|
||||
const stats = gc(store, varStore);
|
||||
|
||||
expect(store.has(hash1)).toBe(true);
|
||||
expect(store.has(hash2)).toBe(true);
|
||||
expect(store.has(hash3)).toBe(true);
|
||||
expect(store.has(hashOrphan)).toBe(false);
|
||||
expect(stats.scanned).toBe(3);
|
||||
|
||||
varStore.close();
|
||||
});
|
||||
|
||||
test("GC integration with refactored variable store", async () => {
|
||||
store = createMemoryStore();
|
||||
await bootstrap(store);
|
||||
|
||||
const schemaA = { type: "object", properties: { x: { type: "number" } } };
|
||||
const schemaB = { type: "object", properties: { y: { type: "string" } } };
|
||||
const schemaAHash = await putSchema(store, schemaA);
|
||||
const schemaBHash = await putSchema(store, schemaB);
|
||||
|
||||
const hashA1 = await store.put(schemaAHash, { x: 1 });
|
||||
const hashA2 = await store.put(schemaAHash, { x: 2 });
|
||||
const hashB = await store.put(schemaBHash, { y: "hello" });
|
||||
const hashOrphan1 = await store.put(schemaAHash, { x: 999 });
|
||||
const hashOrphan2 = await store.put(schemaBHash, { y: "orphan" });
|
||||
|
||||
dbPath = tmpDbPath();
|
||||
const varStore = new VariableStore(dbPath, store);
|
||||
|
||||
// Create variables
|
||||
varStore.set("var1", hashA1);
|
||||
varStore.set("var2", hashA2);
|
||||
varStore.set("var3", hashB);
|
||||
|
||||
// First GC: orphans removed
|
||||
let stats = gc(store, varStore);
|
||||
expect(store.has(hashA1)).toBe(true);
|
||||
expect(store.has(hashA2)).toBe(true);
|
||||
expect(store.has(hashB)).toBe(true);
|
||||
expect(store.has(hashOrphan1)).toBe(false);
|
||||
expect(store.has(hashOrphan2)).toBe(false);
|
||||
expect(stats.scanned).toBe(3);
|
||||
|
||||
// Delete one variable
|
||||
varStore.remove("var2", schemaAHash);
|
||||
|
||||
// Second GC: hashA2 removed
|
||||
stats = gc(store, varStore);
|
||||
expect(store.has(hashA1)).toBe(true);
|
||||
expect(store.has(hashA2)).toBe(false);
|
||||
expect(store.has(hashB)).toBe(true);
|
||||
expect(stats.scanned).toBe(2);
|
||||
|
||||
varStore.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
import { walk } from "./schema.js";
|
||||
import type { Hash, Store } from "./types.js";
|
||||
import type { VariableStore } from "./variable-store.js";
|
||||
|
||||
export interface GcStats {
|
||||
total: number; // Total CAS nodes before GC
|
||||
reachable: number; // Nodes marked as reachable
|
||||
collected: number; // Nodes deleted (swept)
|
||||
scanned: number; // Variables scanned as roots
|
||||
}
|
||||
|
||||
/**
|
||||
* Garbage collection: mark-and-sweep algorithm
|
||||
* - Roots: all variable values (global, not scoped)
|
||||
* - Mark: recursively walk refs from roots
|
||||
* - Sweep: delete unmarked nodes
|
||||
* - Schema preservation: schemas of reachable nodes are also marked
|
||||
*/
|
||||
export function gc(store: Store, varStore: VariableStore): GcStats {
|
||||
// Get all variables (no filters → global)
|
||||
const variables = varStore.list();
|
||||
const scanned = variables.length;
|
||||
|
||||
// Collect unique root hashes from all variables
|
||||
const roots = new Set<Hash>();
|
||||
for (const variable of variables) {
|
||||
roots.add(variable.value);
|
||||
}
|
||||
|
||||
// Mark phase: walk from all roots
|
||||
const reachable = new Set<Hash>();
|
||||
|
||||
for (const rootHash of roots) {
|
||||
walk(store, rootHash, (hash, node) => {
|
||||
// Mark the node itself
|
||||
reachable.add(hash);
|
||||
// Mark the schema (type) of the node
|
||||
reachable.add(node.type);
|
||||
});
|
||||
}
|
||||
|
||||
// Walk the schema chain to ensure bootstrap meta-schema is preserved
|
||||
// For each reachable schema, walk its schema chain (not its references)
|
||||
const schemasToWalk = new Set<Hash>();
|
||||
for (const hash of reachable) {
|
||||
const node = store.get(hash);
|
||||
if (node) {
|
||||
schemasToWalk.add(node.type);
|
||||
}
|
||||
}
|
||||
|
||||
for (const schemaHash of schemasToWalk) {
|
||||
// Walk the schema's type chain (meta-schema, etc.)
|
||||
let current: Hash | null = schemaHash;
|
||||
while (current !== null && !reachable.has(current)) {
|
||||
reachable.add(current);
|
||||
const node = store.get(current);
|
||||
if (!node || node.type === current) {
|
||||
// Self-referencing or missing node, stop
|
||||
break;
|
||||
}
|
||||
current = node.type;
|
||||
}
|
||||
}
|
||||
|
||||
// Preserve all self-referencing nodes (bootstrap meta-schema)
|
||||
// These are nodes where type === hash
|
||||
const allHashes = store.listAll();
|
||||
for (const hash of allHashes) {
|
||||
const node = store.get(hash);
|
||||
if (node && node.type === hash) {
|
||||
reachable.add(hash);
|
||||
}
|
||||
}
|
||||
|
||||
// Count total nodes
|
||||
const total = allHashes.length;
|
||||
|
||||
// Sweep phase: delete unmarked nodes
|
||||
let collected = 0;
|
||||
for (const hash of allHashes) {
|
||||
if (!reachable.has(hash)) {
|
||||
store.delete(hash);
|
||||
collected++;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
total,
|
||||
reachable: reachable.size,
|
||||
collected,
|
||||
scanned,
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,7 @@ export { bootstrap } from "./bootstrap.js";
|
||||
export type { BootstrapCapableStore } from "./bootstrap-capable.js";
|
||||
export { BOOTSTRAP_STORE } from "./bootstrap-capable.js";
|
||||
export { cborEncode } from "./cbor.js";
|
||||
export { type GcStats, gc } from "./gc.js";
|
||||
export { computeHash, computeSelfHash } from "./hash.js";
|
||||
export type { JSONSchema } from "./schema.js";
|
||||
export {
|
||||
@@ -14,12 +15,14 @@ export {
|
||||
} from "./schema.js";
|
||||
export { createMemoryStore } from "./store.js";
|
||||
export type { CasNode, Hash, Store } from "./types.js";
|
||||
export type { Variable, VariableId } from "./variable.js";
|
||||
export type { Variable } from "./variable.js";
|
||||
export {
|
||||
CasNodeNotFoundError,
|
||||
createVariableStore,
|
||||
InvalidScopeError,
|
||||
InvalidTagFormatError,
|
||||
InvalidVariableNameError,
|
||||
SchemaMismatchError,
|
||||
TagLabelConflictError,
|
||||
VariableNotFoundError,
|
||||
VariableStore,
|
||||
} from "./variable-store.js";
|
||||
|
||||
@@ -27,6 +27,14 @@ export class MemStore implements BootstrapCapableStore {
|
||||
return this.#inner.listByType(typeHash);
|
||||
}
|
||||
|
||||
listAll(): Hash[] {
|
||||
return this.#inner.listAll();
|
||||
}
|
||||
|
||||
delete(hash: Hash): void {
|
||||
this.#inner.delete(hash);
|
||||
}
|
||||
|
||||
[BOOTSTRAP_STORE](payload: unknown): Promise<Hash> {
|
||||
return this.#inner[BOOTSTRAP_STORE](payload);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,25 @@ export function createMemoryStore(): BootstrapCapableStore {
|
||||
return set ? [...set] : [];
|
||||
},
|
||||
|
||||
listAll(): Hash[] {
|
||||
return Array.from(data.keys());
|
||||
},
|
||||
|
||||
delete(hash: Hash): void {
|
||||
const node = data.get(hash);
|
||||
if (node) {
|
||||
data.delete(hash);
|
||||
// Remove from type index
|
||||
const set = byType.get(node.type);
|
||||
if (set) {
|
||||
set.delete(hash);
|
||||
if (set.size === 0) {
|
||||
byType.delete(node.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
[BOOTSTRAP_STORE]: putSelfReferencing,
|
||||
};
|
||||
|
||||
|
||||
@@ -24,4 +24,6 @@ export type Store = {
|
||||
get(hash: Hash): CasNode | null;
|
||||
has(hash: Hash): boolean;
|
||||
listByType(typeHash: Hash): Hash[];
|
||||
listAll(): Hash[];
|
||||
delete(hash: Hash): void;
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,30 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { ulid } from "ulidx";
|
||||
import type { Store } from "./types.js";
|
||||
import type { Variable, VariableId } from "./variable.js";
|
||||
import type { Hash, Store } from "./types.js";
|
||||
import type { Variable } from "./variable.js";
|
||||
|
||||
/**
|
||||
* Custom error types for variable operations
|
||||
*/
|
||||
export class VariableNotFoundError extends Error {
|
||||
constructor(id: VariableId) {
|
||||
super(`Variable not found: ${id}`);
|
||||
constructor(
|
||||
public variableName: string,
|
||||
public variableSchema: Hash,
|
||||
) {
|
||||
super(`Variable not found: name=${variableName}, schema=${variableSchema}`);
|
||||
this.name = "VariableNotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidVariableNameError extends Error {
|
||||
constructor(
|
||||
public variableName: string,
|
||||
public reason: string,
|
||||
) {
|
||||
super(`Invalid variable name "${variableName}": ${reason}`);
|
||||
this.name = "InvalidVariableNameError";
|
||||
}
|
||||
}
|
||||
|
||||
export class SchemaMismatchError extends Error {
|
||||
constructor(
|
||||
public expected: string,
|
||||
@@ -23,13 +35,6 @@ export class SchemaMismatchError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidScopeError extends Error {
|
||||
constructor(scope: string) {
|
||||
super(`Invalid scope: scope must end with / (got: ${scope})`);
|
||||
this.name = "InvalidScopeError";
|
||||
}
|
||||
}
|
||||
|
||||
export class CasNodeNotFoundError extends Error {
|
||||
constructor(hash: string) {
|
||||
super(`CAS node not found: ${hash}`);
|
||||
@@ -37,6 +42,24 @@ export class CasNodeNotFoundError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class TagLabelConflictError extends Error {
|
||||
constructor(
|
||||
public conflictName: string,
|
||||
public existingType: "tag" | "label",
|
||||
public attemptedType: "tag" | "label",
|
||||
) {
|
||||
super(`Conflict: '${conflictName}' already exists as a ${existingType}`);
|
||||
this.name = "TagLabelConflictError";
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidTagFormatError extends Error {
|
||||
constructor(tag: string) {
|
||||
super(`Invalid tag format: ${tag}`);
|
||||
this.name = "InvalidTagFormatError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Variable store with SQLite backend
|
||||
*/
|
||||
@@ -48,32 +71,91 @@ export class VariableStore {
|
||||
private casStore: Store,
|
||||
) {
|
||||
this.db = new Database(dbPath, { create: true });
|
||||
// Enable foreign keys
|
||||
this.db.exec("PRAGMA foreign_keys = ON");
|
||||
this.initDb();
|
||||
}
|
||||
|
||||
private initDb(): void {
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS variables (
|
||||
id TEXT PRIMARY KEY,
|
||||
scope TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
schema TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
created INTEGER NOT NULL,
|
||||
updated INTEGER NOT NULL
|
||||
updated INTEGER NOT NULL,
|
||||
PRIMARY KEY (name, schema)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_var_scope ON variables(scope);
|
||||
CREATE INDEX IF NOT EXISTS idx_var_name ON variables(name);
|
||||
CREATE INDEX IF NOT EXISTS idx_var_value ON variables(value);
|
||||
CREATE INDEX IF NOT EXISTS idx_var_schema ON variables(schema);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS variable_tags (
|
||||
variable_name TEXT NOT NULL,
|
||||
variable_schema TEXT NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
PRIMARY KEY (variable_name, variable_schema, key),
|
||||
FOREIGN KEY (variable_name, variable_schema) REFERENCES variables(name, schema) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS variable_labels (
|
||||
variable_name TEXT NOT NULL,
|
||||
variable_schema TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
PRIMARY KEY (variable_name, variable_schema, name),
|
||||
FOREIGN KEY (variable_name, variable_schema) REFERENCES variables(name, schema) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_var_tag_key ON variable_tags(key);
|
||||
CREATE INDEX IF NOT EXISTS idx_var_tag_key_value ON variable_tags(key, value);
|
||||
CREATE INDEX IF NOT EXISTS idx_var_label_name ON variable_labels(name);
|
||||
`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that scope ends with /
|
||||
* Validate variable name format
|
||||
*/
|
||||
private validateScope(scope: string): void {
|
||||
if (!scope.endsWith("/")) {
|
||||
throw new InvalidScopeError(scope);
|
||||
private validateName(name: string): void {
|
||||
// Rule 1: Cannot be empty
|
||||
if (name === "") {
|
||||
throw new InvalidVariableNameError(name, "Name cannot be empty");
|
||||
}
|
||||
|
||||
// Rule 2: No leading slash
|
||||
if (name.startsWith("/")) {
|
||||
throw new InvalidVariableNameError(
|
||||
name,
|
||||
"Name cannot start with leading slash",
|
||||
);
|
||||
}
|
||||
|
||||
// Rule 3: No trailing slash
|
||||
if (name.endsWith("/")) {
|
||||
throw new InvalidVariableNameError(
|
||||
name,
|
||||
"Name cannot end with trailing slash",
|
||||
);
|
||||
}
|
||||
|
||||
// Rule 4: Each segment must match [a-zA-Z0-9._-]+ and no empty segments
|
||||
const segments = name.split("/");
|
||||
for (const segment of segments) {
|
||||
if (segment === "") {
|
||||
throw new InvalidVariableNameError(
|
||||
name,
|
||||
"Name contains empty segment (consecutive slashes //)",
|
||||
);
|
||||
}
|
||||
|
||||
// Check for invalid characters
|
||||
if (!/^[a-zA-Z0-9._-]+$/.test(segment)) {
|
||||
throw new InvalidVariableNameError(
|
||||
name,
|
||||
`Segment "${segment}" contains invalid characters (only a-z, A-Z, 0-9, ., _, - allowed)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,48 +171,230 @@ export class VariableStore {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new variable
|
||||
* Load tags for a variable
|
||||
*/
|
||||
create(scope: string, value: string): Variable {
|
||||
this.validateScope(scope);
|
||||
const schema = this.extractSchema(value);
|
||||
|
||||
const id = ulid();
|
||||
const now = Date.now();
|
||||
|
||||
private loadTags(name: string, schema: Hash): Record<string, string> {
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT INTO variables (id, scope, value, schema, created, updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
SELECT key, value
|
||||
FROM variable_tags
|
||||
WHERE variable_name = ? AND variable_schema = ?
|
||||
`);
|
||||
|
||||
stmt.run(id, scope, value, schema, now, now);
|
||||
const rows = stmt.all(name, schema) as Array<{
|
||||
key: string;
|
||||
value: string;
|
||||
}>;
|
||||
const tags: Record<string, string> = {};
|
||||
for (const row of rows) {
|
||||
tags[row.key] = row.value;
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load labels for a variable
|
||||
*/
|
||||
private loadLabels(name: string, schema: Hash): string[] {
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT name
|
||||
FROM variable_labels
|
||||
WHERE variable_name = ? AND variable_schema = ?
|
||||
ORDER BY name ASC
|
||||
`);
|
||||
|
||||
const rows = stmt.all(name, schema) as Array<{ name: string }>;
|
||||
return rows.map((row) => row.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a variable (upsert: create or update)
|
||||
*/
|
||||
set(
|
||||
name: string,
|
||||
value: string,
|
||||
options?: {
|
||||
tags?: Record<string, string>;
|
||||
labels?: string[];
|
||||
},
|
||||
): Variable {
|
||||
// Validate name format
|
||||
this.validateName(name);
|
||||
|
||||
const schema = this.extractSchema(value);
|
||||
|
||||
// Check if variable exists
|
||||
const existing = this.get(name, schema);
|
||||
|
||||
if (existing !== null) {
|
||||
// Update existing variable
|
||||
const now = Date.now();
|
||||
|
||||
// If options provided, use them; otherwise preserve existing
|
||||
const tags = options?.tags ?? existing.tags;
|
||||
const labels = options?.labels ?? existing.labels;
|
||||
|
||||
// Check for tag/label conflicts when updating with new options
|
||||
if (options !== undefined) {
|
||||
const tagKeys = Object.keys(tags);
|
||||
for (const key of tagKeys) {
|
||||
if (labels.includes(key)) {
|
||||
throw new TagLabelConflictError(key, "label", "tag");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.db.exec("BEGIN TRANSACTION");
|
||||
|
||||
try {
|
||||
// Update value and timestamp
|
||||
const updateStmt = this.db.prepare(`
|
||||
UPDATE variables
|
||||
SET value = ?, updated = ?
|
||||
WHERE name = ? AND schema = ?
|
||||
`);
|
||||
updateStmt.run(value, now, name, schema);
|
||||
|
||||
// If options provided, update tags/labels
|
||||
if (options !== undefined) {
|
||||
// Delete existing tags and labels
|
||||
this.db
|
||||
.prepare(`
|
||||
DELETE FROM variable_tags WHERE variable_name = ? AND variable_schema = ?
|
||||
`)
|
||||
.run(name, schema);
|
||||
|
||||
this.db
|
||||
.prepare(`
|
||||
DELETE FROM variable_labels WHERE variable_name = ? AND variable_schema = ?
|
||||
`)
|
||||
.run(name, schema);
|
||||
|
||||
// Insert new tags
|
||||
const tagKeys = Object.keys(tags);
|
||||
if (tagKeys.length > 0) {
|
||||
const tagStmt = this.db.prepare(`
|
||||
INSERT INTO variable_tags (variable_name, variable_schema, key, value)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`);
|
||||
for (const [key, val] of Object.entries(tags)) {
|
||||
tagStmt.run(name, schema, key, val);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert new labels
|
||||
if (labels.length > 0) {
|
||||
const labelStmt = this.db.prepare(`
|
||||
INSERT INTO variable_labels (variable_name, variable_schema, name)
|
||||
VALUES (?, ?, ?)
|
||||
`);
|
||||
for (const labelName of labels) {
|
||||
labelStmt.run(name, schema, labelName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.db.exec("COMMIT");
|
||||
} catch (e) {
|
||||
this.db.exec("ROLLBACK");
|
||||
throw e;
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
schema,
|
||||
value,
|
||||
created: existing.created,
|
||||
updated: now,
|
||||
tags,
|
||||
labels: [...labels],
|
||||
};
|
||||
}
|
||||
|
||||
// Create new variable
|
||||
const tags = options?.tags ?? {};
|
||||
const labels = options?.labels ?? [];
|
||||
|
||||
// Check for tag/label conflicts
|
||||
const tagKeys = Object.keys(tags);
|
||||
for (const key of tagKeys) {
|
||||
if (labels.includes(key)) {
|
||||
throw new TagLabelConflictError(key, "label", "tag");
|
||||
}
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
this.db.exec("BEGIN TRANSACTION");
|
||||
|
||||
try {
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT INTO variables (name, schema, value, created, updated)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
stmt.run(name, schema, value, now, now);
|
||||
|
||||
// Insert tags
|
||||
if (tagKeys.length > 0) {
|
||||
const tagStmt = this.db.prepare(`
|
||||
INSERT INTO variable_tags (variable_name, variable_schema, key, value)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`);
|
||||
for (const [key, val] of Object.entries(tags)) {
|
||||
tagStmt.run(name, schema, key, val);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert labels
|
||||
if (labels.length > 0) {
|
||||
const labelStmt = this.db.prepare(`
|
||||
INSERT INTO variable_labels (variable_name, variable_schema, name)
|
||||
VALUES (?, ?, ?)
|
||||
`);
|
||||
for (const labelName of labels) {
|
||||
labelStmt.run(name, schema, labelName);
|
||||
}
|
||||
}
|
||||
|
||||
this.db.exec("COMMIT");
|
||||
} catch (e) {
|
||||
this.db.exec("ROLLBACK");
|
||||
throw e;
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
scope,
|
||||
value,
|
||||
name,
|
||||
schema,
|
||||
value,
|
||||
created: now,
|
||||
updated: now,
|
||||
tags,
|
||||
labels: [...labels],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a variable by ID
|
||||
* Get a variable by name, optionally with schema
|
||||
*/
|
||||
get(id: VariableId): Variable | null {
|
||||
/**
|
||||
* Get a variable by name and schema
|
||||
* @param name - Variable name
|
||||
* @param schema - Schema hash (required)
|
||||
* @returns Variable if found, null otherwise
|
||||
*/
|
||||
get(name: string, schema: Hash): Variable | null {
|
||||
// Precise match with schema
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, scope, value, schema, created, updated
|
||||
SELECT name, schema, value, created, updated
|
||||
FROM variables
|
||||
WHERE id = ?
|
||||
WHERE name = ? AND schema = ?
|
||||
`);
|
||||
|
||||
const row = stmt.get(id) as
|
||||
const row = stmt.get(name, schema) as
|
||||
| {
|
||||
id: string;
|
||||
scope: string;
|
||||
value: string;
|
||||
name: string;
|
||||
schema: string;
|
||||
value: string;
|
||||
created: number;
|
||||
updated: number;
|
||||
}
|
||||
@@ -141,23 +405,30 @@ export class VariableStore {
|
||||
return null;
|
||||
}
|
||||
|
||||
const tags = this.loadTags(row.name, row.schema);
|
||||
const labels = this.loadLabels(row.name, row.schema);
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
scope: row.scope,
|
||||
value: row.value,
|
||||
name: row.name,
|
||||
schema: row.schema,
|
||||
value: row.value,
|
||||
created: row.created,
|
||||
updated: row.updated,
|
||||
tags,
|
||||
labels,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a variable's value (with schema validation)
|
||||
*/
|
||||
update(id: VariableId, value: string): Variable {
|
||||
const existing = this.get(id);
|
||||
update(name: string, schema: Hash, value: string): Variable {
|
||||
// Validate name format
|
||||
this.validateName(name);
|
||||
|
||||
const existing = this.get(name, schema);
|
||||
if (existing === null) {
|
||||
throw new VariableNotFoundError(id);
|
||||
throw new VariableNotFoundError(name, schema);
|
||||
}
|
||||
|
||||
const newSchema = this.extractSchema(value);
|
||||
@@ -170,10 +441,10 @@ export class VariableStore {
|
||||
const stmt = this.db.prepare(`
|
||||
UPDATE variables
|
||||
SET value = ?, updated = ?
|
||||
WHERE id = ?
|
||||
WHERE name = ? AND schema = ?
|
||||
`);
|
||||
|
||||
stmt.run(value, now, id);
|
||||
stmt.run(value, now, name, schema);
|
||||
|
||||
return {
|
||||
...existing,
|
||||
@@ -183,60 +454,252 @@ export class VariableStore {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a variable
|
||||
* Remove a variable (or all variants if schema omitted)
|
||||
*/
|
||||
delete(id: VariableId): Variable {
|
||||
const existing = this.get(id);
|
||||
if (existing === null) {
|
||||
throw new VariableNotFoundError(id);
|
||||
remove(name: string): Variable[];
|
||||
remove(name: string, schema: Hash): Variable;
|
||||
remove(name: string, schema?: Hash): Variable | Variable[] {
|
||||
if (schema !== undefined) {
|
||||
// Remove specific (name, schema) variant
|
||||
const existing = this.get(name, schema);
|
||||
if (existing === null) {
|
||||
throw new VariableNotFoundError(name, schema);
|
||||
}
|
||||
|
||||
const stmt = this.db.prepare(`
|
||||
DELETE FROM variables WHERE name = ? AND schema = ?
|
||||
`);
|
||||
|
||||
stmt.run(name, schema);
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
// Remove all schema variants for this name
|
||||
const variants = this.list({ exactName: name });
|
||||
|
||||
if (variants.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const stmt = this.db.prepare(`
|
||||
DELETE FROM variables WHERE id = ?
|
||||
DELETE FROM variables WHERE name = ?
|
||||
`);
|
||||
|
||||
stmt.run(id);
|
||||
stmt.run(name);
|
||||
|
||||
return existing;
|
||||
return variants;
|
||||
}
|
||||
|
||||
/**
|
||||
* List variables matching a scope prefix
|
||||
* List variables with optional filters
|
||||
*/
|
||||
list(options?: { scope?: string }): Variable[] {
|
||||
const scope = options?.scope ?? "";
|
||||
|
||||
// Validate scope format (must end with / if non-empty)
|
||||
if (scope !== "" && !scope.endsWith("/")) {
|
||||
throw new InvalidScopeError(scope);
|
||||
list(options?: {
|
||||
namePrefix?: string;
|
||||
exactName?: string;
|
||||
schema?: Hash;
|
||||
tags?: Record<string, string>;
|
||||
labels?: string[];
|
||||
}): Variable[] {
|
||||
// Validate mutually exclusive options
|
||||
if (options?.namePrefix !== undefined && options?.exactName !== undefined) {
|
||||
throw new Error(
|
||||
"namePrefix and exactName are mutually exclusive - cannot specify both",
|
||||
);
|
||||
}
|
||||
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, scope, value, schema, created, updated
|
||||
FROM variables
|
||||
WHERE scope LIKE ? || '%'
|
||||
ORDER BY created ASC
|
||||
`);
|
||||
const namePrefix = options?.namePrefix ?? "";
|
||||
const exactName = options?.exactName;
|
||||
const schema = options?.schema;
|
||||
const filterTags = options?.tags ?? {};
|
||||
const filterLabels = options?.labels ?? [];
|
||||
|
||||
const rows = stmt.all(scope) as Array<{
|
||||
id: string;
|
||||
scope: string;
|
||||
value: string;
|
||||
// Build query with filters
|
||||
let query = `
|
||||
SELECT DISTINCT v.name, v.schema, v.value, v.created, v.updated
|
||||
FROM variables v
|
||||
`;
|
||||
|
||||
const params: (string | number)[] = [];
|
||||
|
||||
// Tag filters (AND logic)
|
||||
const tagKeys = Object.keys(filterTags);
|
||||
for (let i = 0; i < tagKeys.length; i++) {
|
||||
const key = tagKeys[i] as string;
|
||||
const value = filterTags[key] as string;
|
||||
query += `
|
||||
INNER JOIN variable_tags t${i} ON v.name = t${i}.variable_name
|
||||
AND v.schema = t${i}.variable_schema
|
||||
AND t${i}.key = ? AND t${i}.value = ?
|
||||
`;
|
||||
params.push(key, value);
|
||||
}
|
||||
|
||||
// Label filters (AND logic)
|
||||
for (let i = 0; i < filterLabels.length; i++) {
|
||||
const label = filterLabels[i] as string;
|
||||
query += `
|
||||
INNER JOIN variable_labels l${i} ON v.name = l${i}.variable_name
|
||||
AND v.schema = l${i}.variable_schema
|
||||
AND l${i}.name = ?
|
||||
`;
|
||||
params.push(label);
|
||||
}
|
||||
|
||||
// WHERE clause for name filters and schema
|
||||
const whereClauses: string[] = [];
|
||||
|
||||
if (exactName !== undefined) {
|
||||
whereClauses.push("v.name = ?");
|
||||
params.push(exactName);
|
||||
} else if (namePrefix !== "") {
|
||||
whereClauses.push("v.name LIKE ? || '%'");
|
||||
params.push(namePrefix);
|
||||
}
|
||||
|
||||
if (schema !== undefined) {
|
||||
whereClauses.push("v.schema = ?");
|
||||
params.push(schema);
|
||||
}
|
||||
|
||||
if (whereClauses.length > 0) {
|
||||
query += ` WHERE ${whereClauses.join(" AND ")}`;
|
||||
}
|
||||
|
||||
query += " ORDER BY v.created ASC";
|
||||
|
||||
const stmt = this.db.prepare(query);
|
||||
const rows = stmt.all(...params) as Array<{
|
||||
name: string;
|
||||
schema: string;
|
||||
value: string;
|
||||
created: number;
|
||||
updated: number;
|
||||
}>;
|
||||
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
scope: row.scope,
|
||||
value: row.value,
|
||||
name: row.name,
|
||||
schema: row.schema,
|
||||
value: row.value,
|
||||
created: row.created,
|
||||
updated: row.updated,
|
||||
tags: this.loadTags(row.name, row.schema),
|
||||
labels: this.loadLabels(row.name, row.schema),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add/update/delete tags and labels
|
||||
*/
|
||||
tag(
|
||||
name: string,
|
||||
schema: Hash,
|
||||
operations: {
|
||||
add?: Record<string, string>; // tags to add/update
|
||||
addLabels?: string[]; // labels to add
|
||||
delete?: string[]; // tag keys or label names to delete
|
||||
},
|
||||
): Variable {
|
||||
// Validate name format
|
||||
this.validateName(name);
|
||||
|
||||
const existing = this.get(name, schema);
|
||||
if (existing === null) {
|
||||
throw new VariableNotFoundError(name, schema);
|
||||
}
|
||||
|
||||
const addTags = operations.add ?? {};
|
||||
const addLabels = operations.addLabels ?? [];
|
||||
const deleteNames = operations.delete ?? [];
|
||||
|
||||
// Check for conflicts between tags and labels
|
||||
const newTagKeys = Object.keys(addTags);
|
||||
for (const key of newTagKeys) {
|
||||
// Check if this key is being added as a label in the same operation
|
||||
if (addLabels.includes(key)) {
|
||||
throw new TagLabelConflictError(key, "label", "tag");
|
||||
}
|
||||
// Check if this key already exists as a label (and not being deleted)
|
||||
if (existing.labels.includes(key) && !deleteNames.includes(key)) {
|
||||
throw new TagLabelConflictError(key, "label", "tag");
|
||||
}
|
||||
}
|
||||
|
||||
for (const labelName of addLabels) {
|
||||
// Check if this name is being added as a tag in the same operation
|
||||
if (newTagKeys.includes(labelName)) {
|
||||
throw new TagLabelConflictError(labelName, "tag", "label");
|
||||
}
|
||||
// Check if this name already exists as a tag key (and not being deleted)
|
||||
if (
|
||||
existing.tags[labelName] !== undefined &&
|
||||
!deleteNames.includes(labelName)
|
||||
) {
|
||||
throw new TagLabelConflictError(labelName, "tag", "label");
|
||||
}
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
this.db.exec("BEGIN TRANSACTION");
|
||||
|
||||
try {
|
||||
// Update timestamp
|
||||
const updateStmt = this.db.prepare(`
|
||||
UPDATE variables SET updated = ? WHERE name = ? AND schema = ?
|
||||
`);
|
||||
updateStmt.run(now, name, schema);
|
||||
|
||||
// Delete tags and labels
|
||||
if (deleteNames.length > 0) {
|
||||
const deleteTagStmt = this.db.prepare(`
|
||||
DELETE FROM variable_tags WHERE variable_name = ? AND variable_schema = ? AND key = ?
|
||||
`);
|
||||
const deleteLabelStmt = this.db.prepare(`
|
||||
DELETE FROM variable_labels WHERE variable_name = ? AND variable_schema = ? AND name = ?
|
||||
`);
|
||||
for (const deleteName of deleteNames) {
|
||||
deleteTagStmt.run(name, schema, deleteName);
|
||||
deleteLabelStmt.run(name, schema, deleteName);
|
||||
}
|
||||
}
|
||||
|
||||
// Add or update tags
|
||||
if (newTagKeys.length > 0) {
|
||||
const tagStmt = this.db.prepare(`
|
||||
INSERT OR REPLACE INTO variable_tags (variable_name, variable_schema, key, value)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`);
|
||||
for (const [key, value] of Object.entries(addTags)) {
|
||||
tagStmt.run(name, schema, key, value);
|
||||
}
|
||||
}
|
||||
|
||||
// Add labels (with conflict handling)
|
||||
if (addLabels.length > 0) {
|
||||
const labelStmt = this.db.prepare(`
|
||||
INSERT OR IGNORE INTO variable_labels (variable_name, variable_schema, name)
|
||||
VALUES (?, ?, ?)
|
||||
`);
|
||||
for (const labelName of addLabels) {
|
||||
labelStmt.run(name, schema, labelName);
|
||||
}
|
||||
}
|
||||
|
||||
this.db.exec("COMMIT");
|
||||
} catch (e) {
|
||||
this.db.exec("ROLLBACK");
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Return updated variable
|
||||
const updated = this.get(name, schema);
|
||||
if (updated === null) {
|
||||
throw new VariableNotFoundError(name, schema);
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the database connection
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { Variable } from "./variable.js";
|
||||
|
||||
describe("Variable Type", () => {
|
||||
test("Variable type uses (name, schema) composite key", () => {
|
||||
const variable: Variable = {
|
||||
name: "config",
|
||||
schema: "ABC123DEF4567",
|
||||
value: "XYZ789GHI0123",
|
||||
created: 1234567890000,
|
||||
updated: 1234567890000,
|
||||
tags: { env: "prod" },
|
||||
labels: ["critical"],
|
||||
};
|
||||
|
||||
expect(variable.name).toBe("config");
|
||||
expect(variable.schema).toBe("ABC123DEF4567");
|
||||
// id and scope should not exist
|
||||
expect((variable as unknown as { id?: unknown }).id).toBeUndefined();
|
||||
expect((variable as unknown as { scope?: unknown }).scope).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +1,15 @@
|
||||
import type { Hash } from "./types.js";
|
||||
|
||||
/**
|
||||
* ULID identifier (26-character Crockford Base32)
|
||||
*/
|
||||
export type VariableId = string;
|
||||
|
||||
/**
|
||||
* Variable: mutable binding to an immutable CAS node
|
||||
* Identified by composite key (name, schema)
|
||||
*/
|
||||
export type Variable = {
|
||||
id: VariableId;
|
||||
scope: string; // hierarchical path, must end with /
|
||||
name: string; // variable name (unique per schema)
|
||||
schema: Hash; // schema hash (part of composite key)
|
||||
value: Hash; // CAS node hash
|
||||
schema: Hash; // extracted from value's CAS node.type
|
||||
created: number; // epoch ms
|
||||
updated: number; // epoch ms
|
||||
tags: Record<string, string>; // key-value pairs
|
||||
labels: string[]; // bare identifiers
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user