opencode-subagent-tools 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/README.md +23 -18
- package/SECURITY.md +1 -1
- package/SKILL.md +60 -20
- package/docs/development.md +14 -10
- package/docs/installation.md +1 -7
- package/docs/limitations.md +45 -9
- package/docs/reference.md +83 -32
- package/package.json +5 -5
- package/src/access.ts +79 -79
- package/src/background.ts +602 -0
- package/src/config.ts +47 -47
- package/src/cursor.ts +41 -39
- package/src/discovery.ts +77 -69
- package/src/errors.ts +7 -7
- package/src/events.ts +141 -95
- package/src/format.ts +20 -20
- package/src/index.ts +49 -36
- package/src/interrupt.ts +4 -4
- package/src/live-state.ts +64 -41
- package/src/messages.ts +134 -84
- package/src/permissions.ts +18 -18
- package/src/session-row.ts +82 -21
- package/src/session.ts +69 -69
- package/src/skill.ts +24 -24
- package/src/status.ts +64 -64
- package/src/text.ts +16 -16
- package/src/tokens.ts +51 -51
- package/src/tools/cancel-descendants.ts +303 -170
- package/src/tools/cancel.ts +139 -73
- package/src/tools/index.ts +32 -30
- package/src/tools/input.ts +46 -44
- package/src/tools/list.ts +281 -175
- package/src/tools/names.ts +2 -2
- package/src/tools/output.ts +51 -51
- package/src/tools/send.ts +130 -96
- package/src/tools/usage.ts +181 -143
package/src/live-state.ts
CHANGED
|
@@ -1,90 +1,113 @@
|
|
|
1
|
-
import type { SessionInfoLike } from "./session"
|
|
2
|
-
import {
|
|
1
|
+
import type { SessionInfoLike } from "./session";
|
|
2
|
+
import {
|
|
3
|
+
RUN_STATUS,
|
|
4
|
+
TERMINAL_RUN_STATUSES,
|
|
5
|
+
runStatusFromSession,
|
|
6
|
+
type RunStatus,
|
|
7
|
+
} from "./status";
|
|
3
8
|
|
|
4
|
-
export const MAX_LIVE_SESSIONS = 500
|
|
9
|
+
export const MAX_LIVE_SESSIONS = 500;
|
|
5
10
|
|
|
6
11
|
/** Process-local live evidence for a session. Never persisted. */
|
|
7
12
|
export interface LiveState {
|
|
8
13
|
/** Last live start timestamp; 0 when the last observed transition was terminal. */
|
|
9
|
-
activeAt: number
|
|
10
|
-
status?:
|
|
14
|
+
activeAt: number;
|
|
15
|
+
status?: RunStatus;
|
|
11
16
|
/** Last live start/terminal timestamp, used to drop out-of-order events. */
|
|
12
|
-
lastStartedAt: number
|
|
13
|
-
lastTerminalAt: number
|
|
17
|
+
lastStartedAt: number;
|
|
18
|
+
lastTerminalAt: number;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
export const lastTransitionAt = (state: LiveState | undefined): number =>
|
|
17
|
-
Math.max(state?.lastStartedAt ?? 0, state?.lastTerminalAt ?? 0)
|
|
22
|
+
Math.max(state?.lastStartedAt ?? 0, state?.lastTerminalAt ?? 0);
|
|
18
23
|
|
|
19
24
|
export const createLiveState = () => {
|
|
20
|
-
const live = new Map<string, LiveState>()
|
|
25
|
+
const live = new Map<string, LiveState>();
|
|
21
26
|
|
|
22
27
|
const rememberLive = (id: string, patch: Partial<LiveState>) => {
|
|
23
|
-
const previous = live.get(id)
|
|
28
|
+
const previous = live.get(id);
|
|
24
29
|
// Re-insert so Map iteration order stays least-recently-used first.
|
|
25
|
-
if (previous) live.delete(id)
|
|
30
|
+
if (previous) live.delete(id);
|
|
26
31
|
live.set(id, {
|
|
27
32
|
activeAt: patch.activeAt ?? previous?.activeAt ?? 0,
|
|
28
33
|
status: patch.status ?? previous?.status,
|
|
29
34
|
lastStartedAt: patch.lastStartedAt ?? previous?.lastStartedAt ?? 0,
|
|
30
35
|
lastTerminalAt: patch.lastTerminalAt ?? previous?.lastTerminalAt ?? 0,
|
|
31
|
-
})
|
|
36
|
+
});
|
|
32
37
|
while (live.size > MAX_LIVE_SESSIONS) {
|
|
33
|
-
const oldest = live.keys().next().value
|
|
34
|
-
if (oldest === undefined) break
|
|
35
|
-
live.delete(oldest)
|
|
38
|
+
const oldest = live.keys().next().value;
|
|
39
|
+
if (oldest === undefined) break;
|
|
40
|
+
live.delete(oldest);
|
|
36
41
|
}
|
|
37
|
-
}
|
|
42
|
+
};
|
|
38
43
|
|
|
39
44
|
const rememberStarted = (id: string, at: number) => {
|
|
40
|
-
rememberLive(id, { activeAt: at, status:
|
|
41
|
-
}
|
|
45
|
+
rememberLive(id, { activeAt: at, status: RUN_STATUS.running, lastStartedAt: at });
|
|
46
|
+
};
|
|
42
47
|
|
|
43
|
-
const rememberTerminal = (id: string, status:
|
|
44
|
-
rememberLive(id, { activeAt: 0, status, lastTerminalAt: at })
|
|
45
|
-
}
|
|
48
|
+
const rememberTerminal = (id: string, status: RunStatus, at: number) => {
|
|
49
|
+
rememberLive(id, { activeAt: 0, status, lastTerminalAt: at });
|
|
50
|
+
};
|
|
46
51
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
const forget = (id: string) => {
|
|
53
|
+
live.delete(id);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const liveStatusFor = (id: string): RunStatus | undefined => {
|
|
57
|
+
const state = live.get(id);
|
|
58
|
+
if (!state) return undefined;
|
|
59
|
+
if (state.activeAt > 0) return RUN_STATUS.running;
|
|
60
|
+
return state.status;
|
|
61
|
+
};
|
|
53
62
|
|
|
54
63
|
/**
|
|
55
64
|
* Prefer a live start newer than the session marker (a resumed child with a
|
|
56
65
|
* stale outcome), then a live terminal or idle event at least as new as the
|
|
57
66
|
* marker. Everything else falls back to the session record.
|
|
58
67
|
*/
|
|
59
|
-
const statusFor = (info: SessionInfoLike, id: string):
|
|
60
|
-
const state = live.get(id)
|
|
68
|
+
const statusFor = (info: SessionInfoLike, id: string): RunStatus => {
|
|
69
|
+
const state = live.get(id);
|
|
61
70
|
// `time.idle` is the canonical terminal marker. When it is absent, use
|
|
62
71
|
// `time.updated` as a conservative marker so a stale live event cannot
|
|
63
72
|
// override a newer session record.
|
|
64
|
-
const marker = info.time?.idle ?? info.time?.updated ?? 0
|
|
65
|
-
const activeAt = state?.activeAt ?? 0
|
|
66
|
-
if (activeAt > 0 && (marker === 0 || activeAt > marker)) return
|
|
73
|
+
const marker = info.time?.idle ?? info.time?.updated ?? 0;
|
|
74
|
+
const activeAt = state?.activeAt ?? 0;
|
|
75
|
+
if (activeAt > 0 && (marker === 0 || activeAt > marker)) return RUN_STATUS.running;
|
|
67
76
|
|
|
68
|
-
const
|
|
69
|
-
const
|
|
77
|
+
const sessionStatus = runStatusFromSession(info);
|
|
78
|
+
const liveStatus = state?.status;
|
|
79
|
+
const liveAt = state?.lastTerminalAt ?? 0;
|
|
70
80
|
if (liveStatus !== undefined) {
|
|
71
81
|
const terminal =
|
|
72
|
-
|
|
73
|
-
|
|
82
|
+
TERMINAL_RUN_STATUSES.has(liveStatus) ||
|
|
83
|
+
(liveStatus === RUN_STATUS.unknown && liveAt > 0);
|
|
84
|
+
if (terminal && (marker === 0 || liveAt >= marker)) {
|
|
85
|
+
// An idle-derived `unknown` must not hide a terminal outcome that the
|
|
86
|
+
// session record already knows. Other live evidence keeps precedence.
|
|
87
|
+
if (
|
|
88
|
+
liveStatus === RUN_STATUS.unknown &&
|
|
89
|
+
sessionStatus !== undefined &&
|
|
90
|
+
TERMINAL_RUN_STATUSES.has(sessionStatus)
|
|
91
|
+
) {
|
|
92
|
+
return sessionStatus;
|
|
93
|
+
}
|
|
94
|
+
return liveStatus;
|
|
95
|
+
}
|
|
74
96
|
}
|
|
75
97
|
|
|
76
|
-
return
|
|
77
|
-
}
|
|
98
|
+
return sessionStatus ?? RUN_STATUS.unknown;
|
|
99
|
+
};
|
|
78
100
|
|
|
79
101
|
return {
|
|
80
102
|
live,
|
|
81
103
|
rememberLive,
|
|
82
104
|
rememberStarted,
|
|
83
105
|
rememberTerminal,
|
|
106
|
+
forget,
|
|
84
107
|
liveStatusFor,
|
|
85
108
|
statusFor,
|
|
86
109
|
lastTransitionAt: (id: string) => lastTransitionAt(live.get(id)),
|
|
87
|
-
}
|
|
88
|
-
}
|
|
110
|
+
};
|
|
111
|
+
};
|
|
89
112
|
|
|
90
|
-
export type LiveStore = ReturnType<typeof createLiveState
|
|
113
|
+
export type LiveStore = ReturnType<typeof createLiveState>;
|
package/src/messages.ts
CHANGED
|
@@ -1,114 +1,135 @@
|
|
|
1
|
-
import type { Plugin } from "@opencode/plugin"
|
|
2
|
-
import type { Access } from "./access"
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { Access } from "./access";
|
|
3
|
+
import {
|
|
4
|
+
isSessionActive,
|
|
5
|
+
type BackgroundJobInfo,
|
|
6
|
+
type BackgroundJobStore,
|
|
7
|
+
} from "./background";
|
|
8
|
+
import { errorMessage } from "./errors";
|
|
9
|
+
import type { LiveStore } from "./live-state";
|
|
10
|
+
import type { Guard } from "./permissions";
|
|
11
|
+
import { guardTarget } from "./permissions";
|
|
12
|
+
import { RUN_STATUS, runStatusIsIdle, type RunStatus } from "./status";
|
|
13
|
+
import type { ToolCtx } from "./tools/input";
|
|
9
14
|
|
|
10
15
|
export const DELIVERY = {
|
|
11
16
|
steer: "steer",
|
|
12
17
|
queue: "queue",
|
|
13
|
-
} as const
|
|
18
|
+
} as const;
|
|
14
19
|
|
|
15
|
-
export type Delivery = (typeof DELIVERY)[keyof typeof DELIVERY]
|
|
20
|
+
export type Delivery = (typeof DELIVERY)[keyof typeof DELIVERY];
|
|
16
21
|
|
|
17
|
-
export const DELIVERIES: readonly Delivery[] = Object.values(DELIVERY)
|
|
18
|
-
const DELIVERY_SET: ReadonlySet<string> = new Set(DELIVERIES)
|
|
22
|
+
export const DELIVERIES: readonly Delivery[] = Object.values(DELIVERY);
|
|
23
|
+
const DELIVERY_SET: ReadonlySet<string> = new Set(DELIVERIES);
|
|
19
24
|
|
|
20
25
|
export const isDelivery = (value: unknown): value is Delivery =>
|
|
21
|
-
typeof value === "string" && DELIVERY_SET.has(value)
|
|
26
|
+
typeof value === "string" && DELIVERY_SET.has(value);
|
|
22
27
|
|
|
23
28
|
export const SEND_KIND = {
|
|
24
29
|
prompt: "prompt",
|
|
25
30
|
synthetic: "synthetic",
|
|
26
|
-
} as const
|
|
31
|
+
} as const;
|
|
27
32
|
|
|
28
|
-
export type SendKind = (typeof SEND_KIND)[keyof typeof SEND_KIND]
|
|
33
|
+
export type SendKind = (typeof SEND_KIND)[keyof typeof SEND_KIND];
|
|
29
34
|
|
|
30
|
-
export const SEND_KINDS: readonly SendKind[] = Object.values(SEND_KIND)
|
|
31
|
-
const SEND_KIND_SET: ReadonlySet<string> = new Set(SEND_KINDS)
|
|
35
|
+
export const SEND_KINDS: readonly SendKind[] = Object.values(SEND_KIND);
|
|
36
|
+
const SEND_KIND_SET: ReadonlySet<string> = new Set(SEND_KINDS);
|
|
32
37
|
|
|
33
38
|
export const isSendKind = (value: unknown): value is SendKind =>
|
|
34
|
-
typeof value === "string" && SEND_KIND_SET.has(value)
|
|
39
|
+
typeof value === "string" && SEND_KIND_SET.has(value);
|
|
35
40
|
|
|
36
|
-
export const CONTEXT_DELIVERY = "next-step" as const
|
|
41
|
+
export const CONTEXT_DELIVERY = "next-step" as const;
|
|
42
|
+
|
|
43
|
+
/** Metadata source for context notes injected by this plugin. Never `subagent`. */
|
|
44
|
+
export const CONTEXT_METADATA_SOURCE = "subagent-tools" as const;
|
|
45
|
+
|
|
46
|
+
export type MessageMetadata = Record<string, string | number | boolean | null>;
|
|
37
47
|
|
|
38
48
|
export interface SendResultFields {
|
|
39
|
-
messageID?: string
|
|
40
|
-
status?:
|
|
41
|
-
|
|
49
|
+
messageID?: string;
|
|
50
|
+
status?: RunStatus;
|
|
51
|
+
active?: boolean;
|
|
52
|
+
backgroundJob?: BackgroundJobInfo;
|
|
53
|
+
queued?: boolean;
|
|
54
|
+
pending?: boolean;
|
|
42
55
|
}
|
|
43
56
|
|
|
44
57
|
export interface SendRequestBase {
|
|
45
|
-
sessionID: string
|
|
46
|
-
text: string
|
|
47
|
-
resume: boolean
|
|
58
|
+
sessionID: string;
|
|
59
|
+
text: string;
|
|
60
|
+
resume: boolean;
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
export type SendRequest =
|
|
51
64
|
| (SendRequestBase & { kind: typeof SEND_KIND.prompt; delivery: Delivery })
|
|
52
65
|
| (SendRequestBase & {
|
|
53
|
-
kind: typeof SEND_KIND.synthetic
|
|
54
|
-
delivery?: never
|
|
55
|
-
description?: string
|
|
56
|
-
|
|
66
|
+
kind: typeof SEND_KIND.synthetic;
|
|
67
|
+
delivery?: never;
|
|
68
|
+
description?: string;
|
|
69
|
+
metadata?: MessageMetadata;
|
|
70
|
+
});
|
|
57
71
|
|
|
58
72
|
export class SendAdmissionError extends Error {
|
|
59
73
|
constructor(message: string, options?: { cause?: unknown }) {
|
|
60
|
-
super(message, options)
|
|
61
|
-
this.name = "SendAdmissionError"
|
|
74
|
+
super(message, options);
|
|
75
|
+
this.name = "SendAdmissionError";
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
78
|
|
|
65
79
|
export interface AdmittedMessage {
|
|
66
|
-
id?: string
|
|
67
|
-
delivery?: Delivery
|
|
68
|
-
text?: string
|
|
80
|
+
id?: string;
|
|
81
|
+
delivery?: Delivery;
|
|
82
|
+
text?: string;
|
|
69
83
|
}
|
|
70
84
|
|
|
71
85
|
export interface SendResult {
|
|
72
|
-
admitted?: AdmittedMessage
|
|
73
|
-
status?:
|
|
74
|
-
|
|
86
|
+
admitted?: AdmittedMessage;
|
|
87
|
+
status?: RunStatus;
|
|
88
|
+
active?: boolean;
|
|
89
|
+
backgroundJob?: BackgroundJobInfo;
|
|
90
|
+
queued?: boolean;
|
|
91
|
+
pending: boolean;
|
|
75
92
|
}
|
|
76
93
|
|
|
77
94
|
const admittedFrom = (value: unknown): AdmittedMessage | undefined => {
|
|
78
|
-
if (!value || typeof value !== "object") return undefined
|
|
95
|
+
if (!value || typeof value !== "object") return undefined;
|
|
79
96
|
const item = value as {
|
|
80
|
-
id?: unknown
|
|
81
|
-
delivery?: unknown
|
|
82
|
-
payload?: { text?: unknown }
|
|
83
|
-
}
|
|
84
|
-
const admitted: AdmittedMessage = {}
|
|
85
|
-
if (typeof item.id === "string" && item.id.length > 0) admitted.id = item.id
|
|
86
|
-
if (isDelivery(item.delivery)) admitted.delivery = item.delivery
|
|
87
|
-
if (typeof item.payload?.text === "string") admitted.text = item.payload.text
|
|
88
|
-
return Object.keys(admitted).length > 0 ? admitted : undefined
|
|
89
|
-
}
|
|
97
|
+
id?: unknown;
|
|
98
|
+
delivery?: unknown;
|
|
99
|
+
payload?: { text?: unknown };
|
|
100
|
+
};
|
|
101
|
+
const admitted: AdmittedMessage = {};
|
|
102
|
+
if (typeof item.id === "string" && item.id.length > 0) admitted.id = item.id;
|
|
103
|
+
if (isDelivery(item.delivery)) admitted.delivery = item.delivery;
|
|
104
|
+
if (typeof item.payload?.text === "string") admitted.text = item.payload.text;
|
|
105
|
+
return Object.keys(admitted).length > 0 ? admitted : undefined;
|
|
106
|
+
};
|
|
90
107
|
|
|
91
108
|
export const applySendResult = <T extends SendResultFields>(
|
|
92
109
|
output: T,
|
|
93
110
|
result: SendResult,
|
|
94
111
|
): T => {
|
|
95
|
-
if (result.admitted?.id) output.messageID = result.admitted.id
|
|
96
|
-
if (result.status) output.status = result.status
|
|
97
|
-
if (result.
|
|
98
|
-
|
|
99
|
-
|
|
112
|
+
if (result.admitted?.id) output.messageID = result.admitted.id;
|
|
113
|
+
if (result.status) output.status = result.status;
|
|
114
|
+
if (result.active !== undefined) output.active = result.active;
|
|
115
|
+
if (result.backgroundJob) output.backgroundJob = result.backgroundJob;
|
|
116
|
+
if (result.queued !== undefined) output.queued = result.queued;
|
|
117
|
+
if (result.pending) output.pending = true;
|
|
118
|
+
return output;
|
|
119
|
+
};
|
|
100
120
|
|
|
101
121
|
export interface SendDeps {
|
|
102
|
-
session: Plugin.Context["session"]
|
|
103
|
-
access: Access
|
|
104
|
-
guard: Guard
|
|
105
|
-
live: LiveStore
|
|
122
|
+
session: Plugin.Context["session"];
|
|
123
|
+
access: Access;
|
|
124
|
+
guard: Guard;
|
|
125
|
+
live: LiveStore;
|
|
126
|
+
background: BackgroundJobStore;
|
|
106
127
|
}
|
|
107
128
|
|
|
108
129
|
type SendHandler = (
|
|
109
130
|
request: SendRequest,
|
|
110
131
|
signal: AbortSignal | undefined,
|
|
111
|
-
) => Promise<unknown
|
|
132
|
+
) => Promise<unknown>;
|
|
112
133
|
|
|
113
134
|
const createSendHandlers = (
|
|
114
135
|
session: Plugin.Context["session"],
|
|
@@ -131,43 +152,71 @@ const createSendHandlers = (
|
|
|
131
152
|
text: request.text,
|
|
132
153
|
description:
|
|
133
154
|
request.kind === SEND_KIND.synthetic ? request.description : undefined,
|
|
155
|
+
metadata: request.kind === SEND_KIND.synthetic ? request.metadata : undefined,
|
|
134
156
|
resume: request.resume,
|
|
135
157
|
},
|
|
136
158
|
{ signal },
|
|
137
159
|
),
|
|
138
|
-
}
|
|
139
|
-
return new Map(Object.entries(handlers) as Array<[SendKind, SendHandler]>)
|
|
140
|
-
}
|
|
160
|
+
};
|
|
161
|
+
return new Map(Object.entries(handlers) as Array<[SendKind, SendHandler]>);
|
|
162
|
+
};
|
|
141
163
|
|
|
142
164
|
/**
|
|
143
165
|
* Shared admission path for `steer`, `queue`, and `context`.
|
|
144
166
|
* `prompt` admits a user message; `synthetic` admits a context-only note.
|
|
145
167
|
*/
|
|
146
|
-
export const createSendMessage = ({
|
|
147
|
-
|
|
168
|
+
export const createSendMessage = ({
|
|
169
|
+
session,
|
|
170
|
+
access,
|
|
171
|
+
guard,
|
|
172
|
+
live,
|
|
173
|
+
background,
|
|
174
|
+
}: SendDeps) => {
|
|
175
|
+
const sendHandlers = createSendHandlers(session);
|
|
148
176
|
|
|
149
177
|
return async (
|
|
150
178
|
request: SendRequest,
|
|
151
179
|
context: ToolCtx,
|
|
152
180
|
failureLabel: string,
|
|
153
181
|
): Promise<SendResult> => {
|
|
154
|
-
const info = await access.authorize(request.sessionID, context)
|
|
182
|
+
const info = await access.authorize(request.sessionID, context);
|
|
155
183
|
if (context?.signal?.aborted) {
|
|
156
|
-
throw new SendAdmissionError("Aborted before the message was accepted.")
|
|
184
|
+
throw new SendAdmissionError("Aborted before the message was accepted.");
|
|
157
185
|
}
|
|
158
|
-
await guard([guardTarget(info, request.sessionID)], context)
|
|
186
|
+
await guard([guardTarget(info, request.sessionID)], context);
|
|
159
187
|
if (context?.signal?.aborted) {
|
|
160
|
-
throw new SendAdmissionError("Aborted before the message was accepted.")
|
|
188
|
+
throw new SendAdmissionError("Aborted before the message was accepted.");
|
|
161
189
|
}
|
|
162
190
|
|
|
163
|
-
const handler = sendHandlers.get(request.kind)
|
|
191
|
+
const handler = sendHandlers.get(request.kind);
|
|
164
192
|
if (!handler) {
|
|
165
|
-
throw new SendAdmissionError(`Unsupported send kind: ${String(request.kind)}`)
|
|
193
|
+
throw new SendAdmissionError(`Unsupported send kind: ${String(request.kind)}`);
|
|
166
194
|
}
|
|
167
195
|
|
|
168
|
-
|
|
196
|
+
// Snapshot the observed target before admission: waking a child can change
|
|
197
|
+
// its status immediately, while the output describes the state that led to
|
|
198
|
+
// the queued/pending decision.
|
|
199
|
+
const status = info
|
|
200
|
+
? live.statusFor(info, request.sessionID)
|
|
201
|
+
: live.liveStatusFor(request.sessionID);
|
|
202
|
+
const backgroundJob = background.backgroundJobFor(request.sessionID);
|
|
203
|
+
const active = isSessionActive(status, backgroundJob);
|
|
204
|
+
// `unknown` is not idle: the session may still be running, so a message
|
|
205
|
+
// without resume cannot be reported as pending.
|
|
206
|
+
const pending = runStatusIsIdle(status) && !request.resume;
|
|
207
|
+
// Queue waits for the current execution, not background jobs. It is queued
|
|
208
|
+
// when the target is not idle, or when an idle target was not woken.
|
|
209
|
+
const queued =
|
|
210
|
+
request.kind === SEND_KIND.prompt && request.delivery === DELIVERY.queue
|
|
211
|
+
? status === undefined ||
|
|
212
|
+
status === RUN_STATUS.running ||
|
|
213
|
+
status === RUN_STATUS.unknown ||
|
|
214
|
+
!request.resume
|
|
215
|
+
: undefined;
|
|
216
|
+
|
|
217
|
+
let admitted: unknown;
|
|
169
218
|
try {
|
|
170
|
-
admitted = await handler(request, context?.signal)
|
|
219
|
+
admitted = await handler(request, context?.signal);
|
|
171
220
|
} catch (error) {
|
|
172
221
|
if (context?.signal?.aborted) {
|
|
173
222
|
throw new SendAdmissionError(
|
|
@@ -175,22 +224,23 @@ export const createSendMessage = ({ session, access, guard, live }: SendDeps) =>
|
|
|
175
224
|
{
|
|
176
225
|
cause: error,
|
|
177
226
|
},
|
|
178
|
-
)
|
|
227
|
+
);
|
|
179
228
|
}
|
|
180
229
|
throw new SendAdmissionError(
|
|
181
230
|
`${failureLabel} failed for ${request.sessionID}: ${errorMessage(error)}`,
|
|
182
231
|
{ cause: error },
|
|
183
|
-
)
|
|
232
|
+
);
|
|
184
233
|
}
|
|
185
234
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
235
|
+
return {
|
|
236
|
+
admitted: admittedFrom(admitted),
|
|
237
|
+
status,
|
|
238
|
+
active,
|
|
239
|
+
...(backgroundJob ? { backgroundJob } : {}),
|
|
240
|
+
...(queued !== undefined ? { queued } : {}),
|
|
241
|
+
pending,
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
export type SendMessage = ReturnType<typeof createSendMessage>;
|
package/src/permissions.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { Plugin } from "@opencode/plugin"
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
2
|
import {
|
|
3
3
|
MAX_AGENT_NAME_LENGTH,
|
|
4
4
|
MAX_PROMPT_PREVIEW_LENGTH,
|
|
5
5
|
type SessionInfoLike,
|
|
6
|
-
} from "./session"
|
|
7
|
-
import { clip } from "./text"
|
|
8
|
-
import type { ToolCtx } from "./tools/input"
|
|
6
|
+
} from "./session";
|
|
7
|
+
import { clip } from "./text";
|
|
8
|
+
import type { ToolCtx } from "./tools/input";
|
|
9
9
|
|
|
10
|
-
export const PERMISSION_ACTION = "subagent.control"
|
|
10
|
+
export const PERMISSION_ACTION = "subagent.control";
|
|
11
11
|
|
|
12
12
|
export interface GuardTarget {
|
|
13
|
-
id: string
|
|
14
|
-
title?: string
|
|
15
|
-
agent?: string
|
|
13
|
+
id: string;
|
|
14
|
+
title?: string;
|
|
15
|
+
agent?: string;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export const guardTarget = (
|
|
@@ -22,21 +22,21 @@ export const guardTarget = (
|
|
|
22
22
|
id: sessionID,
|
|
23
23
|
title: clip(info?.title, MAX_PROMPT_PREVIEW_LENGTH),
|
|
24
24
|
agent: clip(info?.agent, MAX_AGENT_NAME_LENGTH),
|
|
25
|
-
})
|
|
25
|
+
});
|
|
26
26
|
|
|
27
27
|
export interface GuardDeps {
|
|
28
|
-
permission: Plugin.Context["permission"]
|
|
29
|
-
enabled: boolean
|
|
28
|
+
permission: Plugin.Context["permission"];
|
|
29
|
+
enabled: boolean;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export const createGuard = ({ permission, enabled }: GuardDeps) => {
|
|
33
33
|
return async (targets: GuardTarget[], context: ToolCtx) => {
|
|
34
|
-
if (!enabled || targets.length === 0) return
|
|
35
|
-
const sessionID = context.sessionID
|
|
34
|
+
if (!enabled || targets.length === 0) return;
|
|
35
|
+
const sessionID = context.sessionID;
|
|
36
36
|
if (!sessionID) {
|
|
37
37
|
throw new Error(
|
|
38
38
|
"Refusing: the calling session is unknown, so permissions cannot be checked.",
|
|
39
|
-
)
|
|
39
|
+
);
|
|
40
40
|
}
|
|
41
41
|
await permission.assert({
|
|
42
42
|
sessionID,
|
|
@@ -55,8 +55,8 @@ export const createGuard = ({ permission, enabled }: GuardDeps) => {
|
|
|
55
55
|
...(target.agent ? { agent: target.agent } : {}),
|
|
56
56
|
})),
|
|
57
57
|
},
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
61
|
|
|
62
|
-
export type Guard = ReturnType<typeof createGuard
|
|
62
|
+
export type Guard = ReturnType<typeof createGuard>;
|
package/src/session-row.ts
CHANGED
|
@@ -1,40 +1,56 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
isSessionActive,
|
|
3
|
+
type BackgroundJobInfo,
|
|
4
|
+
type BackgroundJobStore,
|
|
5
|
+
} from "./background";
|
|
6
|
+
import type { LiveStore } from "./live-state";
|
|
2
7
|
import {
|
|
3
8
|
MAX_AGENT_NAME_LENGTH,
|
|
4
9
|
MAX_TITLE_LENGTH,
|
|
5
10
|
normalizeSessionID,
|
|
6
11
|
type SessionInfoLike,
|
|
7
|
-
} from "./session"
|
|
8
|
-
import type {
|
|
9
|
-
import { clip, textLength } from "./text"
|
|
10
|
-
import { normalizeTokens, type TokenUsage } from "./tokens"
|
|
12
|
+
} from "./session";
|
|
13
|
+
import type { RunStatus } from "./status";
|
|
14
|
+
import { clip, textLength } from "./text";
|
|
15
|
+
import { normalizeTokens, type TokenUsage } from "./tokens";
|
|
11
16
|
|
|
12
17
|
export interface SessionRow {
|
|
13
|
-
id: string
|
|
14
|
-
parentID?: string
|
|
15
|
-
agent?: string
|
|
16
|
-
title?: string
|
|
17
|
-
titleTruncated?: boolean
|
|
18
|
-
launchedAt: number
|
|
19
|
-
status:
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
id: string;
|
|
19
|
+
parentID?: string;
|
|
20
|
+
agent?: string;
|
|
21
|
+
title?: string;
|
|
22
|
+
titleTruncated?: boolean;
|
|
23
|
+
launchedAt: number;
|
|
24
|
+
status: RunStatus;
|
|
25
|
+
/** The session's own execution or background work. Descendants are excluded. */
|
|
26
|
+
active: boolean;
|
|
27
|
+
/** The session or any discovered descendant is active. */
|
|
28
|
+
subtreeActive: boolean;
|
|
29
|
+
/** True only when at least one discovered child is active in its subtree. */
|
|
30
|
+
activeDescendants?: boolean;
|
|
31
|
+
backgroundJob?: BackgroundJobInfo;
|
|
32
|
+
cost?: number;
|
|
33
|
+
tokens?: TokenUsage;
|
|
22
34
|
}
|
|
23
35
|
|
|
24
36
|
export const rowFromInfo = (
|
|
25
37
|
live: LiveStore,
|
|
38
|
+
background: BackgroundJobStore,
|
|
26
39
|
info: SessionInfoLike,
|
|
27
40
|
fallback?: SessionRow,
|
|
28
41
|
): SessionRow | undefined => {
|
|
29
|
-
const id = normalizeSessionID(info.id) ?? fallback?.id
|
|
30
|
-
if (!id) return undefined
|
|
31
|
-
const created = info.time?.created
|
|
32
|
-
const updated = info.time?.updated
|
|
42
|
+
const id = normalizeSessionID(info.id) ?? fallback?.id;
|
|
43
|
+
if (!id) return undefined;
|
|
44
|
+
const created = info.time?.created;
|
|
45
|
+
const updated = info.time?.updated;
|
|
33
46
|
const launchedAt =
|
|
34
47
|
typeof created === "number" && Number.isFinite(created)
|
|
35
48
|
? created
|
|
36
49
|
: (fallback?.launchedAt ??
|
|
37
|
-
(typeof updated === "number" && Number.isFinite(updated) ? updated : 0))
|
|
50
|
+
(typeof updated === "number" && Number.isFinite(updated) ? updated : 0));
|
|
51
|
+
const status = live.statusFor(info, id);
|
|
52
|
+
const backgroundJob = background.backgroundJobFor(id);
|
|
53
|
+
const active = isSessionActive(status, backgroundJob);
|
|
38
54
|
return {
|
|
39
55
|
id,
|
|
40
56
|
parentID: normalizeSessionID(info.parentID) ?? fallback?.parentID,
|
|
@@ -45,11 +61,56 @@ export const rowFromInfo = (
|
|
|
45
61
|
? textLength(info.title) > MAX_TITLE_LENGTH
|
|
46
62
|
: fallback?.titleTruncated,
|
|
47
63
|
launchedAt,
|
|
48
|
-
status
|
|
64
|
+
status,
|
|
65
|
+
active,
|
|
66
|
+
subtreeActive: active,
|
|
67
|
+
...(backgroundJob ? { backgroundJob } : {}),
|
|
49
68
|
cost:
|
|
50
69
|
typeof info.cost === "number" && Number.isFinite(info.cost)
|
|
51
70
|
? info.cost
|
|
52
71
|
: fallback?.cost,
|
|
53
72
|
tokens: normalizeTokens(info.tokens) ?? fallback?.tokens,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Fill `subtreeActive` and `activeDescendants` after discovery built the rows.
|
|
78
|
+
* Missing parents are treated as subtree roots; a malformed cycle stops at the
|
|
79
|
+
* repeated row instead of recursing forever.
|
|
80
|
+
*/
|
|
81
|
+
export const applySubtreeActivity = (rows: SessionRow[]): void => {
|
|
82
|
+
const children = new Map<string, SessionRow[]>();
|
|
83
|
+
for (const row of rows) {
|
|
84
|
+
if (!row.parentID) continue;
|
|
85
|
+
const siblings = children.get(row.parentID) ?? [];
|
|
86
|
+
siblings.push(row);
|
|
87
|
+
children.set(row.parentID, siblings);
|
|
54
88
|
}
|
|
55
|
-
|
|
89
|
+
|
|
90
|
+
const memo = new Map<string, boolean>();
|
|
91
|
+
const visiting = new Set<string>();
|
|
92
|
+
|
|
93
|
+
const subtreeActive = (row: SessionRow): boolean => {
|
|
94
|
+
const cached = memo.get(row.id);
|
|
95
|
+
if (cached !== undefined) return cached;
|
|
96
|
+
// A malformed cycle is treated as active and stops recursion. This is
|
|
97
|
+
// fail-closed and keeps the result independent of row order.
|
|
98
|
+
if (visiting.has(row.id)) return true;
|
|
99
|
+
visiting.add(row.id);
|
|
100
|
+
const descendants = children.get(row.id) ?? [];
|
|
101
|
+
const value = row.active || descendants.some((child) => subtreeActive(child));
|
|
102
|
+
visiting.delete(row.id);
|
|
103
|
+
memo.set(row.id, value);
|
|
104
|
+
return value;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
for (const row of rows) {
|
|
108
|
+
row.subtreeActive = subtreeActive(row);
|
|
109
|
+
const descendants = children.get(row.id) ?? [];
|
|
110
|
+
if (descendants.some((child) => subtreeActive(child))) {
|
|
111
|
+
row.activeDescendants = true;
|
|
112
|
+
} else {
|
|
113
|
+
delete row.activeDescendants;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|