@akanjs/devkit 3.3.0-rc.8 → 3.3.0-rc.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEV_RUNTIME_KNOBS.md +1 -1
- package/README.ko.md +7 -1
- package/README.md +7 -1
- package/akanApp/akanApp.host.test.ts +3 -1
- package/akanApp/akanApp.host.ts +26 -2
- package/akanApp/akanApp.host.wiring.test.ts +111 -0
- package/akanApp/devHostPolicy.ts +5 -0
- package/akanApp/devMetadataRestart.ts +93 -0
- package/akanApp/devRecycler.test.ts +2 -0
- package/akanApp/devRecycler.ts +9 -0
- package/akanConfig/akanBin.ts +4 -0
- package/akanConfig/akanConfig.test.ts +307 -18
- package/akanConfig/akanConfig.ts +104 -210
- package/akanConfig/akanPreview.test.ts +28 -0
- package/akanConfig/akanPreview.ts +24 -0
- package/akanConfig/akanWorkers.test.ts +73 -0
- package/akanConfig/akanWorkers.ts +61 -0
- package/akanConfig/desktopServerConfig.ts +96 -0
- package/akanConfig/dockerImage.ts +233 -0
- package/akanConfig/index.ts +3 -0
- package/akanConfig/nativeFileSettings.ts +33 -0
- package/akanConfig/types.ts +4 -0
- package/app/appBin.test.ts +63 -0
- package/app/appBin.ts +50 -0
- package/app/applicationBuildReporter.test.ts +51 -1
- package/app/applicationBuildReporter.ts +6 -1
- package/app/applicationBuildRunner.test.ts +436 -5
- package/app/applicationBuildRunner.ts +237 -37
- package/app/clientServerBoundary.test.ts +115 -0
- package/app/clientServerBoundary.ts +89 -0
- package/app/dictionarySizes.test.ts +40 -0
- package/app/dictionarySizes.ts +95 -0
- package/app/routeFilesReport.test.ts +59 -0
- package/app/routeFilesReport.ts +46 -0
- package/app/routeJsBudget.ts +18 -4
- package/artifact/implicitRootLayout.test.ts +19 -0
- package/artifact/implicitRootLayout.ts +16 -2
- package/artifact/implicitRootLayoutRender.test.tsx +7 -2
- package/artifact/storeBootstrap.test.ts +28 -0
- package/artifact/storeBootstrap.ts +64 -8
- package/biome.base.json +7 -5
- package/codeAgent/index.ts +1 -34
- package/codeAgent/skills/akan-agent/SKILL.md +2 -2
- package/codeAgent/skills/akan-debug/SKILL.md +11 -5
- package/codeAgent/skills/akan-deps/SKILL.md +10 -3
- package/codeAgent/skills/akan-err/SKILL.md +13 -8
- package/codeAgent/skills/akan-mcp/SKILL.md +1 -1
- package/codeAgent/skills/akan-module/SKILL.md +2 -2
- package/codeAgent/skills/akan-page/SKILL.md +65 -30
- package/codeAgent/skills/akan-query/SKILL.md +13 -20
- package/codeAgent/skills/akan-runtime/SKILL.md +21 -1
- package/codeAgent/skills/akan-service/SKILL.md +21 -20
- package/codeAgent/skills/akan-transport/SKILL.md +5 -14
- package/codeAgent/skills/akan-ui/SKILL.md +16 -21
- package/codeAgent/skills/akan-validate/SKILL.md +8 -2
- package/commandDecorators/command.ts +28 -12
- package/commandDecorators/commandBuilder.ts +8 -1
- package/commandDecorators/commandDecorators.test.ts +70 -3
- package/commandDecorators/helpFormatter.ts +14 -12
- package/context/akanContext.ts +2 -0
- package/csrE2e/devHmrLatency.e2e.test.ts +1 -1
- package/csrE2e/ssrRegistry.e2e.test.ts +108 -22
- package/docs/apiReference.test.ts +25 -0
- package/docs/apiReference.ts +38 -5
- package/docs/declarationReader.ts +30 -0
- package/executors/appExecutor.ts +26 -12
- package/executors/clientRuntimeKeyOrder.fixture.json +682 -0
- package/executors/clientRuntimeKeys.fixture.json +393 -0
- package/executors/clientRuntimeKeys.test.ts +1690 -0
- package/executors/executor.ts +14 -6
- package/executors/executors.test.ts +155 -1
- package/executors/pkgExecutor.ts +2 -1
- package/executors/sysExecutor.ts +112 -8
- package/executors/workspaceExecutor.ts +7 -4
- package/frontendBuild/allRoutesBuilder.ts +99 -7
- package/frontendBuild/autoImportSync.ts +3 -2
- package/frontendBuild/buildRouteClient.test.ts +684 -6
- package/frontendBuild/clientBuildTypes.ts +26 -0
- package/frontendBuild/clientEntriesBundler.test.ts +79 -0
- package/frontendBuild/clientEntriesBundler.ts +70 -1
- package/frontendBuild/clientEntryDiscovery.test.ts +308 -1
- package/frontendBuild/clientEntryDiscovery.ts +134 -12
- package/frontendBuild/clientEntryGroups.test.ts +77 -0
- package/frontendBuild/clientEntryGroups.ts +61 -0
- package/frontendBuild/csrDevArtifactWriter.ts +2 -2
- package/frontendBuild/csrDevModuleCompiler.test.ts +86 -0
- package/frontendBuild/csrDevModuleCompiler.ts +44 -8
- package/frontendBuild/csrDevPatcher.test.ts +4 -5
- package/frontendBuild/csrDevPatcher.ts +37 -11
- package/frontendBuild/csrDevTypes.ts +9 -0
- package/frontendBuild/cssCandidateCache.test.ts +12 -0
- package/frontendBuild/cssCandidateCache.ts +5 -3
- package/frontendBuild/cssCompiler.test.ts +14 -0
- package/frontendBuild/devBarrelImports.test.ts +124 -0
- package/frontendBuild/devBarrelImports.ts +81 -21
- package/frontendBuild/devChangePlanner.ts +0 -1
- package/frontendBuild/devRegistryBundler.ts +4 -2
- package/frontendBuild/devRouteRuntime.test.ts +492 -0
- package/frontendBuild/devRouteRuntime.ts +167 -0
- package/frontendBuild/dictionaryAssetBuilder.test.ts +408 -0
- package/frontendBuild/dictionaryAssetBuilder.ts +185 -17
- package/frontendBuild/dictionaryConstants.ts +136 -0
- package/frontendBuild/dictionaryOwnParts.ts +88 -0
- package/frontendBuild/dictionaryPackState.ts +131 -0
- package/frontendBuild/dictionaryPacking.test.ts +207 -0
- package/frontendBuild/dictionaryPacking.ts +160 -0
- package/frontendBuild/dictionaryRoutes.test.ts +251 -0
- package/frontendBuild/dictionaryRoutes.ts +227 -0
- package/frontendBuild/frontendBuild.test.ts +5 -3
- package/frontendBuild/pagesBundleBuilder.test.ts +84 -0
- package/frontendBuild/pagesBundleBuilder.ts +78 -2
- package/frontendBuild/precompressArtifacts.test.ts +22 -0
- package/frontendBuild/precompressArtifacts.ts +34 -8
- package/frontendBuild/registryMetadataFingerprint.ts +3 -3
- package/frontendBuild/renderedEntries.ts +144 -0
- package/frontendBuild/routeClientBuilder.ts +407 -43
- package/frontendBuild/routeHandoffs.test.ts +155 -0
- package/frontendBuild/routeHandoffs.ts +117 -0
- package/frontendBuild/routeModulePreloads.test.ts +32 -0
- package/frontendBuild/routeModulePreloads.ts +20 -11
- package/frontendBuild/routeRuntimeCheck.test.ts +280 -0
- package/frontendBuild/routeRuntimeCheck.ts +248 -0
- package/frontendBuild/routeRuntimePays.test.ts +143 -0
- package/frontendBuild/routeRuntimePays.ts +117 -0
- package/frontendBuild/ssrBaseArtifactBuilder.ts +30 -35
- package/frontendBuild/ssrDevBundler.test.ts +150 -1
- package/frontendBuild/ssrDevBundler.ts +66 -11
- package/frontendBuild/vendorFileImports.test.ts +254 -0
- package/frontendBuild/vendorFileImports.ts +163 -0
- package/frontendBuild/vendorNarrowing.test.ts +47 -3
- package/frontendBuild/vendorNarrowing.ts +38 -2
- package/frontendBuild/vendorSpecifiers.ts +2 -0
- package/frontendBuild/workerChunks.ts +61 -0
- package/guideline/agentAssets.ts +1 -1
- package/incrementalBuilder/buildBatch.proc.ts +27 -1
- package/incrementalBuilder/buildBatchProtocol.ts +1 -0
- package/incrementalBuilder/builderWatch.ts +11 -1
- package/incrementalBuilder/incrementalBuilder.proc.test.ts +159 -0
- package/incrementalBuilder/incrementalBuilder.proc.ts +31 -3
- package/incrementalBuilder/pendingMetadata.test.ts +27 -0
- package/incrementalBuilder/pendingMetadata.ts +19 -0
- package/integration/devResourceProbe.ts +14 -0
- package/integration/devStability.integration.test.ts +46 -21
- package/integration/renderAdmissionSoak.ts +6 -0
- package/integration/ssrMemoryProbe.ts +5 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/bad.tsx +11 -0
- package/lint/__fixtures__/no-arbitrary-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/bad.tsx +34 -0
- package/lint/__fixtures__/no-daisyui-legacy-class/every-class/good.tsx +2 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/bad.tsx +10 -0
- package/lint/__fixtures__/no-inline-color/every-color-function/good.tsx +2 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/bad.tsx +22 -0
- package/lint/__fixtures__/no-raw-palette-class/every-palette/good.tsx +2 -0
- package/lint/biomeBase.test.ts +50 -3
- package/lint/biomeBase.ts +1 -1
- package/lint/biomeCheck.test.ts +162 -0
- package/lint/biomeCheck.ts +131 -0
- package/lint/linter.test.ts +16 -11
- package/lint/no-arbitrary-color.grit +5 -0
- package/lint/no-conditional-st-hook.grit +5 -1
- package/lint/no-daisyui-legacy-class.grit +8 -0
- package/lint/no-document-cookie.grit +3 -0
- package/lint/no-inline-color.grit +10 -2
- package/lint/no-oversized-list-limit.grit +2 -0
- package/lint/no-raw-palette-class.grit +7 -0
- package/lint/no-unpublished-form-setter.grit +2 -0
- package/lint/no-web-only-api-outside-webkit.grit +5 -0
- package/lint/no-web-storage.grit +3 -0
- package/lint/withheldEnvFile.ts +17 -0
- package/mobile/desktopBin.ts +2 -2
- package/mobile/desktopServerStage.test.ts +18 -3
- package/mobile/desktopServerStage.ts +7 -2
- package/mobile/nativeApp.test.ts +13 -5
- package/mobile/nativeApp.ts +9 -4
- package/mobile/nativeConfig.test.ts +17 -1
- package/mobile/nativeConfig.ts +11 -6
- package/mobile/nativePublicEnv.ts +1 -1
- package/mobile/serverBundleSecrets.ts +43 -0
- package/package.json +11 -12
- package/packageExports.test.ts +3 -2
- package/preview/PreviewView.ts +43 -6
- package/preview/previewView.test.ts +31 -0
- package/quality/dictionaryKeys.test.ts +356 -0
- package/quality/dictionaryKeys.ts +488 -0
- package/quality/objectImportReport.test.ts +36 -0
- package/quality/objectImportReport.ts +105 -0
- package/quality/qualityScanner.ts +1 -1
- package/quality/routeRuntimeReport.test.ts +91 -0
- package/quality/routeRuntimeReport.ts +121 -0
- package/quality/runtimeConstantFacts.ts +71 -0
- package/quality/runtimeConstantGraph.ts +123 -0
- package/quality/runtimeConstantReads.ts +116 -0
- package/quality/runtimeImports.test.ts +190 -0
- package/quality/runtimeImports.ts +233 -0
- package/quality/runtimeKeyNames.ts +46 -0
- package/quality/runtimeLexicalScope.ts +132 -0
- package/quality/runtimeMarkerReach.test.ts +84 -0
- package/quality/runtimeMarkerReach.ts +255 -0
- package/quality/runtimeModuleFacts.ts +290 -0
- package/quality/runtimeNameTable.test.ts +172 -0
- package/quality/runtimeNameTable.ts +477 -0
- package/quality/runtimeScopeReport.test.ts +123 -0
- package/quality/runtimeScopeReport.ts +167 -0
- package/quality/runtimeSliceKey.ts +269 -0
- package/quality/runtimeSyntax.ts +133 -0
- package/quality/runtimeTable.fixture.ts +189 -0
- package/quality/runtimeUse.test.ts +737 -0
- package/quality/runtimeUse.ts +696 -0
- package/quality/runtimeUseReadable.test.ts +393 -0
- package/quality/ssrScanner.test.ts +13 -0
- package/quality/ssrScanner.ts +1 -1
- package/quality/translatorBindings.ts +187 -0
- package/test/fakeTerminal.ts +45 -0
- package/test/secretEnvFixture.ts +134 -0
- package/test/testHelpers.ts +11 -0
- package/transforms/barrelAnalyzer.ts +24 -2
- package/transforms/barrelImportsPlugin.ts +121 -32
- package/transforms/exportReach.test.ts +162 -0
- package/transforms/exportReach.ts +308 -0
- package/transforms/frameworkObjects.fixture.tsx +58 -0
- package/transforms/frameworkObjects.test.ts +54 -0
- package/transforms/modelReference.test.ts +196 -0
- package/transforms/modelReference.ts +39 -0
- package/transforms/moduleParts.test.ts +225 -0
- package/transforms/moduleParts.ts +418 -0
- package/transforms/objectImportEdits.ts +150 -0
- package/transforms/objectMemberImports.test.ts +633 -0
- package/transforms/objectMemberImports.ts +571 -0
- package/transforms/objectMemberLookup.ts +150 -0
- package/transforms/objectModule.test.ts +307 -0
- package/transforms/objectModule.ts +482 -0
- package/transforms/objectModuleAnalyzer.ts +104 -0
- package/transforms/partImports.test.ts +76 -0
- package/transforms/partImports.ts +110 -0
- package/transforms/partModules.test.ts +187 -0
- package/transforms/partModules.ts +90 -0
- package/transforms/partPurity.ts +124 -0
- package/transforms/reactIconImports.test.ts +231 -0
- package/transforms/reactIconImports.ts +325 -0
- package/transforms/routeConstants.ts +122 -0
- package/transforms/routeRuntime.test.ts +835 -0
- package/transforms/routeRuntime.ts +703 -0
- package/transforms/routeRuntimeEdits.ts +92 -0
- package/transforms/routeRuntimeGraph.ts +66 -0
- package/transforms/routeRuntimePlan.ts +244 -0
- package/transforms/secretEnvDeclaration.test.ts +211 -0
- package/transforms/secretEnvDeclaration.ts +219 -0
- package/transforms/secretEnvFile.test.ts +117 -0
- package/transforms/secretEnvFile.ts +113 -0
- package/transforms/secretEnvName.test.ts +54 -0
- package/transforms/secretEnvName.ts +29 -0
- package/transforms/secretEnvSource.test.ts +152 -0
- package/transforms/secretEnvSource.ts +197 -0
- package/transforms/serverEnvEvaluationError.ts +11 -0
- package/transforms/serverEnvPlugin.test.ts +155 -1
- package/transforms/serverEnvPlugin.ts +123 -19
- package/transforms/serverEnvProfile.test.ts +129 -2
- package/transforms/serverEnvProfile.ts +133 -43
- package/transforms/serverEnvSecrets.test.ts +268 -0
- package/transforms/signalDescriptors.test.ts +280 -0
- package/transforms/signalDescriptors.ts +227 -0
- package/transforms/thisFreeExports.ts +87 -0
- package/transforms/transforms.test.ts +177 -0
- package/transforms/tsconfigPackageResolver.test.ts +1 -0
- package/tunnel/createTunnel.ts +5 -1
- package/tunnel/ssh2Module.test.ts +37 -0
- package/tunnel/ssh2Module.ts +28 -0
- package/tunnel/sshTunnel.ts +4 -2
- package/typecheck/projectTypecheck.test.ts +32 -1
- package/typecheck/projectTypecheck.ts +3 -3
- package/typecheck/typeChecker.test.ts +49 -0
- package/typecheck/typeChecker.ts +9 -7
- package/typecheck/withheldDiagnostics.ts +41 -0
- package/util/devInstallCommand.ts +7 -0
- package/util/jsonFormatter.test.ts +13 -9
- package/workflow/executor.ts +6 -2
- package/workflow/index.ts +1 -0
- package/workflow/plan.ts +36 -7
- package/workflow/types.ts +1 -1
- package/workflow/uiTarget.ts +50 -0
- package/workspace/envSeed.test.ts +30 -1
- package/workspace/envSeed.ts +30 -1
- package/workspace/mainCheckout.test.ts +55 -0
- package/workspace/mainCheckout.ts +38 -0
- package/workspace/scanInfo.test.ts +39 -0
- package/workspace/scanInfo.ts +55 -6
- package/workspace/slicePlanner.ts +2 -1
- package/workspace/workspaceLayout.test.ts +22 -1
- package/workspace/workspaceLayout.ts +3 -3
- package/CHANGELOG.md +0 -2903
- package/codeAgent/agent/AkanCodeCompaction.ts +0 -49
- package/codeAgent/agent/AkanCodeServices.ts +0 -122
- package/codeAgent/agent/AkanContextFiles.ts +0 -94
- package/codeAgent/agent/CodeAgent.ts +0 -626
- package/codeAgent/agent/CodeAgentAllowed.ts +0 -35
- package/codeAgent/agent/CodeAgentAsks.ts +0 -66
- package/codeAgent/agent/CodeAgentCommandLine.ts +0 -372
- package/codeAgent/agent/CodeAgentEventMapper.ts +0 -225
- package/codeAgent/agent/CodeAgentGate.ts +0 -258
- package/codeAgent/agent/CodeAgentProxy.ts +0 -83
- package/codeAgent/agent/CodeAgentRpcHost.ts +0 -109
- package/codeAgent/agent/CodeAgentRpcListener.ts +0 -195
- package/codeAgent/agent/CodeAgentStreamPrinter.ts +0 -86
- package/codeAgent/agent/CodeAgentSuspended.ts +0 -85
- package/codeAgent/agent/CodeAgentUi.ts +0 -47
- package/codeAgent/agent/CodeMailbox.ts +0 -148
- package/codeAgent/agent/CodeSessionFork.ts +0 -56
- package/codeAgent/agent/CodeSessionIndex.ts +0 -78
- package/codeAgent/agent/SubagentPool.ts +0 -219
- package/codeAgent/agent/WorkspaceTrust.ts +0 -200
- package/codeAgent/agent/akanCodeCompaction.test.ts +0 -36
- package/codeAgent/agent/akanCodeModel.ts +0 -47
- package/codeAgent/agent/akanCodeServices.test.ts +0 -54
- package/codeAgent/agent/akanContextFiles.test.ts +0 -116
- package/codeAgent/agent/akanSystemPrompt.ts +0 -50
- package/codeAgent/agent/codeAgent.test.ts +0 -337
- package/codeAgent/agent/codeAgentCommandLine.test.ts +0 -212
- package/codeAgent/agent/codeAgentContract.test.ts +0 -755
- package/codeAgent/agent/codeAgentHosts.ts +0 -30
- package/codeAgent/agent/codeAgentProxy.test.ts +0 -126
- package/codeAgent/agent/codeAgentRpcListener.test.ts +0 -215
- package/codeAgent/agent/codeAgentSuspended.test.ts +0 -47
- package/codeAgent/agent/codeMailbox.test.ts +0 -76
- package/codeAgent/agent/codeSessionFork.test.ts +0 -58
- package/codeAgent/agent/codeSessionIndex.test.ts +0 -77
- package/codeAgent/agent/consoleToStderr.ts +0 -8
- package/codeAgent/agent/jsonLines.ts +0 -29
- package/codeAgent/agent/stringArg.ts +0 -5
- package/codeAgent/feedback/DevLogFeedback.ts +0 -82
- package/codeAgent/feedback/PreviewFeedback.ts +0 -46
- package/codeAgent/feedback/TurnFeedback.ts +0 -98
- package/codeAgent/feedback/VerifyFeedback.ts +0 -39
- package/codeAgent/tools/AkanCli.ts +0 -51
- package/codeAgent/tools/AkanCodePlugins.ts +0 -142
- package/codeAgent/tools/AkanEditScope.ts +0 -53
- package/codeAgent/tools/AkanEnvKeys.ts +0 -50
- package/codeAgent/tools/AkanToolPack.ts +0 -86
- package/codeAgent/tools/AkanVerifier.ts +0 -30
- package/codeAgent/tools/AskToolPack.ts +0 -99
- package/codeAgent/tools/DeepSeekWeb.ts +0 -127
- package/codeAgent/tools/MailToolPack.ts +0 -76
- package/codeAgent/tools/McpClient.ts +0 -209
- package/codeAgent/tools/McpOAuth.ts +0 -233
- package/codeAgent/tools/McpOAuthLoopback.ts +0 -64
- package/codeAgent/tools/McpServerConfig.ts +0 -131
- package/codeAgent/tools/McpSignIn.ts +0 -158
- package/codeAgent/tools/McpTokenStore.ts +0 -128
- package/codeAgent/tools/McpToolPack.ts +0 -206
- package/codeAgent/tools/SessionToolPack.ts +0 -91
- package/codeAgent/tools/WebToolPack.ts +0 -315
- package/codeAgent/tools/akanCodePlugins.test.ts +0 -954
- package/codeAgent/tools/askToolPack.test.ts +0 -116
- package/codeAgent/tools/deepSeekWeb.test.ts +0 -113
- package/codeAgent/tools/mcpOAuth.test.ts +0 -648
- package/codeAgent/tools/mcpServerConfig.test.ts +0 -341
- package/piDependencyPins.test.ts +0 -45
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import type { ExtensionAPI, InlineExtension } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { type CodeAgentProfile, type CodeAgentSubagent, codeAgentReadOnlyBuiltins } from "akanjs/common";
|
|
3
|
-
import { Type } from "typebox";
|
|
4
|
-
import type { Workspace } from "../../commandDecorators";
|
|
5
|
-
import type { CodeAgentModelRef } from "./akanCodeModel";
|
|
6
|
-
|
|
7
|
-
export interface SubagentPoolOptions {
|
|
8
|
-
workspace: Workspace;
|
|
9
|
-
cwd: string;
|
|
10
|
-
parent: CodeAgentProfile;
|
|
11
|
-
/** The parent's model, read when a task starts: a child runs on the model the parent runs on now. */
|
|
12
|
-
model?: () => CodeAgentModelRef | undefined;
|
|
13
|
-
depth: number;
|
|
14
|
-
/** What the tree above this pool has spent and has running; a root pool starts its own. */
|
|
15
|
-
spend?: SubagentSpend;
|
|
16
|
-
onNotice?: (message: string) => void;
|
|
17
|
-
/** The running children, whole, for a host that draws them as a rail beside the prompt. */
|
|
18
|
-
onAgents?: (agents: CodeAgentSubagent[]) => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** One object shared down a sub-agent tree, so a single token budget and a single concurrency limit bound it. */
|
|
22
|
-
export interface SubagentSpend {
|
|
23
|
-
tokens: number;
|
|
24
|
-
running: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const maxResultChars = 8_000;
|
|
28
|
-
|
|
29
|
-
// Children are re-reported every second: their token counts climb between start and finish.
|
|
30
|
-
const tickMs = 1_000;
|
|
31
|
-
|
|
32
|
-
export type SubagentKind = keyof typeof SubagentPool.kinds;
|
|
33
|
-
|
|
34
|
-
// Only a child's answer crosses back, never its transcript. Depth, concurrency and token ceilings are checked
|
|
35
|
-
// before a child exists, because an unbounded recursive `task` is a fork bomb that bills per token.
|
|
36
|
-
export class SubagentPool {
|
|
37
|
-
static readonly kinds = {
|
|
38
|
-
explore: {
|
|
39
|
-
readOnly: true,
|
|
40
|
-
desc: "reads, searches and reports back. It cannot write a file or run a command.",
|
|
41
|
-
},
|
|
42
|
-
code: {
|
|
43
|
-
readOnly: false,
|
|
44
|
-
desc: "makes the change itself, with the same tools as this session.",
|
|
45
|
-
},
|
|
46
|
-
} as const;
|
|
47
|
-
|
|
48
|
-
// A writer works unwatched on the parent's checkout, so it has to be asked for by name, never defaulted.
|
|
49
|
-
static readonly defaultKind: SubagentKind = "explore";
|
|
50
|
-
|
|
51
|
-
readonly #options: SubagentPoolOptions;
|
|
52
|
-
readonly #children = new Map<
|
|
53
|
-
string,
|
|
54
|
-
{ kind: SubagentKind; description: string; startedAt: number; agent?: { tokensUsed: number } }
|
|
55
|
-
>();
|
|
56
|
-
readonly #spend: SubagentSpend;
|
|
57
|
-
#ticker: ReturnType<typeof setInterval> | null = null;
|
|
58
|
-
|
|
59
|
-
constructor(options: SubagentPoolOptions) {
|
|
60
|
-
this.#options = options;
|
|
61
|
-
this.#spend = options.spend ?? { tokens: 0, running: 0 };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
static extensionFor(options: SubagentPoolOptions): InlineExtension | undefined {
|
|
65
|
-
if (!options.parent.tools.subagent) return undefined;
|
|
66
|
-
if (options.depth >= options.parent.tools.subagent.maxDepth) return undefined;
|
|
67
|
-
const pool = new SubagentPool(options);
|
|
68
|
-
return { name: "akan-subagent", factory: (pi: ExtensionAPI) => pool.#register(pi) };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
static readonly toolName = "task";
|
|
72
|
-
|
|
73
|
-
#register(pi: ExtensionAPI) {
|
|
74
|
-
pi.registerTool({
|
|
75
|
-
name: SubagentPool.toolName,
|
|
76
|
-
label: "Task",
|
|
77
|
-
description:
|
|
78
|
-
"Run a focused sub-agent on one self-contained question and get back its answer. Use it for searching and reading that would otherwise fill this conversation — the sub-agent's own reading does not come back, only its answer. Give it everything it needs: it cannot see this conversation.",
|
|
79
|
-
promptSnippet: "task: run a sub-agent on one self-contained question and get back only its answer",
|
|
80
|
-
parameters: Type.Object({
|
|
81
|
-
description: Type.String({ description: "Three to five words naming the task, for the activity log." }),
|
|
82
|
-
prompt: Type.String({
|
|
83
|
-
description: "The complete instruction. The sub-agent sees nothing of this conversation.",
|
|
84
|
-
}),
|
|
85
|
-
type: Type.Optional(
|
|
86
|
-
Type.Union(
|
|
87
|
-
Object.entries(SubagentPool.kinds).map(([kind, spec]) => Type.Literal(kind, { description: spec.desc })),
|
|
88
|
-
{ description: `What the sub-agent may do. Defaults to ${SubagentPool.defaultKind}.` },
|
|
89
|
-
),
|
|
90
|
-
),
|
|
91
|
-
}),
|
|
92
|
-
execute: async (id, params, signal) => {
|
|
93
|
-
const kind = (params.type ?? SubagentPool.defaultKind) as SubagentKind;
|
|
94
|
-
const refusal = this.#refuse();
|
|
95
|
-
if (refusal) return { content: [{ type: "text", text: refusal }], details: undefined, isError: true };
|
|
96
|
-
this.#spend.running += 1;
|
|
97
|
-
this.#children.set(id, { kind, description: params.description, startedAt: Date.now() });
|
|
98
|
-
this.#report();
|
|
99
|
-
// Announced at start: a turn quiet for a minute while a child reads looks like one that stopped responding.
|
|
100
|
-
this.#options.onNotice?.(`${kind} sub-agent · ${params.description}`);
|
|
101
|
-
try {
|
|
102
|
-
const text = await this.#run(id, kind, params.prompt, signal);
|
|
103
|
-
return { content: [{ type: "text", text }], details: undefined };
|
|
104
|
-
} finally {
|
|
105
|
-
this.#spend.running -= 1;
|
|
106
|
-
this.#children.delete(id);
|
|
107
|
-
this.#report();
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
#refuse() {
|
|
114
|
-
const budget = this.#options.parent.tools.subagent;
|
|
115
|
-
if (!budget) return "Sub-agents are disabled by this profile.";
|
|
116
|
-
// Refused, never queued: every ancestor keeps its slot while it waits on its child, so a queued child deadlocks.
|
|
117
|
-
if (this.#spend.running >= budget.maxConcurrent)
|
|
118
|
-
return `Too many sub-agents are already running (limit ${budget.maxConcurrent} at once, counted across the whole task tree). ${this.#children.size ? "Wait for one of yours to finish, or do" : "Do"} this part yourself.`;
|
|
119
|
-
if (this.#spend.tokens >= budget.budget)
|
|
120
|
-
return `The sub-agent token budget (${budget.budget}) is spent. Do the rest of this work yourself.`;
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** The parent profile narrowed, never widened: no session file, no `AGENTS.md`, nobody to ask; skills stay. */
|
|
125
|
-
static childOf(parent: CodeAgentProfile, kind: SubagentKind): CodeAgentProfile {
|
|
126
|
-
const budget = parent.tools.subagent;
|
|
127
|
-
return {
|
|
128
|
-
...parent,
|
|
129
|
-
name: `${parent.name}:${kind}`,
|
|
130
|
-
tools: {
|
|
131
|
-
...parent.tools,
|
|
132
|
-
// Filtered, not replaced: a child of a read-only session must never gain a write tool.
|
|
133
|
-
builtin: SubagentPool.kinds[kind].readOnly
|
|
134
|
-
? parent.tools.builtin.filter((tool) => codeAgentReadOnlyBuiltins.includes(tool))
|
|
135
|
-
: parent.tools.builtin,
|
|
136
|
-
subagent: budget ? { ...budget, maxDepth: budget.maxDepth } : false,
|
|
137
|
-
},
|
|
138
|
-
session: { store: "memory", crossSession: false },
|
|
139
|
-
ui: { canPrompt: false },
|
|
140
|
-
context: { projectFiles: false, skills: parent.context.skills },
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// One ticker per pool, unref'd: a child left registered is a bug that must not hold the process open.
|
|
145
|
-
#report() {
|
|
146
|
-
const onAgents = this.#options.onAgents;
|
|
147
|
-
if (!onAgents) return;
|
|
148
|
-
onAgents(
|
|
149
|
-
[...this.#children].map(([id, child]) => ({
|
|
150
|
-
id,
|
|
151
|
-
kind: child.kind,
|
|
152
|
-
description: child.description,
|
|
153
|
-
startedAt: child.startedAt,
|
|
154
|
-
tokens: child.agent?.tokensUsed ?? 0,
|
|
155
|
-
})),
|
|
156
|
-
);
|
|
157
|
-
if (this.#children.size && !this.#ticker) {
|
|
158
|
-
this.#ticker = setInterval(() => this.#report(), tickMs);
|
|
159
|
-
this.#ticker.unref?.();
|
|
160
|
-
} else if (!this.#children.size && this.#ticker) {
|
|
161
|
-
clearInterval(this.#ticker);
|
|
162
|
-
this.#ticker = null;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async #run(id: string, kind: SubagentKind, prompt: string, signal: AbortSignal | undefined) {
|
|
167
|
-
const { CodeAgent } = await import("./CodeAgent");
|
|
168
|
-
const budget = this.#options.parent.tools.subagent;
|
|
169
|
-
const child = SubagentPool.childOf(this.#options.parent, kind);
|
|
170
|
-
const model = this.#options.model?.();
|
|
171
|
-
const agent = await CodeAgent.create({
|
|
172
|
-
workspace: this.#options.workspace,
|
|
173
|
-
cwd: this.#options.cwd,
|
|
174
|
-
profile: child,
|
|
175
|
-
...(model ? { model } : {}),
|
|
176
|
-
apps: [],
|
|
177
|
-
depth: this.#options.depth + 1,
|
|
178
|
-
subagentSpend: this.#spend,
|
|
179
|
-
});
|
|
180
|
-
const running = this.#children.get(id);
|
|
181
|
-
if (running) running.agent = agent;
|
|
182
|
-
let stopped = false;
|
|
183
|
-
const abort = () => {
|
|
184
|
-
stopped = true;
|
|
185
|
-
void agent.abort();
|
|
186
|
-
};
|
|
187
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
188
|
-
// A listener added to an already-aborted signal never fires: this child was stopped while it was being created.
|
|
189
|
-
if (signal?.aborted) stopped = true;
|
|
190
|
-
let finished = false;
|
|
191
|
-
try {
|
|
192
|
-
let answer = "";
|
|
193
|
-
agent.on((event) => {
|
|
194
|
-
if (event.type === "message" && event.role === "assistant") answer = event.text;
|
|
195
|
-
// The engine drops an abort that lands before its run is live, so a stopped child is stopped again there.
|
|
196
|
-
if (event.type === "turn_start" && stopped) void agent.abort();
|
|
197
|
-
});
|
|
198
|
-
if (!stopped) {
|
|
199
|
-
await agent.prompt(prompt);
|
|
200
|
-
await agent.waitForIdle();
|
|
201
|
-
}
|
|
202
|
-
finished = true;
|
|
203
|
-
return answer.length > maxResultChars
|
|
204
|
-
? `${answer.slice(0, maxResultChars)}\n…(truncated)`
|
|
205
|
-
: answer || "(no answer)";
|
|
206
|
-
} finally {
|
|
207
|
-
signal?.removeEventListener("abort", abort);
|
|
208
|
-
// Whatever the outcome: a failed or stopped child was billed too, and its own children are counted by its pool.
|
|
209
|
-
const tokens = agent.tokensUsed;
|
|
210
|
-
this.#spend.tokens += tokens;
|
|
211
|
-
const outcome = stopped ? "stopped" : finished ? "finished" : "failed";
|
|
212
|
-
const ceiling = budget ? budget.budget : 0;
|
|
213
|
-
this.#options.onNotice?.(
|
|
214
|
-
`${kind} sub-agent ${outcome}, ${tokens} tokens (${this.#spend.tokens} of ${ceiling} spent)`,
|
|
215
|
-
);
|
|
216
|
-
agent.dispose();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
chmodSync,
|
|
4
|
-
existsSync,
|
|
5
|
-
lstatSync,
|
|
6
|
-
readdirSync,
|
|
7
|
-
readFileSync,
|
|
8
|
-
realpathSync,
|
|
9
|
-
renameSync,
|
|
10
|
-
statSync,
|
|
11
|
-
writeFileSync,
|
|
12
|
-
} from "node:fs";
|
|
13
|
-
import path from "node:path";
|
|
14
|
-
import { McpServerConfig } from "../tools/McpServerConfig";
|
|
15
|
-
import { akanCodePaths } from "./akanCodePaths";
|
|
16
|
-
|
|
17
|
-
export type WorkspaceTrustKind = "mcp" | "extensions";
|
|
18
|
-
|
|
19
|
-
export interface WorkspaceTrustItem {
|
|
20
|
-
kind: WorkspaceTrustKind;
|
|
21
|
-
lines: string[];
|
|
22
|
-
hash: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type TrustRecord = Partial<Record<WorkspaceTrustKind, string>>;
|
|
26
|
-
|
|
27
|
-
interface ExtensionWalk {
|
|
28
|
-
root: string;
|
|
29
|
-
files: { name: string; path: string }[];
|
|
30
|
-
bytes: number;
|
|
31
|
-
seen: Set<string>;
|
|
32
|
-
unchecked?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const maxExtensionFiles = 10_000;
|
|
36
|
-
const maxExtensionBytes = 64 * 1024 * 1024;
|
|
37
|
-
|
|
38
|
-
// A repo's mcp.json starts processes and its extensions/ load as code, so each waits for a yes to its exact content.
|
|
39
|
-
export class WorkspaceTrust {
|
|
40
|
-
static readonly #granted = new Map<string, string>();
|
|
41
|
-
|
|
42
|
-
static items(workspaceRoot: string, kinds: WorkspaceTrustKind[] = ["mcp", "extensions"]) {
|
|
43
|
-
return kinds
|
|
44
|
-
.map((kind) => WorkspaceTrust.#itemOf(workspaceRoot, kind))
|
|
45
|
-
.filter((item): item is WorkspaceTrustItem => !!item);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
static pending(workspaceRoot: string, kinds?: WorkspaceTrustKind[]) {
|
|
49
|
-
return WorkspaceTrust.items(workspaceRoot, kinds).filter((item) => !WorkspaceTrust.#approved(workspaceRoot, item));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static isTrusted(workspaceRoot: string, kind: WorkspaceTrustKind) {
|
|
53
|
-
const item = WorkspaceTrust.#itemOf(workspaceRoot, kind);
|
|
54
|
-
return !item || WorkspaceTrust.#approved(workspaceRoot, item);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
static approve(workspaceRoot: string, items: WorkspaceTrustItem[], remember: boolean) {
|
|
58
|
-
const key = WorkspaceTrust.#keyOf(workspaceRoot);
|
|
59
|
-
for (const item of items) WorkspaceTrust.#granted.set(`${key}\n${item.kind}`, item.hash);
|
|
60
|
-
const checked = items.filter((item) => item.hash);
|
|
61
|
-
if (!remember || !checked.length) return;
|
|
62
|
-
const all = WorkspaceTrust.#read();
|
|
63
|
-
const record: TrustRecord = { ...all[key] };
|
|
64
|
-
for (const item of checked) record[item.kind] = item.hash;
|
|
65
|
-
WorkspaceTrust.#write({ ...all, [key]: record });
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static carry<T>(workspaceRoot: string, kind: WorkspaceTrustKind, edit: () => T): T {
|
|
69
|
-
const before = WorkspaceTrust.#itemOf(workspaceRoot, kind);
|
|
70
|
-
const trusted = !before || WorkspaceTrust.#approved(workspaceRoot, before);
|
|
71
|
-
const remembered = !before || WorkspaceTrust.#read()[WorkspaceTrust.#keyOf(workspaceRoot)]?.[kind] === before.hash;
|
|
72
|
-
const result = edit();
|
|
73
|
-
const after = WorkspaceTrust.#itemOf(workspaceRoot, kind);
|
|
74
|
-
if (trusted && after) WorkspaceTrust.approve(workspaceRoot, [after], remembered);
|
|
75
|
-
return result;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static #approved(workspaceRoot: string, item: WorkspaceTrustItem) {
|
|
79
|
-
const key = WorkspaceTrust.#keyOf(workspaceRoot);
|
|
80
|
-
return (
|
|
81
|
-
WorkspaceTrust.#granted.get(`${key}\n${item.kind}`) === item.hash ||
|
|
82
|
-
(!!item.hash && WorkspaceTrust.#read()[key]?.[item.kind] === item.hash)
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
static #itemOf(workspaceRoot: string, kind: WorkspaceTrustKind): WorkspaceTrustItem | undefined {
|
|
87
|
-
return kind === "mcp" ? WorkspaceTrust.#mcp(workspaceRoot) : WorkspaceTrust.#extensions(workspaceRoot);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
static #mcp(workspaceRoot: string): WorkspaceTrustItem | undefined {
|
|
91
|
-
const servers = McpServerConfig.read(workspaceRoot, ["workspace"]).sort((a, b) => a.name.localeCompare(b.name));
|
|
92
|
-
if (!servers.length) return undefined;
|
|
93
|
-
return {
|
|
94
|
-
kind: "mcp",
|
|
95
|
-
lines: servers.map((ref) => `${ref.name}: ${McpServerConfig.targetOf(ref)}`),
|
|
96
|
-
hash: WorkspaceTrust.#digest(WorkspaceTrust.#canonical(servers)),
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
//? An item without a hash could not be checked: the trust file never keeps it, so it is asked about at every start.
|
|
101
|
-
static #extensions(workspaceRoot: string): WorkspaceTrustItem | undefined {
|
|
102
|
-
const dir = akanCodePaths.extensionsDir(workspaceRoot);
|
|
103
|
-
let root: string;
|
|
104
|
-
try {
|
|
105
|
-
root = realpathSync(dir);
|
|
106
|
-
} catch {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
const walk: ExtensionWalk = { root, files: [], bytes: 0, seen: new Set() };
|
|
110
|
-
WorkspaceTrust.#walk(walk, dir, "");
|
|
111
|
-
const lines = walk.files.map((file) => file.name);
|
|
112
|
-
if (walk.unchecked) return { kind: "extensions", lines: [walk.unchecked, ...lines], hash: "" };
|
|
113
|
-
if (!walk.files.length) return undefined;
|
|
114
|
-
const hash = createHash("sha256");
|
|
115
|
-
for (const file of walk.files) hash.update(`${file.name}\0${WorkspaceTrust.#digest(readFileSync(file.path))}\n`);
|
|
116
|
-
return { kind: "extensions", lines, hash: hash.digest("hex") };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Links are followed while they stay in the folder, since the engine follows them too.
|
|
120
|
-
static #walk(walk: ExtensionWalk, dir: string, prefix: string) {
|
|
121
|
-
let entries: string[];
|
|
122
|
-
try {
|
|
123
|
-
const real = realpathSync(dir);
|
|
124
|
-
if (walk.seen.has(real)) return;
|
|
125
|
-
walk.seen.add(real);
|
|
126
|
-
entries = readdirSync(dir)
|
|
127
|
-
.filter((entry) => entry !== ".git")
|
|
128
|
-
.sort();
|
|
129
|
-
} catch {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
for (const entry of entries) {
|
|
133
|
-
if (walk.unchecked) return;
|
|
134
|
-
const full = path.join(dir, entry);
|
|
135
|
-
const name = prefix ? `${prefix}/${entry}` : entry;
|
|
136
|
-
try {
|
|
137
|
-
if (lstatSync(full).isSymbolicLink() && !WorkspaceTrust.#inside(walk.root, realpathSync(full))) {
|
|
138
|
-
walk.unchecked = `${name} links out of the folder: it cannot be checked, so it is asked about at every start`;
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const stat = statSync(full);
|
|
142
|
-
if (stat.isDirectory()) WorkspaceTrust.#walk(walk, full, name);
|
|
143
|
-
else if (stat.isFile()) {
|
|
144
|
-
walk.files.push({ name, path: full });
|
|
145
|
-
walk.bytes += stat.size;
|
|
146
|
-
if (walk.files.length > maxExtensionFiles || walk.bytes > maxExtensionBytes)
|
|
147
|
-
walk.unchecked = `more than ${maxExtensionFiles} files or ${maxExtensionBytes >> 20} MiB: too many to check, so asked about at every start`;
|
|
148
|
-
}
|
|
149
|
-
} catch {}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
static #inside(root: string, real: string) {
|
|
154
|
-
const relative = path.relative(root, real);
|
|
155
|
-
return relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
static #keyOf(workspaceRoot: string) {
|
|
159
|
-
try {
|
|
160
|
-
return realpathSync(workspaceRoot);
|
|
161
|
-
} catch {
|
|
162
|
-
return path.resolve(workspaceRoot);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
static #digest(content: string | Buffer) {
|
|
167
|
-
return createHash("sha256").update(content).digest("hex");
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
static #canonical(value: unknown): string {
|
|
171
|
-
if (Array.isArray(value)) return `[${value.map((entry) => WorkspaceTrust.#canonical(entry)).join(",")}]`;
|
|
172
|
-
if (value && typeof value === "object")
|
|
173
|
-
return `{${Object.entries(value)
|
|
174
|
-
.filter(([, entry]) => entry !== undefined)
|
|
175
|
-
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
176
|
-
.map(([key, entry]) => `${JSON.stringify(key)}:${WorkspaceTrust.#canonical(entry)}`)
|
|
177
|
-
.join(",")}}`;
|
|
178
|
-
return JSON.stringify(value);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
static #read(): Record<string, TrustRecord> {
|
|
182
|
-
const file = akanCodePaths.trustFile();
|
|
183
|
-
if (!existsSync(file)) return {};
|
|
184
|
-
try {
|
|
185
|
-
const parsed = JSON.parse(readFileSync(file, "utf8")) as { workspaces?: Record<string, TrustRecord> };
|
|
186
|
-
return parsed.workspaces ?? {};
|
|
187
|
-
} catch {
|
|
188
|
-
return {};
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
static #write(workspaces: Record<string, TrustRecord>) {
|
|
193
|
-
akanCodePaths.ensureGlobalDir();
|
|
194
|
-
const file = akanCodePaths.trustFile();
|
|
195
|
-
const temp = `${file}.${process.pid}.tmp`;
|
|
196
|
-
writeFileSync(temp, `${JSON.stringify({ workspaces }, null, 2)}\n`, { mode: 0o600 });
|
|
197
|
-
renameSync(temp, file);
|
|
198
|
-
chmodSync(file, 0o600);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { AkanCodeCompaction } from "./AkanCodeCompaction";
|
|
3
|
-
import { AkanCodeServices } from "./AkanCodeServices";
|
|
4
|
-
|
|
5
|
-
const big = { provider: "deepseek", id: "deepseek-flash", contextWindow: 1_000_000 };
|
|
6
|
-
const small = { provider: "openai", id: "small", contextWindow: 128_000 };
|
|
7
|
-
|
|
8
|
-
describe("AkanCodeCompaction", () => {
|
|
9
|
-
test("a percent is a share of the window, never closer to the edge than the reply's floor", () => {
|
|
10
|
-
expect(AkanCodeCompaction.thresholdOf(1_000_000, 60)).toBe(600_000);
|
|
11
|
-
expect(AkanCodeCompaction.thresholdOf(128_000, 90)).toBe(128_000 - AkanCodeCompaction.reserveFloor);
|
|
12
|
-
expect(AkanCodeCompaction.thresholdOf(1_000_000, undefined)).toBe(1_000_000 - AkanCodeCompaction.reserveFloor);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("only steps of 10 are taken, and 100 is the default rather than a choice", () => {
|
|
16
|
-
expect(AkanCodeCompaction.normalize(60)).toBe(60);
|
|
17
|
-
expect(AkanCodeCompaction.normalize(100)).toBeUndefined();
|
|
18
|
-
expect(AkanCodeCompaction.normalize(undefined)).toBeUndefined();
|
|
19
|
-
for (const wrong of [0, 55, 110, Number.NaN]) expect(() => AkanCodeCompaction.normalize(wrong)).toThrow();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("the engine reads the share back as this model's reserve and leaves other models at the floor", () => {
|
|
23
|
-
const settings = AkanCodeServices.settings();
|
|
24
|
-
AkanCodeCompaction.apply(settings, big, 60);
|
|
25
|
-
expect(settings.getCompactionSettings(big).reserveTokens).toBe(400_000);
|
|
26
|
-
expect(settings.getCompactionSettings(small).reserveTokens).toBe(AkanCodeCompaction.reserveFloor);
|
|
27
|
-
AkanCodeCompaction.apply(settings, big, undefined);
|
|
28
|
-
expect(settings.getCompactionSettings(big).reserveTokens).toBe(AkanCodeCompaction.reserveFloor);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("a share under the turns every compaction keeps is refused, since each turn would compact again", () => {
|
|
32
|
-
expect(AkanCodeCompaction.problemOf(big, 10, 20_000)).toBeUndefined();
|
|
33
|
-
expect(AkanCodeCompaction.problemOf(small, 10, 20_000)).toContain("every turn would compact again");
|
|
34
|
-
expect(AkanCodeCompaction.problemOf({ ...small, contextWindow: 0 }, 50, 20_000)).toContain("no context window");
|
|
35
|
-
});
|
|
36
|
-
});
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
// The engine's `Model` type (from `pi-ai`) is not re-exported, so it is recovered from `getModel`.
|
|
4
|
-
export type CodeAgentModel = NonNullable<ReturnType<ModelRuntime["getModel"]>>;
|
|
5
|
-
|
|
6
|
-
export interface CodeAgentModelRef {
|
|
7
|
-
provider: string;
|
|
8
|
-
id: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/** DeepSeek's million-token window is what makes the always-loaded `AGENTS.md` affordable on every turn. */
|
|
12
|
-
export const akanCodeDefaultModel: CodeAgentModelRef = { provider: "deepseek", id: "deepseek-flash" };
|
|
13
|
-
|
|
14
|
-
// Declared text-only by the catalogue, yet verified against the provider to see images.
|
|
15
|
-
// `deepseek-v4-pro` is deliberately absent: it accepts an image and then says it cannot view it.
|
|
16
|
-
const catalogueMissesImages = new Set(["deepseek/deepseek-v4-flash"]);
|
|
17
|
-
|
|
18
|
-
const correctInputs = (model: CodeAgentModel | undefined) => {
|
|
19
|
-
if (!model || model.input.includes("image")) return model;
|
|
20
|
-
if (!catalogueMissesImages.has(`${model.provider}/${model.id}`)) return model;
|
|
21
|
-
return { ...model, input: [...model.input, "image" as const] };
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// An empty API key yields a successful empty response rather than an error, so unauthorized providers are skipped.
|
|
25
|
-
export const akanCodeModel = async (runtime: ModelRuntime, ref?: CodeAgentModelRef) => {
|
|
26
|
-
if (ref) {
|
|
27
|
-
const requested = runtime.getModel(ref.provider, ref.id);
|
|
28
|
-
if (!requested) throw new Error(`Unknown model: ${ref.provider}/${ref.id}`);
|
|
29
|
-
if (!runtime.hasConfiguredAuth(ref.provider))
|
|
30
|
-
throw new Error(`No API key configured for ${ref.provider}. Set ${ref.provider.toUpperCase()}_API_KEY.`);
|
|
31
|
-
return correctInputs(requested);
|
|
32
|
-
}
|
|
33
|
-
const fallback = runtime.getModel(akanCodeDefaultModel.provider, akanCodeDefaultModel.id);
|
|
34
|
-
if (fallback && runtime.hasConfiguredAuth(fallback.provider)) return correctInputs(fallback);
|
|
35
|
-
return correctInputs((await runtime.getAvailable())[0]);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const akanCodeModelSupportsImages = (model: CodeAgentModel | undefined) => !!model?.input.includes("image");
|
|
39
|
-
|
|
40
|
-
// Deliberately the only check: `maxTokens >= contextWindow` or `reserveTokens > maxTokens` flag 1 model in 7.
|
|
41
|
-
export const akanCodeModelWarnings = (model: CodeAgentModel | undefined, compactionFloor: number) => {
|
|
42
|
-
if (!model) return [];
|
|
43
|
-
if (model.contextWindow >= compactionFloor) return [];
|
|
44
|
-
return [
|
|
45
|
-
`${model.provider}/${model.id} declares a ${model.contextWindow}-token context window, below the ${compactionFloor} the compaction policy needs, so every turn will compact immediately. Check the model entry in ~/.akan/code/models.json.`,
|
|
46
|
-
];
|
|
47
|
-
};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import { AkanCodeServices } from "./AkanCodeServices";
|
|
7
|
-
import { akanCodePaths } from "./akanCodePaths";
|
|
8
|
-
|
|
9
|
-
const saved: Record<string, string | undefined> = {};
|
|
10
|
-
let home = "";
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
home = mkdtempSync(path.join(tmpdir(), "akan-code-services-test-"));
|
|
14
|
-
for (const key of ["AKAN_CODE_HOME", "ANTHROPIC_API_KEY"]) saved[key] = process.env[key];
|
|
15
|
-
process.env.AKAN_CODE_HOME = home;
|
|
16
|
-
process.env.ANTHROPIC_API_KEY = "test-key-never-sent";
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
rmSync(home, { recursive: true, force: true });
|
|
21
|
-
for (const [key, value] of Object.entries(saved)) {
|
|
22
|
-
if (value === undefined) delete process.env[key];
|
|
23
|
-
else process.env[key] = value;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
const threadsOf = async (runtime: ModelRuntime) => {
|
|
28
|
-
const model = runtime.getModels("anthropic")[0];
|
|
29
|
-
if (!model) throw new Error("pi lists no anthropic model");
|
|
30
|
-
expect(model.baseUrl).toContain("api.anthropic.com");
|
|
31
|
-
const prepare = (runtime as unknown as { prepareRequest: (model: unknown, options: object) => Promise<unknown> })
|
|
32
|
-
.prepareRequest;
|
|
33
|
-
const prepared = (await prepare.call(runtime, model, {})) as { model: { compat?: Record<string, unknown> } };
|
|
34
|
-
return prepared.model.compat?.supportsMessageThreads;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
describe("AkanCodeServices holds the defaults akan code had on pi 1.3.0", () => {
|
|
38
|
-
test("prompt caches are warmed only while a turn streams, never in the idle time between turns", () => {
|
|
39
|
-
expect(AkanCodeServices.settings().getCacheWarmingMode()).toBe("streaming");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("an Anthropic request carries the whole conversation instead of continuing a server-side thread", async () => {
|
|
43
|
-
expect(await threadsOf(await AkanCodeServices.runtime(home))).toBe(false);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test("models.json can still turn Anthropic message threads on", async () => {
|
|
47
|
-
mkdirSync(path.dirname(akanCodePaths.modelsFile()), { recursive: true });
|
|
48
|
-
writeFileSync(
|
|
49
|
-
akanCodePaths.modelsFile(),
|
|
50
|
-
JSON.stringify({ providers: { anthropic: { compat: { supportsMessageThreads: true } } } }),
|
|
51
|
-
);
|
|
52
|
-
expect(await threadsOf(await AkanCodeServices.runtime(home))).toBe(true);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { type AkanContextFile, AkanContextFiles } from "./AkanContextFiles";
|
|
6
|
-
|
|
7
|
-
const roots: string[] = [];
|
|
8
|
-
|
|
9
|
-
const workspace = (files: Record<string, string>) => {
|
|
10
|
-
const root = mkdtempSync(path.join(tmpdir(), "akan-context-"));
|
|
11
|
-
roots.push(root);
|
|
12
|
-
for (const [name, content] of Object.entries(files)) {
|
|
13
|
-
const file = path.join(root, name);
|
|
14
|
-
mkdirSync(path.dirname(file), { recursive: true });
|
|
15
|
-
writeFileSync(file, content);
|
|
16
|
-
}
|
|
17
|
-
return root;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const loaded = (root: string, name = "AGENTS.md"): AkanContextFile[] => [
|
|
21
|
-
{ path: path.join(root, name), content: "the workspace guide" },
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
const namesOf = (files: AkanContextFile[], root: string) =>
|
|
25
|
-
files.map((file) => path.relative(root, file.path).split(path.sep).join("/"));
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe("workspace context files", () => {
|
|
32
|
-
test("a CLAUDE.md beside the AGENTS.md the engine took is loaded too", () => {
|
|
33
|
-
const root = workspace({ "AGENTS.md": "the workspace guide", "CLAUDE.md": "comments override" });
|
|
34
|
-
const files = AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root });
|
|
35
|
-
expect(namesOf(files, root)).toEqual(["AGENTS.md", "CLAUDE.md"]);
|
|
36
|
-
expect(files[1]?.content).toBe("comments override");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("an AGENTS.override.md keeps the CLAUDE.md it was written to displace out", () => {
|
|
40
|
-
const root = workspace({ "AGENTS.override.md": "instead of that", "CLAUDE.md": "the old one" });
|
|
41
|
-
const files = AkanContextFiles.extend(loaded(root, "AGENTS.override.md"), { workspaceRoot: root, cwd: root });
|
|
42
|
-
expect(namesOf(files, root)).toEqual(["AGENTS.override.md"]);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test("the same guide under two names is loaded once", () => {
|
|
46
|
-
const root = workspace({ "AGENTS.md": "the workspace guide" });
|
|
47
|
-
symlinkSync(path.join(root, "AGENTS.md"), path.join(root, "CLAUDE.md"));
|
|
48
|
-
expect(namesOf(AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root }), root)).toEqual([
|
|
49
|
-
"AGENTS.md",
|
|
50
|
-
]);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("an always-on cursor rule is loaded without its frontmatter", () => {
|
|
54
|
-
const root = workspace({
|
|
55
|
-
".cursor/rules/house.mdc": "---\ndescription: house style\nalwaysApply: true\n---\nnever use any\n",
|
|
56
|
-
});
|
|
57
|
-
const files = AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root });
|
|
58
|
-
expect(namesOf(files, root)).toEqual(["AGENTS.md", ".cursor/rules/house.mdc"]);
|
|
59
|
-
expect(files[1]?.content).toBe("never use any\n");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("a scoped cursor rule is left to cursor", () => {
|
|
63
|
-
const root = workspace({
|
|
64
|
-
".cursor/rules/ui.mdc": "---\nglobs: ui/**/*.tsx\n---\nno inline colours\n",
|
|
65
|
-
".cursor/rules/ask.mdc": "---\ndescription: for migrations\n---\nread the plan\n",
|
|
66
|
-
".cursor/rules/off.mdc": "---\nalwaysApply: false\n---\nnot this one\n",
|
|
67
|
-
});
|
|
68
|
-
expect(namesOf(AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root }), root)).toEqual([
|
|
69
|
-
"AGENTS.md",
|
|
70
|
-
]);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test("a rule with no frontmatter at all is unconditional, and so is .cursorrules", () => {
|
|
74
|
-
const root = workspace({ ".cursor/rules/plain.mdc": "just do it", ".cursorrules": "the legacy file" });
|
|
75
|
-
const files = AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root });
|
|
76
|
-
expect(namesOf(files, root).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))).toEqual(
|
|
77
|
-
[".cursorrules", ".cursor/rules/plain.mdc", "AGENTS.md"].sort(),
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test("rules nested under the rules directory are found, in a stable order", () => {
|
|
82
|
-
const root = workspace({
|
|
83
|
-
".cursor/rules/b.mdc": "---\nalwaysApply: true\n---\nsecond\n",
|
|
84
|
-
".cursor/rules/a.mdc": "---\nalwaysApply: true\n---\nfirst\n",
|
|
85
|
-
".cursor/rules/deep/c.mdc": "---\nalwaysApply: true\n---\nthird\n",
|
|
86
|
-
});
|
|
87
|
-
const files = AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root });
|
|
88
|
-
expect(namesOf(files, root)).toEqual([
|
|
89
|
-
"AGENTS.md",
|
|
90
|
-
".cursor/rules/a.mdc",
|
|
91
|
-
".cursor/rules/b.mdc",
|
|
92
|
-
".cursor/rules/deep/c.mdc",
|
|
93
|
-
]);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test("the session's own directory carries its rules as well as the workspace root", () => {
|
|
97
|
-
const root = workspace({
|
|
98
|
-
".cursor/rules/root.mdc": "---\nalwaysApply: true\n---\nworkspace wide\n",
|
|
99
|
-
"apps/akan/.cursor/rules/app.mdc": "---\nalwaysApply: true\n---\napp only\n",
|
|
100
|
-
});
|
|
101
|
-
const cwd = path.join(root, "apps", "akan");
|
|
102
|
-
const files = AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd });
|
|
103
|
-
expect(namesOf(files, root)).toEqual(["AGENTS.md", ".cursor/rules/root.mdc", "apps/akan/.cursor/rules/app.mdc"]);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("an empty rule file adds nothing", () => {
|
|
107
|
-
const root = workspace({ ".cursor/rules/empty.mdc": "---\nalwaysApply: true\n---\n\n" });
|
|
108
|
-
expect(AkanContextFiles.extend(loaded(root), { workspaceRoot: root, cwd: root })).toHaveLength(1);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test("nothing to add leaves the list exactly as the engine built it", () => {
|
|
112
|
-
const root = workspace({ "AGENTS.md": "the workspace guide" });
|
|
113
|
-
const base = loaded(root);
|
|
114
|
-
expect(AkanContextFiles.extend(base, { workspaceRoot: root, cwd: root })).toEqual(base);
|
|
115
|
-
});
|
|
116
|
-
});
|