@stigmer/cli 3.19.0 → 3.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/commands/apply.d.ts.map +1 -1
- package/commands/apply.js +42 -100
- package/commands/apply.js.map +1 -1
- package/commands/validate.js +3 -4
- package/commands/validate.js.map +1 -1
- package/local/bootstrap.d.ts +1 -2
- package/local/bootstrap.d.ts.map +1 -1
- package/local/bootstrap.js +13 -41
- package/local/bootstrap.js.map +1 -1
- package/local/daemon/launch.d.ts +2 -2
- package/local/daemon/launch.d.ts.map +1 -1
- package/local/daemon/launch.js +2 -2
- package/local/daemon/launch.js.map +1 -1
- package/local/plugins/defaults.js +2 -2
- package/local/plugins/defaults.js.map +1 -1
- package/package.json +7 -7
- package/registry/index.d.ts +2 -2
- package/registry/index.d.ts.map +1 -1
- package/registry/index.js +2 -2
- package/registry/index.js.map +1 -1
- package/registry/metadata.d.ts +9 -0
- package/registry/metadata.d.ts.map +1 -1
- package/registry/metadata.js +14 -5
- package/registry/metadata.js.map +1 -1
- package/registry/registry.d.ts +9 -0
- package/registry/registry.d.ts.map +1 -1
- package/registry/registry.js +16 -1
- package/registry/registry.js.map +1 -1
- package/registry/verb-support.d.ts.map +1 -1
- package/registry/verb-support.js +0 -13
- package/registry/verb-support.js.map +1 -1
- package/resources/apply/apply.d.ts +11 -12
- package/resources/apply/apply.d.ts.map +1 -1
- package/resources/apply/apply.js +12 -21
- package/resources/apply/apply.js.map +1 -1
- package/resources/apply/handlers.js +2 -2
- package/resources/apply/handlers.js.map +1 -1
- package/resources/delete.d.ts.map +1 -1
- package/resources/delete.js +0 -1
- package/resources/delete.js.map +1 -1
- package/resources/get-bindings.d.ts.map +1 -1
- package/resources/get-bindings.js +0 -5
- package/resources/get-bindings.js.map +1 -1
- package/resources/list.d.ts.map +1 -1
- package/resources/list.js +0 -1
- package/resources/list.js.map +1 -1
- package/resources/validate.d.ts.map +1 -1
- package/resources/validate.js +0 -2
- package/resources/validate.js.map +1 -1
- package/src/commands/apply.ts +35 -150
- package/src/commands/validate.ts +2 -4
- package/src/local/bootstrap.test.ts +15 -59
- package/src/local/bootstrap.ts +12 -53
- package/src/local/daemon/launch.ts +4 -4
- package/src/local/plugins/defaults.ts +2 -2
- package/src/registry/index.ts +7 -1
- package/src/registry/metadata.ts +15 -5
- package/src/registry/registry.test.ts +1 -7
- package/src/registry/registry.ts +17 -1
- package/src/registry/verb-support.ts +0 -13
- package/src/resources/apply/apply.integration.test.ts +15 -3
- package/src/resources/apply/apply.ts +15 -26
- package/src/resources/apply/handlers.ts +2 -2
- package/src/resources/delete.ts +0 -1
- package/src/resources/get-bindings.ts +0 -5
- package/src/resources/list.ts +0 -1
- package/src/resources/validate.test.ts +0 -1
- package/src/resources/validate.ts +0 -2
- package/local/seedpack-retire.d.ts +0 -63
- package/local/seedpack-retire.d.ts.map +0 -1
- package/local/seedpack-retire.js +0 -361
- package/local/seedpack-retire.js.map +0 -1
- package/resources/apply/declarative.d.ts +0 -106
- package/resources/apply/declarative.d.ts.map +0 -1
- package/resources/apply/declarative.js +0 -397
- package/resources/apply/declarative.js.map +0 -1
- package/resources/apply/synth/project-track.d.ts +0 -21
- package/resources/apply/synth/project-track.d.ts.map +0 -1
- package/resources/apply/synth/project-track.js +0 -102
- package/resources/apply/synth/project-track.js.map +0 -1
- package/resources/apply/synth/reader.d.ts +0 -18
- package/resources/apply/synth/reader.d.ts.map +0 -1
- package/resources/apply/synth/reader.js +0 -55
- package/resources/apply/synth/reader.js.map +0 -1
- package/resources/apply/synth/runtime.d.ts +0 -8
- package/resources/apply/synth/runtime.d.ts.map +0 -1
- package/resources/apply/synth/runtime.js +0 -32
- package/resources/apply/synth/runtime.js.map +0 -1
- package/resources/apply/synth/synthesize.d.ts +0 -43
- package/resources/apply/synth/synthesize.d.ts.map +0 -1
- package/resources/apply/synth/synthesize.js +0 -115
- package/resources/apply/synth/synthesize.js.map +0 -1
- package/src/local/seedpack-retire.test.ts +0 -499
- package/src/local/seedpack-retire.ts +0 -474
- package/src/resources/apply/declarative.integration.test.ts +0 -204
- package/src/resources/apply/declarative.test.ts +0 -132
- package/src/resources/apply/declarative.ts +0 -491
- package/src/resources/apply/synth/capstone.integration.test.ts +0 -134
- package/src/resources/apply/synth/project-track.integration.test.ts +0 -194
- package/src/resources/apply/synth/project-track.ts +0 -126
- package/src/resources/apply/synth/reader.test.ts +0 -90
- package/src/resources/apply/synth/reader.ts +0 -68
- package/src/resources/apply/synth/runtime.test.ts +0 -29
- package/src/resources/apply/synth/runtime.ts +0 -39
- package/src/resources/apply/synth/synthesize.test.ts +0 -109
- package/src/resources/apply/synth/synthesize.ts +0 -161
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
// Unit tests for declarative-mode detection, scanning, and item building —
|
|
2
|
-
// the pure, filesystem-driven logic that does not need a backend.
|
|
3
|
-
|
|
4
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
-
import { tmpdir } from "node:os";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
import { describe, expect, it } from "vitest";
|
|
8
|
-
import { buildDeclarativeItems, detectTrack, scanResourceFiles, scanSkillDirectories } from "./declarative.js";
|
|
9
|
-
|
|
10
|
-
function project(dir: string, body: string): void {
|
|
11
|
-
writeFileSync(join(dir, "stigmer.yaml"), body);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const DECLARATIVE_PROJECT = ["kind: Project", "metadata:", " name: Demo", " slug: demo", "spec:", " description: d", ""].join(
|
|
15
|
-
"\n",
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
describe("detectTrack", () => {
|
|
19
|
-
it("returns atomic when no stigmer.yaml is found", () => {
|
|
20
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
21
|
-
try {
|
|
22
|
-
// A deep child with no config anywhere up to the temp root quickly.
|
|
23
|
-
expect(detectTrack(dir, 2).track).toBe("atomic");
|
|
24
|
-
} finally {
|
|
25
|
-
rmSync(dir, { recursive: true, force: true });
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it("detects the declarative track (stigmer.yaml without entry_point)", () => {
|
|
30
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
31
|
-
try {
|
|
32
|
-
project(dir, DECLARATIVE_PROJECT);
|
|
33
|
-
const detect = detectTrack(dir);
|
|
34
|
-
expect(detect.track).toBe("declarative");
|
|
35
|
-
expect(detect.project?.metadata?.slug).toBe("demo");
|
|
36
|
-
expect(detect.configDir).toBe(dir);
|
|
37
|
-
} finally {
|
|
38
|
-
rmSync(dir, { recursive: true, force: true });
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("detects the project track when entry_point is set", () => {
|
|
43
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
44
|
-
try {
|
|
45
|
-
project(dir, ["kind: Project", "metadata:", " name: P", " slug: p", "spec:", " entryPoint: main.ts", ""].join("\n"));
|
|
46
|
-
expect(detectTrack(dir).track).toBe("project");
|
|
47
|
-
} finally {
|
|
48
|
-
rmSync(dir, { recursive: true, force: true });
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("walks up to a parent stigmer.yaml", () => {
|
|
53
|
-
const root = mkdtempSync(join(tmpdir(), "decl-"));
|
|
54
|
-
try {
|
|
55
|
-
project(root, DECLARATIVE_PROJECT);
|
|
56
|
-
const child = join(root, "a", "b");
|
|
57
|
-
mkdirSync(child, { recursive: true });
|
|
58
|
-
const detect = detectTrack(child);
|
|
59
|
-
expect(detect.track).toBe("declarative");
|
|
60
|
-
expect(detect.configDir).toBe(root);
|
|
61
|
-
} finally {
|
|
62
|
-
rmSync(root, { recursive: true, force: true });
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("rejects an invalid stigmer.yaml rather than falling back to atomic", () => {
|
|
67
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
68
|
-
try {
|
|
69
|
-
project(dir, ["kind: Project", "spec:", " bogusField: nope", ""].join("\n"));
|
|
70
|
-
expect(() => detectTrack(dir)).toThrow(/invalid project configuration/);
|
|
71
|
-
} finally {
|
|
72
|
-
rmSync(dir, { recursive: true, force: true });
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe("scanResourceFiles", () => {
|
|
78
|
-
it("collects top-level and one-level-deep YAML, excluding stigmer.yaml and skills", () => {
|
|
79
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
80
|
-
try {
|
|
81
|
-
project(dir, DECLARATIVE_PROJECT);
|
|
82
|
-
writeFileSync(join(dir, "agent.yaml"), "kind: Agent\n");
|
|
83
|
-
mkdirSync(join(dir, "workflows"));
|
|
84
|
-
writeFileSync(join(dir, "workflows", "wf.yaml"), "kind: Workflow\n");
|
|
85
|
-
// a skill dir must be excluded from YAML scanning
|
|
86
|
-
mkdirSync(join(dir, "my-skill"));
|
|
87
|
-
writeFileSync(join(dir, "my-skill", "SKILL.md"), "---\nname: my-skill\n---\n");
|
|
88
|
-
writeFileSync(join(dir, "my-skill", "config.yaml"), "kind: Agent\n");
|
|
89
|
-
|
|
90
|
-
const files = scanResourceFiles(dir).map((f) => f.replace(`${dir}/`, ""));
|
|
91
|
-
expect(files).toEqual(["agent.yaml", "workflows/wf.yaml"]);
|
|
92
|
-
} finally {
|
|
93
|
-
rmSync(dir, { recursive: true, force: true });
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe("scanSkillDirectories", () => {
|
|
99
|
-
it("finds flat and organized skill directories", () => {
|
|
100
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
101
|
-
try {
|
|
102
|
-
mkdirSync(join(dir, "flat-skill"));
|
|
103
|
-
writeFileSync(join(dir, "flat-skill", "SKILL.md"), "---\nname: flat-skill\n---\n");
|
|
104
|
-
mkdirSync(join(dir, "skills", "nested-skill"), { recursive: true });
|
|
105
|
-
writeFileSync(join(dir, "skills", "nested-skill", "SKILL.md"), "---\nname: nested-skill\n---\n");
|
|
106
|
-
|
|
107
|
-
const dirs = scanSkillDirectories(dir).map((d) => d.replace(`${dir}/`, ""));
|
|
108
|
-
expect(dirs).toEqual(["flat-skill", "skills/nested-skill"]);
|
|
109
|
-
} finally {
|
|
110
|
-
rmSync(dir, { recursive: true, force: true });
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
describe("buildDeclarativeItems", () => {
|
|
116
|
-
it("skips Project, warns-and-skips Organization, and orders the rest", () => {
|
|
117
|
-
const dir = mkdtempSync(join(tmpdir(), "decl-"));
|
|
118
|
-
try {
|
|
119
|
-
writeFileSync(join(dir, "agent.yaml"), "kind: Agent\nmetadata:\n slug: a\n");
|
|
120
|
-
writeFileSync(join(dir, "mcp.yaml"), "kind: McpServer\nmetadata:\n slug: m\n");
|
|
121
|
-
writeFileSync(join(dir, "org.yaml"), "kind: Organization\nmetadata:\n slug: o\n");
|
|
122
|
-
writeFileSync(join(dir, "proj.yaml"), "kind: Project\nmetadata:\n slug: p\n");
|
|
123
|
-
|
|
124
|
-
const warnings: string[] = [];
|
|
125
|
-
const items = buildDeclarativeItems(scanResourceFiles(dir), (line) => warnings.push(line));
|
|
126
|
-
expect(items.map((i) => i.handler.displayName)).toEqual(["MCP Server", "Agent"]);
|
|
127
|
-
expect(warnings.some((w) => w.includes("Organization"))).toBe(true);
|
|
128
|
-
} finally {
|
|
129
|
-
rmSync(dir, { recursive: true, force: true });
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
});
|
|
@@ -1,491 +0,0 @@
|
|
|
1
|
-
// Declarative apply (`stigmer apply` inside a stigmer.yaml project directory).
|
|
2
|
-
//
|
|
3
|
-
// The project is a lightweight membership tracker: it stores only references
|
|
4
|
-
// (org/kind/slug) to its members and the server reconciles by set-difference
|
|
5
|
-
// against the previous member list. The CLI's job is to discover the resources
|
|
6
|
-
// that live beside stigmer.yaml, apply each one, collect references to the
|
|
7
|
-
// member-eligible ones, then apply the project with that member set. Pruning of
|
|
8
|
-
// orphans is therefore entirely server-side — see the note on `--prune` in
|
|
9
|
-
// commands/apply.ts (S1).
|
|
10
|
-
//
|
|
11
|
-
// Two project layouts are supported, mirroring the Go CLI:
|
|
12
|
-
// flat: project/agent.yaml, project/my-skill/SKILL.md
|
|
13
|
-
// organized: project/agents/*.yaml, project/skills/my-skill/SKILL.md
|
|
14
|
-
// Scanning is top-level plus one level of subdirectories — deep trees are not
|
|
15
|
-
// walked, matching Go (this keeps membership predictable and fast).
|
|
16
|
-
|
|
17
|
-
import { existsSync, mkdtempSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
18
|
-
import { tmpdir } from "node:os";
|
|
19
|
-
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
20
|
-
import { create, fromJson, type Message } from "@bufbuild/protobuf";
|
|
21
|
-
import type { McpServer } from "@stigmer/protos/ai/stigmer/agentic/mcpserver/v1/api_pb";
|
|
22
|
-
import {
|
|
23
|
-
type ApiResourceReference,
|
|
24
|
-
ApiResourceReferenceSchema,
|
|
25
|
-
} from "@stigmer/protos/ai/stigmer/commons/apiresource/io_pb";
|
|
26
|
-
import { ApiResourceKind } from "@stigmer/protos/ai/stigmer/commons/apiresource/apiresourcekind/api_resource_kind_pb";
|
|
27
|
-
import {
|
|
28
|
-
type ApiResourceMetadata,
|
|
29
|
-
ApiResourceMetadataSchema,
|
|
30
|
-
} from "@stigmer/protos/ai/stigmer/commons/apiresource/metadata_pb";
|
|
31
|
-
import { type Project, ProjectSchema } from "@stigmer/protos/ai/stigmer/tenancy/project/v1/api_pb";
|
|
32
|
-
import { ProjectCommandController } from "@stigmer/protos/ai/stigmer/tenancy/project/v1/command_pb";
|
|
33
|
-
import { ProjectSpecSchema } from "@stigmer/protos/ai/stigmer/tenancy/project/v1/spec_pb";
|
|
34
|
-
import type { Stigmer } from "@stigmer/sdk";
|
|
35
|
-
import { UsageError } from "../../errors/index.js";
|
|
36
|
-
import { CommandResult } from "../../output/index.js";
|
|
37
|
-
import { loadDocuments } from "../documents.js";
|
|
38
|
-
import { cloneRepository } from "../git.js";
|
|
39
|
-
import { hasSkillFile, type PushResult, pushSkill, pushSkillFromClone } from "../skill.js";
|
|
40
|
-
import { type ApplyItem, applyMessage, marshalItem, resolveHandlerForKind, resourceMetadata, sortApplyItems } from "./apply.js";
|
|
41
|
-
import { discoverAppliedMcpServers } from "./discovery.js";
|
|
42
|
-
import type { ApplyHandler, ControllerFn } from "./handlers.js";
|
|
43
|
-
|
|
44
|
-
const CONFIG_FILE_NAME = "stigmer.yaml";
|
|
45
|
-
const DEFAULT_MAX_DEPTH = 10;
|
|
46
|
-
|
|
47
|
-
/** The detected CLI operation mode (mirrors Go's project.Track). */
|
|
48
|
-
export type Track = "atomic" | "declarative" | "project";
|
|
49
|
-
|
|
50
|
-
export interface DetectResult {
|
|
51
|
-
readonly track: Track;
|
|
52
|
-
/** Absolute path to stigmer.yaml; undefined for the atomic track. */
|
|
53
|
-
readonly configPath?: string;
|
|
54
|
-
/** Directory containing stigmer.yaml; undefined for the atomic track. */
|
|
55
|
-
readonly configDir?: string;
|
|
56
|
-
/** Parsed Project; undefined for the atomic track. */
|
|
57
|
-
readonly project?: Project;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Walk up from `startDir` (max 10 levels, matching Go) looking for stigmer.yaml.
|
|
62
|
-
* Absent → atomic track. Present-but-invalid → a hard error (the user clearly
|
|
63
|
-
* intended a project; help them fix it rather than silently falling back).
|
|
64
|
-
* Present with an entry_point → the SDK "project" track (synthesis), which this
|
|
65
|
-
* wave does not implement.
|
|
66
|
-
*/
|
|
67
|
-
export function detectTrack(startDir: string, maxDepth = DEFAULT_MAX_DEPTH): DetectResult {
|
|
68
|
-
let dir = resolve(startDir);
|
|
69
|
-
for (let depth = 0; depth < maxDepth; depth++) {
|
|
70
|
-
const candidate = join(dir, CONFIG_FILE_NAME);
|
|
71
|
-
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
72
|
-
const project = loadProject(candidate);
|
|
73
|
-
const track: Track = project.spec?.entryPoint ? "project" : "declarative";
|
|
74
|
-
return { track, configPath: candidate, configDir: dirname(candidate), project };
|
|
75
|
-
}
|
|
76
|
-
const parent = dirname(dir);
|
|
77
|
-
if (parent === dir) break;
|
|
78
|
-
dir = parent;
|
|
79
|
-
}
|
|
80
|
-
return { track: "atomic" };
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Strict parse of stigmer.yaml into a Project (proto carries apiVersion/kind as
|
|
84
|
-
// real fields, so the discriminator is accepted, not rejected). Unknown fields
|
|
85
|
-
// are an error — a typo in stigmer.yaml should fail loudly.
|
|
86
|
-
function loadProject(configPath: string): Project {
|
|
87
|
-
const docs = loadDocuments(configPath, { strict: true });
|
|
88
|
-
if (docs.length === 0) throw new UsageError(`${configPath} contains no document`);
|
|
89
|
-
const doc = docs[0];
|
|
90
|
-
if (doc.kind !== "Project") {
|
|
91
|
-
throw new UsageError(`${configPath} must declare 'kind: Project' (found '${doc.kind}')`);
|
|
92
|
-
}
|
|
93
|
-
try {
|
|
94
|
-
return fromJson(ProjectSchema, doc.document, { ignoreUnknownFields: false });
|
|
95
|
-
} catch (err) {
|
|
96
|
-
throw new UsageError(
|
|
97
|
-
`invalid project configuration in ${configPath}: ${(err as Error).message}\n\n` +
|
|
98
|
-
"Fix the issues above or remove the file to use file mode (stigmer apply -f).",
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// =============================================================================
|
|
104
|
-
// Directory scanning
|
|
105
|
-
// =============================================================================
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* YAML resource files beside stigmer.yaml: top-level files plus files in
|
|
109
|
-
* immediate subdirectories. stigmer.yaml itself is excluded, as are skill
|
|
110
|
-
* directories (handled separately) and grouping dirs that hold skills.
|
|
111
|
-
*/
|
|
112
|
-
export function scanResourceFiles(projectDir: string): string[] {
|
|
113
|
-
const files: string[] = [];
|
|
114
|
-
for (const entry of readdirSync(projectDir, { withFileTypes: true })) {
|
|
115
|
-
const full = join(projectDir, entry.name);
|
|
116
|
-
if (entry.isDirectory()) {
|
|
117
|
-
if (isSkillDirectory(full) || containsSkillDirectories(full)) continue;
|
|
118
|
-
for (const sub of readdirSync(full, { withFileTypes: true })) {
|
|
119
|
-
if (!sub.isDirectory() && isYaml(sub.name)) files.push(join(full, sub.name));
|
|
120
|
-
}
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
if (isYaml(entry.name) && entry.name.toLowerCase() !== CONFIG_FILE_NAME) {
|
|
124
|
-
files.push(full);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return files.sort();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Skill directories beside stigmer.yaml: immediate children that contain a
|
|
132
|
-
* SKILL.md (flat layout), plus grandchildren under a grouping dir like skills/
|
|
133
|
-
* (organized layout). Scanning stops one level deep, matching Go.
|
|
134
|
-
*/
|
|
135
|
-
export function scanSkillDirectories(projectDir: string): string[] {
|
|
136
|
-
const skillDirs: string[] = [];
|
|
137
|
-
for (const entry of readdirSync(projectDir, { withFileTypes: true })) {
|
|
138
|
-
if (!entry.isDirectory()) continue;
|
|
139
|
-
const full = join(projectDir, entry.name);
|
|
140
|
-
if (isSkillDirectory(full)) {
|
|
141
|
-
skillDirs.push(full);
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
for (const sub of readdirSync(full, { withFileTypes: true })) {
|
|
145
|
-
const nested = join(full, sub.name);
|
|
146
|
-
if (sub.isDirectory() && isSkillDirectory(nested)) skillDirs.push(nested);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return skillDirs.sort();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function containsSkillDirectories(dir: string): boolean {
|
|
153
|
-
try {
|
|
154
|
-
return readdirSync(dir, { withFileTypes: true }).some((e) => e.isDirectory() && isSkillDirectory(join(dir, e.name)));
|
|
155
|
-
} catch {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function isSkillDirectory(dir: string): boolean {
|
|
161
|
-
return hasSkillFile(dir);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function isYaml(name: string): boolean {
|
|
165
|
-
const ext = extname(name).toLowerCase();
|
|
166
|
-
return ext === ".yaml" || ext === ".yml";
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// =============================================================================
|
|
170
|
-
// Item building
|
|
171
|
-
// =============================================================================
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Build ordered apply items from scanned resource files. Project documents are
|
|
175
|
-
* skipped (stigmer.yaml is already loaded) and Organization documents are
|
|
176
|
-
* skipped with a warning — an organization sits above a project in the resource
|
|
177
|
-
* hierarchy and must be applied independently with file mode.
|
|
178
|
-
*/
|
|
179
|
-
export function buildDeclarativeItems(files: readonly string[], warn: (line: string) => void): ApplyItem[] {
|
|
180
|
-
const items: ApplyItem[] = [];
|
|
181
|
-
for (const file of files) {
|
|
182
|
-
for (const doc of loadDocuments(file, { strict: true })) {
|
|
183
|
-
if (doc.kind === "Project") continue;
|
|
184
|
-
if (doc.kind === "Organization") {
|
|
185
|
-
warn(`Skipping ${file}: Organization is not a project resource. Use 'stigmer apply -f' to manage organizations.`);
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
items.push({ filePath: file, handler: resolveHandlerForKind(doc.kind, file), document: doc.document });
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return sortApplyItems(items);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Member-eligible kinds participate in project membership and reconciliation.
|
|
195
|
-
// Mirrors Go's types.IsProjectMemberKind.
|
|
196
|
-
const MEMBER_KINDS: ReadonlySet<ApiResourceKind> = new Set([
|
|
197
|
-
ApiResourceKind.agent,
|
|
198
|
-
ApiResourceKind.workflow,
|
|
199
|
-
ApiResourceKind.mcp_server,
|
|
200
|
-
ApiResourceKind.skill,
|
|
201
|
-
]);
|
|
202
|
-
|
|
203
|
-
// =============================================================================
|
|
204
|
-
// Orchestration
|
|
205
|
-
// =============================================================================
|
|
206
|
-
|
|
207
|
-
export interface DeclarativeDeps {
|
|
208
|
-
/** Raw command-controller accessor (full-proto apply, preserves metadata.id). */
|
|
209
|
-
readonly controller: ControllerFn;
|
|
210
|
-
/** High-level client for skill push + post-apply MCP discovery. */
|
|
211
|
-
readonly stigmer: Stigmer;
|
|
212
|
-
/** Resolved organization (already validated non-empty by the caller). */
|
|
213
|
-
readonly org: string;
|
|
214
|
-
/** Human progress lines (stderr; not a parity surface). */
|
|
215
|
-
readonly info: (line: string) => void;
|
|
216
|
-
/** Warnings (stderr). */
|
|
217
|
-
readonly warn: (line: string) => void;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Apply a declarative project: scan the directory for YAML resources and skill
|
|
222
|
-
* directories, marshal each resource, then hand everything to the shared
|
|
223
|
-
* reconciler. This adapter's only job is "filesystem → resources + skill
|
|
224
|
-
* sources"; the synthesis track has its own adapter ("SDK → resources + skill
|
|
225
|
-
* sources") and both converge on {@link reconcileProjectMembers} (DD-009 §6).
|
|
226
|
-
*/
|
|
227
|
-
export async function applyDeclarative(detect: DetectResult, deps: DeclarativeDeps): Promise<CommandResult> {
|
|
228
|
-
const { project, configDir } = detect;
|
|
229
|
-
if (project === undefined || configDir === undefined) {
|
|
230
|
-
throw new UsageError("declarative apply requires a stigmer.yaml project");
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const resourceFiles = scanResourceFiles(configDir);
|
|
234
|
-
const skillDirs = scanSkillDirectories(configDir);
|
|
235
|
-
if (resourceFiles.length === 0 && skillDirs.length === 0) {
|
|
236
|
-
return buildNoResourcesResult(configDir);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const items = buildDeclarativeItems(resourceFiles, deps.warn);
|
|
240
|
-
deps.info(`Found ${items.length} resource(s) in ${resourceFiles.length} file(s), ${skillDirs.length} skill(s)`);
|
|
241
|
-
|
|
242
|
-
const resources: ReconcileResource[] = items.map((item) => ({ handler: item.handler, message: marshalItem(item) }));
|
|
243
|
-
const skillSources: SkillSource[] = skillDirs.map((dir) => ({ kind: "local", dir, tag: "latest" }));
|
|
244
|
-
|
|
245
|
-
return reconcileProjectMembers(project, resources, skillSources, deps);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// =============================================================================
|
|
249
|
-
// Shared reconciler (used by BOTH the declarative and the synthesis tracks)
|
|
250
|
-
// =============================================================================
|
|
251
|
-
|
|
252
|
-
/** A resource ready to apply: its handler plus the fully-marshalled message. */
|
|
253
|
-
export interface ReconcileResource {
|
|
254
|
-
readonly handler: ApplyHandler;
|
|
255
|
-
readonly message: Message;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* A skill to push as a project member. `local` is a directory beside the
|
|
260
|
-
* project (declarative) or resolved from a SkillSynth local path (synthesis);
|
|
261
|
-
* `git` is a remote repo cloned on demand (synthesis only — SkillSynth git).
|
|
262
|
-
*/
|
|
263
|
-
export type SkillSource =
|
|
264
|
-
| { readonly kind: "local"; readonly dir: string; readonly tag: string }
|
|
265
|
-
| {
|
|
266
|
-
readonly kind: "git";
|
|
267
|
-
readonly url: string;
|
|
268
|
-
readonly ref: string;
|
|
269
|
-
readonly subdir: string;
|
|
270
|
-
readonly tag: string;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
// Both tracks treat skill artifacts the same way; the default ignore policy
|
|
274
|
-
// mirrors the declarative push (respect .gitignore, no extra patterns).
|
|
275
|
-
const DEFAULT_IGNORE_OPTIONS = { respectGitignore: true, extraIgnore: [] as string[], extraInclude: [] as string[] };
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Reconcile a project's membership: push skills first (agents may reference
|
|
279
|
-
* them), apply MCP servers and run capability discovery on them, apply the
|
|
280
|
-
* remaining resources, collect references to the member-eligible ones, then
|
|
281
|
-
* apply the project with that exact member set so the server can reconcile by
|
|
282
|
-
* set-difference (orphan pruning is server-side; see S1 in commands/apply.ts).
|
|
283
|
-
*/
|
|
284
|
-
export async function reconcileProjectMembers(
|
|
285
|
-
project: Project,
|
|
286
|
-
resources: readonly ReconcileResource[],
|
|
287
|
-
skillSources: readonly SkillSource[],
|
|
288
|
-
deps: DeclarativeDeps,
|
|
289
|
-
): Promise<CommandResult> {
|
|
290
|
-
const members: ApiResourceReference[] = [];
|
|
291
|
-
|
|
292
|
-
// Skills first — agents may reference them.
|
|
293
|
-
for (const src of skillSources) {
|
|
294
|
-
const pushed = await pushSkillSource(deps.stigmer, src, deps.org, deps.info);
|
|
295
|
-
members.push(reference(deps.org, ApiResourceKind.skill, pushed.slug));
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Each resource is applied through the shared apply core, collecting member
|
|
299
|
-
// references from the APPLIED result so membership carries the server's
|
|
300
|
-
// authoritative slug. (This is the unified policy: the declarative track
|
|
301
|
-
// used to read the pre-apply YAML slug, the Go synthesis track read the
|
|
302
|
-
// applied result — converging on the applied result is correct for both and
|
|
303
|
-
// matches what the backend actually stored.)
|
|
304
|
-
const appliedMcpServers: McpServer[] = [];
|
|
305
|
-
const applyOne = async (res: ReconcileResource): Promise<void> => {
|
|
306
|
-
const outcome = await applyMessage(deps.controller, res.handler, res.message, deps.org, false);
|
|
307
|
-
if (outcome.warning !== undefined) deps.warn(outcome.warning);
|
|
308
|
-
if (outcome.appliedMcpServer !== undefined) appliedMcpServers.push(outcome.appliedMcpServer);
|
|
309
|
-
if (MEMBER_KINDS.has(res.handler.kind) && outcome.applied !== undefined) {
|
|
310
|
-
const slug = memberSlugOf(outcome.applied);
|
|
311
|
-
if (slug) members.push(reference(deps.org, res.handler.kind, slug));
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
// MCP servers first, then capability discovery, then everything else — the
|
|
316
|
-
// dependency principle applied to capabilities, not just existence. The
|
|
317
|
-
// server validates agent enabled_tools against discovered capabilities at
|
|
318
|
-
// apply time (stigmer/stigmer#402), and the backend's own post-apply
|
|
319
|
-
// connect is asynchronous; discovering here means agents in this apply
|
|
320
|
-
// validate against the toolset this apply just shipped, not the previous
|
|
321
|
-
// generation's (which would falsely reject e.g. a project upgrade that
|
|
322
|
-
// adds a tool to a server and enables it in an agent in the same pass). Discovery stays
|
|
323
|
-
// best-effort: a failed connect warns, capabilities stay stale, and the
|
|
324
|
-
// dependent apply surfaces an actionable error listing last-known tools.
|
|
325
|
-
for (const res of resources) {
|
|
326
|
-
if (res.handler.kind === ApiResourceKind.mcp_server) await applyOne(res);
|
|
327
|
-
}
|
|
328
|
-
await discoverAppliedMcpServers(deps.stigmer, appliedMcpServers, deps.org, deps.info);
|
|
329
|
-
for (const res of resources) {
|
|
330
|
-
if (res.handler.kind !== ApiResourceKind.mcp_server) await applyOne(res);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
injectOrg(project, deps.org);
|
|
334
|
-
if (project.spec === undefined) project.spec = create(ProjectSpecSchema, {});
|
|
335
|
-
project.spec.members = members;
|
|
336
|
-
const created = (project.metadata?.id ?? "") === "";
|
|
337
|
-
const applied = await deps.controller(ProjectCommandController).apply(project);
|
|
338
|
-
|
|
339
|
-
return buildDeclarativeResult(applied, members, created);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Push one skill source and return the result. Local sources push directly;
|
|
344
|
-
* git sources clone to a temp dir, push from the subdir, then clean up
|
|
345
|
-
* (mirrors `commands/push.ts` --git-url and Go's `pushSkillSynth`).
|
|
346
|
-
*/
|
|
347
|
-
export async function pushSkillSource(
|
|
348
|
-
client: Stigmer,
|
|
349
|
-
src: SkillSource,
|
|
350
|
-
org: string,
|
|
351
|
-
info: (line: string) => void,
|
|
352
|
-
): Promise<PushResult> {
|
|
353
|
-
if (src.kind === "local") {
|
|
354
|
-
info(`Pushing skill from ${basename(src.dir)}...`);
|
|
355
|
-
return pushSkill(client, src.dir, org, src.tag, "", DEFAULT_IGNORE_OPTIONS);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
info(`Pushing skill from ${src.url}...`);
|
|
359
|
-
const tempDir = mkdtempSync(join(tmpdir(), "stigmer-skill-"));
|
|
360
|
-
try {
|
|
361
|
-
cloneRepository(src.url, src.ref, tempDir);
|
|
362
|
-
const skillDir = src.subdir !== "" ? join(tempDir, src.subdir) : tempDir;
|
|
363
|
-
return await pushSkillFromClone(client, skillDir, {
|
|
364
|
-
gitUrl: src.url,
|
|
365
|
-
gitRef: src.ref,
|
|
366
|
-
subdir: src.subdir,
|
|
367
|
-
org,
|
|
368
|
-
tag: src.tag,
|
|
369
|
-
message: "",
|
|
370
|
-
options: DEFAULT_IGNORE_OPTIONS,
|
|
371
|
-
});
|
|
372
|
-
} finally {
|
|
373
|
-
rmSync(tempDir, { recursive: true, force: true });
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// Member slug from an applied resource: prefer the server's slug, fall back to
|
|
378
|
-
// the name (mirrors Go's buildResourceReference / reader name-fallback).
|
|
379
|
-
function memberSlugOf(message: Message): string {
|
|
380
|
-
const meta = resourceMetadata(message);
|
|
381
|
-
return meta?.slug || meta?.name || "";
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/** Dry-run: validate every resource without touching the backend. */
|
|
385
|
-
export function previewDeclarative(detect: DetectResult, deps: Pick<DeclarativeDeps, "controller" | "warn">): {
|
|
386
|
-
results: CommandResult[];
|
|
387
|
-
summary: CommandResult;
|
|
388
|
-
} {
|
|
389
|
-
const { configDir } = detect;
|
|
390
|
-
if (configDir === undefined) throw new UsageError("declarative apply requires a stigmer.yaml project");
|
|
391
|
-
const items = buildDeclarativeItems(scanResourceFiles(configDir), deps.warn);
|
|
392
|
-
const results = items.map((item) => buildDryRunPreview(item));
|
|
393
|
-
const summary = CommandResult.success(`Dry run complete: ${items.length} resource(s) validated`);
|
|
394
|
-
summary.hint("Remove --dry-run to apply resources to the backend");
|
|
395
|
-
return { results, summary };
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
function buildDryRunPreview(item: ApplyItem): CommandResult {
|
|
399
|
-
const meta = metaOf(item);
|
|
400
|
-
const result = CommandResult.success(`Dry run: ${meta?.name ?? item.handler.displayName} is valid`);
|
|
401
|
-
const section = result.addSection(`${item.handler.displayName} Preview`);
|
|
402
|
-
if (meta?.name) section.field("Name", meta.name);
|
|
403
|
-
if (meta?.slug) section.field("Slug", meta.slug);
|
|
404
|
-
return result;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// The document is YAML-shaped JSON; read metadata fields for membership/preview
|
|
408
|
-
// without marshalling (the apply itself marshals strictly).
|
|
409
|
-
function metaOf(item: ApplyItem): { name?: string; slug?: string } | undefined {
|
|
410
|
-
const meta = (item.document as { metadata?: { name?: unknown; slug?: unknown } }).metadata;
|
|
411
|
-
if (meta === undefined) return undefined;
|
|
412
|
-
return {
|
|
413
|
-
name: typeof meta.name === "string" ? meta.name : undefined,
|
|
414
|
-
slug: typeof meta.slug === "string" ? meta.slug : undefined,
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
function reference(org: string, kind: ApiResourceKind, slug: string): ApiResourceReference {
|
|
419
|
-
return create(ApiResourceReferenceSchema, { org, kind, slug });
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
function injectOrg(project: Project, org: string): void {
|
|
423
|
-
if (org === "") return;
|
|
424
|
-
const holder = project as unknown as { metadata?: ApiResourceMetadata };
|
|
425
|
-
if (holder.metadata === undefined) {
|
|
426
|
-
holder.metadata = create(ApiResourceMetadataSchema, { org });
|
|
427
|
-
} else if (holder.metadata.org === "") {
|
|
428
|
-
holder.metadata.org = org;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
// =============================================================================
|
|
433
|
-
// Result builders
|
|
434
|
-
// =============================================================================
|
|
435
|
-
|
|
436
|
-
function buildNoResourcesResult(projectDir: string): CommandResult {
|
|
437
|
-
const result = CommandResult.warning("No resource files found in project directory");
|
|
438
|
-
result
|
|
439
|
-
.addSection("")
|
|
440
|
-
.field("Directory", projectDir)
|
|
441
|
-
.item(
|
|
442
|
-
"Add YAML resource files (Agent, Workflow, McpServer) or skill directories (with SKILL.md) next to stigmer.yaml",
|
|
443
|
-
);
|
|
444
|
-
result.hint("Example: create agent.yaml with kind: Agent, then run 'stigmer apply'");
|
|
445
|
-
return result;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
function buildDeclarativeResult(
|
|
449
|
-
project: Project,
|
|
450
|
-
members: readonly ApiResourceReference[],
|
|
451
|
-
created: boolean,
|
|
452
|
-
): CommandResult {
|
|
453
|
-
const name = project.metadata?.name ?? "project";
|
|
454
|
-
const result = CommandResult.success(`Project ${name} successfully (${created ? "created" : "updated"})`);
|
|
455
|
-
|
|
456
|
-
const section = result.addSection("Project");
|
|
457
|
-
if (project.metadata?.name) section.field("Name", project.metadata.name);
|
|
458
|
-
if (project.metadata?.slug) section.field("Slug", project.metadata.slug);
|
|
459
|
-
if (project.metadata?.id) section.field("ID", project.metadata.id);
|
|
460
|
-
|
|
461
|
-
const counts = countByKind(members);
|
|
462
|
-
if (counts.size > 0) {
|
|
463
|
-
const memberSection = result.addSection("Members Applied");
|
|
464
|
-
for (const kind of [
|
|
465
|
-
ApiResourceKind.agent,
|
|
466
|
-
ApiResourceKind.workflow,
|
|
467
|
-
ApiResourceKind.mcp_server,
|
|
468
|
-
ApiResourceKind.skill,
|
|
469
|
-
]) {
|
|
470
|
-
const count = counts.get(kind);
|
|
471
|
-
if (count !== undefined) memberSection.field(ApiResourceKind[kind], String(count));
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
const recon = project.status?.lastReconciliation;
|
|
476
|
-
if (recon !== undefined && (recon.created.length > 0 || recon.updated.length > 0 || recon.deleted.length > 0)) {
|
|
477
|
-
const reconSection = result.addSection("Reconciliation");
|
|
478
|
-
if (recon.created.length > 0) reconSection.field("Created", String(recon.created.length));
|
|
479
|
-
if (recon.updated.length > 0) reconSection.field("Updated", String(recon.updated.length));
|
|
480
|
-
if (recon.deleted.length > 0) reconSection.field("Pruned", String(recon.deleted.length));
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
if (project.metadata?.slug) result.hint(`View project: stigmer get project ${project.metadata.slug}`);
|
|
484
|
-
return result;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
function countByKind(members: readonly ApiResourceReference[]): Map<ApiResourceKind, number> {
|
|
488
|
-
const counts = new Map<ApiResourceKind, number>();
|
|
489
|
-
for (const member of members) counts.set(member.kind, (counts.get(member.kind) ?? 0) + 1);
|
|
490
|
-
return counts;
|
|
491
|
-
}
|