paseo-office 0.2.0 → 0.2.2
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/OVERVIEW.md +27 -0
- package/README.md +65 -134
- package/client/action-button.tsx +4 -1
- package/client/activity-panel.tsx +6 -4
- package/client/agent-appearance.tsx +257 -0
- package/client/agent-card.tsx +2 -1
- package/client/agent-picker-text.ts +23 -4
- package/client/agent-picker.tsx +5 -8
- package/client/board-sheet.tsx +48 -16
- package/client/board-text.ts +12 -2
- package/client/brand.ts +1 -0
- package/client/chat-chrome.tsx +318 -0
- package/client/chat-compose-tools.tsx +314 -0
- package/client/chat-dictation.tsx +133 -0
- package/client/chat-more.tsx +119 -0
- package/client/chat-panel.tsx +401 -146
- package/client/chat-quick.tsx +198 -0
- package/client/chat-text.ts +25 -6
- package/client/column-sheet.tsx +93 -23
- package/client/conversation-model.ts +19 -5
- package/client/duty-sheet.tsx +321 -0
- package/client/duty-text.ts +20 -0
- package/client/fullscreen-header.tsx +28 -13
- package/client/fullscreen-theme.ts +38 -0
- package/client/host-details.tsx +269 -0
- package/client/host-styles.ts +129 -0
- package/client/host-switches.tsx +212 -83
- package/client/incidents-card.tsx +2 -1
- package/client/janitor-card.tsx +1 -1
- package/client/library-card.tsx +2 -1
- package/client/list-text.ts +8 -6
- package/client/lobby-board.tsx +73 -53
- package/client/machine-card.tsx +225 -85
- package/client/messenger-styles.ts +216 -0
- package/client/new-agent-form.tsx +153 -61
- package/client/new-agent-model.ts +2 -0
- package/client/notes-card.tsx +111 -83
- package/client/notes-days.tsx +1 -1
- package/client/office-brand-context.ts +44 -0
- package/client/office-column.tsx +2 -1
- package/client/office-keyboard.ts +79 -0
- package/client/office-link.ts +7 -1
- package/client/office-mark.tsx +29 -0
- package/client/office-modal.tsx +440 -0
- package/client/office-settings.tsx +75 -30
- package/client/office-state.ts +12 -2
- package/client/office-surface.tsx +591 -81
- package/client/pixel/actors.ts +2 -1
- package/client/pixel/art.ts +33 -19
- package/client/pixel/pixel-office.tsx +2 -2
- package/client/prompt-coach.tsx +152 -0
- package/client/qa-card.tsx +2 -1
- package/client/review-card.tsx +2 -1
- package/client/room-board.tsx +26 -2
- package/client/scene-picker.tsx +158 -0
- package/client/scene-previews.ts +10 -0
- package/client/session-pane.tsx +25 -5
- package/client/session-panel-detail.tsx +238 -0
- package/client/session-panel.tsx +222 -137
- package/client/session-styles.ts +224 -1
- package/client/session-transcript.tsx +243 -0
- package/client/sessions-panel.tsx +181 -22
- package/client/sessions-text.ts +18 -0
- package/client/settings-sections.tsx +126 -0
- package/client/sidebar-launcher.ts +29 -0
- package/client/ssh-hosts.tsx +331 -0
- package/client/ssh-metrics.ts +76 -0
- package/client/standup-card.tsx +2 -1
- package/client/structure-view.tsx +20 -3
- package/client/styles.ts +170 -62
- package/client/team-chat-preview.tsx +96 -0
- package/client/tracker-connections.tsx +252 -0
- package/client/transcript-styles.ts +90 -0
- package/client/use-device-switch.ts +21 -12
- package/client/use-duty-actions.ts +43 -0
- package/client/use-group-photo.ts +48 -0
- package/client/use-new-agent.ts +4 -0
- package/client/use-street.ts +8 -0
- package/client/workspace-office-tools.tsx +102 -0
- package/client/workspace-overlay-route.web.ts +47 -0
- package/client/workspace-overlay.tsx +195 -0
- package/client/world/adapter.ts +37 -2
- package/client/world/escape.ts +58 -0
- package/client/world/use-world-feed.ts +6 -2
- package/client/world/workspace-dock.ts +6 -0
- package/client/world/world-api.ts +45 -4
- package/client/world/world-bridge-page.ts +14 -3
- package/client/world/world-bridge.ts +50 -5
- package/client/world/world-frame.web.ts +8 -0
- package/client/world/world-view-native.tsx +31 -10
- package/client/world/world-view-shared.ts +36 -7
- package/client/world/world-view.tsx +93 -21
- package/docs/office.md +165 -64
- package/index.client.tsx +13 -6
- package/index.server.ts +232 -16
- package/package.json +2 -1
- package/server/activity.ts +42 -19
- package/server/agent-tools.ts +11 -5
- package/server/board-service.ts +381 -10
- package/server/brand-logo.ts +34 -0
- package/server/chat-group.ts +83 -0
- package/server/chat-service.ts +406 -62
- package/server/chat-store.ts +1 -4
- package/server/directory.ts +22 -7
- package/server/duty-service.ts +230 -0
- package/server/group-photo.ts +39 -0
- package/server/home-departure.ts +47 -0
- package/server/hub.ts +43 -1
- package/server/incidents-jira.ts +19 -7
- package/server/incidents-schedule.ts +10 -10
- package/server/incidents-sentry.ts +34 -8
- package/server/live-ports.ts +114 -18
- package/server/lobby-pickup.ts +170 -0
- package/server/machine/details.ts +91 -0
- package/server/mention-queue.ts +14 -0
- package/server/office.ts +110 -6
- package/server/paths.ts +14 -0
- package/server/ssh/LICENSE.paseo +211 -0
- package/server/ssh/client.ts +401 -0
- package/server/ssh/discovery.ts +230 -0
- package/server/ssh/installed.ts +104 -0
- package/server/ssh/monitor.ts +104 -0
- package/server/ssh/pid-files.ts +194 -0
- package/server/ssh/read.ts +136 -0
- package/server/subagent-names.ts +4 -0
- package/server/tracker-credentials.ts +141 -0
- package/server/words.generated.ts +1 -1
- package/server/world.generated.ts +1 -1
- package/shared/appearance.ts +43 -0
- package/shared/branding.ts +53 -0
- package/shared/chat-words.ts +2 -2
- package/shared/contracts.ts +199 -5
- package/shared/duties.ts +100 -0
- package/shared/host-details.ts +25 -0
- package/shared/lobby-assignment.ts +34 -0
- package/shared/model.ts +9 -1
- package/shared/names.ts +2 -0
- package/shared/settings.ts +14 -3
- package/shared/ssh.ts +34 -0
- package/shared/upstream/README.md +1 -3
- package/shared/upstream/chat-engine.d.ts +4 -0
- package/shared/upstream/prompt-coach.d.ts +6 -0
- package/shared/upstream/prompt-coach.js +178 -0
package/server/live-ports.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { stat } from "node:fs/promises";
|
|
2
|
+
import type { AgentSnapshotPayload } from "@getpaseo/protocol/messages";
|
|
2
3
|
import { ACTIVITY_TAIL_LIMIT, startActivityPoller } from "./activity.js";
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
GroupSessionUnavailable,
|
|
6
|
+
lastAssistantText,
|
|
7
|
+
type ChatServicePorts,
|
|
8
|
+
} from "./chat-service.js";
|
|
9
|
+
import { PARENT_AGENT_ID_LABEL } from "./directory.js";
|
|
4
10
|
import type { Hub, HubPorts } from "./hub.js";
|
|
5
11
|
import type { JanitorPorts } from "./janitor.js";
|
|
6
12
|
import { wakeTargetOf } from "./mention-queue.js";
|
|
7
13
|
import { entryWritten, nodeNotesFs } from "./notes-files.js";
|
|
8
|
-
import type
|
|
14
|
+
import { NOTES_WRITER_TITLE, type NotesPorts } from "./notes-writer.js";
|
|
9
15
|
import type { OfficeAgentPorts } from "./office.js";
|
|
10
16
|
import { openPaseoDirectory } from "./paseo-directory.js";
|
|
11
17
|
import type { Paseo, PaseoHolder } from "./paseo-holder.js";
|
|
@@ -44,12 +50,52 @@ export function liveHubPorts(
|
|
|
44
50
|
};
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
/** A finished-turn notification is normal for an available agent. Other attention needs the user. */
|
|
54
|
+
function availableForOfficeWork(agent: AgentSnapshotPayload): boolean {
|
|
55
|
+
const attention = agent.requiresAttention || agent.attentionReason;
|
|
56
|
+
return (
|
|
57
|
+
agent.status === "idle" &&
|
|
58
|
+
!agent.archivedAt &&
|
|
59
|
+
!agent.activeTurn &&
|
|
60
|
+
!agent.backgroundWork?.count &&
|
|
61
|
+
!agent.providerUnavailable &&
|
|
62
|
+
agent.pendingPermissions.length === 0 &&
|
|
63
|
+
(!attention || agent.attentionReason === "finished") &&
|
|
64
|
+
!agent.internal &&
|
|
65
|
+
!agent.background &&
|
|
66
|
+
!agent.labels?.[PARENT_AGENT_ID_LABEL]?.trim() &&
|
|
67
|
+
agent.title !== NOTES_WRITER_TITLE
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
47
71
|
/** Wake ports on the daemon's own API; the holder is set by every hook and handler. */
|
|
48
72
|
export function liveAgentPorts(holder: PaseoHolder): OfficeAgentPorts {
|
|
49
73
|
// Resolves null for an agent the daemon does not know; rejects only for an empty id or a start
|
|
50
74
|
// of an id that several agents share.
|
|
51
75
|
const refresh = (agentId: string) => holder.require().agents.ref(agentId).refresh();
|
|
52
76
|
return {
|
|
77
|
+
async lobbyCandidates() {
|
|
78
|
+
if (!holder.held()) return [];
|
|
79
|
+
const agents = holder.require().agents;
|
|
80
|
+
const ids = new Set<string>();
|
|
81
|
+
let cursor: string | undefined;
|
|
82
|
+
do {
|
|
83
|
+
const result = await agents.list({
|
|
84
|
+
filter: { statuses: ["idle"] },
|
|
85
|
+
sort: [{ key: "updated_at", direction: "asc" }],
|
|
86
|
+
page: { limit: 200, cursor },
|
|
87
|
+
});
|
|
88
|
+
for (const { agent } of result.entries) {
|
|
89
|
+
if (availableForOfficeWork(agent)) ids.add(agent.id);
|
|
90
|
+
}
|
|
91
|
+
cursor = result.pageInfo.nextCursor ?? undefined;
|
|
92
|
+
} while (cursor);
|
|
93
|
+
return [...ids];
|
|
94
|
+
},
|
|
95
|
+
async lobbyEligible(agentId) {
|
|
96
|
+
const result = await refresh(agentId);
|
|
97
|
+
return result !== null && availableForOfficeWork(result.agent);
|
|
98
|
+
},
|
|
53
99
|
async status(agentId) {
|
|
54
100
|
const result = await refresh(agentId);
|
|
55
101
|
return wakeTargetOf(result === null ? null : result.agent);
|
|
@@ -59,35 +105,85 @@ export function liveAgentPorts(holder: PaseoHolder): OfficeAgentPorts {
|
|
|
59
105
|
// The daemon finds archived agents by id too; the queue would drop a post for one unsent.
|
|
60
106
|
return result === null || wakeTargetOf(result.agent) === "gone" ? null : result.agent.id;
|
|
61
107
|
},
|
|
108
|
+
// Waits for the API: the boards load, and copy the lobby claims, before any hook has run.
|
|
109
|
+
async workspaceOf(agentId) {
|
|
110
|
+
const result = await (await holder.wait()).agents.ref(agentId).refresh();
|
|
111
|
+
return result?.agent.workspaceId ?? null;
|
|
112
|
+
},
|
|
62
113
|
// A turn may start between the status read and the send. A prompt with a source agent joins
|
|
63
114
|
// it (the daemon steers agent messages); one as from the user would cut it unless it steers.
|
|
64
|
-
send: (agentId, prompt, source) =>
|
|
115
|
+
send: (agentId, prompt, source, messageId) =>
|
|
65
116
|
holder
|
|
66
117
|
.require()
|
|
67
118
|
.agents.ref(agentId)
|
|
68
|
-
.send(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
119
|
+
.send(prompt, {
|
|
120
|
+
...(source === null
|
|
121
|
+
? { activeTurnBehavior: "steer" as const }
|
|
122
|
+
: { sourceAgentId: source }),
|
|
123
|
+
...(messageId ? { messageId } : {}),
|
|
124
|
+
}),
|
|
72
125
|
};
|
|
73
126
|
}
|
|
74
127
|
|
|
75
128
|
/** The team chat's ports on the daemon's own API; the holder is set by every hook and handler. */
|
|
76
|
-
export function liveChatPorts(holder: PaseoHolder): Pick<ChatServicePorts, "send" | "
|
|
129
|
+
export function liveChatPorts(holder: PaseoHolder): Pick<ChatServicePorts, "send" | "readTurn"> {
|
|
77
130
|
return {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
131
|
+
async send(agentId, prompt, images, delivery) {
|
|
132
|
+
const ref = holder.require().agents.ref(agentId);
|
|
133
|
+
if (delivery?.group) {
|
|
134
|
+
const result = await ref.refresh();
|
|
135
|
+
if (!result || !availableForOfficeWork(result.agent))
|
|
136
|
+
throw new GroupSessionUnavailable("The session is no longer available for a group reply");
|
|
137
|
+
}
|
|
138
|
+
await ref.send(prompt, {
|
|
139
|
+
activeTurnBehavior: "steer",
|
|
140
|
+
...(delivery ? { messageId: delivery.messageId } : {}),
|
|
141
|
+
...(images ? { images } : {}),
|
|
142
|
+
});
|
|
143
|
+
},
|
|
81
144
|
// The hook's own recipe (PluginHookEvents["agent.turn.after"]): the latest whole turn, kept
|
|
82
145
|
// only when it is the turn that ended. A null turn id has no entries.
|
|
83
|
-
async
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
146
|
+
async readTurn(agentId, turnId, group, officeIds = []) {
|
|
147
|
+
const missing = { matches: !group, text: null };
|
|
148
|
+
if (turnId === null) return missing;
|
|
149
|
+
const timeline = holder.require().agents.ref(agentId).timeline;
|
|
150
|
+
const options = { limit: 1, wholeTurns: true };
|
|
151
|
+
let page = await timeline.refetch({ direction: "tail", ...options });
|
|
152
|
+
// Another turn may already have started by the time this hook reads. Only walk back to
|
|
153
|
+
// the reservation; a group message cannot be present in any older turn.
|
|
154
|
+
if (group)
|
|
155
|
+
while (
|
|
156
|
+
!page.error &&
|
|
157
|
+
!page.entries.some((entry) => entry.turnId === turnId) &&
|
|
158
|
+
page.hasOlder &&
|
|
159
|
+
page.startCursor &&
|
|
160
|
+
Date.parse(page.entries[0]?.timestamp ?? "") >= group.sentAt
|
|
161
|
+
) {
|
|
162
|
+
page = await timeline.refetch({
|
|
163
|
+
direction: "before",
|
|
164
|
+
cursor: page.startCursor,
|
|
165
|
+
...options,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
if (page.error) throw new Error(page.error);
|
|
89
169
|
const turn = page.entries.filter((entry) => entry.turnId === turnId);
|
|
90
|
-
|
|
170
|
+
const items = turn.map((entry) => entry.item);
|
|
171
|
+
if (!group) return { matches: true, text: lastAssistantText(items) };
|
|
172
|
+
const index = items.findIndex(
|
|
173
|
+
(item) =>
|
|
174
|
+
item.type === "user_message" &&
|
|
175
|
+
(item.clientMessageId === group.messageId || item.messageId === group.messageId),
|
|
176
|
+
);
|
|
177
|
+
if (index < 0) return missing;
|
|
178
|
+
const known = new Set([group.messageId, ...officeIds]);
|
|
179
|
+
const privateTurn = items.some(
|
|
180
|
+
(item) =>
|
|
181
|
+
item.type === "user_message" &&
|
|
182
|
+
!known.has(item.clientMessageId ?? "") &&
|
|
183
|
+
!known.has(item.messageId ?? ""),
|
|
184
|
+
);
|
|
185
|
+
if (privateTurn) return { matches: true, privateTurn: true, text: null };
|
|
186
|
+
return { matches: true, text: lastAssistantText(items.slice(index + 1)) };
|
|
91
187
|
},
|
|
92
188
|
};
|
|
93
189
|
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { DUTY, LOBBY, USER_AUTHOR, type Post } from "../shared/contracts.js";
|
|
2
|
+
import { assignmentFooter } from "../shared/lobby-assignment.js";
|
|
3
|
+
import type { BoardService } from "./board-service.js";
|
|
4
|
+
import { errorMessage } from "./errors.js";
|
|
5
|
+
import type { MentionQueue } from "./mention-queue.js";
|
|
6
|
+
import type { OfficeAgentPorts } from "./office.js";
|
|
7
|
+
import { indented, oneLine } from "./prompt-text.js";
|
|
8
|
+
import type { Clock } from "./timers.js";
|
|
9
|
+
|
|
10
|
+
// Some transitions to idle (e.g. clearing a permission) don't produce a turn-end hook.
|
|
11
|
+
export const LOBBY_RETRY_MS = 30_000;
|
|
12
|
+
|
|
13
|
+
export interface LobbyPickup {
|
|
14
|
+
refresh(): void;
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PickupPorts {
|
|
19
|
+
service: BoardService;
|
|
20
|
+
agents: OfficeAgentPorts;
|
|
21
|
+
queue: MentionQueue;
|
|
22
|
+
clock: Clock;
|
|
23
|
+
enabled(): boolean;
|
|
24
|
+
/** A duty post that is handed out: one with instructions. A pretend duty is acted out in 3D. */
|
|
25
|
+
instructed(post: Post): boolean;
|
|
26
|
+
log(message: string): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function awaitingPickup(task: Post): boolean {
|
|
30
|
+
return (
|
|
31
|
+
task.kind === "task" &&
|
|
32
|
+
task.state === "open" &&
|
|
33
|
+
(!task.to || !!task.namedPickup) &&
|
|
34
|
+
!task.claimedBy
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The first line of an assignment: whose task it is, or the duty board's. */
|
|
39
|
+
function assignmentHeading(task: Post): string {
|
|
40
|
+
if (task.board === DUTY) return `The office assigned you duty ${task.id} from the duty board.`;
|
|
41
|
+
const author = task.author === USER_AUTHOR ? "the user" : `agent ${oneLine(task.author)}`;
|
|
42
|
+
return `The office assigned you lobby task ${task.id} from ${author}.`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createLobbyPickup(ports: PickupPorts): LobbyPickup {
|
|
46
|
+
const { service, agents, queue, clock } = ports;
|
|
47
|
+
let stopped = false;
|
|
48
|
+
let scanning: Promise<void> | null = null;
|
|
49
|
+
let cancel: (() => void) | undefined;
|
|
50
|
+
|
|
51
|
+
const active = () => !stopped && !service.wakesPaused();
|
|
52
|
+
const canDispatch = (task: Post) => Boolean(task.namedPickup) || ports.enabled();
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The lobby's tasks and the duties handed out: what the office hands to idle agents. A duty
|
|
56
|
+
* already claimed, or with a delivery under way, stays even once it has lost its instructions,
|
|
57
|
+
* so its delivery is recovered and its agent is not left holding it unseen.
|
|
58
|
+
*/
|
|
59
|
+
function pickupPosts(): Post[] {
|
|
60
|
+
const handedOut = (post: Post) =>
|
|
61
|
+
ports.instructed(post) || post.state === "claimed" || Boolean(post.pickupMessageId);
|
|
62
|
+
return [...service.postsOf(LOBBY), ...service.postsOf(DUTY).filter(handedOut)];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hasWork(): boolean {
|
|
66
|
+
return (
|
|
67
|
+
active() &&
|
|
68
|
+
pickupPosts().some(
|
|
69
|
+
(task) => canDispatch(task) && (awaitingPickup(task) || Boolean(task.pickupMessageId)),
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function currentClaim(claim: Post): boolean {
|
|
75
|
+
return pickupPosts().some(
|
|
76
|
+
(post) =>
|
|
77
|
+
post.id === claim.id &&
|
|
78
|
+
post.seq === claim.seq &&
|
|
79
|
+
post.pickupMessageId === claim.pickupMessageId,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function dispatch(task: Post, agentId: string, available: () => boolean): Promise<boolean> {
|
|
84
|
+
const eligible = () =>
|
|
85
|
+
task.namedPickup
|
|
86
|
+
? agents.status(agentId).then((status) => status === "wakeable")
|
|
87
|
+
: agents.lobbyEligible(agentId);
|
|
88
|
+
if (task.namedPickup && !(await eligible())) return false;
|
|
89
|
+
const recovering = Boolean(task.pickupMessageId);
|
|
90
|
+
const claim = recovering ? task : await service.claimLobbyTask(task.id, agentId, task.seq);
|
|
91
|
+
if (!claim?.pickupMessageId) return false;
|
|
92
|
+
// Check again after saving the claim. Never reassign an earlier send whose receipt was lost.
|
|
93
|
+
if (!(await eligible()) || !canDispatch(task) || !active() || !available()) {
|
|
94
|
+
if (!recovering) await service.releaseLobbyTask(claim);
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
if (!currentClaim(claim)) return false;
|
|
98
|
+
const source = task.author === USER_AUTHOR ? null : task.author;
|
|
99
|
+
const prompt = [
|
|
100
|
+
assignmentHeading(task),
|
|
101
|
+
` ${indented(task.text)}`,
|
|
102
|
+
assignmentFooter(claim),
|
|
103
|
+
].join("\n");
|
|
104
|
+
// The daemon deduplicates this ID even after a network failure or a plugin restart.
|
|
105
|
+
await agents.send(agentId, prompt, source, claim.pickupMessageId);
|
|
106
|
+
await service.confirmLobbyDelivery(claim);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function nextFor(agentId: string, automatic: boolean): Post | undefined {
|
|
111
|
+
const board = pickupPosts();
|
|
112
|
+
const existing = board.find((p) => p.state === "claimed" && p.claimedBy === agentId);
|
|
113
|
+
if (existing) return existing.pickupMessageId && canDispatch(existing) ? existing : undefined;
|
|
114
|
+
return board
|
|
115
|
+
.filter(
|
|
116
|
+
(p) => awaitingPickup(p) && p.author !== agentId && (p.to ? p.to === agentId : automatic),
|
|
117
|
+
)
|
|
118
|
+
.sort(
|
|
119
|
+
(a, b) =>
|
|
120
|
+
Number(Boolean(b.to)) - Number(Boolean(a.to)) ||
|
|
121
|
+
a.createdAt.localeCompare(b.createdAt) ||
|
|
122
|
+
Number(a.id.slice(1)) - Number(b.id.slice(1)),
|
|
123
|
+
)[0];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function scan(): Promise<void> {
|
|
127
|
+
const named = service
|
|
128
|
+
.postsOf(LOBBY)
|
|
129
|
+
.flatMap((p) =>
|
|
130
|
+
p.namedPickup && p.to && (awaitingPickup(p) || p.pickupMessageId) ? [p.to] : [],
|
|
131
|
+
);
|
|
132
|
+
const automatic = new Set(ports.enabled() ? await agents.lobbyCandidates() : []);
|
|
133
|
+
for (const agentId of new Set([...named, ...automatic])) {
|
|
134
|
+
if (!hasWork()) break;
|
|
135
|
+
const next = nextFor(agentId, automatic.has(agentId));
|
|
136
|
+
if (!next) continue;
|
|
137
|
+
try {
|
|
138
|
+
await queue.tryPickup(agentId, (available) => dispatch(next, agentId, available));
|
|
139
|
+
} catch (error) {
|
|
140
|
+
ports.log(`[office] lobby pickup for ${agentId} failed: ${errorMessage(error)}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function schedule(delay: number): void {
|
|
146
|
+
cancel?.();
|
|
147
|
+
cancel = undefined;
|
|
148
|
+
if (scanning || !hasWork()) return;
|
|
149
|
+
cancel = clock.setTimeout(() => {
|
|
150
|
+
cancel = undefined;
|
|
151
|
+
scanning = scan()
|
|
152
|
+
.catch((error: unknown) =>
|
|
153
|
+
ports.log(`[office] lobby pickup failed: ${errorMessage(error)}`),
|
|
154
|
+
)
|
|
155
|
+
.finally(() => {
|
|
156
|
+
scanning = null;
|
|
157
|
+
schedule(LOBBY_RETRY_MS);
|
|
158
|
+
});
|
|
159
|
+
}, delay);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
refresh: () => schedule(0),
|
|
164
|
+
async stop() {
|
|
165
|
+
stopped = true;
|
|
166
|
+
cancel?.();
|
|
167
|
+
await scanning;
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile, readdir, statfs } from "node:fs/promises";
|
|
3
|
+
import { availableParallelism, hostname, loadavg } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import type { HostDetails, HostSample } from "../../shared/host-details.js";
|
|
7
|
+
import type { Machine } from "../../shared/contracts.js";
|
|
8
|
+
import { memoryPercent } from "../../shared/machine-text.js";
|
|
9
|
+
|
|
10
|
+
const exec = promisify(execFile);
|
|
11
|
+
const WINDOW = 30 * 60_000;
|
|
12
|
+
|
|
13
|
+
export function recordSample(history: HostSample[], machine: Machine) {
|
|
14
|
+
const at = Date.parse(machine.readAt);
|
|
15
|
+
while (history.length && (history[0].at < at - WINDOW || history.length >= 180)) history.shift();
|
|
16
|
+
history.push({ at, ram: memoryPercent(machine), cpu: machine.cpuPercent });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function swapReading(text: string) {
|
|
20
|
+
const total = /^SwapTotal:\s+(\d+) kB$/m.exec(text);
|
|
21
|
+
const free = /^SwapFree:\s+(\d+) kB$/m.exec(text);
|
|
22
|
+
if (!total || !free) return null;
|
|
23
|
+
return {
|
|
24
|
+
total: Number(total[1]) * 1024,
|
|
25
|
+
used: Math.max(0, Number(total[1]) - Number(free[1])) * 1024,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function rankedApps(root: string): Promise<HostDetails["apps"]> {
|
|
30
|
+
const names = (await readdir(root)).filter((name) => /^\d+$/.test(name));
|
|
31
|
+
const groups = new Map<string, HostDetails["apps"][number]>();
|
|
32
|
+
for (let first = 0; first < names.length; first += 32) {
|
|
33
|
+
const statuses = await Promise.all(
|
|
34
|
+
names
|
|
35
|
+
.slice(first, first + 32)
|
|
36
|
+
.map((pid) => readFile(join(root, pid, "status"), "utf8").catch(() => "")),
|
|
37
|
+
);
|
|
38
|
+
for (const status of statuses) {
|
|
39
|
+
const name = /^Name:\s*([^\n]+)/m.exec(status)?.[1].trim();
|
|
40
|
+
const kb = /^VmRSS:\s+(\d+) kB$/m.exec(status)?.[1];
|
|
41
|
+
if (!name || !kb || Number(kb) === 0) continue;
|
|
42
|
+
const group = groups.get(name) ?? { name, bytes: 0, count: 0 };
|
|
43
|
+
group.bytes += Number(kb) * 1024;
|
|
44
|
+
group.count += 1;
|
|
45
|
+
groups.set(name, group);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return [...groups.values()]
|
|
49
|
+
.sort((a, b) => b.bytes - a.bytes || a.name.localeCompare(b.name))
|
|
50
|
+
.slice(0, 12);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Only fixed cache directories are measured. A missing cache or a slow filesystem can be skipped.
|
|
54
|
+
async function cacheSize(name: string, folder: string) {
|
|
55
|
+
try {
|
|
56
|
+
const { stdout } = await exec("du", ["-skx", "--", folder], { timeout: 2000, maxBuffer: 4096 });
|
|
57
|
+
const kb = /^(\d+)\s/.exec(stdout)?.[1];
|
|
58
|
+
return kb ? { name, bytes: Number(kb) * 1024 } : null;
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function readHostDetails(
|
|
65
|
+
procRoot: string,
|
|
66
|
+
home: string,
|
|
67
|
+
history: HostSample[],
|
|
68
|
+
): Promise<HostDetails> {
|
|
69
|
+
const [apps, memory, fs, caches] = await Promise.all([
|
|
70
|
+
rankedApps(procRoot),
|
|
71
|
+
readFile(join(procRoot, "meminfo"), "utf8").catch(() => ""),
|
|
72
|
+
statfs(home).catch(() => null),
|
|
73
|
+
Promise.all([
|
|
74
|
+
cacheSize("npm download cache", join(home, ".npm", "_cacache")),
|
|
75
|
+
cacheSize("pip download cache", join(home, ".cache", "pip")),
|
|
76
|
+
]),
|
|
77
|
+
]);
|
|
78
|
+
const readAt = Date.now();
|
|
79
|
+
return {
|
|
80
|
+
readAt,
|
|
81
|
+
hostname: hostname(),
|
|
82
|
+
cores: availableParallelism(),
|
|
83
|
+
load: loadavg(),
|
|
84
|
+
swap: swapReading(memory),
|
|
85
|
+
disk:
|
|
86
|
+
fs && fs.blocks > 0 ? { available: fs.bavail * fs.bsize, total: fs.blocks * fs.bsize } : null,
|
|
87
|
+
apps,
|
|
88
|
+
caches: caches.filter((row) => row !== null),
|
|
89
|
+
history: history.filter((row) => row.at >= readAt - WINDOW),
|
|
90
|
+
};
|
|
91
|
+
}
|
package/server/mention-queue.ts
CHANGED
|
@@ -55,6 +55,8 @@ export interface MentionQueuePorts {
|
|
|
55
55
|
|
|
56
56
|
export interface MentionQueue {
|
|
57
57
|
enqueue(agentId: string, delivery: Delivery): void;
|
|
58
|
+
/** Shares the wake limit with mentions. A queued mention gets first use of the agent. */
|
|
59
|
+
tryPickup(agentId: string, run: (available: () => boolean) => Promise<boolean>): Promise<void>;
|
|
58
60
|
/** From agent.turn.after: the agent may be idle now. */
|
|
59
61
|
turnEnded(agentId: string): void;
|
|
60
62
|
/** From agent.close.after and agent.archive.after. */
|
|
@@ -185,6 +187,18 @@ export function createMentionQueue(ports: MentionQueuePorts, paused: boolean): M
|
|
|
185
187
|
}
|
|
186
188
|
|
|
187
189
|
return {
|
|
190
|
+
async tryPickup(agentId, run) {
|
|
191
|
+
const available = () => !stopped && !isPaused && !queues.has(agentId);
|
|
192
|
+
if (!available() || inFlight.has(agentId) || recentWakes(agentId).length >= WAKES_PER_HOUR)
|
|
193
|
+
return;
|
|
194
|
+
inFlight.add(agentId);
|
|
195
|
+
try {
|
|
196
|
+
if (await run(available)) wakes.set(agentId, [...recentWakes(agentId), ports.clock.now()]);
|
|
197
|
+
} finally {
|
|
198
|
+
inFlight.delete(agentId);
|
|
199
|
+
if (queues.has(agentId)) attempt(agentId);
|
|
200
|
+
}
|
|
201
|
+
},
|
|
188
202
|
enqueue(agentId, delivery) {
|
|
189
203
|
if (stopped || delivery.author === agentId) return;
|
|
190
204
|
queues.set(agentId, [...(queues.get(agentId) ?? []), delivery]);
|