-
Notifications
You must be signed in to change notification settings - Fork 94
feat: add project intelligence — persistent cross-session repo knowledge #1328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yossiovadia
wants to merge
14
commits into
ambient-code:main
Choose a base branch
from
yossiovadia:feat/project-intelligence-memory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d55d474
feat: add project intelligence — persistent cross-session repo knowledge
yossiovadia a9d9cea
chore: remove dead code and simplify intelligence API calls
yossiovadia 2d07c72
fix: address review bugs — race condition, error handling, test stubs
yossiovadia 79c7d58
fix: restore missing closing brace in sanitizeK8sName after rebase
yossiovadia 3055875
fix: auto-analysis falls back to ANTHROPIC_API_KEY when Vertex unavai…
4a71cd7
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 99214b3
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 6feea8b
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 0bf07cf
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 09e2258
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 2b708ea
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] c524bdc
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 5f21146
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] 1c66271
Merge branch 'main' into feat/project-intelligence-memory
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "gorm.io/gorm/clause" | ||
|
|
||
| "github.com/openshift-online/rh-trex-ai/pkg/db" | ||
| ) | ||
|
|
||
| type RepoEventDao interface { | ||
| Get(ctx context.Context, id string) (*RepoEvent, error) | ||
| Create(ctx context.Context, re *RepoEvent) (*RepoEvent, error) | ||
| FindByIDs(ctx context.Context, ids []string) (RepoEventList, error) | ||
| All(ctx context.Context) (RepoEventList, error) | ||
| } | ||
|
|
||
| var _ RepoEventDao = &sqlRepoEventDao{} | ||
|
|
||
| type sqlRepoEventDao struct { | ||
| sessionFactory *db.SessionFactory | ||
| } | ||
|
|
||
| func NewRepoEventDao(sessionFactory *db.SessionFactory) RepoEventDao { | ||
| return &sqlRepoEventDao{sessionFactory: sessionFactory} | ||
| } | ||
|
|
||
| func (d *sqlRepoEventDao) Get(ctx context.Context, id string) (*RepoEvent, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| var re RepoEvent | ||
| if err := g2.Take(&re, "id = ?", id).Error; err != nil { | ||
| return nil, err | ||
| } | ||
| return &re, nil | ||
| } | ||
|
|
||
| func (d *sqlRepoEventDao) Create(ctx context.Context, re *RepoEvent) (*RepoEvent, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| if err := g2.Omit(clause.Associations).Create(re).Error; err != nil { | ||
| db.MarkForRollback(ctx, err) | ||
| return nil, err | ||
| } | ||
| return re, nil | ||
| } | ||
|
|
||
| func (d *sqlRepoEventDao) FindByIDs(ctx context.Context, ids []string) (RepoEventList, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| items := RepoEventList{} | ||
| if err := g2.Where("id in (?)", ids).Find(&items).Error; err != nil { | ||
| return nil, err | ||
| } | ||
| return items, nil | ||
| } | ||
|
|
||
| func (d *sqlRepoEventDao) All(ctx context.Context) (RepoEventList, error) { | ||
| g2 := (*d.sessionFactory).New(ctx) | ||
| items := RepoEventList{} | ||
| if err := g2.Find(&items).Error; err != nil { | ||
| return nil, err | ||
| } | ||
| return items, nil | ||
| } |
72 changes: 72 additions & 0 deletions
72
components/ambient-api-server/plugins/repoEvents/handler.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/mux" | ||
|
|
||
| "github.com/openshift-online/rh-trex-ai/pkg/errors" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/handlers" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/services" | ||
|
|
||
| "github.com/ambient-code/platform/components/ambient-api-server/plugins/common" | ||
| ) | ||
|
|
||
| type repoEventHandler struct { | ||
| service RepoEventService | ||
| generic services.GenericService | ||
| } | ||
|
|
||
| func NewRepoEventHandler(svc RepoEventService, generic services.GenericService) *repoEventHandler { | ||
| return &repoEventHandler{ | ||
| service: svc, | ||
| generic: generic, | ||
| } | ||
| } | ||
|
|
||
| func (h repoEventHandler) List(w http.ResponseWriter, r *http.Request) { | ||
| cfg := &handlers.HandlerConfig{ | ||
| Action: func() (interface{}, *errors.ServiceError) { | ||
| ctx := r.Context() | ||
| listArgs := services.NewListArguments(r.URL.Query()) | ||
|
|
||
| if serr := common.ApplyProjectScope(r, listArgs); serr != nil { | ||
| return nil, serr | ||
| } | ||
|
|
||
| var items []RepoEvent | ||
| paging, err := h.generic.List(ctx, "id", listArgs, &items) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| list := RepoEventListAPI{ | ||
| Kind: "RepoEventList", | ||
| Page: int32(paging.Page), | ||
| Size: int32(paging.Size), | ||
| Total: int32(paging.Total), | ||
| Items: []RepoEventAPI{}, | ||
| } | ||
| for _, item := range items { | ||
| list.Items = append(list.Items, PresentRepoEvent(&item)) | ||
| } | ||
| return list, nil | ||
| }, | ||
| } | ||
| handlers.HandleList(w, r, cfg) | ||
| } | ||
|
|
||
| func (h repoEventHandler) Get(w http.ResponseWriter, r *http.Request) { | ||
| cfg := &handlers.HandlerConfig{ | ||
| Action: func() (interface{}, *errors.ServiceError) { | ||
| id := mux.Vars(r)["id"] | ||
| ctx := r.Context() | ||
| re, err := h.service.Get(ctx, id) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return PresentRepoEvent(re), nil | ||
| }, | ||
| } | ||
| handlers.HandleGet(w, r, cfg) | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
components/ambient-api-server/plugins/repoEvents/migration.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "gorm.io/gorm" | ||
|
|
||
| "github.com/go-gormigrate/gormigrate/v2" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/db" | ||
| ) | ||
|
|
||
| func migration() *gormigrate.Migration { | ||
| type RepoEvent struct { | ||
| db.Model | ||
| ResourceType string `gorm:"not null"` | ||
| ResourceID string `gorm:"not null"` | ||
| Action string `gorm:"not null"` | ||
| ActorType string `gorm:"not null"` | ||
| ActorID string `gorm:"not null"` | ||
| ProjectID string `gorm:"not null"` | ||
| Reason *string | ||
| Diff *string `gorm:"type:text"` | ||
| } | ||
|
|
||
| return &gormigrate.Migration{ | ||
| ID: "202604091202", | ||
| Migrate: func(tx *gorm.DB) error { | ||
| if err := tx.AutoMigrate(&RepoEvent{}); err != nil { | ||
| return err | ||
| } | ||
| stmts := []string{ | ||
| `CREATE INDEX IF NOT EXISTS idx_re_resource_type ON repo_events(resource_type)`, | ||
| `CREATE INDEX IF NOT EXISTS idx_re_resource_id ON repo_events(resource_id)`, | ||
| `CREATE INDEX IF NOT EXISTS idx_re_project_id ON repo_events(project_id)`, | ||
| } | ||
| for _, s := range stmts { | ||
| if err := tx.Exec(s).Error; err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| Rollback: func(tx *gorm.DB) error { | ||
| return tx.Migrator().DropTable("repo_events") | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "github.com/openshift-online/rh-trex-ai/pkg/api" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| type RepoEvent struct { | ||
| api.Meta | ||
|
|
||
| // What changed | ||
| ResourceType string `json:"resource_type" gorm:"not null;index"` | ||
| ResourceID string `json:"resource_id" gorm:"not null;index"` | ||
| Action string `json:"action" gorm:"not null"` | ||
|
|
||
| // Who | ||
| ActorType string `json:"actor_type" gorm:"not null"` | ||
| ActorID string `json:"actor_id" gorm:"not null"` | ||
|
|
||
| // Context | ||
| ProjectID string `json:"project_id" gorm:"not null;index"` | ||
| Reason *string `json:"reason,omitempty"` | ||
| Diff *string `json:"diff,omitempty" gorm:"type:text"` | ||
| } | ||
|
|
||
| type RepoEventList []*RepoEvent | ||
| type RepoEventIndex map[string]*RepoEvent | ||
|
|
||
| func (l RepoEventList) Index() RepoEventIndex { | ||
| index := RepoEventIndex{} | ||
| for _, o := range l { | ||
| index[o.ID] = o | ||
| } | ||
| return index | ||
| } | ||
|
|
||
| func (d *RepoEvent) BeforeCreate(tx *gorm.DB) error { | ||
| d.ID = api.NewID() | ||
| return nil | ||
| } |
65 changes: 65 additions & 0 deletions
65
components/ambient-api-server/plugins/repoEvents/plugin.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/mux" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/api/presenters" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/auth" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/db" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/environments" | ||
| "github.com/openshift-online/rh-trex-ai/pkg/registry" | ||
| pkgserver "github.com/openshift-online/rh-trex-ai/pkg/server" | ||
| "github.com/openshift-online/rh-trex-ai/plugins/generic" | ||
|
|
||
| pkgrbac "github.com/ambient-code/platform/components/ambient-api-server/plugins/rbac" | ||
| ) | ||
|
|
||
| type ServiceLocator func() RepoEventService | ||
|
|
||
| func NewServiceLocator(env *environments.Env) ServiceLocator { | ||
| return func() RepoEventService { | ||
| return NewRepoEventService( | ||
| NewRepoEventDao(&env.Database.SessionFactory), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func Service(s *environments.Services) RepoEventService { | ||
| if s == nil { | ||
| return nil | ||
| } | ||
| if obj := s.GetService("RepoEvents"); obj != nil { | ||
| locator := obj.(ServiceLocator) | ||
| return locator() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func init() { | ||
| registry.RegisterService("RepoEvents", func(env interface{}) interface{} { | ||
| return NewServiceLocator(env.(*environments.Env)) | ||
| }) | ||
|
|
||
| pkgserver.RegisterRoutes("repo_events", func(apiV1Router *mux.Router, services pkgserver.ServicesInterface, authMiddleware environments.JWTMiddleware, authzMiddleware auth.AuthorizationMiddleware) { | ||
| envServices := services.(*environments.Services) | ||
| if dbAuthz := pkgrbac.Middleware(envServices); dbAuthz != nil { | ||
| authzMiddleware = dbAuthz | ||
| } | ||
| svc := Service(envServices) | ||
| handler := NewRepoEventHandler(svc, generic.Service(envServices)) | ||
|
|
||
| router := apiV1Router.PathPrefix("/repo_events").Subrouter() | ||
| router.HandleFunc("", handler.List).Methods(http.MethodGet) | ||
| router.HandleFunc("/{id}", handler.Get).Methods(http.MethodGet) | ||
| router.Use(authMiddleware.AuthenticateAccountJWT) | ||
| router.Use(authzMiddleware.AuthorizeApi) | ||
| }) | ||
|
|
||
| presenters.RegisterPath(RepoEvent{}, "repo_events") | ||
| presenters.RegisterPath(&RepoEvent{}, "repo_events") | ||
| presenters.RegisterKind(RepoEvent{}, "RepoEvent") | ||
| presenters.RegisterKind(&RepoEvent{}, "RepoEvent") | ||
|
|
||
| db.RegisterMigration(migration()) | ||
| } |
54 changes: 54 additions & 0 deletions
54
components/ambient-api-server/plugins/repoEvents/presenter.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package repoEvents | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/openshift-online/rh-trex-ai/pkg/api/presenters" | ||
| ) | ||
|
|
||
| type RepoEventAPI struct { | ||
| ID *string `json:"id,omitempty"` | ||
| Kind *string `json:"kind,omitempty"` | ||
| Href *string `json:"href,omitempty"` | ||
| CreatedAt *time.Time `json:"created_at,omitempty"` | ||
| UpdatedAt *time.Time `json:"updated_at,omitempty"` | ||
|
|
||
| ResourceType string `json:"resource_type"` | ||
| ResourceID string `json:"resource_id"` | ||
| Action string `json:"action"` | ||
| ActorType string `json:"actor_type"` | ||
| ActorID string `json:"actor_id"` | ||
| ProjectID string `json:"project_id"` | ||
| Reason *string `json:"reason,omitempty"` | ||
| Diff *string `json:"diff,omitempty"` | ||
| } | ||
|
|
||
| type RepoEventListAPI struct { | ||
| Kind string `json:"kind"` | ||
| Page int32 `json:"page"` | ||
| Size int32 `json:"size"` | ||
| Total int32 `json:"total"` | ||
| Items []RepoEventAPI `json:"items"` | ||
| } | ||
|
|
||
| func ptrTime(v time.Time) *time.Time { return &v } | ||
|
|
||
| func PresentRepoEvent(re *RepoEvent) RepoEventAPI { | ||
| ref := presenters.PresentReference(re.ID, re) | ||
| return RepoEventAPI{ | ||
| ID: ref.Id, | ||
| Kind: ref.Kind, | ||
| Href: ref.Href, | ||
| CreatedAt: ptrTime(re.CreatedAt), | ||
| UpdatedAt: ptrTime(re.UpdatedAt), | ||
|
|
||
| ResourceType: re.ResourceType, | ||
| ResourceID: re.ResourceID, | ||
| Action: re.Action, | ||
| ActorType: re.ActorType, | ||
| ActorID: re.ActorID, | ||
| ProjectID: re.ProjectID, | ||
| Reason: re.Reason, | ||
| Diff: re.Diff, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scope
Getby project before returning repo events.ListappliesApplyProjectScope, butGetfetches by ID only. SinceRepoEventAPIexposes project/resource/actor details, this can leak cross-project events if a caller can guess or obtain an ID.Suggested direction
func (h repoEventHandler) Get(w http.ResponseWriter, r *http.Request) { cfg := &handlers.HandlerConfig{ Action: func() (interface{}, *errors.ServiceError) { id := mux.Vars(r)["id"] ctx := r.Context() re, err := h.service.Get(ctx, id) if err != nil { return nil, err } + projectID := r.URL.Query().Get("project_id") + if projectID == "" { + projectID = r.Header.Get("X-Ambient-Project") + } + if projectID != "" && re.ProjectID != projectID { + return nil, errors.NotFound("RepoEvent", id) + } return PresentRepoEvent(re), nil }, }📝 Committable suggestion
🤖 Prompt for AI Agents