@cmflow/source-pack
v3.22.0
Published
Versioned source code index (chunks + embeddings) built and published at each production release
Readme
source-pack builds, at each production release of a project hosted on GitHub or GitLab, a versioned index of its code
(chunks + embeddings + manifest) and publishes it on the release of the tag. The Dataviz loads it with
readSourcePack() to answer source_search on any production version, without a local checkout.
The package is generic: everything that depends on a project lives in its source-pack.config.ts.
Commands
npx @cmflow/source-pack init # create ./source-pack.config.ts
npx @cmflow/source-pack build # index HEAD, version from package.json
npx @cmflow/source-pack build --tag version-1.2.3 # index the tag, version extracted from it
npx @cmflow/source-pack publish # upload the last build on the release of its tagGlobal option: -c, --config <path> (default ./source-pack.config.ts). Paths of the config are resolved against the directory
the command is run from. The config may be a .ts file (loaded with tsx) or a .mjs file. It can import defineConfig from
@cmflow/source-pack even when the package is not installed in the project (npx usage): the import resolves to the running CLI.
| Command | Options |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| init | --force overwrites an existing file. repository is deduced from the origin remote; exclusions are pre-filled for the detected project type (node, python, jvm, go). |
| build | --ref <commit-ish> (default: --tag, otherwise HEAD), --tag <tag>, --release-version <v> (default: from --tag with publish.tag, otherwise version of package.json at the ref), --skip-embeddings, --dry-run (no LiteLLM call, nothing written, sizes estimated). |
| publish | --archive <path> (default: last build of outputDir), --dry-run (logs the planned upload, no HTTP call). |
Secrets are read from the environment only, never from the config, and are never logged nor written to the pack:
| Variable | Used by |
| ----------------- | ------------------------------------------------------------------------- |
| LITELLM_API_KEY | build (embeddings; the name can be changed with embedding.apiKeyEnv) |
| GITHUB_TOKEN | publish on a GitHub repository (contents: write) |
| GITLAB_TOKEN | publish on a GitLab project (api scope; falls back to CI_JOB_TOKEN) |
Configuration
import { defineConfig } from "@cmflow/source-pack";
export default defineConfig({
outputDir: "./tmp/source-pack",
repository: { provider: "github", owner: "acme", name: "example-api" },
// GitLab: { provider: "gitlab", projectId: "group/project" (or 1234), apiUrl: "https://gitlab.example.com/api/v4" }
sources: {
include: ["**/*.{ts,tsx,js,json,yaml,yml,md,feature}"],
exclude: ["mock/**", "test/functionnal/**", "package-lock.json", "coverage/**", "dist/**"],
maxFileBytes: 1048576
},
chunking: { lines: 100, overlap: 15 },
embedding: {
baseUrl: "https://litellm.example.com",
model: "text-embedding-3-small",
fallbackModels: ["text-embedding-3-small-fallback-1", "text-embedding-3-small-fallback-2"],
referenceModel: "text-embedding-3-small",
dimensions: 1536,
batchSize: 64,
concurrency: 4
},
publish: {
tag: "version-{version}",
assetName: "source-pack-{version}.tar.gz"
}
});- Only files tracked by Git at the indexed commit are read (from the Git objects, not the working tree: the result does not depend
on the checkout, line endings or the OS). Binary files (NUL byte in the first 8000 bytes), symlinks, submodules and files above
maxFileBytesare skipped. Test files (*.spec.*,*.test.*and their Python, Go and JVM equivalents) are excluded by default: they double the index size without describing production behavior. - Globs use minimatch with dot files. Defaults when
sourcesis omitted: common text extensions, excluding tests,node_modules,dist,build,coverageand lockfiles. - Chunks are windows of
lineslines advancing bylines - overlap; blank windows are skipped;id = sha256("<path>:<startLine>:<text>")— identical to the state-control indexer. - Embeddings use the OpenAI-compatible
/v1/embeddingsendpoint of LiteLLM (/v1is appended tobaseUrl), in batches ofbatchSize,concurrency(4) batches at a time, each retriedmaxRetries(3) times before switching to the nextfallbackModelsalias. The embedded text of a chunk is"<path>\n<text>"truncated tomaxInputChars(12000). The manifest records the reference model (referenceModel, default:model): set it to the public model behind the gateway aliases so the manifest never names the regional alias that answered. - Omit
embedding(or use--skip-embeddings) for a lexical-only pack. publish.createRelease(defaultfalse) creates the release when the tag has none; by default the release tool owns releases and a missing release is an error.
An invalid config is rejected with one line per field, e.g.:
Invalid source-pack config (/repo/source-pack.config.ts):
- repository.provider: expected "github" or "gitlab" (received "bitbucket")
- chunking.overlap: expected a value lower than chunking.linesPack format (source-pack-<version>.tar.gz)
| Entry | Content |
| ----------------- | ------------------------------------------------------------------------------------------------------ |
| manifest.json | Project, version, tag, ref, SHA, dates, chunking version + config fingerprint, embedding model, counts |
| chunks.jsonl.gz | One JSON object per line: { id, path, startLine, endLine, text } (lines are 1-based and inclusive) |
| embeddings.f32 | chunks.count × dimensions little-endian float32, in chunk order (absent without embeddings) |
manifest.project.id (github:<owner>/<repo> or gitlab:<project>), sha, config.fingerprint (sha256 of sources +
chunking + chunking format version) and embedding.model/dimensions uniquely identify the pack content.
Packs built by versions 3.19 and 3.20 may also contain an attachments/ directory and a manifest.attachments field. The format
version is unchanged (2): readSourcePack still reads these packs and ignores both.
Publishing
The destination follows repository.provider:
- GitHub: asset of the GitHub release of the tag; an asset with the same name is deleted first (idempotent).
- GitLab:
PUT /projects/:id/packages/generic/<packageName>/<version>/<assetName>(Generic Package Registry,packageNamedefaults tosource-pack), older files with the same name in that package version are deleted, then apackagelink with the same name is created or updated on the GitLab release of the tag.
Reading a pack (Dataviz)
import { embeddingInput, readSourcePack } from "@cmflow/source-pack";
const pack = await readSourcePack("./source-pack-0.3503.0.tar.gz"); // or the downloaded bytes (Uint8Array)
pack.manifest.sha;
pack.chunks[0]; // { id, path, startLine, endLine, text }
pack.getEmbedding(0); // Float32Array(1536) | nullreadSourcePack checks the format version, the chunk count and the embeddings size. Embed queries with the same
manifest.embedding.model/dimensions; use embeddingInput() to re-embed chunks consistently.
Onboarding a project
- In the project root:
npx @cmflow/source-pack init, then adjustsources.exclude(mocks, fixtures, generated data, vendored code…). - Check the result locally:
npx @cmflow/source-pack build --dry-run(chunk count and estimated size), thenLITELLM_API_KEY=… npx @cmflow/source-pack buildandnpx @cmflow/source-pack publish --dry-run. - Commit
source-pack.config.tsand addtmp/source-packto.gitignore. - Add the job to the production merge pipeline only, after the job that creates the tag and its release (snippets below). It must not be required by any other job so that a failure never blocks the release; rerunning it republishes the pack.
- Provide the secrets:
LITELLM_API_KEYandGITHUB_TOKEN(CircleCI shared contextsource-pack) orLITELLM_API_KEYandGITLAB_TOKEN(masked GitLab CI/CD variables). - Enable the project in the « Source pack » tab of the Dataviz.
CircleCI
Copy templates/circleci-job.yml under jobs: and add to the production workflow:
- source-pack:
requires: [cm-release-prod]
filters:
branches:
only: /^production$/
context: source-packThe job resolves the first version-* tag containing the pipeline commit (the release commit created by cmrelease), then runs
build --tag "$TAG" and publish.
GitLab CI
include:
- remote: "https://raw.githubusercontent.com/ClubMediterranee/cmflow/master/packages/source-pack/templates/gitlab-ci.yml"
source-pack:
extends: .source-pack
needs: ["release"]
rules:
- if: $CI_COMMIT_BRANCH == "production"The .source-pack template (templates/gitlab-ci.yml) is allow_failure: true and accepts
SOURCE_PACK_VERSION and SOURCE_PACK_TAG_PATTERN variables.
