Skip to content

Init improvements#1176

Open
amisevsk wants to merge 2 commits intokitops-ml:mainfrom
amisevsk:init-improvements
Open

Init improvements#1176
amisevsk wants to merge 2 commits intokitops-ml:mainfrom
amisevsk:init-improvements

Conversation

@amisevsk
Copy link
Copy Markdown
Contributor

@amisevsk amisevsk commented May 6, 2026

Description

Add a --depth flag for Kitfile generation (kit init and kit import) that can be used to control how far down a directory tree we traverse before starting to add less fine-grained directory entries. For --depth=0 (the default), behaviour is the same as in the current main branch: all files in the context directory are classified and added to the Kitfile, and any subdirectories are added on a best-effort basis (if we can tell they're all one type, we use that type; otherwise we add a code layer for the directory).

Increasing the depth parameter means files in individual subdirectories are also classified in the same way, so --depth=1 includes files in immediate subdirectories, --depth=2 goes one directory further, etc. Depth can be set to -1 to process all files individually.

This flag can lead to much larger Kitfiles if a lot of individual files are added. However, this behaviour may be desirable in some cases, such as when a directory contains a large number of moderate or large files -- adding them as separate entries means they each appear in their own layer, and prevents the entire directory layer from being invalidated if a single file in the directory is updated.

Additionally, this PR makes one small change to how Kitfiles are generated: when directories are added to the Kitfile, we add a trailing slash to make it clearer that the layer represents a directory versus a regular file.

Linked issues

N/A

AI-Assisted Code

  • This PR contains AI-generated code that I have reviewed and tested
  • I take full responsibility for all code in this PR, regardless of how it was created

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances Kitfile generation for kit init and kit import by introducing a --depth option to control how deeply the directory tree is traversed for file-by-file classification, and by making directory layers explicit via trailing slashes.

Changes:

  • Add a --depth flag to kit init and kit import, threading the value through to Kitfile generation.
  • Refactor Kitfile generation to support depth-based recursion and adjust how unknown files/directories are handled.
  • Append trailing slashes to directory layer paths and update fixtures/tests accordingly.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
testing/testdata/kitfile-generation/test_prompt-handling.yaml Updates expected Kitfile output to include trailing slashes for directory layers.
testing/testdata/kitfile-generation/test_directory-handling.yaml Updates expected Kitfile output to include trailing slashes for directory layers.
pkg/lib/kitfile/generate/skill.go Ensures skill directory prompt paths are emitted with a trailing slash.
pkg/lib/kitfile/generate/skill_test.go Updates tests for new GenerateKitfile signature and trailing-slash directory paths.
pkg/lib/kitfile/generate/generate.go Implements depth support, refactors file/dir processing, and adds withTrailingSlash.
pkg/lib/kitfile/generate/generate_test.go Adds coverage for depth-based generation behavior (including subdir README/LICENSE and model parts).
pkg/cmd/kitinit/cmd.go Adds --depth flag and passes it into Kitfile generation.
pkg/cmd/kitimport/util.go Extends kitfile generation helper to accept depth.
pkg/cmd/kitimport/hfimport.go Passes depth through when generating Kitfiles during HF import.
pkg/cmd/kitimport/gitimport.go Passes depth through when generating Kitfiles during Git import.
pkg/cmd/kitimport/cmd.go Adds --depth flag to the import command options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/lib/kitfile/generate/generate.go Outdated
Comment on lines +120 to +127
if len(unknownFiles) > 5 {
output.Logf(output.LogLevelTrace, "Unrecognized files found in %s; adding catch-all code layer", dir.Path)
kitfile.Code = append(kitfile.Code, artifact.Code{Path: "."})
} else if len(unknownFiles) > 0 {
output.Logf(output.LogLevelTrace, "Unrecognized files found in %s; adding as code layers")
for _, f := range unknownFiles {
kitfile.Code = append(kitfile.Code, artifact.Code{Path: f.Path})
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is acceptable; replacing all detected code files with a catch-all layer is no longer desirable, in my opinon; we should avoid adding an opaque Path: "." layer if we can, as it's hard to reason about what it actually contains.

Comment thread pkg/lib/kitfile/generate/generate.go Outdated
// We can, in the future, recurse deeper into the directory tree here. For now, treat secondary dirs as unknowns
directoryContents[int(fileTypeUnknown)] = append(directoryContents[int(fileTypeUnknown)], subdir.Path)
// We can, in the future, recurse deeper into the directory tree here. For now, treat additional dirs as unknowns
directoryContents[int(fileTypeUnknown)] = append(directoryContents[int(fileTypeUnknown)], filepath.Join(withTrailingSlash(dir.Path), subdir.Path))
Copy link
Copy Markdown
Contributor Author

@amisevsk amisevsk May 6, 2026

Choose a reason for hiding this comment

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

Ah yeah this was me getting confused because the test I had written was invalid :D

Forgot to remove this bit

Comment on lines +462 to +466
func withTrailingSlash(p string) string {
if p == "." || strings.HasSuffix(p, "/") {
return p
}
return p + "/"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Paths should all be normalized during DirectoryListing generation (

Path: filepath.ToSlash(relPath),
) so this should never see a windows-style backslash.

Comment on lines +226 to +227
case fileTypeCode:
kitfile.Code = append(kitfile.Code, artifact.Code{Path: file.Path})
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't currently check all the various source code file extensions but this approach is more correct and would have to be made if we started doing that. Not a bug to exhaust an enum in a switch.

amisevsk added 2 commits May 6, 2026 10:07
Add flag `--depth` to commands that generate Kitfiles. The provided
depth controls how far down a directory structure kit will navigate
before starting to group directories into layers -- for example, with
depth=1, files in immediate subdirectories of the context will be
processed individually and added to the Kitfile as appropriate.

Signed-off-by: Angel Misevski <amisevsk@gmail.com>
Signed-off-by: Angel Misevski <amisevsk@gmail.com>
@amisevsk amisevsk force-pushed the init-improvements branch from 3723a9c to 69ab6ca Compare May 6, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants