Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
202 changes: 202 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
build-test:
name: Build and test all packages
runs-on: ubuntu-latest
steps:
# Full history is needed: the secret scan compares the PR's commits
# against origin/main, and the dist-vs-source check diffs the tree.
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: pnpm/action-setup@v4
with:
version: 10.33.2

- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

# Packages are built in dependency order. codeflow-core is the root of
# the graph; everything else depends on it transitively.
#
# codeflow-core
# ├── codeflow-store
# │ ├── codeflow-prd
# │ ├── codeflow-analysis
# │ ├── codeflow-versioning
# │ └── codeflow-agent
# ├── codeflow-mcp
# ├── codeflow-execution
# │ └── codeflow-dtwin
# ├── codeflow-canvas
# ├── codeflow-evolution
# └── codeflow-master (consumes all of the above + CodeRag)
#
# CodeRag is built before the packages that consume its types
# (codeflow-canvas, codeflow-versioning, codeflow-master).
- name: Build packages in dependency order
run: |
set -euo pipefail
for pkg in \
codeflow-core \
codeflow-store \
codeflow-prd \
codeflow-analysis \
codeflow-mcp \
codeflow-execution \
codeflow-dtwin \
CodeRag \
codeflow-canvas \
codeflow-evolution \
codeflow-versioning \
codeflow-agent \
Codeflow_master
do
echo "::group::build $pkg"
npm --prefix "packages/$pkg" run build
echo "::endgroup::"
done

- name: Test packages
run: |
set -uo pipefail
failed=0
for pkg in \
codeflow-store \
codeflow-prd \
codeflow-analysis \
codeflow-mcp \
codeflow-execution \
codeflow-dtwin \
codeflow-canvas \
codeflow-evolution \
codeflow-versioning \
codeflow-agent \
CodeRag \
Codeflow_master
do
echo "::group::test $pkg"
if ! npm --prefix "packages/$pkg" test; then
echo "::error::$pkg tests failed"
failed=1
fi
echo "::endgroup::"
done
exit $failed

# dist/ is committed to this repository, so a stale dist/ means the
# published artifact does not match the source. Rebuilding and diffing
# catches that before it reaches npm.
# Two things can go wrong here, and `git diff` only catches the first:
# a committed file that the build no longer produces, and a file the
# build produces that was never committed. The second is the one that
# silently ships a broken package - the entry point exists in the
# exports map but is missing from the published tarball.
- name: Verify committed dist/ matches source
run: |
set -euo pipefail
git config --global --add safe.directory "$GITHUB_WORKSPACE"

if ! git diff --exit-code -- 'packages/*/dist'; then
echo "::error::Committed dist/ is stale. Run 'pnpm -r build' and commit the result."
git diff --stat -- 'packages/*/dist'
exit 1
fi

# Files the build produced that are not in the index. `dist` is
# gitignored, so a new build output has to be added explicitly.
#
# Only the packages that commit dist/ are checked. codeflow-core,
# CodeRag and the IDE build their output at publish time instead,
# so their dist/ is deliberately absent from the repository.
dist_paths=()
for pkg in codeflow-store codeflow-prd codeflow-analysis codeflow-mcp \
codeflow-execution codeflow-dtwin codeflow-canvas \
codeflow-evolution codeflow-versioning codeflow-agent; do
dist_paths+=(":(glob)packages/$pkg/dist/**")
done

untracked="$(git ls-files --others --ignored --exclude-standard -- "${dist_paths[@]}")"
if [[ -n "$untracked" ]]; then
echo "::error::Built dist/ files are not committed. Run 'git add -f packages/*/dist' and commit."
echo "$untracked"
exit 1
fi

# A package that imports a module it does not declare works locally by
# way of a hoisted copy and fails in CI, where the install is isolated.
- name: Check for undeclared imports
run: python3 scripts/check-undeclared-imports.py

# The build and test loops name packages by their directory under
# packages/, which differs from the npm package name for two of them.
- name: Check CI package paths
run: python3 scripts/check-ci-package-paths.py

# A package can declare an entry point in its exports map that the
# build never produces. TypeScript compiles fine and the failure only
# shows up in the installed package, as ERR_MODULE_NOT_FOUND.
- name: Check declared exports resolve
run: node scripts/check-exports-resolve.mjs

# gitleaks is not preinstalled on the runner image, so the repo's
# `pnpm secrets:scan` script fails with "gitleaks: not found".
#
# The scan is scoped to the commits this PR introduces. A full-history
# scan reports four leaks that predate this branch - a Gemini key and
# an NVIDIA key committed in 10fbd89 (2026-04-02), and a MiniMax key
# in 1f713b5 (2026-05-26). They are not in the current tree, but they
# are still in git history and those credentials should be rotated.
# This gate blocks NEW leaks; purging history is a separate operation.
- name: Secret scan
uses: gitleaks/gitleaks-action@v2
env:
# The action requires a token to scan pull requests.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
version: v8.28.0
args: >-
detect --source . --redact --config .gitleaks.toml
--log-opts="origin/main..HEAD"

codeql:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
permissions:
security-events: write
packages: read
actions: read
contents: read
strategy:
fail-fast: false
matrix:
language: [javascript-typescript]
steps:
- uses: actions/checkout@v4

- uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}

- uses: github/codeql-action/autobuild@v3

- uses: github/codeql-action/analyze@v3
with:
category: "/language:${{ matrix.language }}"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ artifacts
.codeflow-store-test
.codeflow-sandboxes
.test-store
# Test-run scratch state. The store package's suite writes into these
# directories on every run; tracking them makes every test run dirty the
# working tree.
.test-store-*
*.log
*.tsbuildinfo
.env
Expand Down
14 changes: 14 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# pnpm workspace settings for the CodeFlow monorepo.
#
# link-workspace-packages
# Link a workspace package instead of fetching it from the registry when the
# local version satisfies the declared range. Without this, a package whose
# local version drifts outside the declared range is silently resolved from
# npm — so local edits to that package never reach the running app.
#
# prefer-workspace-packages
# When the local version and the published version are identical, pnpm still
# prefers the registry. prefer-workspace-packages makes the workspace copy
# win the tie, so a package is always built and consumed from source.
link-workspace-packages=true
prefer-workspace-packages=true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Every package has a deep dive in [`docs/`](./docs). Each one covers purpose, pub

## Development

The repository is a pnpm workspace (`pnpm-workspace.yaml` at the root). All packages live under `packages/`. There is no root `package.json`; the root only holds the workspace manifest, this README, the documentation in `docs/`, and the `.gitignore`.
The repository is a pnpm workspace (`pnpm-workspace.yaml` at the root). All packages live under `packages/`. The root holds the workspace manifest, a private root `package.json` (workspace scripts + the `packageManager` pin), the shared `tsconfig.json`, the documentation in `docs/`, and the `.gitignore`.

To install everything at once:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"secrets:scan": "gitleaks detect --source . --redact --config .gitleaks.toml",
"secrets:scan:history": "gitleaks detect --source . --log-opts=--all --redact --config .gitleaks.toml",
"secrets:install-hook": "git config core.hooksPath .githooks && echo 'Hooks installed. Pre-commit and pre-push will now run gitleaks.'"
}
},
"packageManager": "pnpm@10.33.2"
}
4 changes: 2 additions & 2 deletions packages/CodeRag/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@abhinav2203/coderag",
"version": "1.0.3",
"version": "1.3.4",
"description": "Standalone code retrieval and MCP server for multi-language repositories built on @abhinav2203/codeflow-core.",
"license": "Apache-2.0",
"type": "module",
Expand Down Expand Up @@ -61,7 +61,7 @@
"access": "public"
},
"dependencies": {
"@abhinav2203/codeflow-core": "^1.1.2",
"@abhinav2203/codeflow-core": "^1.1.7",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh CodeRag's npm lockfile

When npm ci is run inside packages/CodeRag, the tracked lockfile still declares @abhinav2203/codeflow-core as ^1.1.2 and locks 1.1.2, while this manifest now requires ^1.1.7; npm ci exits when the lock and manifest dependencies do not match (npm ci docs). Update the package lock alongside this dependency change.

Useful? React with 👍 / 👎.

"@lancedb/lancedb": "^0.22.0",
"@modelcontextprotocol/sdk": "1.29.0",
"@xenova/transformers": "^2.17.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/CodeRag/src/indexer/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const summarizeEdges = (

const formatSourceRefs = (node: BlueprintNode): string =>
formatList(
node.sourceRefs.map((sourceRef) =>
(node.sourceRefs ?? []).map((sourceRef) =>
`${sourceRef.kind}${sourceRef.symbol ? `:${sourceRef.symbol}` : ""}${sourceRef.path ? ` @ ${sourceRef.path}` : ""}`
)
);
Expand Down Expand Up @@ -178,7 +178,7 @@ export const buildNodeDocument = (
formatFieldList(node.contract.outputs),
"",
"Declared Dependencies:",
formatList(node.contract.dependencies),
formatList(node.contract.dependencies ?? []),
"",
"Source References:",
formatSourceRefs(node),
Expand Down
71 changes: 69 additions & 2 deletions packages/CodeRag/src/store/file-cache.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
import type { Stats } from "node:fs";
import fs from "node:fs/promises";

/**
* Timestamp-resolution safety margin.
*
* The kernel stamps inodes from the *coarse* system clock and truncates it to
* the filesystem's timestamp granularity, so several writes that land inside
* the same granularity window share one mtime value. Measured on this host:
* ~4ms on ext4 (deltas of 0.0ms and 4.000014ms alternate across rapid
* writes); ext3/HFS+ use 1s and FAT/exFAT use 2s.
*
* Consequence: a cache that trusts mtime alone can hand back content that
* was already overwritten — the cached entry looks fresh even though the
* file changed.
*
* The fingerprint (mtime + size + inode) is therefore only conclusive once
* the mtime observed at read time is already older than one granularity
* window: the mtime recorded at read time is the start of the window the
* content was written in, so any write that happens after such a read
* necessarily lands on a strictly greater timestamp and is detected. While
* the file is still inside the window the fingerprint proves nothing, and
* the file is read again.
*
* The margin must exceed the coarsest plausible filesystem granularity (2s
* for FAT/exFAT) plus the coarse-clock lag, hence 4s. Files modified within
* that window are simply re-read until they age out of it.
*/
const MTIME_RESOLUTION_MARGIN_MS = 4000;

type CacheEntry = {
content: string;
mtimeMs: number;
size: number;
ino: number;
/**
* Wall-clock time at which the read that produced `content` started.
* The fingerprint is conclusive only when the mtime observed for that read
* was already outside the filesystem's timestamp resolution window.
*/
readAt: number;
};

export class FileCache {
private readonly cache = new Map<string, CacheEntry>();

async read(filePath: string): Promise<string> {
// The read is timestamped *before* stat() so the anchor used by the
// conclusiveness check is never later than the moment the content was
// actually obtained.
const readAt = Date.now();
// stat() is taken before the read on purpose: the recorded mtime must
// never describe a write that is newer than the content we hold, or a
// later rewrite could hide behind an unchanged fingerprint.
const stats = await fs.stat(filePath);
const cached = this.cache.get(filePath);

if (cached && cached.mtimeMs === stats.mtimeMs) {
if (cached && this.matchesFingerprint(cached, stats) && this.isConclusive(cached)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Test same-size rewrites inside the mtime window

When a source file is rewritten to different same-length content within the filesystem's timestamp-resolution window, this guard is the new behavior that prevents returning stale cached retrieval content. The existing page-index test changes the file size and manually invalidates the cache, so it does not cover this case; add a direct regression test for the same-fingerprint rewrite.

AGENTS.md reference: packages/CodeRag/AGENTS.md:L25-L26

Useful? React with 👍 / 👎.

return cached.content;
}

const content = await fs.readFile(filePath, "utf8");
this.cache.set(filePath, {
content,
mtimeMs: stats.mtimeMs
mtimeMs: stats.mtimeMs,
size: stats.size,
ino: stats.ino,
readAt
});
return content;
}
Expand All @@ -31,4 +77,25 @@ export class FileCache {
clear(): void {
this.cache.clear();
}

/**
* Cheap fingerprint check. mtime alone is not enough (see
* MTIME_RESOLUTION_MARGIN_MS), but a change in size or inode always
* invalidates the entry.
*/
private matchesFingerprint(cached: CacheEntry, stats: Stats): boolean {
return cached.mtimeMs === stats.mtimeMs && cached.size === stats.size && cached.ino === stats.ino;
}

/**
* True when the fingerprint provably identifies the cached content.
*
* The mtime recorded at read time was already older than one resolution
* window, so every write that happens afterwards bumps it and is detected
* by the fingerprint. While the file is still inside the window a rewrite
* is invisible to the fingerprint, so the file has to be read again.
*/
private isConclusive(cached: CacheEntry): boolean {
return cached.mtimeMs <= cached.readAt - MTIME_RESOLUTION_MARGIN_MS;
}
}
Loading
Loading