fix(departments): count ellipsis inside the 600-char recall budget

This commit is contained in:
newbie 2026-11-24 23:14:31 +08:00
parent 22b2497e61
commit 45f1ca9648
5 changed files with 17 additions and 8 deletions

View File

@ -1,6 +1,6 @@
import { renderJson } from "./tools.js";
function snippet(text, limit = 600) {
return text.length <= limit ? text : text.slice(0, limit) + '…';
export function departmentSnippet(text, limit = 600) {
return text.length <= limit ? text : text.slice(0, Math.max(0, limit - 1)) + '…';
}
function inDepartmentWorkspace(deps, exec, job) {
return deps.memory().withWorkspace(deps.workspace(exec), job);
@ -110,7 +110,7 @@ export function createDepartmentTools(deps) {
const requested = revision(args.k) ?? 5;
const k = Math.max(1, Math.min(5, requested));
const hits = await deps.departments().recall(id, query, k, ws);
return { hits: hits.map((hit) => ({ id: hit.record.id, origin: hit.origin, content: snippet(hit.record.content) })) };
return { hits: hits.map((hit) => ({ id: hit.record.id, origin: hit.origin, content: departmentSnippet(hit.record.content) })) };
});
},
};

View File

@ -1,3 +1,4 @@
import { departmentSnippet } from "../memory/departments-tools.js";
import { fail, json, readBody, sameOrigin } from "../http.js";
function errorStatus(message) {
if (message === 'department_not_found')
@ -158,7 +159,7 @@ export async function serveDepartments(deps, req, res) {
const k = Number.isFinite(requested) ? Math.max(1, Math.min(5, requested)) : 5;
const workspace = url.searchParams.get('workspace')?.trim() || undefined;
const hits = await withWorkspace(workspace, () => manager.recall(id, query, k, workspace));
json(res, 200, { ok: true, hits });
json(res, 200, { ok: true, hits: hits.map((hit) => ({ ...hit, content: hit.record.content, contentSnippet: departmentSnippet(hit.record.content) })) });
return;
}
fail(res, 404, 'unknown-department-route');

View File

@ -12,8 +12,8 @@ export interface DepartmentToolDeps {
workspace: (exec?: unknown) => string | undefined
}
function snippet(text: string, limit = 600): string {
return text.length <= limit ? text : text.slice(0, limit) + '…'
export function departmentSnippet(text: string, limit = 600): string {
return text.length <= limit ? text : text.slice(0, Math.max(0, limit - 1)) + '…'
}
function inDepartmentWorkspace<T>(deps: DepartmentToolDeps, exec: unknown, job: () => Promise<T>): Promise<T> {
@ -125,7 +125,7 @@ export function createDepartmentTools(deps: DepartmentToolDeps): ToolDefinitionL
const requested = revision(args.k) ?? 5
const k = Math.max(1, Math.min(5, requested))
const hits = await deps.departments().recall(id, query, k, ws)
return { hits: hits.map((hit: DepartmentRecallHit) => ({ id: hit.record.id, origin: hit.origin, content: snippet(hit.record.content) })) }
return { hits: hits.map((hit: DepartmentRecallHit) => ({ id: hit.record.id, origin: hit.origin, content: departmentSnippet(hit.record.content) })) }
})
},
}

View File

@ -9,6 +9,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
import type { SettingsStore } from '../store.ts'
import type { DepartmentManager } from '../local-memory/departments/index.ts'
import type { LocalMemoryManager } from '../local-memory/manager.ts'
import { departmentSnippet } from '../memory/departments-tools.ts'
import type { DepartmentCreateInput, DepartmentUpdateInput } from '../local-memory/departments/types.ts'
import { fail, json, readBody, sameOrigin } from '../http.ts'
@ -129,7 +130,7 @@ export async function serveDepartments(deps: DepartmentRouteDeps, req: IncomingM
const k = Number.isFinite(requested) ? Math.max(1, Math.min(5, requested)) : 5
const workspace = url.searchParams.get('workspace')?.trim() || undefined
const hits = await withWorkspace(workspace, () => manager.recall(id, query, k, workspace))
json(res, 200, { ok: true, hits })
json(res, 200, { ok: true, hits: hits.map((hit) => ({ ...hit, content: hit.record.content, contentSnippet: departmentSnippet(hit.record.content) })) })
return
}
fail(res, 404, 'unknown-department-route')

View File

@ -71,6 +71,13 @@ check('HTTP create isolates project departments by workspace', httpA.status ===
const listA = await request(server, 'GET', '/dsh-brain/departments/list?scope=project&workspace=' + encodeURIComponent(dualA))
const listB = await request(server, 'GET', '/dsh-brain/departments/list?scope=project&workspace=' + encodeURIComponent(dualB))
check('HTTP list reads the requested workspace', listA.status === 200 && listB.status === 200 && listA.body?.departments?.some((d) => d.id === httpA.body.department.id) && !listA.body?.departments?.some((d) => d.id === httpB.body.department.id) && listB.body?.departments?.some((d) => d.id === httpB.body.department.id) && !listB.body?.departments?.some((d) => d.id === httpA.body.department.id), JSON.stringify({ a: listA.body, b: listB.body }))
const longDept = await createTool.execute({ name: 'SnippetDept', scope: 'project' }, { workspace: project })
const longContent = 'x'.repeat(700)
await rememberTool.execute({ departmentId: longDept.id, content: longContent }, { workspace: project })
const longRecall = await recallTool.execute({ departmentId: longDept.id, query: 'x'.repeat(20) }, { workspace: project })
check('tool recall counts the ellipsis inside the 600-char budget', longRecall.hits[0]?.content.length <= 600 && longRecall.hits[0]?.content.endsWith('…'), JSON.stringify({ length: longRecall.hits[0]?.content.length, tail: longRecall.hits[0]?.content.slice(-3) }))
const longRoute = await request(server, 'GET', '/dsh-brain/departments/recall?id=' + encodeURIComponent(longDept.id) + '&query=' + encodeURIComponent('x'.repeat(20)) + '&workspace=' + encodeURIComponent(project))
check('route recall exposes a bounded contentSnippet', longRoute.status === 200 && longRoute.body?.hits?.[0]?.contentSnippet?.length <= 600 && longRoute.body?.hits?.[0]?.contentSnippet?.endsWith('…') && longRoute.body?.hits?.[0]?.content?.length === 700, JSON.stringify({ snippet: longRoute.body?.hits?.[0]?.contentSnippet?.length, full: longRoute.body?.hits?.[0]?.content?.length }))
const missingName = await request(server, 'POST', '/dsh-brain/departments/create', {}, 'http://127.0.0.1:' + server.address().port)
check('POST create rejects missing name', missingName.status === 400 && missingName.body?.error === 'name-required')
const stale = await request(server, 'POST', '/dsh-brain/departments/update', { id: sameOrigin.body.department.id, expectedRevision: 0, role: 'stale' }, 'http://127.0.0.1:' + server.address().port)