@gamaze/hicortex 0.27.4 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/dashboard.html +1898 -3211
- package/assets/viz.html +229 -2
- package/dist/backfill-privacy.d.ts +68 -45
- package/dist/backfill-privacy.js +236 -145
- package/dist/calibration.d.ts +67 -68
- package/dist/calibration.js +75 -78
- package/dist/capture-health.d.ts +2 -2
- package/dist/capture-health.js +2 -2
- package/dist/classification.d.ts +141 -12
- package/dist/classification.js +214 -27
- package/dist/classify-domains.d.ts +1 -1
- package/dist/classify-domains.js +2 -2
- package/dist/cli-args.d.ts +5 -3
- package/dist/cli-args.js +7 -3
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +64 -16
- package/dist/cluster.d.ts +12 -9
- package/dist/cluster.js +9 -6
- package/dist/consolidate.d.ts +93 -43
- package/dist/consolidate.js +255 -119
- package/dist/dashboard.d.ts +48 -5
- package/dist/dashboard.js +55 -3
- package/dist/db.js +27 -6
- package/dist/dedup.d.ts +2 -3
- package/dist/dedup.js +2 -3
- package/dist/distill-queue.d.ts +36 -4
- package/dist/distill-queue.js +80 -18
- package/dist/distiller.d.ts +1 -1
- package/dist/distiller.js +13 -12
- package/dist/domain-classify.d.ts +8 -9
- package/dist/domain-classify.js +6 -7
- package/dist/graph.d.ts +15 -5
- package/dist/graph.js +46 -13
- package/dist/hermes-transcript-reader.d.ts +1 -1
- package/dist/hermes-transcript-reader.js +1 -1
- package/dist/init.d.ts +30 -2
- package/dist/init.js +48 -3
- package/dist/llm.d.ts +1 -1
- package/dist/llm.js +5 -5
- package/dist/mcp-server.js +323 -59
- package/dist/memory-instructions.d.ts +2 -2
- package/dist/memory-instructions.js +2 -2
- package/dist/nightly.d.ts +1 -1
- package/dist/nightly.js +64 -5
- package/dist/nofit.d.ts +2 -2
- package/dist/nofit.js +2 -2
- package/dist/observed-agents.d.ts +54 -0
- package/dist/observed-agents.js +78 -0
- package/dist/project-canonicalize.d.ts +6 -5
- package/dist/project-canonicalize.js +6 -5
- package/dist/prompts.d.ts +29 -8
- package/dist/prompts.js +39 -16
- package/dist/quality-scoreboard.d.ts +1 -1
- package/dist/recall-hook-cli.d.ts +1 -1
- package/dist/recall-hook-cli.js +2 -2
- package/dist/recall-index.d.ts +28 -14
- package/dist/recall-index.js +22 -10
- package/dist/recall-precision.d.ts +3 -3
- package/dist/recall-precision.js +5 -4
- package/dist/reconcile-projects.d.ts +2 -2
- package/dist/reconcile-projects.js +2 -2
- package/dist/reconsolidation.d.ts +9 -2
- package/dist/reconsolidation.js +48 -31
- package/dist/relink.d.ts +2 -2
- package/dist/relink.js +4 -4
- package/dist/rescore-importance.d.ts +2 -2
- package/dist/rescore-importance.js +22 -3
- package/dist/retrieval.d.ts +14 -0
- package/dist/retrieval.js +11 -11
- package/dist/schema-prototypes.d.ts +1 -1
- package/dist/schema-prototypes.js +1 -1
- package/dist/seed-lesson.js +4 -2
- package/dist/spaces-api.d.ts +104 -0
- package/dist/spaces-api.js +193 -0
- package/dist/spaces-cli.d.ts +90 -0
- package/dist/spaces-cli.js +245 -0
- package/dist/spaces.d.ts +187 -0
- package/dist/spaces.js +360 -0
- package/dist/storage.d.ts +47 -10
- package/dist/storage.js +74 -17
- package/dist/sweep-volatile.d.ts +2 -2
- package/dist/sweep-volatile.js +2 -2
- package/dist/telemetry-cli.d.ts +1 -1
- package/dist/telemetry-cli.js +1 -1
- package/dist/type-classify.d.ts +1 -1
- package/dist/type-classify.js +1 -1
- package/dist/type-labels.d.ts +1 -1
- package/dist/type-labels.js +1 -1
- package/dist/types.d.ts +43 -20
- package/dist/wrapper-prompt.d.ts +2 -2
- package/dist/wrapper-prompt.js +2 -2
- package/opencode-plugin/hicortex/index.ts +562 -364
- package/package.json +2 -2
- package/server.json +2 -2
package/dist/retrieval.js
CHANGED
|
@@ -75,6 +75,7 @@ exports.searchRecent = searchRecent;
|
|
|
75
75
|
const storage = __importStar(require("./storage.js"));
|
|
76
76
|
const schema_prototypes_js_1 = require("./schema-prototypes.js");
|
|
77
77
|
const type_labels_js_1 = require("./type-labels.js");
|
|
78
|
+
const spaces_js_1 = require("./spaces.js");
|
|
78
79
|
const CALIBRATION = __importStar(require("./calibration.js"));
|
|
79
80
|
/** Default decay half-life (days) at importance 0.5 — release-managed
|
|
80
81
|
* (#408): the constant lives in calibration.ts with its provenance. */
|
|
@@ -218,7 +219,7 @@ function getScoringWeights() {
|
|
|
218
219
|
// kill-switch (pure prompt). Range [0, 1].
|
|
219
220
|
//
|
|
220
221
|
// The EMA rate α is a shipped constant (SESSION_INTENT_ALPHA, 0.4), not a
|
|
221
|
-
// second knob —
|
|
222
|
+
// second knob — the 0.15.3 ruling: one knob is enough to tune/disable;
|
|
222
223
|
// exposing α was speculative generality.
|
|
223
224
|
//
|
|
224
225
|
// The centroid itself lives on SessionRecallRegistry; retrieval only needs to
|
|
@@ -798,11 +799,11 @@ async function retrieve(db, embedFn, query, options) {
|
|
|
798
799
|
const sourceAgent = options?.sourceAgent;
|
|
799
800
|
const missionDomains = options?.missionDomains;
|
|
800
801
|
const now = options?.now ?? new Date();
|
|
801
|
-
// #332: hard-omit gate —
|
|
802
|
-
// channel at candidate-build time (never post-hoc, so
|
|
803
|
-
// top-k slot).
|
|
804
|
-
|
|
805
|
-
const visible = (m) =>
|
|
802
|
+
// #332/#642: hard-omit gate — candidates outside the visible set are
|
|
803
|
+
// dropped on EVERY channel at candidate-build time (never post-hoc, so
|
|
804
|
+
// they never consume a top-k slot). isVisiblePrivacy is NULL-safe by
|
|
805
|
+
// construction (spaceOf maps NULL/''/'standard' to the shared pool).
|
|
806
|
+
const visible = (m) => (0, spaces_js_1.isVisiblePrivacy)(m.privacy, (0, spaces_js_1.resolveVisibleSet)(options?.includeConfidential, options?.visibleSpaces));
|
|
806
807
|
// #203 affinity scope — passed to computeScore for every candidate. Built
|
|
807
808
|
// once; absent fields yield no boost (zero-boost neutral).
|
|
808
809
|
const scope = project || (missionDomains && missionDomains.length > 0)
|
|
@@ -812,8 +813,8 @@ async function retrieve(db, embedFn, query, options) {
|
|
|
812
813
|
const queryEmbedding = options?.queryEmbedding ?? (await embedFn(query));
|
|
813
814
|
// 2. Dual retrieval — vector + BM25.
|
|
814
815
|
// #192: sqlite-vec can't push filters into the KNN, so filtered queries must
|
|
815
|
-
// over-fetch — the old flat limit*3 intersected a global top-15 with (for
|
|
816
|
-
//
|
|
816
|
+
// over-fetch — the old flat limit*3 intersected a global top-15 with (for a
|
|
817
|
+
// mid-sized project) a small slice of the corpus, starving every filtered query.
|
|
817
818
|
// #203: project is NO LONGER a filter (soft affinity now), so it does not
|
|
818
819
|
// trigger over-fetch; only sourceAgent still does (it remains a hard filter).
|
|
819
820
|
// #332: the classification gate filters in JS after both lists return (the
|
|
@@ -943,7 +944,7 @@ async function retrieve(db, embedFn, query, options) {
|
|
|
943
944
|
// 6. Sort and take top N — with cold-exposure slots (#192).
|
|
944
945
|
// Effective strength + the promotion stage (#448) make past winners
|
|
945
946
|
// self-reinforcing:
|
|
946
|
-
//
|
|
947
|
+
// The large majority of the production corpus had never been returned by any query. Reserve
|
|
947
948
|
// up to 2 of k for the best-scoring never-accessed candidates so the long
|
|
948
949
|
// tail gets nonzero exposure whenever it is semantically in range. Slots are
|
|
949
950
|
// only "reserved" when cold candidates exist; otherwise the top-k is the
|
|
@@ -1016,8 +1017,7 @@ function searchRecent(db, options) {
|
|
|
1016
1017
|
const limit = options?.limit ?? recallDefaults.recentLimit;
|
|
1017
1018
|
const project = options?.project;
|
|
1018
1019
|
const now = new Date();
|
|
1019
|
-
const
|
|
1020
|
-
const visible = (m) => includeConfidential || m.privacy !== "confidential";
|
|
1020
|
+
const visible = (m) => (0, spaces_js_1.isVisiblePrivacy)(m.privacy, (0, spaces_js_1.resolveVisibleSet)(options?.includeConfidential, options?.visibleSpaces));
|
|
1021
1021
|
// #192 breadth: 30 → 180-day default window (config recentWindowDays).
|
|
1022
1022
|
// "Recent" for a long-lived corpus is a season, not a month; the narrow
|
|
1023
1023
|
// window kept queryless recall re-serving the same few weeks.
|
|
@@ -119,7 +119,7 @@ export declare function computeTagWeights(db: Database.Database, memoryId: strin
|
|
|
119
119
|
* Best prototype match for one memory across ALL configured domains:
|
|
120
120
|
* argmax of cosine(memory embedding, prototype(domain)).
|
|
121
121
|
*
|
|
122
|
-
* Used by the no-fit path (nofit.ts,
|
|
122
|
+
* Used by the no-fit path (nofit.ts, spec 2026-07-07 amendment): when the LLM
|
|
123
123
|
* says no domain fits, the memory can still earn a WEAK primary from pure
|
|
124
124
|
* embedding association — provided the best cosine clears the weak-primary
|
|
125
125
|
* floor (release-managed since #408; the caller checks the floor, this
|
|
@@ -237,7 +237,7 @@ function computeTagWeights(db, memoryId, tags, prototypes) {
|
|
|
237
237
|
* Best prototype match for one memory across ALL configured domains:
|
|
238
238
|
* argmax of cosine(memory embedding, prototype(domain)).
|
|
239
239
|
*
|
|
240
|
-
* Used by the no-fit path (nofit.ts,
|
|
240
|
+
* Used by the no-fit path (nofit.ts, spec 2026-07-07 amendment): when the LLM
|
|
241
241
|
* says no domain fits, the memory can still earn a WEAK primary from pure
|
|
242
242
|
* embedding association — provided the best cosine clears the weak-primary
|
|
243
243
|
* floor (release-managed since #408; the caller checks the floor, this
|
package/dist/seed-lesson.js
CHANGED
|
@@ -55,8 +55,10 @@ Use hicortex_lessons to check for new insights. If a lesson has high confidence,
|
|
|
55
55
|
async function injectSeedLesson(database, log = console.log) {
|
|
56
56
|
try {
|
|
57
57
|
// #332: existence check over the whole lesson store (server-internal —
|
|
58
|
-
// the seed must never re-inject because the gate hid it).
|
|
59
|
-
|
|
58
|
+
// the seed must never re-inject because the gate hid it). #642: allSpaces
|
|
59
|
+
// is the explicit whole-store form (every pool, not just shared +
|
|
60
|
+
// confidential).
|
|
61
|
+
const existing = storage.getLessons(database, 365, null, { allSpaces: true });
|
|
60
62
|
const hasSeed = existing.some((l) => l.content.includes("1% Daily Self-Improvement") ||
|
|
61
63
|
l.source_agent === "hicortex/seed");
|
|
62
64
|
if (hasSeed)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure handlers for the #642 AC-10 admin API: GET /spaces (spaces, members,
|
|
3
|
+
* per-space counts, observed unassigned agents) and PUT /spaces/assign +
|
|
4
|
+
* PUT /spaces/unassign (`{agent, space}`). The marker-key-api.ts layout:
|
|
5
|
+
* validation + response shaping HERE, side-effect-free; the thin adapters in
|
|
6
|
+
* mcp-server.ts own the I/O (fresh config read, persistConfigUpdates, the
|
|
7
|
+
* LIVE re-resolution of the boot module vars, the SQL count assembly).
|
|
8
|
+
*
|
|
9
|
+
* The model rules these handlers enforce (the #642 rulings):
|
|
10
|
+
* - a space EXISTS BY CONFIG PRESENCE (the `domains` precedent — there is
|
|
11
|
+
* no create/remove API; assign to an unconfigured id is a 404 naming
|
|
12
|
+
* that law, never a silent create);
|
|
13
|
+
* - the shared pool ('standard'/NULL) is a FIXED concept, never listed and
|
|
14
|
+
* never assignable — it is not a space;
|
|
15
|
+
* - assignment is a NAME IN A LIST: the patch edits ONLY the target
|
|
16
|
+
* entry's `members` array. The raw `spaces` map returned as the patch
|
|
17
|
+
* preserves every OTHER entry by reference (persistConfigUpdates
|
|
18
|
+
* replaces just the `spaces` key, so unrelated config keys are untouched
|
|
19
|
+
* by construction);
|
|
20
|
+
* - the reserved 'confidential' space is assignable like any other — when
|
|
21
|
+
* it exists only MATERIALIZED (legacy confidentialAgents/confidentialPaths,
|
|
22
|
+
* no explicit spaces.confidential entry), the write SEEDS a raw entry
|
|
23
|
+
* from the resolved SpaceDef first: members + raw path strings ONLY, never
|
|
24
|
+
* llm/privacyLine (resolution re-derives both — llmConfidential*
|
|
25
|
+
* inheritance and the seeded confidential line must keep flowing). From
|
|
26
|
+
* that boot on the explicit entry wins entirely, which is exactly the
|
|
27
|
+
* migration path the boot conflict-warning names.
|
|
28
|
+
*
|
|
29
|
+
* Auth/scope: the adapters mount bearer-only (the standard auth middleware;
|
|
30
|
+
* page-shell exemptions do not apply to data routes) and exist only on the
|
|
31
|
+
* daemon — server mode only by construction. Writable by any token holder
|
|
32
|
+
* is inherent to the single-token model (#642 accepted risk).
|
|
33
|
+
*/
|
|
34
|
+
import { type SpaceDef } from "./spaces.js";
|
|
35
|
+
/** The counts the adapter folds out of SQL GROUP BY (spaceOf-normalized:
|
|
36
|
+
* NULL/''/'standard' count under 'standard'; the Record carries the raw
|
|
37
|
+
* space ids as its other keys). */
|
|
38
|
+
export interface SpacesGetDeps {
|
|
39
|
+
/** The boot-resolved (or live-re-resolved) space list. */
|
|
40
|
+
spaces: SpaceDef[];
|
|
41
|
+
counts: {
|
|
42
|
+
memory: Record<string, number>;
|
|
43
|
+
queued: Record<string, number>;
|
|
44
|
+
};
|
|
45
|
+
/** listObservedAgents output — ALREADY ordered last_seen DESC (newest
|
|
46
|
+
* activity first); the unassigned computation keeps that order. */
|
|
47
|
+
observed: Array<{
|
|
48
|
+
agent: string;
|
|
49
|
+
first_seen: string;
|
|
50
|
+
last_seen: string;
|
|
51
|
+
}>;
|
|
52
|
+
/** Space ids with an EFFECTIVE model (explicit per-space llm or the
|
|
53
|
+
* inherited llmConfidential* endpoint — the adapter resolves this from the
|
|
54
|
+
* boot spaceClients map / the confidential fields, so llmConfigured means
|
|
55
|
+
* "this space's work runs", not "spaces.<id>.llm* is set"). */
|
|
56
|
+
llmConfigured: ReadonlySet<string>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* GET's shaping: every configured space with its members, RAW path pattern
|
|
60
|
+
* strings, memory + queued counts, and whether its work has a model; plus
|
|
61
|
+
* the UNASSIGNED observed agents (first-prompt discovery, AC-11) — ids that
|
|
62
|
+
* appear in NO space's members, newest activity first. The shared pool is
|
|
63
|
+
* deliberately not listed (fixed concept, never a space).
|
|
64
|
+
*/
|
|
65
|
+
export declare function handleSpacesGet(deps: SpacesGetDeps): {
|
|
66
|
+
status: 200;
|
|
67
|
+
body: {
|
|
68
|
+
spaces: Array<{
|
|
69
|
+
id: string;
|
|
70
|
+
members: string[];
|
|
71
|
+
paths: string[];
|
|
72
|
+
memoryCount: number;
|
|
73
|
+
queuedCount: number;
|
|
74
|
+
llmConfigured: boolean;
|
|
75
|
+
}>;
|
|
76
|
+
unassigned: string[];
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export interface SpacesMutationResult {
|
|
80
|
+
status: number;
|
|
81
|
+
body: Record<string, unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* Present ONLY on a 200 that changed something: the NEW raw `spaces` map
|
|
84
|
+
* for persistConfigUpdates (replaces ONLY the `spaces` key). Absent on the
|
|
85
|
+
* idempotent no-ops — an already-member assign / not-a-member unassign
|
|
86
|
+
* answers `{ok:true, idempotent:true}` and writes NOTHING.
|
|
87
|
+
*/
|
|
88
|
+
patch?: {
|
|
89
|
+
spaces: Record<string, unknown>;
|
|
90
|
+
};
|
|
91
|
+
/** Present iff patch is: the applied pair, for the adapter's one log line. */
|
|
92
|
+
applied?: {
|
|
93
|
+
agent: string;
|
|
94
|
+
space: string;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** PUT /spaces/assign — see the module header for the rules. */
|
|
98
|
+
export declare function handleSpacesAssign(body: unknown, config: Record<string, unknown> | null): SpacesMutationResult;
|
|
99
|
+
/** PUT /spaces/unassign — same validation; removing the agent from the raw
|
|
100
|
+
* entry's members (every raw value that sanitizes to it — the roster
|
|
101
|
+
* namespace is sanitized by construction, this also cleans a hand-edited
|
|
102
|
+
* entry). The entry itself STAYS, possibly with an empty members list: a
|
|
103
|
+
* space exists by config presence, an empty roster is fine. */
|
|
104
|
+
export declare function handleSpacesUnassign(body: unknown, config: Record<string, unknown> | null): SpacesMutationResult;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Pure handlers for the #642 AC-10 admin API: GET /spaces (spaces, members,
|
|
4
|
+
* per-space counts, observed unassigned agents) and PUT /spaces/assign +
|
|
5
|
+
* PUT /spaces/unassign (`{agent, space}`). The marker-key-api.ts layout:
|
|
6
|
+
* validation + response shaping HERE, side-effect-free; the thin adapters in
|
|
7
|
+
* mcp-server.ts own the I/O (fresh config read, persistConfigUpdates, the
|
|
8
|
+
* LIVE re-resolution of the boot module vars, the SQL count assembly).
|
|
9
|
+
*
|
|
10
|
+
* The model rules these handlers enforce (the #642 rulings):
|
|
11
|
+
* - a space EXISTS BY CONFIG PRESENCE (the `domains` precedent — there is
|
|
12
|
+
* no create/remove API; assign to an unconfigured id is a 404 naming
|
|
13
|
+
* that law, never a silent create);
|
|
14
|
+
* - the shared pool ('standard'/NULL) is a FIXED concept, never listed and
|
|
15
|
+
* never assignable — it is not a space;
|
|
16
|
+
* - assignment is a NAME IN A LIST: the patch edits ONLY the target
|
|
17
|
+
* entry's `members` array. The raw `spaces` map returned as the patch
|
|
18
|
+
* preserves every OTHER entry by reference (persistConfigUpdates
|
|
19
|
+
* replaces just the `spaces` key, so unrelated config keys are untouched
|
|
20
|
+
* by construction);
|
|
21
|
+
* - the reserved 'confidential' space is assignable like any other — when
|
|
22
|
+
* it exists only MATERIALIZED (legacy confidentialAgents/confidentialPaths,
|
|
23
|
+
* no explicit spaces.confidential entry), the write SEEDS a raw entry
|
|
24
|
+
* from the resolved SpaceDef first: members + raw path strings ONLY, never
|
|
25
|
+
* llm/privacyLine (resolution re-derives both — llmConfidential*
|
|
26
|
+
* inheritance and the seeded confidential line must keep flowing). From
|
|
27
|
+
* that boot on the explicit entry wins entirely, which is exactly the
|
|
28
|
+
* migration path the boot conflict-warning names.
|
|
29
|
+
*
|
|
30
|
+
* Auth/scope: the adapters mount bearer-only (the standard auth middleware;
|
|
31
|
+
* page-shell exemptions do not apply to data routes) and exist only on the
|
|
32
|
+
* daemon — server mode only by construction. Writable by any token holder
|
|
33
|
+
* is inherent to the single-token model (#642 accepted risk).
|
|
34
|
+
*/
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.handleSpacesGet = handleSpacesGet;
|
|
37
|
+
exports.handleSpacesAssign = handleSpacesAssign;
|
|
38
|
+
exports.handleSpacesUnassign = handleSpacesUnassign;
|
|
39
|
+
const spaces_js_1 = require("./spaces.js");
|
|
40
|
+
const identity_store_js_1 = require("./identity-store.js");
|
|
41
|
+
/**
|
|
42
|
+
* GET's shaping: every configured space with its members, RAW path pattern
|
|
43
|
+
* strings, memory + queued counts, and whether its work has a model; plus
|
|
44
|
+
* the UNASSIGNED observed agents (first-prompt discovery, AC-11) — ids that
|
|
45
|
+
* appear in NO space's members, newest activity first. The shared pool is
|
|
46
|
+
* deliberately not listed (fixed concept, never a space).
|
|
47
|
+
*/
|
|
48
|
+
function handleSpacesGet(deps) {
|
|
49
|
+
const assignedTo = new Map();
|
|
50
|
+
const spaces = deps.spaces.map((s) => {
|
|
51
|
+
for (const m of s.members)
|
|
52
|
+
assignedTo.set(m);
|
|
53
|
+
return {
|
|
54
|
+
id: s.id,
|
|
55
|
+
members: s.members,
|
|
56
|
+
paths: s.paths.map((p) => p.raw),
|
|
57
|
+
memoryCount: deps.counts.memory[s.id] ?? 0,
|
|
58
|
+
queuedCount: deps.counts.queued[s.id] ?? 0,
|
|
59
|
+
llmConfigured: deps.llmConfigured.has(s.id),
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
// deps.observed is last_seen DESC already; filter keeps that order (the
|
|
63
|
+
// operator sees the freshest unknown workspace first).
|
|
64
|
+
const unassigned = deps.observed
|
|
65
|
+
.filter((o) => !assignedTo.has(o.agent))
|
|
66
|
+
.map((o) => o.agent);
|
|
67
|
+
return { status: 200, body: { spaces, unassigned } };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Shared body validation, in order (each failure answers before any write):
|
|
71
|
+
* 400 — not a JSON object, or `agent`/`space` not non-empty strings. The
|
|
72
|
+
* agent is then SANITIZED into the one id namespace (sanitizeAgentId — the
|
|
73
|
+
* same grammar as ?agent= and space ids); a value that sanitizes to nothing
|
|
74
|
+
* is a 400 naming why, never a silent odd id.
|
|
75
|
+
*/
|
|
76
|
+
function parseAgentSpaceBody(body) {
|
|
77
|
+
if (body === null || typeof body !== "object" || Array.isArray(body)) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
result: {
|
|
81
|
+
status: 400,
|
|
82
|
+
body: { error: 'Body must be a JSON object: {"agent": "<agent id>", "space": "<space id>"}' },
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const e = body;
|
|
87
|
+
if (typeof e.agent !== "string" || !e.agent.trim()) {
|
|
88
|
+
return { ok: false, result: { status: 400, body: { error: "'agent' must be a non-empty string" } } };
|
|
89
|
+
}
|
|
90
|
+
if (typeof e.space !== "string" || !e.space.trim()) {
|
|
91
|
+
return { ok: false, result: { status: 400, body: { error: "'space' must be a non-empty string" } } };
|
|
92
|
+
}
|
|
93
|
+
const agent = (0, identity_store_js_1.sanitizeAgentId)(e.agent);
|
|
94
|
+
if (!agent) {
|
|
95
|
+
return {
|
|
96
|
+
ok: false,
|
|
97
|
+
result: {
|
|
98
|
+
status: 400,
|
|
99
|
+
body: {
|
|
100
|
+
error: `'agent' sanitizes to an empty id — ids are lowercase alphanumerics with ` +
|
|
101
|
+
`- or _ separators (the ?agent= namespace); the value carries no id-shaped content`,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, agent, space: e.space };
|
|
107
|
+
}
|
|
108
|
+
/** The raw `spaces` map as a SHALLOW copy — every non-target entry keeps its
|
|
109
|
+
* original object reference (its values are preserved verbatim through the
|
|
110
|
+
* persist round-trip); a missing/garbage `spaces` key starts from {} (it
|
|
111
|
+
* matched no space at resolution, so nothing is lost). */
|
|
112
|
+
function rawSpacesCopy(config) {
|
|
113
|
+
const raw = config?.spaces;
|
|
114
|
+
if (raw !== null && raw !== undefined && typeof raw === "object" && !Array.isArray(raw)) {
|
|
115
|
+
return { ...raw };
|
|
116
|
+
}
|
|
117
|
+
return {};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The entry the patch edits: the existing raw entry when there is one, else a
|
|
121
|
+
* SEED from the resolved SpaceDef (members + raw path strings only — never
|
|
122
|
+
* llm/privacyLine: resolution re-derives both, and the seeded confidential
|
|
123
|
+
* line + llmConfidential* inheritance must keep flowing after the write).
|
|
124
|
+
* Reaches the materialized-legacy case (spaces.confidential absent while
|
|
125
|
+
* confidentialAgents/Paths configured) and any raw entry the resolver had to
|
|
126
|
+
* drop for shape (its drop was named at boot).
|
|
127
|
+
*/
|
|
128
|
+
function editableEntry(spaces, spaceId, def) {
|
|
129
|
+
const existing = spaces[spaceId];
|
|
130
|
+
if (existing !== null && existing !== undefined && typeof existing === "object" && !Array.isArray(existing)) {
|
|
131
|
+
return { ...existing };
|
|
132
|
+
}
|
|
133
|
+
return { members: [...def.members], paths: def.paths.map((p) => p.raw) };
|
|
134
|
+
}
|
|
135
|
+
/** PUT /spaces/assign — see the module header for the rules. */
|
|
136
|
+
function handleSpacesAssign(body, config) {
|
|
137
|
+
const parsed = parseAgentSpaceBody(body);
|
|
138
|
+
if (!parsed.ok)
|
|
139
|
+
return parsed.result;
|
|
140
|
+
const { agent, space } = parsed;
|
|
141
|
+
const resolved = (0, spaces_js_1.resolveSpaces)(config);
|
|
142
|
+
const def = resolved.byId.get(space);
|
|
143
|
+
if (!def) {
|
|
144
|
+
return {
|
|
145
|
+
status: 404,
|
|
146
|
+
body: {
|
|
147
|
+
error: `No such space '${space}' — spaces exist by config presence. Add a ` +
|
|
148
|
+
`spaces.${space} entry (members, paths, optional llm) to config.json and restart, then assign.`,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (def.members.includes(agent)) {
|
|
153
|
+
return { status: 200, body: { ok: true, idempotent: true } };
|
|
154
|
+
}
|
|
155
|
+
const spaces = rawSpacesCopy(config);
|
|
156
|
+
const entry = editableEntry(spaces, space, def);
|
|
157
|
+
const members = Array.isArray(entry.members) ? [...entry.members] : [];
|
|
158
|
+
if (!members.includes(agent))
|
|
159
|
+
members.push(agent);
|
|
160
|
+
spaces[space] = { ...entry, members };
|
|
161
|
+
return { status: 200, body: { ok: true }, patch: { spaces }, applied: { agent, space } };
|
|
162
|
+
}
|
|
163
|
+
/** PUT /spaces/unassign — same validation; removing the agent from the raw
|
|
164
|
+
* entry's members (every raw value that sanitizes to it — the roster
|
|
165
|
+
* namespace is sanitized by construction, this also cleans a hand-edited
|
|
166
|
+
* entry). The entry itself STAYS, possibly with an empty members list: a
|
|
167
|
+
* space exists by config presence, an empty roster is fine. */
|
|
168
|
+
function handleSpacesUnassign(body, config) {
|
|
169
|
+
const parsed = parseAgentSpaceBody(body);
|
|
170
|
+
if (!parsed.ok)
|
|
171
|
+
return parsed.result;
|
|
172
|
+
const { agent, space } = parsed;
|
|
173
|
+
const resolved = (0, spaces_js_1.resolveSpaces)(config);
|
|
174
|
+
const def = resolved.byId.get(space);
|
|
175
|
+
if (!def) {
|
|
176
|
+
return {
|
|
177
|
+
status: 404,
|
|
178
|
+
body: {
|
|
179
|
+
error: `No such space '${space}' — spaces exist by config presence. Add a ` +
|
|
180
|
+
`spaces.${space} entry (members, paths, optional llm) to config.json and restart, then assign.`,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (!def.members.includes(agent)) {
|
|
185
|
+
return { status: 200, body: { ok: true, idempotent: true } };
|
|
186
|
+
}
|
|
187
|
+
const spaces = rawSpacesCopy(config);
|
|
188
|
+
const entry = editableEntry(spaces, space, def);
|
|
189
|
+
const rawMembers = Array.isArray(entry.members) ? entry.members : [];
|
|
190
|
+
const members = rawMembers.filter((m) => !(typeof m === "string" && (0, identity_store_js_1.sanitizeAgentId)(m) === agent));
|
|
191
|
+
spaces[space] = { ...entry, members };
|
|
192
|
+
return { status: 200, body: { ok: true }, patch: { spaces }, applied: { agent, space } };
|
|
193
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* spaces-cli — `hicortex space …` (#642 AC-13): the CLI consumer of the AC-10
|
|
3
|
+
* admin API (GET /spaces, PUT /spaces/assign + /spaces/unassign). The
|
|
4
|
+
* dashboard's space management card is the other consumer — one API, two
|
|
5
|
+
* interfaces (the owner's ruling); this module speaks to the SAME endpoints
|
|
6
|
+
* the page does.
|
|
7
|
+
*
|
|
8
|
+
* hicortex space list every configured space (members,
|
|
9
|
+
* counts, llm posture) + the unassigned
|
|
10
|
+
* observed agents
|
|
11
|
+
* hicortex space assign <agent> <space> add an agent to a space
|
|
12
|
+
* hicortex space unassign <agent> <space> remove an agent (it drops to shared-only)
|
|
13
|
+
* hicortex space <name> the ONE-COMMAND form, run INSIDE a
|
|
14
|
+
* workspace: derives the local agent id
|
|
15
|
+
* (workspaceAgentId — the #600 sanitized
|
|
16
|
+
* basename grammar) and assigns it to <name>
|
|
17
|
+
*
|
|
18
|
+
* There is no `space create` / `space remove`: a space EXISTS BY CONFIG
|
|
19
|
+
* PRESENCE (the `domains` precedent — config-owned vocabulary, never a
|
|
20
|
+
* command). Add a spaces.<id> entry to config.json on the server, restart,
|
|
21
|
+
* then assign. Unassign never deletes the entry (an empty members list is a
|
|
22
|
+
* valid state).
|
|
23
|
+
*
|
|
24
|
+
* identity-cli.ts layout: pure URL/token resolution + formatters (unit-tested
|
|
25
|
+
* without a server), authed fetch with a 5 s timeout, SpacesCliError for every
|
|
26
|
+
* expected user-facing failure (cli.ts prints .message + exits 1), and a
|
|
27
|
+
* describeSpacesFailure that distinguishes a down server from an HTTP error —
|
|
28
|
+
* 404 names the "server predates spaces" case, 401 points at the config token
|
|
29
|
+
* (`hicortex status`), unreachable points at the daemon. The server's own
|
|
30
|
+
* 400/404 error text (e.g. the config-presence law) surfaces VERBATIM.
|
|
31
|
+
*/
|
|
32
|
+
/** Thrown for any expected, user-facing failure. cli.ts prints .message + exits 1. */
|
|
33
|
+
export declare class SpacesCliError extends Error {
|
|
34
|
+
}
|
|
35
|
+
export interface SpacesServerTarget {
|
|
36
|
+
baseUrl: string;
|
|
37
|
+
authToken?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolve the server URL + token from a parsed config object. Pure + exported
|
|
41
|
+
* so it is unit-testable without a live config. Mirrors identity-cli.ts
|
|
42
|
+
* resolveIdentityTarget (client mode → serverUrl; server mode →
|
|
43
|
+
* http://127.0.0.1:<port>; token from authToken) — the same rules every CLI
|
|
44
|
+
* consumer of the daemon follows.
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveSpacesTarget(config: Record<string, unknown>): SpacesServerTarget;
|
|
47
|
+
/** The GET /spaces response (handleSpacesGet's body, AC-10). */
|
|
48
|
+
export interface SpacesListResponse {
|
|
49
|
+
spaces: Array<{
|
|
50
|
+
id: string;
|
|
51
|
+
members: string[];
|
|
52
|
+
paths: string[];
|
|
53
|
+
memoryCount: number;
|
|
54
|
+
queuedCount: number;
|
|
55
|
+
llmConfigured: boolean;
|
|
56
|
+
}>;
|
|
57
|
+
unassigned: string[];
|
|
58
|
+
}
|
|
59
|
+
/** GET /spaces. Throws SpacesCliError with a clear message on any failure. */
|
|
60
|
+
export declare function getSpaces(target: SpacesServerTarget): Promise<SpacesListResponse>;
|
|
61
|
+
/**
|
|
62
|
+
* PUT /spaces/assign or /spaces/unassign. Returns the parsed body
|
|
63
|
+
* ({ok:true} or {ok:true, idempotent:true}); throws SpacesCliError carrying
|
|
64
|
+
* the SERVER's error text verbatim (the 404 config-presence law, the 400
|
|
65
|
+
* validation rules) so the operator reads exactly what the server rejected.
|
|
66
|
+
*/
|
|
67
|
+
export declare function putSpacesMutation(target: SpacesServerTarget, verb: "assign" | "unassign", agent: string, space: string): Promise<{
|
|
68
|
+
ok: boolean;
|
|
69
|
+
idempotent?: boolean;
|
|
70
|
+
}>;
|
|
71
|
+
/** Render GET /spaces for the terminal: one block per space + the unassigned
|
|
72
|
+
* list. Pure — the exact bytes `space list` prints. */
|
|
73
|
+
export declare function formatSpacesList(body: SpacesListResponse): string;
|
|
74
|
+
/** `hicortex space` with no subcommand: the usage block listing all four
|
|
75
|
+
* forms + the config-presence law. Pure — printed verbatim. */
|
|
76
|
+
export declare const SPACES_USAGE = "Usage: hicortex space <list | assign <agent> <space> | unassign <agent> <space> | <space>>\n\n space list every configured space (members, counts) + the unassigned agents\n space assign <agent> <space> add an agent to a space (writes the server's config, applies live)\n space unassign <agent> <space> remove an agent from a space (it drops to shared-only)\n space <name> run INSIDE a workspace: assigns THIS workspace \u2014 its folder name,\n the #600 sanitized-basename agent grammar \u2014 to space <name>\n\nThere is no space create/remove: a space EXISTS BY CONFIG PRESENCE (the domains\nprecedent) \u2014 add a spaces.<id> entry (members, paths, optional llm) to\nconfig.json on the server and restart, then assign. 'list', 'assign' and\n'unassign' are reserved words \u2014 a space named so is only addressable via\n'hicortex space assign <agent> <space>'.";
|
|
77
|
+
export interface SpacesCliOptions {
|
|
78
|
+
/** The working directory for the one-command form's agent derivation.
|
|
79
|
+
* Default process.cwd() (the test seam, mirroring buildHookRequest). */
|
|
80
|
+
cwd?: string;
|
|
81
|
+
/** Config override (tests). Defaults to loadConfig() (~/.hicortex/config.json). */
|
|
82
|
+
config?: Record<string, unknown> | null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Dispatch for `hicortex space <sub>` (cli.ts's `space` case). argv[3] ∈
|
|
86
|
+
* {list, assign, unassign} routes to those; ANY OTHER VALUE is a SPACE NAME —
|
|
87
|
+
* the one-command form (argv[3] required: bare `space` is usage help). Throws
|
|
88
|
+
* SpacesCliError on bad usage/failure; cli.ts prints .message + exits 1.
|
|
89
|
+
*/
|
|
90
|
+
export declare function runSpacesCommand(args: string[], opts?: SpacesCliOptions): Promise<void>;
|