@otea/cli 0.0.1 → 0.1.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/LICENSE +661 -0
- package/README.es.md +151 -0
- package/README.md +251 -3
- package/THIRD_PARTY_NOTICES.md +114 -0
- package/bin/otea.mjs +28 -0
- package/dist/app/agent-detail.js +26 -0
- package/dist/app/agent-draft.js +429 -0
- package/dist/app/agent-prompts.js +52 -0
- package/dist/app/agents.js +77 -0
- package/dist/app/asking.js +43 -0
- package/dist/app/board-archive.js +243 -0
- package/dist/app/board-migration.js +272 -0
- package/dist/app/boards.js +870 -0
- package/dist/app/branch-owner.js +48 -0
- package/dist/app/brief.js +53 -0
- package/dist/app/choices.js +61 -0
- package/dist/app/collections.js +101 -0
- package/dist/app/context.js +1 -0
- package/dist/app/crop.js +56 -0
- package/dist/app/devices.js +62 -0
- package/dist/app/draft-request.js +82 -0
- package/dist/app/events.js +12 -0
- package/dist/app/evidence.js +287 -0
- package/dist/app/gates.js +610 -0
- package/dist/app/guide.js +37 -0
- package/dist/app/home.js +145 -0
- package/dist/app/init-boards.js +36 -0
- package/dist/app/intake.js +25 -0
- package/dist/app/item-search.js +141 -0
- package/dist/app/items.js +318 -0
- package/dist/app/keep-waiting.js +44 -0
- package/dist/app/land-order.js +107 -0
- package/dist/app/language.js +52 -0
- package/dist/app/launcher.js +56 -0
- package/dist/app/lease-take.js +54 -0
- package/dist/app/leases.js +86 -0
- package/dist/app/machine.js +39 -0
- package/dist/app/merged.js +89 -0
- package/dist/app/migrate.js +171 -0
- package/dist/app/next.js +1637 -0
- package/dist/app/notes.js +319 -0
- package/dist/app/notify-targets.js +113 -0
- package/dist/app/opened.js +25 -0
- package/dist/app/owner-only.js +34 -0
- package/dist/app/performance.js +28 -0
- package/dist/app/phone.js +97 -0
- package/dist/app/presence.js +596 -0
- package/dist/app/preset-views.js +25 -0
- package/dist/app/presets.js +129 -0
- package/dist/app/priority.js +156 -0
- package/dist/app/profile.js +86 -0
- package/dist/app/project.js +247 -0
- package/dist/app/proofs.js +179 -0
- package/dist/app/questions.js +215 -0
- package/dist/app/reader-filter.js +58 -0
- package/dist/app/relations.js +164 -0
- package/dist/app/relay.js +98 -0
- package/dist/app/repos.js +176 -0
- package/dist/app/restart.js +92 -0
- package/dist/app/set-aside.js +64 -0
- package/dist/app/system-locale.js +6 -0
- package/dist/app/tailscale-cli.js +77 -0
- package/dist/app/tailscale-testing.js +41 -0
- package/dist/app/tailscale.js +177 -0
- package/dist/app/tips.js +30 -0
- package/dist/app/topics-everywhere.js +53 -0
- package/dist/app/topics.js +453 -0
- package/dist/app/undo.js +52 -0
- package/dist/app/validate.js +131 -0
- package/dist/app/watch.js +48 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/commands.js +3303 -0
- package/dist/cli/exit-codes.js +70 -0
- package/dist/cli/forge.js +176 -0
- package/dist/cli/format.js +340 -0
- package/dist/cli/gh.js +17 -0
- package/dist/cli/gitrepo.js +489 -0
- package/dist/cli/help.js +101 -0
- package/dist/cli/hooks.js +232 -0
- package/dist/cli/main.js +37 -0
- package/dist/cli/merged-outside.js +58 -0
- package/dist/cli/names.js +154 -0
- package/dist/cli/opener.js +21 -0
- package/dist/cli/owner.js +154 -0
- package/dist/cli/phone.js +221 -0
- package/dist/cli/ports.js +24 -0
- package/dist/cli/probe.js +18 -0
- package/dist/cli/run.js +246 -0
- package/dist/cli/serve-file.js +21 -0
- package/dist/cli/stats.js +55 -0
- package/dist/cli/version.js +3 -0
- package/dist/core/agent-guide.js +31 -0
- package/dist/core/agent-prompts.js +139 -0
- package/dist/core/authority.js +94 -0
- package/dist/core/board/analysis.js +86 -0
- package/dist/core/board/choices.js +100 -0
- package/dist/core/board/compile.js +362 -0
- package/dist/core/board/engine.js +523 -0
- package/dist/core/board/hints.js +46 -0
- package/dist/core/board/history.js +146 -0
- package/dist/core/board/ids.js +87 -0
- package/dist/core/board/joins.js +30 -0
- package/dist/core/board/mentions.js +68 -0
- package/dist/core/board/merged.js +18 -0
- package/dist/core/board/model.js +135 -0
- package/dist/core/board/next.js +1116 -0
- package/dist/core/board/notes.js +298 -0
- package/dist/core/board/performance.js +218 -0
- package/dist/core/board/plan.js +299 -0
- package/dist/core/board/problems.js +232 -0
- package/dist/core/board/proofs.js +84 -0
- package/dist/core/board/questions.js +114 -0
- package/dist/core/board/reading.js +220 -0
- package/dist/core/board/readings.js +534 -0
- package/dist/core/board/schema.js +335 -0
- package/dist/core/board/shape-words.js +179 -0
- package/dist/core/board/template-words.js +23 -0
- package/dist/core/board/templates-catalogue.js +965 -0
- package/dist/core/board/templates-coverage.js +261 -0
- package/dist/core/board/templates-flows.js +779 -0
- package/dist/core/board/templates.js +586 -0
- package/dist/core/brief.js +93 -0
- package/dist/core/diagram.js +80 -0
- package/dist/core/errors.js +12 -0
- package/dist/core/evidence.js +33 -0
- package/dist/core/fields.js +129 -0
- package/dist/core/hierarchy.js +30 -0
- package/dist/core/icons.js +181 -0
- package/dist/core/image-crop.js +166 -0
- package/dist/core/item.js +49 -0
- package/dist/core/lease.js +31 -0
- package/dist/core/modules.js +318 -0
- package/dist/core/notify/planner.js +117 -0
- package/dist/core/notify/signals.js +66 -0
- package/dist/core/notify/text.js +60 -0
- package/dist/core/path-key.js +22 -0
- package/dist/core/placement.js +161 -0
- package/dist/core/preset-labels.js +66 -0
- package/dist/core/preset-notes.js +109 -0
- package/dist/core/preset.js +541 -0
- package/dist/core/profile.js +55 -0
- package/dist/core/project-id.js +20 -0
- package/dist/core/refusal.js +1 -0
- package/dist/core/relations.js +208 -0
- package/dist/core/repo.js +54 -0
- package/dist/core/samples.js +161 -0
- package/dist/core/search-words.js +68 -0
- package/dist/core/test-report.js +120 -0
- package/dist/core/time.js +8 -0
- package/dist/core/validation.js +60 -0
- package/dist/core/views.js +171 -0
- package/dist/core/workflow.js +51 -0
- package/dist/mcp/commands.js +101 -0
- package/dist/mcp/server.js +239 -0
- package/dist/mcp/words.js +95 -0
- package/dist/server/boards.js +232 -0
- package/dist/server/changes.js +42 -0
- package/dist/server/diff-tip.js +83 -0
- package/dist/server/drift.js +87 -0
- package/dist/server/errors.js +87 -0
- package/dist/server/evidence.js +82 -0
- package/dist/server/hosts.js +6 -0
- package/dist/server/hub.js +98 -0
- package/dist/server/lan-access.js +62 -0
- package/dist/server/lan.js +164 -0
- package/dist/server/listen.js +70 -0
- package/dist/server/notifier.js +156 -0
- package/dist/server/notify-api.js +151 -0
- package/dist/server/pairing.js +124 -0
- package/dist/server/projects.js +191 -0
- package/dist/server/push.js +199 -0
- package/dist/server/server.js +960 -0
- package/dist/server/session.js +43 -0
- package/dist/server/stream.js +36 -0
- package/dist/server/test-push.js +38 -0
- package/dist/server/token.js +37 -0
- package/dist/server/transports.js +54 -0
- package/dist/server/ui.js +44 -0
- package/dist/server/writes.js +361 -0
- package/dist/server/wrong-address.js +36 -0
- package/dist/store/boards.js +111 -0
- package/dist/store/database.js +45 -0
- package/dist/store/machine.js +77 -0
- package/dist/store/migrations.js +493 -0
- package/dist/store/queries.js +585 -0
- package/dist/store/registry.js +72 -0
- package/package.json +72 -4
- package/presets/architecture/playbook.md +69 -0
- package/presets/architecture/preset.yaml +80 -0
- package/presets/architecture/samples.yaml +23 -0
- package/presets/bug-triage/playbook.md +129 -0
- package/presets/bug-triage/preset.yaml +127 -0
- package/presets/bug-triage/samples/wall-slide-after.png +0 -0
- package/presets/bug-triage/samples/wall-slide-before.png +0 -0
- package/presets/bug-triage/samples.yaml +72 -0
- package/presets/change/playbook.md +115 -0
- package/presets/change/preset.yaml +141 -0
- package/presets/change/samples.yaml +62 -0
- package/presets/review/playbook.md +51 -0
- package/presets/review/preset.yaml +62 -0
- package/presets/review/samples.yaml +27 -0
- package/presets/tasks/playbook.md +78 -0
- package/presets/tasks/preset.yaml +61 -0
- package/presets/tasks/samples.yaml +46 -0
- package/schemas/board.schema.json +764 -0
- package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
- package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
- package/ui/dist/assets/index-C3OzJope.js +103 -0
- package/ui/dist/assets/index-COdEjckQ.css +1 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
- package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
- package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
- package/ui/dist/assets/pdf-rELvHkft.js +56 -0
- package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
- package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
- package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
- package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
- package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
- package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
- package/ui/dist/icons/apple-touch-icon.png +0 -0
- package/ui/dist/icons/otea-192.png +0 -0
- package/ui/dist/icons/otea-512.png +0 -0
- package/ui/dist/icons/otea-badge.png +0 -0
- package/ui/dist/icons/otea-maskable-512.png +0 -0
- package/ui/dist/index.html +25 -0
- package/ui/dist/manifest.webmanifest +24 -0
- package/ui/dist/otea-idle.svg +5 -0
- package/ui/dist/otea-working.svg +5 -0
- package/ui/dist/otea-yours-working.svg +5 -0
- package/ui/dist/otea-yours.svg +5 -0
- package/ui/dist/sw.js +1 -0
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import { OteaError } from '../core/errors.js';
|
|
2
|
+
import { isAgentName } from '../core/item.js';
|
|
3
|
+
import { activeLease, DEFAULT_LEASE_TTL_MS, leaseExpiry } from '../core/lease.js';
|
|
4
|
+
import { pathKey } from '../core/path-key.js';
|
|
5
|
+
import { clearListening, clearWaiter, clearWaiting, deleteProcessBeat, findAgent, findProfile, markListening, markWaiting, profileView, saveMcpLoad, saveNameOffer, saveProcessBeat, saveProcessCall, saveProcessStart, saveProfile, selectAgents, selectLastReleases, selectLatestStartBesides, selectLeases, selectListener, selectListenerProcesses, selectListening, selectMcpLoads, selectNameOffers, selectProcessBeats, selectProcessCalls, selectProfileChanges, selectWaiting, takeLease, unconfirmProfile, upsertAgent, } from '../store/queries.js';
|
|
6
|
+
import { systemClock } from './items.js';
|
|
7
|
+
export const CLI_HARNESS = { name: 'cli' };
|
|
8
|
+
// Seen again after this long away, an agent connects anew (its connectedAt starts over).
|
|
9
|
+
export const LIVE_WINDOW_MS = 5 * 60_000;
|
|
10
|
+
// An agent counts as connected while it listens, or ran an otea command within LISTEN_WINDOW_MS: a
|
|
11
|
+
// listener (an open MCP server, a running otea wait) is heard every LISTEN_BEAT_MS, and counts as
|
|
12
|
+
// listening for LISTEN_WINDOW_MS after the last beat, so a closed session drops out within it. An
|
|
13
|
+
// agent working through the command line, between its commands, counts as connected too, never as
|
|
14
|
+
// listening: having run a command lately is no listening. A lease alone keeps no agent connected
|
|
15
|
+
// (P7-R3-10): after its session ended, it would stay «trabajando» until the lease ran out.
|
|
16
|
+
export const LISTEN_BEAT_MS = 60_000;
|
|
17
|
+
export const LISTEN_WINDOW_MS = 150_000;
|
|
18
|
+
// A sighting counts for this long: an agent seen, between its commands, counts as connected, and
|
|
19
|
+
// an ended subagent drops out soon after its last call (P1-11).
|
|
20
|
+
export const ACTIVE_WINDOW_MS = 90_000;
|
|
21
|
+
// Seen again within this, an agent's last-seen time is left as it is: an agent reading item after
|
|
22
|
+
// item writes once, not on every call (each write wakes the live UI).
|
|
23
|
+
export const SEEN_PRECISION_MS = 30_000;
|
|
24
|
+
// A command-line agent has no session Otea can see: back after this long away, it counts as a new
|
|
25
|
+
// one, and its profile stands unconfirmed until it declares it again.
|
|
26
|
+
export const CLI_SESSION_GAP_MS = 60 * 60_000;
|
|
27
|
+
// The harness an agent is known by after a sighting: an MCP client names itself and replaces what
|
|
28
|
+
// was known; the command line only fills a harness nobody named, so an MCP agent that also runs
|
|
29
|
+
// `otea` keeps its client's name.
|
|
30
|
+
function harnessAfter(known, { channel, harness }) {
|
|
31
|
+
const given = harness ? { harness: harness.name, harnessVersion: harness.version ?? null } : null;
|
|
32
|
+
if (channel === 'mcp' && given)
|
|
33
|
+
return given;
|
|
34
|
+
if (known?.harness)
|
|
35
|
+
return { harness: known.harness, harnessVersion: known.harnessVersion };
|
|
36
|
+
return given ?? { harness: null, harnessVersion: null };
|
|
37
|
+
}
|
|
38
|
+
// Records that an agent was seen now. Reading first keeps the many calls of a busy agent from
|
|
39
|
+
// taking the write lock: it writes only when the harness changed or the last sighting is older
|
|
40
|
+
// than SEEN_PRECISION_MS. Returns whether it wrote.
|
|
41
|
+
export function seeAgent(db, sighting, now = systemClock) {
|
|
42
|
+
const at = now();
|
|
43
|
+
const known = findAgent(db, sighting.agent);
|
|
44
|
+
const away = known && Date.parse(at) - Date.parse(known.lastSeen) > CLI_SESSION_GAP_MS;
|
|
45
|
+
if (sighting.newSession || (sighting.channel === 'cli' && away)) {
|
|
46
|
+
unconfirmProfile(db, sighting.agent);
|
|
47
|
+
}
|
|
48
|
+
followClient(db, sighting, at);
|
|
49
|
+
const next = harnessAfter(known, sighting);
|
|
50
|
+
const fresh = known && Date.parse(at) - Date.parse(known.lastSeen) < SEEN_PRECISION_MS;
|
|
51
|
+
const same = known && known.harness === next.harness && known.harnessVersion === next.harnessVersion;
|
|
52
|
+
// It connects anew on its first sighting, in a new session, or back after it was away.
|
|
53
|
+
const back = !known?.connectedAt || Date.parse(at) - Date.parse(known.lastSeen) > LIVE_WINDOW_MS;
|
|
54
|
+
const connectedAt = sighting.newSession || back ? at : (known?.connectedAt ?? at);
|
|
55
|
+
// Heard through the MCP server: its harness loaded Otea's server.
|
|
56
|
+
const mcp = sighting.channel === 'mcp';
|
|
57
|
+
const moved = sighting.folder !== undefined && sighting.folder !== known?.folder;
|
|
58
|
+
// A command it ran is noted as an act, as precisely as a sighting.
|
|
59
|
+
const acted = sighting.call === true &&
|
|
60
|
+
(!known?.actedAt || Date.parse(at) - Date.parse(known.actedAt) >= SEEN_PRECISION_MS);
|
|
61
|
+
const unchanged = fresh && same && connectedAt === known?.connectedAt && !moved && !acted;
|
|
62
|
+
if (unchanged && (!mcp || known?.mcpAt))
|
|
63
|
+
return false;
|
|
64
|
+
upsertAgent(db, {
|
|
65
|
+
name: sighting.agent,
|
|
66
|
+
...next,
|
|
67
|
+
lastSeen: at,
|
|
68
|
+
connectedAt,
|
|
69
|
+
...(mcp ? { mcpAt: at } : {}),
|
|
70
|
+
...(sighting.folder === undefined ? {} : { folder: sighting.folder }),
|
|
71
|
+
...(sighting.call ? { actedAt: at } : {}),
|
|
72
|
+
});
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
// Hears an agent that listens now: sees it, and notes it listening. Returns when, for endListening.
|
|
76
|
+
// The listener it is heard through (its MCP server, its wait) is the process calling: it runs.
|
|
77
|
+
export function hearAgent(db, sighting, now = systemClock) {
|
|
78
|
+
const at = now();
|
|
79
|
+
seeAgent(db, sighting, () => at);
|
|
80
|
+
markListening(db, sighting.agent, at, sighting.session, sighting.launcher, sighting.waiter);
|
|
81
|
+
for (const pid of [sighting.session, sighting.waiter])
|
|
82
|
+
if (pid !== undefined)
|
|
83
|
+
beatProcess(db, pid, () => at);
|
|
84
|
+
return at;
|
|
85
|
+
}
|
|
86
|
+
// A listener that ends (an MCP server whose client closed it, a wait whose session ended) stops
|
|
87
|
+
// counting its agent as listening at once, not a listening window later: unless another one heard
|
|
88
|
+
// the agent after its last beat (`heardAt`, what hearAgent returned). An MCP server that ends
|
|
89
|
+
// gives its `session`: the calls it served heard the agent after its beat, through it.
|
|
90
|
+
export function endListening(db, agent, heardAt, session) {
|
|
91
|
+
clearListening(db, agent, heardAt, session);
|
|
92
|
+
}
|
|
93
|
+
// Whether a process still runs on this computer, where every MCP server of the project runs, as
|
|
94
|
+
// the system tells it by its id alone.
|
|
95
|
+
export function processAlive(pid) {
|
|
96
|
+
try {
|
|
97
|
+
process.kill(pid, 0);
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
return error.code === 'EPERM';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Long past any listening window: the rows of processes killed before they could say they ended.
|
|
105
|
+
const BEATS_KEPT_MS = 24 * 60 * 60_000;
|
|
106
|
+
// A process id alone is no proof an Otea process (an MCP server, a command-line wait) still runs:
|
|
107
|
+
// once it ends, the system may give its id to another process, as Windows does within seconds, and
|
|
108
|
+
// its session would stay open on the desk (P3C-R3-12). So each one says it still runs (beatProcess)
|
|
109
|
+
// as it starts and on every LISTEN_BEAT_MS, and one that ends on its own says so (endProcess).
|
|
110
|
+
export function beatProcess(db, pid, now = systemClock) {
|
|
111
|
+
const at = now();
|
|
112
|
+
saveProcessBeat(db, pid, at, new Date(Date.parse(at) - BEATS_KEPT_MS).toISOString());
|
|
113
|
+
}
|
|
114
|
+
// An MCP server says it starts: it served no call yet. The start is what tells a server its harness
|
|
115
|
+
// stopped talking to from one still in use (claimName).
|
|
116
|
+
export function startProcess(db, pid, now = systemClock) {
|
|
117
|
+
const at = now();
|
|
118
|
+
saveProcessStart(db, pid, at, new Date(Date.parse(at) - BEATS_KEPT_MS).toISOString());
|
|
119
|
+
}
|
|
120
|
+
// An MCP server serves a tool call now; `open` 1 as a long call (a wait) begins, -1 as it ends.
|
|
121
|
+
// A short call is written only when another server started since this one's last noted call, the
|
|
122
|
+
// one thing a later start is compared with, so a busy agent's calls rarely write.
|
|
123
|
+
export function noteCall(db, pid, now = systemClock, open = 0) {
|
|
124
|
+
const at = now();
|
|
125
|
+
if (open === 0) {
|
|
126
|
+
const noted = selectProcessCalls(db, pid);
|
|
127
|
+
if (!noted)
|
|
128
|
+
return;
|
|
129
|
+
const latest = selectLatestStartBesides(db, pid);
|
|
130
|
+
if (noted.calledAt !== null && (latest === null || latest <= noted.calledAt))
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
saveProcessCall(db, pid, at, open);
|
|
134
|
+
}
|
|
135
|
+
// Whether the MCP server `session` started after the server `older`, which has served no call
|
|
136
|
+
// since then and holds none open: its harness talks to the new one only (it reloaded its config
|
|
137
|
+
// and left the old one running, P8-R3-1). Unknown for a server that never noted its start.
|
|
138
|
+
function quietSince(db, older, session) {
|
|
139
|
+
const mine = selectProcessCalls(db, session);
|
|
140
|
+
const theirs = selectProcessCalls(db, older);
|
|
141
|
+
if (!mine?.startedAt || !theirs?.startedAt || theirs.calling > 0)
|
|
142
|
+
return false;
|
|
143
|
+
if (Date.parse(theirs.startedAt) >= Date.parse(mine.startedAt))
|
|
144
|
+
return false;
|
|
145
|
+
return theirs.calledAt === null || Date.parse(theirs.calledAt) < Date.parse(mine.startedAt);
|
|
146
|
+
}
|
|
147
|
+
export function endProcess(db, pid) {
|
|
148
|
+
deleteProcessBeat(db, pid);
|
|
149
|
+
}
|
|
150
|
+
// Which process ids name an Otea process that runs now: one the system says runs (`alive`) that
|
|
151
|
+
// beat within LISTEN_WINDOW_MS. One killed with its session counts as running until then, even
|
|
152
|
+
// once its id is another process's; one that ended on its own, no longer. Read once per look.
|
|
153
|
+
export function runningProcesses(db, now = systemClock, alive = processAlive) {
|
|
154
|
+
const at = Date.parse(now());
|
|
155
|
+
const beats = selectProcessBeats(db);
|
|
156
|
+
return (pid) => {
|
|
157
|
+
const beat = beats.get(pid);
|
|
158
|
+
return beat !== undefined && at - Date.parse(beat) <= LISTEN_WINDOW_MS && alive(pid);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
// An MCP session (`session`, its server's process id) takes an agent name only while no other open
|
|
162
|
+
// session works under it: two sessions under one name would take one item twice and hand off each
|
|
163
|
+
// other's work. A session that closed or went quiet leaves the name free; a command-line wait
|
|
164
|
+
// shares it with its own agent. A new server (`fresh`: its first call under the name) takes the name
|
|
165
|
+
// from the older one, which is refused from then on, when its harness no longer talks to that one:
|
|
166
|
+
// the same harness process (`harness`, its parent's id) started both, or, for a harness that starts
|
|
167
|
+
// each server through a shell of its own, the agent's harness (`client`, the name its MCP client
|
|
168
|
+
// gives) started the new one in this project and the old one served no call since then and holds
|
|
169
|
+
// none open (P8-R3-1). A real second session under the name, at work or waiting, is still refused.
|
|
170
|
+
export function claimName(db, agent, session, alive = processAlive, now = systemClock, harness, fresh = false, client) {
|
|
171
|
+
const heard = selectListener(db, agent);
|
|
172
|
+
if (!heard?.session || heard.session === session || !heard.at)
|
|
173
|
+
return;
|
|
174
|
+
if (Date.parse(now()) - Date.parse(heard.at) >= LISTEN_WINDOW_MS)
|
|
175
|
+
return;
|
|
176
|
+
const running = runningProcesses(db, now, alive);
|
|
177
|
+
if (!running(heard.session))
|
|
178
|
+
return;
|
|
179
|
+
if (fresh && harness !== undefined && heard.harness === harness)
|
|
180
|
+
return;
|
|
181
|
+
const sameClient = client !== undefined && findAgent(db, agent)?.harness === client;
|
|
182
|
+
if (fresh && sameClient && quietSince(db, heard.session, session))
|
|
183
|
+
return;
|
|
184
|
+
// The name it is told is handed to it, so a third session refused the same name is told another:
|
|
185
|
+
// the refused session is the process calling, which runs.
|
|
186
|
+
const at = now();
|
|
187
|
+
beatProcess(db, session, () => at);
|
|
188
|
+
const offers = handedOut(db, session, at, running);
|
|
189
|
+
const free = freeName(db, agent, offers);
|
|
190
|
+
if (free !== null)
|
|
191
|
+
saveNameOffer(db, { name: free, at, session });
|
|
192
|
+
const take = free === null
|
|
193
|
+
? 'passing as another name of your own on every call'
|
|
194
|
+
: `passing as "${free}" on every call (no other agent or session has that name)`;
|
|
195
|
+
throw new OteaError('name_in_use', `Another session works as ${agent} right now: give this one its own name, ${take}, and declare its profile under it`);
|
|
196
|
+
}
|
|
197
|
+
// A name handed out (otea whoami, a refused session) stays that session's for this long: another
|
|
198
|
+
// session asking meanwhile is handed another one, so two sessions started together never share one.
|
|
199
|
+
export const OFFER_WINDOW_MS = LIVE_WINDOW_MS;
|
|
200
|
+
// The names handed lately to a session other than `session` (an MCP server's process id; a
|
|
201
|
+
// command-line session has none Otea can tell apart, so every earlier one is another). A name
|
|
202
|
+
// handed to a session that ended is free again (P4-R3-2): the MCP server it was handed to is gone,
|
|
203
|
+
// or, handed on the command line, the name was heard since through listeners that all ended.
|
|
204
|
+
// `running`: runningProcesses.
|
|
205
|
+
function handedOut(db, session, at, running) {
|
|
206
|
+
const heard = selectListening(db);
|
|
207
|
+
const listeners = selectListenerProcesses(db);
|
|
208
|
+
const other = ({ session: to }) => to === null || to !== session;
|
|
209
|
+
const lately = ({ at: when }) => Date.parse(at) - Date.parse(when) < OFFER_WINDOW_MS;
|
|
210
|
+
const ended = ({ name, at: when, session: to }) => {
|
|
211
|
+
if (to !== null)
|
|
212
|
+
return !running(to);
|
|
213
|
+
const last = heard.get(name);
|
|
214
|
+
return last !== undefined && last >= when && listenersEnded(listeners.get(name), running);
|
|
215
|
+
};
|
|
216
|
+
return new Set(selectNameOffers(db)
|
|
217
|
+
.filter((offer) => lately(offer) && other(offer) && !ended(offer))
|
|
218
|
+
.map(({ name }) => name));
|
|
219
|
+
}
|
|
220
|
+
// Whether the agent's session ended: every process it was heard listening through (its MCP server,
|
|
221
|
+
// its wait) has ended, the test whoami hands its name back on (P4-R3-2). Never for an agent heard
|
|
222
|
+
// through none, such as one at the command line between its commands. An answer is not claimed
|
|
223
|
+
// for it, and another agent's claim takes over its lease (P3C-R3-11).
|
|
224
|
+
export function sessionEnded(db, agent, now = systemClock, alive = processAlive) {
|
|
225
|
+
return listenersEnded(selectListenerProcesses(db).get(agent), runningProcesses(db, now, alive));
|
|
226
|
+
}
|
|
227
|
+
// Whether the agent's session runs now: some process it was heard listening through (its MCP
|
|
228
|
+
// server, its wait) still runs. Never for an agent heard through none, such as one at the command
|
|
229
|
+
// line between its commands: nothing tells. A lease of it that ran out is still its own then
|
|
230
|
+
// (P1-R3-6): it may be at work on a long step with no otea call.
|
|
231
|
+
export function sessionRuns(db, agent, now = systemClock, alive = processAlive) {
|
|
232
|
+
const processes = selectListenerProcesses(db).get(agent);
|
|
233
|
+
if (!processes)
|
|
234
|
+
return false;
|
|
235
|
+
return !listenersEnded(processes, runningProcesses(db, now, alive));
|
|
236
|
+
}
|
|
237
|
+
// Whether the agent's session is alive now (P7-R3-11): a process it listens through (its MCP
|
|
238
|
+
// server, its wait) still runs and beats, or it made an otea call within LISTEN_WINDOW_MS (a
|
|
239
|
+
// command-line agent between its commands). An agent heard through nothing that runs, quiet past
|
|
240
|
+
// that, has no session Otea can see: an answer is not claimed for it, and whoami hands its name
|
|
241
|
+
// back with what it holds, though its lease runs on and still keeps the item from other agents.
|
|
242
|
+
export function sessionAlive(db, agent, now = systemClock, alive = processAlive) {
|
|
243
|
+
if (sessionRuns(db, agent, now, alive))
|
|
244
|
+
return true;
|
|
245
|
+
return calledLately(findAgent(db, agent)?.actedAt, now());
|
|
246
|
+
}
|
|
247
|
+
// Whether an agent that ran an otea command at `actedAt` (a listener's beat is none) still counts
|
|
248
|
+
// as at work at `at`: within LISTEN_WINDOW_MS (P7-R2-9, P7-R3-11).
|
|
249
|
+
function calledLately(actedAt, at) {
|
|
250
|
+
return !!actedAt && Date.parse(at) - Date.parse(actedAt) <= LISTEN_WINDOW_MS;
|
|
251
|
+
}
|
|
252
|
+
// Any otea call of an agent keeps what it holds (P1-R3-6): each live lease of it with less than half
|
|
253
|
+
// of a default lease left is extended to a default lease from now, never shortened, so a busy agent
|
|
254
|
+
// writes it rarely. A lease that already ran out stays as it is: the agent claims it again.
|
|
255
|
+
export function renewLeases(db, agent, now = systemClock) {
|
|
256
|
+
const at = now();
|
|
257
|
+
const half = leaseExpiry(at, DEFAULT_LEASE_TTL_MS / 2);
|
|
258
|
+
const until = leaseExpiry(at, DEFAULT_LEASE_TTL_MS);
|
|
259
|
+
for (const { id, lease } of selectLeases(db)) {
|
|
260
|
+
const live = activeLease(lease, at);
|
|
261
|
+
if (live?.holder !== agent || live.expiresAt >= half)
|
|
262
|
+
continue;
|
|
263
|
+
takeLease(db, id, { holder: agent, expiresAt: until }, at);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// Whether every process an agent was heard listening through (its MCP server, its wait) has ended;
|
|
267
|
+
// never for one heard through none.
|
|
268
|
+
function listenersEnded(processes, alive) {
|
|
269
|
+
const { session = null, waiter = null } = processes ?? {};
|
|
270
|
+
if (session === null && waiter === null)
|
|
271
|
+
return false;
|
|
272
|
+
return !(session !== null && alive(session)) && !(waiter !== null && alive(waiter));
|
|
273
|
+
}
|
|
274
|
+
// A name of the same stem as `base` that no agent ever used and no other session was handed:
|
|
275
|
+
// claude, claude-2 and claude-3 taken give claude-4. Null when none fits an agent name.
|
|
276
|
+
function freeName(db, base, handed) {
|
|
277
|
+
const stem = base.replace(/-\d+$/, '');
|
|
278
|
+
for (let n = 2; n < 100; n += 1) {
|
|
279
|
+
const name = `${stem}-${n}`;
|
|
280
|
+
if (!isAgentName(name))
|
|
281
|
+
return null;
|
|
282
|
+
if (!handed.has(name) && !findAgent(db, name))
|
|
283
|
+
return name;
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
// Whether the process that started a wait is gone (`start`, the parent it had then; `current`, the
|
|
288
|
+
// parent it has now; `chain`, the shells above it up to its harness): one of them ended, or the
|
|
289
|
+
// wait was handed to another parent. A wait left running after its agent's session ended would
|
|
290
|
+
// keep the agent shown as listening until it timed out.
|
|
291
|
+
export function ownerGone(start, current, alive = processAlive, chain = [start]) {
|
|
292
|
+
if (start <= 1)
|
|
293
|
+
return false;
|
|
294
|
+
return current !== start || !chain.every(alive);
|
|
295
|
+
}
|
|
296
|
+
// The beat of a wait (watch's `beat`): hears the agent again, never as a new session, and notes it
|
|
297
|
+
// waiting. Far inside CLI_SESSION_GAP_MS, so a wait of hours is no time away and its profile stays
|
|
298
|
+
// confirmed.
|
|
299
|
+
export function waitBeat(db, { newSession: _, ...sighting }, now = systemClock) {
|
|
300
|
+
const run = () => {
|
|
301
|
+
const at = hearAgent(db, sighting, now);
|
|
302
|
+
markWaiting(db, sighting.agent, at);
|
|
303
|
+
keepLeases(db, sighting.agent, at);
|
|
304
|
+
return at;
|
|
305
|
+
};
|
|
306
|
+
return { everyMs: LISTEN_BEAT_MS, run };
|
|
307
|
+
}
|
|
308
|
+
// A waiting agent is alive: a parent waiting on its children, or on the person's answer, keeps
|
|
309
|
+
// what it holds, each live lease extended to a default lease from now, never shortened. A lease
|
|
310
|
+
// that already ran out stays as it is (Atasco): the agent claims it again.
|
|
311
|
+
function keepLeases(db, agent, at) {
|
|
312
|
+
const until = leaseExpiry(at, DEFAULT_LEASE_TTL_MS);
|
|
313
|
+
for (const { id, lease } of selectLeases(db)) {
|
|
314
|
+
const live = activeLease(lease, at);
|
|
315
|
+
if (live?.holder !== agent || live.expiresAt >= until)
|
|
316
|
+
continue;
|
|
317
|
+
takeLease(db, id, { holder: agent, expiresAt: until }, at);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
// A wait that ends (it found what it waited for, or its session ended) stops its agent waiting at
|
|
321
|
+
// once, unless another wait heard it after `heardAt`, its last beat. A wait that timed out leaves
|
|
322
|
+
// it: the agent calls it again at once, and the desk would blink to nobody waiting between them.
|
|
323
|
+
export function endWaiting(db, agent, heardAt) {
|
|
324
|
+
clearWaiting(db, agent, heardAt);
|
|
325
|
+
}
|
|
326
|
+
// A wait that ends by itself, however it ends: its process is no longer one to watch, so its
|
|
327
|
+
// agent is not counted as gone when it exits. Only a wait whose session ended stays noted: one
|
|
328
|
+
// killed with it, or one that saw its session gone and stopped.
|
|
329
|
+
export function endWaiter(db, agent, waiter) {
|
|
330
|
+
clearWaiter(db, agent, waiter);
|
|
331
|
+
}
|
|
332
|
+
// The agents an MCP server should stop hearing on its beats (`calls`: when each last called through
|
|
333
|
+
// it, in ms): a subagent that has not called within the listening window has ended, though the
|
|
334
|
+
// session's own agent (`own`), idle in its chat with the server open, still listens.
|
|
335
|
+
export function quietVoices(calls, own, now) {
|
|
336
|
+
return [...calls]
|
|
337
|
+
.filter(([name, at]) => name !== own && now - at > LISTEN_WINDOW_MS)
|
|
338
|
+
.map(([name]) => name);
|
|
339
|
+
}
|
|
340
|
+
// A harness loaded Otea's MCP server (its client's initialize): every agent of that harness can
|
|
341
|
+
// reach it, whatever name it took, and otea connect --check says the server works. `session`: the
|
|
342
|
+
// server's process, so the desk says MCP is at hand only while it runs (P4-R3-7).
|
|
343
|
+
export function mcpLoaded(db, harness, now = systemClock, session) {
|
|
344
|
+
const key = harnessKey(harness.name);
|
|
345
|
+
if (key === '')
|
|
346
|
+
return;
|
|
347
|
+
const at = now();
|
|
348
|
+
saveMcpLoad(db, { harness: key, name: harness.name, at, session: session ?? null });
|
|
349
|
+
if (session !== undefined)
|
|
350
|
+
beatProcess(db, session, () => at);
|
|
351
|
+
}
|
|
352
|
+
// A profile's harness is the MCP client's: when the client names another one, the profile follows.
|
|
353
|
+
function followClient(db, { agent, channel, harness }, at) {
|
|
354
|
+
if (channel !== 'mcp' || !harness)
|
|
355
|
+
return;
|
|
356
|
+
const declared = findProfile(db, agent);
|
|
357
|
+
if (!declared || declared.profile.harness === harness.name)
|
|
358
|
+
return;
|
|
359
|
+
const profile = { ...declared.profile, harness: harness.name };
|
|
360
|
+
saveProfile(db, agent, profile, { at, channel, confirmed: declared.confirmed });
|
|
361
|
+
}
|
|
362
|
+
// The agents seen in the project, never the human: live ones first, then the most recently seen.
|
|
363
|
+
// `processes` says whether a process id runs on this computer, and a listener (its MCP server, its
|
|
364
|
+
// wait) runs while it also beats (runningProcesses): an agent heard through a listener that is gone
|
|
365
|
+
// no longer listens, and if it did nothing since, its session ended (killed, or the computer
|
|
366
|
+
// restarted): it is gone, though a lease of its runs on.
|
|
367
|
+
export function listAgents(db, now = systemClock, processes = processAlive) {
|
|
368
|
+
const at = now();
|
|
369
|
+
const alive = runningProcesses(db, () => at, processes);
|
|
370
|
+
const holding = new Map();
|
|
371
|
+
for (const { id, lease } of selectLeases(db)) {
|
|
372
|
+
const live = activeLease(lease, at);
|
|
373
|
+
if (live)
|
|
374
|
+
holding.set(live.holder, [...(holding.get(live.holder) ?? []), id]);
|
|
375
|
+
}
|
|
376
|
+
const heard = selectListening(db);
|
|
377
|
+
const waits = selectWaiting(db);
|
|
378
|
+
const listeners = selectListenerProcesses(db);
|
|
379
|
+
const since = (last, window) => last !== undefined && Date.parse(at) - Date.parse(last) <= window;
|
|
380
|
+
const within = (last) => since(last, LISTEN_WINDOW_MS);
|
|
381
|
+
// Every listener process it was heard through has ended.
|
|
382
|
+
const silenced = (name) => listenersEnded(listeners.get(name), alive);
|
|
383
|
+
const records = selectAgents(db);
|
|
384
|
+
const released = selectLastReleases(db);
|
|
385
|
+
const names = records.map(({ name }) => name);
|
|
386
|
+
// A subagent that let go of what it held and ran nothing since, for a short while, has finished,
|
|
387
|
+
// whatever wait of it still beats in its parent's shells (P1-R3-8).
|
|
388
|
+
const finished = (record, held) => {
|
|
389
|
+
const release = released.get(record.name);
|
|
390
|
+
if (held.length > 0 || release === undefined || !isSubagent(record.name, names))
|
|
391
|
+
return false;
|
|
392
|
+
const quiet = Date.parse(record.actedAt ?? record.lastSeen) <= Date.parse(release);
|
|
393
|
+
return quiet && Date.parse(at) - Date.parse(release) > ACTIVE_WINDOW_MS;
|
|
394
|
+
};
|
|
395
|
+
const declaredOf = new Map(records.map(({ name }) => [name, findProfile(db, name)]));
|
|
396
|
+
// The harness an agent works through, as one key: its MCP client's, else the one it declared.
|
|
397
|
+
const keyOf = ({ name, harness }) => {
|
|
398
|
+
const named = harness && harness.toLowerCase() !== 'cli' ? harness : declaredOf.get(name)?.profile.harness;
|
|
399
|
+
return named ? harnessKey(named) : null;
|
|
400
|
+
};
|
|
401
|
+
// Per harness: the latest ask for MCP.
|
|
402
|
+
const asks = new Map();
|
|
403
|
+
for (const record of records) {
|
|
404
|
+
const key = keyOf(record);
|
|
405
|
+
const value = record.mcpAskedAt;
|
|
406
|
+
if (!key || key === 'cli' || !value)
|
|
407
|
+
continue;
|
|
408
|
+
const known = asks.get(key);
|
|
409
|
+
if (known === undefined || known < value)
|
|
410
|
+
asks.set(key, value);
|
|
411
|
+
}
|
|
412
|
+
// The harnesses with an MCP server running now: the last one each loaded, or one an agent of it
|
|
413
|
+
// listens through. A load an ended session left is no server at hand (P4-R3-7).
|
|
414
|
+
const serving = new Set();
|
|
415
|
+
for (const load of selectMcpLoads(db))
|
|
416
|
+
if (load.session !== null && alive(load.session))
|
|
417
|
+
serving.add(load.harness);
|
|
418
|
+
for (const record of records) {
|
|
419
|
+
const key = keyOf(record);
|
|
420
|
+
const session = listeners.get(record.name)?.session ?? null;
|
|
421
|
+
if (key && session !== null && alive(session))
|
|
422
|
+
serving.add(key);
|
|
423
|
+
}
|
|
424
|
+
const mcpOf = (record) => {
|
|
425
|
+
if (record.mcpAt)
|
|
426
|
+
return null;
|
|
427
|
+
const key = keyOf(record);
|
|
428
|
+
const asked = [record.mcpAskedAt, key ? asks.get(key) : undefined]
|
|
429
|
+
.filter((each) => Boolean(each))
|
|
430
|
+
.sort()
|
|
431
|
+
.at(-1);
|
|
432
|
+
if (asked === undefined)
|
|
433
|
+
return null;
|
|
434
|
+
return key && serving.has(key) ? 'unused' : 'missing';
|
|
435
|
+
};
|
|
436
|
+
const agents = records.map((record) => {
|
|
437
|
+
const { name, harness, harnessVersion, connectedAt, lastSeen, mcpAt, folder } = record;
|
|
438
|
+
const held = holding.get(name) ?? [];
|
|
439
|
+
const heardAt = heard.get(name);
|
|
440
|
+
const done = finished(record, held);
|
|
441
|
+
const gone = done || (heardAt !== undefined && silenced(name));
|
|
442
|
+
const listening = within(heardAt) && !gone;
|
|
443
|
+
// Its session ended: it was last heard through a listener now gone, and did nothing since; or
|
|
444
|
+
// it is a subagent that finished.
|
|
445
|
+
const ended = done || (gone && heardAt !== undefined && Date.parse(lastSeen) <= Date.parse(heardAt));
|
|
446
|
+
// It was seen lately (a command-line agent between its commands); a profile given for it by
|
|
447
|
+
// the person, with no sighting of its own, is no act.
|
|
448
|
+
const active = connectedAt !== null && since(lastSeen, ACTIVE_WINDOW_MS);
|
|
449
|
+
// It ran an otea command within the listening window: still at work, between two items too
|
|
450
|
+
// (P7-R2-9). What it holds counts only while it is heard so (P7-R3-10).
|
|
451
|
+
const called = calledLately(record.actedAt, at);
|
|
452
|
+
const declared = declaredOf.get(name);
|
|
453
|
+
const mcp = mcpOf(record);
|
|
454
|
+
return {
|
|
455
|
+
name,
|
|
456
|
+
harness,
|
|
457
|
+
harnessVersion,
|
|
458
|
+
connectedAt,
|
|
459
|
+
holding: held,
|
|
460
|
+
lastSeen,
|
|
461
|
+
actedAt: record.actedAt ?? null,
|
|
462
|
+
live: !ended && (listening || active || called),
|
|
463
|
+
sessionEnded: silenced(name),
|
|
464
|
+
listening,
|
|
465
|
+
waiting: within(waits.get(name)) && !gone,
|
|
466
|
+
profile: declared ? profileView(declared.profile) : null,
|
|
467
|
+
profileConfirmed: declared?.confirmed ?? false,
|
|
468
|
+
profileChangedAt: profileChangedAt(db, name),
|
|
469
|
+
mcpAt: mcpAt ?? null,
|
|
470
|
+
mcpMissing: mcp === 'missing',
|
|
471
|
+
mcp,
|
|
472
|
+
folder: folder ?? null,
|
|
473
|
+
};
|
|
474
|
+
});
|
|
475
|
+
return agents.sort((a, b) => Number(b.live) - Number(a.live) ||
|
|
476
|
+
Date.parse(b.lastSeen) - Date.parse(a.lastSeen) ||
|
|
477
|
+
a.name.localeCompare(b.name));
|
|
478
|
+
}
|
|
479
|
+
// Whether `name` is a subagent's, as the guide has them named: another agent's name, then a role
|
|
480
|
+
// (claude-haiku-hacer, claude-haiku-integrar-2). A number alone after it is another session of the
|
|
481
|
+
// same agent (claude-2, as whoami hands it out), never a subagent.
|
|
482
|
+
export function isSubagent(name, names) {
|
|
483
|
+
return names.some((parent) => {
|
|
484
|
+
if (parent === name || !name.startsWith(`${parent}-`))
|
|
485
|
+
return false;
|
|
486
|
+
return !/^\d+$/.test(name.slice(parent.length + 1));
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
// What changes when an agent's presence does, leaving out `live`, which only time changes: the
|
|
490
|
+
// stream tells the UI to read the agents again when it differs.
|
|
491
|
+
export function presenceSignature(db, now = systemClock) {
|
|
492
|
+
const agents = listAgents(db, now).sort((a, b) => a.name.localeCompare(b.name));
|
|
493
|
+
return JSON.stringify(agents.map(({ name, harness, holding, lastSeen, profile, profileConfirmed, waiting, mcp }) => [
|
|
494
|
+
name,
|
|
495
|
+
harness,
|
|
496
|
+
holding,
|
|
497
|
+
lastSeen,
|
|
498
|
+
profile,
|
|
499
|
+
profileConfirmed,
|
|
500
|
+
waiting,
|
|
501
|
+
mcp,
|
|
502
|
+
]));
|
|
503
|
+
}
|
|
504
|
+
// When the agent last changed model or effort: a harness the client renamed is no change of its.
|
|
505
|
+
export function profileChangedAt(db, agent) {
|
|
506
|
+
const changes = selectProfileChanges(db, agent);
|
|
507
|
+
const key = ({ profile }) => `${profile.model} ${profile.effort}`;
|
|
508
|
+
const keys = changes.map(key);
|
|
509
|
+
const at = keys.findLastIndex((each, i) => i > 0 && each !== keys[i - 1]);
|
|
510
|
+
return at < 0 ? null : (changes[at]?.at ?? null);
|
|
511
|
+
}
|
|
512
|
+
// A harness as one key, however it is written: "Claude Code" is claude-code, and "Open Code 2.0"
|
|
513
|
+
// is opencode, its version left out.
|
|
514
|
+
export const harnessKey = (name) => name
|
|
515
|
+
.toLowerCase()
|
|
516
|
+
.replace(/[\s_-]*v?\d+(?:\.\d+)*$/, '')
|
|
517
|
+
.replace(/[^a-z0-9]/g, '');
|
|
518
|
+
function sameFolder(a, b) {
|
|
519
|
+
const [x, y] = [pathKey(a, process.platform), pathKey(b, process.platform)];
|
|
520
|
+
const inside = (outer, inner) => inner === outer || inner.startsWith(outer.endsWith('/') ? outer : `${outer}/`);
|
|
521
|
+
return inside(x, y) || inside(y, x);
|
|
522
|
+
}
|
|
523
|
+
export function nameFor(db, { folder, harness, session, }, now = systemClock, processes = processAlive) {
|
|
524
|
+
const agents = listAgents(db, now, processes);
|
|
525
|
+
const alive = runningProcesses(db, now, processes);
|
|
526
|
+
const handed = handedOut(db, session, now(), alive);
|
|
527
|
+
const working = [
|
|
528
|
+
...new Set([...agents.filter(({ live }) => live).map(({ name }) => name), ...handed]),
|
|
529
|
+
];
|
|
530
|
+
const wanted = harness && harness.toLowerCase() !== 'cli' ? harnessKey(harness) : undefined;
|
|
531
|
+
// The harnesses it is known by: its MCP client's, and the one its profile declares. A profile
|
|
532
|
+
// declared for the command line (a test name, P6-R2-2) is known as cli; the command line it was
|
|
533
|
+
// merely seen through says nothing of the harness behind it.
|
|
534
|
+
const harnesses = (agent) => [agent.harness?.toLowerCase() === 'cli' ? null : agent.harness, agent.profile?.harness]
|
|
535
|
+
.filter((each) => Boolean(each))
|
|
536
|
+
.map(harnessKey);
|
|
537
|
+
// The best fit first: an agent whose harness is the one named, then one with a profile, then one
|
|
538
|
+
// with none (a name seen only on a failed command, P2S-R2-3); the most recently seen within each.
|
|
539
|
+
const rank = (agent) => wanted !== undefined && harnesses(agent).includes(wanted) ? 2 : agent.profile ? 1 : 0;
|
|
540
|
+
// Busy: a lease, or another session listening under the name. The MCP server asking (`session`,
|
|
541
|
+
// its process id) is no other session: a harness that keeps one server for its new sessions
|
|
542
|
+
// (OpenCode does) asks through the server its agent was heard by. A server that is gone (its
|
|
543
|
+
// harness was killed) is no session either.
|
|
544
|
+
const listens = (agent) => {
|
|
545
|
+
if (!agent.listening)
|
|
546
|
+
return false;
|
|
547
|
+
const heard = selectListener(db, agent.name);
|
|
548
|
+
if (!heard?.session)
|
|
549
|
+
return true;
|
|
550
|
+
return heard.session !== session && alive(heard.session);
|
|
551
|
+
};
|
|
552
|
+
// Its session ended: every process it was heard listening through is gone (P4-R3-2). Its
|
|
553
|
+
// lease is then the returning session's to carry on, and its last acts no other terminal's.
|
|
554
|
+
const listeners = selectListenerProcesses(db);
|
|
555
|
+
const ended = (agent) => listenersEnded(listeners.get(agent.name), alive);
|
|
556
|
+
// A lease keeps a name busy only while its session is alive: one heard through nothing that
|
|
557
|
+
// runs, quiet past the listening window, left it to whoever comes back under it (P7-R3-11).
|
|
558
|
+
const holds = (agent) => agent.holding.length > 0 && !ended(agent) && sessionAlive(db, agent.name, now, processes);
|
|
559
|
+
const busy = (agent) => listens(agent) || holds(agent) || handed.has(agent.name);
|
|
560
|
+
const earlier = agents
|
|
561
|
+
.filter((agent) => agent.connectedAt !== null && agent.folder !== null)
|
|
562
|
+
.filter((agent) => sameFolder(agent.folder ?? '', folder))
|
|
563
|
+
.filter((agent) => {
|
|
564
|
+
const known = harnesses(agent);
|
|
565
|
+
return wanted === undefined || known.length === 0 || known.includes(wanted);
|
|
566
|
+
})
|
|
567
|
+
.sort((a, b) => rank(b) - rank(a) || Date.parse(b.lastSeen) - Date.parse(a.lastSeen));
|
|
568
|
+
const fits = earlier.filter((agent) => !busy(agent));
|
|
569
|
+
const [first] = fits;
|
|
570
|
+
const taken = first === undefined ? earlier[0] : undefined;
|
|
571
|
+
const known = first ? (first.profile?.harness ?? first.harness) : null;
|
|
572
|
+
return {
|
|
573
|
+
name: first?.name ?? null,
|
|
574
|
+
// Live on a lease alone, quiet with nothing running, it acted no moment ago.
|
|
575
|
+
lately: first?.live === true && !ended(first) && sessionAlive(db, first.name, now, processes),
|
|
576
|
+
holding: first?.holding ?? [],
|
|
577
|
+
others: fits.slice(1).map(({ name }) => name),
|
|
578
|
+
working: working.filter((name) => name !== first?.name),
|
|
579
|
+
suggested: taken ? freeName(db, taken.name, handed) : null,
|
|
580
|
+
harness: first && harnesses(first).length > 0 ? known : null,
|
|
581
|
+
sameHarness: first !== undefined && wanted !== undefined && harnesses(first).includes(wanted),
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
// What otea whoami tells a session (nameFor), and the name it hands it noted as handed out, so a
|
|
585
|
+
// session asking next is handed another one. The MCP server asking (`session`) is the process
|
|
586
|
+
// calling: it runs.
|
|
587
|
+
export function offerName(db, asking, now = systemClock, alive = processAlive) {
|
|
588
|
+
const at = now();
|
|
589
|
+
if (asking.session !== undefined)
|
|
590
|
+
beatProcess(db, asking.session, () => at);
|
|
591
|
+
const advice = nameFor(db, asking, () => at, alive);
|
|
592
|
+
const name = advice.name ?? advice.suggested;
|
|
593
|
+
if (name !== null)
|
|
594
|
+
saveNameOffer(db, { name, at, session: asking.session ?? null });
|
|
595
|
+
return advice;
|
|
596
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { typeViews } from '../core/views.js';
|
|
2
|
+
import { listCollections } from '../store/queries.js';
|
|
3
|
+
// Types and fields as clients see them are built in the core, so Studio's preview builds them from
|
|
4
|
+
// a draft with the same code.
|
|
5
|
+
export { fieldViews, typeViews } from '../core/views.js';
|
|
6
|
+
function allFields(types) {
|
|
7
|
+
const fields = Object.fromEntries(types.flatMap((type) => Object.entries(type.fields)).reverse());
|
|
8
|
+
const order = [...new Set(types.flatMap((type) => Object.keys(type.fields)))];
|
|
9
|
+
return order.map((name) => ({ name, ...fields[name] }));
|
|
10
|
+
}
|
|
11
|
+
// Every loaded preset (official, then project) and the collections governed by it.
|
|
12
|
+
export function listPresetViews(ctx) {
|
|
13
|
+
const collections = listCollections(ctx.db);
|
|
14
|
+
return [...ctx.presets.presets.values()].map(({ name, version, origin, icon, types, view, names }) => ({
|
|
15
|
+
name,
|
|
16
|
+
version,
|
|
17
|
+
origin,
|
|
18
|
+
icon: icon ?? null,
|
|
19
|
+
fields: allFields(types),
|
|
20
|
+
types: typeViews(types),
|
|
21
|
+
view,
|
|
22
|
+
collections: collections.filter(({ preset }) => preset === name).map(({ name }) => name),
|
|
23
|
+
...(names ? { names } : {}),
|
|
24
|
+
}));
|
|
25
|
+
}
|