@ours.network/fleet 2.4.0-nightly.2 → 2.4.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/dist/application/model-catalog.d.ts +1 -1
- package/dist/application/model-catalog.js +2 -2
- package/dist/application/task-room-service.d.ts +8 -0
- package/dist/application/task-room-service.js +15 -0
- package/dist/build-info.json +5 -5
- package/dist/docs.d.ts +1 -1
- package/dist/docs.js +22 -0
- package/dist/doctor.js +2 -2
- package/dist/harness/codex-runtime.d.ts +2 -0
- package/dist/harness/codex-runtime.js +7 -1
- package/dist/harness/codex.js +2 -3
- package/dist/ops.js +6 -0
- package/dist/rooms-tasks/cli.js +24 -0
- package/dist/rooms-tasks/deletion.js +4 -0
- package/dist/rooms-tasks/task-review.js +2 -0
- package/dist/rooms-tasks/task-state.d.ts +6 -1
- package/dist/rooms-tasks/task-state.js +20 -1
- package/dist/rooms-tasks/types.d.ts +17 -0
- package/dist/rooms-tasks/workspace-removal.d.ts +85 -0
- package/dist/rooms-tasks/workspace-removal.js +633 -0
- package/dist/rooms-tasks/workspace.d.ts +9 -1
- package/dist/rooms-tasks/workspace.js +13 -1
- package/dist/spawn.js +2 -0
- package/dist/web/resource-views.d.ts +1 -1
- package/dist/web/resource-views.js +1 -7
- package/dist/web/runtime.js +0 -13
- package/dist/web/server.d.ts +0 -3
- package/dist/web/server.js +10 -24
- package/docs/task-workspaces.md +51 -1
- package/package.json +7 -7
- package/presets/brain-catalog.json +5 -2
- package/dist/channels/identity-reads.d.ts +0 -69
- package/dist/channels/identity-reads.js +0 -47
- package/dist/channels/indexer.d.ts +0 -55
- package/dist/channels/indexer.js +0 -214
- package/dist/channels/runtime.d.ts +0 -30
- package/dist/channels/runtime.js +0 -125
- package/dist/channels/service.d.ts +0 -61
- package/dist/channels/service.js +0 -88
- package/dist/channels/sources.d.ts +0 -46
- package/dist/channels/sources.js +0 -68
- package/dist/channels/store.d.ts +0 -103
- package/dist/channels/store.js +0 -204
package/dist/channels/indexer.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { IdentityReadError } from './identity-reads.js';
|
|
2
|
-
import { norm } from './sources.js';
|
|
3
|
-
/**
|
|
4
|
-
* Event-driven Channels projection. One passive resource-event stream per live
|
|
5
|
-
* perspective source; idle coverage frames are ignored and a committed frame
|
|
6
|
-
* only schedules work when the source position advanced. Work is debounced,
|
|
7
|
-
* at most one observation runs per source, and each observation reads only
|
|
8
|
-
* that source's contacts and activity (one lease-free daemon read), never other sources.
|
|
9
|
-
*/
|
|
10
|
-
export class ChannelsIndexer {
|
|
11
|
-
deps;
|
|
12
|
-
watches = new Map();
|
|
13
|
-
internal = new Set();
|
|
14
|
-
syncing;
|
|
15
|
-
resync = false;
|
|
16
|
-
stopped = false;
|
|
17
|
-
debounceMs;
|
|
18
|
-
constructor(deps) {
|
|
19
|
-
this.deps = deps;
|
|
20
|
-
this.debounceMs = deps.debounceMs ?? 250;
|
|
21
|
-
}
|
|
22
|
-
/** Re-derive sources and the exclusion set; start/stop streams accordingly. */
|
|
23
|
-
sync() {
|
|
24
|
-
if (this.syncing) {
|
|
25
|
-
this.resync = true;
|
|
26
|
-
return this.syncing;
|
|
27
|
-
}
|
|
28
|
-
this.syncing = (async () => {
|
|
29
|
-
try {
|
|
30
|
-
do {
|
|
31
|
-
this.resync = false;
|
|
32
|
-
await this.syncOnce();
|
|
33
|
-
} while (this.resync && !this.stopped);
|
|
34
|
-
}
|
|
35
|
-
finally {
|
|
36
|
-
this.syncing = undefined;
|
|
37
|
-
}
|
|
38
|
-
})();
|
|
39
|
-
return this.syncing;
|
|
40
|
-
}
|
|
41
|
-
stop() {
|
|
42
|
-
this.stopped = true;
|
|
43
|
-
for (const watch of this.watches.values())
|
|
44
|
-
this.retire(watch);
|
|
45
|
-
this.watches.clear();
|
|
46
|
-
}
|
|
47
|
-
async syncOnce() {
|
|
48
|
-
if (this.stopped)
|
|
49
|
-
return;
|
|
50
|
-
const found = await this.deps.discover();
|
|
51
|
-
if (this.stopped)
|
|
52
|
-
return;
|
|
53
|
-
const internalChanged = found.internal.size !== this.internal.size || [...found.internal].some(cid => !this.internal.has(cid));
|
|
54
|
-
this.internal = found.internal;
|
|
55
|
-
this.deps.store.setSources(found.sources);
|
|
56
|
-
// Cached edges of every source, ended ones included, lose newly internal identities.
|
|
57
|
-
if (internalChanged)
|
|
58
|
-
this.deps.store.excludeContacts(found.internal);
|
|
59
|
-
const live = new Map(found.sources.filter(s => !s.ended).map(s => [s.cid, s]));
|
|
60
|
-
for (const [cid, watch] of this.watches) {
|
|
61
|
-
const next = live.get(cid);
|
|
62
|
-
if (!next || next.name !== watch.source.name) {
|
|
63
|
-
this.retire(watch);
|
|
64
|
-
this.watches.delete(cid);
|
|
65
|
-
}
|
|
66
|
-
else
|
|
67
|
-
watch.source = next;
|
|
68
|
-
}
|
|
69
|
-
for (const source of live.values()) {
|
|
70
|
-
let watch = this.watches.get(source.cid);
|
|
71
|
-
if (!watch) {
|
|
72
|
-
watch = { source, abort: new AbortController(), inflight: false, dirty: false };
|
|
73
|
-
this.watches.set(source.cid, watch);
|
|
74
|
-
void this.follow(watch);
|
|
75
|
-
this.schedule(watch); // bounded bootstrap/backfill for a new source
|
|
76
|
-
}
|
|
77
|
-
else if (internalChanged)
|
|
78
|
-
this.schedule(watch);
|
|
79
|
-
}
|
|
80
|
-
this.deps.onChange();
|
|
81
|
-
}
|
|
82
|
-
/** The daemon source of `sourceCid` changed under a caller: drop its pin and observe again. */
|
|
83
|
-
invalidate(sourceCid) {
|
|
84
|
-
const watch = this.watches.get(sourceCid.toUpperCase());
|
|
85
|
-
if (!watch || watch.abort.signal.aborted)
|
|
86
|
-
return;
|
|
87
|
-
watch.generation = undefined;
|
|
88
|
-
watch.position = undefined;
|
|
89
|
-
this.schedule(watch);
|
|
90
|
-
}
|
|
91
|
-
retire(watch) {
|
|
92
|
-
watch.abort.abort();
|
|
93
|
-
if (watch.timer)
|
|
94
|
-
clearTimeout(watch.timer);
|
|
95
|
-
watch.timer = undefined;
|
|
96
|
-
}
|
|
97
|
-
async follow(watch) {
|
|
98
|
-
let backoff = 1_000;
|
|
99
|
-
while (!watch.abort.signal.aborted) {
|
|
100
|
-
try {
|
|
101
|
-
for await (const frame of this.deps.events.resourceEvents(watch.source.name, {
|
|
102
|
-
signal: watch.abort.signal, ...(watch.generation ? { expectedGeneration: watch.generation } : {}),
|
|
103
|
-
})) {
|
|
104
|
-
backoff = 1_000;
|
|
105
|
-
if (frame.kind === 'coverage')
|
|
106
|
-
continue;
|
|
107
|
-
// ready/committed at the already-observed position change nothing; resync always reads.
|
|
108
|
-
if (frame.kind !== 'resync' && frame.producerGeneration === watch.generation && frame.position === watch.position)
|
|
109
|
-
continue;
|
|
110
|
-
if (frame.producerGeneration !== watch.generation)
|
|
111
|
-
watch.position = undefined;
|
|
112
|
-
watch.generation = frame.producerGeneration;
|
|
113
|
-
this.schedule(watch);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
if (watch.abort.signal.aborted)
|
|
118
|
-
return;
|
|
119
|
-
this.deps.log(`channels: ${watch.source.name} event stream interrupted: ${error.message}`);
|
|
120
|
-
}
|
|
121
|
-
if (this.retired(watch))
|
|
122
|
-
return;
|
|
123
|
-
// The stream ended or failed: data may be behind until it is re-established.
|
|
124
|
-
watch.generation = undefined;
|
|
125
|
-
this.deps.store.markStale(watch.source.cid);
|
|
126
|
-
this.deps.onChange();
|
|
127
|
-
await new Promise(resolve => setTimeout(resolve, backoff));
|
|
128
|
-
backoff = Math.min(backoff * 2, 30_000);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
retired(watch) {
|
|
132
|
-
return this.stopped || watch.abort.signal.aborted || this.watches.get(watch.source.cid) !== watch;
|
|
133
|
-
}
|
|
134
|
-
schedule(watch) {
|
|
135
|
-
watch.dirty = true;
|
|
136
|
-
if (watch.timer || watch.inflight || watch.abort.signal.aborted)
|
|
137
|
-
return;
|
|
138
|
-
watch.timer = setTimeout(() => { watch.timer = undefined; void this.run(watch); }, this.debounceMs);
|
|
139
|
-
}
|
|
140
|
-
async run(watch) {
|
|
141
|
-
if (watch.inflight || watch.abort.signal.aborted)
|
|
142
|
-
return;
|
|
143
|
-
watch.inflight = true;
|
|
144
|
-
watch.dirty = false;
|
|
145
|
-
try {
|
|
146
|
-
const observation = await this.observe(watch);
|
|
147
|
-
// Retired (stopped, removed or replaced) work never touches the store or a successor.
|
|
148
|
-
if (!observation || this.retired(watch))
|
|
149
|
-
return;
|
|
150
|
-
this.deps.store.replaceEdges(watch.source.cid, edgesFrom(observation, this.internal), observation.source);
|
|
151
|
-
watch.generation = observation.source.producerGeneration;
|
|
152
|
-
watch.position = observation.source.position;
|
|
153
|
-
this.deps.onChange();
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
if (this.retired(watch))
|
|
157
|
-
return;
|
|
158
|
-
this.deps.store.markStale(watch.source.cid);
|
|
159
|
-
this.deps.onChange();
|
|
160
|
-
if (error instanceof IdentityReadError && (error.code === 'identity_not_found' || error.code === 'identity_mismatch'))
|
|
161
|
-
void this.sync();
|
|
162
|
-
else
|
|
163
|
-
this.deps.log(`channels: ${watch.source.name} observation failed: ${error.message}`);
|
|
164
|
-
}
|
|
165
|
-
finally {
|
|
166
|
-
watch.inflight = false;
|
|
167
|
-
if (watch.dirty && !this.retired(watch))
|
|
168
|
-
this.schedule(watch);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
async observe(watch) {
|
|
172
|
-
for (let attempt = 0;; attempt++) {
|
|
173
|
-
try {
|
|
174
|
-
return await this.deps.reader.contactsActivity({
|
|
175
|
-
identity: watch.source.name, expectedCid: watch.source.cid,
|
|
176
|
-
...(watch.generation ? { expectedProducerGeneration: watch.generation } : {}),
|
|
177
|
-
}, watch.abort.signal);
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
if (!(error instanceof IdentityReadError) || attempt >= 2)
|
|
181
|
-
throw error;
|
|
182
|
-
// A new daemon source generation (daemon restart): drop the stale pin and read again.
|
|
183
|
-
if (error.code === 'source_generation_changed') {
|
|
184
|
-
watch.generation = undefined;
|
|
185
|
-
watch.position = undefined;
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
// The identity committed during the read: its own wake will follow; retry briefly.
|
|
189
|
-
if (error.code !== 'source_changed')
|
|
190
|
-
throw error;
|
|
191
|
-
await new Promise(resolve => setTimeout(resolve, 100 * (attempt + 1)));
|
|
192
|
-
if (watch.abort.signal.aborted)
|
|
193
|
-
return undefined;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
/** Contacts of one observation, minus internal identities, as metadata-only edges. */
|
|
199
|
-
export function edgesFrom(observation, internal) {
|
|
200
|
-
const edges = [];
|
|
201
|
-
for (const contact of observation.contacts) {
|
|
202
|
-
const cid = norm(contact.container_id);
|
|
203
|
-
if (internal.has(cid))
|
|
204
|
-
continue;
|
|
205
|
-
const activity = observation.activity[contact.container_id];
|
|
206
|
-
const root = observation.roots[contact.container_id];
|
|
207
|
-
edges.push({
|
|
208
|
-
contactCid: cid, contactName: contact.name,
|
|
209
|
-
lastActivity: activity && 'at' in activity ? activity.at : '',
|
|
210
|
-
...(root?.root_cid ? { rootCid: norm(root.root_cid), rootName: root.root_name, roleId: root.role_id } : {}),
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
return edges;
|
|
214
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { RoleRepository } from '../application/role-repository.js';
|
|
2
|
-
import { ChannelsService } from './service.js';
|
|
3
|
-
export declare const channelsPrivateDir: () => string;
|
|
4
|
-
/**
|
|
5
|
-
* Build the Fleet-side Channels service over the workspace's ordinary daemon
|
|
6
|
-
* client profile: identity discovery, passive resource events and lease-free
|
|
7
|
-
* identity reads. No identity is ever bound. Returns undefined when this host
|
|
8
|
-
* has no daemon profile.
|
|
9
|
-
*/
|
|
10
|
-
export declare function createChannels(options: {
|
|
11
|
-
configPath: string;
|
|
12
|
-
repository: RoleRepository;
|
|
13
|
-
log?: (message: string) => void;
|
|
14
|
-
}): {
|
|
15
|
-
service: ChannelsService;
|
|
16
|
-
close(): void;
|
|
17
|
-
} | undefined;
|
|
18
|
-
/**
|
|
19
|
-
* The files Channels source discovery reads: the manifest (Owner CID, roles), split role
|
|
20
|
-
* definitions, permanent and temporary role definitions, agent journals and room records.
|
|
21
|
-
* Conversation ledgers and other state under the role roots are not inputs.
|
|
22
|
-
*/
|
|
23
|
-
export declare function channelSourceInputs(configPath: string): {
|
|
24
|
-
journals: string;
|
|
25
|
-
rooms: string;
|
|
26
|
-
splitRoot: string;
|
|
27
|
-
permanentRoles: string;
|
|
28
|
-
temporaryRoles: string;
|
|
29
|
-
affects: (changed: string) => boolean;
|
|
30
|
-
};
|
package/dist/channels/runtime.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { join, relative, resolve, sep } from 'node:path';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import { attachOursClient } from '@ours.network/sdk/client';
|
|
4
|
-
import { readClientProfile } from '../client-profile.js';
|
|
5
|
-
import { loadConfig, splitRootFor } from '../config.js';
|
|
6
|
-
import { agentsRoot, stateRoot, tmpRoot } from '../paths.js';
|
|
7
|
-
import { observeFleetResources, publishFleetResourceCommit } from '../application/resource-observer.js';
|
|
8
|
-
import { listRoomRecords } from '../rooms-tasks/room-state.js';
|
|
9
|
-
import { privateRuntimeRoot } from '../agent-ours/service.js';
|
|
10
|
-
import { createIdentityReader } from './identity-reads.js';
|
|
11
|
-
import { ChannelsIndexer } from './indexer.js';
|
|
12
|
-
import { ChannelsService } from './service.js';
|
|
13
|
-
import { readAgentRecords, resolveSources } from './sources.js';
|
|
14
|
-
import { ChannelsStore } from './store.js';
|
|
15
|
-
export const channelsPrivateDir = () => join(stateRoot(), 'private-channels');
|
|
16
|
-
/**
|
|
17
|
-
* Build the Fleet-side Channels service over the workspace's ordinary daemon
|
|
18
|
-
* client profile: identity discovery, passive resource events and lease-free
|
|
19
|
-
* identity reads. No identity is ever bound. Returns undefined when this host
|
|
20
|
-
* has no daemon profile.
|
|
21
|
-
*/
|
|
22
|
-
export function createChannels(options) {
|
|
23
|
-
let profile;
|
|
24
|
-
try {
|
|
25
|
-
profile = readClientProfile();
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
const log = options.log ?? (message => console.warn(message));
|
|
31
|
-
const store = new ChannelsStore(channelsPrivateDir());
|
|
32
|
-
const reader = createIdentityReader({ endpoint: profile.endpoint, credentialPath: profile.credentialPath });
|
|
33
|
-
const attach = () => attachOursClient({
|
|
34
|
-
endpoint: profile.endpoint, expectedInstanceId: profile.expectedInstanceId, credentialPath: profile.credentialPath,
|
|
35
|
-
sessionMode: 'external', leaseToken: `ours-fleet-channels-${process.pid}-${randomUUID()}`, env: {},
|
|
36
|
-
});
|
|
37
|
-
let eventsClient;
|
|
38
|
-
const events = {
|
|
39
|
-
async *resourceEvents(identity, opts) {
|
|
40
|
-
eventsClient ??= await attach();
|
|
41
|
-
yield* eventsClient.resourceEvents(identity, opts);
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
let last;
|
|
45
|
-
const discover = async () => {
|
|
46
|
-
const ownerCid = loadConfig(options.configPath).rooms?.owner.expected_cid;
|
|
47
|
-
const roleByIdentity = new Map();
|
|
48
|
-
for (const role of await options.repository.list(true))
|
|
49
|
-
if (role.config?.identity)
|
|
50
|
-
roleByIdentity.set(role.config.identity, role.id);
|
|
51
|
-
const client = await attach();
|
|
52
|
-
let identities;
|
|
53
|
-
try {
|
|
54
|
-
identities = (await client.listIdentities());
|
|
55
|
-
}
|
|
56
|
-
finally {
|
|
57
|
-
await client.close().catch(() => { });
|
|
58
|
-
}
|
|
59
|
-
const found = resolveSources({
|
|
60
|
-
ownerCid: ownerCid ?? '',
|
|
61
|
-
identities,
|
|
62
|
-
agents: readAgentRecords(privateRuntimeRoot()),
|
|
63
|
-
roleByIdentity,
|
|
64
|
-
roomCids: listRoomRecords().map(room => room.room_identity_cid).filter((cid) => typeof cid === 'string'),
|
|
65
|
-
});
|
|
66
|
-
if (!ownerCid)
|
|
67
|
-
found.internal.delete('');
|
|
68
|
-
last = found;
|
|
69
|
-
return found;
|
|
70
|
-
};
|
|
71
|
-
const indexer = new ChannelsIndexer({ store, reader, events, discover, log, onChange: () => publishFleetResourceCommit(store.path) });
|
|
72
|
-
const sync = () => { void indexer.sync().catch(error => log(`channels: sync failed: ${error.message}`)); };
|
|
73
|
-
const service = new ChannelsService({ store, reader, indexer, ownerRoot: () => last?.rootCid, ready: () => last !== undefined });
|
|
74
|
-
sync();
|
|
75
|
-
// Re-derive sources when one of their inputs commits, so new or stopped agents change
|
|
76
|
-
// perspectives without polling. Bursts coalesce. The configuration scope also tracks the
|
|
77
|
-
// manifest file itself; every wake is filtered to the actual inputs (never this index).
|
|
78
|
-
const lifetime = new AbortController();
|
|
79
|
-
const inputs = channelSourceInputs(options.configPath);
|
|
80
|
-
let pending, relevant = false;
|
|
81
|
-
observeFleetResources({
|
|
82
|
-
configPath: options.configPath, retryWatchFailures: true,
|
|
83
|
-
paths: [inputs.journals, inputs.rooms, inputs.splitRoot, inputs.permanentRoles, inputs.temporaryRoles],
|
|
84
|
-
onInvalidated: path => { if (path === undefined || inputs.affects(path))
|
|
85
|
-
relevant = true; },
|
|
86
|
-
}, () => {
|
|
87
|
-
if (!relevant || pending || lifetime.signal.aborted)
|
|
88
|
-
return;
|
|
89
|
-
relevant = false;
|
|
90
|
-
pending = setTimeout(() => { pending = undefined; sync(); }, 1_000);
|
|
91
|
-
pending.unref?.();
|
|
92
|
-
}, lifetime.signal);
|
|
93
|
-
return {
|
|
94
|
-
service,
|
|
95
|
-
close() { lifetime.abort(); if (pending)
|
|
96
|
-
clearTimeout(pending); indexer.stop(); void eventsClient?.close().catch(() => { }); store.close(); },
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* The files Channels source discovery reads: the manifest (Owner CID, roles), split role
|
|
101
|
-
* definitions, permanent and temporary role definitions, agent journals and room records.
|
|
102
|
-
* Conversation ledgers and other state under the role roots are not inputs.
|
|
103
|
-
*/
|
|
104
|
-
export function channelSourceInputs(configPath) {
|
|
105
|
-
const config = resolve(configPath);
|
|
106
|
-
const journals = resolve(privateRuntimeRoot()), rooms = resolve(stateRoot(), 'rooms'), splitRoot = resolve(splitRootFor(config));
|
|
107
|
-
const permanentRoles = resolve(agentsRoot()), temporaryRoles = resolve(tmpRoot());
|
|
108
|
-
const under = (path, root) => { const local = relative(root, path); return local !== '..' && !local.startsWith('..' + sep) && !local.startsWith(sep) ? local : undefined; };
|
|
109
|
-
const affects = (changed) => {
|
|
110
|
-
const path = resolve(changed);
|
|
111
|
-
if (path === config || under(path, journals) !== undefined || under(path, rooms) !== undefined || under(path, splitRoot) !== undefined)
|
|
112
|
-
return true;
|
|
113
|
-
for (const root of [permanentRoles, temporaryRoles]) {
|
|
114
|
-
const local = under(path, root);
|
|
115
|
-
if (local === undefined)
|
|
116
|
-
continue;
|
|
117
|
-
const parts = local.split(sep).filter(Boolean);
|
|
118
|
-
// The roles directory, a role directory appearing/disappearing, or its definition.
|
|
119
|
-
return parts.length <= 1 || (parts.length === 2 && parts[1] === 'role.yaml');
|
|
120
|
-
}
|
|
121
|
-
// Ancestor replacement of any input root.
|
|
122
|
-
return [config, journals, rooms, splitRoot, permanentRoles, temporaryRoles].some(root => under(root, path) !== undefined);
|
|
123
|
-
};
|
|
124
|
-
return { journals, rooms, splitRoot, permanentRoles, temporaryRoles, affects };
|
|
125
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { type IdentityHistoryPage, type IdentityReader } from './identity-reads.js';
|
|
2
|
-
import type { ChannelsIndexer } from './indexer.js';
|
|
3
|
-
import type { ChannelPage, ChannelPerspective, ChannelRow, ChannelsStore } from './store.js';
|
|
4
|
-
export interface ChannelsServiceDeps {
|
|
5
|
-
store: ChannelsStore;
|
|
6
|
-
reader: IdentityReader;
|
|
7
|
-
indexer: ChannelsIndexer;
|
|
8
|
-
/** Actual root CID of this workspace's Owner, when known. */
|
|
9
|
-
ownerRoot(): string | undefined;
|
|
10
|
-
/** False until the first discovery completed. */
|
|
11
|
-
ready(): boolean;
|
|
12
|
-
}
|
|
13
|
-
export type ChannelRowView = ChannelRow & {
|
|
14
|
-
claimsOwnerRoot: boolean;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Owner-facing Channels queries over the durable projection. Served only
|
|
18
|
-
* behind the Fleet web Owner session; agents get no route to it.
|
|
19
|
-
*/
|
|
20
|
-
export declare class ChannelsService {
|
|
21
|
-
private readonly deps;
|
|
22
|
-
constructor(deps: ChannelsServiceDeps);
|
|
23
|
-
status(): {
|
|
24
|
-
ready: boolean;
|
|
25
|
-
sources: ReturnType<ChannelsStore['sourcesSummary']>;
|
|
26
|
-
};
|
|
27
|
-
private view;
|
|
28
|
-
list(options: {
|
|
29
|
-
cursor?: string;
|
|
30
|
-
limit?: number;
|
|
31
|
-
q?: string;
|
|
32
|
-
root?: string;
|
|
33
|
-
}): ChannelPage & {
|
|
34
|
-
channels: ChannelRowView[];
|
|
35
|
-
};
|
|
36
|
-
/** The SSE snapshot: the first Recent page. */
|
|
37
|
-
snapshot(): {
|
|
38
|
-
ready: boolean;
|
|
39
|
-
channels: ChannelRowView[];
|
|
40
|
-
nextCursor: string | null;
|
|
41
|
-
};
|
|
42
|
-
get(cid: string): {
|
|
43
|
-
channel: ChannelRowView;
|
|
44
|
-
perspectives: ChannelPerspective[];
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* One perspective's own history with this contact; never a merged view, never consuming.
|
|
48
|
-
* Pinned to the source's CID and last observed daemon source generation; a response is
|
|
49
|
-
* released only while that same perspective is still current.
|
|
50
|
-
*/
|
|
51
|
-
history(contactCid: string, sourceCid: string, page: {
|
|
52
|
-
before_seq?: number;
|
|
53
|
-
limit?: number;
|
|
54
|
-
}): Promise<IdentityHistoryPage & {
|
|
55
|
-
perspective: {
|
|
56
|
-
sourceCid: string;
|
|
57
|
-
kind: string;
|
|
58
|
-
roleId?: string;
|
|
59
|
-
};
|
|
60
|
-
}>;
|
|
61
|
-
}
|
package/dist/channels/service.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { FleetError } from '../application/errors.js';
|
|
2
|
-
import { IdentityReadError } from './identity-reads.js';
|
|
3
|
-
/**
|
|
4
|
-
* Owner-facing Channels queries over the durable projection. Served only
|
|
5
|
-
* behind the Fleet web Owner session; agents get no route to it.
|
|
6
|
-
*/
|
|
7
|
-
export class ChannelsService {
|
|
8
|
-
deps;
|
|
9
|
-
constructor(deps) {
|
|
10
|
-
this.deps = deps;
|
|
11
|
-
}
|
|
12
|
-
status() {
|
|
13
|
-
return { ready: this.deps.ready(), sources: this.deps.store.sourcesSummary() };
|
|
14
|
-
}
|
|
15
|
-
view(row) {
|
|
16
|
-
const root = this.deps.ownerRoot();
|
|
17
|
-
return { ...row, claimsOwnerRoot: !!root && row.claimedRoot?.rootCid === root };
|
|
18
|
-
}
|
|
19
|
-
list(options) {
|
|
20
|
-
let page;
|
|
21
|
-
try {
|
|
22
|
-
page = this.deps.store.list(options);
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
throw new FleetError('invalid_request', error.message);
|
|
26
|
-
}
|
|
27
|
-
return { ...page, channels: page.channels.map(row => this.view(row)) };
|
|
28
|
-
}
|
|
29
|
-
/** The SSE snapshot: the first Recent page. */
|
|
30
|
-
snapshot() {
|
|
31
|
-
const page = this.deps.store.list({ limit: 50 });
|
|
32
|
-
return { ready: this.deps.ready(), channels: page.channels.map(row => this.view(row)), nextCursor: page.nextCursor };
|
|
33
|
-
}
|
|
34
|
-
get(cid) {
|
|
35
|
-
const found = this.deps.store.get(cid);
|
|
36
|
-
if (!found)
|
|
37
|
-
throw new FleetError('resource_not_found', 'no such channel');
|
|
38
|
-
return { channel: this.view(found.channel), perspectives: found.perspectives };
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* One perspective's own history with this contact; never a merged view, never consuming.
|
|
42
|
-
* Pinned to the source's CID and last observed daemon source generation; a response is
|
|
43
|
-
* released only while that same perspective is still current.
|
|
44
|
-
*/
|
|
45
|
-
async history(contactCid, sourceCid, page) {
|
|
46
|
-
const contact = contactCid.toUpperCase(), source = sourceCid.toUpperCase();
|
|
47
|
-
const perspective = this.deps.store.hasPerspective(contact, source);
|
|
48
|
-
if (!perspective)
|
|
49
|
-
throw new FleetError('resource_not_found', 'that identity is not a perspective of this channel');
|
|
50
|
-
if (perspective.ended)
|
|
51
|
-
throw new FleetError('capability_unavailable', 'this perspective has ended; its history is no longer available');
|
|
52
|
-
let result;
|
|
53
|
-
try {
|
|
54
|
-
result = await this.deps.reader.history({
|
|
55
|
-
identity: perspective.name, expectedCid: source, peer_cid: contact,
|
|
56
|
-
...(perspective.generation ? { expectedProducerGeneration: perspective.generation } : {}),
|
|
57
|
-
...(page.before_seq !== undefined ? { before_seq: page.before_seq } : {}),
|
|
58
|
-
...(page.limit !== undefined ? { limit: page.limit } : {}),
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
if (error instanceof IdentityReadError) {
|
|
63
|
-
if (error.code === 'identity_not_found' || error.code === 'identity_mismatch') {
|
|
64
|
-
void this.deps.indexer.sync();
|
|
65
|
-
throw new FleetError('resource_not_found', 'this perspective is no longer available');
|
|
66
|
-
}
|
|
67
|
-
if (error.code === 'source_generation_changed') {
|
|
68
|
-
// The daemon restarted under this perspective: re-observe it before paging again.
|
|
69
|
-
this.deps.indexer.invalidate(source);
|
|
70
|
-
throw new FleetError('conflict', 'the identity source changed; reload this history', { retryable: true });
|
|
71
|
-
}
|
|
72
|
-
if (error.code === 'source_changed')
|
|
73
|
-
throw new FleetError('conflict', 'the identity changed during the read; retry', { retryable: true });
|
|
74
|
-
throw new FleetError('capability_unavailable', error.message);
|
|
75
|
-
}
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
// The perspective may have ended, left or been replaced while the read was in flight.
|
|
79
|
-
const current = this.deps.store.hasPerspective(contact, source);
|
|
80
|
-
if (!current)
|
|
81
|
-
throw new FleetError('resource_not_found', 'this perspective is no longer available');
|
|
82
|
-
if (current.ended)
|
|
83
|
-
throw new FleetError('capability_unavailable', 'this perspective has ended; its history is no longer available');
|
|
84
|
-
if (current.name !== perspective.name || current.generation !== perspective.generation)
|
|
85
|
-
throw new FleetError('conflict', 'this perspective changed during the read; retry', { retryable: true });
|
|
86
|
-
return { ...result, perspective: { sourceCid: source, kind: current.kind, ...(current.roleId ? { roleId: current.roleId } : {}) } };
|
|
87
|
-
}
|
|
88
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Who Channels reads (perspective sources) and which CIDs are never channels
|
|
3
|
-
* (the internal-exclusion set). Both come from local records only: a peer's
|
|
4
|
-
* claimed root never makes it internal, because root claims are not proven.
|
|
5
|
-
*/
|
|
6
|
-
export interface PerspectiveSource {
|
|
7
|
-
kind: 'owner' | 'agent';
|
|
8
|
-
/** Daemon identity name. */
|
|
9
|
-
name: string;
|
|
10
|
-
cid: string;
|
|
11
|
-
/** Fleet role id for agent sources (the "Open chat with agent" target). */
|
|
12
|
-
roleId?: string;
|
|
13
|
-
/** Released/retired/removed: kept as metadata only, never read again. */
|
|
14
|
-
ended: boolean;
|
|
15
|
-
}
|
|
16
|
-
export interface ChannelSources {
|
|
17
|
-
ownerCid: string;
|
|
18
|
-
rootCid?: string;
|
|
19
|
-
sources: PerspectiveSource[];
|
|
20
|
-
internal: Set<string>;
|
|
21
|
-
}
|
|
22
|
-
export interface DaemonIdentityRow {
|
|
23
|
-
name: string;
|
|
24
|
-
cid?: string;
|
|
25
|
-
kind?: string;
|
|
26
|
-
}
|
|
27
|
-
export interface AgentRecord {
|
|
28
|
-
name: string;
|
|
29
|
-
cid: string;
|
|
30
|
-
phase: string;
|
|
31
|
-
}
|
|
32
|
-
export declare const norm: (cid: string) => string;
|
|
33
|
-
/**
|
|
34
|
-
* Retained supervisor journals (`private-ours/<binder>/state.json` and archived
|
|
35
|
-
* `retired-<instance>/state.json`). They exist for stopped agents too, so a
|
|
36
|
-
* stopped agent stays a perspective source; retired generations mark ended.
|
|
37
|
-
*/
|
|
38
|
-
export declare function readAgentRecords(privateRoot: string): AgentRecord[];
|
|
39
|
-
export declare function resolveSources(input: {
|
|
40
|
-
ownerCid: string;
|
|
41
|
-
identities: DaemonIdentityRow[];
|
|
42
|
-
agents: AgentRecord[];
|
|
43
|
-
/** Fleet role id by daemon identity name, for roles this workspace defines. */
|
|
44
|
-
roleByIdentity: Map<string, string>;
|
|
45
|
-
roomCids: string[];
|
|
46
|
-
}): ChannelSources;
|
package/dist/channels/sources.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
3
|
-
const CID = /^[0-9a-f]{64}$/i;
|
|
4
|
-
const ENDED_PHASES = new Set(['TERMINAL_INTENT', 'CLEANUP_PENDING', 'RELEASED', 'FAILED']);
|
|
5
|
-
export const norm = (cid) => cid.toUpperCase();
|
|
6
|
-
/**
|
|
7
|
-
* Retained supervisor journals (`private-ours/<binder>/state.json` and archived
|
|
8
|
-
* `retired-<instance>/state.json`). They exist for stopped agents too, so a
|
|
9
|
-
* stopped agent stays a perspective source; retired generations mark ended.
|
|
10
|
-
*/
|
|
11
|
-
export function readAgentRecords(privateRoot) {
|
|
12
|
-
if (!existsSync(privateRoot))
|
|
13
|
-
return [];
|
|
14
|
-
const records = [];
|
|
15
|
-
const read = (path, retired) => {
|
|
16
|
-
try {
|
|
17
|
-
const value = JSON.parse(readFileSync(path, 'utf8'));
|
|
18
|
-
if (typeof value.name !== 'string' || typeof value.cid !== 'string' || !CID.test(value.cid))
|
|
19
|
-
return;
|
|
20
|
-
records.push({ name: value.name, cid: norm(value.cid), phase: retired ? 'RELEASED' : String(value.phase ?? '') });
|
|
21
|
-
}
|
|
22
|
-
catch { /* missing or corrupt journal: not a source */ }
|
|
23
|
-
};
|
|
24
|
-
for (const entry of readdirSync(privateRoot, { withFileTypes: true })) {
|
|
25
|
-
if (!entry.isDirectory())
|
|
26
|
-
continue;
|
|
27
|
-
const dir = join(privateRoot, entry.name);
|
|
28
|
-
read(join(dir, 'state.json'), false);
|
|
29
|
-
for (const child of readdirSync(dir, { withFileTypes: true })) {
|
|
30
|
-
if (child.isDirectory() && child.name.startsWith('retired-'))
|
|
31
|
-
read(join(dir, child.name, 'state.json'), true);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return records;
|
|
35
|
-
}
|
|
36
|
-
export function resolveSources(input) {
|
|
37
|
-
const ownerCid = norm(input.ownerCid);
|
|
38
|
-
const live = new Map(input.identities.filter(row => row.cid && CID.test(row.cid)).map(row => [norm(row.cid), row]));
|
|
39
|
-
const rootRow = input.identities.find(row => row.kind === 'root' && row.cid && CID.test(row.cid));
|
|
40
|
-
const rootCid = rootRow ? norm(rootRow.cid) : undefined;
|
|
41
|
-
const internal = new Set([ownerCid]);
|
|
42
|
-
if (rootCid)
|
|
43
|
-
internal.add(rootCid);
|
|
44
|
-
for (const cid of input.roomCids)
|
|
45
|
-
if (CID.test(cid))
|
|
46
|
-
internal.add(norm(cid));
|
|
47
|
-
const sources = [];
|
|
48
|
-
const ownerRow = live.get(ownerCid);
|
|
49
|
-
if (ownerRow)
|
|
50
|
-
sources.push({ kind: 'owner', name: ownerRow.name, cid: ownerCid, ended: false });
|
|
51
|
-
// One source per agent CID: a live journal wins over retired generations.
|
|
52
|
-
const byCid = new Map();
|
|
53
|
-
for (const record of input.agents) {
|
|
54
|
-
internal.add(record.cid);
|
|
55
|
-
const prior = byCid.get(record.cid);
|
|
56
|
-
if (!prior || (ENDED_PHASES.has(prior.phase) && !ENDED_PHASES.has(record.phase)))
|
|
57
|
-
byCid.set(record.cid, record);
|
|
58
|
-
}
|
|
59
|
-
for (const record of byCid.values()) {
|
|
60
|
-
const roleId = input.roleByIdentity.get(record.name);
|
|
61
|
-
if (roleId === undefined || record.cid === ownerCid)
|
|
62
|
-
continue; // not a workspace role: excluded, not a perspective
|
|
63
|
-
const row = live.get(record.cid);
|
|
64
|
-
sources.push({ kind: 'agent', name: record.name, cid: record.cid, roleId,
|
|
65
|
-
ended: ENDED_PHASES.has(record.phase) || !row || row.name !== record.name });
|
|
66
|
-
}
|
|
67
|
-
return { ownerCid, ...(rootCid ? { rootCid } : {}), sources, internal };
|
|
68
|
-
}
|