Repository navigation
Expand file tree
/
Copy pathruntime.go
More file actions
453 lines (377 loc) · 13.6 KB
/
Copy pathruntime.go
File metadata and controls
453 lines (377 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/codefly-dev/core/agents/helpers/code"
"github.com/codefly-dev/core/agents/services"
basev0 "github.com/codefly-dev/core/generated/go/codefly/base/v0"
"github.com/codefly-dev/core/llmout"
"github.com/codefly-dev/core/resources"
runners "github.com/codefly-dev/core/runners/base"
rustrunner "github.com/codefly-dev/core/runners/rust"
"github.com/codefly-dev/core/runners/testselection"
"github.com/codefly-dev/core/wool"
runtimev0 "github.com/codefly-dev/core/generated/go/codefly/services/runtime/v0"
)
type Runtime struct {
services.RuntimeServer
*Service
// Native runner environment
runnerEnv *runners.NativeEnvironment
// Running cargo process
runner runners.Proc
testProc runners.Proc
}
func NewRuntime(service *Service) *Runtime {
return &Runtime{Service: service}
}
func (s *Runtime) Load(ctx context.Context, req *runtimev0.LoadRequest) (*runtimev0.LoadResponse, error) {
err := s.Base.Load(ctx, req.Identity, s.Settings)
if err != nil {
return s.Runtime.LoadErrorf(err, "loading base")
}
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
if req.DisableCatch {
s.Wool.DisableCatch()
}
s.Runtime.SetEnvironment(req.Environment)
s.sourceLocation, err = s.LocalDirCreate(ctx, "%s", s.Settings.RustSourceDir())
if err != nil {
return s.Runtime.LoadErrorf(err, "creating source location")
}
if s.Watcher != nil {
s.Watcher.Pause()
}
s.Endpoints, err = s.Base.Service.LoadEndpoints(ctx)
if err != nil {
return s.Runtime.LoadErrorf(err, "loading endpoints")
}
s.RestEndpoint, err = resources.FindRestEndpoint(ctx, s.Endpoints)
if err != nil {
// REST endpoint is optional; log but don't fail.
s.Wool.Debug("no REST endpoint found", wool.ErrField(err))
s.RestEndpoint = nil
}
return s.Runtime.LoadResponse()
}
func (s *Runtime) Init(ctx context.Context, req *runtimev0.InitRequest) (*runtimev0.InitResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
s.Runtime.LogInitRequest(req)
s.Runtime.RuntimeContext = req.RuntimeContext
s.Wool.Forwardf("starting execution environment in %s mode", s.Runtime.RuntimeContext.Kind)
s.EnvironmentVariables.SetRuntimeContext(s.Runtime.RuntimeContext)
s.NetworkMappings = req.ProposedNetworkMappings
// Service's own configuration: configurations/<env>/*.env (incl. *.secret.env)
// → the service's own configured values injected into its environment. Without
// this a service never receives its own config (e.g. provider API keys via
// CODEFLY__SERVICE_SECRET_CONFIGURATION__...). Mirror python-fastapi, which
// already injects req.Configuration first.
err := s.EnvironmentVariables.AddConfigurations(ctx, req.Configuration)
if err != nil {
return s.Runtime.InitError(err)
}
// Project configurations
err = s.EnvironmentVariables.AddConfigurations(ctx, req.WorkspaceConfigurations...)
if err != nil {
return s.Runtime.InitError(err)
}
// Dependency configurations
confs := resources.FilterConfigurations(req.DependenciesConfigurations, s.Runtime.RuntimeContext)
s.Wool.Trace("adding configurations", wool.Field("configurations", resources.MakeManyConfigurationSummary(confs)))
err = s.EnvironmentVariables.AddConfigurations(ctx, confs...)
if err != nil {
return s.Runtime.InitError(err)
}
// Endpoint networking: add our own endpoint addresses as env vars.
if s.RestEndpoint != nil {
net, err := resources.FindNetworkInstanceInNetworkMappings(ctx, s.NetworkMappings, s.RestEndpoint, resources.NewNativeNetworkAccess())
if err != nil {
return s.Runtime.InitError(err)
}
s.Infof("REST will run on %s", net.Address)
nm, err := resources.FindNetworkMapping(ctx, s.NetworkMappings, s.RestEndpoint)
if err != nil {
return s.Runtime.InitError(err)
}
err = s.EnvironmentVariables.AddEndpoints(ctx, []*basev0.NetworkMapping{nm}, resources.NewNativeNetworkAccess())
if err != nil {
return s.Runtime.InitError(err)
}
}
// Hot reload watcher
if s.Settings.HotReload {
s.Wool.Trace("starting hot reload")
dependencies := requirements.Clone()
dependencies.Localize(s.Location)
s.Wool.Trace("setting up code watcher", wool.Field("dep", dependencies.All()))
conf := services.NewWatchConfiguration(dependencies)
err = s.SetupWatcher(ctx, conf, s.EventHandler)
if err != nil {
s.Wool.Warn("error in watcher", wool.ErrField(err))
}
}
if s.Watcher != nil {
s.Watcher.Resume()
}
// Create native runner environment
if s.runnerEnv == nil {
s.runnerEnv, err = runners.NewNativeEnvironment(ctx, s.sourceLocation)
if err != nil {
return s.Runtime.InitErrorf(err, "cannot create native environment")
}
}
s.activeEnv = s.runnerEnv
allEnvs, err := s.EnvironmentVariables.All()
if err != nil {
return s.Runtime.InitErrorf(err, "cannot get environment variables")
}
s.runnerEnv.WithEnvironmentVariables(ctx, allEnvs...)
err = s.runnerEnv.Init(ctx)
if err != nil {
return s.Runtime.InitErrorf(err, "cannot init native environment")
}
s.Wool.Info("successful init of rust runner")
return s.Runtime.InitResponse()
}
func (s *Runtime) Start(ctx context.Context, req *runtimev0.StartRequest) (*runtimev0.StartResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
s.Wool.Forwardf("building and starting rust service...")
// Stop before replacing the runner
if s.runner != nil {
err := s.runner.Stop(ctx)
if err != nil {
return s.Runtime.StartError(err)
}
}
// Add dependency network mappings
err := s.EnvironmentVariables.AddEndpoints(ctx, req.DependenciesNetworkMappings, resources.NetworkAccessFromRuntimeContext(s.Runtime.RuntimeContext))
if err != nil {
return s.Runtime.StartError(err)
}
// Add fixture
s.EnvironmentVariables.SetFixture(req.Fixture)
// Add per-service runtime overrides (--set <service>:KEY=VAL)
s.EnvironmentVariables.AddOverrides(req.GetOverrides())
startEnvs, err := s.EnvironmentVariables.All()
if err != nil {
return s.Runtime.StartErrorf(err, "getting environment variables")
}
runningContext := s.Wool.Inject(context.Background())
var proc runners.Proc
if s.Settings.HotReload {
// Use cargo-watch for hot-reload
proc, err = s.runnerEnv.NewProcess("cargo", "watch", "-x", "run")
} else {
// Use cargo run directly
proc, err = s.runnerEnv.NewProcess("cargo", "run")
}
if err != nil {
return s.Runtime.StartErrorf(err, "creating cargo process")
}
proc.WithEnvironmentVariables(ctx, startEnvs...)
proc.WithOutput(s.Logger)
s.runner = proc
err = s.runner.Start(runningContext)
if err != nil {
if !s.Settings.HotReload {
return s.Runtime.StartErrorf(err, "starting cargo")
}
s.Wool.Info("compile error, waiting for hot-reload")
return s.Runtime.StartResponse()
}
s.Wool.Forwardf("rust service started and running")
return s.Runtime.StartResponse()
}
func (s *Runtime) Build(ctx context.Context, req *runtimev0.BuildRequest) (*runtimev0.BuildResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
s.Infof("running cargo build")
envs, err := s.EnvironmentVariables.All()
if err != nil {
return s.Runtime.BuildErrorf(err, "getting environment variables")
}
proc, err := s.runnerEnv.NewProcess("cargo", "build")
if err != nil {
return s.Runtime.BuildErrorf(err, "creating cargo build process")
}
proc.WithEnvironmentVariables(ctx, envs...)
var output strings.Builder
proc.WithOutput(&output)
err = proc.Run(ctx)
// Compress before the output reaches the model; on failure the cargo errors
// are the payload, and a gRPC error drops the body, so they go in the message.
compressed := llmout.Compress("cargo", []string{"build"}, output.String())
if err != nil {
return s.Runtime.BuildErrorf(err, "cargo build failed:\n%s", compressed)
}
return s.Runtime.BuildResponse(compressed)
}
func (s *Runtime) Test(ctx context.Context, req *runtimev0.TestRequest) (*runtimev0.TestResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
args, err := cargoTestArgs(req)
if err != nil {
return s.Runtime.TestErrorf(err, "invalid Rust test request")
}
s.Wool.Info("running cargo tests",
wool.Field("target", req.GetTarget()),
wool.Field("filters", req.GetFilters()),
wool.Field("suite", req.GetSuite()),
wool.Field("extra_args", req.GetExtraArgs()))
testEnvs, err := s.EnvironmentVariables.All()
if err != nil {
return s.Runtime.TestErrorf(err, "getting environment variables")
}
// Stable Rust rejects libtest's JSON formatter unless the nightly-only
// -Z unstable-options gate is enabled. Process construction cannot detect
// that execution-time failure, so the former fallback was unreachable and
// every stable Cargo suite was reported as failed. Plain cargo test is the
// portable contract; structured event support must use a stable interface
// when one exists rather than silently requiring nightly.
proc, err := s.runnerEnv.NewProcess("cargo", args...)
if err != nil {
return s.Runtime.TestErrorf(err, "creating cargo test process")
}
proc.WithEnvironmentVariables(ctx, testEnvs...)
var output strings.Builder
proc.WithOutput(&output)
started := time.Now()
s.testProc = proc
runErr := proc.Run(ctx)
s.testProc = nil
duration := time.Since(started)
// Compress the cargo test output (failures are the bulk of it) before it
// reaches the model. On failure it carries via the error message, since a
// gRPC error drops the response body.
compressed := llmout.Compress("cargo", args, output.String())
s.Wool.Forwardf("cargo test output:\n%s", compressed)
summary := rustrunner.ParseCargoTest(output.String())
s.Wool.Forwardf("Tests: %s", summary.SummaryLine())
for _, failure := range summary.Failures {
s.Wool.Forwardf("%s", failure)
}
response := rustrunner.ParseCargoTestStructured(output.String()).
ToProtoResponse("cargo-test", req.GetSuite(), duration)
if runErr == nil && len(req.GetFilters()) > 0 && response.GetCounts().GetTotal() == 0 {
runErr = fmt.Errorf("cargo test filter %q matched no tests", req.GetFilters()[0])
}
if runErr != nil {
return response, fmt.Errorf("cargo test failed: %w\n%s", runErr, compressed)
}
return response, nil
}
// cargoTestArgs is the one Rust-native translation boundary for Codefly's
// language-neutral TestRequest. Unsupported semantics fail closed: the agent
// never certifies a broader run because a requested selector was ignored.
func cargoTestArgs(req *runtimev0.TestRequest) ([]string, error) {
if err := testselection.ValidateRequest(req); err != nil {
return nil, err
}
if req.GetSelection() != nil {
return nil, fmt.Errorf("typed Rust test selection is not supported yet")
}
if req.GetFormula() != nil {
return nil, fmt.Errorf("Rust test formulas are not supported")
}
if req.GetRace() {
return nil, fmt.Errorf("Rust cargo test does not support the race option")
}
if req.GetCoverage() {
return nil, fmt.Errorf("Rust cargo test does not provide built-in coverage")
}
if req.GetTimeout() != "" {
return nil, fmt.Errorf("Rust cargo test does not support a portable per-test timeout")
}
if len(req.GetFilters()) > 1 {
return nil, fmt.Errorf(
"stable cargo test accepts one native substring filter; got %d",
len(req.GetFilters()),
)
}
args := []string{"test"}
target := strings.TrimSpace(req.GetTarget())
if target == "" {
args = append(args, "--workspace")
}
switch req.GetSuite() {
case "":
case "unit":
args = append(args, "--lib", "--bins")
case "integration":
args = append(args, "--tests")
default:
return nil, fmt.Errorf("unsupported Rust test suite %q", req.GetSuite())
}
if req.GetVerbose() {
args = append(args, "--verbose")
}
if target != "" {
switch {
case filepath.Base(target) == "Cargo.toml":
args = append(args, "--manifest-path", target)
case strings.Contains(target, "/") || strings.HasPrefix(target, "."):
args = append(args, "--manifest-path", filepath.Join(target, "Cargo.toml"))
default:
args = append(args, "--package", target)
}
}
args = append(args, req.GetExtraArgs()...)
if len(req.GetFilters()) == 1 {
filter := strings.TrimSpace(req.GetFilters()[0])
if filter == "" || strings.HasPrefix(filter, "-") {
return nil, fmt.Errorf("Rust test filter must be a non-empty native substring")
}
args = append(args, filter)
}
return args, nil
}
func (s *Runtime) Information(ctx context.Context, req *runtimev0.InformationRequest) (*runtimev0.InformationResponse, error) {
return s.Runtime.InformationResponse(ctx, req)
}
func (s *Runtime) Stop(ctx context.Context, _ *runtimev0.StopRequest) (*runtimev0.StopResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
s.Wool.Trace("stopping service")
if s.testProc != nil {
s.Wool.Trace("stopping test process")
_ = s.testProc.Stop(ctx)
s.testProc = nil
}
if s.runner != nil {
s.Wool.Trace("stopping runner")
err := s.runner.Stop(ctx)
if err != nil {
return s.Runtime.StopError(err)
}
s.runner = nil
s.Wool.Trace("runner stopped")
}
// Cancel the watcher and let its Start goroutine's deferred close of Events
// run exactly once — Stop must not close Events itself, or it races that
// goroutine into a "close of closed channel" panic.
s.Base.StopWatcher()
s.Wool.Trace("base stopped")
return s.Runtime.StopResponse()
}
func (s *Runtime) Destroy(ctx context.Context, _ *runtimev0.DestroyRequest) (*runtimev0.DestroyResponse, error) {
defer s.Wool.Catch()
ctx = s.Wool.Inject(ctx)
s.Wool.Trace("destroying rust service")
// Stop the native environment if needed
if s.runnerEnv != nil {
_ = s.runnerEnv.Shutdown(ctx)
}
return s.Runtime.DestroyResponse()
}
/* Event handling for hot-reload */
func (s *Runtime) EventHandler(event code.Change) error {
s.Wool.Info("detected change requiring re-build", wool.Field("path", event.Path))
s.Runtime.DesiredStart()
return nil
}