Skip to content

Commit 87d51ee

Browse files
committed
refactor(sim-cli): declare data-keyed output per command, share the tool code-path rule
- workflows outline declares dataKeys in its contract instead of the renderer guessing from a record's shape, so a resource that only holds nested objects keeps its humanized labels. - toolOfCodeFile maps tools/<key>.js to its declaration once, for the reader and the apply's warning. - check:sim-cli-bundle builds its four findings through one helper.
1 parent 23d3b81 commit 87d51ee

7 files changed

Lines changed: 76 additions & 65 deletions

File tree

‎packages/sim-cli/src/commands/protocol/workspace-source-apply.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
ONLY_KINDS_HELP,
8181
parseKinds,
8282
readWorkspaceDir,
83+
toolOfCodeFile,
8384
type WorkspaceSubmission,
8485
} from './workspace-source-dir'
8586

@@ -354,8 +355,9 @@ async function deletedRecordedPaths(
354355
/**
355356
* The text files the directory sent for a resource the apply reprinted that the reprint no longer
356357
* holds, such as a tool whose code the server keeps as `stored()` (a credential in it), reprinted
357-
* without `tools/<key>.js`. Workflow folders keep files no lock named, so their function files are
358-
* pruned by lock instead ({@link orphanedWorkflowFiles}); blobs are never reprinted.
358+
* without `tools/<key>.js` ({@link saveApplied} keeps that one on disk). Workflow folders keep
359+
* files no lock named, so their function files are pruned by lock instead
360+
* ({@link orphanedWorkflowFiles}); blobs are never reprinted.
359361
*/
360362
function droppedResourceFiles(
361363
sent: Readonly<Record<string, string>>,
@@ -397,15 +399,18 @@ async function saveApplied(
397399
records: ReadonlyArray<readonly [string, string]>
398400
): Promise<void> {
399401
assertWorkspacePaths(Object.keys(data.files))
400-
const dropped = droppedResourceFiles(submission.files, data.files)
401-
const toolCode = dropped.filter((path) => kindOfPath(path) === 'custom-tool')
402-
const orphaned = [
403-
...(await orphanedWorkflowFiles(dir, data.files, written.digests)),
404-
...dropped.filter((path) => kindOfPath(path) !== 'custom-tool'),
405-
]
402+
const toolCode: Array<{ path: string; declaration: string }> = []
403+
const orphaned = await orphanedWorkflowFiles(dir, data.files, written.digests)
404+
for (const path of droppedResourceFiles(submission.files, data.files)) {
405+
const tool = toolOfCodeFile(path)
406+
if (tool) toolCode.push({ path, declaration: tool.declaration })
407+
else orphaned.push(path)
408+
}
406409
const kept = { ...written.digests, ...appliedBlobDigests(submission, data.base) }
407410
const deleted = await deletedRecordedPaths(dir, written.digests, submission.kinds, data.base)
408-
for (const path of [...orphaned, ...deleted, ...toolCode]) delete kept[path]
411+
for (const path of [...orphaned, ...deleted, ...toolCode.map(({ path }) => path)]) {
412+
delete kept[path]
413+
}
409414
const record = writtenRecord(
410415
data.files,
411416
kept,
@@ -420,9 +425,9 @@ async function saveApplied(
420425
],
421426
remove: [...orphaned, ...removedWorkflowRecords(deleted)],
422427
})
423-
for (const path of toolCode) {
428+
for (const { path, declaration } of toolCode) {
424429
printError(
425-
`warning: ${path} is no longer named by ${path.replace(/\.js$/, '.ts')} (its code is now kept as stored() or printed inline), so it was left on disk as written and no apply sends it: delete it, or name it again with codeFile(). Never commit one that holds a credential.`
430+
`warning: ${path} is no longer named by ${declaration} (its code is now kept as stored() or printed inline), so it was left on disk as written and no apply sends it: delete it, or name it again with codeFile(). Never commit one that holds a credential.`
426431
)
427432
}
428433
}

‎packages/sim-cli/src/commands/protocol/workspace-source-dir.ts‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -566,20 +566,26 @@ export interface WorkspaceSubmission {
566566
hashed: ReadonlyMap<string, StatDigest>
567567
}
568568

569+
/** The tool a code file (`tools/<key>.js`) belongs to and its declaration path; `null` for any other path. */
570+
export function toolOfCodeFile(path: string): { key: string; declaration: string } | null {
571+
if (kindOfPath(path) !== 'custom-tool') return null
572+
const [key] = toolCodec.group([path]).groups.keys()
573+
if (key === undefined) return null
574+
const declaration = toolDeclarationPath(key)
575+
return path === declaration ? null : { key, declaration }
576+
}
577+
569578
/**
570-
* Whether `path` is a tool's code file (`tools/<key>.js`) that the tool's declaration does not
571-
* name: the declaration reads whole without it (its code is `stored()` or inline), so the server
572-
* would only refuse it as unused (`E_TOOL_CODE`). One with no declaration is still sent, so the
573-
* server names what is missing.
579+
* Whether `path` is a tool's code file that the tool's declaration does not name: the declaration
580+
* reads whole without it (its code is `stored()` or inline), so the server would only refuse it as
581+
* unused (`E_TOOL_CODE`). One with no declaration is still sent, so the server names what is
582+
* missing.
574583
*/
575584
function isUnnamedToolCode(files: Readonly<Record<string, string>>, path: string): boolean {
576-
if (kindOfPath(path) !== 'custom-tool') return false
577-
const [key] = KIND_LAYOUTS['custom-tool'].group([path]).groups.keys()
578-
if (key === undefined) return false
579-
const declaration = toolDeclarationPath(key)
580-
const text = files[declaration]
581-
if (path === declaration || text === undefined) return false
582-
return toolCodec.parse(key, { [declaration]: text }).decl !== undefined
585+
const tool = toolOfCodeFile(path)
586+
const text = tool ? files[tool.declaration] : undefined
587+
if (!tool || text === undefined) return false
588+
return toolCodec.parse(tool.key, { [tool.declaration]: text }, []).decl !== undefined
583589
}
584590

585591
/**

‎packages/sim-cli/src/contract/commands.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ export const CLI_CONTRACT: CliContract = {
638638
getWorkflowOutline: {
639639
command: 'workflows outline',
640640
describe: 'Show a workflow’s draft as a compact read-only outline',
641+
dataKeys: true,
641642
},
642643
getWorkflowSource: {
643644
command: 'workflows source get',

‎packages/sim-cli/src/contract/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ export interface CommandSpec {
348348
* column. Only meaningful when `columns` is absent.
349349
*/
350350
expand?: string
351+
/**
352+
* The record's keys are data, not field names, as in `workflows outline`'s blocks keyed by block
353+
* id: the display formats print each key as sent rather than humanized, so it can be copied back.
354+
*/
355+
dataKeys?: true
351356
/**
352357
* The response IS a document, not a record to look at.
353358
*

‎packages/sim-cli/src/runtime/result.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('a record keyed by data rather than by field names', () => {
5353
const outline = { blocks: { [blockId]: { type: 'agent', name: 'Agent 1', enabled: true } } }
5454

5555
it('labels each entry with its key exactly as the API sent it', () => {
56-
renderResult('getWorkflowOutline', 'text', outline, {})
56+
renderResult('getWorkflowOutline', 'text', outline, CLI_CONTRACT.getWorkflowOutline ?? {})
5757
expect(logged[0].split('\t')[0]).toBe(blockId)
5858
})
5959
})

‎packages/sim-cli/src/runtime/result.ts‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,10 @@ function inferHeader(key: string, format: ColumnSpec['format'] | null): string {
210210
return humanizeKey(trimmed || key)
211211
}
212212

213-
/**
214-
* Whether a record is a map keyed by data, such as `workflows outline`'s blocks keyed by block id,
215-
* rather than a resource keyed by field names: every value is itself an object. Its keys are
216-
* printed as sent, since a humanized block id can no longer be copied.
217-
*/
218-
function keyedByData(record: object): boolean {
219-
const values = Object.values(record)
220-
return (
221-
values.length > 0 &&
222-
values.every((value) => value !== null && typeof value === 'object' && !Array.isArray(value))
223-
)
224-
}
225-
226-
/** A record's fields, each labelled by its humanized field name, or as sent when keyed by data. */
227-
function inferredFields(record: object): Array<[string, string]> {
228-
const literal = keyedByData(record)
213+
/** A record's fields, each labelled by its humanized field name, or as sent when its keys are data. */
214+
function inferredFields(record: object, dataKeys: boolean): Array<[string, string]> {
229215
return Object.entries(record).map<[string, string]>(([key, value]) => [
230-
literal ? key : inferHeader(key, inferFormat(key, value)),
216+
dataKeys ? key : inferHeader(key, inferFormat(key, value)),
231217
inferredCell(key, value),
232218
])
233219
}
@@ -478,7 +464,7 @@ export function renderResult(
478464
const fields = spec.fields
479465
? fieldsFrom(data, spec.fields, options)
480466
: data && typeof data === 'object'
481-
? inferredFields(data)
467+
? inferredFields(data, spec.dataKeys === true)
482468
: []
483469

484470
printRecord(format, fields, data)

‎scripts/check-sim-cli-bundle.ts‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,16 @@ async function watchedPackages(): Promise<Set<string>> {
8383
return new Set(paths.flatMap((entry) => WATCHED_PACKAGE.exec(String(entry))?.[1] ?? []))
8484
}
8585

86-
function sorted(values: Iterable<string>): string[] {
87-
return [...values].sort(compareStrings)
86+
/** One message per name in `names` that `known` lacks, in name order. */
87+
function missingFrom(
88+
names: ReadonlySet<string>,
89+
known: ReadonlySet<string>,
90+
message: (name: string) => string
91+
): string[] {
92+
return [...names]
93+
.filter((name) => !known.has(name))
94+
.sort(compareStrings)
95+
.map(message)
8896
}
8997

9098
async function main(): Promise<void> {
@@ -95,30 +103,30 @@ async function main(): Promise<void> {
95103
input.includes('node_modules/') ? [] : (WORKSPACE_PACKAGE.exec(input)?.[1] ?? [])
96104
)
97105
)
98-
const noticed = await noticedPackages()
99-
const watched = await watchedPackages()
106+
const [noticed, watched] = await Promise.all([noticedPackages(), watchedPackages()])
100107

101108
const problems = [
102-
...sorted(npmPackages)
103-
.filter((name) => !noticed.has(name))
104-
.map((name) => `${name} is bundled but has no notice in ${LICENSES_FILE}.`),
105-
...sorted(noticed)
106-
.filter((name) => !npmPackages.has(name))
107-
.map(
108-
(name) => `${LICENSES_FILE} has a notice for ${name}, which the bundle no longer holds.`
109-
),
110-
...sorted(workspacePackages)
111-
.filter((name) => !watched.has(name))
112-
.map(
113-
(name) =>
114-
`packages/${name} is bundled but ${PUBLISH_WORKFLOW} does not watch 'packages/${name}/**', so a change there never republishes the CLI.`
115-
),
116-
...sorted(watched)
117-
.filter((name) => !workspacePackages.has(name))
118-
.map(
119-
(name) =>
120-
`${PUBLISH_WORKFLOW} watches 'packages/${name}/**', which the bundle does not hold.`
121-
),
109+
...missingFrom(
110+
npmPackages,
111+
noticed,
112+
(name) => `${name} is bundled but has no notice in ${LICENSES_FILE}.`
113+
),
114+
...missingFrom(
115+
noticed,
116+
npmPackages,
117+
(name) => `${LICENSES_FILE} has a notice for ${name}, which the bundle no longer holds.`
118+
),
119+
...missingFrom(
120+
workspacePackages,
121+
watched,
122+
(name) =>
123+
`packages/${name} is bundled but ${PUBLISH_WORKFLOW} does not watch 'packages/${name}/**', so a change there never republishes the CLI.`
124+
),
125+
...missingFrom(
126+
watched,
127+
workspacePackages,
128+
(name) => `${PUBLISH_WORKFLOW} watches 'packages/${name}/**', which the bundle does not hold.`
129+
),
122130
]
123131

124132
if (problems.length > 0) {

0 commit comments

Comments
 (0)