Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1f5fed0

Browse files
authored
inference: Surface lua print(message) to UI (#52408)
1 parent 146208d commit 1f5fed0

File tree

26 files changed

+194
-111
lines changed

26 files changed

+194
-111
lines changed

client/web/src/enterprise/codeintel/configuration/backend.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export const AutoIndexJobFieldsFragment = gql`
4949
export const INFER_JOBS_SCRIPT = gql`
5050
query InferAutoIndexJobsForRepo($repository: ID!, $rev: String, $script: String) {
5151
inferAutoIndexJobsForRepo(repository: $repository, rev: $rev, script: $script) {
52-
...AutoIndexJobDescriptionFields
52+
jobs {
53+
...AutoIndexJobDescriptionFields
54+
}
55+
inferenceOutput
5356
}
5457
}
5558

client/web/src/enterprise/codeintel/configuration/components/inference-script/InferenceScriptPreview.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
useForm,
1212
Form,
1313
Label,
14+
Text,
1415
} from '@sourcegraph/wildcard'
1516

17+
import { LogOutput } from '../../../../../components/LogOutput'
1618
import {
1719
GetRepoIdResult,
1820
GetRepoIdVariables,
@@ -84,10 +86,19 @@ export const InferenceScriptPreview: React.FunctionComponent<InferenceScriptPrev
8486
) : error ? (
8587
<ErrorAlert error={error} />
8688
) : data ? (
87-
<InferenceForm
88-
initialFormData={autoIndexJobsToFormData({ jobs: data.inferAutoIndexJobsForRepo })}
89-
readOnly={true}
90-
/>
89+
<>
90+
{data.inferAutoIndexJobsForRepo.inferenceOutput && (
91+
<>
92+
<Text weight="bold">Script output:</Text>
93+
<LogOutput text={data.inferAutoIndexJobsForRepo.inferenceOutput} />
94+
</>
95+
)}
96+
97+
<InferenceForm
98+
initialFormData={autoIndexJobsToFormData({ jobs: data.inferAutoIndexJobsForRepo.jobs })}
99+
readOnly={true}
100+
/>
101+
</>
91102
) : (
92103
<></>
93104
)}

cmd/frontend/graphqlbackend/codeintel.autoindexing.graphql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ extend type Query {
8181
"""
8282
Return (but do not enqueue) descriptions of auto indexing jobs at the current revision.
8383
"""
84-
inferAutoIndexJobsForRepo(repository: ID!, rev: String, script: String): [AutoIndexJobDescription!]!
84+
inferAutoIndexJobsForRepo(repository: ID!, rev: String, script: String): InferAutoIndexJobsResult!
8585
}
8686

8787
extend type Mutation {
@@ -192,6 +192,21 @@ extend type Repository {
192192
codeIntelSummary: CodeIntelRepositorySummary!
193193
}
194194

195+
"""
196+
The result of running the auto-index inference script over a particular repo.
197+
"""
198+
type InferAutoIndexJobsResult {
199+
"""
200+
The list of inferred jobs.
201+
"""
202+
jobs: [AutoIndexJobDescription!]!
203+
204+
"""
205+
The output from the inference script.
206+
"""
207+
inferenceOutput: String!
208+
}
209+
195210
"""
196211
A list of precise code intelligence indexes.
197212
"""

enterprise/internal/codeintel/autoindexing/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/internal/codeintel/autoindexing/internal/background/summary/job_summary_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewSummaryBuilder(
5050

5151
if autoIndexingEnabled() {
5252
commit := "HEAD"
53-
indexJobs, err := jobSelector.InferIndexJobsFromRepositoryStructure(ctx, repositoryWithCount.RepositoryID, commit, "", false)
53+
result, err := jobSelector.InferIndexJobsFromRepositoryStructure(ctx, repositoryWithCount.RepositoryID, commit, "", false)
5454
if err != nil {
5555
if errors.As(err, &inference.LimitError{}) {
5656
continue
@@ -78,7 +78,7 @@ func NewSummaryBuilder(
7878
blocklist[key] = struct{}{}
7979
}
8080

81-
inferredAvailableIndexers = uploadsshared.PopulateInferredAvailableIndexers(indexJobs, blocklist, inferredAvailableIndexers)
81+
inferredAvailableIndexers = uploadsshared.PopulateInferredAvailableIndexers(result.IndexJobs, blocklist, inferredAvailableIndexers)
8282
// inferredAvailableIndexers = uploadsshared.PopulateInferredAvailableIndexers(indexJobHints, blocklist, inferredAvailableIndexers)
8383
}
8484

enterprise/internal/codeintel/autoindexing/internal/inference/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/internal/codeintel/autoindexing/internal/inference/service.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/autoindexing/internal/inference/lua"
1616
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/autoindexing/internal/inference/luatypes"
17+
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/autoindexing/shared"
1718
"github.com/sourcegraph/sourcegraph/internal/api"
1819
"github.com/sourcegraph/sourcegraph/internal/gitserver"
1920
"github.com/sourcegraph/sourcegraph/internal/gitserver/gitdomain"
@@ -40,6 +41,7 @@ type indexJobOrHint struct {
4041

4142
type invocationContext struct {
4243
sandbox *luasandbox.Sandbox
44+
printSink io.Writer
4345
gitService GitService
4446
repo api.RepoName
4547
commit string
@@ -82,7 +84,7 @@ func newService(
8284
// is assumed to be a table of recognizer instances. Keys conflicting with the default recognizers
8385
// will overwrite them (to disable or change default behavior). Each recognizer's generate function
8486
// is invoked and the resulting index jobs are combined into a flattened list.
85-
func (s *Service) InferIndexJobs(ctx context.Context, repo api.RepoName, commit, overrideScript string) (_ []config.IndexJob, err error) {
87+
func (s *Service) InferIndexJobs(ctx context.Context, repo api.RepoName, commit, overrideScript string) (_ *shared.InferenceResult, err error) {
8688
ctx, _, endObservation := s.operations.inferIndexJobs.With(ctx, &err, observation.Args{Attrs: []attribute.KeyValue{
8789
attribute.String("repo", string(repo)),
8890
attribute.String("commit", commit),
@@ -108,7 +110,7 @@ func (s *Service) InferIndexJobs(ctx context.Context, repo api.RepoName, commit,
108110
},
109111
}
110112

111-
jobOrHints, err := s.inferIndexJobOrHints(ctx, repo, commit, overrideScript, functionTable)
113+
jobOrHints, logs, err := s.inferIndexJobOrHints(ctx, repo, commit, overrideScript, functionTable)
112114
if err != nil {
113115
return nil, err
114116
}
@@ -122,7 +124,10 @@ func (s *Service) InferIndexJobs(ctx context.Context, repo api.RepoName, commit,
122124
jobs = append(jobs, *jobOrHint.indexJob)
123125
}
124126

125-
return jobs, nil
127+
return &shared.InferenceResult{
128+
IndexJobs: jobs,
129+
InferenceOutput: logs,
130+
}, nil
126131
}
127132

128133
// InferIndexJobHints invokes the given script in a fresh Lua sandbox. The return value of this script
@@ -155,7 +160,7 @@ func (s *Service) InferIndexJobHints(ctx context.Context, repo api.RepoName, com
155160
},
156161
}
157162

158-
jobOrHints, err := s.inferIndexJobOrHints(ctx, repo, commit, overrideScript, functionTable)
163+
jobOrHints, _, err := s.inferIndexJobOrHints(ctx, repo, commit, overrideScript, functionTable)
159164
if err != nil {
160165
return nil, err
161166
}
@@ -183,26 +188,32 @@ func (s *Service) inferIndexJobOrHints(
183188
commit string,
184189
overrideScript string,
185190
invocationContextMethods invocationFunctionTable,
186-
) ([]indexJobOrHint, error) {
191+
) (_ []indexJobOrHint, logs string, _ error) {
187192
sandbox, err := s.createSandbox(ctx)
188193
if err != nil {
189-
return nil, err
194+
return nil, "", err
190195
}
191196
defer sandbox.Close()
192197

193-
recognizers, err := s.setupRecognizers(ctx, sandbox, overrideScript)
194-
if err != nil || len(recognizers) == 0 {
195-
return nil, err
196-
}
198+
var buf bytes.Buffer
199+
defer func() { logs = buf.String() }()
197200

198201
invocationContext := invocationContext{
199202
sandbox: sandbox,
203+
printSink: &buf,
200204
gitService: s.gitService,
201205
repo: repo,
202206
commit: commit,
203207
invocationFunctionTable: invocationContextMethods,
204208
}
205-
return s.invokeRecognizers(ctx, invocationContext, recognizers)
209+
210+
recognizers, err := s.setupRecognizers(ctx, invocationContext, overrideScript)
211+
if err != nil || len(recognizers) == 0 {
212+
return nil, logs, err
213+
}
214+
215+
jobsOrHints, err := s.invokeRecognizers(ctx, invocationContext, recognizers)
216+
return jobsOrHints, logs, err
206217
}
207218

208219
// createSandbox creates a Lua sandbox wih the modules loaded for use with auto indexing inference.
@@ -232,12 +243,14 @@ func (s *Service) createSandbox(ctx context.Context) (_ *luasandbox.Sandbox, err
232243

233244
// setupRecognizers runs the given default and override scripts in the given sandbox and converts the
234245
// script return values to a list of recognizer instances.
235-
func (s *Service) setupRecognizers(ctx context.Context, sandbox *luasandbox.Sandbox, overrideScript string) (_ []*luatypes.Recognizer, err error) {
246+
func (s *Service) setupRecognizers(ctx context.Context, invocationContext invocationContext, overrideScript string) (_ []*luatypes.Recognizer, err error) {
236247
ctx, _, endObservation := s.operations.setupRecognizers.With(ctx, &err, observation.Args{})
237248
defer endObservation(1, observation.Args{})
238249

239-
opts := luasandbox.RunOptions{}
240-
rawRecognizers, err := sandbox.RunScriptNamed(ctx, opts, lua.Scripts, "recognizers.lua")
250+
opts := luasandbox.RunOptions{
251+
PrintSink: invocationContext.printSink,
252+
}
253+
rawRecognizers, err := invocationContext.sandbox.RunScriptNamed(ctx, opts, lua.Scripts, "recognizers.lua")
241254
if err != nil {
242255
return nil, err
243256
}
@@ -248,7 +261,7 @@ func (s *Service) setupRecognizers(ctx context.Context, sandbox *luasandbox.Sand
248261
}
249262

250263
if overrideScript != "" {
251-
rawRecognizers, err := sandbox.RunScript(ctx, opts, overrideScript)
264+
rawRecognizers, err := invocationContext.sandbox.RunScript(ctx, opts, overrideScript)
252265
if err != nil {
253266
return nil, err
254267
}
@@ -533,7 +546,9 @@ func (s *Service) invokeLinearizedRecognizer(
533546
return nil, nil
534547
}
535548

536-
opts := luasandbox.RunOptions{}
549+
opts := luasandbox.RunOptions{
550+
PrintSink: invocationContext.printSink,
551+
}
537552
args := []any{registrationAPI, callPaths, callContentsByPath}
538553
value, err := invocationContext.sandbox.Call(ctx, opts, invocationContext.callback(recognizer), args...)
539554
if err != nil {

enterprise/internal/codeintel/autoindexing/internal/inference/service_generator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func testGenerator(t *testing.T, testCase generatorTestCase) {
143143
t.Run(testCase.description, func(t *testing.T) {
144144
service := testService(t, testCase.repositoryContents)
145145

146-
jobs, err := service.InferIndexJobs(
146+
result, err := service.InferIndexJobs(
147147
context.Background(),
148148
"github.com/test/test",
149149
"HEAD",
@@ -152,7 +152,7 @@ func testGenerator(t *testing.T, testCase generatorTestCase) {
152152
if err != nil {
153153
t.Fatalf("unexpected error inferring jobs: %s", err)
154154
}
155-
if diff := cmp.Diff(sortIndexJobs(testCase.expected), sortIndexJobs(jobs)); diff != "" {
155+
if diff := cmp.Diff(sortIndexJobs(testCase.expected), sortIndexJobs(result.IndexJobs)); diff != "" {
156156
t.Errorf("unexpected index jobs (-want +got):\n%s", diff)
157157
}
158158
})

enterprise/internal/codeintel/autoindexing/internal/jobselector/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/internal/codeintel/autoindexing/internal/jobselector/iface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package jobselector
33
import (
44
"context"
55

6+
"github.com/sourcegraph/sourcegraph/enterprise/internal/codeintel/autoindexing/shared"
67
"github.com/sourcegraph/sourcegraph/internal/api"
78
"github.com/sourcegraph/sourcegraph/lib/codeintel/autoindex/config"
89
)
910

1011
type InferenceService interface {
11-
InferIndexJobs(ctx context.Context, repo api.RepoName, commit, overrideScript string) ([]config.IndexJob, error)
12+
InferIndexJobs(ctx context.Context, repo api.RepoName, commit, overrideScript string) (*shared.InferenceResult, error)
1213
InferIndexJobHints(ctx context.Context, repo api.RepoName, commit, overrideScript string) ([]config.IndexJobHint, error)
1314
}

0 commit comments

Comments
 (0)