mirror of
https://github.com/garrytan/gbrain.git
synced 2026-08-17 02:12:40 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88287775e5 |
@@ -457,7 +457,6 @@ unresolvable+true|false, pre-v80 NULL/NULL rows survive).
|
||||
- `src/core/think/prompt.ts` extension — anti-bias rewrite. `withCalibration` option on `buildThinkSystemPrompt` adds anti-bias rules. `buildCalibrationBlock()` emits the `<calibration>` XML. `buildThinkUserMessage` has TWO shapes: default (question first), and with-calibration (retrieval → calibration → question) when opt-in. Wired into `runThink` via `opts.withCalibration` + `opts.calibrationHolder`.
|
||||
- `src/commands/calibration.ts` — CLI: `gbrain calibration` (read + print), `--regenerate`, `--undo-wave <ver>`, `ab-report`. MCP op `get_calibration_profile` (scope: read) backs the same data path. Source-scoped via `sourceScopeOpts(ctx)`.
|
||||
- `src/commands/serve-http.ts` extension — three admin routes: `/admin/api/calibration/profile`, `/admin/api/calibration/charts/:type` (image/svg+xml; type in {brier-trend, domain-bars, pattern-statements, abandoned-threads}), `/admin/api/calibration/pattern/:id` (drill-down).
|
||||
- `src/core/owner-holder.ts` — single source of truth for "the brain owner" holder string. `DEFAULT_OWNER_HOLDER = 'self'` (matches the consolidate facts→takes writer + `docs/takes-vs-facts.md`); `resolveOwnerHolder({override, configValue})` returns override > `emotional_weight.user_holder` config > `'self'`. Consumed by the calibration_profile cycle phase, `gbrain calibration` CLI, the `get_calibration_profile` op, `think`'s calibration block, `emotional-weight`'s `DEFAULT_USER_HOLDER`, and doctor's `calibration_freshness`. Pure; unit-tested in `test/owner-holder.test.ts`. Does NOT unify owner-identity fragmentation (`self`/`brain`/`people-<owner>`) — tracked separately.
|
||||
- `src/commands/takes.ts` extension — `gbrain takes revisit <slug>` opens $EDITOR on the source page with a `<!-- gbrain:revisit -->` cursor marker.
|
||||
- `src/commands/doctor.ts` extension — 4 checks: `abandoned_threads`, `calibration_freshness`, `grade_confidence_drift` (mitigation surface; math ships later), `voice_gate_health`.
|
||||
- `admin/src/pages/Calibration.tsx` — Calibration tab. Single-column layout. `<TrustedSVG>` wrapper handles `dangerouslySetInnerHTML` for the server-rendered SVG.
|
||||
|
||||
@@ -91,18 +91,3 @@ First full takes extraction run on a ~100K-page brain:
|
||||
4. **Self-reported ≠ verified.** "Reports 7 figures" → holder=person, weight=0.75, NOT world/1.0
|
||||
5. **No false precision.** Use 0.05 increments (0.35, 0.55, 0.75), not 0.74 or 0.82
|
||||
6. **"So what" test.** Skip Twitter handles, follower counts, obvious metadata
|
||||
|
||||
## Owner-holder canonicalization
|
||||
|
||||
"The brain owner" is, by convention, the holder string **`self`** — the value the
|
||||
dream `consolidate` phase stamps when it promotes the owner's hot facts into cold
|
||||
takes. Calibration, `think`, and the `doctor` calibration check resolve the owner
|
||||
holder through `resolveOwnerHolder` (`src/core/owner-holder.ts`): explicit override
|
||||
> `emotional_weight.user_holder` config > `self`.
|
||||
|
||||
Known limitation (tracked in garrytan/gbrain#2465): the owner can also
|
||||
appear under `brain` (a take the owner asserts, via `propose_takes`) and
|
||||
`people/<owner>` (extraction that names the owner). The resolver selects the
|
||||
*default* canonical owner string for reads; it does not merge those other
|
||||
strings. Per-take attribution for other people (e.g. `people/george`) is
|
||||
unaffected and correct.
|
||||
|
||||
@@ -23,7 +23,6 @@ import { runPhaseCalibrationProfile } from '../core/cycle/calibration-profile.ts
|
||||
import { sourceScopeOpts, type OperationContext } from '../core/operations.ts';
|
||||
import type { GBrainConfig } from '../core/config.ts';
|
||||
import { GBrainError } from '../core/types.ts';
|
||||
import { resolveOwnerHolder } from '../core/owner-holder.ts';
|
||||
|
||||
export interface CalibrationProfileRow {
|
||||
/** BIGSERIAL → string (postgres.js int8 wire shape; never Number() — int8
|
||||
@@ -168,10 +167,7 @@ export async function runCalibration(
|
||||
config: GBrainConfig,
|
||||
): Promise<void> {
|
||||
const { opts } = parseArgs(args);
|
||||
const holder = resolveOwnerHolder({
|
||||
override: opts.holder,
|
||||
configValue: await engine.getConfig('emotional_weight.user_holder'),
|
||||
});
|
||||
const holder = opts.holder ?? 'garry';
|
||||
// Resolve --source / GBRAIN_SOURCE / .gbrain-source so the (now reachable, #2035)
|
||||
// calibration command targets the right source in a multi-source brain instead
|
||||
// of always reading `default`. No signal → 'default' (prior behavior).
|
||||
@@ -257,15 +253,12 @@ export async function getCalibrationProfileOp(
|
||||
ctx: OperationContext,
|
||||
params: { holder?: string },
|
||||
): Promise<CalibrationProfileRow | null> {
|
||||
const holder = resolveOwnerHolder({
|
||||
override: params.holder,
|
||||
configValue: await ctx.engine.getConfig('emotional_weight.user_holder'),
|
||||
});
|
||||
const holder = params.holder ?? 'garry';
|
||||
if (typeof holder !== 'string' || holder.length === 0) {
|
||||
throw new GBrainError(
|
||||
'INVALID_HOLDER',
|
||||
'get_calibration_profile.holder must be a non-empty string',
|
||||
'pass holder="<slug>" or omit to default to the owner holder (config emotional_weight.user_holder, else "self")',
|
||||
'pass holder="<slug>" or omit to default to "garry"',
|
||||
);
|
||||
}
|
||||
const scope = sourceScopeOpts(ctx);
|
||||
|
||||
@@ -28,7 +28,6 @@ import type { DbUrlSource } from '../core/config.ts';
|
||||
import { gbrainPath, loadConfig } from '../core/config.ts';
|
||||
import { reflexEnabled } from '../core/context/reflex.ts';
|
||||
import { resolveSocketPath } from '../core/context/resolve-ipc.ts';
|
||||
import { resolveOwnerHolder } from '../core/owner-holder.ts';
|
||||
import { homedir } from 'os';
|
||||
import { dirname, isAbsolute, join, resolve as resolvePath } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
@@ -1288,19 +1287,14 @@ export async function checkAbandonedThreads(engine: BrainEngine): Promise<Check>
|
||||
|
||||
/**
|
||||
* calibration_freshness: warns when the active calibration profile is
|
||||
* older than 7 days (configurable). Default holder resolves via resolveOwnerHolder
|
||||
* (config emotional_weight.user_holder, else 'self'). Multi-source
|
||||
* older than 7 days (configurable). Default holder 'garry'. Multi-source
|
||||
* brains see one row per source; this check uses the most recent across
|
||||
* all sources.
|
||||
*/
|
||||
export async function checkCalibrationFreshness(engine: BrainEngine): Promise<Check> {
|
||||
try {
|
||||
const ownerHolder = resolveOwnerHolder({
|
||||
configValue: await engine.getConfig('emotional_weight.user_holder'),
|
||||
});
|
||||
const rows = await engine.executeRaw<{ generated_at: Date | null }>(
|
||||
`SELECT MAX(generated_at) AS generated_at FROM calibration_profiles WHERE holder = $1`,
|
||||
[ownerHolder],
|
||||
`SELECT MAX(generated_at) AS generated_at FROM calibration_profiles WHERE holder = 'garry'`,
|
||||
);
|
||||
const generated = rows[0]?.generated_at;
|
||||
if (!generated) {
|
||||
|
||||
@@ -45,7 +45,6 @@ import {
|
||||
type IngestionContentType,
|
||||
type IngestionEvent,
|
||||
} from '../core/ingestion/types.ts';
|
||||
import { resolveOwnerHolder } from '../core/owner-holder.ts';
|
||||
|
||||
/**
|
||||
* /health endpoint timeout. 3s rather than 5s: Fly.io's default
|
||||
@@ -1191,7 +1190,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption
|
||||
app.get('/admin/api/calibration/pattern/:id', requireAdmin, async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { getLatestProfile } = await import('./calibration.ts');
|
||||
const holder = resolveOwnerHolder({ override: (req.query.holder as string) || undefined, configValue: await engine.getConfig('emotional_weight.user_holder') });
|
||||
const holder = (req.query.holder as string) || 'garry';
|
||||
const profile = await getLatestProfile(engine, { holder });
|
||||
if (!profile) {
|
||||
res.status(404).json({ error: 'no_profile' });
|
||||
@@ -1241,7 +1240,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption
|
||||
app.get('/admin/api/calibration/profile', requireAdmin, async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { getLatestProfile } = await import('./calibration.ts');
|
||||
const holder = resolveOwnerHolder({ override: (req.query.holder as string) || undefined, configValue: await engine.getConfig('emotional_weight.user_holder') });
|
||||
const holder = (req.query.holder as string) || 'garry';
|
||||
const profile = await getLatestProfile(engine, { holder });
|
||||
res.json(profile);
|
||||
} catch (err) {
|
||||
@@ -1258,7 +1257,7 @@ export async function runServeHttp(engine: BrainEngine, options: ServeHttpOption
|
||||
renderAbandonedThreadsCard,
|
||||
renderPatternStatementsCard,
|
||||
} = await import('../core/calibration/svg-renderer.ts');
|
||||
const holder = resolveOwnerHolder({ override: (req.query.holder as string) || undefined, configValue: await engine.getConfig('emotional_weight.user_holder') });
|
||||
const holder = (req.query.holder as string) || 'garry';
|
||||
const type = req.params.type;
|
||||
const profile = await getLatestProfile(engine, { holder });
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ import {
|
||||
} from '../core/takes-fence.ts';
|
||||
import { withPageLock } from '../core/page-lock.ts';
|
||||
import { resolveSourceId } from '../core/source-resolver.ts';
|
||||
import { resolveOwnerHolder } from '../core/owner-holder.ts';
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
@@ -365,7 +364,7 @@ async function cmdResolve(engine: BrainEngine, args: string[], sourceId?: string
|
||||
// --evidence is the v0.30.0 alias for --source on the resolve subcommand
|
||||
// (semantic clarity: "what evidence resolved this bet?").
|
||||
const source = flagValue(args, '--evidence') ?? flagValue(args, '--source');
|
||||
const resolvedBy = flagValue(args, '--by') ?? resolveOwnerHolder({ configValue: await engine.getConfig('emotional_weight.user_holder') });
|
||||
const resolvedBy = flagValue(args, '--by') ?? 'garry';
|
||||
const dirArg = flagValue(args, '--dir');
|
||||
|
||||
const pageId = await getPageId(engine, slug, sourceId);
|
||||
|
||||
@@ -68,7 +68,6 @@ import {
|
||||
type BrainstormCheckpoint,
|
||||
type CheckpointCross,
|
||||
} from './checkpoint.ts';
|
||||
import { resolveOwnerHolder } from '../owner-holder.ts';
|
||||
|
||||
export { BudgetExhausted };
|
||||
|
||||
@@ -140,7 +139,7 @@ export interface BrainstormOptions {
|
||||
modelOverride?: string;
|
||||
/** Skip the cost-preview TTY grace window. Required for non-interactive callers. */
|
||||
skipCostPreview?: boolean;
|
||||
/** When set, force the user holder for calibration profile lookup. Falls back to config (`emotional_weight.user_holder`) then `'self'`. */
|
||||
/** When set, force the user holder for calibration profile lookup. Falls back to config (`emotional_weight.user_holder`) then `'garry'`. */
|
||||
holderOverride?: string;
|
||||
/** Source scope. */
|
||||
sourceId?: string;
|
||||
@@ -624,7 +623,7 @@ async function _runBrainstormInner(
|
||||
}
|
||||
|
||||
// ---- Phase 3: calibration context (cold-start fallback) ----
|
||||
const holder = resolveOwnerHolder({ override: opts.holderOverride, configValue: config.emotional_weight?.user_holder });
|
||||
const holder = opts.holderOverride ?? config.emotional_weight?.user_holder ?? 'garry';
|
||||
const calibContext = await loadCalibrationContext(engine, {
|
||||
holder,
|
||||
sourceId: opts.sourceId,
|
||||
|
||||
@@ -25,7 +25,7 @@ import type { BrainEngine } from '../engine.ts';
|
||||
|
||||
export interface ABRunInput {
|
||||
question: string;
|
||||
/** Holder context for calibration. Resolves via resolveOwnerHolder (config emotional_weight.user_holder, else 'self'). */
|
||||
/** Holder context for calibration. Default 'garry'. */
|
||||
holder?: string;
|
||||
/** Engine for DB write. */
|
||||
engine: BrainEngine;
|
||||
|
||||
@@ -18,8 +18,9 @@
|
||||
* at runtime.
|
||||
*/
|
||||
|
||||
import { chunkText as recursiveChunk } from './recursive.ts';
|
||||
import { chunkText as recursiveChunk, capByEstimatedTokens, DEFAULT_MAX_EST_TOKENS } from './recursive.ts';
|
||||
import { buildQualifiedName } from './qualified-names.ts';
|
||||
import { estimateEmbeddingTokens } from '../cjk.ts';
|
||||
|
||||
// Embed the tree-sitter runtime + per-language grammars as files.
|
||||
// `with { type: 'file' }` returns a path (string) at runtime. Bun bundles
|
||||
@@ -111,7 +112,15 @@ import G_ZIG from '../../assets/wasm/grammars/tree-sitter-zig.wasm' with { type:
|
||||
// chunks get the new columns populated. Without this, the v28 backfill
|
||||
// gives every existing chunk a search_vector but subsequent Layer 5 AST
|
||||
// work would silently no-op.
|
||||
export const CHUNKER_VERSION = 4;
|
||||
//
|
||||
// v5: estimated-token hard cap on AST-path chunks (capCodeChunks). A node
|
||||
// splitLargeNode can't subdivide (giant single-statement function, huge
|
||||
// literal) previously shipped WHOLE regardless of size and could overflow
|
||||
// strict per-request embedding-token limits (local llama-server crashes
|
||||
// past ~2,050 tokens, measured). Mirrors the markdown
|
||||
// chunker's v4 cap; fallback-path chunks are already capped inside
|
||||
// recursiveChunk.
|
||||
export const CHUNKER_VERSION = 5;
|
||||
|
||||
// Lazy-loaded tree-sitter module (v0.22.x API: Parser is default export)
|
||||
let Parser: typeof import('web-tree-sitter') | null = null;
|
||||
@@ -708,7 +717,7 @@ export async function chunkCodeTextFull(
|
||||
if (chunks.length === 0) {
|
||||
return { chunks: fallbackChunks(source, filePath, language, opts), edges: rawEdges };
|
||||
}
|
||||
return { chunks: mergeSmallSiblings(chunks, chunkTarget), edges: rawEdges };
|
||||
return { chunks: capCodeChunks(mergeSmallSiblings(chunks, chunkTarget)), edges: rawEdges };
|
||||
} catch {
|
||||
return { chunks: fallbackChunks(source, filePath, language, opts), edges: [] };
|
||||
} finally {
|
||||
@@ -791,6 +800,33 @@ function mergeSmallSiblings(chunks: CodeChunk[], chunkTarget: number): CodeChunk
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* v5 final safety pass for AST-path chunks: split any chunk whose
|
||||
* ESTIMATED embedding tokens (conservative per-char-class heuristic,
|
||||
* cjk.ts) exceed DEFAULT_MAX_EST_TOKENS. Reaches chunks the AST logic
|
||||
* can't subdivide — splitLargeNode returns [] for nodes with < 2 body
|
||||
* children (giant single-statement functions, huge literals), which
|
||||
* previously shipped whole at any size.
|
||||
*
|
||||
* Split pieces inherit the source chunk's metadata verbatim; start/end
|
||||
* lines become approximate for pieces after the first. Acceptable —
|
||||
* these chunks exist for embedding + retrieval, and the alternative was
|
||||
* an embedding request the server rejects (or worse, crashes on).
|
||||
*/
|
||||
function capCodeChunks(chunks: CodeChunk[]): CodeChunk[] {
|
||||
if (chunks.every((c) => estimateEmbeddingTokens(c.text) <= DEFAULT_MAX_EST_TOKENS)) {
|
||||
return chunks;
|
||||
}
|
||||
const out: CodeChunk[] = [];
|
||||
for (const c of chunks) {
|
||||
const pieces = capByEstimatedTokens(c.text, DEFAULT_MAX_EST_TOKENS);
|
||||
for (const piece of pieces) {
|
||||
out.push({ ...c, text: piece, index: out.length, metadata: { ...c.metadata } });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function buildMergedChunk(group: CodeChunk[], index: number): CodeChunk {
|
||||
const first = group[0]!;
|
||||
const last = group[group.length - 1]!;
|
||||
|
||||
@@ -17,7 +17,13 @@
|
||||
* Lossless invariant: non-overlapping portions reassemble to original.
|
||||
*/
|
||||
|
||||
import { countCJKAwareWords, CJK_SENTENCE_DELIMITERS, CJK_CLAUSE_DELIMITERS } from '../cjk.ts';
|
||||
import {
|
||||
countCJKAwareWords,
|
||||
CJK_SENTENCE_DELIMITERS,
|
||||
CJK_CLAUSE_DELIMITERS,
|
||||
charEmbedTokenWeight,
|
||||
estimateEmbeddingTokens,
|
||||
} from '../cjk.ts';
|
||||
|
||||
/**
|
||||
* Markdown chunker version. Folded into the per-page chunker_version column
|
||||
@@ -33,8 +39,20 @@ import { countCJKAwareWords, CJK_SENTENCE_DELIMITERS, CJK_CLAUSE_DELIMITERS } fr
|
||||
* re-embed (not re-chunk) so existing pages pick up the wrapper on the
|
||||
* post-upgrade reembed sweep. See
|
||||
* `src/core/contextual-retrieval-service.ts`.
|
||||
*
|
||||
* v4: estimated-token hard cap + whitespace-word undercount fix. The word
|
||||
* pipeline counted a 150-char URL as ONE whitespace word, so URL/phone/
|
||||
* email-dense docs (CJK density < 0.30 → whitespace fallback) produced
|
||||
* 3-4K-char chunks that overflow strict per-request embedding-token
|
||||
* limits (measured: local llama-server crashes past ~2,050 tokens; URL
|
||||
* soup tokenizes at ~1.6 chars/token). Two changes:
|
||||
* 1. countWords() floors the count at ceil(nonWhitespaceChars/6) so a
|
||||
* URL counts roughly per-character, not as one word.
|
||||
* 2. capByEstimatedTokens() final pass guarantees every chunk fits
|
||||
* `maxTokens` (default 1500) under a conservative per-char-class
|
||||
* token estimate, regardless of how word counting misjudged it.
|
||||
*/
|
||||
export const MARKDOWN_CHUNKER_VERSION = 3;
|
||||
export const MARKDOWN_CHUNKER_VERSION = 4;
|
||||
|
||||
const DELIMITERS: string[][] = [
|
||||
['\n\n'], // L0: paragraphs
|
||||
@@ -48,8 +66,20 @@ export interface ChunkOptions {
|
||||
chunkSize?: number; // target words per chunk (default 300)
|
||||
chunkOverlap?: number; // overlap words (default 50)
|
||||
maxChars?: number; // hard cap on any chunk's char length (default 6000)
|
||||
/**
|
||||
* v4: hard cap on any chunk's ESTIMATED embedding tokens (default 1500).
|
||||
* Estimate = conservative per-char-class weights (see cjk.ts
|
||||
* estimateEmbeddingTokens) — deliberately high, so the real tokenizer
|
||||
* count stays below this value. Default leaves headroom for the
|
||||
* contextual-retrieval wrapper (≤ ~630 chars) under a ~2,050-token
|
||||
* per-request embedding server limit.
|
||||
*/
|
||||
maxTokens?: number;
|
||||
}
|
||||
|
||||
/** v4 default for ChunkOptions.maxTokens — see the field doc above. */
|
||||
export const DEFAULT_MAX_EST_TOKENS = 1500;
|
||||
|
||||
export interface TextChunk {
|
||||
text: string;
|
||||
index: number;
|
||||
@@ -73,6 +103,7 @@ export function chunkText(text: string, opts?: ChunkOptions): TextChunk[] {
|
||||
const chunkSize = opts?.chunkSize || 300;
|
||||
const chunkOverlap = opts?.chunkOverlap || 50;
|
||||
const maxChars = opts?.maxChars || 6000;
|
||||
const maxTokens = opts?.maxTokens || DEFAULT_MAX_EST_TOKENS;
|
||||
|
||||
if (!text || text.trim().length === 0) return [];
|
||||
|
||||
@@ -89,8 +120,9 @@ export function chunkText(text: string, opts?: ChunkOptions): TextChunk[] {
|
||||
|
||||
const wordCount = countWords(stripped);
|
||||
if (wordCount <= chunkSize) {
|
||||
// Single-chunk path: still apply the maxChars cap.
|
||||
const capped = capByChars(stripped.trim(), maxChars);
|
||||
// Single-chunk path: still apply the maxChars + maxTokens caps.
|
||||
const capped = capByChars(stripped.trim(), maxChars)
|
||||
.flatMap((t) => capByEstimatedTokens(t, maxTokens));
|
||||
return capped.map((t, i) => ({ text: t, index: i }));
|
||||
}
|
||||
|
||||
@@ -101,9 +133,14 @@ export function chunkText(text: string, opts?: ChunkOptions): TextChunk[] {
|
||||
// v0.32.7: hard char cap. Catches pathological CJK + whitespace-less text
|
||||
// that the word-level pipeline can't bound (a single Chinese paragraph can
|
||||
// exceed 8192 OpenAI embedding tokens at any word count).
|
||||
// v4: estimated-token cap on top — the char cap alone passes token-dense
|
||||
// content (URL soup at ~1.6 chars/token) that overflows strict embedding
|
||||
// server limits.
|
||||
const capped: string[] = [];
|
||||
for (const chunk of withOverlap) {
|
||||
capped.push(...capByChars(chunk.trim(), maxChars));
|
||||
for (const piece of capByChars(chunk.trim(), maxChars)) {
|
||||
capped.push(...capByEstimatedTokens(piece, maxTokens));
|
||||
}
|
||||
}
|
||||
return capped.map((t, i) => ({ text: t, index: i }));
|
||||
}
|
||||
@@ -132,6 +169,68 @@ function capByChars(text: string, maxChars: number): string[] {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* How far back (in chars) the token cap looks for a friendly cut point
|
||||
* before falling back to a hard cut. 300 covers typical rollup/list line
|
||||
* lengths so forced splits land at line starts, not mid-URL.
|
||||
*/
|
||||
const TOKEN_CAP_CUT_LOOKBACK = 300;
|
||||
|
||||
/**
|
||||
* v4: hard-cap a chunk's ESTIMATED embedding tokens. Final safety pass —
|
||||
* runs after capByChars on every chunk, so no upstream miscounting
|
||||
* (whitespace-word fallback, overlap inflation, char-cap survivors) can
|
||||
* emit a chunk past `maxTokens`.
|
||||
*
|
||||
* Cut placement prefers, within the last TOKEN_CAP_CUT_LOOKBACK chars of
|
||||
* the window: a newline, then any whitespace, then a hard cut. This keeps
|
||||
* forced splits off mid-line/mid-URL positions for list-shaped content
|
||||
* and inside code fences. No overlap is added (pieces stay lossless
|
||||
* modulo the trims the char cap already applies).
|
||||
*
|
||||
* @internal exported for the code chunker (code.ts) and tests.
|
||||
*/
|
||||
export function capByEstimatedTokens(text: string, maxTokens: number): string[] {
|
||||
if (text.length === 0) return [];
|
||||
if (estimateEmbeddingTokens(text) <= maxTokens) return [text];
|
||||
|
||||
const out: string[] = [];
|
||||
let start = 0;
|
||||
while (start < text.length) {
|
||||
// Greedily extend the window until the next char would break the cap.
|
||||
// Always take at least one char so the loop makes forward progress.
|
||||
let est = 0;
|
||||
let end = start;
|
||||
while (end < text.length) {
|
||||
const w = charEmbedTokenWeight(text.charCodeAt(end));
|
||||
if (est + w > maxTokens && end > start) break;
|
||||
est += w;
|
||||
end++;
|
||||
}
|
||||
|
||||
if (end < text.length) {
|
||||
const windowStart = Math.max(start + 1, end - TOKEN_CAP_CUT_LOOKBACK);
|
||||
let cut = text.lastIndexOf('\n', end - 1);
|
||||
if (cut < windowStart) {
|
||||
cut = -1;
|
||||
for (let i = end - 1; i >= windowStart; i--) {
|
||||
const code = text.charCodeAt(i);
|
||||
if (code === 0x20 || (code >= 0x09 && code <= 0x0d)) {
|
||||
cut = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cut >= windowStart) end = cut + 1;
|
||||
}
|
||||
|
||||
const slice = text.slice(start, end).trim();
|
||||
if (slice.length > 0) out.push(slice);
|
||||
start = end;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function recursiveSplit(text: string, level: number, target: number): string[] {
|
||||
if (level >= DELIMITERS.length) {
|
||||
// Level 4: split on whitespace
|
||||
@@ -317,7 +416,19 @@ function extractTrailingContext(text: string, targetWords: number): string {
|
||||
* Delegated to src/core/cjk.ts so the slugify whitelist, expansion
|
||||
* detection, and PGLite keyword fallback all agree on what "CJK enough"
|
||||
* means.
|
||||
*
|
||||
* v4: floored at ceil(nonWhitespaceChars/6). The whitespace fallback
|
||||
* counts a 150-char URL as ONE word, so URL/phone/email-dense docs
|
||||
* (whose ASCII mass pushes CJK density below the 0.30 threshold) were
|
||||
* sized at a fraction of their real bulk and merged into 3-4K-char
|
||||
* chunks. The floor makes long whitespace-less runs count roughly
|
||||
* per-character while leaving normal Latin prose untouched (average
|
||||
* English word ≈ 5 chars < 6, so the whitespace count still wins).
|
||||
* Kept local to the chunker — search/expansion.ts keeps the original
|
||||
* countCJKAwareWords semantics for its query-length check.
|
||||
*/
|
||||
function countWords(text: string): number {
|
||||
return countCJKAwareWords(text);
|
||||
const cjkAware = countCJKAwareWords(text);
|
||||
const nonWhitespace = text.replace(/\s/g, '').length;
|
||||
return Math.max(cjkAware, Math.ceil(nonWhitespace / 6));
|
||||
}
|
||||
|
||||
@@ -65,3 +65,64 @@ export function countCJKAwareWords(s: string): number {
|
||||
export function escapeLikePattern(s: string): string {
|
||||
return s.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Conservative per-char-class embedding-token weights (markdown chunker v4).
|
||||
*
|
||||
* Why this exists: the chunker's "word" counting drastically UNDER-counts
|
||||
* whitespace-less ASCII runs (a 150-char URL = 1 whitespace word), so
|
||||
* word-based size targets can emit chunks that overflow an embedding
|
||||
* server's per-request token limit. Measured on a local Qwen3-embedding
|
||||
* llama-server stack:
|
||||
* - URL/phone/email-dense text tokenizes at ~1.6 chars/token
|
||||
* - base64-ish / minified blobs approach ~1.3 chars/token (worst case)
|
||||
* - Korean prose tokenizes NO WORSE than 1 char/token in practice
|
||||
*
|
||||
* Weights are deliberately HIGH (tokens are overestimated) so any cap
|
||||
* based on this estimate is safe against real tokenizers:
|
||||
* - CJK char → 1.0 token (real CJK prose is cheaper)
|
||||
* - other non-space → 0.75 token (≈1.33 chars/token, covers base64)
|
||||
* - whitespace → 0.1 token (mostly folds into neighbor tokens)
|
||||
*/
|
||||
export const EMBED_TOKEN_WEIGHT_CJK = 1.0;
|
||||
export const EMBED_TOKEN_WEIGHT_OTHER = 0.75;
|
||||
export const EMBED_TOKEN_WEIGHT_WS = 0.1;
|
||||
|
||||
/** BMP CJK check by UTF-16 code unit — same ranges as CJK_SLUG_CHARS. */
|
||||
export function isCJKCodeUnit(code: number): boolean {
|
||||
return (
|
||||
(code >= 0x4e00 && code <= 0x9fff) || // Han
|
||||
(code >= 0x3040 && code <= 0x309f) || // Hiragana
|
||||
(code >= 0x30a0 && code <= 0x30ff) || // Katakana
|
||||
(code >= 0xac00 && code <= 0xd7af) // Hangul Syllables
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-code-unit token weight. Unrecognized whitespace (exotic Unicode
|
||||
* spaces) intentionally falls into OTHER — that only overestimates.
|
||||
*/
|
||||
export function charEmbedTokenWeight(code: number): number {
|
||||
if (isCJKCodeUnit(code)) return EMBED_TOKEN_WEIGHT_CJK;
|
||||
if (
|
||||
code === 0x20 || (code >= 0x09 && code <= 0x0d) ||
|
||||
code === 0xa0 || code === 0x3000
|
||||
) {
|
||||
return EMBED_TOKEN_WEIGHT_WS;
|
||||
}
|
||||
return EMBED_TOKEN_WEIGHT_OTHER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenizer-free embedding-token estimate (conservative overestimate).
|
||||
* See weight docs above. Astral chars count as 2 OTHER code units —
|
||||
* another overestimate, which is the safe direction.
|
||||
*/
|
||||
export function estimateEmbeddingTokens(s: string): number {
|
||||
if (s.length === 0) return 0;
|
||||
let est = 0;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
est += charEmbedTokenWeight(s.charCodeAt(i));
|
||||
}
|
||||
return Math.ceil(est);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
*/
|
||||
|
||||
import { BaseCyclePhase, type ScopedReadOpts, type BasePhaseOpts } from './base-phase.ts';
|
||||
import { resolveOwnerHolder } from '../owner-holder.ts';
|
||||
import { chat as gatewayChat } from '../ai/gateway.ts';
|
||||
import { TIER_DEFAULTS } from '../model-config.ts';
|
||||
import { gateVoice, type VoiceGateGenerator, type VoiceGateJudge } from '../calibration/voice-gate.ts';
|
||||
@@ -97,7 +96,7 @@ export type PatternStatementsGenerator = (input: {
|
||||
export type BiasTagsGenerator = (patterns: string[]) => Promise<string[]>;
|
||||
|
||||
export interface CalibrationProfileOpts extends BasePhaseOpts {
|
||||
/** Holder to generate the profile for. Default resolves via resolveOwnerHolder (config emotional_weight.user_holder, else 'self'). */
|
||||
/** Holder to generate the profile for. Default 'garry'. */
|
||||
holder?: string;
|
||||
/** Inject the patterns generator (tests). */
|
||||
patternsGenerator?: PatternStatementsGenerator;
|
||||
@@ -228,10 +227,7 @@ class CalibrationProfilePhase extends BaseCyclePhase {
|
||||
_ctx: OperationContext,
|
||||
opts: CalibrationProfileOpts,
|
||||
): Promise<{ summary: string; details: Record<string, unknown>; status?: PhaseStatus }> {
|
||||
const holder = resolveOwnerHolder({
|
||||
override: opts.holder,
|
||||
configValue: await engine.getConfig('emotional_weight.user_holder'),
|
||||
});
|
||||
const holder = opts.holder ?? 'garry';
|
||||
const promptVersion = opts.promptVersion ?? CALIBRATION_PROFILE_PROMPT_VERSION;
|
||||
const modelId = opts.model ?? TIER_DEFAULTS.reasoning;
|
||||
const gradeCompletion = opts.gradeCompletion ?? 1.0;
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
* See `loadHighEmotionTags` for the resolution path.
|
||||
*/
|
||||
|
||||
import { DEFAULT_OWNER_HOLDER } from '../owner-holder.ts';
|
||||
|
||||
/**
|
||||
* Default high-emotion tag seed list. Pages with any tag in this set get the
|
||||
* tag-emotion boost in the formula below. Override via config key
|
||||
@@ -45,12 +43,11 @@ export const HIGH_EMOTION_TAGS: ReadonlySet<string> = new Set([
|
||||
]);
|
||||
|
||||
/**
|
||||
* Holder name treated as "the user" for the user-as-holder ratio. Configurable
|
||||
* via the `emotional_weight.user_holder` config key; defaults to the canonical
|
||||
* owner holder ('self', DEFAULT_OWNER_HOLDER) so it matches the consolidate
|
||||
* facts→takes writer instead of a hardcoded name.
|
||||
* Holder name treated as "the user" for the Garry-as-holder ratio. Configurable
|
||||
* via the `emotional_weight.user_holder` config key (defaults to 'garry' to
|
||||
* match the v0.28 schema's takes table convention).
|
||||
*/
|
||||
export const DEFAULT_USER_HOLDER = DEFAULT_OWNER_HOLDER;
|
||||
export const DEFAULT_USER_HOLDER = 'garry';
|
||||
|
||||
export interface EmotionalWeightTake {
|
||||
holder: string;
|
||||
|
||||
@@ -3289,7 +3289,7 @@ const get_calibration_profile: Operation = {
|
||||
holder: {
|
||||
type: 'string',
|
||||
description:
|
||||
"Holder slug, e.g. 'self' or 'people/charlie-example'. Defaults to config emotional_weight.user_holder, else 'self', when omitted.",
|
||||
"Holder slug, e.g. 'garry' or 'people/charlie-example'. Defaults to 'garry' when omitted.",
|
||||
},
|
||||
},
|
||||
handler: async (ctx, p) => {
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/**
|
||||
* Canonical holder string for "the brain owner," resolved in ONE place so the
|
||||
* calibration / think / doctor / emotional-weight defaults stop disagreeing.
|
||||
*
|
||||
* The default matches the consolidate facts→takes writer
|
||||
* (src/core/cycle/phases/consolidate.ts: holder:'self') and docs/takes-vs-facts.md.
|
||||
* Do NOT introduce a fourth literal — three already exist historically
|
||||
* ('garry', 'system', 'self'); this is the source of truth.
|
||||
*
|
||||
* NORMALIZATION NOTE: the brain owner may also appear under other holder
|
||||
* strings — 'brain' (propose_takes when the author asserts a claim) and
|
||||
* people/<owner> (extraction that names the owner). This resolver only selects
|
||||
* the *default* canonical owner string for reads; it does NOT merge those other
|
||||
* strings. Unifying them is owner-identity entity-resolution, tracked separately
|
||||
* (see garrytan/gbrain#2465). Until then, historical owner takes
|
||||
* under 'brain'/people-<owner> are not folded into the default profile.
|
||||
*/
|
||||
export const DEFAULT_OWNER_HOLDER = 'self';
|
||||
|
||||
export function resolveOwnerHolder(
|
||||
opts: { override?: string | null; configValue?: string | null },
|
||||
): string {
|
||||
return opts.override ?? opts.configValue ?? DEFAULT_OWNER_HOLDER;
|
||||
}
|
||||
@@ -23,7 +23,6 @@ import { runGather, renderPagesBlock, takesHitToTakeForPrompt } from './gather.t
|
||||
import { renderTakesBlock } from './sanitize.ts';
|
||||
import { buildThinkSystemPrompt, buildThinkUserMessage } from './prompt.ts';
|
||||
import { resolveCitations, type ParsedCitation } from './cite-render.ts';
|
||||
import { resolveOwnerHolder } from '../owner-holder.ts';
|
||||
import { resolveModel } from '../model-config.ts';
|
||||
import { chat as gatewayChat, probeChatModel, type ChatResult } from '../ai/gateway.ts';
|
||||
import { AIConfigError } from '../ai/errors.ts';
|
||||
@@ -77,8 +76,8 @@ export interface RunThinkOpts {
|
||||
*/
|
||||
withCalibration?: boolean;
|
||||
/**
|
||||
* Holder to retrieve the calibration profile for. Resolves via resolveOwnerHolder
|
||||
* (config emotional_weight.user_holder, else 'self'). Only consulted when withCalibration=true.
|
||||
* Holder to retrieve the calibration profile for. Default 'garry'. Only
|
||||
* consulted when withCalibration=true.
|
||||
*/
|
||||
calibrationHolder?: string;
|
||||
/**
|
||||
@@ -309,10 +308,7 @@ export async function runThink(
|
||||
try {
|
||||
const { getLatestProfile } = await import('../../commands/calibration.ts');
|
||||
const profile = await getLatestProfile(engine, {
|
||||
holder: resolveOwnerHolder({
|
||||
override: opts.calibrationHolder,
|
||||
configValue: await engine.getConfig('emotional_weight.user_holder'),
|
||||
}),
|
||||
holder: opts.calibrationHolder ?? 'garry',
|
||||
});
|
||||
if (profile) {
|
||||
calibrationBlockOpts = {
|
||||
|
||||
@@ -27,12 +27,6 @@ function buildMockEngine(opts: { rows: CalibrationProfileRow[] }): {
|
||||
const capturedParams: unknown[][] = [];
|
||||
const engine = {
|
||||
kind: 'pglite',
|
||||
// #2464: getCalibrationProfileOp resolves the owner holder via
|
||||
// resolveOwnerHolder(config emotional_weight.user_holder, else 'self'), so the
|
||||
// mock must implement getConfig. null = key unset → resolver falls back to 'self'.
|
||||
async getConfig(): Promise<string | null> {
|
||||
return null;
|
||||
},
|
||||
async executeRaw<T>(sql: string, params?: unknown[]): Promise<T[]> {
|
||||
capturedSql.push(sql);
|
||||
capturedParams.push(params ?? []);
|
||||
@@ -217,17 +211,17 @@ describe('formatProfileText', () => {
|
||||
// ─── getCalibrationProfileOp ────────────────────────────────────────
|
||||
|
||||
describe('getCalibrationProfileOp (MCP)', () => {
|
||||
test('defaults holder to "self" when omitted (config emotional_weight.user_holder unset)', async () => {
|
||||
const { engine } = buildMockEngine({ rows: [buildProfile({ holder: 'self' })] });
|
||||
test('defaults holder to "garry" when omitted', async () => {
|
||||
const { engine } = buildMockEngine({ rows: [buildProfile({ holder: 'garry' })] });
|
||||
const ctx = buildCtx(engine);
|
||||
const result = await getCalibrationProfileOp(ctx, {});
|
||||
expect(result?.holder).toBe('self');
|
||||
expect(result?.holder).toBe('garry');
|
||||
});
|
||||
|
||||
test('routes through sourceScopeOpts: scalar source-bound client gets source-scoped result', async () => {
|
||||
const rows = [
|
||||
buildProfile({ holder: 'self', source_id: 'default' }),
|
||||
buildProfile({ holder: 'self', source_id: 'tenant-b' }),
|
||||
buildProfile({ holder: 'garry', source_id: 'default' }),
|
||||
buildProfile({ holder: 'garry', source_id: 'tenant-b' }),
|
||||
];
|
||||
const { engine } = buildMockEngine({ rows });
|
||||
const ctx = buildCtx(engine, { sourceId: 'tenant-b' });
|
||||
@@ -237,8 +231,8 @@ describe('getCalibrationProfileOp (MCP)', () => {
|
||||
|
||||
test('federated read scope sees the union of allowed sources', async () => {
|
||||
const rows = [
|
||||
buildProfile({ holder: 'self', source_id: 'tenant-a' }),
|
||||
buildProfile({ holder: 'self', source_id: 'tenant-z' }),
|
||||
buildProfile({ holder: 'garry', source_id: 'tenant-a' }),
|
||||
buildProfile({ holder: 'garry', source_id: 'tenant-z' }),
|
||||
];
|
||||
const { engine } = buildMockEngine({ rows });
|
||||
const ctx = buildCtx(engine, { allowedSources: ['tenant-a', 'tenant-b'] });
|
||||
|
||||
@@ -32,7 +32,7 @@ interface CapturedSql {
|
||||
params: unknown[];
|
||||
}
|
||||
|
||||
function buildMockEngine(opts: { scorecard: TakesScorecard; userHolder?: string | null }): {
|
||||
function buildMockEngine(opts: { scorecard: TakesScorecard }): {
|
||||
engine: BrainEngine;
|
||||
captured: CapturedSql[];
|
||||
} {
|
||||
@@ -42,10 +42,6 @@ function buildMockEngine(opts: { scorecard: TakesScorecard; userHolder?: string
|
||||
async getScorecard() {
|
||||
return opts.scorecard;
|
||||
},
|
||||
async getConfig(key: string): Promise<string | null> {
|
||||
if (key === 'emotional_weight.user_holder') return opts.userHolder ?? null;
|
||||
return null;
|
||||
},
|
||||
async executeRaw<T>(sql: string, params?: unknown[]): Promise<T[]> {
|
||||
captured.push({ sql, params: params ?? [] });
|
||||
return [];
|
||||
@@ -238,7 +234,7 @@ describe('runPhaseCalibrationProfile — phase integration', () => {
|
||||
// grade_completion, domain_scorecards_json, patterns[], voice_passed, voice_attempts,
|
||||
// bias_tags[], model_id
|
||||
expect(insert!.params[0]).toBe('default'); // source_id
|
||||
expect(insert!.params[1]).toBe('self'); // holder (resolved via resolveOwnerHolder, no override)
|
||||
expect(insert!.params[1]).toBe('garry'); // holder
|
||||
expect(insert!.params[2]).toBe(12); // total_resolved
|
||||
expect(insert!.params[9]).toBe(true); // voice_gate_passed
|
||||
expect(insert!.params[10]).toBe(1); // voice_gate_attempts
|
||||
@@ -334,24 +330,4 @@ describe('runPhaseCalibrationProfile — phase integration', () => {
|
||||
const insert = captured.find(c => c.sql.includes('INSERT INTO calibration_profiles'));
|
||||
expect(insert!.params[0]).toBe('tenant-b');
|
||||
});
|
||||
|
||||
test('cold-brain summary uses resolved owner holder self when user_holder unset', async () => {
|
||||
const { engine } = buildMockEngine({
|
||||
scorecard: { total_bets: 0, resolved: 0, correct: 0, incorrect: 0, partial: 0,
|
||||
accuracy: null, brier: null, partial_rate: null, unresolvable_count: 0, unresolvable_rate: null },
|
||||
});
|
||||
const result = await runPhaseCalibrationProfile(buildCtx(engine), {});
|
||||
expect(result.summary).toContain('holder=self');
|
||||
expect(result.summary).not.toContain('holder=garry');
|
||||
});
|
||||
|
||||
test('configured user_holder overrides the default in the cold-brain summary', async () => {
|
||||
const { engine } = buildMockEngine({
|
||||
scorecard: { total_bets: 0, resolved: 0, correct: 0, incorrect: 0, partial: 0,
|
||||
accuracy: null, brier: null, partial_rate: null, unresolvable_count: 0, unresolvable_rate: null },
|
||||
userHolder: 'people/charlie-example',
|
||||
});
|
||||
const result = await runPhaseCalibrationProfile(buildCtx(engine), {});
|
||||
expect(result.summary).toContain('holder=people/charlie-example');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,19 +15,22 @@ import { describe, test, expect } from 'bun:test';
|
||||
import { CHUNKER_VERSION } from '../src/core/chunkers/code.ts';
|
||||
|
||||
describe('Layer 12 — CHUNKER_VERSION constant', () => {
|
||||
test('bumped to 4 for Cathedral II', () => {
|
||||
test('bumped to 5 for the estimated-token hard cap', () => {
|
||||
// v3: v0.19.0 Chonkie parity (tokenizer + small-sibling merge).
|
||||
// v4: v0.20.0 Cathedral II (qualified names + parent scope + doc_comment
|
||||
// + fence extraction + chunk-grain FTS). Folded into content_hash
|
||||
// so any bump forces clean re-chunks on next sync.
|
||||
expect(CHUNKER_VERSION).toBe(4);
|
||||
// v5: estimated-token hard cap on AST-path chunks (capCodeChunks) so
|
||||
// un-subdividable giant nodes can't overflow strict embedding
|
||||
// server token limits.
|
||||
expect(CHUNKER_VERSION).toBe(5);
|
||||
});
|
||||
|
||||
test('is stable across imports (not recomputed at call time)', async () => {
|
||||
const a = (await import('../src/core/chunkers/code.ts')).CHUNKER_VERSION;
|
||||
const b = (await import('../src/core/chunkers/code.ts')).CHUNKER_VERSION;
|
||||
expect(a).toBe(b);
|
||||
expect(a).toBe(4);
|
||||
expect(a).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import { describe, test, expect } from 'bun:test';
|
||||
import { chunkCodeText, detectCodeLanguage, CHUNKER_VERSION } from '../../src/core/chunkers/code.ts';
|
||||
|
||||
describe('CHUNKER_VERSION', () => {
|
||||
test('v0.20.0 Cathedral II Layer 12 bumped to 4', () => {
|
||||
expect(CHUNKER_VERSION).toBe(4);
|
||||
test('v5: estimated-token hard cap on AST-path chunks', () => {
|
||||
expect(CHUNKER_VERSION).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -135,13 +135,14 @@ describe('Recursive Text Chunker', () => {
|
||||
});
|
||||
|
||||
describe('CJK chunking (v0.32.7)', () => {
|
||||
test('MARKDOWN_CHUNKER_VERSION is 3', async () => {
|
||||
test('MARKDOWN_CHUNKER_VERSION is 4', async () => {
|
||||
// v0.40.3.0: bumped 2→3 to signal the post-upgrade reembed sweep that
|
||||
// contextual retrieval wrapping is now applied at embed time. Chunk
|
||||
// boundaries themselves are unchanged; the bump forces re-embed for
|
||||
// pages where chunker_version < 3.
|
||||
// contextual retrieval wrapping is now applied at embed time.
|
||||
// v4: estimated-token hard cap + whitespace-word undercount floor
|
||||
// (URL-dense docs produced chunks past strict embedding server token
|
||||
// limits). Boundary change → forces re-chunk for chunker_version < 4.
|
||||
const mod = await import('../../src/core/chunkers/recursive.ts');
|
||||
expect(mod.MARKDOWN_CHUNKER_VERSION).toBe(3);
|
||||
expect(mod.MARKDOWN_CHUNKER_VERSION).toBe(4);
|
||||
});
|
||||
|
||||
test('long pure-Chinese paragraph splits into multiple chunks', () => {
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Markdown chunker v4 / code chunker v5 — estimated-token hard cap
|
||||
* regression tests.
|
||||
*
|
||||
* Reproduces a field failure: a local llama-server embedding backend
|
||||
* (`-ub 2048`) crashes deterministically (trace/BPT trap → EOF at the
|
||||
* client) when a single chunk exceeds ~2,050 real tokens. Two content
|
||||
* shapes triggered it:
|
||||
*
|
||||
* 1. Korean docs carrying one long source URL per line.
|
||||
* The URLs' ASCII mass pushes CJK density below 0.30, flipping
|
||||
* countCJKAwareWords to whitespace counting, where a 150-char URL
|
||||
* counts as ONE word → chunks ballooned to 3-4K chars ≈ 2,000+
|
||||
* real tokens (URL soup tokenizes at ~1.6 chars/token).
|
||||
*
|
||||
* 2. Large JSON code blocks (~7K chars) that the word pipeline
|
||||
* undercounts the same way (few whitespace tokens).
|
||||
*
|
||||
* The fix: every emitted chunk must satisfy
|
||||
* estimateEmbeddingTokens(chunk) <= maxTokens (default 1500)
|
||||
* where the estimate deliberately OVERSTATES real tokenizer counts.
|
||||
*/
|
||||
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { chunkText, capByEstimatedTokens, DEFAULT_MAX_EST_TOKENS } from '../../src/core/chunkers/recursive.ts';
|
||||
import { chunkCodeText } from '../../src/core/chunkers/code.ts';
|
||||
import { estimateEmbeddingTokens } from '../../src/core/cjk.ts';
|
||||
|
||||
/** Synthesize the failing shape: Korean rollup lines each ending in a long Notion URL. */
|
||||
function urlDenseKoreanRollup(lines: number): string {
|
||||
const out: string[] = ['# 링크가 줄마다 붙는 한국어 예시 문서', ''];
|
||||
for (let i = 0; i < lines; i++) {
|
||||
const hex32 = (i * 2654435761 >>> 0).toString(16).padStart(8, '0').repeat(4);
|
||||
out.push(
|
||||
`- **항목 ${i}**: 이 줄은 청커 동작 검증을 위한 의미 없는 한국어 예시 문장입니다 · 전화 000-0000-${String(1000 + i)} · ` +
|
||||
`이메일 user${i}@example.com · 링크: https://docs.example.com/pages/${hex32}?v=abcdef0123456789&ref=sample`,
|
||||
);
|
||||
}
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
/** Synthesize a large pretty-printed JSON block with CJK values. */
|
||||
function bigJsonBlock(targetChars: number): string {
|
||||
const entries: string[] = [];
|
||||
let i = 0;
|
||||
let len = 0;
|
||||
while (len < targetChars) {
|
||||
const row =
|
||||
` "item_${i}": { "name": "예시-${i}", "url": "https://example.com/api/v2/items/${i}?token=abc${i}def", "qty": ${i % 100}, "memo": "한국어 값이 섞인 예시 데이터" }`;
|
||||
entries.push(row);
|
||||
len += row.length;
|
||||
i++;
|
||||
}
|
||||
return `{\n${entries.join(',\n')}\n}`;
|
||||
}
|
||||
|
||||
describe('v4 estimated-token cap — URL-dense Korean doc (field-failure shape)', () => {
|
||||
test('every chunk stays under the estimated-token cap', () => {
|
||||
const md = urlDenseKoreanRollup(60);
|
||||
const chunks = chunkText(md);
|
||||
expect(chunks.length).toBeGreaterThan(0);
|
||||
for (const c of chunks) {
|
||||
expect(estimateEmbeddingTokens(c.text)).toBeLessThanOrEqual(DEFAULT_MAX_EST_TOKENS);
|
||||
}
|
||||
});
|
||||
|
||||
test('no chunk reaches the measured 3K-char danger zone for URL soup', () => {
|
||||
const md = urlDenseKoreanRollup(60);
|
||||
const chunks = chunkText(md);
|
||||
// 1500 est tokens at the OTHER weight (0.75/char) bounds chunks to
|
||||
// ~2,000 chars for pure ASCII — well under the ~3,300 chars where
|
||||
// URL-dense content crosses ~2,050 real tokens (1.6 chars/token).
|
||||
for (const c of chunks) {
|
||||
expect(c.text.length).toBeLessThanOrEqual(2600);
|
||||
}
|
||||
});
|
||||
|
||||
test('content is preserved (no lines dropped by the cap)', () => {
|
||||
const md = urlDenseKoreanRollup(60);
|
||||
const chunks = chunkText(md);
|
||||
const joined = chunks.map((c) => c.text).join('\n');
|
||||
// Spot-check first / middle / last rollup lines survive chunking.
|
||||
for (const marker of ['항목 0', '항목 30', '항목 59']) {
|
||||
expect(joined).toContain(marker);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('v4 estimated-token cap — large JSON blocks', () => {
|
||||
test('7K-char pretty JSON through the prose path stays under the cap', () => {
|
||||
const md = `설정 파일 원문 보존:\n\n\`\`\`\n${bigJsonBlock(7000)}\n\`\`\`\n`;
|
||||
const chunks = chunkText(md);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
for (const c of chunks) {
|
||||
expect(estimateEmbeddingTokens(c.text)).toBeLessThanOrEqual(DEFAULT_MAX_EST_TOKENS);
|
||||
}
|
||||
});
|
||||
|
||||
test('7K-char minified JSON (single whitespace-less token) stays under the cap', () => {
|
||||
const minified = bigJsonBlock(7000).replace(/\n\s*/g, '');
|
||||
const chunks = chunkText(minified);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
for (const c of chunks) {
|
||||
expect(estimateEmbeddingTokens(c.text)).toBeLessThanOrEqual(DEFAULT_MAX_EST_TOKENS);
|
||||
}
|
||||
});
|
||||
|
||||
test('json fence via the code chunker stays under the cap (+header slack)', async () => {
|
||||
const chunks = await chunkCodeText(bigJsonBlock(7000), 'fence.json');
|
||||
expect(chunks.length).toBeGreaterThan(0);
|
||||
for (const c of chunks) {
|
||||
// buildChunk prepends a short "[JSON] fence.json:…" header AFTER the
|
||||
// body-level cap; allow ~60 est tokens of header slack. Real-token
|
||||
// safety margin (2,050 − overestimated 1,500) absorbs this easily.
|
||||
expect(estimateEmbeddingTokens(c.text)).toBeLessThanOrEqual(DEFAULT_MAX_EST_TOKENS + 60);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('v4 word-count floor — behavior preserved for normal content', () => {
|
||||
test('Latin prose chunking is unchanged by the floor (avg word < 6 chars)', () => {
|
||||
const prose = Array.from({ length: 120 }, (_, i) =>
|
||||
`This is sentence number ${i} and it talks about ordinary things in plain words.`,
|
||||
).join(' ');
|
||||
const chunks = chunkText(prose);
|
||||
// Historical behavior: ~1,560 whitespace words → multiple ~300-word chunks.
|
||||
expect(chunks.length).toBeGreaterThan(3);
|
||||
for (const c of chunks) {
|
||||
const words = c.text.split(/\s+/).length;
|
||||
expect(words).toBeLessThanOrEqual(300 * 1.5 + 50); // merge cap + overlap
|
||||
}
|
||||
});
|
||||
|
||||
test('Korean prose (CJK-dense, no URLs) never triggers the token cap', () => {
|
||||
const prose = Array.from({ length: 80 }, (_, i) =>
|
||||
`이 문장은 순수 한국어 산문의 청킹 동작을 확인하기 위한 ${i}번째 예시 문장입니다.`,
|
||||
).join(' ');
|
||||
const chunks = chunkText(prose);
|
||||
expect(chunks.length).toBeGreaterThan(1);
|
||||
for (const c of chunks) {
|
||||
// CJK-dense chunks are char-counted (≈450 max) — nowhere near 1500.
|
||||
expect(estimateEmbeddingTokens(c.text)).toBeLessThanOrEqual(700);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('capByEstimatedTokens unit behavior', () => {
|
||||
test('returns input unchanged when under the cap', () => {
|
||||
expect(capByEstimatedTokens('short text', 1500)).toEqual(['short text']);
|
||||
expect(capByEstimatedTokens('', 1500)).toEqual([]);
|
||||
});
|
||||
|
||||
test('prefers newline cut points within the lookback window', () => {
|
||||
const line = 'x'.repeat(100);
|
||||
const text = Array.from({ length: 40 }, () => line).join('\n');
|
||||
const pieces = capByEstimatedTokens(text, 1000);
|
||||
expect(pieces.length).toBeGreaterThan(1);
|
||||
for (const p of pieces) {
|
||||
// Every piece should be whole lines (multiples of the 100-char line).
|
||||
for (const l of p.split('\n')) {
|
||||
expect(l).toBe(line);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('makes forward progress on whitespace-less input (hard cut)', () => {
|
||||
const blob = 'a'.repeat(10_000);
|
||||
const pieces = capByEstimatedTokens(blob, 1000);
|
||||
expect(pieces.length).toBeGreaterThan(1);
|
||||
expect(pieces.join('')).toBe(blob);
|
||||
for (const p of pieces) {
|
||||
expect(estimateEmbeddingTokens(p)).toBeLessThanOrEqual(1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateEmbeddingTokens — weight sanity', () => {
|
||||
test('overestimates URL-dense ASCII (0.75/char ≥ measured ~0.63/char)', () => {
|
||||
const url = 'https://docs.example.com/pages/a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4?v=abc&ref=sample';
|
||||
const est = estimateEmbeddingTokens(url);
|
||||
expect(est).toBeGreaterThanOrEqual(Math.floor(url.length * 0.7));
|
||||
});
|
||||
|
||||
test('counts CJK at 1 token/char', () => {
|
||||
expect(estimateEmbeddingTokens('가나다라마')).toBe(5);
|
||||
});
|
||||
|
||||
test('whitespace is nearly free', () => {
|
||||
expect(estimateEmbeddingTokens(' \n\t ')).toBeLessThanOrEqual(1);
|
||||
});
|
||||
|
||||
test('empty string is 0', () => {
|
||||
expect(estimateEmbeddingTokens('')).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -32,12 +32,6 @@ function buildMockEngine(opts: {
|
||||
}): BrainEngine {
|
||||
return {
|
||||
kind: 'pglite',
|
||||
// #2464: checkCalibrationFreshness resolves the owner holder via
|
||||
// resolveOwnerHolder(config emotional_weight.user_holder, else 'self'), so the
|
||||
// mock must implement getConfig. null = key unset → resolver falls back to 'self'.
|
||||
async getConfig(): Promise<string | null> {
|
||||
return null;
|
||||
},
|
||||
async executeRaw<T>(sql: string): Promise<T[]> {
|
||||
if (opts.throwOn && opts.throwOn.test(sql)) {
|
||||
throw new Error('mock engine error: ' + sql.slice(0, 50));
|
||||
|
||||
@@ -112,7 +112,3 @@ describe('computeEmotionalWeight', () => {
|
||||
expect(HIGH_EMOTION_TAGS.has('mental-health')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test('DEFAULT_USER_HOLDER is the canonical owner holder self', () => {
|
||||
expect(DEFAULT_USER_HOLDER).toBe('self');
|
||||
});
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { resolveOwnerHolder, DEFAULT_OWNER_HOLDER } from '../src/core/owner-holder.ts';
|
||||
|
||||
describe('owner-holder', () => {
|
||||
test('DEFAULT_OWNER_HOLDER is self', () => {
|
||||
expect(DEFAULT_OWNER_HOLDER).toBe('self');
|
||||
});
|
||||
|
||||
test('defaults to self when nothing provided', () => {
|
||||
expect(resolveOwnerHolder({})).toBe('self');
|
||||
});
|
||||
|
||||
test('null/undefined config falls back to self', () => {
|
||||
expect(resolveOwnerHolder({ configValue: null })).toBe('self');
|
||||
expect(resolveOwnerHolder({ configValue: undefined })).toBe('self');
|
||||
});
|
||||
|
||||
test('uses config value when set and no override', () => {
|
||||
expect(resolveOwnerHolder({ configValue: 'people/charlie-example' }))
|
||||
.toBe('people/charlie-example');
|
||||
});
|
||||
|
||||
test('override beats config and default', () => {
|
||||
expect(resolveOwnerHolder({ override: 'world', configValue: 'people/charlie-example' }))
|
||||
.toBe('world');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user