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
22 changes: 14 additions & 8 deletions Sources/agentd/ActivitySummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ struct ActivitySummary: Codable, Sendable {
sourceBatchIds: sourceBatchIds.sorted(),
displayIds: displayIds.sorted(),
droppedCounts: dropped,
droppedReasonCounts: droppedReasonCounts.sortedByKey(),
droppedReasonCounts: droppedReasonCounts,
apps: appCounters.map { key, count in
ActivityAppSummary(appName: key.appName, bundleId: key.bundleId, frameCount: count)
}.sorted(),
Expand Down Expand Up @@ -261,14 +261,26 @@ struct ActivitySummary: Codable, Sendable {
}

private static func githubPullRequestLabel(_ raw: String) -> String? {
guard let components = URLComponents(string: raw), components.host == "github.com" else {
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
if let extractedLabel = githubPullRequestExtractedLabel(trimmed) {
return extractedLabel
}

guard let components = URLComponents(string: trimmed), components.host == "github.com" else {
return nil
}
let parts = components.path.split(separator: "/").map(String.init)
guard parts.count >= 4, parts[2] == "pull", let number = Int(parts[3]) else { return nil }
return "\(parts[0])/\(parts[1])#\(number)"
}

private static func githubPullRequestExtractedLabel(_ raw: String) -> String? {
guard let match = raw.wholeMatch(of: #/([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)#([0-9]+)/#) else {
return nil
}
return "\(match.1)/\(match.2)#\(match.3)"
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unanchored regex falsely matches non-PR GitHub URLs

Low Severity

githubPullRequestExtractedLabel uses an unanchored firstMatch regex, and it's tried before URL-based parsing in githubPullRequestLabel. Since githubPullRequestLabel is also called on documentPath values (not just pre-extracted metadata labels), a URL like https://github.com/owner/repo#123 (a repo page with a numeric fragment) would match the substring owner/repo#123 and be returned as a PR label. Previously, URL parsing would have correctly rejected this because the path doesn't contain /pull/.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2087fee. Configure here.


private static func isoDate(_ raw: String) -> Date? {
ISO8601DateFormatter().date(from: raw)
}
Expand Down Expand Up @@ -495,9 +507,3 @@ extension DropCounts {
)
}
}

extension Dictionary where Key == String, Value == Int {
fileprivate func sortedByKey() -> [String: Int] {
Dictionary(uniqueKeysWithValues: sorted { $0.key < $1.key })
}
}
60 changes: 60 additions & 0 deletions Tests/agentdTests/DiagnosticCLITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,66 @@ final class DiagnosticCLITests: XCTestCase {
XCTAssertTrue(markdown.contains("secret.ocrText:openai: 1"))
}

func testActivitySummaryExtractsActivePullRequestLabelMetadata() async throws {
let root = try temporaryDirectory()
defer { try? FileManager.default.removeItem(at: root) }
let now = Date(timeIntervalSince1970: 21_600)
try writeBatch(
ActivitySummaryTests.batch(
id: "batch_label_only",
startedAt: Date(timeIntervalSince1970: 7_000),
endedAt: Date(timeIntervalSince1970: 7_030),
frames: [],
metadata: [
"activePullRequest": "evalops/agentd#113",
"activePullRequest.firstSeenAt": "1970-01-01T01:56:40Z",
"activePullRequest.foregroundSeconds": "45",
]
),
to: root.appendingPathComponent("batch_label_only.json")
)

let summary = try await ActivitySummary.run(
options: ActivityOptions(sinceHours: 6, batchDirectory: root, windowLabel: "6h"),
now: now
)

XCTAssertEqual(summary.artifacts.map(\.label), ["evalops/agentd#113"])
XCTAssertEqual(summary.artifacts.first?.url, "evalops/agentd#113")
XCTAssertEqual(summary.artifacts.first?.foregroundSeconds, 45)
}

func testActivitySummaryIgnoresNonPullRequestGitHubDocumentPath() async throws {
let root = try temporaryDirectory()
defer { try? FileManager.default.removeItem(at: root) }
let now = Date(timeIntervalSince1970: 21_600)
try writeBatch(
ActivitySummaryTests.batch(
id: "batch_non_pr_url",
startedAt: Date(timeIntervalSince1970: 7_000),
endedAt: Date(timeIntervalSince1970: 7_030),
frames: [
ActivitySummaryTests.frame(
appName: "Google Chrome",
bundleId: "com.google.Chrome",
windowTitle: "cerebro",
documentPath: "https://github.com/evalops/cerebro#123",
capturedAt: Date(timeIntervalSince1970: 7_000),
displayId: 42
)
]
),
to: root.appendingPathComponent("batch_non_pr_url.json")
)

let summary = try await ActivitySummary.run(
options: ActivityOptions(sinceHours: 6, batchDirectory: root, windowLabel: "6h"),
now: now
)

XCTAssertTrue(summary.artifacts.isEmpty)
}

func testActivitySummaryArtifactsWriteInstructionsAndResource() async throws {
let batchRoot = try temporaryDirectory()
let artifactRoot = try temporaryDirectory()
Expand Down