Rachit-Tw commited on
Commit
e8cbb4a
·
1 Parent(s): a54d8de

chore: optimize temp storage, caching and file retrieval for HF hosting

Browse files
Files changed (7) hide show
  1. .gitignore +4 -0
  2. index.ts +4 -3
  3. src/checkout.ts +10 -1
  4. src/review.ts +22 -11
  5. src/symbol-graph-cache.ts +1 -1
  6. src/symbol-graph.ts +4 -3
  7. src/utils.ts +12 -7
.gitignore CHANGED
@@ -3,6 +3,10 @@ lib/
3
  .env
4
  logs.txt
5
  tmp_hf_space/
 
 
 
 
6
  .DS_Store
7
  *.log
8
  .gemini/
 
3
  .env
4
  logs.txt
5
  tmp_hf_space/
6
+ prix_tmp_*
7
+ prix-data/
8
+ parsers/
9
+ /tmp/prix*
10
  .DS_Store
11
  *.log
12
  .gemini/
index.ts CHANGED
@@ -8,6 +8,7 @@ import {setOctokit} from './src/octokit'
8
  import {checkoutRepo} from './src/checkout'
9
  import {rmSync} from 'fs'
10
  import {join} from 'path'
 
11
  import {feedbackCapture} from './src/feedback-capture'
12
  import {feedbackTracker} from './src/feedback-tracker'
13
 
@@ -100,10 +101,10 @@ export default (app: Probot, {getRouter}: any) => {
100
  const options = getOptions()
101
  const prompts = new Prompts()
102
 
103
- // Create a unique temporary directory for this audit
104
  const tempDir = join(
105
- process.cwd(),
106
- `tmp_${payload.pull_request.id}_${Date.now()}`
107
  )
108
 
109
  // Create a safe, isolated execution context
 
8
  import {checkoutRepo} from './src/checkout'
9
  import {rmSync} from 'fs'
10
  import {join} from 'path'
11
+ import {tmpdir} from 'os'
12
  import {feedbackCapture} from './src/feedback-capture'
13
  import {feedbackTracker} from './src/feedback-tracker'
14
 
 
101
  const options = getOptions()
102
  const prompts = new Prompts()
103
 
104
+ // Create a unique temporary directory for this audit in the system temp folder
105
  const tempDir = join(
106
+ tmpdir(),
107
+ `prix_tmp_${payload.pull_request.id}_${Date.now()}`
108
  )
109
 
110
  // Create a safe, isolated execution context
src/checkout.ts CHANGED
@@ -15,8 +15,17 @@ export const checkoutRepo = async (
15
  const ctx = getCurrentContext()
16
  if (!ctx) throw new Error('No context available for checkout')
17
 
 
 
 
 
 
 
18
  const {owner, repo} = ctx.repo
19
- const installationId = ctx.probotContext.payload.installation.id
 
 
 
20
 
21
  const tokenResp =
22
  await ctx.probotContext.octokit.apps.createInstallationAccessToken({
 
15
  const ctx = getCurrentContext()
16
  if (!ctx) throw new Error('No context available for checkout')
17
 
18
+ try {
19
+ prixExec('git --version')
20
+ } catch (e) {
21
+ throw new Error('git command not found in PATH. Please install git.')
22
+ }
23
+
24
  const {owner, repo} = ctx.repo
25
+ const installationId = ctx.probotContext.payload.installation?.id
26
+ if (!installationId) {
27
+ throw new Error('Installation ID not found in payload')
28
+ }
29
 
30
  const tokenResp =
31
  await ctx.probotContext.octokit.apps.createInstallationAccessToken({
src/review.ts CHANGED
@@ -1,5 +1,7 @@
1
  import {prixExec} from './utils'
2
  import {Project, SyntaxKind} from 'ts-morph'
 
 
3
 
4
  import pLimit from 'p-limit'
5
  import {Bot} from './bot'
@@ -54,10 +56,13 @@ export const codeReview = async (
54
  const commenter: Commenter = new Commenter()
55
  const project = new Project()
56
 
57
- // Initialize shared context engine once at the start
58
  try {
59
- const workingDir = prixExec('pwd', {encoding: 'utf8'}).trim()
60
- await unifiedContextEngine.initialize(workingDir)
 
 
 
61
  } catch (e) {
62
  info(`Context engine initialization failed: ${e}`)
63
  }
@@ -224,14 +229,20 @@ export const codeReview = async (
224
  return null
225
  }
226
  try {
227
- const contents = await octokit.repos.getContent({
228
- owner: repo.owner,
229
- repo: repo.repo,
230
- path: file.filename,
231
- ref: context.payload.pull_request.base.sha
232
- })
233
- if (contents.data != null) {
234
- if (!Array.isArray(contents.data)) {
 
 
 
 
 
 
235
  if (
236
  contents.data.type === 'file' &&
237
  contents.data.content != null
 
1
  import {prixExec} from './utils'
2
  import {Project, SyntaxKind} from 'ts-morph'
3
+ import {readFileSync, existsSync} from 'fs'
4
+ import {join} from 'path'
5
 
6
  import pLimit from 'p-limit'
7
  import {Bot} from './bot'
 
56
  const commenter: Commenter = new Commenter()
57
  const project = new Project()
58
 
59
+ // Initialize shared context engine once at the start using the cloned repo path
60
  try {
61
+ const store = als.getStore()
62
+ const workingDir = store?.workingDir || process.cwd()
63
+ const repoInfo = store?.repo
64
+ const stableId = repoInfo ? `${repoInfo.owner}/${repoInfo.repo}` : undefined
65
+ await unifiedContextEngine.initialize(workingDir, stableId)
66
  } catch (e) {
67
  info(`Context engine initialization failed: ${e}`)
68
  }
 
229
  return null
230
  }
231
  try {
232
+ const store = als.getStore()
233
+ const workingDir = store?.workingDir || process.cwd()
234
+ const localPath = join(workingDir, file.filename)
235
+
236
+ if (existsSync(localPath)) {
237
+ fileContent = readFileSync(localPath, 'utf8')
238
+ } else {
239
+ const contents = await octokit.repos.getContent({
240
+ owner: repo.owner,
241
+ repo: repo.repo,
242
+ path: file.filename,
243
+ ref: context.payload.pull_request.base.sha
244
+ })
245
+ if (contents.data != null && !Array.isArray(contents.data)) {
246
  if (
247
  contents.data.type === 'file' &&
248
  contents.data.content != null
src/symbol-graph-cache.ts CHANGED
@@ -230,7 +230,7 @@ export class SymbolGraphCache {
230
  }
231
 
232
  private sanitizeFilename(path: string): string {
233
- return path.replace(/[^a-z0-9]/gi, '_').substring(0, 100)
234
  }
235
 
236
  rebuildIfNeeded(changedFiles: string[]): {
 
230
  }
231
 
232
  private sanitizeFilename(path: string): string {
233
+ return path.replace(/[\/\\]/g, '_').substring(0, 100)
234
  }
235
 
236
  rebuildIfNeeded(changedFiles: string[]): {
src/symbol-graph.ts CHANGED
@@ -119,10 +119,11 @@ export class UnifiedContextEngine {
119
  this.project = new Project()
120
  }
121
 
122
- async initialize(repoPath: string): Promise<RepoContext> {
123
  this.repoPath = repoPath
124
- this.graphCache = new SymbolGraphCache(repoPath)
125
- this.incrementalGraph = new IncrementalSymbolGraph(repoPath)
 
126
  await this.buildSymbolGraph(repoPath)
127
  return this.buildRepoContext()
128
  }
 
119
  this.project = new Project()
120
  }
121
 
122
+ async initialize(repoPath: string, stableId?: string): Promise<RepoContext> {
123
  this.repoPath = repoPath
124
+ const cacheId = stableId || repoPath
125
+ this.graphCache = new SymbolGraphCache(cacheId)
126
+ this.incrementalGraph = new IncrementalSymbolGraph(cacheId)
127
  await this.buildSymbolGraph(repoPath)
128
  return this.buildRepoContext()
129
  }
src/utils.ts CHANGED
@@ -12,11 +12,16 @@ export const prixExec = (
12
  const context = als.getStore()
13
  const cwd = options.cwd || context?.workingDir || process.cwd()
14
 
15
- const result = execSync(command, {
16
- ...options,
17
- cwd,
18
- encoding: 'utf8'
19
- })
20
-
21
- return result ? result.toString() : ''
 
 
 
 
 
22
  }
 
12
  const context = als.getStore()
13
  const cwd = options.cwd || context?.workingDir || process.cwd()
14
 
15
+ try {
16
+ const result = execSync(command, {
17
+ ...options,
18
+ cwd,
19
+ encoding: 'utf8'
20
+ })
21
+ return result ? result.toString() : ''
22
+ } catch (err: any) {
23
+ const errorMessage = err.stderr ? err.stderr.toString() : err.message
24
+ console.error(`prixExec failed: "${command}" in ${cwd}. Error: ${errorMessage}`)
25
+ throw err
26
+ }
27
  }