Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further endo daemon CLI support for dot-delimited paths #2325

Merged
merged 1 commit into from
Jun 24, 2024
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
3 changes: 2 additions & 1 deletion packages/cli/src/commands/adopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';

export const adoptCommand = async ({
messageNumberText,
Expand All @@ -12,5 +13,5 @@ export const adoptCommand = async ({
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
// TODO less bad number parsing.
const messageNumber = Number(messageNumberText);
await E(agent).adopt(messageNumber, edgeName, name);
await E(agent).adopt(messageNumber, edgeName, parsePetNamePath(name));
});
2 changes: 1 addition & 1 deletion packages/cli/src/commands/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const evalCommand = async ({
source,
codeNames,
petNames,
resultName,
parsePetNamePath(resultName),
);
console.log(result);
});
5 changes: 4 additions & 1 deletion packages/cli/src/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { makeReaderRef } from '@endo/daemon';
import bundleSource from '@endo/bundle-source';

import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';
import { randomHex16 } from '../random.js';

const textEncoder = new TextEncoder();
Expand All @@ -25,6 +26,7 @@ export const install = async ({
let bundleReaderRef;
/** @type {string | undefined} */
let temporaryBundleName;
await null;
if (programPath !== undefined) {
if (bundleName === undefined) {
// TODO alternately, make a temporary session-scoped GC pet store
Expand All @@ -41,6 +43,7 @@ export const install = async ({

await withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
// Prepare a bundle, with the given name.
await null;
if (bundleReaderRef !== undefined) {
await E(agent).storeBlob(bundleReaderRef, bundleName);
}
Expand All @@ -53,7 +56,7 @@ export const install = async ({
)}, $id, $cancelled)`,
['apps', 'bundle', 'powers'],
['APPS', bundleName, powersName],
webletName,
parsePetNamePath(webletName),
);
const webletLocation = await E(weblet).getLocation();
process.stdout.write(`${webletLocation}\n`);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { parsePetNamePath } from '../pet-name.js';

export const mkdir = async ({ agentNames, directoryPath }) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
await E(agent).makeDirectory(...parsePetNamePath(directoryPath));
await E(agent).makeDirectory(parsePetNamePath(directoryPath));
});
9 changes: 7 additions & 2 deletions packages/cli/src/commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';

export const remove = async ({ petNames, agentNames }) =>
export const remove = async ({ petNamePaths, agentNames }) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) =>
Promise.all(petNames.map(petName => E(agent).remove(petName))),
Promise.all(
petNamePaths.map(petNamePath =>
E(agent).remove(...parsePetNamePath(petNamePath)),
),
),
);
7 changes: 6 additions & 1 deletion packages/cli/src/commands/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';

export const request = async ({
description,
Expand All @@ -10,7 +11,11 @@ export const request = async ({
agentNames,
}) => {
await withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
const result = await E(agent).request(toName, description, resultName);
const result = await E(agent).request(
toName,
description,
parsePetNamePath(resultName),
);
console.log(result);
});
};
9 changes: 7 additions & 2 deletions packages/cli/src/commands/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parsePetNamePath } from '../pet-name.js';

export const spawn = async ({ petNames, agentNames }) =>
export const spawn = async ({ petNamePaths, agentNames }) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) =>
Promise.all(petNames.map(petName => E(agent).provideWorker(petName))),
Promise.all(
petNamePaths.map(petNamePath =>
E(agent).provideWorker(parsePetNamePath(petNamePath)),
),
),
);
11 changes: 6 additions & 5 deletions packages/cli/src/endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const main = async rawArgs => {
UNCONFINED: importPath,
name: resultName,
bundle: bundleName,
worker: workerName = 'NEW',
worker: workerName = undefined,
as: agentNames,
powers: powersName = 'NONE',
} = cmd.opts();
Expand Down Expand Up @@ -291,10 +291,10 @@ export const main = async rawArgs => {
.command('remove [names...]')
.description('forget a named value')
.option(...commonOptions.as)
.action(async (petNames, cmd) => {
.action(async (petNamePaths, cmd) => {
const { as: agentNames } = cmd.opts();
const { remove } = await import('./commands/remove.js');
return remove({ petNames, agentNames });
return remove({ petNamePaths, agentNames });
});

program
Expand Down Expand Up @@ -405,10 +405,10 @@ export const main = async rawArgs => {
.command('spawn [names...]')
.description('creates a worker')
.option(...commonOptions.as)
.action(async (petNames, cmd) => {
.action(async (petNamePaths, cmd) => {
const { as: agentNames } = cmd.opts();
const { spawn } = await import('./commands/spawn.js');
return spawn({ petNames, agentNames });
return spawn({ petNamePaths, agentNames });
});

program
Expand Down Expand Up @@ -591,6 +591,7 @@ export const main = async rawArgs => {
.description('erases persistent state and stops if running')
.action(async cmd => {
const { force } = cmd.opts();
await null;
const doPurge =
force ||
/^y(es)?$/u.test(
Expand Down
19 changes: 17 additions & 2 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ const makeDaemonCore = async (
context.thisDiesIfThatDies(hubId);

const hub = provide(hubId, 'hub');
return E(hub).lookup(...path);
return E(hub).lookup(path);
};

/**
Expand Down Expand Up @@ -708,6 +708,7 @@ const makeDaemonCore = async (
* @param {Context} context
*/
const evaluateFormula = async (id, formulaNumber, formula, context) => {
await null;
if (Object.hasOwn(makers, formula.type)) {
const make = makers[formula.type];
const value = await /** @type {unknown} */ (
Expand Down Expand Up @@ -839,6 +840,7 @@ const makeDaemonCore = async (
const formulateReadableBlob = async (readerRef, deferredTasks) => {
const { formulaNumber, contentSha512 } = await formulaGraphJobs.enqueue(
async () => {
await null;
const values = {
formulaNumber: await randomHex512(),
contentSha512: await contentStore.store(makeRefReader(readerRef)),
Expand Down Expand Up @@ -988,6 +990,7 @@ const makeDaemonCore = async (
* @type {DaemonCore['formulateWorker']}
*/
const formulateWorker = async deferredTasks => {
await null;
return formulateNumberedWorker(
await formulaGraphJobs.enqueue(async () => {
const formulaNumber = await randomHex512();
Expand All @@ -1011,6 +1014,7 @@ const makeDaemonCore = async (
const { specifiedWorkerId, ...remainingSpecifiedIdentifiers } =
specifiedIdentifiers;

await null;
const storeId = (await formulateNumberedPetStore(await randomHex512())).id;

const hostFormulaNumber = await randomHex512();
Expand Down Expand Up @@ -1064,6 +1068,7 @@ const makeDaemonCore = async (
deferredTasks,
specifiedWorkerId,
) => {
await null;
return formulateNumberedHost(
await formulaGraphJobs.enqueue(async () => {
const identifiers = await formulateHostDependencies({
Expand Down Expand Up @@ -1123,6 +1128,7 @@ const makeDaemonCore = async (

/** @type {DaemonCore['formulateGuest']} */
const formulateGuest = async (hostAgentId, hostHandleId, deferredTasks) => {
await null;
return formulateNumberedGuest(
await formulaGraphJobs.enqueue(async () => {
const identifiers = await formulateGuestDependencies(
Expand Down Expand Up @@ -1212,6 +1218,7 @@ const makeDaemonCore = async (
if (typeof formulaIdOrPath === 'string') {
return formulaIdOrPath;
}
await null;
return (
/* eslint-disable no-use-before-define */
(
Expand Down Expand Up @@ -1657,11 +1664,19 @@ const makeDaemonCore = async (
const petStore = await provide(petStoreId, 'pet-store');

/**
* @param {string} petName - The pet name to inspect.
* @param {string|string[]} petName - The pet name to inspect.
* @returns {Promise<KnownEndoInspectors[string]>} An
* inspector for the value of the given pet name.
*/
const lookup = async petName => {
if (Array.isArray(petName)) {
if (petName.length !== 1) {
throw Error(
'PetStoreInspector.lookup(path) requires path length of 1',
);
}
petName = petName[0];
}
const id = petStore.identifyLocal(petName);
if (id === undefined) {
throw new Error(`Unknown pet name ${petName}`);
Expand Down
18 changes: 13 additions & 5 deletions packages/daemon/src/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ export const makeDirectoryMaker = ({
/** @type {MakeDirectoryNode} */
const makeDirectoryNode = petStore => {
/** @type {EndoDirectory['lookup']} */
const lookup = (...petNamePath) => {
const lookup = petNamePath => {
if (typeof petNamePath === 'string') {
petNamePath = [petNamePath];
}
const [headName, ...tailNames] = petNamePath;

const id = petStore.identifyLocal(headName);
if (id === undefined) {
throw new TypeError(`Unknown pet name: ${q(headName)}`);
Expand All @@ -41,6 +45,7 @@ export const makeDirectoryMaker = ({

/** @type {EndoDirectory['reverseLookup']} */
const reverseLookup = async presence => {
await null;
const id = getIdForRef(await presence);
if (id === undefined) {
return harden([]);
Expand All @@ -62,7 +67,7 @@ export const makeDirectoryMaker = ({
// eslint-disable-next-line no-use-before-define
return { hub: directory, name: tailName };
}
const nameHub = /** @type {NameHub} */ (await lookup(...headPath));
const nameHub = /** @type {NameHub} */ (await lookup(headPath));
return { hub: nameHub, name: tailName };
};

Expand Down Expand Up @@ -126,12 +131,13 @@ export const makeDirectoryMaker = ({
if (petNamePath.length === 0) {
return petStore.list();
}
const hub = /** @type {NameHub} */ (await lookup(...petNamePath));
const hub = /** @type {NameHub} */ (await lookup(petNamePath));
return hub.list();
};

/** @type {EndoDirectory['listIdentifiers']} */
const listIdentifiers = async (...petNamePath) => {
petNamePath = petNamePath || [];
const names = await list(...petNamePath);
const identities = new Set();
await Promise.all(
Expand All @@ -153,12 +159,13 @@ export const makeDirectoryMaker = ({
yield* petStore.followNameChanges();
return;
}
const hub = /** @type {NameHub} */ (await lookup(...petNamePath));
const hub = /** @type {NameHub} */ (await lookup(petNamePath));
yield* hub.followNameChanges();
};

/** @type {EndoDirectory['remove']} */
const remove = async (...petNamePath) => {
await null;
if (petNamePath.length === 1) {
const petName = petNamePath[0];
await petStore.remove(petName);
Expand Down Expand Up @@ -211,6 +218,7 @@ export const makeDirectoryMaker = ({
if (typeof petNamePath === 'string') {
petNamePath = [petNamePath];
}
await null;
if (petNamePath.length === 1) {
const petName = petNamePath[0];
await petStore.write(petName, id);
Expand All @@ -221,7 +229,7 @@ export const makeDirectoryMaker = ({
};

/** @type {EndoDirectory['makeDirectory']} */
const makeDirectory = async (...directoryPetNamePath) => {
const makeDirectory = async directoryPetNamePath => {
const { value: directory, id } = await formulateDirectory();
await write(directoryPetNamePath, id);
return directory;
Expand Down
5 changes: 3 additions & 2 deletions packages/daemon/src/guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export const makeGuestMaker = ({ provide, makeMailbox, makeDirectoryNode }) => {
HOST: hostHandleId,
});

const directory = makeDirectoryNode(specialStore);
const mailbox = makeMailbox({
petStore: specialStore,
directory,
selfId: handleId,
context,
});
const { petStore, handle } = mailbox;
const directory = makeDirectoryNode(petStore);
const { handle } = mailbox;

const { reverseIdentify } = specialStore;
const {
Expand Down
Loading
Loading