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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions __tests__/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ describe('Sync Module', () => {
});

describe('getChangedFiles()', () => {
it('should report the most recent indexed timestamp', () => {
const lastIndexed = cg.getLastIndexedAt();

expect(lastIndexed).toEqual(expect.any(Number));
expect(lastIndexed).toBeGreaterThan(0);
expect(new Date(lastIndexed!).toISOString()).toMatch(/^\d{4}-\d{2}-\d{2}T/);
});

it('should detect added files', () => {
// Add a new file
fs.writeFileSync(
Expand Down
49 changes: 48 additions & 1 deletion src/bin/codegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,39 @@ function createVerboseProgress(): (progress: { phase: string; current: number; t
};
}

function toIsoTimestamp(timestamp: number | null | undefined): string | null {
if (timestamp == null) {
return null;
}
return new Date(timestamp).toISOString();
}

async function getConfiguredAgentCount(projectPath: string): Promise<number> {
const previousCwd = process.cwd();
try {
if (fs.existsSync(projectPath) && fs.statSync(projectPath).isDirectory()) {
process.chdir(projectPath);
}

const { detectAll } = await import('../installer/targets/registry');
const configuredTargets = new Set<string>();

for (const location of ['global', 'local'] as const) {
for (const { target, detection } of detectAll(location)) {
if (detection.alreadyConfigured) {
configuredTargets.add(target.id);
}
}
}

return configuredTargets.size;
} catch {
return 0;
} finally {
process.chdir(previousCwd);
}
}

/**
* Print success message
*/
Expand Down Expand Up @@ -689,11 +722,20 @@ program
.option('-j, --json', 'Output as JSON')
.action(async (pathArg: string | undefined, options: { json?: boolean }) => {
const projectPath = resolveProjectPath(pathArg);
const indexPath = getCodeGraphDir(projectPath);
const agentCount = options.json ? await getConfiguredAgentCount(projectPath) : 0;

try {
if (!isInitialized(projectPath)) {
if (options.json) {
console.log(JSON.stringify({ initialized: false, projectPath }));
console.log(JSON.stringify({
initialized: false,
projectPath,
indexPath,
lastIndexed: null,
agentCount,
version: packageJson.version,
}));
return;
}
console.log(chalk.bold('\nCodeGraph Status\n'));
Expand All @@ -709,12 +751,17 @@ program
const changes = cg.getChangedFiles();
const backend = cg.getBackend();
const journalMode = cg.getJournalMode();
const lastIndexed = toIsoTimestamp(cg.getLastIndexedAt());

// JSON output mode
if (options.json) {
console.log(JSON.stringify({
initialized: true,
projectPath,
indexPath,
lastIndexed,
agentCount,
version: packageJson.version,
fileCount: stats.fileCount,
nodeCount: stats.nodeCount,
edgeCount: stats.edgeCount,
Expand Down
10 changes: 10 additions & 0 deletions src/db/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,16 @@ export class QueryBuilder {
return rows.map(rowToFileRecord);
}

/**
* Get the most recent index timestamp across all tracked files.
*/
getLastIndexedAt(): number | null {
const row = this.db.prepare('SELECT MAX(indexed_at) AS last_indexed_at FROM files').get() as {
last_indexed_at: number | null;
} | undefined;
return row?.last_indexed_at ?? null;
}

/**
* Get files that need re-indexing (hash changed)
*/
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,13 @@ export class CodeGraph {
return this.queries.getAllFiles();
}

/**
* Get the most recent index timestamp across all tracked files.
*/
getLastIndexedAt(): number | null {
return this.queries.getLastIndexedAt();
}

// ===========================================================================
// Graph Query Methods
// ===========================================================================
Expand Down