Skip to content
Merged
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
1 change: 1 addition & 0 deletions changelog.d/changed-activity-parallel-tracks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Activity Monitor now shows independent CI and Review columns on each PR, preserving current-head required App/check identity, freshness and each reviewer's own completion receipt. Source repair returns visually to PR in progress only with verified successful source-edit evidence; assignments, administrative writes and owner/adoption waits stay explicit without changing native stages or merge authority.
1 change: 1 addition & 0 deletions changelog.d/security-activity-code-work-git.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Harden Activity Monitor source-work verification with credential-free, bounded, read-only Git inspection that cannot execute workspace hooks, filters or helpers; unsupported normalization and unstable repository evidence stay unknown.
20 changes: 14 additions & 6 deletions src/crux/agent/busy_visibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ const (
const copilotQuestionFormMessage = "Copilot CLI asked the user a question; no human is attached — dismissed with Escape so the kick can be delivered"

type transcriptActivityScan struct {
name string
uid int
name string
uid int
target kickTranscriptTarget
}

type transcriptActivityResult struct {
name string
at time.Time
ok bool
name string
at time.Time
ok bool
codeWork *crux.CodeWork
since time.Time
}

// transcriptSessionCache remembers, per agent, the events.jsonl mtime last
Expand Down Expand Up @@ -324,7 +327,7 @@ func (m *Manager) pendingTranscriptActivityScansLocked(now time.Time) []transcri
if !agent.lastTranscriptScanAt.IsZero() && now.Sub(agent.lastTranscriptScanAt) < transcriptLivenessScanInterval {
continue
}
scans = append(scans, transcriptActivityScan{name: agent.Name, uid: agent.UID})
scans = append(scans, transcriptActivityScan{name: agent.Name, uid: agent.UID, target: m.kickTranscriptTargetLocked(agent)})
}
return scans
}
Expand Down Expand Up @@ -367,6 +370,8 @@ func scanTranscriptActivity(scans []transcriptActivityScan, caches map[string]*t
defer func() { <-sem }()
at, ok := scanAgentTranscript(scan, perAgent[i], now)
results[i] = transcriptActivityResult{name: scan.name, at: at, ok: ok}
results[i].codeWork = scanCodeWork(scan.target, perAgent[i], now)
results[i].since = scan.target.since
}()
}
wg.Wait()
Expand All @@ -385,6 +390,9 @@ func (m *Manager) applyTranscriptActivityResultsLocked(results []transcriptActiv
if result.ok {
agent.LastTranscriptActivity = result.at
}
if agent.LastKick != nil && agent.LastKick.Equal(result.since) {
agent.CodeWork = cloneCodeWork(result.codeWork)
}
}
}

Expand Down
215 changes: 215 additions & 0 deletions src/crux/agent/code_work.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package agent

import (
"context"
"encoding/json"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"time"

"github.com/hivecommons/hive/crux"
"github.com/hivecommons/hive/crux/pushbroker"
)

const (
codeWorkTranscriptLimit = 8 << 20
codeWorkFreshWindow = 2 * time.Minute
codeWorkGitTimeout = 2 * time.Second
)

type sourceEdit struct {
path string
at time.Time
started time.Time
}

type sourceEditCall struct {
paths []string
at time.Time
}

// ObserveCodeWorkTranscript reads explicit successful edit results, never
// prose or generic shell intentions, then verifies a real source change.
// Unsupported/partial transcripts and unverifiable worktrees stay unknown.
func ObserveCodeWorkTranscript(r io.Reader, workDir string, since, now time.Time) *crux.CodeWork {
if since.IsZero() {
return nil
}
calls := map[string]sourceEditCall{}
var edits []sourceEdit
err := forEachTranscriptLine(io.LimitReader(r, codeWorkTranscriptLimit), func(line []byte) error {
var event copilotTranscriptEvent
if err := json.Unmarshal(line, &event); err != nil {
return err
}
at := parseTranscriptTime(event.Timestamp)
if at.IsZero() || at.Before(since) || at.After(now) {
return nil
}
switch event.Type {
case "assistant.message":
for _, request := range event.Data.ToolRequests {
if request.ToolCallID == "" {
continue
}
calls[request.ToolCallID] = sourceEditCall{paths: sourceEditPaths(request.Name, request.Arguments), at: at}
}
case "tool.execution_start":
if event.Data.ToolCallID == "" {
return nil
}
calls[event.Data.ToolCallID] = sourceEditCall{paths: sourceEditPaths(event.Data.ToolName, event.Data.Arguments), at: at}
case "tool.execution_complete":
if event.Data.ToolCallID == "" || event.Data.Success == nil || !*event.Data.Success ||
transcriptText(event.Data.Error) != "" || now.Sub(at) > codeWorkFreshWindow {
return nil
}
call := calls[event.Data.ToolCallID]
for _, path := range call.paths {
edits = append(edits, sourceEdit{path: path, at: at, started: call.at})
}
delete(calls, event.Data.ToolCallID)
}
return nil
})
if err != nil {
return nil
}
for i := len(edits) - 1; i >= 0; i-- {
if observation := verifySourceEdit(edits[i], now); observation != nil {
observation.KickAt = since
return observation
}
}
return nil
}

func sourceEditPaths(name string, arguments json.RawMessage) []string {
var paths []string
switch name {
case "edit", "create", "Edit", "Write":
var args struct {
Path string `json:"path"`
FilePath string `json:"file_path"`
}
if json.Unmarshal(arguments, &args) != nil {
return nil
}
path := args.Path
if path == "" {
path = args.FilePath
}
if filepath.IsAbs(path) {
paths = append(paths, filepath.Clean(path))
}
case "apply_patch":
var patch string
if json.Unmarshal(arguments, &patch) != nil {
var args struct {
Input string `json:"input"`
Patch string `json:"patch"`
}
if json.Unmarshal(arguments, &args) != nil {
return nil
}
patch = args.Input
if patch == "" {
patch = args.Patch
}
}
for _, line := range strings.Split(patch, "\n") {
for _, prefix := range []string{"*** Update File: ", "*** Add File: "} {
if strings.HasPrefix(line, prefix) {
path := strings.TrimPrefix(line, prefix)
// A tool may have changed CWD since launch. Relative paths
// are intentionally not attributed using a guessed directory.
if filepath.IsAbs(path) {
paths = append(paths, filepath.Clean(path))
}
}
}
}
}
return paths
}

func verifySourceEdit(edit sourceEdit, now time.Time) *crux.CodeWork {
info, err := os.Lstat(edit.path)
if err != nil || !info.Mode().IsRegular() || info.ModTime().Before(edit.started) || info.ModTime().After(edit.at) || info.ModTime().After(now) {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), codeWorkGitTimeout)
defer cancel()
observation, err := pushbroker.ObserveWorkspaceFile(ctx, edit.path)
if err != nil || observation == nil {
return nil
}
origin := observation.Origin
if strings.HasPrefix(origin, "git@") {
parts := strings.SplitN(origin, ":", 2)
if len(parts) != 2 {
return nil
}
origin = "ssh://" + strings.TrimPrefix(parts[0], "git@") + "/" + parts[1]
}
remote, err := url.Parse(origin)
if err != nil || remote.Hostname() == "" {
return nil
}
repo := strings.TrimSuffix(strings.Trim(remote.Path, "/"), ".git")
if len(strings.Split(repo, "/")) != 2 {
return nil
}
after, err := os.Lstat(edit.path)
if err != nil || !os.SameFile(info, after) || info.Size() != after.Size() || !info.ModTime().Equal(after.ModTime()) {
return nil
}
return &crux.CodeWork{Repo: repo, Head: observation.Head, Path: observation.Path, Source: crux.CodeWorkSource, At: edit.at, ObservedAt: now}
}

func cloneCodeWork(observation *crux.CodeWork) *crux.CodeWork {
if observation == nil {
return nil
}
copy := *observation
return &copy
}

func scanCodeWork(target kickTranscriptTarget, cache *transcriptSessionCache, now time.Time) *crux.CodeWork {
if target.backend != "copilot" || target.since.IsZero() || cache == nil {
return nil
}
// Reuse the existing liveness scan's session index; never repeat the
// potentially large resume-handle walk or scan on dashboard reads.
name := ""
var newest time.Time
ambiguous := false
for session, at := range cache.sessions {
if at.Before(target.since) || at.After(now) || now.Sub(at) > codeWorkFreshWindow {
continue
}
if at.After(newest) {
name, newest, ambiguous = session, at, false
} else if at.Equal(newest) {
ambiguous = true
}
}
if name == "" || ambiguous {
return nil
}
path := filepath.Join(AgentHome(target.agentName, target.uid, "copilot"), ".copilot", "session-state", name, "events.jsonl")
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
info, err := file.Stat()
if err != nil || info.Size() > codeWorkTranscriptLimit {
return nil
}
return ObserveCodeWorkTranscript(file, target.workDir, target.since, now)
}
Loading
Loading