Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ export function buildGitInvocation(repoPath: string, args: string[], configs: st
return [...cfg, '-C', repoPath, ...args];
}

export function buildAutoEmbedArgs(slugs: string[], sourceId?: string): string[] {
return sourceId ? ['--source', sourceId, '--slugs', ...slugs] : ['--slugs', ...slugs];
}

/**
* Shell out to git with a generous maxBuffer.
*
Expand Down Expand Up @@ -962,19 +966,13 @@ async function performSyncInner(engine: BrainEngine, opts: SyncOpts): Promise<Sy
}

// Auto-embed (skip for large syncs — embedding calls OpenAI).
// TODO(multi-source): runEmbed → src/commands/embed.ts:175 + :418 call
// upsertChunks defaulting to source='default'. For non-default-source syncs
// the page row lives at (sourceId, slug) so this fails with "Page not found"
// OR (when a same-slug 'default' row coexists) updates the wrong source's
// chunks. Data R1 MED 2 — deferred to a follow-up PR; threading sourceId
// through embed.ts is a larger refactor than this fix's scope. The current
// try/catch swallows the failure as best-effort, so the sync result still
// reports `embedded: 0` for the right reason.
// Thread sourceId so incremental source syncs embed the page row they just
// imported instead of falling back to the default source.
let embedded = 0;
if (!noEmbed && pagesAffected.length > 0 && pagesAffected.length <= 100) {
try {
const { runEmbed } = await import('./embed.ts');
await runEmbed(engine, ['--slugs', ...pagesAffected]);
await runEmbed(engine, buildAutoEmbedArgs(pagesAffected, opts.sourceId));
// Before commit 2 lands: runEmbed is void. Best estimate is pagesAffected,
// since runEmbed re-embeds every requested slug. Commit 2 sharpens this
// with EmbedResult.embedded.
Expand Down
17 changes: 16 additions & 1 deletion test/sync.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, test, expect, beforeAll, afterAll, beforeEach, afterEach } from 'bun:test';
import { buildSyncManifest, isSyncable, pathToSlug } from '../src/core/sync.ts';
import { buildGitInvocation } from '../src/commands/sync.ts';
import { buildAutoEmbedArgs, buildGitInvocation } from '../src/commands/sync.ts';
import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from 'fs';
import { join } from 'path';
import { execSync } from 'child_process';
Expand Down Expand Up @@ -560,3 +560,18 @@ describe('git() helper invocation order (CJK wave v0.32.7)', () => {
]);
});
});

describe('sync auto-embed arguments', () => {
test('scopes incremental source sync embedding to the same source', () => {
expect(buildAutoEmbedArgs(['hello-js'], 'source-a')).toEqual([
'--source',
'source-a',
'--slugs',
'hello-js',
]);
});

test('keeps default-source sync embed arguments unchanged', () => {
expect(buildAutoEmbedArgs(['people/alice'])).toEqual(['--slugs', 'people/alice']);
});
});
Loading