@aragon-agent/cli 0.6.55 → 0.6.56
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/CHANGELOG.md +2912 -2864
- package/README.md +160 -45
- package/dist/agent/controller.d.ts +47 -0
- package/dist/agent/controller.js +153 -2
- package/dist/agent/governance-wiring.js +8 -1
- package/dist/agent/system-prompt.d.ts +26 -0
- package/dist/agent/system-prompt.js +11 -0
- package/dist/app/App.js +101 -3
- package/dist/cli.js +29 -1
- package/dist/commands/builtins.js +4 -0
- package/dist/commands/catalog.js +1 -0
- package/dist/commands/registry.d.ts +8 -0
- package/dist/config/app-paths.d.ts +10 -0
- package/dist/config/app-paths.js +12 -0
- package/dist/config/cli-commands.d.ts +29 -0
- package/dist/config/cli-commands.js +76 -1
- package/dist/config/env.d.ts +2 -1
- package/dist/config/env.js +12 -1
- package/dist/config/load.d.ts +10 -0
- package/dist/config/load.js +13 -2
- package/dist/config/store.js +11 -1
- package/dist/exec/bridge/server.d.ts +7 -0
- package/dist/exec/bridge/server.js +6 -2
- package/dist/exec/cli-commands.js +4 -0
- package/dist/exec/index.js +2 -0
- package/dist/exec/options.d.ts +11 -0
- package/dist/exec/options.js +21 -1
- package/dist/gene/block.d.ts +33 -0
- package/dist/gene/block.js +51 -0
- package/dist/gene/capacity.d.ts +74 -0
- package/dist/gene/capacity.js +169 -0
- package/dist/gene/catalog-reconcile.d.ts +13 -0
- package/dist/gene/catalog-reconcile.js +105 -0
- package/dist/gene/catalog-stats.d.ts +27 -0
- package/dist/gene/catalog-stats.js +89 -0
- package/dist/gene/catalog-sync.d.ts +82 -0
- package/dist/gene/catalog-sync.js +324 -0
- package/dist/gene/catalog.d.ts +160 -0
- package/dist/gene/catalog.js +433 -0
- package/dist/gene/cli-commands.d.ts +50 -0
- package/dist/gene/cli-commands.js +500 -0
- package/dist/gene/command-report.d.ts +40 -0
- package/dist/gene/command-report.js +246 -0
- package/dist/gene/command.d.ts +29 -0
- package/dist/gene/command.js +561 -0
- package/dist/gene/commit-apply-types.d.ts +13 -0
- package/dist/gene/commit-apply-types.js +1 -0
- package/dist/gene/commit-compound.d.ts +34 -0
- package/dist/gene/commit-compound.js +212 -0
- package/dist/gene/commit-counts.d.ts +41 -0
- package/dist/gene/commit-counts.js +70 -0
- package/dist/gene/commit-exec.d.ts +42 -0
- package/dist/gene/commit-exec.js +134 -0
- package/dist/gene/commit-head.d.ts +52 -0
- package/dist/gene/commit-head.js +97 -0
- package/dist/gene/commit-plan.d.ts +95 -0
- package/dist/gene/commit-plan.js +68 -0
- package/dist/gene/commit-prepare.d.ts +63 -0
- package/dist/gene/commit-prepare.js +159 -0
- package/dist/gene/commit-types.d.ts +234 -0
- package/dist/gene/commit-types.js +3 -0
- package/dist/gene/curator/agent.d.ts +137 -0
- package/dist/gene/curator/agent.js +203 -0
- package/dist/gene/curator/budget.d.ts +69 -0
- package/dist/gene/curator/budget.js +131 -0
- package/dist/gene/curator/conflicts.d.ts +54 -0
- package/dist/gene/curator/conflicts.js +143 -0
- package/dist/gene/curator/curator.d.ts +170 -0
- package/dist/gene/curator/curator.js +617 -0
- package/dist/gene/curator/deps.d.ts +34 -0
- package/dist/gene/curator/deps.js +48 -0
- package/dist/gene/curator/deterministic.d.ts +62 -0
- package/dist/gene/curator/deterministic.js +287 -0
- package/dist/gene/curator/executors.d.ts +36 -0
- package/dist/gene/curator/executors.js +379 -0
- package/dist/gene/curator/lanes.d.ts +52 -0
- package/dist/gene/curator/lanes.js +99 -0
- package/dist/gene/curator/lease.d.ts +76 -0
- package/dist/gene/curator/lease.js +171 -0
- package/dist/gene/curator/library.d.ts +36 -0
- package/dist/gene/curator/library.js +169 -0
- package/dist/gene/curator/lifecycle.d.ts +52 -0
- package/dist/gene/curator/lifecycle.js +109 -0
- package/dist/gene/curator/maintenance.d.ts +71 -0
- package/dist/gene/curator/maintenance.js +213 -0
- package/dist/gene/curator/probation.d.ts +51 -0
- package/dist/gene/curator/probation.js +71 -0
- package/dist/gene/curator/probe.d.ts +131 -0
- package/dist/gene/curator/probe.js +534 -0
- package/dist/gene/curator/prompt.d.ts +18 -0
- package/dist/gene/curator/prompt.js +128 -0
- package/dist/gene/curator/publish.d.ts +64 -0
- package/dist/gene/curator/publish.js +113 -0
- package/dist/gene/curator/recheck.d.ts +55 -0
- package/dist/gene/curator/recheck.js +103 -0
- package/dist/gene/curator/run-events.d.ts +63 -0
- package/dist/gene/curator/run-events.js +55 -0
- package/dist/gene/curator/similarity.d.ts +13 -0
- package/dist/gene/curator/similarity.js +46 -0
- package/dist/gene/curator/switches.d.ts +78 -0
- package/dist/gene/curator/switches.js +146 -0
- package/dist/gene/curator/task-context.d.ts +93 -0
- package/dist/gene/curator/task-context.js +63 -0
- package/dist/gene/curator/task-runner.d.ts +56 -0
- package/dist/gene/curator/task-runner.js +257 -0
- package/dist/gene/curator/tool-support.d.ts +112 -0
- package/dist/gene/curator/tool-support.js +197 -0
- package/dist/gene/curator/toolkit.d.ts +49 -0
- package/dist/gene/curator/toolkit.js +104 -0
- package/dist/gene/curator/tools-read.d.ts +4 -0
- package/dist/gene/curator/tools-read.js +177 -0
- package/dist/gene/curator/tools-release.d.ts +73 -0
- package/dist/gene/curator/tools-release.js +287 -0
- package/dist/gene/curator/tools-verify.d.ts +47 -0
- package/dist/gene/curator/tools-verify.js +202 -0
- package/dist/gene/curator/tools-write.d.ts +26 -0
- package/dist/gene/curator/tools-write.js +394 -0
- package/dist/gene/curator/tools.d.ts +23 -0
- package/dist/gene/curator/tools.js +21 -0
- package/dist/gene/curator/validate/common.d.ts +87 -0
- package/dist/gene/curator/validate/common.js +132 -0
- package/dist/gene/curator/validate/evidence-tokens.d.ts +36 -0
- package/dist/gene/curator/validate/evidence-tokens.js +139 -0
- package/dist/gene/curator/validate/index.d.ts +23 -0
- package/dist/gene/curator/validate/index.js +62 -0
- package/dist/gene/curator/validate/knowledge.d.ts +25 -0
- package/dist/gene/curator/validate/knowledge.js +216 -0
- package/dist/gene/curator/validate/live-check.d.ts +14 -0
- package/dist/gene/curator/validate/live-check.js +144 -0
- package/dist/gene/curator/validate/ports.d.ts +67 -0
- package/dist/gene/curator/validate/ports.js +1 -0
- package/dist/gene/curator/validate/prompts.d.ts +41 -0
- package/dist/gene/curator/validate/prompts.js +161 -0
- package/dist/gene/curator/validate/skill.d.ts +15 -0
- package/dist/gene/curator/validate/skill.js +251 -0
- package/dist/gene/curator/validate/tool.d.ts +6 -0
- package/dist/gene/curator/validate/tool.js +217 -0
- package/dist/gene/curator/validate/types.d.ts +90 -0
- package/dist/gene/curator/validate/types.js +1 -0
- package/dist/gene/curator/validator-ports.d.ts +38 -0
- package/dist/gene/curator/validator-ports.js +29 -0
- package/dist/gene/diag.d.ts +209 -0
- package/dist/gene/diag.js +549 -0
- package/dist/gene/env.d.ts +10 -0
- package/dist/gene/env.js +214 -0
- package/dist/gene/eval-recorder.d.ts +84 -0
- package/dist/gene/eval-recorder.js +222 -0
- package/dist/gene/explain-transport.d.ts +3 -0
- package/dist/gene/explain-transport.js +39 -0
- package/dist/gene/explain.d.ts +96 -0
- package/dist/gene/explain.js +293 -0
- package/dist/gene/fs.d.ts +39 -0
- package/dist/gene/fs.js +139 -0
- package/dist/gene/fsck.d.ts +68 -0
- package/dist/gene/fsck.js +134 -0
- package/dist/gene/gc-library-sweeps.d.ts +9 -0
- package/dist/gene/gc-library-sweeps.js +220 -0
- package/dist/gene/gc-rejected-set.d.ts +15 -0
- package/dist/gene/gc-rejected-set.js +63 -0
- package/dist/gene/gc-safe.d.ts +12 -0
- package/dist/gene/gc-safe.js +74 -0
- package/dist/gene/gc-scope-sweeps.d.ts +18 -0
- package/dist/gene/gc-scope-sweeps.js +297 -0
- package/dist/gene/gc-types.d.ts +55 -0
- package/dist/gene/gc-types.js +26 -0
- package/dist/gene/gc.d.ts +42 -0
- package/dist/gene/gc.js +129 -0
- package/dist/gene/gene-errors.d.ts +23 -0
- package/dist/gene/gene-errors.js +29 -0
- package/dist/gene/hints.d.ts +91 -0
- package/dist/gene/hints.js +394 -0
- package/dist/gene/hub.d.ts +149 -0
- package/dist/gene/hub.js +371 -0
- package/dist/gene/import-plan.d.ts +80 -0
- package/dist/gene/import-plan.js +294 -0
- package/dist/gene/import-proposal.d.ts +23 -0
- package/dist/gene/import-proposal.js +101 -0
- package/dist/gene/import-skill-port.d.ts +6 -0
- package/dist/gene/import-skill-port.js +53 -0
- package/dist/gene/import-sources.d.ts +48 -0
- package/dist/gene/import-sources.js +140 -0
- package/dist/gene/index-writer.d.ts +36 -0
- package/dist/gene/index-writer.js +101 -0
- package/dist/gene/learn/learn-call.d.ts +63 -0
- package/dist/gene/learn/learn-call.js +157 -0
- package/dist/gene/learn/learn-prompt.d.ts +51 -0
- package/dist/gene/learn/learn-prompt.js +245 -0
- package/dist/gene/learn/learner.d.ts +181 -0
- package/dist/gene/learn/learner.js +426 -0
- package/dist/gene/learn/ledger.d.ts +220 -0
- package/dist/gene/learn/ledger.js +394 -0
- package/dist/gene/learn/proposal-build.d.ts +83 -0
- package/dist/gene/learn/proposal-build.js +211 -0
- package/dist/gene/learn/proposal-guard.d.ts +43 -0
- package/dist/gene/learn/proposal-guard.js +37 -0
- package/dist/gene/learn/signals-port.d.ts +47 -0
- package/dist/gene/learn/signals-port.js +86 -0
- package/dist/gene/maintenance-paths.d.ts +7 -0
- package/dist/gene/maintenance-paths.js +20 -0
- package/dist/gene/maintenance-probation.d.ts +33 -0
- package/dist/gene/maintenance-probation.js +127 -0
- package/dist/gene/maintenance-proposals.d.ts +24 -0
- package/dist/gene/maintenance-proposals.js +68 -0
- package/dist/gene/maintenance-runner.d.ts +92 -0
- package/dist/gene/maintenance-runner.js +262 -0
- package/dist/gene/maintenance-tasks.d.ts +37 -0
- package/dist/gene/maintenance-tasks.js +95 -0
- package/dist/gene/manager-actions.d.ts +12 -0
- package/dist/gene/manager-actions.js +279 -0
- package/dist/gene/manager-approve.d.ts +29 -0
- package/dist/gene/manager-approve.js +84 -0
- package/dist/gene/manager-context.d.ts +90 -0
- package/dist/gene/manager-context.js +201 -0
- package/dist/gene/manager-detail.d.ts +17 -0
- package/dist/gene/manager-detail.js +313 -0
- package/dist/gene/manager-gc.d.ts +13 -0
- package/dist/gene/manager-gc.js +81 -0
- package/dist/gene/manager-library.d.ts +39 -0
- package/dist/gene/manager-library.js +90 -0
- package/dist/gene/manager-list.d.ts +27 -0
- package/dist/gene/manager-list.js +170 -0
- package/dist/gene/manager-queue.d.ts +16 -0
- package/dist/gene/manager-queue.js +124 -0
- package/dist/gene/manager-stats.d.ts +28 -0
- package/dist/gene/manager-stats.js +53 -0
- package/dist/gene/manager-view.d.ts +27 -0
- package/dist/gene/manager-view.js +124 -0
- package/dist/gene/manager.d.ts +56 -0
- package/dist/gene/manager.js +179 -0
- package/dist/gene/orphan-scopes.d.ts +37 -0
- package/dist/gene/orphan-scopes.js +150 -0
- package/dist/gene/paths.d.ts +73 -0
- package/dist/gene/paths.js +239 -0
- package/dist/gene/plan-content.d.ts +73 -0
- package/dist/gene/plan-content.js +522 -0
- package/dist/gene/plan-lifecycle.d.ts +29 -0
- package/dist/gene/plan-lifecycle.js +310 -0
- package/dist/gene/policy.d.ts +71 -0
- package/dist/gene/policy.js +313 -0
- package/dist/gene/project-learning.d.ts +25 -0
- package/dist/gene/project-learning.js +54 -0
- package/dist/gene/propagation.d.ts +48 -0
- package/dist/gene/propagation.js +45 -0
- package/dist/gene/queue-lock.d.ts +22 -0
- package/dist/gene/queue-lock.js +99 -0
- package/dist/gene/queue-record.d.ts +57 -0
- package/dist/gene/queue-record.js +145 -0
- package/dist/gene/queue.d.ts +235 -0
- package/dist/gene/queue.js +764 -0
- package/dist/gene/read.d.ts +61 -0
- package/dist/gene/read.js +252 -0
- package/dist/gene/runlog.d.ts +101 -0
- package/dist/gene/runlog.js +404 -0
- package/dist/gene/schema-lite.d.ts +24 -0
- package/dist/gene/schema-lite.js +159 -0
- package/dist/gene/scope-guard.d.ts +23 -0
- package/dist/gene/scope-guard.js +9 -0
- package/dist/gene/scope-lock.d.ts +48 -0
- package/dist/gene/scope-lock.js +222 -0
- package/dist/gene/scope-verify.d.ts +63 -0
- package/dist/gene/scope-verify.js +123 -0
- package/dist/gene/secrets.d.ts +43 -0
- package/dist/gene/secrets.js +161 -0
- package/dist/gene/service-context.d.ts +43 -0
- package/dist/gene/service-context.js +79 -0
- package/dist/gene/service-prepare.d.ts +25 -0
- package/dist/gene/service-prepare.js +219 -0
- package/dist/gene/service-types.d.ts +196 -0
- package/dist/gene/service-types.js +1 -0
- package/dist/gene/service-view.d.ts +41 -0
- package/dist/gene/service-view.js +69 -0
- package/dist/gene/service.d.ts +114 -0
- package/dist/gene/service.js +761 -0
- package/dist/gene/session-start.d.ts +36 -0
- package/dist/gene/session-start.js +212 -0
- package/dist/gene/store-port.d.ts +88 -0
- package/dist/gene/store-port.js +10 -0
- package/dist/gene/store-rows.d.ts +36 -0
- package/dist/gene/store-rows.js +72 -0
- package/dist/gene/store.d.ts +125 -0
- package/dist/gene/store.js +622 -0
- package/dist/gene/tombstones.d.ts +47 -0
- package/dist/gene/tombstones.js +61 -0
- package/dist/gene/tool-runner.d.ts +47 -0
- package/dist/gene/tool-runner.js +154 -0
- package/dist/gene/tool-screen.d.ts +41 -0
- package/dist/gene/tool-screen.js +227 -0
- package/dist/gene/tools.d.ts +112 -0
- package/dist/gene/tools.js +539 -0
- package/dist/gene/transfer.d.ts +85 -0
- package/dist/gene/transfer.js +433 -0
- package/dist/gene/ui-hints.d.ts +15 -0
- package/dist/gene/ui-hints.js +67 -0
- package/dist/gene/usage-fold-cursor.d.ts +24 -0
- package/dist/gene/usage-fold-cursor.js +115 -0
- package/dist/gene/usage-fold.d.ts +49 -0
- package/dist/gene/usage-fold.js +397 -0
- package/dist/gene/usage.d.ts +67 -0
- package/dist/gene/usage.js +231 -0
- package/dist/gene/version-service.d.ts +118 -0
- package/dist/gene/version-service.js +453 -0
- package/dist/gene/wiring-events.d.ts +43 -0
- package/dist/gene/wiring-events.js +113 -0
- package/dist/gene/wiring-models.d.ts +46 -0
- package/dist/gene/wiring-models.js +81 -0
- package/dist/gene/wiring.d.ts +200 -0
- package/dist/gene/wiring.js +764 -0
- package/dist/governance/prompts/orchestrator-block.js +3 -2
- package/dist/host/gene.d.ts +26 -0
- package/dist/host/gene.js +24 -0
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.js +1 -0
- package/dist/team/runtime.d.ts +5 -0
- package/dist/team/runtime.js +23 -0
- package/dist/team/subagent.d.ts +34 -0
- package/dist/team/subagent.js +39 -2
- package/dist/tools/index.d.ts +36 -2
- package/dist/tools/index.js +48 -7
- package/package.json +3 -3
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cold-start import: preview, trim to what fits, commit (gene-bank spec 12.4, I2-3).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* TWO STEPS, AND THE FIRST WRITES NOTHING. `preview` reads the sources, applies every rule that
|
|
7
|
+
* can be applied without a model, and reports what a commit would queue - a directory snapshot
|
|
8
|
+
* before and after a preview is identical. `commit` re-reads the sources (a preview holds hashes,
|
|
9
|
+
* not text), refuses what changed since, and enqueues. Nothing here writes a gene: the curator
|
|
10
|
+
* drafts, validates and publishes, so a bad source costs a queue slot and some tokens, never a
|
|
11
|
+
* wrong gene.
|
|
12
|
+
*
|
|
13
|
+
* THREE LIMITS, EACH A DIFFERENT FAILURE IT PREVENTS
|
|
14
|
+
* 1. COUNT `min(gene.importMaxItems, queueRoom, sources)`. `queueRoom` is the queue's cap
|
|
15
|
+
* minus what is pending minus 100 held back for the reviewer's live proposals;
|
|
16
|
+
* an import that filled the queue would make live `p=4` proposals get dropped.
|
|
17
|
+
* 2. BUDGET estimated tokens must fit in half of today's remaining allowance; a source that
|
|
18
|
+
* does not fit is "left for next time", not dropped. The curator enforces the same
|
|
19
|
+
* half at claim time (`importClaimAllowed`), because the budget moves after a
|
|
20
|
+
* preview.
|
|
21
|
+
* 3. IDEMPOTENCE the same source at the same content is never queued twice: its dedupe key is
|
|
22
|
+
* looked for in pending, claimed, done and dead, and its reference in the ledger
|
|
23
|
+
* (`import-ledger.jsonl`), which outlives the pruning of `done/`.
|
|
24
|
+
*
|
|
25
|
+
* A SOURCE THE USER ASKED NEVER TO LEARN AGAIN (`/gene forget`) IS SKIPPED, using the tombstone
|
|
26
|
+
* store the version service uses. The preview check is a pre-filter (a source is not yet a gene, so
|
|
27
|
+
* its fingerprint is approximate); the version service's own check at publish stays the authority.
|
|
28
|
+
*
|
|
29
|
+
* THE PREVIEW TABLE is process-local: at most four previews, ten minutes each, holding references
|
|
30
|
+
* and hashes only (no text). A commit after a restart or an expiry answers `GENE_STALE_BASIS`
|
|
31
|
+
* with `preview_expired` and the person previews again.
|
|
32
|
+
*/
|
|
33
|
+
import { randomBytes } from 'node:crypto';
|
|
34
|
+
import { CHARS_PER_TOKEN } from '@aragon-agent/core';
|
|
35
|
+
import { embedFields, encodeVec } from '@aragon-agent/tui/model/gene/hashvec';
|
|
36
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
37
|
+
import { fingerprintOf } from '@aragon-agent/tui/model/gene/validate';
|
|
38
|
+
import { IMPORT_MIN_REMAINING_SHARE } from './curator/budget.js';
|
|
39
|
+
import { appendJsonl, readText } from './fs.js';
|
|
40
|
+
import { geneFail, geneOk } from './gene-errors.js';
|
|
41
|
+
import { buildImportProposal, importDedupeKey, sourceRefOf } from './import-proposal.js';
|
|
42
|
+
import { readMemorySources, readSkillSources, } from './import-sources.js';
|
|
43
|
+
import { importLedgerFile } from './paths.js';
|
|
44
|
+
import { TombstoneStore } from './tombstones.js';
|
|
45
|
+
/** Items held back for the reviewer's live proposals (the queue is never filled by an import). */
|
|
46
|
+
export const QUEUE_RESERVE = 100;
|
|
47
|
+
/** Prompt, tools and the drafted answer, on top of reading the source (a planning estimate). */
|
|
48
|
+
export const IMPORT_TOKEN_OVERHEAD = 1500;
|
|
49
|
+
const PREVIEW_TTL_MS = 10 * 60 * 1000;
|
|
50
|
+
const PREVIEW_SLOTS = 4;
|
|
51
|
+
const TITLES_SHOWN = 8;
|
|
52
|
+
const LEDGER_MAX_BYTES = 8 * 1024 * 1024;
|
|
53
|
+
function bump(into, reason, by = 1) {
|
|
54
|
+
into[reason] = (into[reason] ?? 0) + by;
|
|
55
|
+
}
|
|
56
|
+
/** Newest first; the id breaks ties so two previews of one state list the same order. */
|
|
57
|
+
function recentFirst(a, b) {
|
|
58
|
+
return b.mtimeMs - a.mtimeMs || a.sourceId.localeCompare(b.sourceId);
|
|
59
|
+
}
|
|
60
|
+
export function estimateImportTokens(item) {
|
|
61
|
+
return Math.ceil(item.text.length / CHARS_PER_TOKEN) + IMPORT_TOKEN_OVERHEAD;
|
|
62
|
+
}
|
|
63
|
+
export class GeneImporter {
|
|
64
|
+
deps;
|
|
65
|
+
previews = new Map();
|
|
66
|
+
tombstones;
|
|
67
|
+
constructor(deps) {
|
|
68
|
+
this.deps = deps;
|
|
69
|
+
this.tombstones = new TombstoneStore(deps.core.lib.store, deps.core.now);
|
|
70
|
+
}
|
|
71
|
+
// ---- reading -----------------------------------------------------------------
|
|
72
|
+
read(source) {
|
|
73
|
+
const { core } = this.deps;
|
|
74
|
+
const project = core.project();
|
|
75
|
+
if (source === 'memory')
|
|
76
|
+
return geneOk(readMemorySources(project, core.secrets()));
|
|
77
|
+
const port = this.deps.skills?.() ?? null;
|
|
78
|
+
if (port === null) {
|
|
79
|
+
return geneFail('GENE_UNAVAILABLE', 'Installed skills are not available here.', 'skills_unavailable');
|
|
80
|
+
}
|
|
81
|
+
return geneOk(readSkillSources(port, project, core.secrets()));
|
|
82
|
+
}
|
|
83
|
+
// ---- idempotence and the user's refusals ---------------------------------------
|
|
84
|
+
/** Dedupe keys of every proposal in any queue state, and every ledger reference. */
|
|
85
|
+
async knownKeys() {
|
|
86
|
+
const keys = new Set();
|
|
87
|
+
for (const state of ['pending', 'claimed', 'done', 'dead']) {
|
|
88
|
+
for (const entry of await this.deps.core.lib.queue.list(state)) {
|
|
89
|
+
const key = entry.item?.proposal.dedupeKey;
|
|
90
|
+
if (key !== undefined)
|
|
91
|
+
keys.add(key);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { keys, refs: await this.ledgerRefs() };
|
|
95
|
+
}
|
|
96
|
+
async ledgerRefs() {
|
|
97
|
+
const text = await readText(importLedgerFile(this.deps.core.lib.ctx));
|
|
98
|
+
const refs = new Set();
|
|
99
|
+
if (text === null || text.length > LEDGER_MAX_BYTES)
|
|
100
|
+
return refs;
|
|
101
|
+
for (const line of text.split('\n')) {
|
|
102
|
+
try {
|
|
103
|
+
const ref = JSON.parse(line).ref;
|
|
104
|
+
if (typeof ref === 'string')
|
|
105
|
+
refs.add(ref);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// A torn line is a record that was never completed.
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return refs;
|
|
112
|
+
}
|
|
113
|
+
async isForgotten(item) {
|
|
114
|
+
const parts = { name: item.title, purpose: item.text.slice(0, 160), trigger: '' };
|
|
115
|
+
const hit = await this.tombstones.isForgotten({
|
|
116
|
+
fingerprint: fingerprintOf(item.type, item.scope.kind, parts),
|
|
117
|
+
vec: encodeVec(embedFields({ ...parts, tags: [], body: item.text })),
|
|
118
|
+
scope: item.scope,
|
|
119
|
+
type: item.type,
|
|
120
|
+
});
|
|
121
|
+
return hit !== null;
|
|
122
|
+
}
|
|
123
|
+
/** Drop what was imported before or was forgotten on purpose; count each reason. */
|
|
124
|
+
async admissible(items, skipped) {
|
|
125
|
+
const known = await this.knownKeys();
|
|
126
|
+
const projectAllowed = (await this.deps.projectMayLearn?.()) ?? true;
|
|
127
|
+
const kept = [];
|
|
128
|
+
for (const item of items) {
|
|
129
|
+
if (item.scope.kind === 'project' && !projectAllowed) {
|
|
130
|
+
bump(skipped, 'project_excluded');
|
|
131
|
+
}
|
|
132
|
+
else if (known.keys.has(importDedupeKey(item)) || known.refs.has(sourceRefOf(item))) {
|
|
133
|
+
bump(skipped, 'already_imported');
|
|
134
|
+
}
|
|
135
|
+
else if (await this.isForgotten(item)) {
|
|
136
|
+
bump(skipped, 'forgotten');
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
kept.push(item);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return kept;
|
|
143
|
+
}
|
|
144
|
+
// ---- the three limits ------------------------------------------------------------
|
|
145
|
+
async queueRoom() {
|
|
146
|
+
const counts = await this.deps.core.lib.queue.counts();
|
|
147
|
+
return Math.max(0, GENE_LIMITS.queueMax - counts.pending - QUEUE_RESERVE);
|
|
148
|
+
}
|
|
149
|
+
async budgetOf() {
|
|
150
|
+
const dailyTokens = this.deps.core.config().dailyTokens;
|
|
151
|
+
const remaining = Math.max(0, dailyTokens - (await this.deps.tokensToday()));
|
|
152
|
+
const allowed = Math.floor(remaining * IMPORT_MIN_REMAINING_SHARE);
|
|
153
|
+
return { dailyTokens, remaining, allowed, exceeded: false };
|
|
154
|
+
}
|
|
155
|
+
/** The newest sources that fit the count, the room and the token allowance. */
|
|
156
|
+
trim(items, limits) {
|
|
157
|
+
const cap = Math.min(limits.count, limits.room);
|
|
158
|
+
const chosen = [];
|
|
159
|
+
let tokens = 0;
|
|
160
|
+
let overBudget = false;
|
|
161
|
+
for (const item of [...items].sort(recentFirst)) {
|
|
162
|
+
if (chosen.length >= cap)
|
|
163
|
+
break;
|
|
164
|
+
const cost = estimateImportTokens(item);
|
|
165
|
+
if (tokens + cost > limits.tokens) {
|
|
166
|
+
overBudget = true;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
chosen.push(item);
|
|
170
|
+
tokens += cost;
|
|
171
|
+
}
|
|
172
|
+
return { chosen, tokens, overBudget };
|
|
173
|
+
}
|
|
174
|
+
// ---- preview -----------------------------------------------------------------------
|
|
175
|
+
/** Report what a commit would queue. Writes nothing. */
|
|
176
|
+
async preview(source, limit) {
|
|
177
|
+
const read = this.read(source);
|
|
178
|
+
if (!read.ok)
|
|
179
|
+
return read;
|
|
180
|
+
const skipped = {};
|
|
181
|
+
for (const skip of read.value.skipped)
|
|
182
|
+
bump(skipped, skip.reason);
|
|
183
|
+
const eligible = await this.admissible(read.value.items, skipped);
|
|
184
|
+
const budget = await this.budgetOf();
|
|
185
|
+
const room = await this.queueRoom();
|
|
186
|
+
const count = Math.min(this.deps.core.config().importMaxItems, limit ?? Number.MAX_SAFE_INTEGER);
|
|
187
|
+
const { chosen, tokens, overBudget } = this.trim(eligible, { count, room, tokens: budget.allowed });
|
|
188
|
+
const previewId = this.remember(source, chosen);
|
|
189
|
+
return geneOk({
|
|
190
|
+
previewId,
|
|
191
|
+
source,
|
|
192
|
+
eligible: eligible.length,
|
|
193
|
+
willQueue: chosen.length,
|
|
194
|
+
leftForLater: eligible.length - chosen.length,
|
|
195
|
+
skipped,
|
|
196
|
+
estimatedTokens: tokens,
|
|
197
|
+
budget: { ...budget, exceeded: overBudget },
|
|
198
|
+
queueRoom: room,
|
|
199
|
+
titles: chosen.slice(0, TITLES_SHOWN).map((item) => item.title.replace(/\s+/g, ' ').slice(0, 60)),
|
|
200
|
+
provider: this.deps.provider(),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
remember(source, chosen) {
|
|
204
|
+
const now = this.deps.core.now();
|
|
205
|
+
for (const [id, held] of this.previews) {
|
|
206
|
+
if (now - held.at > PREVIEW_TTL_MS)
|
|
207
|
+
this.previews.delete(id);
|
|
208
|
+
}
|
|
209
|
+
const previewId = `pv_${randomBytes(5).toString('hex')}`;
|
|
210
|
+
this.previews.set(previewId, {
|
|
211
|
+
source,
|
|
212
|
+
at: now,
|
|
213
|
+
refs: chosen.map((item) => ({ sourceId: item.sourceId, contentHash: item.contentHash })),
|
|
214
|
+
});
|
|
215
|
+
while (this.previews.size > PREVIEW_SLOTS) {
|
|
216
|
+
const oldest = this.previews.keys().next().value;
|
|
217
|
+
this.previews.delete(oldest);
|
|
218
|
+
}
|
|
219
|
+
return previewId;
|
|
220
|
+
}
|
|
221
|
+
// ---- commit -----------------------------------------------------------------------------
|
|
222
|
+
/** Queue what the preview chose, minus what changed since. A preview is single-use. */
|
|
223
|
+
async commit(previewId) {
|
|
224
|
+
const held = this.previews.get(previewId);
|
|
225
|
+
this.previews.delete(previewId);
|
|
226
|
+
const now = this.deps.core.now();
|
|
227
|
+
if (held === undefined || now - held.at > PREVIEW_TTL_MS) {
|
|
228
|
+
return geneFail('GENE_STALE_BASIS', 'That preview has expired; preview again.', 'preview_expired');
|
|
229
|
+
}
|
|
230
|
+
const read = this.read(held.source);
|
|
231
|
+
if (!read.ok)
|
|
232
|
+
return read;
|
|
233
|
+
const byId = new Map(read.value.items.map((item) => [item.sourceId, item]));
|
|
234
|
+
const skipped = {};
|
|
235
|
+
let changed = 0;
|
|
236
|
+
const fresh = [];
|
|
237
|
+
for (const ref of held.refs) {
|
|
238
|
+
const item = byId.get(ref.sourceId);
|
|
239
|
+
if (item === undefined || item.contentHash !== ref.contentHash)
|
|
240
|
+
changed += 1;
|
|
241
|
+
else
|
|
242
|
+
fresh.push(item);
|
|
243
|
+
}
|
|
244
|
+
const admitted = await this.admissible(fresh, skipped);
|
|
245
|
+
const room = await this.queueRoom();
|
|
246
|
+
const chosen = admitted.slice(0, room);
|
|
247
|
+
const outcome = await this.enqueueAll(chosen, previewId);
|
|
248
|
+
return geneOk({
|
|
249
|
+
...outcome,
|
|
250
|
+
changed,
|
|
251
|
+
leftForLater: admitted.length - chosen.length,
|
|
252
|
+
skipped,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async enqueueAll(items, previewId) {
|
|
256
|
+
const { core } = this.deps;
|
|
257
|
+
const random = this.deps.random ?? ((n) => Math.floor(Math.random() * n));
|
|
258
|
+
let queued = 0;
|
|
259
|
+
let merged = 0;
|
|
260
|
+
let failed = 0;
|
|
261
|
+
for (const [index, item] of items.entries()) {
|
|
262
|
+
const proposal = buildImportProposal(item, {
|
|
263
|
+
now: core.now(),
|
|
264
|
+
sessionId: `import-${previewId}`,
|
|
265
|
+
index,
|
|
266
|
+
random,
|
|
267
|
+
knownSecrets: core.secrets(),
|
|
268
|
+
});
|
|
269
|
+
const result = await core.lib.queue.enqueue(proposal);
|
|
270
|
+
if (!result.ok) {
|
|
271
|
+
failed += 1;
|
|
272
|
+
if (result.reason === 'queue_full')
|
|
273
|
+
break;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (result.merged)
|
|
277
|
+
merged += 1;
|
|
278
|
+
else
|
|
279
|
+
queued += 1;
|
|
280
|
+
await this.record(item, result.proposalId);
|
|
281
|
+
}
|
|
282
|
+
return { queued, merged, failed };
|
|
283
|
+
}
|
|
284
|
+
/** A ledger write failing only means a later import may re-check the queue states instead. */
|
|
285
|
+
async record(item, proposalId) {
|
|
286
|
+
try {
|
|
287
|
+
const line = { t: this.deps.core.now(), ref: sourceRefOf(item), proposalId };
|
|
288
|
+
await appendJsonl(importLedgerFile(this.deps.core.lib.ctx), line);
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
// See above: the queue states remain the primary idempotence check.
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GeneEvidenceInput, GeneProposal } from '@aragon-agent/tui/model/gene/types';
|
|
2
|
+
import { type ImportItem } from './import-sources.js';
|
|
3
|
+
export declare const IMPORT_PRIORITY: 4;
|
|
4
|
+
/** `<sourceKind>:<sourceId>@<contentHash>`: the stable reference of a source at one content. */
|
|
5
|
+
export declare function sourceRefOf(item: Pick<ImportItem, 'sourceKind' | 'sourceId' | 'contentHash'>): string;
|
|
6
|
+
/** The fingerprint the queue's dedupe key is built from. */
|
|
7
|
+
export declare function importFingerprint(item: Pick<ImportItem, 'sourceKind' | 'sourceId' | 'contentHash'>): string;
|
|
8
|
+
/** The dedupe key a proposal for `item` has (also what the importer looks for in every state). */
|
|
9
|
+
export declare function importDedupeKey(item: ImportItem): string;
|
|
10
|
+
/** The text cut into evidence entries of at most `proposalEvidenceTextMax` characters. */
|
|
11
|
+
export declare function evidenceChunks(text: string): string[];
|
|
12
|
+
export declare function importEvidence(item: ImportItem, knownSecrets: readonly string[]): GeneEvidenceInput[];
|
|
13
|
+
export interface ImportProposalContext {
|
|
14
|
+
now: number;
|
|
15
|
+
/** Shared by every proposal of one import run: `import-<previewId>`. */
|
|
16
|
+
sessionId: string;
|
|
17
|
+
/** Position within the run, for the unique `runId`. */
|
|
18
|
+
index: number;
|
|
19
|
+
random: (n: number) => number;
|
|
20
|
+
knownSecrets: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
/** A queue-ready `create` proposal for one source. */
|
|
23
|
+
export declare function buildImportProposal(item: ImportItem, ctx: ImportProposalContext): GeneProposal;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The proposal a cold-start import queues for one source (gene-bank spec 10.5, I2-3 rules 1, 4, 5).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`. PURE: no I/O, no clock.
|
|
5
|
+
*
|
|
6
|
+
* AN IMPORT IS A CREATE REQUEST, NOT A GENE. The proposal carries the source's original text as
|
|
7
|
+
* evidence and nothing the curator has to take on trust: no draft, no name, no body. The curator
|
|
8
|
+
* drafts the English candidate and validation decides, exactly as for a learned proposal.
|
|
9
|
+
*
|
|
10
|
+
* THE FIELDS THAT MATTER FOR SCHEDULING AND FOR IDEMPOTENCE
|
|
11
|
+
* - `origin.source: 'import'`, `priority: 4` - the LOWEST class, so a cold-start import is
|
|
12
|
+
* drained after everything live, and when the queue is full the new import is the one dropped.
|
|
13
|
+
* - `dedupeKey` is the queue's own formula (`kind | scope | fingerprint | basis`) with the
|
|
14
|
+
* fingerprint `sha1(sourceKind:sourceId:contentHash)`, so the queue folds a repeated request by
|
|
15
|
+
* itself; the importer additionally checks this key against every queue state.
|
|
16
|
+
* - The source identity rides on the FIRST evidence entry (`source.path` is the reference,
|
|
17
|
+
* `source.hash` the content hash), because a proposal has no field of its own for it.
|
|
18
|
+
*
|
|
19
|
+
* EVIDENCE IS REDACTED AGAIN here (e-mail, phone, configured keys) even though the secret screen
|
|
20
|
+
* already refused anything that looked like a credential: the queue file is a record that outlives
|
|
21
|
+
* the source.
|
|
22
|
+
*/
|
|
23
|
+
import { escapeGeneText } from '@aragon-agent/tui/model/gene/escape';
|
|
24
|
+
import { mintProposalId } from '@aragon-agent/tui/model/gene/ids';
|
|
25
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
26
|
+
import { dedupeKeyOf } from '@aragon-agent/tui/model/gene/proposal';
|
|
27
|
+
import { sha1Of } from './import-sources.js';
|
|
28
|
+
import { redactSecrets } from './secrets.js';
|
|
29
|
+
export const IMPORT_PRIORITY = 4;
|
|
30
|
+
const TITLE_MAX = 80;
|
|
31
|
+
const WHY = {
|
|
32
|
+
'import-memory': 'The user already keeps this as a memory note; a validated gene makes it reusable.',
|
|
33
|
+
'import-skills': 'The user already has this skill installed; a validated gene makes it reusable.',
|
|
34
|
+
};
|
|
35
|
+
/** `<sourceKind>:<sourceId>@<contentHash>`: the stable reference of a source at one content. */
|
|
36
|
+
export function sourceRefOf(item) {
|
|
37
|
+
return `${item.sourceKind}:${item.sourceId}@${item.contentHash}`;
|
|
38
|
+
}
|
|
39
|
+
/** The fingerprint the queue's dedupe key is built from. */
|
|
40
|
+
export function importFingerprint(item) {
|
|
41
|
+
return sha1Of(`${item.sourceKind}:${item.sourceId}:${item.contentHash}`);
|
|
42
|
+
}
|
|
43
|
+
/** The dedupe key a proposal for `item` has (also what the importer looks for in every state). */
|
|
44
|
+
export function importDedupeKey(item) {
|
|
45
|
+
return dedupeKeyOf({ kind: 'create', scope: item.scope, fingerprint: importFingerprint(item) });
|
|
46
|
+
}
|
|
47
|
+
/** The text cut into evidence entries of at most `proposalEvidenceTextMax` characters. */
|
|
48
|
+
export function evidenceChunks(text) {
|
|
49
|
+
const size = GENE_LIMITS.proposalEvidenceTextMax;
|
|
50
|
+
const chunks = [];
|
|
51
|
+
for (let at = 0; at < text.length; at += size)
|
|
52
|
+
chunks.push(text.slice(at, at + size));
|
|
53
|
+
return chunks;
|
|
54
|
+
}
|
|
55
|
+
export function importEvidence(item, knownSecrets) {
|
|
56
|
+
const chunks = evidenceChunks(redactSecrets(item.text, knownSecrets).text);
|
|
57
|
+
return chunks.map((text, index) => ({
|
|
58
|
+
kind: 'file',
|
|
59
|
+
trust: item.trust,
|
|
60
|
+
text,
|
|
61
|
+
...(index === 0
|
|
62
|
+
? { source: { path: sourceRefOf(item), hash: item.contentHash } }
|
|
63
|
+
: { source: { hash: item.contentHash } }),
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
/** A queue-ready `create` proposal for one source. */
|
|
67
|
+
export function buildImportProposal(item, ctx) {
|
|
68
|
+
const label = escapeGeneText(item.title, 'line').slice(0, TITLE_MAX);
|
|
69
|
+
return {
|
|
70
|
+
schema: 1,
|
|
71
|
+
id: mintProposalId(ctx.random),
|
|
72
|
+
createdAt: ctx.now,
|
|
73
|
+
kind: 'create',
|
|
74
|
+
type: item.type,
|
|
75
|
+
scope: item.scope,
|
|
76
|
+
origin: {
|
|
77
|
+
source: 'import',
|
|
78
|
+
sessionId: ctx.sessionId,
|
|
79
|
+
runId: `${ctx.sessionId}-${ctx.index + 1}`,
|
|
80
|
+
...(item.scope.kind === 'project' && item.scope.key !== undefined ? { projectKey: item.scope.key } : {}),
|
|
81
|
+
taskLabel: label,
|
|
82
|
+
goal: 'Cold-start import of an existing note or skill.',
|
|
83
|
+
},
|
|
84
|
+
why: WHY[item.sourceKind],
|
|
85
|
+
expectedBenefit: 'Reusable after validation; nothing is used by an Agent before it is published.',
|
|
86
|
+
...(item.preferenceHint
|
|
87
|
+
? {
|
|
88
|
+
draft: {
|
|
89
|
+
payload: {
|
|
90
|
+
kind: 'preference',
|
|
91
|
+
preference: { horizon: 'long_term', statedBy: 'inferred', confirmed: false },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
: {}),
|
|
96
|
+
evidence: importEvidence(item, ctx.knownSecrets),
|
|
97
|
+
priority: IMPORT_PRIORITY,
|
|
98
|
+
dedupeKey: importDedupeKey(item),
|
|
99
|
+
merged: 0,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SkillService } from '../skills/service.js';
|
|
2
|
+
import type { SkillSourcePort } from './import-sources.js';
|
|
3
|
+
/** The skills of `service` as the import adapter reads them: description first, then the body. */
|
|
4
|
+
export declare function skillPortOf(service: SkillService): SkillSourcePort;
|
|
5
|
+
/** A read-only skill service over the persisted configuration; null when it cannot be built. */
|
|
6
|
+
export declare function createStandaloneSkillPort(cwd: string): SkillSourcePort | null;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the skills import gets its skills (gene-bank spec 12.4, I2-3).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* It reuses `SkillService` - discovery, scope precedence, disabled/invalid flags - and never builds
|
|
7
|
+
* a `SkillRegistry` of its own: two scanners would disagree about which `SKILL.md` wins. A live
|
|
8
|
+
* session hands over ITS service (`skillPortOf`); the desktop main process and `aragon gene
|
|
9
|
+
* import-skills` have none, so `createStandaloneSkillPort` builds a read-only one the way
|
|
10
|
+
* `aragon skills` does (no approval gate: it only reads).
|
|
11
|
+
*/
|
|
12
|
+
import { loadConfig } from '../config/load.js';
|
|
13
|
+
import { createNodeSkillHost } from '../skills/node-host.js';
|
|
14
|
+
import { SkillService } from '../skills/service.js';
|
|
15
|
+
const DENY_ALL = { canPrompt: () => false, request: async () => false };
|
|
16
|
+
/** The skills of `service` as the import adapter reads them: description first, then the body. */
|
|
17
|
+
export function skillPortOf(service) {
|
|
18
|
+
return {
|
|
19
|
+
list: () => service.list(),
|
|
20
|
+
read(name) {
|
|
21
|
+
try {
|
|
22
|
+
const record = service.get(name);
|
|
23
|
+
if (record === undefined)
|
|
24
|
+
return null;
|
|
25
|
+
const { body } = service.loadBody(name, { countUse: false });
|
|
26
|
+
return `${record.description}\n\n${body}`;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** A read-only skill service over the persisted configuration; null when it cannot be built. */
|
|
35
|
+
export function createStandaloneSkillPort(cwd) {
|
|
36
|
+
try {
|
|
37
|
+
const config = loadConfig({ cwd }, { quiet: true });
|
|
38
|
+
const service = new SkillService({
|
|
39
|
+
host: createNodeSkillHost(),
|
|
40
|
+
getCwd: () => cwd,
|
|
41
|
+
config: config.skills,
|
|
42
|
+
runtime: { forcedSkills: [], approveAll: false },
|
|
43
|
+
approval: DENY_ALL,
|
|
44
|
+
notify: () => undefined,
|
|
45
|
+
persist: () => undefined,
|
|
46
|
+
});
|
|
47
|
+
service.discover();
|
|
48
|
+
return skillPortOf(service);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { SkillRecord } from '@aragon-agent/core';
|
|
2
|
+
import type { GeneScope, GeneType } from '@aragon-agent/tui/model/gene/types';
|
|
3
|
+
export type ImportSourceKind = 'import-memory' | 'import-skills';
|
|
4
|
+
export type SkipReason = 'secret' | 'injection' | 'too_long' | 'disabled' | 'empty' | 'unreadable' | 'already_imported' | 'forgotten' | 'project_excluded';
|
|
5
|
+
export interface ImportItem {
|
|
6
|
+
sourceKind: ImportSourceKind;
|
|
7
|
+
/** Stable across runs: `<scope>:<noteId>` or `<scope>:<skillName>`. */
|
|
8
|
+
sourceId: string;
|
|
9
|
+
title: string;
|
|
10
|
+
/** The original text, unmodified: it becomes the evidence. */
|
|
11
|
+
text: string;
|
|
12
|
+
/** sha1 of the text; a changed source is a new source. */
|
|
13
|
+
contentHash: string;
|
|
14
|
+
mtimeMs: number;
|
|
15
|
+
scope: GeneScope;
|
|
16
|
+
type: GeneType;
|
|
17
|
+
/** The note reads like a personal preference: it is drafted as an unconfirmed one. */
|
|
18
|
+
preferenceHint: boolean;
|
|
19
|
+
/** Where the author is trusted from: a note the agent wrote is not the user's own words. */
|
|
20
|
+
trust: 'user' | 'agent' | 'untrusted';
|
|
21
|
+
}
|
|
22
|
+
export interface SkippedSource {
|
|
23
|
+
sourceId: string;
|
|
24
|
+
reason: SkipReason;
|
|
25
|
+
}
|
|
26
|
+
export interface SourceRead {
|
|
27
|
+
items: ImportItem[];
|
|
28
|
+
skipped: SkippedSource[];
|
|
29
|
+
}
|
|
30
|
+
/** What the skills adapter needs from a session's `SkillService` (never a new registry). */
|
|
31
|
+
export interface SkillSourcePort {
|
|
32
|
+
list(): readonly SkillRecord[];
|
|
33
|
+
/** The text of `SKILL.md`, or null when it cannot be read. */
|
|
34
|
+
read(name: string): string | null;
|
|
35
|
+
}
|
|
36
|
+
/** The longest source an import accepts: what a proposal can carry as evidence. */
|
|
37
|
+
export declare const IMPORT_SOURCE_MAX_CHARS: number;
|
|
38
|
+
export declare function sha1Of(text: string): string;
|
|
39
|
+
/** One verdict for a source's text, or null when it is acceptable. */
|
|
40
|
+
export declare function screenSourceText(text: string, knownSecrets: readonly string[]): Exclude<SkipReason, 'disabled' | 'unreadable' | 'already_imported' | 'forgotten' | 'project_excluded'> | null;
|
|
41
|
+
/** Memory notes of the user scope and this project, as knowledge candidates. */
|
|
42
|
+
export declare function readMemorySources(project: {
|
|
43
|
+
key: string;
|
|
44
|
+
} | null, knownSecrets: readonly string[]): SourceRead;
|
|
45
|
+
/** The skills a session has installed (not the ones shipped with the product), as SKILL candidates. */
|
|
46
|
+
export declare function readSkillSources(port: SkillSourcePort, project: {
|
|
47
|
+
key: string;
|
|
48
|
+
} | null, knownSecrets: readonly string[]): SourceRead;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source adapters for the cold-start import (gene-bank spec 12.4, iteration-2 I2-3).
|
|
3
|
+
*
|
|
4
|
+
* ASCII ONLY - the whole `gene/` tree is in `glyphs.test.ts::inScope`.
|
|
5
|
+
*
|
|
6
|
+
* PURE READS. An adapter turns what the user already has - memory notes, installed skills - into
|
|
7
|
+
* `ImportItem`s and reports what it left out and why. It writes nothing, queues nothing and does
|
|
8
|
+
* not translate: the original text travels as evidence and the curator drafts the English gene.
|
|
9
|
+
*
|
|
10
|
+
* WHAT IS REFUSED HERE, AND WHY EACH IS A SKIP AND NOT A REPAIR
|
|
11
|
+
* - `secret` the text matches the secret screen: the whole source is skipped. A partly
|
|
12
|
+
* scrubbed note is a note nobody checked.
|
|
13
|
+
* - `injection` the text reads like instructions aimed at an Agent (the same screen the
|
|
14
|
+
* version service applies to a draft). A third-party skill is the likely source.
|
|
15
|
+
* - `too_long` more than the proposal can carry as evidence (12 x 1500 characters). It is NOT
|
|
16
|
+
* cut: a gene drafted from half a source, with nothing saying so, is worse than
|
|
17
|
+
* no gene.
|
|
18
|
+
* - `disabled` the user switched the note or skill off, so it is not part of their working
|
|
19
|
+
* set. `invalid` skills are the same.
|
|
20
|
+
* - `empty` / `unreadable`
|
|
21
|
+
*
|
|
22
|
+
* SKILLS THAT SHIP WITH THE PRODUCT (`bundled` scope) ARE NOT THE USER'S EXPERIENCE and are not
|
|
23
|
+
* imported. Scripts a skill references are never turned into tool genes: a tool gene needs a
|
|
24
|
+
* schema and a validation, and neither can be inferred from an arbitrary script. They are noted
|
|
25
|
+
* in the evidence text as "referenced files" so a person can see them.
|
|
26
|
+
*/
|
|
27
|
+
import { createHash } from 'node:crypto';
|
|
28
|
+
import { GENE_LIMITS } from '@aragon-agent/tui/model/gene/limits';
|
|
29
|
+
import { screenInjection } from '@aragon-agent/tui/model/gene/validate-base';
|
|
30
|
+
import { listCatalog, readNote } from '../memory/store.js';
|
|
31
|
+
import { screenSecrets } from './secrets.js';
|
|
32
|
+
/** The longest source an import accepts: what a proposal can carry as evidence. */
|
|
33
|
+
export const IMPORT_SOURCE_MAX_CHARS = GENE_LIMITS.proposalEvidenceMax * GENE_LIMITS.proposalEvidenceTextMax;
|
|
34
|
+
const PREFERENCE_TAG = /prefer|style|habit|convention/i;
|
|
35
|
+
export function sha1Of(text) {
|
|
36
|
+
return createHash('sha1').update(text).digest('hex');
|
|
37
|
+
}
|
|
38
|
+
/** One verdict for a source's text, or null when it is acceptable. */
|
|
39
|
+
export function screenSourceText(text, knownSecrets) {
|
|
40
|
+
if (text.trim().length === 0)
|
|
41
|
+
return 'empty';
|
|
42
|
+
if (text.length > IMPORT_SOURCE_MAX_CHARS)
|
|
43
|
+
return 'too_long';
|
|
44
|
+
if (screenSecrets(text, knownSecrets).hit)
|
|
45
|
+
return 'secret';
|
|
46
|
+
if (screenInjection(text) !== null)
|
|
47
|
+
return 'injection';
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
function noteTargets(project) {
|
|
51
|
+
const global = { target: { scope: 'global' }, scope: { kind: 'user' } };
|
|
52
|
+
if (project === null)
|
|
53
|
+
return [global];
|
|
54
|
+
return [
|
|
55
|
+
global,
|
|
56
|
+
{
|
|
57
|
+
target: { scope: 'project', projectKey: project.key },
|
|
58
|
+
scope: { kind: 'project', key: project.key },
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
/** Memory notes of the user scope and this project, as knowledge candidates. */
|
|
63
|
+
export function readMemorySources(project, knownSecrets) {
|
|
64
|
+
const out = { items: [], skipped: [] };
|
|
65
|
+
for (const { target, scope } of noteTargets(project)) {
|
|
66
|
+
const catalog = listCatalog(target);
|
|
67
|
+
for (let n = 0; n < catalog.skipped; n += 1) {
|
|
68
|
+
out.skipped.push({ sourceId: `${scope.kind}:unreadable-${n + 1}`, reason: 'unreadable' });
|
|
69
|
+
}
|
|
70
|
+
for (const row of catalog.rows) {
|
|
71
|
+
const sourceId = `${scope.kind}:${row.id}`;
|
|
72
|
+
if (!row.enabled) {
|
|
73
|
+
out.skipped.push({ sourceId, reason: 'disabled' });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const note = readNote(target, row.id);
|
|
77
|
+
if (!note.ok) {
|
|
78
|
+
out.skipped.push({ sourceId, reason: 'unreadable' });
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const text = `${note.value.title}\n\n${note.value.body}`.trim();
|
|
82
|
+
const verdict = screenSourceText(text, knownSecrets);
|
|
83
|
+
if (verdict !== null) {
|
|
84
|
+
out.skipped.push({ sourceId, reason: verdict });
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
out.items.push({
|
|
88
|
+
sourceKind: 'import-memory',
|
|
89
|
+
sourceId,
|
|
90
|
+
title: note.value.title,
|
|
91
|
+
text,
|
|
92
|
+
contentHash: sha1Of(text),
|
|
93
|
+
mtimeMs: note.value.updatedAt,
|
|
94
|
+
scope,
|
|
95
|
+
type: 'knowledge',
|
|
96
|
+
preferenceHint: note.value.tags.some((tag) => PREFERENCE_TAG.test(tag)),
|
|
97
|
+
trust: note.value.source === 'agent' ? 'agent' : 'user',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/** The skills a session has installed (not the ones shipped with the product), as SKILL candidates. */
|
|
104
|
+
export function readSkillSources(port, project, knownSecrets) {
|
|
105
|
+
const out = { items: [], skipped: [] };
|
|
106
|
+
for (const record of port.list()) {
|
|
107
|
+
if (record.scope === 'bundled')
|
|
108
|
+
continue;
|
|
109
|
+
const sourceId = `${record.scope}:${record.name}`;
|
|
110
|
+
if (record.disabled || record.invalid) {
|
|
111
|
+
out.skipped.push({ sourceId, reason: 'disabled' });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const raw = port.read(record.name);
|
|
115
|
+
if (raw === null) {
|
|
116
|
+
out.skipped.push({ sourceId, reason: 'unreadable' });
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const text = raw.trim();
|
|
120
|
+
const verdict = screenSourceText(text, knownSecrets);
|
|
121
|
+
if (verdict !== null) {
|
|
122
|
+
out.skipped.push({ sourceId, reason: verdict });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const projectScoped = record.scope === 'project' && project !== null;
|
|
126
|
+
out.items.push({
|
|
127
|
+
sourceKind: 'import-skills',
|
|
128
|
+
sourceId,
|
|
129
|
+
title: record.name,
|
|
130
|
+
text,
|
|
131
|
+
contentHash: sha1Of(text),
|
|
132
|
+
mtimeMs: record.manifest?.updatedAt ?? 0,
|
|
133
|
+
scope: projectScoped ? { kind: 'project', key: project.key } : { kind: 'user' },
|
|
134
|
+
type: 'skill',
|
|
135
|
+
preferenceHint: false,
|
|
136
|
+
trust: 'untrusted',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|