self-evolving-workflow 0.0.0-stage → 0.1.3
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/LICENSE +27 -0
- package/README.md +90 -2
- package/dist/agent-memory-prompts.d.ts +13 -0
- package/dist/agent-memory-prompts.js +55 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +6 -0
- package/dist/closeout.d.ts +105 -0
- package/dist/closeout.js +479 -0
- package/dist/code.d.ts +39 -0
- package/dist/code.js +124 -0
- package/dist/common.d.ts +9 -0
- package/dist/common.js +42 -0
- package/dist/context-envelope.d.ts +59 -0
- package/dist/context-envelope.js +23 -0
- package/dist/host-adapters.d.ts +34 -0
- package/dist/host-adapters.js +151 -0
- package/dist/http.d.ts +3 -0
- package/dist/http.js +90 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +13 -0
- package/dist/knowledge-cli.d.ts +183 -0
- package/dist/knowledge-cli.js +654 -0
- package/dist/knowledge-reads.d.ts +71 -0
- package/dist/knowledge-reads.js +193 -0
- package/dist/memory.d.ts +97 -0
- package/dist/memory.js +250 -0
- package/dist/retrospective.d.ts +57 -0
- package/dist/retrospective.js +248 -0
- package/dist/search.d.ts +36 -0
- package/dist/search.js +112 -0
- package/dist/skill-addition.d.ts +25 -0
- package/dist/skill-addition.js +89 -0
- package/dist/skill-design.d.ts +96 -0
- package/dist/skill-design.js +226 -0
- package/dist/skill-replay.d.ts +55 -0
- package/dist/skill-replay.js +451 -0
- package/dist/skill-route.d.ts +30 -0
- package/dist/skill-route.js +75 -0
- package/dist/transcript-evidence.d.ts +3 -0
- package/dist/transcript-evidence.js +58 -0
- package/dist/upstream/chunker.d.ts +24 -0
- package/dist/upstream/chunker.js +96 -0
- package/dist/upstream/file-protocol.d.ts +45 -0
- package/dist/upstream/file-protocol.js +117 -0
- package/dist/upstream/frontmatter.d.ts +47 -0
- package/dist/upstream/frontmatter.js +86 -0
- package/dist/upstream/graph-search.d.ts +33 -0
- package/dist/upstream/graph-search.js +67 -0
- package/dist/upstream/l1-extraction.d.ts +26 -0
- package/dist/upstream/l1-extraction.js +405 -0
- package/dist/upstream/llm.d.ts +8 -0
- package/dist/upstream/llm.js +1 -0
- package/dist/upstream/merge.d.ts +50 -0
- package/dist/upstream/merge.js +153 -0
- package/dist/upstream/prompts.d.ts +53 -0
- package/dist/upstream/prompts.js +185 -0
- package/dist/upstream/search-utils.d.ts +34 -0
- package/dist/upstream/search-utils.js +55 -0
- package/dist/upstream/slug.d.ts +26 -0
- package/dist/upstream/slug.js +103 -0
- package/dist/upstream/template.d.ts +29 -0
- package/dist/upstream/template.js +80 -0
- package/dist/upstream/tokenize.d.ts +4 -0
- package/dist/upstream/tokenize.js +29 -0
- package/dist/wiki-change.d.ts +76 -0
- package/dist/wiki-change.js +299 -0
- package/dist/wiki-entry.d.ts +26 -0
- package/dist/wiki-entry.js +93 -0
- package/dist/wiki-ingest.d.ts +12 -0
- package/dist/wiki-ingest.js +126 -0
- package/dist/wiki-migration.d.ts +16 -0
- package/dist/wiki-migration.js +268 -0
- package/dist/wiki-tokens.d.ts +1 -0
- package/dist/wiki-tokens.js +46 -0
- package/dist/wiki.d.ts +103 -0
- package/dist/wiki.js +330 -0
- package/dist/workflow-cli.d.ts +2 -0
- package/dist/workflow-cli.js +258 -0
- package/dist/workflow-install.d.ts +36 -0
- package/dist/workflow-install.js +148 -0
- package/dist/workflow-knowledge.d.ts +16 -0
- package/dist/workflow-knowledge.js +24 -0
- package/dist/workflow-mcp.d.ts +2 -0
- package/dist/workflow-mcp.js +144 -0
- package/dist/workflow-names.d.ts +4 -0
- package/dist/workflow-names.js +18 -0
- package/dist/workflow-runtime.d.ts +622 -0
- package/dist/workflow-runtime.js +627 -0
- package/dist/workflow-skills.d.ts +48 -0
- package/dist/workflow-skills.js +141 -0
- package/package.json +76 -4
- package/skills/ew-odyssey/SKILL.md +237 -0
- package/skills/ew-odyssey/references/audit-artifacts.md +7 -0
- package/skills/ew-odyssey/references/odyssey-base-codex.md +3 -0
- package/skills/ew-odyssey/references/odyssey-base.md +278 -0
- package/skills/ew-odyssey/references/odyssey-debug.codex.md +305 -0
- package/skills/ew-odyssey/references/odyssey-debug.md +283 -0
- package/skills/ew-odyssey/references/odyssey-defensive.codex.md +395 -0
- package/skills/ew-odyssey/references/odyssey-defensive.md +369 -0
- package/skills/ew-odyssey/references/odyssey-improve.codex.md +270 -0
- package/skills/ew-odyssey/references/odyssey-improve.md +248 -0
- package/skills/ew-odyssey/references/odyssey-planex.codex.md +352 -0
- package/skills/ew-odyssey/references/odyssey-planex.md +312 -0
- package/skills/ew-odyssey/references/odyssey-review.codex.md +283 -0
- package/skills/ew-odyssey/references/odyssey-review.md +251 -0
- package/skills/ew-odyssey/references/odyssey-security.md +249 -0
- package/skills/ew-odyssey/references/odyssey-ui.codex.md +294 -0
- package/skills/ew-odyssey/references/odyssey-ui.md +250 -0
- package/skills/ew-odyssey/references/prepare/odyssey-debug.md +105 -0
- package/skills/ew-odyssey/references/prepare/odyssey-defensive.md +121 -0
- package/skills/ew-odyssey/references/prepare/odyssey-improve.md +93 -0
- package/skills/ew-odyssey/references/prepare/odyssey-planex.md +104 -0
- package/skills/ew-odyssey/references/prepare/odyssey-review.md +98 -0
- package/skills/ew-odyssey/references/prepare/odyssey-security.md +94 -0
- package/skills/ew-odyssey/references/prepare/odyssey-ui.md +87 -0
- package/skills/ew-orchestrate/SKILL.md +236 -0
- package/skills/ew-orchestrate/references/auto-policy.md +165 -0
- package/skills/ew-orchestrate/references/companion-route.md +52 -0
- package/skills/ew-orchestrate/references/ew-orchestrate.md +27 -0
- package/skills/ew-orchestrate/references/goal.md +53 -0
- package/skills/ew-orchestrate/references/knowledge-route.md +163 -0
- package/skills/ew-orchestrate/references/orchestrator-run-loop.md +36 -0
- package/skills/ew-orchestrate/references/planning.md +137 -0
- package/skills/ew-orchestrate/steps/ew-analyze/SKILL.md +62 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/boundary-grill.md +50 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/issue-gaps-analyze.md +59 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/prepare.md +42 -0
- package/skills/ew-orchestrate/steps/ew-analyze/references/workflow.md +401 -0
- package/skills/ew-orchestrate/steps/ew-brainstorm/SKILL.md +32 -0
- package/skills/ew-orchestrate/steps/ew-companion/SKILL.md +9 -0
- package/skills/ew-orchestrate/steps/ew-execute/SKILL.md +71 -0
- package/skills/ew-orchestrate/steps/ew-execute/references/prepare.md +69 -0
- package/skills/ew-orchestrate/steps/ew-execute/references/workflow.md +373 -0
- package/skills/ew-orchestrate/steps/ew-plan/SKILL.md +102 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/boundary-grill.md +50 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/prepare.md +51 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/tdd.md +53 -0
- package/skills/ew-orchestrate/steps/ew-plan/references/workflow.md +278 -0
- package/skills/ew-orchestrate/steps/ew-review/SKILL.md +44 -0
- package/skills/ew-orchestrate/steps/ew-review/references/odyssey-base-codex.md +3 -0
- package/skills/ew-orchestrate/steps/ew-review/references/prepare.md +76 -0
- package/skills/ew-orchestrate/steps/ew-review/references/workflow.md +283 -0
- package/skills/ew-skill-design/SKILL.md +28 -0
- package/skills/ew-skill-design/references/package-contract.md +28 -0
- package/skills/ew-skill-design/references/validation.md +7 -0
- package/skills/team-swarm/SKILL.md +231 -0
- package/skills/team-swarm/references/mcp-coordination.md +10 -0
- package/skills/team-swarm/roles/analyst/role.md +181 -0
- package/skills/team-swarm/roles/ant/role.md +149 -0
- package/skills/team-swarm/roles/coordinator/commands/converge.md +154 -0
- package/skills/team-swarm/roles/coordinator/commands/init-swarm.md +134 -0
- package/skills/team-swarm/roles/coordinator/commands/iterate.md +202 -0
- package/skills/team-swarm/roles/coordinator/role.md +228 -0
- package/skills/team-swarm/roles/scorer/role.md +150 -0
- package/skills/team-swarm/scripts/aco.py +646 -0
- package/skills/team-swarm/scripts/pheromone.py +144 -0
- package/skills/team-swarm/scripts/scoring.py +111 -0
- package/skills/team-swarm/scripts/test_aco.py +606 -0
- package/skills/team-swarm/specs/ant-output-schema.md +127 -0
- package/skills/team-swarm/specs/convergence-criteria.md +108 -0
- package/skills/team-swarm/specs/pheromone-schema.md +125 -0
- package/skills/team-swarm/specs/swarm-config-template.json +71 -0
- package/skills/team-swarm/specs/swarm-protocol.md +123 -0
- package/skills/workflow-closeout/SKILL.md +10 -0
- package/skills/workflow-evolve/SKILL.md +79 -0
- package/skills/workflow-evolve/references/agy-gate.md +27 -0
- package/skills/workflow-evolve/references/auto-loop.md +20 -0
- package/skills/workflow-evolve/references/history-review.md +14 -0
- package/skills/workflow-evolve/scripts/codex-memory-replay.mjs +76 -0
- package/skills/workflow-evolve/scripts/codex-replay.mjs +1 -0
- package/skills/workflow-evolve/scripts/evolve-loop.mjs +259 -0
- package/skills/workflow-evolve/scripts/memory-codex-mcp.mjs +124 -0
- package/workflows/index.json +23 -0
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { CodeGraph } from './code.js';
|
|
5
|
+
import { InputError, requireText } from './common.js';
|
|
6
|
+
import { createSearchServer } from './http.js';
|
|
7
|
+
import { MemoryStore } from './memory.js';
|
|
8
|
+
import { context as buildContext, parseScope, search } from './search.js';
|
|
9
|
+
import { Wiki } from './wiki.js';
|
|
10
|
+
import { harvestCompletion, publishReference, readCloseoutCandidate, readCompletionReceipts, readHarvest, readSkillCompletions, recordSkillCompletion, } from './closeout.js';
|
|
11
|
+
import { reviewSession, reviewSkillExecution } from './retrospective.js';
|
|
12
|
+
import { draftSkillAddition } from './skill-addition.js';
|
|
13
|
+
import { changeSkillVersion, replaySkillAddition } from './skill-replay.js';
|
|
14
|
+
import { migrateLegacyWiki, validateWikiMemberUid } from './wiki-migration.js';
|
|
15
|
+
import { isRepositoryId, isWikiCategory } from './wiki-entry.js';
|
|
16
|
+
import { applyWikiChange, listWikiChanges, proposeWikiChange } from './wiki-change.js';
|
|
17
|
+
import { KnowledgeReads, validateKnowledgeReads } from './knowledge-reads.js';
|
|
18
|
+
import { decideSkillRoute } from './skill-route.js';
|
|
19
|
+
import { evolveSessionReceipt, readEvolveSessions, workflowDbPath } from './workflow-runtime.js';
|
|
20
|
+
import { applySkillDesign, createSkillDesignTask, readSkillDesignTask, replaySkillDesign, reviewSkillDesign, runSkillDesign, } from './skill-design.js';
|
|
21
|
+
export function knowledgeHelp(invocation) {
|
|
22
|
+
const skills = invocation === 'evolve' ? 'skills' : 'evolve';
|
|
23
|
+
return `Usage: ${invocation} <command> [options]
|
|
24
|
+
|
|
25
|
+
code build|sync Build or update the project's code graph
|
|
26
|
+
code search <query> Find code symbols
|
|
27
|
+
code callers|callees <symbol-id> Follow call edges (--depth 1)
|
|
28
|
+
code trace <from-id> <to-id> Find a path between code symbols
|
|
29
|
+
wiki build Index Markdown pages under wiki/
|
|
30
|
+
wiki ingest <file.md|file.txt> Generate and merge wiki pages
|
|
31
|
+
wiki search <query> Search wiki pages (--hop 0)
|
|
32
|
+
wiki get <id> Read a Wiki entry, including history (--resolved for scoped reference)
|
|
33
|
+
wiki references <query> Search active applicable references (--repository, --task, --path)
|
|
34
|
+
wiki reference <id> Compatibility alias for wiki get <id> --resolved
|
|
35
|
+
wiki constraints Resolve active constraints (--task, --path, --category, --repository, --keyword)
|
|
36
|
+
wiki migrate <legacy-root> Preview legacy Wiki migration (--project, --scope project|global|team|personal, --uid, --knowhow-only, --apply)
|
|
37
|
+
wiki propose <change.json> Stage create, update, promote, or retire (--evidence or --closeout and --candidate)
|
|
38
|
+
wiki changes [change-id] Read change previews and application receipts (--entry)
|
|
39
|
+
wiki apply <change-id> Apply an approved Wiki change
|
|
40
|
+
closeout harvest <receipt.json|id> Extract Wiki candidates from a receipt or recorded Skill completion (--project)
|
|
41
|
+
closeout session <id> Harvest a committed Session receipt (--project, --receipt)
|
|
42
|
+
closeout skill <execution-id> Record and harvest a standalone Skill completion
|
|
43
|
+
(--skill, --skill-version, --status, --transcript, --knowledge-receipt, --project, --record-only)
|
|
44
|
+
closeout resume <receipt-id> Compatibility alias for closeout harvest <receipt-id>
|
|
45
|
+
closeout scan [source-project-root] Reconcile committed Session and standalone Skill receipts (--project)
|
|
46
|
+
closeout show <receipt-id> Show harvested candidates (--project)
|
|
47
|
+
closeout route <closeout-id> <candidate-id> Decide Wiki, Skill addition, or meta Skill design
|
|
48
|
+
closeout publish <closeout-id> <candidate-id> <title>
|
|
49
|
+
Publish a reviewed reference
|
|
50
|
+
session review <id> Review Session signals (--transcript <messages.json>)
|
|
51
|
+
${skills} review <execution-id> Review a standalone Skill receipt (--project <id>)
|
|
52
|
+
${skills} draft <SKILL.md> <instruction> Draft a Skill addition (--priority high|normal|low, --evidence <ref>, --when)
|
|
53
|
+
${skills} replay <draft.json> Compare baseline and candidate (--runner <script>, --scenarios <json>)
|
|
54
|
+
${skills} apply|rollback <receipt-id> Apply a validated version or revert it
|
|
55
|
+
skill-design task <task-id> Read a generated meta Skill design task
|
|
56
|
+
skill-design run <task-id> Run the explicit meta Skill design runner (--runner)
|
|
57
|
+
skill-design review <design-id> Review the generated design against its baseline
|
|
58
|
+
skill-design replay <design-id> Replay the reviewed design (--runner, --scenarios)
|
|
59
|
+
skill-design apply|rollback <design-id> Apply or roll back the replayed design
|
|
60
|
+
memory put <text> Save a memory (--id, --type, --scene, --session, --priority)
|
|
61
|
+
memory extract <messages.json> Extract L1 memories (--session required, --mode chat|code)
|
|
62
|
+
memory get|delete <id> Read or delete a memory
|
|
63
|
+
memory search <query> Search memory (--type, --scene, --session)
|
|
64
|
+
search <query> Search available indexes (--scope all|code|wiki|memory)
|
|
65
|
+
context <query> Build Agent Memory context envelope (--scope all|code|wiki|memory, --include-memory)
|
|
66
|
+
serve Serve GET /search on 127.0.0.1 (--port 4310)
|
|
67
|
+
|
|
68
|
+
Options:
|
|
69
|
+
--root <dir> Code project root and parent of wiki/ (default current directory)
|
|
70
|
+
--wiki-root <dir> Override the parent of wiki/
|
|
71
|
+
--db <path> Memory DB (default <root>/.agent-memory/memory.db)
|
|
72
|
+
--namespace <name> Memory namespace (default default)
|
|
73
|
+
--memory-session <id> Memory session filter (context uses this instead of its context session)
|
|
74
|
+
--context-file <path> Load shared task, path and Wiki filters from JSON
|
|
75
|
+
--trace Include knowledge_reads search/read evidence in the result
|
|
76
|
+
--reads <path> Load and persist a knowledge_reads receipt across CLI calls
|
|
77
|
+
--trace-file <path> Compatibility alias for --reads
|
|
78
|
+
--from-search <id> Associate an exact read with an earlier search ID
|
|
79
|
+
--resolved Read Wiki references using the active scope context
|
|
80
|
+
--json Compatibility flag. Agent Memory output is always JSON
|
|
81
|
+
--limit <n> Maximum results per source (default 10)
|
|
82
|
+
--help Show this message
|
|
83
|
+
|
|
84
|
+
Ingest and extract use AGENT_MEMORY_LLM_BASE_URL, AGENT_MEMORY_LLM_MODEL and optional
|
|
85
|
+
AGENT_MEMORY_LLM_API_KEY for an OpenAI-compatible chat endpoint. Message files contain
|
|
86
|
+
an array of {id, role, timestamp, content}. Commands return JSON on stdout.
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
89
|
+
export const knowledgeOptions = {
|
|
90
|
+
root: { type: 'string' },
|
|
91
|
+
'wiki-root': { type: 'string' },
|
|
92
|
+
db: { type: 'string' },
|
|
93
|
+
namespace: { type: 'string' },
|
|
94
|
+
scope: { type: 'string' },
|
|
95
|
+
limit: { type: 'string' },
|
|
96
|
+
depth: { type: 'string' },
|
|
97
|
+
hop: { type: 'string' },
|
|
98
|
+
type: { type: 'string' },
|
|
99
|
+
scene: { type: 'string' },
|
|
100
|
+
session: { type: 'string' },
|
|
101
|
+
'memory-session': { type: 'string' },
|
|
102
|
+
'context-file': { type: 'string' },
|
|
103
|
+
id: { type: 'string' },
|
|
104
|
+
priority: { type: 'string' },
|
|
105
|
+
port: { type: 'string' },
|
|
106
|
+
mode: { type: 'string' },
|
|
107
|
+
skill: { type: 'string' }, // 收尾路由和执行回执的 Skill 标识,旧查询也可携带。
|
|
108
|
+
task: { type: 'string' },
|
|
109
|
+
category: { type: 'string', multiple: true },
|
|
110
|
+
keyword: { type: 'string' },
|
|
111
|
+
repository: { type: 'string' },
|
|
112
|
+
'include-path-scoped': { type: 'boolean' },
|
|
113
|
+
'include-memory': { type: 'boolean' },
|
|
114
|
+
trace: { type: 'boolean' },
|
|
115
|
+
'trace-file': { type: 'string' },
|
|
116
|
+
reads: { type: 'string' },
|
|
117
|
+
'from-search': { type: 'string' },
|
|
118
|
+
resolved: { type: 'boolean' },
|
|
119
|
+
path: { type: 'string', multiple: true },
|
|
120
|
+
history: { type: 'boolean' },
|
|
121
|
+
project: { type: 'string' },
|
|
122
|
+
uid: { type: 'string' },
|
|
123
|
+
'knowhow-only': { type: 'boolean' },
|
|
124
|
+
apply: { type: 'boolean' },
|
|
125
|
+
transcript: { type: 'string' },
|
|
126
|
+
'knowledge-receipt': { type: 'string' },
|
|
127
|
+
receipt: { type: 'string' },
|
|
128
|
+
'skill-version': { type: 'string' },
|
|
129
|
+
'repeat-count': { type: 'string' },
|
|
130
|
+
'step-count': { type: 'string' },
|
|
131
|
+
'signal-kind': { type: 'string', multiple: true },
|
|
132
|
+
'cross-skill': { type: 'boolean' },
|
|
133
|
+
'new-contract': { type: 'boolean' },
|
|
134
|
+
'new-artifacts': { type: 'boolean' },
|
|
135
|
+
'requires-routing': { type: 'boolean' },
|
|
136
|
+
'skill-intent': { type: 'boolean' },
|
|
137
|
+
status: { type: 'string' },
|
|
138
|
+
'record-only': { type: 'boolean' },
|
|
139
|
+
evidence: { type: 'string', multiple: true },
|
|
140
|
+
entry: { type: 'string' },
|
|
141
|
+
closeout: { type: 'string' },
|
|
142
|
+
candidate: { type: 'string' },
|
|
143
|
+
when: { type: 'string' },
|
|
144
|
+
runner: { type: 'string' },
|
|
145
|
+
scenarios: { type: 'string' },
|
|
146
|
+
help: { type: 'boolean', short: 'h' },
|
|
147
|
+
json: { type: 'boolean' },
|
|
148
|
+
};
|
|
149
|
+
function modelClient() {
|
|
150
|
+
const url = new URL(requireText(process.env.AGENT_MEMORY_LLM_BASE_URL, 'AGENT_MEMORY_LLM_BASE_URL'));
|
|
151
|
+
if (!['http:', 'https:'].includes(url.protocol))
|
|
152
|
+
throw new InputError('LLM endpoint must use HTTP or HTTPS');
|
|
153
|
+
url.pathname = url.pathname.replace(/\/$/, '') + '/chat/completions';
|
|
154
|
+
const model = requireText(process.env.AGENT_MEMORY_LLM_MODEL, 'AGENT_MEMORY_LLM_MODEL');
|
|
155
|
+
const key = process.env.AGENT_MEMORY_LLM_API_KEY;
|
|
156
|
+
return {
|
|
157
|
+
async chat({ system, prompt }) {
|
|
158
|
+
const response = await fetch(url, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'Content-Type': 'application/json', ...(key && { Authorization: `Bearer ${key}` }) },
|
|
161
|
+
body: JSON.stringify({ model, messages: [{ role: 'system', content: system }, { role: 'user', content: prompt }] }),
|
|
162
|
+
signal: AbortSignal.timeout(120_000),
|
|
163
|
+
});
|
|
164
|
+
if (!response.ok)
|
|
165
|
+
throw new Error(`LLM request failed (HTTP ${response.status})`);
|
|
166
|
+
const data = await response.json();
|
|
167
|
+
return requireText(data.choices?.[0]?.message?.content, 'LLM response content');
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function readContextFile(path) {
|
|
172
|
+
const value = JSON.parse(readFileSync(path, 'utf8').replace(/^\uFEFF/, ''));
|
|
173
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
174
|
+
throw new InputError('Context file must contain a JSON object');
|
|
175
|
+
const input = value;
|
|
176
|
+
const list = (field) => {
|
|
177
|
+
if (input[field] === undefined)
|
|
178
|
+
return undefined;
|
|
179
|
+
if (!Array.isArray(input[field]) || input[field].some(item => typeof item !== 'string' || !item.trim())) {
|
|
180
|
+
throw new InputError(`Context file ${field} must be a list of non-empty strings`);
|
|
181
|
+
}
|
|
182
|
+
return [...new Set(input[field].map(item => item.trim()))];
|
|
183
|
+
};
|
|
184
|
+
const text = (field) => {
|
|
185
|
+
if (input[field] === undefined)
|
|
186
|
+
return undefined;
|
|
187
|
+
if (typeof input[field] !== 'string' || !input[field].trim())
|
|
188
|
+
throw new InputError(`Context file ${field} must be a non-empty string`);
|
|
189
|
+
return input[field].trim();
|
|
190
|
+
};
|
|
191
|
+
if (input.includePathScoped !== undefined && typeof input.includePathScoped !== 'boolean') {
|
|
192
|
+
throw new InputError('Context file includePathScoped must be boolean');
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
task: text('task'), paths: list('paths'), categories: list('categories'),
|
|
196
|
+
repository: text('repository'), keyword: text('keyword'),
|
|
197
|
+
...(input.includePathScoped === undefined ? {} : { includePathScoped: input.includePathScoped }),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function cliWikiContext(values) {
|
|
201
|
+
const file = typeof values['context-file'] === 'string' ? readContextFile(values['context-file']) : {};
|
|
202
|
+
const paths = Array.isArray(values.path) ? values.path : file.paths;
|
|
203
|
+
const categories = Array.isArray(values.category) ? values.category : file.categories;
|
|
204
|
+
return {
|
|
205
|
+
task: typeof values.task === 'string' ? values.task : file.task,
|
|
206
|
+
paths, categories,
|
|
207
|
+
repository: typeof values.repository === 'string' ? values.repository : file.repository,
|
|
208
|
+
keyword: typeof values.keyword === 'string' ? values.keyword : file.keyword,
|
|
209
|
+
includePathScoped: typeof values['include-path-scoped'] === 'boolean'
|
|
210
|
+
? values['include-path-scoped'] : file.includePathScoped,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
export async function runKnowledgeCli(argv, invocation = 'agent-memory') {
|
|
214
|
+
const { values, positionals } = parseArgs({ args: argv, options: knowledgeOptions, allowPositionals: true });
|
|
215
|
+
if (values.help || !positionals.length) {
|
|
216
|
+
process.stdout.write(knowledgeHelp(invocation));
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const [command, action, ...args] = positionals;
|
|
220
|
+
const root = resolve(values.root ?? '.');
|
|
221
|
+
const contextValues = cliWikiContext(values);
|
|
222
|
+
const wiki = new Wiki(values['wiki-root'] ?? root);
|
|
223
|
+
const autoRefresh = invocation === 'evolve';
|
|
224
|
+
const openCode = () => new CodeGraph(root, { autoRefresh });
|
|
225
|
+
const db = values.db ?? join(root, '.agent-memory', 'memory.db');
|
|
226
|
+
const openMemory = () => new MemoryStore(db, { namespace: values.namespace });
|
|
227
|
+
const limit = values.limit === undefined ? undefined : Number(values.limit);
|
|
228
|
+
const wikiOptions = { limit, hop: values.hop === undefined ? undefined : Number(values.hop), includeHistory: values.history };
|
|
229
|
+
const memorySession = values['memory-session'] ?? (command === 'memory' || command === 'search' ? values.session : undefined);
|
|
230
|
+
const memoryOptions = { limit, type: values.type, scene: values.scene, session: memorySession };
|
|
231
|
+
if (values.reads !== undefined && values['trace-file'] !== undefined && values.reads !== values['trace-file']) {
|
|
232
|
+
throw new InputError('Use either --reads or --trace-file, not both');
|
|
233
|
+
}
|
|
234
|
+
const traceFile = values.reads ?? values['trace-file'];
|
|
235
|
+
const initialTrace = traceFile && existsSync(traceFile)
|
|
236
|
+
? validateKnowledgeReads(JSON.parse(readFileSync(traceFile, 'utf8').replace(/^\uFEFF/, '')))
|
|
237
|
+
: undefined;
|
|
238
|
+
const knowledgeReads = values.trace || traceFile
|
|
239
|
+
? new KnowledgeReads(initialTrace)
|
|
240
|
+
: undefined;
|
|
241
|
+
const print = (result) => process.stdout.write(JSON.stringify(result, null, 2) + '\n');
|
|
242
|
+
const withTrace = (result) => {
|
|
243
|
+
if (!knowledgeReads)
|
|
244
|
+
return result;
|
|
245
|
+
if (traceFile) {
|
|
246
|
+
mkdirSync(dirname(resolve(traceFile)), { recursive: true });
|
|
247
|
+
writeFileSync(traceFile, JSON.stringify(knowledgeReads.toJSON(), null, 2) + '\n');
|
|
248
|
+
}
|
|
249
|
+
return result && typeof result === 'object' && !Array.isArray(result)
|
|
250
|
+
? { ...result, knowledge_reads: knowledgeReads.toJSON() }
|
|
251
|
+
: { result, knowledge_reads: knowledgeReads.toJSON() };
|
|
252
|
+
};
|
|
253
|
+
function sourcesFor(scope) {
|
|
254
|
+
const useCode = command !== 'context' &&
|
|
255
|
+
(scope === 'code' || (scope === 'all' && (autoRefresh || CodeGraph.hasIndex(root))));
|
|
256
|
+
return {
|
|
257
|
+
code: useCode ? openCode() : undefined,
|
|
258
|
+
wiki: scope === 'wiki' || (scope === 'all' && existsSync(wiki.indexPath)) ? wiki : undefined,
|
|
259
|
+
memory: scope === 'memory' || (scope === 'all' && (db === ':memory:' || existsSync(db))) ? openMemory() : undefined,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
if (command === 'serve') {
|
|
263
|
+
const port = Number(values.port ?? '4310');
|
|
264
|
+
if (action !== undefined || !Number.isInteger(port) || port < 0 || port > 65535) {
|
|
265
|
+
throw new InputError(`Usage: ${invocation} serve [--port 0..65535]`);
|
|
266
|
+
}
|
|
267
|
+
const sources = sourcesFor(parseScope(values.scope ?? 'all'));
|
|
268
|
+
const server = createSearchServer(sources);
|
|
269
|
+
server.on('searchError', (error) => console.error(error));
|
|
270
|
+
server.on('close', () => sources.memory?.close());
|
|
271
|
+
server.on('error', error => {
|
|
272
|
+
console.error(error.message);
|
|
273
|
+
process.exitCode = 1;
|
|
274
|
+
server.close();
|
|
275
|
+
});
|
|
276
|
+
for (const signal of ['SIGINT', 'SIGTERM'])
|
|
277
|
+
process.once(signal, () => server.close());
|
|
278
|
+
server.listen(port, '127.0.0.1', () => {
|
|
279
|
+
const address = server.address();
|
|
280
|
+
if (address && typeof address === 'object')
|
|
281
|
+
print({ url: `http://127.0.0.1:${address.port}/search` });
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (command === 'search') {
|
|
286
|
+
const query = requireText([action, ...args].join(' '), 'query');
|
|
287
|
+
const scope = parseScope(values.scope ?? 'all');
|
|
288
|
+
const sources = sourcesFor(scope);
|
|
289
|
+
try {
|
|
290
|
+
print(withTrace(await search(sources, query, {
|
|
291
|
+
scope, limit, wiki: wikiOptions, memory: memoryOptions, knowledgeReads,
|
|
292
|
+
})));
|
|
293
|
+
}
|
|
294
|
+
finally {
|
|
295
|
+
sources.memory?.close();
|
|
296
|
+
}
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (command === 'context') {
|
|
300
|
+
const query = requireText([action, ...args].join(' '), 'query');
|
|
301
|
+
const scope = parseScope(values.scope ?? 'all');
|
|
302
|
+
if (contextValues.categories && !contextValues.categories.every(isWikiCategory)) {
|
|
303
|
+
throw new InputError('Unknown Wiki context category');
|
|
304
|
+
}
|
|
305
|
+
if (contextValues.repository !== undefined && !isRepositoryId(contextValues.repository)) {
|
|
306
|
+
throw new InputError('Expected a persisted repository UUID');
|
|
307
|
+
}
|
|
308
|
+
const sources = sourcesFor(scope);
|
|
309
|
+
try {
|
|
310
|
+
print(withTrace(await buildContext(sources, query, {
|
|
311
|
+
limit,
|
|
312
|
+
context: {
|
|
313
|
+
task: contextValues.task,
|
|
314
|
+
paths: contextValues.paths,
|
|
315
|
+
categories: contextValues.categories,
|
|
316
|
+
repository: contextValues.repository,
|
|
317
|
+
keyword: contextValues.keyword,
|
|
318
|
+
includePathScoped: contextValues.includePathScoped,
|
|
319
|
+
},
|
|
320
|
+
wiki: wikiOptions,
|
|
321
|
+
memory: memoryOptions,
|
|
322
|
+
includeWorkingMemory: values['include-memory'],
|
|
323
|
+
knowledgeReads,
|
|
324
|
+
contextScope: {
|
|
325
|
+
...(values.session ? { session_id: values.session } : {}),
|
|
326
|
+
...(contextValues.task ? { task_kind: contextValues.task } : {}),
|
|
327
|
+
...(contextValues.paths?.length ? { paths: contextValues.paths } : {}),
|
|
328
|
+
},
|
|
329
|
+
})));
|
|
330
|
+
}
|
|
331
|
+
finally {
|
|
332
|
+
sources.memory?.close();
|
|
333
|
+
}
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (command === 'code') {
|
|
337
|
+
const code = openCode();
|
|
338
|
+
switch (action) {
|
|
339
|
+
case 'build': return print(await code.build());
|
|
340
|
+
case 'sync': return print(await code.sync());
|
|
341
|
+
case 'search': {
|
|
342
|
+
const query = args.join(' ');
|
|
343
|
+
const result = await code.search(query, limit);
|
|
344
|
+
if (knowledgeReads)
|
|
345
|
+
knowledgeReads.recordSearch({
|
|
346
|
+
source: 'code', query,
|
|
347
|
+
returnedIds: result.flatMap(item => typeof item.node?.id === 'string' ? [item.node.id] : []),
|
|
348
|
+
});
|
|
349
|
+
return print(withTrace(result));
|
|
350
|
+
}
|
|
351
|
+
case 'callers':
|
|
352
|
+
case 'callees': return print(await code.neighbors(requireText(args[0], 'symbol id'), action, Number(values.depth ?? '1')));
|
|
353
|
+
case 'trace': return print(await code.trace(requireText(args[0], 'from id'), requireText(args[1], 'to id')));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (command === 'wiki') {
|
|
357
|
+
switch (action) {
|
|
358
|
+
case 'build': return print(wiki.build());
|
|
359
|
+
case 'search': {
|
|
360
|
+
const query = args.join(' ');
|
|
361
|
+
const result = wiki.search(query, wikiOptions);
|
|
362
|
+
if (knowledgeReads)
|
|
363
|
+
knowledgeReads.recordSearch({
|
|
364
|
+
source: 'wiki', query, filters: wikiOptions, returnedIds: result.results.map(hit => hit.id),
|
|
365
|
+
});
|
|
366
|
+
return print(withTrace(result));
|
|
367
|
+
}
|
|
368
|
+
case 'get':
|
|
369
|
+
if (!values.resolved)
|
|
370
|
+
return print(withTrace(wiki.getEntry(requireText(args[0], 'wiki entry ID'), {
|
|
371
|
+
knowledgeReads, fromSearchId: values['from-search'],
|
|
372
|
+
})));
|
|
373
|
+
case 'constraints':
|
|
374
|
+
case 'references':
|
|
375
|
+
case 'reference': {
|
|
376
|
+
const categories = contextValues.categories;
|
|
377
|
+
const repository = contextValues.repository;
|
|
378
|
+
if (categories && !categories.every(isWikiCategory)) {
|
|
379
|
+
throw new InputError(action === 'constraints' ? 'Unknown Wiki constraint category' : 'Unknown Wiki reference category');
|
|
380
|
+
}
|
|
381
|
+
if (repository !== undefined && !isRepositoryId(repository))
|
|
382
|
+
throw new InputError('Expected a persisted repository UUID');
|
|
383
|
+
const context = { task: contextValues.task, paths: contextValues.paths,
|
|
384
|
+
categories: categories, repository, keyword: contextValues.keyword };
|
|
385
|
+
if (action === 'references') {
|
|
386
|
+
const query = args.join(' ');
|
|
387
|
+
const result = wiki.references(query, context, wikiOptions);
|
|
388
|
+
if (knowledgeReads)
|
|
389
|
+
knowledgeReads.recordSearch({
|
|
390
|
+
source: 'wiki_references', query, filters: { ...context, ...wikiOptions },
|
|
391
|
+
returnedIds: result.results.map(hit => hit.id),
|
|
392
|
+
});
|
|
393
|
+
return print(withTrace(result));
|
|
394
|
+
}
|
|
395
|
+
if (action === 'reference' || (action === 'get' && values.resolved)) {
|
|
396
|
+
return print(withTrace(wiki.getReference(requireText(args[0], 'Wiki entry ID'), context, {
|
|
397
|
+
knowledgeReads, fromSearchId: values['from-search'],
|
|
398
|
+
})));
|
|
399
|
+
}
|
|
400
|
+
const result = wiki.constraints({ ...context, includePathScoped: contextValues.includePathScoped });
|
|
401
|
+
if (knowledgeReads)
|
|
402
|
+
for (const constraint of result) {
|
|
403
|
+
knowledgeReads.bindConstraint({
|
|
404
|
+
knowledgeId: constraint.id, scope: constraint.entry?.scope ?? [],
|
|
405
|
+
categories: constraint.entry?.categories, content: constraint.body,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return print(withTrace(result));
|
|
409
|
+
}
|
|
410
|
+
case 'migrate': {
|
|
411
|
+
const scope = values.scope ?? 'project';
|
|
412
|
+
if (scope !== 'project' && scope !== 'global' && scope !== 'team' && scope !== 'personal') {
|
|
413
|
+
throw new InputError('Expected Wiki migration scope project, global, team, or personal');
|
|
414
|
+
}
|
|
415
|
+
const source = resolve(requireText(args[0], 'legacy source root'));
|
|
416
|
+
const uid = scope === 'personal' ? validateWikiMemberUid(values.uid) : undefined;
|
|
417
|
+
const collab = join(root, '.workflow', 'collab');
|
|
418
|
+
if (!values['wiki-root'] && (scope === 'team' || scope === 'personal')) {
|
|
419
|
+
const parents = [join(root, '.workflow'), collab];
|
|
420
|
+
if (uid)
|
|
421
|
+
parents.push(join(collab, 'personal'), join(collab, 'personal', uid));
|
|
422
|
+
for (const parent of parents) {
|
|
423
|
+
const info = lstatSync(parent, { throwIfNoEntry: false });
|
|
424
|
+
if (info && !info.isDirectory())
|
|
425
|
+
throw new InputError(`Invalid Wiki migration directory: ${parent}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
const layerRoot = scope === 'global' ? source : scope === 'team' ? collab
|
|
429
|
+
: uid ? join(collab, 'personal', uid) : root;
|
|
430
|
+
const destination = new Wiki(values['wiki-root'] ?? layerRoot);
|
|
431
|
+
return print(migrateLegacyWiki(destination, source, requireText(values.project, 'legacy project ID'), values.apply, {
|
|
432
|
+
scope, uid, knowhowOnly: values['knowhow-only'],
|
|
433
|
+
}));
|
|
434
|
+
}
|
|
435
|
+
case 'propose': {
|
|
436
|
+
const change = JSON.parse(readFileSync(requireText(args[0], 'change file'), 'utf8').replace(/^\uFEFF/, ''));
|
|
437
|
+
const evidence = values.evidence ?? [];
|
|
438
|
+
if (values.closeout !== undefined || values.candidate !== undefined) {
|
|
439
|
+
const closeout = requireText(values.closeout, 'closeout ID');
|
|
440
|
+
const candidate = readCloseoutCandidate(wiki, closeout, requireText(values.candidate, 'candidate ID'));
|
|
441
|
+
return print(proposeWikiChange(wiki, change, evidence, {
|
|
442
|
+
closeout, id: candidate.id, sources: candidate.sources,
|
|
443
|
+
related: [...(candidate.matched_entry ? [candidate.matched_entry] : []),
|
|
444
|
+
...(candidate.related_entries ?? []).map(entry => entry.id)],
|
|
445
|
+
}));
|
|
446
|
+
}
|
|
447
|
+
return print(proposeWikiChange(wiki, change, evidence));
|
|
448
|
+
}
|
|
449
|
+
case 'changes': return print(listWikiChanges(wiki, { id: args[0], entry: values.entry }));
|
|
450
|
+
case 'apply': return print(applyWikiChange(wiki, requireText(args[0], 'Wiki change ID')));
|
|
451
|
+
case 'ingest': return print(await wiki.ingest(requireText(args[0], 'source file'), { llm: modelClient() }));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (command === 'closeout') {
|
|
455
|
+
if (action === 'publish')
|
|
456
|
+
return print(publishReference(wiki, requireText(args[0], 'closeout ID'), requireText(args[1], 'candidate ID'), args.slice(2).join(' ')));
|
|
457
|
+
if (action === 'route') {
|
|
458
|
+
const candidate = readCloseoutCandidate(wiki, requireText(args[0], 'closeout ID'), requireText(args[1], 'candidate ID'));
|
|
459
|
+
const closeoutId = requireText(args[0], 'closeout ID');
|
|
460
|
+
const candidateId = requireText(args[1], 'candidate ID');
|
|
461
|
+
const decision = decideSkillRoute({
|
|
462
|
+
content: candidate.content,
|
|
463
|
+
sources: candidate.sources,
|
|
464
|
+
matchedEntry: candidate.matched_entry,
|
|
465
|
+
relatedEntries: candidate.related_entries?.map(entry => entry.id),
|
|
466
|
+
skillId: values.skill,
|
|
467
|
+
skillVersion: values['skill-version'],
|
|
468
|
+
signalKinds: values['signal-kind'],
|
|
469
|
+
repeatCount: values['repeat-count'] === undefined ? undefined : Number(values['repeat-count']),
|
|
470
|
+
stepCount: values['step-count'] === undefined ? undefined : Number(values['step-count']),
|
|
471
|
+
crossSkill: values['cross-skill'],
|
|
472
|
+
requiresNewContract: values['new-contract'],
|
|
473
|
+
requiresNewArtifacts: values['new-artifacts'],
|
|
474
|
+
requiresRouting: values['requires-routing'],
|
|
475
|
+
explicitSkillIntent: values['skill-intent'],
|
|
476
|
+
});
|
|
477
|
+
if (decision.route !== 'skill-design')
|
|
478
|
+
return print(decision);
|
|
479
|
+
if (!values.skill)
|
|
480
|
+
throw new InputError('Skill-design route requires --skill <skill-id> for the replay/apply chain');
|
|
481
|
+
const task = createSkillDesignTask(root, {
|
|
482
|
+
closeoutId, candidateId, content: candidate.content, sources: candidate.sources,
|
|
483
|
+
decision, skillId: values.skill, skillVersion: values['skill-version'],
|
|
484
|
+
});
|
|
485
|
+
return print({ ...decision, design_task: task });
|
|
486
|
+
}
|
|
487
|
+
const project = requireText(values.project, 'source project ID');
|
|
488
|
+
if (action === 'harvest' || action === 'resume') {
|
|
489
|
+
const source = requireText(args[0], action === 'harvest' ? 'receipt path or ID' : 'receipt ID');
|
|
490
|
+
if (action === 'harvest' && existsSync(source)) {
|
|
491
|
+
return print(await harvestCompletion(wiki, project, JSON.parse(readFileSync(source, 'utf8').replace(/^\uFEFF/, '')), modelClient));
|
|
492
|
+
}
|
|
493
|
+
const saved = readSkillCompletions(wiki, project, source)[0];
|
|
494
|
+
if (!saved)
|
|
495
|
+
throw new InputError(`Standalone Skill completion receipt not found: ${source}`);
|
|
496
|
+
return print(await harvestCompletion(wiki, project, saved.receipt, modelClient));
|
|
497
|
+
}
|
|
498
|
+
if (action === 'show')
|
|
499
|
+
return print(readHarvest(wiki, project, requireText(args[0], 'completion receipt ID')));
|
|
500
|
+
if (action === 'session') {
|
|
501
|
+
const session = requireText(args[0], 'Session ID');
|
|
502
|
+
const [native] = readEvolveSessions(root, session);
|
|
503
|
+
if (native) {
|
|
504
|
+
const receipt = evolveSessionReceipt(native);
|
|
505
|
+
if (values.receipt && values.receipt !== receipt.id)
|
|
506
|
+
throw new InputError('Evolve Session receipt ID mismatch');
|
|
507
|
+
return print(await harvestCompletion(wiki, project, receipt, modelClient));
|
|
508
|
+
}
|
|
509
|
+
const receipts = [...readCompletionReceipts(root, session, values.receipt)];
|
|
510
|
+
if (receipts.length !== 1) {
|
|
511
|
+
throw new InputError(`Expected one committed completion receipt for ${session}, found ${receipts.length}`);
|
|
512
|
+
}
|
|
513
|
+
const [{ receipt, directory }] = receipts;
|
|
514
|
+
return print(await harvestCompletion(wiki, project, receipt, modelClient, directory));
|
|
515
|
+
}
|
|
516
|
+
if (action === 'skill') {
|
|
517
|
+
const transcript = values.transcript ? JSON.parse(readFileSync(values.transcript, 'utf8').replace(/^\uFEFF/, '')) : [];
|
|
518
|
+
if (!Array.isArray(transcript))
|
|
519
|
+
throw new InputError('Transcript must be a JSON message array');
|
|
520
|
+
const knowledgeReads = values['knowledge-receipt']
|
|
521
|
+
? validateKnowledgeReads(JSON.parse(readFileSync(values['knowledge-receipt'], 'utf8').replace(/^\uFEFF/, '')))
|
|
522
|
+
: undefined;
|
|
523
|
+
const recorded = recordSkillCompletion(wiki, project, {
|
|
524
|
+
execution: requireText(args[0], 'Skill execution ID'),
|
|
525
|
+
skill: requireText(values.skill, 'Skill ID'), version: requireText(values['skill-version'], 'Skill version'),
|
|
526
|
+
status: requireText(values.status, 'Skill status'), messages: transcript, session: values.session, knowledgeReads,
|
|
527
|
+
});
|
|
528
|
+
if (!('receipt' in recorded))
|
|
529
|
+
return print(recorded);
|
|
530
|
+
if (values['record-only'])
|
|
531
|
+
return print({ receipt: recorded.receipt.id });
|
|
532
|
+
return print({ receipt: recorded.receipt.id, ...await harvestCompletion(wiki, project, recorded.receipt, modelClient) });
|
|
533
|
+
}
|
|
534
|
+
if (action === 'scan') {
|
|
535
|
+
const results = [];
|
|
536
|
+
const errors = [];
|
|
537
|
+
const sessionSource = args[0] ?? root;
|
|
538
|
+
const hasHistoricalSessions = existsSync(join(sessionSource, '.workflow', 'sessions'));
|
|
539
|
+
if (args[0] && !hasHistoricalSessions && !existsSync(workflowDbPath(sessionSource))) {
|
|
540
|
+
throw new InputError('Cannot locate Evolve or historical Sessions in the source project');
|
|
541
|
+
}
|
|
542
|
+
const sessionReceipts = hasHistoricalSessions
|
|
543
|
+
? readCompletionReceipts(sessionSource) : [];
|
|
544
|
+
const consume = async (id, receipt, directory) => {
|
|
545
|
+
try {
|
|
546
|
+
results.push(await harvestCompletion(wiki, project, receipt, modelClient, directory));
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
errors.push({ receipt: id, error: error instanceof Error ? error.message : String(error) });
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
for (const { receipt, directory } of sessionReceipts) {
|
|
553
|
+
await consume(requireText(receipt.transition_id, 'transition ID'), receipt, directory);
|
|
554
|
+
}
|
|
555
|
+
for (const session of readEvolveSessions(sessionSource).filter(item => item.status === 'completed')) {
|
|
556
|
+
const receipt = evolveSessionReceipt(session);
|
|
557
|
+
await consume(receipt.id, receipt);
|
|
558
|
+
}
|
|
559
|
+
for (const { id, receipt } of readSkillCompletions(wiki, project)) {
|
|
560
|
+
await consume(id, receipt);
|
|
561
|
+
}
|
|
562
|
+
if (errors.length > 0) {
|
|
563
|
+
process.exitCode = 1;
|
|
564
|
+
return print({ results, errors });
|
|
565
|
+
}
|
|
566
|
+
return print(results);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
if (command === 'session' && action === 'review') {
|
|
570
|
+
const transcript = values.transcript ? JSON.parse(readFileSync(values.transcript, 'utf8')) : undefined;
|
|
571
|
+
if (transcript !== undefined && !Array.isArray(transcript))
|
|
572
|
+
throw new InputError('Transcript must be a JSON message array');
|
|
573
|
+
return print(await reviewSession(root, requireText(args[0], 'Session ID'), { model: modelClient, transcript }));
|
|
574
|
+
}
|
|
575
|
+
if (command === 'evolve' && action === 'review') {
|
|
576
|
+
return print(await reviewSkillExecution(wiki, requireText(values.project, 'source project ID'), requireText(args[0], 'Skill execution ID'), modelClient));
|
|
577
|
+
}
|
|
578
|
+
if (command === 'evolve' && action === 'draft') {
|
|
579
|
+
const priority = values.priority;
|
|
580
|
+
if (priority !== 'high' && priority !== 'normal' && priority !== 'low')
|
|
581
|
+
throw new InputError('Unknown Skill priority');
|
|
582
|
+
return print(draftSkillAddition({
|
|
583
|
+
entry: requireText(args[0], 'Skill entry path'),
|
|
584
|
+
priority,
|
|
585
|
+
instruction: args.slice(1).join(' '),
|
|
586
|
+
sources: values.evidence ?? [],
|
|
587
|
+
when: values.when,
|
|
588
|
+
}));
|
|
589
|
+
}
|
|
590
|
+
if (command === 'evolve' && action === 'replay') {
|
|
591
|
+
const draft = JSON.parse(readFileSync(requireText(args[0], 'draft file'), 'utf8').replace(/^\uFEFF/, ''));
|
|
592
|
+
const scenarios = JSON.parse(readFileSync(requireText(values.scenarios, 'scenarios file'), 'utf8').replace(/^\uFEFF/, ''));
|
|
593
|
+
return print(replaySkillAddition(root, draft, { runner: requireText(values.runner, 'replay runner'), scenarios }));
|
|
594
|
+
}
|
|
595
|
+
if (command === 'evolve' && (action === 'apply' || action === 'rollback')) {
|
|
596
|
+
return print(changeSkillVersion(root, requireText(args[0], 'replay receipt ID'), action === 'rollback'));
|
|
597
|
+
}
|
|
598
|
+
if (command === 'skill-design') {
|
|
599
|
+
if (action === 'task')
|
|
600
|
+
return print(readSkillDesignTask(root, requireText(args[0], 'Skill design task ID')));
|
|
601
|
+
if (action === 'run')
|
|
602
|
+
return print(runSkillDesign(root, requireText(args[0], 'Skill design task ID'), requireText(values.runner, 'design runner')));
|
|
603
|
+
if (action === 'review')
|
|
604
|
+
return print(reviewSkillDesign(root, requireText(args[0], 'Skill design receipt ID')));
|
|
605
|
+
if (action === 'replay') {
|
|
606
|
+
const scenarios = JSON.parse(readFileSync(requireText(values.scenarios, 'scenarios file'), 'utf8').replace(/^\uFEFF/, ''));
|
|
607
|
+
return print(replaySkillDesign(root, requireText(args[0], 'Skill design receipt ID'), {
|
|
608
|
+
runner: requireText(values.runner, 'replay runner'), scenarios,
|
|
609
|
+
}));
|
|
610
|
+
}
|
|
611
|
+
if (action === 'apply' || action === 'rollback') {
|
|
612
|
+
return print(applySkillDesign(root, requireText(args[0], 'Skill design receipt ID'), action === 'rollback'));
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
if (command === 'memory' && ['put', 'get', 'delete', 'search', 'extract'].includes(action)) {
|
|
616
|
+
const memory = openMemory();
|
|
617
|
+
try {
|
|
618
|
+
switch (action) {
|
|
619
|
+
case 'put': return print(await memory.put({
|
|
620
|
+
content: args.join(' '), id: values.id, type: values.type, scene: values.scene, session: values.session,
|
|
621
|
+
priority: values.priority === undefined ? undefined : Number(values.priority),
|
|
622
|
+
}));
|
|
623
|
+
case 'get': return print(withTrace(memory.get(requireText(args[0], 'id'), {
|
|
624
|
+
knowledgeReads, fromSearchId: values['from-search'],
|
|
625
|
+
}) ?? null));
|
|
626
|
+
case 'delete': return print({ deleted: memory.delete(requireText(args[0], 'id')) });
|
|
627
|
+
case 'search': {
|
|
628
|
+
const query = args.join(' ');
|
|
629
|
+
const result = await memory.search(query, memoryOptions);
|
|
630
|
+
if (knowledgeReads)
|
|
631
|
+
knowledgeReads.recordSearch({
|
|
632
|
+
source: 'memory', query, filters: memoryOptions, returnedIds: result.results.map(hit => hit.id),
|
|
633
|
+
});
|
|
634
|
+
return print(withTrace(result));
|
|
635
|
+
}
|
|
636
|
+
case 'extract': {
|
|
637
|
+
const messages = JSON.parse(readFileSync(requireText(args[0], 'messages file'), 'utf8').replace(/^\uFEFF/, ''));
|
|
638
|
+
if (!Array.isArray(messages))
|
|
639
|
+
throw new InputError('Messages file must contain a JSON array');
|
|
640
|
+
const mode = values.mode ?? 'chat';
|
|
641
|
+
if (mode !== 'chat' && mode !== 'code')
|
|
642
|
+
throw new InputError('mode must be chat or code');
|
|
643
|
+
return print(await memory.extract(messages, {
|
|
644
|
+
session: requireText(values.session, 'session'), mode, llm: modelClient(),
|
|
645
|
+
}));
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
finally {
|
|
650
|
+
memory.close();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
throw new InputError(`Unknown command. Run ${invocation} --help`);
|
|
654
|
+
}
|