@unieai/rabi-tool-session-query 0.2.1-rc.3 → 0.2.2-rc.5
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/lib/index.js +3 -4
- package/lib/types/workspace-access.d.ts +1 -1
- package/locale/de.json +6 -0
- package/locale/en.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +16 -12
- package/src/index.ts +136 -0
- package/src/input.ts +307 -0
- package/src/operations.ts +280 -0
- package/src/presentation.ts +255 -0
- package/src/service-boundary.ts +179 -0
- package/src/workspace-access.ts +256 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session-query service error containment and model-safe translation.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query/service-boundary
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Context } from '@unieai/cordis'
|
|
8
|
+
import { HarnessError } from '@unieai/rabi-llm'
|
|
9
|
+
import {
|
|
10
|
+
SessionQueryError,
|
|
11
|
+
type SessionQueryErrorCode,
|
|
12
|
+
} from '@unieai/rabi-session-query'
|
|
13
|
+
|
|
14
|
+
interface ModelSafeServiceFailure {
|
|
15
|
+
readonly code: SessionQueryErrorCode | 'SESSION_QUERY_TOOL_FAILED'
|
|
16
|
+
readonly message: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const UNPRINTABLE_SERVICE_ERROR = '[unprintable session query failure]'
|
|
20
|
+
|
|
21
|
+
const SAFE_SESSION_QUERY_FAILURES = {
|
|
22
|
+
SESSION_QUERY_ABORTED: {
|
|
23
|
+
code: 'SESSION_QUERY_ABORTED',
|
|
24
|
+
message: 'session query was cancelled',
|
|
25
|
+
},
|
|
26
|
+
SESSION_QUERY_CORRUPT_SESSION: {
|
|
27
|
+
code: 'SESSION_QUERY_CORRUPT_SESSION',
|
|
28
|
+
message: 'session event history is corrupt',
|
|
29
|
+
},
|
|
30
|
+
SESSION_QUERY_EVENT_NOT_FOUND: {
|
|
31
|
+
code: 'SESSION_QUERY_EVENT_NOT_FOUND',
|
|
32
|
+
message: 'session event was not found',
|
|
33
|
+
},
|
|
34
|
+
SESSION_QUERY_INDEX_FAILED: {
|
|
35
|
+
code: 'SESSION_QUERY_INDEX_FAILED',
|
|
36
|
+
message: 'session search index is unavailable',
|
|
37
|
+
},
|
|
38
|
+
SESSION_QUERY_INVALID_CONFIG: {
|
|
39
|
+
code: 'SESSION_QUERY_TOOL_FAILED',
|
|
40
|
+
message: 'session query operation failed',
|
|
41
|
+
},
|
|
42
|
+
SESSION_QUERY_INVALID_CURSOR: {
|
|
43
|
+
code: 'SESSION_QUERY_INVALID_CURSOR',
|
|
44
|
+
message: 'session search continuation is invalid',
|
|
45
|
+
},
|
|
46
|
+
SESSION_QUERY_INVALID_FILTER: {
|
|
47
|
+
code: 'SESSION_QUERY_INVALID_FILTER',
|
|
48
|
+
message: 'session query filters were rejected',
|
|
49
|
+
},
|
|
50
|
+
SESSION_QUERY_INVALID_LIMIT: {
|
|
51
|
+
code: 'SESSION_QUERY_INVALID_LIMIT',
|
|
52
|
+
message: 'session query result limit was rejected',
|
|
53
|
+
},
|
|
54
|
+
SESSION_QUERY_INVALID_QUERY: {
|
|
55
|
+
code: 'SESSION_QUERY_INVALID_QUERY',
|
|
56
|
+
message: 'session query was rejected',
|
|
57
|
+
},
|
|
58
|
+
SESSION_QUERY_INVALID_LINEAGE: {
|
|
59
|
+
code: 'SESSION_QUERY_INVALID_LINEAGE',
|
|
60
|
+
message: 'session lineage is invalid',
|
|
61
|
+
},
|
|
62
|
+
SESSION_QUERY_INVALID_SURFACE: {
|
|
63
|
+
code: 'SESSION_QUERY_INVALID_SURFACE',
|
|
64
|
+
message: 'session event history is invalid',
|
|
65
|
+
},
|
|
66
|
+
SESSION_QUERY_INVALID_WINDOW: {
|
|
67
|
+
code: 'SESSION_QUERY_INVALID_WINDOW',
|
|
68
|
+
message: 'session event window is invalid',
|
|
69
|
+
},
|
|
70
|
+
SESSION_QUERY_PERSISTENCE_FAILED: {
|
|
71
|
+
code: 'SESSION_QUERY_PERSISTENCE_FAILED',
|
|
72
|
+
message: 'session history storage is unavailable',
|
|
73
|
+
},
|
|
74
|
+
SESSION_QUERY_SEARCH_DISABLED: {
|
|
75
|
+
code: 'SESSION_QUERY_SEARCH_DISABLED',
|
|
76
|
+
message: 'session search is disabled in this deployment',
|
|
77
|
+
},
|
|
78
|
+
SESSION_QUERY_SESSION_NOT_FOUND: {
|
|
79
|
+
code: 'SESSION_QUERY_SESSION_NOT_FOUND',
|
|
80
|
+
message: 'session was not found',
|
|
81
|
+
},
|
|
82
|
+
SESSION_QUERY_STALE_CURSOR: {
|
|
83
|
+
code: 'SESSION_QUERY_STALE_CURSOR',
|
|
84
|
+
message: 'session history changed while paging; retry the complete search call',
|
|
85
|
+
},
|
|
86
|
+
SESSION_QUERY_SOURCE_CONFLICT: {
|
|
87
|
+
code: 'SESSION_QUERY_TOOL_FAILED',
|
|
88
|
+
message: 'session query operation failed',
|
|
89
|
+
},
|
|
90
|
+
} satisfies Record<SessionQueryErrorCode, ModelSafeServiceFailure>
|
|
91
|
+
|
|
92
|
+
function unauthorizedTarget(): HarnessError {
|
|
93
|
+
return new HarnessError(
|
|
94
|
+
'session target is outside the caller workspace',
|
|
95
|
+
'SESSION_QUERY_TOOL_UNAUTHORIZED',
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function call<Value>(
|
|
100
|
+
ctx: Context,
|
|
101
|
+
signal: AbortSignal,
|
|
102
|
+
operation: string,
|
|
103
|
+
invoke: () => Promise<Value>,
|
|
104
|
+
): Promise<Value> {
|
|
105
|
+
signal.throwIfAborted()
|
|
106
|
+
try {
|
|
107
|
+
const value = await invoke()
|
|
108
|
+
signal.throwIfAborted()
|
|
109
|
+
return value
|
|
110
|
+
} catch (error: unknown) {
|
|
111
|
+
signal.throwIfAborted()
|
|
112
|
+
throw sanitizeError(ctx, operation, error)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function sanitizeError(
|
|
117
|
+
ctx: Context,
|
|
118
|
+
operation: string,
|
|
119
|
+
error: unknown,
|
|
120
|
+
): HarnessError {
|
|
121
|
+
const generic = genericFailure()
|
|
122
|
+
const diagnostic = fullError(error)
|
|
123
|
+
try {
|
|
124
|
+
ctx.logger.warn(`tool-session-query: ${operation} failed: ${diagnostic}`)
|
|
125
|
+
if (error instanceof SessionQueryError) {
|
|
126
|
+
const code: unknown = error.code
|
|
127
|
+
const failure = typeof code === 'string' && Object.hasOwn(SAFE_SESSION_QUERY_FAILURES, code)
|
|
128
|
+
? SAFE_SESSION_QUERY_FAILURES[code as SessionQueryErrorCode]
|
|
129
|
+
: undefined
|
|
130
|
+
if (failure !== undefined && failure.code !== 'SESSION_QUERY_TOOL_FAILED') {
|
|
131
|
+
return new SessionQueryError(failure.message, failure.code)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (error instanceof HarnessError && error.code === 'SESSION_QUERY_TOOL_UNAUTHORIZED') {
|
|
135
|
+
return unauthorizedTarget()
|
|
136
|
+
}
|
|
137
|
+
} catch {
|
|
138
|
+
return generic
|
|
139
|
+
}
|
|
140
|
+
return generic
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function genericFailure(): HarnessError {
|
|
144
|
+
return new HarnessError(
|
|
145
|
+
'session query operation failed',
|
|
146
|
+
'SESSION_QUERY_TOOL_FAILED',
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function fullError(error: unknown): string {
|
|
151
|
+
try {
|
|
152
|
+
return renderFullError(error)
|
|
153
|
+
} catch {
|
|
154
|
+
return UNPRINTABLE_SERVICE_ERROR
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function renderFullError(error: unknown): string {
|
|
159
|
+
if (!(error instanceof Error)) return String(error)
|
|
160
|
+
const diagnostics: string[] = []
|
|
161
|
+
const seen = new Set<Error>()
|
|
162
|
+
let current: unknown = error
|
|
163
|
+
while (current instanceof Error && !seen.has(current)) {
|
|
164
|
+
seen.add(current)
|
|
165
|
+
diagnostics.push(current.stack ?? String(current))
|
|
166
|
+
current = current.cause
|
|
167
|
+
}
|
|
168
|
+
/* v8 ignore next -- defensive containment for a cyclic Error.cause graph */
|
|
169
|
+
if (current instanceof Error) diagnostics.push('[circular error cause]')
|
|
170
|
+
else if (current !== undefined) diagnostics.push(renderFullError(current))
|
|
171
|
+
return diagnostics.join('\nCaused by: ')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Model-safe session-query invocation and error translation boundary. */
|
|
175
|
+
export const serviceBoundary = {
|
|
176
|
+
unauthorizedTarget,
|
|
177
|
+
call,
|
|
178
|
+
sanitizeError,
|
|
179
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caller identity, workspace authorization, and visible lineage projection.
|
|
3
|
+
*
|
|
4
|
+
* @module @unieai/rabi-tool-session-query/workspace-access
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Context } from '@unieai/cordis'
|
|
8
|
+
import { brandString } from '@unieai/rabi-brand'
|
|
9
|
+
import { HarnessError } from '@unieai/rabi-llm'
|
|
10
|
+
import {
|
|
11
|
+
type SessionHeader,
|
|
12
|
+
type SessionId as SessionIdValue,
|
|
13
|
+
} from '@unieai/rabi-session'
|
|
14
|
+
import type { TurnBoundaryProjection } from '@unieai/rabi-agent'
|
|
15
|
+
import type {
|
|
16
|
+
SessionLineageNode,
|
|
17
|
+
SessionRecord,
|
|
18
|
+
} from '@unieai/rabi-session-query'
|
|
19
|
+
import type { ToolRunContext } from '@unieai/rabi-tools'
|
|
20
|
+
import { serviceBoundary } from './service-boundary.ts'
|
|
21
|
+
|
|
22
|
+
interface Caller {
|
|
23
|
+
readonly id: SessionIdValue
|
|
24
|
+
readonly header: SessionHeader
|
|
25
|
+
/** The caller's own-session boundary fold (the `turnBoundary` projection). */
|
|
26
|
+
readonly boundary: TurnBoundaryProjection
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface TitleView {
|
|
30
|
+
readonly text: string
|
|
31
|
+
readonly unavailableCode?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface CompleteTitleMap extends ReadonlyMap<SessionIdValue, TitleView> {
|
|
35
|
+
get(id: SessionIdValue): TitleView
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface AuthorizedDescendant {
|
|
39
|
+
readonly record: SessionRecord
|
|
40
|
+
readonly descendants: Array<AuthorizedDescendant | null>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface DescendantProjectionFrame {
|
|
44
|
+
readonly node: SessionLineageNode
|
|
45
|
+
readonly target: Array<AuthorizedDescendant | null>
|
|
46
|
+
readonly next: DescendantProjectionFrame | undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface DescendantVisit {
|
|
50
|
+
readonly node: AuthorizedDescendant | null
|
|
51
|
+
readonly depth: number
|
|
52
|
+
readonly next: DescendantVisit | undefined
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function callerOf(exec: ToolRunContext, ctx: Context): Caller {
|
|
56
|
+
const agent = exec.agent
|
|
57
|
+
if (agent === undefined) {
|
|
58
|
+
throw new HarnessError(
|
|
59
|
+
'session query tools require an agent-bound caller',
|
|
60
|
+
'SESSION_QUERY_TOOL_MISSING_AGENT',
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
id: agent.session.id,
|
|
65
|
+
header: agent.session.header,
|
|
66
|
+
boundary: ctx.agents.turnBoundaryOf(agent.session),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function targetId(args: { readonly session_id?: string }, caller: Caller): SessionIdValue {
|
|
71
|
+
return args.session_id === undefined ? caller.id : brandString<SessionIdValue>(args.session_id)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function authorizeTarget(
|
|
75
|
+
ctx: Context,
|
|
76
|
+
caller: Caller,
|
|
77
|
+
target: SessionIdValue,
|
|
78
|
+
signal: AbortSignal,
|
|
79
|
+
): Promise<void> {
|
|
80
|
+
if (target === caller.id) return
|
|
81
|
+
const cwd = caller.header.cwd
|
|
82
|
+
if (cwd === undefined) throw serviceBoundary.unauthorizedTarget()
|
|
83
|
+
const records = await serviceBoundary.call(ctx, signal, 'target authorization', () =>
|
|
84
|
+
ctx.sessionQuery.filterSessions([
|
|
85
|
+
{ kind: 'id', values: [target] },
|
|
86
|
+
{ kind: 'cwd', values: [cwd] },
|
|
87
|
+
], signal))
|
|
88
|
+
if (records.length !== 1) throw serviceBoundary.unauthorizedTarget()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function recordAuthorized(record: SessionRecord, caller: Caller): boolean {
|
|
92
|
+
return headerAuthorized(record.header, caller)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function headerAuthorized(header: SessionHeader, caller: Caller): boolean {
|
|
96
|
+
if (header.id === caller.id) return header.cwd === caller.header.cwd
|
|
97
|
+
return caller.header.cwd !== undefined && header.cwd === caller.header.cwd
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function assertObservedTargetAuthorized(
|
|
101
|
+
caller: Caller,
|
|
102
|
+
target: SessionIdValue,
|
|
103
|
+
observed: SessionHeader,
|
|
104
|
+
): void {
|
|
105
|
+
if (observed.id !== target || !headerAuthorized(observed, caller)) {
|
|
106
|
+
throw serviceBoundary.unauthorizedTarget()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function authorizeSessionIds(
|
|
111
|
+
ctx: Context,
|
|
112
|
+
caller: Caller,
|
|
113
|
+
ids: readonly SessionIdValue[],
|
|
114
|
+
signal: AbortSignal,
|
|
115
|
+
): Promise<ReadonlySet<SessionIdValue>> {
|
|
116
|
+
const unique = [...new Set(ids)]
|
|
117
|
+
const authorized = new Set<SessionIdValue>()
|
|
118
|
+
if (unique.includes(caller.id)) authorized.add(caller.id)
|
|
119
|
+
const cwd = caller.header.cwd
|
|
120
|
+
const other = unique.filter(id => id !== caller.id)
|
|
121
|
+
if (cwd === undefined || other.length === 0) return authorized
|
|
122
|
+
const records = await serviceBoundary.call(ctx, signal, 'session-id authorization', () =>
|
|
123
|
+
ctx.sessionQuery.filterSessions([
|
|
124
|
+
{ kind: 'id', values: other },
|
|
125
|
+
{ kind: 'cwd', values: [cwd] },
|
|
126
|
+
], signal))
|
|
127
|
+
const requested = new Set(other)
|
|
128
|
+
for (const record of records) {
|
|
129
|
+
if (requested.has(record.header.id) && recordAuthorized(record, caller)) {
|
|
130
|
+
authorized.add(record.header.id)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return authorized
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function readTitles(
|
|
137
|
+
ctx: Context,
|
|
138
|
+
caller: Caller,
|
|
139
|
+
ids: readonly SessionIdValue[],
|
|
140
|
+
signal: AbortSignal,
|
|
141
|
+
): Promise<CompleteTitleMap> {
|
|
142
|
+
const result = new Map<SessionIdValue, TitleView>()
|
|
143
|
+
const observations = await serviceBoundary.call(ctx, signal, 'title observation', () =>
|
|
144
|
+
ctx.sessionQuery.readTitleSnapshots(ids, signal))
|
|
145
|
+
for (const observation of observations) {
|
|
146
|
+
if (observation.status === 'rejected') {
|
|
147
|
+
result.set(observation.sessionId, unavailableTitle(ctx, observation.reason))
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
assertObservedTargetAuthorized(caller, observation.sessionId, observation.value.session)
|
|
151
|
+
result.set(observation.sessionId, { text: observation.value.title?.title ?? 'untitled' })
|
|
152
|
+
}
|
|
153
|
+
return result as CompleteTitleMap
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function readTitle(
|
|
157
|
+
ctx: Context,
|
|
158
|
+
caller: Caller,
|
|
159
|
+
id: SessionIdValue,
|
|
160
|
+
signal: AbortSignal,
|
|
161
|
+
): Promise<TitleView> {
|
|
162
|
+
return (await readTitles(ctx, caller, [id], signal)).get(id)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function unavailableTitle(
|
|
166
|
+
ctx: Context,
|
|
167
|
+
error: unknown,
|
|
168
|
+
): TitleView {
|
|
169
|
+
const sanitized = serviceBoundary.sanitizeError(ctx, 'title observation item', error)
|
|
170
|
+
if (sanitized.code === 'SESSION_QUERY_TOOL_UNAUTHORIZED') throw sanitized
|
|
171
|
+
return { text: 'untitled', unavailableCode: sanitized.code }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function authorizeDescendants(
|
|
175
|
+
nodes: readonly SessionLineageNode[],
|
|
176
|
+
caller: Caller,
|
|
177
|
+
): Array<AuthorizedDescendant | null> {
|
|
178
|
+
const result: Array<AuthorizedDescendant | null> = []
|
|
179
|
+
let pending: DescendantProjectionFrame | undefined
|
|
180
|
+
for (const node of [...nodes].reverse()) {
|
|
181
|
+
pending = { node, target: result, next: pending }
|
|
182
|
+
}
|
|
183
|
+
while (pending !== undefined) {
|
|
184
|
+
const current = pending
|
|
185
|
+
pending = current.next
|
|
186
|
+
if (!recordAuthorized(current.node.session, caller)) {
|
|
187
|
+
current.target.push(null)
|
|
188
|
+
continue
|
|
189
|
+
}
|
|
190
|
+
const projected: AuthorizedDescendant = {
|
|
191
|
+
record: current.node.session,
|
|
192
|
+
descendants: [],
|
|
193
|
+
}
|
|
194
|
+
current.target.push(projected)
|
|
195
|
+
for (const child of [...current.node.descendants].reverse()) {
|
|
196
|
+
pending = {
|
|
197
|
+
node: child,
|
|
198
|
+
target: projected.descendants,
|
|
199
|
+
next: pending,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return result
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function * visitDescendants(
|
|
207
|
+
nodes: readonly (AuthorizedDescendant | null)[],
|
|
208
|
+
): Generator<DescendantVisit> {
|
|
209
|
+
let pending: DescendantVisit | undefined
|
|
210
|
+
for (const node of [...nodes].reverse()) {
|
|
211
|
+
pending = { node, depth: 0, next: pending }
|
|
212
|
+
}
|
|
213
|
+
while (pending !== undefined) {
|
|
214
|
+
const current = pending
|
|
215
|
+
pending = current.next
|
|
216
|
+
yield current
|
|
217
|
+
if (current.node === null) continue
|
|
218
|
+
for (const child of [...current.node.descendants].reverse()) {
|
|
219
|
+
pending = {
|
|
220
|
+
node: child,
|
|
221
|
+
depth: current.depth + 1,
|
|
222
|
+
next: pending,
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function descendantIds(nodes: readonly (AuthorizedDescendant | null)[]): SessionIdValue[] {
|
|
229
|
+
const ids: SessionIdValue[] = []
|
|
230
|
+
for (const { node } of visitDescendants(nodes)) {
|
|
231
|
+
if (node !== null) ids.push(node.record.header.id)
|
|
232
|
+
}
|
|
233
|
+
return ids
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function titleText(view: TitleView): string {
|
|
237
|
+
return view.unavailableCode === undefined
|
|
238
|
+
? view.text
|
|
239
|
+
: `${view.text} (title unavailable: ${view.unavailableCode})`
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Workspace-scoped caller authorization, title access, and lineage projection. */
|
|
243
|
+
export const workspaceAccess = {
|
|
244
|
+
callerOf,
|
|
245
|
+
targetId,
|
|
246
|
+
authorizeTarget,
|
|
247
|
+
recordAuthorized,
|
|
248
|
+
assertObservedTargetAuthorized,
|
|
249
|
+
authorizeSessionIds,
|
|
250
|
+
readTitles,
|
|
251
|
+
readTitle,
|
|
252
|
+
authorizeDescendants,
|
|
253
|
+
visitDescendants,
|
|
254
|
+
descendantIds,
|
|
255
|
+
titleText,
|
|
256
|
+
}
|