Skip to content

Commit d4dcee2

Browse files
fix(repos): label deferred symlink content accurately
Report contentless large symlink targets as not returned while preserving their ResourceLink and requested-path identity. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21420b11-5dae-49b6-ac77-965faec7f88b
1 parent 783ff28 commit d4dcee2

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

pkg/github/repositories.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,16 +1092,14 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
10921092
// GetContent when the API returns null content with a
10931093
// base64 encoding field, and to avoid DetectContentType
10941094
// misclassifying them as binary.
1095-
if fileSize == 0 && read.ContentAvailable {
1095+
if read.ContentAvailable && len(read.Content) == 0 {
10961096
result := &mcp.ResourceContents{
10971097
URI: resourceURI,
10981098
Text: "",
10991099
MIMEType: "text/plain",
11001100
}
11011101
message := fmt.Sprintf("successfully downloaded empty file (SHA: %s)%s", fileSHA, successNote)
1102-
if read.Metadata != nil {
1103-
message = marshalRepositoryPathMetadata(read.Metadata, "dereferenced_target", successNote)
1104-
}
1102+
message = repositoryReadMessage(read, message, successNote)
11051103
return attachIFC(utils.NewToolResultResource(message, result)), nil, nil
11061104
}
11071105

@@ -1117,9 +1115,9 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
11171115
message := fmt.Sprintf("File %s is too large to display (%d bytes). Use the download URL to fetch the content: %s (SHA: %s)%s",
11181116
path, fileSize, fileContent.GetDownloadURL(), fileSHA, successNote)
11191117
if read.Metadata != nil {
1120-
message = marshalRepositoryPathMetadata(read.Metadata, "dereferenced_target", successNote)
11211118
resourceLink.Title = fmt.Sprintf("Dereferenced target %s via symlink %s", read.Metadata.ResolvedTargetPath, path)
11221119
}
1120+
message = repositoryReadMessage(read, message, successNote)
11231121
return attachIFC(utils.NewToolResultResourceLink(
11241122
message,
11251123
resourceLink)), nil, nil
@@ -1149,9 +1147,7 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
11491147
MIMEType: contentType,
11501148
}
11511149
message := fmt.Sprintf("successfully downloaded text file (SHA: %s)%s", fileSHA, successNote)
1152-
if read.Metadata != nil {
1153-
message = marshalRepositoryPathMetadata(read.Metadata, "dereferenced_target", successNote)
1154-
}
1150+
message = repositoryReadMessage(read, message, successNote)
11551151
return attachIFC(utils.NewToolResultResource(message, result)), nil, nil
11561152
}
11571153

@@ -1161,9 +1157,7 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
11611157
MIMEType: contentType,
11621158
}
11631159
message := fmt.Sprintf("successfully downloaded binary file (SHA: %s)%s", fileSHA, successNote)
1164-
if read.Metadata != nil {
1165-
message = marshalRepositoryPathMetadata(read.Metadata, "dereferenced_target", successNote)
1166-
}
1160+
message = repositoryReadMessage(read, message, successNote)
11671161
return attachIFC(utils.NewToolResultResource(message, result)), nil, nil
11681162
} else if dirContent != nil {
11691163
// file content or file SHA is nil which means it's a directory

pkg/github/repositories_helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ func marshalRepositoryPathMetadata(metadata *repositoryPathMetadata, content, no
270270
return string(payload)
271271
}
272272

273+
func repositoryReadMessage(read *repositoryFileRead, fallback, note string) string {
274+
if read.Metadata == nil {
275+
return fallback
276+
}
277+
content := "dereferenced_target"
278+
if !read.ContentAvailable {
279+
content = "not_returned"
280+
}
281+
return marshalRepositoryPathMetadata(read.Metadata, content, note)
282+
}
283+
273284
func symlinkTargetAtPath(ctx context.Context, client *github.Client, owner, repo, treeish, path string) (string, bool, *github.Response, error) {
274285
entry, resp, err := getTreeEntry(ctx, client, owner, repo, treeish, path)
275286
if err != nil {

pkg/github/repositories_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ func Test_GetFileContents_ContentlessRequestCounts(t *testing.T) {
759759
assert.Equal(t, 4, requests)
760760
metadata := repositoryPathMetadataFromResult(t, result)
761761
assert.Equal(t, "target.bin", metadata.ResolvedTargetPath)
762-
assert.Equal(t, "dereferenced_target", metadata.Content)
762+
assert.Equal(t, "not_returned", metadata.Content)
763763
})
764764

765765
t.Run("truncated tree fails closed", func(t *testing.T) {

0 commit comments

Comments
 (0)